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