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