]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/cp/name-lookup.c
tree-vect-loop-manip.c (rename_variables_in_bb): Rename PHI nodes when copying loop...
[thirdparty/gcc.git] / gcc / cp / name-lookup.c
CommitLineData
aed81407 1/* Definitions for C++ name lookup routines.
cbe34bb5 2 Copyright (C) 2003-2017 Free Software Foundation, Inc.
aed81407
GDR
3 Contributed by Gabriel Dos Reis <gdr@integrable-solutions.net>
4
ed3cf953 5This file is part of GCC.
aed81407 6
ed3cf953 7GCC is free software; you can redistribute it and/or modify
aed81407 8it under the terms of the GNU General Public License as published by
e77f031d 9the Free Software Foundation; either version 3, or (at your option)
aed81407
GDR
10any later version.
11
ed3cf953 12GCC is distributed in the hope that it will be useful,
aed81407
GDR
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
e77f031d
NC
18along with GCC; see the file COPYING3. If not see
19<http://www.gnu.org/licenses/>. */
aed81407
GDR
20
21#include "config.h"
22#include "system.h"
23#include "coretypes.h"
2adfab87
AM
24#include "cp-tree.h"
25#include "timevar.h"
d8a2d370
DN
26#include "stringpool.h"
27#include "print-tree.h"
28#include "attribs.h"
6097b0c3 29#include "debug.h"
39dabefd 30#include "c-family/c-pragma.h"
501c95ff 31#include "params.h"
52ed68f7
DM
32#include "gcc-rich-location.h"
33#include "spellcheck-tree.h"
34#include "parser.h"
00e8de68 35
b655c310
NS
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);
15f8ac7f 40
3c9cca88
NS
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
3d7ff728
NS
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)
3c9cca88
NS
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
3a9cc685
NS
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
aa7bda5f
NS
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
3c9cca88
NS
86static tree *
87find_namespace_slot (tree ns, tree name, bool create_p = false)
aa7bda5f 88{
e833f686
NS
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);
98c28dd4 92 return slot;
3c9cca88
NS
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;
aa7bda5f
NS
101}
102
b655c310 103/* Add DECL to the list of things declared in B. */
5880f14f 104
b655c310 105static void
9d029ddf 106add_decl_to_level (cp_binding_level *b, tree decl)
5880f14f 107{
5256a7f5 108 gcc_assert (b->kind != sk_class);
b655c310 109
c957e9c0
NS
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);
b655c310 132}
daafa301 133
59a4ede9
NS
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
f35a733d 154struct name_lookup
b655c310 155{
932f48ac
NS
156public:
157 typedef std::pair<tree, tree> using_pair;
158 typedef vec<using_pair, va_heap, vl_embed> using_queue;
159
f35a733d
NS
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. */
9dda0ace 164 int flags; /* Lookup flags. */
3d7ff728
NS
165 bool deduping; /* Full deduping is needed because using declarations
166 are in play. */
f35a733d
NS
167 vec<tree, va_heap, vl_embed> *scopes;
168 name_lookup *previous; /* Previously active lookup. */
f35a733d
NS
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:
9dda0ace
NS
177 name_lookup (tree n, int f = 0)
178 : name (n), value (NULL_TREE), type (NULL_TREE), flags (f),
3d7ff728 179 deduping (false), scopes (NULL), previous (NULL)
f35a733d
NS
180 {
181 preserve_state ();
182 }
183 ~name_lookup ()
184 {
f35a733d
NS
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
9dda0ace
NS
221private:
222 static tree ambiguous (tree thing, tree current);
3d7ff728 223 void add_overload (tree fns);
9dda0ace
NS
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
932f48ac
NS
237private:
238 using_queue *queue_namespace (using_queue *queue, int depth, tree scope);
3c9feefc
NS
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)
932f48ac
NS
243 {
244 if (usings)
245 queue = do_queue_usings (queue, depth, usings);
246 return queue;
247 }
248
9dda0ace 249private:
f35a733d
NS
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:
9dda0ace
NS
262 /* Search namespace + inlines + maybe usings as qualified lookup. */
263 bool search_qualified (tree scope, bool usings = true);
264
932f48ac
NS
265 /* Search namespace + inlines + usings as unqualified lookup. */
266 bool search_unqualified (tree scope, cp_binding_level *);
267
9dda0ace 268 /* ADL lookup of ARGS. */
f35a733d 269 tree search_adl (tree fns, vec<tree, va_gc> *args);
b655c310 270};
8db29d88 271
f35a733d
NS
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;
8db29d88 275
f35a733d
NS
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 ()
8db29d88 287{
f35a733d
NS
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 }
32196b87
NS
308
309 /* Unmark the outer partial lookup. */
3d7ff728
NS
310 if (previous->deduping)
311 lookup_mark (previous->value, false);
f35a733d 312 }
b655c310 313 else
f35a733d
NS
314 scopes = shared_scopes;
315 active = this;
8db29d88
AO
316}
317
f35a733d 318/* Restore the marking state of a lookup we interrupted. */
daafa301 319
f35a733d
NS
320void
321name_lookup::restore_state ()
5e0c54e5 322{
3d7ff728
NS
323 if (deduping)
324 lookup_mark (value, false);
325
f35a733d
NS
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 }
5e0c54e5 334
f35a733d
NS
335 active = previous;
336 if (previous)
5e0c54e5 337 {
32196b87
NS
338 free (scopes);
339
f35a733d
NS
340 unsigned length = vec_safe_length (previous->scopes);
341 for (unsigned ix = 0; ix != length; ix++)
b655c310 342 {
f35a733d
NS
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;
b655c310 361 }
5e0c54e5 362
32196b87 363 /* Remark the outer partial lookup. */
3d7ff728
NS
364 if (previous->deduping)
365 lookup_mark (previous->value, true);
f35a733d
NS
366 }
367 else
368 shared_scopes = scopes;
5e0c54e5 369}
5e0c54e5 370
f35a733d
NS
371void
372name_lookup::mark_seen (tree scope)
b655c310 373{
f35a733d
NS
374 gcc_checking_assert (!seen_p (scope));
375 LOOKUP_SEEN_P (scope) = true;
376 vec_safe_push (scopes, scope);
377}
daafa301 378
f35a733d
NS
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 }
5e0c54e5 389
f35a733d
NS
390 return result;
391}
daafa301 392
9dda0ace
NS
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
3d7ff728
NS
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
9dda0ace
NS
431/* Add a NEW_VAL, a found value binding into the current value binding. */
432
433void
434name_lookup::add_value (tree new_val)
435{
3d7ff728
NS
436 if (OVL_P (new_val) && (!value || OVL_P (value)))
437 add_overload (new_val);
438 else if (!value)
9dda0ace
NS
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))))
71bbbd13
NS
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);
9dda0ace 452 else
3d7ff728
NS
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 }
9dda0ace
NS
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
3c9cca88
NS
540 if (tree *binding = find_namespace_slot (scope, name))
541 found |= process_binding (MAYBE_STAT_DECL (*binding),
542 MAYBE_STAT_TYPE (*binding));
9dda0ace
NS
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
3c9feefc
NS
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]);
9dda0ace
NS
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;
3c9feefc
NS
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);
9dda0ace
NS
585
586 /* Look in its inline children. */
3c9feefc
NS
587 if (vec<tree, va_gc> *inlinees = DECL_NAMESPACE_INLINEES (scope))
588 for (unsigned ix = inlinees->length (); ix--;)
589 found |= search_usings ((*inlinees)[ix]);
9dda0ace
NS
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
932f48ac
NS
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. */
3c9feefc
NS
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]);
932f48ac
NS
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 *
3c9feefc
NS
652name_lookup::do_queue_usings (using_queue *queue, int depth,
653 vec<tree, va_gc> *usings)
932f48ac 654{
3c9feefc
NS
655 for (unsigned ix = usings->length (); ix--;)
656 queue = queue_namespace (queue, depth, (*usings)[ix]);
932f48ac
NS
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
f35a733d
NS
717/* FNS is a value binding. If it is a (set of overloaded) functions,
718 add them into the current value. */
5e0c54e5 719
f35a733d
NS
720void
721name_lookup::add_fns (tree fns)
722{
723 if (!fns)
724 return;
725 else if (TREE_CODE (fns) == OVERLOAD)
5e0c54e5 726 {
f35a733d
NS
727 if (TREE_TYPE (fns) != unknown_type_node)
728 fns = OVL_FUNCTION (fns);
b655c310 729 }
f35a733d
NS
730 else if (!DECL_DECLARES_FUNCTION_P (fns))
731 return;
daafa301 732
3d7ff728 733 add_overload (fns);
5e0c54e5
GDR
734}
735
f35a733d 736/* Add functions of a namespace to the lookup structure. */
daafa301 737
f35a733d
NS
738void
739name_lookup::adl_namespace_only (tree scope)
5e0c54e5 740{
f35a733d 741 mark_seen (scope);
5e0c54e5 742
f35a733d 743 /* Look down into inline namespaces. */
3c9feefc
NS
744 if (vec<tree, va_gc> *inlinees = DECL_NAMESPACE_INLINEES (scope))
745 for (unsigned ix = inlinees->length (); ix--;)
746 adl_namespace_only ((*inlinees)[ix]);
b655c310 747
3c9cca88
NS
748 if (tree fns = find_namespace_value (scope, name))
749 add_fns (ovl_skip_hidden (fns));
f35a733d 750}
0cbd7506 751
f35a733d
NS
752/* Find the containing non-inlined namespace, add it and all its
753 inlinees. */
b655c310 754
f35a733d
NS
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);
5e0c54e5
GDR
766}
767
f35a733d 768/* Adds the class and its friends to the lookup structure. */
daafa301 769
f35a733d
NS
770void
771name_lookup::adl_class_only (tree type)
5e0c54e5 772{
b655c310
NS
773 /* Backend-built structures, such as __builtin_va_list, aren't
774 affected by all this. */
775 if (!CLASS_TYPE_P (type))
f35a733d
NS
776 return;
777
778 type = TYPE_MAIN_VARIANT (type);
5e0c54e5 779
f35a733d
NS
780 if (see_and_mark (type))
781 return;
782
783 tree context = decl_namespace_context (type);
784 adl_namespace (context);
5e0c54e5 785
b655c310 786 complete_type (type);
daafa301 787
f35a733d
NS
788 /* Add friends. */
789 for (tree list = DECL_FRIENDLIST (TYPE_MAIN_DECL (type)); list;
b655c310 790 list = TREE_CHAIN (list))
f35a733d
NS
791 if (name == FRIEND_NAME (list))
792 for (tree friends = FRIEND_DECLS (list); friends;
b655c310
NS
793 friends = TREE_CHAIN (friends))
794 {
795 tree fn = TREE_VALUE (friends);
5e0c54e5 796
b655c310
NS
797 /* Only interested in global functions with potentially hidden
798 (i.e. unqualified) declarations. */
799 if (CP_DECL_CONTEXT (fn) != context)
800 continue;
f35a733d 801
25d446fd
NS
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
b655c310
NS
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;
5e0c54e5 813
f35a733d
NS
814 add_fns (fn);
815 }
5e0c54e5
GDR
816}
817
b655c310
NS
818/* Adds the class and its bases to the lookup structure.
819 Returns true on error. */
daafa301 820
f35a733d
NS
821void
822name_lookup::adl_bases (tree type)
5e0c54e5 823{
f35a733d 824 adl_class_only (type);
82f2836c 825
f35a733d
NS
826 /* Process baseclasses. */
827 if (tree binfo = TYPE_BINFO (type))
5e0c54e5 828 {
f35a733d 829 tree base_binfo;
b655c310 830 int i;
aed81407 831
f35a733d
NS
832 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
833 adl_bases (BINFO_TYPE (base_binfo));
b655c310 834 }
89b578be
MM
835}
836
b655c310
NS
837/* Adds everything associated with a class argument type to the lookup
838 structure. Returns true on error.
daafa301 839
b655c310
NS
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
f35a733d
NS
853void
854name_lookup::adl_class (tree type)
aed81407 855{
b655c310
NS
856 /* Backend build structures, such as __builtin_va_list, aren't
857 affected by all this. */
858 if (!CLASS_TYPE_P (type))
f35a733d 859 return;
aed81407 860
f35a733d
NS
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;
aed81407 866
f35a733d
NS
867 adl_bases (type);
868 mark_found (type);
daafa301 869
f35a733d
NS
870 if (TYPE_CLASS_SCOPE_P (type))
871 adl_class_only (TYPE_CONTEXT (type));
c87ceb13 872
b655c310
NS
873 /* Process template arguments. */
874 if (CLASSTYPE_TEMPLATE_INFO (type)
875 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
876 {
f35a733d
NS
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));
b655c310 880 }
90ea9897
MM
881}
882
f35a733d
NS
883void
884name_lookup::adl_expr (tree expr)
885{
886 if (!expr)
887 return;
90ea9897 888
f35a733d
NS
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)
90ea9897 927{
b655c310 928 if (!type)
f35a733d 929 return;
7de5bccc 930
b655c310 931 if (TYPE_PTRDATAMEM_P (type))
90ea9897 932 {
b655c310 933 /* Pointer to member: associate class type and value type. */
f35a733d
NS
934 adl_type (TYPE_PTRMEM_CLASS_TYPE (type));
935 adl_type (TYPE_PTRMEM_POINTED_TO_TYPE (type));
936 return;
89b578be 937 }
f35a733d
NS
938
939 switch (TREE_CODE (type))
b655c310 940 {
b655c310
NS
941 case RECORD_TYPE:
942 if (TYPE_PTRMEMFUNC_P (type))
f35a733d
NS
943 {
944 adl_type (TYPE_PTRMEMFUNC_FN_TYPE (type));
945 return;
946 }
b655c310
NS
947 /* FALLTHRU */
948 case UNION_TYPE:
f35a733d
NS
949 adl_class (type);
950 return;
951
b655c310
NS
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. */
f35a733d
NS
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
b655c310
NS
973 case LANG_TYPE:
974 gcc_assert (type == unknown_type_node
975 || type == init_list_type_node);
f35a733d
NS
976 return;
977
b655c310 978 case TYPE_PACK_EXPANSION:
f35a733d
NS
979 adl_type (PACK_EXPANSION_PATTERN (type));
980 return;
c8094d83 981
b655c310 982 default:
f35a733d 983 break;
b655c310 984 }
00e8de68
GDR
985}
986
f35a733d
NS
987/* Adds everything associated with a template argument to the lookup
988 structure. */
00e8de68 989
f35a733d
NS
990void
991name_lookup::adl_template_arg (tree arg)
00e8de68 992{
f35a733d 993 /* [basic.lookup.koenig]
00e8de68 994
f35a733d
NS
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. ] */
00e8de68 1003
f35a733d
NS
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)
00e8de68 1009 {
f35a733d 1010 tree ctx = CP_DECL_CONTEXT (arg);
b655c310 1011
f35a733d
NS
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);
00e8de68 1018 }
f35a733d
NS
1019 /* It's an argument pack; handle it recursively. */
1020 else if (ARGUMENT_PACK_P (arg))
b655c310 1021 {
f35a733d
NS
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));
b655c310 1026 }
f35a733d
NS
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);
00e8de68
GDR
1031}
1032
f35a733d
NS
1033/* Perform ADL lookup. FNS is the existing lookup result and ARGS are
1034 the call arguments. */
9485d254 1035
f35a733d
NS
1036tree
1037name_lookup::search_adl (tree fns, vec<tree, va_gc> *args)
9485d254 1038{
3d7ff728
NS
1039 if (fns)
1040 {
1041 deduping = true;
1042 lookup_mark (fns, true);
1043 }
f35a733d 1044 value = fns;
7f82286e 1045
f35a733d
NS
1046 unsigned ix;
1047 tree arg;
1048
1049 FOR_EACH_VEC_ELT_REVERSE (*args, ix, arg)
b67b23f0
NS
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
f35a733d
NS
1055 adl_expr (arg);
1056
f35a733d 1057 fns = value;
c87ceb13 1058
b655c310
NS
1059 return fns;
1060}
c87ceb13 1061
9dda0ace
NS
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);
9dda0ace
NS
1068static void diagnose_name_conflict (tree, tree);
1069
f35a733d
NS
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). */
c87ceb13 1073
f35a733d 1074tree
b655c310 1075lookup_arg_dependent (tree name, tree fns, vec<tree, va_gc> *args)
c87ceb13 1076{
f35a733d
NS
1077 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
1078 name_lookup lookup (name);
1079 fns = lookup.search_adl (fns, args);
b655c310 1080 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
f35a733d
NS
1081 return fns;
1082}
1083
bff8b385
NS
1084/* FNS is an overload set of conversion functions. Return the
1085 overloads converting to TYPE. */
9e931c2a
NS
1086
1087static tree
bff8b385 1088extract_conversion_operator (tree fns, tree type)
9e931c2a 1089{
2e12a855 1090 tree convs = NULL_TREE;
bff8b385 1091 tree tpls = NULL_TREE;
9e931c2a 1092
bff8b385 1093 for (ovl_iterator iter (fns); iter; ++iter)
9e931c2a 1094 {
bff8b385
NS
1095 if (same_type_p (DECL_CONV_FN_TYPE (*iter), type))
1096 convs = lookup_add (*iter, convs);
2e12a855 1097
bff8b385
NS
1098 if (TREE_CODE (*iter) == TEMPLATE_DECL)
1099 tpls = lookup_add (*iter, tpls);
9e931c2a
NS
1100 }
1101
bff8b385
NS
1102 if (!convs)
1103 convs = tpls;
1104
2e12a855 1105 return convs;
9e931c2a
NS
1106}
1107
783dc739 1108/* Binary search of (ordered) MEMBER_VEC for NAME. */
9e931c2a 1109
b991151b 1110static tree
783dc739 1111member_vec_binary_search (vec<tree, va_gc> *member_vec, tree name)
9e931c2a 1112{
783dc739 1113 for (unsigned lo = 0, hi = member_vec->length (); lo < hi;)
9e931c2a 1114 {
b991151b 1115 unsigned mid = (lo + hi) / 2;
783dc739 1116 tree binding = (*member_vec)[mid];
b991151b
NS
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;
9e931c2a 1125 }
9e931c2a 1126
b991151b
NS
1127 return NULL_TREE;
1128}
bff8b385 1129
783dc739 1130/* Linear search of (unordered) MEMBER_VEC for NAME. */
b991151b
NS
1131
1132static tree
783dc739 1133member_vec_linear_search (vec<tree, va_gc> *member_vec, tree name)
b991151b 1134{
783dc739 1135 for (int ix = member_vec->length (); ix--;)
b991151b
NS
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. */
783dc739 1140 if (tree binding = (*member_vec)[ix])
b991151b
NS
1141 {
1142 if (OVL_NAME (binding) == name)
1143 return binding;
1144 }
1145 else
1146 break;
1147
1148 return NULL_TREE;
9e931c2a
NS
1149}
1150
45e3a33d 1151/* Linear search of (partially ordered) fields of KLASS for NAME. */
9e931c2a 1152
b991151b 1153static tree
45e3a33d 1154fields_linear_search (tree klass, tree name, bool want_type)
9e931c2a 1155{
45e3a33d 1156 for (tree fields = TYPE_FIELDS (klass); fields; fields = DECL_CHAIN (fields))
9e931c2a 1157 {
45e3a33d 1158 tree decl = fields;
9e931c2a 1159
45e3a33d
NS
1160 if (!want_type
1161 && TREE_CODE (decl) == FIELD_DECL
1162 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
9e931c2a 1163 {
45e3a33d
NS
1164 tree anon = TREE_TYPE (decl);
1165 gcc_assert (COMPLETE_TYPE_P (anon));
1166 tree temp;
1167
783dc739
NS
1168 if (vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (anon))
1169 temp = member_vec_linear_search (member_vec, name);
18a01e85 1170 else
45e3a33d 1171 temp = fields_linear_search (anon, name, want_type);
18a01e85 1172
45e3a33d
NS
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;
18a01e85 1178 }
9e931c2a 1179 }
9e931c2a 1180
45e3a33d 1181 if (DECL_NAME (decl) != name)
9e931c2a 1182 continue;
45e3a33d
NS
1183
1184 if (TREE_CODE (decl) == USING_DECL)
9e931c2a
NS
1185 {
1186 decl = strip_using_decl (decl);
1187 if (is_overloaded_fn (decl))
1188 continue;
1189 }
1190
45e3a33d
NS
1191 if (DECL_DECLARES_FUNCTION_P (decl))
1192 /* Functions are found separately. */
1193 continue;
1194
1195 if (!want_type || DECL_DECLARES_TYPE_P (decl))
9e931c2a
NS
1196 return decl;
1197 }
1198
9e931c2a
NS
1199 return NULL_TREE;
1200}
1201
b991151b
NS
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;
783dc739 1219 vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (klass);
b991151b 1220
783dc739 1221 if (COMPLETE_TYPE_P (klass) && member_vec)
b991151b 1222 {
783dc739 1223 val = member_vec_binary_search (member_vec, lookup);
45e3a33d
NS
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 }
b991151b 1246 }
45e3a33d
NS
1247 else
1248 {
783dc739
NS
1249 if (member_vec && type_or_fns <= 0)
1250 val = member_vec_linear_search (member_vec, lookup);
b991151b 1251
45e3a33d
NS
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 }
b991151b
NS
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. */
9e931c2a
NS
1279
1280tree
20614c86 1281get_class_binding (tree klass, tree name, int type_or_fns)
9e931c2a 1282{
20614c86 1283 klass = complete_type (klass);
9e931c2a 1284
20614c86 1285 if (COMPLETE_TYPE_P (klass))
9e931c2a 1286 {
20614c86 1287 /* Lazily declare functions, if we're going to search these. */
9e931c2a
NS
1288 if (IDENTIFIER_CTOR_P (name))
1289 {
20614c86
NS
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);
9e931c2a 1296 }
20614c86 1297 else if (IDENTIFIER_DTOR_P (name))
9e931c2a 1298 {
20614c86
NS
1299 if (CLASSTYPE_LAZY_DESTRUCTOR (klass))
1300 lazily_declare_fn (sfk_destructor, klass);
9e931c2a 1301 }
20614c86 1302 else if (name == cp_assignment_operator_id (NOP_EXPR))
9e931c2a 1303 {
20614c86
NS
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);
9e931c2a
NS
1308 }
1309 }
1310
20614c86 1311 return get_class_binding_direct (klass, name, type_or_fns);
9e931c2a
NS
1312}
1313
fcaf3065
NS
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 *
783dc739 1320get_member_slot (tree klass, tree name)
fcaf3065
NS
1321{
1322 bool complete_p = COMPLETE_TYPE_P (klass);
1323
783dc739
NS
1324 vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (klass);
1325 if (!member_vec)
fcaf3065 1326 {
783dc739
NS
1327 vec_alloc (member_vec, 8);
1328 CLASSTYPE_MEMBER_VEC (klass) = member_vec;
45e3a33d
NS
1329 if (complete_p)
1330 {
783dc739 1331 /* If the class is complete but had no member_vec, we need
45e3a33d
NS
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);
783dc739 1336 member_vec = CLASSTYPE_MEMBER_VEC (klass);
45e3a33d 1337 }
fcaf3065
NS
1338 }
1339
1340 if (IDENTIFIER_CONV_OP_P (name))
1341 name = conv_op_identifier;
1342
783dc739 1343 unsigned ix, length = member_vec->length ();
fcaf3065
NS
1344 for (ix = 0; ix < length; ix++)
1345 {
783dc739 1346 tree *slot = &(*member_vec)[ix];
fcaf3065
NS
1347 tree fn_name = OVL_NAME (*slot);
1348
1349 if (fn_name == name)
1350 {
45e3a33d
NS
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));
fcaf3065
NS
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. */
783dc739
NS
1376 vec_safe_reserve_exact (member_vec, 1);
1377 member_vec->quick_insert (ix, NULL_TREE);
fcaf3065
NS
1378 }
1379 else
1380 {
1381 gcc_checking_assert (ix == length);
783dc739 1382 vec_safe_push (member_vec, NULL_TREE);
fcaf3065 1383 }
783dc739 1384 CLASSTYPE_MEMBER_VEC (klass) = member_vec;
fcaf3065 1385
783dc739 1386 tree *slot = &(*member_vec)[ix];
fcaf3065
NS
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
783dc739
NS
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. */
20614c86
NS
1401
1402static int
783dc739 1403member_name_cmp (const void *a_p, const void *b_p)
20614c86
NS
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
45e3a33d
NS
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). */
12f71313
NS
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;
20614c86
NS
1461}
1462
fe920c2d
NS
1463static struct {
1464 gt_pointer_operator new_value;
1465 void *cookie;
1466} resort_data;
1467
783dc739 1468/* This routine compares two fields like member_name_cmp but using the
20614c86
NS
1469 pointer operator in resort_field_decl_data. We don't have to deal
1470 with duplicates here. */
fe920c2d
NS
1471
1472static int
783dc739 1473resort_member_name_cmp (const void *a_p, const void *b_p)
fe920c2d 1474{
20614c86
NS
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);
fe920c2d 1479
20614c86
NS
1480 resort_data.new_value (&name_a, resort_data.cookie);
1481 resort_data.new_value (&name_b, resort_data.cookie);
fe920c2d 1482
20614c86 1483 gcc_checking_assert (name_a != name_b);
fe920c2d 1484
20614c86 1485 return name_a < name_b ? -1 : +1;
fe920c2d
NS
1486}
1487
783dc739 1488/* Resort CLASSTYPE_MEMBER_VEC because pointers have been reordered. */
fe920c2d
NS
1489
1490void
783dc739 1491resort_type_member_vec (void *obj, void */*orig_obj*/,
20614c86 1492 gt_pointer_operator new_value, void* cookie)
fe920c2d 1493{
783dc739 1494 if (vec<tree, va_gc> *member_vec = (vec<tree, va_gc> *) obj)
fe920c2d
NS
1495 {
1496 resort_data.new_value = new_value;
1497 resort_data.cookie = cookie;
783dc739
NS
1498 qsort (member_vec->address (), member_vec->length (),
1499 sizeof (tree), resort_member_name_cmp);
fe920c2d
NS
1500 }
1501}
1502
1887fb46
NS
1503/* Recursively count the number of fields in KLASS, including anonymous
1504 union members. */
41970ff1 1505
1887fb46
NS
1506static unsigned
1507count_class_fields (tree klass)
41970ff1 1508{
1887fb46
NS
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
18a01e85 1520 return n_fields;
41970ff1
NS
1521}
1522
783dc739
NS
1523/* Append all the nonfunction members fields of KLASS to MEMBER_VEC.
1524 Recurse for anonymous members. MEMBER_VEC must have space. */
18a01e85 1525
45e3a33d 1526static void
783dc739 1527member_vec_append_class_fields (vec<tree, va_gc> *member_vec, tree klass)
18a01e85 1528{
1887fb46
NS
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)))
783dc739 1534 member_vec_append_class_fields (member_vec, TREE_TYPE (fields));
1887fb46 1535 else if (DECL_NAME (fields))
45e3a33d
NS
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);
783dc739 1542 member_vec->quick_push (field);
45e3a33d 1543 }
18a01e85
NS
1544}
1545
783dc739
NS
1546/* Append all of the enum values of ENUMTYPE to MEMBER_VEC.
1547 MEMBER_VEC must have space. */
18a01e85 1548
45e3a33d 1549static void
783dc739 1550member_vec_append_enum_values (vec<tree, va_gc> *member_vec, tree enumtype)
18a01e85 1551{
1887fb46
NS
1552 for (tree values = TYPE_VALUES (enumtype);
1553 values; values = TREE_CHAIN (values))
783dc739 1554 member_vec->quick_push (TREE_VALUE (values));
45e3a33d
NS
1555}
1556
783dc739 1557/* MEMBER_VEC has just had new DECLs added to it, but is sorted.
45e3a33d
NS
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
783dc739 1566 member_name_cmp will have ordered duplicates as
45e3a33d
NS
1567 <fns><using><type> */
1568
1569static void
783dc739 1570member_vec_dedup (vec<tree, va_gc> *member_vec)
45e3a33d 1571{
783dc739 1572 unsigned len = member_vec->length ();
45e3a33d
NS
1573 unsigned store = 0;
1574
783dc739 1575 tree current = (*member_vec)[0], name = OVL_NAME (current);
45e3a33d
NS
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 }
1887fb46 1608
45e3a33d
NS
1609 for (jx = ix + 1; jx < len; jx++)
1610 {
783dc739 1611 next = (*member_vec)[jx];
45e3a33d
NS
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 }
783dc739 1661 (*member_vec)[store++] = current;
45e3a33d
NS
1662 }
1663
1664 while (store++ < len)
783dc739 1665 member_vec->pop ();
18a01e85
NS
1666}
1667
783dc739
NS
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
45e3a33d
NS
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. */
41970ff1
NS
1673
1674void
45e3a33d 1675set_class_bindings (tree klass, unsigned extra)
41970ff1 1676{
45e3a33d 1677 unsigned n_fields = count_class_fields (klass);
783dc739 1678 vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (klass);
fe920c2d 1679
783dc739 1680 if (member_vec || n_fields >= 8)
18a01e85 1681 {
45e3a33d 1682 /* Append the new fields. */
783dc739
NS
1683 vec_safe_reserve_exact (member_vec, extra + n_fields);
1684 member_vec_append_class_fields (member_vec, klass);
45e3a33d
NS
1685 }
1686
783dc739 1687 if (member_vec)
45e3a33d 1688 {
783dc739
NS
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);
18a01e85 1693 }
41970ff1
NS
1694}
1695
18a01e85 1696/* Insert lately defined enum ENUMTYPE into KLASS for the sorted case. */
41970ff1
NS
1697
1698void
d876eb05 1699insert_late_enum_def_bindings (tree klass, tree enumtype)
41970ff1 1700{
45e3a33d 1701 int n_fields;
783dc739 1702 vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (klass);
1887fb46 1703
45e3a33d 1704 /* The enum bindings will already be on the TYPE_FIELDS, so don't
1887fb46 1705 count them twice. */
783dc739 1706 if (!member_vec)
1887fb46 1707 n_fields = count_class_fields (klass);
45e3a33d
NS
1708 else
1709 n_fields = list_length (TYPE_VALUES (enumtype));
1887fb46 1710
783dc739 1711 if (member_vec || n_fields >= 8)
18a01e85 1712 {
783dc739
NS
1713 vec_safe_reserve_exact (member_vec, n_fields);
1714 if (CLASSTYPE_MEMBER_VEC (klass))
1715 member_vec_append_enum_values (member_vec, enumtype);
1887fb46 1716 else
783dc739
NS
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);
18a01e85 1722 }
41970ff1
NS
1723}
1724
b655c310
NS
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. */
9306cccb 1728
b655c310 1729#define ENTRY_INDEX(HASH, COUNT) (((HASH) >> 3) & ((COUNT) - 1))
c87ceb13 1730
b655c310 1731/* A free list of "binding_entry"s awaiting for re-use. */
c87ceb13 1732
b655c310 1733static GTY((deletable)) binding_entry free_binding_entry = NULL;
c8094d83 1734
b655c310 1735/* The binding oracle; see cp-tree.h. */
c87ceb13 1736
b655c310 1737cp_binding_oracle_function *cp_binding_oracle;
575bfb00 1738
b655c310
NS
1739/* If we have a binding oracle, ask it for all namespace-scoped
1740 definitions of NAME. */
557831a9 1741
b655c310
NS
1742static inline void
1743query_oracle (tree name)
557831a9 1744{
b655c310
NS
1745 if (!cp_binding_oracle)
1746 return;
557831a9 1747
b655c310
NS
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;
575bfb00 1753
b655c310 1754 cp_binding_oracle (CP_ORACLE_IDENTIFIER, name);
c87ceb13 1755}
00e8de68 1756
b655c310 1757/* Create a binding_entry object for (NAME, TYPE). */
00e8de68 1758
b655c310
NS
1759static inline binding_entry
1760binding_entry_make (tree name, tree type)
00e8de68 1761{
b655c310 1762 binding_entry entry;
89bd2c03 1763
b655c310 1764 if (free_binding_entry)
00e8de68 1765 {
b655c310
NS
1766 entry = free_binding_entry;
1767 free_binding_entry = entry->chain;
00e8de68 1768 }
c8094d83 1769 else
b655c310 1770 entry = ggc_alloc<binding_entry_s> ();
00e8de68 1771
b655c310
NS
1772 entry->name = name;
1773 entry->type = type;
1774 entry->chain = NULL;
a5e6b29b 1775
b655c310
NS
1776 return entry;
1777}
a5e6b29b 1778
b655c310
NS
1779/* Put ENTRY back on the free list. */
1780#if 0
1781static inline void
1782binding_entry_free (binding_entry entry)
a5e6b29b 1783{
b655c310
NS
1784 entry->name = NULL;
1785 entry->type = NULL;
1786 entry->chain = free_binding_entry;
1787 free_binding_entry = entry;
1788}
1789#endif
a5e6b29b 1790
b655c310
NS
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;
2a50edcd 1796
b655c310
NS
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;
a5e6b29b 1800
b655c310
NS
1801 /* Number of "binding_entry"s in this table. */
1802 size_t entry_count;
1803};
a5e6b29b 1804
b655c310 1805/* Construct TABLE with an initial CHAIN_COUNT. */
a5e6b29b 1806
b655c310
NS
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}
a5e6b29b 1814
b655c310
NS
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;
a5e6b29b 1822
b655c310
NS
1823 if (table == NULL)
1824 return;
a5e6b29b 1825
b655c310
NS
1826 for (i = 0, count = table->chain_count; i < count; ++i)
1827 {
1828 binding_entry temp = table->chain[i];
1829 while (temp != NULL)
a5e6b29b 1830 {
b655c310
NS
1831 binding_entry entry = temp;
1832 temp = entry->chain;
1833 binding_entry_free (entry);
a5e6b29b 1834 }
b655c310
NS
1835 table->chain[i] = NULL;
1836 }
1837 table->entry_count = 0;
1838}
1839#endif
a5e6b29b 1840
b655c310 1841/* Allocate a table with CHAIN_COUNT, assumed to be a power of two. */
a5e6b29b 1842
b655c310
NS
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}
a5e6b29b 1851
b655c310 1852/* Expand TABLE to twice its current chain_count. */
a5e6b29b 1853
b655c310
NS
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])
a5e6b29b 1868 {
b655c310
NS
1869 const unsigned int hash = IDENTIFIER_HASH_VALUE (entry->name);
1870 const size_t j = ENTRY_INDEX (hash, new_chain_count);
10827cd8 1871
b655c310
NS
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}
10827cd8 1879
b655c310 1880/* Insert a binding for NAME to TYPE into TABLE. */
10827cd8 1881
b655c310
NS
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);
c8094d83 1888
b655c310
NS
1889 entry->chain = table->chain[i];
1890 table->chain[i] = entry;
1891 ++table->entry_count;
b1a19c7c 1892
b655c310
NS
1893 if (3 * table->chain_count < 5 * table->entry_count)
1894 binding_table_expand (table);
1895}
a5e6b29b 1896
b655c310 1897/* Return the binding_entry, if any, that maps NAME. */
c8094d83 1898
b655c310
NS
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)];
c8094d83 1904
b655c310
NS
1905 while (entry != NULL && entry->name != name)
1906 entry = entry->chain;
a5e6b29b 1907
b655c310
NS
1908 return entry;
1909}
ecba6c56 1910
b655c310 1911/* Apply PROC -- with DATA -- to all entries in TABLE. */
a5e6b29b 1912
b655c310
NS
1913void
1914binding_table_foreach (binding_table table, bt_foreach_proc proc, void *data)
1915{
1916 size_t chain_count;
1917 size_t i;
a5e6b29b 1918
b655c310
NS
1919 if (!table)
1920 return;
a5e6b29b 1921
b655c310
NS
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
199b7a35 1936
b655c310 1937/* A free list of "cxx_binding"s, connected by their PREVIOUS. */
f7cbd40e 1938
b655c310 1939static GTY((deletable)) cxx_binding *free_bindings;
f7cbd40e 1940
b655c310
NS
1941/* Initialize VALUE and TYPE field for BINDING, and set the PREVIOUS
1942 field to NULL. */
d0940d56 1943
b655c310
NS
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}
a5e6b29b 1951
b655c310 1952/* (GC)-allocate a binding object with VALUE and TYPE member initialized. */
3797cb21 1953
b655c310
NS
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> ();
a5e6b29b 1965
b655c310 1966 cxx_binding_init (binding, value, type);
a5e6b29b 1967
b655c310
NS
1968 return binding;
1969}
a5e6b29b 1970
b655c310 1971/* Put BINDING back on the free list. */
a5e6b29b 1972
b655c310
NS
1973static inline void
1974cxx_binding_free (cxx_binding *binding)
1975{
1976 binding->scope = NULL;
1977 binding->previous = free_bindings;
1978 free_bindings = binding;
1979}
a5e6b29b 1980
b655c310
NS
1981/* Create a new binding for NAME (with the indicated VALUE and TYPE
1982 bindings) in the class scope indicated by SCOPE. */
a5e6b29b 1983
b655c310
NS
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}
a5e6b29b 1993
b655c310
NS
1994/* Make DECL the innermost binding for ID. The LEVEL is the binding
1995 level at which this declaration is being bound. */
a5e6b29b 1996
b655c310
NS
1997void
1998push_binding (tree id, tree decl, cp_binding_level* level)
1999{
2000 cxx_binding *binding;
a5e6b29b 2001
b655c310
NS
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);
a5e6b29b 2009
b655c310
NS
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);
a5e6b29b 2014
b655c310
NS
2015 /* And put it on the front of the list of bindings for ID. */
2016 IDENTIFIER_BINDING (id) = binding;
575bfb00
LC
2017}
2018
b655c310
NS
2019/* Remove the binding for DECL which should be the innermost binding
2020 for ID. */
575bfb00 2021
b655c310 2022void
9c82d7b6 2023pop_local_binding (tree id, tree decl)
575bfb00 2024{
b655c310 2025 cxx_binding *binding;
d63d5d0c 2026
b655c310
NS
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;
d63d5d0c 2032
b655c310
NS
2033 /* Get the innermost binding for ID. */
2034 binding = IDENTIFIER_BINDING (id);
a5e6b29b 2035
b655c310
NS
2036 /* The name should be bound. */
2037 gcc_assert (binding != NULL);
a5e6b29b 2038
b655c310
NS
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;
a5e6b29b 2043 else
b655c310
NS
2044 {
2045 gcc_assert (binding->type == decl);
2046 binding->type = NULL_TREE;
2047 }
00e8de68 2048
b655c310 2049 if (!binding->value && !binding->type)
00e8de68 2050 {
b655c310
NS
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);
00e8de68 2057 }
b655c310 2058}
00e8de68 2059
b655c310
NS
2060/* Remove the bindings for the decls of the current level and leave
2061 the current scope. */
00e8de68 2062
b655c310
NS
2063void
2064pop_bindings_and_leave_scope (void)
2065{
9c82d7b6 2066 for (tree t = get_local_decls (); t; t = DECL_CHAIN (t))
9d029ddf
NS
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
b655c310 2074 leave_scope ();
00e8de68 2075}
a5e6b29b 2076
b655c310
NS
2077/* Strip non dependent using declarations. If DECL is dependent,
2078 surreptitiously create a typename_type and return it. */
a5e6b29b
GDR
2079
2080tree
b655c310 2081strip_using_decl (tree decl)
a5e6b29b 2082{
b655c310
NS
2083 if (decl == NULL_TREE)
2084 return NULL_TREE;
a5e6b29b 2085
b655c310
NS
2086 while (TREE_CODE (decl) == USING_DECL && !DECL_DEPENDENT_P (decl))
2087 decl = USING_DECL_DECLS (decl);
a5e6b29b 2088
b655c310
NS
2089 if (TREE_CODE (decl) == USING_DECL && DECL_DEPENDENT_P (decl)
2090 && USING_DECL_TYPENAME_P (decl))
a5e6b29b 2091 {
b655c310
NS
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);
a5e6b29b
GDR
2103 }
2104
b655c310
NS
2105 return decl;
2106}
a5e6b29b 2107
ef4c5e78
NS
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
b655c310
NS
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
ae5cbc33 2134
b655c310
NS
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.
ae5cbc33 2141
b655c310
NS
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. */
ef4c5e78 2182 || anticipated_builtin_p (target_bval))
b655c310
NS
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))))
a5e6b29b 2190 {
b655c310
NS
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;
a5e6b29b 2199 }
b655c310
NS
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:
a5e6b29b 2211
b655c310 2212 [dcl.typedef]
00e8de68 2213
b655c310
NS
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.
00e8de68 2217
b655c310
NS
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:
00e8de68 2225
b655c310 2226 [class.mem]
00e8de68 2227
b655c310
NS
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]
00e8de68 2244
b655c310
NS
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;
b655c310
NS
2250 else
2251 {
2252 if (!error_operand_p (bval))
2253 diagnose_name_conflict (decl, bval);
2254 ok = false;
2255 }
00e8de68 2256
b655c310 2257 return ok;
00e8de68
GDR
2258}
2259
b655c310
NS
2260/* Diagnose a name conflict between DECL and BVAL. */
2261
00e8de68 2262static void
b655c310
NS
2263diagnose_name_conflict (tree decl, tree bval)
2264{
2265 if (TREE_CODE (decl) == TREE_CODE (bval)
71bbbd13 2266 && TREE_CODE (decl) != NAMESPACE_DECL
9d029ddf 2267 && !DECL_DECLARES_FUNCTION_P (decl)
71bbbd13
NS
2268 && (TREE_CODE (decl) != TYPE_DECL
2269 || DECL_ARTIFICIAL (decl) == DECL_ARTIFICIAL (bval))
9d029ddf 2270 && CP_DECL_CONTEXT (decl) == CP_DECL_CONTEXT (bval))
b655c310 2271 error ("redeclaration of %q#D", decl);
00e8de68 2272 else
b655c310
NS
2273 error ("%q#D conflicts with a previous declaration", decl);
2274
2275 inform (location_of (bval), "previous declaration %q#D", bval);
00e8de68
GDR
2276}
2277
b655c310 2278/* Wrapper for supplement_binding_1. */
00e8de68 2279
b655c310
NS
2280static bool
2281supplement_binding (cxx_binding *binding, tree decl)
00e8de68 2282{
b655c310
NS
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;
00e8de68
GDR
2288}
2289
9d029ddf
NS
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
3c9cca88 2340/* Push DECL into nonclass LEVEL BINDING or SLOT. OLD is the current
3a9cc685
NS
2341 binding value (possibly with anticipated builtins stripped).
2342 Diagnose conflicts and return updated decl. */
2343
2344static tree
3c9cca88 2345update_binding (cp_binding_level *level, cxx_binding *binding, tree *slot,
3a9cc685
NS
2346 tree old, tree decl, bool is_friend)
2347{
2348 tree to_val = decl;
e2f35333
NS
2349 tree old_type = slot ? MAYBE_STAT_TYPE (*slot) : binding->type;
2350 tree to_type = old_type;
3a9cc685 2351
3c9cca88
NS
2352 gcc_assert (level->kind == sk_namespace ? !binding
2353 : level->kind != sk_class && !slot);
3a9cc685
NS
2354 if (old == error_mark_node)
2355 old = NULL_TREE;
2356
e2f35333
NS
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
3a9cc685
NS
2383 if (old && TREE_CODE (old) == TYPE_DECL && DECL_ARTIFICIAL (old))
2384 {
e2f35333 2385 /* Slide old into the type slot. */
3a9cc685
NS
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
c14c0b15
NS
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
3a9cc685
NS
2428 to_val = ovl_insert (decl, old);
2429 }
3a9cc685
NS
2430 else if (!old)
2431 ;
3a9cc685
NS
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 {
e2f35333 2437 if (same_type_p (TREE_TYPE (old), TREE_TYPE (decl)))
3a9cc685
NS
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 {
71bbbd13
NS
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))
3a9cc685 2448 goto conflict;
71bbbd13
NS
2449
2450 /* The new one must be an alias at this point. */
2451 gcc_assert (DECL_NAMESPACE_ALIAS (decl));
2452 return old;
3a9cc685
NS
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
e2f35333 2472 done:
3a9cc685
NS
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
3c9cca88
NS
2492 if (slot)
2493 {
2494 if (STAT_HACK_P (*slot))
2495 {
e2f35333 2496 STAT_TYPE (*slot) = to_type;
3c9cca88
NS
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 {
e2f35333 2506 binding->type = to_type;
3c9cca88
NS
2507 binding->value = to_val;
2508 }
3a9cc685
NS
2509 }
2510
2511 return decl;
2512}
2513
9bc3f420 2514/* Table of identifiers to extern C functions (or LISTS thereof). */
539f481a 2515
9bc3f420 2516static GTY(()) hash_table<named_decl_hash> *extern_c_fns;
539f481a
NS
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)
9bc3f420 2530 extern_c_fns = hash_table<named_decl_hash>::create_ggc (127);
539f481a 2531
9bc3f420
NS
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)
539f481a 2536 {
9bc3f420
NS
2537 if (TREE_CODE (old) == OVERLOAD)
2538 old = OVL_FUNCTION (old);
539f481a
NS
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
9bc3f420
NS
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 }
539f481a 2579 }
9bc3f420
NS
2580 else
2581 *slot = decl;
539f481a
NS
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)
9bc3f420
NS
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
539f481a
NS
2600 return NULL_TREE;
2601}
2602
c0c24822
NS
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
9db84ece
NS
2731 && (same_type_p (TREE_TYPE (old), TREE_TYPE (decl))
2732 || (!dependent_type_p (TREE_TYPE (decl))
2733 && !dependent_type_p (TREE_TYPE (old))
2734 && can_convert (TREE_TYPE (old), TREE_TYPE (decl),
2735 tf_none))))
c0c24822
NS
2736 warning_code = OPT_Wshadow_compatible_local;
2737 else
2738 return;
2739
2740 const char *msg;
2741 if (TREE_CODE (old) == PARM_DECL)
2742 msg = "declaration of %q#D shadows a parameter";
2743 else if (is_capture_proxy (old))
2744 msg = "declaration of %qD shadows a lambda capture";
2745 else
2746 msg = "declaration of %qD shadows a previous local";
2747
2748 if (warning_at (input_location, warning_code, msg, decl))
2749 {
2750 shadowed = old;
2751 goto inform_shadowed;
2752 }
2753 return;
2754 }
2755
2756 if (!warn_shadow)
2757 return;
2758
2759 /* Don't warn for artificial things that are not implicit typedefs. */
2760 if (DECL_ARTIFICIAL (decl) && !DECL_IMPLICIT_TYPEDEF_P (decl))
2761 return;
2762
2763 if (nonlambda_method_basetype ())
2764 if (tree member = lookup_member (current_nonlambda_class_type (),
2765 DECL_NAME (decl), /*protect=*/0,
2766 /*want_type=*/false, tf_warning_or_error))
2767 {
2768 member = MAYBE_BASELINK_FUNCTIONS (member);
2769
2770 /* Warn if a variable shadows a non-function, or the variable
2771 is a function or a pointer-to-function. */
5256a7f5 2772 if (!OVL_P (member)
c0c24822
NS
2773 || TREE_CODE (decl) == FUNCTION_DECL
2774 || TYPE_PTRFN_P (TREE_TYPE (decl))
2775 || TYPE_PTRMEMFUNC_P (TREE_TYPE (decl)))
2776 {
2777 if (warning_at (input_location, OPT_Wshadow,
2778 "declaration of %qD shadows a member of %qT",
2779 decl, current_nonlambda_class_type ())
2780 && DECL_P (member))
2781 {
2782 shadowed = member;
2783 goto inform_shadowed;
2784 }
2785 }
2786 return;
2787 }
2788
2789 /* Now look for a namespace shadow. */
3c9cca88 2790 old = find_namespace_value (current_namespace, DECL_NAME (decl));
c0c24822
NS
2791 if (old
2792 && (VAR_P (old)
2793 || (TREE_CODE (old) == TYPE_DECL
2794 && (!DECL_ARTIFICIAL (old)
2795 || TREE_CODE (decl) == TYPE_DECL)))
2796 && !instantiating_current_function_p ())
2797 /* XXX shadow warnings in outer-more namespaces */
2798 {
2799 if (warning_at (input_location, OPT_Wshadow,
2800 "declaration of %qD shadows a global declaration",
2801 decl))
2802 {
2803 shadowed = old;
2804 goto inform_shadowed;
2805 }
2806 return;
2807 }
2808
2809 return;
2810
2811 inform_shadowed:
2812 inform (DECL_SOURCE_LOCATION (shadowed), "shadowed declaration is here");
2813}
2814
e4ea7a4c
NS
2815/* DECL is being pushed inside function CTX. Set its context, if
2816 needed. */
2817
2818static void
2819set_decl_context_in_fn (tree ctx, tree decl)
2820{
2821 if (!DECL_CONTEXT (decl)
2822 /* A local declaration for a function doesn't constitute
2823 nesting. */
2824 && TREE_CODE (decl) != FUNCTION_DECL
2825 /* A local declaration for an `extern' variable is in the
2826 scope of the current namespace, not the current
2827 function. */
2828 && !(VAR_P (decl) && DECL_EXTERNAL (decl))
2829 /* When parsing the parameter list of a function declarator,
2830 don't set DECL_CONTEXT to an enclosing function. When we
2831 push the PARM_DECLs in order to process the function body,
2832 current_binding_level->this_entity will be set. */
2833 && !(TREE_CODE (decl) == PARM_DECL
2834 && current_binding_level->kind == sk_function_parms
2835 && current_binding_level->this_entity == NULL))
2836 DECL_CONTEXT (decl) = ctx;
2837
2838 /* If this is the declaration for a namespace-scope function,
2839 but the declaration itself is in a local scope, mark the
2840 declaration. */
2841 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (decl))
2842 DECL_LOCAL_FUNCTION_P (decl) = 1;
2843}
2844
2845/* DECL is a local-scope decl with linkage. SHADOWED is true if the
2846 name is already bound at the current level.
2847
2848 [basic.link] If there is a visible declaration of an entity with
2849 linkage having the same name and type, ignoring entities declared
2850 outside the innermost enclosing namespace scope, the block scope
2851 declaration declares that same entity and receives the linkage of
2852 the previous declaration.
2853
2854 Also, make sure that this decl matches any existing external decl
2855 in the enclosing namespace. */
2856
2857static void
2858set_local_extern_decl_linkage (tree decl, bool shadowed)
2859{
2860 tree ns_value = decl; /* Unique marker. */
2861
2862 if (!shadowed)
2863 {
2864 tree loc_value = innermost_non_namespace_value (DECL_NAME (decl));
2865 if (!loc_value)
2866 {
2867 ns_value
3c9cca88 2868 = find_namespace_value (current_namespace, DECL_NAME (decl));
e4ea7a4c
NS
2869 loc_value = ns_value;
2870 }
2871 if (loc_value == error_mark_node)
2872 loc_value = NULL_TREE;
2873
2874 for (ovl_iterator iter (loc_value); iter; ++iter)
ef4c5e78 2875 if (!iter.hidden_p ()
e4ea7a4c
NS
2876 && (TREE_STATIC (*iter) || DECL_EXTERNAL (*iter))
2877 && decls_match (*iter, decl))
2878 {
2879 /* The standard only says that the local extern inherits
2880 linkage from the previous decl; in particular, default
2881 args are not shared. Add the decl into a hash table to
2882 make sure only the previous decl in this case is seen
2883 by the middle end. */
2884 struct cxx_int_tree_map *h;
2885
2886 /* We inherit the outer decl's linkage. But we're a
2887 different decl. */
2888 TREE_PUBLIC (decl) = TREE_PUBLIC (*iter);
2889
2890 if (cp_function_chain->extern_decl_map == NULL)
2891 cp_function_chain->extern_decl_map
2892 = hash_table<cxx_int_tree_map_hasher>::create_ggc (20);
2893
2894 h = ggc_alloc<cxx_int_tree_map> ();
2895 h->uid = DECL_UID (decl);
2896 h->to = *iter;
2897 cxx_int_tree_map **loc = cp_function_chain->extern_decl_map
2898 ->find_slot (h, INSERT);
2899 *loc = h;
2900 break;
2901 }
2902 }
2903
2904 if (TREE_PUBLIC (decl))
2905 {
2906 /* DECL is externally visible. Make sure it matches a matching
3c9cca88 2907 decl in the namespace scope. We only really need to check
e4ea7a4c
NS
2908 this when inserting the decl, not when we find an existing
2909 match in the current scope. However, in practice we're
2910 going to be inserting a new decl in the majority of cases --
2911 who writes multiple extern decls for the same thing in the
2912 same local scope? Doing it here often avoids a duplicate
2913 namespace lookup. */
2914
2915 /* Avoid repeating a lookup. */
2916 if (ns_value == decl)
3c9cca88 2917 ns_value = find_namespace_value (current_namespace, DECL_NAME (decl));
e4ea7a4c
NS
2918
2919 if (ns_value == error_mark_node)
2920 ns_value = NULL_TREE;
2921
2922 for (ovl_iterator iter (ns_value); iter; ++iter)
2923 {
2924 tree other = *iter;
2925
2926 if (!(TREE_PUBLIC (other) || DECL_EXTERNAL (other)))
2927 ; /* Not externally visible. */
2928 else if (DECL_EXTERN_C_P (decl) && DECL_EXTERN_C_P (other))
2929 ; /* Both are extern "C", we'll check via that mechanism. */
2930 else if (TREE_CODE (other) != TREE_CODE (decl)
2931 || ((VAR_P (decl) || matching_fn_p (other, decl))
2932 && !comptypes (TREE_TYPE (decl), TREE_TYPE (other),
2933 COMPARE_REDECLARATION)))
2934 {
2935 if (permerror (DECL_SOURCE_LOCATION (decl),
2936 "local external declaration %q#D", decl))
2937 inform (DECL_SOURCE_LOCATION (other),
2938 "does not match previous declaration %q#D", other);
2939 break;
2940 }
2941 }
2942 }
2943}
2944
3a9cc685
NS
2945/* Record DECL as belonging to the current lexical scope. Check for
2946 errors (such as an incompatible declaration for the same name
2947 already seen in the same scope). IS_FRIEND is true if DECL is
b655c310 2948 declared as a friend.
00e8de68 2949
3a9cc685
NS
2950 Returns either DECL or an old decl for the same name. If an old
2951 decl is returned, it may have been smashed to agree with what DECL
2952 says. */
89b578be 2953
b655c310 2954static tree
3a9cc685 2955do_pushdecl (tree decl, bool is_friend)
89b578be 2956{
3a9cc685 2957 if (decl == error_mark_node)
b655c310 2958 return error_mark_node;
00e8de68 2959
3a9cc685
NS
2960 if (!DECL_TEMPLATE_PARM_P (decl) && current_function_decl)
2961 set_decl_context_in_fn (current_function_decl, decl);
c8094d83 2962
3a9cc685
NS
2963 /* The binding level we will be pushing into. During local class
2964 pushing, we want to push to the containing scope. */
2965 cp_binding_level *level = current_binding_level;
2966 while (level->kind == sk_class)
2967 level = level->level_chain;
00e8de68 2968
e833f686
NS
2969 /* An anonymous namespace has a NULL DECL_NAME, but we still want to
2970 insert it. Other NULL-named decls, not so much. */
2971 tree name = DECL_NAME (decl);
2972 if (name || TREE_CODE (decl) == NAMESPACE_DECL)
00e8de68 2973 {
3c9cca88 2974 cxx_binding *binding = NULL; /* Local scope binding. */
3a9cc685 2975 tree ns = NULL_TREE; /* Searched namespace. */
3c9cca88 2976 tree *slot = NULL; /* Binding slot in namespace. */
3a9cc685 2977 tree old = NULL_TREE;
00e8de68 2978
3a9cc685 2979 if (level->kind == sk_namespace)
b655c310 2980 {
3a9cc685
NS
2981 /* We look in the decl's namespace for an existing
2982 declaration, even though we push into the current
2983 namespace. */
2984 ns = (DECL_NAMESPACE_SCOPE_P (decl)
2985 ? CP_DECL_CONTEXT (decl) : current_namespace);
2986 /* Create the binding, if this is current namespace, because
2987 that's where we'll be pushing anyway. */
3c9cca88
NS
2988 slot = find_namespace_slot (ns, name, ns == current_namespace);
2989 if (slot)
2990 old = MAYBE_STAT_DECL (*slot);
b655c310 2991 }
3a9cc685 2992 else
3c9cca88
NS
2993 {
2994 binding = find_local_binding (level, name);
2995 if (binding)
2996 old = binding->value;
2997 }
00e8de68 2998
3a9cc685
NS
2999 if (current_function_decl && VAR_OR_FUNCTION_DECL_P (decl)
3000 && DECL_EXTERNAL (decl))
3001 set_local_extern_decl_linkage (decl, old != NULL_TREE);
00e8de68 3002
3a9cc685
NS
3003 if (old == error_mark_node)
3004 old = NULL_TREE;
00e8de68 3005
3a9cc685
NS
3006 for (ovl_iterator iter (old); iter; ++iter)
3007 if (iter.using_p ())
3008 ; /* Ignore using decls here. */
3009 else if (tree match = duplicate_decls (decl, *iter, is_friend))
ef4c5e78 3010 {
274c1516
NS
3011 if (match == error_mark_node)
3012 ;
3013 else if (TREE_CODE (match) == TYPE_DECL)
3014 /* The IDENTIFIER will have the type referring to the
3015 now-smashed TYPE_DECL, because ...? Reset it. */
3016 SET_IDENTIFIER_TYPE_VALUE (name, TREE_TYPE (match));
3017 else if (iter.hidden_p () && !DECL_HIDDEN_P (match))
ef4c5e78
NS
3018 {
3019 /* Unhiding a previously hidden decl. */
3020 tree head = iter.reveal_node (old);
3021 if (head != old)
3022 {
3023 if (!ns)
3c9cca88
NS
3024 {
3025 update_local_overload (binding, head);
3026 binding->value = head;
3027 }
3028 else if (STAT_HACK_P (*slot))
3029 STAT_DECL (*slot) = head;
3030 else
3031 *slot = head;
ef4c5e78 3032 }
ef4c5e78
NS
3033 if (TREE_CODE (match) == FUNCTION_DECL
3034 && DECL_EXTERN_C_P (match))
3035 /* We need to check and register the fn now. */
3036 check_extern_c_conflict (match);
3037 }
3038 return match;
3039 }
c8094d83 3040
3a9cc685 3041 /* We are pushing a new decl. */
00e8de68 3042
ef4c5e78
NS
3043 /* Skip a hidden builtin we failed to match already. There can
3044 only be one. */
3045 if (old && anticipated_builtin_p (old))
3046 old = OVL_CHAIN (old);
5294e4c3 3047
3a9cc685 3048 check_template_shadow (decl);
00e8de68 3049
3a9cc685 3050 if (DECL_DECLARES_FUNCTION_P (decl))
b655c310 3051 {
3a9cc685 3052 check_default_args (decl);
00e8de68 3053
3a9cc685 3054 if (is_friend)
b655c310 3055 {
3a9cc685
NS
3056 if (level->kind != sk_namespace)
3057 /* In a local class, a friend function declaration must
3058 find a matching decl in the innermost non-class scope.
3059 [class.friend/11] */
3060 error ("friend declaration %qD in local class without "
3061 "prior local declaration", decl);
3062 else if (!flag_friend_injection)
3063 /* Hide it from ordinary lookup. */
3064 DECL_ANTICIPATED (decl) = DECL_HIDDEN_FRIEND_P (decl) = true;
b655c310
NS
3065 }
3066 }
00e8de68 3067
3a9cc685 3068 if (level->kind != sk_namespace)
b655c310 3069 {
3a9cc685 3070 check_local_shadow (decl);
00e8de68 3071
3a9cc685
NS
3072 if (TREE_CODE (decl) == NAMESPACE_DECL)
3073 /* A local namespace alias. */
3074 set_identifier_type_value (name, NULL_TREE);
00e8de68 3075
3a9cc685
NS
3076 if (!binding)
3077 binding = create_local_binding (level, name);
b655c310 3078 }
3c9cca88 3079 else if (!slot)
b655c310 3080 {
3a9cc685 3081 ns = current_namespace;
3c9cca88 3082 slot = find_namespace_slot (ns, name, true);
be3b7dcf
NS
3083 /* Update OLD to reflect the namespace we're going to be
3084 pushing into. */
3085 old = MAYBE_STAT_DECL (*slot);
b655c310 3086 }
fdf03377 3087
3c9cca88 3088 old = update_binding (level, binding, slot, old, decl, is_friend);
fdf03377 3089
3a9cc685
NS
3090 if (old != decl)
3091 /* An existing decl matched, use it. */
3092 decl = old;
3093 else if (TREE_CODE (decl) == TYPE_DECL)
3094 {
3095 tree type = TREE_TYPE (decl);
00e8de68 3096
3a9cc685 3097 if (type != error_mark_node)
b655c310 3098 {
3a9cc685
NS
3099 if (TYPE_NAME (type) != decl)
3100 set_underlying_type (decl);
00e8de68 3101
3a9cc685
NS
3102 if (!ns)
3103 set_identifier_type_value_with_scope (name, decl, level);
b655c310 3104 else
3a9cc685 3105 SET_IDENTIFIER_TYPE_VALUE (name, global_type_node);
b655c310 3106 }
7b3b6ae4 3107
3a9cc685
NS
3108 /* If this is a locally defined typedef in a function that
3109 is not a template instantation, record it to implement
3110 -Wunused-local-typedefs. */
3111 if (!instantiating_current_function_p ())
3112 record_locally_defined_typedef (decl);
3113 }
3114 else if (VAR_P (decl))
3115 maybe_register_incomplete_var (decl);
3116 else if (TREE_CODE (decl) == FUNCTION_DECL && DECL_EXTERN_C_P (decl))
3117 check_extern_c_conflict (decl);
00e8de68 3118 }
3a9cc685
NS
3119 else
3120 add_decl_to_level (level, decl);
00e8de68 3121
3a9cc685 3122 return decl;
00e8de68
GDR
3123}
3124
4f15a5da 3125/* Record a decl-node X as belonging to the current lexical scope.
32196b87
NS
3126 It's a friend if IS_FRIEND is true -- which affects exactly where
3127 we push it. */
575bfb00
LC
3128
3129tree
4f15a5da 3130pushdecl (tree x, bool is_friend)
575bfb00 3131{
b655c310 3132 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
32196b87 3133 tree ret = do_pushdecl (x, is_friend);
b655c310 3134 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
575bfb00
LC
3135 return ret;
3136}
3137
b655c310
NS
3138/* Enter DECL into the symbol table, if that's appropriate. Returns
3139 DECL, or a modified version thereof. */
00e8de68 3140
b655c310
NS
3141tree
3142maybe_push_decl (tree decl)
00e8de68 3143{
b655c310 3144 tree type = TREE_TYPE (decl);
00e8de68 3145
b655c310
NS
3146 /* Add this decl to the current binding level, but not if it comes
3147 from another scope, e.g. a static member variable. TEM may equal
3148 DECL or it may be a previous decl of the same name. */
3149 if (decl == error_mark_node
3150 || (TREE_CODE (decl) != PARM_DECL
3151 && DECL_CONTEXT (decl) != NULL_TREE
3152 /* Definitions of namespace members outside their namespace are
3153 possible. */
3154 && !DECL_NAMESPACE_SCOPE_P (decl))
3155 || (TREE_CODE (decl) == TEMPLATE_DECL && !namespace_bindings_p ())
3156 || type == unknown_type_node
3157 /* The declaration of a template specialization does not affect
3158 the functions available for overload resolution, so we do not
3159 call pushdecl. */
3160 || (TREE_CODE (decl) == FUNCTION_DECL
3161 && DECL_TEMPLATE_SPECIALIZATION (decl)))
3162 return decl;
00e8de68 3163 else
b655c310 3164 return pushdecl (decl);
00e8de68
GDR
3165}
3166
b655c310 3167/* Bind DECL to ID in the current_binding_level, assumed to be a local
9d029ddf
NS
3168 binding level. If IS_USING is true, DECL got here through a
3169 using-declaration. */
00e8de68 3170
9d029ddf
NS
3171static void
3172push_local_binding (tree id, tree decl, bool is_using)
00e8de68 3173{
b655c310
NS
3174 /* Skip over any local classes. This makes sense if we call
3175 push_local_binding with a friend decl of a local class. */
3c9cca88 3176 cp_binding_level *b = innermost_nonclass_level ();
5ad4f1c8 3177
3c9cca88
NS
3178 gcc_assert (b->kind != sk_namespace);
3179 if (find_local_binding (b, id))
b655c310
NS
3180 {
3181 /* Supplement the existing binding. */
3182 if (!supplement_binding (IDENTIFIER_BINDING (id), decl))
3183 /* It didn't work. Something else must be bound at this
3184 level. Do not add DECL to the list of things to pop
3185 later. */
3186 return;
3187 }
3188 else
3189 /* Create a new binding. */
3190 push_binding (id, decl, b);
5a167978 3191
9d029ddf
NS
3192 if (TREE_CODE (decl) == OVERLOAD || is_using)
3193 /* We must put the OVERLOAD or using into a TREE_LIST since we
3194 cannot use the decl's chain itself. */
b655c310 3195 decl = build_tree_list (NULL_TREE, decl);
5a167978 3196
b655c310
NS
3197 /* And put DECL on the list of things declared by the current
3198 binding level. */
9d029ddf 3199 add_decl_to_level (b, decl);
5a167978
GDR
3200}
3201
b655c310
NS
3202/* Check to see whether or not DECL is a variable that would have been
3203 in scope under the ARM, but is not in scope under the ANSI/ISO
3204 standard. If so, issue an error message. If name lookup would
3205 work in both cases, but return a different result, this function
3206 returns the result of ANSI/ISO lookup. Otherwise, it returns
3207 DECL. */
5a167978 3208
b655c310
NS
3209tree
3210check_for_out_of_scope_variable (tree decl)
5a167978 3211{
b655c310 3212 tree shadowed;
c8094d83 3213
b655c310
NS
3214 /* We only care about out of scope variables. */
3215 if (!(VAR_P (decl) && DECL_DEAD_FOR_LOCAL (decl)))
3216 return decl;
420bf978 3217
b655c310
NS
3218 shadowed = DECL_HAS_SHADOWED_FOR_VAR_P (decl)
3219 ? DECL_SHADOWED_FOR_VAR (decl) : NULL_TREE ;
3220 while (shadowed != NULL_TREE && VAR_P (shadowed)
3221 && DECL_DEAD_FOR_LOCAL (shadowed))
3222 shadowed = DECL_HAS_SHADOWED_FOR_VAR_P (shadowed)
3223 ? DECL_SHADOWED_FOR_VAR (shadowed) : NULL_TREE;
3224 if (!shadowed)
3c9cca88 3225 shadowed = find_namespace_value (current_namespace, DECL_NAME (decl));
b655c310
NS
3226 if (shadowed)
3227 {
3228 if (!DECL_ERROR_REPORTED (decl))
3229 {
3230 warning (0, "name lookup of %qD changed", DECL_NAME (decl));
3231 warning_at (DECL_SOURCE_LOCATION (shadowed), 0,
3232 " matches this %qD under ISO standard rules",
3233 shadowed);
3234 warning_at (DECL_SOURCE_LOCATION (decl), 0,
3235 " matches this %qD under old rules", decl);
3236 DECL_ERROR_REPORTED (decl) = 1;
3237 }
3238 return shadowed;
3239 }
5a167978 3240
b655c310
NS
3241 /* If we have already complained about this declaration, there's no
3242 need to do it again. */
3243 if (DECL_ERROR_REPORTED (decl))
3244 return decl;
a5e6b29b 3245
b655c310 3246 DECL_ERROR_REPORTED (decl) = 1;
a5e6b29b 3247
b655c310
NS
3248 if (TREE_TYPE (decl) == error_mark_node)
3249 return decl;
a5e6b29b 3250
b655c310
NS
3251 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
3252 {
3253 error ("name lookup of %qD changed for ISO %<for%> scoping",
3254 DECL_NAME (decl));
3255 error (" cannot use obsolete binding at %q+D because "
3256 "it has a destructor", decl);
3257 return error_mark_node;
3258 }
3259 else
3260 {
3261 permerror (input_location, "name lookup of %qD changed for ISO %<for%> scoping",
3262 DECL_NAME (decl));
3263 if (flag_permissive)
3264 permerror (DECL_SOURCE_LOCATION (decl),
3265 " using obsolete binding at %qD", decl);
3266 else
3267 {
3268 static bool hint;
3269 if (!hint)
3270 {
3271 inform (input_location, "(if you use %<-fpermissive%> G++ will accept your code)");
3272 hint = true;
3273 }
3274 }
3275 }
a5e6b29b 3276
b655c310 3277 return decl;
a5e6b29b 3278}
b655c310
NS
3279\f
3280/* true means unconditionally make a BLOCK for the next level pushed. */
a5e6b29b 3281
b655c310 3282static bool keep_next_level_flag;
d5f4eddd 3283
b655c310 3284static int binding_depth = 0;
d5f4eddd 3285
b655c310
NS
3286static void
3287indent (int depth)
d5f4eddd 3288{
b655c310 3289 int i;
d5f4eddd 3290
b655c310
NS
3291 for (i = 0; i < depth * 2; i++)
3292 putc (' ', stderr);
d5f4eddd
JM
3293}
3294
b655c310
NS
3295/* Return a string describing the kind of SCOPE we have. */
3296static const char *
3297cp_binding_level_descriptor (cp_binding_level *scope)
ed3cf953 3298{
b655c310
NS
3299 /* The order of this table must match the "scope_kind"
3300 enumerators. */
3301 static const char* scope_kind_names[] = {
3302 "block-scope",
3303 "cleanup-scope",
3304 "try-scope",
3305 "catch-scope",
3306 "for-scope",
3307 "function-parameter-scope",
3308 "class-scope",
3309 "namespace-scope",
3310 "template-parameter-scope",
3311 "template-explicit-spec-scope"
3312 };
3313 const scope_kind kind = scope->explicit_spec_p
3314 ? sk_template_spec : scope->kind;
ed3cf953 3315
b655c310 3316 return scope_kind_names[kind];
ed3cf953
GDR
3317}
3318
b655c310
NS
3319/* Output a debugging information about SCOPE when performing
3320 ACTION at LINE. */
3321static void
3322cp_binding_level_debug (cp_binding_level *scope, int line, const char *action)
ed3cf953 3323{
b655c310
NS
3324 const char *desc = cp_binding_level_descriptor (scope);
3325 if (scope->this_entity)
0f2c4a8f 3326 verbatim ("%s %<%s(%E)%> %p %d\n", action, desc,
b655c310
NS
3327 scope->this_entity, (void *) scope, line);
3328 else
3329 verbatim ("%s %s %p %d\n", action, desc, (void *) scope, line);
ed3cf953
GDR
3330}
3331
b655c310
NS
3332/* Return the estimated initial size of the hashtable of a NAMESPACE
3333 scope. */
ed3cf953 3334
b655c310
NS
3335static inline size_t
3336namespace_scope_ht_size (tree ns)
ed3cf953 3337{
b655c310 3338 tree name = DECL_NAME (ns);
ed3cf953 3339
b655c310
NS
3340 return name == std_identifier
3341 ? NAMESPACE_STD_HT_SIZE
ad9870f2 3342 : (name == global_identifier
b655c310
NS
3343 ? GLOBAL_SCOPE_HT_SIZE
3344 : NAMESPACE_ORDINARY_HT_SIZE);
ed3cf953 3345}
ed3cf953 3346
b655c310 3347/* A chain of binding_level structures awaiting reuse. */
ecba6c56 3348
b655c310 3349static GTY((deletable)) cp_binding_level *free_binding_level;
ecba6c56 3350
b655c310 3351/* Insert SCOPE as the innermost binding level. */
ecba6c56 3352
b655c310
NS
3353void
3354push_binding_level (cp_binding_level *scope)
3355{
3356 /* Add it to the front of currently active scopes stack. */
3357 scope->level_chain = current_binding_level;
3358 current_binding_level = scope;
3359 keep_next_level_flag = false;
3360
3361 if (ENABLE_SCOPE_CHECKING)
ecba6c56 3362 {
b655c310
NS
3363 scope->binding_depth = binding_depth;
3364 indent (binding_depth);
3365 cp_binding_level_debug (scope, LOCATION_LINE (input_location),
3366 "push");
3367 binding_depth++;
ecba6c56 3368 }
ecba6c56
DS
3369}
3370
b655c310
NS
3371/* Create a new KIND scope and make it the top of the active scopes stack.
3372 ENTITY is the scope of the associated C++ entity (namespace, class,
3373 function, C++0x enumeration); it is NULL otherwise. */
3a636414 3374
b655c310
NS
3375cp_binding_level *
3376begin_scope (scope_kind kind, tree entity)
3a636414 3377{
b655c310 3378 cp_binding_level *scope;
3a636414 3379
b655c310
NS
3380 /* Reuse or create a struct for this binding level. */
3381 if (!ENABLE_SCOPE_CHECKING && free_binding_level)
3a636414 3382 {
b655c310
NS
3383 scope = free_binding_level;
3384 free_binding_level = scope->level_chain;
3385 memset (scope, 0, sizeof (cp_binding_level));
3a636414 3386 }
b655c310
NS
3387 else
3388 scope = ggc_cleared_alloc<cp_binding_level> ();
3a636414 3389
b655c310
NS
3390 scope->this_entity = entity;
3391 scope->more_cleanups_ok = true;
3392 switch (kind)
3393 {
3394 case sk_cleanup:
3395 scope->keep = true;
3396 break;
a5e6b29b 3397
b655c310
NS
3398 case sk_template_spec:
3399 scope->explicit_spec_p = true;
3400 kind = sk_template_parms;
3401 /* Fall through. */
3402 case sk_template_parms:
3403 case sk_block:
3404 case sk_try:
3405 case sk_catch:
3406 case sk_for:
3407 case sk_cond:
3408 case sk_class:
3409 case sk_scoped_enum:
3410 case sk_function_parms:
3411 case sk_transaction:
3412 case sk_omp:
3413 scope->keep = keep_next_level_flag;
3414 break;
a5e6b29b 3415
b655c310
NS
3416 case sk_namespace:
3417 NAMESPACE_LEVEL (entity) = scope;
a5e6b29b 3418 break;
b655c310
NS
3419
3420 default:
3421 /* Should not happen. */
3422 gcc_unreachable ();
3423 break;
3424 }
3425 scope->kind = kind;
3426
3427 push_binding_level (scope);
3428
3429 return scope;
575bfb00
LC
3430}
3431
b655c310
NS
3432/* We're about to leave current scope. Pop the top of the stack of
3433 currently active scopes. Return the enclosing scope, now active. */
575bfb00 3434
b655c310
NS
3435cp_binding_level *
3436leave_scope (void)
575bfb00 3437{
b655c310 3438 cp_binding_level *scope = current_binding_level;
a5e6b29b 3439
b655c310
NS
3440 if (scope->kind == sk_namespace && class_binding_level)
3441 current_binding_level = class_binding_level;
2b8dfc07 3442
b655c310
NS
3443 /* We cannot leave a scope, if there are none left. */
3444 if (NAMESPACE_LEVEL (global_namespace))
3445 gcc_assert (!global_scope_p (scope));
ed3cf953 3446
b655c310
NS
3447 if (ENABLE_SCOPE_CHECKING)
3448 {
3449 indent (--binding_depth);
3450 cp_binding_level_debug (scope, LOCATION_LINE (input_location),
3451 "leave");
3452 }
ed3cf953 3453
b655c310
NS
3454 /* Move one nesting level up. */
3455 current_binding_level = scope->level_chain;
3456
3457 /* Namespace-scopes are left most probably temporarily, not
3458 completely; they can be reopened later, e.g. in namespace-extension
3459 or any name binding activity that requires us to resume a
3460 namespace. For classes, we cache some binding levels. For other
3461 scopes, we just make the structure available for reuse. */
3462 if (scope->kind != sk_namespace
3463 && scope->kind != sk_class)
00e8de68 3464 {
b655c310
NS
3465 scope->level_chain = free_binding_level;
3466 gcc_assert (!ENABLE_SCOPE_CHECKING
3467 || scope->binding_depth == binding_depth);
3468 free_binding_level = scope;
00e8de68 3469 }
b655c310
NS
3470
3471 if (scope->kind == sk_class)
00e8de68 3472 {
b655c310
NS
3473 /* Reset DEFINING_CLASS_P to allow for reuse of a
3474 class-defining scope in a non-defining context. */
3475 scope->defining_class_p = 0;
3476
3477 /* Find the innermost enclosing class scope, and reset
3478 CLASS_BINDING_LEVEL appropriately. */
3479 class_binding_level = NULL;
3480 for (scope = current_binding_level; scope; scope = scope->level_chain)
3481 if (scope->kind == sk_class)
3482 {
3483 class_binding_level = scope;
3484 break;
3485 }
00e8de68 3486 }
b655c310
NS
3487
3488 return current_binding_level;
00e8de68 3489}
575bfb00 3490
b655c310
NS
3491static void
3492resume_scope (cp_binding_level* b)
575bfb00 3493{
b655c310
NS
3494 /* Resuming binding levels is meant only for namespaces,
3495 and those cannot nest into classes. */
3496 gcc_assert (!class_binding_level);
3497 /* Also, resuming a non-directly nested namespace is a no-no. */
3498 gcc_assert (b->level_chain == current_binding_level);
3499 current_binding_level = b;
3500 if (ENABLE_SCOPE_CHECKING)
3501 {
3502 b->binding_depth = binding_depth;
3503 indent (binding_depth);
3504 cp_binding_level_debug (b, LOCATION_LINE (input_location), "resume");
3505 binding_depth++;
3506 }
575bfb00
LC
3507}
3508
b655c310 3509/* Return the innermost binding level that is not for a class scope. */
fde6f97e 3510
b655c310
NS
3511static cp_binding_level *
3512innermost_nonclass_level (void)
fde6f97e 3513{
b655c310 3514 cp_binding_level *b;
fde6f97e 3515
b655c310
NS
3516 b = current_binding_level;
3517 while (b->kind == sk_class)
3518 b = b->level_chain;
fde6f97e 3519
b655c310 3520 return b;
fde6f97e 3521}
00e8de68 3522
b655c310
NS
3523/* We're defining an object of type TYPE. If it needs a cleanup, but
3524 we're not allowed to add any more objects with cleanups to the current
3525 scope, create a new binding level. */
a5e6b29b 3526
b655c310
NS
3527void
3528maybe_push_cleanup_level (tree type)
a5e6b29b 3529{
b655c310
NS
3530 if (type != error_mark_node
3531 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
3532 && current_binding_level->more_cleanups_ok == 0)
a5e6b29b 3533 {
b655c310
NS
3534 begin_scope (sk_cleanup, NULL);
3535 current_binding_level->statement_list = push_stmt_list ();
3536 }
3537}
a5e6b29b 3538
b655c310 3539/* Return true if we are in the global binding level. */
a5e6b29b 3540
b655c310
NS
3541bool
3542global_bindings_p (void)
3543{
3544 return global_scope_p (current_binding_level);
3545}
a5e6b29b 3546
b655c310
NS
3547/* True if we are currently in a toplevel binding level. This
3548 means either the global binding level or a namespace in a toplevel
3549 binding level. Since there are no non-toplevel namespace levels,
3550 this really means any namespace or template parameter level. We
3551 also include a class whose context is toplevel. */
1a32490a 3552
b655c310
NS
3553bool
3554toplevel_bindings_p (void)
3555{
3556 cp_binding_level *b = innermost_nonclass_level ();
a5e6b29b 3557
b655c310
NS
3558 return b->kind == sk_namespace || b->kind == sk_template_parms;
3559}
a5e6b29b 3560
b655c310
NS
3561/* True if this is a namespace scope, or if we are defining a class
3562 which is itself at namespace scope, or whose enclosing class is
3563 such a class, etc. */
a5e6b29b 3564
b655c310
NS
3565bool
3566namespace_bindings_p (void)
3567{
3568 cp_binding_level *b = innermost_nonclass_level ();
a5e6b29b 3569
b655c310
NS
3570 return b->kind == sk_namespace;
3571}
a5e6b29b 3572
b655c310 3573/* True if the innermost non-class scope is a block scope. */
a5e6b29b 3574
b655c310
NS
3575bool
3576local_bindings_p (void)
3577{
3578 cp_binding_level *b = innermost_nonclass_level ();
3579 return b->kind < sk_function_parms || b->kind == sk_omp;
3580}
a5e6b29b 3581
b655c310 3582/* True if the current level needs to have a BLOCK made. */
a5e6b29b 3583
b655c310
NS
3584bool
3585kept_level_p (void)
3586{
3587 return (current_binding_level->blocks != NULL_TREE
3588 || current_binding_level->keep
3589 || current_binding_level->kind == sk_cleanup
3590 || current_binding_level->names != NULL_TREE
3591 || current_binding_level->using_directives);
575bfb00
LC
3592}
3593
b655c310 3594/* Returns the kind of the innermost scope. */
575bfb00 3595
b655c310
NS
3596scope_kind
3597innermost_scope_kind (void)
575bfb00 3598{
b655c310 3599 return current_binding_level->kind;
a5e6b29b
GDR
3600}
3601
b655c310 3602/* Returns true if this scope was created to store template parameters. */
5a167978 3603
b655c310
NS
3604bool
3605template_parm_scope_p (void)
5a167978 3606{
b655c310
NS
3607 return innermost_scope_kind () == sk_template_parms;
3608}
7756db03 3609
b655c310
NS
3610/* If KEEP is true, make a BLOCK node for the next binding level,
3611 unconditionally. Otherwise, use the normal logic to decide whether
3612 or not to create a BLOCK. */
5a167978 3613
b655c310
NS
3614void
3615keep_next_level (bool keep)
3616{
3617 keep_next_level_flag = keep;
3618}
5a167978 3619
9c82d7b6 3620/* Return the list of declarations of the current local scope. */
5a167978 3621
b655c310 3622tree
9c82d7b6 3623get_local_decls (void)
b655c310 3624{
9c82d7b6
NS
3625 gcc_assert (current_binding_level->kind != sk_namespace
3626 && current_binding_level->kind != sk_class);
b655c310
NS
3627 return current_binding_level->names;
3628}
5a167978 3629
b655c310 3630/* Return how many function prototypes we are currently nested inside. */
5a167978 3631
b655c310
NS
3632int
3633function_parm_depth (void)
3634{
3635 int level = 0;
3636 cp_binding_level *b;
8597cab1 3637
b655c310
NS
3638 for (b = current_binding_level;
3639 b->kind == sk_function_parms;
3640 b = b->level_chain)
3641 ++level;
3642
3643 return level;
5a167978
GDR
3644}
3645
b655c310
NS
3646/* For debugging. */
3647static int no_print_functions = 0;
3648static int no_print_builtins = 0;
5a167978
GDR
3649
3650static void
b655c310 3651print_binding_level (cp_binding_level* lvl)
5a167978 3652{
b655c310
NS
3653 tree t;
3654 int i = 0, len;
3655 fprintf (stderr, " blocks=%p", (void *) lvl->blocks);
3656 if (lvl->more_cleanups_ok)
3657 fprintf (stderr, " more-cleanups-ok");
3658 if (lvl->have_cleanups)
3659 fprintf (stderr, " have-cleanups");
3660 fprintf (stderr, "\n");
3661 if (lvl->names)
3662 {
3663 fprintf (stderr, " names:\t");
3664 /* We can probably fit 3 names to a line? */
3665 for (t = lvl->names; t; t = TREE_CHAIN (t))
3666 {
3667 if (no_print_functions && (TREE_CODE (t) == FUNCTION_DECL))
3668 continue;
3669 if (no_print_builtins
3670 && (TREE_CODE (t) == TYPE_DECL)
3671 && DECL_IS_BUILTIN (t))
3672 continue;
5a167978 3673
b655c310
NS
3674 /* Function decls tend to have longer names. */
3675 if (TREE_CODE (t) == FUNCTION_DECL)
3676 len = 3;
3677 else
3678 len = 2;
3679 i += len;
3680 if (i > 6)
3681 {
3682 fprintf (stderr, "\n\t");
3683 i = len;
3684 }
3685 print_node_brief (stderr, "", t, 0);
3686 if (t == error_mark_node)
3687 break;
3688 }
3689 if (i)
3690 fprintf (stderr, "\n");
3691 }
3692 if (vec_safe_length (lvl->class_shadowed))
5a167978 3693 {
b655c310
NS
3694 size_t i;
3695 cp_class_binding *b;
3696 fprintf (stderr, " class-shadowed:");
3697 FOR_EACH_VEC_ELT (*lvl->class_shadowed, i, b)
3698 fprintf (stderr, " %s ", IDENTIFIER_POINTER (b->identifier));
3699 fprintf (stderr, "\n");
5a167978 3700 }
b655c310 3701 if (lvl->type_shadowed)
44fd0e80 3702 {
b655c310
NS
3703 fprintf (stderr, " type-shadowed:");
3704 for (t = lvl->type_shadowed; t; t = TREE_CHAIN (t))
3705 {
3706 fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
3707 }
3708 fprintf (stderr, "\n");
44fd0e80 3709 }
b655c310 3710}
44fd0e80 3711
b655c310
NS
3712DEBUG_FUNCTION void
3713debug (cp_binding_level &ref)
3714{
3715 print_binding_level (&ref);
3716}
3717
3718DEBUG_FUNCTION void
3719debug (cp_binding_level *ptr)
3720{
3721 if (ptr)
3722 debug (*ptr);
3723 else
3724 fprintf (stderr, "<nil>\n");
3725}
3726
3727
3728void
3729print_other_binding_stack (cp_binding_level *stack)
3730{
3731 cp_binding_level *level;
3732 for (level = stack; !global_scope_p (level); level = level->level_chain)
44fd0e80 3733 {
b655c310
NS
3734 fprintf (stderr, "binding level %p\n", (void *) level);
3735 print_binding_level (level);
44fd0e80 3736 }
b655c310 3737}
44fd0e80 3738
b655c310
NS
3739void
3740print_binding_stack (void)
3741{
3742 cp_binding_level *b;
3743 fprintf (stderr, "current_binding_level=%p\n"
3744 "class_binding_level=%p\n"
3745 "NAMESPACE_LEVEL (global_namespace)=%p\n",
3746 (void *) current_binding_level, (void *) class_binding_level,
3747 (void *) NAMESPACE_LEVEL (global_namespace));
3748 if (class_binding_level)
5a167978 3749 {
b655c310
NS
3750 for (b = class_binding_level; b; b = b->level_chain)
3751 if (b == current_binding_level)
3752 break;
3753 if (b)
3754 b = class_binding_level;
3755 else
3756 b = current_binding_level;
3757 }
3758 else
3759 b = current_binding_level;
3760 print_other_binding_stack (b);
3761 fprintf (stderr, "global:\n");
3762 print_binding_level (NAMESPACE_LEVEL (global_namespace));
3763}
3764\f
3765/* Return the type associated with ID. */
5a167978 3766
b655c310
NS
3767static tree
3768identifier_type_value_1 (tree id)
3769{
3770 /* There is no type with that name, anywhere. */
3771 if (REAL_IDENTIFIER_TYPE_VALUE (id) == NULL_TREE)
3772 return NULL_TREE;
3773 /* This is not the type marker, but the real thing. */
3774 if (REAL_IDENTIFIER_TYPE_VALUE (id) != global_type_node)
3775 return REAL_IDENTIFIER_TYPE_VALUE (id);
3776 /* Have to search for it. It must be on the global level, now.
3777 Ask lookup_name not to return non-types. */
3778 id = lookup_name_real (id, 2, 1, /*block_p=*/true, 0, 0);
3779 if (id)
3780 return TREE_TYPE (id);
3781 return NULL_TREE;
3782}
44fd0e80 3783
b655c310 3784/* Wrapper for identifier_type_value_1. */
1374a761 3785
b655c310
NS
3786tree
3787identifier_type_value (tree id)
3788{
3789 tree ret;
3790 timevar_start (TV_NAME_LOOKUP);
3791 ret = identifier_type_value_1 (id);
3792 timevar_stop (TV_NAME_LOOKUP);
3793 return ret;
3794}
44fd0e80 3795
b655c310
NS
3796/* Push a definition of struct, union or enum tag named ID. into
3797 binding_level B. DECL is a TYPE_DECL for the type. We assume that
3798 the tag ID is not already defined. */
1374a761 3799
b655c310
NS
3800static void
3801set_identifier_type_value_with_scope (tree id, tree decl, cp_binding_level *b)
3802{
3803 tree type;
5a167978 3804
b655c310 3805 if (b->kind != sk_namespace)
5a167978 3806 {
b655c310
NS
3807 /* Shadow the marker, not the real thing, so that the marker
3808 gets restored later. */
3809 tree old_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
3810 b->type_shadowed
3811 = tree_cons (id, old_type_value, b->type_shadowed);
3812 type = decl ? TREE_TYPE (decl) : NULL_TREE;
3813 TREE_TYPE (b->type_shadowed) = type;
19831e2b
OW
3814 }
3815 else
3816 {
3c9cca88
NS
3817 tree *slot = find_namespace_slot (current_namespace, id, true);
3818 gcc_assert (decl);
3819 update_binding (b, NULL, slot, MAYBE_STAT_DECL (*slot), decl, false);
44fd0e80 3820
b655c310
NS
3821 /* Store marker instead of real type. */
3822 type = global_type_node;
3823 }
3824 SET_IDENTIFIER_TYPE_VALUE (id, type);
5a167978
GDR
3825}
3826
b655c310
NS
3827/* As set_identifier_type_value_with_scope, but using
3828 current_binding_level. */
5a167978
GDR
3829
3830void
b655c310 3831set_identifier_type_value (tree id, tree decl)
5a167978 3832{
b655c310
NS
3833 set_identifier_type_value_with_scope (id, decl, current_binding_level);
3834}
5a167978 3835
b655c310 3836/* Return the name for the constructor (or destructor) for the
5fee5eca 3837 specified class. */
5a167978 3838
b655c310
NS
3839tree
3840constructor_name (tree type)
3841{
56b2a94b
NS
3842 tree decl = TYPE_NAME (TYPE_MAIN_VARIANT (type));
3843
3844 return decl ? DECL_NAME (decl) : NULL_TREE;
b655c310 3845}
5a167978 3846
b655c310
NS
3847/* Returns TRUE if NAME is the name for the constructor for TYPE,
3848 which must be a class type. */
5a167978 3849
b655c310
NS
3850bool
3851constructor_name_p (tree name, tree type)
3852{
b655c310
NS
3853 gcc_assert (MAYBE_CLASS_TYPE_P (type));
3854
b655c310
NS
3855 /* These don't have names. */
3856 if (TREE_CODE (type) == DECLTYPE_TYPE
3857 || TREE_CODE (type) == TYPEOF_TYPE)
3858 return false;
3859
56b2a94b 3860 if (name && name == constructor_name (type))
b655c310 3861 return true;
5fee5eca 3862
b655c310 3863 return false;
5a167978
GDR
3864}
3865
b655c310 3866/* Counter used to create anonymous type names. */
5a167978 3867
b655c310
NS
3868static GTY(()) int anon_cnt;
3869
3870/* Return an IDENTIFIER which can be used as a name for
3871 unnamed structs and unions. */
3872
3873tree
3874make_anon_name (void)
5a167978 3875{
b655c310
NS
3876 char buf[32];
3877
3878 sprintf (buf, anon_aggrname_format (), anon_cnt++);
3879 return get_identifier (buf);
3880}
3881
3882/* This code is practically identical to that for creating
3883 anonymous names, but is just used for lambdas instead. This isn't really
3884 necessary, but it's convenient to avoid treating lambdas like other
3885 unnamed types. */
3886
3887static GTY(()) int lambda_cnt = 0;
3888
3889tree
3890make_lambda_name (void)
3891{
3892 char buf[32];
3893
3894 sprintf (buf, LAMBDANAME_FORMAT, lambda_cnt++);
3895 return get_identifier (buf);
3896}
c8094d83 3897
b655c310
NS
3898/* Insert another USING_DECL into the current binding level, returning
3899 this declaration. If this is a redeclaration, do nothing, and
3900 return NULL_TREE if this not in namespace scope (in namespace
3901 scope, a using decl might extend any previous bindings). */
87c465f5 3902
b655c310
NS
3903static tree
3904push_using_decl_1 (tree scope, tree name)
87c465f5 3905{
b655c310 3906 tree decl;
87c465f5 3907
b655c310
NS
3908 gcc_assert (TREE_CODE (scope) == NAMESPACE_DECL);
3909 gcc_assert (identifier_p (name));
3910 for (decl = current_binding_level->usings; decl; decl = DECL_CHAIN (decl))
3911 if (USING_DECL_SCOPE (decl) == scope && DECL_NAME (decl) == name)
3912 break;
3913 if (decl)
3914 return namespace_bindings_p () ? decl : NULL_TREE;
3915 decl = build_lang_decl (USING_DECL, name, NULL_TREE);
3916 USING_DECL_SCOPE (decl) = scope;
3917 DECL_CHAIN (decl) = current_binding_level->usings;
3918 current_binding_level->usings = decl;
3919 return decl;
87c465f5
KL
3920}
3921
b655c310 3922/* Wrapper for push_using_decl_1. */
87c465f5 3923
b655c310
NS
3924static tree
3925push_using_decl (tree scope, tree name)
87c465f5 3926{
b655c310
NS
3927 tree ret;
3928 timevar_start (TV_NAME_LOOKUP);
3929 ret = push_using_decl_1 (scope, name);
3930 timevar_stop (TV_NAME_LOOKUP);
3931 return ret;
3932}
87c465f5 3933
b655c310
NS
3934/* Same as pushdecl, but define X in binding-level LEVEL. We rely on the
3935 caller to set DECL_CONTEXT properly.
87c465f5 3936
b655c310
NS
3937 Note that this must only be used when X will be the new innermost
3938 binding for its name, as we tack it onto the front of IDENTIFIER_BINDING
3939 without checking to see if the current IDENTIFIER_BINDING comes from a
3940 closer binding level than LEVEL. */
87c465f5 3941
b655c310 3942static tree
5256a7f5 3943do_pushdecl_with_scope (tree x, cp_binding_level *level, bool is_friend)
b655c310
NS
3944{
3945 cp_binding_level *b;
3946 tree function_decl = current_function_decl;
87c465f5 3947
b655c310
NS
3948 current_function_decl = NULL_TREE;
3949 if (level->kind == sk_class)
3950 {
3951 b = class_binding_level;
3952 class_binding_level = level;
3953 pushdecl_class_level (x);
3954 class_binding_level = b;
3955 }
3956 else
3957 {
3958 b = current_binding_level;
3959 current_binding_level = level;
4f15a5da 3960 x = pushdecl (x, is_friend);
b655c310 3961 current_binding_level = b;
87c465f5 3962 }
b655c310
NS
3963 current_function_decl = function_decl;
3964 return x;
87c465f5 3965}
9e931c2a 3966
d16d5eac 3967/* Inject X into the local scope just before the function parms. */
00e8de68 3968
b655c310 3969tree
d16d5eac 3970pushdecl_outermost_localscope (tree x)
00e8de68 3971{
c8634a1a 3972 cp_binding_level *b = NULL;
b655c310 3973 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
d16d5eac 3974
c8634a1a
NS
3975 /* Find the scope just inside the function parms. */
3976 for (cp_binding_level *n = current_binding_level;
3977 n->kind != sk_function_parms; n = b->level_chain)
3978 b = n;
3979
5256a7f5 3980 tree ret = b ? do_pushdecl_with_scope (x, b, false) : error_mark_node;
b655c310 3981 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
c8634a1a 3982
b655c310 3983 return ret;
00e8de68
GDR
3984}
3985
b655c310
NS
3986/* Check a non-member using-declaration. Return the name and scope
3987 being used, and the USING_DECL, or NULL_TREE on failure. */
d2f2c87b 3988
b655c310
NS
3989static tree
3990validate_nonmember_using_decl (tree decl, tree scope, tree name)
3991{
3992 /* [namespace.udecl]
3993 A using-declaration for a class member shall be a
3994 member-declaration. */
3995 if (TYPE_P (scope))
90ea9897 3996 {
b655c310
NS
3997 error ("%qT is not a namespace or unscoped enum", scope);
3998 return NULL_TREE;
d2f2c87b 3999 }
b655c310
NS
4000 else if (scope == error_mark_node)
4001 return NULL_TREE;
d2f2c87b 4002
b655c310 4003 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR)
90ea9897 4004 {
b655c310
NS
4005 /* 7.3.3/5
4006 A using-declaration shall not name a template-id. */
4007 error ("a using-declaration cannot specify a template-id. "
4008 "Try %<using %D%>", name);
4009 return NULL_TREE;
90ea9897
MM
4010 }
4011
b655c310 4012 if (TREE_CODE (decl) == NAMESPACE_DECL)
00e8de68 4013 {
b655c310
NS
4014 error ("namespace %qD not allowed in using-declaration", decl);
4015 return NULL_TREE;
00e8de68
GDR
4016 }
4017
b655c310 4018 if (TREE_CODE (decl) == SCOPE_REF)
90ea9897 4019 {
b655c310
NS
4020 /* It's a nested name with template parameter dependent scope.
4021 This can only be using-declaration for class member. */
4022 error ("%qT is not a namespace", TREE_OPERAND (decl, 0));
4023 return NULL_TREE;
90ea9897 4024 }
00e8de68 4025
9d029ddf 4026 decl = OVL_FIRST (decl);
575bfb00 4027
b655c310
NS
4028 /* Make a USING_DECL. */
4029 tree using_decl = push_using_decl (scope, name);
575bfb00 4030
b655c310
NS
4031 if (using_decl == NULL_TREE
4032 && at_function_scope_p ()
4033 && VAR_P (decl))
4034 /* C++11 7.3.3/10. */
4035 error ("%qD is already declared in this scope", name);
4036
4037 return using_decl;
00e8de68
GDR
4038}
4039
9d029ddf
NS
4040/* Process a local-scope or namespace-scope using declaration. SCOPE
4041 is the nominated scope to search for NAME. VALUE_P and TYPE_P
4042 point to the binding for NAME in the current scope and are
4043 updated. */
1d786913 4044
b655c310 4045static void
9d029ddf 4046do_nonmember_using_decl (tree scope, tree name, tree *value_p, tree *type_p)
5a167978 4047{
9dda0ace 4048 name_lookup lookup (name, 0);
c8094d83 4049
9dda0ace 4050 if (!qualified_namespace_lookup (scope, &lookup))
5a167978 4051 {
b655c310
NS
4052 error ("%qD not declared", name);
4053 return;
5a167978 4054 }
9d029ddf
NS
4055 else if (TREE_CODE (lookup.value) == TREE_LIST)
4056 {
4057 error ("reference to %qD is ambiguous", name);
4058 print_candidates (lookup.value);
4059 lookup.value = NULL_TREE;
4060 }
4061
4062 if (lookup.type && TREE_CODE (lookup.type) == TREE_LIST)
4063 {
4064 error ("reference to %qD is ambiguous", name);
4065 print_candidates (lookup.type);
4066 lookup.type = NULL_TREE;
4067 }
4068
4069 tree value = *value_p;
4070 tree type = *type_p;
98ed9dae 4071
b655c310
NS
4072 /* Shift the old and new bindings around so we're comparing class and
4073 enumeration names to each other. */
9d029ddf 4074 if (value && DECL_IMPLICIT_TYPEDEF_P (value))
5a167978 4075 {
9d029ddf
NS
4076 type = value;
4077 value = NULL_TREE;
98ed9dae 4078 }
b655c310 4079
9d029ddf 4080 if (lookup.value && DECL_IMPLICIT_TYPEDEF_P (lookup.value))
140bec21 4081 {
9d029ddf
NS
4082 lookup.type = lookup.value;
4083 lookup.value = NULL_TREE;
140bec21 4084 }
b655c310 4085
9d029ddf 4086 if (lookup.value && lookup.value != value)
98ed9dae 4087 {
b655c310 4088 /* Check for using functions. */
9d029ddf 4089 if (OVL_P (lookup.value) && (!value || OVL_P (value)))
b655c310 4090 {
9d029ddf 4091 for (lkp_iterator usings (lookup.value); usings; ++usings)
b655c310 4092 {
9d029ddf 4093 tree new_fn = *usings;
577b02d8 4094
b655c310 4095 /* [namespace.udecl]
c8094d83 4096
b655c310
NS
4097 If a function declaration in namespace scope or block
4098 scope has the same name and the same parameter types as a
4099 function introduced by a using declaration the program is
4100 ill-formed. */
9d029ddf
NS
4101 bool found = false;
4102 for (ovl_iterator old (value); !found && old; ++old)
b655c310 4103 {
9d029ddf 4104 tree old_fn = *old;
3db45ab5 4105
b655c310 4106 if (new_fn == old_fn)
9d029ddf
NS
4107 /* The function already exists in the current
4108 namespace. */
4109 found = true;
4110 else if (old.using_p ())
4111 continue; /* This is a using decl. */
ef4c5e78 4112 else if (old.hidden_p () && !DECL_HIDDEN_FRIEND_P (old_fn))
9d029ddf
NS
4113 continue; /* This is an anticipated builtin. */
4114 else if (!matching_fn_p (new_fn, old_fn))
4115 continue; /* Parameters do not match. */
4116 else if (decls_match (new_fn, old_fn))
4117 found = true;
4118 else
b655c310 4119 {
9d029ddf
NS
4120 diagnose_name_conflict (new_fn, old_fn);
4121 found = true;
b655c310
NS
4122 }
4123 }
1e9f5818 4124
9d029ddf
NS
4125 if (!found)
4126 /* Unlike the overload case we don't drop anticipated
4127 builtins here. They don't cause a problem, and
4128 we'd like to match them with a future
4129 declaration. */
4130 value = ovl_insert (new_fn, value, true);
b01e6d2b 4131 }
98ed9dae 4132 }
9d029ddf
NS
4133 else if (value
4134 /* Ignore anticipated builtins. */
ef4c5e78 4135 && !anticipated_builtin_p (value)
9d029ddf
NS
4136 && !decls_match (lookup.value, value))
4137 diagnose_name_conflict (lookup.value, value);
b655c310 4138 else
9d029ddf 4139 value = lookup.value;
b655c310
NS
4140 }
4141
9d029ddf 4142 if (lookup.type && lookup.type != type)
b655c310 4143 {
9d029ddf
NS
4144 if (type && !decls_match (lookup.type, type))
4145 diagnose_name_conflict (lookup.type, type);
b655c310 4146 else
9d029ddf 4147 type = lookup.type;
b655c310 4148 }
9d029ddf
NS
4149
4150 /* If bind->value is empty, shift any class or enumeration name back. */
4151 if (!value)
b655c310 4152 {
9d029ddf
NS
4153 value = type;
4154 type = NULL_TREE;
1e9f5818 4155 }
98ed9dae 4156
9d029ddf
NS
4157 *value_p = value;
4158 *type_p = type;
5a167978
GDR
4159}
4160
322763f5
NS
4161/* Returns true if ANCESTOR encloses DESCENDANT, including matching.
4162 Both are namespaces. */
4163
4164bool
4165is_nested_namespace (tree ancestor, tree descendant, bool inline_only)
4166{
4167 int depth = SCOPE_DEPTH (ancestor);
4168
4169 if (!depth && !inline_only)
4170 /* The global namespace encloses everything. */
4171 return true;
4172
4173 while (SCOPE_DEPTH (descendant) > depth
4174 && (!inline_only || DECL_NAMESPACE_INLINE_P (descendant)))
4175 descendant = CP_DECL_CONTEXT (descendant);
4176
4177 return ancestor == descendant;
4178}
4179
b655c310
NS
4180/* Returns true if ROOT (a namespace, class, or function) encloses
4181 CHILD. CHILD may be either a class type or a namespace. */
575bfb00 4182
b655c310
NS
4183bool
4184is_ancestor (tree root, tree child)
00e8de68 4185{
b655c310
NS
4186 gcc_assert ((TREE_CODE (root) == NAMESPACE_DECL
4187 || TREE_CODE (root) == FUNCTION_DECL
4188 || CLASS_TYPE_P (root)));
4189 gcc_assert ((TREE_CODE (child) == NAMESPACE_DECL
4190 || CLASS_TYPE_P (child)));
00e8de68 4191
b655c310
NS
4192 /* The global namespace encloses everything. */
4193 if (root == global_namespace)
4194 return true;
00e8de68 4195
322763f5
NS
4196 /* Search until we reach namespace scope. */
4197 while (TREE_CODE (child) != NAMESPACE_DECL)
b655c310 4198 {
b655c310
NS
4199 /* If we've reached the ROOT, it encloses CHILD. */
4200 if (root == child)
4201 return true;
4202 /* Go out one level. */
4203 if (TYPE_P (child))
4204 child = TYPE_NAME (child);
322763f5 4205 child = CP_DECL_CONTEXT (child);
b655c310 4206 }
322763f5
NS
4207
4208 if (TREE_CODE (root) == NAMESPACE_DECL)
4209 return is_nested_namespace (root, child);
4210
4211 return false;
575bfb00
LC
4212}
4213
b655c310
NS
4214/* Enter the class or namespace scope indicated by T suitable for name
4215 lookup. T can be arbitrary scope, not necessary nested inside the
4216 current scope. Returns a non-null scope to pop iff pop_scope
4217 should be called later to exit this scope. */
ed3cf953 4218
b655c310
NS
4219tree
4220push_scope (tree t)
ed3cf953 4221{
b655c310
NS
4222 if (TREE_CODE (t) == NAMESPACE_DECL)
4223 push_decl_namespace (t);
4224 else if (CLASS_TYPE_P (t))
4225 {
4226 if (!at_class_scope_p ()
4227 || !same_type_p (current_class_type, t))
4228 push_nested_class (t);
4229 else
4230 /* T is the same as the current scope. There is therefore no
4231 need to re-enter the scope. Since we are not actually
4232 pushing a new scope, our caller should not call
4233 pop_scope. */
4234 t = NULL_TREE;
4235 }
ed3cf953 4236
b655c310 4237 return t;
575bfb00
LC
4238}
4239
b655c310 4240/* Leave scope pushed by push_scope. */
575bfb00
LC
4241
4242void
b655c310 4243pop_scope (tree t)
575bfb00 4244{
b655c310
NS
4245 if (t == NULL_TREE)
4246 return;
4247 if (TREE_CODE (t) == NAMESPACE_DECL)
4248 pop_decl_namespace ();
4249 else if CLASS_TYPE_P (t)
4250 pop_nested_class ();
ed3cf953
GDR
4251}
4252
b655c310 4253/* Subroutine of push_inner_scope. */
5a167978 4254
b655c310
NS
4255static void
4256push_inner_scope_r (tree outer, tree inner)
5a167978 4257{
b655c310 4258 tree prev;
5ae9ba3e 4259
b655c310
NS
4260 if (outer == inner
4261 || (TREE_CODE (inner) != NAMESPACE_DECL && !CLASS_TYPE_P (inner)))
abc088aa 4262 return;
b655c310
NS
4263
4264 prev = CP_DECL_CONTEXT (TREE_CODE (inner) == NAMESPACE_DECL ? inner : TYPE_NAME (inner));
4265 if (outer != prev)
4266 push_inner_scope_r (outer, prev);
4267 if (TREE_CODE (inner) == NAMESPACE_DECL)
5ae9ba3e 4268 {
b655c310
NS
4269 cp_binding_level *save_template_parm = 0;
4270 /* Temporary take out template parameter scopes. They are saved
4271 in reversed order in save_template_parm. */
4272 while (current_binding_level->kind == sk_template_parms)
160594b0 4273 {
b655c310
NS
4274 cp_binding_level *b = current_binding_level;
4275 current_binding_level = b->level_chain;
4276 b->level_chain = save_template_parm;
4277 save_template_parm = b;
160594b0 4278 }
b655c310
NS
4279
4280 resume_scope (NAMESPACE_LEVEL (inner));
4281 current_namespace = inner;
4282
4283 /* Restore template parameter scopes. */
4284 while (save_template_parm)
160594b0 4285 {
b655c310
NS
4286 cp_binding_level *b = save_template_parm;
4287 save_template_parm = b->level_chain;
4288 b->level_chain = current_binding_level;
4289 current_binding_level = b;
160594b0 4290 }
5ae9ba3e 4291 }
b655c310
NS
4292 else
4293 pushclass (inner);
c8094d83 4294}
5a167978 4295
b655c310
NS
4296/* Enter the scope INNER from current scope. INNER must be a scope
4297 nested inside current scope. This works with both name lookup and
4298 pushing name into scope. In case a template parameter scope is present,
4299 namespace is pushed under the template parameter scope according to
4300 name lookup rule in 14.6.1/6.
4301
4302 Return the former current scope suitable for pop_inner_scope. */
5a167978 4303
ae099258 4304tree
b655c310 4305push_inner_scope (tree inner)
5a167978 4306{
b655c310
NS
4307 tree outer = current_scope ();
4308 if (!outer)
4309 outer = current_namespace;
5a167978 4310
b655c310
NS
4311 push_inner_scope_r (outer, inner);
4312 return outer;
5a167978
GDR
4313}
4314
b655c310 4315/* Exit the current scope INNER back to scope OUTER. */
00e8de68 4316
b655c310
NS
4317void
4318pop_inner_scope (tree outer, tree inner)
0ed5edac 4319{
b655c310
NS
4320 if (outer == inner
4321 || (TREE_CODE (inner) != NAMESPACE_DECL && !CLASS_TYPE_P (inner)))
4322 return;
0ed5edac 4323
b655c310 4324 while (outer != inner)
65567efa 4325 {
b655c310 4326 if (TREE_CODE (inner) == NAMESPACE_DECL)
65567efa 4327 {
b655c310
NS
4328 cp_binding_level *save_template_parm = 0;
4329 /* Temporary take out template parameter scopes. They are saved
4330 in reversed order in save_template_parm. */
4331 while (current_binding_level->kind == sk_template_parms)
65567efa 4332 {
b655c310
NS
4333 cp_binding_level *b = current_binding_level;
4334 current_binding_level = b->level_chain;
4335 b->level_chain = save_template_parm;
4336 save_template_parm = b;
65567efa
JM
4337 }
4338
b655c310 4339 pop_namespace ();
65567efa 4340
b655c310
NS
4341 /* Restore template parameter scopes. */
4342 while (save_template_parm)
7cb73573 4343 {
b655c310
NS
4344 cp_binding_level *b = save_template_parm;
4345 save_template_parm = b->level_chain;
4346 b->level_chain = current_binding_level;
4347 current_binding_level = b;
7cb73573 4348 }
e3501bab 4349 }
65567efa 4350 else
b655c310
NS
4351 popclass ();
4352
4353 inner = CP_DECL_CONTEXT (TREE_CODE (inner) == NAMESPACE_DECL ? inner : TYPE_NAME (inner));
65567efa 4354 }
b655c310
NS
4355}
4356\f
4357/* Do a pushlevel for class declarations. */
65567efa 4358
b655c310
NS
4359void
4360pushlevel_class (void)
4361{
4362 class_binding_level = begin_scope (sk_class, current_class_type);
65567efa 4363}
0ed5edac 4364
b655c310
NS
4365/* ...and a poplevel for class declarations. */
4366
4367void
4368poplevel_class (void)
00e8de68 4369{
b655c310
NS
4370 cp_binding_level *level = class_binding_level;
4371 cp_class_binding *cb;
4372 size_t i;
4373 tree shadowed;
00e8de68 4374
575bfb00 4375 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
b655c310 4376 gcc_assert (level != 0);
c8094d83 4377
b655c310
NS
4378 /* If we're leaving a toplevel class, cache its binding level. */
4379 if (current_class_depth == 1)
4380 previous_class_level = level;
4381 for (shadowed = level->type_shadowed;
4382 shadowed;
4383 shadowed = TREE_CHAIN (shadowed))
4384 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (shadowed), TREE_VALUE (shadowed));
00e8de68 4385
b655c310
NS
4386 /* Remove the bindings for all of the class-level declarations. */
4387 if (level->class_shadowed)
00e8de68 4388 {
b655c310 4389 FOR_EACH_VEC_ELT (*level->class_shadowed, i, cb)
0cbd7506 4390 {
b655c310
NS
4391 IDENTIFIER_BINDING (cb->identifier) = cb->base->previous;
4392 cxx_binding_free (cb->base);
0cbd7506 4393 }
b655c310
NS
4394 ggc_free (level->class_shadowed);
4395 level->class_shadowed = NULL;
00e8de68
GDR
4396 }
4397
b655c310
NS
4398 /* Now, pop out of the binding level which we created up in the
4399 `pushlevel_class' routine. */
4400 gcc_assert (current_binding_level == level);
4401 leave_scope ();
4402 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4403}
4404
4405/* Set INHERITED_VALUE_BINDING_P on BINDING to true or false, as
4406 appropriate. DECL is the value to which a name has just been
4407 bound. CLASS_TYPE is the class in which the lookup occurred. */
4408
4409static void
4410set_inherited_value_binding_p (cxx_binding *binding, tree decl,
4411 tree class_type)
4412{
4413 if (binding->value == decl && TREE_CODE (decl) != TREE_LIST)
00e8de68 4414 {
b655c310
NS
4415 tree context;
4416
4417 if (TREE_CODE (decl) == OVERLOAD)
4418 context = ovl_scope (decl);
b9e75696 4419 else
ed36980c 4420 {
b655c310
NS
4421 gcc_assert (DECL_P (decl));
4422 context = context_for_name_lookup (decl);
ed36980c 4423 }
00e8de68 4424
b655c310
NS
4425 if (is_properly_derived_from (class_type, context))
4426 INHERITED_VALUE_BINDING_P (binding) = 1;
4427 else
4428 INHERITED_VALUE_BINDING_P (binding) = 0;
4429 }
4430 else if (binding->value == decl)
4431 /* We only encounter a TREE_LIST when there is an ambiguity in the
4432 base classes. Such an ambiguity can be overridden by a
4433 definition in this class. */
4434 INHERITED_VALUE_BINDING_P (binding) = 1;
4435 else
4436 INHERITED_VALUE_BINDING_P (binding) = 0;
00e8de68
GDR
4437}
4438
b655c310 4439/* Make the declaration of X appear in CLASS scope. */
00e8de68 4440
b655c310
NS
4441bool
4442pushdecl_class_level (tree x)
00e8de68 4443{
b655c310
NS
4444 bool is_valid = true;
4445 bool subtime;
00e8de68 4446
b655c310
NS
4447 /* Do nothing if we're adding to an outer lambda closure type,
4448 outer_binding will add it later if it's needed. */
4449 if (current_class_type != class_binding_level->this_entity)
4450 return true;
00e8de68 4451
b655c310
NS
4452 subtime = timevar_cond_start (TV_NAME_LOOKUP);
4453 /* Get the name of X. */
848bf88d 4454 tree name = OVL_NAME (x);
00e8de68 4455
b655c310 4456 if (name)
00e8de68 4457 {
b655c310
NS
4458 is_valid = push_class_level_binding (name, x);
4459 if (TREE_CODE (x) == TYPE_DECL)
4460 set_identifier_type_value (name, x);
00e8de68 4461 }
b655c310
NS
4462 else if (ANON_AGGR_TYPE_P (TREE_TYPE (x)))
4463 {
4464 /* If X is an anonymous aggregate, all of its members are
4465 treated as if they were members of the class containing the
4466 aggregate, for naming purposes. */
4467 tree f;
00e8de68 4468
b655c310
NS
4469 for (f = TYPE_FIELDS (TREE_TYPE (x)); f; f = DECL_CHAIN (f))
4470 {
4471 location_t save_location = input_location;
4472 input_location = DECL_SOURCE_LOCATION (f);
4473 if (!pushdecl_class_level (f))
4474 is_valid = false;
4475 input_location = save_location;
4476 }
4477 }
575bfb00 4478 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
b655c310 4479 return is_valid;
00e8de68
GDR
4480}
4481
b655c310
NS
4482/* Return the BINDING (if any) for NAME in SCOPE, which is a class
4483 scope. If the value returned is non-NULL, and the PREVIOUS field
4484 is not set, callers must set the PREVIOUS field explicitly. */
5a167978 4485
b655c310
NS
4486static cxx_binding *
4487get_class_binding (tree name, cp_binding_level *scope)
5a167978 4488{
b655c310
NS
4489 tree class_type;
4490 tree type_binding;
4491 tree value_binding;
4492 cxx_binding *binding;
5a167978 4493
b655c310 4494 class_type = scope->this_entity;
5a167978 4495
b655c310
NS
4496 /* Get the type binding. */
4497 type_binding = lookup_member (class_type, name,
4498 /*protect=*/2, /*want_type=*/true,
4499 tf_warning_or_error);
4500 /* Get the value binding. */
4501 value_binding = lookup_member (class_type, name,
4502 /*protect=*/2, /*want_type=*/false,
4503 tf_warning_or_error);
5a167978 4504
b655c310
NS
4505 if (value_binding
4506 && (TREE_CODE (value_binding) == TYPE_DECL
4507 || DECL_CLASS_TEMPLATE_P (value_binding)
4508 || (TREE_CODE (value_binding) == TREE_LIST
4509 && TREE_TYPE (value_binding) == error_mark_node
4510 && (TREE_CODE (TREE_VALUE (value_binding))
4511 == TYPE_DECL))))
4512 /* We found a type binding, even when looking for a non-type
4513 binding. This means that we already processed this binding
4514 above. */
4515 ;
4516 else if (value_binding)
4517 {
4518 if (TREE_CODE (value_binding) == TREE_LIST
4519 && TREE_TYPE (value_binding) == error_mark_node)
4520 /* NAME is ambiguous. */
4521 ;
4522 else if (BASELINK_P (value_binding))
4523 /* NAME is some overloaded functions. */
4524 value_binding = BASELINK_FUNCTIONS (value_binding);
4525 }
5a167978 4526
b655c310
NS
4527 /* If we found either a type binding or a value binding, create a
4528 new binding object. */
4529 if (type_binding || value_binding)
4530 {
4531 binding = new_class_binding (name,
4532 value_binding,
4533 type_binding,
4534 scope);
4535 /* This is a class-scope binding, not a block-scope binding. */
4536 LOCAL_BINDING_P (binding) = 0;
4537 set_inherited_value_binding_p (binding, value_binding, class_type);
4538 }
575bfb00 4539 else
b655c310
NS
4540 binding = NULL;
4541
4542 return binding;
575bfb00
LC
4543}
4544
b655c310
NS
4545/* Make the declaration(s) of X appear in CLASS scope under the name
4546 NAME. Returns true if the binding is valid. */
575bfb00 4547
b655c310
NS
4548static bool
4549push_class_level_binding_1 (tree name, tree x)
575bfb00 4550{
b655c310
NS
4551 cxx_binding *binding;
4552 tree decl = x;
4553 bool ok;
5a167978 4554
b655c310
NS
4555 /* The class_binding_level will be NULL if x is a template
4556 parameter name in a member template. */
4557 if (!class_binding_level)
4558 return true;
5a167978 4559
b655c310
NS
4560 if (name == error_mark_node)
4561 return false;
166206ce 4562
b655c310
NS
4563 /* Can happen for an erroneous declaration (c++/60384). */
4564 if (!identifier_p (name))
4565 {
4566 gcc_assert (errorcount || sorrycount);
4567 return false;
4568 }
5a167978 4569
b655c310
NS
4570 /* Check for invalid member names. But don't worry about a default
4571 argument-scope lambda being pushed after the class is complete. */
4572 gcc_assert (TYPE_BEING_DEFINED (current_class_type)
4573 || LAMBDA_TYPE_P (TREE_TYPE (decl)));
4574 /* Check that we're pushing into the right binding level. */
4575 gcc_assert (current_class_type == class_binding_level->this_entity);
5a167978 4576
b655c310
NS
4577 /* We could have been passed a tree list if this is an ambiguous
4578 declaration. If so, pull the declaration out because
4579 check_template_shadow will not handle a TREE_LIST. */
4580 if (TREE_CODE (decl) == TREE_LIST
4581 && TREE_TYPE (decl) == error_mark_node)
4582 decl = TREE_VALUE (decl);
6097b0c3 4583
b655c310
NS
4584 if (!check_template_shadow (decl))
4585 return false;
5a167978 4586
b655c310 4587 /* [class.mem]
00e8de68 4588
b655c310
NS
4589 If T is the name of a class, then each of the following shall
4590 have a name different from T:
00e8de68 4591
b655c310 4592 -- every static data member of class T;
00e8de68 4593
b655c310
NS
4594 -- every member of class T that is itself a type;
4595
4596 -- every enumerator of every member of class T that is an
4597 enumerated type;
4598
4599 -- every member of every anonymous union that is a member of
4600 class T.
4601
4602 (Non-static data members were also forbidden to have the same
4603 name as T until TC1.) */
4604 if ((VAR_P (x)
4605 || TREE_CODE (x) == CONST_DECL
4606 || (TREE_CODE (x) == TYPE_DECL
4607 && !DECL_SELF_REFERENCE_P (x))
4608 /* A data member of an anonymous union. */
4609 || (TREE_CODE (x) == FIELD_DECL
4610 && DECL_CONTEXT (x) != current_class_type))
56b2a94b 4611 && DECL_NAME (x) == DECL_NAME (TYPE_NAME (current_class_type)))
b655c310
NS
4612 {
4613 tree scope = context_for_name_lookup (x);
4614 if (TYPE_P (scope) && same_type_p (scope, current_class_type))
4615 {
4616 error ("%qD has the same name as the class in which it is "
4617 "declared",
4618 x);
4619 return false;
4620 }
4621 }
4622
4623 /* Get the current binding for NAME in this class, if any. */
4624 binding = IDENTIFIER_BINDING (name);
4625 if (!binding || binding->scope != class_binding_level)
00e8de68 4626 {
b655c310
NS
4627 binding = get_class_binding (name, class_binding_level);
4628 /* If a new binding was created, put it at the front of the
4629 IDENTIFIER_BINDING list. */
4630 if (binding)
0cbd7506 4631 {
b655c310
NS
4632 binding->previous = IDENTIFIER_BINDING (name);
4633 IDENTIFIER_BINDING (name) = binding;
0cbd7506 4634 }
b655c310
NS
4635 }
4636
4637 /* If there is already a binding, then we may need to update the
4638 current value. */
4639 if (binding && binding->value)
4640 {
4641 tree bval = binding->value;
4642 tree old_decl = NULL_TREE;
4643 tree target_decl = strip_using_decl (decl);
4644 tree target_bval = strip_using_decl (bval);
4645
4646 if (INHERITED_VALUE_BINDING_P (binding))
0cbd7506 4647 {
b655c310
NS
4648 /* If the old binding was from a base class, and was for a
4649 tag name, slide it over to make room for the new binding.
4650 The old binding is still visible if explicitly qualified
4651 with a class-key. */
4652 if (TREE_CODE (target_bval) == TYPE_DECL
4653 && DECL_ARTIFICIAL (target_bval)
4654 && !(TREE_CODE (target_decl) == TYPE_DECL
4655 && DECL_ARTIFICIAL (target_decl)))
4656 {
4657 old_decl = binding->type;
4658 binding->type = bval;
4659 binding->value = NULL_TREE;
4660 INHERITED_VALUE_BINDING_P (binding) = 0;
4661 }
4662 else
4663 {
4664 old_decl = bval;
4665 /* Any inherited type declaration is hidden by the type
4666 declaration in the derived class. */
4667 if (TREE_CODE (target_decl) == TYPE_DECL
4668 && DECL_ARTIFICIAL (target_decl))
4669 binding->type = NULL_TREE;
4670 }
00e8de68 4671 }
b655c310 4672 else if (TREE_CODE (target_decl) == OVERLOAD
5256a7f5 4673 && OVL_P (target_bval))
b655c310
NS
4674 old_decl = bval;
4675 else if (TREE_CODE (decl) == USING_DECL
4676 && TREE_CODE (bval) == USING_DECL
4677 && same_type_p (USING_DECL_SCOPE (decl),
4678 USING_DECL_SCOPE (bval)))
4679 /* This is a using redeclaration that will be diagnosed later
4680 in supplement_binding */
4681 ;
4682 else if (TREE_CODE (decl) == USING_DECL
4683 && TREE_CODE (bval) == USING_DECL
4684 && DECL_DEPENDENT_P (decl)
4685 && DECL_DEPENDENT_P (bval))
4686 return true;
4687 else if (TREE_CODE (decl) == USING_DECL
5256a7f5 4688 && OVL_P (target_bval))
b655c310
NS
4689 old_decl = bval;
4690 else if (TREE_CODE (bval) == USING_DECL
5256a7f5 4691 && OVL_P (target_decl))
b655c310
NS
4692 return true;
4693
4694 if (old_decl && binding->scope == class_binding_level)
0cbd7506 4695 {
b655c310
NS
4696 binding->value = x;
4697 /* It is always safe to clear INHERITED_VALUE_BINDING_P
4698 here. This function is only used to register bindings
4699 from with the class definition itself. */
4700 INHERITED_VALUE_BINDING_P (binding) = 0;
4701 return true;
0cbd7506 4702 }
00e8de68 4703 }
00e8de68 4704
b655c310
NS
4705 /* Note that we declared this value so that we can issue an error if
4706 this is an invalid redeclaration of a name already used for some
4707 other purpose. */
4708 note_name_declared_in_class (name, decl);
575bfb00 4709
b655c310
NS
4710 /* If we didn't replace an existing binding, put the binding on the
4711 stack of bindings for the identifier, and update the shadowed
4712 list. */
4713 if (binding && binding->scope == class_binding_level)
4714 /* Supplement the existing binding. */
4715 ok = supplement_binding (binding, decl);
4716 else
5a167978 4717 {
b655c310
NS
4718 /* Create a new binding. */
4719 push_binding (name, decl, class_binding_level);
4720 ok = true;
5a167978
GDR
4721 }
4722
b655c310 4723 return ok;
575bfb00
LC
4724}
4725
b655c310 4726/* Wrapper for push_class_level_binding_1. */
575bfb00 4727
b655c310
NS
4728bool
4729push_class_level_binding (tree name, tree x)
575bfb00 4730{
b655c310 4731 bool ret;
575bfb00 4732 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
b655c310 4733 ret = push_class_level_binding_1 (name, x);
575bfb00 4734 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
b655c310 4735 return ret;
5a167978
GDR
4736}
4737
b655c310
NS
4738/* Process "using SCOPE::NAME" in a class scope. Return the
4739 USING_DECL created. */
5a167978 4740
b655c310
NS
4741tree
4742do_class_using_decl (tree scope, tree name)
5a167978 4743{
b655c310
NS
4744 if (name == error_mark_node)
4745 return NULL_TREE;
166206ce 4746
b655c310
NS
4747 if (!scope || !TYPE_P (scope))
4748 {
4749 error ("using-declaration for non-member at class scope");
4750 return NULL_TREE;
4751 }
166206ce 4752
b655c310
NS
4753 /* Make sure the name is not invalid */
4754 if (TREE_CODE (name) == BIT_NOT_EXPR)
6097b0c3 4755 {
b655c310
NS
4756 error ("%<%T::%D%> names destructor", scope, name);
4757 return NULL_TREE;
6097b0c3 4758 }
0c29f2a2 4759
b655c310
NS
4760 /* Using T::T declares inheriting ctors, even if T is a typedef. */
4761 if (MAYBE_CLASS_TYPE_P (scope)
4762 && (name == TYPE_IDENTIFIER (scope)
4763 || constructor_name_p (name, scope)))
6097b0c3 4764 {
b655c310
NS
4765 maybe_warn_cpp0x (CPP0X_INHERITING_CTORS);
4766 name = ctor_identifier;
4767 CLASSTYPE_NON_AGGREGATE (current_class_type) = true;
d19c0f4b 4768 }
0c29f2a2
NS
4769
4770 /* Cannot introduce a constructor name. */
b655c310
NS
4771 if (constructor_name_p (name, current_class_type))
4772 {
4773 error ("%<%T::%D%> names constructor in %qT",
4774 scope, name, current_class_type);
4775 return NULL_TREE;
4776 }
4777
b655c310 4778 /* From [namespace.udecl]:
1b255e8f 4779
b655c310
NS
4780 A using-declaration used as a member-declaration shall refer to a
4781 member of a base class of the class being defined.
4782
4783 In general, we cannot check this constraint in a template because
4784 we do not know the entire set of base classes of the current
4785 class type. Morover, if SCOPE is dependent, it might match a
4786 non-dependent base. */
4787
0c29f2a2
NS
4788 tree decl = NULL_TREE;
4789 if (!dependent_scope_p (scope))
86098eb8 4790 {
b655c310 4791 base_kind b_kind;
0c29f2a2
NS
4792 tree binfo = lookup_base (current_class_type, scope, ba_any, &b_kind,
4793 tf_warning_or_error);
b655c310 4794 if (b_kind < bk_proper_base)
86098eb8 4795 {
0c29f2a2
NS
4796 /* If there are dependent bases, scope might resolve at
4797 instantiation time, even if it isn't exactly one of the
4798 dependent bases. */
4799 if (b_kind == bk_same_type || !any_dependent_bases_p ())
9deb204a 4800 {
b655c310
NS
4801 error_not_base_type (scope, current_class_type);
4802 return NULL_TREE;
9deb204a 4803 }
86098eb8 4804 }
84eb0f1a 4805 else if (name == ctor_identifier && !binfo_direct_p (binfo))
b655c310
NS
4806 {
4807 error ("cannot inherit constructors from indirect base %qT", scope);
4808 return NULL_TREE;
4809 }
0c29f2a2
NS
4810 else if (!IDENTIFIER_CONV_OP_P (name)
4811 || !dependent_type_p (TREE_TYPE (name)))
b655c310
NS
4812 {
4813 decl = lookup_member (binfo, name, 0, false, tf_warning_or_error);
4814 if (!decl)
4815 {
4816 error ("no members matching %<%T::%D%> in %q#T", scope, name,
4817 scope);
4818 return NULL_TREE;
4819 }
0c29f2a2 4820
b655c310
NS
4821 /* The binfo from which the functions came does not matter. */
4822 if (BASELINK_P (decl))
4823 decl = BASELINK_FUNCTIONS (decl);
4824 }
86098eb8 4825 }
86098eb8 4826
0c29f2a2 4827 tree value = build_lang_decl (USING_DECL, name, NULL_TREE);
b655c310
NS
4828 USING_DECL_DECLS (value) = decl;
4829 USING_DECL_SCOPE (value) = scope;
4830 DECL_DEPENDENT_P (value) = !decl;
a5e6b29b 4831
b655c310 4832 return value;
a5e6b29b
GDR
4833}
4834
b655c310 4835\f
4b4b2e58
NS
4836/* Return the binding for NAME in NS. If NS is NULL, look in
4837 global_namespace. */
4838
a5e6b29b 4839tree
06aa5490 4840get_namespace_binding (tree ns, tree name)
a5e6b29b 4841{
b655c310 4842 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4b4b2e58
NS
4843 if (!ns)
4844 ns = global_namespace;
3c9cca88
NS
4845 gcc_checking_assert (!DECL_NAMESPACE_ALIAS (ns));
4846 tree ret = find_namespace_value (ns, name);
b655c310 4847 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3c9cca88 4848 return ret;
5a167978
GDR
4849}
4850
87e3d7cf
NS
4851/* Push internal DECL into the global namespace. Does not do the
4852 full overload fn handling and does not add it to the list of things
4853 in the namespace. */
1e003829 4854
b655c310 4855void
87e3d7cf 4856set_global_binding (tree decl)
1e003829 4857{
b655c310 4858 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4b4b2e58 4859
87e3d7cf 4860 tree *slot = find_namespace_slot (global_namespace, DECL_NAME (decl), true);
3c9cca88
NS
4861 tree old = MAYBE_STAT_DECL (*slot);
4862
4863 if (!old)
87e3d7cf
NS
4864 *slot = decl;
4865 else if (old == decl)
3c9cca88
NS
4866 ;
4867 else if (!STAT_HACK_P (*slot)
87e3d7cf
NS
4868 && TREE_CODE (decl) == TYPE_DECL && DECL_ARTIFICIAL (decl))
4869 *slot = stat_hack (old, decl);
3c9cca88
NS
4870 else if (!STAT_HACK_P (*slot)
4871 && TREE_CODE (old) == TYPE_DECL && DECL_ARTIFICIAL (old))
87e3d7cf 4872 *slot = stat_hack (decl, old);
3c9cca88
NS
4873 else
4874 /* The user's placed something in the implementor's
4875 namespace. */
87e3d7cf 4876 diagnose_name_conflict (decl, old);
4b4b2e58 4877
b655c310 4878 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
1e003829
JM
4879}
4880
b655c310
NS
4881/* Set the context of a declaration to scope. Complain if we are not
4882 outside scope. */
5a167978 4883
b655c310
NS
4884void
4885set_decl_namespace (tree decl, tree scope, bool friendp)
5a167978 4886{
b655c310
NS
4887 /* Get rid of namespace aliases. */
4888 scope = ORIGINAL_NAMESPACE (scope);
af92ab36 4889
b655c310 4890 /* It is ok for friends to be qualified in parallel space. */
322763f5 4891 if (!friendp && !is_nested_namespace (current_namespace, scope))
b655c310
NS
4892 error ("declaration of %qD not in a namespace surrounding %qD",
4893 decl, scope);
4894 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
af92ab36 4895
5ec046c0
NS
4896 /* See whether this has been declared in the namespace or inline
4897 children. */
4898 tree old = NULL_TREE;
4899 {
4900 name_lookup lookup (DECL_NAME (decl), LOOKUP_HIDDEN);
4901 if (!lookup.search_qualified (scope, /*usings=*/false))
4902 /* No old declaration at all. */
4903 goto not_found;
4904 old = lookup.value;
4905 }
c8094d83 4906
b655c310
NS
4907 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
4908 if (TREE_CODE (old) == TREE_LIST)
5a167978 4909 {
5ec046c0
NS
4910 ambiguous:
4911 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
b655c310
NS
4912 error ("reference to %qD is ambiguous", decl);
4913 print_candidates (old);
4914 return;
4915 }
5ec046c0
NS
4916
4917 if (!DECL_DECLARES_FUNCTION_P (decl))
b655c310 4918 {
b655c310
NS
4919 /* Don't compare non-function decls with decls_match here, since
4920 it can't check for the correct constness at this
5ec046c0
NS
4921 point. pushdecl will find those errors later. */
4922
4923 /* We might have found it in an inline namespace child of SCOPE. */
4924 if (TREE_CODE (decl) == TREE_CODE (old))
4925 DECL_CONTEXT (decl) = DECL_CONTEXT (old);
4926
4927 found:
4928 /* Writing "N::i" to declare something directly in "N" is invalid. */
4929 if (CP_DECL_CONTEXT (decl) == current_namespace
4930 && at_namespace_scope_p ())
4931 error ("explicit qualification in declaration of %qD", decl);
b655c310
NS
4932 return;
4933 }
5ec046c0 4934
b655c310 4935 /* Since decl is a function, old should contain a function decl. */
e1cad930 4936 if (!OVL_P (old))
5ec046c0
NS
4937 goto not_found;
4938
b655c310
NS
4939 /* We handle these in check_explicit_instantiation_namespace. */
4940 if (processing_explicit_instantiation)
4941 return;
4942 if (processing_template_decl || processing_specialization)
4943 /* We have not yet called push_template_decl to turn a
4944 FUNCTION_DECL into a TEMPLATE_DECL, so the declarations won't
4945 match. But, we'll check later, when we construct the
4946 template. */
4947 return;
4948 /* Instantiations or specializations of templates may be declared as
4949 friends in any namespace. */
4950 if (friendp && DECL_USE_TEMPLATE (decl))
4951 return;
5ec046c0
NS
4952
4953 tree found;
4954 found = NULL_TREE;
4955
4956 for (lkp_iterator iter (old); iter; ++iter)
b655c310 4957 {
5ec046c0
NS
4958 if (iter.using_p ())
4959 continue;
87c976ae 4960
5ec046c0
NS
4961 tree ofn = *iter;
4962
4963 /* Adjust DECL_CONTEXT first so decls_match will return true
4964 if DECL will match a declaration in an inline namespace. */
4965 DECL_CONTEXT (decl) = DECL_CONTEXT (ofn);
4966 if (decls_match (decl, ofn))
b655c310 4967 {
5ec046c0 4968 if (found)
b655c310 4969 {
5ec046c0
NS
4970 /* We found more than one matching declaration. */
4971 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
4972 goto ambiguous;
b655c310 4973 }
5ec046c0 4974 found = ofn;
5a167978
GDR
4975 }
4976 }
5ec046c0
NS
4977
4978 if (found)
5a167978 4979 {
5ec046c0
NS
4980 if (DECL_HIDDEN_FRIEND_P (found))
4981 {
4982 pedwarn (DECL_SOURCE_LOCATION (decl), 0,
4983 "%qD has not been declared within %qD", decl, scope);
4984 inform (DECL_SOURCE_LOCATION (found),
4985 "only here as a %<friend%>");
4986 }
4987 DECL_CONTEXT (decl) = DECL_CONTEXT (found);
4988 goto found;
5a167978 4989 }
b655c310 4990
5ec046c0 4991 not_found:
b655c310
NS
4992 /* It didn't work, go back to the explicit scope. */
4993 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
b655c310 4994 error ("%qD should have been declared inside %qD", decl, scope);
5a167978
GDR
4995}
4996
b655c310 4997/* Return the namespace where the current declaration is declared. */
00e8de68
GDR
4998
4999tree
b655c310 5000current_decl_namespace (void)
00e8de68 5001{
b655c310
NS
5002 tree result;
5003 /* If we have been pushed into a different namespace, use it. */
5004 if (!vec_safe_is_empty (decl_namespace_list))
5005 return decl_namespace_list->last ();
00e8de68 5006
b655c310
NS
5007 if (current_class_type)
5008 result = decl_namespace_context (current_class_type);
5009 else if (current_function_decl)
5010 result = decl_namespace_context (current_function_decl);
5011 else
5012 result = current_namespace;
5013 return result;
00e8de68
GDR
5014}
5015
b655c310
NS
5016/* Process any ATTRIBUTES on a namespace definition. Returns true if
5017 attribute visibility is seen. */
00e8de68 5018
b655c310
NS
5019bool
5020handle_namespace_attrs (tree ns, tree attributes)
00e8de68 5021{
b655c310
NS
5022 tree d;
5023 bool saw_vis = false;
5024
5025 for (d = attributes; d; d = TREE_CHAIN (d))
af79925b 5026 {
b655c310
NS
5027 tree name = get_attribute_name (d);
5028 tree args = TREE_VALUE (d);
5029
5030 if (is_attribute_p ("visibility", name))
5031 {
5032 /* attribute visibility is a property of the syntactic block
5033 rather than the namespace as a whole, so we don't touch the
5034 NAMESPACE_DECL at all. */
5035 tree x = args ? TREE_VALUE (args) : NULL_TREE;
5036 if (x == NULL_TREE || TREE_CODE (x) != STRING_CST || TREE_CHAIN (args))
5037 {
5038 warning (OPT_Wattributes,
5039 "%qD attribute requires a single NTBS argument",
5040 name);
5041 continue;
5042 }
5043
5044 if (!TREE_PUBLIC (ns))
5045 warning (OPT_Wattributes,
5046 "%qD attribute is meaningless since members of the "
5047 "anonymous namespace get local symbols", name);
5048
5049 push_visibility (TREE_STRING_POINTER (x), 1);
5050 saw_vis = true;
5051 }
5052 else if (is_attribute_p ("abi_tag", name))
5053 {
44e00a7a 5054 if (!DECL_NAME (ns))
b655c310 5055 {
44e00a7a 5056 warning (OPT_Wattributes, "ignoring %qD attribute on anonymous "
b655c310
NS
5057 "namespace", name);
5058 continue;
5059 }
44e00a7a 5060 if (!DECL_NAMESPACE_INLINE_P (ns))
b655c310 5061 {
44e00a7a 5062 warning (OPT_Wattributes, "ignoring %qD attribute on non-inline "
b655c310
NS
5063 "namespace", name);
5064 continue;
5065 }
5066 if (!args)
5067 {
5068 tree dn = DECL_NAME (ns);
5069 args = build_string (IDENTIFIER_LENGTH (dn) + 1,
5070 IDENTIFIER_POINTER (dn));
5071 TREE_TYPE (args) = char_array_type_node;
5072 args = fix_string_type (args);
5073 args = build_tree_list (NULL_TREE, args);
5074 }
5075 if (check_abi_tag_args (args, name))
5076 DECL_ATTRIBUTES (ns) = tree_cons (name, args,
5077 DECL_ATTRIBUTES (ns));
5078 }
5079 else
5080 {
5081 warning (OPT_Wattributes, "%qD attribute directive ignored",
5082 name);
5083 continue;
5084 }
af79925b 5085 }
bd3d082e 5086
b655c310
NS
5087 return saw_vis;
5088}
e1cad930 5089
b655c310 5090/* Temporarily set the namespace for the current declaration. */
bd3d082e 5091
b655c310
NS
5092void
5093push_decl_namespace (tree decl)
bd3d082e 5094{
b655c310
NS
5095 if (TREE_CODE (decl) != NAMESPACE_DECL)
5096 decl = decl_namespace_context (decl);
5097 vec_safe_push (decl_namespace_list, ORIGINAL_NAMESPACE (decl));
00e8de68
GDR
5098}
5099
b655c310 5100/* [namespace.memdef]/2 */
d63d5d0c 5101
b655c310
NS
5102void
5103pop_decl_namespace (void)
d63d5d0c 5104{
b655c310
NS
5105 decl_namespace_list->pop ();
5106}
d63d5d0c 5107
b655c310 5108/* Process a namespace-alias declaration. */
501c95ff
NF
5109
5110void
b655c310 5111do_namespace_alias (tree alias, tree name_space)
501c95ff 5112{
b655c310
NS
5113 if (name_space == error_mark_node)
5114 return;
501c95ff 5115
b655c310 5116 gcc_assert (TREE_CODE (name_space) == NAMESPACE_DECL);
501c95ff 5117
b655c310 5118 name_space = ORIGINAL_NAMESPACE (name_space);
501c95ff 5119
b655c310
NS
5120 /* Build the alias. */
5121 alias = build_lang_decl (NAMESPACE_DECL, alias, void_type_node);
5122 DECL_NAMESPACE_ALIAS (alias) = name_space;
5123 DECL_EXTERNAL (alias) = 1;
5124 DECL_CONTEXT (alias) = FROB_CONTEXT (current_scope ());
5125 pushdecl (alias);
501c95ff 5126
b655c310
NS
5127 /* Emit debug info for namespace alias. */
5128 if (!building_stmt_list_p ())
5129 (*debug_hooks->early_global_decl) (alias);
5130}
501c95ff 5131
b655c310
NS
5132/* Like pushdecl, only it places X in the current namespace,
5133 if appropriate. */
501c95ff 5134
b655c310
NS
5135tree
5136pushdecl_namespace_level (tree x, bool is_friend)
5137{
5138 cp_binding_level *b = current_binding_level;
5139 tree t;
501c95ff 5140
b655c310 5141 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
5256a7f5 5142 t = do_pushdecl_with_scope
d16d5eac 5143 (x, NAMESPACE_LEVEL (current_namespace), is_friend);
501c95ff 5144
b655c310
NS
5145 /* Now, the type_shadowed stack may screw us. Munge it so it does
5146 what we want. */
5147 if (TREE_CODE (t) == TYPE_DECL)
52ed68f7 5148 {
b655c310
NS
5149 tree name = DECL_NAME (t);
5150 tree newval;
5151 tree *ptr = (tree *)0;
5152 for (; !global_scope_p (b); b = b->level_chain)
52ed68f7 5153 {
b655c310
NS
5154 tree shadowed = b->type_shadowed;
5155 for (; shadowed; shadowed = TREE_CHAIN (shadowed))
5156 if (TREE_PURPOSE (shadowed) == name)
5157 {
5158 ptr = &TREE_VALUE (shadowed);
5159 /* Can't break out of the loop here because sometimes
5160 a binding level will have duplicate bindings for
5161 PT names. It's gross, but I haven't time to fix it. */
5162 }
5163 }
5164 newval = TREE_TYPE (t);
5165 if (ptr == (tree *)0)
5166 {
5167 /* @@ This shouldn't be needed. My test case "zstring.cc" trips
5168 up here if this is changed to an assertion. --KR */
5169 SET_IDENTIFIER_TYPE_VALUE (name, t);
5170 }
5171 else
5172 {
5173 *ptr = newval;
52ed68f7 5174 }
52ed68f7 5175 }
b655c310
NS
5176 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
5177 return t;
501c95ff
NF
5178}
5179
9d029ddf 5180/* Process a using-declaration appearing in namespace scope. */
ebed7175 5181
b655c310 5182void
9d029ddf 5183finish_namespace_using_decl (tree decl, tree scope, tree name)
ebed7175 5184{
b655c310 5185 tree orig_decl = decl;
fcb2cdfc 5186
3c9cca88
NS
5187 gcc_checking_assert (current_binding_level->kind == sk_namespace
5188 && !processing_template_decl);
b655c310
NS
5189 decl = validate_nonmember_using_decl (decl, scope, name);
5190 if (decl == NULL_TREE)
5191 return;
ebed7175 5192
3c9cca88
NS
5193 tree *slot = find_namespace_slot (current_namespace, name, true);
5194 tree val = slot ? MAYBE_STAT_DECL (*slot) : NULL_TREE;
5195 tree type = slot ? MAYBE_STAT_TYPE (*slot) : NULL_TREE;
5196 do_nonmember_using_decl (scope, name, &val, &type);
5197 if (STAT_HACK_P (*slot))
5198 {
5199 STAT_DECL (*slot) = val;
5200 STAT_TYPE (*slot) = type;
5201 }
5202 else if (type)
5203 *slot = stat_hack (val, type);
5204 else
5205 *slot = val;
b655c310
NS
5206
5207 /* Emit debug info. */
3c9cca88 5208 cp_emit_debug_info_for_using (orig_decl, current_namespace);
9d029ddf 5209}
b655c310 5210
3c9cca88 5211/* Process a using-declaration at function scope. */
9d029ddf
NS
5212
5213void
5214finish_local_using_decl (tree decl, tree scope, tree name)
5215{
5216 tree orig_decl = decl;
5217
5218 gcc_checking_assert (current_binding_level->kind != sk_class
5219 && current_binding_level->kind != sk_namespace);
5220 decl = validate_nonmember_using_decl (decl, scope, name);
5221 if (decl == NULL_TREE)
5222 return;
5223
e1cad930 5224 add_decl_expr (decl);
9d029ddf
NS
5225
5226 cxx_binding *binding = find_local_binding (current_binding_level, name);
5227 tree value = binding ? binding->value : NULL_TREE;
5228 tree type = binding ? binding->type : NULL_TREE;
5229
5230 do_nonmember_using_decl (scope, name, &value, &type);
5231
5232 if (!value)
5233 ;
5234 else if (binding && value == binding->value)
5235 ;
5236 else if (binding && binding->value && TREE_CODE (value) == OVERLOAD)
5237 {
5238 update_local_overload (IDENTIFIER_BINDING (name), value);
5239 IDENTIFIER_BINDING (name)->value = value;
5240 }
5241 else
5242 /* Install the new binding. */
5243 push_local_binding (name, value, true);
5244
5245 if (!type)
5246 ;
5247 else if (binding && type == binding->type)
5248 ;
5249 else
5250 {
5251 push_local_binding (name, type, true);
5252 set_identifier_type_value (name, type);
5253 }
5254
5255 /* Emit debug info. */
5256 if (!processing_template_decl)
5257 cp_emit_debug_info_for_using (orig_decl, current_scope ());
ebed7175
DM
5258}
5259
b655c310
NS
5260/* Return the declarations that are members of the namespace NS. */
5261
5262tree
5263cp_namespace_decls (tree ns)
5264{
5265 return NAMESPACE_LEVEL (ns)->names;
52ed68f7
DM
5266}
5267
b655c310 5268/* Combine prefer_type and namespaces_only into flags. */
9ca6556e 5269
b655c310
NS
5270static int
5271lookup_flags (int prefer_type, int namespaces_only)
5272{
5273 if (namespaces_only)
5274 return LOOKUP_PREFER_NAMESPACES;
5275 if (prefer_type > 1)
5276 return LOOKUP_PREFER_TYPES;
5277 if (prefer_type > 0)
5278 return LOOKUP_PREFER_BOTH;
5279 return 0;
5280}
9ca6556e 5281
b655c310
NS
5282/* Given a lookup that returned VAL, use FLAGS to decide if we want to
5283 ignore it or not. Subroutine of lookup_name_real and
5284 lookup_type_scope. */
172a4594
DS
5285
5286static bool
b655c310 5287qualify_lookup (tree val, int flags)
172a4594 5288{
b655c310 5289 if (val == NULL_TREE)
172a4594 5290 return false;
b655c310
NS
5291 if ((flags & LOOKUP_PREFER_NAMESPACES) && TREE_CODE (val) == NAMESPACE_DECL)
5292 return true;
5293 if (flags & LOOKUP_PREFER_TYPES)
5294 {
5295 tree target_val = strip_using_decl (val);
5296 if (TREE_CODE (target_val) == TYPE_DECL
5297 || TREE_CODE (target_val) == TEMPLATE_DECL)
5298 return true;
5299 }
5300 if (flags & (LOOKUP_PREFER_NAMESPACES | LOOKUP_PREFER_TYPES))
74788b80 5301 return false;
b655c310 5302 /* Look through lambda things that we shouldn't be able to see. */
5c263e84 5303 if (!(flags & LOOKUP_HIDDEN) && is_lambda_ignored_entity (val))
b655c310
NS
5304 return false;
5305 return true;
5306}
74788b80 5307
b655c310
NS
5308/* Suggest alternatives for NAME, an IDENTIFIER_NODE for which name
5309 lookup failed. Search through all available namespaces and print out
5310 possible candidates. If no exact matches are found, and
5311 SUGGEST_MISSPELLINGS is true, then also look for near-matches and
5312 suggest the best near-match, if there is one. */
90ea9897 5313
b655c310
NS
5314void
5315suggest_alternatives_for (location_t location, tree name,
5316 bool suggest_misspellings)
90ea9897 5317{
b655c310 5318 vec<tree> candidates = vNULL;
c957e9c0
NS
5319 vec<tree> worklist = vNULL;
5320 unsigned limit = PARAM_VALUE (CXX_MAX_NAMESPACES_FOR_DIAGNOSTIC_HELP);
5321 bool limited = false;
5322
5323 /* Breadth-first search of namespaces. Up to limit namespaces
5324 searched (limit zero == unlimited). */
5325 worklist.safe_push (global_namespace);
5326 for (unsigned ix = 0; ix != worklist.length (); ix++)
b655c310 5327 {
c957e9c0 5328 tree ns = worklist[ix];
25396db9 5329 name_lookup lookup (name);
90ea9897 5330
25396db9
NS
5331 if (lookup.search_qualified (ns, false))
5332 candidates.safe_push (lookup.value);
00e8de68 5333
c957e9c0 5334 if (!limited)
00e8de68 5335 {
c957e9c0
NS
5336 /* Look for child namespaces. We have to do this
5337 indirectly because they are chained in reverse order,
5338 which is confusing to the user. */
5339 vec<tree> children = vNULL;
5340
5341 for (tree decl = NAMESPACE_LEVEL (ns)->names;
5342 decl; decl = TREE_CHAIN (decl))
5343 if (TREE_CODE (decl) == NAMESPACE_DECL
25396db9
NS
5344 && !DECL_NAMESPACE_ALIAS (decl)
5345 && !DECL_NAMESPACE_INLINE_P (decl))
c957e9c0
NS
5346 children.safe_push (decl);
5347
5348 while (!limited && !children.is_empty ())
b655c310 5349 {
c957e9c0
NS
5350 if (worklist.length () == limit)
5351 {
5352 /* Unconditionally warn that the search was truncated. */
5353 inform (location,
5354 "maximum limit of %d namespaces searched for %qE",
5355 limit, name);
5356 limited = true;
5357 }
5358 else
5359 worklist.safe_push (children.pop ());
b655c310 5360 }
c957e9c0 5361 children.release ();
b655c310 5362 }
b655c310 5363 }
c957e9c0 5364 worklist.release ();
c8094d83 5365
c957e9c0
NS
5366 if (candidates.length ())
5367 {
5368 inform_n (location, candidates.length (),
5369 "suggested alternative:",
5370 "suggested alternatives:");
5371 for (unsigned ix = 0; ix != candidates.length (); ix++)
5372 {
5373 tree val = candidates[ix];
c8094d83 5374
c957e9c0
NS
5375 inform (location_of (val), " %qE", val);
5376 }
5377 candidates.release ();
5378 }
5379 else if (!suggest_misspellings)
5380 ;
5381 else if (const char *fuzzy = lookup_name_fuzzy (name, FUZZY_LOOKUP_NAME))
5382 {
5383 /* Show a spelling correction. */
5384 gcc_rich_location richloc (location);
00e8de68 5385
c957e9c0
NS
5386 richloc.add_fixit_replace (fuzzy);
5387 inform_at_rich_loc (&richloc, "suggested alternative: %qs", fuzzy);
5388 }
b655c310 5389}
00e8de68 5390
b655c310
NS
5391/* Subroutine of maybe_suggest_missing_header for handling unrecognized names
5392 for some of the most common names within "std::".
5393 Given non-NULL NAME, a name for lookup within "std::", return the header
eea77d1f 5394 name defining it within the C++ Standard Library (with '<' and '>'),
b655c310 5395 or NULL. */
00e8de68 5396
b655c310
NS
5397static const char *
5398get_std_name_hint (const char *name)
5399{
5400 struct std_name_hint
5401 {
5402 const char *name;
5403 const char *header;
5404 };
5405 static const std_name_hint hints[] = {
5406 /* <array>. */
eea77d1f 5407 {"array", "<array>"}, // C++11
b655c310 5408 /* <deque>. */
eea77d1f 5409 {"deque", "<deque>"},
b655c310 5410 /* <forward_list>. */
eea77d1f 5411 {"forward_list", "<forward_list>"}, // C++11
b655c310 5412 /* <fstream>. */
eea77d1f
DM
5413 {"basic_filebuf", "<fstream>"},
5414 {"basic_ifstream", "<fstream>"},
5415 {"basic_ofstream", "<fstream>"},
5416 {"basic_fstream", "<fstream>"},
b655c310 5417 /* <iostream>. */
eea77d1f
DM
5418 {"cin", "<iostream>"},
5419 {"cout", "<iostream>"},
5420 {"cerr", "<iostream>"},
5421 {"clog", "<iostream>"},
5422 {"wcin", "<iostream>"},
5423 {"wcout", "<iostream>"},
5424 {"wclog", "<iostream>"},
b655c310 5425 /* <list>. */
eea77d1f 5426 {"list", "<list>"},
b655c310 5427 /* <map>. */
eea77d1f
DM
5428 {"map", "<map>"},
5429 {"multimap", "<map>"},
b655c310 5430 /* <queue>. */
eea77d1f
DM
5431 {"queue", "<queue>"},
5432 {"priority_queue", "<queue>"},
b655c310 5433 /* <ostream>. */
eea77d1f
DM
5434 {"ostream", "<ostream>"},
5435 {"wostream", "<ostream>"},
5436 {"ends", "<ostream>"},
5437 {"flush", "<ostream>"},
5438 {"endl", "<ostream>"},
b655c310 5439 /* <set>. */
eea77d1f
DM
5440 {"set", "<set>"},
5441 {"multiset", "<set>"},
b655c310 5442 /* <sstream>. */
eea77d1f
DM
5443 {"basic_stringbuf", "<sstream>"},
5444 {"basic_istringstream", "<sstream>"},
5445 {"basic_ostringstream", "<sstream>"},
5446 {"basic_stringstream", "<sstream>"},
b655c310 5447 /* <stack>. */
eea77d1f 5448 {"stack", "<stack>"},
b655c310 5449 /* <string>. */
eea77d1f
DM
5450 {"string", "<string>"},
5451 {"wstring", "<string>"},
5452 {"u16string", "<string>"},
5453 {"u32string", "<string>"},
b655c310 5454 /* <unordered_map>. */
eea77d1f
DM
5455 {"unordered_map", "<unordered_map>"}, // C++11
5456 {"unordered_multimap", "<unordered_map>"}, // C++11
b655c310 5457 /* <unordered_set>. */
eea77d1f
DM
5458 {"unordered_set", "<unordered_set>"}, // C++11
5459 {"unordered_multiset", "<unordered_set>"}, // C++11
b655c310 5460 /* <vector>. */
eea77d1f 5461 {"vector", "<vector>"},
b655c310
NS
5462 };
5463 const size_t num_hints = sizeof (hints) / sizeof (hints[0]);
5464 for (size_t i = 0; i < num_hints; i++)
5465 {
5466 if (0 == strcmp (name, hints[i].name))
5467 return hints[i].header;
5468 }
5469 return NULL;
5470}
00e8de68 5471
f661e57e
DM
5472/* If SCOPE is the "std" namespace, then suggest pertinent header
5473 files for NAME at LOCATION.
5474 Return true iff a suggestion was offered. */
00e8de68 5475
f661e57e 5476static bool
b655c310
NS
5477maybe_suggest_missing_header (location_t location, tree name, tree scope)
5478{
5479 if (scope == NULL_TREE)
f661e57e 5480 return false;
b655c310 5481 if (TREE_CODE (scope) != NAMESPACE_DECL)
f661e57e 5482 return false;
b655c310
NS
5483 /* We only offer suggestions for the "std" namespace. */
5484 if (scope != std_node)
f661e57e 5485 return false;
b655c310 5486 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
c8094d83 5487
b655c310
NS
5488 const char *name_str = IDENTIFIER_POINTER (name);
5489 const char *header_hint = get_std_name_hint (name_str);
f661e57e
DM
5490 if (!header_hint)
5491 return false;
5492
5493 gcc_rich_location richloc (location);
5494 maybe_add_include_fixit (&richloc, header_hint);
5495 inform_at_rich_loc (&richloc,
5496 "%<std::%s%> is defined in header %qs;"
5497 " did you forget to %<#include %s%>?",
5498 name_str, header_hint, header_hint);
5499 return true;
b655c310 5500}
c8094d83 5501
b655c310
NS
5502/* Look for alternatives for NAME, an IDENTIFIER_NODE for which name
5503 lookup failed within the explicitly provided SCOPE. Suggest the
5504 the best meaningful candidates (if any) as a fix-it hint.
5505 Return true iff a suggestion was provided. */
c8094d83 5506
b655c310
NS
5507bool
5508suggest_alternative_in_explicit_scope (location_t location, tree name,
5509 tree scope)
5510{
5511 /* Resolve any namespace aliases. */
5512 scope = ORIGINAL_NAMESPACE (scope);
b9f673eb 5513
f661e57e
DM
5514 if (maybe_suggest_missing_header (location, name, scope))
5515 return true;
5516
b655c310 5517 cp_binding_level *level = NAMESPACE_LEVEL (scope);
d4d8c232 5518
b655c310
NS
5519 best_match <tree, const char *> bm (name);
5520 consider_binding_level (name, bm, level, false, FUZZY_LOOKUP_NAME);
d4d8c232 5521
b655c310
NS
5522 /* See if we have a good suggesion for the user. */
5523 const char *fuzzy_name = bm.get_best_meaningful_candidate ();
5524 if (fuzzy_name)
5525 {
5526 gcc_rich_location richloc (location);
5527 richloc.add_fixit_replace (fuzzy_name);
5528 inform_at_rich_loc (&richloc, "suggested alternative: %qs",
5529 fuzzy_name);
5530 return true;
5531 }
d4d8c232 5532
b655c310
NS
5533 return false;
5534}
d4d8c232 5535
b655c310
NS
5536/* Look up NAME (an IDENTIFIER_NODE) in SCOPE (either a NAMESPACE_DECL
5537 or a class TYPE).
00e8de68 5538
b655c310
NS
5539 If PREFER_TYPE is > 0, we only return TYPE_DECLs or namespaces.
5540 If PREFER_TYPE is > 1, we only return TYPE_DECLs.
00e8de68 5541
b655c310
NS
5542 Returns a DECL (or OVERLOAD, or BASELINK) representing the
5543 declaration found. If no suitable declaration can be found,
5544 ERROR_MARK_NODE is returned. If COMPLAIN is true and SCOPE is
5545 neither a class-type nor a namespace a diagnostic is issued. */
00e8de68 5546
98803730 5547tree
b655c310
NS
5548lookup_qualified_name (tree scope, tree name, int prefer_type, bool complain,
5549 bool find_hidden)
98803730 5550{
b655c310
NS
5551 tree t = NULL_TREE;
5552
5553 if (TREE_CODE (scope) == NAMESPACE_DECL)
5554 {
b655c310
NS
5555 int flags = lookup_flags (prefer_type, /*namespaces_only*/false);
5556 if (find_hidden)
5557 flags |= LOOKUP_HIDDEN;
9dda0ace
NS
5558 name_lookup lookup (name, flags);
5559
5560 if (qualified_namespace_lookup (scope, &lookup))
5561 t = lookup.value;
b655c310
NS
5562 }
5563 else if (cxx_dialect != cxx98 && TREE_CODE (scope) == ENUMERAL_TYPE)
5564 t = lookup_enumerator (scope, name);
5565 else if (is_class_type (scope, complain))
5566 t = lookup_member (scope, name, 2, prefer_type, tf_warning_or_error);
5567
5568 if (!t)
5569 return error_mark_node;
5570 return t;
98803730
MS
5571}
5572
b655c310
NS
5573/* [namespace.qual]
5574 Accepts the NAME to lookup and its qualifying SCOPE.
5575 Returns the name/type pair found into the cxx_binding *RESULT,
5576 or false on error. */
461c6fce 5577
b655c310 5578static bool
9dda0ace
NS
5579qualified_namespace_lookup (tree scope, name_lookup *lookup)
5580{
b655c310 5581 timevar_start (TV_NAME_LOOKUP);
9dda0ace
NS
5582 query_oracle (lookup->name);
5583 bool found = lookup->search_qualified (ORIGINAL_NAMESPACE (scope));
b655c310 5584 timevar_stop (TV_NAME_LOOKUP);
9dda0ace 5585 return found;
575bfb00
LC
5586}
5587
b655c310
NS
5588/* Helper function for lookup_name_fuzzy.
5589 Traverse binding level LVL, looking for good name matches for NAME
5590 (and BM). */
5591static void
5592consider_binding_level (tree name, best_match <tree, const char *> &bm,
5593 cp_binding_level *lvl, bool look_within_fields,
5594 enum lookup_name_fuzzy_kind kind)
00e8de68 5595{
b655c310
NS
5596 if (look_within_fields)
5597 if (lvl->this_entity && TREE_CODE (lvl->this_entity) == RECORD_TYPE)
5598 {
5599 tree type = lvl->this_entity;
5600 bool want_type_p = (kind == FUZZY_LOOKUP_TYPENAME);
5601 tree best_matching_field
5602 = lookup_member_fuzzy (type, name, want_type_p);
5603 if (best_matching_field)
5604 bm.consider (IDENTIFIER_POINTER (best_matching_field));
5605 }
00e8de68 5606
b655c310 5607 for (tree t = lvl->names; t; t = TREE_CHAIN (t))
00e8de68 5608 {
b655c310 5609 tree d = t;
00e8de68 5610
b655c310
NS
5611 /* OVERLOADs or decls from using declaration are wrapped into
5612 TREE_LIST. */
5613 if (TREE_CODE (d) == TREE_LIST)
87c976ae 5614 d = OVL_FIRST (TREE_VALUE (d));
00e8de68 5615
b655c310
NS
5616 /* Don't use bindings from implicitly declared functions,
5617 as they were likely misspellings themselves. */
5618 if (TREE_TYPE (d) == error_mark_node)
5619 continue;
00e8de68 5620
b655c310
NS
5621 /* Skip anticipated decls of builtin functions. */
5622 if (TREE_CODE (d) == FUNCTION_DECL
5623 && DECL_BUILT_IN (d)
5624 && DECL_ANTICIPATED (d))
5625 continue;
575bfb00 5626
b655c310
NS
5627 if (tree name = DECL_NAME (d))
5628 /* Ignore internal names with spaces in them. */
5629 if (!strchr (IDENTIFIER_POINTER (name), ' '))
5630 bm.consider (IDENTIFIER_POINTER (name));
5631 }
575bfb00
LC
5632}
5633
b655c310
NS
5634/* Search for near-matches for NAME within the current bindings, and within
5635 macro names, returning the best match as a const char *, or NULL if
5636 no reasonable match is found. */
575bfb00 5637
b655c310
NS
5638const char *
5639lookup_name_fuzzy (tree name, enum lookup_name_fuzzy_kind kind)
e8f43da6 5640{
b655c310 5641 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
e8f43da6 5642
b655c310 5643 best_match <tree, const char *> bm (name);
e8f43da6 5644
b655c310
NS
5645 cp_binding_level *lvl;
5646 for (lvl = scope_chain->class_bindings; lvl; lvl = lvl->level_chain)
5647 consider_binding_level (name, bm, lvl, true, kind);
e8f43da6 5648
b655c310
NS
5649 for (lvl = current_binding_level; lvl; lvl = lvl->level_chain)
5650 consider_binding_level (name, bm, lvl, false, kind);
e8f43da6 5651
b655c310
NS
5652 /* Consider macros: if the user misspelled a macro name e.g. "SOME_MACRO"
5653 as:
5654 x = SOME_OTHER_MACRO (y);
5655 then "SOME_OTHER_MACRO" will survive to the frontend and show up
5656 as a misspelled identifier.
e8f43da6 5657
b655c310
NS
5658 Use the best distance so far so that a candidate is only set if
5659 a macro is better than anything so far. This allows early rejection
5660 (without calculating the edit distance) of macro names that must have
5661 distance >= bm.get_best_distance (), and means that we only get a
5662 non-NULL result for best_macro_match if it's better than any of
5663 the identifiers already checked. */
5664 best_macro_match bmm (name, bm.get_best_distance (), parse_in);
5665 cpp_hashnode *best_macro = bmm.get_best_meaningful_candidate ();
5666 /* If a macro is the closest so far to NAME, consider it. */
5667 if (best_macro)
5668 bm.consider ((const char *)best_macro->ident.str);
00e8de68 5669
b655c310
NS
5670 /* Try the "starts_decl_specifier_p" keywords to detect
5671 "singed" vs "signed" typos. */
5672 for (unsigned i = 0; i < num_c_common_reswords; i++)
5673 {
5674 const c_common_resword *resword = &c_common_reswords[i];
00e8de68 5675
b655c310
NS
5676 if (kind == FUZZY_LOOKUP_TYPENAME)
5677 if (!cp_keyword_starts_decl_specifier_p (resword->rid))
5678 continue;
00e8de68 5679
b655c310
NS
5680 tree resword_identifier = ridpointers [resword->rid];
5681 if (!resword_identifier)
5682 continue;
5683 gcc_assert (TREE_CODE (resword_identifier) == IDENTIFIER_NODE);
00e8de68 5684
b655c310
NS
5685 /* Only consider reserved words that survived the
5686 filtering in init_reswords (e.g. for -std). */
84c0088f 5687 if (!IDENTIFIER_KEYWORD_P (resword_identifier))
b655c310 5688 continue;
00e8de68 5689
b655c310
NS
5690 bm.consider (IDENTIFIER_POINTER (resword_identifier));
5691 }
5692
5693 return bm.get_best_meaningful_candidate ();
5694}
5a167978 5695
b655c310 5696/* Subroutine of outer_binding.
5a167978 5697
b655c310
NS
5698 Returns TRUE if BINDING is a binding to a template parameter of
5699 SCOPE. In that case SCOPE is the scope of a primary template
5700 parameter -- in the sense of G++, i.e, a template that has its own
5701 template header.
5a167978 5702
b655c310 5703 Returns FALSE otherwise. */
5a167978
GDR
5704
5705static bool
b655c310
NS
5706binding_to_template_parms_of_scope_p (cxx_binding *binding,
5707 cp_binding_level *scope)
5a167978 5708{
b655c310
NS
5709 tree binding_value, tmpl, tinfo;
5710 int level;
5a167978 5711
b655c310
NS
5712 if (!binding || !scope || !scope->this_entity)
5713 return false;
5714
5715 binding_value = binding->value ? binding->value : binding->type;
5716 tinfo = get_template_info (scope->this_entity);
5717
5718 /* BINDING_VALUE must be a template parm. */
5719 if (binding_value == NULL_TREE
5720 || (!DECL_P (binding_value)
5721 || !DECL_TEMPLATE_PARM_P (binding_value)))
5722 return false;
5723
5724 /* The level of BINDING_VALUE. */
5725 level =
5726 template_type_parameter_p (binding_value)
5727 ? TEMPLATE_PARM_LEVEL (TEMPLATE_TYPE_PARM_INDEX
5728 (TREE_TYPE (binding_value)))
5729 : TEMPLATE_PARM_LEVEL (DECL_INITIAL (binding_value));
5730
5731 /* The template of the current scope, iff said scope is a primary
5732 template. */
5733 tmpl = (tinfo
5734 && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
5735 ? TI_TEMPLATE (tinfo)
5736 : NULL_TREE);
5737
5738 /* If the level of the parm BINDING_VALUE equals the depth of TMPL,
5739 then BINDING_VALUE is a parameter of TMPL. */
5740 return (tmpl && level == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)));
5a167978
GDR
5741}
5742
b655c310
NS
5743/* Return the innermost non-namespace binding for NAME from a scope
5744 containing BINDING, or, if BINDING is NULL, the current scope.
5745 Please note that for a given template, the template parameters are
5746 considered to be in the scope containing the current scope.
5747 If CLASS_P is false, then class bindings are ignored. */
86098eb8 5748
b655c310
NS
5749cxx_binding *
5750outer_binding (tree name,
5751 cxx_binding *binding,
5752 bool class_p)
86098eb8 5753{
b655c310
NS
5754 cxx_binding *outer;
5755 cp_binding_level *scope;
5756 cp_binding_level *outer_scope;
d4ccba66 5757
b655c310 5758 if (binding)
86098eb8 5759 {
b655c310
NS
5760 scope = binding->scope->level_chain;
5761 outer = binding->previous;
5762 }
5763 else
5764 {
5765 scope = current_binding_level;
5766 outer = IDENTIFIER_BINDING (name);
86098eb8 5767 }
b655c310 5768 outer_scope = outer ? outer->scope : NULL;
d4ccba66 5769
b655c310
NS
5770 /* Because we create class bindings lazily, we might be missing a
5771 class binding for NAME. If there are any class binding levels
5772 between the LAST_BINDING_LEVEL and the scope in which OUTER was
5773 declared, we must lookup NAME in those class scopes. */
5774 if (class_p)
5775 while (scope && scope != outer_scope && scope->kind != sk_namespace)
5776 {
5777 if (scope->kind == sk_class)
5778 {
5779 cxx_binding *class_binding;
d4ccba66 5780
b655c310
NS
5781 class_binding = get_class_binding (name, scope);
5782 if (class_binding)
5783 {
5784 /* Thread this new class-scope binding onto the
5785 IDENTIFIER_BINDING list so that future lookups
5786 find it quickly. */
5787 class_binding->previous = outer;
5788 if (binding)
5789 binding->previous = class_binding;
5790 else
5791 IDENTIFIER_BINDING (name) = class_binding;
5792 return class_binding;
5793 }
5794 }
5795 /* If we are in a member template, the template parms of the member
5796 template are considered to be inside the scope of the containing
5797 class, but within G++ the class bindings are all pushed between the
5798 template parms and the function body. So if the outer binding is
5799 a template parm for the current scope, return it now rather than
5800 look for a class binding. */
5801 if (outer_scope && outer_scope->kind == sk_template_parms
5802 && binding_to_template_parms_of_scope_p (outer, scope))
5803 return outer;
5804
5805 scope = scope->level_chain;
5806 }
5807
5808 return outer;
86098eb8
JM
5809}
5810
b655c310
NS
5811/* Return the innermost block-scope or class-scope value binding for
5812 NAME, or NULL_TREE if there is no such binding. */
5a167978 5813
b655c310
NS
5814tree
5815innermost_non_namespace_value (tree name)
5a167978 5816{
b655c310
NS
5817 cxx_binding *binding;
5818 binding = outer_binding (name, /*binding=*/NULL, /*class_p=*/true);
5819 return binding ? binding->value : NULL_TREE;
5820}
5a167978 5821
b655c310
NS
5822/* Look up NAME in the current binding level and its superiors in the
5823 namespace of variables, functions and typedefs. Return a ..._DECL
5824 node of some kind representing its definition if there is only one
5825 such declaration, or return a TREE_LIST with all the overloaded
5826 definitions if there are many, or return 0 if it is undefined.
5827 Hidden name, either friend declaration or built-in function, are
5828 not ignored.
86098eb8 5829
b655c310
NS
5830 If PREFER_TYPE is > 0, we prefer TYPE_DECLs or namespaces.
5831 If PREFER_TYPE is > 1, we reject non-type decls (e.g. namespaces).
5832 Otherwise we prefer non-TYPE_DECLs.
c8094d83 5833
b655c310
NS
5834 If NONCLASS is nonzero, bindings in class scopes are ignored. If
5835 BLOCK_P is false, bindings in block scopes are ignored. */
4cfaec1c 5836
b655c310
NS
5837static tree
5838lookup_name_real_1 (tree name, int prefer_type, int nonclass, bool block_p,
5839 int namespaces_only, int flags)
5840{
5841 cxx_binding *iter;
5842 tree val = NULL_TREE;
5a167978 5843
b655c310
NS
5844 query_oracle (name);
5845
5846 /* Conversion operators are handled specially because ordinary
5847 unqualified name lookup will not find template conversion
5848 operators. */
84c0088f 5849 if (IDENTIFIER_CONV_OP_P (name))
d63d5d0c 5850 {
b655c310 5851 cp_binding_level *level;
d63d5d0c 5852
b655c310
NS
5853 for (level = current_binding_level;
5854 level && level->kind != sk_namespace;
5855 level = level->level_chain)
5856 {
5857 tree class_type;
5858 tree operators;
5859
5860 /* A conversion operator can only be declared in a class
5861 scope. */
5862 if (level->kind != sk_class)
5863 continue;
5864
5865 /* Lookup the conversion operator in the class. */
5866 class_type = level->this_entity;
5867 operators = lookup_fnfields (class_type, name, /*protect=*/0);
5868 if (operators)
5869 return operators;
5870 }
5871
5872 return NULL_TREE;
d63d5d0c 5873 }
c8094d83 5874
b655c310
NS
5875 flags |= lookup_flags (prefer_type, namespaces_only);
5876
5877 /* First, look in non-namespace scopes. */
5878
5879 if (current_class_type == NULL_TREE)
5880 nonclass = 1;
5a167978 5881
b655c310
NS
5882 if (block_p || !nonclass)
5883 for (iter = outer_binding (name, NULL, !nonclass);
5884 iter;
5885 iter = outer_binding (name, iter, !nonclass))
5886 {
5887 tree binding;
5a167978 5888
b655c310
NS
5889 /* Skip entities we don't want. */
5890 if (LOCAL_BINDING_P (iter) ? !block_p : nonclass)
5891 continue;
5a167978 5892
b655c310
NS
5893 /* If this is the kind of thing we're looking for, we're done. */
5894 if (qualify_lookup (iter->value, flags))
5895 binding = iter->value;
5896 else if ((flags & LOOKUP_PREFER_TYPES)
5897 && qualify_lookup (iter->type, flags))
5898 binding = iter->type;
5899 else
5900 binding = NULL_TREE;
5a167978 5901
b655c310
NS
5902 if (binding)
5903 {
ef4c5e78 5904 if (TREE_CODE (binding) == TYPE_DECL && DECL_HIDDEN_P (binding))
b655c310
NS
5905 {
5906 /* A non namespace-scope binding can only be hidden in the
5907 presence of a local class, due to friend declarations.
5a167978 5908
b655c310 5909 In particular, consider:
ba796308 5910
b655c310
NS
5911 struct C;
5912 void f() {
5913 struct A {
5914 friend struct B;
5915 friend struct C;
5916 void g() {
5917 B* b; // error: B is hidden
5918 C* c; // OK, finds ::C
5919 }
5920 };
5921 B *b; // error: B is hidden
5922 C *c; // OK, finds ::C
5923 struct B {};
5924 B *bb; // OK
5925 }
5a167978 5926
b655c310
NS
5927 The standard says that "B" is a local class in "f"
5928 (but not nested within "A") -- but that name lookup
5929 for "B" does not find this declaration until it is
5930 declared directly with "f".
5a167978 5931
b655c310 5932 In particular:
c8094d83 5933
b655c310 5934 [class.friend]
5a167978 5935
b655c310
NS
5936 If a friend declaration appears in a local class and
5937 the name specified is an unqualified name, a prior
5938 declaration is looked up without considering scopes
5939 that are outside the innermost enclosing non-class
5940 scope. For a friend function declaration, if there is
5941 no prior declaration, the program is ill-formed. For a
5942 friend class declaration, if there is no prior
5943 declaration, the class that is specified belongs to the
5944 innermost enclosing non-class scope, but if it is
5945 subsequently referenced, its name is not found by name
5946 lookup until a matching declaration is provided in the
5947 innermost enclosing nonclass scope.
ccb14335 5948
b655c310
NS
5949 So just keep looking for a non-hidden binding.
5950 */
5951 gcc_assert (TREE_CODE (binding) == TYPE_DECL);
5952 continue;
5953 }
5954 val = binding;
5955 break;
5956 }
5957 }
2395cd2e 5958
b655c310
NS
5959 /* Now lookup in namespace scopes. */
5960 if (!val)
932f48ac
NS
5961 {
5962 name_lookup lookup (name, flags);
5963 if (lookup.search_unqualified
5964 (current_decl_namespace (), current_binding_level))
5965 val = lookup.value;
5966 }
c8b2e872 5967
b655c310
NS
5968 /* If we have a single function from a using decl, pull it out. */
5969 if (val && TREE_CODE (val) == OVERLOAD && !really_overloaded_fn (val))
5970 val = OVL_FUNCTION (val);
5971
5972 return val;
db10df3d
JM
5973}
5974
b655c310 5975/* Wrapper for lookup_name_real_1. */
db10df3d 5976
b655c310
NS
5977tree
5978lookup_name_real (tree name, int prefer_type, int nonclass, bool block_p,
5979 int namespaces_only, int flags)
db10df3d 5980{
b655c310
NS
5981 tree ret;
5982 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
5983 ret = lookup_name_real_1 (name, prefer_type, nonclass, block_p,
5984 namespaces_only, flags);
5985 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
5986 return ret;
5987}
db10df3d 5988
b655c310
NS
5989tree
5990lookup_name_nonclass (tree name)
5991{
5992 return lookup_name_real (name, 0, 1, /*block_p=*/true, 0, 0);
db10df3d
JM
5993}
5994
b655c310
NS
5995tree
5996lookup_name (tree name)
5997{
5998 return lookup_name_real (name, 0, 0, /*block_p=*/true, 0, 0);
5999}
db10df3d 6000
b655c310
NS
6001tree
6002lookup_name_prefer_type (tree name, int prefer_type)
db10df3d 6003{
b655c310
NS
6004 return lookup_name_real (name, prefer_type, 0, /*block_p=*/true, 0, 0);
6005}
db10df3d 6006
b655c310
NS
6007/* Look up NAME for type used in elaborated name specifier in
6008 the scopes given by SCOPE. SCOPE can be either TS_CURRENT or
6009 TS_WITHIN_ENCLOSING_NON_CLASS. Although not implied by the
6010 name, more scopes are checked if cleanup or template parameter
6011 scope is encountered.
db10df3d 6012
b655c310
NS
6013 Unlike lookup_name_real, we make sure that NAME is actually
6014 declared in the desired scope, not from inheritance, nor using
6015 directive. For using declaration, there is DR138 still waiting
6016 to be resolved. Hidden name coming from an earlier friend
6017 declaration is also returned.
db10df3d 6018
b655c310
NS
6019 A TYPE_DECL best matching the NAME is returned. Catching error
6020 and issuing diagnostics are caller's responsibility. */
db10df3d 6021
b655c310
NS
6022static tree
6023lookup_type_scope_1 (tree name, tag_scope scope)
6024{
6025 cxx_binding *iter = NULL;
6026 tree val = NULL_TREE;
3c9cca88 6027 cp_binding_level *level = NULL;
db10df3d 6028
b655c310
NS
6029 /* Look in non-namespace scope first. */
6030 if (current_binding_level->kind != sk_namespace)
6031 iter = outer_binding (name, NULL, /*class_p=*/ true);
6032 for (; iter; iter = outer_binding (name, iter, /*class_p=*/ true))
5a167978 6033 {
b655c310
NS
6034 /* Check if this is the kind of thing we're looking for.
6035 If SCOPE is TS_CURRENT, also make sure it doesn't come from
6036 base class. For ITER->VALUE, we can simply use
6037 INHERITED_VALUE_BINDING_P. For ITER->TYPE, we have to use
6038 our own check.
5a167978 6039
b655c310
NS
6040 We check ITER->TYPE before ITER->VALUE in order to handle
6041 typedef struct C {} C;
6042 correctly. */
5a167978 6043
b655c310
NS
6044 if (qualify_lookup (iter->type, LOOKUP_PREFER_TYPES)
6045 && (scope != ts_current
6046 || LOCAL_BINDING_P (iter)
6047 || DECL_CONTEXT (iter->type) == iter->scope->this_entity))
6048 val = iter->type;
6049 else if ((scope != ts_current
6050 || !INHERITED_VALUE_BINDING_P (iter))
6051 && qualify_lookup (iter->value, LOOKUP_PREFER_TYPES))
6052 val = iter->value;
5a167978 6053
b655c310
NS
6054 if (val)
6055 break;
6056 }
5a167978 6057
b655c310 6058 /* Look in namespace scope. */
3c9cca88
NS
6059 if (val)
6060 level = iter->scope;
6061 else
5a167978 6062 {
3c9cca88 6063 tree ns = current_decl_namespace ();
b655c310 6064
3c9cca88 6065 if (tree *slot = find_namespace_slot (ns, name))
b655c310
NS
6066 {
6067 /* If this is the kind of thing we're looking for, we're done. */
3c9cca88
NS
6068 if (tree type = MAYBE_STAT_TYPE (*slot))
6069 if (qualify_lookup (type, LOOKUP_PREFER_TYPES))
6070 val = type;
6071 if (!val)
6072 {
6073 if (tree decl = MAYBE_STAT_DECL (*slot))
6074 if (qualify_lookup (decl, LOOKUP_PREFER_TYPES))
6075 val = decl;
6076 }
6077 level = NAMESPACE_LEVEL (ns);
b655c310 6078 }
5a167978 6079 }
b655c310
NS
6080
6081 /* Type found, check if it is in the allowed scopes, ignoring cleanup
6082 and template parameter scopes. */
6083 if (val)
5a167978 6084 {
b655c310
NS
6085 cp_binding_level *b = current_binding_level;
6086 while (b)
6087 {
3c9cca88 6088 if (level == b)
b655c310 6089 return val;
5d80a306 6090
b655c310
NS
6091 if (b->kind == sk_cleanup || b->kind == sk_template_parms
6092 || b->kind == sk_function_parms)
6093 b = b->level_chain;
6094 else if (b->kind == sk_class
6095 && scope == ts_within_enclosing_non_class)
6096 b = b->level_chain;
6097 else
6098 break;
6099 }
5a167978 6100 }
5a167978 6101
b655c310 6102 return NULL_TREE;
5a167978 6103}
b655c310
NS
6104
6105/* Wrapper for lookup_type_scope_1. */
5a167978 6106
b655c310
NS
6107tree
6108lookup_type_scope (tree name, tag_scope scope)
c166b898 6109{
b655c310
NS
6110 tree ret;
6111 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6112 ret = lookup_type_scope_1 (name, scope);
6113 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6114 return ret;
c166b898
ILT
6115}
6116
b655c310
NS
6117/* Returns true iff DECL is a block-scope extern declaration of a function
6118 or variable. */
6119
6120bool
6121is_local_extern (tree decl)
5a167978 6122{
b655c310 6123 cxx_binding *binding;
5a167978 6124
b655c310
NS
6125 /* For functions, this is easy. */
6126 if (TREE_CODE (decl) == FUNCTION_DECL)
6127 return DECL_LOCAL_FUNCTION_P (decl);
d63d5d0c 6128
b655c310
NS
6129 if (!VAR_P (decl))
6130 return false;
6131 if (!current_function_decl)
6132 return false;
5a167978 6133
b655c310
NS
6134 /* For variables, this is not easy. We need to look at the binding stack
6135 for the identifier to see whether the decl we have is a local. */
6136 for (binding = IDENTIFIER_BINDING (DECL_NAME (decl));
6137 binding && binding->scope->kind != sk_namespace;
6138 binding = binding->previous)
6139 if (binding->value == decl)
6140 return LOCAL_BINDING_P (binding);
5a167978 6141
b655c310
NS
6142 return false;
6143}
ca1085f0 6144
00e8de68
GDR
6145/* The type TYPE is being declared. If it is a class template, or a
6146 specialization of a class template, do any processing required and
6147 perform error-checking. If IS_FRIEND is nonzero, this TYPE is
6148 being declared a friend. B is the binding level at which this TYPE
6149 should be bound.
6150
6151 Returns the TYPE_DECL for TYPE, which may have been altered by this
6152 processing. */
6153
6154static tree
bd3d082e 6155maybe_process_template_type_declaration (tree type, int is_friend,
2c140474 6156 cp_binding_level *b)
00e8de68
GDR
6157{
6158 tree decl = TYPE_NAME (type);
6159
6160 if (processing_template_parmlist)
6161 /* You can't declare a new template type in a template parameter
6162 list. But, you can declare a non-template type:
6163
0cbd7506 6164 template <class A*> struct S;
00e8de68
GDR
6165
6166 is a forward-declaration of `A'. */
6167 ;
c43e95f8
MM
6168 else if (b->kind == sk_namespace
6169 && current_binding_level->kind != sk_namespace)
6170 /* If this new type is being injected into a containing scope,
6171 then it's not a template type. */
6172 ;
00e8de68
GDR
6173 else
6174 {
9e1e64ec
PC
6175 gcc_assert (MAYBE_CLASS_TYPE_P (type)
6176 || TREE_CODE (type) == ENUMERAL_TYPE);
00e8de68
GDR
6177
6178 if (processing_template_decl)
6179 {
6180 /* This may change after the call to
6181 push_template_decl_real, but we want the original value. */
6182 tree name = DECL_NAME (decl);
6183
bd3d082e 6184 decl = push_template_decl_real (decl, is_friend);
79faac54
PC
6185 if (decl == error_mark_node)
6186 return error_mark_node;
6187
00e8de68
GDR
6188 /* If the current binding level is the binding level for the
6189 template parameters (see the comment in
6190 begin_template_parm_list) and the enclosing level is a class
6191 scope, and we're not looking at a friend, push the
6192 declaration of the member class into the class scope. In the
6193 friend case, push_template_decl will already have put the
6194 friend into global scope, if appropriate. */
6195 if (TREE_CODE (type) != ENUMERAL_TYPE
bd3d082e 6196 && !is_friend && b->kind == sk_template_parms
00e8de68
GDR
6197 && b->level_chain->kind == sk_class)
6198 {
6199 finish_member_declaration (CLASSTYPE_TI_TEMPLATE (type));
e57df6fe 6200
00e8de68
GDR
6201 if (!COMPLETE_TYPE_P (current_class_type))
6202 {
6203 maybe_add_class_template_decl_list (current_class_type,
6204 type, /*friend_p=*/0);
c72a1a86 6205 /* Put this UTD in the table of UTDs for the class. */
e57df6fe
KL
6206 if (CLASSTYPE_NESTED_UTDS (current_class_type) == NULL)
6207 CLASSTYPE_NESTED_UTDS (current_class_type) =
6208 binding_table_new (SCOPE_DEFAULT_HT_SIZE);
6209
6210 binding_table_insert
6211 (CLASSTYPE_NESTED_UTDS (current_class_type), name, type);
00e8de68
GDR
6212 }
6213 }
6214 }
6215 }
6216
6217 return decl;
6218}
6219
5a24482e
KL
6220/* Push a tag name NAME for struct/class/union/enum type TYPE. In case
6221 that the NAME is a class template, the tag is processed but not pushed.
6222
6223 The pushed scope depend on the SCOPE parameter:
6224 - When SCOPE is TS_CURRENT, put it into the inner-most non-sk_cleanup
6225 scope.
6226 - When SCOPE is TS_GLOBAL, put it in the inner-most non-class and
6227 non-template-parameter scope. This case is needed for forward
6228 declarations.
6229 - When SCOPE is TS_WITHIN_ENCLOSING_NON_CLASS, this is similar to
6230 TS_GLOBAL case except that names within template-parameter scopes
6231 are not pushed at all.
6232
c6f9f83b 6233 Returns TYPE upon success and ERROR_MARK_NODE otherwise. */
00e8de68 6234
575bfb00 6235static tree
5256a7f5 6236do_pushtag (tree name, tree type, tag_scope scope)
00e8de68 6237{
6a000704 6238 tree decl;
00e8de68 6239
5256a7f5 6240 cp_binding_level *b = current_binding_level;
7c82a41e
MM
6241 while (/* Cleanup scopes are not scopes from the point of view of
6242 the language. */
6243 b->kind == sk_cleanup
b344d949
JM
6244 /* Neither are function parameter scopes. */
6245 || b->kind == sk_function_parms
7c82a41e
MM
6246 /* Neither are the scopes used to hold template parameters
6247 for an explicit specialization. For an ordinary template
6248 declaration, these scopes are not scopes from the point of
5a24482e
KL
6249 view of the language. */
6250 || (b->kind == sk_template_parms
6251 && (b->explicit_spec_p || scope == ts_global))
c443f3d5 6252 /* Pushing into a class is ok for lambdas or when we want current */
00e8de68 6253 || (b->kind == sk_class
c443f3d5 6254 && scope != ts_lambda
bd3d082e 6255 && (scope != ts_current
00e8de68
GDR
6256 /* We may be defining a new type in the initializer
6257 of a static member variable. We allow this when
6258 not pedantic, and it is particularly useful for
6259 type punning via an anonymous union. */
6260 || COMPLETE_TYPE_P (b->this_entity))))
6261 b = b->level_chain;
6262
9dc6f476 6263 gcc_assert (identifier_p (name));
3db45ab5 6264
6a000704 6265 /* Do C++ gratuitous typedefing. */
575bfb00 6266 if (identifier_type_value_1 (name) != type)
00e8de68 6267 {
6a000704
NS
6268 tree tdef;
6269 int in_class = 0;
6270 tree context = TYPE_CONTEXT (type);
00e8de68 6271
6a000704
NS
6272 if (! context)
6273 {
6274 tree cs = current_scope ();
3db45ab5 6275
d5f4eddd 6276 if (scope == ts_current
c443f3d5 6277 || scope == ts_lambda
d5f4eddd 6278 || (cs && TREE_CODE (cs) == FUNCTION_DECL))
6a000704 6279 context = cs;
c443f3d5 6280 else if (cs && TYPE_P (cs))
6a000704
NS
6281 /* When declaring a friend class of a local class, we want
6282 to inject the newly named class into the scope
6283 containing the local class, not the namespace
6284 scope. */
6285 context = decl_function_context (get_type_decl (cs));
6286 }
6287 if (!context)
6288 context = current_namespace;
bd3d082e 6289
6a000704
NS
6290 if (b->kind == sk_class
6291 || (b->kind == sk_template_parms
6292 && b->level_chain->kind == sk_class))
6293 in_class = 1;
00e8de68 6294
6a000704
NS
6295 tdef = create_implicit_typedef (name, type);
6296 DECL_CONTEXT (tdef) = FROB_CONTEXT (context);
6297 if (scope == ts_within_enclosing_non_class)
00e8de68 6298 {
6a000704
NS
6299 /* This is a friend. Make this TYPE_DECL node hidden from
6300 ordinary name lookup. Its corresponding TEMPLATE_DECL
6301 will be marked in push_template_decl_real. */
6302 retrofit_lang_decl (tdef);
6303 DECL_ANTICIPATED (tdef) = 1;
6304 DECL_FRIEND_P (tdef) = 1;
6305 }
e57df6fe 6306
6a000704
NS
6307 decl = maybe_process_template_type_declaration
6308 (type, scope == ts_within_enclosing_non_class, b);
6309 if (decl == error_mark_node)
575bfb00 6310 return decl;
3db45ab5 6311
6a000704
NS
6312 if (b->kind == sk_class)
6313 {
c443f3d5
NS
6314 if (!TYPE_BEING_DEFINED (current_class_type)
6315 && scope != ts_lambda)
575bfb00 6316 return error_mark_node;
0efc4442 6317
6a000704
NS
6318 if (!PROCESSING_REAL_TEMPLATE_DECL_P ())
6319 /* Put this TYPE_DECL on the TYPE_FIELDS list for the
6320 class. But if it's a member template class, we want
6321 the TEMPLATE_DECL, not the TYPE_DECL, so this is done
6322 later. */
6323 finish_member_declaration (decl);
6324 else
6325 pushdecl_class_level (decl);
00e8de68 6326 }
6a000704 6327 else if (b->kind != sk_template_parms)
c5f8391c 6328 {
5256a7f5 6329 decl = do_pushdecl_with_scope (decl, b, /*is_friend=*/false);
c5f8391c 6330 if (decl == error_mark_node)
575bfb00 6331 return decl;
d3e19c2c
PC
6332
6333 if (DECL_CONTEXT (decl) == std_node
ad9870f2 6334 && init_list_identifier == DECL_NAME (TYPE_NAME (type))
d3e19c2c
PC
6335 && !CLASSTYPE_TEMPLATE_INFO (type))
6336 {
6337 error ("declaration of std::initializer_list does not match "
6338 "#include <initializer_list>, isn't a template");
6339 return error_mark_node;
6340 }
c5f8391c 6341 }
6a000704 6342
dc3ca06f
SM
6343 if (! in_class)
6344 set_identifier_type_value_with_scope (name, tdef, b);
6345
6a000704
NS
6346 TYPE_CONTEXT (type) = DECL_CONTEXT (decl);
6347
6348 /* If this is a local class, keep track of it. We need this
6349 information for name-mangling, and so that it is possible to
6350 find all function definitions in a translation unit in a
6351 convenient way. (It's otherwise tricky to find a member
6352 function definition it's only pointed to from within a local
6353 class.) */
9ededfc5 6354 if (TYPE_FUNCTION_SCOPE_P (type))
fdaf2f48
JM
6355 {
6356 if (processing_template_decl)
6357 {
6358 /* Push a DECL_EXPR so we call pushtag at the right time in
6359 template instantiation rather than in some nested context. */
6360 add_decl_expr (decl);
6361 }
6362 else
9771b263 6363 vec_safe_push (local_classes, type);
fdaf2f48 6364 }
00e8de68 6365 }
c443f3d5 6366
6a000704
NS
6367 if (b->kind == sk_class
6368 && !COMPLETE_TYPE_P (current_class_type))
00e8de68 6369 {
6a000704
NS
6370 maybe_add_class_template_decl_list (current_class_type,
6371 type, /*friend_p=*/0);
3db45ab5 6372
6a000704
NS
6373 if (CLASSTYPE_NESTED_UTDS (current_class_type) == NULL)
6374 CLASSTYPE_NESTED_UTDS (current_class_type)
6375 = binding_table_new (SCOPE_DEFAULT_HT_SIZE);
3db45ab5 6376
6a000704
NS
6377 binding_table_insert
6378 (CLASSTYPE_NESTED_UTDS (current_class_type), name, type);
00e8de68 6379 }
6a000704
NS
6380
6381 decl = TYPE_NAME (type);
6382 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
6a000704 6383
b9e75696
JM
6384 /* Set type visibility now if this is a forward declaration. */
6385 TREE_PUBLIC (decl) = 1;
6386 determine_visibility (decl);
6387
575bfb00
LC
6388 return type;
6389}
6390
5256a7f5 6391/* Wrapper for do_pushtag. */
575bfb00
LC
6392
6393tree
6394pushtag (tree name, tree type, tag_scope scope)
6395{
6396 tree ret;
6397 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
5256a7f5 6398 ret = do_pushtag (name, type, scope);
575bfb00
LC
6399 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6400 return ret;
00e8de68 6401}
8db29d88 6402
00e8de68 6403\f
00e8de68
GDR
6404/* Subroutines for reverting temporarily to top-level for instantiation
6405 of templates and such. We actually need to clear out the class- and
6406 local-value slots of all identifiers, so that only the global values
6407 are at all visible. Simply setting current_binding_level to the global
6408 scope isn't enough, because more binding levels may be pushed. */
6409struct saved_scope *scope_chain;
6410
71f15f31
RG
6411/* Return true if ID has not already been marked. */
6412
6413static inline bool
6414store_binding_p (tree id)
6415{
6416 if (!id || !IDENTIFIER_BINDING (id))
6417 return false;
6418
6419 if (IDENTIFIER_MARKED (id))
6420 return false;
6421
6422 return true;
6423}
6424
6425/* Add an appropriate binding to *OLD_BINDINGS which needs to already
6426 have enough space reserved. */
89b578be 6427
f44b0c8e 6428static void
9771b263 6429store_binding (tree id, vec<cxx_saved_binding, va_gc> **old_bindings)
89b578be 6430{
f32682ca 6431 cxx_saved_binding saved;
89b578be 6432
71f15f31 6433 gcc_checking_assert (store_binding_p (id));
c8094d83 6434
f44b0c8e 6435 IDENTIFIER_MARKED (id) = 1;
89b578be 6436
f32682ca
DN
6437 saved.identifier = id;
6438 saved.binding = IDENTIFIER_BINDING (id);
6439 saved.real_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
9771b263 6440 (*old_bindings)->quick_push (saved);
89b578be 6441 IDENTIFIER_BINDING (id) = NULL;
89b578be
MM
6442}
6443
f44b0c8e 6444static void
9771b263 6445store_bindings (tree names, vec<cxx_saved_binding, va_gc> **old_bindings)
00e8de68 6446{
199d1d48 6447 static vec<tree> bindings_need_stored;
71f15f31
RG
6448 tree t, id;
6449 size_t i;
00e8de68 6450
575bfb00 6451 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
00e8de68
GDR
6452 for (t = names; t; t = TREE_CHAIN (t))
6453 {
00e8de68
GDR
6454 if (TREE_CODE (t) == TREE_LIST)
6455 id = TREE_PURPOSE (t);
6456 else
6457 id = DECL_NAME (t);
6458
71f15f31 6459 if (store_binding_p (id))
9771b263 6460 bindings_need_stored.safe_push (id);
71f15f31 6461 }
9771b263 6462 if (!bindings_need_stored.is_empty ())
71f15f31 6463 {
9771b263
DN
6464 vec_safe_reserve_exact (*old_bindings, bindings_need_stored.length ());
6465 for (i = 0; bindings_need_stored.iterate (i, &id); ++i)
71f15f31 6466 {
5764ee3c 6467 /* We can apparently have duplicates in NAMES. */
71f15f31
RG
6468 if (store_binding_p (id))
6469 store_binding (id, old_bindings);
6470 }
9771b263 6471 bindings_need_stored.truncate (0);
00e8de68 6472 }
575bfb00 6473 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
00e8de68
GDR
6474}
6475
89b578be
MM
6476/* Like store_bindings, but NAMES is a vector of cp_class_binding
6477 objects, rather than a TREE_LIST. */
6478
f44b0c8e 6479static void
9771b263
DN
6480store_class_bindings (vec<cp_class_binding, va_gc> *names,
6481 vec<cxx_saved_binding, va_gc> **old_bindings)
89b578be 6482{
199d1d48 6483 static vec<tree> bindings_need_stored;
89b578be
MM
6484 size_t i;
6485 cp_class_binding *cb;
89b578be 6486
9771b263 6487 for (i = 0; vec_safe_iterate (names, i, &cb); ++i)
71f15f31 6488 if (store_binding_p (cb->identifier))
9771b263
DN
6489 bindings_need_stored.safe_push (cb->identifier);
6490 if (!bindings_need_stored.is_empty ())
71f15f31
RG
6491 {
6492 tree id;
9771b263
DN
6493 vec_safe_reserve_exact (*old_bindings, bindings_need_stored.length ());
6494 for (i = 0; bindings_need_stored.iterate (i, &id); ++i)
71f15f31 6495 store_binding (id, old_bindings);
9771b263 6496 bindings_need_stored.truncate (0);
71f15f31 6497 }
89b578be
MM
6498}
6499
5c712250
PP
6500/* A chain of saved_scope structures awaiting reuse. */
6501
6502static GTY((deletable)) struct saved_scope *free_saved_scope;
6503
c405923d
NS
6504static void
6505do_push_to_top_level (void)
00e8de68
GDR
6506{
6507 struct saved_scope *s;
2c140474 6508 cp_binding_level *b;
f44b0c8e
MM
6509 cxx_saved_binding *sb;
6510 size_t i;
30bcc028 6511 bool need_pop;
00e8de68 6512
5c712250
PP
6513 /* Reuse or create a new structure for this saved scope. */
6514 if (free_saved_scope != NULL)
6515 {
6516 s = free_saved_scope;
6517 free_saved_scope = s->prev;
6518
6519 vec<cxx_saved_binding, va_gc> *old_bindings = s->old_bindings;
6520 memset (s, 0, sizeof (*s));
6521 /* Also reuse the structure's old_bindings vector. */
6522 vec_safe_truncate (old_bindings, 0);
6523 s->old_bindings = old_bindings;
6524 }
6525 else
6526 s = ggc_cleared_alloc<saved_scope> ();
00e8de68
GDR
6527
6528 b = scope_chain ? current_binding_level : 0;
6529
6530 /* If we're in the middle of some function, save our state. */
6531 if (cfun)
6532 {
30bcc028 6533 need_pop = true;
d2784db4 6534 push_function_context ();
00e8de68
GDR
6535 }
6536 else
30bcc028 6537 need_pop = false;
00e8de68 6538
89b578be 6539 if (scope_chain && previous_class_level)
f44b0c8e
MM
6540 store_class_bindings (previous_class_level->class_shadowed,
6541 &s->old_bindings);
00e8de68
GDR
6542
6543 /* Have to include the global scope, because class-scope decls
6544 aren't listed anywhere useful. */
6545 for (; b; b = b->level_chain)
6546 {
6547 tree t;
6548
6549 /* Template IDs are inserted into the global level. If they were
6550 inserted into namespace level, finish_file wouldn't find them
6551 when doing pending instantiations. Therefore, don't stop at
6552 namespace level, but continue until :: . */
c353b8e3 6553 if (global_scope_p (b))
00e8de68
GDR
6554 break;
6555
f44b0c8e 6556 store_bindings (b->names, &s->old_bindings);
00e8de68
GDR
6557 /* We also need to check class_shadowed to save class-level type
6558 bindings, since pushclass doesn't fill in b->names. */
6559 if (b->kind == sk_class)
f44b0c8e 6560 store_class_bindings (b->class_shadowed, &s->old_bindings);
00e8de68
GDR
6561
6562 /* Unwind type-value slots back to top level. */
6563 for (t = b->type_shadowed; t; t = TREE_CHAIN (t))
6564 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (t), TREE_VALUE (t));
6565 }
f44b0c8e 6566
9771b263 6567 FOR_EACH_VEC_SAFE_ELT (s->old_bindings, i, sb)
f44b0c8e
MM
6568 IDENTIFIER_MARKED (sb->identifier) = 0;
6569
00e8de68 6570 s->prev = scope_chain;
00e8de68
GDR
6571 s->bindings = b;
6572 s->need_pop_function_context = need_pop;
6573 s->function_decl = current_function_decl;
7d882b83
ILT
6574 s->unevaluated_operand = cp_unevaluated_operand;
6575 s->inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
04f7a48e 6576 s->x_stmt_tree.stmts_are_full_exprs_p = true;
00e8de68
GDR
6577
6578 scope_chain = s;
6579 current_function_decl = NULL_TREE;
9771b263 6580 vec_alloc (current_lang_base, 10);
00e8de68
GDR
6581 current_lang_name = lang_name_cplusplus;
6582 current_namespace = global_namespace;
c888c93b 6583 push_class_stack ();
7d882b83
ILT
6584 cp_unevaluated_operand = 0;
6585 c_inhibit_evaluation_warnings = 0;
00e8de68
GDR
6586}
6587
575bfb00 6588static void
c405923d 6589do_pop_from_top_level (void)
00e8de68
GDR
6590{
6591 struct saved_scope *s = scope_chain;
6592 cxx_saved_binding *saved;
f44b0c8e 6593 size_t i;
00e8de68 6594
00e8de68 6595 /* Clear out class-level bindings cache. */
89b578be 6596 if (previous_class_level)
00e8de68 6597 invalidate_class_lookup_cache ();
c888c93b 6598 pop_class_stack ();
00e8de68
GDR
6599
6600 current_lang_base = 0;
6601
6602 scope_chain = s->prev;
9771b263 6603 FOR_EACH_VEC_SAFE_ELT (s->old_bindings, i, saved)
00e8de68
GDR
6604 {
6605 tree id = saved->identifier;
6606
6607 IDENTIFIER_BINDING (id) = saved->binding;
00e8de68
GDR
6608 SET_IDENTIFIER_TYPE_VALUE (id, saved->real_type_value);
6609 }
6610
6611 /* If we were in the middle of compiling a function, restore our
6612 state. */
6613 if (s->need_pop_function_context)
d2784db4 6614 pop_function_context ();
00e8de68 6615 current_function_decl = s->function_decl;
7d882b83
ILT
6616 cp_unevaluated_operand = s->unevaluated_operand;
6617 c_inhibit_evaluation_warnings = s->inhibit_evaluation_warnings;
5c712250
PP
6618
6619 /* Make this saved_scope structure available for reuse by
6620 push_to_top_level. */
6621 s->prev = free_saved_scope;
6622 free_saved_scope = s;
00e8de68
GDR
6623}
6624
c405923d
NS
6625/* Push into the scope of the namespace NS, even if it is deeply
6626 nested within another namespace. */
575bfb00 6627
c405923d
NS
6628static void
6629do_push_nested_namespace (tree ns)
6630{
6631 if (ns == global_namespace)
6632 do_push_to_top_level ();
6633 else
6634 {
6635 do_push_nested_namespace (CP_DECL_CONTEXT (ns));
6636 gcc_checking_assert
e833f686 6637 (find_namespace_value (current_namespace, DECL_NAME (ns)) == ns);
c405923d
NS
6638 resume_scope (NAMESPACE_LEVEL (ns));
6639 current_namespace = ns;
6640 }
6641}
6642
6643/* Pop back from the scope of the namespace NS, which was previously
6644 entered with push_nested_namespace. */
6645
6646static void
6647do_pop_nested_namespace (tree ns)
6648{
6649 while (ns != global_namespace)
6650 {
6651 ns = CP_DECL_CONTEXT (ns);
6652 current_namespace = ns;
6653 leave_scope ();
6654 }
6655
6656 do_pop_from_top_level ();
6657}
6658
44e00a7a
NS
6659/* Add TARGET to USINGS, if it does not already exist there.
6660 We used to build the complete graph of usings at this point, from
6661 the POV of the source namespaces. Now we build that as we perform
6662 the unqualified search. */
65cc1407
NS
6663
6664static void
3c9feefc 6665add_using_namespace (vec<tree, va_gc> *&usings, tree target)
65cc1407 6666{
3c9feefc
NS
6667 if (usings)
6668 for (unsigned ix = usings->length (); ix--;)
6669 if ((*usings)[ix] == target)
6670 return;
65cc1407 6671
3c9feefc 6672 vec_safe_push (usings, target);
65cc1407
NS
6673}
6674
44e00a7a 6675/* Tell the debug system of a using directive. */
65cc1407
NS
6676
6677static void
e071b767 6678emit_debug_info_using_namespace (tree from, tree target, bool implicit)
65cc1407 6679{
44e00a7a
NS
6680 /* Emit debugging info. */
6681 tree context = from != global_namespace ? from : NULL_TREE;
e071b767
JJ
6682 debug_hooks->imported_module_or_decl (target, NULL_TREE, context, false,
6683 implicit);
65cc1407
NS
6684}
6685
6686/* Process a namespace-scope using directive. */
6687
6688void
6689finish_namespace_using_directive (tree target, tree attribs)
6690{
6691 gcc_checking_assert (namespace_bindings_p ());
6692 if (target == error_mark_node)
6693 return;
6694
44e00a7a 6695 add_using_namespace (DECL_NAMESPACE_USING (current_namespace),
65cc1407 6696 ORIGINAL_NAMESPACE (target));
44e00a7a 6697 emit_debug_info_using_namespace (current_namespace,
e071b767 6698 ORIGINAL_NAMESPACE (target), false);
65cc1407
NS
6699
6700 if (attribs == error_mark_node)
6701 return;
6702
6703 for (tree a = attribs; a; a = TREE_CHAIN (a))
6704 {
6705 tree name = get_attribute_name (a);
6706 if (is_attribute_p ("strong", name))
6707 {
6708 warning (0, "strong using directive no longer supported");
6709 if (CP_DECL_CONTEXT (target) == current_namespace)
6710 inform (DECL_SOURCE_LOCATION (target),
6711 "you may use an inline namespace instead");
6712 }
6713 else
6714 warning (OPT_Wattributes, "%qD attribute directive ignored", name);
6715 }
6716}
6717
6718/* Process a function-scope using-directive. */
6719
6720void
6721finish_local_using_directive (tree target, tree attribs)
6722{
6723 gcc_checking_assert (local_bindings_p ());
6724 if (target == error_mark_node)
6725 return;
6726
6727 if (attribs)
6728 warning (OPT_Wattributes, "attributes ignored on local using directive");
6729
6730 add_stmt (build_stmt (input_location, USING_STMT, target));
6731
44e00a7a
NS
6732 add_using_namespace (current_binding_level->using_directives,
6733 ORIGINAL_NAMESPACE (target));
65cc1407
NS
6734}
6735
c405923d
NS
6736/* Pushes X into the global namespace. */
6737
6738tree
6739pushdecl_top_level (tree x, bool is_friend)
575bfb00
LC
6740{
6741 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
c405923d
NS
6742 do_push_to_top_level ();
6743 x = pushdecl_namespace_level (x, is_friend);
6744 do_pop_from_top_level ();
575bfb00 6745 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
c405923d
NS
6746 return x;
6747}
6748
6749/* Pushes X into the global namespace and calls cp_finish_decl to
6750 register the variable, initializing it with INIT. */
6751
6752tree
6753pushdecl_top_level_and_finish (tree x, tree init)
6754{
6755 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6756 do_push_to_top_level ();
6757 x = pushdecl_namespace_level (x, false);
6758 cp_finish_decl (x, init, false, NULL_TREE, 0);
6759 do_pop_from_top_level ();
6760 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6761 return x;
575bfb00
LC
6762}
6763
945bf9e1
NS
6764/* Enter the namespaces from current_namerspace to NS. */
6765
6766static int
6767push_inline_namespaces (tree ns)
6768{
6769 int count = 0;
6770 if (ns != current_namespace)
6771 {
6772 gcc_assert (ns != global_namespace);
6773 count += push_inline_namespaces (CP_DECL_CONTEXT (ns));
6774 resume_scope (NAMESPACE_LEVEL (ns));
6775 current_namespace = ns;
6776 count++;
6777 }
6778 return count;
6779}
6780
3a77e7cc
NS
6781/* Push into the scope of the NAME namespace. If NAME is NULL_TREE,
6782 then we enter an anonymous namespace. If MAKE_INLINE is true, then
6783 we create an inline namespace (it is up to the caller to check upon
6784 redefinition). Return the number of namespaces entered. */
b655c310 6785
3a77e7cc
NS
6786int
6787push_namespace (tree name, bool make_inline)
b655c310 6788{
b655c310 6789 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3a77e7cc 6790 int count = 0;
b655c310
NS
6791
6792 /* We should not get here if the global_namespace is not yet constructed
6793 nor if NAME designates the global namespace: The global scope is
6794 constructed elsewhere. */
e833f686 6795 gcc_checking_assert (global_namespace != NULL && name != global_identifier);
3a77e7cc 6796
945bf9e1
NS
6797 tree ns = NULL_TREE;
6798 {
6799 name_lookup lookup (name, 0);
6800 if (!lookup.search_qualified (current_namespace, /*usings=*/false))
6801 ;
6802 else if (TREE_CODE (lookup.value) != NAMESPACE_DECL)
6803 ;
6804 else if (tree dna = DECL_NAMESPACE_ALIAS (lookup.value))
6805 {
6806 /* A namespace alias is not allowed here, but if the alias
6807 is for a namespace also inside the current scope,
6808 accept it with a diagnostic. That's better than dying
6809 horribly. */
6810 if (is_nested_namespace (current_namespace, CP_DECL_CONTEXT (dna)))
6811 {
6812 error ("namespace alias %qD not allowed here, "
6813 "assuming %qD", lookup.value, dna);
6814 ns = dna;
6815 }
6816 }
6817 else
6818 ns = lookup.value;
6819 }
b655c310 6820
3a77e7cc 6821 bool new_ns = false;
945bf9e1
NS
6822 if (ns)
6823 /* DR2061. NS might be a member of an inline namespace. We
6824 need to push into those namespaces. */
6825 count += push_inline_namespaces (CP_DECL_CONTEXT (ns));
6826 else
b655c310 6827 {
3a77e7cc 6828 ns = build_lang_decl (NAMESPACE_DECL, name, void_type_node);
322763f5
NS
6829 SCOPE_DEPTH (ns) = SCOPE_DEPTH (current_namespace) + 1;
6830 if (!SCOPE_DEPTH (ns))
6831 /* We only allow depth 255. */
6832 sorry ("cannot nest more than %d namespaces",
6833 SCOPE_DEPTH (current_namespace));
3a77e7cc
NS
6834 DECL_CONTEXT (ns) = FROB_CONTEXT (current_namespace);
6835 new_ns = true;
6836
6837 if (pushdecl (ns) == error_mark_node)
6838 ns = NULL_TREE;
b655c310 6839 else
b655c310 6840 {
e833f686 6841 if (!name)
3a77e7cc 6842 {
e833f686 6843 SET_DECL_ASSEMBLER_NAME (ns, anon_identifier);
3a77e7cc
NS
6844
6845 if (!make_inline)
44e00a7a
NS
6846 add_using_namespace (DECL_NAMESPACE_USING (current_namespace),
6847 ns);
3a77e7cc
NS
6848 }
6849 else if (TREE_PUBLIC (current_namespace))
6850 TREE_PUBLIC (ns) = 1;
6851
6852 if (make_inline)
3c9feefc
NS
6853 {
6854 DECL_NAMESPACE_INLINE_P (ns) = true;
6855 vec_safe_push (DECL_NAMESPACE_INLINEES (current_namespace), ns);
6856 }
e071b767 6857
e833f686 6858 if (!name || make_inline)
e071b767 6859 emit_debug_info_using_namespace (current_namespace, ns, true);
b655c310 6860 }
3a77e7cc
NS
6861 }
6862
6863 if (ns)
6864 {
6865 if (make_inline && !DECL_NAMESPACE_INLINE_P (ns))
b655c310 6866 {
3a77e7cc
NS
6867 error ("inline namespace must be specified at initial definition");
6868 inform (DECL_SOURCE_LOCATION (ns), "%qD defined here", ns);
b655c310 6869 }
3a77e7cc
NS
6870 if (new_ns)
6871 begin_scope (sk_namespace, ns);
6872 else
6873 resume_scope (NAMESPACE_LEVEL (ns));
6874 current_namespace = ns;
6875 count++;
b655c310 6876 }
b655c310
NS
6877
6878 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3a77e7cc 6879 return count;
b655c310
NS
6880}
6881
6882/* Pop from the scope of the current namespace. */
6883
6884void
6885pop_namespace (void)
6886{
3c9feefc
NS
6887 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6888
b655c310
NS
6889 gcc_assert (current_namespace != global_namespace);
6890 current_namespace = CP_DECL_CONTEXT (current_namespace);
6891 /* The binding level is not popped, as it might be re-opened later. */
6892 leave_scope ();
3c9feefc
NS
6893
6894 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
b655c310
NS
6895}
6896
c405923d 6897/* External entry points for do_{push_to/pop_from}_top_level. */
b655c310
NS
6898
6899void
c405923d 6900push_to_top_level (void)
b655c310 6901{
c405923d
NS
6902 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6903 do_push_to_top_level ();
6904 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
b655c310
NS
6905}
6906
c405923d
NS
6907void
6908pop_from_top_level (void)
6909{
6910 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6911 do_pop_from_top_level ();
6912 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6913}
6914
6915/* External entry points for do_{push,pop}_nested_namespace. */
6916
6917void
6918push_nested_namespace (tree ns)
6919{
6920 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6921 do_push_nested_namespace (ns);
6922 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6923}
b655c310
NS
6924
6925void
6926pop_nested_namespace (tree ns)
6927{
6928 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6929 gcc_assert (current_namespace == ns);
c405923d 6930 do_pop_nested_namespace (ns);
b655c310
NS
6931 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6932}
575bfb00 6933
00e8de68 6934/* Pop off extraneous binding levels left over due to syntax errors.
00e8de68
GDR
6935 We don't pop past namespaces, as they might be valid. */
6936
6937void
6938pop_everything (void)
6939{
6940 if (ENABLE_SCOPE_CHECKING)
6941 verbatim ("XXX entering pop_everything ()\n");
056a17ee 6942 while (!namespace_bindings_p ())
00e8de68
GDR
6943 {
6944 if (current_binding_level->kind == sk_class)
6945 pop_nested_class ();
6946 else
6947 poplevel (0, 0, 0);
6948 }
6949 if (ENABLE_SCOPE_CHECKING)
6950 verbatim ("XXX leaving pop_everything ()\n");
6951}
6952
6097b0c3 6953/* Emit debugging information for using declarations and directives.
c8094d83 6954 If input tree is overloaded fn then emit debug info for all
6097b0c3
DP
6955 candidates. */
6956
98ed9dae 6957void
6097b0c3
DP
6958cp_emit_debug_info_for_using (tree t, tree context)
6959{
099f36ab 6960 /* Don't try to emit any debug information if we have errors. */
1da2ed5f 6961 if (seen_error ())
099f36ab
JM
6962 return;
6963
c8094d83 6964 /* Ignore this FUNCTION_DECL if it refers to a builtin declaration
6097b0c3 6965 of a builtin function. */
c8094d83 6966 if (TREE_CODE (t) == FUNCTION_DECL
6097b0c3
DP
6967 && DECL_EXTERNAL (t)
6968 && DECL_BUILT_IN (t))
6969 return;
6970
6971 /* Do not supply context to imported_module_or_decl, if
6972 it is a global namespace. */
6973 if (context == global_namespace)
6974 context = NULL_TREE;
c8094d83 6975
e1cad930 6976 t = MAYBE_BASELINK_FUNCTIONS (t);
c8094d83 6977
6097b0c3 6978 /* FIXME: Handle TEMPLATE_DECLs. */
87c976ae
NS
6979 for (lkp_iterator iter (t); iter; ++iter)
6980 {
6981 tree fn = *iter;
6982 if (TREE_CODE (fn) != TEMPLATE_DECL)
6983 {
6984 if (building_stmt_list_p ())
6985 add_stmt (build_stmt (input_location, USING_STMT, fn));
6986 else
e071b767
JJ
6987 debug_hooks->imported_module_or_decl (fn, NULL_TREE, context,
6988 false, false);
87c976ae
NS
6989 }
6990 }
98ed9dae 6991}
6097b0c3 6992
28ea4c88 6993#include "gt-cp-name-lookup.h"