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