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