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