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