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