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