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