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