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