[PR c++/84733] ICE in check-local-shadow
[official-gcc.git] / gcc / cp / name-lookup.c
blob80a92ab0ac4874109eaaf0c02b6ae386d669c776
1 /* Definitions for C++ name lookup routines.
2 Copyright (C) 2003-2018 Free Software Foundation, Inc.
3 Contributed by Gabriel Dos Reis <gdr@integrable-solutions.net>
5 This file is part of GCC.
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.
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.
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/>. */
21 #include "config.h"
22 #define INCLUDE_UNIQUE_PTR
23 #include "system.h"
24 #include "coretypes.h"
25 #include "cp-tree.h"
26 #include "timevar.h"
27 #include "stringpool.h"
28 #include "print-tree.h"
29 #include "attribs.h"
30 #include "debug.h"
31 #include "c-family/c-pragma.h"
32 #include "params.h"
33 #include "gcc-rich-location.h"
34 #include "spellcheck-tree.h"
35 #include "parser.h"
36 #include "c-family/name-hint.h"
37 #include "c-family/known-headers.h"
38 #include "c-family/c-spellcheck.h"
40 static cxx_binding *cxx_binding_make (tree value, tree type);
41 static cp_binding_level *innermost_nonclass_level (void);
42 static void set_identifier_type_value_with_scope (tree id, tree decl,
43 cp_binding_level *b);
45 /* Create an overload suitable for recording an artificial TYPE_DECL
46 and another decl. We use this machanism to implement the struct
47 stat hack within a namespace. It'd be nice to use it everywhere. */
49 #define STAT_HACK_P(N) ((N) && TREE_CODE (N) == OVERLOAD && OVL_LOOKUP_P (N))
50 #define STAT_TYPE(N) TREE_TYPE (N)
51 #define STAT_DECL(N) OVL_FUNCTION (N)
52 #define MAYBE_STAT_DECL(N) (STAT_HACK_P (N) ? STAT_DECL (N) : N)
53 #define MAYBE_STAT_TYPE(N) (STAT_HACK_P (N) ? STAT_TYPE (N) : NULL_TREE)
55 /* Create a STAT_HACK node with DECL as the value binding and TYPE as
56 the type binding. */
58 static tree
59 stat_hack (tree decl = NULL_TREE, tree type = NULL_TREE)
61 tree result = make_node (OVERLOAD);
63 /* Mark this as a lookup, so we can tell this is a stat hack. */
64 OVL_LOOKUP_P (result) = true;
65 STAT_DECL (result) = decl;
66 STAT_TYPE (result) = type;
67 return result;
70 /* Create a local binding level for NAME. */
72 static cxx_binding *
73 create_local_binding (cp_binding_level *level, tree name)
75 cxx_binding *binding = cxx_binding_make (NULL, NULL);
77 INHERITED_VALUE_BINDING_P (binding) = false;
78 LOCAL_BINDING_P (binding) = true;
79 binding->scope = level;
80 binding->previous = IDENTIFIER_BINDING (name);
82 IDENTIFIER_BINDING (name) = binding;
84 return binding;
87 /* Find the binding for NAME in namespace NS. If CREATE_P is true,
88 make an empty binding if there wasn't one. */
90 static tree *
91 find_namespace_slot (tree ns, tree name, bool create_p = false)
93 tree *slot = DECL_NAMESPACE_BINDINGS (ns)
94 ->find_slot_with_hash (name, name ? IDENTIFIER_HASH_VALUE (name) : 0,
95 create_p ? INSERT : NO_INSERT);
96 return slot;
99 static tree
100 find_namespace_value (tree ns, tree name)
102 tree *b = find_namespace_slot (ns, name);
104 return b ? MAYBE_STAT_DECL (*b) : NULL_TREE;
107 /* Add DECL to the list of things declared in B. */
109 static void
110 add_decl_to_level (cp_binding_level *b, tree decl)
112 gcc_assert (b->kind != sk_class);
114 /* Make sure we don't create a circular list. xref_tag can end
115 up pushing the same artificial decl more than once. We
116 should have already detected that in update_binding. */
117 gcc_assert (b->names != decl);
119 /* We build up the list in reverse order, and reverse it later if
120 necessary. */
121 TREE_CHAIN (decl) = b->names;
122 b->names = decl;
124 /* If appropriate, add decl to separate list of statics. We
125 include extern variables because they might turn out to be
126 static later. It's OK for this list to contain a few false
127 positives. */
128 if (b->kind == sk_namespace
129 && ((VAR_P (decl)
130 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
131 || (TREE_CODE (decl) == FUNCTION_DECL
132 && (!TREE_PUBLIC (decl)
133 || decl_anon_ns_mem_p (decl)
134 || DECL_DECLARED_INLINE_P (decl)))))
135 vec_safe_push (static_decls, decl);
138 /* Find the binding for NAME in the local binding level B. */
140 static cxx_binding *
141 find_local_binding (cp_binding_level *b, tree name)
143 if (cxx_binding *binding = IDENTIFIER_BINDING (name))
144 for (;; b = b->level_chain)
146 if (binding->scope == b
147 && !(VAR_P (binding->value)
148 && DECL_DEAD_FOR_LOCAL (binding->value)))
149 return binding;
151 /* Cleanup contours are transparent to the language. */
152 if (b->kind != sk_cleanup)
153 break;
155 return NULL;
158 struct name_lookup
160 public:
161 typedef std::pair<tree, tree> using_pair;
162 typedef vec<using_pair, va_heap, vl_embed> using_queue;
164 public:
165 tree name; /* The identifier being looked for. */
166 tree value; /* A (possibly ambiguous) set of things found. */
167 tree type; /* A type that has been found. */
168 int flags; /* Lookup flags. */
169 bool deduping; /* Full deduping is needed because using declarations
170 are in play. */
171 vec<tree, va_heap, vl_embed> *scopes;
172 name_lookup *previous; /* Previously active lookup. */
174 protected:
175 /* Marked scope stack for outermost name lookup. */
176 static vec<tree, va_heap, vl_embed> *shared_scopes;
177 /* Currently active lookup. */
178 static name_lookup *active;
180 public:
181 name_lookup (tree n, int f = 0)
182 : name (n), value (NULL_TREE), type (NULL_TREE), flags (f),
183 deduping (false), scopes (NULL), previous (NULL)
185 preserve_state ();
187 ~name_lookup ()
189 restore_state ();
192 private: /* Uncopyable, unmovable, unassignable. I am a rock. */
193 name_lookup (const name_lookup &);
194 name_lookup &operator= (const name_lookup &);
196 protected:
197 static bool seen_p (tree scope)
199 return LOOKUP_SEEN_P (scope);
201 static bool found_p (tree scope)
203 return LOOKUP_FOUND_P (scope);
206 void mark_seen (tree scope); /* Mark and add to scope vector. */
207 static void mark_found (tree scope)
209 gcc_checking_assert (seen_p (scope));
210 LOOKUP_FOUND_P (scope) = true;
212 bool see_and_mark (tree scope)
214 bool ret = seen_p (scope);
215 if (!ret)
216 mark_seen (scope);
217 return ret;
219 bool find_and_mark (tree scope);
221 private:
222 void preserve_state ();
223 void restore_state ();
225 private:
226 static tree ambiguous (tree thing, tree current);
227 void add_overload (tree fns);
228 void add_value (tree new_val);
229 void add_type (tree new_type);
230 bool process_binding (tree val_bind, tree type_bind);
232 /* Look in only namespace. */
233 bool search_namespace_only (tree scope);
234 /* Look in namespace and its (recursive) inlines. Ignore using
235 directives. Return true if something found (inc dups). */
236 bool search_namespace (tree scope);
237 /* Look in the using directives of namespace + inlines using
238 qualified lookup rules. */
239 bool search_usings (tree scope);
241 private:
242 using_queue *queue_namespace (using_queue *queue, int depth, tree scope);
243 using_queue *do_queue_usings (using_queue *queue, int depth,
244 vec<tree, va_gc> *usings);
245 using_queue *queue_usings (using_queue *queue, int depth,
246 vec<tree, va_gc> *usings)
248 if (usings)
249 queue = do_queue_usings (queue, depth, usings);
250 return queue;
253 private:
254 void add_fns (tree);
256 void adl_expr (tree);
257 void adl_type (tree);
258 void adl_template_arg (tree);
259 void adl_class (tree);
260 void adl_bases (tree);
261 void adl_class_only (tree);
262 void adl_namespace (tree);
263 void adl_namespace_only (tree);
265 public:
266 /* Search namespace + inlines + maybe usings as qualified lookup. */
267 bool search_qualified (tree scope, bool usings = true);
269 /* Search namespace + inlines + usings as unqualified lookup. */
270 bool search_unqualified (tree scope, cp_binding_level *);
272 /* ADL lookup of ARGS. */
273 tree search_adl (tree fns, vec<tree, va_gc> *args);
276 /* Scope stack shared by all outermost lookups. This avoids us
277 allocating and freeing on every single lookup. */
278 vec<tree, va_heap, vl_embed> *name_lookup::shared_scopes;
280 /* Currently active lookup. */
281 name_lookup *name_lookup::active;
283 /* Name lookup is recursive, becase ADL can cause template
284 instatiation. This is of course a rare event, so we optimize for
285 it not happening. When we discover an active name-lookup, which
286 must be an ADL lookup, we need to unmark the marked scopes and also
287 unmark the lookup we might have been accumulating. */
289 void
290 name_lookup::preserve_state ()
292 previous = active;
293 if (previous)
295 unsigned length = vec_safe_length (previous->scopes);
296 vec_safe_reserve (previous->scopes, length * 2);
297 for (unsigned ix = length; ix--;)
299 tree decl = (*previous->scopes)[ix];
301 gcc_checking_assert (LOOKUP_SEEN_P (decl));
302 LOOKUP_SEEN_P (decl) = false;
304 /* Preserve the FOUND_P state on the interrupted lookup's
305 stack. */
306 if (LOOKUP_FOUND_P (decl))
308 LOOKUP_FOUND_P (decl) = false;
309 previous->scopes->quick_push (decl);
313 /* Unmark the outer partial lookup. */
314 if (previous->deduping)
315 lookup_mark (previous->value, false);
317 else
318 scopes = shared_scopes;
319 active = this;
322 /* Restore the marking state of a lookup we interrupted. */
324 void
325 name_lookup::restore_state ()
327 if (deduping)
328 lookup_mark (value, false);
330 /* Unmark and empty this lookup's scope stack. */
331 for (unsigned ix = vec_safe_length (scopes); ix--;)
333 tree decl = scopes->pop ();
334 gcc_checking_assert (LOOKUP_SEEN_P (decl));
335 LOOKUP_SEEN_P (decl) = false;
336 LOOKUP_FOUND_P (decl) = false;
339 active = previous;
340 if (previous)
342 free (scopes);
344 unsigned length = vec_safe_length (previous->scopes);
345 for (unsigned ix = 0; ix != length; ix++)
347 tree decl = (*previous->scopes)[ix];
348 if (LOOKUP_SEEN_P (decl))
350 /* The remainder of the scope stack must be recording
351 FOUND_P decls, which we want to pop off. */
354 tree decl = previous->scopes->pop ();
355 gcc_checking_assert (LOOKUP_SEEN_P (decl)
356 && !LOOKUP_FOUND_P (decl));
357 LOOKUP_FOUND_P (decl) = true;
359 while (++ix != length);
360 break;
363 gcc_checking_assert (!LOOKUP_FOUND_P (decl));
364 LOOKUP_SEEN_P (decl) = true;
367 /* Remark the outer partial lookup. */
368 if (previous->deduping)
369 lookup_mark (previous->value, true);
371 else
372 shared_scopes = scopes;
375 void
376 name_lookup::mark_seen (tree scope)
378 gcc_checking_assert (!seen_p (scope));
379 LOOKUP_SEEN_P (scope) = true;
380 vec_safe_push (scopes, scope);
383 bool
384 name_lookup::find_and_mark (tree scope)
386 bool result = LOOKUP_FOUND_P (scope);
387 if (!result)
389 LOOKUP_FOUND_P (scope) = true;
390 if (!LOOKUP_SEEN_P (scope))
391 vec_safe_push (scopes, scope);
394 return result;
397 /* THING and CURRENT are ambiguous, concatenate them. */
399 tree
400 name_lookup::ambiguous (tree thing, tree current)
402 if (TREE_CODE (current) != TREE_LIST)
404 current = build_tree_list (NULL_TREE, current);
405 TREE_TYPE (current) = error_mark_node;
407 current = tree_cons (NULL_TREE, thing, current);
408 TREE_TYPE (current) = error_mark_node;
410 return current;
413 /* FNS is a new overload set to add to the exising set. */
415 void
416 name_lookup::add_overload (tree fns)
418 if (!deduping && TREE_CODE (fns) == OVERLOAD)
420 tree probe = fns;
421 if (flags & LOOKUP_HIDDEN)
422 probe = ovl_skip_hidden (probe);
423 if (probe && TREE_CODE (probe) == OVERLOAD && OVL_USING_P (probe))
425 /* We're about to add something found by a using
426 declaration, so need to engage deduping mode. */
427 lookup_mark (value, true);
428 deduping = true;
432 value = lookup_maybe_add (fns, value, deduping);
435 /* Add a NEW_VAL, a found value binding into the current value binding. */
437 void
438 name_lookup::add_value (tree new_val)
440 if (OVL_P (new_val) && (!value || OVL_P (value)))
441 add_overload (new_val);
442 else if (!value)
443 value = new_val;
444 else if (value == new_val)
446 else if ((TREE_CODE (value) == TYPE_DECL
447 && TREE_CODE (new_val) == TYPE_DECL
448 && same_type_p (TREE_TYPE (value), TREE_TYPE (new_val))))
449 /* Typedefs to the same type. */;
450 else if (TREE_CODE (value) == NAMESPACE_DECL
451 && TREE_CODE (new_val) == NAMESPACE_DECL
452 && ORIGINAL_NAMESPACE (value) == ORIGINAL_NAMESPACE (new_val))
453 /* Namespace (possibly aliased) to the same namespace. Locate
454 the namespace*/
455 value = ORIGINAL_NAMESPACE (value);
456 else
458 if (deduping)
460 /* Disengage deduping mode. */
461 lookup_mark (value, false);
462 deduping = false;
464 value = ambiguous (new_val, value);
468 /* Add a NEW_TYPE, a found type binding into the current type binding. */
470 void
471 name_lookup::add_type (tree new_type)
473 if (!type)
474 type = new_type;
475 else if (TREE_CODE (type) == TREE_LIST
476 || !same_type_p (TREE_TYPE (type), TREE_TYPE (new_type)))
477 type = ambiguous (new_type, type);
480 /* Process a found binding containing NEW_VAL and NEW_TYPE. Returns
481 true if we actually found something noteworthy. */
483 bool
484 name_lookup::process_binding (tree new_val, tree new_type)
486 /* Did we really see a type? */
487 if (new_type
488 && (LOOKUP_NAMESPACES_ONLY (flags)
489 || (!(flags & LOOKUP_HIDDEN)
490 && DECL_LANG_SPECIFIC (new_type)
491 && DECL_ANTICIPATED (new_type))))
492 new_type = NULL_TREE;
494 if (new_val && !(flags & LOOKUP_HIDDEN))
495 new_val = ovl_skip_hidden (new_val);
497 /* Do we really see a value? */
498 if (new_val)
499 switch (TREE_CODE (new_val))
501 case TEMPLATE_DECL:
502 /* If we expect types or namespaces, and not templates,
503 or this is not a template class. */
504 if ((LOOKUP_QUALIFIERS_ONLY (flags)
505 && !DECL_TYPE_TEMPLATE_P (new_val)))
506 new_val = NULL_TREE;
507 break;
508 case TYPE_DECL:
509 if (LOOKUP_NAMESPACES_ONLY (flags)
510 || (new_type && (flags & LOOKUP_PREFER_TYPES)))
511 new_val = NULL_TREE;
512 break;
513 case NAMESPACE_DECL:
514 if (LOOKUP_TYPES_ONLY (flags))
515 new_val = NULL_TREE;
516 break;
517 default:
518 if (LOOKUP_QUALIFIERS_ONLY (flags))
519 new_val = NULL_TREE;
522 if (!new_val)
524 new_val = new_type;
525 new_type = NULL_TREE;
528 /* Merge into the lookup */
529 if (new_val)
530 add_value (new_val);
531 if (new_type)
532 add_type (new_type);
534 return new_val != NULL_TREE;
537 /* Look in exactly namespace SCOPE. */
539 bool
540 name_lookup::search_namespace_only (tree scope)
542 bool found = false;
544 if (tree *binding = find_namespace_slot (scope, name))
545 found |= process_binding (MAYBE_STAT_DECL (*binding),
546 MAYBE_STAT_TYPE (*binding));
548 return found;
551 /* Conditionally look in namespace SCOPE and inline children. */
553 bool
554 name_lookup::search_namespace (tree scope)
556 if (see_and_mark (scope))
557 /* We've visited this scope before. Return what we found then. */
558 return found_p (scope);
560 /* Look in exactly namespace. */
561 bool found = search_namespace_only (scope);
563 /* Recursively look in its inline children. */
564 if (vec<tree, va_gc> *inlinees = DECL_NAMESPACE_INLINEES (scope))
565 for (unsigned ix = inlinees->length (); ix--;)
566 found |= search_namespace ((*inlinees)[ix]);
568 if (found)
569 mark_found (scope);
571 return found;
574 /* Recursively follow using directives of SCOPE & its inline children.
575 Such following is essentially a flood-fill algorithm. */
577 bool
578 name_lookup::search_usings (tree scope)
580 /* We do not check seen_p here, as that was already set during the
581 namespace_only walk. */
582 if (found_p (scope))
583 return true;
585 bool found = false;
586 if (vec<tree, va_gc> *usings = DECL_NAMESPACE_USING (scope))
587 for (unsigned ix = usings->length (); ix--;)
588 found |= search_qualified ((*usings)[ix], true);
590 /* Look in its inline children. */
591 if (vec<tree, va_gc> *inlinees = DECL_NAMESPACE_INLINEES (scope))
592 for (unsigned ix = inlinees->length (); ix--;)
593 found |= search_usings ((*inlinees)[ix]);
595 if (found)
596 mark_found (scope);
598 return found;
601 /* Qualified namespace lookup in SCOPE.
602 1) Look in SCOPE (+inlines). If found, we're done.
603 2) Otherwise, if USINGS is true,
604 recurse for every using directive of SCOPE (+inlines).
606 Trickiness is (a) loops and (b) multiple paths to same namespace.
607 In both cases we want to not repeat any lookups, and know whether
608 to stop the caller's step #2. Do this via the FOUND_P marker. */
610 bool
611 name_lookup::search_qualified (tree scope, bool usings)
613 bool found = false;
615 if (seen_p (scope))
616 found = found_p (scope);
617 else
619 found = search_namespace (scope);
620 if (!found && usings)
621 found = search_usings (scope);
624 return found;
627 /* Add SCOPE to the unqualified search queue, recursively add its
628 inlines and those via using directives. */
630 name_lookup::using_queue *
631 name_lookup::queue_namespace (using_queue *queue, int depth, tree scope)
633 if (see_and_mark (scope))
634 return queue;
636 /* Record it. */
637 tree common = scope;
638 while (SCOPE_DEPTH (common) > depth)
639 common = CP_DECL_CONTEXT (common);
640 vec_safe_push (queue, using_pair (common, scope));
642 /* Queue its inline children. */
643 if (vec<tree, va_gc> *inlinees = DECL_NAMESPACE_INLINEES (scope))
644 for (unsigned ix = inlinees->length (); ix--;)
645 queue = queue_namespace (queue, depth, (*inlinees)[ix]);
647 /* Queue its using targets. */
648 queue = queue_usings (queue, depth, DECL_NAMESPACE_USING (scope));
650 return queue;
653 /* Add the namespaces in USINGS to the unqualified search queue. */
655 name_lookup::using_queue *
656 name_lookup::do_queue_usings (using_queue *queue, int depth,
657 vec<tree, va_gc> *usings)
659 for (unsigned ix = usings->length (); ix--;)
660 queue = queue_namespace (queue, depth, (*usings)[ix]);
662 return queue;
665 /* Unqualified namespace lookup in SCOPE.
666 1) add scope+inlins to worklist.
667 2) recursively add target of every using directive
668 3) for each worklist item where SCOPE is common ancestor, search it
669 4) if nothing find, scope=parent, goto 1. */
671 bool
672 name_lookup::search_unqualified (tree scope, cp_binding_level *level)
674 /* Make static to avoid continual reallocation. We're not
675 recursive. */
676 static using_queue *queue = NULL;
677 bool found = false;
678 int length = vec_safe_length (queue);
680 /* Queue local using-directives. */
681 for (; level->kind != sk_namespace; level = level->level_chain)
682 queue = queue_usings (queue, SCOPE_DEPTH (scope), level->using_directives);
684 for (; !found; scope = CP_DECL_CONTEXT (scope))
686 gcc_assert (!DECL_NAMESPACE_ALIAS (scope));
687 int depth = SCOPE_DEPTH (scope);
689 /* Queue namespaces reachable from SCOPE. */
690 queue = queue_namespace (queue, depth, scope);
692 /* Search every queued namespace where SCOPE is the common
693 ancestor. Adjust the others. */
694 unsigned ix = length;
697 using_pair &pair = (*queue)[ix];
698 while (pair.first == scope)
700 found |= search_namespace_only (pair.second);
701 pair = queue->pop ();
702 if (ix == queue->length ())
703 goto done;
705 /* The depth is the same as SCOPE, find the parent scope. */
706 if (SCOPE_DEPTH (pair.first) == depth)
707 pair.first = CP_DECL_CONTEXT (pair.first);
708 ix++;
710 while (ix < queue->length ());
711 done:;
712 if (scope == global_namespace)
713 break;
715 /* If looking for hidden names, we only look in the innermost
716 namespace scope. [namespace.memdef]/3 If a friend
717 declaration in a non-local class first declares a class,
718 function, class template or function template the friend is a
719 member of the innermost enclosing namespace. See also
720 [basic.lookup.unqual]/7 */
721 if (flags & LOOKUP_HIDDEN)
722 break;
725 vec_safe_truncate (queue, length);
727 return found;
730 /* FNS is a value binding. If it is a (set of overloaded) functions,
731 add them into the current value. */
733 void
734 name_lookup::add_fns (tree fns)
736 if (!fns)
737 return;
738 else if (TREE_CODE (fns) == OVERLOAD)
740 if (TREE_TYPE (fns) != unknown_type_node)
741 fns = OVL_FUNCTION (fns);
743 else if (!DECL_DECLARES_FUNCTION_P (fns))
744 return;
746 add_overload (fns);
749 /* Add functions of a namespace to the lookup structure. */
751 void
752 name_lookup::adl_namespace_only (tree scope)
754 mark_seen (scope);
756 /* Look down into inline namespaces. */
757 if (vec<tree, va_gc> *inlinees = DECL_NAMESPACE_INLINEES (scope))
758 for (unsigned ix = inlinees->length (); ix--;)
759 adl_namespace_only ((*inlinees)[ix]);
761 if (tree fns = find_namespace_value (scope, name))
762 add_fns (ovl_skip_hidden (fns));
765 /* Find the containing non-inlined namespace, add it and all its
766 inlinees. */
768 void
769 name_lookup::adl_namespace (tree scope)
771 if (seen_p (scope))
772 return;
774 /* Find the containing non-inline namespace. */
775 while (DECL_NAMESPACE_INLINE_P (scope))
776 scope = CP_DECL_CONTEXT (scope);
778 adl_namespace_only (scope);
781 /* Adds the class and its friends to the lookup structure. */
783 void
784 name_lookup::adl_class_only (tree type)
786 /* Backend-built structures, such as __builtin_va_list, aren't
787 affected by all this. */
788 if (!CLASS_TYPE_P (type))
789 return;
791 type = TYPE_MAIN_VARIANT (type);
793 if (see_and_mark (type))
794 return;
796 tree context = decl_namespace_context (type);
797 adl_namespace (context);
799 complete_type (type);
801 /* Add friends. */
802 for (tree list = DECL_FRIENDLIST (TYPE_MAIN_DECL (type)); list;
803 list = TREE_CHAIN (list))
804 if (name == FRIEND_NAME (list))
805 for (tree friends = FRIEND_DECLS (list); friends;
806 friends = TREE_CHAIN (friends))
808 tree fn = TREE_VALUE (friends);
810 /* Only interested in global functions with potentially hidden
811 (i.e. unqualified) declarations. */
812 if (CP_DECL_CONTEXT (fn) != context)
813 continue;
815 /* Only interested in anticipated friends. (Non-anticipated
816 ones will have been inserted during the namespace
817 adl.) */
818 if (!DECL_ANTICIPATED (fn))
819 continue;
821 /* Template specializations are never found by name lookup.
822 (Templates themselves can be found, but not template
823 specializations.) */
824 if (TREE_CODE (fn) == FUNCTION_DECL && DECL_USE_TEMPLATE (fn))
825 continue;
827 add_fns (fn);
831 /* Adds the class and its bases to the lookup structure.
832 Returns true on error. */
834 void
835 name_lookup::adl_bases (tree type)
837 adl_class_only (type);
839 /* Process baseclasses. */
840 if (tree binfo = TYPE_BINFO (type))
842 tree base_binfo;
843 int i;
845 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
846 adl_bases (BINFO_TYPE (base_binfo));
850 /* Adds everything associated with a class argument type to the lookup
851 structure. Returns true on error.
853 If T is a class type (including unions), its associated classes are: the
854 class itself; the class of which it is a member, if any; and its direct
855 and indirect base classes. Its associated namespaces are the namespaces
856 of which its associated classes are members. Furthermore, if T is a
857 class template specialization, its associated namespaces and classes
858 also include: the namespaces and classes associated with the types of
859 the template arguments provided for template type parameters (excluding
860 template template parameters); the namespaces of which any template
861 template arguments are members; and the classes of which any member
862 templates used as template template arguments are members. [ Note:
863 non-type template arguments do not contribute to the set of associated
864 namespaces. --end note] */
866 void
867 name_lookup::adl_class (tree type)
869 /* Backend build structures, such as __builtin_va_list, aren't
870 affected by all this. */
871 if (!CLASS_TYPE_P (type))
872 return;
874 type = TYPE_MAIN_VARIANT (type);
875 /* We don't set found here because we have to have set seen first,
876 which is done in the adl_bases walk. */
877 if (found_p (type))
878 return;
880 adl_bases (type);
881 mark_found (type);
883 if (TYPE_CLASS_SCOPE_P (type))
884 adl_class_only (TYPE_CONTEXT (type));
886 /* Process template arguments. */
887 if (CLASSTYPE_TEMPLATE_INFO (type)
888 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
890 tree list = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
891 for (int i = 0; i < TREE_VEC_LENGTH (list); ++i)
892 adl_template_arg (TREE_VEC_ELT (list, i));
896 void
897 name_lookup::adl_expr (tree expr)
899 if (!expr)
900 return;
902 gcc_assert (!TYPE_P (expr));
904 if (TREE_TYPE (expr) != unknown_type_node)
906 adl_type (TREE_TYPE (expr));
907 return;
910 if (TREE_CODE (expr) == ADDR_EXPR)
911 expr = TREE_OPERAND (expr, 0);
912 if (TREE_CODE (expr) == COMPONENT_REF
913 || TREE_CODE (expr) == OFFSET_REF)
914 expr = TREE_OPERAND (expr, 1);
915 expr = MAYBE_BASELINK_FUNCTIONS (expr);
917 if (OVL_P (expr))
918 for (lkp_iterator iter (expr); iter; ++iter)
919 adl_type (TREE_TYPE (*iter));
920 else if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
922 /* The working paper doesn't currently say how to handle
923 template-id arguments. The sensible thing would seem to be
924 to handle the list of template candidates like a normal
925 overload set, and handle the template arguments like we do
926 for class template specializations. */
928 /* First the templates. */
929 adl_expr (TREE_OPERAND (expr, 0));
931 /* Now the arguments. */
932 if (tree args = TREE_OPERAND (expr, 1))
933 for (int ix = TREE_VEC_LENGTH (args); ix--;)
934 adl_template_arg (TREE_VEC_ELT (args, ix));
938 void
939 name_lookup::adl_type (tree type)
941 if (!type)
942 return;
944 if (TYPE_PTRDATAMEM_P (type))
946 /* Pointer to member: associate class type and value type. */
947 adl_type (TYPE_PTRMEM_CLASS_TYPE (type));
948 adl_type (TYPE_PTRMEM_POINTED_TO_TYPE (type));
949 return;
952 switch (TREE_CODE (type))
954 case RECORD_TYPE:
955 if (TYPE_PTRMEMFUNC_P (type))
957 adl_type (TYPE_PTRMEMFUNC_FN_TYPE (type));
958 return;
960 /* FALLTHRU */
961 case UNION_TYPE:
962 adl_class (type);
963 return;
965 case METHOD_TYPE:
966 /* The basetype is referenced in the first arg type, so just
967 fall through. */
968 case FUNCTION_TYPE:
969 /* Associate the parameter types. */
970 for (tree args = TYPE_ARG_TYPES (type); args; args = TREE_CHAIN (args))
971 adl_type (TREE_VALUE (args));
972 /* FALLTHROUGH */
974 case POINTER_TYPE:
975 case REFERENCE_TYPE:
976 case ARRAY_TYPE:
977 adl_type (TREE_TYPE (type));
978 return;
980 case ENUMERAL_TYPE:
981 if (TYPE_CLASS_SCOPE_P (type))
982 adl_class_only (TYPE_CONTEXT (type));
983 adl_namespace (decl_namespace_context (type));
984 return;
986 case LANG_TYPE:
987 gcc_assert (type == unknown_type_node
988 || type == init_list_type_node);
989 return;
991 case TYPE_PACK_EXPANSION:
992 adl_type (PACK_EXPANSION_PATTERN (type));
993 return;
995 default:
996 break;
1000 /* Adds everything associated with a template argument to the lookup
1001 structure. */
1003 void
1004 name_lookup::adl_template_arg (tree arg)
1006 /* [basic.lookup.koenig]
1008 If T is a template-id, its associated namespaces and classes are
1009 ... the namespaces and classes associated with the types of the
1010 template arguments provided for template type parameters
1011 (excluding template template parameters); the namespaces in which
1012 any template template arguments are defined; and the classes in
1013 which any member templates used as template template arguments
1014 are defined. [Note: non-type template arguments do not
1015 contribute to the set of associated namespaces. ] */
1017 /* Consider first template template arguments. */
1018 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
1019 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
1021 else if (TREE_CODE (arg) == TEMPLATE_DECL)
1023 tree ctx = CP_DECL_CONTEXT (arg);
1025 /* It's not a member template. */
1026 if (TREE_CODE (ctx) == NAMESPACE_DECL)
1027 adl_namespace (ctx);
1028 /* Otherwise, it must be member template. */
1029 else
1030 adl_class_only (ctx);
1032 /* It's an argument pack; handle it recursively. */
1033 else if (ARGUMENT_PACK_P (arg))
1035 tree args = ARGUMENT_PACK_ARGS (arg);
1036 int i, len = TREE_VEC_LENGTH (args);
1037 for (i = 0; i < len; ++i)
1038 adl_template_arg (TREE_VEC_ELT (args, i));
1040 /* It's not a template template argument, but it is a type template
1041 argument. */
1042 else if (TYPE_P (arg))
1043 adl_type (arg);
1046 /* Perform ADL lookup. FNS is the existing lookup result and ARGS are
1047 the call arguments. */
1049 tree
1050 name_lookup::search_adl (tree fns, vec<tree, va_gc> *args)
1052 if (fns)
1054 deduping = true;
1055 lookup_mark (fns, true);
1057 value = fns;
1059 unsigned ix;
1060 tree arg;
1062 FOR_EACH_VEC_ELT_REVERSE (*args, ix, arg)
1063 /* OMP reduction operators put an ADL-significant type as the
1064 first arg. */
1065 if (TYPE_P (arg))
1066 adl_type (arg);
1067 else
1068 adl_expr (arg);
1070 fns = value;
1072 return fns;
1075 static bool qualified_namespace_lookup (tree, name_lookup *);
1076 static void consider_binding_level (tree name,
1077 best_match <tree, const char *> &bm,
1078 cp_binding_level *lvl,
1079 bool look_within_fields,
1080 enum lookup_name_fuzzy_kind kind);
1081 static void diagnose_name_conflict (tree, tree);
1083 /* ADL lookup of NAME. FNS is the result of regular lookup, and we
1084 don't add duplicates to it. ARGS is the vector of call
1085 arguments (which will not be empty). */
1087 tree
1088 lookup_arg_dependent (tree name, tree fns, vec<tree, va_gc> *args)
1090 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
1091 name_lookup lookup (name);
1092 fns = lookup.search_adl (fns, args);
1093 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
1094 return fns;
1097 /* FNS is an overload set of conversion functions. Return the
1098 overloads converting to TYPE. */
1100 static tree
1101 extract_conversion_operator (tree fns, tree type)
1103 tree convs = NULL_TREE;
1104 tree tpls = NULL_TREE;
1106 for (ovl_iterator iter (fns); iter; ++iter)
1108 if (same_type_p (DECL_CONV_FN_TYPE (*iter), type))
1109 convs = lookup_add (*iter, convs);
1111 if (TREE_CODE (*iter) == TEMPLATE_DECL)
1112 tpls = lookup_add (*iter, tpls);
1115 if (!convs)
1116 convs = tpls;
1118 return convs;
1121 /* Binary search of (ordered) MEMBER_VEC for NAME. */
1123 static tree
1124 member_vec_binary_search (vec<tree, va_gc> *member_vec, tree name)
1126 for (unsigned lo = 0, hi = member_vec->length (); lo < hi;)
1128 unsigned mid = (lo + hi) / 2;
1129 tree binding = (*member_vec)[mid];
1130 tree binding_name = OVL_NAME (binding);
1132 if (binding_name > name)
1133 hi = mid;
1134 else if (binding_name < name)
1135 lo = mid + 1;
1136 else
1137 return binding;
1140 return NULL_TREE;
1143 /* Linear search of (unordered) MEMBER_VEC for NAME. */
1145 static tree
1146 member_vec_linear_search (vec<tree, va_gc> *member_vec, tree name)
1148 for (int ix = member_vec->length (); ix--;)
1149 if (tree binding = (*member_vec)[ix])
1150 if (OVL_NAME (binding) == name)
1151 return binding;
1153 return NULL_TREE;
1156 /* Linear search of (partially ordered) fields of KLASS for NAME. */
1158 static tree
1159 fields_linear_search (tree klass, tree name, bool want_type)
1161 for (tree fields = TYPE_FIELDS (klass); fields; fields = DECL_CHAIN (fields))
1163 tree decl = fields;
1165 if (TREE_CODE (decl) == FIELD_DECL
1166 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
1168 if (tree temp = search_anon_aggr (TREE_TYPE (decl), name, want_type))
1169 return temp;
1172 if (DECL_NAME (decl) != name)
1173 continue;
1175 if (TREE_CODE (decl) == USING_DECL)
1177 decl = strip_using_decl (decl);
1178 if (is_overloaded_fn (decl))
1179 continue;
1182 if (DECL_DECLARES_FUNCTION_P (decl))
1183 /* Functions are found separately. */
1184 continue;
1186 if (!want_type || DECL_DECLARES_TYPE_P (decl))
1187 return decl;
1190 return NULL_TREE;
1193 /* Look for NAME member inside of anonymous aggregate ANON. Although
1194 such things should only contain FIELD_DECLs, we check that too
1195 late, and would give very confusing errors if we weren't
1196 permissive here. */
1198 tree
1199 search_anon_aggr (tree anon, tree name, bool want_type)
1201 gcc_assert (COMPLETE_TYPE_P (anon));
1202 tree ret = get_class_binding_direct (anon, name, want_type);
1203 return ret;
1206 /* Look for NAME as an immediate member of KLASS (including
1207 anon-members or unscoped enum member). TYPE_OR_FNS is zero for
1208 regular search. >0 to get a type binding (if there is one) and <0
1209 if you want (just) the member function binding.
1211 Use this if you do not want lazy member creation. */
1213 tree
1214 get_class_binding_direct (tree klass, tree name, int type_or_fns)
1216 gcc_checking_assert (RECORD_OR_UNION_TYPE_P (klass));
1218 /* Conversion operators can only be found by the marker conversion
1219 operator name. */
1220 bool conv_op = IDENTIFIER_CONV_OP_P (name);
1221 tree lookup = conv_op ? conv_op_identifier : name;
1222 tree val = NULL_TREE;
1223 vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (klass);
1225 if (COMPLETE_TYPE_P (klass) && member_vec)
1227 val = member_vec_binary_search (member_vec, lookup);
1228 if (!val)
1230 else if (type_or_fns > 0)
1232 if (STAT_HACK_P (val))
1233 val = STAT_TYPE (val);
1234 else if (!DECL_DECLARES_TYPE_P (val))
1235 val = NULL_TREE;
1237 else if (STAT_HACK_P (val))
1238 val = STAT_DECL (val);
1240 if (val && TREE_CODE (val) == OVERLOAD
1241 && TREE_CODE (OVL_FUNCTION (val)) == USING_DECL)
1243 /* An overload with a dependent USING_DECL. Does the caller
1244 want the USING_DECL or the functions? */
1245 if (type_or_fns < 0)
1246 val = OVL_CHAIN (val);
1247 else
1248 val = OVL_FUNCTION (val);
1251 else
1253 if (member_vec && type_or_fns <= 0)
1254 val = member_vec_linear_search (member_vec, lookup);
1256 if (type_or_fns < 0)
1257 /* Don't bother looking for field. We don't want it. */;
1258 else if (!val || (TREE_CODE (val) == OVERLOAD && OVL_USING_P (val)))
1259 /* Dependent using declarations are a 'field', make sure we
1260 return that even if we saw an overload already. */
1261 if (tree field_val = fields_linear_search (klass, lookup,
1262 type_or_fns > 0))
1263 if (!val || TREE_CODE (field_val) == USING_DECL)
1264 val = field_val;
1267 /* Extract the conversion operators asked for, unless the general
1268 conversion operator was requested. */
1269 if (val && conv_op)
1271 gcc_checking_assert (OVL_FUNCTION (val) == conv_op_marker);
1272 val = OVL_CHAIN (val);
1273 if (tree type = TREE_TYPE (name))
1274 val = extract_conversion_operator (val, type);
1277 return val;
1280 /* Look for NAME's binding in exactly KLASS. See
1281 get_class_binding_direct for argument description. Does lazy
1282 special function creation as necessary. */
1284 tree
1285 get_class_binding (tree klass, tree name, int type_or_fns)
1287 klass = complete_type (klass);
1289 if (COMPLETE_TYPE_P (klass))
1291 /* Lazily declare functions, if we're going to search these. */
1292 if (IDENTIFIER_CTOR_P (name))
1294 if (CLASSTYPE_LAZY_DEFAULT_CTOR (klass))
1295 lazily_declare_fn (sfk_constructor, klass);
1296 if (CLASSTYPE_LAZY_COPY_CTOR (klass))
1297 lazily_declare_fn (sfk_copy_constructor, klass);
1298 if (CLASSTYPE_LAZY_MOVE_CTOR (klass))
1299 lazily_declare_fn (sfk_move_constructor, klass);
1301 else if (IDENTIFIER_DTOR_P (name))
1303 if (CLASSTYPE_LAZY_DESTRUCTOR (klass))
1304 lazily_declare_fn (sfk_destructor, klass);
1306 else if (name == assign_op_identifier)
1308 if (CLASSTYPE_LAZY_COPY_ASSIGN (klass))
1309 lazily_declare_fn (sfk_copy_assignment, klass);
1310 if (CLASSTYPE_LAZY_MOVE_ASSIGN (klass))
1311 lazily_declare_fn (sfk_move_assignment, klass);
1315 return get_class_binding_direct (klass, name, type_or_fns);
1318 /* Find the slot containing overloads called 'NAME'. If there is no
1319 such slot and the class is complete, create an empty one, at the
1320 correct point in the sorted member vector. Otherwise return NULL.
1321 Deals with conv_op marker handling. */
1323 tree *
1324 find_member_slot (tree klass, tree name)
1326 bool complete_p = COMPLETE_TYPE_P (klass);
1328 vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (klass);
1329 if (!member_vec)
1331 vec_alloc (member_vec, 8);
1332 CLASSTYPE_MEMBER_VEC (klass) = member_vec;
1333 if (complete_p)
1335 /* If the class is complete but had no member_vec, we need
1336 to add the TYPE_FIELDS into it. We're also most likely
1337 to be adding ctors & dtors, so ask for 6 spare slots (the
1338 abstract cdtors and their clones). */
1339 set_class_bindings (klass, 6);
1340 member_vec = CLASSTYPE_MEMBER_VEC (klass);
1344 if (IDENTIFIER_CONV_OP_P (name))
1345 name = conv_op_identifier;
1347 unsigned ix, length = member_vec->length ();
1348 for (ix = 0; ix < length; ix++)
1350 tree *slot = &(*member_vec)[ix];
1351 tree fn_name = OVL_NAME (*slot);
1353 if (fn_name == name)
1355 /* If we found an existing slot, it must be a function set.
1356 Even with insertion after completion, because those only
1357 happen with artificial fns that have unspellable names.
1358 This means we do not have to deal with the stat hack
1359 either. */
1360 gcc_checking_assert (OVL_P (*slot));
1361 if (name == conv_op_identifier)
1363 gcc_checking_assert (OVL_FUNCTION (*slot) == conv_op_marker);
1364 /* Skip the conv-op marker. */
1365 slot = &OVL_CHAIN (*slot);
1367 return slot;
1370 if (complete_p && fn_name > name)
1371 break;
1374 /* No slot found, add one if the class is complete. */
1375 if (complete_p)
1377 /* Do exact allocation, as we don't expect to add many. */
1378 gcc_assert (name != conv_op_identifier);
1379 vec_safe_reserve_exact (member_vec, 1);
1380 CLASSTYPE_MEMBER_VEC (klass) = member_vec;
1381 member_vec->quick_insert (ix, NULL_TREE);
1382 return &(*member_vec)[ix];
1385 return NULL;
1388 /* KLASS is an incomplete class to which we're adding a method NAME.
1389 Add a slot and deal with conv_op marker handling. */
1391 tree *
1392 add_member_slot (tree klass, tree name)
1394 gcc_assert (!COMPLETE_TYPE_P (klass));
1396 vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (klass);
1397 vec_safe_push (member_vec, NULL_TREE);
1398 CLASSTYPE_MEMBER_VEC (klass) = member_vec;
1400 tree *slot = &member_vec->last ();
1401 if (IDENTIFIER_CONV_OP_P (name))
1403 /* Install the marker prefix. */
1404 *slot = ovl_make (conv_op_marker, NULL_TREE);
1405 slot = &OVL_CHAIN (*slot);
1408 return slot;
1411 /* Comparison function to compare two MEMBER_VEC entries by name.
1412 Because we can have duplicates during insertion of TYPE_FIELDS, we
1413 do extra checking so deduping doesn't have to deal with so many
1414 cases. */
1416 static int
1417 member_name_cmp (const void *a_p, const void *b_p)
1419 tree a = *(const tree *)a_p;
1420 tree b = *(const tree *)b_p;
1421 tree name_a = DECL_NAME (TREE_CODE (a) == OVERLOAD ? OVL_FUNCTION (a) : a);
1422 tree name_b = DECL_NAME (TREE_CODE (b) == OVERLOAD ? OVL_FUNCTION (b) : b);
1424 gcc_checking_assert (name_a && name_b);
1425 if (name_a != name_b)
1426 return name_a < name_b ? -1 : +1;
1428 if (name_a == conv_op_identifier)
1430 /* Strip the conv-op markers. */
1431 gcc_checking_assert (OVL_FUNCTION (a) == conv_op_marker
1432 && OVL_FUNCTION (b) == conv_op_marker);
1433 a = OVL_CHAIN (a);
1434 b = OVL_CHAIN (b);
1437 if (TREE_CODE (a) == OVERLOAD)
1438 a = OVL_FUNCTION (a);
1439 if (TREE_CODE (b) == OVERLOAD)
1440 b = OVL_FUNCTION (b);
1442 /* We're in STAT_HACK or USING_DECL territory (or possibly error-land). */
1443 if (TREE_CODE (a) != TREE_CODE (b))
1445 /* If one of them is a TYPE_DECL, it loses. */
1446 if (TREE_CODE (a) == TYPE_DECL)
1447 return +1;
1448 else if (TREE_CODE (b) == TYPE_DECL)
1449 return -1;
1451 /* If one of them is a USING_DECL, it loses. */
1452 if (TREE_CODE (a) == USING_DECL)
1453 return +1;
1454 else if (TREE_CODE (b) == USING_DECL)
1455 return -1;
1457 /* There are no other cases with different kinds of decls, as
1458 duplicate detection should have kicked in earlier. However,
1459 some erroneous cases get though. */
1460 gcc_assert (errorcount);
1463 /* Using source location would be the best thing here, but we can
1464 get identically-located decls in the following circumstances:
1466 1) duplicate artificial type-decls for the same type.
1468 2) pack expansions of using-decls.
1470 We should not be doing #1, but in either case it doesn't matter
1471 how we order these. Use UID as a proxy for source ordering, so
1472 that identically-located decls still have a well-defined stable
1473 ordering. */
1474 if (DECL_UID (a) != DECL_UID (b))
1475 return DECL_UID (a) < DECL_UID (b) ? -1 : +1;
1476 gcc_assert (a == b);
1477 return 0;
1480 static struct {
1481 gt_pointer_operator new_value;
1482 void *cookie;
1483 } resort_data;
1485 /* This routine compares two fields like member_name_cmp but using the
1486 pointer operator in resort_field_decl_data. We don't have to deal
1487 with duplicates here. */
1489 static int
1490 resort_member_name_cmp (const void *a_p, const void *b_p)
1492 tree a = *(const tree *)a_p;
1493 tree b = *(const tree *)b_p;
1494 tree name_a = OVL_NAME (a);
1495 tree name_b = OVL_NAME (b);
1497 resort_data.new_value (&name_a, resort_data.cookie);
1498 resort_data.new_value (&name_b, resort_data.cookie);
1500 gcc_checking_assert (name_a != name_b);
1502 return name_a < name_b ? -1 : +1;
1505 /* Resort CLASSTYPE_MEMBER_VEC because pointers have been reordered. */
1507 void
1508 resort_type_member_vec (void *obj, void */*orig_obj*/,
1509 gt_pointer_operator new_value, void* cookie)
1511 if (vec<tree, va_gc> *member_vec = (vec<tree, va_gc> *) obj)
1513 resort_data.new_value = new_value;
1514 resort_data.cookie = cookie;
1515 member_vec->qsort (resort_member_name_cmp);
1519 /* Recursively count the number of fields in KLASS, including anonymous
1520 union members. */
1522 static unsigned
1523 count_class_fields (tree klass)
1525 unsigned n_fields = 0;
1527 for (tree fields = TYPE_FIELDS (klass); fields; fields = DECL_CHAIN (fields))
1528 if (DECL_DECLARES_FUNCTION_P (fields))
1529 /* Functions are dealt with separately. */;
1530 else if (TREE_CODE (fields) == FIELD_DECL
1531 && ANON_AGGR_TYPE_P (TREE_TYPE (fields)))
1532 n_fields += count_class_fields (TREE_TYPE (fields));
1533 else if (DECL_NAME (fields))
1534 n_fields += 1;
1536 return n_fields;
1539 /* Append all the nonfunction members fields of KLASS to MEMBER_VEC.
1540 Recurse for anonymous members. MEMBER_VEC must have space. */
1542 static void
1543 member_vec_append_class_fields (vec<tree, va_gc> *member_vec, tree klass)
1545 for (tree fields = TYPE_FIELDS (klass); fields; fields = DECL_CHAIN (fields))
1546 if (DECL_DECLARES_FUNCTION_P (fields))
1547 /* Functions are handled separately. */;
1548 else if (TREE_CODE (fields) == FIELD_DECL
1549 && ANON_AGGR_TYPE_P (TREE_TYPE (fields)))
1550 member_vec_append_class_fields (member_vec, TREE_TYPE (fields));
1551 else if (DECL_NAME (fields))
1553 tree field = fields;
1554 /* Mark a conv-op USING_DECL with the conv-op-marker. */
1555 if (TREE_CODE (field) == USING_DECL
1556 && IDENTIFIER_CONV_OP_P (DECL_NAME (field)))
1557 field = ovl_make (conv_op_marker, field);
1558 member_vec->quick_push (field);
1562 /* Append all of the enum values of ENUMTYPE to MEMBER_VEC.
1563 MEMBER_VEC must have space. */
1565 static void
1566 member_vec_append_enum_values (vec<tree, va_gc> *member_vec, tree enumtype)
1568 for (tree values = TYPE_VALUES (enumtype);
1569 values; values = TREE_CHAIN (values))
1570 member_vec->quick_push (TREE_VALUE (values));
1573 /* MEMBER_VEC has just had new DECLs added to it, but is sorted.
1574 DeDup adjacent DECLS of the same name. We already dealt with
1575 conflict resolution when adding the fields or methods themselves.
1576 There are three cases (which could all be combined):
1577 1) a TYPE_DECL and non TYPE_DECL. Deploy STAT_HACK as appropriate.
1578 2) a USING_DECL and an overload. If the USING_DECL is dependent,
1579 it wins. Otherwise the OVERLOAD does.
1580 3) two USING_DECLS. ...
1582 member_name_cmp will have ordered duplicates as
1583 <fns><using><type> */
1585 static void
1586 member_vec_dedup (vec<tree, va_gc> *member_vec)
1588 unsigned len = member_vec->length ();
1589 unsigned store = 0;
1591 if (!len)
1592 return;
1594 tree name = OVL_NAME ((*member_vec)[0]);
1595 for (unsigned jx, ix = 0; ix < len; ix = jx)
1597 tree current = NULL_TREE;
1598 tree to_type = NULL_TREE;
1599 tree to_using = NULL_TREE;
1600 tree marker = NULL_TREE;
1602 for (jx = ix; jx < len; jx++)
1604 tree next = (*member_vec)[jx];
1605 if (jx != ix)
1607 tree next_name = OVL_NAME (next);
1608 if (next_name != name)
1610 name = next_name;
1611 break;
1615 if (IDENTIFIER_CONV_OP_P (name))
1617 marker = next;
1618 next = OVL_CHAIN (next);
1621 if (TREE_CODE (next) == USING_DECL)
1623 if (IDENTIFIER_CTOR_P (name))
1624 /* Dependent inherited ctor. */
1625 continue;
1627 next = strip_using_decl (next);
1628 if (TREE_CODE (next) == USING_DECL)
1630 to_using = next;
1631 continue;
1634 if (is_overloaded_fn (next))
1635 continue;
1638 if (DECL_DECLARES_TYPE_P (next))
1640 to_type = next;
1641 continue;
1644 if (!current)
1645 current = next;
1648 if (to_using)
1650 if (!current)
1651 current = to_using;
1652 else
1653 current = ovl_make (to_using, current);
1656 if (to_type)
1658 if (!current)
1659 current = to_type;
1660 else
1661 current = stat_hack (current, to_type);
1664 if (current)
1666 if (marker)
1668 OVL_CHAIN (marker) = current;
1669 current = marker;
1671 (*member_vec)[store++] = current;
1675 while (store++ < len)
1676 member_vec->pop ();
1679 /* Add the non-function members to CLASSTYPE_MEMBER_VEC. If there is
1680 no existing MEMBER_VEC and fewer than 8 fields, do nothing. We
1681 know there must be at least 1 field -- the self-reference
1682 TYPE_DECL, except for anon aggregates, which will have at least
1683 one field. */
1685 void
1686 set_class_bindings (tree klass, unsigned extra)
1688 unsigned n_fields = count_class_fields (klass);
1689 vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (klass);
1691 if (member_vec || n_fields >= 8)
1693 /* Append the new fields. */
1694 vec_safe_reserve_exact (member_vec, extra + n_fields);
1695 member_vec_append_class_fields (member_vec, klass);
1698 if (member_vec)
1700 CLASSTYPE_MEMBER_VEC (klass) = member_vec;
1701 member_vec->qsort (member_name_cmp);
1702 member_vec_dedup (member_vec);
1706 /* Insert lately defined enum ENUMTYPE into KLASS for the sorted case. */
1708 void
1709 insert_late_enum_def_bindings (tree klass, tree enumtype)
1711 int n_fields;
1712 vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (klass);
1714 /* The enum bindings will already be on the TYPE_FIELDS, so don't
1715 count them twice. */
1716 if (!member_vec)
1717 n_fields = count_class_fields (klass);
1718 else
1719 n_fields = list_length (TYPE_VALUES (enumtype));
1721 if (member_vec || n_fields >= 8)
1723 vec_safe_reserve_exact (member_vec, n_fields);
1724 if (CLASSTYPE_MEMBER_VEC (klass))
1725 member_vec_append_enum_values (member_vec, enumtype);
1726 else
1727 member_vec_append_class_fields (member_vec, klass);
1728 CLASSTYPE_MEMBER_VEC (klass) = member_vec;
1729 member_vec->qsort (member_name_cmp);
1730 member_vec_dedup (member_vec);
1734 /* Compute the chain index of a binding_entry given the HASH value of its
1735 name and the total COUNT of chains. COUNT is assumed to be a power
1736 of 2. */
1738 #define ENTRY_INDEX(HASH, COUNT) (((HASH) >> 3) & ((COUNT) - 1))
1740 /* A free list of "binding_entry"s awaiting for re-use. */
1742 static GTY((deletable)) binding_entry free_binding_entry = NULL;
1744 /* The binding oracle; see cp-tree.h. */
1746 cp_binding_oracle_function *cp_binding_oracle;
1748 /* If we have a binding oracle, ask it for all namespace-scoped
1749 definitions of NAME. */
1751 static inline void
1752 query_oracle (tree name)
1754 if (!cp_binding_oracle)
1755 return;
1757 /* LOOKED_UP holds the set of identifiers that we have already
1758 looked up with the oracle. */
1759 static hash_set<tree> looked_up;
1760 if (looked_up.add (name))
1761 return;
1763 cp_binding_oracle (CP_ORACLE_IDENTIFIER, name);
1766 /* Create a binding_entry object for (NAME, TYPE). */
1768 static inline binding_entry
1769 binding_entry_make (tree name, tree type)
1771 binding_entry entry;
1773 if (free_binding_entry)
1775 entry = free_binding_entry;
1776 free_binding_entry = entry->chain;
1778 else
1779 entry = ggc_alloc<binding_entry_s> ();
1781 entry->name = name;
1782 entry->type = type;
1783 entry->chain = NULL;
1785 return entry;
1788 /* Put ENTRY back on the free list. */
1789 #if 0
1790 static inline void
1791 binding_entry_free (binding_entry entry)
1793 entry->name = NULL;
1794 entry->type = NULL;
1795 entry->chain = free_binding_entry;
1796 free_binding_entry = entry;
1798 #endif
1800 /* The datatype used to implement the mapping from names to types at
1801 a given scope. */
1802 struct GTY(()) binding_table_s {
1803 /* Array of chains of "binding_entry"s */
1804 binding_entry * GTY((length ("%h.chain_count"))) chain;
1806 /* The number of chains in this table. This is the length of the
1807 member "chain" considered as an array. */
1808 size_t chain_count;
1810 /* Number of "binding_entry"s in this table. */
1811 size_t entry_count;
1814 /* Construct TABLE with an initial CHAIN_COUNT. */
1816 static inline void
1817 binding_table_construct (binding_table table, size_t chain_count)
1819 table->chain_count = chain_count;
1820 table->entry_count = 0;
1821 table->chain = ggc_cleared_vec_alloc<binding_entry> (table->chain_count);
1824 /* Make TABLE's entries ready for reuse. */
1825 #if 0
1826 static void
1827 binding_table_free (binding_table table)
1829 size_t i;
1830 size_t count;
1832 if (table == NULL)
1833 return;
1835 for (i = 0, count = table->chain_count; i < count; ++i)
1837 binding_entry temp = table->chain[i];
1838 while (temp != NULL)
1840 binding_entry entry = temp;
1841 temp = entry->chain;
1842 binding_entry_free (entry);
1844 table->chain[i] = NULL;
1846 table->entry_count = 0;
1848 #endif
1850 /* Allocate a table with CHAIN_COUNT, assumed to be a power of two. */
1852 static inline binding_table
1853 binding_table_new (size_t chain_count)
1855 binding_table table = ggc_alloc<binding_table_s> ();
1856 table->chain = NULL;
1857 binding_table_construct (table, chain_count);
1858 return table;
1861 /* Expand TABLE to twice its current chain_count. */
1863 static void
1864 binding_table_expand (binding_table table)
1866 const size_t old_chain_count = table->chain_count;
1867 const size_t old_entry_count = table->entry_count;
1868 const size_t new_chain_count = 2 * old_chain_count;
1869 binding_entry *old_chains = table->chain;
1870 size_t i;
1872 binding_table_construct (table, new_chain_count);
1873 for (i = 0; i < old_chain_count; ++i)
1875 binding_entry entry = old_chains[i];
1876 for (; entry != NULL; entry = old_chains[i])
1878 const unsigned int hash = IDENTIFIER_HASH_VALUE (entry->name);
1879 const size_t j = ENTRY_INDEX (hash, new_chain_count);
1881 old_chains[i] = entry->chain;
1882 entry->chain = table->chain[j];
1883 table->chain[j] = entry;
1886 table->entry_count = old_entry_count;
1889 /* Insert a binding for NAME to TYPE into TABLE. */
1891 static void
1892 binding_table_insert (binding_table table, tree name, tree type)
1894 const unsigned int hash = IDENTIFIER_HASH_VALUE (name);
1895 const size_t i = ENTRY_INDEX (hash, table->chain_count);
1896 binding_entry entry = binding_entry_make (name, type);
1898 entry->chain = table->chain[i];
1899 table->chain[i] = entry;
1900 ++table->entry_count;
1902 if (3 * table->chain_count < 5 * table->entry_count)
1903 binding_table_expand (table);
1906 /* Return the binding_entry, if any, that maps NAME. */
1908 binding_entry
1909 binding_table_find (binding_table table, tree name)
1911 const unsigned int hash = IDENTIFIER_HASH_VALUE (name);
1912 binding_entry entry = table->chain[ENTRY_INDEX (hash, table->chain_count)];
1914 while (entry != NULL && entry->name != name)
1915 entry = entry->chain;
1917 return entry;
1920 /* Apply PROC -- with DATA -- to all entries in TABLE. */
1922 void
1923 binding_table_foreach (binding_table table, bt_foreach_proc proc, void *data)
1925 size_t chain_count;
1926 size_t i;
1928 if (!table)
1929 return;
1931 chain_count = table->chain_count;
1932 for (i = 0; i < chain_count; ++i)
1934 binding_entry entry = table->chain[i];
1935 for (; entry != NULL; entry = entry->chain)
1936 proc (entry, data);
1940 #ifndef ENABLE_SCOPE_CHECKING
1941 # define ENABLE_SCOPE_CHECKING 0
1942 #else
1943 # define ENABLE_SCOPE_CHECKING 1
1944 #endif
1946 /* A free list of "cxx_binding"s, connected by their PREVIOUS. */
1948 static GTY((deletable)) cxx_binding *free_bindings;
1950 /* Initialize VALUE and TYPE field for BINDING, and set the PREVIOUS
1951 field to NULL. */
1953 static inline void
1954 cxx_binding_init (cxx_binding *binding, tree value, tree type)
1956 binding->value = value;
1957 binding->type = type;
1958 binding->previous = NULL;
1961 /* (GC)-allocate a binding object with VALUE and TYPE member initialized. */
1963 static cxx_binding *
1964 cxx_binding_make (tree value, tree type)
1966 cxx_binding *binding;
1967 if (free_bindings)
1969 binding = free_bindings;
1970 free_bindings = binding->previous;
1972 else
1973 binding = ggc_alloc<cxx_binding> ();
1975 cxx_binding_init (binding, value, type);
1977 return binding;
1980 /* Put BINDING back on the free list. */
1982 static inline void
1983 cxx_binding_free (cxx_binding *binding)
1985 binding->scope = NULL;
1986 binding->previous = free_bindings;
1987 free_bindings = binding;
1990 /* Create a new binding for NAME (with the indicated VALUE and TYPE
1991 bindings) in the class scope indicated by SCOPE. */
1993 static cxx_binding *
1994 new_class_binding (tree name, tree value, tree type, cp_binding_level *scope)
1996 cp_class_binding cb = {cxx_binding_make (value, type), name};
1997 cxx_binding *binding = cb.base;
1998 vec_safe_push (scope->class_shadowed, cb);
1999 binding->scope = scope;
2000 return binding;
2003 /* Make DECL the innermost binding for ID. The LEVEL is the binding
2004 level at which this declaration is being bound. */
2006 void
2007 push_binding (tree id, tree decl, cp_binding_level* level)
2009 cxx_binding *binding;
2011 if (level != class_binding_level)
2013 binding = cxx_binding_make (decl, NULL_TREE);
2014 binding->scope = level;
2016 else
2017 binding = new_class_binding (id, decl, /*type=*/NULL_TREE, level);
2019 /* Now, fill in the binding information. */
2020 binding->previous = IDENTIFIER_BINDING (id);
2021 INHERITED_VALUE_BINDING_P (binding) = 0;
2022 LOCAL_BINDING_P (binding) = (level != class_binding_level);
2024 /* And put it on the front of the list of bindings for ID. */
2025 IDENTIFIER_BINDING (id) = binding;
2028 /* Remove the binding for DECL which should be the innermost binding
2029 for ID. */
2031 void
2032 pop_local_binding (tree id, tree decl)
2034 cxx_binding *binding;
2036 if (id == NULL_TREE)
2037 /* It's easiest to write the loops that call this function without
2038 checking whether or not the entities involved have names. We
2039 get here for such an entity. */
2040 return;
2042 /* Get the innermost binding for ID. */
2043 binding = IDENTIFIER_BINDING (id);
2045 /* The name should be bound. */
2046 gcc_assert (binding != NULL);
2048 /* The DECL will be either the ordinary binding or the type
2049 binding for this identifier. Remove that binding. */
2050 if (binding->value == decl)
2051 binding->value = NULL_TREE;
2052 else
2054 gcc_assert (binding->type == decl);
2055 binding->type = NULL_TREE;
2058 if (!binding->value && !binding->type)
2060 /* We're completely done with the innermost binding for this
2061 identifier. Unhook it from the list of bindings. */
2062 IDENTIFIER_BINDING (id) = binding->previous;
2064 /* Add it to the free list. */
2065 cxx_binding_free (binding);
2069 /* Remove the bindings for the decls of the current level and leave
2070 the current scope. */
2072 void
2073 pop_bindings_and_leave_scope (void)
2075 for (tree t = get_local_decls (); t; t = DECL_CHAIN (t))
2077 tree decl = TREE_CODE (t) == TREE_LIST ? TREE_VALUE (t) : t;
2078 tree name = OVL_NAME (decl);
2080 pop_local_binding (name, decl);
2083 leave_scope ();
2086 /* Strip non dependent using declarations. If DECL is dependent,
2087 surreptitiously create a typename_type and return it. */
2089 tree
2090 strip_using_decl (tree decl)
2092 if (decl == NULL_TREE)
2093 return NULL_TREE;
2095 while (TREE_CODE (decl) == USING_DECL && !DECL_DEPENDENT_P (decl))
2096 decl = USING_DECL_DECLS (decl);
2098 if (TREE_CODE (decl) == USING_DECL && DECL_DEPENDENT_P (decl)
2099 && USING_DECL_TYPENAME_P (decl))
2101 /* We have found a type introduced by a using
2102 declaration at class scope that refers to a dependent
2103 type.
2105 using typename :: [opt] nested-name-specifier unqualified-id ;
2107 decl = make_typename_type (TREE_TYPE (decl),
2108 DECL_NAME (decl),
2109 typename_type, tf_error);
2110 if (decl != error_mark_node)
2111 decl = TYPE_NAME (decl);
2114 return decl;
2117 /* Return true if OVL is an overload for an anticipated builtin. */
2119 static bool
2120 anticipated_builtin_p (tree ovl)
2122 if (TREE_CODE (ovl) != OVERLOAD)
2123 return false;
2125 if (!OVL_HIDDEN_P (ovl))
2126 return false;
2128 tree fn = OVL_FUNCTION (ovl);
2129 gcc_checking_assert (DECL_ANTICIPATED (fn));
2131 if (DECL_HIDDEN_FRIEND_P (fn))
2132 return false;
2134 return true;
2137 /* BINDING records an existing declaration for a name in the current scope.
2138 But, DECL is another declaration for that same identifier in the
2139 same scope. This is the `struct stat' hack whereby a non-typedef
2140 class name or enum-name can be bound at the same level as some other
2141 kind of entity.
2142 3.3.7/1
2144 A class name (9.1) or enumeration name (7.2) can be hidden by the
2145 name of an object, function, or enumerator declared in the same scope.
2146 If a class or enumeration name and an object, function, or enumerator
2147 are declared in the same scope (in any order) with the same name, the
2148 class or enumeration name is hidden wherever the object, function, or
2149 enumerator name is visible.
2151 It's the responsibility of the caller to check that
2152 inserting this name is valid here. Returns nonzero if the new binding
2153 was successful. */
2155 static bool
2156 supplement_binding_1 (cxx_binding *binding, tree decl)
2158 tree bval = binding->value;
2159 bool ok = true;
2160 tree target_bval = strip_using_decl (bval);
2161 tree target_decl = strip_using_decl (decl);
2163 if (TREE_CODE (target_decl) == TYPE_DECL && DECL_ARTIFICIAL (target_decl)
2164 && target_decl != target_bval
2165 && (TREE_CODE (target_bval) != TYPE_DECL
2166 /* We allow pushing an enum multiple times in a class
2167 template in order to handle late matching of underlying
2168 type on an opaque-enum-declaration followed by an
2169 enum-specifier. */
2170 || (processing_template_decl
2171 && TREE_CODE (TREE_TYPE (target_decl)) == ENUMERAL_TYPE
2172 && TREE_CODE (TREE_TYPE (target_bval)) == ENUMERAL_TYPE
2173 && (dependent_type_p (ENUM_UNDERLYING_TYPE
2174 (TREE_TYPE (target_decl)))
2175 || dependent_type_p (ENUM_UNDERLYING_TYPE
2176 (TREE_TYPE (target_bval)))))))
2177 /* The new name is the type name. */
2178 binding->type = decl;
2179 else if (/* TARGET_BVAL is null when push_class_level_binding moves
2180 an inherited type-binding out of the way to make room
2181 for a new value binding. */
2182 !target_bval
2183 /* TARGET_BVAL is error_mark_node when TARGET_DECL's name
2184 has been used in a non-class scope prior declaration.
2185 In that case, we should have already issued a
2186 diagnostic; for graceful error recovery purpose, pretend
2187 this was the intended declaration for that name. */
2188 || target_bval == error_mark_node
2189 /* If TARGET_BVAL is anticipated but has not yet been
2190 declared, pretend it is not there at all. */
2191 || anticipated_builtin_p (target_bval))
2192 binding->value = decl;
2193 else if (TREE_CODE (target_bval) == TYPE_DECL
2194 && DECL_ARTIFICIAL (target_bval)
2195 && target_decl != target_bval
2196 && (TREE_CODE (target_decl) != TYPE_DECL
2197 || same_type_p (TREE_TYPE (target_decl),
2198 TREE_TYPE (target_bval))))
2200 /* The old binding was a type name. It was placed in
2201 VALUE field because it was thought, at the point it was
2202 declared, to be the only entity with such a name. Move the
2203 type name into the type slot; it is now hidden by the new
2204 binding. */
2205 binding->type = bval;
2206 binding->value = decl;
2207 binding->value_is_inherited = false;
2209 else if (TREE_CODE (target_bval) == TYPE_DECL
2210 && TREE_CODE (target_decl) == TYPE_DECL
2211 && DECL_NAME (target_decl) == DECL_NAME (target_bval)
2212 && binding->scope->kind != sk_class
2213 && (same_type_p (TREE_TYPE (target_decl), TREE_TYPE (target_bval))
2214 /* If either type involves template parameters, we must
2215 wait until instantiation. */
2216 || uses_template_parms (TREE_TYPE (target_decl))
2217 || uses_template_parms (TREE_TYPE (target_bval))))
2218 /* We have two typedef-names, both naming the same type to have
2219 the same name. In general, this is OK because of:
2221 [dcl.typedef]
2223 In a given scope, a typedef specifier can be used to redefine
2224 the name of any type declared in that scope to refer to the
2225 type to which it already refers.
2227 However, in class scopes, this rule does not apply due to the
2228 stricter language in [class.mem] prohibiting redeclarations of
2229 members. */
2230 ok = false;
2231 /* There can be two block-scope declarations of the same variable,
2232 so long as they are `extern' declarations. However, there cannot
2233 be two declarations of the same static data member:
2235 [class.mem]
2237 A member shall not be declared twice in the
2238 member-specification. */
2239 else if (VAR_P (target_decl)
2240 && VAR_P (target_bval)
2241 && DECL_EXTERNAL (target_decl) && DECL_EXTERNAL (target_bval)
2242 && !DECL_CLASS_SCOPE_P (target_decl))
2244 duplicate_decls (decl, binding->value, /*newdecl_is_friend=*/false);
2245 ok = false;
2247 else if (TREE_CODE (decl) == NAMESPACE_DECL
2248 && TREE_CODE (bval) == NAMESPACE_DECL
2249 && DECL_NAMESPACE_ALIAS (decl)
2250 && DECL_NAMESPACE_ALIAS (bval)
2251 && ORIGINAL_NAMESPACE (bval) == ORIGINAL_NAMESPACE (decl))
2252 /* [namespace.alias]
2254 In a declarative region, a namespace-alias-definition can be
2255 used to redefine a namespace-alias declared in that declarative
2256 region to refer only to the namespace to which it already
2257 refers. */
2258 ok = false;
2259 else
2261 if (!error_operand_p (bval))
2262 diagnose_name_conflict (decl, bval);
2263 ok = false;
2266 return ok;
2269 /* Diagnose a name conflict between DECL and BVAL. */
2271 static void
2272 diagnose_name_conflict (tree decl, tree bval)
2274 if (TREE_CODE (decl) == TREE_CODE (bval)
2275 && TREE_CODE (decl) != NAMESPACE_DECL
2276 && !DECL_DECLARES_FUNCTION_P (decl)
2277 && (TREE_CODE (decl) != TYPE_DECL
2278 || DECL_ARTIFICIAL (decl) == DECL_ARTIFICIAL (bval))
2279 && CP_DECL_CONTEXT (decl) == CP_DECL_CONTEXT (bval))
2280 error ("redeclaration of %q#D", decl);
2281 else
2282 error ("%q#D conflicts with a previous declaration", decl);
2284 inform (location_of (bval), "previous declaration %q#D", bval);
2287 /* Wrapper for supplement_binding_1. */
2289 static bool
2290 supplement_binding (cxx_binding *binding, tree decl)
2292 bool ret;
2293 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
2294 ret = supplement_binding_1 (binding, decl);
2295 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
2296 return ret;
2299 /* Replace BINDING's current value on its scope's name list with
2300 NEWVAL. */
2302 static void
2303 update_local_overload (cxx_binding *binding, tree newval)
2305 tree *d;
2307 for (d = &binding->scope->names; ; d = &TREE_CHAIN (*d))
2308 if (*d == binding->value)
2310 /* Stitch new list node in. */
2311 *d = tree_cons (NULL_TREE, NULL_TREE, TREE_CHAIN (*d));
2312 break;
2314 else if (TREE_CODE (*d) == TREE_LIST && TREE_VALUE (*d) == binding->value)
2315 break;
2317 TREE_VALUE (*d) = newval;
2320 /* Compares the parameter-type-lists of ONE and TWO and
2321 returns false if they are different. If the DECLs are template
2322 functions, the return types and the template parameter lists are
2323 compared too (DR 565). */
2325 static bool
2326 matching_fn_p (tree one, tree two)
2328 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (one)),
2329 TYPE_ARG_TYPES (TREE_TYPE (two))))
2330 return false;
2332 if (TREE_CODE (one) == TEMPLATE_DECL
2333 && TREE_CODE (two) == TEMPLATE_DECL)
2335 /* Compare template parms. */
2336 if (!comp_template_parms (DECL_TEMPLATE_PARMS (one),
2337 DECL_TEMPLATE_PARMS (two)))
2338 return false;
2340 /* And return type. */
2341 if (!same_type_p (TREE_TYPE (TREE_TYPE (one)),
2342 TREE_TYPE (TREE_TYPE (two))))
2343 return false;
2346 return true;
2349 /* Push DECL into nonclass LEVEL BINDING or SLOT. OLD is the current
2350 binding value (possibly with anticipated builtins stripped).
2351 Diagnose conflicts and return updated decl. */
2353 static tree
2354 update_binding (cp_binding_level *level, cxx_binding *binding, tree *slot,
2355 tree old, tree decl, bool is_friend)
2357 tree to_val = decl;
2358 tree old_type = slot ? MAYBE_STAT_TYPE (*slot) : binding->type;
2359 tree to_type = old_type;
2361 gcc_assert (level->kind == sk_namespace ? !binding
2362 : level->kind != sk_class && !slot);
2363 if (old == error_mark_node)
2364 old = NULL_TREE;
2366 if (TREE_CODE (decl) == TYPE_DECL && DECL_ARTIFICIAL (decl))
2368 tree other = to_type;
2370 if (old && TREE_CODE (old) == TYPE_DECL && DECL_ARTIFICIAL (old))
2371 other = old;
2373 /* Pushing an artificial typedef. See if this matches either
2374 the type slot or the old value slot. */
2375 if (!other)
2377 else if (same_type_p (TREE_TYPE (other), TREE_TYPE (decl)))
2378 /* Two artificial decls to same type. Do nothing. */
2379 return other;
2380 else
2381 goto conflict;
2383 if (old)
2385 /* Slide decl into the type slot, keep old unaltered */
2386 to_type = decl;
2387 to_val = old;
2388 goto done;
2392 if (old && TREE_CODE (old) == TYPE_DECL && DECL_ARTIFICIAL (old))
2394 /* Slide old into the type slot. */
2395 to_type = old;
2396 old = NULL_TREE;
2399 if (DECL_DECLARES_FUNCTION_P (decl))
2401 if (!old)
2403 else if (OVL_P (old))
2405 for (ovl_iterator iter (old); iter; ++iter)
2407 tree fn = *iter;
2409 if (iter.using_p () && matching_fn_p (fn, decl))
2411 /* If a function declaration in namespace scope or
2412 block scope has the same name and the same
2413 parameter-type- list (8.3.5) as a function
2414 introduced by a using-declaration, and the
2415 declarations do not declare the same function,
2416 the program is ill-formed. [namespace.udecl]/14 */
2417 if (tree match = duplicate_decls (decl, fn, is_friend))
2418 return match;
2419 else
2420 /* FIXME: To preserve existing error behavior, we
2421 still push the decl. This might change. */
2422 diagnose_name_conflict (decl, fn);
2426 else
2427 goto conflict;
2429 if (to_type != old_type
2430 && warn_shadow
2431 && MAYBE_CLASS_TYPE_P (TREE_TYPE (to_type))
2432 && !(DECL_IN_SYSTEM_HEADER (decl)
2433 && DECL_IN_SYSTEM_HEADER (to_type)))
2434 warning (OPT_Wshadow, "%q#D hides constructor for %q#D",
2435 decl, to_type);
2437 to_val = ovl_insert (decl, old);
2439 else if (!old)
2441 else if (TREE_CODE (old) != TREE_CODE (decl))
2442 /* Different kinds of decls conflict. */
2443 goto conflict;
2444 else if (TREE_CODE (old) == TYPE_DECL)
2446 if (same_type_p (TREE_TYPE (old), TREE_TYPE (decl)))
2447 /* Two type decls to the same type. Do nothing. */
2448 return old;
2449 else
2450 goto conflict;
2452 else if (TREE_CODE (old) == NAMESPACE_DECL)
2454 /* Two maybe-aliased namespaces. If they're to the same target
2455 namespace, that's ok. */
2456 if (ORIGINAL_NAMESPACE (old) != ORIGINAL_NAMESPACE (decl))
2457 goto conflict;
2459 /* The new one must be an alias at this point. */
2460 gcc_assert (DECL_NAMESPACE_ALIAS (decl));
2461 return old;
2463 else if (TREE_CODE (old) == VAR_DECL)
2465 /* There can be two block-scope declarations of the same
2466 variable, so long as they are `extern' declarations. */
2467 if (!DECL_EXTERNAL (old) || !DECL_EXTERNAL (decl))
2468 goto conflict;
2469 else if (tree match = duplicate_decls (decl, old, false))
2470 return match;
2471 else
2472 goto conflict;
2474 else
2476 conflict:
2477 diagnose_name_conflict (decl, old);
2478 to_val = NULL_TREE;
2481 done:
2482 if (to_val)
2484 if (level->kind != sk_namespace
2485 && !to_type && binding->value && OVL_P (to_val))
2486 update_local_overload (binding, to_val);
2487 else
2489 tree to_add = to_val;
2491 if (level->kind == sk_namespace)
2492 to_add = decl;
2493 else if (to_type == decl)
2494 to_add = decl;
2495 else if (TREE_CODE (to_add) == OVERLOAD)
2496 to_add = build_tree_list (NULL_TREE, to_add);
2498 add_decl_to_level (level, to_add);
2501 if (slot)
2503 if (STAT_HACK_P (*slot))
2505 STAT_TYPE (*slot) = to_type;
2506 STAT_DECL (*slot) = to_val;
2508 else if (to_type)
2509 *slot = stat_hack (to_val, to_type);
2510 else
2511 *slot = to_val;
2513 else
2515 binding->type = to_type;
2516 binding->value = to_val;
2520 return decl;
2523 /* Table of identifiers to extern C declarations (or LISTS thereof). */
2525 static GTY(()) hash_table<named_decl_hash> *extern_c_decls;
2527 /* DECL has C linkage. If we have an existing instance, make sure the
2528 new one is compatible. Make sure it has the same exception
2529 specification [7.5, 7.6]. Add DECL to the map. */
2531 static void
2532 check_extern_c_conflict (tree decl)
2534 /* Ignore artificial or system header decls. */
2535 if (DECL_ARTIFICIAL (decl) || DECL_IN_SYSTEM_HEADER (decl))
2536 return;
2538 if (!extern_c_decls)
2539 extern_c_decls = hash_table<named_decl_hash>::create_ggc (127);
2541 tree *slot = extern_c_decls
2542 ->find_slot_with_hash (DECL_NAME (decl),
2543 IDENTIFIER_HASH_VALUE (DECL_NAME (decl)), INSERT);
2544 if (tree old = *slot)
2546 if (TREE_CODE (old) == OVERLOAD)
2547 old = OVL_FUNCTION (old);
2549 int mismatch = 0;
2550 if (DECL_CONTEXT (old) == DECL_CONTEXT (decl))
2551 ; /* If they're in the same context, we'll have already complained
2552 about a (possible) mismatch, when inserting the decl. */
2553 else if (!decls_match (decl, old))
2554 mismatch = 1;
2555 else if (TREE_CODE (decl) == FUNCTION_DECL
2556 && !comp_except_specs (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (old)),
2557 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl)),
2558 ce_normal))
2559 mismatch = -1;
2560 else if (DECL_ASSEMBLER_NAME_SET_P (old))
2561 SET_DECL_ASSEMBLER_NAME (decl, DECL_ASSEMBLER_NAME (old));
2563 if (mismatch)
2565 pedwarn (input_location, 0,
2566 "conflicting C language linkage declaration %q#D", decl);
2567 inform (DECL_SOURCE_LOCATION (old),
2568 "previous declaration %q#D", old);
2569 if (mismatch < 0)
2570 inform (input_location,
2571 "due to different exception specifications");
2573 else
2575 if (old == *slot)
2576 /* The hash table expects OVERLOADS, so construct one with
2577 OLD as both the function and the chain. This allocate
2578 an excess OVERLOAD node, but it's rare to have multiple
2579 extern "C" decls of the same name. And we save
2580 complicating the hash table logic (which is used
2581 elsewhere). */
2582 *slot = ovl_make (old, old);
2584 slot = &OVL_CHAIN (*slot);
2586 /* Chain it on for c_linkage_binding's use. */
2587 *slot = tree_cons (NULL_TREE, decl, *slot);
2590 else
2591 *slot = decl;
2594 /* Returns a list of C-linkage decls with the name NAME. Used in
2595 c-family/c-pragma.c to implement redefine_extname pragma. */
2597 tree
2598 c_linkage_bindings (tree name)
2600 if (extern_c_decls)
2601 if (tree *slot = extern_c_decls
2602 ->find_slot_with_hash (name, IDENTIFIER_HASH_VALUE (name), NO_INSERT))
2604 tree result = *slot;
2605 if (TREE_CODE (result) == OVERLOAD)
2606 result = OVL_CHAIN (result);
2607 return result;
2610 return NULL_TREE;
2613 /* DECL is being declared at a local scope. Emit suitable shadow
2614 warnings. */
2616 static void
2617 check_local_shadow (tree decl)
2619 /* Don't complain about the parms we push and then pop
2620 while tentatively parsing a function declarator. */
2621 if (TREE_CODE (decl) == PARM_DECL && !DECL_CONTEXT (decl))
2622 return;
2624 /* Inline decls shadow nothing. */
2625 if (DECL_FROM_INLINE (decl))
2626 return;
2628 /* External decls are something else. */
2629 if (DECL_EXTERNAL (decl))
2630 return;
2632 tree old = NULL_TREE;
2633 cp_binding_level *old_scope = NULL;
2634 if (cxx_binding *binding = outer_binding (DECL_NAME (decl), NULL, true))
2636 old = binding->value;
2637 old_scope = binding->scope;
2639 while (old && VAR_P (old) && DECL_DEAD_FOR_LOCAL (old))
2640 old = DECL_SHADOWED_FOR_VAR (old);
2642 tree shadowed = NULL_TREE;
2643 if (old
2644 && (TREE_CODE (old) == PARM_DECL
2645 || VAR_P (old)
2646 || (TREE_CODE (old) == TYPE_DECL
2647 && (!DECL_ARTIFICIAL (old)
2648 || TREE_CODE (decl) == TYPE_DECL)))
2649 && (!DECL_ARTIFICIAL (decl)
2650 || DECL_IMPLICIT_TYPEDEF_P (decl)
2651 || (VAR_P (decl) && DECL_ANON_UNION_VAR_P (decl))))
2653 /* DECL shadows a local thing possibly of interest. */
2655 /* Don't complain if it's from an enclosing function. */
2656 if (DECL_CONTEXT (old) == current_function_decl
2657 && TREE_CODE (decl) != PARM_DECL
2658 && TREE_CODE (old) == PARM_DECL)
2660 /* Go to where the parms should be and see if we find
2661 them there. */
2662 cp_binding_level *b = current_binding_level->level_chain;
2664 if (FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
2665 /* Skip the ctor/dtor cleanup level. */
2666 b = b->level_chain;
2668 /* ARM $8.3 */
2669 if (b->kind == sk_function_parms)
2671 error ("declaration of %q#D shadows a parameter", decl);
2672 return;
2676 /* The local structure or class can't use parameters of
2677 the containing function anyway. */
2678 if (DECL_CONTEXT (old) != current_function_decl)
2680 for (cp_binding_level *scope = current_binding_level;
2681 scope != old_scope; scope = scope->level_chain)
2682 if (scope->kind == sk_class
2683 && !LAMBDA_TYPE_P (scope->this_entity))
2684 return;
2686 /* Error if redeclaring a local declared in a
2687 init-statement or in the condition of an if or
2688 switch statement when the new declaration is in the
2689 outermost block of the controlled statement.
2690 Redeclaring a variable from a for or while condition is
2691 detected elsewhere. */
2692 else if (VAR_P (old)
2693 && old_scope == current_binding_level->level_chain
2694 && (old_scope->kind == sk_cond || old_scope->kind == sk_for))
2696 error ("redeclaration of %q#D", decl);
2697 inform (DECL_SOURCE_LOCATION (old),
2698 "%q#D previously declared here", old);
2699 return;
2701 /* C++11:
2702 3.3.3/3: The name declared in an exception-declaration (...)
2703 shall not be redeclared in the outermost block of the handler.
2704 3.3.3/2: A parameter name shall not be redeclared (...) in
2705 the outermost block of any handler associated with a
2706 function-try-block.
2707 3.4.1/15: The function parameter names shall not be redeclared
2708 in the exception-declaration nor in the outermost block of a
2709 handler for the function-try-block. */
2710 else if ((TREE_CODE (old) == VAR_DECL
2711 && old_scope == current_binding_level->level_chain
2712 && old_scope->kind == sk_catch)
2713 || (TREE_CODE (old) == PARM_DECL
2714 && (current_binding_level->kind == sk_catch
2715 || current_binding_level->level_chain->kind == sk_catch)
2716 && in_function_try_handler))
2718 if (permerror (input_location, "redeclaration of %q#D", decl))
2719 inform (DECL_SOURCE_LOCATION (old),
2720 "%q#D previously declared here", old);
2721 return;
2724 /* If '-Wshadow=compatible-local' is specified without other
2725 -Wshadow= flags, we will warn only when the type of the
2726 shadowing variable (DECL) can be converted to that of the
2727 shadowed parameter (OLD_LOCAL). The reason why we only check
2728 if DECL's type can be converted to OLD_LOCAL's type (but not the
2729 other way around) is because when users accidentally shadow a
2730 parameter, more than often they would use the variable
2731 thinking (mistakenly) it's still the parameter. It would be
2732 rare that users would use the variable in the place that
2733 expects the parameter but thinking it's a new decl. */
2735 enum opt_code warning_code;
2736 if (warn_shadow)
2737 warning_code = OPT_Wshadow;
2738 else if (warn_shadow_local)
2739 warning_code = OPT_Wshadow_local;
2740 else if (warn_shadow_compatible_local
2741 && (same_type_p (TREE_TYPE (old), TREE_TYPE (decl))
2742 || (!dependent_type_p (TREE_TYPE (decl))
2743 && !dependent_type_p (TREE_TYPE (old))
2744 && can_convert (TREE_TYPE (old), TREE_TYPE (decl),
2745 tf_none))))
2746 warning_code = OPT_Wshadow_compatible_local;
2747 else
2748 return;
2750 const char *msg;
2751 if (TREE_CODE (old) == PARM_DECL)
2752 msg = "declaration of %q#D shadows a parameter";
2753 else if (is_capture_proxy (old))
2754 msg = "declaration of %qD shadows a lambda capture";
2755 else
2756 msg = "declaration of %qD shadows a previous local";
2758 if (warning_at (input_location, warning_code, msg, decl))
2760 shadowed = old;
2761 goto inform_shadowed;
2763 return;
2766 if (!warn_shadow)
2767 return;
2769 /* Don't warn for artificial things that are not implicit typedefs. */
2770 if (DECL_ARTIFICIAL (decl) && !DECL_IMPLICIT_TYPEDEF_P (decl))
2771 return;
2773 if (nonlambda_method_basetype ())
2774 if (tree member = lookup_member (current_nonlambda_class_type (),
2775 DECL_NAME (decl), /*protect=*/0,
2776 /*want_type=*/false, tf_warning_or_error))
2778 member = MAYBE_BASELINK_FUNCTIONS (member);
2780 /* Warn if a variable shadows a non-function, or the variable
2781 is a function or a pointer-to-function. */
2782 if (!OVL_P (member)
2783 || TREE_CODE (decl) == FUNCTION_DECL
2784 || TYPE_PTRFN_P (TREE_TYPE (decl))
2785 || TYPE_PTRMEMFUNC_P (TREE_TYPE (decl)))
2787 if (warning_at (input_location, OPT_Wshadow,
2788 "declaration of %qD shadows a member of %qT",
2789 decl, current_nonlambda_class_type ())
2790 && DECL_P (member))
2792 shadowed = member;
2793 goto inform_shadowed;
2796 return;
2799 /* Now look for a namespace shadow. */
2800 old = find_namespace_value (current_namespace, DECL_NAME (decl));
2801 if (old
2802 && (VAR_P (old)
2803 || (TREE_CODE (old) == TYPE_DECL
2804 && (!DECL_ARTIFICIAL (old)
2805 || TREE_CODE (decl) == TYPE_DECL)))
2806 && !instantiating_current_function_p ())
2807 /* XXX shadow warnings in outer-more namespaces */
2809 if (warning_at (input_location, OPT_Wshadow,
2810 "declaration of %qD shadows a global declaration",
2811 decl))
2813 shadowed = old;
2814 goto inform_shadowed;
2816 return;
2819 return;
2821 inform_shadowed:
2822 inform (DECL_SOURCE_LOCATION (shadowed), "shadowed declaration is here");
2825 /* DECL is being pushed inside function CTX. Set its context, if
2826 needed. */
2828 static void
2829 set_decl_context_in_fn (tree ctx, tree decl)
2831 if (!DECL_CONTEXT (decl)
2832 /* A local declaration for a function doesn't constitute
2833 nesting. */
2834 && TREE_CODE (decl) != FUNCTION_DECL
2835 /* A local declaration for an `extern' variable is in the
2836 scope of the current namespace, not the current
2837 function. */
2838 && !(VAR_P (decl) && DECL_EXTERNAL (decl))
2839 /* When parsing the parameter list of a function declarator,
2840 don't set DECL_CONTEXT to an enclosing function. When we
2841 push the PARM_DECLs in order to process the function body,
2842 current_binding_level->this_entity will be set. */
2843 && !(TREE_CODE (decl) == PARM_DECL
2844 && current_binding_level->kind == sk_function_parms
2845 && current_binding_level->this_entity == NULL))
2846 DECL_CONTEXT (decl) = ctx;
2848 /* If this is the declaration for a namespace-scope function,
2849 but the declaration itself is in a local scope, mark the
2850 declaration. */
2851 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (decl))
2852 DECL_LOCAL_FUNCTION_P (decl) = 1;
2855 /* DECL is a local-scope decl with linkage. SHADOWED is true if the
2856 name is already bound at the current level.
2858 [basic.link] If there is a visible declaration of an entity with
2859 linkage having the same name and type, ignoring entities declared
2860 outside the innermost enclosing namespace scope, the block scope
2861 declaration declares that same entity and receives the linkage of
2862 the previous declaration.
2864 Also, make sure that this decl matches any existing external decl
2865 in the enclosing namespace. */
2867 static void
2868 set_local_extern_decl_linkage (tree decl, bool shadowed)
2870 tree ns_value = decl; /* Unique marker. */
2872 if (!shadowed)
2874 tree loc_value = innermost_non_namespace_value (DECL_NAME (decl));
2875 if (!loc_value)
2877 ns_value
2878 = find_namespace_value (current_namespace, DECL_NAME (decl));
2879 loc_value = ns_value;
2881 if (loc_value == error_mark_node)
2882 loc_value = NULL_TREE;
2884 for (ovl_iterator iter (loc_value); iter; ++iter)
2885 if (!iter.hidden_p ()
2886 && (TREE_STATIC (*iter) || DECL_EXTERNAL (*iter))
2887 && decls_match (*iter, decl))
2889 /* The standard only says that the local extern inherits
2890 linkage from the previous decl; in particular, default
2891 args are not shared. Add the decl into a hash table to
2892 make sure only the previous decl in this case is seen
2893 by the middle end. */
2894 struct cxx_int_tree_map *h;
2896 /* We inherit the outer decl's linkage. But we're a
2897 different decl. */
2898 TREE_PUBLIC (decl) = TREE_PUBLIC (*iter);
2900 if (cp_function_chain->extern_decl_map == NULL)
2901 cp_function_chain->extern_decl_map
2902 = hash_table<cxx_int_tree_map_hasher>::create_ggc (20);
2904 h = ggc_alloc<cxx_int_tree_map> ();
2905 h->uid = DECL_UID (decl);
2906 h->to = *iter;
2907 cxx_int_tree_map **loc = cp_function_chain->extern_decl_map
2908 ->find_slot (h, INSERT);
2909 *loc = h;
2910 break;
2914 if (TREE_PUBLIC (decl))
2916 /* DECL is externally visible. Make sure it matches a matching
2917 decl in the namespace scope. We only really need to check
2918 this when inserting the decl, not when we find an existing
2919 match in the current scope. However, in practice we're
2920 going to be inserting a new decl in the majority of cases --
2921 who writes multiple extern decls for the same thing in the
2922 same local scope? Doing it here often avoids a duplicate
2923 namespace lookup. */
2925 /* Avoid repeating a lookup. */
2926 if (ns_value == decl)
2927 ns_value = find_namespace_value (current_namespace, DECL_NAME (decl));
2929 if (ns_value == error_mark_node)
2930 ns_value = NULL_TREE;
2932 for (ovl_iterator iter (ns_value); iter; ++iter)
2934 tree other = *iter;
2936 if (!(TREE_PUBLIC (other) || DECL_EXTERNAL (other)))
2937 ; /* Not externally visible. */
2938 else if (DECL_EXTERN_C_P (decl) && DECL_EXTERN_C_P (other))
2939 ; /* Both are extern "C", we'll check via that mechanism. */
2940 else if (TREE_CODE (other) != TREE_CODE (decl)
2941 || ((VAR_P (decl) || matching_fn_p (other, decl))
2942 && !comptypes (TREE_TYPE (decl), TREE_TYPE (other),
2943 COMPARE_REDECLARATION)))
2945 if (permerror (DECL_SOURCE_LOCATION (decl),
2946 "local external declaration %q#D", decl))
2947 inform (DECL_SOURCE_LOCATION (other),
2948 "does not match previous declaration %q#D", other);
2949 break;
2955 /* Record DECL as belonging to the current lexical scope. Check for
2956 errors (such as an incompatible declaration for the same name
2957 already seen in the same scope). IS_FRIEND is true if DECL is
2958 declared as a friend.
2960 Returns either DECL or an old decl for the same name. If an old
2961 decl is returned, it may have been smashed to agree with what DECL
2962 says. */
2964 static tree
2965 do_pushdecl (tree decl, bool is_friend)
2967 if (decl == error_mark_node)
2968 return error_mark_node;
2970 if (!DECL_TEMPLATE_PARM_P (decl) && current_function_decl)
2971 set_decl_context_in_fn (current_function_decl, decl);
2973 /* The binding level we will be pushing into. During local class
2974 pushing, we want to push to the containing scope. */
2975 cp_binding_level *level = current_binding_level;
2976 while (level->kind == sk_class)
2977 level = level->level_chain;
2979 /* An anonymous namespace has a NULL DECL_NAME, but we still want to
2980 insert it. Other NULL-named decls, not so much. */
2981 tree name = DECL_NAME (decl);
2982 if (name || TREE_CODE (decl) == NAMESPACE_DECL)
2984 cxx_binding *binding = NULL; /* Local scope binding. */
2985 tree ns = NULL_TREE; /* Searched namespace. */
2986 tree *slot = NULL; /* Binding slot in namespace. */
2987 tree old = NULL_TREE;
2989 if (level->kind == sk_namespace)
2991 /* We look in the decl's namespace for an existing
2992 declaration, even though we push into the current
2993 namespace. */
2994 ns = (DECL_NAMESPACE_SCOPE_P (decl)
2995 ? CP_DECL_CONTEXT (decl) : current_namespace);
2996 /* Create the binding, if this is current namespace, because
2997 that's where we'll be pushing anyway. */
2998 slot = find_namespace_slot (ns, name, ns == current_namespace);
2999 if (slot)
3000 old = MAYBE_STAT_DECL (*slot);
3002 else
3004 binding = find_local_binding (level, name);
3005 if (binding)
3006 old = binding->value;
3009 if (current_function_decl && VAR_OR_FUNCTION_DECL_P (decl)
3010 && DECL_EXTERNAL (decl))
3011 set_local_extern_decl_linkage (decl, old != NULL_TREE);
3013 if (old == error_mark_node)
3014 old = NULL_TREE;
3016 for (ovl_iterator iter (old); iter; ++iter)
3017 if (iter.using_p ())
3018 ; /* Ignore using decls here. */
3019 else if (tree match = duplicate_decls (decl, *iter, is_friend))
3021 if (match == error_mark_node)
3023 else if (TREE_CODE (match) == TYPE_DECL)
3024 /* The IDENTIFIER will have the type referring to the
3025 now-smashed TYPE_DECL, because ...? Reset it. */
3026 SET_IDENTIFIER_TYPE_VALUE (name, TREE_TYPE (match));
3027 else if (iter.hidden_p () && !DECL_HIDDEN_P (match))
3029 /* Unhiding a previously hidden decl. */
3030 tree head = iter.reveal_node (old);
3031 if (head != old)
3033 if (!ns)
3035 update_local_overload (binding, head);
3036 binding->value = head;
3038 else if (STAT_HACK_P (*slot))
3039 STAT_DECL (*slot) = head;
3040 else
3041 *slot = head;
3043 if (DECL_EXTERN_C_P (match))
3044 /* We need to check and register the decl now. */
3045 check_extern_c_conflict (match);
3047 return match;
3050 /* We are pushing a new decl. */
3052 /* Skip a hidden builtin we failed to match already. There can
3053 only be one. */
3054 if (old && anticipated_builtin_p (old))
3055 old = OVL_CHAIN (old);
3057 check_template_shadow (decl);
3058 bool visible_injection = false;
3060 if (DECL_DECLARES_FUNCTION_P (decl))
3062 check_default_args (decl);
3064 if (is_friend)
3066 if (level->kind != sk_namespace)
3068 /* In a local class, a friend function declaration must
3069 find a matching decl in the innermost non-class scope.
3070 [class.friend/11] */
3071 error ("friend declaration %qD in local class without "
3072 "prior local declaration", decl);
3073 /* Don't attempt to push it. */
3074 return error_mark_node;
3076 if (!flag_friend_injection)
3077 /* Hide it from ordinary lookup. */
3078 DECL_ANTICIPATED (decl) = DECL_HIDDEN_FRIEND_P (decl) = true;
3079 else
3080 visible_injection = true;
3084 if (level->kind != sk_namespace)
3086 check_local_shadow (decl);
3088 if (TREE_CODE (decl) == NAMESPACE_DECL)
3089 /* A local namespace alias. */
3090 set_identifier_type_value (name, NULL_TREE);
3092 if (!binding)
3093 binding = create_local_binding (level, name);
3095 else if (!slot)
3097 ns = current_namespace;
3098 slot = find_namespace_slot (ns, name, true);
3099 /* Update OLD to reflect the namespace we're going to be
3100 pushing into. */
3101 old = MAYBE_STAT_DECL (*slot);
3104 old = update_binding (level, binding, slot, old, decl, is_friend);
3106 if (old != decl)
3107 /* An existing decl matched, use it. */
3108 decl = old;
3109 else if (TREE_CODE (decl) == TYPE_DECL)
3111 tree type = TREE_TYPE (decl);
3113 if (type != error_mark_node)
3115 if (TYPE_NAME (type) != decl)
3116 set_underlying_type (decl);
3118 if (!ns)
3119 set_identifier_type_value_with_scope (name, decl, level);
3120 else
3121 SET_IDENTIFIER_TYPE_VALUE (name, global_type_node);
3124 /* If this is a locally defined typedef in a function that
3125 is not a template instantation, record it to implement
3126 -Wunused-local-typedefs. */
3127 if (!instantiating_current_function_p ())
3128 record_locally_defined_typedef (decl);
3130 else if (VAR_P (decl))
3131 maybe_register_incomplete_var (decl);
3132 else if (visible_injection)
3133 warning (0, "injected friend %qD is visible"
3134 " due to %<-ffriend-injection%>", decl);
3136 if ((VAR_P (decl) || TREE_CODE (decl) == FUNCTION_DECL)
3137 && DECL_EXTERN_C_P (decl))
3138 check_extern_c_conflict (decl);
3140 else
3141 add_decl_to_level (level, decl);
3143 return decl;
3146 /* Record a decl-node X as belonging to the current lexical scope.
3147 It's a friend if IS_FRIEND is true -- which affects exactly where
3148 we push it. */
3150 tree
3151 pushdecl (tree x, bool is_friend)
3153 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3154 tree ret = do_pushdecl (x, is_friend);
3155 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3156 return ret;
3159 /* Enter DECL into the symbol table, if that's appropriate. Returns
3160 DECL, or a modified version thereof. */
3162 tree
3163 maybe_push_decl (tree decl)
3165 tree type = TREE_TYPE (decl);
3167 /* Add this decl to the current binding level, but not if it comes
3168 from another scope, e.g. a static member variable. TEM may equal
3169 DECL or it may be a previous decl of the same name. */
3170 if (decl == error_mark_node
3171 || (TREE_CODE (decl) != PARM_DECL
3172 && DECL_CONTEXT (decl) != NULL_TREE
3173 /* Definitions of namespace members outside their namespace are
3174 possible. */
3175 && !DECL_NAMESPACE_SCOPE_P (decl))
3176 || (TREE_CODE (decl) == TEMPLATE_DECL && !namespace_bindings_p ())
3177 || type == unknown_type_node
3178 /* The declaration of a template specialization does not affect
3179 the functions available for overload resolution, so we do not
3180 call pushdecl. */
3181 || (TREE_CODE (decl) == FUNCTION_DECL
3182 && DECL_TEMPLATE_SPECIALIZATION (decl)))
3183 return decl;
3184 else
3185 return pushdecl (decl);
3188 /* Bind DECL to ID in the current_binding_level, assumed to be a local
3189 binding level. If IS_USING is true, DECL got here through a
3190 using-declaration. */
3192 static void
3193 push_local_binding (tree id, tree decl, bool is_using)
3195 /* Skip over any local classes. This makes sense if we call
3196 push_local_binding with a friend decl of a local class. */
3197 cp_binding_level *b = innermost_nonclass_level ();
3199 gcc_assert (b->kind != sk_namespace);
3200 if (find_local_binding (b, id))
3202 /* Supplement the existing binding. */
3203 if (!supplement_binding (IDENTIFIER_BINDING (id), decl))
3204 /* It didn't work. Something else must be bound at this
3205 level. Do not add DECL to the list of things to pop
3206 later. */
3207 return;
3209 else
3210 /* Create a new binding. */
3211 push_binding (id, decl, b);
3213 if (TREE_CODE (decl) == OVERLOAD || is_using)
3214 /* We must put the OVERLOAD or using into a TREE_LIST since we
3215 cannot use the decl's chain itself. */
3216 decl = build_tree_list (NULL_TREE, decl);
3218 /* And put DECL on the list of things declared by the current
3219 binding level. */
3220 add_decl_to_level (b, decl);
3223 /* Check to see whether or not DECL is a variable that would have been
3224 in scope under the ARM, but is not in scope under the ANSI/ISO
3225 standard. If so, issue an error message. If name lookup would
3226 work in both cases, but return a different result, this function
3227 returns the result of ANSI/ISO lookup. Otherwise, it returns
3228 DECL.
3230 FIXME: Scheduled for removal after GCC-8 is done. */
3232 tree
3233 check_for_out_of_scope_variable (tree decl)
3235 tree shadowed;
3237 /* We only care about out of scope variables. */
3238 if (!(VAR_P (decl) && DECL_DEAD_FOR_LOCAL (decl)))
3239 return decl;
3241 shadowed = DECL_HAS_SHADOWED_FOR_VAR_P (decl)
3242 ? DECL_SHADOWED_FOR_VAR (decl) : NULL_TREE ;
3243 while (shadowed != NULL_TREE && VAR_P (shadowed)
3244 && DECL_DEAD_FOR_LOCAL (shadowed))
3245 shadowed = DECL_HAS_SHADOWED_FOR_VAR_P (shadowed)
3246 ? DECL_SHADOWED_FOR_VAR (shadowed) : NULL_TREE;
3247 if (!shadowed)
3248 shadowed = find_namespace_value (current_namespace, DECL_NAME (decl));
3249 if (shadowed)
3251 if (!DECL_ERROR_REPORTED (decl)
3252 && flag_permissive
3253 && warning (0, "name lookup of %qD changed", DECL_NAME (decl)))
3255 inform (DECL_SOURCE_LOCATION (shadowed),
3256 "matches this %qD under ISO standard rules", shadowed);
3257 inform (DECL_SOURCE_LOCATION (decl),
3258 " matches this %qD under old rules", decl);
3260 DECL_ERROR_REPORTED (decl) = 1;
3261 return shadowed;
3264 /* If we have already complained about this declaration, there's no
3265 need to do it again. */
3266 if (DECL_ERROR_REPORTED (decl))
3267 return decl;
3269 DECL_ERROR_REPORTED (decl) = 1;
3271 if (TREE_TYPE (decl) == error_mark_node)
3272 return decl;
3274 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
3276 error ("name lookup of %qD changed for ISO %<for%> scoping",
3277 DECL_NAME (decl));
3278 inform (DECL_SOURCE_LOCATION (decl),
3279 "cannot use obsolete binding %qD because it has a destructor",
3280 decl);
3281 return error_mark_node;
3283 else
3285 permerror (input_location,
3286 "name lookup of %qD changed for ISO %<for%> scoping",
3287 DECL_NAME (decl));
3288 if (flag_permissive)
3289 inform (DECL_SOURCE_LOCATION (decl),
3290 "using obsolete binding %qD", decl);
3291 static bool hint;
3292 if (!hint)
3293 inform (input_location, flag_permissive
3294 ? "this flexibility is deprecated and will be removed"
3295 : "if you use %<-fpermissive%> G++ will accept your code");
3296 hint = true;
3299 return decl;
3302 /* true means unconditionally make a BLOCK for the next level pushed. */
3304 static bool keep_next_level_flag;
3306 static int binding_depth = 0;
3308 static void
3309 indent (int depth)
3311 int i;
3313 for (i = 0; i < depth * 2; i++)
3314 putc (' ', stderr);
3317 /* Return a string describing the kind of SCOPE we have. */
3318 static const char *
3319 cp_binding_level_descriptor (cp_binding_level *scope)
3321 /* The order of this table must match the "scope_kind"
3322 enumerators. */
3323 static const char* scope_kind_names[] = {
3324 "block-scope",
3325 "cleanup-scope",
3326 "try-scope",
3327 "catch-scope",
3328 "for-scope",
3329 "function-parameter-scope",
3330 "class-scope",
3331 "namespace-scope",
3332 "template-parameter-scope",
3333 "template-explicit-spec-scope"
3335 const scope_kind kind = scope->explicit_spec_p
3336 ? sk_template_spec : scope->kind;
3338 return scope_kind_names[kind];
3341 /* Output a debugging information about SCOPE when performing
3342 ACTION at LINE. */
3343 static void
3344 cp_binding_level_debug (cp_binding_level *scope, int line, const char *action)
3346 const char *desc = cp_binding_level_descriptor (scope);
3347 if (scope->this_entity)
3348 verbatim ("%s %<%s(%E)%> %p %d\n", action, desc,
3349 scope->this_entity, (void *) scope, line);
3350 else
3351 verbatim ("%s %s %p %d\n", action, desc, (void *) scope, line);
3354 /* Return the estimated initial size of the hashtable of a NAMESPACE
3355 scope. */
3357 static inline size_t
3358 namespace_scope_ht_size (tree ns)
3360 tree name = DECL_NAME (ns);
3362 return name == std_identifier
3363 ? NAMESPACE_STD_HT_SIZE
3364 : (name == global_identifier
3365 ? GLOBAL_SCOPE_HT_SIZE
3366 : NAMESPACE_ORDINARY_HT_SIZE);
3369 /* A chain of binding_level structures awaiting reuse. */
3371 static GTY((deletable)) cp_binding_level *free_binding_level;
3373 /* Insert SCOPE as the innermost binding level. */
3375 void
3376 push_binding_level (cp_binding_level *scope)
3378 /* Add it to the front of currently active scopes stack. */
3379 scope->level_chain = current_binding_level;
3380 current_binding_level = scope;
3381 keep_next_level_flag = false;
3383 if (ENABLE_SCOPE_CHECKING)
3385 scope->binding_depth = binding_depth;
3386 indent (binding_depth);
3387 cp_binding_level_debug (scope, LOCATION_LINE (input_location),
3388 "push");
3389 binding_depth++;
3393 /* Create a new KIND scope and make it the top of the active scopes stack.
3394 ENTITY is the scope of the associated C++ entity (namespace, class,
3395 function, C++0x enumeration); it is NULL otherwise. */
3397 cp_binding_level *
3398 begin_scope (scope_kind kind, tree entity)
3400 cp_binding_level *scope;
3402 /* Reuse or create a struct for this binding level. */
3403 if (!ENABLE_SCOPE_CHECKING && free_binding_level)
3405 scope = free_binding_level;
3406 free_binding_level = scope->level_chain;
3407 memset (scope, 0, sizeof (cp_binding_level));
3409 else
3410 scope = ggc_cleared_alloc<cp_binding_level> ();
3412 scope->this_entity = entity;
3413 scope->more_cleanups_ok = true;
3414 switch (kind)
3416 case sk_cleanup:
3417 scope->keep = true;
3418 break;
3420 case sk_template_spec:
3421 scope->explicit_spec_p = true;
3422 kind = sk_template_parms;
3423 /* Fall through. */
3424 case sk_template_parms:
3425 case sk_block:
3426 case sk_try:
3427 case sk_catch:
3428 case sk_for:
3429 case sk_cond:
3430 case sk_class:
3431 case sk_scoped_enum:
3432 case sk_function_parms:
3433 case sk_transaction:
3434 case sk_omp:
3435 scope->keep = keep_next_level_flag;
3436 break;
3438 case sk_namespace:
3439 NAMESPACE_LEVEL (entity) = scope;
3440 break;
3442 default:
3443 /* Should not happen. */
3444 gcc_unreachable ();
3445 break;
3447 scope->kind = kind;
3449 push_binding_level (scope);
3451 return scope;
3454 /* We're about to leave current scope. Pop the top of the stack of
3455 currently active scopes. Return the enclosing scope, now active. */
3457 cp_binding_level *
3458 leave_scope (void)
3460 cp_binding_level *scope = current_binding_level;
3462 if (scope->kind == sk_namespace && class_binding_level)
3463 current_binding_level = class_binding_level;
3465 /* We cannot leave a scope, if there are none left. */
3466 if (NAMESPACE_LEVEL (global_namespace))
3467 gcc_assert (!global_scope_p (scope));
3469 if (ENABLE_SCOPE_CHECKING)
3471 indent (--binding_depth);
3472 cp_binding_level_debug (scope, LOCATION_LINE (input_location),
3473 "leave");
3476 /* Move one nesting level up. */
3477 current_binding_level = scope->level_chain;
3479 /* Namespace-scopes are left most probably temporarily, not
3480 completely; they can be reopened later, e.g. in namespace-extension
3481 or any name binding activity that requires us to resume a
3482 namespace. For classes, we cache some binding levels. For other
3483 scopes, we just make the structure available for reuse. */
3484 if (scope->kind != sk_namespace
3485 && scope->kind != sk_class)
3487 scope->level_chain = free_binding_level;
3488 gcc_assert (!ENABLE_SCOPE_CHECKING
3489 || scope->binding_depth == binding_depth);
3490 free_binding_level = scope;
3493 if (scope->kind == sk_class)
3495 /* Reset DEFINING_CLASS_P to allow for reuse of a
3496 class-defining scope in a non-defining context. */
3497 scope->defining_class_p = 0;
3499 /* Find the innermost enclosing class scope, and reset
3500 CLASS_BINDING_LEVEL appropriately. */
3501 class_binding_level = NULL;
3502 for (scope = current_binding_level; scope; scope = scope->level_chain)
3503 if (scope->kind == sk_class)
3505 class_binding_level = scope;
3506 break;
3510 return current_binding_level;
3513 static void
3514 resume_scope (cp_binding_level* b)
3516 /* Resuming binding levels is meant only for namespaces,
3517 and those cannot nest into classes. */
3518 gcc_assert (!class_binding_level);
3519 /* Also, resuming a non-directly nested namespace is a no-no. */
3520 gcc_assert (b->level_chain == current_binding_level);
3521 current_binding_level = b;
3522 if (ENABLE_SCOPE_CHECKING)
3524 b->binding_depth = binding_depth;
3525 indent (binding_depth);
3526 cp_binding_level_debug (b, LOCATION_LINE (input_location), "resume");
3527 binding_depth++;
3531 /* Return the innermost binding level that is not for a class scope. */
3533 static cp_binding_level *
3534 innermost_nonclass_level (void)
3536 cp_binding_level *b;
3538 b = current_binding_level;
3539 while (b->kind == sk_class)
3540 b = b->level_chain;
3542 return b;
3545 /* We're defining an object of type TYPE. If it needs a cleanup, but
3546 we're not allowed to add any more objects with cleanups to the current
3547 scope, create a new binding level. */
3549 void
3550 maybe_push_cleanup_level (tree type)
3552 if (type != error_mark_node
3553 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
3554 && current_binding_level->more_cleanups_ok == 0)
3556 begin_scope (sk_cleanup, NULL);
3557 current_binding_level->statement_list = push_stmt_list ();
3561 /* Return true if we are in the global binding level. */
3563 bool
3564 global_bindings_p (void)
3566 return global_scope_p (current_binding_level);
3569 /* True if we are currently in a toplevel binding level. This
3570 means either the global binding level or a namespace in a toplevel
3571 binding level. Since there are no non-toplevel namespace levels,
3572 this really means any namespace or template parameter level. We
3573 also include a class whose context is toplevel. */
3575 bool
3576 toplevel_bindings_p (void)
3578 cp_binding_level *b = innermost_nonclass_level ();
3580 return b->kind == sk_namespace || b->kind == sk_template_parms;
3583 /* True if this is a namespace scope, or if we are defining a class
3584 which is itself at namespace scope, or whose enclosing class is
3585 such a class, etc. */
3587 bool
3588 namespace_bindings_p (void)
3590 cp_binding_level *b = innermost_nonclass_level ();
3592 return b->kind == sk_namespace;
3595 /* True if the innermost non-class scope is a block scope. */
3597 bool
3598 local_bindings_p (void)
3600 cp_binding_level *b = innermost_nonclass_level ();
3601 return b->kind < sk_function_parms || b->kind == sk_omp;
3604 /* True if the current level needs to have a BLOCK made. */
3606 bool
3607 kept_level_p (void)
3609 return (current_binding_level->blocks != NULL_TREE
3610 || current_binding_level->keep
3611 || current_binding_level->kind == sk_cleanup
3612 || current_binding_level->names != NULL_TREE
3613 || current_binding_level->using_directives);
3616 /* Returns the kind of the innermost scope. */
3618 scope_kind
3619 innermost_scope_kind (void)
3621 return current_binding_level->kind;
3624 /* Returns true if this scope was created to store template parameters. */
3626 bool
3627 template_parm_scope_p (void)
3629 return innermost_scope_kind () == sk_template_parms;
3632 /* If KEEP is true, make a BLOCK node for the next binding level,
3633 unconditionally. Otherwise, use the normal logic to decide whether
3634 or not to create a BLOCK. */
3636 void
3637 keep_next_level (bool keep)
3639 keep_next_level_flag = keep;
3642 /* Return the list of declarations of the current local scope. */
3644 tree
3645 get_local_decls (void)
3647 gcc_assert (current_binding_level->kind != sk_namespace
3648 && current_binding_level->kind != sk_class);
3649 return current_binding_level->names;
3652 /* Return how many function prototypes we are currently nested inside. */
3655 function_parm_depth (void)
3657 int level = 0;
3658 cp_binding_level *b;
3660 for (b = current_binding_level;
3661 b->kind == sk_function_parms;
3662 b = b->level_chain)
3663 ++level;
3665 return level;
3668 /* For debugging. */
3669 static int no_print_functions = 0;
3670 static int no_print_builtins = 0;
3672 static void
3673 print_binding_level (cp_binding_level* lvl)
3675 tree t;
3676 int i = 0, len;
3677 fprintf (stderr, " blocks=%p", (void *) lvl->blocks);
3678 if (lvl->more_cleanups_ok)
3679 fprintf (stderr, " more-cleanups-ok");
3680 if (lvl->have_cleanups)
3681 fprintf (stderr, " have-cleanups");
3682 fprintf (stderr, "\n");
3683 if (lvl->names)
3685 fprintf (stderr, " names:\t");
3686 /* We can probably fit 3 names to a line? */
3687 for (t = lvl->names; t; t = TREE_CHAIN (t))
3689 if (no_print_functions && (TREE_CODE (t) == FUNCTION_DECL))
3690 continue;
3691 if (no_print_builtins
3692 && (TREE_CODE (t) == TYPE_DECL)
3693 && DECL_IS_BUILTIN (t))
3694 continue;
3696 /* Function decls tend to have longer names. */
3697 if (TREE_CODE (t) == FUNCTION_DECL)
3698 len = 3;
3699 else
3700 len = 2;
3701 i += len;
3702 if (i > 6)
3704 fprintf (stderr, "\n\t");
3705 i = len;
3707 print_node_brief (stderr, "", t, 0);
3708 if (t == error_mark_node)
3709 break;
3711 if (i)
3712 fprintf (stderr, "\n");
3714 if (vec_safe_length (lvl->class_shadowed))
3716 size_t i;
3717 cp_class_binding *b;
3718 fprintf (stderr, " class-shadowed:");
3719 FOR_EACH_VEC_ELT (*lvl->class_shadowed, i, b)
3720 fprintf (stderr, " %s ", IDENTIFIER_POINTER (b->identifier));
3721 fprintf (stderr, "\n");
3723 if (lvl->type_shadowed)
3725 fprintf (stderr, " type-shadowed:");
3726 for (t = lvl->type_shadowed; t; t = TREE_CHAIN (t))
3728 fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
3730 fprintf (stderr, "\n");
3734 DEBUG_FUNCTION void
3735 debug (cp_binding_level &ref)
3737 print_binding_level (&ref);
3740 DEBUG_FUNCTION void
3741 debug (cp_binding_level *ptr)
3743 if (ptr)
3744 debug (*ptr);
3745 else
3746 fprintf (stderr, "<nil>\n");
3750 void
3751 print_other_binding_stack (cp_binding_level *stack)
3753 cp_binding_level *level;
3754 for (level = stack; !global_scope_p (level); level = level->level_chain)
3756 fprintf (stderr, "binding level %p\n", (void *) level);
3757 print_binding_level (level);
3761 void
3762 print_binding_stack (void)
3764 cp_binding_level *b;
3765 fprintf (stderr, "current_binding_level=%p\n"
3766 "class_binding_level=%p\n"
3767 "NAMESPACE_LEVEL (global_namespace)=%p\n",
3768 (void *) current_binding_level, (void *) class_binding_level,
3769 (void *) NAMESPACE_LEVEL (global_namespace));
3770 if (class_binding_level)
3772 for (b = class_binding_level; b; b = b->level_chain)
3773 if (b == current_binding_level)
3774 break;
3775 if (b)
3776 b = class_binding_level;
3777 else
3778 b = current_binding_level;
3780 else
3781 b = current_binding_level;
3782 print_other_binding_stack (b);
3783 fprintf (stderr, "global:\n");
3784 print_binding_level (NAMESPACE_LEVEL (global_namespace));
3787 /* Return the type associated with ID. */
3789 static tree
3790 identifier_type_value_1 (tree id)
3792 /* There is no type with that name, anywhere. */
3793 if (REAL_IDENTIFIER_TYPE_VALUE (id) == NULL_TREE)
3794 return NULL_TREE;
3795 /* This is not the type marker, but the real thing. */
3796 if (REAL_IDENTIFIER_TYPE_VALUE (id) != global_type_node)
3797 return REAL_IDENTIFIER_TYPE_VALUE (id);
3798 /* Have to search for it. It must be on the global level, now.
3799 Ask lookup_name not to return non-types. */
3800 id = lookup_name_real (id, 2, 1, /*block_p=*/true, 0, 0);
3801 if (id)
3802 return TREE_TYPE (id);
3803 return NULL_TREE;
3806 /* Wrapper for identifier_type_value_1. */
3808 tree
3809 identifier_type_value (tree id)
3811 tree ret;
3812 timevar_start (TV_NAME_LOOKUP);
3813 ret = identifier_type_value_1 (id);
3814 timevar_stop (TV_NAME_LOOKUP);
3815 return ret;
3818 /* Push a definition of struct, union or enum tag named ID. into
3819 binding_level B. DECL is a TYPE_DECL for the type. We assume that
3820 the tag ID is not already defined. */
3822 static void
3823 set_identifier_type_value_with_scope (tree id, tree decl, cp_binding_level *b)
3825 tree type;
3827 if (b->kind != sk_namespace)
3829 /* Shadow the marker, not the real thing, so that the marker
3830 gets restored later. */
3831 tree old_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
3832 b->type_shadowed
3833 = tree_cons (id, old_type_value, b->type_shadowed);
3834 type = decl ? TREE_TYPE (decl) : NULL_TREE;
3835 TREE_TYPE (b->type_shadowed) = type;
3837 else
3839 tree *slot = find_namespace_slot (current_namespace, id, true);
3840 gcc_assert (decl);
3841 update_binding (b, NULL, slot, MAYBE_STAT_DECL (*slot), decl, false);
3843 /* Store marker instead of real type. */
3844 type = global_type_node;
3846 SET_IDENTIFIER_TYPE_VALUE (id, type);
3849 /* As set_identifier_type_value_with_scope, but using
3850 current_binding_level. */
3852 void
3853 set_identifier_type_value (tree id, tree decl)
3855 set_identifier_type_value_with_scope (id, decl, current_binding_level);
3858 /* Return the name for the constructor (or destructor) for the
3859 specified class. */
3861 tree
3862 constructor_name (tree type)
3864 tree decl = TYPE_NAME (TYPE_MAIN_VARIANT (type));
3866 return decl ? DECL_NAME (decl) : NULL_TREE;
3869 /* Returns TRUE if NAME is the name for the constructor for TYPE,
3870 which must be a class type. */
3872 bool
3873 constructor_name_p (tree name, tree type)
3875 gcc_assert (MAYBE_CLASS_TYPE_P (type));
3877 /* These don't have names. */
3878 if (TREE_CODE (type) == DECLTYPE_TYPE
3879 || TREE_CODE (type) == TYPEOF_TYPE)
3880 return false;
3882 if (name && name == constructor_name (type))
3883 return true;
3885 return false;
3888 /* Counter used to create anonymous type names. */
3890 static GTY(()) int anon_cnt;
3892 /* Return an IDENTIFIER which can be used as a name for
3893 unnamed structs and unions. */
3895 tree
3896 make_anon_name (void)
3898 char buf[32];
3900 sprintf (buf, anon_aggrname_format (), anon_cnt++);
3901 return get_identifier (buf);
3904 /* This code is practically identical to that for creating
3905 anonymous names, but is just used for lambdas instead. This isn't really
3906 necessary, but it's convenient to avoid treating lambdas like other
3907 unnamed types. */
3909 static GTY(()) int lambda_cnt = 0;
3911 tree
3912 make_lambda_name (void)
3914 char buf[32];
3916 sprintf (buf, LAMBDANAME_FORMAT, lambda_cnt++);
3917 return get_identifier (buf);
3920 /* Insert another USING_DECL into the current binding level, returning
3921 this declaration. If this is a redeclaration, do nothing, and
3922 return NULL_TREE if this not in namespace scope (in namespace
3923 scope, a using decl might extend any previous bindings). */
3925 static tree
3926 push_using_decl_1 (tree scope, tree name)
3928 tree decl;
3930 gcc_assert (TREE_CODE (scope) == NAMESPACE_DECL);
3931 gcc_assert (identifier_p (name));
3932 for (decl = current_binding_level->usings; decl; decl = DECL_CHAIN (decl))
3933 if (USING_DECL_SCOPE (decl) == scope && DECL_NAME (decl) == name)
3934 break;
3935 if (decl)
3936 return namespace_bindings_p () ? decl : NULL_TREE;
3937 decl = build_lang_decl (USING_DECL, name, NULL_TREE);
3938 USING_DECL_SCOPE (decl) = scope;
3939 DECL_CHAIN (decl) = current_binding_level->usings;
3940 current_binding_level->usings = decl;
3941 return decl;
3944 /* Wrapper for push_using_decl_1. */
3946 static tree
3947 push_using_decl (tree scope, tree name)
3949 tree ret;
3950 timevar_start (TV_NAME_LOOKUP);
3951 ret = push_using_decl_1 (scope, name);
3952 timevar_stop (TV_NAME_LOOKUP);
3953 return ret;
3956 /* Same as pushdecl, but define X in binding-level LEVEL. We rely on the
3957 caller to set DECL_CONTEXT properly.
3959 Note that this must only be used when X will be the new innermost
3960 binding for its name, as we tack it onto the front of IDENTIFIER_BINDING
3961 without checking to see if the current IDENTIFIER_BINDING comes from a
3962 closer binding level than LEVEL. */
3964 static tree
3965 do_pushdecl_with_scope (tree x, cp_binding_level *level, bool is_friend)
3967 cp_binding_level *b;
3969 if (level->kind == sk_class)
3971 b = class_binding_level;
3972 class_binding_level = level;
3973 pushdecl_class_level (x);
3974 class_binding_level = b;
3976 else
3978 tree function_decl = current_function_decl;
3979 if (level->kind == sk_namespace)
3980 current_function_decl = NULL_TREE;
3981 b = current_binding_level;
3982 current_binding_level = level;
3983 x = pushdecl (x, is_friend);
3984 current_binding_level = b;
3985 current_function_decl = function_decl;
3987 return x;
3990 /* Inject X into the local scope just before the function parms. */
3992 tree
3993 pushdecl_outermost_localscope (tree x)
3995 cp_binding_level *b = NULL;
3996 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3998 /* Find the scope just inside the function parms. */
3999 for (cp_binding_level *n = current_binding_level;
4000 n->kind != sk_function_parms; n = b->level_chain)
4001 b = n;
4003 tree ret = b ? do_pushdecl_with_scope (x, b, false) : error_mark_node;
4004 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4006 return ret;
4009 /* Check a non-member using-declaration. Return the name and scope
4010 being used, and the USING_DECL, or NULL_TREE on failure. */
4012 static tree
4013 validate_nonmember_using_decl (tree decl, tree scope, tree name)
4015 /* [namespace.udecl]
4016 A using-declaration for a class member shall be a
4017 member-declaration. */
4018 if (TYPE_P (scope))
4020 error ("%qT is not a namespace or unscoped enum", scope);
4021 return NULL_TREE;
4023 else if (scope == error_mark_node)
4024 return NULL_TREE;
4026 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR)
4028 /* 7.3.3/5
4029 A using-declaration shall not name a template-id. */
4030 error ("a using-declaration cannot specify a template-id. "
4031 "Try %<using %D%>", name);
4032 return NULL_TREE;
4035 if (TREE_CODE (decl) == NAMESPACE_DECL)
4037 error ("namespace %qD not allowed in using-declaration", decl);
4038 return NULL_TREE;
4041 if (TREE_CODE (decl) == SCOPE_REF)
4043 /* It's a nested name with template parameter dependent scope.
4044 This can only be using-declaration for class member. */
4045 error ("%qT is not a namespace", TREE_OPERAND (decl, 0));
4046 return NULL_TREE;
4049 decl = OVL_FIRST (decl);
4051 /* Make a USING_DECL. */
4052 tree using_decl = push_using_decl (scope, name);
4054 if (using_decl == NULL_TREE
4055 && at_function_scope_p ()
4056 && VAR_P (decl))
4057 /* C++11 7.3.3/10. */
4058 error ("%qD is already declared in this scope", name);
4060 return using_decl;
4063 /* Process a local-scope or namespace-scope using declaration. SCOPE
4064 is the nominated scope to search for NAME. VALUE_P and TYPE_P
4065 point to the binding for NAME in the current scope and are
4066 updated. */
4068 static void
4069 do_nonmember_using_decl (tree scope, tree name, tree *value_p, tree *type_p)
4071 name_lookup lookup (name, 0);
4073 if (!qualified_namespace_lookup (scope, &lookup))
4075 error ("%qD not declared", name);
4076 return;
4078 else if (TREE_CODE (lookup.value) == TREE_LIST)
4080 error ("reference to %qD is ambiguous", name);
4081 print_candidates (lookup.value);
4082 lookup.value = NULL_TREE;
4085 if (lookup.type && TREE_CODE (lookup.type) == TREE_LIST)
4087 error ("reference to %qD is ambiguous", name);
4088 print_candidates (lookup.type);
4089 lookup.type = NULL_TREE;
4092 tree value = *value_p;
4093 tree type = *type_p;
4095 /* Shift the old and new bindings around so we're comparing class and
4096 enumeration names to each other. */
4097 if (value && DECL_IMPLICIT_TYPEDEF_P (value))
4099 type = value;
4100 value = NULL_TREE;
4103 if (lookup.value && DECL_IMPLICIT_TYPEDEF_P (lookup.value))
4105 lookup.type = lookup.value;
4106 lookup.value = NULL_TREE;
4109 if (lookup.value && lookup.value != value)
4111 /* Check for using functions. */
4112 if (OVL_P (lookup.value) && (!value || OVL_P (value)))
4114 for (lkp_iterator usings (lookup.value); usings; ++usings)
4116 tree new_fn = *usings;
4118 /* [namespace.udecl]
4120 If a function declaration in namespace scope or block
4121 scope has the same name and the same parameter types as a
4122 function introduced by a using declaration the program is
4123 ill-formed. */
4124 bool found = false;
4125 for (ovl_iterator old (value); !found && old; ++old)
4127 tree old_fn = *old;
4129 if (new_fn == old_fn)
4130 /* The function already exists in the current
4131 namespace. */
4132 found = true;
4133 else if (old.using_p ())
4134 continue; /* This is a using decl. */
4135 else if (old.hidden_p () && !DECL_HIDDEN_FRIEND_P (old_fn))
4136 continue; /* This is an anticipated builtin. */
4137 else if (!matching_fn_p (new_fn, old_fn))
4138 continue; /* Parameters do not match. */
4139 else if (decls_match (new_fn, old_fn))
4140 found = true;
4141 else
4143 diagnose_name_conflict (new_fn, old_fn);
4144 found = true;
4148 if (!found)
4149 /* Unlike the overload case we don't drop anticipated
4150 builtins here. They don't cause a problem, and
4151 we'd like to match them with a future
4152 declaration. */
4153 value = ovl_insert (new_fn, value, true);
4156 else if (value
4157 /* Ignore anticipated builtins. */
4158 && !anticipated_builtin_p (value)
4159 && !decls_match (lookup.value, value))
4160 diagnose_name_conflict (lookup.value, value);
4161 else
4162 value = lookup.value;
4165 if (lookup.type && lookup.type != type)
4167 if (type && !decls_match (lookup.type, type))
4168 diagnose_name_conflict (lookup.type, type);
4169 else
4170 type = lookup.type;
4173 /* If bind->value is empty, shift any class or enumeration name back. */
4174 if (!value)
4176 value = type;
4177 type = NULL_TREE;
4180 *value_p = value;
4181 *type_p = type;
4184 /* Returns true if ANCESTOR encloses DESCENDANT, including matching.
4185 Both are namespaces. */
4187 bool
4188 is_nested_namespace (tree ancestor, tree descendant, bool inline_only)
4190 int depth = SCOPE_DEPTH (ancestor);
4192 if (!depth && !inline_only)
4193 /* The global namespace encloses everything. */
4194 return true;
4196 while (SCOPE_DEPTH (descendant) > depth
4197 && (!inline_only || DECL_NAMESPACE_INLINE_P (descendant)))
4198 descendant = CP_DECL_CONTEXT (descendant);
4200 return ancestor == descendant;
4203 /* Returns true if ROOT (a namespace, class, or function) encloses
4204 CHILD. CHILD may be either a class type or a namespace. */
4206 bool
4207 is_ancestor (tree root, tree child)
4209 gcc_assert ((TREE_CODE (root) == NAMESPACE_DECL
4210 || TREE_CODE (root) == FUNCTION_DECL
4211 || CLASS_TYPE_P (root)));
4212 gcc_assert ((TREE_CODE (child) == NAMESPACE_DECL
4213 || CLASS_TYPE_P (child)));
4215 /* The global namespace encloses everything. */
4216 if (root == global_namespace)
4217 return true;
4219 /* Search until we reach namespace scope. */
4220 while (TREE_CODE (child) != NAMESPACE_DECL)
4222 /* If we've reached the ROOT, it encloses CHILD. */
4223 if (root == child)
4224 return true;
4225 /* Go out one level. */
4226 if (TYPE_P (child))
4227 child = TYPE_NAME (child);
4228 child = CP_DECL_CONTEXT (child);
4231 if (TREE_CODE (root) == NAMESPACE_DECL)
4232 return is_nested_namespace (root, child);
4234 return false;
4237 /* Enter the class or namespace scope indicated by T suitable for name
4238 lookup. T can be arbitrary scope, not necessary nested inside the
4239 current scope. Returns a non-null scope to pop iff pop_scope
4240 should be called later to exit this scope. */
4242 tree
4243 push_scope (tree t)
4245 if (TREE_CODE (t) == NAMESPACE_DECL)
4246 push_decl_namespace (t);
4247 else if (CLASS_TYPE_P (t))
4249 if (!at_class_scope_p ()
4250 || !same_type_p (current_class_type, t))
4251 push_nested_class (t);
4252 else
4253 /* T is the same as the current scope. There is therefore no
4254 need to re-enter the scope. Since we are not actually
4255 pushing a new scope, our caller should not call
4256 pop_scope. */
4257 t = NULL_TREE;
4260 return t;
4263 /* Leave scope pushed by push_scope. */
4265 void
4266 pop_scope (tree t)
4268 if (t == NULL_TREE)
4269 return;
4270 if (TREE_CODE (t) == NAMESPACE_DECL)
4271 pop_decl_namespace ();
4272 else if CLASS_TYPE_P (t)
4273 pop_nested_class ();
4276 /* Subroutine of push_inner_scope. */
4278 static void
4279 push_inner_scope_r (tree outer, tree inner)
4281 tree prev;
4283 if (outer == inner
4284 || (TREE_CODE (inner) != NAMESPACE_DECL && !CLASS_TYPE_P (inner)))
4285 return;
4287 prev = CP_DECL_CONTEXT (TREE_CODE (inner) == NAMESPACE_DECL ? inner : TYPE_NAME (inner));
4288 if (outer != prev)
4289 push_inner_scope_r (outer, prev);
4290 if (TREE_CODE (inner) == NAMESPACE_DECL)
4292 cp_binding_level *save_template_parm = 0;
4293 /* Temporary take out template parameter scopes. They are saved
4294 in reversed order in save_template_parm. */
4295 while (current_binding_level->kind == sk_template_parms)
4297 cp_binding_level *b = current_binding_level;
4298 current_binding_level = b->level_chain;
4299 b->level_chain = save_template_parm;
4300 save_template_parm = b;
4303 resume_scope (NAMESPACE_LEVEL (inner));
4304 current_namespace = inner;
4306 /* Restore template parameter scopes. */
4307 while (save_template_parm)
4309 cp_binding_level *b = save_template_parm;
4310 save_template_parm = b->level_chain;
4311 b->level_chain = current_binding_level;
4312 current_binding_level = b;
4315 else
4316 pushclass (inner);
4319 /* Enter the scope INNER from current scope. INNER must be a scope
4320 nested inside current scope. This works with both name lookup and
4321 pushing name into scope. In case a template parameter scope is present,
4322 namespace is pushed under the template parameter scope according to
4323 name lookup rule in 14.6.1/6.
4325 Return the former current scope suitable for pop_inner_scope. */
4327 tree
4328 push_inner_scope (tree inner)
4330 tree outer = current_scope ();
4331 if (!outer)
4332 outer = current_namespace;
4334 push_inner_scope_r (outer, inner);
4335 return outer;
4338 /* Exit the current scope INNER back to scope OUTER. */
4340 void
4341 pop_inner_scope (tree outer, tree inner)
4343 if (outer == inner
4344 || (TREE_CODE (inner) != NAMESPACE_DECL && !CLASS_TYPE_P (inner)))
4345 return;
4347 while (outer != inner)
4349 if (TREE_CODE (inner) == NAMESPACE_DECL)
4351 cp_binding_level *save_template_parm = 0;
4352 /* Temporary take out template parameter scopes. They are saved
4353 in reversed order in save_template_parm. */
4354 while (current_binding_level->kind == sk_template_parms)
4356 cp_binding_level *b = current_binding_level;
4357 current_binding_level = b->level_chain;
4358 b->level_chain = save_template_parm;
4359 save_template_parm = b;
4362 pop_namespace ();
4364 /* Restore template parameter scopes. */
4365 while (save_template_parm)
4367 cp_binding_level *b = save_template_parm;
4368 save_template_parm = b->level_chain;
4369 b->level_chain = current_binding_level;
4370 current_binding_level = b;
4373 else
4374 popclass ();
4376 inner = CP_DECL_CONTEXT (TREE_CODE (inner) == NAMESPACE_DECL ? inner : TYPE_NAME (inner));
4380 /* Do a pushlevel for class declarations. */
4382 void
4383 pushlevel_class (void)
4385 class_binding_level = begin_scope (sk_class, current_class_type);
4388 /* ...and a poplevel for class declarations. */
4390 void
4391 poplevel_class (void)
4393 cp_binding_level *level = class_binding_level;
4394 cp_class_binding *cb;
4395 size_t i;
4396 tree shadowed;
4398 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4399 gcc_assert (level != 0);
4401 /* If we're leaving a toplevel class, cache its binding level. */
4402 if (current_class_depth == 1)
4403 previous_class_level = level;
4404 for (shadowed = level->type_shadowed;
4405 shadowed;
4406 shadowed = TREE_CHAIN (shadowed))
4407 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (shadowed), TREE_VALUE (shadowed));
4409 /* Remove the bindings for all of the class-level declarations. */
4410 if (level->class_shadowed)
4412 FOR_EACH_VEC_ELT (*level->class_shadowed, i, cb)
4414 IDENTIFIER_BINDING (cb->identifier) = cb->base->previous;
4415 cxx_binding_free (cb->base);
4417 ggc_free (level->class_shadowed);
4418 level->class_shadowed = NULL;
4421 /* Now, pop out of the binding level which we created up in the
4422 `pushlevel_class' routine. */
4423 gcc_assert (current_binding_level == level);
4424 leave_scope ();
4425 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4428 /* Set INHERITED_VALUE_BINDING_P on BINDING to true or false, as
4429 appropriate. DECL is the value to which a name has just been
4430 bound. CLASS_TYPE is the class in which the lookup occurred. */
4432 static void
4433 set_inherited_value_binding_p (cxx_binding *binding, tree decl,
4434 tree class_type)
4436 if (binding->value == decl && TREE_CODE (decl) != TREE_LIST)
4438 tree context;
4440 if (TREE_CODE (decl) == OVERLOAD)
4441 context = ovl_scope (decl);
4442 else
4444 gcc_assert (DECL_P (decl));
4445 context = context_for_name_lookup (decl);
4448 if (is_properly_derived_from (class_type, context))
4449 INHERITED_VALUE_BINDING_P (binding) = 1;
4450 else
4451 INHERITED_VALUE_BINDING_P (binding) = 0;
4453 else if (binding->value == decl)
4454 /* We only encounter a TREE_LIST when there is an ambiguity in the
4455 base classes. Such an ambiguity can be overridden by a
4456 definition in this class. */
4457 INHERITED_VALUE_BINDING_P (binding) = 1;
4458 else
4459 INHERITED_VALUE_BINDING_P (binding) = 0;
4462 /* Make the declaration of X appear in CLASS scope. */
4464 bool
4465 pushdecl_class_level (tree x)
4467 bool is_valid = true;
4468 bool subtime;
4470 /* Do nothing if we're adding to an outer lambda closure type,
4471 outer_binding will add it later if it's needed. */
4472 if (current_class_type != class_binding_level->this_entity)
4473 return true;
4475 subtime = timevar_cond_start (TV_NAME_LOOKUP);
4476 /* Get the name of X. */
4477 tree name = OVL_NAME (x);
4479 if (name)
4481 is_valid = push_class_level_binding (name, x);
4482 if (TREE_CODE (x) == TYPE_DECL)
4483 set_identifier_type_value (name, x);
4485 else if (ANON_AGGR_TYPE_P (TREE_TYPE (x)))
4487 /* If X is an anonymous aggregate, all of its members are
4488 treated as if they were members of the class containing the
4489 aggregate, for naming purposes. */
4490 tree f;
4492 for (f = TYPE_FIELDS (TREE_TYPE (x)); f; f = DECL_CHAIN (f))
4494 location_t save_location = input_location;
4495 input_location = DECL_SOURCE_LOCATION (f);
4496 if (!pushdecl_class_level (f))
4497 is_valid = false;
4498 input_location = save_location;
4501 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4502 return is_valid;
4505 /* Return the BINDING (if any) for NAME in SCOPE, which is a class
4506 scope. If the value returned is non-NULL, and the PREVIOUS field
4507 is not set, callers must set the PREVIOUS field explicitly. */
4509 static cxx_binding *
4510 get_class_binding (tree name, cp_binding_level *scope)
4512 tree class_type;
4513 tree type_binding;
4514 tree value_binding;
4515 cxx_binding *binding;
4517 class_type = scope->this_entity;
4519 /* Get the type binding. */
4520 type_binding = lookup_member (class_type, name,
4521 /*protect=*/2, /*want_type=*/true,
4522 tf_warning_or_error);
4523 /* Get the value binding. */
4524 value_binding = lookup_member (class_type, name,
4525 /*protect=*/2, /*want_type=*/false,
4526 tf_warning_or_error);
4528 if (value_binding
4529 && (TREE_CODE (value_binding) == TYPE_DECL
4530 || DECL_CLASS_TEMPLATE_P (value_binding)
4531 || (TREE_CODE (value_binding) == TREE_LIST
4532 && TREE_TYPE (value_binding) == error_mark_node
4533 && (TREE_CODE (TREE_VALUE (value_binding))
4534 == TYPE_DECL))))
4535 /* We found a type binding, even when looking for a non-type
4536 binding. This means that we already processed this binding
4537 above. */
4539 else if (value_binding)
4541 if (TREE_CODE (value_binding) == TREE_LIST
4542 && TREE_TYPE (value_binding) == error_mark_node)
4543 /* NAME is ambiguous. */
4545 else if (BASELINK_P (value_binding))
4546 /* NAME is some overloaded functions. */
4547 value_binding = BASELINK_FUNCTIONS (value_binding);
4550 /* If we found either a type binding or a value binding, create a
4551 new binding object. */
4552 if (type_binding || value_binding)
4554 binding = new_class_binding (name,
4555 value_binding,
4556 type_binding,
4557 scope);
4558 /* This is a class-scope binding, not a block-scope binding. */
4559 LOCAL_BINDING_P (binding) = 0;
4560 set_inherited_value_binding_p (binding, value_binding, class_type);
4562 else
4563 binding = NULL;
4565 return binding;
4568 /* Make the declaration(s) of X appear in CLASS scope under the name
4569 NAME. Returns true if the binding is valid. */
4571 static bool
4572 push_class_level_binding_1 (tree name, tree x)
4574 cxx_binding *binding;
4575 tree decl = x;
4576 bool ok;
4578 /* The class_binding_level will be NULL if x is a template
4579 parameter name in a member template. */
4580 if (!class_binding_level)
4581 return true;
4583 if (name == error_mark_node)
4584 return false;
4586 /* Can happen for an erroneous declaration (c++/60384). */
4587 if (!identifier_p (name))
4589 gcc_assert (errorcount || sorrycount);
4590 return false;
4593 /* Check for invalid member names. But don't worry about a default
4594 argument-scope lambda being pushed after the class is complete. */
4595 gcc_assert (TYPE_BEING_DEFINED (current_class_type)
4596 || LAMBDA_TYPE_P (TREE_TYPE (decl)));
4597 /* Check that we're pushing into the right binding level. */
4598 gcc_assert (current_class_type == class_binding_level->this_entity);
4600 /* We could have been passed a tree list if this is an ambiguous
4601 declaration. If so, pull the declaration out because
4602 check_template_shadow will not handle a TREE_LIST. */
4603 if (TREE_CODE (decl) == TREE_LIST
4604 && TREE_TYPE (decl) == error_mark_node)
4605 decl = TREE_VALUE (decl);
4607 if (!check_template_shadow (decl))
4608 return false;
4610 /* [class.mem]
4612 If T is the name of a class, then each of the following shall
4613 have a name different from T:
4615 -- every static data member of class T;
4617 -- every member of class T that is itself a type;
4619 -- every enumerator of every member of class T that is an
4620 enumerated type;
4622 -- every member of every anonymous union that is a member of
4623 class T.
4625 (Non-static data members were also forbidden to have the same
4626 name as T until TC1.) */
4627 if ((VAR_P (x)
4628 || TREE_CODE (x) == CONST_DECL
4629 || (TREE_CODE (x) == TYPE_DECL
4630 && !DECL_SELF_REFERENCE_P (x))
4631 /* A data member of an anonymous union. */
4632 || (TREE_CODE (x) == FIELD_DECL
4633 && DECL_CONTEXT (x) != current_class_type))
4634 && DECL_NAME (x) == DECL_NAME (TYPE_NAME (current_class_type)))
4636 tree scope = context_for_name_lookup (x);
4637 if (TYPE_P (scope) && same_type_p (scope, current_class_type))
4639 error ("%qD has the same name as the class in which it is "
4640 "declared",
4642 return false;
4646 /* Get the current binding for NAME in this class, if any. */
4647 binding = IDENTIFIER_BINDING (name);
4648 if (!binding || binding->scope != class_binding_level)
4650 binding = get_class_binding (name, class_binding_level);
4651 /* If a new binding was created, put it at the front of the
4652 IDENTIFIER_BINDING list. */
4653 if (binding)
4655 binding->previous = IDENTIFIER_BINDING (name);
4656 IDENTIFIER_BINDING (name) = binding;
4660 /* If there is already a binding, then we may need to update the
4661 current value. */
4662 if (binding && binding->value)
4664 tree bval = binding->value;
4665 tree old_decl = NULL_TREE;
4666 tree target_decl = strip_using_decl (decl);
4667 tree target_bval = strip_using_decl (bval);
4669 if (INHERITED_VALUE_BINDING_P (binding))
4671 /* If the old binding was from a base class, and was for a
4672 tag name, slide it over to make room for the new binding.
4673 The old binding is still visible if explicitly qualified
4674 with a class-key. */
4675 if (TREE_CODE (target_bval) == TYPE_DECL
4676 && DECL_ARTIFICIAL (target_bval)
4677 && !(TREE_CODE (target_decl) == TYPE_DECL
4678 && DECL_ARTIFICIAL (target_decl)))
4680 old_decl = binding->type;
4681 binding->type = bval;
4682 binding->value = NULL_TREE;
4683 INHERITED_VALUE_BINDING_P (binding) = 0;
4685 else
4687 old_decl = bval;
4688 /* Any inherited type declaration is hidden by the type
4689 declaration in the derived class. */
4690 if (TREE_CODE (target_decl) == TYPE_DECL
4691 && DECL_ARTIFICIAL (target_decl))
4692 binding->type = NULL_TREE;
4695 else if (TREE_CODE (target_decl) == OVERLOAD
4696 && OVL_P (target_bval))
4697 old_decl = bval;
4698 else if (TREE_CODE (decl) == USING_DECL
4699 && TREE_CODE (bval) == USING_DECL
4700 && same_type_p (USING_DECL_SCOPE (decl),
4701 USING_DECL_SCOPE (bval)))
4702 /* This is a using redeclaration that will be diagnosed later
4703 in supplement_binding */
4705 else if (TREE_CODE (decl) == USING_DECL
4706 && TREE_CODE (bval) == USING_DECL
4707 && DECL_DEPENDENT_P (decl)
4708 && DECL_DEPENDENT_P (bval))
4709 return true;
4710 else if (TREE_CODE (decl) == USING_DECL
4711 && OVL_P (target_bval))
4712 old_decl = bval;
4713 else if (TREE_CODE (bval) == USING_DECL
4714 && OVL_P (target_decl))
4715 return true;
4717 if (old_decl && binding->scope == class_binding_level)
4719 binding->value = x;
4720 /* It is always safe to clear INHERITED_VALUE_BINDING_P
4721 here. This function is only used to register bindings
4722 from with the class definition itself. */
4723 INHERITED_VALUE_BINDING_P (binding) = 0;
4724 return true;
4728 /* Note that we declared this value so that we can issue an error if
4729 this is an invalid redeclaration of a name already used for some
4730 other purpose. */
4731 note_name_declared_in_class (name, decl);
4733 /* If we didn't replace an existing binding, put the binding on the
4734 stack of bindings for the identifier, and update the shadowed
4735 list. */
4736 if (binding && binding->scope == class_binding_level)
4737 /* Supplement the existing binding. */
4738 ok = supplement_binding (binding, decl);
4739 else
4741 /* Create a new binding. */
4742 push_binding (name, decl, class_binding_level);
4743 ok = true;
4746 return ok;
4749 /* Wrapper for push_class_level_binding_1. */
4751 bool
4752 push_class_level_binding (tree name, tree x)
4754 bool ret;
4755 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4756 ret = push_class_level_binding_1 (name, x);
4757 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4758 return ret;
4761 /* Process "using SCOPE::NAME" in a class scope. Return the
4762 USING_DECL created. */
4764 tree
4765 do_class_using_decl (tree scope, tree name)
4767 if (name == error_mark_node)
4768 return NULL_TREE;
4770 if (!scope || !TYPE_P (scope))
4772 error ("using-declaration for non-member at class scope");
4773 return NULL_TREE;
4776 /* Make sure the name is not invalid */
4777 if (TREE_CODE (name) == BIT_NOT_EXPR)
4779 error ("%<%T::%D%> names destructor", scope, name);
4780 return NULL_TREE;
4783 /* Using T::T declares inheriting ctors, even if T is a typedef. */
4784 if (MAYBE_CLASS_TYPE_P (scope)
4785 && (name == TYPE_IDENTIFIER (scope)
4786 || constructor_name_p (name, scope)))
4788 maybe_warn_cpp0x (CPP0X_INHERITING_CTORS);
4789 name = ctor_identifier;
4790 CLASSTYPE_NON_AGGREGATE (current_class_type) = true;
4793 /* Cannot introduce a constructor name. */
4794 if (constructor_name_p (name, current_class_type))
4796 error ("%<%T::%D%> names constructor in %qT",
4797 scope, name, current_class_type);
4798 return NULL_TREE;
4801 /* From [namespace.udecl]:
4803 A using-declaration used as a member-declaration shall refer to a
4804 member of a base class of the class being defined.
4806 In general, we cannot check this constraint in a template because
4807 we do not know the entire set of base classes of the current
4808 class type. Morover, if SCOPE is dependent, it might match a
4809 non-dependent base. */
4811 tree decl = NULL_TREE;
4812 if (!dependent_scope_p (scope))
4814 base_kind b_kind;
4815 tree binfo = lookup_base (current_class_type, scope, ba_any, &b_kind,
4816 tf_warning_or_error);
4817 if (b_kind < bk_proper_base)
4819 /* If there are dependent bases, scope might resolve at
4820 instantiation time, even if it isn't exactly one of the
4821 dependent bases. */
4822 if (b_kind == bk_same_type || !any_dependent_bases_p ())
4824 error_not_base_type (scope, current_class_type);
4825 return NULL_TREE;
4828 else if (name == ctor_identifier && !binfo_direct_p (binfo))
4830 error ("cannot inherit constructors from indirect base %qT", scope);
4831 return NULL_TREE;
4833 else if (!IDENTIFIER_CONV_OP_P (name)
4834 || !dependent_type_p (TREE_TYPE (name)))
4836 decl = lookup_member (binfo, name, 0, false, tf_warning_or_error);
4837 if (!decl)
4839 error ("no members matching %<%T::%D%> in %q#T", scope, name,
4840 scope);
4841 return NULL_TREE;
4844 /* The binfo from which the functions came does not matter. */
4845 if (BASELINK_P (decl))
4846 decl = BASELINK_FUNCTIONS (decl);
4850 tree value = build_lang_decl (USING_DECL, name, NULL_TREE);
4851 USING_DECL_DECLS (value) = decl;
4852 USING_DECL_SCOPE (value) = scope;
4853 DECL_DEPENDENT_P (value) = !decl;
4855 return value;
4859 /* Return the binding for NAME in NS. If NS is NULL, look in
4860 global_namespace. */
4862 tree
4863 get_namespace_binding (tree ns, tree name)
4865 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4866 if (!ns)
4867 ns = global_namespace;
4868 gcc_checking_assert (!DECL_NAMESPACE_ALIAS (ns));
4869 tree ret = find_namespace_value (ns, name);
4870 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4871 return ret;
4874 /* Push internal DECL into the global namespace. Does not do the
4875 full overload fn handling and does not add it to the list of things
4876 in the namespace. */
4878 void
4879 set_global_binding (tree decl)
4881 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4883 tree *slot = find_namespace_slot (global_namespace, DECL_NAME (decl), true);
4885 if (*slot)
4886 /* The user's placed something in the implementor's namespace. */
4887 diagnose_name_conflict (decl, MAYBE_STAT_DECL (*slot));
4889 /* Force the binding, so compiler internals continue to work. */
4890 *slot = decl;
4892 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4895 /* Set the context of a declaration to scope. Complain if we are not
4896 outside scope. */
4898 void
4899 set_decl_namespace (tree decl, tree scope, bool friendp)
4901 /* Get rid of namespace aliases. */
4902 scope = ORIGINAL_NAMESPACE (scope);
4904 /* It is ok for friends to be qualified in parallel space. */
4905 if (!friendp && !is_nested_namespace (current_namespace, scope))
4906 error ("declaration of %qD not in a namespace surrounding %qD",
4907 decl, scope);
4908 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
4910 /* See whether this has been declared in the namespace or inline
4911 children. */
4912 tree old = NULL_TREE;
4914 name_lookup lookup (DECL_NAME (decl), LOOKUP_HIDDEN);
4915 if (!lookup.search_qualified (scope, /*usings=*/false))
4916 /* No old declaration at all. */
4917 goto not_found;
4918 old = lookup.value;
4921 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
4922 if (TREE_CODE (old) == TREE_LIST)
4924 ambiguous:
4925 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
4926 error ("reference to %qD is ambiguous", decl);
4927 print_candidates (old);
4928 return;
4931 if (!DECL_DECLARES_FUNCTION_P (decl))
4933 /* Don't compare non-function decls with decls_match here, since
4934 it can't check for the correct constness at this
4935 point. pushdecl will find those errors later. */
4937 /* We might have found it in an inline namespace child of SCOPE. */
4938 if (TREE_CODE (decl) == TREE_CODE (old))
4939 DECL_CONTEXT (decl) = DECL_CONTEXT (old);
4941 found:
4942 /* Writing "N::i" to declare something directly in "N" is invalid. */
4943 if (CP_DECL_CONTEXT (decl) == current_namespace
4944 && at_namespace_scope_p ())
4945 error ("explicit qualification in declaration of %qD", decl);
4946 return;
4949 /* Since decl is a function, old should contain a function decl. */
4950 if (!OVL_P (old))
4951 goto not_found;
4953 /* We handle these in check_explicit_instantiation_namespace. */
4954 if (processing_explicit_instantiation)
4955 return;
4956 if (processing_template_decl || processing_specialization)
4957 /* We have not yet called push_template_decl to turn a
4958 FUNCTION_DECL into a TEMPLATE_DECL, so the declarations won't
4959 match. But, we'll check later, when we construct the
4960 template. */
4961 return;
4962 /* Instantiations or specializations of templates may be declared as
4963 friends in any namespace. */
4964 if (friendp && DECL_USE_TEMPLATE (decl))
4965 return;
4967 tree found;
4968 found = NULL_TREE;
4970 for (lkp_iterator iter (old); iter; ++iter)
4972 if (iter.using_p ())
4973 continue;
4975 tree ofn = *iter;
4977 /* Adjust DECL_CONTEXT first so decls_match will return true
4978 if DECL will match a declaration in an inline namespace. */
4979 DECL_CONTEXT (decl) = DECL_CONTEXT (ofn);
4980 if (decls_match (decl, ofn))
4982 if (found)
4984 /* We found more than one matching declaration. */
4985 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
4986 goto ambiguous;
4988 found = ofn;
4992 if (found)
4994 if (DECL_HIDDEN_FRIEND_P (found))
4996 pedwarn (DECL_SOURCE_LOCATION (decl), 0,
4997 "%qD has not been declared within %qD", decl, scope);
4998 inform (DECL_SOURCE_LOCATION (found),
4999 "only here as a %<friend%>");
5001 DECL_CONTEXT (decl) = DECL_CONTEXT (found);
5002 goto found;
5005 not_found:
5006 /* It didn't work, go back to the explicit scope. */
5007 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
5008 error ("%qD should have been declared inside %qD", decl, scope);
5011 /* Return the namespace where the current declaration is declared. */
5013 tree
5014 current_decl_namespace (void)
5016 tree result;
5017 /* If we have been pushed into a different namespace, use it. */
5018 if (!vec_safe_is_empty (decl_namespace_list))
5019 return decl_namespace_list->last ();
5021 if (current_class_type)
5022 result = decl_namespace_context (current_class_type);
5023 else if (current_function_decl)
5024 result = decl_namespace_context (current_function_decl);
5025 else
5026 result = current_namespace;
5027 return result;
5030 /* Process any ATTRIBUTES on a namespace definition. Returns true if
5031 attribute visibility is seen. */
5033 bool
5034 handle_namespace_attrs (tree ns, tree attributes)
5036 tree d;
5037 bool saw_vis = false;
5039 for (d = attributes; d; d = TREE_CHAIN (d))
5041 tree name = get_attribute_name (d);
5042 tree args = TREE_VALUE (d);
5044 if (is_attribute_p ("visibility", name))
5046 /* attribute visibility is a property of the syntactic block
5047 rather than the namespace as a whole, so we don't touch the
5048 NAMESPACE_DECL at all. */
5049 tree x = args ? TREE_VALUE (args) : NULL_TREE;
5050 if (x == NULL_TREE || TREE_CODE (x) != STRING_CST || TREE_CHAIN (args))
5052 warning (OPT_Wattributes,
5053 "%qD attribute requires a single NTBS argument",
5054 name);
5055 continue;
5058 if (!TREE_PUBLIC (ns))
5059 warning (OPT_Wattributes,
5060 "%qD attribute is meaningless since members of the "
5061 "anonymous namespace get local symbols", name);
5063 push_visibility (TREE_STRING_POINTER (x), 1);
5064 saw_vis = true;
5066 else if (is_attribute_p ("abi_tag", name))
5068 if (!DECL_NAME (ns))
5070 warning (OPT_Wattributes, "ignoring %qD attribute on anonymous "
5071 "namespace", name);
5072 continue;
5074 if (!DECL_NAMESPACE_INLINE_P (ns))
5076 warning (OPT_Wattributes, "ignoring %qD attribute on non-inline "
5077 "namespace", name);
5078 continue;
5080 if (!args)
5082 tree dn = DECL_NAME (ns);
5083 args = build_string (IDENTIFIER_LENGTH (dn) + 1,
5084 IDENTIFIER_POINTER (dn));
5085 TREE_TYPE (args) = char_array_type_node;
5086 args = fix_string_type (args);
5087 args = build_tree_list (NULL_TREE, args);
5089 if (check_abi_tag_args (args, name))
5090 DECL_ATTRIBUTES (ns) = tree_cons (name, args,
5091 DECL_ATTRIBUTES (ns));
5093 else
5095 warning (OPT_Wattributes, "%qD attribute directive ignored",
5096 name);
5097 continue;
5101 return saw_vis;
5104 /* Temporarily set the namespace for the current declaration. */
5106 void
5107 push_decl_namespace (tree decl)
5109 if (TREE_CODE (decl) != NAMESPACE_DECL)
5110 decl = decl_namespace_context (decl);
5111 vec_safe_push (decl_namespace_list, ORIGINAL_NAMESPACE (decl));
5114 /* [namespace.memdef]/2 */
5116 void
5117 pop_decl_namespace (void)
5119 decl_namespace_list->pop ();
5122 /* Process a namespace-alias declaration. */
5124 void
5125 do_namespace_alias (tree alias, tree name_space)
5127 if (name_space == error_mark_node)
5128 return;
5130 gcc_assert (TREE_CODE (name_space) == NAMESPACE_DECL);
5132 name_space = ORIGINAL_NAMESPACE (name_space);
5134 /* Build the alias. */
5135 alias = build_lang_decl (NAMESPACE_DECL, alias, void_type_node);
5136 DECL_NAMESPACE_ALIAS (alias) = name_space;
5137 DECL_EXTERNAL (alias) = 1;
5138 DECL_CONTEXT (alias) = FROB_CONTEXT (current_scope ());
5139 pushdecl (alias);
5141 /* Emit debug info for namespace alias. */
5142 if (!building_stmt_list_p ())
5143 (*debug_hooks->early_global_decl) (alias);
5146 /* Like pushdecl, only it places X in the current namespace,
5147 if appropriate. */
5149 tree
5150 pushdecl_namespace_level (tree x, bool is_friend)
5152 cp_binding_level *b = current_binding_level;
5153 tree t;
5155 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
5156 t = do_pushdecl_with_scope
5157 (x, NAMESPACE_LEVEL (current_namespace), is_friend);
5159 /* Now, the type_shadowed stack may screw us. Munge it so it does
5160 what we want. */
5161 if (TREE_CODE (t) == TYPE_DECL)
5163 tree name = DECL_NAME (t);
5164 tree newval;
5165 tree *ptr = (tree *)0;
5166 for (; !global_scope_p (b); b = b->level_chain)
5168 tree shadowed = b->type_shadowed;
5169 for (; shadowed; shadowed = TREE_CHAIN (shadowed))
5170 if (TREE_PURPOSE (shadowed) == name)
5172 ptr = &TREE_VALUE (shadowed);
5173 /* Can't break out of the loop here because sometimes
5174 a binding level will have duplicate bindings for
5175 PT names. It's gross, but I haven't time to fix it. */
5178 newval = TREE_TYPE (t);
5179 if (ptr == (tree *)0)
5181 /* @@ This shouldn't be needed. My test case "zstring.cc" trips
5182 up here if this is changed to an assertion. --KR */
5183 SET_IDENTIFIER_TYPE_VALUE (name, t);
5185 else
5187 *ptr = newval;
5190 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
5191 return t;
5194 /* Process a using-declaration appearing in namespace scope. */
5196 void
5197 finish_namespace_using_decl (tree decl, tree scope, tree name)
5199 tree orig_decl = decl;
5201 gcc_checking_assert (current_binding_level->kind == sk_namespace
5202 && !processing_template_decl);
5203 decl = validate_nonmember_using_decl (decl, scope, name);
5204 if (decl == NULL_TREE)
5205 return;
5207 tree *slot = find_namespace_slot (current_namespace, name, true);
5208 tree val = slot ? MAYBE_STAT_DECL (*slot) : NULL_TREE;
5209 tree type = slot ? MAYBE_STAT_TYPE (*slot) : NULL_TREE;
5210 do_nonmember_using_decl (scope, name, &val, &type);
5211 if (STAT_HACK_P (*slot))
5213 STAT_DECL (*slot) = val;
5214 STAT_TYPE (*slot) = type;
5216 else if (type)
5217 *slot = stat_hack (val, type);
5218 else
5219 *slot = val;
5221 /* Emit debug info. */
5222 cp_emit_debug_info_for_using (orig_decl, current_namespace);
5225 /* Process a using-declaration at function scope. */
5227 void
5228 finish_local_using_decl (tree decl, tree scope, tree name)
5230 tree orig_decl = decl;
5232 gcc_checking_assert (current_binding_level->kind != sk_class
5233 && current_binding_level->kind != sk_namespace);
5234 decl = validate_nonmember_using_decl (decl, scope, name);
5235 if (decl == NULL_TREE)
5236 return;
5238 add_decl_expr (decl);
5240 cxx_binding *binding = find_local_binding (current_binding_level, name);
5241 tree value = binding ? binding->value : NULL_TREE;
5242 tree type = binding ? binding->type : NULL_TREE;
5244 do_nonmember_using_decl (scope, name, &value, &type);
5246 if (!value)
5248 else if (binding && value == binding->value)
5250 else if (binding && binding->value && TREE_CODE (value) == OVERLOAD)
5252 update_local_overload (IDENTIFIER_BINDING (name), value);
5253 IDENTIFIER_BINDING (name)->value = value;
5255 else
5256 /* Install the new binding. */
5257 push_local_binding (name, value, true);
5259 if (!type)
5261 else if (binding && type == binding->type)
5263 else
5265 push_local_binding (name, type, true);
5266 set_identifier_type_value (name, type);
5269 /* Emit debug info. */
5270 if (!processing_template_decl)
5271 cp_emit_debug_info_for_using (orig_decl, current_scope ());
5274 /* Return the declarations that are members of the namespace NS. */
5276 tree
5277 cp_namespace_decls (tree ns)
5279 return NAMESPACE_LEVEL (ns)->names;
5282 /* Combine prefer_type and namespaces_only into flags. */
5284 static int
5285 lookup_flags (int prefer_type, int namespaces_only)
5287 if (namespaces_only)
5288 return LOOKUP_PREFER_NAMESPACES;
5289 if (prefer_type > 1)
5290 return LOOKUP_PREFER_TYPES;
5291 if (prefer_type > 0)
5292 return LOOKUP_PREFER_BOTH;
5293 return 0;
5296 /* Given a lookup that returned VAL, use FLAGS to decide if we want to
5297 ignore it or not. Subroutine of lookup_name_real and
5298 lookup_type_scope. */
5300 static bool
5301 qualify_lookup (tree val, int flags)
5303 if (val == NULL_TREE)
5304 return false;
5305 if ((flags & LOOKUP_PREFER_NAMESPACES) && TREE_CODE (val) == NAMESPACE_DECL)
5306 return true;
5307 if (flags & LOOKUP_PREFER_TYPES)
5309 tree target_val = strip_using_decl (val);
5310 if (TREE_CODE (target_val) == TYPE_DECL
5311 || TREE_CODE (target_val) == TEMPLATE_DECL)
5312 return true;
5314 if (flags & (LOOKUP_PREFER_NAMESPACES | LOOKUP_PREFER_TYPES))
5315 return false;
5316 /* Look through lambda things that we shouldn't be able to see. */
5317 if (!(flags & LOOKUP_HIDDEN) && is_lambda_ignored_entity (val))
5318 return false;
5319 return true;
5322 /* Suggest alternatives for NAME, an IDENTIFIER_NODE for which name
5323 lookup failed. Search through all available namespaces and print out
5324 possible candidates. If no exact matches are found, and
5325 SUGGEST_MISSPELLINGS is true, then also look for near-matches and
5326 suggest the best near-match, if there is one. */
5328 void
5329 suggest_alternatives_for (location_t location, tree name,
5330 bool suggest_misspellings)
5332 vec<tree> candidates = vNULL;
5333 vec<tree> worklist = vNULL;
5334 unsigned limit = PARAM_VALUE (CXX_MAX_NAMESPACES_FOR_DIAGNOSTIC_HELP);
5335 bool limited = false;
5337 /* Breadth-first search of namespaces. Up to limit namespaces
5338 searched (limit zero == unlimited). */
5339 worklist.safe_push (global_namespace);
5340 for (unsigned ix = 0; ix != worklist.length (); ix++)
5342 tree ns = worklist[ix];
5343 name_lookup lookup (name);
5345 if (lookup.search_qualified (ns, false))
5346 candidates.safe_push (lookup.value);
5348 if (!limited)
5350 /* Look for child namespaces. We have to do this
5351 indirectly because they are chained in reverse order,
5352 which is confusing to the user. */
5353 vec<tree> children = vNULL;
5355 for (tree decl = NAMESPACE_LEVEL (ns)->names;
5356 decl; decl = TREE_CHAIN (decl))
5357 if (TREE_CODE (decl) == NAMESPACE_DECL
5358 && !DECL_NAMESPACE_ALIAS (decl)
5359 && !DECL_NAMESPACE_INLINE_P (decl))
5360 children.safe_push (decl);
5362 while (!limited && !children.is_empty ())
5364 if (worklist.length () == limit)
5366 /* Unconditionally warn that the search was truncated. */
5367 inform (location,
5368 "maximum limit of %d namespaces searched for %qE",
5369 limit, name);
5370 limited = true;
5372 else
5373 worklist.safe_push (children.pop ());
5375 children.release ();
5378 worklist.release ();
5380 if (candidates.length ())
5382 inform_n (location, candidates.length (),
5383 "suggested alternative:",
5384 "suggested alternatives:");
5385 for (unsigned ix = 0; ix != candidates.length (); ix++)
5387 tree val = candidates[ix];
5389 inform (location_of (val), " %qE", val);
5391 candidates.release ();
5393 else if (!suggest_misspellings)
5395 else if (name_hint hint = lookup_name_fuzzy (name, FUZZY_LOOKUP_NAME,
5396 location))
5398 /* Show a spelling correction. */
5399 gcc_rich_location richloc (location);
5401 richloc.add_fixit_replace (hint.suggestion ());
5402 inform (&richloc, "suggested alternative: %qs", hint.suggestion ());
5406 /* Subroutine of maybe_suggest_missing_header for handling unrecognized names
5407 for some of the most common names within "std::".
5408 Given non-NULL NAME, a name for lookup within "std::", return the header
5409 name defining it within the C++ Standard Library (with '<' and '>'),
5410 or NULL. */
5412 static const char *
5413 get_std_name_hint (const char *name)
5415 struct std_name_hint
5417 const char *name;
5418 const char *header;
5420 static const std_name_hint hints[] = {
5421 /* <array>. */
5422 {"array", "<array>"}, // C++11
5423 /* <complex>. */
5424 {"complex", "<complex>"},
5425 {"complex_literals", "<complex>"},
5426 /* <deque>. */
5427 {"deque", "<deque>"},
5428 /* <forward_list>. */
5429 {"forward_list", "<forward_list>"}, // C++11
5430 /* <fstream>. */
5431 {"basic_filebuf", "<fstream>"},
5432 {"basic_ifstream", "<fstream>"},
5433 {"basic_ofstream", "<fstream>"},
5434 {"basic_fstream", "<fstream>"},
5435 /* <iostream>. */
5436 {"cin", "<iostream>"},
5437 {"cout", "<iostream>"},
5438 {"cerr", "<iostream>"},
5439 {"clog", "<iostream>"},
5440 {"wcin", "<iostream>"},
5441 {"wcout", "<iostream>"},
5442 {"wclog", "<iostream>"},
5443 /* <list>. */
5444 {"list", "<list>"},
5445 /* <map>. */
5446 {"map", "<map>"},
5447 {"multimap", "<map>"},
5448 /* <queue>. */
5449 {"queue", "<queue>"},
5450 {"priority_queue", "<queue>"},
5451 /* <ostream>. */
5452 {"ostream", "<ostream>"},
5453 {"wostream", "<ostream>"},
5454 {"ends", "<ostream>"},
5455 {"flush", "<ostream>"},
5456 {"endl", "<ostream>"},
5457 /* <set>. */
5458 {"set", "<set>"},
5459 {"multiset", "<set>"},
5460 /* <sstream>. */
5461 {"basic_stringbuf", "<sstream>"},
5462 {"basic_istringstream", "<sstream>"},
5463 {"basic_ostringstream", "<sstream>"},
5464 {"basic_stringstream", "<sstream>"},
5465 /* <stack>. */
5466 {"stack", "<stack>"},
5467 /* <string>. */
5468 {"string", "<string>"},
5469 {"wstring", "<string>"},
5470 {"u16string", "<string>"},
5471 {"u32string", "<string>"},
5472 /* <unordered_map>. */
5473 {"unordered_map", "<unordered_map>"}, // C++11
5474 {"unordered_multimap", "<unordered_map>"}, // C++11
5475 /* <unordered_set>. */
5476 {"unordered_set", "<unordered_set>"}, // C++11
5477 {"unordered_multiset", "<unordered_set>"}, // C++11
5478 /* <vector>. */
5479 {"vector", "<vector>"},
5481 const size_t num_hints = sizeof (hints) / sizeof (hints[0]);
5482 for (size_t i = 0; i < num_hints; i++)
5484 if (strcmp (name, hints[i].name) == 0)
5485 return hints[i].header;
5487 return NULL;
5490 /* If SCOPE is the "std" namespace, then suggest pertinent header
5491 files for NAME at LOCATION.
5492 Return true iff a suggestion was offered. */
5494 static bool
5495 maybe_suggest_missing_header (location_t location, tree name, tree scope)
5497 if (scope == NULL_TREE)
5498 return false;
5499 if (TREE_CODE (scope) != NAMESPACE_DECL)
5500 return false;
5501 /* We only offer suggestions for the "std" namespace. */
5502 if (scope != std_node)
5503 return false;
5504 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
5506 const char *name_str = IDENTIFIER_POINTER (name);
5507 const char *header_hint = get_std_name_hint (name_str);
5508 if (!header_hint)
5509 return false;
5511 gcc_rich_location richloc (location);
5512 maybe_add_include_fixit (&richloc, header_hint);
5513 inform (&richloc,
5514 "%<std::%s%> is defined in header %qs;"
5515 " did you forget to %<#include %s%>?",
5516 name_str, header_hint, header_hint);
5517 return true;
5520 /* Look for alternatives for NAME, an IDENTIFIER_NODE for which name
5521 lookup failed within the explicitly provided SCOPE. Suggest the
5522 the best meaningful candidates (if any) as a fix-it hint.
5523 Return true iff a suggestion was provided. */
5525 bool
5526 suggest_alternative_in_explicit_scope (location_t location, tree name,
5527 tree scope)
5529 /* Something went very wrong; don't suggest anything. */
5530 if (name == error_mark_node)
5531 return false;
5533 /* Resolve any namespace aliases. */
5534 scope = ORIGINAL_NAMESPACE (scope);
5536 if (maybe_suggest_missing_header (location, name, scope))
5537 return true;
5539 cp_binding_level *level = NAMESPACE_LEVEL (scope);
5541 best_match <tree, const char *> bm (name);
5542 consider_binding_level (name, bm, level, false, FUZZY_LOOKUP_NAME);
5544 /* See if we have a good suggesion for the user. */
5545 const char *fuzzy_name = bm.get_best_meaningful_candidate ();
5546 if (fuzzy_name)
5548 gcc_rich_location richloc (location);
5549 richloc.add_fixit_replace (fuzzy_name);
5550 inform (&richloc, "suggested alternative: %qs",
5551 fuzzy_name);
5552 return true;
5555 return false;
5558 /* Look up NAME (an IDENTIFIER_NODE) in SCOPE (either a NAMESPACE_DECL
5559 or a class TYPE).
5561 If PREFER_TYPE is > 0, we only return TYPE_DECLs or namespaces.
5562 If PREFER_TYPE is > 1, we only return TYPE_DECLs.
5564 Returns a DECL (or OVERLOAD, or BASELINK) representing the
5565 declaration found. If no suitable declaration can be found,
5566 ERROR_MARK_NODE is returned. If COMPLAIN is true and SCOPE is
5567 neither a class-type nor a namespace a diagnostic is issued. */
5569 tree
5570 lookup_qualified_name (tree scope, tree name, int prefer_type, bool complain,
5571 bool find_hidden)
5573 tree t = NULL_TREE;
5575 if (TREE_CODE (scope) == NAMESPACE_DECL)
5577 int flags = lookup_flags (prefer_type, /*namespaces_only*/false);
5578 if (find_hidden)
5579 flags |= LOOKUP_HIDDEN;
5580 name_lookup lookup (name, flags);
5582 if (qualified_namespace_lookup (scope, &lookup))
5583 t = lookup.value;
5585 else if (cxx_dialect != cxx98 && TREE_CODE (scope) == ENUMERAL_TYPE)
5586 t = lookup_enumerator (scope, name);
5587 else if (is_class_type (scope, complain))
5588 t = lookup_member (scope, name, 2, prefer_type, tf_warning_or_error);
5590 if (!t)
5591 return error_mark_node;
5592 return t;
5595 /* [namespace.qual]
5596 Accepts the NAME to lookup and its qualifying SCOPE.
5597 Returns the name/type pair found into the cxx_binding *RESULT,
5598 or false on error. */
5600 static bool
5601 qualified_namespace_lookup (tree scope, name_lookup *lookup)
5603 timevar_start (TV_NAME_LOOKUP);
5604 query_oracle (lookup->name);
5605 bool found = lookup->search_qualified (ORIGINAL_NAMESPACE (scope));
5606 timevar_stop (TV_NAME_LOOKUP);
5607 return found;
5610 /* Helper function for lookup_name_fuzzy.
5611 Traverse binding level LVL, looking for good name matches for NAME
5612 (and BM). */
5613 static void
5614 consider_binding_level (tree name, best_match <tree, const char *> &bm,
5615 cp_binding_level *lvl, bool look_within_fields,
5616 enum lookup_name_fuzzy_kind kind)
5618 if (look_within_fields)
5619 if (lvl->this_entity && TREE_CODE (lvl->this_entity) == RECORD_TYPE)
5621 tree type = lvl->this_entity;
5622 bool want_type_p = (kind == FUZZY_LOOKUP_TYPENAME);
5623 tree best_matching_field
5624 = lookup_member_fuzzy (type, name, want_type_p);
5625 if (best_matching_field)
5626 bm.consider (IDENTIFIER_POINTER (best_matching_field));
5629 /* Only suggest names reserved for the implementation if NAME begins
5630 with an underscore. */
5631 bool consider_implementation_names = (IDENTIFIER_POINTER (name)[0] == '_');
5633 for (tree t = lvl->names; t; t = TREE_CHAIN (t))
5635 tree d = t;
5637 /* OVERLOADs or decls from using declaration are wrapped into
5638 TREE_LIST. */
5639 if (TREE_CODE (d) == TREE_LIST)
5640 d = OVL_FIRST (TREE_VALUE (d));
5642 /* Don't use bindings from implicitly declared functions,
5643 as they were likely misspellings themselves. */
5644 if (TREE_TYPE (d) == error_mark_node)
5645 continue;
5647 /* Skip anticipated decls of builtin functions. */
5648 if (TREE_CODE (d) == FUNCTION_DECL
5649 && DECL_BUILT_IN (d)
5650 && DECL_ANTICIPATED (d))
5651 continue;
5653 tree suggestion = DECL_NAME (d);
5654 if (!suggestion)
5655 continue;
5657 const char *suggestion_str = IDENTIFIER_POINTER (suggestion);
5659 /* Ignore internal names with spaces in them. */
5660 if (strchr (suggestion_str, ' '))
5661 continue;
5663 /* Don't suggest names that are reserved for use by the
5664 implementation, unless NAME began with an underscore. */
5665 if (name_reserved_for_implementation_p (suggestion_str)
5666 && !consider_implementation_names)
5667 continue;
5669 bm.consider (suggestion_str);
5673 /* Subclass of deferred_diagnostic. Notify the user that the
5674 given macro was used before it was defined.
5675 This can be done in the C++ frontend since tokenization happens
5676 upfront. */
5678 class macro_use_before_def : public deferred_diagnostic
5680 public:
5681 /* Ctor. LOC is the location of the usage. MACRO is the
5682 macro that was used. */
5683 macro_use_before_def (location_t loc, cpp_hashnode *macro)
5684 : deferred_diagnostic (loc), m_macro (macro)
5686 gcc_assert (macro);
5689 ~macro_use_before_def ()
5691 if (is_suppressed_p ())
5692 return;
5694 source_location def_loc = cpp_macro_definition_location (m_macro);
5695 if (def_loc != UNKNOWN_LOCATION)
5697 inform (get_location (), "the macro %qs had not yet been defined",
5698 (const char *)m_macro->ident.str);
5699 inform (def_loc, "it was later defined here");
5703 private:
5704 cpp_hashnode *m_macro;
5707 /* Determine if it can ever make sense to offer RID as a suggestion for
5708 a misspelling.
5710 Subroutine of lookup_name_fuzzy. */
5712 static bool
5713 suggest_rid_p (enum rid rid)
5715 switch (rid)
5717 /* Support suggesting function-like keywords. */
5718 case RID_STATIC_ASSERT:
5719 return true;
5721 default:
5722 /* Support suggesting the various decl-specifier words, to handle
5723 e.g. "singed" vs "signed" typos. */
5724 if (cp_keyword_starts_decl_specifier_p (rid))
5725 return true;
5727 /* Otherwise, don't offer it. This avoids suggesting e.g. "if"
5728 and "do" for short misspellings, which are likely to lead to
5729 nonsensical results. */
5730 return false;
5734 /* Search for near-matches for NAME within the current bindings, and within
5735 macro names, returning the best match as a const char *, or NULL if
5736 no reasonable match is found.
5738 Use LOC for any deferred diagnostics. */
5740 name_hint
5741 lookup_name_fuzzy (tree name, enum lookup_name_fuzzy_kind kind, location_t loc)
5743 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
5745 /* First, try some well-known names in the C++ standard library, in case
5746 the user forgot a #include. */
5747 const char *header_hint
5748 = get_cp_stdlib_header_for_name (IDENTIFIER_POINTER (name));
5749 if (header_hint)
5750 return name_hint (NULL,
5751 new suggest_missing_header (loc,
5752 IDENTIFIER_POINTER (name),
5753 header_hint));
5755 best_match <tree, const char *> bm (name);
5757 cp_binding_level *lvl;
5758 for (lvl = scope_chain->class_bindings; lvl; lvl = lvl->level_chain)
5759 consider_binding_level (name, bm, lvl, true, kind);
5761 for (lvl = current_binding_level; lvl; lvl = lvl->level_chain)
5762 consider_binding_level (name, bm, lvl, false, kind);
5764 /* Consider macros: if the user misspelled a macro name e.g. "SOME_MACRO"
5766 x = SOME_OTHER_MACRO (y);
5767 then "SOME_OTHER_MACRO" will survive to the frontend and show up
5768 as a misspelled identifier.
5770 Use the best distance so far so that a candidate is only set if
5771 a macro is better than anything so far. This allows early rejection
5772 (without calculating the edit distance) of macro names that must have
5773 distance >= bm.get_best_distance (), and means that we only get a
5774 non-NULL result for best_macro_match if it's better than any of
5775 the identifiers already checked. */
5776 best_macro_match bmm (name, bm.get_best_distance (), parse_in);
5777 cpp_hashnode *best_macro = bmm.get_best_meaningful_candidate ();
5778 /* If a macro is the closest so far to NAME, consider it. */
5779 if (best_macro)
5780 bm.consider ((const char *)best_macro->ident.str);
5781 else if (bmm.get_best_distance () == 0)
5783 /* If we have an exact match for a macro name, then the
5784 macro has been used before it was defined. */
5785 cpp_hashnode *macro = bmm.blithely_get_best_candidate ();
5786 if (macro && (macro->flags & NODE_BUILTIN) == 0)
5787 return name_hint (NULL,
5788 new macro_use_before_def (loc, macro));
5791 /* Try the "starts_decl_specifier_p" keywords to detect
5792 "singed" vs "signed" typos. */
5793 for (unsigned i = 0; i < num_c_common_reswords; i++)
5795 const c_common_resword *resword = &c_common_reswords[i];
5797 if (!suggest_rid_p (resword->rid))
5798 continue;
5800 tree resword_identifier = ridpointers [resword->rid];
5801 if (!resword_identifier)
5802 continue;
5803 gcc_assert (TREE_CODE (resword_identifier) == IDENTIFIER_NODE);
5805 /* Only consider reserved words that survived the
5806 filtering in init_reswords (e.g. for -std). */
5807 if (!IDENTIFIER_KEYWORD_P (resword_identifier))
5808 continue;
5810 bm.consider (IDENTIFIER_POINTER (resword_identifier));
5813 return name_hint (bm.get_best_meaningful_candidate (), NULL);
5816 /* Subroutine of outer_binding.
5818 Returns TRUE if BINDING is a binding to a template parameter of
5819 SCOPE. In that case SCOPE is the scope of a primary template
5820 parameter -- in the sense of G++, i.e, a template that has its own
5821 template header.
5823 Returns FALSE otherwise. */
5825 static bool
5826 binding_to_template_parms_of_scope_p (cxx_binding *binding,
5827 cp_binding_level *scope)
5829 tree binding_value, tmpl, tinfo;
5830 int level;
5832 if (!binding || !scope || !scope->this_entity)
5833 return false;
5835 binding_value = binding->value ? binding->value : binding->type;
5836 tinfo = get_template_info (scope->this_entity);
5838 /* BINDING_VALUE must be a template parm. */
5839 if (binding_value == NULL_TREE
5840 || (!DECL_P (binding_value)
5841 || !DECL_TEMPLATE_PARM_P (binding_value)))
5842 return false;
5844 /* The level of BINDING_VALUE. */
5845 level =
5846 template_type_parameter_p (binding_value)
5847 ? TEMPLATE_PARM_LEVEL (TEMPLATE_TYPE_PARM_INDEX
5848 (TREE_TYPE (binding_value)))
5849 : TEMPLATE_PARM_LEVEL (DECL_INITIAL (binding_value));
5851 /* The template of the current scope, iff said scope is a primary
5852 template. */
5853 tmpl = (tinfo
5854 && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
5855 ? TI_TEMPLATE (tinfo)
5856 : NULL_TREE);
5858 /* If the level of the parm BINDING_VALUE equals the depth of TMPL,
5859 then BINDING_VALUE is a parameter of TMPL. */
5860 return (tmpl && level == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)));
5863 /* Return the innermost non-namespace binding for NAME from a scope
5864 containing BINDING, or, if BINDING is NULL, the current scope.
5865 Please note that for a given template, the template parameters are
5866 considered to be in the scope containing the current scope.
5867 If CLASS_P is false, then class bindings are ignored. */
5869 cxx_binding *
5870 outer_binding (tree name,
5871 cxx_binding *binding,
5872 bool class_p)
5874 cxx_binding *outer;
5875 cp_binding_level *scope;
5876 cp_binding_level *outer_scope;
5878 if (binding)
5880 scope = binding->scope->level_chain;
5881 outer = binding->previous;
5883 else
5885 scope = current_binding_level;
5886 outer = IDENTIFIER_BINDING (name);
5888 outer_scope = outer ? outer->scope : NULL;
5890 /* Because we create class bindings lazily, we might be missing a
5891 class binding for NAME. If there are any class binding levels
5892 between the LAST_BINDING_LEVEL and the scope in which OUTER was
5893 declared, we must lookup NAME in those class scopes. */
5894 if (class_p)
5895 while (scope && scope != outer_scope && scope->kind != sk_namespace)
5897 if (scope->kind == sk_class)
5899 cxx_binding *class_binding;
5901 class_binding = get_class_binding (name, scope);
5902 if (class_binding)
5904 /* Thread this new class-scope binding onto the
5905 IDENTIFIER_BINDING list so that future lookups
5906 find it quickly. */
5907 class_binding->previous = outer;
5908 if (binding)
5909 binding->previous = class_binding;
5910 else
5911 IDENTIFIER_BINDING (name) = class_binding;
5912 return class_binding;
5915 /* If we are in a member template, the template parms of the member
5916 template are considered to be inside the scope of the containing
5917 class, but within G++ the class bindings are all pushed between the
5918 template parms and the function body. So if the outer binding is
5919 a template parm for the current scope, return it now rather than
5920 look for a class binding. */
5921 if (outer_scope && outer_scope->kind == sk_template_parms
5922 && binding_to_template_parms_of_scope_p (outer, scope))
5923 return outer;
5925 scope = scope->level_chain;
5928 return outer;
5931 /* Return the innermost block-scope or class-scope value binding for
5932 NAME, or NULL_TREE if there is no such binding. */
5934 tree
5935 innermost_non_namespace_value (tree name)
5937 cxx_binding *binding;
5938 binding = outer_binding (name, /*binding=*/NULL, /*class_p=*/true);
5939 return binding ? binding->value : NULL_TREE;
5942 /* Look up NAME in the current binding level and its superiors in the
5943 namespace of variables, functions and typedefs. Return a ..._DECL
5944 node of some kind representing its definition if there is only one
5945 such declaration, or return a TREE_LIST with all the overloaded
5946 definitions if there are many, or return 0 if it is undefined.
5947 Hidden name, either friend declaration or built-in function, are
5948 not ignored.
5950 If PREFER_TYPE is > 0, we prefer TYPE_DECLs or namespaces.
5951 If PREFER_TYPE is > 1, we reject non-type decls (e.g. namespaces).
5952 Otherwise we prefer non-TYPE_DECLs.
5954 If NONCLASS is nonzero, bindings in class scopes are ignored. If
5955 BLOCK_P is false, bindings in block scopes are ignored. */
5957 static tree
5958 lookup_name_real_1 (tree name, int prefer_type, int nonclass, bool block_p,
5959 int namespaces_only, int flags)
5961 cxx_binding *iter;
5962 tree val = NULL_TREE;
5964 query_oracle (name);
5966 /* Conversion operators are handled specially because ordinary
5967 unqualified name lookup will not find template conversion
5968 operators. */
5969 if (IDENTIFIER_CONV_OP_P (name))
5971 cp_binding_level *level;
5973 for (level = current_binding_level;
5974 level && level->kind != sk_namespace;
5975 level = level->level_chain)
5977 tree class_type;
5978 tree operators;
5980 /* A conversion operator can only be declared in a class
5981 scope. */
5982 if (level->kind != sk_class)
5983 continue;
5985 /* Lookup the conversion operator in the class. */
5986 class_type = level->this_entity;
5987 operators = lookup_fnfields (class_type, name, /*protect=*/0);
5988 if (operators)
5989 return operators;
5992 return NULL_TREE;
5995 flags |= lookup_flags (prefer_type, namespaces_only);
5997 /* First, look in non-namespace scopes. */
5999 if (current_class_type == NULL_TREE)
6000 nonclass = 1;
6002 if (block_p || !nonclass)
6003 for (iter = outer_binding (name, NULL, !nonclass);
6004 iter;
6005 iter = outer_binding (name, iter, !nonclass))
6007 tree binding;
6009 /* Skip entities we don't want. */
6010 if (LOCAL_BINDING_P (iter) ? !block_p : nonclass)
6011 continue;
6013 /* If this is the kind of thing we're looking for, we're done. */
6014 if (qualify_lookup (iter->value, flags))
6015 binding = iter->value;
6016 else if ((flags & LOOKUP_PREFER_TYPES)
6017 && qualify_lookup (iter->type, flags))
6018 binding = iter->type;
6019 else
6020 binding = NULL_TREE;
6022 if (binding)
6024 if (TREE_CODE (binding) == TYPE_DECL && DECL_HIDDEN_P (binding))
6026 /* A non namespace-scope binding can only be hidden in the
6027 presence of a local class, due to friend declarations.
6029 In particular, consider:
6031 struct C;
6032 void f() {
6033 struct A {
6034 friend struct B;
6035 friend struct C;
6036 void g() {
6037 B* b; // error: B is hidden
6038 C* c; // OK, finds ::C
6041 B *b; // error: B is hidden
6042 C *c; // OK, finds ::C
6043 struct B {};
6044 B *bb; // OK
6047 The standard says that "B" is a local class in "f"
6048 (but not nested within "A") -- but that name lookup
6049 for "B" does not find this declaration until it is
6050 declared directly with "f".
6052 In particular:
6054 [class.friend]
6056 If a friend declaration appears in a local class and
6057 the name specified is an unqualified name, a prior
6058 declaration is looked up without considering scopes
6059 that are outside the innermost enclosing non-class
6060 scope. For a friend function declaration, if there is
6061 no prior declaration, the program is ill-formed. For a
6062 friend class declaration, if there is no prior
6063 declaration, the class that is specified belongs to the
6064 innermost enclosing non-class scope, but if it is
6065 subsequently referenced, its name is not found by name
6066 lookup until a matching declaration is provided in the
6067 innermost enclosing nonclass scope.
6069 So just keep looking for a non-hidden binding.
6071 gcc_assert (TREE_CODE (binding) == TYPE_DECL);
6072 continue;
6074 val = binding;
6075 break;
6079 /* Now lookup in namespace scopes. */
6080 if (!val)
6082 name_lookup lookup (name, flags);
6083 if (lookup.search_unqualified
6084 (current_decl_namespace (), current_binding_level))
6085 val = lookup.value;
6088 /* If we have a single function from a using decl, pull it out. */
6089 if (val && TREE_CODE (val) == OVERLOAD && !really_overloaded_fn (val))
6090 val = OVL_FUNCTION (val);
6092 return val;
6095 /* Wrapper for lookup_name_real_1. */
6097 tree
6098 lookup_name_real (tree name, int prefer_type, int nonclass, bool block_p,
6099 int namespaces_only, int flags)
6101 tree ret;
6102 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6103 ret = lookup_name_real_1 (name, prefer_type, nonclass, block_p,
6104 namespaces_only, flags);
6105 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6106 return ret;
6109 tree
6110 lookup_name_nonclass (tree name)
6112 return lookup_name_real (name, 0, 1, /*block_p=*/true, 0, 0);
6115 tree
6116 lookup_name (tree name)
6118 return lookup_name_real (name, 0, 0, /*block_p=*/true, 0, 0);
6121 tree
6122 lookup_name_prefer_type (tree name, int prefer_type)
6124 return lookup_name_real (name, prefer_type, 0, /*block_p=*/true, 0, 0);
6127 /* Look up NAME for type used in elaborated name specifier in
6128 the scopes given by SCOPE. SCOPE can be either TS_CURRENT or
6129 TS_WITHIN_ENCLOSING_NON_CLASS. Although not implied by the
6130 name, more scopes are checked if cleanup or template parameter
6131 scope is encountered.
6133 Unlike lookup_name_real, we make sure that NAME is actually
6134 declared in the desired scope, not from inheritance, nor using
6135 directive. For using declaration, there is DR138 still waiting
6136 to be resolved. Hidden name coming from an earlier friend
6137 declaration is also returned.
6139 A TYPE_DECL best matching the NAME is returned. Catching error
6140 and issuing diagnostics are caller's responsibility. */
6142 static tree
6143 lookup_type_scope_1 (tree name, tag_scope scope)
6145 cxx_binding *iter = NULL;
6146 tree val = NULL_TREE;
6147 cp_binding_level *level = NULL;
6149 /* Look in non-namespace scope first. */
6150 if (current_binding_level->kind != sk_namespace)
6151 iter = outer_binding (name, NULL, /*class_p=*/ true);
6152 for (; iter; iter = outer_binding (name, iter, /*class_p=*/ true))
6154 /* Check if this is the kind of thing we're looking for.
6155 If SCOPE is TS_CURRENT, also make sure it doesn't come from
6156 base class. For ITER->VALUE, we can simply use
6157 INHERITED_VALUE_BINDING_P. For ITER->TYPE, we have to use
6158 our own check.
6160 We check ITER->TYPE before ITER->VALUE in order to handle
6161 typedef struct C {} C;
6162 correctly. */
6164 if (qualify_lookup (iter->type, LOOKUP_PREFER_TYPES)
6165 && (scope != ts_current
6166 || LOCAL_BINDING_P (iter)
6167 || DECL_CONTEXT (iter->type) == iter->scope->this_entity))
6168 val = iter->type;
6169 else if ((scope != ts_current
6170 || !INHERITED_VALUE_BINDING_P (iter))
6171 && qualify_lookup (iter->value, LOOKUP_PREFER_TYPES))
6172 val = iter->value;
6174 if (val)
6175 break;
6178 /* Look in namespace scope. */
6179 if (val)
6180 level = iter->scope;
6181 else
6183 tree ns = current_decl_namespace ();
6185 if (tree *slot = find_namespace_slot (ns, name))
6187 /* If this is the kind of thing we're looking for, we're done. */
6188 if (tree type = MAYBE_STAT_TYPE (*slot))
6189 if (qualify_lookup (type, LOOKUP_PREFER_TYPES))
6190 val = type;
6191 if (!val)
6193 if (tree decl = MAYBE_STAT_DECL (*slot))
6194 if (qualify_lookup (decl, LOOKUP_PREFER_TYPES))
6195 val = decl;
6197 level = NAMESPACE_LEVEL (ns);
6201 /* Type found, check if it is in the allowed scopes, ignoring cleanup
6202 and template parameter scopes. */
6203 if (val)
6205 cp_binding_level *b = current_binding_level;
6206 while (b)
6208 if (level == b)
6209 return val;
6211 if (b->kind == sk_cleanup || b->kind == sk_template_parms
6212 || b->kind == sk_function_parms)
6213 b = b->level_chain;
6214 else if (b->kind == sk_class
6215 && scope == ts_within_enclosing_non_class)
6216 b = b->level_chain;
6217 else
6218 break;
6222 return NULL_TREE;
6225 /* Wrapper for lookup_type_scope_1. */
6227 tree
6228 lookup_type_scope (tree name, tag_scope scope)
6230 tree ret;
6231 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6232 ret = lookup_type_scope_1 (name, scope);
6233 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6234 return ret;
6237 /* Returns true iff DECL is a block-scope extern declaration of a function
6238 or variable. */
6240 bool
6241 is_local_extern (tree decl)
6243 cxx_binding *binding;
6245 /* For functions, this is easy. */
6246 if (TREE_CODE (decl) == FUNCTION_DECL)
6247 return DECL_LOCAL_FUNCTION_P (decl);
6249 if (!VAR_P (decl))
6250 return false;
6251 if (!current_function_decl)
6252 return false;
6254 /* For variables, this is not easy. We need to look at the binding stack
6255 for the identifier to see whether the decl we have is a local. */
6256 for (binding = IDENTIFIER_BINDING (DECL_NAME (decl));
6257 binding && binding->scope->kind != sk_namespace;
6258 binding = binding->previous)
6259 if (binding->value == decl)
6260 return LOCAL_BINDING_P (binding);
6262 return false;
6265 /* The type TYPE is being declared. If it is a class template, or a
6266 specialization of a class template, do any processing required and
6267 perform error-checking. If IS_FRIEND is nonzero, this TYPE is
6268 being declared a friend. B is the binding level at which this TYPE
6269 should be bound.
6271 Returns the TYPE_DECL for TYPE, which may have been altered by this
6272 processing. */
6274 static tree
6275 maybe_process_template_type_declaration (tree type, int is_friend,
6276 cp_binding_level *b)
6278 tree decl = TYPE_NAME (type);
6280 if (processing_template_parmlist)
6281 /* You can't declare a new template type in a template parameter
6282 list. But, you can declare a non-template type:
6284 template <class A*> struct S;
6286 is a forward-declaration of `A'. */
6288 else if (b->kind == sk_namespace
6289 && current_binding_level->kind != sk_namespace)
6290 /* If this new type is being injected into a containing scope,
6291 then it's not a template type. */
6293 else
6295 gcc_assert (MAYBE_CLASS_TYPE_P (type)
6296 || TREE_CODE (type) == ENUMERAL_TYPE);
6298 if (processing_template_decl)
6300 /* This may change after the call to
6301 push_template_decl_real, but we want the original value. */
6302 tree name = DECL_NAME (decl);
6304 decl = push_template_decl_real (decl, is_friend);
6305 if (decl == error_mark_node)
6306 return error_mark_node;
6308 /* If the current binding level is the binding level for the
6309 template parameters (see the comment in
6310 begin_template_parm_list) and the enclosing level is a class
6311 scope, and we're not looking at a friend, push the
6312 declaration of the member class into the class scope. In the
6313 friend case, push_template_decl will already have put the
6314 friend into global scope, if appropriate. */
6315 if (TREE_CODE (type) != ENUMERAL_TYPE
6316 && !is_friend && b->kind == sk_template_parms
6317 && b->level_chain->kind == sk_class)
6319 finish_member_declaration (CLASSTYPE_TI_TEMPLATE (type));
6321 if (!COMPLETE_TYPE_P (current_class_type))
6323 maybe_add_class_template_decl_list (current_class_type,
6324 type, /*friend_p=*/0);
6325 /* Put this UTD in the table of UTDs for the class. */
6326 if (CLASSTYPE_NESTED_UTDS (current_class_type) == NULL)
6327 CLASSTYPE_NESTED_UTDS (current_class_type) =
6328 binding_table_new (SCOPE_DEFAULT_HT_SIZE);
6330 binding_table_insert
6331 (CLASSTYPE_NESTED_UTDS (current_class_type), name, type);
6337 return decl;
6340 /* Push a tag name NAME for struct/class/union/enum type TYPE. In case
6341 that the NAME is a class template, the tag is processed but not pushed.
6343 The pushed scope depend on the SCOPE parameter:
6344 - When SCOPE is TS_CURRENT, put it into the inner-most non-sk_cleanup
6345 scope.
6346 - When SCOPE is TS_GLOBAL, put it in the inner-most non-class and
6347 non-template-parameter scope. This case is needed for forward
6348 declarations.
6349 - When SCOPE is TS_WITHIN_ENCLOSING_NON_CLASS, this is similar to
6350 TS_GLOBAL case except that names within template-parameter scopes
6351 are not pushed at all.
6353 Returns TYPE upon success and ERROR_MARK_NODE otherwise. */
6355 static tree
6356 do_pushtag (tree name, tree type, tag_scope scope)
6358 tree decl;
6360 cp_binding_level *b = current_binding_level;
6361 while (/* Cleanup scopes are not scopes from the point of view of
6362 the language. */
6363 b->kind == sk_cleanup
6364 /* Neither are function parameter scopes. */
6365 || b->kind == sk_function_parms
6366 /* Neither are the scopes used to hold template parameters
6367 for an explicit specialization. For an ordinary template
6368 declaration, these scopes are not scopes from the point of
6369 view of the language. */
6370 || (b->kind == sk_template_parms
6371 && (b->explicit_spec_p || scope == ts_global))
6372 || (b->kind == sk_class
6373 && (scope != ts_current
6374 /* We may be defining a new type in the initializer
6375 of a static member variable. We allow this when
6376 not pedantic, and it is particularly useful for
6377 type punning via an anonymous union. */
6378 || COMPLETE_TYPE_P (b->this_entity))))
6379 b = b->level_chain;
6381 gcc_assert (identifier_p (name));
6383 /* Do C++ gratuitous typedefing. */
6384 if (identifier_type_value_1 (name) != type)
6386 tree tdef;
6387 int in_class = 0;
6388 tree context = TYPE_CONTEXT (type);
6390 if (! context)
6392 tree cs = current_scope ();
6394 if (scope == ts_current
6395 || (cs && TREE_CODE (cs) == FUNCTION_DECL))
6396 context = cs;
6397 else if (cs && TYPE_P (cs))
6398 /* When declaring a friend class of a local class, we want
6399 to inject the newly named class into the scope
6400 containing the local class, not the namespace
6401 scope. */
6402 context = decl_function_context (get_type_decl (cs));
6404 if (!context)
6405 context = current_namespace;
6407 if (b->kind == sk_class
6408 || (b->kind == sk_template_parms
6409 && b->level_chain->kind == sk_class))
6410 in_class = 1;
6412 tdef = create_implicit_typedef (name, type);
6413 DECL_CONTEXT (tdef) = FROB_CONTEXT (context);
6414 if (scope == ts_within_enclosing_non_class)
6416 /* This is a friend. Make this TYPE_DECL node hidden from
6417 ordinary name lookup. Its corresponding TEMPLATE_DECL
6418 will be marked in push_template_decl_real. */
6419 retrofit_lang_decl (tdef);
6420 DECL_ANTICIPATED (tdef) = 1;
6421 DECL_FRIEND_P (tdef) = 1;
6424 decl = maybe_process_template_type_declaration
6425 (type, scope == ts_within_enclosing_non_class, b);
6426 if (decl == error_mark_node)
6427 return decl;
6429 if (b->kind == sk_class)
6431 if (!TYPE_BEING_DEFINED (current_class_type))
6432 return error_mark_node;
6434 if (!PROCESSING_REAL_TEMPLATE_DECL_P ())
6435 /* Put this TYPE_DECL on the TYPE_FIELDS list for the
6436 class. But if it's a member template class, we want
6437 the TEMPLATE_DECL, not the TYPE_DECL, so this is done
6438 later. */
6439 finish_member_declaration (decl);
6440 else
6441 pushdecl_class_level (decl);
6443 else if (b->kind != sk_template_parms)
6445 decl = do_pushdecl_with_scope (decl, b, /*is_friend=*/false);
6446 if (decl == error_mark_node)
6447 return decl;
6449 if (DECL_CONTEXT (decl) == std_node
6450 && init_list_identifier == DECL_NAME (TYPE_NAME (type))
6451 && !CLASSTYPE_TEMPLATE_INFO (type))
6453 error ("declaration of std::initializer_list does not match "
6454 "#include <initializer_list>, isn't a template");
6455 return error_mark_node;
6459 if (! in_class)
6460 set_identifier_type_value_with_scope (name, tdef, b);
6462 TYPE_CONTEXT (type) = DECL_CONTEXT (decl);
6464 /* If this is a local class, keep track of it. We need this
6465 information for name-mangling, and so that it is possible to
6466 find all function definitions in a translation unit in a
6467 convenient way. (It's otherwise tricky to find a member
6468 function definition it's only pointed to from within a local
6469 class.) */
6470 if (TYPE_FUNCTION_SCOPE_P (type))
6472 if (processing_template_decl)
6474 /* Push a DECL_EXPR so we call pushtag at the right time in
6475 template instantiation rather than in some nested context. */
6476 add_decl_expr (decl);
6478 /* Lambdas use LAMBDA_EXPR_DISCRIMINATOR instead. */
6479 else if (!LAMBDA_TYPE_P (type))
6480 vec_safe_push (local_classes, type);
6484 if (b->kind == sk_class
6485 && !COMPLETE_TYPE_P (current_class_type))
6487 maybe_add_class_template_decl_list (current_class_type,
6488 type, /*friend_p=*/0);
6490 if (CLASSTYPE_NESTED_UTDS (current_class_type) == NULL)
6491 CLASSTYPE_NESTED_UTDS (current_class_type)
6492 = binding_table_new (SCOPE_DEFAULT_HT_SIZE);
6494 binding_table_insert
6495 (CLASSTYPE_NESTED_UTDS (current_class_type), name, type);
6498 decl = TYPE_NAME (type);
6499 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
6501 /* Set type visibility now if this is a forward declaration. */
6502 TREE_PUBLIC (decl) = 1;
6503 determine_visibility (decl);
6505 return type;
6508 /* Wrapper for do_pushtag. */
6510 tree
6511 pushtag (tree name, tree type, tag_scope scope)
6513 tree ret;
6514 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6515 ret = do_pushtag (name, type, scope);
6516 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6517 return ret;
6521 /* Subroutines for reverting temporarily to top-level for instantiation
6522 of templates and such. We actually need to clear out the class- and
6523 local-value slots of all identifiers, so that only the global values
6524 are at all visible. Simply setting current_binding_level to the global
6525 scope isn't enough, because more binding levels may be pushed. */
6526 struct saved_scope *scope_chain;
6528 /* Return true if ID has not already been marked. */
6530 static inline bool
6531 store_binding_p (tree id)
6533 if (!id || !IDENTIFIER_BINDING (id))
6534 return false;
6536 if (IDENTIFIER_MARKED (id))
6537 return false;
6539 return true;
6542 /* Add an appropriate binding to *OLD_BINDINGS which needs to already
6543 have enough space reserved. */
6545 static void
6546 store_binding (tree id, vec<cxx_saved_binding, va_gc> **old_bindings)
6548 cxx_saved_binding saved;
6550 gcc_checking_assert (store_binding_p (id));
6552 IDENTIFIER_MARKED (id) = 1;
6554 saved.identifier = id;
6555 saved.binding = IDENTIFIER_BINDING (id);
6556 saved.real_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
6557 (*old_bindings)->quick_push (saved);
6558 IDENTIFIER_BINDING (id) = NULL;
6561 static void
6562 store_bindings (tree names, vec<cxx_saved_binding, va_gc> **old_bindings)
6564 static vec<tree> bindings_need_stored;
6565 tree t, id;
6566 size_t i;
6568 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6569 for (t = names; t; t = TREE_CHAIN (t))
6571 if (TREE_CODE (t) == TREE_LIST)
6572 id = TREE_PURPOSE (t);
6573 else
6574 id = DECL_NAME (t);
6576 if (store_binding_p (id))
6577 bindings_need_stored.safe_push (id);
6579 if (!bindings_need_stored.is_empty ())
6581 vec_safe_reserve_exact (*old_bindings, bindings_need_stored.length ());
6582 for (i = 0; bindings_need_stored.iterate (i, &id); ++i)
6584 /* We can apparently have duplicates in NAMES. */
6585 if (store_binding_p (id))
6586 store_binding (id, old_bindings);
6588 bindings_need_stored.truncate (0);
6590 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6593 /* Like store_bindings, but NAMES is a vector of cp_class_binding
6594 objects, rather than a TREE_LIST. */
6596 static void
6597 store_class_bindings (vec<cp_class_binding, va_gc> *names,
6598 vec<cxx_saved_binding, va_gc> **old_bindings)
6600 static vec<tree> bindings_need_stored;
6601 size_t i;
6602 cp_class_binding *cb;
6604 for (i = 0; vec_safe_iterate (names, i, &cb); ++i)
6605 if (store_binding_p (cb->identifier))
6606 bindings_need_stored.safe_push (cb->identifier);
6607 if (!bindings_need_stored.is_empty ())
6609 tree id;
6610 vec_safe_reserve_exact (*old_bindings, bindings_need_stored.length ());
6611 for (i = 0; bindings_need_stored.iterate (i, &id); ++i)
6612 store_binding (id, old_bindings);
6613 bindings_need_stored.truncate (0);
6617 /* A chain of saved_scope structures awaiting reuse. */
6619 static GTY((deletable)) struct saved_scope *free_saved_scope;
6621 static void
6622 do_push_to_top_level (void)
6624 struct saved_scope *s;
6625 cp_binding_level *b;
6626 cxx_saved_binding *sb;
6627 size_t i;
6628 bool need_pop;
6630 /* Reuse or create a new structure for this saved scope. */
6631 if (free_saved_scope != NULL)
6633 s = free_saved_scope;
6634 free_saved_scope = s->prev;
6636 vec<cxx_saved_binding, va_gc> *old_bindings = s->old_bindings;
6637 memset (s, 0, sizeof (*s));
6638 /* Also reuse the structure's old_bindings vector. */
6639 vec_safe_truncate (old_bindings, 0);
6640 s->old_bindings = old_bindings;
6642 else
6643 s = ggc_cleared_alloc<saved_scope> ();
6645 b = scope_chain ? current_binding_level : 0;
6647 /* If we're in the middle of some function, save our state. */
6648 if (cfun)
6650 need_pop = true;
6651 push_function_context ();
6653 else
6654 need_pop = false;
6656 if (scope_chain && previous_class_level)
6657 store_class_bindings (previous_class_level->class_shadowed,
6658 &s->old_bindings);
6660 /* Have to include the global scope, because class-scope decls
6661 aren't listed anywhere useful. */
6662 for (; b; b = b->level_chain)
6664 tree t;
6666 /* Template IDs are inserted into the global level. If they were
6667 inserted into namespace level, finish_file wouldn't find them
6668 when doing pending instantiations. Therefore, don't stop at
6669 namespace level, but continue until :: . */
6670 if (global_scope_p (b))
6671 break;
6673 store_bindings (b->names, &s->old_bindings);
6674 /* We also need to check class_shadowed to save class-level type
6675 bindings, since pushclass doesn't fill in b->names. */
6676 if (b->kind == sk_class)
6677 store_class_bindings (b->class_shadowed, &s->old_bindings);
6679 /* Unwind type-value slots back to top level. */
6680 for (t = b->type_shadowed; t; t = TREE_CHAIN (t))
6681 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (t), TREE_VALUE (t));
6684 FOR_EACH_VEC_SAFE_ELT (s->old_bindings, i, sb)
6685 IDENTIFIER_MARKED (sb->identifier) = 0;
6687 s->prev = scope_chain;
6688 s->bindings = b;
6689 s->need_pop_function_context = need_pop;
6690 s->function_decl = current_function_decl;
6691 s->unevaluated_operand = cp_unevaluated_operand;
6692 s->inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
6693 s->x_stmt_tree.stmts_are_full_exprs_p = true;
6695 scope_chain = s;
6696 current_function_decl = NULL_TREE;
6697 vec_alloc (current_lang_base, 10);
6698 current_lang_name = lang_name_cplusplus;
6699 current_namespace = global_namespace;
6700 push_class_stack ();
6701 cp_unevaluated_operand = 0;
6702 c_inhibit_evaluation_warnings = 0;
6705 static void
6706 do_pop_from_top_level (void)
6708 struct saved_scope *s = scope_chain;
6709 cxx_saved_binding *saved;
6710 size_t i;
6712 /* Clear out class-level bindings cache. */
6713 if (previous_class_level)
6714 invalidate_class_lookup_cache ();
6715 pop_class_stack ();
6717 current_lang_base = 0;
6719 scope_chain = s->prev;
6720 FOR_EACH_VEC_SAFE_ELT (s->old_bindings, i, saved)
6722 tree id = saved->identifier;
6724 IDENTIFIER_BINDING (id) = saved->binding;
6725 SET_IDENTIFIER_TYPE_VALUE (id, saved->real_type_value);
6728 /* If we were in the middle of compiling a function, restore our
6729 state. */
6730 if (s->need_pop_function_context)
6731 pop_function_context ();
6732 current_function_decl = s->function_decl;
6733 cp_unevaluated_operand = s->unevaluated_operand;
6734 c_inhibit_evaluation_warnings = s->inhibit_evaluation_warnings;
6736 /* Make this saved_scope structure available for reuse by
6737 push_to_top_level. */
6738 s->prev = free_saved_scope;
6739 free_saved_scope = s;
6742 /* Push into the scope of the namespace NS, even if it is deeply
6743 nested within another namespace. */
6745 static void
6746 do_push_nested_namespace (tree ns)
6748 if (ns == global_namespace)
6749 do_push_to_top_level ();
6750 else
6752 do_push_nested_namespace (CP_DECL_CONTEXT (ns));
6753 gcc_checking_assert
6754 (find_namespace_value (current_namespace, DECL_NAME (ns)) == ns);
6755 resume_scope (NAMESPACE_LEVEL (ns));
6756 current_namespace = ns;
6760 /* Pop back from the scope of the namespace NS, which was previously
6761 entered with push_nested_namespace. */
6763 static void
6764 do_pop_nested_namespace (tree ns)
6766 while (ns != global_namespace)
6768 ns = CP_DECL_CONTEXT (ns);
6769 current_namespace = ns;
6770 leave_scope ();
6773 do_pop_from_top_level ();
6776 /* Add TARGET to USINGS, if it does not already exist there.
6777 We used to build the complete graph of usings at this point, from
6778 the POV of the source namespaces. Now we build that as we perform
6779 the unqualified search. */
6781 static void
6782 add_using_namespace (vec<tree, va_gc> *&usings, tree target)
6784 if (usings)
6785 for (unsigned ix = usings->length (); ix--;)
6786 if ((*usings)[ix] == target)
6787 return;
6789 vec_safe_push (usings, target);
6792 /* Tell the debug system of a using directive. */
6794 static void
6795 emit_debug_info_using_namespace (tree from, tree target, bool implicit)
6797 /* Emit debugging info. */
6798 tree context = from != global_namespace ? from : NULL_TREE;
6799 debug_hooks->imported_module_or_decl (target, NULL_TREE, context, false,
6800 implicit);
6803 /* Process a namespace-scope using directive. */
6805 void
6806 finish_namespace_using_directive (tree target, tree attribs)
6808 gcc_checking_assert (namespace_bindings_p ());
6809 if (target == error_mark_node)
6810 return;
6812 add_using_namespace (DECL_NAMESPACE_USING (current_namespace),
6813 ORIGINAL_NAMESPACE (target));
6814 emit_debug_info_using_namespace (current_namespace,
6815 ORIGINAL_NAMESPACE (target), false);
6817 if (attribs == error_mark_node)
6818 return;
6820 for (tree a = attribs; a; a = TREE_CHAIN (a))
6822 tree name = get_attribute_name (a);
6823 if (is_attribute_p ("strong", name))
6825 warning (0, "strong using directive no longer supported");
6826 if (CP_DECL_CONTEXT (target) == current_namespace)
6827 inform (DECL_SOURCE_LOCATION (target),
6828 "you may use an inline namespace instead");
6830 else
6831 warning (OPT_Wattributes, "%qD attribute directive ignored", name);
6835 /* Process a function-scope using-directive. */
6837 void
6838 finish_local_using_directive (tree target, tree attribs)
6840 gcc_checking_assert (local_bindings_p ());
6841 if (target == error_mark_node)
6842 return;
6844 if (attribs)
6845 warning (OPT_Wattributes, "attributes ignored on local using directive");
6847 add_stmt (build_stmt (input_location, USING_STMT, target));
6849 add_using_namespace (current_binding_level->using_directives,
6850 ORIGINAL_NAMESPACE (target));
6853 /* Pushes X into the global namespace. */
6855 tree
6856 pushdecl_top_level (tree x, bool is_friend)
6858 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6859 do_push_to_top_level ();
6860 x = pushdecl_namespace_level (x, is_friend);
6861 do_pop_from_top_level ();
6862 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6863 return x;
6866 /* Pushes X into the global namespace and calls cp_finish_decl to
6867 register the variable, initializing it with INIT. */
6869 tree
6870 pushdecl_top_level_and_finish (tree x, tree init)
6872 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6873 do_push_to_top_level ();
6874 x = pushdecl_namespace_level (x, false);
6875 cp_finish_decl (x, init, false, NULL_TREE, 0);
6876 do_pop_from_top_level ();
6877 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6878 return x;
6881 /* Enter the namespaces from current_namerspace to NS. */
6883 static int
6884 push_inline_namespaces (tree ns)
6886 int count = 0;
6887 if (ns != current_namespace)
6889 gcc_assert (ns != global_namespace);
6890 count += push_inline_namespaces (CP_DECL_CONTEXT (ns));
6891 resume_scope (NAMESPACE_LEVEL (ns));
6892 current_namespace = ns;
6893 count++;
6895 return count;
6898 /* Push into the scope of the NAME namespace. If NAME is NULL_TREE,
6899 then we enter an anonymous namespace. If MAKE_INLINE is true, then
6900 we create an inline namespace (it is up to the caller to check upon
6901 redefinition). Return the number of namespaces entered. */
6904 push_namespace (tree name, bool make_inline)
6906 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6907 int count = 0;
6909 /* We should not get here if the global_namespace is not yet constructed
6910 nor if NAME designates the global namespace: The global scope is
6911 constructed elsewhere. */
6912 gcc_checking_assert (global_namespace != NULL && name != global_identifier);
6914 tree ns = NULL_TREE;
6916 name_lookup lookup (name, 0);
6917 if (!lookup.search_qualified (current_namespace, /*usings=*/false))
6919 else if (TREE_CODE (lookup.value) != NAMESPACE_DECL)
6921 else if (tree dna = DECL_NAMESPACE_ALIAS (lookup.value))
6923 /* A namespace alias is not allowed here, but if the alias
6924 is for a namespace also inside the current scope,
6925 accept it with a diagnostic. That's better than dying
6926 horribly. */
6927 if (is_nested_namespace (current_namespace, CP_DECL_CONTEXT (dna)))
6929 error ("namespace alias %qD not allowed here, "
6930 "assuming %qD", lookup.value, dna);
6931 ns = dna;
6934 else
6935 ns = lookup.value;
6938 bool new_ns = false;
6939 if (ns)
6940 /* DR2061. NS might be a member of an inline namespace. We
6941 need to push into those namespaces. */
6942 count += push_inline_namespaces (CP_DECL_CONTEXT (ns));
6943 else
6945 ns = build_lang_decl (NAMESPACE_DECL, name, void_type_node);
6946 SCOPE_DEPTH (ns) = SCOPE_DEPTH (current_namespace) + 1;
6947 if (!SCOPE_DEPTH (ns))
6948 /* We only allow depth 255. */
6949 sorry ("cannot nest more than %d namespaces",
6950 SCOPE_DEPTH (current_namespace));
6951 DECL_CONTEXT (ns) = FROB_CONTEXT (current_namespace);
6952 new_ns = true;
6954 if (pushdecl (ns) == error_mark_node)
6955 ns = NULL_TREE;
6956 else
6958 if (!name)
6960 SET_DECL_ASSEMBLER_NAME (ns, anon_identifier);
6962 if (!make_inline)
6963 add_using_namespace (DECL_NAMESPACE_USING (current_namespace),
6964 ns);
6966 else if (TREE_PUBLIC (current_namespace))
6967 TREE_PUBLIC (ns) = 1;
6969 if (make_inline)
6971 DECL_NAMESPACE_INLINE_P (ns) = true;
6972 vec_safe_push (DECL_NAMESPACE_INLINEES (current_namespace), ns);
6975 if (!name || make_inline)
6976 emit_debug_info_using_namespace (current_namespace, ns, true);
6980 if (ns)
6982 if (make_inline && !DECL_NAMESPACE_INLINE_P (ns))
6984 error ("inline namespace must be specified at initial definition");
6985 inform (DECL_SOURCE_LOCATION (ns), "%qD defined here", ns);
6987 if (new_ns)
6988 begin_scope (sk_namespace, ns);
6989 else
6990 resume_scope (NAMESPACE_LEVEL (ns));
6991 current_namespace = ns;
6992 count++;
6995 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6996 return count;
6999 /* Pop from the scope of the current namespace. */
7001 void
7002 pop_namespace (void)
7004 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
7006 gcc_assert (current_namespace != global_namespace);
7007 current_namespace = CP_DECL_CONTEXT (current_namespace);
7008 /* The binding level is not popped, as it might be re-opened later. */
7009 leave_scope ();
7011 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
7014 /* External entry points for do_{push_to/pop_from}_top_level. */
7016 void
7017 push_to_top_level (void)
7019 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
7020 do_push_to_top_level ();
7021 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
7024 void
7025 pop_from_top_level (void)
7027 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
7028 do_pop_from_top_level ();
7029 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
7032 /* External entry points for do_{push,pop}_nested_namespace. */
7034 void
7035 push_nested_namespace (tree ns)
7037 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
7038 do_push_nested_namespace (ns);
7039 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
7042 void
7043 pop_nested_namespace (tree ns)
7045 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
7046 gcc_assert (current_namespace == ns);
7047 do_pop_nested_namespace (ns);
7048 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
7051 /* Pop off extraneous binding levels left over due to syntax errors.
7052 We don't pop past namespaces, as they might be valid. */
7054 void
7055 pop_everything (void)
7057 if (ENABLE_SCOPE_CHECKING)
7058 verbatim ("XXX entering pop_everything ()\n");
7059 while (!namespace_bindings_p ())
7061 if (current_binding_level->kind == sk_class)
7062 pop_nested_class ();
7063 else
7064 poplevel (0, 0, 0);
7066 if (ENABLE_SCOPE_CHECKING)
7067 verbatim ("XXX leaving pop_everything ()\n");
7070 /* Emit debugging information for using declarations and directives.
7071 If input tree is overloaded fn then emit debug info for all
7072 candidates. */
7074 void
7075 cp_emit_debug_info_for_using (tree t, tree context)
7077 /* Don't try to emit any debug information if we have errors. */
7078 if (seen_error ())
7079 return;
7081 /* Ignore this FUNCTION_DECL if it refers to a builtin declaration
7082 of a builtin function. */
7083 if (TREE_CODE (t) == FUNCTION_DECL
7084 && DECL_EXTERNAL (t)
7085 && DECL_BUILT_IN (t))
7086 return;
7088 /* Do not supply context to imported_module_or_decl, if
7089 it is a global namespace. */
7090 if (context == global_namespace)
7091 context = NULL_TREE;
7093 t = MAYBE_BASELINK_FUNCTIONS (t);
7095 /* FIXME: Handle TEMPLATE_DECLs. */
7096 for (lkp_iterator iter (t); iter; ++iter)
7098 tree fn = *iter;
7099 if (TREE_CODE (fn) != TEMPLATE_DECL)
7101 if (building_stmt_list_p ())
7102 add_stmt (build_stmt (input_location, USING_STMT, fn));
7103 else
7104 debug_hooks->imported_module_or_decl (fn, NULL_TREE, context,
7105 false, false);
7110 #include "gt-cp-name-lookup.h"