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