[PR c++/84426] ICE after conflicting member decl
[official-gcc.git] / gcc / cp / name-lookup.c
blobc61f01c4f88bec01d41b901021ff0223ad257434
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 (!want_type
1166 && TREE_CODE (decl) == FIELD_DECL
1167 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
1169 if (tree temp = search_anon_aggr (TREE_TYPE (decl), name))
1170 return temp;
1173 if (DECL_NAME (decl) != name)
1174 continue;
1176 if (TREE_CODE (decl) == USING_DECL)
1178 decl = strip_using_decl (decl);
1179 if (is_overloaded_fn (decl))
1180 continue;
1183 if (DECL_DECLARES_FUNCTION_P (decl))
1184 /* Functions are found separately. */
1185 continue;
1187 if (!want_type || DECL_DECLARES_TYPE_P (decl))
1188 return decl;
1191 return NULL_TREE;
1194 /* Look for NAME field inside of anonymous aggregate ANON. */
1196 tree
1197 search_anon_aggr (tree anon, tree name)
1199 gcc_assert (COMPLETE_TYPE_P (anon));
1200 tree ret;
1202 if (vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (anon))
1203 ret = member_vec_linear_search (member_vec, name);
1204 else
1205 ret = fields_linear_search (anon, name, false);
1207 if (ret)
1209 /* Anon members can only contain fields. */
1210 gcc_assert (!STAT_HACK_P (ret) && !DECL_DECLARES_TYPE_P (ret));
1211 return ret;
1213 return NULL_TREE;
1216 /* Look for NAME as an immediate member of KLASS (including
1217 anon-members or unscoped enum member). TYPE_OR_FNS is zero for
1218 regular search. >0 to get a type binding (if there is one) and <0
1219 if you want (just) the member function binding.
1221 Use this if you do not want lazy member creation. */
1223 tree
1224 get_class_binding_direct (tree klass, tree name, int type_or_fns)
1226 gcc_checking_assert (RECORD_OR_UNION_TYPE_P (klass));
1228 /* Conversion operators can only be found by the marker conversion
1229 operator name. */
1230 bool conv_op = IDENTIFIER_CONV_OP_P (name);
1231 tree lookup = conv_op ? conv_op_identifier : name;
1232 tree val = NULL_TREE;
1233 vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (klass);
1235 if (COMPLETE_TYPE_P (klass) && member_vec)
1237 val = member_vec_binary_search (member_vec, lookup);
1238 if (!val)
1240 else if (type_or_fns > 0)
1242 if (STAT_HACK_P (val))
1243 val = STAT_TYPE (val);
1244 else if (!DECL_DECLARES_TYPE_P (val))
1245 val = NULL_TREE;
1247 else if (STAT_HACK_P (val))
1248 val = STAT_DECL (val);
1250 if (val && TREE_CODE (val) == OVERLOAD
1251 && TREE_CODE (OVL_FUNCTION (val)) == USING_DECL)
1253 /* An overload with a dependent USING_DECL. Does the caller
1254 want the USING_DECL or the functions? */
1255 if (type_or_fns < 0)
1256 val = OVL_CHAIN (val);
1257 else
1258 val = OVL_FUNCTION (val);
1261 else
1263 if (member_vec && type_or_fns <= 0)
1264 val = member_vec_linear_search (member_vec, lookup);
1266 if (type_or_fns < 0)
1267 /* Don't bother looking for field. We don't want it. */;
1268 else if (!val || (TREE_CODE (val) == OVERLOAD && OVL_USING_P (val)))
1269 /* Dependent using declarations are a 'field', make sure we
1270 return that even if we saw an overload already. */
1271 if (tree field_val = fields_linear_search (klass, lookup,
1272 type_or_fns > 0))
1273 if (!val || TREE_CODE (field_val) == USING_DECL)
1274 val = field_val;
1277 /* Extract the conversion operators asked for, unless the general
1278 conversion operator was requested. */
1279 if (val && conv_op)
1281 gcc_checking_assert (OVL_FUNCTION (val) == conv_op_marker);
1282 val = OVL_CHAIN (val);
1283 if (tree type = TREE_TYPE (name))
1284 val = extract_conversion_operator (val, type);
1287 return val;
1290 /* Look for NAME's binding in exactly KLASS. See
1291 get_class_binding_direct for argument description. Does lazy
1292 special function creation as necessary. */
1294 tree
1295 get_class_binding (tree klass, tree name, int type_or_fns)
1297 klass = complete_type (klass);
1299 if (COMPLETE_TYPE_P (klass))
1301 /* Lazily declare functions, if we're going to search these. */
1302 if (IDENTIFIER_CTOR_P (name))
1304 if (CLASSTYPE_LAZY_DEFAULT_CTOR (klass))
1305 lazily_declare_fn (sfk_constructor, klass);
1306 if (CLASSTYPE_LAZY_COPY_CTOR (klass))
1307 lazily_declare_fn (sfk_copy_constructor, klass);
1308 if (CLASSTYPE_LAZY_MOVE_CTOR (klass))
1309 lazily_declare_fn (sfk_move_constructor, klass);
1311 else if (IDENTIFIER_DTOR_P (name))
1313 if (CLASSTYPE_LAZY_DESTRUCTOR (klass))
1314 lazily_declare_fn (sfk_destructor, klass);
1316 else if (name == assign_op_identifier)
1318 if (CLASSTYPE_LAZY_COPY_ASSIGN (klass))
1319 lazily_declare_fn (sfk_copy_assignment, klass);
1320 if (CLASSTYPE_LAZY_MOVE_ASSIGN (klass))
1321 lazily_declare_fn (sfk_move_assignment, klass);
1325 return get_class_binding_direct (klass, name, type_or_fns);
1328 /* Find the slot containing overloads called 'NAME'. If there is no
1329 such slot and the class is complete, create an empty one, at the
1330 correct point in the sorted member vector. Otherwise return NULL.
1331 Deals with conv_op marker handling. */
1333 tree *
1334 find_member_slot (tree klass, tree name)
1336 bool complete_p = COMPLETE_TYPE_P (klass);
1338 vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (klass);
1339 if (!member_vec)
1341 vec_alloc (member_vec, 8);
1342 CLASSTYPE_MEMBER_VEC (klass) = member_vec;
1343 if (complete_p)
1345 /* If the class is complete but had no member_vec, we need
1346 to add the TYPE_FIELDS into it. We're also most likely
1347 to be adding ctors & dtors, so ask for 6 spare slots (the
1348 abstract cdtors and their clones). */
1349 set_class_bindings (klass, 6);
1350 member_vec = CLASSTYPE_MEMBER_VEC (klass);
1354 if (IDENTIFIER_CONV_OP_P (name))
1355 name = conv_op_identifier;
1357 unsigned ix, length = member_vec->length ();
1358 for (ix = 0; ix < length; ix++)
1360 tree *slot = &(*member_vec)[ix];
1361 tree fn_name = OVL_NAME (*slot);
1363 if (fn_name == name)
1365 /* If we found an existing slot, it must be a function set.
1366 Even with insertion after completion, because those only
1367 happen with artificial fns that have unspellable names.
1368 This means we do not have to deal with the stat hack
1369 either. */
1370 gcc_checking_assert (OVL_P (*slot));
1371 if (name == conv_op_identifier)
1373 gcc_checking_assert (OVL_FUNCTION (*slot) == conv_op_marker);
1374 /* Skip the conv-op marker. */
1375 slot = &OVL_CHAIN (*slot);
1377 return slot;
1380 if (complete_p && fn_name > name)
1381 break;
1384 /* No slot found, add one if the class is complete. */
1385 if (complete_p)
1387 /* Do exact allocation, as we don't expect to add many. */
1388 gcc_assert (name != conv_op_identifier);
1389 vec_safe_reserve_exact (member_vec, 1);
1390 CLASSTYPE_MEMBER_VEC (klass) = member_vec;
1391 member_vec->quick_insert (ix, NULL_TREE);
1392 return &(*member_vec)[ix];
1395 return NULL;
1398 /* KLASS is an incomplete class to which we're adding a method NAME.
1399 Add a slot and deal with conv_op marker handling. */
1401 tree *
1402 add_member_slot (tree klass, tree name)
1404 gcc_assert (!COMPLETE_TYPE_P (klass));
1406 vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (klass);
1407 vec_safe_push (member_vec, NULL_TREE);
1408 CLASSTYPE_MEMBER_VEC (klass) = member_vec;
1410 tree *slot = &member_vec->last ();
1411 if (IDENTIFIER_CONV_OP_P (name))
1413 /* Install the marker prefix. */
1414 *slot = ovl_make (conv_op_marker, NULL_TREE);
1415 slot = &OVL_CHAIN (*slot);
1418 return slot;
1421 /* Comparison function to compare two MEMBER_VEC entries by name.
1422 Because we can have duplicates during insertion of TYPE_FIELDS, we
1423 do extra checking so deduping doesn't have to deal with so many
1424 cases. */
1426 static int
1427 member_name_cmp (const void *a_p, const void *b_p)
1429 tree a = *(const tree *)a_p;
1430 tree b = *(const tree *)b_p;
1431 tree name_a = DECL_NAME (TREE_CODE (a) == OVERLOAD ? OVL_FUNCTION (a) : a);
1432 tree name_b = DECL_NAME (TREE_CODE (b) == OVERLOAD ? OVL_FUNCTION (b) : b);
1434 gcc_checking_assert (name_a && name_b);
1435 if (name_a != name_b)
1436 return name_a < name_b ? -1 : +1;
1438 if (name_a == conv_op_identifier)
1440 /* Strip the conv-op markers. */
1441 gcc_checking_assert (OVL_FUNCTION (a) == conv_op_marker
1442 && OVL_FUNCTION (b) == conv_op_marker);
1443 a = OVL_CHAIN (a);
1444 b = OVL_CHAIN (b);
1447 if (TREE_CODE (a) == OVERLOAD)
1448 a = OVL_FUNCTION (a);
1449 if (TREE_CODE (b) == OVERLOAD)
1450 b = OVL_FUNCTION (b);
1452 /* We're in STAT_HACK or USING_DECL territory (or possibly error-land). */
1453 if (TREE_CODE (a) != TREE_CODE (b))
1455 /* If one of them is a TYPE_DECL, it loses. */
1456 if (TREE_CODE (a) == TYPE_DECL)
1457 return +1;
1458 else if (TREE_CODE (b) == TYPE_DECL)
1459 return -1;
1461 /* If one of them is a USING_DECL, it loses. */
1462 if (TREE_CODE (a) == USING_DECL)
1463 return +1;
1464 else if (TREE_CODE (b) == USING_DECL)
1465 return -1;
1467 /* There are no other cases with different kinds of decls, as
1468 duplicate detection should have kicked in earlier. However,
1469 some erroneous cases get though. */
1470 gcc_assert (errorcount);
1473 /* Using source location would be the best thing here, but we can
1474 get identically-located decls in the following circumstances:
1476 1) duplicate artificial type-decls for the same type.
1478 2) pack expansions of using-decls.
1480 We should not be doing #1, but in either case it doesn't matter
1481 how we order these. Use UID as a proxy for source ordering, so
1482 that identically-located decls still have a well-defined stable
1483 ordering. */
1484 if (DECL_UID (a) != DECL_UID (b))
1485 return DECL_UID (a) < DECL_UID (b) ? -1 : +1;
1486 gcc_assert (a == b);
1487 return 0;
1490 static struct {
1491 gt_pointer_operator new_value;
1492 void *cookie;
1493 } resort_data;
1495 /* This routine compares two fields like member_name_cmp but using the
1496 pointer operator in resort_field_decl_data. We don't have to deal
1497 with duplicates here. */
1499 static int
1500 resort_member_name_cmp (const void *a_p, const void *b_p)
1502 tree a = *(const tree *)a_p;
1503 tree b = *(const tree *)b_p;
1504 tree name_a = OVL_NAME (a);
1505 tree name_b = OVL_NAME (b);
1507 resort_data.new_value (&name_a, resort_data.cookie);
1508 resort_data.new_value (&name_b, resort_data.cookie);
1510 gcc_checking_assert (name_a != name_b);
1512 return name_a < name_b ? -1 : +1;
1515 /* Resort CLASSTYPE_MEMBER_VEC because pointers have been reordered. */
1517 void
1518 resort_type_member_vec (void *obj, void */*orig_obj*/,
1519 gt_pointer_operator new_value, void* cookie)
1521 if (vec<tree, va_gc> *member_vec = (vec<tree, va_gc> *) obj)
1523 resort_data.new_value = new_value;
1524 resort_data.cookie = cookie;
1525 member_vec->qsort (resort_member_name_cmp);
1529 /* Recursively count the number of fields in KLASS, including anonymous
1530 union members. */
1532 static unsigned
1533 count_class_fields (tree klass)
1535 unsigned n_fields = 0;
1537 for (tree fields = TYPE_FIELDS (klass); fields; fields = DECL_CHAIN (fields))
1538 if (DECL_DECLARES_FUNCTION_P (fields))
1539 /* Functions are dealt with separately. */;
1540 else if (TREE_CODE (fields) == FIELD_DECL
1541 && ANON_AGGR_TYPE_P (TREE_TYPE (fields)))
1542 n_fields += count_class_fields (TREE_TYPE (fields));
1543 else if (DECL_NAME (fields))
1544 n_fields += 1;
1546 return n_fields;
1549 /* Append all the nonfunction members fields of KLASS to MEMBER_VEC.
1550 Recurse for anonymous members. MEMBER_VEC must have space. */
1552 static void
1553 member_vec_append_class_fields (vec<tree, va_gc> *member_vec, tree klass)
1555 for (tree fields = TYPE_FIELDS (klass); fields; fields = DECL_CHAIN (fields))
1556 if (DECL_DECLARES_FUNCTION_P (fields))
1557 /* Functions are handled separately. */;
1558 else if (TREE_CODE (fields) == FIELD_DECL
1559 && ANON_AGGR_TYPE_P (TREE_TYPE (fields)))
1560 member_vec_append_class_fields (member_vec, TREE_TYPE (fields));
1561 else if (DECL_NAME (fields))
1563 tree field = fields;
1564 /* Mark a conv-op USING_DECL with the conv-op-marker. */
1565 if (TREE_CODE (field) == USING_DECL
1566 && IDENTIFIER_CONV_OP_P (DECL_NAME (field)))
1567 field = ovl_make (conv_op_marker, field);
1568 member_vec->quick_push (field);
1572 /* Append all of the enum values of ENUMTYPE to MEMBER_VEC.
1573 MEMBER_VEC must have space. */
1575 static void
1576 member_vec_append_enum_values (vec<tree, va_gc> *member_vec, tree enumtype)
1578 for (tree values = TYPE_VALUES (enumtype);
1579 values; values = TREE_CHAIN (values))
1580 member_vec->quick_push (TREE_VALUE (values));
1583 /* MEMBER_VEC has just had new DECLs added to it, but is sorted.
1584 DeDup adjacent DECLS of the same name. We already dealt with
1585 conflict resolution when adding the fields or methods themselves.
1586 There are three cases (which could all be combined):
1587 1) a TYPE_DECL and non TYPE_DECL. Deploy STAT_HACK as appropriate.
1588 2) a USING_DECL and an overload. If the USING_DECL is dependent,
1589 it wins. Otherwise the OVERLOAD does.
1590 3) two USING_DECLS. ...
1592 member_name_cmp will have ordered duplicates as
1593 <fns><using><type> */
1595 static void
1596 member_vec_dedup (vec<tree, va_gc> *member_vec)
1598 unsigned len = member_vec->length ();
1599 unsigned store = 0;
1601 if (!len)
1602 return;
1604 tree current = (*member_vec)[0], name = OVL_NAME (current);
1605 tree next = NULL_TREE, next_name = NULL_TREE;
1606 for (unsigned jx, ix = 0; ix < len;
1607 ix = jx, current = next, name = next_name)
1609 tree to_type = NULL_TREE;
1610 tree to_using = NULL_TREE;
1611 tree marker = NULL_TREE;
1612 if (IDENTIFIER_CONV_OP_P (name))
1614 marker = current;
1615 current = OVL_CHAIN (current);
1616 name = DECL_NAME (OVL_FUNCTION (marker));
1617 gcc_checking_assert (name == conv_op_identifier);
1620 if (TREE_CODE (current) == USING_DECL)
1622 current = strip_using_decl (current);
1623 if (is_overloaded_fn (current))
1624 current = NULL_TREE;
1625 else if (TREE_CODE (current) == USING_DECL)
1627 to_using = current;
1628 current = NULL_TREE;
1632 if (current && DECL_DECLARES_TYPE_P (current))
1634 to_type = current;
1635 current = NULL_TREE;
1638 for (jx = ix + 1; jx < len; jx++)
1640 next = (*member_vec)[jx];
1641 next_name = OVL_NAME (next);
1642 if (next_name != name)
1643 break;
1645 if (marker)
1647 gcc_checking_assert (OVL_FUNCTION (marker)
1648 == OVL_FUNCTION (next));
1649 next = OVL_CHAIN (next);
1652 if (TREE_CODE (next) == USING_DECL)
1654 next = strip_using_decl (next);
1655 if (is_overloaded_fn (next))
1656 next = NULL_TREE;
1657 else if (TREE_CODE (next) == USING_DECL)
1659 to_using = next;
1660 next = NULL_TREE;
1664 if (next && DECL_DECLARES_TYPE_P (next))
1665 to_type = next;
1668 if (to_using)
1670 if (!current)
1671 current = to_using;
1672 else
1673 current = ovl_make (to_using, current);
1676 if (to_type)
1678 if (!current)
1679 current = to_type;
1680 else
1681 current = stat_hack (current, to_type);
1684 gcc_assert (current);
1685 if (marker)
1687 OVL_CHAIN (marker) = current;
1688 current = marker;
1690 (*member_vec)[store++] = current;
1693 while (store++ < len)
1694 member_vec->pop ();
1697 /* Add the non-function members to CLASSTYPE_MEMBER_VEC. If there is
1698 no existing MEMBER_VEC and fewer than 8 fields, do nothing. We
1699 know there must be at least 1 field -- the self-reference
1700 TYPE_DECL, except for anon aggregates, which will have at least
1701 one field. */
1703 void
1704 set_class_bindings (tree klass, unsigned extra)
1706 unsigned n_fields = count_class_fields (klass);
1707 vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (klass);
1709 if (member_vec || n_fields >= 8)
1711 /* Append the new fields. */
1712 vec_safe_reserve_exact (member_vec, extra + n_fields);
1713 member_vec_append_class_fields (member_vec, klass);
1716 if (member_vec)
1718 CLASSTYPE_MEMBER_VEC (klass) = member_vec;
1719 member_vec->qsort (member_name_cmp);
1720 member_vec_dedup (member_vec);
1724 /* Insert lately defined enum ENUMTYPE into KLASS for the sorted case. */
1726 void
1727 insert_late_enum_def_bindings (tree klass, tree enumtype)
1729 int n_fields;
1730 vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (klass);
1732 /* The enum bindings will already be on the TYPE_FIELDS, so don't
1733 count them twice. */
1734 if (!member_vec)
1735 n_fields = count_class_fields (klass);
1736 else
1737 n_fields = list_length (TYPE_VALUES (enumtype));
1739 if (member_vec || n_fields >= 8)
1741 vec_safe_reserve_exact (member_vec, n_fields);
1742 if (CLASSTYPE_MEMBER_VEC (klass))
1743 member_vec_append_enum_values (member_vec, enumtype);
1744 else
1745 member_vec_append_class_fields (member_vec, klass);
1746 CLASSTYPE_MEMBER_VEC (klass) = member_vec;
1747 member_vec->qsort (member_name_cmp);
1748 member_vec_dedup (member_vec);
1752 /* Compute the chain index of a binding_entry given the HASH value of its
1753 name and the total COUNT of chains. COUNT is assumed to be a power
1754 of 2. */
1756 #define ENTRY_INDEX(HASH, COUNT) (((HASH) >> 3) & ((COUNT) - 1))
1758 /* A free list of "binding_entry"s awaiting for re-use. */
1760 static GTY((deletable)) binding_entry free_binding_entry = NULL;
1762 /* The binding oracle; see cp-tree.h. */
1764 cp_binding_oracle_function *cp_binding_oracle;
1766 /* If we have a binding oracle, ask it for all namespace-scoped
1767 definitions of NAME. */
1769 static inline void
1770 query_oracle (tree name)
1772 if (!cp_binding_oracle)
1773 return;
1775 /* LOOKED_UP holds the set of identifiers that we have already
1776 looked up with the oracle. */
1777 static hash_set<tree> looked_up;
1778 if (looked_up.add (name))
1779 return;
1781 cp_binding_oracle (CP_ORACLE_IDENTIFIER, name);
1784 /* Create a binding_entry object for (NAME, TYPE). */
1786 static inline binding_entry
1787 binding_entry_make (tree name, tree type)
1789 binding_entry entry;
1791 if (free_binding_entry)
1793 entry = free_binding_entry;
1794 free_binding_entry = entry->chain;
1796 else
1797 entry = ggc_alloc<binding_entry_s> ();
1799 entry->name = name;
1800 entry->type = type;
1801 entry->chain = NULL;
1803 return entry;
1806 /* Put ENTRY back on the free list. */
1807 #if 0
1808 static inline void
1809 binding_entry_free (binding_entry entry)
1811 entry->name = NULL;
1812 entry->type = NULL;
1813 entry->chain = free_binding_entry;
1814 free_binding_entry = entry;
1816 #endif
1818 /* The datatype used to implement the mapping from names to types at
1819 a given scope. */
1820 struct GTY(()) binding_table_s {
1821 /* Array of chains of "binding_entry"s */
1822 binding_entry * GTY((length ("%h.chain_count"))) chain;
1824 /* The number of chains in this table. This is the length of the
1825 member "chain" considered as an array. */
1826 size_t chain_count;
1828 /* Number of "binding_entry"s in this table. */
1829 size_t entry_count;
1832 /* Construct TABLE with an initial CHAIN_COUNT. */
1834 static inline void
1835 binding_table_construct (binding_table table, size_t chain_count)
1837 table->chain_count = chain_count;
1838 table->entry_count = 0;
1839 table->chain = ggc_cleared_vec_alloc<binding_entry> (table->chain_count);
1842 /* Make TABLE's entries ready for reuse. */
1843 #if 0
1844 static void
1845 binding_table_free (binding_table table)
1847 size_t i;
1848 size_t count;
1850 if (table == NULL)
1851 return;
1853 for (i = 0, count = table->chain_count; i < count; ++i)
1855 binding_entry temp = table->chain[i];
1856 while (temp != NULL)
1858 binding_entry entry = temp;
1859 temp = entry->chain;
1860 binding_entry_free (entry);
1862 table->chain[i] = NULL;
1864 table->entry_count = 0;
1866 #endif
1868 /* Allocate a table with CHAIN_COUNT, assumed to be a power of two. */
1870 static inline binding_table
1871 binding_table_new (size_t chain_count)
1873 binding_table table = ggc_alloc<binding_table_s> ();
1874 table->chain = NULL;
1875 binding_table_construct (table, chain_count);
1876 return table;
1879 /* Expand TABLE to twice its current chain_count. */
1881 static void
1882 binding_table_expand (binding_table table)
1884 const size_t old_chain_count = table->chain_count;
1885 const size_t old_entry_count = table->entry_count;
1886 const size_t new_chain_count = 2 * old_chain_count;
1887 binding_entry *old_chains = table->chain;
1888 size_t i;
1890 binding_table_construct (table, new_chain_count);
1891 for (i = 0; i < old_chain_count; ++i)
1893 binding_entry entry = old_chains[i];
1894 for (; entry != NULL; entry = old_chains[i])
1896 const unsigned int hash = IDENTIFIER_HASH_VALUE (entry->name);
1897 const size_t j = ENTRY_INDEX (hash, new_chain_count);
1899 old_chains[i] = entry->chain;
1900 entry->chain = table->chain[j];
1901 table->chain[j] = entry;
1904 table->entry_count = old_entry_count;
1907 /* Insert a binding for NAME to TYPE into TABLE. */
1909 static void
1910 binding_table_insert (binding_table table, tree name, tree type)
1912 const unsigned int hash = IDENTIFIER_HASH_VALUE (name);
1913 const size_t i = ENTRY_INDEX (hash, table->chain_count);
1914 binding_entry entry = binding_entry_make (name, type);
1916 entry->chain = table->chain[i];
1917 table->chain[i] = entry;
1918 ++table->entry_count;
1920 if (3 * table->chain_count < 5 * table->entry_count)
1921 binding_table_expand (table);
1924 /* Return the binding_entry, if any, that maps NAME. */
1926 binding_entry
1927 binding_table_find (binding_table table, tree name)
1929 const unsigned int hash = IDENTIFIER_HASH_VALUE (name);
1930 binding_entry entry = table->chain[ENTRY_INDEX (hash, table->chain_count)];
1932 while (entry != NULL && entry->name != name)
1933 entry = entry->chain;
1935 return entry;
1938 /* Apply PROC -- with DATA -- to all entries in TABLE. */
1940 void
1941 binding_table_foreach (binding_table table, bt_foreach_proc proc, void *data)
1943 size_t chain_count;
1944 size_t i;
1946 if (!table)
1947 return;
1949 chain_count = table->chain_count;
1950 for (i = 0; i < chain_count; ++i)
1952 binding_entry entry = table->chain[i];
1953 for (; entry != NULL; entry = entry->chain)
1954 proc (entry, data);
1958 #ifndef ENABLE_SCOPE_CHECKING
1959 # define ENABLE_SCOPE_CHECKING 0
1960 #else
1961 # define ENABLE_SCOPE_CHECKING 1
1962 #endif
1964 /* A free list of "cxx_binding"s, connected by their PREVIOUS. */
1966 static GTY((deletable)) cxx_binding *free_bindings;
1968 /* Initialize VALUE and TYPE field for BINDING, and set the PREVIOUS
1969 field to NULL. */
1971 static inline void
1972 cxx_binding_init (cxx_binding *binding, tree value, tree type)
1974 binding->value = value;
1975 binding->type = type;
1976 binding->previous = NULL;
1979 /* (GC)-allocate a binding object with VALUE and TYPE member initialized. */
1981 static cxx_binding *
1982 cxx_binding_make (tree value, tree type)
1984 cxx_binding *binding;
1985 if (free_bindings)
1987 binding = free_bindings;
1988 free_bindings = binding->previous;
1990 else
1991 binding = ggc_alloc<cxx_binding> ();
1993 cxx_binding_init (binding, value, type);
1995 return binding;
1998 /* Put BINDING back on the free list. */
2000 static inline void
2001 cxx_binding_free (cxx_binding *binding)
2003 binding->scope = NULL;
2004 binding->previous = free_bindings;
2005 free_bindings = binding;
2008 /* Create a new binding for NAME (with the indicated VALUE and TYPE
2009 bindings) in the class scope indicated by SCOPE. */
2011 static cxx_binding *
2012 new_class_binding (tree name, tree value, tree type, cp_binding_level *scope)
2014 cp_class_binding cb = {cxx_binding_make (value, type), name};
2015 cxx_binding *binding = cb.base;
2016 vec_safe_push (scope->class_shadowed, cb);
2017 binding->scope = scope;
2018 return binding;
2021 /* Make DECL the innermost binding for ID. The LEVEL is the binding
2022 level at which this declaration is being bound. */
2024 void
2025 push_binding (tree id, tree decl, cp_binding_level* level)
2027 cxx_binding *binding;
2029 if (level != class_binding_level)
2031 binding = cxx_binding_make (decl, NULL_TREE);
2032 binding->scope = level;
2034 else
2035 binding = new_class_binding (id, decl, /*type=*/NULL_TREE, level);
2037 /* Now, fill in the binding information. */
2038 binding->previous = IDENTIFIER_BINDING (id);
2039 INHERITED_VALUE_BINDING_P (binding) = 0;
2040 LOCAL_BINDING_P (binding) = (level != class_binding_level);
2042 /* And put it on the front of the list of bindings for ID. */
2043 IDENTIFIER_BINDING (id) = binding;
2046 /* Remove the binding for DECL which should be the innermost binding
2047 for ID. */
2049 void
2050 pop_local_binding (tree id, tree decl)
2052 cxx_binding *binding;
2054 if (id == NULL_TREE)
2055 /* It's easiest to write the loops that call this function without
2056 checking whether or not the entities involved have names. We
2057 get here for such an entity. */
2058 return;
2060 /* Get the innermost binding for ID. */
2061 binding = IDENTIFIER_BINDING (id);
2063 /* The name should be bound. */
2064 gcc_assert (binding != NULL);
2066 /* The DECL will be either the ordinary binding or the type
2067 binding for this identifier. Remove that binding. */
2068 if (binding->value == decl)
2069 binding->value = NULL_TREE;
2070 else
2072 gcc_assert (binding->type == decl);
2073 binding->type = NULL_TREE;
2076 if (!binding->value && !binding->type)
2078 /* We're completely done with the innermost binding for this
2079 identifier. Unhook it from the list of bindings. */
2080 IDENTIFIER_BINDING (id) = binding->previous;
2082 /* Add it to the free list. */
2083 cxx_binding_free (binding);
2087 /* Remove the bindings for the decls of the current level and leave
2088 the current scope. */
2090 void
2091 pop_bindings_and_leave_scope (void)
2093 for (tree t = get_local_decls (); t; t = DECL_CHAIN (t))
2095 tree decl = TREE_CODE (t) == TREE_LIST ? TREE_VALUE (t) : t;
2096 tree name = OVL_NAME (decl);
2098 pop_local_binding (name, decl);
2101 leave_scope ();
2104 /* Strip non dependent using declarations. If DECL is dependent,
2105 surreptitiously create a typename_type and return it. */
2107 tree
2108 strip_using_decl (tree decl)
2110 if (decl == NULL_TREE)
2111 return NULL_TREE;
2113 while (TREE_CODE (decl) == USING_DECL && !DECL_DEPENDENT_P (decl))
2114 decl = USING_DECL_DECLS (decl);
2116 if (TREE_CODE (decl) == USING_DECL && DECL_DEPENDENT_P (decl)
2117 && USING_DECL_TYPENAME_P (decl))
2119 /* We have found a type introduced by a using
2120 declaration at class scope that refers to a dependent
2121 type.
2123 using typename :: [opt] nested-name-specifier unqualified-id ;
2125 decl = make_typename_type (TREE_TYPE (decl),
2126 DECL_NAME (decl),
2127 typename_type, tf_error);
2128 if (decl != error_mark_node)
2129 decl = TYPE_NAME (decl);
2132 return decl;
2135 /* Return true if OVL is an overload for an anticipated builtin. */
2137 static bool
2138 anticipated_builtin_p (tree ovl)
2140 if (TREE_CODE (ovl) != OVERLOAD)
2141 return false;
2143 if (!OVL_HIDDEN_P (ovl))
2144 return false;
2146 tree fn = OVL_FUNCTION (ovl);
2147 gcc_checking_assert (DECL_ANTICIPATED (fn));
2149 if (DECL_HIDDEN_FRIEND_P (fn))
2150 return false;
2152 return true;
2155 /* BINDING records an existing declaration for a name in the current scope.
2156 But, DECL is another declaration for that same identifier in the
2157 same scope. This is the `struct stat' hack whereby a non-typedef
2158 class name or enum-name can be bound at the same level as some other
2159 kind of entity.
2160 3.3.7/1
2162 A class name (9.1) or enumeration name (7.2) can be hidden by the
2163 name of an object, function, or enumerator declared in the same scope.
2164 If a class or enumeration name and an object, function, or enumerator
2165 are declared in the same scope (in any order) with the same name, the
2166 class or enumeration name is hidden wherever the object, function, or
2167 enumerator name is visible.
2169 It's the responsibility of the caller to check that
2170 inserting this name is valid here. Returns nonzero if the new binding
2171 was successful. */
2173 static bool
2174 supplement_binding_1 (cxx_binding *binding, tree decl)
2176 tree bval = binding->value;
2177 bool ok = true;
2178 tree target_bval = strip_using_decl (bval);
2179 tree target_decl = strip_using_decl (decl);
2181 if (TREE_CODE (target_decl) == TYPE_DECL && DECL_ARTIFICIAL (target_decl)
2182 && target_decl != target_bval
2183 && (TREE_CODE (target_bval) != TYPE_DECL
2184 /* We allow pushing an enum multiple times in a class
2185 template in order to handle late matching of underlying
2186 type on an opaque-enum-declaration followed by an
2187 enum-specifier. */
2188 || (processing_template_decl
2189 && TREE_CODE (TREE_TYPE (target_decl)) == ENUMERAL_TYPE
2190 && TREE_CODE (TREE_TYPE (target_bval)) == ENUMERAL_TYPE
2191 && (dependent_type_p (ENUM_UNDERLYING_TYPE
2192 (TREE_TYPE (target_decl)))
2193 || dependent_type_p (ENUM_UNDERLYING_TYPE
2194 (TREE_TYPE (target_bval)))))))
2195 /* The new name is the type name. */
2196 binding->type = decl;
2197 else if (/* TARGET_BVAL is null when push_class_level_binding moves
2198 an inherited type-binding out of the way to make room
2199 for a new value binding. */
2200 !target_bval
2201 /* TARGET_BVAL is error_mark_node when TARGET_DECL's name
2202 has been used in a non-class scope prior declaration.
2203 In that case, we should have already issued a
2204 diagnostic; for graceful error recovery purpose, pretend
2205 this was the intended declaration for that name. */
2206 || target_bval == error_mark_node
2207 /* If TARGET_BVAL is anticipated but has not yet been
2208 declared, pretend it is not there at all. */
2209 || anticipated_builtin_p (target_bval))
2210 binding->value = decl;
2211 else if (TREE_CODE (target_bval) == TYPE_DECL
2212 && DECL_ARTIFICIAL (target_bval)
2213 && target_decl != target_bval
2214 && (TREE_CODE (target_decl) != TYPE_DECL
2215 || same_type_p (TREE_TYPE (target_decl),
2216 TREE_TYPE (target_bval))))
2218 /* The old binding was a type name. It was placed in
2219 VALUE field because it was thought, at the point it was
2220 declared, to be the only entity with such a name. Move the
2221 type name into the type slot; it is now hidden by the new
2222 binding. */
2223 binding->type = bval;
2224 binding->value = decl;
2225 binding->value_is_inherited = false;
2227 else if (TREE_CODE (target_bval) == TYPE_DECL
2228 && TREE_CODE (target_decl) == TYPE_DECL
2229 && DECL_NAME (target_decl) == DECL_NAME (target_bval)
2230 && binding->scope->kind != sk_class
2231 && (same_type_p (TREE_TYPE (target_decl), TREE_TYPE (target_bval))
2232 /* If either type involves template parameters, we must
2233 wait until instantiation. */
2234 || uses_template_parms (TREE_TYPE (target_decl))
2235 || uses_template_parms (TREE_TYPE (target_bval))))
2236 /* We have two typedef-names, both naming the same type to have
2237 the same name. In general, this is OK because of:
2239 [dcl.typedef]
2241 In a given scope, a typedef specifier can be used to redefine
2242 the name of any type declared in that scope to refer to the
2243 type to which it already refers.
2245 However, in class scopes, this rule does not apply due to the
2246 stricter language in [class.mem] prohibiting redeclarations of
2247 members. */
2248 ok = false;
2249 /* There can be two block-scope declarations of the same variable,
2250 so long as they are `extern' declarations. However, there cannot
2251 be two declarations of the same static data member:
2253 [class.mem]
2255 A member shall not be declared twice in the
2256 member-specification. */
2257 else if (VAR_P (target_decl)
2258 && VAR_P (target_bval)
2259 && DECL_EXTERNAL (target_decl) && DECL_EXTERNAL (target_bval)
2260 && !DECL_CLASS_SCOPE_P (target_decl))
2262 duplicate_decls (decl, binding->value, /*newdecl_is_friend=*/false);
2263 ok = false;
2265 else if (TREE_CODE (decl) == NAMESPACE_DECL
2266 && TREE_CODE (bval) == NAMESPACE_DECL
2267 && DECL_NAMESPACE_ALIAS (decl)
2268 && DECL_NAMESPACE_ALIAS (bval)
2269 && ORIGINAL_NAMESPACE (bval) == ORIGINAL_NAMESPACE (decl))
2270 /* [namespace.alias]
2272 In a declarative region, a namespace-alias-definition can be
2273 used to redefine a namespace-alias declared in that declarative
2274 region to refer only to the namespace to which it already
2275 refers. */
2276 ok = false;
2277 else
2279 if (!error_operand_p (bval))
2280 diagnose_name_conflict (decl, bval);
2281 ok = false;
2284 return ok;
2287 /* Diagnose a name conflict between DECL and BVAL. */
2289 static void
2290 diagnose_name_conflict (tree decl, tree bval)
2292 if (TREE_CODE (decl) == TREE_CODE (bval)
2293 && TREE_CODE (decl) != NAMESPACE_DECL
2294 && !DECL_DECLARES_FUNCTION_P (decl)
2295 && (TREE_CODE (decl) != TYPE_DECL
2296 || DECL_ARTIFICIAL (decl) == DECL_ARTIFICIAL (bval))
2297 && CP_DECL_CONTEXT (decl) == CP_DECL_CONTEXT (bval))
2298 error ("redeclaration of %q#D", decl);
2299 else
2300 error ("%q#D conflicts with a previous declaration", decl);
2302 inform (location_of (bval), "previous declaration %q#D", bval);
2305 /* Wrapper for supplement_binding_1. */
2307 static bool
2308 supplement_binding (cxx_binding *binding, tree decl)
2310 bool ret;
2311 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
2312 ret = supplement_binding_1 (binding, decl);
2313 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
2314 return ret;
2317 /* Replace BINDING's current value on its scope's name list with
2318 NEWVAL. */
2320 static void
2321 update_local_overload (cxx_binding *binding, tree newval)
2323 tree *d;
2325 for (d = &binding->scope->names; ; d = &TREE_CHAIN (*d))
2326 if (*d == binding->value)
2328 /* Stitch new list node in. */
2329 *d = tree_cons (NULL_TREE, NULL_TREE, TREE_CHAIN (*d));
2330 break;
2332 else if (TREE_CODE (*d) == TREE_LIST && TREE_VALUE (*d) == binding->value)
2333 break;
2335 TREE_VALUE (*d) = newval;
2338 /* Compares the parameter-type-lists of ONE and TWO and
2339 returns false if they are different. If the DECLs are template
2340 functions, the return types and the template parameter lists are
2341 compared too (DR 565). */
2343 static bool
2344 matching_fn_p (tree one, tree two)
2346 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (one)),
2347 TYPE_ARG_TYPES (TREE_TYPE (two))))
2348 return false;
2350 if (TREE_CODE (one) == TEMPLATE_DECL
2351 && TREE_CODE (two) == TEMPLATE_DECL)
2353 /* Compare template parms. */
2354 if (!comp_template_parms (DECL_TEMPLATE_PARMS (one),
2355 DECL_TEMPLATE_PARMS (two)))
2356 return false;
2358 /* And return type. */
2359 if (!same_type_p (TREE_TYPE (TREE_TYPE (one)),
2360 TREE_TYPE (TREE_TYPE (two))))
2361 return false;
2364 return true;
2367 /* Push DECL into nonclass LEVEL BINDING or SLOT. OLD is the current
2368 binding value (possibly with anticipated builtins stripped).
2369 Diagnose conflicts and return updated decl. */
2371 static tree
2372 update_binding (cp_binding_level *level, cxx_binding *binding, tree *slot,
2373 tree old, tree decl, bool is_friend)
2375 tree to_val = decl;
2376 tree old_type = slot ? MAYBE_STAT_TYPE (*slot) : binding->type;
2377 tree to_type = old_type;
2379 gcc_assert (level->kind == sk_namespace ? !binding
2380 : level->kind != sk_class && !slot);
2381 if (old == error_mark_node)
2382 old = NULL_TREE;
2384 if (TREE_CODE (decl) == TYPE_DECL && DECL_ARTIFICIAL (decl))
2386 tree other = to_type;
2388 if (old && TREE_CODE (old) == TYPE_DECL && DECL_ARTIFICIAL (old))
2389 other = old;
2391 /* Pushing an artificial typedef. See if this matches either
2392 the type slot or the old value slot. */
2393 if (!other)
2395 else if (same_type_p (TREE_TYPE (other), TREE_TYPE (decl)))
2396 /* Two artificial decls to same type. Do nothing. */
2397 return other;
2398 else
2399 goto conflict;
2401 if (old)
2403 /* Slide decl into the type slot, keep old unaltered */
2404 to_type = decl;
2405 to_val = old;
2406 goto done;
2410 if (old && TREE_CODE (old) == TYPE_DECL && DECL_ARTIFICIAL (old))
2412 /* Slide old into the type slot. */
2413 to_type = old;
2414 old = NULL_TREE;
2417 if (DECL_DECLARES_FUNCTION_P (decl))
2419 if (!old)
2421 else if (OVL_P (old))
2423 for (ovl_iterator iter (old); iter; ++iter)
2425 tree fn = *iter;
2427 if (iter.using_p () && matching_fn_p (fn, decl))
2429 /* If a function declaration in namespace scope or
2430 block scope has the same name and the same
2431 parameter-type- list (8.3.5) as a function
2432 introduced by a using-declaration, and the
2433 declarations do not declare the same function,
2434 the program is ill-formed. [namespace.udecl]/14 */
2435 if (tree match = duplicate_decls (decl, fn, is_friend))
2436 return match;
2437 else
2438 /* FIXME: To preserve existing error behavior, we
2439 still push the decl. This might change. */
2440 diagnose_name_conflict (decl, fn);
2444 else
2445 goto conflict;
2447 if (to_type != old_type
2448 && warn_shadow
2449 && MAYBE_CLASS_TYPE_P (TREE_TYPE (to_type))
2450 && !(DECL_IN_SYSTEM_HEADER (decl)
2451 && DECL_IN_SYSTEM_HEADER (to_type)))
2452 warning (OPT_Wshadow, "%q#D hides constructor for %q#D",
2453 decl, to_type);
2455 to_val = ovl_insert (decl, old);
2457 else if (!old)
2459 else if (TREE_CODE (old) != TREE_CODE (decl))
2460 /* Different kinds of decls conflict. */
2461 goto conflict;
2462 else if (TREE_CODE (old) == TYPE_DECL)
2464 if (same_type_p (TREE_TYPE (old), TREE_TYPE (decl)))
2465 /* Two type decls to the same type. Do nothing. */
2466 return old;
2467 else
2468 goto conflict;
2470 else if (TREE_CODE (old) == NAMESPACE_DECL)
2472 /* Two maybe-aliased namespaces. If they're to the same target
2473 namespace, that's ok. */
2474 if (ORIGINAL_NAMESPACE (old) != ORIGINAL_NAMESPACE (decl))
2475 goto conflict;
2477 /* The new one must be an alias at this point. */
2478 gcc_assert (DECL_NAMESPACE_ALIAS (decl));
2479 return old;
2481 else if (TREE_CODE (old) == VAR_DECL)
2483 /* There can be two block-scope declarations of the same
2484 variable, so long as they are `extern' declarations. */
2485 if (!DECL_EXTERNAL (old) || !DECL_EXTERNAL (decl))
2486 goto conflict;
2487 else if (tree match = duplicate_decls (decl, old, false))
2488 return match;
2489 else
2490 goto conflict;
2492 else
2494 conflict:
2495 diagnose_name_conflict (decl, old);
2496 to_val = NULL_TREE;
2499 done:
2500 if (to_val)
2502 if (level->kind != sk_namespace
2503 && !to_type && binding->value && OVL_P (to_val))
2504 update_local_overload (binding, to_val);
2505 else
2507 tree to_add = to_val;
2509 if (level->kind == sk_namespace)
2510 to_add = decl;
2511 else if (to_type == decl)
2512 to_add = decl;
2513 else if (TREE_CODE (to_add) == OVERLOAD)
2514 to_add = build_tree_list (NULL_TREE, to_add);
2516 add_decl_to_level (level, to_add);
2519 if (slot)
2521 if (STAT_HACK_P (*slot))
2523 STAT_TYPE (*slot) = to_type;
2524 STAT_DECL (*slot) = to_val;
2526 else if (to_type)
2527 *slot = stat_hack (to_val, to_type);
2528 else
2529 *slot = to_val;
2531 else
2533 binding->type = to_type;
2534 binding->value = to_val;
2538 return decl;
2541 /* Table of identifiers to extern C declarations (or LISTS thereof). */
2543 static GTY(()) hash_table<named_decl_hash> *extern_c_decls;
2545 /* DECL has C linkage. If we have an existing instance, make sure the
2546 new one is compatible. Make sure it has the same exception
2547 specification [7.5, 7.6]. Add DECL to the map. */
2549 static void
2550 check_extern_c_conflict (tree decl)
2552 /* Ignore artificial or system header decls. */
2553 if (DECL_ARTIFICIAL (decl) || DECL_IN_SYSTEM_HEADER (decl))
2554 return;
2556 if (!extern_c_decls)
2557 extern_c_decls = hash_table<named_decl_hash>::create_ggc (127);
2559 tree *slot = extern_c_decls
2560 ->find_slot_with_hash (DECL_NAME (decl),
2561 IDENTIFIER_HASH_VALUE (DECL_NAME (decl)), INSERT);
2562 if (tree old = *slot)
2564 if (TREE_CODE (old) == OVERLOAD)
2565 old = OVL_FUNCTION (old);
2567 int mismatch = 0;
2568 if (DECL_CONTEXT (old) == DECL_CONTEXT (decl))
2569 ; /* If they're in the same context, we'll have already complained
2570 about a (possible) mismatch, when inserting the decl. */
2571 else if (!decls_match (decl, old))
2572 mismatch = 1;
2573 else if (TREE_CODE (decl) == FUNCTION_DECL
2574 && !comp_except_specs (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (old)),
2575 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl)),
2576 ce_normal))
2577 mismatch = -1;
2578 else if (DECL_ASSEMBLER_NAME_SET_P (old))
2579 SET_DECL_ASSEMBLER_NAME (decl, DECL_ASSEMBLER_NAME (old));
2581 if (mismatch)
2583 pedwarn (input_location, 0,
2584 "conflicting C language linkage declaration %q#D", decl);
2585 inform (DECL_SOURCE_LOCATION (old),
2586 "previous declaration %q#D", old);
2587 if (mismatch < 0)
2588 inform (input_location,
2589 "due to different exception specifications");
2591 else
2593 if (old == *slot)
2594 /* The hash table expects OVERLOADS, so construct one with
2595 OLD as both the function and the chain. This allocate
2596 an excess OVERLOAD node, but it's rare to have multiple
2597 extern "C" decls of the same name. And we save
2598 complicating the hash table logic (which is used
2599 elsewhere). */
2600 *slot = ovl_make (old, old);
2602 slot = &OVL_CHAIN (*slot);
2604 /* Chain it on for c_linkage_binding's use. */
2605 *slot = tree_cons (NULL_TREE, decl, *slot);
2608 else
2609 *slot = decl;
2612 /* Returns a list of C-linkage decls with the name NAME. Used in
2613 c-family/c-pragma.c to implement redefine_extname pragma. */
2615 tree
2616 c_linkage_bindings (tree name)
2618 if (extern_c_decls)
2619 if (tree *slot = extern_c_decls
2620 ->find_slot_with_hash (name, IDENTIFIER_HASH_VALUE (name), NO_INSERT))
2622 tree result = *slot;
2623 if (TREE_CODE (result) == OVERLOAD)
2624 result = OVL_CHAIN (result);
2625 return result;
2628 return NULL_TREE;
2631 /* DECL is being declared at a local scope. Emit suitable shadow
2632 warnings. */
2634 static void
2635 check_local_shadow (tree decl)
2637 /* Don't complain about the parms we push and then pop
2638 while tentatively parsing a function declarator. */
2639 if (TREE_CODE (decl) == PARM_DECL && !DECL_CONTEXT (decl))
2640 return;
2642 /* Inline decls shadow nothing. */
2643 if (DECL_FROM_INLINE (decl))
2644 return;
2646 /* External decls are something else. */
2647 if (DECL_EXTERNAL (decl))
2648 return;
2650 tree old = NULL_TREE;
2651 cp_binding_level *old_scope = NULL;
2652 if (cxx_binding *binding = outer_binding (DECL_NAME (decl), NULL, true))
2654 old = binding->value;
2655 old_scope = binding->scope;
2657 while (old && VAR_P (old) && DECL_DEAD_FOR_LOCAL (old))
2658 old = DECL_SHADOWED_FOR_VAR (old);
2660 tree shadowed = NULL_TREE;
2661 if (old
2662 && (TREE_CODE (old) == PARM_DECL
2663 || VAR_P (old)
2664 || (TREE_CODE (old) == TYPE_DECL
2665 && (!DECL_ARTIFICIAL (old)
2666 || TREE_CODE (decl) == TYPE_DECL)))
2667 && (!DECL_ARTIFICIAL (decl)
2668 || DECL_IMPLICIT_TYPEDEF_P (decl)
2669 || (VAR_P (decl) && DECL_ANON_UNION_VAR_P (decl))))
2671 /* DECL shadows a local thing possibly of interest. */
2673 /* Don't complain if it's from an enclosing function. */
2674 if (DECL_CONTEXT (old) == current_function_decl
2675 && TREE_CODE (decl) != PARM_DECL
2676 && TREE_CODE (old) == PARM_DECL)
2678 /* Go to where the parms should be and see if we find
2679 them there. */
2680 cp_binding_level *b = current_binding_level->level_chain;
2682 if (FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
2683 /* Skip the ctor/dtor cleanup level. */
2684 b = b->level_chain;
2686 /* ARM $8.3 */
2687 if (b->kind == sk_function_parms)
2689 error ("declaration of %q#D shadows a parameter", decl);
2690 return;
2694 /* The local structure or class can't use parameters of
2695 the containing function anyway. */
2696 if (DECL_CONTEXT (old) != current_function_decl)
2698 for (cp_binding_level *scope = current_binding_level;
2699 scope != old_scope; scope = scope->level_chain)
2700 if (scope->kind == sk_class
2701 && !LAMBDA_TYPE_P (scope->this_entity))
2702 return;
2704 /* Error if redeclaring a local declared in a
2705 init-statement or in the condition of an if or
2706 switch statement when the new declaration is in the
2707 outermost block of the controlled statement.
2708 Redeclaring a variable from a for or while condition is
2709 detected elsewhere. */
2710 else if (VAR_P (old)
2711 && old_scope == current_binding_level->level_chain
2712 && (old_scope->kind == sk_cond || old_scope->kind == sk_for))
2714 error ("redeclaration of %q#D", decl);
2715 inform (DECL_SOURCE_LOCATION (old),
2716 "%q#D previously declared here", old);
2717 return;
2719 /* C++11:
2720 3.3.3/3: The name declared in an exception-declaration (...)
2721 shall not be redeclared in the outermost block of the handler.
2722 3.3.3/2: A parameter name shall not be redeclared (...) in
2723 the outermost block of any handler associated with a
2724 function-try-block.
2725 3.4.1/15: The function parameter names shall not be redeclared
2726 in the exception-declaration nor in the outermost block of a
2727 handler for the function-try-block. */
2728 else if ((TREE_CODE (old) == VAR_DECL
2729 && old_scope == current_binding_level->level_chain
2730 && old_scope->kind == sk_catch)
2731 || (TREE_CODE (old) == PARM_DECL
2732 && (current_binding_level->kind == sk_catch
2733 || current_binding_level->level_chain->kind == sk_catch)
2734 && in_function_try_handler))
2736 if (permerror (input_location, "redeclaration of %q#D", decl))
2737 inform (DECL_SOURCE_LOCATION (old),
2738 "%q#D previously declared here", old);
2739 return;
2742 /* If '-Wshadow=compatible-local' is specified without other
2743 -Wshadow= flags, we will warn only when the type of the
2744 shadowing variable (DECL) can be converted to that of the
2745 shadowed parameter (OLD_LOCAL). The reason why we only check
2746 if DECL's type can be converted to OLD_LOCAL's type (but not the
2747 other way around) is because when users accidentally shadow a
2748 parameter, more than often they would use the variable
2749 thinking (mistakenly) it's still the parameter. It would be
2750 rare that users would use the variable in the place that
2751 expects the parameter but thinking it's a new decl. */
2753 enum opt_code warning_code;
2754 if (warn_shadow)
2755 warning_code = OPT_Wshadow;
2756 else if (warn_shadow_local)
2757 warning_code = OPT_Wshadow_local;
2758 else if (warn_shadow_compatible_local
2759 && (same_type_p (TREE_TYPE (old), TREE_TYPE (decl))
2760 || (!dependent_type_p (TREE_TYPE (decl))
2761 && !dependent_type_p (TREE_TYPE (old))
2762 && can_convert (TREE_TYPE (old), TREE_TYPE (decl),
2763 tf_none))))
2764 warning_code = OPT_Wshadow_compatible_local;
2765 else
2766 return;
2768 const char *msg;
2769 if (TREE_CODE (old) == PARM_DECL)
2770 msg = "declaration of %q#D shadows a parameter";
2771 else if (is_capture_proxy (old))
2772 msg = "declaration of %qD shadows a lambda capture";
2773 else
2774 msg = "declaration of %qD shadows a previous local";
2776 if (warning_at (input_location, warning_code, msg, decl))
2778 shadowed = old;
2779 goto inform_shadowed;
2781 return;
2784 if (!warn_shadow)
2785 return;
2787 /* Don't warn for artificial things that are not implicit typedefs. */
2788 if (DECL_ARTIFICIAL (decl) && !DECL_IMPLICIT_TYPEDEF_P (decl))
2789 return;
2791 if (nonlambda_method_basetype ())
2792 if (tree member = lookup_member (current_nonlambda_class_type (),
2793 DECL_NAME (decl), /*protect=*/0,
2794 /*want_type=*/false, tf_warning_or_error))
2796 member = MAYBE_BASELINK_FUNCTIONS (member);
2798 /* Warn if a variable shadows a non-function, or the variable
2799 is a function or a pointer-to-function. */
2800 if (!OVL_P (member)
2801 || TREE_CODE (decl) == FUNCTION_DECL
2802 || TYPE_PTRFN_P (TREE_TYPE (decl))
2803 || TYPE_PTRMEMFUNC_P (TREE_TYPE (decl)))
2805 if (warning_at (input_location, OPT_Wshadow,
2806 "declaration of %qD shadows a member of %qT",
2807 decl, current_nonlambda_class_type ())
2808 && DECL_P (member))
2810 shadowed = member;
2811 goto inform_shadowed;
2814 return;
2817 /* Now look for a namespace shadow. */
2818 old = find_namespace_value (current_namespace, DECL_NAME (decl));
2819 if (old
2820 && (VAR_P (old)
2821 || (TREE_CODE (old) == TYPE_DECL
2822 && (!DECL_ARTIFICIAL (old)
2823 || TREE_CODE (decl) == TYPE_DECL)))
2824 && !instantiating_current_function_p ())
2825 /* XXX shadow warnings in outer-more namespaces */
2827 if (warning_at (input_location, OPT_Wshadow,
2828 "declaration of %qD shadows a global declaration",
2829 decl))
2831 shadowed = old;
2832 goto inform_shadowed;
2834 return;
2837 return;
2839 inform_shadowed:
2840 inform (DECL_SOURCE_LOCATION (shadowed), "shadowed declaration is here");
2843 /* DECL is being pushed inside function CTX. Set its context, if
2844 needed. */
2846 static void
2847 set_decl_context_in_fn (tree ctx, tree decl)
2849 if (!DECL_CONTEXT (decl)
2850 /* A local declaration for a function doesn't constitute
2851 nesting. */
2852 && TREE_CODE (decl) != FUNCTION_DECL
2853 /* A local declaration for an `extern' variable is in the
2854 scope of the current namespace, not the current
2855 function. */
2856 && !(VAR_P (decl) && DECL_EXTERNAL (decl))
2857 /* When parsing the parameter list of a function declarator,
2858 don't set DECL_CONTEXT to an enclosing function. When we
2859 push the PARM_DECLs in order to process the function body,
2860 current_binding_level->this_entity will be set. */
2861 && !(TREE_CODE (decl) == PARM_DECL
2862 && current_binding_level->kind == sk_function_parms
2863 && current_binding_level->this_entity == NULL))
2864 DECL_CONTEXT (decl) = ctx;
2866 /* If this is the declaration for a namespace-scope function,
2867 but the declaration itself is in a local scope, mark the
2868 declaration. */
2869 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (decl))
2870 DECL_LOCAL_FUNCTION_P (decl) = 1;
2873 /* DECL is a local-scope decl with linkage. SHADOWED is true if the
2874 name is already bound at the current level.
2876 [basic.link] If there is a visible declaration of an entity with
2877 linkage having the same name and type, ignoring entities declared
2878 outside the innermost enclosing namespace scope, the block scope
2879 declaration declares that same entity and receives the linkage of
2880 the previous declaration.
2882 Also, make sure that this decl matches any existing external decl
2883 in the enclosing namespace. */
2885 static void
2886 set_local_extern_decl_linkage (tree decl, bool shadowed)
2888 tree ns_value = decl; /* Unique marker. */
2890 if (!shadowed)
2892 tree loc_value = innermost_non_namespace_value (DECL_NAME (decl));
2893 if (!loc_value)
2895 ns_value
2896 = find_namespace_value (current_namespace, DECL_NAME (decl));
2897 loc_value = ns_value;
2899 if (loc_value == error_mark_node)
2900 loc_value = NULL_TREE;
2902 for (ovl_iterator iter (loc_value); iter; ++iter)
2903 if (!iter.hidden_p ()
2904 && (TREE_STATIC (*iter) || DECL_EXTERNAL (*iter))
2905 && decls_match (*iter, decl))
2907 /* The standard only says that the local extern inherits
2908 linkage from the previous decl; in particular, default
2909 args are not shared. Add the decl into a hash table to
2910 make sure only the previous decl in this case is seen
2911 by the middle end. */
2912 struct cxx_int_tree_map *h;
2914 /* We inherit the outer decl's linkage. But we're a
2915 different decl. */
2916 TREE_PUBLIC (decl) = TREE_PUBLIC (*iter);
2918 if (cp_function_chain->extern_decl_map == NULL)
2919 cp_function_chain->extern_decl_map
2920 = hash_table<cxx_int_tree_map_hasher>::create_ggc (20);
2922 h = ggc_alloc<cxx_int_tree_map> ();
2923 h->uid = DECL_UID (decl);
2924 h->to = *iter;
2925 cxx_int_tree_map **loc = cp_function_chain->extern_decl_map
2926 ->find_slot (h, INSERT);
2927 *loc = h;
2928 break;
2932 if (TREE_PUBLIC (decl))
2934 /* DECL is externally visible. Make sure it matches a matching
2935 decl in the namespace scope. We only really need to check
2936 this when inserting the decl, not when we find an existing
2937 match in the current scope. However, in practice we're
2938 going to be inserting a new decl in the majority of cases --
2939 who writes multiple extern decls for the same thing in the
2940 same local scope? Doing it here often avoids a duplicate
2941 namespace lookup. */
2943 /* Avoid repeating a lookup. */
2944 if (ns_value == decl)
2945 ns_value = find_namespace_value (current_namespace, DECL_NAME (decl));
2947 if (ns_value == error_mark_node)
2948 ns_value = NULL_TREE;
2950 for (ovl_iterator iter (ns_value); iter; ++iter)
2952 tree other = *iter;
2954 if (!(TREE_PUBLIC (other) || DECL_EXTERNAL (other)))
2955 ; /* Not externally visible. */
2956 else if (DECL_EXTERN_C_P (decl) && DECL_EXTERN_C_P (other))
2957 ; /* Both are extern "C", we'll check via that mechanism. */
2958 else if (TREE_CODE (other) != TREE_CODE (decl)
2959 || ((VAR_P (decl) || matching_fn_p (other, decl))
2960 && !comptypes (TREE_TYPE (decl), TREE_TYPE (other),
2961 COMPARE_REDECLARATION)))
2963 if (permerror (DECL_SOURCE_LOCATION (decl),
2964 "local external declaration %q#D", decl))
2965 inform (DECL_SOURCE_LOCATION (other),
2966 "does not match previous declaration %q#D", other);
2967 break;
2973 /* Record DECL as belonging to the current lexical scope. Check for
2974 errors (such as an incompatible declaration for the same name
2975 already seen in the same scope). IS_FRIEND is true if DECL is
2976 declared as a friend.
2978 Returns either DECL or an old decl for the same name. If an old
2979 decl is returned, it may have been smashed to agree with what DECL
2980 says. */
2982 static tree
2983 do_pushdecl (tree decl, bool is_friend)
2985 if (decl == error_mark_node)
2986 return error_mark_node;
2988 if (!DECL_TEMPLATE_PARM_P (decl) && current_function_decl)
2989 set_decl_context_in_fn (current_function_decl, decl);
2991 /* The binding level we will be pushing into. During local class
2992 pushing, we want to push to the containing scope. */
2993 cp_binding_level *level = current_binding_level;
2994 while (level->kind == sk_class)
2995 level = level->level_chain;
2997 /* An anonymous namespace has a NULL DECL_NAME, but we still want to
2998 insert it. Other NULL-named decls, not so much. */
2999 tree name = DECL_NAME (decl);
3000 if (name || TREE_CODE (decl) == NAMESPACE_DECL)
3002 cxx_binding *binding = NULL; /* Local scope binding. */
3003 tree ns = NULL_TREE; /* Searched namespace. */
3004 tree *slot = NULL; /* Binding slot in namespace. */
3005 tree old = NULL_TREE;
3007 if (level->kind == sk_namespace)
3009 /* We look in the decl's namespace for an existing
3010 declaration, even though we push into the current
3011 namespace. */
3012 ns = (DECL_NAMESPACE_SCOPE_P (decl)
3013 ? CP_DECL_CONTEXT (decl) : current_namespace);
3014 /* Create the binding, if this is current namespace, because
3015 that's where we'll be pushing anyway. */
3016 slot = find_namespace_slot (ns, name, ns == current_namespace);
3017 if (slot)
3018 old = MAYBE_STAT_DECL (*slot);
3020 else
3022 binding = find_local_binding (level, name);
3023 if (binding)
3024 old = binding->value;
3027 if (current_function_decl && VAR_OR_FUNCTION_DECL_P (decl)
3028 && DECL_EXTERNAL (decl))
3029 set_local_extern_decl_linkage (decl, old != NULL_TREE);
3031 if (old == error_mark_node)
3032 old = NULL_TREE;
3034 for (ovl_iterator iter (old); iter; ++iter)
3035 if (iter.using_p ())
3036 ; /* Ignore using decls here. */
3037 else if (tree match = duplicate_decls (decl, *iter, is_friend))
3039 if (match == error_mark_node)
3041 else if (TREE_CODE (match) == TYPE_DECL)
3042 /* The IDENTIFIER will have the type referring to the
3043 now-smashed TYPE_DECL, because ...? Reset it. */
3044 SET_IDENTIFIER_TYPE_VALUE (name, TREE_TYPE (match));
3045 else if (iter.hidden_p () && !DECL_HIDDEN_P (match))
3047 /* Unhiding a previously hidden decl. */
3048 tree head = iter.reveal_node (old);
3049 if (head != old)
3051 if (!ns)
3053 update_local_overload (binding, head);
3054 binding->value = head;
3056 else if (STAT_HACK_P (*slot))
3057 STAT_DECL (*slot) = head;
3058 else
3059 *slot = head;
3061 if (DECL_EXTERN_C_P (match))
3062 /* We need to check and register the decl now. */
3063 check_extern_c_conflict (match);
3065 return match;
3068 /* We are pushing a new decl. */
3070 /* Skip a hidden builtin we failed to match already. There can
3071 only be one. */
3072 if (old && anticipated_builtin_p (old))
3073 old = OVL_CHAIN (old);
3075 check_template_shadow (decl);
3076 bool visible_injection = false;
3078 if (DECL_DECLARES_FUNCTION_P (decl))
3080 check_default_args (decl);
3082 if (is_friend)
3084 if (level->kind != sk_namespace)
3086 /* In a local class, a friend function declaration must
3087 find a matching decl in the innermost non-class scope.
3088 [class.friend/11] */
3089 error ("friend declaration %qD in local class without "
3090 "prior local declaration", decl);
3091 /* Don't attempt to push it. */
3092 return error_mark_node;
3094 if (!flag_friend_injection)
3095 /* Hide it from ordinary lookup. */
3096 DECL_ANTICIPATED (decl) = DECL_HIDDEN_FRIEND_P (decl) = true;
3097 else
3098 visible_injection = true;
3102 if (level->kind != sk_namespace)
3104 check_local_shadow (decl);
3106 if (TREE_CODE (decl) == NAMESPACE_DECL)
3107 /* A local namespace alias. */
3108 set_identifier_type_value (name, NULL_TREE);
3110 if (!binding)
3111 binding = create_local_binding (level, name);
3113 else if (!slot)
3115 ns = current_namespace;
3116 slot = find_namespace_slot (ns, name, true);
3117 /* Update OLD to reflect the namespace we're going to be
3118 pushing into. */
3119 old = MAYBE_STAT_DECL (*slot);
3122 old = update_binding (level, binding, slot, old, decl, is_friend);
3124 if (old != decl)
3125 /* An existing decl matched, use it. */
3126 decl = old;
3127 else if (TREE_CODE (decl) == TYPE_DECL)
3129 tree type = TREE_TYPE (decl);
3131 if (type != error_mark_node)
3133 if (TYPE_NAME (type) != decl)
3134 set_underlying_type (decl);
3136 if (!ns)
3137 set_identifier_type_value_with_scope (name, decl, level);
3138 else
3139 SET_IDENTIFIER_TYPE_VALUE (name, global_type_node);
3142 /* If this is a locally defined typedef in a function that
3143 is not a template instantation, record it to implement
3144 -Wunused-local-typedefs. */
3145 if (!instantiating_current_function_p ())
3146 record_locally_defined_typedef (decl);
3148 else if (VAR_P (decl))
3149 maybe_register_incomplete_var (decl);
3150 else if (visible_injection)
3151 warning (0, "injected friend %qD is visible"
3152 " due to %<-ffriend-injection%>", decl);
3154 if ((VAR_P (decl) || TREE_CODE (decl) == FUNCTION_DECL)
3155 && DECL_EXTERN_C_P (decl))
3156 check_extern_c_conflict (decl);
3158 else
3159 add_decl_to_level (level, decl);
3161 return decl;
3164 /* Record a decl-node X as belonging to the current lexical scope.
3165 It's a friend if IS_FRIEND is true -- which affects exactly where
3166 we push it. */
3168 tree
3169 pushdecl (tree x, bool is_friend)
3171 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3172 tree ret = do_pushdecl (x, is_friend);
3173 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3174 return ret;
3177 /* Enter DECL into the symbol table, if that's appropriate. Returns
3178 DECL, or a modified version thereof. */
3180 tree
3181 maybe_push_decl (tree decl)
3183 tree type = TREE_TYPE (decl);
3185 /* Add this decl to the current binding level, but not if it comes
3186 from another scope, e.g. a static member variable. TEM may equal
3187 DECL or it may be a previous decl of the same name. */
3188 if (decl == error_mark_node
3189 || (TREE_CODE (decl) != PARM_DECL
3190 && DECL_CONTEXT (decl) != NULL_TREE
3191 /* Definitions of namespace members outside their namespace are
3192 possible. */
3193 && !DECL_NAMESPACE_SCOPE_P (decl))
3194 || (TREE_CODE (decl) == TEMPLATE_DECL && !namespace_bindings_p ())
3195 || type == unknown_type_node
3196 /* The declaration of a template specialization does not affect
3197 the functions available for overload resolution, so we do not
3198 call pushdecl. */
3199 || (TREE_CODE (decl) == FUNCTION_DECL
3200 && DECL_TEMPLATE_SPECIALIZATION (decl)))
3201 return decl;
3202 else
3203 return pushdecl (decl);
3206 /* Bind DECL to ID in the current_binding_level, assumed to be a local
3207 binding level. If IS_USING is true, DECL got here through a
3208 using-declaration. */
3210 static void
3211 push_local_binding (tree id, tree decl, bool is_using)
3213 /* Skip over any local classes. This makes sense if we call
3214 push_local_binding with a friend decl of a local class. */
3215 cp_binding_level *b = innermost_nonclass_level ();
3217 gcc_assert (b->kind != sk_namespace);
3218 if (find_local_binding (b, id))
3220 /* Supplement the existing binding. */
3221 if (!supplement_binding (IDENTIFIER_BINDING (id), decl))
3222 /* It didn't work. Something else must be bound at this
3223 level. Do not add DECL to the list of things to pop
3224 later. */
3225 return;
3227 else
3228 /* Create a new binding. */
3229 push_binding (id, decl, b);
3231 if (TREE_CODE (decl) == OVERLOAD || is_using)
3232 /* We must put the OVERLOAD or using into a TREE_LIST since we
3233 cannot use the decl's chain itself. */
3234 decl = build_tree_list (NULL_TREE, decl);
3236 /* And put DECL on the list of things declared by the current
3237 binding level. */
3238 add_decl_to_level (b, decl);
3241 /* Check to see whether or not DECL is a variable that would have been
3242 in scope under the ARM, but is not in scope under the ANSI/ISO
3243 standard. If so, issue an error message. If name lookup would
3244 work in both cases, but return a different result, this function
3245 returns the result of ANSI/ISO lookup. Otherwise, it returns
3246 DECL.
3248 FIXME: Scheduled for removal after GCC-8 is done. */
3250 tree
3251 check_for_out_of_scope_variable (tree decl)
3253 tree shadowed;
3255 /* We only care about out of scope variables. */
3256 if (!(VAR_P (decl) && DECL_DEAD_FOR_LOCAL (decl)))
3257 return decl;
3259 shadowed = DECL_HAS_SHADOWED_FOR_VAR_P (decl)
3260 ? DECL_SHADOWED_FOR_VAR (decl) : NULL_TREE ;
3261 while (shadowed != NULL_TREE && VAR_P (shadowed)
3262 && DECL_DEAD_FOR_LOCAL (shadowed))
3263 shadowed = DECL_HAS_SHADOWED_FOR_VAR_P (shadowed)
3264 ? DECL_SHADOWED_FOR_VAR (shadowed) : NULL_TREE;
3265 if (!shadowed)
3266 shadowed = find_namespace_value (current_namespace, DECL_NAME (decl));
3267 if (shadowed)
3269 if (!DECL_ERROR_REPORTED (decl)
3270 && flag_permissive
3271 && warning (0, "name lookup of %qD changed", DECL_NAME (decl)))
3273 inform (DECL_SOURCE_LOCATION (shadowed),
3274 "matches this %qD under ISO standard rules", shadowed);
3275 inform (DECL_SOURCE_LOCATION (decl),
3276 " matches this %qD under old rules", decl);
3278 DECL_ERROR_REPORTED (decl) = 1;
3279 return shadowed;
3282 /* If we have already complained about this declaration, there's no
3283 need to do it again. */
3284 if (DECL_ERROR_REPORTED (decl))
3285 return decl;
3287 DECL_ERROR_REPORTED (decl) = 1;
3289 if (TREE_TYPE (decl) == error_mark_node)
3290 return decl;
3292 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
3294 error ("name lookup of %qD changed for ISO %<for%> scoping",
3295 DECL_NAME (decl));
3296 inform (DECL_SOURCE_LOCATION (decl),
3297 "cannot use obsolete binding %qD because it has a destructor",
3298 decl);
3299 return error_mark_node;
3301 else
3303 permerror (input_location,
3304 "name lookup of %qD changed for ISO %<for%> scoping",
3305 DECL_NAME (decl));
3306 if (flag_permissive)
3307 inform (DECL_SOURCE_LOCATION (decl),
3308 "using obsolete binding %qD", decl);
3309 static bool hint;
3310 if (!hint)
3311 inform (input_location, flag_permissive
3312 ? "this flexibility is deprecated and will be removed"
3313 : "if you use %<-fpermissive%> G++ will accept your code");
3314 hint = true;
3317 return decl;
3320 /* true means unconditionally make a BLOCK for the next level pushed. */
3322 static bool keep_next_level_flag;
3324 static int binding_depth = 0;
3326 static void
3327 indent (int depth)
3329 int i;
3331 for (i = 0; i < depth * 2; i++)
3332 putc (' ', stderr);
3335 /* Return a string describing the kind of SCOPE we have. */
3336 static const char *
3337 cp_binding_level_descriptor (cp_binding_level *scope)
3339 /* The order of this table must match the "scope_kind"
3340 enumerators. */
3341 static const char* scope_kind_names[] = {
3342 "block-scope",
3343 "cleanup-scope",
3344 "try-scope",
3345 "catch-scope",
3346 "for-scope",
3347 "function-parameter-scope",
3348 "class-scope",
3349 "namespace-scope",
3350 "template-parameter-scope",
3351 "template-explicit-spec-scope"
3353 const scope_kind kind = scope->explicit_spec_p
3354 ? sk_template_spec : scope->kind;
3356 return scope_kind_names[kind];
3359 /* Output a debugging information about SCOPE when performing
3360 ACTION at LINE. */
3361 static void
3362 cp_binding_level_debug (cp_binding_level *scope, int line, const char *action)
3364 const char *desc = cp_binding_level_descriptor (scope);
3365 if (scope->this_entity)
3366 verbatim ("%s %<%s(%E)%> %p %d\n", action, desc,
3367 scope->this_entity, (void *) scope, line);
3368 else
3369 verbatim ("%s %s %p %d\n", action, desc, (void *) scope, line);
3372 /* Return the estimated initial size of the hashtable of a NAMESPACE
3373 scope. */
3375 static inline size_t
3376 namespace_scope_ht_size (tree ns)
3378 tree name = DECL_NAME (ns);
3380 return name == std_identifier
3381 ? NAMESPACE_STD_HT_SIZE
3382 : (name == global_identifier
3383 ? GLOBAL_SCOPE_HT_SIZE
3384 : NAMESPACE_ORDINARY_HT_SIZE);
3387 /* A chain of binding_level structures awaiting reuse. */
3389 static GTY((deletable)) cp_binding_level *free_binding_level;
3391 /* Insert SCOPE as the innermost binding level. */
3393 void
3394 push_binding_level (cp_binding_level *scope)
3396 /* Add it to the front of currently active scopes stack. */
3397 scope->level_chain = current_binding_level;
3398 current_binding_level = scope;
3399 keep_next_level_flag = false;
3401 if (ENABLE_SCOPE_CHECKING)
3403 scope->binding_depth = binding_depth;
3404 indent (binding_depth);
3405 cp_binding_level_debug (scope, LOCATION_LINE (input_location),
3406 "push");
3407 binding_depth++;
3411 /* Create a new KIND scope and make it the top of the active scopes stack.
3412 ENTITY is the scope of the associated C++ entity (namespace, class,
3413 function, C++0x enumeration); it is NULL otherwise. */
3415 cp_binding_level *
3416 begin_scope (scope_kind kind, tree entity)
3418 cp_binding_level *scope;
3420 /* Reuse or create a struct for this binding level. */
3421 if (!ENABLE_SCOPE_CHECKING && free_binding_level)
3423 scope = free_binding_level;
3424 free_binding_level = scope->level_chain;
3425 memset (scope, 0, sizeof (cp_binding_level));
3427 else
3428 scope = ggc_cleared_alloc<cp_binding_level> ();
3430 scope->this_entity = entity;
3431 scope->more_cleanups_ok = true;
3432 switch (kind)
3434 case sk_cleanup:
3435 scope->keep = true;
3436 break;
3438 case sk_template_spec:
3439 scope->explicit_spec_p = true;
3440 kind = sk_template_parms;
3441 /* Fall through. */
3442 case sk_template_parms:
3443 case sk_block:
3444 case sk_try:
3445 case sk_catch:
3446 case sk_for:
3447 case sk_cond:
3448 case sk_class:
3449 case sk_scoped_enum:
3450 case sk_function_parms:
3451 case sk_transaction:
3452 case sk_omp:
3453 scope->keep = keep_next_level_flag;
3454 break;
3456 case sk_namespace:
3457 NAMESPACE_LEVEL (entity) = scope;
3458 break;
3460 default:
3461 /* Should not happen. */
3462 gcc_unreachable ();
3463 break;
3465 scope->kind = kind;
3467 push_binding_level (scope);
3469 return scope;
3472 /* We're about to leave current scope. Pop the top of the stack of
3473 currently active scopes. Return the enclosing scope, now active. */
3475 cp_binding_level *
3476 leave_scope (void)
3478 cp_binding_level *scope = current_binding_level;
3480 if (scope->kind == sk_namespace && class_binding_level)
3481 current_binding_level = class_binding_level;
3483 /* We cannot leave a scope, if there are none left. */
3484 if (NAMESPACE_LEVEL (global_namespace))
3485 gcc_assert (!global_scope_p (scope));
3487 if (ENABLE_SCOPE_CHECKING)
3489 indent (--binding_depth);
3490 cp_binding_level_debug (scope, LOCATION_LINE (input_location),
3491 "leave");
3494 /* Move one nesting level up. */
3495 current_binding_level = scope->level_chain;
3497 /* Namespace-scopes are left most probably temporarily, not
3498 completely; they can be reopened later, e.g. in namespace-extension
3499 or any name binding activity that requires us to resume a
3500 namespace. For classes, we cache some binding levels. For other
3501 scopes, we just make the structure available for reuse. */
3502 if (scope->kind != sk_namespace
3503 && scope->kind != sk_class)
3505 scope->level_chain = free_binding_level;
3506 gcc_assert (!ENABLE_SCOPE_CHECKING
3507 || scope->binding_depth == binding_depth);
3508 free_binding_level = scope;
3511 if (scope->kind == sk_class)
3513 /* Reset DEFINING_CLASS_P to allow for reuse of a
3514 class-defining scope in a non-defining context. */
3515 scope->defining_class_p = 0;
3517 /* Find the innermost enclosing class scope, and reset
3518 CLASS_BINDING_LEVEL appropriately. */
3519 class_binding_level = NULL;
3520 for (scope = current_binding_level; scope; scope = scope->level_chain)
3521 if (scope->kind == sk_class)
3523 class_binding_level = scope;
3524 break;
3528 return current_binding_level;
3531 static void
3532 resume_scope (cp_binding_level* b)
3534 /* Resuming binding levels is meant only for namespaces,
3535 and those cannot nest into classes. */
3536 gcc_assert (!class_binding_level);
3537 /* Also, resuming a non-directly nested namespace is a no-no. */
3538 gcc_assert (b->level_chain == current_binding_level);
3539 current_binding_level = b;
3540 if (ENABLE_SCOPE_CHECKING)
3542 b->binding_depth = binding_depth;
3543 indent (binding_depth);
3544 cp_binding_level_debug (b, LOCATION_LINE (input_location), "resume");
3545 binding_depth++;
3549 /* Return the innermost binding level that is not for a class scope. */
3551 static cp_binding_level *
3552 innermost_nonclass_level (void)
3554 cp_binding_level *b;
3556 b = current_binding_level;
3557 while (b->kind == sk_class)
3558 b = b->level_chain;
3560 return b;
3563 /* We're defining an object of type TYPE. If it needs a cleanup, but
3564 we're not allowed to add any more objects with cleanups to the current
3565 scope, create a new binding level. */
3567 void
3568 maybe_push_cleanup_level (tree type)
3570 if (type != error_mark_node
3571 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
3572 && current_binding_level->more_cleanups_ok == 0)
3574 begin_scope (sk_cleanup, NULL);
3575 current_binding_level->statement_list = push_stmt_list ();
3579 /* Return true if we are in the global binding level. */
3581 bool
3582 global_bindings_p (void)
3584 return global_scope_p (current_binding_level);
3587 /* True if we are currently in a toplevel binding level. This
3588 means either the global binding level or a namespace in a toplevel
3589 binding level. Since there are no non-toplevel namespace levels,
3590 this really means any namespace or template parameter level. We
3591 also include a class whose context is toplevel. */
3593 bool
3594 toplevel_bindings_p (void)
3596 cp_binding_level *b = innermost_nonclass_level ();
3598 return b->kind == sk_namespace || b->kind == sk_template_parms;
3601 /* True if this is a namespace scope, or if we are defining a class
3602 which is itself at namespace scope, or whose enclosing class is
3603 such a class, etc. */
3605 bool
3606 namespace_bindings_p (void)
3608 cp_binding_level *b = innermost_nonclass_level ();
3610 return b->kind == sk_namespace;
3613 /* True if the innermost non-class scope is a block scope. */
3615 bool
3616 local_bindings_p (void)
3618 cp_binding_level *b = innermost_nonclass_level ();
3619 return b->kind < sk_function_parms || b->kind == sk_omp;
3622 /* True if the current level needs to have a BLOCK made. */
3624 bool
3625 kept_level_p (void)
3627 return (current_binding_level->blocks != NULL_TREE
3628 || current_binding_level->keep
3629 || current_binding_level->kind == sk_cleanup
3630 || current_binding_level->names != NULL_TREE
3631 || current_binding_level->using_directives);
3634 /* Returns the kind of the innermost scope. */
3636 scope_kind
3637 innermost_scope_kind (void)
3639 return current_binding_level->kind;
3642 /* Returns true if this scope was created to store template parameters. */
3644 bool
3645 template_parm_scope_p (void)
3647 return innermost_scope_kind () == sk_template_parms;
3650 /* If KEEP is true, make a BLOCK node for the next binding level,
3651 unconditionally. Otherwise, use the normal logic to decide whether
3652 or not to create a BLOCK. */
3654 void
3655 keep_next_level (bool keep)
3657 keep_next_level_flag = keep;
3660 /* Return the list of declarations of the current local scope. */
3662 tree
3663 get_local_decls (void)
3665 gcc_assert (current_binding_level->kind != sk_namespace
3666 && current_binding_level->kind != sk_class);
3667 return current_binding_level->names;
3670 /* Return how many function prototypes we are currently nested inside. */
3673 function_parm_depth (void)
3675 int level = 0;
3676 cp_binding_level *b;
3678 for (b = current_binding_level;
3679 b->kind == sk_function_parms;
3680 b = b->level_chain)
3681 ++level;
3683 return level;
3686 /* For debugging. */
3687 static int no_print_functions = 0;
3688 static int no_print_builtins = 0;
3690 static void
3691 print_binding_level (cp_binding_level* lvl)
3693 tree t;
3694 int i = 0, len;
3695 fprintf (stderr, " blocks=%p", (void *) lvl->blocks);
3696 if (lvl->more_cleanups_ok)
3697 fprintf (stderr, " more-cleanups-ok");
3698 if (lvl->have_cleanups)
3699 fprintf (stderr, " have-cleanups");
3700 fprintf (stderr, "\n");
3701 if (lvl->names)
3703 fprintf (stderr, " names:\t");
3704 /* We can probably fit 3 names to a line? */
3705 for (t = lvl->names; t; t = TREE_CHAIN (t))
3707 if (no_print_functions && (TREE_CODE (t) == FUNCTION_DECL))
3708 continue;
3709 if (no_print_builtins
3710 && (TREE_CODE (t) == TYPE_DECL)
3711 && DECL_IS_BUILTIN (t))
3712 continue;
3714 /* Function decls tend to have longer names. */
3715 if (TREE_CODE (t) == FUNCTION_DECL)
3716 len = 3;
3717 else
3718 len = 2;
3719 i += len;
3720 if (i > 6)
3722 fprintf (stderr, "\n\t");
3723 i = len;
3725 print_node_brief (stderr, "", t, 0);
3726 if (t == error_mark_node)
3727 break;
3729 if (i)
3730 fprintf (stderr, "\n");
3732 if (vec_safe_length (lvl->class_shadowed))
3734 size_t i;
3735 cp_class_binding *b;
3736 fprintf (stderr, " class-shadowed:");
3737 FOR_EACH_VEC_ELT (*lvl->class_shadowed, i, b)
3738 fprintf (stderr, " %s ", IDENTIFIER_POINTER (b->identifier));
3739 fprintf (stderr, "\n");
3741 if (lvl->type_shadowed)
3743 fprintf (stderr, " type-shadowed:");
3744 for (t = lvl->type_shadowed; t; t = TREE_CHAIN (t))
3746 fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
3748 fprintf (stderr, "\n");
3752 DEBUG_FUNCTION void
3753 debug (cp_binding_level &ref)
3755 print_binding_level (&ref);
3758 DEBUG_FUNCTION void
3759 debug (cp_binding_level *ptr)
3761 if (ptr)
3762 debug (*ptr);
3763 else
3764 fprintf (stderr, "<nil>\n");
3768 void
3769 print_other_binding_stack (cp_binding_level *stack)
3771 cp_binding_level *level;
3772 for (level = stack; !global_scope_p (level); level = level->level_chain)
3774 fprintf (stderr, "binding level %p\n", (void *) level);
3775 print_binding_level (level);
3779 void
3780 print_binding_stack (void)
3782 cp_binding_level *b;
3783 fprintf (stderr, "current_binding_level=%p\n"
3784 "class_binding_level=%p\n"
3785 "NAMESPACE_LEVEL (global_namespace)=%p\n",
3786 (void *) current_binding_level, (void *) class_binding_level,
3787 (void *) NAMESPACE_LEVEL (global_namespace));
3788 if (class_binding_level)
3790 for (b = class_binding_level; b; b = b->level_chain)
3791 if (b == current_binding_level)
3792 break;
3793 if (b)
3794 b = class_binding_level;
3795 else
3796 b = current_binding_level;
3798 else
3799 b = current_binding_level;
3800 print_other_binding_stack (b);
3801 fprintf (stderr, "global:\n");
3802 print_binding_level (NAMESPACE_LEVEL (global_namespace));
3805 /* Return the type associated with ID. */
3807 static tree
3808 identifier_type_value_1 (tree id)
3810 /* There is no type with that name, anywhere. */
3811 if (REAL_IDENTIFIER_TYPE_VALUE (id) == NULL_TREE)
3812 return NULL_TREE;
3813 /* This is not the type marker, but the real thing. */
3814 if (REAL_IDENTIFIER_TYPE_VALUE (id) != global_type_node)
3815 return REAL_IDENTIFIER_TYPE_VALUE (id);
3816 /* Have to search for it. It must be on the global level, now.
3817 Ask lookup_name not to return non-types. */
3818 id = lookup_name_real (id, 2, 1, /*block_p=*/true, 0, 0);
3819 if (id)
3820 return TREE_TYPE (id);
3821 return NULL_TREE;
3824 /* Wrapper for identifier_type_value_1. */
3826 tree
3827 identifier_type_value (tree id)
3829 tree ret;
3830 timevar_start (TV_NAME_LOOKUP);
3831 ret = identifier_type_value_1 (id);
3832 timevar_stop (TV_NAME_LOOKUP);
3833 return ret;
3836 /* Push a definition of struct, union or enum tag named ID. into
3837 binding_level B. DECL is a TYPE_DECL for the type. We assume that
3838 the tag ID is not already defined. */
3840 static void
3841 set_identifier_type_value_with_scope (tree id, tree decl, cp_binding_level *b)
3843 tree type;
3845 if (b->kind != sk_namespace)
3847 /* Shadow the marker, not the real thing, so that the marker
3848 gets restored later. */
3849 tree old_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
3850 b->type_shadowed
3851 = tree_cons (id, old_type_value, b->type_shadowed);
3852 type = decl ? TREE_TYPE (decl) : NULL_TREE;
3853 TREE_TYPE (b->type_shadowed) = type;
3855 else
3857 tree *slot = find_namespace_slot (current_namespace, id, true);
3858 gcc_assert (decl);
3859 update_binding (b, NULL, slot, MAYBE_STAT_DECL (*slot), decl, false);
3861 /* Store marker instead of real type. */
3862 type = global_type_node;
3864 SET_IDENTIFIER_TYPE_VALUE (id, type);
3867 /* As set_identifier_type_value_with_scope, but using
3868 current_binding_level. */
3870 void
3871 set_identifier_type_value (tree id, tree decl)
3873 set_identifier_type_value_with_scope (id, decl, current_binding_level);
3876 /* Return the name for the constructor (or destructor) for the
3877 specified class. */
3879 tree
3880 constructor_name (tree type)
3882 tree decl = TYPE_NAME (TYPE_MAIN_VARIANT (type));
3884 return decl ? DECL_NAME (decl) : NULL_TREE;
3887 /* Returns TRUE if NAME is the name for the constructor for TYPE,
3888 which must be a class type. */
3890 bool
3891 constructor_name_p (tree name, tree type)
3893 gcc_assert (MAYBE_CLASS_TYPE_P (type));
3895 /* These don't have names. */
3896 if (TREE_CODE (type) == DECLTYPE_TYPE
3897 || TREE_CODE (type) == TYPEOF_TYPE)
3898 return false;
3900 if (name && name == constructor_name (type))
3901 return true;
3903 return false;
3906 /* Counter used to create anonymous type names. */
3908 static GTY(()) int anon_cnt;
3910 /* Return an IDENTIFIER which can be used as a name for
3911 unnamed structs and unions. */
3913 tree
3914 make_anon_name (void)
3916 char buf[32];
3918 sprintf (buf, anon_aggrname_format (), anon_cnt++);
3919 return get_identifier (buf);
3922 /* This code is practically identical to that for creating
3923 anonymous names, but is just used for lambdas instead. This isn't really
3924 necessary, but it's convenient to avoid treating lambdas like other
3925 unnamed types. */
3927 static GTY(()) int lambda_cnt = 0;
3929 tree
3930 make_lambda_name (void)
3932 char buf[32];
3934 sprintf (buf, LAMBDANAME_FORMAT, lambda_cnt++);
3935 return get_identifier (buf);
3938 /* Insert another USING_DECL into the current binding level, returning
3939 this declaration. If this is a redeclaration, do nothing, and
3940 return NULL_TREE if this not in namespace scope (in namespace
3941 scope, a using decl might extend any previous bindings). */
3943 static tree
3944 push_using_decl_1 (tree scope, tree name)
3946 tree decl;
3948 gcc_assert (TREE_CODE (scope) == NAMESPACE_DECL);
3949 gcc_assert (identifier_p (name));
3950 for (decl = current_binding_level->usings; decl; decl = DECL_CHAIN (decl))
3951 if (USING_DECL_SCOPE (decl) == scope && DECL_NAME (decl) == name)
3952 break;
3953 if (decl)
3954 return namespace_bindings_p () ? decl : NULL_TREE;
3955 decl = build_lang_decl (USING_DECL, name, NULL_TREE);
3956 USING_DECL_SCOPE (decl) = scope;
3957 DECL_CHAIN (decl) = current_binding_level->usings;
3958 current_binding_level->usings = decl;
3959 return decl;
3962 /* Wrapper for push_using_decl_1. */
3964 static tree
3965 push_using_decl (tree scope, tree name)
3967 tree ret;
3968 timevar_start (TV_NAME_LOOKUP);
3969 ret = push_using_decl_1 (scope, name);
3970 timevar_stop (TV_NAME_LOOKUP);
3971 return ret;
3974 /* Same as pushdecl, but define X in binding-level LEVEL. We rely on the
3975 caller to set DECL_CONTEXT properly.
3977 Note that this must only be used when X will be the new innermost
3978 binding for its name, as we tack it onto the front of IDENTIFIER_BINDING
3979 without checking to see if the current IDENTIFIER_BINDING comes from a
3980 closer binding level than LEVEL. */
3982 static tree
3983 do_pushdecl_with_scope (tree x, cp_binding_level *level, bool is_friend)
3985 cp_binding_level *b;
3986 tree function_decl = current_function_decl;
3988 current_function_decl = NULL_TREE;
3989 if (level->kind == sk_class)
3991 b = class_binding_level;
3992 class_binding_level = level;
3993 pushdecl_class_level (x);
3994 class_binding_level = b;
3996 else
3998 b = current_binding_level;
3999 current_binding_level = level;
4000 x = pushdecl (x, is_friend);
4001 current_binding_level = b;
4003 current_function_decl = function_decl;
4004 return x;
4007 /* Inject X into the local scope just before the function parms. */
4009 tree
4010 pushdecl_outermost_localscope (tree x)
4012 cp_binding_level *b = NULL;
4013 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4015 /* Find the scope just inside the function parms. */
4016 for (cp_binding_level *n = current_binding_level;
4017 n->kind != sk_function_parms; n = b->level_chain)
4018 b = n;
4020 tree ret = b ? do_pushdecl_with_scope (x, b, false) : error_mark_node;
4021 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4023 return ret;
4026 /* Check a non-member using-declaration. Return the name and scope
4027 being used, and the USING_DECL, or NULL_TREE on failure. */
4029 static tree
4030 validate_nonmember_using_decl (tree decl, tree scope, tree name)
4032 /* [namespace.udecl]
4033 A using-declaration for a class member shall be a
4034 member-declaration. */
4035 if (TYPE_P (scope))
4037 error ("%qT is not a namespace or unscoped enum", scope);
4038 return NULL_TREE;
4040 else if (scope == error_mark_node)
4041 return NULL_TREE;
4043 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR)
4045 /* 7.3.3/5
4046 A using-declaration shall not name a template-id. */
4047 error ("a using-declaration cannot specify a template-id. "
4048 "Try %<using %D%>", name);
4049 return NULL_TREE;
4052 if (TREE_CODE (decl) == NAMESPACE_DECL)
4054 error ("namespace %qD not allowed in using-declaration", decl);
4055 return NULL_TREE;
4058 if (TREE_CODE (decl) == SCOPE_REF)
4060 /* It's a nested name with template parameter dependent scope.
4061 This can only be using-declaration for class member. */
4062 error ("%qT is not a namespace", TREE_OPERAND (decl, 0));
4063 return NULL_TREE;
4066 decl = OVL_FIRST (decl);
4068 /* Make a USING_DECL. */
4069 tree using_decl = push_using_decl (scope, name);
4071 if (using_decl == NULL_TREE
4072 && at_function_scope_p ()
4073 && VAR_P (decl))
4074 /* C++11 7.3.3/10. */
4075 error ("%qD is already declared in this scope", name);
4077 return using_decl;
4080 /* Process a local-scope or namespace-scope using declaration. SCOPE
4081 is the nominated scope to search for NAME. VALUE_P and TYPE_P
4082 point to the binding for NAME in the current scope and are
4083 updated. */
4085 static void
4086 do_nonmember_using_decl (tree scope, tree name, tree *value_p, tree *type_p)
4088 name_lookup lookup (name, 0);
4090 if (!qualified_namespace_lookup (scope, &lookup))
4092 error ("%qD not declared", name);
4093 return;
4095 else if (TREE_CODE (lookup.value) == TREE_LIST)
4097 error ("reference to %qD is ambiguous", name);
4098 print_candidates (lookup.value);
4099 lookup.value = NULL_TREE;
4102 if (lookup.type && TREE_CODE (lookup.type) == TREE_LIST)
4104 error ("reference to %qD is ambiguous", name);
4105 print_candidates (lookup.type);
4106 lookup.type = NULL_TREE;
4109 tree value = *value_p;
4110 tree type = *type_p;
4112 /* Shift the old and new bindings around so we're comparing class and
4113 enumeration names to each other. */
4114 if (value && DECL_IMPLICIT_TYPEDEF_P (value))
4116 type = value;
4117 value = NULL_TREE;
4120 if (lookup.value && DECL_IMPLICIT_TYPEDEF_P (lookup.value))
4122 lookup.type = lookup.value;
4123 lookup.value = NULL_TREE;
4126 if (lookup.value && lookup.value != value)
4128 /* Check for using functions. */
4129 if (OVL_P (lookup.value) && (!value || OVL_P (value)))
4131 for (lkp_iterator usings (lookup.value); usings; ++usings)
4133 tree new_fn = *usings;
4135 /* [namespace.udecl]
4137 If a function declaration in namespace scope or block
4138 scope has the same name and the same parameter types as a
4139 function introduced by a using declaration the program is
4140 ill-formed. */
4141 bool found = false;
4142 for (ovl_iterator old (value); !found && old; ++old)
4144 tree old_fn = *old;
4146 if (new_fn == old_fn)
4147 /* The function already exists in the current
4148 namespace. */
4149 found = true;
4150 else if (old.using_p ())
4151 continue; /* This is a using decl. */
4152 else if (old.hidden_p () && !DECL_HIDDEN_FRIEND_P (old_fn))
4153 continue; /* This is an anticipated builtin. */
4154 else if (!matching_fn_p (new_fn, old_fn))
4155 continue; /* Parameters do not match. */
4156 else if (decls_match (new_fn, old_fn))
4157 found = true;
4158 else
4160 diagnose_name_conflict (new_fn, old_fn);
4161 found = true;
4165 if (!found)
4166 /* Unlike the overload case we don't drop anticipated
4167 builtins here. They don't cause a problem, and
4168 we'd like to match them with a future
4169 declaration. */
4170 value = ovl_insert (new_fn, value, true);
4173 else if (value
4174 /* Ignore anticipated builtins. */
4175 && !anticipated_builtin_p (value)
4176 && !decls_match (lookup.value, value))
4177 diagnose_name_conflict (lookup.value, value);
4178 else
4179 value = lookup.value;
4182 if (lookup.type && lookup.type != type)
4184 if (type && !decls_match (lookup.type, type))
4185 diagnose_name_conflict (lookup.type, type);
4186 else
4187 type = lookup.type;
4190 /* If bind->value is empty, shift any class or enumeration name back. */
4191 if (!value)
4193 value = type;
4194 type = NULL_TREE;
4197 *value_p = value;
4198 *type_p = type;
4201 /* Returns true if ANCESTOR encloses DESCENDANT, including matching.
4202 Both are namespaces. */
4204 bool
4205 is_nested_namespace (tree ancestor, tree descendant, bool inline_only)
4207 int depth = SCOPE_DEPTH (ancestor);
4209 if (!depth && !inline_only)
4210 /* The global namespace encloses everything. */
4211 return true;
4213 while (SCOPE_DEPTH (descendant) > depth
4214 && (!inline_only || DECL_NAMESPACE_INLINE_P (descendant)))
4215 descendant = CP_DECL_CONTEXT (descendant);
4217 return ancestor == descendant;
4220 /* Returns true if ROOT (a namespace, class, or function) encloses
4221 CHILD. CHILD may be either a class type or a namespace. */
4223 bool
4224 is_ancestor (tree root, tree child)
4226 gcc_assert ((TREE_CODE (root) == NAMESPACE_DECL
4227 || TREE_CODE (root) == FUNCTION_DECL
4228 || CLASS_TYPE_P (root)));
4229 gcc_assert ((TREE_CODE (child) == NAMESPACE_DECL
4230 || CLASS_TYPE_P (child)));
4232 /* The global namespace encloses everything. */
4233 if (root == global_namespace)
4234 return true;
4236 /* Search until we reach namespace scope. */
4237 while (TREE_CODE (child) != NAMESPACE_DECL)
4239 /* If we've reached the ROOT, it encloses CHILD. */
4240 if (root == child)
4241 return true;
4242 /* Go out one level. */
4243 if (TYPE_P (child))
4244 child = TYPE_NAME (child);
4245 child = CP_DECL_CONTEXT (child);
4248 if (TREE_CODE (root) == NAMESPACE_DECL)
4249 return is_nested_namespace (root, child);
4251 return false;
4254 /* Enter the class or namespace scope indicated by T suitable for name
4255 lookup. T can be arbitrary scope, not necessary nested inside the
4256 current scope. Returns a non-null scope to pop iff pop_scope
4257 should be called later to exit this scope. */
4259 tree
4260 push_scope (tree t)
4262 if (TREE_CODE (t) == NAMESPACE_DECL)
4263 push_decl_namespace (t);
4264 else if (CLASS_TYPE_P (t))
4266 if (!at_class_scope_p ()
4267 || !same_type_p (current_class_type, t))
4268 push_nested_class (t);
4269 else
4270 /* T is the same as the current scope. There is therefore no
4271 need to re-enter the scope. Since we are not actually
4272 pushing a new scope, our caller should not call
4273 pop_scope. */
4274 t = NULL_TREE;
4277 return t;
4280 /* Leave scope pushed by push_scope. */
4282 void
4283 pop_scope (tree t)
4285 if (t == NULL_TREE)
4286 return;
4287 if (TREE_CODE (t) == NAMESPACE_DECL)
4288 pop_decl_namespace ();
4289 else if CLASS_TYPE_P (t)
4290 pop_nested_class ();
4293 /* Subroutine of push_inner_scope. */
4295 static void
4296 push_inner_scope_r (tree outer, tree inner)
4298 tree prev;
4300 if (outer == inner
4301 || (TREE_CODE (inner) != NAMESPACE_DECL && !CLASS_TYPE_P (inner)))
4302 return;
4304 prev = CP_DECL_CONTEXT (TREE_CODE (inner) == NAMESPACE_DECL ? inner : TYPE_NAME (inner));
4305 if (outer != prev)
4306 push_inner_scope_r (outer, prev);
4307 if (TREE_CODE (inner) == NAMESPACE_DECL)
4309 cp_binding_level *save_template_parm = 0;
4310 /* Temporary take out template parameter scopes. They are saved
4311 in reversed order in save_template_parm. */
4312 while (current_binding_level->kind == sk_template_parms)
4314 cp_binding_level *b = current_binding_level;
4315 current_binding_level = b->level_chain;
4316 b->level_chain = save_template_parm;
4317 save_template_parm = b;
4320 resume_scope (NAMESPACE_LEVEL (inner));
4321 current_namespace = inner;
4323 /* Restore template parameter scopes. */
4324 while (save_template_parm)
4326 cp_binding_level *b = save_template_parm;
4327 save_template_parm = b->level_chain;
4328 b->level_chain = current_binding_level;
4329 current_binding_level = b;
4332 else
4333 pushclass (inner);
4336 /* Enter the scope INNER from current scope. INNER must be a scope
4337 nested inside current scope. This works with both name lookup and
4338 pushing name into scope. In case a template parameter scope is present,
4339 namespace is pushed under the template parameter scope according to
4340 name lookup rule in 14.6.1/6.
4342 Return the former current scope suitable for pop_inner_scope. */
4344 tree
4345 push_inner_scope (tree inner)
4347 tree outer = current_scope ();
4348 if (!outer)
4349 outer = current_namespace;
4351 push_inner_scope_r (outer, inner);
4352 return outer;
4355 /* Exit the current scope INNER back to scope OUTER. */
4357 void
4358 pop_inner_scope (tree outer, tree inner)
4360 if (outer == inner
4361 || (TREE_CODE (inner) != NAMESPACE_DECL && !CLASS_TYPE_P (inner)))
4362 return;
4364 while (outer != inner)
4366 if (TREE_CODE (inner) == NAMESPACE_DECL)
4368 cp_binding_level *save_template_parm = 0;
4369 /* Temporary take out template parameter scopes. They are saved
4370 in reversed order in save_template_parm. */
4371 while (current_binding_level->kind == sk_template_parms)
4373 cp_binding_level *b = current_binding_level;
4374 current_binding_level = b->level_chain;
4375 b->level_chain = save_template_parm;
4376 save_template_parm = b;
4379 pop_namespace ();
4381 /* Restore template parameter scopes. */
4382 while (save_template_parm)
4384 cp_binding_level *b = save_template_parm;
4385 save_template_parm = b->level_chain;
4386 b->level_chain = current_binding_level;
4387 current_binding_level = b;
4390 else
4391 popclass ();
4393 inner = CP_DECL_CONTEXT (TREE_CODE (inner) == NAMESPACE_DECL ? inner : TYPE_NAME (inner));
4397 /* Do a pushlevel for class declarations. */
4399 void
4400 pushlevel_class (void)
4402 class_binding_level = begin_scope (sk_class, current_class_type);
4405 /* ...and a poplevel for class declarations. */
4407 void
4408 poplevel_class (void)
4410 cp_binding_level *level = class_binding_level;
4411 cp_class_binding *cb;
4412 size_t i;
4413 tree shadowed;
4415 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4416 gcc_assert (level != 0);
4418 /* If we're leaving a toplevel class, cache its binding level. */
4419 if (current_class_depth == 1)
4420 previous_class_level = level;
4421 for (shadowed = level->type_shadowed;
4422 shadowed;
4423 shadowed = TREE_CHAIN (shadowed))
4424 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (shadowed), TREE_VALUE (shadowed));
4426 /* Remove the bindings for all of the class-level declarations. */
4427 if (level->class_shadowed)
4429 FOR_EACH_VEC_ELT (*level->class_shadowed, i, cb)
4431 IDENTIFIER_BINDING (cb->identifier) = cb->base->previous;
4432 cxx_binding_free (cb->base);
4434 ggc_free (level->class_shadowed);
4435 level->class_shadowed = NULL;
4438 /* Now, pop out of the binding level which we created up in the
4439 `pushlevel_class' routine. */
4440 gcc_assert (current_binding_level == level);
4441 leave_scope ();
4442 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4445 /* Set INHERITED_VALUE_BINDING_P on BINDING to true or false, as
4446 appropriate. DECL is the value to which a name has just been
4447 bound. CLASS_TYPE is the class in which the lookup occurred. */
4449 static void
4450 set_inherited_value_binding_p (cxx_binding *binding, tree decl,
4451 tree class_type)
4453 if (binding->value == decl && TREE_CODE (decl) != TREE_LIST)
4455 tree context;
4457 if (TREE_CODE (decl) == OVERLOAD)
4458 context = ovl_scope (decl);
4459 else
4461 gcc_assert (DECL_P (decl));
4462 context = context_for_name_lookup (decl);
4465 if (is_properly_derived_from (class_type, context))
4466 INHERITED_VALUE_BINDING_P (binding) = 1;
4467 else
4468 INHERITED_VALUE_BINDING_P (binding) = 0;
4470 else if (binding->value == decl)
4471 /* We only encounter a TREE_LIST when there is an ambiguity in the
4472 base classes. Such an ambiguity can be overridden by a
4473 definition in this class. */
4474 INHERITED_VALUE_BINDING_P (binding) = 1;
4475 else
4476 INHERITED_VALUE_BINDING_P (binding) = 0;
4479 /* Make the declaration of X appear in CLASS scope. */
4481 bool
4482 pushdecl_class_level (tree x)
4484 bool is_valid = true;
4485 bool subtime;
4487 /* Do nothing if we're adding to an outer lambda closure type,
4488 outer_binding will add it later if it's needed. */
4489 if (current_class_type != class_binding_level->this_entity)
4490 return true;
4492 subtime = timevar_cond_start (TV_NAME_LOOKUP);
4493 /* Get the name of X. */
4494 tree name = OVL_NAME (x);
4496 if (name)
4498 is_valid = push_class_level_binding (name, x);
4499 if (TREE_CODE (x) == TYPE_DECL)
4500 set_identifier_type_value (name, x);
4502 else if (ANON_AGGR_TYPE_P (TREE_TYPE (x)))
4504 /* If X is an anonymous aggregate, all of its members are
4505 treated as if they were members of the class containing the
4506 aggregate, for naming purposes. */
4507 tree f;
4509 for (f = TYPE_FIELDS (TREE_TYPE (x)); f; f = DECL_CHAIN (f))
4511 location_t save_location = input_location;
4512 input_location = DECL_SOURCE_LOCATION (f);
4513 if (!pushdecl_class_level (f))
4514 is_valid = false;
4515 input_location = save_location;
4518 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4519 return is_valid;
4522 /* Return the BINDING (if any) for NAME in SCOPE, which is a class
4523 scope. If the value returned is non-NULL, and the PREVIOUS field
4524 is not set, callers must set the PREVIOUS field explicitly. */
4526 static cxx_binding *
4527 get_class_binding (tree name, cp_binding_level *scope)
4529 tree class_type;
4530 tree type_binding;
4531 tree value_binding;
4532 cxx_binding *binding;
4534 class_type = scope->this_entity;
4536 /* Get the type binding. */
4537 type_binding = lookup_member (class_type, name,
4538 /*protect=*/2, /*want_type=*/true,
4539 tf_warning_or_error);
4540 /* Get the value binding. */
4541 value_binding = lookup_member (class_type, name,
4542 /*protect=*/2, /*want_type=*/false,
4543 tf_warning_or_error);
4545 if (value_binding
4546 && (TREE_CODE (value_binding) == TYPE_DECL
4547 || DECL_CLASS_TEMPLATE_P (value_binding)
4548 || (TREE_CODE (value_binding) == TREE_LIST
4549 && TREE_TYPE (value_binding) == error_mark_node
4550 && (TREE_CODE (TREE_VALUE (value_binding))
4551 == TYPE_DECL))))
4552 /* We found a type binding, even when looking for a non-type
4553 binding. This means that we already processed this binding
4554 above. */
4556 else if (value_binding)
4558 if (TREE_CODE (value_binding) == TREE_LIST
4559 && TREE_TYPE (value_binding) == error_mark_node)
4560 /* NAME is ambiguous. */
4562 else if (BASELINK_P (value_binding))
4563 /* NAME is some overloaded functions. */
4564 value_binding = BASELINK_FUNCTIONS (value_binding);
4567 /* If we found either a type binding or a value binding, create a
4568 new binding object. */
4569 if (type_binding || value_binding)
4571 binding = new_class_binding (name,
4572 value_binding,
4573 type_binding,
4574 scope);
4575 /* This is a class-scope binding, not a block-scope binding. */
4576 LOCAL_BINDING_P (binding) = 0;
4577 set_inherited_value_binding_p (binding, value_binding, class_type);
4579 else
4580 binding = NULL;
4582 return binding;
4585 /* Make the declaration(s) of X appear in CLASS scope under the name
4586 NAME. Returns true if the binding is valid. */
4588 static bool
4589 push_class_level_binding_1 (tree name, tree x)
4591 cxx_binding *binding;
4592 tree decl = x;
4593 bool ok;
4595 /* The class_binding_level will be NULL if x is a template
4596 parameter name in a member template. */
4597 if (!class_binding_level)
4598 return true;
4600 if (name == error_mark_node)
4601 return false;
4603 /* Can happen for an erroneous declaration (c++/60384). */
4604 if (!identifier_p (name))
4606 gcc_assert (errorcount || sorrycount);
4607 return false;
4610 /* Check for invalid member names. But don't worry about a default
4611 argument-scope lambda being pushed after the class is complete. */
4612 gcc_assert (TYPE_BEING_DEFINED (current_class_type)
4613 || LAMBDA_TYPE_P (TREE_TYPE (decl)));
4614 /* Check that we're pushing into the right binding level. */
4615 gcc_assert (current_class_type == class_binding_level->this_entity);
4617 /* We could have been passed a tree list if this is an ambiguous
4618 declaration. If so, pull the declaration out because
4619 check_template_shadow will not handle a TREE_LIST. */
4620 if (TREE_CODE (decl) == TREE_LIST
4621 && TREE_TYPE (decl) == error_mark_node)
4622 decl = TREE_VALUE (decl);
4624 if (!check_template_shadow (decl))
4625 return false;
4627 /* [class.mem]
4629 If T is the name of a class, then each of the following shall
4630 have a name different from T:
4632 -- every static data member of class T;
4634 -- every member of class T that is itself a type;
4636 -- every enumerator of every member of class T that is an
4637 enumerated type;
4639 -- every member of every anonymous union that is a member of
4640 class T.
4642 (Non-static data members were also forbidden to have the same
4643 name as T until TC1.) */
4644 if ((VAR_P (x)
4645 || TREE_CODE (x) == CONST_DECL
4646 || (TREE_CODE (x) == TYPE_DECL
4647 && !DECL_SELF_REFERENCE_P (x))
4648 /* A data member of an anonymous union. */
4649 || (TREE_CODE (x) == FIELD_DECL
4650 && DECL_CONTEXT (x) != current_class_type))
4651 && DECL_NAME (x) == DECL_NAME (TYPE_NAME (current_class_type)))
4653 tree scope = context_for_name_lookup (x);
4654 if (TYPE_P (scope) && same_type_p (scope, current_class_type))
4656 error ("%qD has the same name as the class in which it is "
4657 "declared",
4659 return false;
4663 /* Get the current binding for NAME in this class, if any. */
4664 binding = IDENTIFIER_BINDING (name);
4665 if (!binding || binding->scope != class_binding_level)
4667 binding = get_class_binding (name, class_binding_level);
4668 /* If a new binding was created, put it at the front of the
4669 IDENTIFIER_BINDING list. */
4670 if (binding)
4672 binding->previous = IDENTIFIER_BINDING (name);
4673 IDENTIFIER_BINDING (name) = binding;
4677 /* If there is already a binding, then we may need to update the
4678 current value. */
4679 if (binding && binding->value)
4681 tree bval = binding->value;
4682 tree old_decl = NULL_TREE;
4683 tree target_decl = strip_using_decl (decl);
4684 tree target_bval = strip_using_decl (bval);
4686 if (INHERITED_VALUE_BINDING_P (binding))
4688 /* If the old binding was from a base class, and was for a
4689 tag name, slide it over to make room for the new binding.
4690 The old binding is still visible if explicitly qualified
4691 with a class-key. */
4692 if (TREE_CODE (target_bval) == TYPE_DECL
4693 && DECL_ARTIFICIAL (target_bval)
4694 && !(TREE_CODE (target_decl) == TYPE_DECL
4695 && DECL_ARTIFICIAL (target_decl)))
4697 old_decl = binding->type;
4698 binding->type = bval;
4699 binding->value = NULL_TREE;
4700 INHERITED_VALUE_BINDING_P (binding) = 0;
4702 else
4704 old_decl = bval;
4705 /* Any inherited type declaration is hidden by the type
4706 declaration in the derived class. */
4707 if (TREE_CODE (target_decl) == TYPE_DECL
4708 && DECL_ARTIFICIAL (target_decl))
4709 binding->type = NULL_TREE;
4712 else if (TREE_CODE (target_decl) == OVERLOAD
4713 && OVL_P (target_bval))
4714 old_decl = bval;
4715 else if (TREE_CODE (decl) == USING_DECL
4716 && TREE_CODE (bval) == USING_DECL
4717 && same_type_p (USING_DECL_SCOPE (decl),
4718 USING_DECL_SCOPE (bval)))
4719 /* This is a using redeclaration that will be diagnosed later
4720 in supplement_binding */
4722 else if (TREE_CODE (decl) == USING_DECL
4723 && TREE_CODE (bval) == USING_DECL
4724 && DECL_DEPENDENT_P (decl)
4725 && DECL_DEPENDENT_P (bval))
4726 return true;
4727 else if (TREE_CODE (decl) == USING_DECL
4728 && OVL_P (target_bval))
4729 old_decl = bval;
4730 else if (TREE_CODE (bval) == USING_DECL
4731 && OVL_P (target_decl))
4732 return true;
4734 if (old_decl && binding->scope == class_binding_level)
4736 binding->value = x;
4737 /* It is always safe to clear INHERITED_VALUE_BINDING_P
4738 here. This function is only used to register bindings
4739 from with the class definition itself. */
4740 INHERITED_VALUE_BINDING_P (binding) = 0;
4741 return true;
4745 /* Note that we declared this value so that we can issue an error if
4746 this is an invalid redeclaration of a name already used for some
4747 other purpose. */
4748 note_name_declared_in_class (name, decl);
4750 /* If we didn't replace an existing binding, put the binding on the
4751 stack of bindings for the identifier, and update the shadowed
4752 list. */
4753 if (binding && binding->scope == class_binding_level)
4754 /* Supplement the existing binding. */
4755 ok = supplement_binding (binding, decl);
4756 else
4758 /* Create a new binding. */
4759 push_binding (name, decl, class_binding_level);
4760 ok = true;
4763 return ok;
4766 /* Wrapper for push_class_level_binding_1. */
4768 bool
4769 push_class_level_binding (tree name, tree x)
4771 bool ret;
4772 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4773 ret = push_class_level_binding_1 (name, x);
4774 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4775 return ret;
4778 /* Process "using SCOPE::NAME" in a class scope. Return the
4779 USING_DECL created. */
4781 tree
4782 do_class_using_decl (tree scope, tree name)
4784 if (name == error_mark_node)
4785 return NULL_TREE;
4787 if (!scope || !TYPE_P (scope))
4789 error ("using-declaration for non-member at class scope");
4790 return NULL_TREE;
4793 /* Make sure the name is not invalid */
4794 if (TREE_CODE (name) == BIT_NOT_EXPR)
4796 error ("%<%T::%D%> names destructor", scope, name);
4797 return NULL_TREE;
4800 /* Using T::T declares inheriting ctors, even if T is a typedef. */
4801 if (MAYBE_CLASS_TYPE_P (scope)
4802 && (name == TYPE_IDENTIFIER (scope)
4803 || constructor_name_p (name, scope)))
4805 maybe_warn_cpp0x (CPP0X_INHERITING_CTORS);
4806 name = ctor_identifier;
4807 CLASSTYPE_NON_AGGREGATE (current_class_type) = true;
4810 /* Cannot introduce a constructor name. */
4811 if (constructor_name_p (name, current_class_type))
4813 error ("%<%T::%D%> names constructor in %qT",
4814 scope, name, current_class_type);
4815 return NULL_TREE;
4818 /* From [namespace.udecl]:
4820 A using-declaration used as a member-declaration shall refer to a
4821 member of a base class of the class being defined.
4823 In general, we cannot check this constraint in a template because
4824 we do not know the entire set of base classes of the current
4825 class type. Morover, if SCOPE is dependent, it might match a
4826 non-dependent base. */
4828 tree decl = NULL_TREE;
4829 if (!dependent_scope_p (scope))
4831 base_kind b_kind;
4832 tree binfo = lookup_base (current_class_type, scope, ba_any, &b_kind,
4833 tf_warning_or_error);
4834 if (b_kind < bk_proper_base)
4836 /* If there are dependent bases, scope might resolve at
4837 instantiation time, even if it isn't exactly one of the
4838 dependent bases. */
4839 if (b_kind == bk_same_type || !any_dependent_bases_p ())
4841 error_not_base_type (scope, current_class_type);
4842 return NULL_TREE;
4845 else if (name == ctor_identifier && !binfo_direct_p (binfo))
4847 error ("cannot inherit constructors from indirect base %qT", scope);
4848 return NULL_TREE;
4850 else if (!IDENTIFIER_CONV_OP_P (name)
4851 || !dependent_type_p (TREE_TYPE (name)))
4853 decl = lookup_member (binfo, name, 0, false, tf_warning_or_error);
4854 if (!decl)
4856 error ("no members matching %<%T::%D%> in %q#T", scope, name,
4857 scope);
4858 return NULL_TREE;
4861 /* The binfo from which the functions came does not matter. */
4862 if (BASELINK_P (decl))
4863 decl = BASELINK_FUNCTIONS (decl);
4867 tree value = build_lang_decl (USING_DECL, name, NULL_TREE);
4868 USING_DECL_DECLS (value) = decl;
4869 USING_DECL_SCOPE (value) = scope;
4870 DECL_DEPENDENT_P (value) = !decl;
4872 return value;
4876 /* Return the binding for NAME in NS. If NS is NULL, look in
4877 global_namespace. */
4879 tree
4880 get_namespace_binding (tree ns, tree name)
4882 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4883 if (!ns)
4884 ns = global_namespace;
4885 gcc_checking_assert (!DECL_NAMESPACE_ALIAS (ns));
4886 tree ret = find_namespace_value (ns, name);
4887 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4888 return ret;
4891 /* Push internal DECL into the global namespace. Does not do the
4892 full overload fn handling and does not add it to the list of things
4893 in the namespace. */
4895 void
4896 set_global_binding (tree decl)
4898 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4900 tree *slot = find_namespace_slot (global_namespace, DECL_NAME (decl), true);
4902 if (*slot)
4903 /* The user's placed something in the implementor's namespace. */
4904 diagnose_name_conflict (decl, MAYBE_STAT_DECL (*slot));
4906 /* Force the binding, so compiler internals continue to work. */
4907 *slot = decl;
4909 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4912 /* Set the context of a declaration to scope. Complain if we are not
4913 outside scope. */
4915 void
4916 set_decl_namespace (tree decl, tree scope, bool friendp)
4918 /* Get rid of namespace aliases. */
4919 scope = ORIGINAL_NAMESPACE (scope);
4921 /* It is ok for friends to be qualified in parallel space. */
4922 if (!friendp && !is_nested_namespace (current_namespace, scope))
4923 error ("declaration of %qD not in a namespace surrounding %qD",
4924 decl, scope);
4925 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
4927 /* See whether this has been declared in the namespace or inline
4928 children. */
4929 tree old = NULL_TREE;
4931 name_lookup lookup (DECL_NAME (decl), LOOKUP_HIDDEN);
4932 if (!lookup.search_qualified (scope, /*usings=*/false))
4933 /* No old declaration at all. */
4934 goto not_found;
4935 old = lookup.value;
4938 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
4939 if (TREE_CODE (old) == TREE_LIST)
4941 ambiguous:
4942 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
4943 error ("reference to %qD is ambiguous", decl);
4944 print_candidates (old);
4945 return;
4948 if (!DECL_DECLARES_FUNCTION_P (decl))
4950 /* Don't compare non-function decls with decls_match here, since
4951 it can't check for the correct constness at this
4952 point. pushdecl will find those errors later. */
4954 /* We might have found it in an inline namespace child of SCOPE. */
4955 if (TREE_CODE (decl) == TREE_CODE (old))
4956 DECL_CONTEXT (decl) = DECL_CONTEXT (old);
4958 found:
4959 /* Writing "N::i" to declare something directly in "N" is invalid. */
4960 if (CP_DECL_CONTEXT (decl) == current_namespace
4961 && at_namespace_scope_p ())
4962 error ("explicit qualification in declaration of %qD", decl);
4963 return;
4966 /* Since decl is a function, old should contain a function decl. */
4967 if (!OVL_P (old))
4968 goto not_found;
4970 /* We handle these in check_explicit_instantiation_namespace. */
4971 if (processing_explicit_instantiation)
4972 return;
4973 if (processing_template_decl || processing_specialization)
4974 /* We have not yet called push_template_decl to turn a
4975 FUNCTION_DECL into a TEMPLATE_DECL, so the declarations won't
4976 match. But, we'll check later, when we construct the
4977 template. */
4978 return;
4979 /* Instantiations or specializations of templates may be declared as
4980 friends in any namespace. */
4981 if (friendp && DECL_USE_TEMPLATE (decl))
4982 return;
4984 tree found;
4985 found = NULL_TREE;
4987 for (lkp_iterator iter (old); iter; ++iter)
4989 if (iter.using_p ())
4990 continue;
4992 tree ofn = *iter;
4994 /* Adjust DECL_CONTEXT first so decls_match will return true
4995 if DECL will match a declaration in an inline namespace. */
4996 DECL_CONTEXT (decl) = DECL_CONTEXT (ofn);
4997 if (decls_match (decl, ofn))
4999 if (found)
5001 /* We found more than one matching declaration. */
5002 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
5003 goto ambiguous;
5005 found = ofn;
5009 if (found)
5011 if (DECL_HIDDEN_FRIEND_P (found))
5013 pedwarn (DECL_SOURCE_LOCATION (decl), 0,
5014 "%qD has not been declared within %qD", decl, scope);
5015 inform (DECL_SOURCE_LOCATION (found),
5016 "only here as a %<friend%>");
5018 DECL_CONTEXT (decl) = DECL_CONTEXT (found);
5019 goto found;
5022 not_found:
5023 /* It didn't work, go back to the explicit scope. */
5024 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
5025 error ("%qD should have been declared inside %qD", decl, scope);
5028 /* Return the namespace where the current declaration is declared. */
5030 tree
5031 current_decl_namespace (void)
5033 tree result;
5034 /* If we have been pushed into a different namespace, use it. */
5035 if (!vec_safe_is_empty (decl_namespace_list))
5036 return decl_namespace_list->last ();
5038 if (current_class_type)
5039 result = decl_namespace_context (current_class_type);
5040 else if (current_function_decl)
5041 result = decl_namespace_context (current_function_decl);
5042 else
5043 result = current_namespace;
5044 return result;
5047 /* Process any ATTRIBUTES on a namespace definition. Returns true if
5048 attribute visibility is seen. */
5050 bool
5051 handle_namespace_attrs (tree ns, tree attributes)
5053 tree d;
5054 bool saw_vis = false;
5056 for (d = attributes; d; d = TREE_CHAIN (d))
5058 tree name = get_attribute_name (d);
5059 tree args = TREE_VALUE (d);
5061 if (is_attribute_p ("visibility", name))
5063 /* attribute visibility is a property of the syntactic block
5064 rather than the namespace as a whole, so we don't touch the
5065 NAMESPACE_DECL at all. */
5066 tree x = args ? TREE_VALUE (args) : NULL_TREE;
5067 if (x == NULL_TREE || TREE_CODE (x) != STRING_CST || TREE_CHAIN (args))
5069 warning (OPT_Wattributes,
5070 "%qD attribute requires a single NTBS argument",
5071 name);
5072 continue;
5075 if (!TREE_PUBLIC (ns))
5076 warning (OPT_Wattributes,
5077 "%qD attribute is meaningless since members of the "
5078 "anonymous namespace get local symbols", name);
5080 push_visibility (TREE_STRING_POINTER (x), 1);
5081 saw_vis = true;
5083 else if (is_attribute_p ("abi_tag", name))
5085 if (!DECL_NAME (ns))
5087 warning (OPT_Wattributes, "ignoring %qD attribute on anonymous "
5088 "namespace", name);
5089 continue;
5091 if (!DECL_NAMESPACE_INLINE_P (ns))
5093 warning (OPT_Wattributes, "ignoring %qD attribute on non-inline "
5094 "namespace", name);
5095 continue;
5097 if (!args)
5099 tree dn = DECL_NAME (ns);
5100 args = build_string (IDENTIFIER_LENGTH (dn) + 1,
5101 IDENTIFIER_POINTER (dn));
5102 TREE_TYPE (args) = char_array_type_node;
5103 args = fix_string_type (args);
5104 args = build_tree_list (NULL_TREE, args);
5106 if (check_abi_tag_args (args, name))
5107 DECL_ATTRIBUTES (ns) = tree_cons (name, args,
5108 DECL_ATTRIBUTES (ns));
5110 else
5112 warning (OPT_Wattributes, "%qD attribute directive ignored",
5113 name);
5114 continue;
5118 return saw_vis;
5121 /* Temporarily set the namespace for the current declaration. */
5123 void
5124 push_decl_namespace (tree decl)
5126 if (TREE_CODE (decl) != NAMESPACE_DECL)
5127 decl = decl_namespace_context (decl);
5128 vec_safe_push (decl_namespace_list, ORIGINAL_NAMESPACE (decl));
5131 /* [namespace.memdef]/2 */
5133 void
5134 pop_decl_namespace (void)
5136 decl_namespace_list->pop ();
5139 /* Process a namespace-alias declaration. */
5141 void
5142 do_namespace_alias (tree alias, tree name_space)
5144 if (name_space == error_mark_node)
5145 return;
5147 gcc_assert (TREE_CODE (name_space) == NAMESPACE_DECL);
5149 name_space = ORIGINAL_NAMESPACE (name_space);
5151 /* Build the alias. */
5152 alias = build_lang_decl (NAMESPACE_DECL, alias, void_type_node);
5153 DECL_NAMESPACE_ALIAS (alias) = name_space;
5154 DECL_EXTERNAL (alias) = 1;
5155 DECL_CONTEXT (alias) = FROB_CONTEXT (current_scope ());
5156 pushdecl (alias);
5158 /* Emit debug info for namespace alias. */
5159 if (!building_stmt_list_p ())
5160 (*debug_hooks->early_global_decl) (alias);
5163 /* Like pushdecl, only it places X in the current namespace,
5164 if appropriate. */
5166 tree
5167 pushdecl_namespace_level (tree x, bool is_friend)
5169 cp_binding_level *b = current_binding_level;
5170 tree t;
5172 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
5173 t = do_pushdecl_with_scope
5174 (x, NAMESPACE_LEVEL (current_namespace), is_friend);
5176 /* Now, the type_shadowed stack may screw us. Munge it so it does
5177 what we want. */
5178 if (TREE_CODE (t) == TYPE_DECL)
5180 tree name = DECL_NAME (t);
5181 tree newval;
5182 tree *ptr = (tree *)0;
5183 for (; !global_scope_p (b); b = b->level_chain)
5185 tree shadowed = b->type_shadowed;
5186 for (; shadowed; shadowed = TREE_CHAIN (shadowed))
5187 if (TREE_PURPOSE (shadowed) == name)
5189 ptr = &TREE_VALUE (shadowed);
5190 /* Can't break out of the loop here because sometimes
5191 a binding level will have duplicate bindings for
5192 PT names. It's gross, but I haven't time to fix it. */
5195 newval = TREE_TYPE (t);
5196 if (ptr == (tree *)0)
5198 /* @@ This shouldn't be needed. My test case "zstring.cc" trips
5199 up here if this is changed to an assertion. --KR */
5200 SET_IDENTIFIER_TYPE_VALUE (name, t);
5202 else
5204 *ptr = newval;
5207 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
5208 return t;
5211 /* Process a using-declaration appearing in namespace scope. */
5213 void
5214 finish_namespace_using_decl (tree decl, tree scope, tree name)
5216 tree orig_decl = decl;
5218 gcc_checking_assert (current_binding_level->kind == sk_namespace
5219 && !processing_template_decl);
5220 decl = validate_nonmember_using_decl (decl, scope, name);
5221 if (decl == NULL_TREE)
5222 return;
5224 tree *slot = find_namespace_slot (current_namespace, name, true);
5225 tree val = slot ? MAYBE_STAT_DECL (*slot) : NULL_TREE;
5226 tree type = slot ? MAYBE_STAT_TYPE (*slot) : NULL_TREE;
5227 do_nonmember_using_decl (scope, name, &val, &type);
5228 if (STAT_HACK_P (*slot))
5230 STAT_DECL (*slot) = val;
5231 STAT_TYPE (*slot) = type;
5233 else if (type)
5234 *slot = stat_hack (val, type);
5235 else
5236 *slot = val;
5238 /* Emit debug info. */
5239 cp_emit_debug_info_for_using (orig_decl, current_namespace);
5242 /* Process a using-declaration at function scope. */
5244 void
5245 finish_local_using_decl (tree decl, tree scope, tree name)
5247 tree orig_decl = decl;
5249 gcc_checking_assert (current_binding_level->kind != sk_class
5250 && current_binding_level->kind != sk_namespace);
5251 decl = validate_nonmember_using_decl (decl, scope, name);
5252 if (decl == NULL_TREE)
5253 return;
5255 add_decl_expr (decl);
5257 cxx_binding *binding = find_local_binding (current_binding_level, name);
5258 tree value = binding ? binding->value : NULL_TREE;
5259 tree type = binding ? binding->type : NULL_TREE;
5261 do_nonmember_using_decl (scope, name, &value, &type);
5263 if (!value)
5265 else if (binding && value == binding->value)
5267 else if (binding && binding->value && TREE_CODE (value) == OVERLOAD)
5269 update_local_overload (IDENTIFIER_BINDING (name), value);
5270 IDENTIFIER_BINDING (name)->value = value;
5272 else
5273 /* Install the new binding. */
5274 push_local_binding (name, value, true);
5276 if (!type)
5278 else if (binding && type == binding->type)
5280 else
5282 push_local_binding (name, type, true);
5283 set_identifier_type_value (name, type);
5286 /* Emit debug info. */
5287 if (!processing_template_decl)
5288 cp_emit_debug_info_for_using (orig_decl, current_scope ());
5291 /* Return the declarations that are members of the namespace NS. */
5293 tree
5294 cp_namespace_decls (tree ns)
5296 return NAMESPACE_LEVEL (ns)->names;
5299 /* Combine prefer_type and namespaces_only into flags. */
5301 static int
5302 lookup_flags (int prefer_type, int namespaces_only)
5304 if (namespaces_only)
5305 return LOOKUP_PREFER_NAMESPACES;
5306 if (prefer_type > 1)
5307 return LOOKUP_PREFER_TYPES;
5308 if (prefer_type > 0)
5309 return LOOKUP_PREFER_BOTH;
5310 return 0;
5313 /* Given a lookup that returned VAL, use FLAGS to decide if we want to
5314 ignore it or not. Subroutine of lookup_name_real and
5315 lookup_type_scope. */
5317 static bool
5318 qualify_lookup (tree val, int flags)
5320 if (val == NULL_TREE)
5321 return false;
5322 if ((flags & LOOKUP_PREFER_NAMESPACES) && TREE_CODE (val) == NAMESPACE_DECL)
5323 return true;
5324 if (flags & LOOKUP_PREFER_TYPES)
5326 tree target_val = strip_using_decl (val);
5327 if (TREE_CODE (target_val) == TYPE_DECL
5328 || TREE_CODE (target_val) == TEMPLATE_DECL)
5329 return true;
5331 if (flags & (LOOKUP_PREFER_NAMESPACES | LOOKUP_PREFER_TYPES))
5332 return false;
5333 /* Look through lambda things that we shouldn't be able to see. */
5334 if (!(flags & LOOKUP_HIDDEN) && is_lambda_ignored_entity (val))
5335 return false;
5336 return true;
5339 /* Suggest alternatives for NAME, an IDENTIFIER_NODE for which name
5340 lookup failed. Search through all available namespaces and print out
5341 possible candidates. If no exact matches are found, and
5342 SUGGEST_MISSPELLINGS is true, then also look for near-matches and
5343 suggest the best near-match, if there is one. */
5345 void
5346 suggest_alternatives_for (location_t location, tree name,
5347 bool suggest_misspellings)
5349 vec<tree> candidates = vNULL;
5350 vec<tree> worklist = vNULL;
5351 unsigned limit = PARAM_VALUE (CXX_MAX_NAMESPACES_FOR_DIAGNOSTIC_HELP);
5352 bool limited = false;
5354 /* Breadth-first search of namespaces. Up to limit namespaces
5355 searched (limit zero == unlimited). */
5356 worklist.safe_push (global_namespace);
5357 for (unsigned ix = 0; ix != worklist.length (); ix++)
5359 tree ns = worklist[ix];
5360 name_lookup lookup (name);
5362 if (lookup.search_qualified (ns, false))
5363 candidates.safe_push (lookup.value);
5365 if (!limited)
5367 /* Look for child namespaces. We have to do this
5368 indirectly because they are chained in reverse order,
5369 which is confusing to the user. */
5370 vec<tree> children = vNULL;
5372 for (tree decl = NAMESPACE_LEVEL (ns)->names;
5373 decl; decl = TREE_CHAIN (decl))
5374 if (TREE_CODE (decl) == NAMESPACE_DECL
5375 && !DECL_NAMESPACE_ALIAS (decl)
5376 && !DECL_NAMESPACE_INLINE_P (decl))
5377 children.safe_push (decl);
5379 while (!limited && !children.is_empty ())
5381 if (worklist.length () == limit)
5383 /* Unconditionally warn that the search was truncated. */
5384 inform (location,
5385 "maximum limit of %d namespaces searched for %qE",
5386 limit, name);
5387 limited = true;
5389 else
5390 worklist.safe_push (children.pop ());
5392 children.release ();
5395 worklist.release ();
5397 if (candidates.length ())
5399 inform_n (location, candidates.length (),
5400 "suggested alternative:",
5401 "suggested alternatives:");
5402 for (unsigned ix = 0; ix != candidates.length (); ix++)
5404 tree val = candidates[ix];
5406 inform (location_of (val), " %qE", val);
5408 candidates.release ();
5410 else if (!suggest_misspellings)
5412 else if (name_hint hint = lookup_name_fuzzy (name, FUZZY_LOOKUP_NAME,
5413 location))
5415 /* Show a spelling correction. */
5416 gcc_rich_location richloc (location);
5418 richloc.add_fixit_replace (hint.suggestion ());
5419 inform (&richloc, "suggested alternative: %qs", hint.suggestion ());
5423 /* Subroutine of maybe_suggest_missing_header for handling unrecognized names
5424 for some of the most common names within "std::".
5425 Given non-NULL NAME, a name for lookup within "std::", return the header
5426 name defining it within the C++ Standard Library (with '<' and '>'),
5427 or NULL. */
5429 static const char *
5430 get_std_name_hint (const char *name)
5432 struct std_name_hint
5434 const char *name;
5435 const char *header;
5437 static const std_name_hint hints[] = {
5438 /* <array>. */
5439 {"array", "<array>"}, // C++11
5440 /* <complex>. */
5441 {"complex", "<complex>"},
5442 {"complex_literals", "<complex>"},
5443 /* <deque>. */
5444 {"deque", "<deque>"},
5445 /* <forward_list>. */
5446 {"forward_list", "<forward_list>"}, // C++11
5447 /* <fstream>. */
5448 {"basic_filebuf", "<fstream>"},
5449 {"basic_ifstream", "<fstream>"},
5450 {"basic_ofstream", "<fstream>"},
5451 {"basic_fstream", "<fstream>"},
5452 /* <iostream>. */
5453 {"cin", "<iostream>"},
5454 {"cout", "<iostream>"},
5455 {"cerr", "<iostream>"},
5456 {"clog", "<iostream>"},
5457 {"wcin", "<iostream>"},
5458 {"wcout", "<iostream>"},
5459 {"wclog", "<iostream>"},
5460 /* <list>. */
5461 {"list", "<list>"},
5462 /* <map>. */
5463 {"map", "<map>"},
5464 {"multimap", "<map>"},
5465 /* <queue>. */
5466 {"queue", "<queue>"},
5467 {"priority_queue", "<queue>"},
5468 /* <ostream>. */
5469 {"ostream", "<ostream>"},
5470 {"wostream", "<ostream>"},
5471 {"ends", "<ostream>"},
5472 {"flush", "<ostream>"},
5473 {"endl", "<ostream>"},
5474 /* <set>. */
5475 {"set", "<set>"},
5476 {"multiset", "<set>"},
5477 /* <sstream>. */
5478 {"basic_stringbuf", "<sstream>"},
5479 {"basic_istringstream", "<sstream>"},
5480 {"basic_ostringstream", "<sstream>"},
5481 {"basic_stringstream", "<sstream>"},
5482 /* <stack>. */
5483 {"stack", "<stack>"},
5484 /* <string>. */
5485 {"string", "<string>"},
5486 {"wstring", "<string>"},
5487 {"u16string", "<string>"},
5488 {"u32string", "<string>"},
5489 /* <unordered_map>. */
5490 {"unordered_map", "<unordered_map>"}, // C++11
5491 {"unordered_multimap", "<unordered_map>"}, // C++11
5492 /* <unordered_set>. */
5493 {"unordered_set", "<unordered_set>"}, // C++11
5494 {"unordered_multiset", "<unordered_set>"}, // C++11
5495 /* <vector>. */
5496 {"vector", "<vector>"},
5498 const size_t num_hints = sizeof (hints) / sizeof (hints[0]);
5499 for (size_t i = 0; i < num_hints; i++)
5501 if (strcmp (name, hints[i].name) == 0)
5502 return hints[i].header;
5504 return NULL;
5507 /* If SCOPE is the "std" namespace, then suggest pertinent header
5508 files for NAME at LOCATION.
5509 Return true iff a suggestion was offered. */
5511 static bool
5512 maybe_suggest_missing_header (location_t location, tree name, tree scope)
5514 if (scope == NULL_TREE)
5515 return false;
5516 if (TREE_CODE (scope) != NAMESPACE_DECL)
5517 return false;
5518 /* We only offer suggestions for the "std" namespace. */
5519 if (scope != std_node)
5520 return false;
5521 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
5523 const char *name_str = IDENTIFIER_POINTER (name);
5524 const char *header_hint = get_std_name_hint (name_str);
5525 if (!header_hint)
5526 return false;
5528 gcc_rich_location richloc (location);
5529 maybe_add_include_fixit (&richloc, header_hint);
5530 inform (&richloc,
5531 "%<std::%s%> is defined in header %qs;"
5532 " did you forget to %<#include %s%>?",
5533 name_str, header_hint, header_hint);
5534 return true;
5537 /* Look for alternatives for NAME, an IDENTIFIER_NODE for which name
5538 lookup failed within the explicitly provided SCOPE. Suggest the
5539 the best meaningful candidates (if any) as a fix-it hint.
5540 Return true iff a suggestion was provided. */
5542 bool
5543 suggest_alternative_in_explicit_scope (location_t location, tree name,
5544 tree scope)
5546 /* Something went very wrong; don't suggest anything. */
5547 if (name == error_mark_node)
5548 return false;
5550 /* Resolve any namespace aliases. */
5551 scope = ORIGINAL_NAMESPACE (scope);
5553 if (maybe_suggest_missing_header (location, name, scope))
5554 return true;
5556 cp_binding_level *level = NAMESPACE_LEVEL (scope);
5558 best_match <tree, const char *> bm (name);
5559 consider_binding_level (name, bm, level, false, FUZZY_LOOKUP_NAME);
5561 /* See if we have a good suggesion for the user. */
5562 const char *fuzzy_name = bm.get_best_meaningful_candidate ();
5563 if (fuzzy_name)
5565 gcc_rich_location richloc (location);
5566 richloc.add_fixit_replace (fuzzy_name);
5567 inform (&richloc, "suggested alternative: %qs",
5568 fuzzy_name);
5569 return true;
5572 return false;
5575 /* Look up NAME (an IDENTIFIER_NODE) in SCOPE (either a NAMESPACE_DECL
5576 or a class TYPE).
5578 If PREFER_TYPE is > 0, we only return TYPE_DECLs or namespaces.
5579 If PREFER_TYPE is > 1, we only return TYPE_DECLs.
5581 Returns a DECL (or OVERLOAD, or BASELINK) representing the
5582 declaration found. If no suitable declaration can be found,
5583 ERROR_MARK_NODE is returned. If COMPLAIN is true and SCOPE is
5584 neither a class-type nor a namespace a diagnostic is issued. */
5586 tree
5587 lookup_qualified_name (tree scope, tree name, int prefer_type, bool complain,
5588 bool find_hidden)
5590 tree t = NULL_TREE;
5592 if (TREE_CODE (scope) == NAMESPACE_DECL)
5594 int flags = lookup_flags (prefer_type, /*namespaces_only*/false);
5595 if (find_hidden)
5596 flags |= LOOKUP_HIDDEN;
5597 name_lookup lookup (name, flags);
5599 if (qualified_namespace_lookup (scope, &lookup))
5600 t = lookup.value;
5602 else if (cxx_dialect != cxx98 && TREE_CODE (scope) == ENUMERAL_TYPE)
5603 t = lookup_enumerator (scope, name);
5604 else if (is_class_type (scope, complain))
5605 t = lookup_member (scope, name, 2, prefer_type, tf_warning_or_error);
5607 if (!t)
5608 return error_mark_node;
5609 return t;
5612 /* [namespace.qual]
5613 Accepts the NAME to lookup and its qualifying SCOPE.
5614 Returns the name/type pair found into the cxx_binding *RESULT,
5615 or false on error. */
5617 static bool
5618 qualified_namespace_lookup (tree scope, name_lookup *lookup)
5620 timevar_start (TV_NAME_LOOKUP);
5621 query_oracle (lookup->name);
5622 bool found = lookup->search_qualified (ORIGINAL_NAMESPACE (scope));
5623 timevar_stop (TV_NAME_LOOKUP);
5624 return found;
5627 /* Helper function for lookup_name_fuzzy.
5628 Traverse binding level LVL, looking for good name matches for NAME
5629 (and BM). */
5630 static void
5631 consider_binding_level (tree name, best_match <tree, const char *> &bm,
5632 cp_binding_level *lvl, bool look_within_fields,
5633 enum lookup_name_fuzzy_kind kind)
5635 if (look_within_fields)
5636 if (lvl->this_entity && TREE_CODE (lvl->this_entity) == RECORD_TYPE)
5638 tree type = lvl->this_entity;
5639 bool want_type_p = (kind == FUZZY_LOOKUP_TYPENAME);
5640 tree best_matching_field
5641 = lookup_member_fuzzy (type, name, want_type_p);
5642 if (best_matching_field)
5643 bm.consider (IDENTIFIER_POINTER (best_matching_field));
5646 /* Only suggest names reserved for the implementation if NAME begins
5647 with an underscore. */
5648 bool consider_implementation_names = (IDENTIFIER_POINTER (name)[0] == '_');
5650 for (tree t = lvl->names; t; t = TREE_CHAIN (t))
5652 tree d = t;
5654 /* OVERLOADs or decls from using declaration are wrapped into
5655 TREE_LIST. */
5656 if (TREE_CODE (d) == TREE_LIST)
5657 d = OVL_FIRST (TREE_VALUE (d));
5659 /* Don't use bindings from implicitly declared functions,
5660 as they were likely misspellings themselves. */
5661 if (TREE_TYPE (d) == error_mark_node)
5662 continue;
5664 /* Skip anticipated decls of builtin functions. */
5665 if (TREE_CODE (d) == FUNCTION_DECL
5666 && DECL_BUILT_IN (d)
5667 && DECL_ANTICIPATED (d))
5668 continue;
5670 tree suggestion = DECL_NAME (d);
5671 if (!suggestion)
5672 continue;
5674 const char *suggestion_str = IDENTIFIER_POINTER (suggestion);
5676 /* Ignore internal names with spaces in them. */
5677 if (strchr (suggestion_str, ' '))
5678 continue;
5680 /* Don't suggest names that are reserved for use by the
5681 implementation, unless NAME began with an underscore. */
5682 if (name_reserved_for_implementation_p (suggestion_str)
5683 && !consider_implementation_names)
5684 continue;
5686 bm.consider (suggestion_str);
5690 /* Subclass of deferred_diagnostic. Notify the user that the
5691 given macro was used before it was defined.
5692 This can be done in the C++ frontend since tokenization happens
5693 upfront. */
5695 class macro_use_before_def : public deferred_diagnostic
5697 public:
5698 /* Ctor. LOC is the location of the usage. MACRO is the
5699 macro that was used. */
5700 macro_use_before_def (location_t loc, cpp_hashnode *macro)
5701 : deferred_diagnostic (loc), m_macro (macro)
5703 gcc_assert (macro);
5706 ~macro_use_before_def ()
5708 if (is_suppressed_p ())
5709 return;
5711 source_location def_loc = cpp_macro_definition_location (m_macro);
5712 if (def_loc != UNKNOWN_LOCATION)
5714 inform (get_location (), "the macro %qs had not yet been defined",
5715 (const char *)m_macro->ident.str);
5716 inform (def_loc, "it was later defined here");
5720 private:
5721 cpp_hashnode *m_macro;
5724 /* Determine if it can ever make sense to offer RID as a suggestion for
5725 a misspelling.
5727 Subroutine of lookup_name_fuzzy. */
5729 static bool
5730 suggest_rid_p (enum rid rid)
5732 switch (rid)
5734 /* Support suggesting function-like keywords. */
5735 case RID_STATIC_ASSERT:
5736 return true;
5738 default:
5739 /* Support suggesting the various decl-specifier words, to handle
5740 e.g. "singed" vs "signed" typos. */
5741 if (cp_keyword_starts_decl_specifier_p (rid))
5742 return true;
5744 /* Otherwise, don't offer it. This avoids suggesting e.g. "if"
5745 and "do" for short misspellings, which are likely to lead to
5746 nonsensical results. */
5747 return false;
5751 /* Search for near-matches for NAME within the current bindings, and within
5752 macro names, returning the best match as a const char *, or NULL if
5753 no reasonable match is found.
5755 Use LOC for any deferred diagnostics. */
5757 name_hint
5758 lookup_name_fuzzy (tree name, enum lookup_name_fuzzy_kind kind, location_t loc)
5760 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
5762 /* First, try some well-known names in the C++ standard library, in case
5763 the user forgot a #include. */
5764 const char *header_hint
5765 = get_cp_stdlib_header_for_name (IDENTIFIER_POINTER (name));
5766 if (header_hint)
5767 return name_hint (NULL,
5768 new suggest_missing_header (loc,
5769 IDENTIFIER_POINTER (name),
5770 header_hint));
5772 best_match <tree, const char *> bm (name);
5774 cp_binding_level *lvl;
5775 for (lvl = scope_chain->class_bindings; lvl; lvl = lvl->level_chain)
5776 consider_binding_level (name, bm, lvl, true, kind);
5778 for (lvl = current_binding_level; lvl; lvl = lvl->level_chain)
5779 consider_binding_level (name, bm, lvl, false, kind);
5781 /* Consider macros: if the user misspelled a macro name e.g. "SOME_MACRO"
5783 x = SOME_OTHER_MACRO (y);
5784 then "SOME_OTHER_MACRO" will survive to the frontend and show up
5785 as a misspelled identifier.
5787 Use the best distance so far so that a candidate is only set if
5788 a macro is better than anything so far. This allows early rejection
5789 (without calculating the edit distance) of macro names that must have
5790 distance >= bm.get_best_distance (), and means that we only get a
5791 non-NULL result for best_macro_match if it's better than any of
5792 the identifiers already checked. */
5793 best_macro_match bmm (name, bm.get_best_distance (), parse_in);
5794 cpp_hashnode *best_macro = bmm.get_best_meaningful_candidate ();
5795 /* If a macro is the closest so far to NAME, consider it. */
5796 if (best_macro)
5797 bm.consider ((const char *)best_macro->ident.str);
5798 else if (bmm.get_best_distance () == 0)
5800 /* If we have an exact match for a macro name, then the
5801 macro has been used before it was defined. */
5802 cpp_hashnode *macro = bmm.blithely_get_best_candidate ();
5803 if (macro && (macro->flags & NODE_BUILTIN) == 0)
5804 return name_hint (NULL,
5805 new macro_use_before_def (loc, macro));
5808 /* Try the "starts_decl_specifier_p" keywords to detect
5809 "singed" vs "signed" typos. */
5810 for (unsigned i = 0; i < num_c_common_reswords; i++)
5812 const c_common_resword *resword = &c_common_reswords[i];
5814 if (!suggest_rid_p (resword->rid))
5815 continue;
5817 tree resword_identifier = ridpointers [resword->rid];
5818 if (!resword_identifier)
5819 continue;
5820 gcc_assert (TREE_CODE (resword_identifier) == IDENTIFIER_NODE);
5822 /* Only consider reserved words that survived the
5823 filtering in init_reswords (e.g. for -std). */
5824 if (!IDENTIFIER_KEYWORD_P (resword_identifier))
5825 continue;
5827 bm.consider (IDENTIFIER_POINTER (resword_identifier));
5830 return name_hint (bm.get_best_meaningful_candidate (), NULL);
5833 /* Subroutine of outer_binding.
5835 Returns TRUE if BINDING is a binding to a template parameter of
5836 SCOPE. In that case SCOPE is the scope of a primary template
5837 parameter -- in the sense of G++, i.e, a template that has its own
5838 template header.
5840 Returns FALSE otherwise. */
5842 static bool
5843 binding_to_template_parms_of_scope_p (cxx_binding *binding,
5844 cp_binding_level *scope)
5846 tree binding_value, tmpl, tinfo;
5847 int level;
5849 if (!binding || !scope || !scope->this_entity)
5850 return false;
5852 binding_value = binding->value ? binding->value : binding->type;
5853 tinfo = get_template_info (scope->this_entity);
5855 /* BINDING_VALUE must be a template parm. */
5856 if (binding_value == NULL_TREE
5857 || (!DECL_P (binding_value)
5858 || !DECL_TEMPLATE_PARM_P (binding_value)))
5859 return false;
5861 /* The level of BINDING_VALUE. */
5862 level =
5863 template_type_parameter_p (binding_value)
5864 ? TEMPLATE_PARM_LEVEL (TEMPLATE_TYPE_PARM_INDEX
5865 (TREE_TYPE (binding_value)))
5866 : TEMPLATE_PARM_LEVEL (DECL_INITIAL (binding_value));
5868 /* The template of the current scope, iff said scope is a primary
5869 template. */
5870 tmpl = (tinfo
5871 && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
5872 ? TI_TEMPLATE (tinfo)
5873 : NULL_TREE);
5875 /* If the level of the parm BINDING_VALUE equals the depth of TMPL,
5876 then BINDING_VALUE is a parameter of TMPL. */
5877 return (tmpl && level == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)));
5880 /* Return the innermost non-namespace binding for NAME from a scope
5881 containing BINDING, or, if BINDING is NULL, the current scope.
5882 Please note that for a given template, the template parameters are
5883 considered to be in the scope containing the current scope.
5884 If CLASS_P is false, then class bindings are ignored. */
5886 cxx_binding *
5887 outer_binding (tree name,
5888 cxx_binding *binding,
5889 bool class_p)
5891 cxx_binding *outer;
5892 cp_binding_level *scope;
5893 cp_binding_level *outer_scope;
5895 if (binding)
5897 scope = binding->scope->level_chain;
5898 outer = binding->previous;
5900 else
5902 scope = current_binding_level;
5903 outer = IDENTIFIER_BINDING (name);
5905 outer_scope = outer ? outer->scope : NULL;
5907 /* Because we create class bindings lazily, we might be missing a
5908 class binding for NAME. If there are any class binding levels
5909 between the LAST_BINDING_LEVEL and the scope in which OUTER was
5910 declared, we must lookup NAME in those class scopes. */
5911 if (class_p)
5912 while (scope && scope != outer_scope && scope->kind != sk_namespace)
5914 if (scope->kind == sk_class)
5916 cxx_binding *class_binding;
5918 class_binding = get_class_binding (name, scope);
5919 if (class_binding)
5921 /* Thread this new class-scope binding onto the
5922 IDENTIFIER_BINDING list so that future lookups
5923 find it quickly. */
5924 class_binding->previous = outer;
5925 if (binding)
5926 binding->previous = class_binding;
5927 else
5928 IDENTIFIER_BINDING (name) = class_binding;
5929 return class_binding;
5932 /* If we are in a member template, the template parms of the member
5933 template are considered to be inside the scope of the containing
5934 class, but within G++ the class bindings are all pushed between the
5935 template parms and the function body. So if the outer binding is
5936 a template parm for the current scope, return it now rather than
5937 look for a class binding. */
5938 if (outer_scope && outer_scope->kind == sk_template_parms
5939 && binding_to_template_parms_of_scope_p (outer, scope))
5940 return outer;
5942 scope = scope->level_chain;
5945 return outer;
5948 /* Return the innermost block-scope or class-scope value binding for
5949 NAME, or NULL_TREE if there is no such binding. */
5951 tree
5952 innermost_non_namespace_value (tree name)
5954 cxx_binding *binding;
5955 binding = outer_binding (name, /*binding=*/NULL, /*class_p=*/true);
5956 return binding ? binding->value : NULL_TREE;
5959 /* Look up NAME in the current binding level and its superiors in the
5960 namespace of variables, functions and typedefs. Return a ..._DECL
5961 node of some kind representing its definition if there is only one
5962 such declaration, or return a TREE_LIST with all the overloaded
5963 definitions if there are many, or return 0 if it is undefined.
5964 Hidden name, either friend declaration or built-in function, are
5965 not ignored.
5967 If PREFER_TYPE is > 0, we prefer TYPE_DECLs or namespaces.
5968 If PREFER_TYPE is > 1, we reject non-type decls (e.g. namespaces).
5969 Otherwise we prefer non-TYPE_DECLs.
5971 If NONCLASS is nonzero, bindings in class scopes are ignored. If
5972 BLOCK_P is false, bindings in block scopes are ignored. */
5974 static tree
5975 lookup_name_real_1 (tree name, int prefer_type, int nonclass, bool block_p,
5976 int namespaces_only, int flags)
5978 cxx_binding *iter;
5979 tree val = NULL_TREE;
5981 query_oracle (name);
5983 /* Conversion operators are handled specially because ordinary
5984 unqualified name lookup will not find template conversion
5985 operators. */
5986 if (IDENTIFIER_CONV_OP_P (name))
5988 cp_binding_level *level;
5990 for (level = current_binding_level;
5991 level && level->kind != sk_namespace;
5992 level = level->level_chain)
5994 tree class_type;
5995 tree operators;
5997 /* A conversion operator can only be declared in a class
5998 scope. */
5999 if (level->kind != sk_class)
6000 continue;
6002 /* Lookup the conversion operator in the class. */
6003 class_type = level->this_entity;
6004 operators = lookup_fnfields (class_type, name, /*protect=*/0);
6005 if (operators)
6006 return operators;
6009 return NULL_TREE;
6012 flags |= lookup_flags (prefer_type, namespaces_only);
6014 /* First, look in non-namespace scopes. */
6016 if (current_class_type == NULL_TREE)
6017 nonclass = 1;
6019 if (block_p || !nonclass)
6020 for (iter = outer_binding (name, NULL, !nonclass);
6021 iter;
6022 iter = outer_binding (name, iter, !nonclass))
6024 tree binding;
6026 /* Skip entities we don't want. */
6027 if (LOCAL_BINDING_P (iter) ? !block_p : nonclass)
6028 continue;
6030 /* If this is the kind of thing we're looking for, we're done. */
6031 if (qualify_lookup (iter->value, flags))
6032 binding = iter->value;
6033 else if ((flags & LOOKUP_PREFER_TYPES)
6034 && qualify_lookup (iter->type, flags))
6035 binding = iter->type;
6036 else
6037 binding = NULL_TREE;
6039 if (binding)
6041 if (TREE_CODE (binding) == TYPE_DECL && DECL_HIDDEN_P (binding))
6043 /* A non namespace-scope binding can only be hidden in the
6044 presence of a local class, due to friend declarations.
6046 In particular, consider:
6048 struct C;
6049 void f() {
6050 struct A {
6051 friend struct B;
6052 friend struct C;
6053 void g() {
6054 B* b; // error: B is hidden
6055 C* c; // OK, finds ::C
6058 B *b; // error: B is hidden
6059 C *c; // OK, finds ::C
6060 struct B {};
6061 B *bb; // OK
6064 The standard says that "B" is a local class in "f"
6065 (but not nested within "A") -- but that name lookup
6066 for "B" does not find this declaration until it is
6067 declared directly with "f".
6069 In particular:
6071 [class.friend]
6073 If a friend declaration appears in a local class and
6074 the name specified is an unqualified name, a prior
6075 declaration is looked up without considering scopes
6076 that are outside the innermost enclosing non-class
6077 scope. For a friend function declaration, if there is
6078 no prior declaration, the program is ill-formed. For a
6079 friend class declaration, if there is no prior
6080 declaration, the class that is specified belongs to the
6081 innermost enclosing non-class scope, but if it is
6082 subsequently referenced, its name is not found by name
6083 lookup until a matching declaration is provided in the
6084 innermost enclosing nonclass scope.
6086 So just keep looking for a non-hidden binding.
6088 gcc_assert (TREE_CODE (binding) == TYPE_DECL);
6089 continue;
6091 val = binding;
6092 break;
6096 /* Now lookup in namespace scopes. */
6097 if (!val)
6099 name_lookup lookup (name, flags);
6100 if (lookup.search_unqualified
6101 (current_decl_namespace (), current_binding_level))
6102 val = lookup.value;
6105 /* If we have a single function from a using decl, pull it out. */
6106 if (val && TREE_CODE (val) == OVERLOAD && !really_overloaded_fn (val))
6107 val = OVL_FUNCTION (val);
6109 return val;
6112 /* Wrapper for lookup_name_real_1. */
6114 tree
6115 lookup_name_real (tree name, int prefer_type, int nonclass, bool block_p,
6116 int namespaces_only, int flags)
6118 tree ret;
6119 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6120 ret = lookup_name_real_1 (name, prefer_type, nonclass, block_p,
6121 namespaces_only, flags);
6122 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6123 return ret;
6126 tree
6127 lookup_name_nonclass (tree name)
6129 return lookup_name_real (name, 0, 1, /*block_p=*/true, 0, 0);
6132 tree
6133 lookup_name (tree name)
6135 return lookup_name_real (name, 0, 0, /*block_p=*/true, 0, 0);
6138 tree
6139 lookup_name_prefer_type (tree name, int prefer_type)
6141 return lookup_name_real (name, prefer_type, 0, /*block_p=*/true, 0, 0);
6144 /* Look up NAME for type used in elaborated name specifier in
6145 the scopes given by SCOPE. SCOPE can be either TS_CURRENT or
6146 TS_WITHIN_ENCLOSING_NON_CLASS. Although not implied by the
6147 name, more scopes are checked if cleanup or template parameter
6148 scope is encountered.
6150 Unlike lookup_name_real, we make sure that NAME is actually
6151 declared in the desired scope, not from inheritance, nor using
6152 directive. For using declaration, there is DR138 still waiting
6153 to be resolved. Hidden name coming from an earlier friend
6154 declaration is also returned.
6156 A TYPE_DECL best matching the NAME is returned. Catching error
6157 and issuing diagnostics are caller's responsibility. */
6159 static tree
6160 lookup_type_scope_1 (tree name, tag_scope scope)
6162 cxx_binding *iter = NULL;
6163 tree val = NULL_TREE;
6164 cp_binding_level *level = NULL;
6166 /* Look in non-namespace scope first. */
6167 if (current_binding_level->kind != sk_namespace)
6168 iter = outer_binding (name, NULL, /*class_p=*/ true);
6169 for (; iter; iter = outer_binding (name, iter, /*class_p=*/ true))
6171 /* Check if this is the kind of thing we're looking for.
6172 If SCOPE is TS_CURRENT, also make sure it doesn't come from
6173 base class. For ITER->VALUE, we can simply use
6174 INHERITED_VALUE_BINDING_P. For ITER->TYPE, we have to use
6175 our own check.
6177 We check ITER->TYPE before ITER->VALUE in order to handle
6178 typedef struct C {} C;
6179 correctly. */
6181 if (qualify_lookup (iter->type, LOOKUP_PREFER_TYPES)
6182 && (scope != ts_current
6183 || LOCAL_BINDING_P (iter)
6184 || DECL_CONTEXT (iter->type) == iter->scope->this_entity))
6185 val = iter->type;
6186 else if ((scope != ts_current
6187 || !INHERITED_VALUE_BINDING_P (iter))
6188 && qualify_lookup (iter->value, LOOKUP_PREFER_TYPES))
6189 val = iter->value;
6191 if (val)
6192 break;
6195 /* Look in namespace scope. */
6196 if (val)
6197 level = iter->scope;
6198 else
6200 tree ns = current_decl_namespace ();
6202 if (tree *slot = find_namespace_slot (ns, name))
6204 /* If this is the kind of thing we're looking for, we're done. */
6205 if (tree type = MAYBE_STAT_TYPE (*slot))
6206 if (qualify_lookup (type, LOOKUP_PREFER_TYPES))
6207 val = type;
6208 if (!val)
6210 if (tree decl = MAYBE_STAT_DECL (*slot))
6211 if (qualify_lookup (decl, LOOKUP_PREFER_TYPES))
6212 val = decl;
6214 level = NAMESPACE_LEVEL (ns);
6218 /* Type found, check if it is in the allowed scopes, ignoring cleanup
6219 and template parameter scopes. */
6220 if (val)
6222 cp_binding_level *b = current_binding_level;
6223 while (b)
6225 if (level == b)
6226 return val;
6228 if (b->kind == sk_cleanup || b->kind == sk_template_parms
6229 || b->kind == sk_function_parms)
6230 b = b->level_chain;
6231 else if (b->kind == sk_class
6232 && scope == ts_within_enclosing_non_class)
6233 b = b->level_chain;
6234 else
6235 break;
6239 return NULL_TREE;
6242 /* Wrapper for lookup_type_scope_1. */
6244 tree
6245 lookup_type_scope (tree name, tag_scope scope)
6247 tree ret;
6248 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6249 ret = lookup_type_scope_1 (name, scope);
6250 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6251 return ret;
6254 /* Returns true iff DECL is a block-scope extern declaration of a function
6255 or variable. */
6257 bool
6258 is_local_extern (tree decl)
6260 cxx_binding *binding;
6262 /* For functions, this is easy. */
6263 if (TREE_CODE (decl) == FUNCTION_DECL)
6264 return DECL_LOCAL_FUNCTION_P (decl);
6266 if (!VAR_P (decl))
6267 return false;
6268 if (!current_function_decl)
6269 return false;
6271 /* For variables, this is not easy. We need to look at the binding stack
6272 for the identifier to see whether the decl we have is a local. */
6273 for (binding = IDENTIFIER_BINDING (DECL_NAME (decl));
6274 binding && binding->scope->kind != sk_namespace;
6275 binding = binding->previous)
6276 if (binding->value == decl)
6277 return LOCAL_BINDING_P (binding);
6279 return false;
6282 /* The type TYPE is being declared. If it is a class template, or a
6283 specialization of a class template, do any processing required and
6284 perform error-checking. If IS_FRIEND is nonzero, this TYPE is
6285 being declared a friend. B is the binding level at which this TYPE
6286 should be bound.
6288 Returns the TYPE_DECL for TYPE, which may have been altered by this
6289 processing. */
6291 static tree
6292 maybe_process_template_type_declaration (tree type, int is_friend,
6293 cp_binding_level *b)
6295 tree decl = TYPE_NAME (type);
6297 if (processing_template_parmlist)
6298 /* You can't declare a new template type in a template parameter
6299 list. But, you can declare a non-template type:
6301 template <class A*> struct S;
6303 is a forward-declaration of `A'. */
6305 else if (b->kind == sk_namespace
6306 && current_binding_level->kind != sk_namespace)
6307 /* If this new type is being injected into a containing scope,
6308 then it's not a template type. */
6310 else
6312 gcc_assert (MAYBE_CLASS_TYPE_P (type)
6313 || TREE_CODE (type) == ENUMERAL_TYPE);
6315 if (processing_template_decl)
6317 /* This may change after the call to
6318 push_template_decl_real, but we want the original value. */
6319 tree name = DECL_NAME (decl);
6321 decl = push_template_decl_real (decl, is_friend);
6322 if (decl == error_mark_node)
6323 return error_mark_node;
6325 /* If the current binding level is the binding level for the
6326 template parameters (see the comment in
6327 begin_template_parm_list) and the enclosing level is a class
6328 scope, and we're not looking at a friend, push the
6329 declaration of the member class into the class scope. In the
6330 friend case, push_template_decl will already have put the
6331 friend into global scope, if appropriate. */
6332 if (TREE_CODE (type) != ENUMERAL_TYPE
6333 && !is_friend && b->kind == sk_template_parms
6334 && b->level_chain->kind == sk_class)
6336 finish_member_declaration (CLASSTYPE_TI_TEMPLATE (type));
6338 if (!COMPLETE_TYPE_P (current_class_type))
6340 maybe_add_class_template_decl_list (current_class_type,
6341 type, /*friend_p=*/0);
6342 /* Put this UTD in the table of UTDs for the class. */
6343 if (CLASSTYPE_NESTED_UTDS (current_class_type) == NULL)
6344 CLASSTYPE_NESTED_UTDS (current_class_type) =
6345 binding_table_new (SCOPE_DEFAULT_HT_SIZE);
6347 binding_table_insert
6348 (CLASSTYPE_NESTED_UTDS (current_class_type), name, type);
6354 return decl;
6357 /* Push a tag name NAME for struct/class/union/enum type TYPE. In case
6358 that the NAME is a class template, the tag is processed but not pushed.
6360 The pushed scope depend on the SCOPE parameter:
6361 - When SCOPE is TS_CURRENT, put it into the inner-most non-sk_cleanup
6362 scope.
6363 - When SCOPE is TS_GLOBAL, put it in the inner-most non-class and
6364 non-template-parameter scope. This case is needed for forward
6365 declarations.
6366 - When SCOPE is TS_WITHIN_ENCLOSING_NON_CLASS, this is similar to
6367 TS_GLOBAL case except that names within template-parameter scopes
6368 are not pushed at all.
6370 Returns TYPE upon success and ERROR_MARK_NODE otherwise. */
6372 static tree
6373 do_pushtag (tree name, tree type, tag_scope scope)
6375 tree decl;
6377 cp_binding_level *b = current_binding_level;
6378 while (/* Cleanup scopes are not scopes from the point of view of
6379 the language. */
6380 b->kind == sk_cleanup
6381 /* Neither are function parameter scopes. */
6382 || b->kind == sk_function_parms
6383 /* Neither are the scopes used to hold template parameters
6384 for an explicit specialization. For an ordinary template
6385 declaration, these scopes are not scopes from the point of
6386 view of the language. */
6387 || (b->kind == sk_template_parms
6388 && (b->explicit_spec_p || scope == ts_global))
6389 || (b->kind == sk_class
6390 && (scope != ts_current
6391 /* We may be defining a new type in the initializer
6392 of a static member variable. We allow this when
6393 not pedantic, and it is particularly useful for
6394 type punning via an anonymous union. */
6395 || COMPLETE_TYPE_P (b->this_entity))))
6396 b = b->level_chain;
6398 gcc_assert (identifier_p (name));
6400 /* Do C++ gratuitous typedefing. */
6401 if (identifier_type_value_1 (name) != type)
6403 tree tdef;
6404 int in_class = 0;
6405 tree context = TYPE_CONTEXT (type);
6407 if (! context)
6409 tree cs = current_scope ();
6411 if (scope == ts_current
6412 || (cs && TREE_CODE (cs) == FUNCTION_DECL))
6413 context = cs;
6414 else if (cs && TYPE_P (cs))
6415 /* When declaring a friend class of a local class, we want
6416 to inject the newly named class into the scope
6417 containing the local class, not the namespace
6418 scope. */
6419 context = decl_function_context (get_type_decl (cs));
6421 if (!context)
6422 context = current_namespace;
6424 if (b->kind == sk_class
6425 || (b->kind == sk_template_parms
6426 && b->level_chain->kind == sk_class))
6427 in_class = 1;
6429 tdef = create_implicit_typedef (name, type);
6430 DECL_CONTEXT (tdef) = FROB_CONTEXT (context);
6431 if (scope == ts_within_enclosing_non_class)
6433 /* This is a friend. Make this TYPE_DECL node hidden from
6434 ordinary name lookup. Its corresponding TEMPLATE_DECL
6435 will be marked in push_template_decl_real. */
6436 retrofit_lang_decl (tdef);
6437 DECL_ANTICIPATED (tdef) = 1;
6438 DECL_FRIEND_P (tdef) = 1;
6441 decl = maybe_process_template_type_declaration
6442 (type, scope == ts_within_enclosing_non_class, b);
6443 if (decl == error_mark_node)
6444 return decl;
6446 if (b->kind == sk_class)
6448 if (!TYPE_BEING_DEFINED (current_class_type))
6449 return error_mark_node;
6451 if (!PROCESSING_REAL_TEMPLATE_DECL_P ())
6452 /* Put this TYPE_DECL on the TYPE_FIELDS list for the
6453 class. But if it's a member template class, we want
6454 the TEMPLATE_DECL, not the TYPE_DECL, so this is done
6455 later. */
6456 finish_member_declaration (decl);
6457 else
6458 pushdecl_class_level (decl);
6460 else if (b->kind != sk_template_parms)
6462 decl = do_pushdecl_with_scope (decl, b, /*is_friend=*/false);
6463 if (decl == error_mark_node)
6464 return decl;
6466 if (DECL_CONTEXT (decl) == std_node
6467 && init_list_identifier == DECL_NAME (TYPE_NAME (type))
6468 && !CLASSTYPE_TEMPLATE_INFO (type))
6470 error ("declaration of std::initializer_list does not match "
6471 "#include <initializer_list>, isn't a template");
6472 return error_mark_node;
6476 if (! in_class)
6477 set_identifier_type_value_with_scope (name, tdef, b);
6479 TYPE_CONTEXT (type) = DECL_CONTEXT (decl);
6481 /* If this is a local class, keep track of it. We need this
6482 information for name-mangling, and so that it is possible to
6483 find all function definitions in a translation unit in a
6484 convenient way. (It's otherwise tricky to find a member
6485 function definition it's only pointed to from within a local
6486 class.) */
6487 if (TYPE_FUNCTION_SCOPE_P (type))
6489 if (processing_template_decl)
6491 /* Push a DECL_EXPR so we call pushtag at the right time in
6492 template instantiation rather than in some nested context. */
6493 add_decl_expr (decl);
6495 /* Lambdas use LAMBDA_EXPR_DISCRIMINATOR instead. */
6496 else if (!LAMBDA_TYPE_P (type))
6497 vec_safe_push (local_classes, type);
6501 if (b->kind == sk_class
6502 && !COMPLETE_TYPE_P (current_class_type))
6504 maybe_add_class_template_decl_list (current_class_type,
6505 type, /*friend_p=*/0);
6507 if (CLASSTYPE_NESTED_UTDS (current_class_type) == NULL)
6508 CLASSTYPE_NESTED_UTDS (current_class_type)
6509 = binding_table_new (SCOPE_DEFAULT_HT_SIZE);
6511 binding_table_insert
6512 (CLASSTYPE_NESTED_UTDS (current_class_type), name, type);
6515 decl = TYPE_NAME (type);
6516 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
6518 /* Set type visibility now if this is a forward declaration. */
6519 TREE_PUBLIC (decl) = 1;
6520 determine_visibility (decl);
6522 return type;
6525 /* Wrapper for do_pushtag. */
6527 tree
6528 pushtag (tree name, tree type, tag_scope scope)
6530 tree ret;
6531 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6532 ret = do_pushtag (name, type, scope);
6533 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6534 return ret;
6538 /* Subroutines for reverting temporarily to top-level for instantiation
6539 of templates and such. We actually need to clear out the class- and
6540 local-value slots of all identifiers, so that only the global values
6541 are at all visible. Simply setting current_binding_level to the global
6542 scope isn't enough, because more binding levels may be pushed. */
6543 struct saved_scope *scope_chain;
6545 /* Return true if ID has not already been marked. */
6547 static inline bool
6548 store_binding_p (tree id)
6550 if (!id || !IDENTIFIER_BINDING (id))
6551 return false;
6553 if (IDENTIFIER_MARKED (id))
6554 return false;
6556 return true;
6559 /* Add an appropriate binding to *OLD_BINDINGS which needs to already
6560 have enough space reserved. */
6562 static void
6563 store_binding (tree id, vec<cxx_saved_binding, va_gc> **old_bindings)
6565 cxx_saved_binding saved;
6567 gcc_checking_assert (store_binding_p (id));
6569 IDENTIFIER_MARKED (id) = 1;
6571 saved.identifier = id;
6572 saved.binding = IDENTIFIER_BINDING (id);
6573 saved.real_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
6574 (*old_bindings)->quick_push (saved);
6575 IDENTIFIER_BINDING (id) = NULL;
6578 static void
6579 store_bindings (tree names, vec<cxx_saved_binding, va_gc> **old_bindings)
6581 static vec<tree> bindings_need_stored;
6582 tree t, id;
6583 size_t i;
6585 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6586 for (t = names; t; t = TREE_CHAIN (t))
6588 if (TREE_CODE (t) == TREE_LIST)
6589 id = TREE_PURPOSE (t);
6590 else
6591 id = DECL_NAME (t);
6593 if (store_binding_p (id))
6594 bindings_need_stored.safe_push (id);
6596 if (!bindings_need_stored.is_empty ())
6598 vec_safe_reserve_exact (*old_bindings, bindings_need_stored.length ());
6599 for (i = 0; bindings_need_stored.iterate (i, &id); ++i)
6601 /* We can apparently have duplicates in NAMES. */
6602 if (store_binding_p (id))
6603 store_binding (id, old_bindings);
6605 bindings_need_stored.truncate (0);
6607 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6610 /* Like store_bindings, but NAMES is a vector of cp_class_binding
6611 objects, rather than a TREE_LIST. */
6613 static void
6614 store_class_bindings (vec<cp_class_binding, va_gc> *names,
6615 vec<cxx_saved_binding, va_gc> **old_bindings)
6617 static vec<tree> bindings_need_stored;
6618 size_t i;
6619 cp_class_binding *cb;
6621 for (i = 0; vec_safe_iterate (names, i, &cb); ++i)
6622 if (store_binding_p (cb->identifier))
6623 bindings_need_stored.safe_push (cb->identifier);
6624 if (!bindings_need_stored.is_empty ())
6626 tree id;
6627 vec_safe_reserve_exact (*old_bindings, bindings_need_stored.length ());
6628 for (i = 0; bindings_need_stored.iterate (i, &id); ++i)
6629 store_binding (id, old_bindings);
6630 bindings_need_stored.truncate (0);
6634 /* A chain of saved_scope structures awaiting reuse. */
6636 static GTY((deletable)) struct saved_scope *free_saved_scope;
6638 static void
6639 do_push_to_top_level (void)
6641 struct saved_scope *s;
6642 cp_binding_level *b;
6643 cxx_saved_binding *sb;
6644 size_t i;
6645 bool need_pop;
6647 /* Reuse or create a new structure for this saved scope. */
6648 if (free_saved_scope != NULL)
6650 s = free_saved_scope;
6651 free_saved_scope = s->prev;
6653 vec<cxx_saved_binding, va_gc> *old_bindings = s->old_bindings;
6654 memset (s, 0, sizeof (*s));
6655 /* Also reuse the structure's old_bindings vector. */
6656 vec_safe_truncate (old_bindings, 0);
6657 s->old_bindings = old_bindings;
6659 else
6660 s = ggc_cleared_alloc<saved_scope> ();
6662 b = scope_chain ? current_binding_level : 0;
6664 /* If we're in the middle of some function, save our state. */
6665 if (cfun)
6667 need_pop = true;
6668 push_function_context ();
6670 else
6671 need_pop = false;
6673 if (scope_chain && previous_class_level)
6674 store_class_bindings (previous_class_level->class_shadowed,
6675 &s->old_bindings);
6677 /* Have to include the global scope, because class-scope decls
6678 aren't listed anywhere useful. */
6679 for (; b; b = b->level_chain)
6681 tree t;
6683 /* Template IDs are inserted into the global level. If they were
6684 inserted into namespace level, finish_file wouldn't find them
6685 when doing pending instantiations. Therefore, don't stop at
6686 namespace level, but continue until :: . */
6687 if (global_scope_p (b))
6688 break;
6690 store_bindings (b->names, &s->old_bindings);
6691 /* We also need to check class_shadowed to save class-level type
6692 bindings, since pushclass doesn't fill in b->names. */
6693 if (b->kind == sk_class)
6694 store_class_bindings (b->class_shadowed, &s->old_bindings);
6696 /* Unwind type-value slots back to top level. */
6697 for (t = b->type_shadowed; t; t = TREE_CHAIN (t))
6698 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (t), TREE_VALUE (t));
6701 FOR_EACH_VEC_SAFE_ELT (s->old_bindings, i, sb)
6702 IDENTIFIER_MARKED (sb->identifier) = 0;
6704 s->prev = scope_chain;
6705 s->bindings = b;
6706 s->need_pop_function_context = need_pop;
6707 s->function_decl = current_function_decl;
6708 s->unevaluated_operand = cp_unevaluated_operand;
6709 s->inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
6710 s->x_stmt_tree.stmts_are_full_exprs_p = true;
6712 scope_chain = s;
6713 current_function_decl = NULL_TREE;
6714 vec_alloc (current_lang_base, 10);
6715 current_lang_name = lang_name_cplusplus;
6716 current_namespace = global_namespace;
6717 push_class_stack ();
6718 cp_unevaluated_operand = 0;
6719 c_inhibit_evaluation_warnings = 0;
6722 static void
6723 do_pop_from_top_level (void)
6725 struct saved_scope *s = scope_chain;
6726 cxx_saved_binding *saved;
6727 size_t i;
6729 /* Clear out class-level bindings cache. */
6730 if (previous_class_level)
6731 invalidate_class_lookup_cache ();
6732 pop_class_stack ();
6734 current_lang_base = 0;
6736 scope_chain = s->prev;
6737 FOR_EACH_VEC_SAFE_ELT (s->old_bindings, i, saved)
6739 tree id = saved->identifier;
6741 IDENTIFIER_BINDING (id) = saved->binding;
6742 SET_IDENTIFIER_TYPE_VALUE (id, saved->real_type_value);
6745 /* If we were in the middle of compiling a function, restore our
6746 state. */
6747 if (s->need_pop_function_context)
6748 pop_function_context ();
6749 current_function_decl = s->function_decl;
6750 cp_unevaluated_operand = s->unevaluated_operand;
6751 c_inhibit_evaluation_warnings = s->inhibit_evaluation_warnings;
6753 /* Make this saved_scope structure available for reuse by
6754 push_to_top_level. */
6755 s->prev = free_saved_scope;
6756 free_saved_scope = s;
6759 /* Push into the scope of the namespace NS, even if it is deeply
6760 nested within another namespace. */
6762 static void
6763 do_push_nested_namespace (tree ns)
6765 if (ns == global_namespace)
6766 do_push_to_top_level ();
6767 else
6769 do_push_nested_namespace (CP_DECL_CONTEXT (ns));
6770 gcc_checking_assert
6771 (find_namespace_value (current_namespace, DECL_NAME (ns)) == ns);
6772 resume_scope (NAMESPACE_LEVEL (ns));
6773 current_namespace = ns;
6777 /* Pop back from the scope of the namespace NS, which was previously
6778 entered with push_nested_namespace. */
6780 static void
6781 do_pop_nested_namespace (tree ns)
6783 while (ns != global_namespace)
6785 ns = CP_DECL_CONTEXT (ns);
6786 current_namespace = ns;
6787 leave_scope ();
6790 do_pop_from_top_level ();
6793 /* Add TARGET to USINGS, if it does not already exist there.
6794 We used to build the complete graph of usings at this point, from
6795 the POV of the source namespaces. Now we build that as we perform
6796 the unqualified search. */
6798 static void
6799 add_using_namespace (vec<tree, va_gc> *&usings, tree target)
6801 if (usings)
6802 for (unsigned ix = usings->length (); ix--;)
6803 if ((*usings)[ix] == target)
6804 return;
6806 vec_safe_push (usings, target);
6809 /* Tell the debug system of a using directive. */
6811 static void
6812 emit_debug_info_using_namespace (tree from, tree target, bool implicit)
6814 /* Emit debugging info. */
6815 tree context = from != global_namespace ? from : NULL_TREE;
6816 debug_hooks->imported_module_or_decl (target, NULL_TREE, context, false,
6817 implicit);
6820 /* Process a namespace-scope using directive. */
6822 void
6823 finish_namespace_using_directive (tree target, tree attribs)
6825 gcc_checking_assert (namespace_bindings_p ());
6826 if (target == error_mark_node)
6827 return;
6829 add_using_namespace (DECL_NAMESPACE_USING (current_namespace),
6830 ORIGINAL_NAMESPACE (target));
6831 emit_debug_info_using_namespace (current_namespace,
6832 ORIGINAL_NAMESPACE (target), false);
6834 if (attribs == error_mark_node)
6835 return;
6837 for (tree a = attribs; a; a = TREE_CHAIN (a))
6839 tree name = get_attribute_name (a);
6840 if (is_attribute_p ("strong", name))
6842 warning (0, "strong using directive no longer supported");
6843 if (CP_DECL_CONTEXT (target) == current_namespace)
6844 inform (DECL_SOURCE_LOCATION (target),
6845 "you may use an inline namespace instead");
6847 else
6848 warning (OPT_Wattributes, "%qD attribute directive ignored", name);
6852 /* Process a function-scope using-directive. */
6854 void
6855 finish_local_using_directive (tree target, tree attribs)
6857 gcc_checking_assert (local_bindings_p ());
6858 if (target == error_mark_node)
6859 return;
6861 if (attribs)
6862 warning (OPT_Wattributes, "attributes ignored on local using directive");
6864 add_stmt (build_stmt (input_location, USING_STMT, target));
6866 add_using_namespace (current_binding_level->using_directives,
6867 ORIGINAL_NAMESPACE (target));
6870 /* Pushes X into the global namespace. */
6872 tree
6873 pushdecl_top_level (tree x, bool is_friend)
6875 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6876 do_push_to_top_level ();
6877 x = pushdecl_namespace_level (x, is_friend);
6878 do_pop_from_top_level ();
6879 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6880 return x;
6883 /* Pushes X into the global namespace and calls cp_finish_decl to
6884 register the variable, initializing it with INIT. */
6886 tree
6887 pushdecl_top_level_and_finish (tree x, tree init)
6889 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6890 do_push_to_top_level ();
6891 x = pushdecl_namespace_level (x, false);
6892 cp_finish_decl (x, init, false, NULL_TREE, 0);
6893 do_pop_from_top_level ();
6894 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6895 return x;
6898 /* Enter the namespaces from current_namerspace to NS. */
6900 static int
6901 push_inline_namespaces (tree ns)
6903 int count = 0;
6904 if (ns != current_namespace)
6906 gcc_assert (ns != global_namespace);
6907 count += push_inline_namespaces (CP_DECL_CONTEXT (ns));
6908 resume_scope (NAMESPACE_LEVEL (ns));
6909 current_namespace = ns;
6910 count++;
6912 return count;
6915 /* Push into the scope of the NAME namespace. If NAME is NULL_TREE,
6916 then we enter an anonymous namespace. If MAKE_INLINE is true, then
6917 we create an inline namespace (it is up to the caller to check upon
6918 redefinition). Return the number of namespaces entered. */
6921 push_namespace (tree name, bool make_inline)
6923 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6924 int count = 0;
6926 /* We should not get here if the global_namespace is not yet constructed
6927 nor if NAME designates the global namespace: The global scope is
6928 constructed elsewhere. */
6929 gcc_checking_assert (global_namespace != NULL && name != global_identifier);
6931 tree ns = NULL_TREE;
6933 name_lookup lookup (name, 0);
6934 if (!lookup.search_qualified (current_namespace, /*usings=*/false))
6936 else if (TREE_CODE (lookup.value) != NAMESPACE_DECL)
6938 else if (tree dna = DECL_NAMESPACE_ALIAS (lookup.value))
6940 /* A namespace alias is not allowed here, but if the alias
6941 is for a namespace also inside the current scope,
6942 accept it with a diagnostic. That's better than dying
6943 horribly. */
6944 if (is_nested_namespace (current_namespace, CP_DECL_CONTEXT (dna)))
6946 error ("namespace alias %qD not allowed here, "
6947 "assuming %qD", lookup.value, dna);
6948 ns = dna;
6951 else
6952 ns = lookup.value;
6955 bool new_ns = false;
6956 if (ns)
6957 /* DR2061. NS might be a member of an inline namespace. We
6958 need to push into those namespaces. */
6959 count += push_inline_namespaces (CP_DECL_CONTEXT (ns));
6960 else
6962 ns = build_lang_decl (NAMESPACE_DECL, name, void_type_node);
6963 SCOPE_DEPTH (ns) = SCOPE_DEPTH (current_namespace) + 1;
6964 if (!SCOPE_DEPTH (ns))
6965 /* We only allow depth 255. */
6966 sorry ("cannot nest more than %d namespaces",
6967 SCOPE_DEPTH (current_namespace));
6968 DECL_CONTEXT (ns) = FROB_CONTEXT (current_namespace);
6969 new_ns = true;
6971 if (pushdecl (ns) == error_mark_node)
6972 ns = NULL_TREE;
6973 else
6975 if (!name)
6977 SET_DECL_ASSEMBLER_NAME (ns, anon_identifier);
6979 if (!make_inline)
6980 add_using_namespace (DECL_NAMESPACE_USING (current_namespace),
6981 ns);
6983 else if (TREE_PUBLIC (current_namespace))
6984 TREE_PUBLIC (ns) = 1;
6986 if (make_inline)
6988 DECL_NAMESPACE_INLINE_P (ns) = true;
6989 vec_safe_push (DECL_NAMESPACE_INLINEES (current_namespace), ns);
6992 if (!name || make_inline)
6993 emit_debug_info_using_namespace (current_namespace, ns, true);
6997 if (ns)
6999 if (make_inline && !DECL_NAMESPACE_INLINE_P (ns))
7001 error ("inline namespace must be specified at initial definition");
7002 inform (DECL_SOURCE_LOCATION (ns), "%qD defined here", ns);
7004 if (new_ns)
7005 begin_scope (sk_namespace, ns);
7006 else
7007 resume_scope (NAMESPACE_LEVEL (ns));
7008 current_namespace = ns;
7009 count++;
7012 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
7013 return count;
7016 /* Pop from the scope of the current namespace. */
7018 void
7019 pop_namespace (void)
7021 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
7023 gcc_assert (current_namespace != global_namespace);
7024 current_namespace = CP_DECL_CONTEXT (current_namespace);
7025 /* The binding level is not popped, as it might be re-opened later. */
7026 leave_scope ();
7028 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
7031 /* External entry points for do_{push_to/pop_from}_top_level. */
7033 void
7034 push_to_top_level (void)
7036 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
7037 do_push_to_top_level ();
7038 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
7041 void
7042 pop_from_top_level (void)
7044 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
7045 do_pop_from_top_level ();
7046 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
7049 /* External entry points for do_{push,pop}_nested_namespace. */
7051 void
7052 push_nested_namespace (tree ns)
7054 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
7055 do_push_nested_namespace (ns);
7056 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
7059 void
7060 pop_nested_namespace (tree ns)
7062 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
7063 gcc_assert (current_namespace == ns);
7064 do_pop_nested_namespace (ns);
7065 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
7068 /* Pop off extraneous binding levels left over due to syntax errors.
7069 We don't pop past namespaces, as they might be valid. */
7071 void
7072 pop_everything (void)
7074 if (ENABLE_SCOPE_CHECKING)
7075 verbatim ("XXX entering pop_everything ()\n");
7076 while (!namespace_bindings_p ())
7078 if (current_binding_level->kind == sk_class)
7079 pop_nested_class ();
7080 else
7081 poplevel (0, 0, 0);
7083 if (ENABLE_SCOPE_CHECKING)
7084 verbatim ("XXX leaving pop_everything ()\n");
7087 /* Emit debugging information for using declarations and directives.
7088 If input tree is overloaded fn then emit debug info for all
7089 candidates. */
7091 void
7092 cp_emit_debug_info_for_using (tree t, tree context)
7094 /* Don't try to emit any debug information if we have errors. */
7095 if (seen_error ())
7096 return;
7098 /* Ignore this FUNCTION_DECL if it refers to a builtin declaration
7099 of a builtin function. */
7100 if (TREE_CODE (t) == FUNCTION_DECL
7101 && DECL_EXTERNAL (t)
7102 && DECL_BUILT_IN (t))
7103 return;
7105 /* Do not supply context to imported_module_or_decl, if
7106 it is a global namespace. */
7107 if (context == global_namespace)
7108 context = NULL_TREE;
7110 t = MAYBE_BASELINK_FUNCTIONS (t);
7112 /* FIXME: Handle TEMPLATE_DECLs. */
7113 for (lkp_iterator iter (t); iter; ++iter)
7115 tree fn = *iter;
7116 if (TREE_CODE (fn) != TEMPLATE_DECL)
7118 if (building_stmt_list_p ())
7119 add_stmt (build_stmt (input_location, USING_STMT, fn));
7120 else
7121 debug_hooks->imported_module_or_decl (fn, NULL_TREE, context,
7122 false, false);
7127 #include "gt-cp-name-lookup.h"