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