gcc/
[official-gcc.git] / gcc / cp / name-lookup.c
blob9c0390175b3bdb4b2d0074e409b34e5ad82e49f8
1 /* Definitions for C++ name lookup routines.
2 Copyright (C) 2003-2017 Free Software Foundation, Inc.
3 Contributed by Gabriel Dos Reis <gdr@integrable-solutions.net>
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 #include "system.h"
23 #include "coretypes.h"
24 #include "cp-tree.h"
25 #include "timevar.h"
26 #include "stringpool.h"
27 #include "print-tree.h"
28 #include "attribs.h"
29 #include "debug.h"
30 #include "c-family/c-pragma.h"
31 #include "params.h"
32 #include "gcc-rich-location.h"
33 #include "spellcheck-tree.h"
34 #include "parser.h"
36 static cxx_binding *cxx_binding_make (tree value, tree type);
37 static cp_binding_level *innermost_nonclass_level (void);
38 static void set_identifier_type_value_with_scope (tree id, tree decl,
39 cp_binding_level *b);
41 /* Create a local binding level for NAME. */
43 static cxx_binding *
44 create_local_binding (cp_binding_level *level, tree name)
46 cxx_binding *binding = cxx_binding_make (NULL, NULL);
48 INHERITED_VALUE_BINDING_P (binding) = false;
49 LOCAL_BINDING_P (binding) = true;
50 binding->scope = level;
51 binding->previous = IDENTIFIER_BINDING (name);
53 IDENTIFIER_BINDING (name) = binding;
55 return binding;
58 /* Find the binding for NAME in namespace NS. If CREATE_P is true,
59 make an empty binding if there wasn't one. */
61 static cxx_binding *
62 find_namespace_binding (tree ns, tree name, bool create_p = false)
64 cp_binding_level *level = NAMESPACE_LEVEL (ns);
65 cxx_binding *binding = IDENTIFIER_NAMESPACE_BINDINGS (name);
67 for (;binding; binding = binding->previous)
68 if (binding->scope == level)
69 return binding;
71 if (create_p)
73 binding = cxx_binding_make (NULL, NULL);
74 binding->previous = IDENTIFIER_NAMESPACE_BINDINGS (name);
75 binding->scope = level;
76 binding->is_local = false;
77 binding->value_is_inherited = false;
78 IDENTIFIER_NAMESPACE_BINDINGS (name) = binding;
81 return binding;
84 /* Add DECL to the list of things declared in B. */
86 static void
87 add_decl_to_level (cp_binding_level *b, tree decl)
89 /* We used to record virtual tables as if they were ordinary
90 variables, but no longer do so. */
91 gcc_assert (!(VAR_P (decl) && DECL_VIRTUAL_P (decl)));
93 if (TREE_CODE (decl) == NAMESPACE_DECL
94 && !DECL_NAMESPACE_ALIAS (decl))
96 DECL_CHAIN (decl) = b->namespaces;
97 b->namespaces = decl;
99 else
101 /* We build up the list in reverse order, and reverse it later if
102 necessary. */
103 TREE_CHAIN (decl) = b->names;
104 b->names = decl;
106 /* If appropriate, add decl to separate list of statics. We
107 include extern variables because they might turn out to be
108 static later. It's OK for this list to contain a few false
109 positives. */
110 if (b->kind == sk_namespace)
111 if ((VAR_P (decl)
112 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
113 || (TREE_CODE (decl) == FUNCTION_DECL
114 && (!TREE_PUBLIC (decl)
115 || decl_anon_ns_mem_p (decl)
116 || DECL_DECLARED_INLINE_P (decl))))
117 vec_safe_push (static_decls, decl);
121 /* Find the binding for NAME in the local binding level B. */
123 static cxx_binding *
124 find_local_binding (cp_binding_level *b, tree name)
126 if (cxx_binding *binding = IDENTIFIER_BINDING (name))
127 for (;; b = b->level_chain)
129 if (binding->scope == b
130 && !(VAR_P (binding->value)
131 && DECL_DEAD_FOR_LOCAL (binding->value)))
132 return binding;
134 /* Cleanup contours are transparent to the language. */
135 if (b->kind != sk_cleanup)
136 break;
138 return NULL;
141 struct name_lookup
143 public:
144 typedef std::pair<tree, tree> using_pair;
145 typedef vec<using_pair, va_heap, vl_embed> using_queue;
147 public:
148 tree name; /* The identifier being looked for. */
149 tree value; /* A (possibly ambiguous) set of things found. */
150 tree type; /* A type that has been found. */
151 int flags; /* Lookup flags. */
152 vec<tree, va_heap, vl_embed> *scopes;
153 name_lookup *previous; /* Previously active lookup. */
155 protected:
156 /* Marked scope stack for outermost name lookup. */
157 static vec<tree, va_heap, vl_embed> *shared_scopes;
158 /* Currently active lookup. */
159 static name_lookup *active;
161 public:
162 name_lookup (tree n, int f = 0)
163 : name (n), value (NULL_TREE), type (NULL_TREE), flags (f),
164 scopes (NULL), previous (NULL)
166 preserve_state ();
168 ~name_lookup ()
170 restore_state ();
173 private: /* Uncopyable, unmovable, unassignable. I am a rock. */
174 name_lookup (const name_lookup &);
175 name_lookup &operator= (const name_lookup &);
177 protected:
178 static bool seen_p (tree scope)
180 return LOOKUP_SEEN_P (scope);
182 static bool found_p (tree scope)
184 return LOOKUP_FOUND_P (scope);
187 void mark_seen (tree scope); /* Mark and add to scope vector. */
188 static void mark_found (tree scope)
190 gcc_checking_assert (seen_p (scope));
191 LOOKUP_FOUND_P (scope) = true;
193 bool see_and_mark (tree scope)
195 bool ret = seen_p (scope);
196 if (!ret)
197 mark_seen (scope);
198 return ret;
200 bool find_and_mark (tree scope);
202 private:
203 void preserve_state ();
204 void restore_state ();
206 private:
207 static tree ambiguous (tree thing, tree current);
208 void add_value (tree new_val);
209 void add_type (tree new_type);
210 bool process_binding (tree val_bind, tree type_bind);
212 /* Look in only namespace. */
213 bool search_namespace_only (tree scope);
214 /* Look in namespace and its (recursive) inlines. Ignore using
215 directives. Return true if something found (inc dups). */
216 bool search_namespace (tree scope);
217 /* Look in the using directives of namespace + inlines using
218 qualified lookup rules. */
219 bool search_usings (tree scope);
221 private:
222 using_queue *queue_namespace (using_queue *queue, int depth, tree scope);
223 using_queue *do_queue_usings (using_queue *queue, int depth,
224 vec<tree, va_gc> *usings);
225 using_queue *queue_usings (using_queue *queue, int depth,
226 vec<tree, va_gc> *usings)
228 if (usings)
229 queue = do_queue_usings (queue, depth, usings);
230 return queue;
233 private:
234 void add_fns (tree);
236 void adl_expr (tree);
237 void adl_type (tree);
238 void adl_template_arg (tree);
239 void adl_class (tree);
240 void adl_bases (tree);
241 void adl_class_only (tree);
242 void adl_namespace (tree);
243 void adl_namespace_only (tree);
245 public:
246 /* Search namespace + inlines + maybe usings as qualified lookup. */
247 bool search_qualified (tree scope, bool usings = true);
249 /* Search namespace + inlines + usings as unqualified lookup. */
250 bool search_unqualified (tree scope, cp_binding_level *);
252 /* ADL lookup of ARGS. */
253 tree search_adl (tree fns, vec<tree, va_gc> *args);
256 /* Scope stack shared by all outermost lookups. This avoids us
257 allocating and freeing on every single lookup. */
258 vec<tree, va_heap, vl_embed> *name_lookup::shared_scopes;
260 /* Currently active lookup. */
261 name_lookup *name_lookup::active;
263 /* Name lookup is recursive, becase ADL can cause template
264 instatiation. This is of course a rare event, so we optimize for
265 it not happening. When we discover an active name-lookup, which
266 must be an ADL lookup, we need to unmark the marked scopes and also
267 unmark the lookup we might have been accumulating. */
269 void
270 name_lookup::preserve_state ()
272 previous = active;
273 if (previous)
275 unsigned length = vec_safe_length (previous->scopes);
276 vec_safe_reserve (previous->scopes, length * 2);
277 for (unsigned ix = length; ix--;)
279 tree decl = (*previous->scopes)[ix];
281 gcc_checking_assert (LOOKUP_SEEN_P (decl));
282 LOOKUP_SEEN_P (decl) = false;
284 /* Preserve the FOUND_P state on the interrupted lookup's
285 stack. */
286 if (LOOKUP_FOUND_P (decl))
288 LOOKUP_FOUND_P (decl) = false;
289 previous->scopes->quick_push (decl);
293 /* Unmark the outer partial lookup. */
294 lookup_mark (previous->value, false);
296 else
297 scopes = shared_scopes;
298 active = this;
301 /* Restore the marking state of a lookup we interrupted. */
303 void
304 name_lookup::restore_state ()
306 /* Unmark and empty this lookup's scope stack. */
307 for (unsigned ix = vec_safe_length (scopes); ix--;)
309 tree decl = scopes->pop ();
310 gcc_checking_assert (LOOKUP_SEEN_P (decl));
311 LOOKUP_SEEN_P (decl) = false;
312 LOOKUP_FOUND_P (decl) = false;
315 active = previous;
316 if (previous)
318 free (scopes);
320 unsigned length = vec_safe_length (previous->scopes);
321 for (unsigned ix = 0; ix != length; ix++)
323 tree decl = (*previous->scopes)[ix];
324 if (LOOKUP_SEEN_P (decl))
326 /* The remainder of the scope stack must be recording
327 FOUND_P decls, which we want to pop off. */
330 tree decl = previous->scopes->pop ();
331 gcc_checking_assert (LOOKUP_SEEN_P (decl)
332 && !LOOKUP_FOUND_P (decl));
333 LOOKUP_FOUND_P (decl) = true;
335 while (++ix != length);
336 break;
339 gcc_checking_assert (!LOOKUP_FOUND_P (decl));
340 LOOKUP_SEEN_P (decl) = true;
343 /* Remark the outer partial lookup. */
344 lookup_mark (previous->value, true);
346 else
347 shared_scopes = scopes;
350 void
351 name_lookup::mark_seen (tree scope)
353 gcc_checking_assert (!seen_p (scope));
354 LOOKUP_SEEN_P (scope) = true;
355 vec_safe_push (scopes, scope);
358 bool
359 name_lookup::find_and_mark (tree scope)
361 bool result = LOOKUP_FOUND_P (scope);
362 if (!result)
364 LOOKUP_FOUND_P (scope) = true;
365 if (!LOOKUP_SEEN_P (scope))
366 vec_safe_push (scopes, scope);
369 return result;
372 /* THING and CURRENT are ambiguous, concatenate them. */
374 tree
375 name_lookup::ambiguous (tree thing, tree current)
377 if (TREE_CODE (current) != TREE_LIST)
379 current = build_tree_list (NULL_TREE, current);
380 TREE_TYPE (current) = error_mark_node;
382 current = tree_cons (NULL_TREE, thing, current);
383 TREE_TYPE (current) = error_mark_node;
385 return current;
388 /* Add a NEW_VAL, a found value binding into the current value binding. */
390 void
391 name_lookup::add_value (tree new_val)
393 if (!value)
394 value = new_val;
395 else if (value == new_val)
397 else if ((TREE_CODE (value) == TYPE_DECL
398 && TREE_CODE (new_val) == TYPE_DECL
399 && same_type_p (TREE_TYPE (value), TREE_TYPE (new_val))))
401 else if (OVL_P (value) && OVL_P (new_val))
402 value = lookup_add (new_val, value);
403 else
404 value = ambiguous (new_val, value);
407 /* Add a NEW_TYPE, a found type binding into the current type binding. */
409 void
410 name_lookup::add_type (tree new_type)
412 if (!type)
413 type = new_type;
414 else if (TREE_CODE (type) == TREE_LIST
415 || !same_type_p (TREE_TYPE (type), TREE_TYPE (new_type)))
416 type = ambiguous (new_type, type);
419 /* Process a found binding containing NEW_VAL and NEW_TYPE. Returns
420 true if we actually found something noteworthy. */
422 bool
423 name_lookup::process_binding (tree new_val, tree new_type)
425 /* Did we really see a type? */
426 if (new_type
427 && (LOOKUP_NAMESPACES_ONLY (flags)
428 || (!(flags & LOOKUP_HIDDEN)
429 && DECL_LANG_SPECIFIC (new_type)
430 && DECL_ANTICIPATED (new_type))))
431 new_type = NULL_TREE;
433 if (new_val && !(flags & LOOKUP_HIDDEN))
434 new_val = ovl_skip_hidden (new_val);
436 /* Do we really see a value? */
437 if (new_val)
438 switch (TREE_CODE (new_val))
440 case TEMPLATE_DECL:
441 /* If we expect types or namespaces, and not templates,
442 or this is not a template class. */
443 if ((LOOKUP_QUALIFIERS_ONLY (flags)
444 && !DECL_TYPE_TEMPLATE_P (new_val)))
445 new_val = NULL_TREE;
446 break;
447 case TYPE_DECL:
448 if (LOOKUP_NAMESPACES_ONLY (flags)
449 || (new_type && (flags & LOOKUP_PREFER_TYPES)))
450 new_val = NULL_TREE;
451 break;
452 case NAMESPACE_DECL:
453 if (LOOKUP_TYPES_ONLY (flags))
454 new_val = NULL_TREE;
455 break;
456 default:
457 if (LOOKUP_QUALIFIERS_ONLY (flags))
458 new_val = NULL_TREE;
461 if (!new_val)
463 new_val = new_type;
464 new_type = NULL_TREE;
467 /* Merge into the lookup */
468 if (new_val)
469 add_value (new_val);
470 if (new_type)
471 add_type (new_type);
473 return new_val != NULL_TREE;
476 /* Look in exactly namespace SCOPE. */
478 bool
479 name_lookup::search_namespace_only (tree scope)
481 bool found = false;
483 if (cxx_binding *binding = find_namespace_binding (scope, name))
484 found |= process_binding (binding->value, binding->type);
486 return found;
489 /* Conditionally look in namespace SCOPE and inline children. */
491 bool
492 name_lookup::search_namespace (tree scope)
494 if (see_and_mark (scope))
495 /* We've visited this scope before. Return what we found then. */
496 return found_p (scope);
498 /* Look in exactly namespace. */
499 bool found = search_namespace_only (scope);
501 /* Recursively look in its inline children. */
502 if (vec<tree, va_gc> *inlinees = DECL_NAMESPACE_INLINEES (scope))
503 for (unsigned ix = inlinees->length (); ix--;)
504 found |= search_namespace ((*inlinees)[ix]);
506 if (found)
507 mark_found (scope);
509 return found;
512 /* Recursively follow using directives of SCOPE & its inline children.
513 Such following is essentially a flood-fill algorithm. */
515 bool
516 name_lookup::search_usings (tree scope)
518 /* We do not check seen_p here, as that was already set during the
519 namespace_only walk. */
520 if (found_p (scope))
521 return true;
523 bool found = false;
524 if (vec<tree, va_gc> *usings = DECL_NAMESPACE_USING (scope))
525 for (unsigned ix = usings->length (); ix--;)
526 found |= search_qualified ((*usings)[ix], true);
528 /* Look in its inline children. */
529 if (vec<tree, va_gc> *inlinees = DECL_NAMESPACE_INLINEES (scope))
530 for (unsigned ix = inlinees->length (); ix--;)
531 found |= search_usings ((*inlinees)[ix]);
533 if (found)
534 mark_found (scope);
536 return found;
539 /* Qualified namespace lookup in SCOPE.
540 1) Look in SCOPE (+inlines). If found, we're done.
541 2) Otherwise, if USINGS is true,
542 recurse for every using directive of SCOPE (+inlines).
544 Trickiness is (a) loops and (b) multiple paths to same namespace.
545 In both cases we want to not repeat any lookups, and know whether
546 to stop the caller's step #2. Do this via the FOUND_P marker. */
548 bool
549 name_lookup::search_qualified (tree scope, bool usings)
551 bool found = false;
553 if (seen_p (scope))
554 found = found_p (scope);
555 else
557 found = search_namespace (scope);
558 if (!found && usings)
559 found = search_usings (scope);
562 return found;
565 /* Add SCOPE to the unqualified search queue, recursively add its
566 inlines and those via using directives. */
568 name_lookup::using_queue *
569 name_lookup::queue_namespace (using_queue *queue, int depth, tree scope)
571 if (see_and_mark (scope))
572 return queue;
574 /* Record it. */
575 tree common = scope;
576 while (SCOPE_DEPTH (common) > depth)
577 common = CP_DECL_CONTEXT (common);
578 vec_safe_push (queue, using_pair (common, scope));
580 /* Queue its inline children. */
581 if (vec<tree, va_gc> *inlinees = DECL_NAMESPACE_INLINEES (scope))
582 for (unsigned ix = inlinees->length (); ix--;)
583 queue = queue_namespace (queue, depth, (*inlinees)[ix]);
585 /* Queue its using targets. */
586 queue = queue_usings (queue, depth, DECL_NAMESPACE_USING (scope));
588 return queue;
591 /* Add the namespaces in USINGS to the unqualified search queue. */
593 name_lookup::using_queue *
594 name_lookup::do_queue_usings (using_queue *queue, int depth,
595 vec<tree, va_gc> *usings)
597 for (unsigned ix = usings->length (); ix--;)
598 queue = queue_namespace (queue, depth, (*usings)[ix]);
600 return queue;
603 /* Unqualified namespace lookup in SCOPE.
604 1) add scope+inlins to worklist.
605 2) recursively add target of every using directive
606 3) for each worklist item where SCOPE is common ancestor, search it
607 4) if nothing find, scope=parent, goto 1. */
609 bool
610 name_lookup::search_unqualified (tree scope, cp_binding_level *level)
612 /* Make static to avoid continual reallocation. We're not
613 recursive. */
614 static using_queue *queue = NULL;
615 bool found = false;
616 int length = vec_safe_length (queue);
618 /* Queue local using-directives. */
619 for (; level->kind != sk_namespace; level = level->level_chain)
620 queue = queue_usings (queue, SCOPE_DEPTH (scope), level->using_directives);
622 for (; !found; scope = CP_DECL_CONTEXT (scope))
624 gcc_assert (!DECL_NAMESPACE_ALIAS (scope));
625 int depth = SCOPE_DEPTH (scope);
627 /* Queue namespaces reachable from SCOPE. */
628 queue = queue_namespace (queue, depth, scope);
630 /* Search every queued namespace where SCOPE is the common
631 ancestor. Adjust the others. */
632 unsigned ix = length;
635 using_pair &pair = (*queue)[ix];
636 while (pair.first == scope)
638 found |= search_namespace_only (pair.second);
639 pair = queue->pop ();
640 if (ix == queue->length ())
641 goto done;
643 /* The depth is the same as SCOPE, find the parent scope. */
644 if (SCOPE_DEPTH (pair.first) == depth)
645 pair.first = CP_DECL_CONTEXT (pair.first);
646 ix++;
648 while (ix < queue->length ());
649 done:;
650 if (scope == global_namespace)
651 break;
654 vec_safe_truncate (queue, length);
656 return found;
659 /* FNS is a value binding. If it is a (set of overloaded) functions,
660 add them into the current value. */
662 void
663 name_lookup::add_fns (tree fns)
665 if (!fns)
666 return;
667 else if (TREE_CODE (fns) == OVERLOAD)
669 if (TREE_TYPE (fns) != unknown_type_node)
670 fns = OVL_FUNCTION (fns);
672 else if (!DECL_DECLARES_FUNCTION_P (fns))
673 return;
675 /* Only add those that aren't already there. */
676 value = lookup_maybe_add (fns, value);
679 /* Add functions of a namespace to the lookup structure. */
681 void
682 name_lookup::adl_namespace_only (tree scope)
684 mark_seen (scope);
686 /* Look down into inline namespaces. */
687 if (vec<tree, va_gc> *inlinees = DECL_NAMESPACE_INLINEES (scope))
688 for (unsigned ix = inlinees->length (); ix--;)
689 adl_namespace_only ((*inlinees)[ix]);
691 if (cxx_binding *binding = find_namespace_binding (scope, name))
692 add_fns (ovl_skip_hidden (binding->value));
695 /* Find the containing non-inlined namespace, add it and all its
696 inlinees. */
698 void
699 name_lookup::adl_namespace (tree scope)
701 if (seen_p (scope))
702 return;
704 /* Find the containing non-inline namespace. */
705 while (DECL_NAMESPACE_INLINE_P (scope))
706 scope = CP_DECL_CONTEXT (scope);
708 adl_namespace_only (scope);
711 /* Adds the class and its friends to the lookup structure. */
713 void
714 name_lookup::adl_class_only (tree type)
716 /* Backend-built structures, such as __builtin_va_list, aren't
717 affected by all this. */
718 if (!CLASS_TYPE_P (type))
719 return;
721 type = TYPE_MAIN_VARIANT (type);
723 if (see_and_mark (type))
724 return;
726 tree context = decl_namespace_context (type);
727 adl_namespace (context);
729 complete_type (type);
731 /* Add friends. */
732 for (tree list = DECL_FRIENDLIST (TYPE_MAIN_DECL (type)); list;
733 list = TREE_CHAIN (list))
734 if (name == FRIEND_NAME (list))
735 for (tree friends = FRIEND_DECLS (list); friends;
736 friends = TREE_CHAIN (friends))
738 tree fn = TREE_VALUE (friends);
740 /* Only interested in global functions with potentially hidden
741 (i.e. unqualified) declarations. */
742 if (CP_DECL_CONTEXT (fn) != context)
743 continue;
745 /* Template specializations are never found by name lookup.
746 (Templates themselves can be found, but not template
747 specializations.) */
748 if (TREE_CODE (fn) == FUNCTION_DECL && DECL_USE_TEMPLATE (fn))
749 continue;
751 add_fns (fn);
755 /* Adds the class and its bases to the lookup structure.
756 Returns true on error. */
758 void
759 name_lookup::adl_bases (tree type)
761 adl_class_only (type);
763 /* Process baseclasses. */
764 if (tree binfo = TYPE_BINFO (type))
766 tree base_binfo;
767 int i;
769 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
770 adl_bases (BINFO_TYPE (base_binfo));
774 /* Adds everything associated with a class argument type to the lookup
775 structure. Returns true on error.
777 If T is a class type (including unions), its associated classes are: the
778 class itself; the class of which it is a member, if any; and its direct
779 and indirect base classes. Its associated namespaces are the namespaces
780 of which its associated classes are members. Furthermore, if T is a
781 class template specialization, its associated namespaces and classes
782 also include: the namespaces and classes associated with the types of
783 the template arguments provided for template type parameters (excluding
784 template template parameters); the namespaces of which any template
785 template arguments are members; and the classes of which any member
786 templates used as template template arguments are members. [ Note:
787 non-type template arguments do not contribute to the set of associated
788 namespaces. --end note] */
790 void
791 name_lookup::adl_class (tree type)
793 /* Backend build structures, such as __builtin_va_list, aren't
794 affected by all this. */
795 if (!CLASS_TYPE_P (type))
796 return;
798 type = TYPE_MAIN_VARIANT (type);
799 /* We don't set found here because we have to have set seen first,
800 which is done in the adl_bases walk. */
801 if (found_p (type))
802 return;
804 adl_bases (type);
805 mark_found (type);
807 if (TYPE_CLASS_SCOPE_P (type))
808 adl_class_only (TYPE_CONTEXT (type));
810 /* Process template arguments. */
811 if (CLASSTYPE_TEMPLATE_INFO (type)
812 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
814 tree list = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
815 for (int i = 0; i < TREE_VEC_LENGTH (list); ++i)
816 adl_template_arg (TREE_VEC_ELT (list, i));
820 void
821 name_lookup::adl_expr (tree expr)
823 if (!expr)
824 return;
826 gcc_assert (!TYPE_P (expr));
828 if (TREE_TYPE (expr) != unknown_type_node)
830 adl_type (TREE_TYPE (expr));
831 return;
834 if (TREE_CODE (expr) == ADDR_EXPR)
835 expr = TREE_OPERAND (expr, 0);
836 if (TREE_CODE (expr) == COMPONENT_REF
837 || TREE_CODE (expr) == OFFSET_REF)
838 expr = TREE_OPERAND (expr, 1);
839 expr = MAYBE_BASELINK_FUNCTIONS (expr);
841 if (OVL_P (expr))
842 for (lkp_iterator iter (expr); iter; ++iter)
843 adl_type (TREE_TYPE (*iter));
844 else if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
846 /* The working paper doesn't currently say how to handle
847 template-id arguments. The sensible thing would seem to be
848 to handle the list of template candidates like a normal
849 overload set, and handle the template arguments like we do
850 for class template specializations. */
852 /* First the templates. */
853 adl_expr (TREE_OPERAND (expr, 0));
855 /* Now the arguments. */
856 if (tree args = TREE_OPERAND (expr, 1))
857 for (int ix = TREE_VEC_LENGTH (args); ix--;)
858 adl_template_arg (TREE_VEC_ELT (args, ix));
862 void
863 name_lookup::adl_type (tree type)
865 if (!type)
866 return;
868 if (TYPE_PTRDATAMEM_P (type))
870 /* Pointer to member: associate class type and value type. */
871 adl_type (TYPE_PTRMEM_CLASS_TYPE (type));
872 adl_type (TYPE_PTRMEM_POINTED_TO_TYPE (type));
873 return;
876 switch (TREE_CODE (type))
878 case RECORD_TYPE:
879 if (TYPE_PTRMEMFUNC_P (type))
881 adl_type (TYPE_PTRMEMFUNC_FN_TYPE (type));
882 return;
884 /* FALLTHRU */
885 case UNION_TYPE:
886 adl_class (type);
887 return;
889 case METHOD_TYPE:
890 /* The basetype is referenced in the first arg type, so just
891 fall through. */
892 case FUNCTION_TYPE:
893 /* Associate the parameter types. */
894 for (tree args = TYPE_ARG_TYPES (type); args; args = TREE_CHAIN (args))
895 adl_type (TREE_VALUE (args));
896 /* FALLTHROUGH */
898 case POINTER_TYPE:
899 case REFERENCE_TYPE:
900 case ARRAY_TYPE:
901 adl_type (TREE_TYPE (type));
902 return;
904 case ENUMERAL_TYPE:
905 if (TYPE_CLASS_SCOPE_P (type))
906 adl_class_only (TYPE_CONTEXT (type));
907 adl_namespace (decl_namespace_context (type));
908 return;
910 case LANG_TYPE:
911 gcc_assert (type == unknown_type_node
912 || type == init_list_type_node);
913 return;
915 case TYPE_PACK_EXPANSION:
916 adl_type (PACK_EXPANSION_PATTERN (type));
917 return;
919 default:
920 break;
924 /* Adds everything associated with a template argument to the lookup
925 structure. */
927 void
928 name_lookup::adl_template_arg (tree arg)
930 /* [basic.lookup.koenig]
932 If T is a template-id, its associated namespaces and classes are
933 ... the namespaces and classes associated with the types of the
934 template arguments provided for template type parameters
935 (excluding template template parameters); the namespaces in which
936 any template template arguments are defined; and the classes in
937 which any member templates used as template template arguments
938 are defined. [Note: non-type template arguments do not
939 contribute to the set of associated namespaces. ] */
941 /* Consider first template template arguments. */
942 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
943 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
945 else if (TREE_CODE (arg) == TEMPLATE_DECL)
947 tree ctx = CP_DECL_CONTEXT (arg);
949 /* It's not a member template. */
950 if (TREE_CODE (ctx) == NAMESPACE_DECL)
951 adl_namespace (ctx);
952 /* Otherwise, it must be member template. */
953 else
954 adl_class_only (ctx);
956 /* It's an argument pack; handle it recursively. */
957 else if (ARGUMENT_PACK_P (arg))
959 tree args = ARGUMENT_PACK_ARGS (arg);
960 int i, len = TREE_VEC_LENGTH (args);
961 for (i = 0; i < len; ++i)
962 adl_template_arg (TREE_VEC_ELT (args, i));
964 /* It's not a template template argument, but it is a type template
965 argument. */
966 else if (TYPE_P (arg))
967 adl_type (arg);
970 /* Perform ADL lookup. FNS is the existing lookup result and ARGS are
971 the call arguments. */
973 tree
974 name_lookup::search_adl (tree fns, vec<tree, va_gc> *args)
976 lookup_mark (fns, true);
977 value = fns;
979 unsigned ix;
980 tree arg;
982 FOR_EACH_VEC_ELT_REVERSE (*args, ix, arg)
983 /* OMP reduction operators put an ADL-significant type as the
984 first arg. */
985 if (TYPE_P (arg))
986 adl_type (arg);
987 else
988 adl_expr (arg);
990 fns = value;
991 lookup_mark (fns, false);
993 return fns;
996 static bool qualified_namespace_lookup (tree, name_lookup *);
997 static void consider_binding_level (tree name,
998 best_match <tree, const char *> &bm,
999 cp_binding_level *lvl,
1000 bool look_within_fields,
1001 enum lookup_name_fuzzy_kind kind);
1002 static void diagnose_name_conflict (tree, tree);
1004 /* ADL lookup of NAME. FNS is the result of regular lookup, and we
1005 don't add duplicates to it. ARGS is the vector of call
1006 arguments (which will not be empty). */
1008 tree
1009 lookup_arg_dependent (tree name, tree fns, vec<tree, va_gc> *args)
1011 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
1012 name_lookup lookup (name);
1013 fns = lookup.search_adl (fns, args);
1014 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
1015 return fns;
1018 /* Compute the chain index of a binding_entry given the HASH value of its
1019 name and the total COUNT of chains. COUNT is assumed to be a power
1020 of 2. */
1022 #define ENTRY_INDEX(HASH, COUNT) (((HASH) >> 3) & ((COUNT) - 1))
1024 /* A free list of "binding_entry"s awaiting for re-use. */
1026 static GTY((deletable)) binding_entry free_binding_entry = NULL;
1028 /* The binding oracle; see cp-tree.h. */
1030 cp_binding_oracle_function *cp_binding_oracle;
1032 /* If we have a binding oracle, ask it for all namespace-scoped
1033 definitions of NAME. */
1035 static inline void
1036 query_oracle (tree name)
1038 if (!cp_binding_oracle)
1039 return;
1041 /* LOOKED_UP holds the set of identifiers that we have already
1042 looked up with the oracle. */
1043 static hash_set<tree> looked_up;
1044 if (looked_up.add (name))
1045 return;
1047 cp_binding_oracle (CP_ORACLE_IDENTIFIER, name);
1050 /* Create a binding_entry object for (NAME, TYPE). */
1052 static inline binding_entry
1053 binding_entry_make (tree name, tree type)
1055 binding_entry entry;
1057 if (free_binding_entry)
1059 entry = free_binding_entry;
1060 free_binding_entry = entry->chain;
1062 else
1063 entry = ggc_alloc<binding_entry_s> ();
1065 entry->name = name;
1066 entry->type = type;
1067 entry->chain = NULL;
1069 return entry;
1072 /* Put ENTRY back on the free list. */
1073 #if 0
1074 static inline void
1075 binding_entry_free (binding_entry entry)
1077 entry->name = NULL;
1078 entry->type = NULL;
1079 entry->chain = free_binding_entry;
1080 free_binding_entry = entry;
1082 #endif
1084 /* The datatype used to implement the mapping from names to types at
1085 a given scope. */
1086 struct GTY(()) binding_table_s {
1087 /* Array of chains of "binding_entry"s */
1088 binding_entry * GTY((length ("%h.chain_count"))) chain;
1090 /* The number of chains in this table. This is the length of the
1091 member "chain" considered as an array. */
1092 size_t chain_count;
1094 /* Number of "binding_entry"s in this table. */
1095 size_t entry_count;
1098 /* Construct TABLE with an initial CHAIN_COUNT. */
1100 static inline void
1101 binding_table_construct (binding_table table, size_t chain_count)
1103 table->chain_count = chain_count;
1104 table->entry_count = 0;
1105 table->chain = ggc_cleared_vec_alloc<binding_entry> (table->chain_count);
1108 /* Make TABLE's entries ready for reuse. */
1109 #if 0
1110 static void
1111 binding_table_free (binding_table table)
1113 size_t i;
1114 size_t count;
1116 if (table == NULL)
1117 return;
1119 for (i = 0, count = table->chain_count; i < count; ++i)
1121 binding_entry temp = table->chain[i];
1122 while (temp != NULL)
1124 binding_entry entry = temp;
1125 temp = entry->chain;
1126 binding_entry_free (entry);
1128 table->chain[i] = NULL;
1130 table->entry_count = 0;
1132 #endif
1134 /* Allocate a table with CHAIN_COUNT, assumed to be a power of two. */
1136 static inline binding_table
1137 binding_table_new (size_t chain_count)
1139 binding_table table = ggc_alloc<binding_table_s> ();
1140 table->chain = NULL;
1141 binding_table_construct (table, chain_count);
1142 return table;
1145 /* Expand TABLE to twice its current chain_count. */
1147 static void
1148 binding_table_expand (binding_table table)
1150 const size_t old_chain_count = table->chain_count;
1151 const size_t old_entry_count = table->entry_count;
1152 const size_t new_chain_count = 2 * old_chain_count;
1153 binding_entry *old_chains = table->chain;
1154 size_t i;
1156 binding_table_construct (table, new_chain_count);
1157 for (i = 0; i < old_chain_count; ++i)
1159 binding_entry entry = old_chains[i];
1160 for (; entry != NULL; entry = old_chains[i])
1162 const unsigned int hash = IDENTIFIER_HASH_VALUE (entry->name);
1163 const size_t j = ENTRY_INDEX (hash, new_chain_count);
1165 old_chains[i] = entry->chain;
1166 entry->chain = table->chain[j];
1167 table->chain[j] = entry;
1170 table->entry_count = old_entry_count;
1173 /* Insert a binding for NAME to TYPE into TABLE. */
1175 static void
1176 binding_table_insert (binding_table table, tree name, tree type)
1178 const unsigned int hash = IDENTIFIER_HASH_VALUE (name);
1179 const size_t i = ENTRY_INDEX (hash, table->chain_count);
1180 binding_entry entry = binding_entry_make (name, type);
1182 entry->chain = table->chain[i];
1183 table->chain[i] = entry;
1184 ++table->entry_count;
1186 if (3 * table->chain_count < 5 * table->entry_count)
1187 binding_table_expand (table);
1190 /* Return the binding_entry, if any, that maps NAME. */
1192 binding_entry
1193 binding_table_find (binding_table table, tree name)
1195 const unsigned int hash = IDENTIFIER_HASH_VALUE (name);
1196 binding_entry entry = table->chain[ENTRY_INDEX (hash, table->chain_count)];
1198 while (entry != NULL && entry->name != name)
1199 entry = entry->chain;
1201 return entry;
1204 /* Apply PROC -- with DATA -- to all entries in TABLE. */
1206 void
1207 binding_table_foreach (binding_table table, bt_foreach_proc proc, void *data)
1209 size_t chain_count;
1210 size_t i;
1212 if (!table)
1213 return;
1215 chain_count = table->chain_count;
1216 for (i = 0; i < chain_count; ++i)
1218 binding_entry entry = table->chain[i];
1219 for (; entry != NULL; entry = entry->chain)
1220 proc (entry, data);
1224 #ifndef ENABLE_SCOPE_CHECKING
1225 # define ENABLE_SCOPE_CHECKING 0
1226 #else
1227 # define ENABLE_SCOPE_CHECKING 1
1228 #endif
1230 /* A free list of "cxx_binding"s, connected by their PREVIOUS. */
1232 static GTY((deletable)) cxx_binding *free_bindings;
1234 /* Initialize VALUE and TYPE field for BINDING, and set the PREVIOUS
1235 field to NULL. */
1237 static inline void
1238 cxx_binding_init (cxx_binding *binding, tree value, tree type)
1240 binding->value = value;
1241 binding->type = type;
1242 binding->previous = NULL;
1245 /* (GC)-allocate a binding object with VALUE and TYPE member initialized. */
1247 static cxx_binding *
1248 cxx_binding_make (tree value, tree type)
1250 cxx_binding *binding;
1251 if (free_bindings)
1253 binding = free_bindings;
1254 free_bindings = binding->previous;
1256 else
1257 binding = ggc_alloc<cxx_binding> ();
1259 cxx_binding_init (binding, value, type);
1261 return binding;
1264 /* Put BINDING back on the free list. */
1266 static inline void
1267 cxx_binding_free (cxx_binding *binding)
1269 binding->scope = NULL;
1270 binding->previous = free_bindings;
1271 free_bindings = binding;
1274 /* Create a new binding for NAME (with the indicated VALUE and TYPE
1275 bindings) in the class scope indicated by SCOPE. */
1277 static cxx_binding *
1278 new_class_binding (tree name, tree value, tree type, cp_binding_level *scope)
1280 cp_class_binding cb = {cxx_binding_make (value, type), name};
1281 cxx_binding *binding = cb.base;
1282 vec_safe_push (scope->class_shadowed, cb);
1283 binding->scope = scope;
1284 return binding;
1287 /* Make DECL the innermost binding for ID. The LEVEL is the binding
1288 level at which this declaration is being bound. */
1290 void
1291 push_binding (tree id, tree decl, cp_binding_level* level)
1293 cxx_binding *binding;
1295 if (level != class_binding_level)
1297 binding = cxx_binding_make (decl, NULL_TREE);
1298 binding->scope = level;
1300 else
1301 binding = new_class_binding (id, decl, /*type=*/NULL_TREE, level);
1303 /* Now, fill in the binding information. */
1304 binding->previous = IDENTIFIER_BINDING (id);
1305 INHERITED_VALUE_BINDING_P (binding) = 0;
1306 LOCAL_BINDING_P (binding) = (level != class_binding_level);
1308 /* And put it on the front of the list of bindings for ID. */
1309 IDENTIFIER_BINDING (id) = binding;
1312 /* Remove the binding for DECL which should be the innermost binding
1313 for ID. */
1315 void
1316 pop_local_binding (tree id, tree decl)
1318 cxx_binding *binding;
1320 if (id == NULL_TREE)
1321 /* It's easiest to write the loops that call this function without
1322 checking whether or not the entities involved have names. We
1323 get here for such an entity. */
1324 return;
1326 /* Get the innermost binding for ID. */
1327 binding = IDENTIFIER_BINDING (id);
1329 /* The name should be bound. */
1330 gcc_assert (binding != NULL);
1332 /* The DECL will be either the ordinary binding or the type
1333 binding for this identifier. Remove that binding. */
1334 if (binding->value == decl)
1335 binding->value = NULL_TREE;
1336 else
1338 gcc_assert (binding->type == decl);
1339 binding->type = NULL_TREE;
1342 if (!binding->value && !binding->type)
1344 /* We're completely done with the innermost binding for this
1345 identifier. Unhook it from the list of bindings. */
1346 IDENTIFIER_BINDING (id) = binding->previous;
1348 /* Add it to the free list. */
1349 cxx_binding_free (binding);
1353 /* Remove the bindings for the decls of the current level and leave
1354 the current scope. */
1356 void
1357 pop_bindings_and_leave_scope (void)
1359 for (tree t = get_local_decls (); t; t = DECL_CHAIN (t))
1361 tree decl = TREE_CODE (t) == TREE_LIST ? TREE_VALUE (t) : t;
1362 tree name = OVL_NAME (decl);
1364 pop_local_binding (name, decl);
1367 leave_scope ();
1370 /* Strip non dependent using declarations. If DECL is dependent,
1371 surreptitiously create a typename_type and return it. */
1373 tree
1374 strip_using_decl (tree decl)
1376 if (decl == NULL_TREE)
1377 return NULL_TREE;
1379 while (TREE_CODE (decl) == USING_DECL && !DECL_DEPENDENT_P (decl))
1380 decl = USING_DECL_DECLS (decl);
1382 if (TREE_CODE (decl) == USING_DECL && DECL_DEPENDENT_P (decl)
1383 && USING_DECL_TYPENAME_P (decl))
1385 /* We have found a type introduced by a using
1386 declaration at class scope that refers to a dependent
1387 type.
1389 using typename :: [opt] nested-name-specifier unqualified-id ;
1391 decl = make_typename_type (TREE_TYPE (decl),
1392 DECL_NAME (decl),
1393 typename_type, tf_error);
1394 if (decl != error_mark_node)
1395 decl = TYPE_NAME (decl);
1398 return decl;
1401 /* Return true if OVL is an overload for an anticipated builtin. */
1403 static bool
1404 anticipated_builtin_p (tree ovl)
1406 if (TREE_CODE (ovl) != OVERLOAD)
1407 return false;
1409 if (!OVL_HIDDEN_P (ovl))
1410 return false;
1412 tree fn = OVL_FUNCTION (ovl);
1413 gcc_checking_assert (DECL_ANTICIPATED (fn));
1415 if (DECL_HIDDEN_FRIEND_P (fn))
1416 return false;
1418 return true;
1421 /* BINDING records an existing declaration for a name in the current scope.
1422 But, DECL is another declaration for that same identifier in the
1423 same scope. This is the `struct stat' hack whereby a non-typedef
1424 class name or enum-name can be bound at the same level as some other
1425 kind of entity.
1426 3.3.7/1
1428 A class name (9.1) or enumeration name (7.2) can be hidden by the
1429 name of an object, function, or enumerator declared in the same scope.
1430 If a class or enumeration name and an object, function, or enumerator
1431 are declared in the same scope (in any order) with the same name, the
1432 class or enumeration name is hidden wherever the object, function, or
1433 enumerator name is visible.
1435 It's the responsibility of the caller to check that
1436 inserting this name is valid here. Returns nonzero if the new binding
1437 was successful. */
1439 static bool
1440 supplement_binding_1 (cxx_binding *binding, tree decl)
1442 tree bval = binding->value;
1443 bool ok = true;
1444 tree target_bval = strip_using_decl (bval);
1445 tree target_decl = strip_using_decl (decl);
1447 if (TREE_CODE (target_decl) == TYPE_DECL && DECL_ARTIFICIAL (target_decl)
1448 && target_decl != target_bval
1449 && (TREE_CODE (target_bval) != TYPE_DECL
1450 /* We allow pushing an enum multiple times in a class
1451 template in order to handle late matching of underlying
1452 type on an opaque-enum-declaration followed by an
1453 enum-specifier. */
1454 || (processing_template_decl
1455 && TREE_CODE (TREE_TYPE (target_decl)) == ENUMERAL_TYPE
1456 && TREE_CODE (TREE_TYPE (target_bval)) == ENUMERAL_TYPE
1457 && (dependent_type_p (ENUM_UNDERLYING_TYPE
1458 (TREE_TYPE (target_decl)))
1459 || dependent_type_p (ENUM_UNDERLYING_TYPE
1460 (TREE_TYPE (target_bval)))))))
1461 /* The new name is the type name. */
1462 binding->type = decl;
1463 else if (/* TARGET_BVAL is null when push_class_level_binding moves
1464 an inherited type-binding out of the way to make room
1465 for a new value binding. */
1466 !target_bval
1467 /* TARGET_BVAL is error_mark_node when TARGET_DECL's name
1468 has been used in a non-class scope prior declaration.
1469 In that case, we should have already issued a
1470 diagnostic; for graceful error recovery purpose, pretend
1471 this was the intended declaration for that name. */
1472 || target_bval == error_mark_node
1473 /* If TARGET_BVAL is anticipated but has not yet been
1474 declared, pretend it is not there at all. */
1475 || anticipated_builtin_p (target_bval))
1476 binding->value = decl;
1477 else if (TREE_CODE (target_bval) == TYPE_DECL
1478 && DECL_ARTIFICIAL (target_bval)
1479 && target_decl != target_bval
1480 && (TREE_CODE (target_decl) != TYPE_DECL
1481 || same_type_p (TREE_TYPE (target_decl),
1482 TREE_TYPE (target_bval))))
1484 /* The old binding was a type name. It was placed in
1485 VALUE field because it was thought, at the point it was
1486 declared, to be the only entity with such a name. Move the
1487 type name into the type slot; it is now hidden by the new
1488 binding. */
1489 binding->type = bval;
1490 binding->value = decl;
1491 binding->value_is_inherited = false;
1493 else if (TREE_CODE (target_bval) == TYPE_DECL
1494 && TREE_CODE (target_decl) == TYPE_DECL
1495 && DECL_NAME (target_decl) == DECL_NAME (target_bval)
1496 && binding->scope->kind != sk_class
1497 && (same_type_p (TREE_TYPE (target_decl), TREE_TYPE (target_bval))
1498 /* If either type involves template parameters, we must
1499 wait until instantiation. */
1500 || uses_template_parms (TREE_TYPE (target_decl))
1501 || uses_template_parms (TREE_TYPE (target_bval))))
1502 /* We have two typedef-names, both naming the same type to have
1503 the same name. In general, this is OK because of:
1505 [dcl.typedef]
1507 In a given scope, a typedef specifier can be used to redefine
1508 the name of any type declared in that scope to refer to the
1509 type to which it already refers.
1511 However, in class scopes, this rule does not apply due to the
1512 stricter language in [class.mem] prohibiting redeclarations of
1513 members. */
1514 ok = false;
1515 /* There can be two block-scope declarations of the same variable,
1516 so long as they are `extern' declarations. However, there cannot
1517 be two declarations of the same static data member:
1519 [class.mem]
1521 A member shall not be declared twice in the
1522 member-specification. */
1523 else if (VAR_P (target_decl)
1524 && VAR_P (target_bval)
1525 && DECL_EXTERNAL (target_decl) && DECL_EXTERNAL (target_bval)
1526 && !DECL_CLASS_SCOPE_P (target_decl))
1528 duplicate_decls (decl, binding->value, /*newdecl_is_friend=*/false);
1529 ok = false;
1531 else if (TREE_CODE (decl) == NAMESPACE_DECL
1532 && TREE_CODE (bval) == NAMESPACE_DECL
1533 && DECL_NAMESPACE_ALIAS (decl)
1534 && DECL_NAMESPACE_ALIAS (bval)
1535 && ORIGINAL_NAMESPACE (bval) == ORIGINAL_NAMESPACE (decl))
1536 /* [namespace.alias]
1538 In a declarative region, a namespace-alias-definition can be
1539 used to redefine a namespace-alias declared in that declarative
1540 region to refer only to the namespace to which it already
1541 refers. */
1542 ok = false;
1543 else if (maybe_remove_implicit_alias (bval))
1545 /* There was a mangling compatibility alias using this mangled name,
1546 but now we have a real decl that wants to use it instead. */
1547 binding->value = decl;
1549 else
1551 if (!error_operand_p (bval))
1552 diagnose_name_conflict (decl, bval);
1553 ok = false;
1556 return ok;
1559 /* Diagnose a name conflict between DECL and BVAL. */
1561 static void
1562 diagnose_name_conflict (tree decl, tree bval)
1564 if (TREE_CODE (decl) == TREE_CODE (bval)
1565 && (TREE_CODE (decl) != TYPE_DECL
1566 || (DECL_ARTIFICIAL (decl) && DECL_ARTIFICIAL (bval))
1567 || (!DECL_ARTIFICIAL (decl) && !DECL_ARTIFICIAL (bval)))
1568 && !DECL_DECLARES_FUNCTION_P (decl)
1569 && CP_DECL_CONTEXT (decl) == CP_DECL_CONTEXT (bval))
1570 error ("redeclaration of %q#D", decl);
1571 else
1572 error ("%q#D conflicts with a previous declaration", decl);
1574 inform (location_of (bval), "previous declaration %q#D", bval);
1577 /* Wrapper for supplement_binding_1. */
1579 static bool
1580 supplement_binding (cxx_binding *binding, tree decl)
1582 bool ret;
1583 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
1584 ret = supplement_binding_1 (binding, decl);
1585 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
1586 return ret;
1589 /* Replace BINDING's current value on its scope's name list with
1590 NEWVAL. */
1592 static void
1593 update_local_overload (cxx_binding *binding, tree newval)
1595 tree *d;
1597 for (d = &binding->scope->names; ; d = &TREE_CHAIN (*d))
1598 if (*d == binding->value)
1600 /* Stitch new list node in. */
1601 *d = tree_cons (NULL_TREE, NULL_TREE, TREE_CHAIN (*d));
1602 break;
1604 else if (TREE_CODE (*d) == TREE_LIST && TREE_VALUE (*d) == binding->value)
1605 break;
1607 TREE_VALUE (*d) = newval;
1610 /* Compares the parameter-type-lists of ONE and TWO and
1611 returns false if they are different. If the DECLs are template
1612 functions, the return types and the template parameter lists are
1613 compared too (DR 565). */
1615 static bool
1616 matching_fn_p (tree one, tree two)
1618 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (one)),
1619 TYPE_ARG_TYPES (TREE_TYPE (two))))
1620 return false;
1622 if (TREE_CODE (one) == TEMPLATE_DECL
1623 && TREE_CODE (two) == TEMPLATE_DECL)
1625 /* Compare template parms. */
1626 if (!comp_template_parms (DECL_TEMPLATE_PARMS (one),
1627 DECL_TEMPLATE_PARMS (two)))
1628 return false;
1630 /* And return type. */
1631 if (!same_type_p (TREE_TYPE (TREE_TYPE (one)),
1632 TREE_TYPE (TREE_TYPE (two))))
1633 return false;
1636 return true;
1639 /* Push DECL into nonclass LEVEL BINDING. OLD is the current
1640 binding value (possibly with anticipated builtins stripped).
1641 Diagnose conflicts and return updated decl. */
1643 static tree
1644 update_binding (cp_binding_level *level, cxx_binding *binding,
1645 tree old, tree decl, bool is_friend)
1647 tree to_val = decl;
1648 tree to_type = NULL_TREE;
1650 gcc_assert (level->kind != sk_class);
1651 if (old == error_mark_node)
1652 old = NULL_TREE;
1654 if (old && TREE_CODE (old) == TYPE_DECL && DECL_ARTIFICIAL (old))
1656 /* Slide the tdef out of the way. We'll undo this below, if
1657 we're pushing a matching tdef. */
1658 to_type = old;
1659 old = NULL_TREE;
1662 if (DECL_DECLARES_FUNCTION_P (decl))
1664 if (!old)
1666 else if (OVL_P (old))
1668 for (ovl_iterator iter (old); iter; ++iter)
1670 tree fn = *iter;
1672 if (iter.using_p () && matching_fn_p (fn, decl))
1674 /* If a function declaration in namespace scope or
1675 block scope has the same name and the same
1676 parameter-type- list (8.3.5) as a function
1677 introduced by a using-declaration, and the
1678 declarations do not declare the same function,
1679 the program is ill-formed. [namespace.udecl]/14 */
1680 if (tree match = duplicate_decls (decl, fn, is_friend))
1681 return match;
1682 else
1683 /* FIXME: To preserve existing error behavior, we
1684 still push the decl. This might change. */
1685 diagnose_name_conflict (decl, fn);
1689 else
1690 goto conflict;
1692 to_val = ovl_insert (decl, old);
1694 else if (to_type && TREE_CODE (decl) == TYPE_DECL)
1696 /* We thought we wanted to slide an artificial typedef out of
1697 the way, to make way for another typedef. That's not always
1698 what we want to do. */
1699 if (!DECL_ARTIFICIAL (decl))
1700 ; /* Slide. */
1701 else if (same_type_p (TREE_TYPE (to_type), TREE_TYPE (decl)))
1702 /* Two artificial decls to same type. Do nothing. */
1703 return to_type;
1704 else
1705 goto conflict;
1707 else if (!old)
1709 else if (TREE_CODE (decl) == TYPE_DECL && DECL_ARTIFICIAL (decl))
1711 /* Slide DECL into the type slot. */
1712 to_type = decl;
1713 to_val = old;
1715 else if (TREE_CODE (old) != TREE_CODE (decl))
1716 /* Different kinds of decls conflict. */
1717 goto conflict;
1718 else if (TREE_CODE (old) == TYPE_DECL)
1720 if (DECL_ARTIFICIAL (decl))
1722 /* Slide DECL into the type slot instead. */
1723 to_type = decl;
1724 to_val = old;
1726 else if (same_type_p (TREE_TYPE (old), TREE_TYPE (decl)))
1727 /* Two type decls to the same type. Do nothing. */
1728 return old;
1729 else
1730 goto conflict;
1732 else if (TREE_CODE (old) == NAMESPACE_DECL)
1734 if (DECL_NAMESPACE_ALIAS (old) && DECL_NAMESPACE_ALIAS (decl)
1735 && ORIGINAL_NAMESPACE (old) == ORIGINAL_NAMESPACE (decl))
1736 /* In a declarative region, a namespace-alias-definition can be
1737 used to redefine a namespace-alias declared in that declarative
1738 region to refer only to the namespace to which it already
1739 refers. [namespace.alias] */
1740 return old;
1741 else
1742 goto conflict;
1744 else if (TREE_CODE (old) == VAR_DECL)
1746 /* There can be two block-scope declarations of the same
1747 variable, so long as they are `extern' declarations. */
1748 if (!DECL_EXTERNAL (old) || !DECL_EXTERNAL (decl))
1749 goto conflict;
1750 else if (tree match = duplicate_decls (decl, old, false))
1751 return match;
1752 else
1753 goto conflict;
1755 else
1757 conflict:
1758 diagnose_name_conflict (decl, old);
1759 to_val = NULL_TREE;
1762 if (to_val)
1764 if (level->kind != sk_namespace
1765 && !to_type && binding->value && OVL_P (to_val))
1766 update_local_overload (binding, to_val);
1767 else
1769 tree to_add = to_val;
1771 if (level->kind == sk_namespace)
1772 to_add = decl;
1773 else if (to_type == decl)
1774 to_add = decl;
1775 else if (TREE_CODE (to_add) == OVERLOAD)
1776 to_add = build_tree_list (NULL_TREE, to_add);
1778 add_decl_to_level (level, to_add);
1781 if (to_type == binding->type)
1782 to_type = NULL_TREE;
1784 if (to_type)
1786 gcc_checking_assert (TREE_CODE (to_type) == TYPE_DECL
1787 && DECL_ARTIFICIAL (to_type));
1789 tree type = TREE_TYPE (to_type);
1790 if (to_type != decl
1791 && MAYBE_CLASS_TYPE_P (type) && warn_shadow
1792 && (!DECL_IN_SYSTEM_HEADER (decl)
1793 || !DECL_IN_SYSTEM_HEADER (to_type)))
1794 warning (OPT_Wshadow, "%q#D hides constructor for %q#T",
1795 decl, type);
1798 if (to_type)
1799 binding->type = to_type;
1800 binding->value = to_val;
1803 return decl;
1806 /* Map of identifiers to extern C functions (or LISTS thereof). */
1808 static GTY(()) hash_map<lang_identifier *, tree> *extern_c_fns;
1810 /* DECL has C linkage. If we have an existing instance, make sure it
1811 has the same exception specification [7.5, 7.6]. If there's no
1812 instance, add DECL to the map. */
1814 static void
1815 check_extern_c_conflict (tree decl)
1817 /* Ignore artificial or system header decls. */
1818 if (DECL_ARTIFICIAL (decl) || DECL_IN_SYSTEM_HEADER (decl))
1819 return;
1821 if (!extern_c_fns)
1822 extern_c_fns = hash_map<lang_identifier *,tree>::create_ggc (127);
1824 bool existed;
1825 tree *slot = &extern_c_fns->get_or_insert (DECL_NAME (decl), &existed);
1826 if (!existed)
1827 *slot = decl;
1828 else
1830 tree old = *slot;
1831 if (TREE_CODE (old) == TREE_LIST)
1832 old = TREE_VALUE (old);
1834 int mismatch = 0;
1835 if (DECL_CONTEXT (old) == DECL_CONTEXT (decl))
1836 ; /* If they're in the same context, we'll have already complained
1837 about a (possible) mismatch, when inserting the decl. */
1838 else if (!decls_match (decl, old))
1839 mismatch = 1;
1840 else if (!comp_except_specs (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (old)),
1841 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl)),
1842 ce_normal))
1843 mismatch = -1;
1844 else if (DECL_ASSEMBLER_NAME_SET_P (old))
1845 SET_DECL_ASSEMBLER_NAME (decl, DECL_ASSEMBLER_NAME (old));
1847 if (mismatch)
1849 pedwarn (input_location, 0,
1850 "declaration of %q#D with C language linkage", decl);
1851 pedwarn (DECL_SOURCE_LOCATION (old), 0,
1852 "conflicts with previous declaration %q#D", old);
1853 if (mismatch < 0)
1854 pedwarn (input_location, 0,
1855 "due to different exception specifications");
1857 else
1858 /* Chain it on for c_linkage_binding's use. */
1859 *slot = tree_cons (NULL_TREE, decl, *slot);
1863 /* Returns a list of C-linkage decls with the name NAME. Used in
1864 c-family/c-pragma.c to implement redefine_extname pragma. */
1866 tree
1867 c_linkage_bindings (tree name)
1869 if (extern_c_fns)
1870 if (tree *slot = extern_c_fns->get (name))
1871 return *slot;
1872 return NULL_TREE;
1875 /* DECL is being declared at a local scope. Emit suitable shadow
1876 warnings. */
1878 static void
1879 check_local_shadow (tree decl)
1881 /* Don't complain about the parms we push and then pop
1882 while tentatively parsing a function declarator. */
1883 if (TREE_CODE (decl) == PARM_DECL && !DECL_CONTEXT (decl))
1884 return;
1886 /* Inline decls shadow nothing. */
1887 if (DECL_FROM_INLINE (decl))
1888 return;
1890 /* External decls are something else. */
1891 if (DECL_EXTERNAL (decl))
1892 return;
1894 tree old = NULL_TREE;
1895 cp_binding_level *old_scope = NULL;
1896 if (cxx_binding *binding = outer_binding (DECL_NAME (decl), NULL, true))
1898 old = binding->value;
1899 old_scope = binding->scope;
1901 while (old && VAR_P (old) && DECL_DEAD_FOR_LOCAL (old))
1902 old = DECL_SHADOWED_FOR_VAR (old);
1904 tree shadowed = NULL_TREE;
1905 if (old
1906 && (TREE_CODE (old) == PARM_DECL
1907 || VAR_P (old)
1908 || (TREE_CODE (old) == TYPE_DECL
1909 && (!DECL_ARTIFICIAL (old)
1910 || TREE_CODE (decl) == TYPE_DECL)))
1911 && (!DECL_ARTIFICIAL (decl)
1912 || DECL_IMPLICIT_TYPEDEF_P (decl)
1913 || (VAR_P (decl) && DECL_ANON_UNION_VAR_P (decl))))
1915 /* DECL shadows a local thing possibly of interest. */
1917 /* Don't complain if it's from an enclosing function. */
1918 if (DECL_CONTEXT (old) == current_function_decl
1919 && TREE_CODE (decl) != PARM_DECL
1920 && TREE_CODE (old) == PARM_DECL)
1922 /* Go to where the parms should be and see if we find
1923 them there. */
1924 cp_binding_level *b = current_binding_level->level_chain;
1926 if (FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
1927 /* Skip the ctor/dtor cleanup level. */
1928 b = b->level_chain;
1930 /* ARM $8.3 */
1931 if (b->kind == sk_function_parms)
1933 error ("declaration of %q#D shadows a parameter", decl);
1934 return;
1938 /* The local structure or class can't use parameters of
1939 the containing function anyway. */
1940 if (DECL_CONTEXT (old) != current_function_decl)
1942 for (cp_binding_level *scope = current_binding_level;
1943 scope != old_scope; scope = scope->level_chain)
1944 if (scope->kind == sk_class
1945 && !LAMBDA_TYPE_P (scope->this_entity))
1946 return;
1948 /* Error if redeclaring a local declared in a
1949 init-statement or in the condition of an if or
1950 switch statement when the new declaration is in the
1951 outermost block of the controlled statement.
1952 Redeclaring a variable from a for or while condition is
1953 detected elsewhere. */
1954 else if (VAR_P (old)
1955 && old_scope == current_binding_level->level_chain
1956 && (old_scope->kind == sk_cond || old_scope->kind == sk_for))
1958 error ("redeclaration of %q#D", decl);
1959 inform (DECL_SOURCE_LOCATION (old),
1960 "%q#D previously declared here", old);
1961 return;
1963 /* C++11:
1964 3.3.3/3: The name declared in an exception-declaration (...)
1965 shall not be redeclared in the outermost block of the handler.
1966 3.3.3/2: A parameter name shall not be redeclared (...) in
1967 the outermost block of any handler associated with a
1968 function-try-block.
1969 3.4.1/15: The function parameter names shall not be redeclared
1970 in the exception-declaration nor in the outermost block of a
1971 handler for the function-try-block. */
1972 else if ((TREE_CODE (old) == VAR_DECL
1973 && old_scope == current_binding_level->level_chain
1974 && old_scope->kind == sk_catch)
1975 || (TREE_CODE (old) == PARM_DECL
1976 && (current_binding_level->kind == sk_catch
1977 || current_binding_level->level_chain->kind == sk_catch)
1978 && in_function_try_handler))
1980 if (permerror (input_location, "redeclaration of %q#D", decl))
1981 inform (DECL_SOURCE_LOCATION (old),
1982 "%q#D previously declared here", old);
1983 return;
1986 /* If '-Wshadow=compatible-local' is specified without other
1987 -Wshadow= flags, we will warn only when the type of the
1988 shadowing variable (DECL) can be converted to that of the
1989 shadowed parameter (OLD_LOCAL). The reason why we only check
1990 if DECL's type can be converted to OLD_LOCAL's type (but not the
1991 other way around) is because when users accidentally shadow a
1992 parameter, more than often they would use the variable
1993 thinking (mistakenly) it's still the parameter. It would be
1994 rare that users would use the variable in the place that
1995 expects the parameter but thinking it's a new decl. */
1997 enum opt_code warning_code;
1998 if (warn_shadow)
1999 warning_code = OPT_Wshadow;
2000 else if (warn_shadow_local)
2001 warning_code = OPT_Wshadow_local;
2002 else if (warn_shadow_compatible_local
2003 && can_convert (TREE_TYPE (old), TREE_TYPE (decl), tf_none))
2004 warning_code = OPT_Wshadow_compatible_local;
2005 else
2006 return;
2008 const char *msg;
2009 if (TREE_CODE (old) == PARM_DECL)
2010 msg = "declaration of %q#D shadows a parameter";
2011 else if (is_capture_proxy (old))
2012 msg = "declaration of %qD shadows a lambda capture";
2013 else
2014 msg = "declaration of %qD shadows a previous local";
2016 if (warning_at (input_location, warning_code, msg, decl))
2018 shadowed = old;
2019 goto inform_shadowed;
2021 return;
2024 if (!warn_shadow)
2025 return;
2027 /* Don't warn for artificial things that are not implicit typedefs. */
2028 if (DECL_ARTIFICIAL (decl) && !DECL_IMPLICIT_TYPEDEF_P (decl))
2029 return;
2031 if (nonlambda_method_basetype ())
2032 if (tree member = lookup_member (current_nonlambda_class_type (),
2033 DECL_NAME (decl), /*protect=*/0,
2034 /*want_type=*/false, tf_warning_or_error))
2036 member = MAYBE_BASELINK_FUNCTIONS (member);
2038 /* Warn if a variable shadows a non-function, or the variable
2039 is a function or a pointer-to-function. */
2040 if ((TREE_CODE (member) != FUNCTION_DECL
2041 && TREE_CODE (member) != OVERLOAD)
2042 || TREE_CODE (decl) == FUNCTION_DECL
2043 || TYPE_PTRFN_P (TREE_TYPE (decl))
2044 || TYPE_PTRMEMFUNC_P (TREE_TYPE (decl)))
2046 if (warning_at (input_location, OPT_Wshadow,
2047 "declaration of %qD shadows a member of %qT",
2048 decl, current_nonlambda_class_type ())
2049 && DECL_P (member))
2051 shadowed = member;
2052 goto inform_shadowed;
2055 return;
2058 /* Now look for a namespace shadow. */
2059 old = get_namespace_binding (current_namespace, DECL_NAME (decl));
2060 if (old
2061 && (VAR_P (old)
2062 || (TREE_CODE (old) == TYPE_DECL
2063 && (!DECL_ARTIFICIAL (old)
2064 || TREE_CODE (decl) == TYPE_DECL)))
2065 && !instantiating_current_function_p ())
2066 /* XXX shadow warnings in outer-more namespaces */
2068 if (warning_at (input_location, OPT_Wshadow,
2069 "declaration of %qD shadows a global declaration",
2070 decl))
2072 shadowed = old;
2073 goto inform_shadowed;
2075 return;
2078 return;
2080 inform_shadowed:
2081 inform (DECL_SOURCE_LOCATION (shadowed), "shadowed declaration is here");
2084 /* DECL is being pushed inside function CTX. Set its context, if
2085 needed. */
2087 static void
2088 set_decl_context_in_fn (tree ctx, tree decl)
2090 if (!DECL_CONTEXT (decl)
2091 /* A local declaration for a function doesn't constitute
2092 nesting. */
2093 && TREE_CODE (decl) != FUNCTION_DECL
2094 /* A local declaration for an `extern' variable is in the
2095 scope of the current namespace, not the current
2096 function. */
2097 && !(VAR_P (decl) && DECL_EXTERNAL (decl))
2098 /* When parsing the parameter list of a function declarator,
2099 don't set DECL_CONTEXT to an enclosing function. When we
2100 push the PARM_DECLs in order to process the function body,
2101 current_binding_level->this_entity will be set. */
2102 && !(TREE_CODE (decl) == PARM_DECL
2103 && current_binding_level->kind == sk_function_parms
2104 && current_binding_level->this_entity == NULL))
2105 DECL_CONTEXT (decl) = ctx;
2107 /* If this is the declaration for a namespace-scope function,
2108 but the declaration itself is in a local scope, mark the
2109 declaration. */
2110 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (decl))
2111 DECL_LOCAL_FUNCTION_P (decl) = 1;
2114 /* DECL is a local-scope decl with linkage. SHADOWED is true if the
2115 name is already bound at the current level.
2117 [basic.link] If there is a visible declaration of an entity with
2118 linkage having the same name and type, ignoring entities declared
2119 outside the innermost enclosing namespace scope, the block scope
2120 declaration declares that same entity and receives the linkage of
2121 the previous declaration.
2123 Also, make sure that this decl matches any existing external decl
2124 in the enclosing namespace. */
2126 static void
2127 set_local_extern_decl_linkage (tree decl, bool shadowed)
2129 tree ns_value = decl; /* Unique marker. */
2131 if (!shadowed)
2133 tree loc_value = innermost_non_namespace_value (DECL_NAME (decl));
2134 if (!loc_value)
2136 ns_value
2137 = get_namespace_binding (current_namespace, DECL_NAME (decl));
2138 loc_value = ns_value;
2140 if (loc_value == error_mark_node)
2141 loc_value = NULL_TREE;
2143 for (ovl_iterator iter (loc_value); iter; ++iter)
2144 if (!iter.hidden_p ()
2145 && (TREE_STATIC (*iter) || DECL_EXTERNAL (*iter))
2146 && decls_match (*iter, decl))
2148 /* The standard only says that the local extern inherits
2149 linkage from the previous decl; in particular, default
2150 args are not shared. Add the decl into a hash table to
2151 make sure only the previous decl in this case is seen
2152 by the middle end. */
2153 struct cxx_int_tree_map *h;
2155 /* We inherit the outer decl's linkage. But we're a
2156 different decl. */
2157 TREE_PUBLIC (decl) = TREE_PUBLIC (*iter);
2159 if (cp_function_chain->extern_decl_map == NULL)
2160 cp_function_chain->extern_decl_map
2161 = hash_table<cxx_int_tree_map_hasher>::create_ggc (20);
2163 h = ggc_alloc<cxx_int_tree_map> ();
2164 h->uid = DECL_UID (decl);
2165 h->to = *iter;
2166 cxx_int_tree_map **loc = cp_function_chain->extern_decl_map
2167 ->find_slot (h, INSERT);
2168 *loc = h;
2169 break;
2173 if (TREE_PUBLIC (decl))
2175 /* DECL is externally visible. Make sure it matches a matching
2176 decl in the namespace scpe. We only really need to check
2177 this when inserting the decl, not when we find an existing
2178 match in the current scope. However, in practice we're
2179 going to be inserting a new decl in the majority of cases --
2180 who writes multiple extern decls for the same thing in the
2181 same local scope? Doing it here often avoids a duplicate
2182 namespace lookup. */
2184 /* Avoid repeating a lookup. */
2185 if (ns_value == decl)
2186 ns_value = get_namespace_binding (current_namespace, DECL_NAME (decl));
2188 if (ns_value == error_mark_node)
2189 ns_value = NULL_TREE;
2191 for (ovl_iterator iter (ns_value); iter; ++iter)
2193 tree other = *iter;
2195 if (!(TREE_PUBLIC (other) || DECL_EXTERNAL (other)))
2196 ; /* Not externally visible. */
2197 else if (DECL_EXTERN_C_P (decl) && DECL_EXTERN_C_P (other))
2198 ; /* Both are extern "C", we'll check via that mechanism. */
2199 else if (TREE_CODE (other) != TREE_CODE (decl)
2200 || ((VAR_P (decl) || matching_fn_p (other, decl))
2201 && !comptypes (TREE_TYPE (decl), TREE_TYPE (other),
2202 COMPARE_REDECLARATION)))
2204 if (permerror (DECL_SOURCE_LOCATION (decl),
2205 "local external declaration %q#D", decl))
2206 inform (DECL_SOURCE_LOCATION (other),
2207 "does not match previous declaration %q#D", other);
2208 break;
2214 /* Record DECL as belonging to the current lexical scope. Check for
2215 errors (such as an incompatible declaration for the same name
2216 already seen in the same scope). IS_FRIEND is true if DECL is
2217 declared as a friend.
2219 Returns either DECL or an old decl for the same name. If an old
2220 decl is returned, it may have been smashed to agree with what DECL
2221 says. */
2223 static tree
2224 do_pushdecl (tree decl, bool is_friend)
2226 if (decl == error_mark_node)
2227 return error_mark_node;
2229 if (!DECL_TEMPLATE_PARM_P (decl) && current_function_decl)
2230 set_decl_context_in_fn (current_function_decl, decl);
2232 /* The binding level we will be pushing into. During local class
2233 pushing, we want to push to the containing scope. */
2234 cp_binding_level *level = current_binding_level;
2235 while (level->kind == sk_class)
2236 level = level->level_chain;
2238 if (tree name = DECL_NAME (decl))
2240 cxx_binding *binding = NULL;
2241 tree ns = NULL_TREE; /* Searched namespace. */
2242 tree old = NULL_TREE;
2244 if (level->kind == sk_namespace)
2246 /* We look in the decl's namespace for an existing
2247 declaration, even though we push into the current
2248 namespace. */
2249 ns = (DECL_NAMESPACE_SCOPE_P (decl)
2250 ? CP_DECL_CONTEXT (decl) : current_namespace);
2251 /* Create the binding, if this is current namespace, because
2252 that's where we'll be pushing anyway. */
2253 binding = find_namespace_binding (ns, name, ns == current_namespace);
2255 else
2256 binding = find_local_binding (level, name);
2258 if (binding)
2259 old = binding->value;
2261 if (current_function_decl && VAR_OR_FUNCTION_DECL_P (decl)
2262 && DECL_EXTERNAL (decl))
2263 set_local_extern_decl_linkage (decl, old != NULL_TREE);
2265 if (old == error_mark_node)
2266 old = NULL_TREE;
2268 for (ovl_iterator iter (old); iter; ++iter)
2269 if (iter.using_p ())
2270 ; /* Ignore using decls here. */
2271 else if (tree match = duplicate_decls (decl, *iter, is_friend))
2273 if (iter.hidden_p ()
2274 && match != error_mark_node
2275 && !DECL_HIDDEN_P (match))
2277 /* Unhiding a previously hidden decl. */
2278 tree head = iter.reveal_node (old);
2279 if (head != old)
2281 if (!ns)
2282 update_local_overload (binding, head);
2283 binding->value = head;
2286 if (TREE_CODE (match) == FUNCTION_DECL
2287 && DECL_EXTERN_C_P (match))
2288 /* We need to check and register the fn now. */
2289 check_extern_c_conflict (match);
2291 return match;
2294 /* We are pushing a new decl. */
2296 /* Skip a hidden builtin we failed to match already. There can
2297 only be one. */
2298 if (old && anticipated_builtin_p (old))
2299 old = OVL_CHAIN (old);
2301 check_template_shadow (decl);
2303 if (DECL_DECLARES_FUNCTION_P (decl))
2305 check_default_args (decl);
2307 if (is_friend)
2309 if (level->kind != sk_namespace)
2310 /* In a local class, a friend function declaration must
2311 find a matching decl in the innermost non-class scope.
2312 [class.friend/11] */
2313 error ("friend declaration %qD in local class without "
2314 "prior local declaration", decl);
2315 else if (!flag_friend_injection)
2316 /* Hide it from ordinary lookup. */
2317 DECL_ANTICIPATED (decl) = DECL_HIDDEN_FRIEND_P (decl) = true;
2321 if (level->kind != sk_namespace)
2323 check_local_shadow (decl);
2325 if (TREE_CODE (decl) == NAMESPACE_DECL)
2326 /* A local namespace alias. */
2327 set_identifier_type_value (name, NULL_TREE);
2329 if (!binding)
2330 binding = create_local_binding (level, name);
2332 else if (!binding)
2334 ns = current_namespace;
2335 binding = find_namespace_binding (ns, name, true);
2338 old = update_binding (level, binding, old, decl, is_friend);
2340 if (old != decl)
2341 /* An existing decl matched, use it. */
2342 decl = old;
2343 else if (TREE_CODE (decl) == TYPE_DECL)
2345 tree type = TREE_TYPE (decl);
2347 if (type != error_mark_node)
2349 if (TYPE_NAME (type) != decl)
2350 set_underlying_type (decl);
2352 if (!ns)
2353 set_identifier_type_value_with_scope (name, decl, level);
2354 else
2355 SET_IDENTIFIER_TYPE_VALUE (name, global_type_node);
2358 /* If this is a locally defined typedef in a function that
2359 is not a template instantation, record it to implement
2360 -Wunused-local-typedefs. */
2361 if (!instantiating_current_function_p ())
2362 record_locally_defined_typedef (decl);
2364 else if (VAR_P (decl))
2365 maybe_register_incomplete_var (decl);
2366 else if (TREE_CODE (decl) == FUNCTION_DECL && DECL_EXTERN_C_P (decl))
2367 check_extern_c_conflict (decl);
2369 else
2370 add_decl_to_level (level, decl);
2372 return decl;
2375 /* Record a decl-node X as belonging to the current lexical scope.
2376 It's a friend if IS_FRIEND is true -- which affects exactly where
2377 we push it. */
2379 tree
2380 pushdecl (tree x, bool is_friend)
2382 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
2383 tree ret = do_pushdecl (x, is_friend);
2384 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
2385 return ret;
2388 /* Enter DECL into the symbol table, if that's appropriate. Returns
2389 DECL, or a modified version thereof. */
2391 tree
2392 maybe_push_decl (tree decl)
2394 tree type = TREE_TYPE (decl);
2396 /* Add this decl to the current binding level, but not if it comes
2397 from another scope, e.g. a static member variable. TEM may equal
2398 DECL or it may be a previous decl of the same name. */
2399 if (decl == error_mark_node
2400 || (TREE_CODE (decl) != PARM_DECL
2401 && DECL_CONTEXT (decl) != NULL_TREE
2402 /* Definitions of namespace members outside their namespace are
2403 possible. */
2404 && !DECL_NAMESPACE_SCOPE_P (decl))
2405 || (TREE_CODE (decl) == TEMPLATE_DECL && !namespace_bindings_p ())
2406 || type == unknown_type_node
2407 /* The declaration of a template specialization does not affect
2408 the functions available for overload resolution, so we do not
2409 call pushdecl. */
2410 || (TREE_CODE (decl) == FUNCTION_DECL
2411 && DECL_TEMPLATE_SPECIALIZATION (decl)))
2412 return decl;
2413 else
2414 return pushdecl (decl);
2417 /* Bind DECL to ID in the current_binding_level, assumed to be a local
2418 binding level. If IS_USING is true, DECL got here through a
2419 using-declaration. */
2421 static void
2422 push_local_binding (tree id, tree decl, bool is_using)
2424 cp_binding_level *b;
2426 /* Skip over any local classes. This makes sense if we call
2427 push_local_binding with a friend decl of a local class. */
2428 b = innermost_nonclass_level ();
2430 cxx_binding *binding = NULL;
2431 if (b->kind == sk_namespace)
2432 binding = find_namespace_binding (current_namespace, id);
2433 else
2434 binding = find_local_binding (b, id);
2436 if (binding)
2438 /* Supplement the existing binding. */
2439 if (!supplement_binding (IDENTIFIER_BINDING (id), decl))
2440 /* It didn't work. Something else must be bound at this
2441 level. Do not add DECL to the list of things to pop
2442 later. */
2443 return;
2445 else
2446 /* Create a new binding. */
2447 push_binding (id, decl, b);
2449 if (TREE_CODE (decl) == OVERLOAD || is_using)
2450 /* We must put the OVERLOAD or using into a TREE_LIST since we
2451 cannot use the decl's chain itself. */
2452 decl = build_tree_list (NULL_TREE, decl);
2454 /* And put DECL on the list of things declared by the current
2455 binding level. */
2456 add_decl_to_level (b, decl);
2459 /* Check to see whether or not DECL is a variable that would have been
2460 in scope under the ARM, but is not in scope under the ANSI/ISO
2461 standard. If so, issue an error message. If name lookup would
2462 work in both cases, but return a different result, this function
2463 returns the result of ANSI/ISO lookup. Otherwise, it returns
2464 DECL. */
2466 tree
2467 check_for_out_of_scope_variable (tree decl)
2469 tree shadowed;
2471 /* We only care about out of scope variables. */
2472 if (!(VAR_P (decl) && DECL_DEAD_FOR_LOCAL (decl)))
2473 return decl;
2475 shadowed = DECL_HAS_SHADOWED_FOR_VAR_P (decl)
2476 ? DECL_SHADOWED_FOR_VAR (decl) : NULL_TREE ;
2477 while (shadowed != NULL_TREE && VAR_P (shadowed)
2478 && DECL_DEAD_FOR_LOCAL (shadowed))
2479 shadowed = DECL_HAS_SHADOWED_FOR_VAR_P (shadowed)
2480 ? DECL_SHADOWED_FOR_VAR (shadowed) : NULL_TREE;
2481 if (!shadowed)
2482 shadowed = get_namespace_binding (current_namespace, DECL_NAME (decl));
2483 if (shadowed)
2485 if (!DECL_ERROR_REPORTED (decl))
2487 warning (0, "name lookup of %qD changed", DECL_NAME (decl));
2488 warning_at (DECL_SOURCE_LOCATION (shadowed), 0,
2489 " matches this %qD under ISO standard rules",
2490 shadowed);
2491 warning_at (DECL_SOURCE_LOCATION (decl), 0,
2492 " matches this %qD under old rules", decl);
2493 DECL_ERROR_REPORTED (decl) = 1;
2495 return shadowed;
2498 /* If we have already complained about this declaration, there's no
2499 need to do it again. */
2500 if (DECL_ERROR_REPORTED (decl))
2501 return decl;
2503 DECL_ERROR_REPORTED (decl) = 1;
2505 if (TREE_TYPE (decl) == error_mark_node)
2506 return decl;
2508 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
2510 error ("name lookup of %qD changed for ISO %<for%> scoping",
2511 DECL_NAME (decl));
2512 error (" cannot use obsolete binding at %q+D because "
2513 "it has a destructor", decl);
2514 return error_mark_node;
2516 else
2518 permerror (input_location, "name lookup of %qD changed for ISO %<for%> scoping",
2519 DECL_NAME (decl));
2520 if (flag_permissive)
2521 permerror (DECL_SOURCE_LOCATION (decl),
2522 " using obsolete binding at %qD", decl);
2523 else
2525 static bool hint;
2526 if (!hint)
2528 inform (input_location, "(if you use %<-fpermissive%> G++ will accept your code)");
2529 hint = true;
2534 return decl;
2537 /* true means unconditionally make a BLOCK for the next level pushed. */
2539 static bool keep_next_level_flag;
2541 static int binding_depth = 0;
2543 static void
2544 indent (int depth)
2546 int i;
2548 for (i = 0; i < depth * 2; i++)
2549 putc (' ', stderr);
2552 /* Return a string describing the kind of SCOPE we have. */
2553 static const char *
2554 cp_binding_level_descriptor (cp_binding_level *scope)
2556 /* The order of this table must match the "scope_kind"
2557 enumerators. */
2558 static const char* scope_kind_names[] = {
2559 "block-scope",
2560 "cleanup-scope",
2561 "try-scope",
2562 "catch-scope",
2563 "for-scope",
2564 "function-parameter-scope",
2565 "class-scope",
2566 "namespace-scope",
2567 "template-parameter-scope",
2568 "template-explicit-spec-scope"
2570 const scope_kind kind = scope->explicit_spec_p
2571 ? sk_template_spec : scope->kind;
2573 return scope_kind_names[kind];
2576 /* Output a debugging information about SCOPE when performing
2577 ACTION at LINE. */
2578 static void
2579 cp_binding_level_debug (cp_binding_level *scope, int line, const char *action)
2581 const char *desc = cp_binding_level_descriptor (scope);
2582 if (scope->this_entity)
2583 verbatim ("%s %<%s(%E)%> %p %d\n", action, desc,
2584 scope->this_entity, (void *) scope, line);
2585 else
2586 verbatim ("%s %s %p %d\n", action, desc, (void *) scope, line);
2589 /* Return the estimated initial size of the hashtable of a NAMESPACE
2590 scope. */
2592 static inline size_t
2593 namespace_scope_ht_size (tree ns)
2595 tree name = DECL_NAME (ns);
2597 return name == std_identifier
2598 ? NAMESPACE_STD_HT_SIZE
2599 : (name == global_identifier
2600 ? GLOBAL_SCOPE_HT_SIZE
2601 : NAMESPACE_ORDINARY_HT_SIZE);
2604 /* A chain of binding_level structures awaiting reuse. */
2606 static GTY((deletable)) cp_binding_level *free_binding_level;
2608 /* Insert SCOPE as the innermost binding level. */
2610 void
2611 push_binding_level (cp_binding_level *scope)
2613 /* Add it to the front of currently active scopes stack. */
2614 scope->level_chain = current_binding_level;
2615 current_binding_level = scope;
2616 keep_next_level_flag = false;
2618 if (ENABLE_SCOPE_CHECKING)
2620 scope->binding_depth = binding_depth;
2621 indent (binding_depth);
2622 cp_binding_level_debug (scope, LOCATION_LINE (input_location),
2623 "push");
2624 binding_depth++;
2628 /* Create a new KIND scope and make it the top of the active scopes stack.
2629 ENTITY is the scope of the associated C++ entity (namespace, class,
2630 function, C++0x enumeration); it is NULL otherwise. */
2632 cp_binding_level *
2633 begin_scope (scope_kind kind, tree entity)
2635 cp_binding_level *scope;
2637 /* Reuse or create a struct for this binding level. */
2638 if (!ENABLE_SCOPE_CHECKING && free_binding_level)
2640 scope = free_binding_level;
2641 free_binding_level = scope->level_chain;
2642 memset (scope, 0, sizeof (cp_binding_level));
2644 else
2645 scope = ggc_cleared_alloc<cp_binding_level> ();
2647 scope->this_entity = entity;
2648 scope->more_cleanups_ok = true;
2649 switch (kind)
2651 case sk_cleanup:
2652 scope->keep = true;
2653 break;
2655 case sk_template_spec:
2656 scope->explicit_spec_p = true;
2657 kind = sk_template_parms;
2658 /* Fall through. */
2659 case sk_template_parms:
2660 case sk_block:
2661 case sk_try:
2662 case sk_catch:
2663 case sk_for:
2664 case sk_cond:
2665 case sk_class:
2666 case sk_scoped_enum:
2667 case sk_function_parms:
2668 case sk_transaction:
2669 case sk_omp:
2670 scope->keep = keep_next_level_flag;
2671 break;
2673 case sk_namespace:
2674 NAMESPACE_LEVEL (entity) = scope;
2675 break;
2677 default:
2678 /* Should not happen. */
2679 gcc_unreachable ();
2680 break;
2682 scope->kind = kind;
2684 push_binding_level (scope);
2686 return scope;
2689 /* We're about to leave current scope. Pop the top of the stack of
2690 currently active scopes. Return the enclosing scope, now active. */
2692 cp_binding_level *
2693 leave_scope (void)
2695 cp_binding_level *scope = current_binding_level;
2697 if (scope->kind == sk_namespace && class_binding_level)
2698 current_binding_level = class_binding_level;
2700 /* We cannot leave a scope, if there are none left. */
2701 if (NAMESPACE_LEVEL (global_namespace))
2702 gcc_assert (!global_scope_p (scope));
2704 if (ENABLE_SCOPE_CHECKING)
2706 indent (--binding_depth);
2707 cp_binding_level_debug (scope, LOCATION_LINE (input_location),
2708 "leave");
2711 /* Move one nesting level up. */
2712 current_binding_level = scope->level_chain;
2714 /* Namespace-scopes are left most probably temporarily, not
2715 completely; they can be reopened later, e.g. in namespace-extension
2716 or any name binding activity that requires us to resume a
2717 namespace. For classes, we cache some binding levels. For other
2718 scopes, we just make the structure available for reuse. */
2719 if (scope->kind != sk_namespace
2720 && scope->kind != sk_class)
2722 scope->level_chain = free_binding_level;
2723 gcc_assert (!ENABLE_SCOPE_CHECKING
2724 || scope->binding_depth == binding_depth);
2725 free_binding_level = scope;
2728 if (scope->kind == sk_class)
2730 /* Reset DEFINING_CLASS_P to allow for reuse of a
2731 class-defining scope in a non-defining context. */
2732 scope->defining_class_p = 0;
2734 /* Find the innermost enclosing class scope, and reset
2735 CLASS_BINDING_LEVEL appropriately. */
2736 class_binding_level = NULL;
2737 for (scope = current_binding_level; scope; scope = scope->level_chain)
2738 if (scope->kind == sk_class)
2740 class_binding_level = scope;
2741 break;
2745 return current_binding_level;
2748 static void
2749 resume_scope (cp_binding_level* b)
2751 /* Resuming binding levels is meant only for namespaces,
2752 and those cannot nest into classes. */
2753 gcc_assert (!class_binding_level);
2754 /* Also, resuming a non-directly nested namespace is a no-no. */
2755 gcc_assert (b->level_chain == current_binding_level);
2756 current_binding_level = b;
2757 if (ENABLE_SCOPE_CHECKING)
2759 b->binding_depth = binding_depth;
2760 indent (binding_depth);
2761 cp_binding_level_debug (b, LOCATION_LINE (input_location), "resume");
2762 binding_depth++;
2766 /* Return the innermost binding level that is not for a class scope. */
2768 static cp_binding_level *
2769 innermost_nonclass_level (void)
2771 cp_binding_level *b;
2773 b = current_binding_level;
2774 while (b->kind == sk_class)
2775 b = b->level_chain;
2777 return b;
2780 /* We're defining an object of type TYPE. If it needs a cleanup, but
2781 we're not allowed to add any more objects with cleanups to the current
2782 scope, create a new binding level. */
2784 void
2785 maybe_push_cleanup_level (tree type)
2787 if (type != error_mark_node
2788 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
2789 && current_binding_level->more_cleanups_ok == 0)
2791 begin_scope (sk_cleanup, NULL);
2792 current_binding_level->statement_list = push_stmt_list ();
2796 /* Return true if we are in the global binding level. */
2798 bool
2799 global_bindings_p (void)
2801 return global_scope_p (current_binding_level);
2804 /* True if we are currently in a toplevel binding level. This
2805 means either the global binding level or a namespace in a toplevel
2806 binding level. Since there are no non-toplevel namespace levels,
2807 this really means any namespace or template parameter level. We
2808 also include a class whose context is toplevel. */
2810 bool
2811 toplevel_bindings_p (void)
2813 cp_binding_level *b = innermost_nonclass_level ();
2815 return b->kind == sk_namespace || b->kind == sk_template_parms;
2818 /* True if this is a namespace scope, or if we are defining a class
2819 which is itself at namespace scope, or whose enclosing class is
2820 such a class, etc. */
2822 bool
2823 namespace_bindings_p (void)
2825 cp_binding_level *b = innermost_nonclass_level ();
2827 return b->kind == sk_namespace;
2830 /* True if the innermost non-class scope is a block scope. */
2832 bool
2833 local_bindings_p (void)
2835 cp_binding_level *b = innermost_nonclass_level ();
2836 return b->kind < sk_function_parms || b->kind == sk_omp;
2839 /* True if the current level needs to have a BLOCK made. */
2841 bool
2842 kept_level_p (void)
2844 return (current_binding_level->blocks != NULL_TREE
2845 || current_binding_level->keep
2846 || current_binding_level->kind == sk_cleanup
2847 || current_binding_level->names != NULL_TREE
2848 || current_binding_level->using_directives);
2851 /* Returns the kind of the innermost scope. */
2853 scope_kind
2854 innermost_scope_kind (void)
2856 return current_binding_level->kind;
2859 /* Returns true if this scope was created to store template parameters. */
2861 bool
2862 template_parm_scope_p (void)
2864 return innermost_scope_kind () == sk_template_parms;
2867 /* If KEEP is true, make a BLOCK node for the next binding level,
2868 unconditionally. Otherwise, use the normal logic to decide whether
2869 or not to create a BLOCK. */
2871 void
2872 keep_next_level (bool keep)
2874 keep_next_level_flag = keep;
2877 /* Return the list of declarations of the current local scope. */
2879 tree
2880 get_local_decls (void)
2882 gcc_assert (current_binding_level->kind != sk_namespace
2883 && current_binding_level->kind != sk_class);
2884 return current_binding_level->names;
2887 /* Return how many function prototypes we are currently nested inside. */
2890 function_parm_depth (void)
2892 int level = 0;
2893 cp_binding_level *b;
2895 for (b = current_binding_level;
2896 b->kind == sk_function_parms;
2897 b = b->level_chain)
2898 ++level;
2900 return level;
2903 /* For debugging. */
2904 static int no_print_functions = 0;
2905 static int no_print_builtins = 0;
2907 static void
2908 print_binding_level (cp_binding_level* lvl)
2910 tree t;
2911 int i = 0, len;
2912 fprintf (stderr, " blocks=%p", (void *) lvl->blocks);
2913 if (lvl->more_cleanups_ok)
2914 fprintf (stderr, " more-cleanups-ok");
2915 if (lvl->have_cleanups)
2916 fprintf (stderr, " have-cleanups");
2917 fprintf (stderr, "\n");
2918 if (lvl->names)
2920 fprintf (stderr, " names:\t");
2921 /* We can probably fit 3 names to a line? */
2922 for (t = lvl->names; t; t = TREE_CHAIN (t))
2924 if (no_print_functions && (TREE_CODE (t) == FUNCTION_DECL))
2925 continue;
2926 if (no_print_builtins
2927 && (TREE_CODE (t) == TYPE_DECL)
2928 && DECL_IS_BUILTIN (t))
2929 continue;
2931 /* Function decls tend to have longer names. */
2932 if (TREE_CODE (t) == FUNCTION_DECL)
2933 len = 3;
2934 else
2935 len = 2;
2936 i += len;
2937 if (i > 6)
2939 fprintf (stderr, "\n\t");
2940 i = len;
2942 print_node_brief (stderr, "", t, 0);
2943 if (t == error_mark_node)
2944 break;
2946 if (i)
2947 fprintf (stderr, "\n");
2949 if (vec_safe_length (lvl->class_shadowed))
2951 size_t i;
2952 cp_class_binding *b;
2953 fprintf (stderr, " class-shadowed:");
2954 FOR_EACH_VEC_ELT (*lvl->class_shadowed, i, b)
2955 fprintf (stderr, " %s ", IDENTIFIER_POINTER (b->identifier));
2956 fprintf (stderr, "\n");
2958 if (lvl->type_shadowed)
2960 fprintf (stderr, " type-shadowed:");
2961 for (t = lvl->type_shadowed; t; t = TREE_CHAIN (t))
2963 fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
2965 fprintf (stderr, "\n");
2969 DEBUG_FUNCTION void
2970 debug (cp_binding_level &ref)
2972 print_binding_level (&ref);
2975 DEBUG_FUNCTION void
2976 debug (cp_binding_level *ptr)
2978 if (ptr)
2979 debug (*ptr);
2980 else
2981 fprintf (stderr, "<nil>\n");
2985 void
2986 print_other_binding_stack (cp_binding_level *stack)
2988 cp_binding_level *level;
2989 for (level = stack; !global_scope_p (level); level = level->level_chain)
2991 fprintf (stderr, "binding level %p\n", (void *) level);
2992 print_binding_level (level);
2996 void
2997 print_binding_stack (void)
2999 cp_binding_level *b;
3000 fprintf (stderr, "current_binding_level=%p\n"
3001 "class_binding_level=%p\n"
3002 "NAMESPACE_LEVEL (global_namespace)=%p\n",
3003 (void *) current_binding_level, (void *) class_binding_level,
3004 (void *) NAMESPACE_LEVEL (global_namespace));
3005 if (class_binding_level)
3007 for (b = class_binding_level; b; b = b->level_chain)
3008 if (b == current_binding_level)
3009 break;
3010 if (b)
3011 b = class_binding_level;
3012 else
3013 b = current_binding_level;
3015 else
3016 b = current_binding_level;
3017 print_other_binding_stack (b);
3018 fprintf (stderr, "global:\n");
3019 print_binding_level (NAMESPACE_LEVEL (global_namespace));
3022 /* Return the type associated with ID. */
3024 static tree
3025 identifier_type_value_1 (tree id)
3027 /* There is no type with that name, anywhere. */
3028 if (REAL_IDENTIFIER_TYPE_VALUE (id) == NULL_TREE)
3029 return NULL_TREE;
3030 /* This is not the type marker, but the real thing. */
3031 if (REAL_IDENTIFIER_TYPE_VALUE (id) != global_type_node)
3032 return REAL_IDENTIFIER_TYPE_VALUE (id);
3033 /* Have to search for it. It must be on the global level, now.
3034 Ask lookup_name not to return non-types. */
3035 id = lookup_name_real (id, 2, 1, /*block_p=*/true, 0, 0);
3036 if (id)
3037 return TREE_TYPE (id);
3038 return NULL_TREE;
3041 /* Wrapper for identifier_type_value_1. */
3043 tree
3044 identifier_type_value (tree id)
3046 tree ret;
3047 timevar_start (TV_NAME_LOOKUP);
3048 ret = identifier_type_value_1 (id);
3049 timevar_stop (TV_NAME_LOOKUP);
3050 return ret;
3054 /* Return the IDENTIFIER_GLOBAL_VALUE of T, for use in common code, since
3055 the definition of IDENTIFIER_GLOBAL_VALUE is different for C and C++. */
3057 tree
3058 identifier_global_value (tree t)
3060 return IDENTIFIER_GLOBAL_VALUE (t);
3063 /* Push a definition of struct, union or enum tag named ID. into
3064 binding_level B. DECL is a TYPE_DECL for the type. We assume that
3065 the tag ID is not already defined. */
3067 static void
3068 set_identifier_type_value_with_scope (tree id, tree decl, cp_binding_level *b)
3070 tree type;
3072 if (b->kind != sk_namespace)
3074 /* Shadow the marker, not the real thing, so that the marker
3075 gets restored later. */
3076 tree old_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
3077 b->type_shadowed
3078 = tree_cons (id, old_type_value, b->type_shadowed);
3079 type = decl ? TREE_TYPE (decl) : NULL_TREE;
3080 TREE_TYPE (b->type_shadowed) = type;
3082 else
3084 cxx_binding *binding
3085 = find_namespace_binding (current_namespace, id, true);
3087 if (binding->value)
3088 supplement_binding (binding, decl);
3089 else
3090 binding->value = decl;
3092 /* Store marker instead of real type. */
3093 type = global_type_node;
3095 SET_IDENTIFIER_TYPE_VALUE (id, type);
3098 /* As set_identifier_type_value_with_scope, but using
3099 current_binding_level. */
3101 void
3102 set_identifier_type_value (tree id, tree decl)
3104 set_identifier_type_value_with_scope (id, decl, current_binding_level);
3107 /* Return the name for the constructor (or destructor) for the
3108 specified class TYPE. When given a template, this routine doesn't
3109 lose the specialization. */
3111 static inline tree
3112 constructor_name_full (tree type)
3114 return TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
3117 /* Return the name for the constructor (or destructor) for the
3118 specified class. When given a template, return the plain
3119 unspecialized name. */
3121 tree
3122 constructor_name (tree type)
3124 tree name;
3125 name = constructor_name_full (type);
3126 if (IDENTIFIER_TEMPLATE (name))
3127 name = IDENTIFIER_TEMPLATE (name);
3128 return name;
3131 /* Returns TRUE if NAME is the name for the constructor for TYPE,
3132 which must be a class type. */
3134 bool
3135 constructor_name_p (tree name, tree type)
3137 tree ctor_name;
3139 gcc_assert (MAYBE_CLASS_TYPE_P (type));
3141 if (!name)
3142 return false;
3144 if (!identifier_p (name))
3145 return false;
3147 /* These don't have names. */
3148 if (TREE_CODE (type) == DECLTYPE_TYPE
3149 || TREE_CODE (type) == TYPEOF_TYPE)
3150 return false;
3152 ctor_name = constructor_name_full (type);
3153 if (name == ctor_name)
3154 return true;
3155 if (IDENTIFIER_TEMPLATE (ctor_name)
3156 && name == IDENTIFIER_TEMPLATE (ctor_name))
3157 return true;
3158 return false;
3161 /* Counter used to create anonymous type names. */
3163 static GTY(()) int anon_cnt;
3165 /* Return an IDENTIFIER which can be used as a name for
3166 unnamed structs and unions. */
3168 tree
3169 make_anon_name (void)
3171 char buf[32];
3173 sprintf (buf, anon_aggrname_format (), anon_cnt++);
3174 return get_identifier (buf);
3177 /* This code is practically identical to that for creating
3178 anonymous names, but is just used for lambdas instead. This isn't really
3179 necessary, but it's convenient to avoid treating lambdas like other
3180 unnamed types. */
3182 static GTY(()) int lambda_cnt = 0;
3184 tree
3185 make_lambda_name (void)
3187 char buf[32];
3189 sprintf (buf, LAMBDANAME_FORMAT, lambda_cnt++);
3190 return get_identifier (buf);
3193 /* Insert another USING_DECL into the current binding level, returning
3194 this declaration. If this is a redeclaration, do nothing, and
3195 return NULL_TREE if this not in namespace scope (in namespace
3196 scope, a using decl might extend any previous bindings). */
3198 static tree
3199 push_using_decl_1 (tree scope, tree name)
3201 tree decl;
3203 gcc_assert (TREE_CODE (scope) == NAMESPACE_DECL);
3204 gcc_assert (identifier_p (name));
3205 for (decl = current_binding_level->usings; decl; decl = DECL_CHAIN (decl))
3206 if (USING_DECL_SCOPE (decl) == scope && DECL_NAME (decl) == name)
3207 break;
3208 if (decl)
3209 return namespace_bindings_p () ? decl : NULL_TREE;
3210 decl = build_lang_decl (USING_DECL, name, NULL_TREE);
3211 USING_DECL_SCOPE (decl) = scope;
3212 DECL_CHAIN (decl) = current_binding_level->usings;
3213 current_binding_level->usings = decl;
3214 return decl;
3217 /* Wrapper for push_using_decl_1. */
3219 static tree
3220 push_using_decl (tree scope, tree name)
3222 tree ret;
3223 timevar_start (TV_NAME_LOOKUP);
3224 ret = push_using_decl_1 (scope, name);
3225 timevar_stop (TV_NAME_LOOKUP);
3226 return ret;
3229 /* Same as pushdecl, but define X in binding-level LEVEL. We rely on the
3230 caller to set DECL_CONTEXT properly.
3232 Note that this must only be used when X will be the new innermost
3233 binding for its name, as we tack it onto the front of IDENTIFIER_BINDING
3234 without checking to see if the current IDENTIFIER_BINDING comes from a
3235 closer binding level than LEVEL. */
3237 static tree
3238 pushdecl_with_scope_1 (tree x, cp_binding_level *level, bool is_friend)
3240 cp_binding_level *b;
3241 tree function_decl = current_function_decl;
3243 current_function_decl = NULL_TREE;
3244 if (level->kind == sk_class)
3246 b = class_binding_level;
3247 class_binding_level = level;
3248 pushdecl_class_level (x);
3249 class_binding_level = b;
3251 else
3253 b = current_binding_level;
3254 current_binding_level = level;
3255 x = pushdecl (x, is_friend);
3256 current_binding_level = b;
3258 current_function_decl = function_decl;
3259 return x;
3262 /* Inject X into the local scope just before the function parms. */
3264 tree
3265 pushdecl_outermost_localscope (tree x)
3267 cp_binding_level *b = NULL;
3268 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3270 /* Find the scope just inside the function parms. */
3271 for (cp_binding_level *n = current_binding_level;
3272 n->kind != sk_function_parms; n = b->level_chain)
3273 b = n;
3275 tree ret = b ? pushdecl_with_scope_1 (x, b, false) : error_mark_node;
3276 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3278 return ret;
3281 /* Check a non-member using-declaration. Return the name and scope
3282 being used, and the USING_DECL, or NULL_TREE on failure. */
3284 static tree
3285 validate_nonmember_using_decl (tree decl, tree scope, tree name)
3287 /* [namespace.udecl]
3288 A using-declaration for a class member shall be a
3289 member-declaration. */
3290 if (TYPE_P (scope))
3292 error ("%qT is not a namespace or unscoped enum", scope);
3293 return NULL_TREE;
3295 else if (scope == error_mark_node)
3296 return NULL_TREE;
3298 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR)
3300 /* 7.3.3/5
3301 A using-declaration shall not name a template-id. */
3302 error ("a using-declaration cannot specify a template-id. "
3303 "Try %<using %D%>", name);
3304 return NULL_TREE;
3307 if (TREE_CODE (decl) == NAMESPACE_DECL)
3309 error ("namespace %qD not allowed in using-declaration", decl);
3310 return NULL_TREE;
3313 if (TREE_CODE (decl) == SCOPE_REF)
3315 /* It's a nested name with template parameter dependent scope.
3316 This can only be using-declaration for class member. */
3317 error ("%qT is not a namespace", TREE_OPERAND (decl, 0));
3318 return NULL_TREE;
3321 decl = OVL_FIRST (decl);
3323 /* Make a USING_DECL. */
3324 tree using_decl = push_using_decl (scope, name);
3326 if (using_decl == NULL_TREE
3327 && at_function_scope_p ()
3328 && VAR_P (decl))
3329 /* C++11 7.3.3/10. */
3330 error ("%qD is already declared in this scope", name);
3332 return using_decl;
3335 /* Process a local-scope or namespace-scope using declaration. SCOPE
3336 is the nominated scope to search for NAME. VALUE_P and TYPE_P
3337 point to the binding for NAME in the current scope and are
3338 updated. */
3340 static void
3341 do_nonmember_using_decl (tree scope, tree name, tree *value_p, tree *type_p)
3343 name_lookup lookup (name, 0);
3345 if (!qualified_namespace_lookup (scope, &lookup))
3346 /* Lookup error */
3347 return;
3349 if (!lookup.value)
3351 error ("%qD not declared", name);
3352 return;
3354 else if (TREE_CODE (lookup.value) == TREE_LIST)
3356 error ("reference to %qD is ambiguous", name);
3357 print_candidates (lookup.value);
3358 lookup.value = NULL_TREE;
3361 if (lookup.type && TREE_CODE (lookup.type) == TREE_LIST)
3363 error ("reference to %qD is ambiguous", name);
3364 print_candidates (lookup.type);
3365 lookup.type = NULL_TREE;
3368 tree value = *value_p;
3369 tree type = *type_p;
3371 /* Shift the old and new bindings around so we're comparing class and
3372 enumeration names to each other. */
3373 if (value && DECL_IMPLICIT_TYPEDEF_P (value))
3375 type = value;
3376 value = NULL_TREE;
3379 if (lookup.value && DECL_IMPLICIT_TYPEDEF_P (lookup.value))
3381 lookup.type = lookup.value;
3382 lookup.value = NULL_TREE;
3385 if (lookup.value && lookup.value != value)
3387 /* Check for using functions. */
3388 if (OVL_P (lookup.value) && (!value || OVL_P (value)))
3390 for (lkp_iterator usings (lookup.value); usings; ++usings)
3392 tree new_fn = *usings;
3394 /* [namespace.udecl]
3396 If a function declaration in namespace scope or block
3397 scope has the same name and the same parameter types as a
3398 function introduced by a using declaration the program is
3399 ill-formed. */
3400 bool found = false;
3401 for (ovl_iterator old (value); !found && old; ++old)
3403 tree old_fn = *old;
3405 if (new_fn == old_fn)
3406 /* The function already exists in the current
3407 namespace. */
3408 found = true;
3409 else if (old.using_p ())
3410 continue; /* This is a using decl. */
3411 else if (old.hidden_p () && !DECL_HIDDEN_FRIEND_P (old_fn))
3412 continue; /* This is an anticipated builtin. */
3413 else if (!matching_fn_p (new_fn, old_fn))
3414 continue; /* Parameters do not match. */
3415 else if (decls_match (new_fn, old_fn))
3416 found = true;
3417 else
3419 diagnose_name_conflict (new_fn, old_fn);
3420 found = true;
3424 if (!found)
3425 /* Unlike the overload case we don't drop anticipated
3426 builtins here. They don't cause a problem, and
3427 we'd like to match them with a future
3428 declaration. */
3429 value = ovl_insert (new_fn, value, true);
3432 else if (value
3433 /* Ignore anticipated builtins. */
3434 && !anticipated_builtin_p (value)
3435 && !decls_match (lookup.value, value))
3436 diagnose_name_conflict (lookup.value, value);
3437 else
3438 value = lookup.value;
3441 if (lookup.type && lookup.type != type)
3443 if (type && !decls_match (lookup.type, type))
3444 diagnose_name_conflict (lookup.type, type);
3445 else
3446 type = lookup.type;
3449 /* If bind->value is empty, shift any class or enumeration name back. */
3450 if (!value)
3452 value = type;
3453 type = NULL_TREE;
3456 *value_p = value;
3457 *type_p = type;
3460 /* Returns true if ANCESTOR encloses DESCENDANT, including matching.
3461 Both are namespaces. */
3463 bool
3464 is_nested_namespace (tree ancestor, tree descendant, bool inline_only)
3466 int depth = SCOPE_DEPTH (ancestor);
3468 if (!depth && !inline_only)
3469 /* The global namespace encloses everything. */
3470 return true;
3472 while (SCOPE_DEPTH (descendant) > depth
3473 && (!inline_only || DECL_NAMESPACE_INLINE_P (descendant)))
3474 descendant = CP_DECL_CONTEXT (descendant);
3476 return ancestor == descendant;
3479 /* Returns true if ROOT (a namespace, class, or function) encloses
3480 CHILD. CHILD may be either a class type or a namespace. */
3482 bool
3483 is_ancestor (tree root, tree child)
3485 gcc_assert ((TREE_CODE (root) == NAMESPACE_DECL
3486 || TREE_CODE (root) == FUNCTION_DECL
3487 || CLASS_TYPE_P (root)));
3488 gcc_assert ((TREE_CODE (child) == NAMESPACE_DECL
3489 || CLASS_TYPE_P (child)));
3491 /* The global namespace encloses everything. */
3492 if (root == global_namespace)
3493 return true;
3495 /* Search until we reach namespace scope. */
3496 while (TREE_CODE (child) != NAMESPACE_DECL)
3498 /* If we've reached the ROOT, it encloses CHILD. */
3499 if (root == child)
3500 return true;
3501 /* Go out one level. */
3502 if (TYPE_P (child))
3503 child = TYPE_NAME (child);
3504 child = CP_DECL_CONTEXT (child);
3507 if (TREE_CODE (root) == NAMESPACE_DECL)
3508 return is_nested_namespace (root, child);
3510 return false;
3513 /* Enter the class or namespace scope indicated by T suitable for name
3514 lookup. T can be arbitrary scope, not necessary nested inside the
3515 current scope. Returns a non-null scope to pop iff pop_scope
3516 should be called later to exit this scope. */
3518 tree
3519 push_scope (tree t)
3521 if (TREE_CODE (t) == NAMESPACE_DECL)
3522 push_decl_namespace (t);
3523 else if (CLASS_TYPE_P (t))
3525 if (!at_class_scope_p ()
3526 || !same_type_p (current_class_type, t))
3527 push_nested_class (t);
3528 else
3529 /* T is the same as the current scope. There is therefore no
3530 need to re-enter the scope. Since we are not actually
3531 pushing a new scope, our caller should not call
3532 pop_scope. */
3533 t = NULL_TREE;
3536 return t;
3539 /* Leave scope pushed by push_scope. */
3541 void
3542 pop_scope (tree t)
3544 if (t == NULL_TREE)
3545 return;
3546 if (TREE_CODE (t) == NAMESPACE_DECL)
3547 pop_decl_namespace ();
3548 else if CLASS_TYPE_P (t)
3549 pop_nested_class ();
3552 /* Subroutine of push_inner_scope. */
3554 static void
3555 push_inner_scope_r (tree outer, tree inner)
3557 tree prev;
3559 if (outer == inner
3560 || (TREE_CODE (inner) != NAMESPACE_DECL && !CLASS_TYPE_P (inner)))
3561 return;
3563 prev = CP_DECL_CONTEXT (TREE_CODE (inner) == NAMESPACE_DECL ? inner : TYPE_NAME (inner));
3564 if (outer != prev)
3565 push_inner_scope_r (outer, prev);
3566 if (TREE_CODE (inner) == NAMESPACE_DECL)
3568 cp_binding_level *save_template_parm = 0;
3569 /* Temporary take out template parameter scopes. They are saved
3570 in reversed order in save_template_parm. */
3571 while (current_binding_level->kind == sk_template_parms)
3573 cp_binding_level *b = current_binding_level;
3574 current_binding_level = b->level_chain;
3575 b->level_chain = save_template_parm;
3576 save_template_parm = b;
3579 resume_scope (NAMESPACE_LEVEL (inner));
3580 current_namespace = inner;
3582 /* Restore template parameter scopes. */
3583 while (save_template_parm)
3585 cp_binding_level *b = save_template_parm;
3586 save_template_parm = b->level_chain;
3587 b->level_chain = current_binding_level;
3588 current_binding_level = b;
3591 else
3592 pushclass (inner);
3595 /* Enter the scope INNER from current scope. INNER must be a scope
3596 nested inside current scope. This works with both name lookup and
3597 pushing name into scope. In case a template parameter scope is present,
3598 namespace is pushed under the template parameter scope according to
3599 name lookup rule in 14.6.1/6.
3601 Return the former current scope suitable for pop_inner_scope. */
3603 tree
3604 push_inner_scope (tree inner)
3606 tree outer = current_scope ();
3607 if (!outer)
3608 outer = current_namespace;
3610 push_inner_scope_r (outer, inner);
3611 return outer;
3614 /* Exit the current scope INNER back to scope OUTER. */
3616 void
3617 pop_inner_scope (tree outer, tree inner)
3619 if (outer == inner
3620 || (TREE_CODE (inner) != NAMESPACE_DECL && !CLASS_TYPE_P (inner)))
3621 return;
3623 while (outer != inner)
3625 if (TREE_CODE (inner) == NAMESPACE_DECL)
3627 cp_binding_level *save_template_parm = 0;
3628 /* Temporary take out template parameter scopes. They are saved
3629 in reversed order in save_template_parm. */
3630 while (current_binding_level->kind == sk_template_parms)
3632 cp_binding_level *b = current_binding_level;
3633 current_binding_level = b->level_chain;
3634 b->level_chain = save_template_parm;
3635 save_template_parm = b;
3638 pop_namespace ();
3640 /* Restore template parameter scopes. */
3641 while (save_template_parm)
3643 cp_binding_level *b = save_template_parm;
3644 save_template_parm = b->level_chain;
3645 b->level_chain = current_binding_level;
3646 current_binding_level = b;
3649 else
3650 popclass ();
3652 inner = CP_DECL_CONTEXT (TREE_CODE (inner) == NAMESPACE_DECL ? inner : TYPE_NAME (inner));
3656 /* Do a pushlevel for class declarations. */
3658 void
3659 pushlevel_class (void)
3661 class_binding_level = begin_scope (sk_class, current_class_type);
3664 /* ...and a poplevel for class declarations. */
3666 void
3667 poplevel_class (void)
3669 cp_binding_level *level = class_binding_level;
3670 cp_class_binding *cb;
3671 size_t i;
3672 tree shadowed;
3674 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3675 gcc_assert (level != 0);
3677 /* If we're leaving a toplevel class, cache its binding level. */
3678 if (current_class_depth == 1)
3679 previous_class_level = level;
3680 for (shadowed = level->type_shadowed;
3681 shadowed;
3682 shadowed = TREE_CHAIN (shadowed))
3683 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (shadowed), TREE_VALUE (shadowed));
3685 /* Remove the bindings for all of the class-level declarations. */
3686 if (level->class_shadowed)
3688 FOR_EACH_VEC_ELT (*level->class_shadowed, i, cb)
3690 IDENTIFIER_BINDING (cb->identifier) = cb->base->previous;
3691 cxx_binding_free (cb->base);
3693 ggc_free (level->class_shadowed);
3694 level->class_shadowed = NULL;
3697 /* Now, pop out of the binding level which we created up in the
3698 `pushlevel_class' routine. */
3699 gcc_assert (current_binding_level == level);
3700 leave_scope ();
3701 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3704 /* Set INHERITED_VALUE_BINDING_P on BINDING to true or false, as
3705 appropriate. DECL is the value to which a name has just been
3706 bound. CLASS_TYPE is the class in which the lookup occurred. */
3708 static void
3709 set_inherited_value_binding_p (cxx_binding *binding, tree decl,
3710 tree class_type)
3712 if (binding->value == decl && TREE_CODE (decl) != TREE_LIST)
3714 tree context;
3716 if (TREE_CODE (decl) == OVERLOAD)
3717 context = ovl_scope (decl);
3718 else
3720 gcc_assert (DECL_P (decl));
3721 context = context_for_name_lookup (decl);
3724 if (is_properly_derived_from (class_type, context))
3725 INHERITED_VALUE_BINDING_P (binding) = 1;
3726 else
3727 INHERITED_VALUE_BINDING_P (binding) = 0;
3729 else if (binding->value == decl)
3730 /* We only encounter a TREE_LIST when there is an ambiguity in the
3731 base classes. Such an ambiguity can be overridden by a
3732 definition in this class. */
3733 INHERITED_VALUE_BINDING_P (binding) = 1;
3734 else
3735 INHERITED_VALUE_BINDING_P (binding) = 0;
3738 /* Make the declaration of X appear in CLASS scope. */
3740 bool
3741 pushdecl_class_level (tree x)
3743 bool is_valid = true;
3744 bool subtime;
3746 /* Do nothing if we're adding to an outer lambda closure type,
3747 outer_binding will add it later if it's needed. */
3748 if (current_class_type != class_binding_level->this_entity)
3749 return true;
3751 subtime = timevar_cond_start (TV_NAME_LOOKUP);
3752 /* Get the name of X. */
3753 tree name = OVL_NAME (x);
3755 if (name)
3757 is_valid = push_class_level_binding (name, x);
3758 if (TREE_CODE (x) == TYPE_DECL)
3759 set_identifier_type_value (name, x);
3761 else if (ANON_AGGR_TYPE_P (TREE_TYPE (x)))
3763 /* If X is an anonymous aggregate, all of its members are
3764 treated as if they were members of the class containing the
3765 aggregate, for naming purposes. */
3766 tree f;
3768 for (f = TYPE_FIELDS (TREE_TYPE (x)); f; f = DECL_CHAIN (f))
3770 location_t save_location = input_location;
3771 input_location = DECL_SOURCE_LOCATION (f);
3772 if (!pushdecl_class_level (f))
3773 is_valid = false;
3774 input_location = save_location;
3777 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3778 return is_valid;
3781 /* Return the BINDING (if any) for NAME in SCOPE, which is a class
3782 scope. If the value returned is non-NULL, and the PREVIOUS field
3783 is not set, callers must set the PREVIOUS field explicitly. */
3785 static cxx_binding *
3786 get_class_binding (tree name, cp_binding_level *scope)
3788 tree class_type;
3789 tree type_binding;
3790 tree value_binding;
3791 cxx_binding *binding;
3793 class_type = scope->this_entity;
3795 /* Get the type binding. */
3796 type_binding = lookup_member (class_type, name,
3797 /*protect=*/2, /*want_type=*/true,
3798 tf_warning_or_error);
3799 /* Get the value binding. */
3800 value_binding = lookup_member (class_type, name,
3801 /*protect=*/2, /*want_type=*/false,
3802 tf_warning_or_error);
3804 if (value_binding
3805 && (TREE_CODE (value_binding) == TYPE_DECL
3806 || DECL_CLASS_TEMPLATE_P (value_binding)
3807 || (TREE_CODE (value_binding) == TREE_LIST
3808 && TREE_TYPE (value_binding) == error_mark_node
3809 && (TREE_CODE (TREE_VALUE (value_binding))
3810 == TYPE_DECL))))
3811 /* We found a type binding, even when looking for a non-type
3812 binding. This means that we already processed this binding
3813 above. */
3815 else if (value_binding)
3817 if (TREE_CODE (value_binding) == TREE_LIST
3818 && TREE_TYPE (value_binding) == error_mark_node)
3819 /* NAME is ambiguous. */
3821 else if (BASELINK_P (value_binding))
3822 /* NAME is some overloaded functions. */
3823 value_binding = BASELINK_FUNCTIONS (value_binding);
3826 /* If we found either a type binding or a value binding, create a
3827 new binding object. */
3828 if (type_binding || value_binding)
3830 binding = new_class_binding (name,
3831 value_binding,
3832 type_binding,
3833 scope);
3834 /* This is a class-scope binding, not a block-scope binding. */
3835 LOCAL_BINDING_P (binding) = 0;
3836 set_inherited_value_binding_p (binding, value_binding, class_type);
3838 else
3839 binding = NULL;
3841 return binding;
3844 /* Make the declaration(s) of X appear in CLASS scope under the name
3845 NAME. Returns true if the binding is valid. */
3847 static bool
3848 push_class_level_binding_1 (tree name, tree x)
3850 cxx_binding *binding;
3851 tree decl = x;
3852 bool ok;
3854 /* The class_binding_level will be NULL if x is a template
3855 parameter name in a member template. */
3856 if (!class_binding_level)
3857 return true;
3859 if (name == error_mark_node)
3860 return false;
3862 /* Can happen for an erroneous declaration (c++/60384). */
3863 if (!identifier_p (name))
3865 gcc_assert (errorcount || sorrycount);
3866 return false;
3869 /* Check for invalid member names. But don't worry about a default
3870 argument-scope lambda being pushed after the class is complete. */
3871 gcc_assert (TYPE_BEING_DEFINED (current_class_type)
3872 || LAMBDA_TYPE_P (TREE_TYPE (decl)));
3873 /* Check that we're pushing into the right binding level. */
3874 gcc_assert (current_class_type == class_binding_level->this_entity);
3876 /* We could have been passed a tree list if this is an ambiguous
3877 declaration. If so, pull the declaration out because
3878 check_template_shadow will not handle a TREE_LIST. */
3879 if (TREE_CODE (decl) == TREE_LIST
3880 && TREE_TYPE (decl) == error_mark_node)
3881 decl = TREE_VALUE (decl);
3883 if (!check_template_shadow (decl))
3884 return false;
3886 /* [class.mem]
3888 If T is the name of a class, then each of the following shall
3889 have a name different from T:
3891 -- every static data member of class T;
3893 -- every member of class T that is itself a type;
3895 -- every enumerator of every member of class T that is an
3896 enumerated type;
3898 -- every member of every anonymous union that is a member of
3899 class T.
3901 (Non-static data members were also forbidden to have the same
3902 name as T until TC1.) */
3903 if ((VAR_P (x)
3904 || TREE_CODE (x) == CONST_DECL
3905 || (TREE_CODE (x) == TYPE_DECL
3906 && !DECL_SELF_REFERENCE_P (x))
3907 /* A data member of an anonymous union. */
3908 || (TREE_CODE (x) == FIELD_DECL
3909 && DECL_CONTEXT (x) != current_class_type))
3910 && DECL_NAME (x) == constructor_name (current_class_type))
3912 tree scope = context_for_name_lookup (x);
3913 if (TYPE_P (scope) && same_type_p (scope, current_class_type))
3915 error ("%qD has the same name as the class in which it is "
3916 "declared",
3918 return false;
3922 /* Get the current binding for NAME in this class, if any. */
3923 binding = IDENTIFIER_BINDING (name);
3924 if (!binding || binding->scope != class_binding_level)
3926 binding = get_class_binding (name, class_binding_level);
3927 /* If a new binding was created, put it at the front of the
3928 IDENTIFIER_BINDING list. */
3929 if (binding)
3931 binding->previous = IDENTIFIER_BINDING (name);
3932 IDENTIFIER_BINDING (name) = binding;
3936 /* If there is already a binding, then we may need to update the
3937 current value. */
3938 if (binding && binding->value)
3940 tree bval = binding->value;
3941 tree old_decl = NULL_TREE;
3942 tree target_decl = strip_using_decl (decl);
3943 tree target_bval = strip_using_decl (bval);
3945 if (INHERITED_VALUE_BINDING_P (binding))
3947 /* If the old binding was from a base class, and was for a
3948 tag name, slide it over to make room for the new binding.
3949 The old binding is still visible if explicitly qualified
3950 with a class-key. */
3951 if (TREE_CODE (target_bval) == TYPE_DECL
3952 && DECL_ARTIFICIAL (target_bval)
3953 && !(TREE_CODE (target_decl) == TYPE_DECL
3954 && DECL_ARTIFICIAL (target_decl)))
3956 old_decl = binding->type;
3957 binding->type = bval;
3958 binding->value = NULL_TREE;
3959 INHERITED_VALUE_BINDING_P (binding) = 0;
3961 else
3963 old_decl = bval;
3964 /* Any inherited type declaration is hidden by the type
3965 declaration in the derived class. */
3966 if (TREE_CODE (target_decl) == TYPE_DECL
3967 && DECL_ARTIFICIAL (target_decl))
3968 binding->type = NULL_TREE;
3971 else if (TREE_CODE (target_decl) == OVERLOAD
3972 && is_overloaded_fn (target_bval))
3973 old_decl = bval;
3974 else if (TREE_CODE (decl) == USING_DECL
3975 && TREE_CODE (bval) == USING_DECL
3976 && same_type_p (USING_DECL_SCOPE (decl),
3977 USING_DECL_SCOPE (bval)))
3978 /* This is a using redeclaration that will be diagnosed later
3979 in supplement_binding */
3981 else if (TREE_CODE (decl) == USING_DECL
3982 && TREE_CODE (bval) == USING_DECL
3983 && DECL_DEPENDENT_P (decl)
3984 && DECL_DEPENDENT_P (bval))
3985 return true;
3986 else if (TREE_CODE (decl) == USING_DECL
3987 && is_overloaded_fn (target_bval))
3988 old_decl = bval;
3989 else if (TREE_CODE (bval) == USING_DECL
3990 && is_overloaded_fn (target_decl))
3991 return true;
3993 if (old_decl && binding->scope == class_binding_level)
3995 binding->value = x;
3996 /* It is always safe to clear INHERITED_VALUE_BINDING_P
3997 here. This function is only used to register bindings
3998 from with the class definition itself. */
3999 INHERITED_VALUE_BINDING_P (binding) = 0;
4000 return true;
4004 /* Note that we declared this value so that we can issue an error if
4005 this is an invalid redeclaration of a name already used for some
4006 other purpose. */
4007 note_name_declared_in_class (name, decl);
4009 /* If we didn't replace an existing binding, put the binding on the
4010 stack of bindings for the identifier, and update the shadowed
4011 list. */
4012 if (binding && binding->scope == class_binding_level)
4013 /* Supplement the existing binding. */
4014 ok = supplement_binding (binding, decl);
4015 else
4017 /* Create a new binding. */
4018 push_binding (name, decl, class_binding_level);
4019 ok = true;
4022 return ok;
4025 /* Wrapper for push_class_level_binding_1. */
4027 bool
4028 push_class_level_binding (tree name, tree x)
4030 bool ret;
4031 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4032 ret = push_class_level_binding_1 (name, x);
4033 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4034 return ret;
4037 /* Process "using SCOPE::NAME" in a class scope. Return the
4038 USING_DECL created. */
4040 tree
4041 do_class_using_decl (tree scope, tree name)
4043 /* The USING_DECL returned by this function. */
4044 tree value;
4045 /* The declaration (or declarations) name by this using
4046 declaration. NULL if we are in a template and cannot figure out
4047 what has been named. */
4048 tree decl;
4049 /* True if SCOPE is a dependent type. */
4050 bool scope_dependent_p;
4051 /* True if SCOPE::NAME is dependent. */
4052 bool name_dependent_p;
4053 /* True if any of the bases of CURRENT_CLASS_TYPE are dependent. */
4054 bool bases_dependent_p;
4055 tree binfo;
4057 if (name == error_mark_node)
4058 return NULL_TREE;
4060 if (!scope || !TYPE_P (scope))
4062 error ("using-declaration for non-member at class scope");
4063 return NULL_TREE;
4066 /* Make sure the name is not invalid */
4067 if (TREE_CODE (name) == BIT_NOT_EXPR)
4069 error ("%<%T::%D%> names destructor", scope, name);
4070 return NULL_TREE;
4072 /* Using T::T declares inheriting ctors, even if T is a typedef. */
4073 if (MAYBE_CLASS_TYPE_P (scope)
4074 && (name == TYPE_IDENTIFIER (scope)
4075 || constructor_name_p (name, scope)))
4077 maybe_warn_cpp0x (CPP0X_INHERITING_CTORS);
4078 name = ctor_identifier;
4079 CLASSTYPE_NON_AGGREGATE (current_class_type) = true;
4081 if (constructor_name_p (name, current_class_type))
4083 error ("%<%T::%D%> names constructor in %qT",
4084 scope, name, current_class_type);
4085 return NULL_TREE;
4088 scope_dependent_p = dependent_scope_p (scope);
4089 name_dependent_p = (scope_dependent_p
4090 || (IDENTIFIER_TYPENAME_P (name)
4091 && dependent_type_p (TREE_TYPE (name))));
4093 bases_dependent_p = any_dependent_bases_p ();
4095 decl = NULL_TREE;
4097 /* From [namespace.udecl]:
4099 A using-declaration used as a member-declaration shall refer to a
4100 member of a base class of the class being defined.
4102 In general, we cannot check this constraint in a template because
4103 we do not know the entire set of base classes of the current
4104 class type. Morover, if SCOPE is dependent, it might match a
4105 non-dependent base. */
4107 if (!scope_dependent_p)
4109 base_kind b_kind;
4110 binfo = lookup_base (current_class_type, scope, ba_any, &b_kind,
4111 tf_warning_or_error);
4112 if (b_kind < bk_proper_base)
4114 if (!bases_dependent_p || b_kind == bk_same_type)
4116 error_not_base_type (scope, current_class_type);
4117 return NULL_TREE;
4120 else if (name == ctor_identifier
4121 && BINFO_INHERITANCE_CHAIN (BINFO_INHERITANCE_CHAIN (binfo)))
4123 error ("cannot inherit constructors from indirect base %qT", scope);
4124 return NULL_TREE;
4126 else if (!name_dependent_p)
4128 decl = lookup_member (binfo, name, 0, false, tf_warning_or_error);
4129 if (!decl)
4131 error ("no members matching %<%T::%D%> in %q#T", scope, name,
4132 scope);
4133 return NULL_TREE;
4135 /* The binfo from which the functions came does not matter. */
4136 if (BASELINK_P (decl))
4137 decl = BASELINK_FUNCTIONS (decl);
4141 value = build_lang_decl (USING_DECL, name, NULL_TREE);
4142 USING_DECL_DECLS (value) = decl;
4143 USING_DECL_SCOPE (value) = scope;
4144 DECL_DEPENDENT_P (value) = !decl;
4146 return value;
4150 /* Return the binding for NAME in NS. If NS is NULL, look in
4151 global_namespace. */
4153 tree
4154 get_namespace_binding (tree ns, tree name)
4156 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4157 if (!ns)
4158 ns = global_namespace;
4159 cxx_binding *binding = find_namespace_binding (ns, name);
4160 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4161 return binding ? binding->value : NULL_TREE;
4164 static void
4165 set_namespace_binding (tree scope, tree name, tree val)
4167 if (scope == NULL_TREE)
4168 scope = global_namespace;
4169 cxx_binding *binding = find_namespace_binding (scope, name, true);
4170 if (!binding->value
4171 /* For templates and using we create a single element OVERLOAD.
4172 Look for the chain to know whether this is really augmenting
4173 an existing overload. */
4174 || (TREE_CODE (val) == OVERLOAD && OVL_CHAIN (val))
4175 || val == error_mark_node)
4176 binding->value = val;
4177 else
4178 supplement_binding (binding, val);
4181 /* Set value binding of NAME in the global namespace to VAL. Does not
4182 add it to the list of things in the namespace. */
4184 void
4185 set_global_binding (tree name, tree val)
4187 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4189 set_namespace_binding (global_namespace, name, val);
4191 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4194 /* Set the context of a declaration to scope. Complain if we are not
4195 outside scope. */
4197 void
4198 set_decl_namespace (tree decl, tree scope, bool friendp)
4200 tree old;
4202 /* Get rid of namespace aliases. */
4203 scope = ORIGINAL_NAMESPACE (scope);
4205 /* It is ok for friends to be qualified in parallel space. */
4206 if (!friendp && !is_nested_namespace (current_namespace, scope))
4207 error ("declaration of %qD not in a namespace surrounding %qD",
4208 decl, scope);
4209 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
4211 /* Writing "int N::i" to declare a variable within "N" is invalid. */
4212 if (scope == current_namespace)
4214 if (at_namespace_scope_p ())
4215 error ("explicit qualification in declaration of %qD",
4216 decl);
4217 return;
4220 /* See whether this has been declared in the namespace. */
4221 old = lookup_qualified_name (scope, DECL_NAME (decl), /*type*/false,
4222 /*complain*/true, /*hidden*/true);
4223 if (old == error_mark_node)
4224 /* No old declaration at all. */
4225 goto complain;
4226 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
4227 if (TREE_CODE (old) == TREE_LIST)
4229 error ("reference to %qD is ambiguous", decl);
4230 print_candidates (old);
4231 return;
4233 if (!OVL_P (decl))
4235 /* We might have found OLD in an inline namespace inside SCOPE. */
4236 if (TREE_CODE (decl) == TREE_CODE (old))
4237 DECL_CONTEXT (decl) = DECL_CONTEXT (old);
4238 /* Don't compare non-function decls with decls_match here, since
4239 it can't check for the correct constness at this
4240 point. pushdecl will find those errors later. */
4241 return;
4243 /* Since decl is a function, old should contain a function decl. */
4244 if (!OVL_P (old))
4245 goto complain;
4246 /* We handle these in check_explicit_instantiation_namespace. */
4247 if (processing_explicit_instantiation)
4248 return;
4249 if (processing_template_decl || processing_specialization)
4250 /* We have not yet called push_template_decl to turn a
4251 FUNCTION_DECL into a TEMPLATE_DECL, so the declarations won't
4252 match. But, we'll check later, when we construct the
4253 template. */
4254 return;
4255 /* Instantiations or specializations of templates may be declared as
4256 friends in any namespace. */
4257 if (friendp && DECL_USE_TEMPLATE (decl))
4258 return;
4259 if (OVL_P (old))
4261 tree found = NULL_TREE;
4263 for (ovl_iterator iter (old); iter; ++iter)
4265 tree ofn = *iter;
4266 /* Adjust DECL_CONTEXT first so decls_match will return true
4267 if DECL will match a declaration in an inline namespace. */
4268 DECL_CONTEXT (decl) = DECL_CONTEXT (ofn);
4269 if (decls_match (decl, ofn))
4271 if (found && !decls_match (found, ofn))
4273 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
4274 error ("reference to %qD is ambiguous", decl);
4275 print_candidates (old);
4276 return;
4278 found = ofn;
4281 if (found)
4283 if (!is_nested_namespace (scope, CP_DECL_CONTEXT (found), true))
4284 goto complain;
4285 if (DECL_HIDDEN_FRIEND_P (found))
4287 pedwarn (DECL_SOURCE_LOCATION (decl), 0,
4288 "%qD has not been declared within %qD", decl, scope);
4289 inform (DECL_SOURCE_LOCATION (found),
4290 "only here as a %<friend%>");
4292 DECL_CONTEXT (decl) = DECL_CONTEXT (found);
4293 return;
4296 else
4298 DECL_CONTEXT (decl) = DECL_CONTEXT (old);
4299 if (decls_match (decl, old))
4300 return;
4303 /* It didn't work, go back to the explicit scope. */
4304 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
4305 complain:
4306 error ("%qD should have been declared inside %qD", decl, scope);
4309 /* Return the namespace where the current declaration is declared. */
4311 tree
4312 current_decl_namespace (void)
4314 tree result;
4315 /* If we have been pushed into a different namespace, use it. */
4316 if (!vec_safe_is_empty (decl_namespace_list))
4317 return decl_namespace_list->last ();
4319 if (current_class_type)
4320 result = decl_namespace_context (current_class_type);
4321 else if (current_function_decl)
4322 result = decl_namespace_context (current_function_decl);
4323 else
4324 result = current_namespace;
4325 return result;
4328 /* Process any ATTRIBUTES on a namespace definition. Returns true if
4329 attribute visibility is seen. */
4331 bool
4332 handle_namespace_attrs (tree ns, tree attributes)
4334 tree d;
4335 bool saw_vis = false;
4337 for (d = attributes; d; d = TREE_CHAIN (d))
4339 tree name = get_attribute_name (d);
4340 tree args = TREE_VALUE (d);
4342 if (is_attribute_p ("visibility", name))
4344 /* attribute visibility is a property of the syntactic block
4345 rather than the namespace as a whole, so we don't touch the
4346 NAMESPACE_DECL at all. */
4347 tree x = args ? TREE_VALUE (args) : NULL_TREE;
4348 if (x == NULL_TREE || TREE_CODE (x) != STRING_CST || TREE_CHAIN (args))
4350 warning (OPT_Wattributes,
4351 "%qD attribute requires a single NTBS argument",
4352 name);
4353 continue;
4356 if (!TREE_PUBLIC (ns))
4357 warning (OPT_Wattributes,
4358 "%qD attribute is meaningless since members of the "
4359 "anonymous namespace get local symbols", name);
4361 push_visibility (TREE_STRING_POINTER (x), 1);
4362 saw_vis = true;
4364 else if (is_attribute_p ("abi_tag", name))
4366 if (!DECL_NAME (ns))
4368 warning (OPT_Wattributes, "ignoring %qD attribute on anonymous "
4369 "namespace", name);
4370 continue;
4372 if (!DECL_NAMESPACE_INLINE_P (ns))
4374 warning (OPT_Wattributes, "ignoring %qD attribute on non-inline "
4375 "namespace", name);
4376 continue;
4378 if (!args)
4380 tree dn = DECL_NAME (ns);
4381 args = build_string (IDENTIFIER_LENGTH (dn) + 1,
4382 IDENTIFIER_POINTER (dn));
4383 TREE_TYPE (args) = char_array_type_node;
4384 args = fix_string_type (args);
4385 args = build_tree_list (NULL_TREE, args);
4387 if (check_abi_tag_args (args, name))
4388 DECL_ATTRIBUTES (ns) = tree_cons (name, args,
4389 DECL_ATTRIBUTES (ns));
4391 else
4393 warning (OPT_Wattributes, "%qD attribute directive ignored",
4394 name);
4395 continue;
4399 return saw_vis;
4402 /* Temporarily set the namespace for the current declaration. */
4404 void
4405 push_decl_namespace (tree decl)
4407 if (TREE_CODE (decl) != NAMESPACE_DECL)
4408 decl = decl_namespace_context (decl);
4409 vec_safe_push (decl_namespace_list, ORIGINAL_NAMESPACE (decl));
4412 /* [namespace.memdef]/2 */
4414 void
4415 pop_decl_namespace (void)
4417 decl_namespace_list->pop ();
4420 /* Process a namespace-alias declaration. */
4422 void
4423 do_namespace_alias (tree alias, tree name_space)
4425 if (name_space == error_mark_node)
4426 return;
4428 gcc_assert (TREE_CODE (name_space) == NAMESPACE_DECL);
4430 name_space = ORIGINAL_NAMESPACE (name_space);
4432 /* Build the alias. */
4433 alias = build_lang_decl (NAMESPACE_DECL, alias, void_type_node);
4434 DECL_NAMESPACE_ALIAS (alias) = name_space;
4435 DECL_EXTERNAL (alias) = 1;
4436 DECL_CONTEXT (alias) = FROB_CONTEXT (current_scope ());
4437 pushdecl (alias);
4439 /* Emit debug info for namespace alias. */
4440 if (!building_stmt_list_p ())
4441 (*debug_hooks->early_global_decl) (alias);
4444 /* Like pushdecl, only it places X in the current namespace,
4445 if appropriate. */
4447 tree
4448 pushdecl_namespace_level (tree x, bool is_friend)
4450 cp_binding_level *b = current_binding_level;
4451 tree t;
4453 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4454 t = pushdecl_with_scope_1
4455 (x, NAMESPACE_LEVEL (current_namespace), is_friend);
4457 /* Now, the type_shadowed stack may screw us. Munge it so it does
4458 what we want. */
4459 if (TREE_CODE (t) == TYPE_DECL)
4461 tree name = DECL_NAME (t);
4462 tree newval;
4463 tree *ptr = (tree *)0;
4464 for (; !global_scope_p (b); b = b->level_chain)
4466 tree shadowed = b->type_shadowed;
4467 for (; shadowed; shadowed = TREE_CHAIN (shadowed))
4468 if (TREE_PURPOSE (shadowed) == name)
4470 ptr = &TREE_VALUE (shadowed);
4471 /* Can't break out of the loop here because sometimes
4472 a binding level will have duplicate bindings for
4473 PT names. It's gross, but I haven't time to fix it. */
4476 newval = TREE_TYPE (t);
4477 if (ptr == (tree *)0)
4479 /* @@ This shouldn't be needed. My test case "zstring.cc" trips
4480 up here if this is changed to an assertion. --KR */
4481 SET_IDENTIFIER_TYPE_VALUE (name, t);
4483 else
4485 *ptr = newval;
4488 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4489 return t;
4492 /* Process a using-declaration appearing in namespace scope. */
4494 void
4495 finish_namespace_using_decl (tree decl, tree scope, tree name)
4497 tree orig_decl = decl;
4499 gcc_checking_assert (current_binding_level->kind == sk_namespace);
4500 decl = validate_nonmember_using_decl (decl, scope, name);
4501 if (decl == NULL_TREE)
4502 return;
4504 cxx_binding *binding
4505 = find_namespace_binding (current_namespace, name, true);
4507 tree value = binding->value;
4508 tree type = binding->type;
4510 do_nonmember_using_decl (scope, name, &value, &type);
4512 /* Copy declarations found. */
4513 binding->value = value;
4514 binding->type = type;
4516 /* Emit debug info. */
4517 gcc_assert (!processing_template_decl);
4518 if (!processing_template_decl)
4519 cp_emit_debug_info_for_using (orig_decl, current_namespace);
4522 /* Process a using-declaration at local scope. */
4524 void
4525 finish_local_using_decl (tree decl, tree scope, tree name)
4527 tree orig_decl = decl;
4529 gcc_checking_assert (current_binding_level->kind != sk_class
4530 && current_binding_level->kind != sk_namespace);
4531 decl = validate_nonmember_using_decl (decl, scope, name);
4532 if (decl == NULL_TREE)
4533 return;
4535 add_decl_expr (decl);
4537 cxx_binding *binding = find_local_binding (current_binding_level, name);
4538 tree value = binding ? binding->value : NULL_TREE;
4539 tree type = binding ? binding->type : NULL_TREE;
4541 do_nonmember_using_decl (scope, name, &value, &type);
4543 if (!value)
4545 else if (binding && value == binding->value)
4547 else if (binding && binding->value && TREE_CODE (value) == OVERLOAD)
4549 update_local_overload (IDENTIFIER_BINDING (name), value);
4550 IDENTIFIER_BINDING (name)->value = value;
4552 else
4553 /* Install the new binding. */
4554 push_local_binding (name, value, true);
4556 if (!type)
4558 else if (binding && type == binding->type)
4560 else
4562 push_local_binding (name, type, true);
4563 set_identifier_type_value (name, type);
4566 /* Emit debug info. */
4567 if (!processing_template_decl)
4568 cp_emit_debug_info_for_using (orig_decl, current_scope ());
4571 /* Return the declarations that are members of the namespace NS. */
4573 tree
4574 cp_namespace_decls (tree ns)
4576 return NAMESPACE_LEVEL (ns)->names;
4579 /* Combine prefer_type and namespaces_only into flags. */
4581 static int
4582 lookup_flags (int prefer_type, int namespaces_only)
4584 if (namespaces_only)
4585 return LOOKUP_PREFER_NAMESPACES;
4586 if (prefer_type > 1)
4587 return LOOKUP_PREFER_TYPES;
4588 if (prefer_type > 0)
4589 return LOOKUP_PREFER_BOTH;
4590 return 0;
4593 /* Given a lookup that returned VAL, use FLAGS to decide if we want to
4594 ignore it or not. Subroutine of lookup_name_real and
4595 lookup_type_scope. */
4597 static bool
4598 qualify_lookup (tree val, int flags)
4600 if (val == NULL_TREE)
4601 return false;
4602 if ((flags & LOOKUP_PREFER_NAMESPACES) && TREE_CODE (val) == NAMESPACE_DECL)
4603 return true;
4604 if (flags & LOOKUP_PREFER_TYPES)
4606 tree target_val = strip_using_decl (val);
4607 if (TREE_CODE (target_val) == TYPE_DECL
4608 || TREE_CODE (target_val) == TEMPLATE_DECL)
4609 return true;
4611 if (flags & (LOOKUP_PREFER_NAMESPACES | LOOKUP_PREFER_TYPES))
4612 return false;
4613 /* Look through lambda things that we shouldn't be able to see. */
4614 if (is_lambda_ignored_entity (val))
4615 return false;
4616 return true;
4619 /* Suggest alternatives for NAME, an IDENTIFIER_NODE for which name
4620 lookup failed. Search through all available namespaces and print out
4621 possible candidates. If no exact matches are found, and
4622 SUGGEST_MISSPELLINGS is true, then also look for near-matches and
4623 suggest the best near-match, if there is one. */
4625 void
4626 suggest_alternatives_for (location_t location, tree name,
4627 bool suggest_misspellings)
4629 vec<tree> candidates = vNULL;
4630 vec<tree> namespaces_to_search = vNULL;
4631 int max_to_search = PARAM_VALUE (CXX_MAX_NAMESPACES_FOR_DIAGNOSTIC_HELP);
4632 int n_searched = 0;
4633 tree t;
4634 unsigned ix;
4636 namespaces_to_search.safe_push (global_namespace);
4638 while (!namespaces_to_search.is_empty ()
4639 && n_searched < max_to_search)
4641 tree scope = namespaces_to_search.pop ();
4642 name_lookup lookup (name, 0);
4643 cp_binding_level *level = NAMESPACE_LEVEL (scope);
4645 n_searched++;
4647 /* Look in this namespace. */
4648 if (qualified_namespace_lookup (scope, &lookup))
4649 candidates.safe_push (lookup.value);
4651 /* Add child namespaces. */
4652 for (t = level->namespaces; t; t = DECL_CHAIN (t))
4653 namespaces_to_search.safe_push (t);
4656 /* If we stopped before we could examine all namespaces, inform the
4657 user. Do this even if we don't have any candidates, since there
4658 might be more candidates further down that we weren't able to
4659 find. */
4660 if (n_searched >= max_to_search
4661 && !namespaces_to_search.is_empty ())
4662 inform (location,
4663 "maximum limit of %d namespaces searched for %qE",
4664 max_to_search, name);
4666 namespaces_to_search.release ();
4668 /* Nothing useful to report for NAME. Report on likely misspellings,
4669 or do nothing. */
4670 if (candidates.is_empty ())
4672 if (suggest_misspellings)
4674 const char *fuzzy_name = lookup_name_fuzzy (name, FUZZY_LOOKUP_NAME);
4675 if (fuzzy_name)
4677 gcc_rich_location richloc (location);
4678 richloc.add_fixit_replace (fuzzy_name);
4679 inform_at_rich_loc (&richloc, "suggested alternative: %qs",
4680 fuzzy_name);
4683 return;
4686 inform_n (location, candidates.length (),
4687 "suggested alternative:",
4688 "suggested alternatives:");
4690 FOR_EACH_VEC_ELT (candidates, ix, t)
4691 inform (location_of (t), " %qE", t);
4693 candidates.release ();
4696 /* Subroutine of maybe_suggest_missing_header for handling unrecognized names
4697 for some of the most common names within "std::".
4698 Given non-NULL NAME, a name for lookup within "std::", return the header
4699 name defining it within the C++ Standard Library (without '<' and '>'),
4700 or NULL. */
4702 static const char *
4703 get_std_name_hint (const char *name)
4705 struct std_name_hint
4707 const char *name;
4708 const char *header;
4710 static const std_name_hint hints[] = {
4711 /* <array>. */
4712 {"array", "array"}, // C++11
4713 /* <deque>. */
4714 {"deque", "deque"},
4715 /* <forward_list>. */
4716 {"forward_list", "forward_list"}, // C++11
4717 /* <fstream>. */
4718 {"basic_filebuf", "fstream"},
4719 {"basic_ifstream", "fstream"},
4720 {"basic_ofstream", "fstream"},
4721 {"basic_fstream", "fstream"},
4722 /* <iostream>. */
4723 {"cin", "iostream"},
4724 {"cout", "iostream"},
4725 {"cerr", "iostream"},
4726 {"clog", "iostream"},
4727 {"wcin", "iostream"},
4728 {"wcout", "iostream"},
4729 {"wclog", "iostream"},
4730 /* <list>. */
4731 {"list", "list"},
4732 /* <map>. */
4733 {"map", "map"},
4734 {"multimap", "map"},
4735 /* <queue>. */
4736 {"queue", "queue"},
4737 {"priority_queue", "queue"},
4738 /* <ostream>. */
4739 {"ostream", "ostream"},
4740 {"wostream", "ostream"},
4741 {"ends", "ostream"},
4742 {"flush", "ostream"},
4743 {"endl", "ostream"},
4744 /* <set>. */
4745 {"set", "set"},
4746 {"multiset", "set"},
4747 /* <sstream>. */
4748 {"basic_stringbuf", "sstream"},
4749 {"basic_istringstream", "sstream"},
4750 {"basic_ostringstream", "sstream"},
4751 {"basic_stringstream", "sstream"},
4752 /* <stack>. */
4753 {"stack", "stack"},
4754 /* <string>. */
4755 {"string", "string"},
4756 {"wstring", "string"},
4757 {"u16string", "string"},
4758 {"u32string", "string"},
4759 /* <unordered_map>. */
4760 {"unordered_map", "unordered_map"}, // C++11
4761 {"unordered_multimap", "unordered_map"}, // C++11
4762 /* <unordered_set>. */
4763 {"unordered_set", "unordered_set"}, // C++11
4764 {"unordered_multiset", "unordered_set"}, // C++11
4765 /* <vector>. */
4766 {"vector", "vector"},
4768 const size_t num_hints = sizeof (hints) / sizeof (hints[0]);
4769 for (size_t i = 0; i < num_hints; i++)
4771 if (0 == strcmp (name, hints[i].name))
4772 return hints[i].header;
4774 return NULL;
4777 /* Subroutine of suggest_alternative_in_explicit_scope, for use when we have no
4778 suggestions to offer.
4779 If SCOPE is the "std" namespace, then suggest pertinent header
4780 files for NAME. */
4782 static void
4783 maybe_suggest_missing_header (location_t location, tree name, tree scope)
4785 if (scope == NULL_TREE)
4786 return;
4787 if (TREE_CODE (scope) != NAMESPACE_DECL)
4788 return;
4789 /* We only offer suggestions for the "std" namespace. */
4790 if (scope != std_node)
4791 return;
4792 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
4794 const char *name_str = IDENTIFIER_POINTER (name);
4795 const char *header_hint = get_std_name_hint (name_str);
4796 if (header_hint)
4797 inform (location,
4798 "%<std::%s%> is defined in header %<<%s>%>;"
4799 " did you forget to %<#include <%s>%>?",
4800 name_str, header_hint, header_hint);
4803 /* Look for alternatives for NAME, an IDENTIFIER_NODE for which name
4804 lookup failed within the explicitly provided SCOPE. Suggest the
4805 the best meaningful candidates (if any) as a fix-it hint.
4806 Return true iff a suggestion was provided. */
4808 bool
4809 suggest_alternative_in_explicit_scope (location_t location, tree name,
4810 tree scope)
4812 /* Resolve any namespace aliases. */
4813 scope = ORIGINAL_NAMESPACE (scope);
4815 cp_binding_level *level = NAMESPACE_LEVEL (scope);
4817 best_match <tree, const char *> bm (name);
4818 consider_binding_level (name, bm, level, false, FUZZY_LOOKUP_NAME);
4820 /* See if we have a good suggesion for the user. */
4821 const char *fuzzy_name = bm.get_best_meaningful_candidate ();
4822 if (fuzzy_name)
4824 gcc_rich_location richloc (location);
4825 richloc.add_fixit_replace (fuzzy_name);
4826 inform_at_rich_loc (&richloc, "suggested alternative: %qs",
4827 fuzzy_name);
4828 return true;
4830 else
4831 maybe_suggest_missing_header (location, name, scope);
4833 return false;
4836 /* Look up NAME (an IDENTIFIER_NODE) in SCOPE (either a NAMESPACE_DECL
4837 or a class TYPE).
4839 If PREFER_TYPE is > 0, we only return TYPE_DECLs or namespaces.
4840 If PREFER_TYPE is > 1, we only return TYPE_DECLs.
4842 Returns a DECL (or OVERLOAD, or BASELINK) representing the
4843 declaration found. If no suitable declaration can be found,
4844 ERROR_MARK_NODE is returned. If COMPLAIN is true and SCOPE is
4845 neither a class-type nor a namespace a diagnostic is issued. */
4847 tree
4848 lookup_qualified_name (tree scope, tree name, int prefer_type, bool complain,
4849 bool find_hidden)
4851 tree t = NULL_TREE;
4853 if (TREE_CODE (scope) == NAMESPACE_DECL)
4855 int flags = lookup_flags (prefer_type, /*namespaces_only*/false);
4856 if (find_hidden)
4857 flags |= LOOKUP_HIDDEN;
4858 name_lookup lookup (name, flags);
4860 if (qualified_namespace_lookup (scope, &lookup))
4861 t = lookup.value;
4863 else if (cxx_dialect != cxx98 && TREE_CODE (scope) == ENUMERAL_TYPE)
4864 t = lookup_enumerator (scope, name);
4865 else if (is_class_type (scope, complain))
4866 t = lookup_member (scope, name, 2, prefer_type, tf_warning_or_error);
4868 if (!t)
4869 return error_mark_node;
4870 return t;
4873 /* [namespace.qual]
4874 Accepts the NAME to lookup and its qualifying SCOPE.
4875 Returns the name/type pair found into the cxx_binding *RESULT,
4876 or false on error. */
4878 static bool
4879 qualified_namespace_lookup (tree scope, name_lookup *lookup)
4881 timevar_start (TV_NAME_LOOKUP);
4882 query_oracle (lookup->name);
4883 bool found = lookup->search_qualified (ORIGINAL_NAMESPACE (scope));
4884 timevar_stop (TV_NAME_LOOKUP);
4885 return found;
4888 /* Helper function for lookup_name_fuzzy.
4889 Traverse binding level LVL, looking for good name matches for NAME
4890 (and BM). */
4891 static void
4892 consider_binding_level (tree name, best_match <tree, const char *> &bm,
4893 cp_binding_level *lvl, bool look_within_fields,
4894 enum lookup_name_fuzzy_kind kind)
4896 if (look_within_fields)
4897 if (lvl->this_entity && TREE_CODE (lvl->this_entity) == RECORD_TYPE)
4899 tree type = lvl->this_entity;
4900 bool want_type_p = (kind == FUZZY_LOOKUP_TYPENAME);
4901 tree best_matching_field
4902 = lookup_member_fuzzy (type, name, want_type_p);
4903 if (best_matching_field)
4904 bm.consider (IDENTIFIER_POINTER (best_matching_field));
4907 for (tree t = lvl->names; t; t = TREE_CHAIN (t))
4909 tree d = t;
4911 /* OVERLOADs or decls from using declaration are wrapped into
4912 TREE_LIST. */
4913 if (TREE_CODE (d) == TREE_LIST)
4914 d = OVL_FIRST (TREE_VALUE (d));
4916 /* Don't use bindings from implicitly declared functions,
4917 as they were likely misspellings themselves. */
4918 if (TREE_TYPE (d) == error_mark_node)
4919 continue;
4921 /* Skip anticipated decls of builtin functions. */
4922 if (TREE_CODE (d) == FUNCTION_DECL
4923 && DECL_BUILT_IN (d)
4924 && DECL_ANTICIPATED (d))
4925 continue;
4927 if (tree name = DECL_NAME (d))
4928 /* Ignore internal names with spaces in them. */
4929 if (!strchr (IDENTIFIER_POINTER (name), ' '))
4930 bm.consider (IDENTIFIER_POINTER (name));
4934 /* Search for near-matches for NAME within the current bindings, and within
4935 macro names, returning the best match as a const char *, or NULL if
4936 no reasonable match is found. */
4938 const char *
4939 lookup_name_fuzzy (tree name, enum lookup_name_fuzzy_kind kind)
4941 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
4943 best_match <tree, const char *> bm (name);
4945 cp_binding_level *lvl;
4946 for (lvl = scope_chain->class_bindings; lvl; lvl = lvl->level_chain)
4947 consider_binding_level (name, bm, lvl, true, kind);
4949 for (lvl = current_binding_level; lvl; lvl = lvl->level_chain)
4950 consider_binding_level (name, bm, lvl, false, kind);
4952 /* Consider macros: if the user misspelled a macro name e.g. "SOME_MACRO"
4954 x = SOME_OTHER_MACRO (y);
4955 then "SOME_OTHER_MACRO" will survive to the frontend and show up
4956 as a misspelled identifier.
4958 Use the best distance so far so that a candidate is only set if
4959 a macro is better than anything so far. This allows early rejection
4960 (without calculating the edit distance) of macro names that must have
4961 distance >= bm.get_best_distance (), and means that we only get a
4962 non-NULL result for best_macro_match if it's better than any of
4963 the identifiers already checked. */
4964 best_macro_match bmm (name, bm.get_best_distance (), parse_in);
4965 cpp_hashnode *best_macro = bmm.get_best_meaningful_candidate ();
4966 /* If a macro is the closest so far to NAME, consider it. */
4967 if (best_macro)
4968 bm.consider ((const char *)best_macro->ident.str);
4970 /* Try the "starts_decl_specifier_p" keywords to detect
4971 "singed" vs "signed" typos. */
4972 for (unsigned i = 0; i < num_c_common_reswords; i++)
4974 const c_common_resword *resword = &c_common_reswords[i];
4976 if (kind == FUZZY_LOOKUP_TYPENAME)
4977 if (!cp_keyword_starts_decl_specifier_p (resword->rid))
4978 continue;
4980 tree resword_identifier = ridpointers [resword->rid];
4981 if (!resword_identifier)
4982 continue;
4983 gcc_assert (TREE_CODE (resword_identifier) == IDENTIFIER_NODE);
4985 /* Only consider reserved words that survived the
4986 filtering in init_reswords (e.g. for -std). */
4987 if (!C_IS_RESERVED_WORD (resword_identifier))
4988 continue;
4990 bm.consider (IDENTIFIER_POINTER (resword_identifier));
4993 return bm.get_best_meaningful_candidate ();
4996 /* Subroutine of outer_binding.
4998 Returns TRUE if BINDING is a binding to a template parameter of
4999 SCOPE. In that case SCOPE is the scope of a primary template
5000 parameter -- in the sense of G++, i.e, a template that has its own
5001 template header.
5003 Returns FALSE otherwise. */
5005 static bool
5006 binding_to_template_parms_of_scope_p (cxx_binding *binding,
5007 cp_binding_level *scope)
5009 tree binding_value, tmpl, tinfo;
5010 int level;
5012 if (!binding || !scope || !scope->this_entity)
5013 return false;
5015 binding_value = binding->value ? binding->value : binding->type;
5016 tinfo = get_template_info (scope->this_entity);
5018 /* BINDING_VALUE must be a template parm. */
5019 if (binding_value == NULL_TREE
5020 || (!DECL_P (binding_value)
5021 || !DECL_TEMPLATE_PARM_P (binding_value)))
5022 return false;
5024 /* The level of BINDING_VALUE. */
5025 level =
5026 template_type_parameter_p (binding_value)
5027 ? TEMPLATE_PARM_LEVEL (TEMPLATE_TYPE_PARM_INDEX
5028 (TREE_TYPE (binding_value)))
5029 : TEMPLATE_PARM_LEVEL (DECL_INITIAL (binding_value));
5031 /* The template of the current scope, iff said scope is a primary
5032 template. */
5033 tmpl = (tinfo
5034 && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
5035 ? TI_TEMPLATE (tinfo)
5036 : NULL_TREE);
5038 /* If the level of the parm BINDING_VALUE equals the depth of TMPL,
5039 then BINDING_VALUE is a parameter of TMPL. */
5040 return (tmpl && level == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)));
5043 /* Return the innermost non-namespace binding for NAME from a scope
5044 containing BINDING, or, if BINDING is NULL, the current scope.
5045 Please note that for a given template, the template parameters are
5046 considered to be in the scope containing the current scope.
5047 If CLASS_P is false, then class bindings are ignored. */
5049 cxx_binding *
5050 outer_binding (tree name,
5051 cxx_binding *binding,
5052 bool class_p)
5054 cxx_binding *outer;
5055 cp_binding_level *scope;
5056 cp_binding_level *outer_scope;
5058 if (binding)
5060 scope = binding->scope->level_chain;
5061 outer = binding->previous;
5063 else
5065 scope = current_binding_level;
5066 outer = IDENTIFIER_BINDING (name);
5068 outer_scope = outer ? outer->scope : NULL;
5070 /* Because we create class bindings lazily, we might be missing a
5071 class binding for NAME. If there are any class binding levels
5072 between the LAST_BINDING_LEVEL and the scope in which OUTER was
5073 declared, we must lookup NAME in those class scopes. */
5074 if (class_p)
5075 while (scope && scope != outer_scope && scope->kind != sk_namespace)
5077 if (scope->kind == sk_class)
5079 cxx_binding *class_binding;
5081 class_binding = get_class_binding (name, scope);
5082 if (class_binding)
5084 /* Thread this new class-scope binding onto the
5085 IDENTIFIER_BINDING list so that future lookups
5086 find it quickly. */
5087 class_binding->previous = outer;
5088 if (binding)
5089 binding->previous = class_binding;
5090 else
5091 IDENTIFIER_BINDING (name) = class_binding;
5092 return class_binding;
5095 /* If we are in a member template, the template parms of the member
5096 template are considered to be inside the scope of the containing
5097 class, but within G++ the class bindings are all pushed between the
5098 template parms and the function body. So if the outer binding is
5099 a template parm for the current scope, return it now rather than
5100 look for a class binding. */
5101 if (outer_scope && outer_scope->kind == sk_template_parms
5102 && binding_to_template_parms_of_scope_p (outer, scope))
5103 return outer;
5105 scope = scope->level_chain;
5108 return outer;
5111 /* Return the innermost block-scope or class-scope value binding for
5112 NAME, or NULL_TREE if there is no such binding. */
5114 tree
5115 innermost_non_namespace_value (tree name)
5117 cxx_binding *binding;
5118 binding = outer_binding (name, /*binding=*/NULL, /*class_p=*/true);
5119 return binding ? binding->value : NULL_TREE;
5122 /* Look up NAME in the current binding level and its superiors in the
5123 namespace of variables, functions and typedefs. Return a ..._DECL
5124 node of some kind representing its definition if there is only one
5125 such declaration, or return a TREE_LIST with all the overloaded
5126 definitions if there are many, or return 0 if it is undefined.
5127 Hidden name, either friend declaration or built-in function, are
5128 not ignored.
5130 If PREFER_TYPE is > 0, we prefer TYPE_DECLs or namespaces.
5131 If PREFER_TYPE is > 1, we reject non-type decls (e.g. namespaces).
5132 Otherwise we prefer non-TYPE_DECLs.
5134 If NONCLASS is nonzero, bindings in class scopes are ignored. If
5135 BLOCK_P is false, bindings in block scopes are ignored. */
5137 static tree
5138 lookup_name_real_1 (tree name, int prefer_type, int nonclass, bool block_p,
5139 int namespaces_only, int flags)
5141 cxx_binding *iter;
5142 tree val = NULL_TREE;
5144 query_oracle (name);
5146 /* Conversion operators are handled specially because ordinary
5147 unqualified name lookup will not find template conversion
5148 operators. */
5149 if (IDENTIFIER_TYPENAME_P (name))
5151 cp_binding_level *level;
5153 for (level = current_binding_level;
5154 level && level->kind != sk_namespace;
5155 level = level->level_chain)
5157 tree class_type;
5158 tree operators;
5160 /* A conversion operator can only be declared in a class
5161 scope. */
5162 if (level->kind != sk_class)
5163 continue;
5165 /* Lookup the conversion operator in the class. */
5166 class_type = level->this_entity;
5167 operators = lookup_fnfields (class_type, name, /*protect=*/0);
5168 if (operators)
5169 return operators;
5172 return NULL_TREE;
5175 flags |= lookup_flags (prefer_type, namespaces_only);
5177 /* First, look in non-namespace scopes. */
5179 if (current_class_type == NULL_TREE)
5180 nonclass = 1;
5182 if (block_p || !nonclass)
5183 for (iter = outer_binding (name, NULL, !nonclass);
5184 iter;
5185 iter = outer_binding (name, iter, !nonclass))
5187 tree binding;
5189 /* Skip entities we don't want. */
5190 if (LOCAL_BINDING_P (iter) ? !block_p : nonclass)
5191 continue;
5193 /* If this is the kind of thing we're looking for, we're done. */
5194 if (qualify_lookup (iter->value, flags))
5195 binding = iter->value;
5196 else if ((flags & LOOKUP_PREFER_TYPES)
5197 && qualify_lookup (iter->type, flags))
5198 binding = iter->type;
5199 else
5200 binding = NULL_TREE;
5202 if (binding)
5204 if (TREE_CODE (binding) == TYPE_DECL && DECL_HIDDEN_P (binding))
5206 /* A non namespace-scope binding can only be hidden in the
5207 presence of a local class, due to friend declarations.
5209 In particular, consider:
5211 struct C;
5212 void f() {
5213 struct A {
5214 friend struct B;
5215 friend struct C;
5216 void g() {
5217 B* b; // error: B is hidden
5218 C* c; // OK, finds ::C
5221 B *b; // error: B is hidden
5222 C *c; // OK, finds ::C
5223 struct B {};
5224 B *bb; // OK
5227 The standard says that "B" is a local class in "f"
5228 (but not nested within "A") -- but that name lookup
5229 for "B" does not find this declaration until it is
5230 declared directly with "f".
5232 In particular:
5234 [class.friend]
5236 If a friend declaration appears in a local class and
5237 the name specified is an unqualified name, a prior
5238 declaration is looked up without considering scopes
5239 that are outside the innermost enclosing non-class
5240 scope. For a friend function declaration, if there is
5241 no prior declaration, the program is ill-formed. For a
5242 friend class declaration, if there is no prior
5243 declaration, the class that is specified belongs to the
5244 innermost enclosing non-class scope, but if it is
5245 subsequently referenced, its name is not found by name
5246 lookup until a matching declaration is provided in the
5247 innermost enclosing nonclass scope.
5249 So just keep looking for a non-hidden binding.
5251 gcc_assert (TREE_CODE (binding) == TYPE_DECL);
5252 continue;
5254 val = binding;
5255 break;
5259 /* Now lookup in namespace scopes. */
5260 if (!val)
5262 name_lookup lookup (name, flags);
5263 if (lookup.search_unqualified
5264 (current_decl_namespace (), current_binding_level))
5265 val = lookup.value;
5268 /* If we have a single function from a using decl, pull it out. */
5269 if (val && TREE_CODE (val) == OVERLOAD && !really_overloaded_fn (val))
5270 val = OVL_FUNCTION (val);
5272 return val;
5275 /* Wrapper for lookup_name_real_1. */
5277 tree
5278 lookup_name_real (tree name, int prefer_type, int nonclass, bool block_p,
5279 int namespaces_only, int flags)
5281 tree ret;
5282 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
5283 ret = lookup_name_real_1 (name, prefer_type, nonclass, block_p,
5284 namespaces_only, flags);
5285 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
5286 return ret;
5289 tree
5290 lookup_name_nonclass (tree name)
5292 return lookup_name_real (name, 0, 1, /*block_p=*/true, 0, 0);
5295 tree
5296 lookup_name (tree name)
5298 return lookup_name_real (name, 0, 0, /*block_p=*/true, 0, 0);
5301 tree
5302 lookup_name_prefer_type (tree name, int prefer_type)
5304 return lookup_name_real (name, prefer_type, 0, /*block_p=*/true, 0, 0);
5307 /* Look up NAME for type used in elaborated name specifier in
5308 the scopes given by SCOPE. SCOPE can be either TS_CURRENT or
5309 TS_WITHIN_ENCLOSING_NON_CLASS. Although not implied by the
5310 name, more scopes are checked if cleanup or template parameter
5311 scope is encountered.
5313 Unlike lookup_name_real, we make sure that NAME is actually
5314 declared in the desired scope, not from inheritance, nor using
5315 directive. For using declaration, there is DR138 still waiting
5316 to be resolved. Hidden name coming from an earlier friend
5317 declaration is also returned.
5319 A TYPE_DECL best matching the NAME is returned. Catching error
5320 and issuing diagnostics are caller's responsibility. */
5322 static tree
5323 lookup_type_scope_1 (tree name, tag_scope scope)
5325 cxx_binding *iter = NULL;
5326 tree val = NULL_TREE;
5328 /* Look in non-namespace scope first. */
5329 if (current_binding_level->kind != sk_namespace)
5330 iter = outer_binding (name, NULL, /*class_p=*/ true);
5331 for (; iter; iter = outer_binding (name, iter, /*class_p=*/ true))
5333 /* Check if this is the kind of thing we're looking for.
5334 If SCOPE is TS_CURRENT, also make sure it doesn't come from
5335 base class. For ITER->VALUE, we can simply use
5336 INHERITED_VALUE_BINDING_P. For ITER->TYPE, we have to use
5337 our own check.
5339 We check ITER->TYPE before ITER->VALUE in order to handle
5340 typedef struct C {} C;
5341 correctly. */
5343 if (qualify_lookup (iter->type, LOOKUP_PREFER_TYPES)
5344 && (scope != ts_current
5345 || LOCAL_BINDING_P (iter)
5346 || DECL_CONTEXT (iter->type) == iter->scope->this_entity))
5347 val = iter->type;
5348 else if ((scope != ts_current
5349 || !INHERITED_VALUE_BINDING_P (iter))
5350 && qualify_lookup (iter->value, LOOKUP_PREFER_TYPES))
5351 val = iter->value;
5353 if (val)
5354 break;
5357 /* Look in namespace scope. */
5358 if (!val)
5360 iter = find_namespace_binding (current_decl_namespace (), name);
5362 if (iter)
5364 /* If this is the kind of thing we're looking for, we're done. */
5365 if (qualify_lookup (iter->type, LOOKUP_PREFER_TYPES))
5366 val = iter->type;
5367 else if (qualify_lookup (iter->value, LOOKUP_PREFER_TYPES))
5368 val = iter->value;
5373 /* Type found, check if it is in the allowed scopes, ignoring cleanup
5374 and template parameter scopes. */
5375 if (val)
5377 cp_binding_level *b = current_binding_level;
5378 while (b)
5380 if (iter->scope == b)
5381 return val;
5383 if (b->kind == sk_cleanup || b->kind == sk_template_parms
5384 || b->kind == sk_function_parms)
5385 b = b->level_chain;
5386 else if (b->kind == sk_class
5387 && scope == ts_within_enclosing_non_class)
5388 b = b->level_chain;
5389 else
5390 break;
5394 return NULL_TREE;
5397 /* Wrapper for lookup_type_scope_1. */
5399 tree
5400 lookup_type_scope (tree name, tag_scope scope)
5402 tree ret;
5403 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
5404 ret = lookup_type_scope_1 (name, scope);
5405 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
5406 return ret;
5409 /* Returns true iff DECL is a block-scope extern declaration of a function
5410 or variable. */
5412 bool
5413 is_local_extern (tree decl)
5415 cxx_binding *binding;
5417 /* For functions, this is easy. */
5418 if (TREE_CODE (decl) == FUNCTION_DECL)
5419 return DECL_LOCAL_FUNCTION_P (decl);
5421 if (!VAR_P (decl))
5422 return false;
5423 if (!current_function_decl)
5424 return false;
5426 /* For variables, this is not easy. We need to look at the binding stack
5427 for the identifier to see whether the decl we have is a local. */
5428 for (binding = IDENTIFIER_BINDING (DECL_NAME (decl));
5429 binding && binding->scope->kind != sk_namespace;
5430 binding = binding->previous)
5431 if (binding->value == decl)
5432 return LOCAL_BINDING_P (binding);
5434 return false;
5437 /* The type TYPE is being declared. If it is a class template, or a
5438 specialization of a class template, do any processing required and
5439 perform error-checking. If IS_FRIEND is nonzero, this TYPE is
5440 being declared a friend. B is the binding level at which this TYPE
5441 should be bound.
5443 Returns the TYPE_DECL for TYPE, which may have been altered by this
5444 processing. */
5446 static tree
5447 maybe_process_template_type_declaration (tree type, int is_friend,
5448 cp_binding_level *b)
5450 tree decl = TYPE_NAME (type);
5452 if (processing_template_parmlist)
5453 /* You can't declare a new template type in a template parameter
5454 list. But, you can declare a non-template type:
5456 template <class A*> struct S;
5458 is a forward-declaration of `A'. */
5460 else if (b->kind == sk_namespace
5461 && current_binding_level->kind != sk_namespace)
5462 /* If this new type is being injected into a containing scope,
5463 then it's not a template type. */
5465 else
5467 gcc_assert (MAYBE_CLASS_TYPE_P (type)
5468 || TREE_CODE (type) == ENUMERAL_TYPE);
5470 if (processing_template_decl)
5472 /* This may change after the call to
5473 push_template_decl_real, but we want the original value. */
5474 tree name = DECL_NAME (decl);
5476 decl = push_template_decl_real (decl, is_friend);
5477 if (decl == error_mark_node)
5478 return error_mark_node;
5480 /* If the current binding level is the binding level for the
5481 template parameters (see the comment in
5482 begin_template_parm_list) and the enclosing level is a class
5483 scope, and we're not looking at a friend, push the
5484 declaration of the member class into the class scope. In the
5485 friend case, push_template_decl will already have put the
5486 friend into global scope, if appropriate. */
5487 if (TREE_CODE (type) != ENUMERAL_TYPE
5488 && !is_friend && b->kind == sk_template_parms
5489 && b->level_chain->kind == sk_class)
5491 finish_member_declaration (CLASSTYPE_TI_TEMPLATE (type));
5493 if (!COMPLETE_TYPE_P (current_class_type))
5495 maybe_add_class_template_decl_list (current_class_type,
5496 type, /*friend_p=*/0);
5497 /* Put this UTD in the table of UTDs for the class. */
5498 if (CLASSTYPE_NESTED_UTDS (current_class_type) == NULL)
5499 CLASSTYPE_NESTED_UTDS (current_class_type) =
5500 binding_table_new (SCOPE_DEFAULT_HT_SIZE);
5502 binding_table_insert
5503 (CLASSTYPE_NESTED_UTDS (current_class_type), name, type);
5509 return decl;
5512 /* Push a tag name NAME for struct/class/union/enum type TYPE. In case
5513 that the NAME is a class template, the tag is processed but not pushed.
5515 The pushed scope depend on the SCOPE parameter:
5516 - When SCOPE is TS_CURRENT, put it into the inner-most non-sk_cleanup
5517 scope.
5518 - When SCOPE is TS_GLOBAL, put it in the inner-most non-class and
5519 non-template-parameter scope. This case is needed for forward
5520 declarations.
5521 - When SCOPE is TS_WITHIN_ENCLOSING_NON_CLASS, this is similar to
5522 TS_GLOBAL case except that names within template-parameter scopes
5523 are not pushed at all.
5525 Returns TYPE upon success and ERROR_MARK_NODE otherwise. */
5527 static tree
5528 pushtag_1 (tree name, tree type, tag_scope scope)
5530 cp_binding_level *b;
5531 tree decl;
5533 b = current_binding_level;
5534 while (/* Cleanup scopes are not scopes from the point of view of
5535 the language. */
5536 b->kind == sk_cleanup
5537 /* Neither are function parameter scopes. */
5538 || b->kind == sk_function_parms
5539 /* Neither are the scopes used to hold template parameters
5540 for an explicit specialization. For an ordinary template
5541 declaration, these scopes are not scopes from the point of
5542 view of the language. */
5543 || (b->kind == sk_template_parms
5544 && (b->explicit_spec_p || scope == ts_global))
5545 /* Pushing into a class is ok for lambdas or when we want current */
5546 || (b->kind == sk_class
5547 && scope != ts_lambda
5548 && (scope != ts_current
5549 /* We may be defining a new type in the initializer
5550 of a static member variable. We allow this when
5551 not pedantic, and it is particularly useful for
5552 type punning via an anonymous union. */
5553 || COMPLETE_TYPE_P (b->this_entity))))
5554 b = b->level_chain;
5556 gcc_assert (identifier_p (name));
5558 /* Do C++ gratuitous typedefing. */
5559 if (identifier_type_value_1 (name) != type)
5561 tree tdef;
5562 int in_class = 0;
5563 tree context = TYPE_CONTEXT (type);
5565 if (! context)
5567 tree cs = current_scope ();
5569 if (scope == ts_current
5570 || scope == ts_lambda
5571 || (cs && TREE_CODE (cs) == FUNCTION_DECL))
5572 context = cs;
5573 else if (cs && TYPE_P (cs))
5574 /* When declaring a friend class of a local class, we want
5575 to inject the newly named class into the scope
5576 containing the local class, not the namespace
5577 scope. */
5578 context = decl_function_context (get_type_decl (cs));
5580 if (!context)
5581 context = current_namespace;
5583 if (b->kind == sk_class
5584 || (b->kind == sk_template_parms
5585 && b->level_chain->kind == sk_class))
5586 in_class = 1;
5588 tdef = create_implicit_typedef (name, type);
5589 DECL_CONTEXT (tdef) = FROB_CONTEXT (context);
5590 if (scope == ts_within_enclosing_non_class)
5592 /* This is a friend. Make this TYPE_DECL node hidden from
5593 ordinary name lookup. Its corresponding TEMPLATE_DECL
5594 will be marked in push_template_decl_real. */
5595 retrofit_lang_decl (tdef);
5596 DECL_ANTICIPATED (tdef) = 1;
5597 DECL_FRIEND_P (tdef) = 1;
5600 decl = maybe_process_template_type_declaration
5601 (type, scope == ts_within_enclosing_non_class, b);
5602 if (decl == error_mark_node)
5603 return decl;
5605 if (b->kind == sk_class)
5607 if (!TYPE_BEING_DEFINED (current_class_type)
5608 && scope != ts_lambda)
5609 return error_mark_node;
5611 if (!PROCESSING_REAL_TEMPLATE_DECL_P ())
5612 /* Put this TYPE_DECL on the TYPE_FIELDS list for the
5613 class. But if it's a member template class, we want
5614 the TEMPLATE_DECL, not the TYPE_DECL, so this is done
5615 later. */
5616 finish_member_declaration (decl);
5617 else
5618 pushdecl_class_level (decl);
5620 else if (b->kind != sk_template_parms)
5622 decl = pushdecl_with_scope_1 (decl, b, /*is_friend=*/false);
5623 if (decl == error_mark_node)
5624 return decl;
5626 if (DECL_CONTEXT (decl) == std_node
5627 && init_list_identifier == DECL_NAME (TYPE_NAME (type))
5628 && !CLASSTYPE_TEMPLATE_INFO (type))
5630 error ("declaration of std::initializer_list does not match "
5631 "#include <initializer_list>, isn't a template");
5632 return error_mark_node;
5636 if (! in_class)
5637 set_identifier_type_value_with_scope (name, tdef, b);
5639 TYPE_CONTEXT (type) = DECL_CONTEXT (decl);
5641 /* If this is a local class, keep track of it. We need this
5642 information for name-mangling, and so that it is possible to
5643 find all function definitions in a translation unit in a
5644 convenient way. (It's otherwise tricky to find a member
5645 function definition it's only pointed to from within a local
5646 class.) */
5647 if (TYPE_FUNCTION_SCOPE_P (type))
5649 if (processing_template_decl)
5651 /* Push a DECL_EXPR so we call pushtag at the right time in
5652 template instantiation rather than in some nested context. */
5653 add_decl_expr (decl);
5655 else
5656 vec_safe_push (local_classes, type);
5660 if (b->kind == sk_class
5661 && !COMPLETE_TYPE_P (current_class_type))
5663 maybe_add_class_template_decl_list (current_class_type,
5664 type, /*friend_p=*/0);
5666 if (CLASSTYPE_NESTED_UTDS (current_class_type) == NULL)
5667 CLASSTYPE_NESTED_UTDS (current_class_type)
5668 = binding_table_new (SCOPE_DEFAULT_HT_SIZE);
5670 binding_table_insert
5671 (CLASSTYPE_NESTED_UTDS (current_class_type), name, type);
5674 decl = TYPE_NAME (type);
5675 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
5677 /* Set type visibility now if this is a forward declaration. */
5678 TREE_PUBLIC (decl) = 1;
5679 determine_visibility (decl);
5681 return type;
5684 /* Wrapper for pushtag_1. */
5686 tree
5687 pushtag (tree name, tree type, tag_scope scope)
5689 tree ret;
5690 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
5691 ret = pushtag_1 (name, type, scope);
5692 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
5693 return ret;
5697 /* Subroutines for reverting temporarily to top-level for instantiation
5698 of templates and such. We actually need to clear out the class- and
5699 local-value slots of all identifiers, so that only the global values
5700 are at all visible. Simply setting current_binding_level to the global
5701 scope isn't enough, because more binding levels may be pushed. */
5702 struct saved_scope *scope_chain;
5704 /* Return true if ID has not already been marked. */
5706 static inline bool
5707 store_binding_p (tree id)
5709 if (!id || !IDENTIFIER_BINDING (id))
5710 return false;
5712 if (IDENTIFIER_MARKED (id))
5713 return false;
5715 return true;
5718 /* Add an appropriate binding to *OLD_BINDINGS which needs to already
5719 have enough space reserved. */
5721 static void
5722 store_binding (tree id, vec<cxx_saved_binding, va_gc> **old_bindings)
5724 cxx_saved_binding saved;
5726 gcc_checking_assert (store_binding_p (id));
5728 IDENTIFIER_MARKED (id) = 1;
5730 saved.identifier = id;
5731 saved.binding = IDENTIFIER_BINDING (id);
5732 saved.real_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
5733 (*old_bindings)->quick_push (saved);
5734 IDENTIFIER_BINDING (id) = NULL;
5737 static void
5738 store_bindings (tree names, vec<cxx_saved_binding, va_gc> **old_bindings)
5740 static vec<tree> bindings_need_stored;
5741 tree t, id;
5742 size_t i;
5744 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
5745 for (t = names; t; t = TREE_CHAIN (t))
5747 if (TREE_CODE (t) == TREE_LIST)
5748 id = TREE_PURPOSE (t);
5749 else
5750 id = DECL_NAME (t);
5752 if (store_binding_p (id))
5753 bindings_need_stored.safe_push (id);
5755 if (!bindings_need_stored.is_empty ())
5757 vec_safe_reserve_exact (*old_bindings, bindings_need_stored.length ());
5758 for (i = 0; bindings_need_stored.iterate (i, &id); ++i)
5760 /* We can apparently have duplicates in NAMES. */
5761 if (store_binding_p (id))
5762 store_binding (id, old_bindings);
5764 bindings_need_stored.truncate (0);
5766 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
5769 /* Like store_bindings, but NAMES is a vector of cp_class_binding
5770 objects, rather than a TREE_LIST. */
5772 static void
5773 store_class_bindings (vec<cp_class_binding, va_gc> *names,
5774 vec<cxx_saved_binding, va_gc> **old_bindings)
5776 static vec<tree> bindings_need_stored;
5777 size_t i;
5778 cp_class_binding *cb;
5780 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
5781 for (i = 0; vec_safe_iterate (names, i, &cb); ++i)
5782 if (store_binding_p (cb->identifier))
5783 bindings_need_stored.safe_push (cb->identifier);
5784 if (!bindings_need_stored.is_empty ())
5786 tree id;
5787 vec_safe_reserve_exact (*old_bindings, bindings_need_stored.length ());
5788 for (i = 0; bindings_need_stored.iterate (i, &id); ++i)
5789 store_binding (id, old_bindings);
5790 bindings_need_stored.truncate (0);
5792 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
5795 /* A chain of saved_scope structures awaiting reuse. */
5797 static GTY((deletable)) struct saved_scope *free_saved_scope;
5799 static void
5800 do_push_to_top_level (void)
5802 struct saved_scope *s;
5803 cp_binding_level *b;
5804 cxx_saved_binding *sb;
5805 size_t i;
5806 bool need_pop;
5808 /* Reuse or create a new structure for this saved scope. */
5809 if (free_saved_scope != NULL)
5811 s = free_saved_scope;
5812 free_saved_scope = s->prev;
5814 vec<cxx_saved_binding, va_gc> *old_bindings = s->old_bindings;
5815 memset (s, 0, sizeof (*s));
5816 /* Also reuse the structure's old_bindings vector. */
5817 vec_safe_truncate (old_bindings, 0);
5818 s->old_bindings = old_bindings;
5820 else
5821 s = ggc_cleared_alloc<saved_scope> ();
5823 b = scope_chain ? current_binding_level : 0;
5825 /* If we're in the middle of some function, save our state. */
5826 if (cfun)
5828 need_pop = true;
5829 push_function_context ();
5831 else
5832 need_pop = false;
5834 if (scope_chain && previous_class_level)
5835 store_class_bindings (previous_class_level->class_shadowed,
5836 &s->old_bindings);
5838 /* Have to include the global scope, because class-scope decls
5839 aren't listed anywhere useful. */
5840 for (; b; b = b->level_chain)
5842 tree t;
5844 /* Template IDs are inserted into the global level. If they were
5845 inserted into namespace level, finish_file wouldn't find them
5846 when doing pending instantiations. Therefore, don't stop at
5847 namespace level, but continue until :: . */
5848 if (global_scope_p (b))
5849 break;
5851 store_bindings (b->names, &s->old_bindings);
5852 /* We also need to check class_shadowed to save class-level type
5853 bindings, since pushclass doesn't fill in b->names. */
5854 if (b->kind == sk_class)
5855 store_class_bindings (b->class_shadowed, &s->old_bindings);
5857 /* Unwind type-value slots back to top level. */
5858 for (t = b->type_shadowed; t; t = TREE_CHAIN (t))
5859 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (t), TREE_VALUE (t));
5862 FOR_EACH_VEC_SAFE_ELT (s->old_bindings, i, sb)
5863 IDENTIFIER_MARKED (sb->identifier) = 0;
5865 s->prev = scope_chain;
5866 s->bindings = b;
5867 s->need_pop_function_context = need_pop;
5868 s->function_decl = current_function_decl;
5869 s->unevaluated_operand = cp_unevaluated_operand;
5870 s->inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
5871 s->x_stmt_tree.stmts_are_full_exprs_p = true;
5873 scope_chain = s;
5874 current_function_decl = NULL_TREE;
5875 vec_alloc (current_lang_base, 10);
5876 current_lang_name = lang_name_cplusplus;
5877 current_namespace = global_namespace;
5878 push_class_stack ();
5879 cp_unevaluated_operand = 0;
5880 c_inhibit_evaluation_warnings = 0;
5883 static void
5884 do_pop_from_top_level (void)
5886 struct saved_scope *s = scope_chain;
5887 cxx_saved_binding *saved;
5888 size_t i;
5890 /* Clear out class-level bindings cache. */
5891 if (previous_class_level)
5892 invalidate_class_lookup_cache ();
5893 pop_class_stack ();
5895 current_lang_base = 0;
5897 scope_chain = s->prev;
5898 FOR_EACH_VEC_SAFE_ELT (s->old_bindings, i, saved)
5900 tree id = saved->identifier;
5902 IDENTIFIER_BINDING (id) = saved->binding;
5903 SET_IDENTIFIER_TYPE_VALUE (id, saved->real_type_value);
5906 /* If we were in the middle of compiling a function, restore our
5907 state. */
5908 if (s->need_pop_function_context)
5909 pop_function_context ();
5910 current_function_decl = s->function_decl;
5911 cp_unevaluated_operand = s->unevaluated_operand;
5912 c_inhibit_evaluation_warnings = s->inhibit_evaluation_warnings;
5914 /* Make this saved_scope structure available for reuse by
5915 push_to_top_level. */
5916 s->prev = free_saved_scope;
5917 free_saved_scope = s;
5920 /* Push into the scope of the namespace NS, even if it is deeply
5921 nested within another namespace. */
5923 static void
5924 do_push_nested_namespace (tree ns)
5926 if (ns == global_namespace)
5927 do_push_to_top_level ();
5928 else
5930 do_push_nested_namespace (CP_DECL_CONTEXT (ns));
5931 gcc_checking_assert
5932 (get_namespace_binding (current_namespace,
5933 DECL_NAME (ns) ? DECL_NAME (ns)
5934 : anon_identifier) == ns);
5935 resume_scope (NAMESPACE_LEVEL (ns));
5936 current_namespace = ns;
5940 /* Pop back from the scope of the namespace NS, which was previously
5941 entered with push_nested_namespace. */
5943 static void
5944 do_pop_nested_namespace (tree ns)
5946 while (ns != global_namespace)
5948 ns = CP_DECL_CONTEXT (ns);
5949 current_namespace = ns;
5950 leave_scope ();
5953 do_pop_from_top_level ();
5956 /* Add TARGET to USINGS, if it does not already exist there.
5957 We used to build the complete graph of usings at this point, from
5958 the POV of the source namespaces. Now we build that as we perform
5959 the unqualified search. */
5961 static void
5962 add_using_namespace (vec<tree, va_gc> *&usings, tree target)
5964 if (usings)
5965 for (unsigned ix = usings->length (); ix--;)
5966 if ((*usings)[ix] == target)
5967 return;
5969 vec_safe_push (usings, target);
5972 /* Tell the debug system of a using directive. */
5974 static void
5975 emit_debug_info_using_namespace (tree from, tree target)
5977 /* Emit debugging info. */
5978 tree context = from != global_namespace ? from : NULL_TREE;
5979 debug_hooks->imported_module_or_decl (target, NULL_TREE, context, false);
5982 /* Process a namespace-scope using directive. */
5984 void
5985 finish_namespace_using_directive (tree target, tree attribs)
5987 gcc_checking_assert (namespace_bindings_p ());
5988 if (target == error_mark_node)
5989 return;
5991 add_using_namespace (DECL_NAMESPACE_USING (current_namespace),
5992 ORIGINAL_NAMESPACE (target));
5993 emit_debug_info_using_namespace (current_namespace,
5994 ORIGINAL_NAMESPACE (target));
5996 if (attribs == error_mark_node)
5997 return;
5999 for (tree a = attribs; a; a = TREE_CHAIN (a))
6001 tree name = get_attribute_name (a);
6002 if (is_attribute_p ("strong", name))
6004 warning (0, "strong using directive no longer supported");
6005 if (CP_DECL_CONTEXT (target) == current_namespace)
6006 inform (DECL_SOURCE_LOCATION (target),
6007 "you may use an inline namespace instead");
6009 else
6010 warning (OPT_Wattributes, "%qD attribute directive ignored", name);
6014 /* Process a function-scope using-directive. */
6016 void
6017 finish_local_using_directive (tree target, tree attribs)
6019 gcc_checking_assert (local_bindings_p ());
6020 if (target == error_mark_node)
6021 return;
6023 if (attribs)
6024 warning (OPT_Wattributes, "attributes ignored on local using directive");
6026 add_stmt (build_stmt (input_location, USING_STMT, target));
6028 add_using_namespace (current_binding_level->using_directives,
6029 ORIGINAL_NAMESPACE (target));
6032 /* Pushes X into the global namespace. */
6034 tree
6035 pushdecl_top_level (tree x, bool is_friend)
6037 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6038 do_push_to_top_level ();
6039 x = pushdecl_namespace_level (x, is_friend);
6040 do_pop_from_top_level ();
6041 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6042 return x;
6045 /* Pushes X into the global namespace and calls cp_finish_decl to
6046 register the variable, initializing it with INIT. */
6048 tree
6049 pushdecl_top_level_and_finish (tree x, tree init)
6051 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6052 do_push_to_top_level ();
6053 x = pushdecl_namespace_level (x, false);
6054 cp_finish_decl (x, init, false, NULL_TREE, 0);
6055 do_pop_from_top_level ();
6056 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6057 return x;
6060 /* Enter the namespaces from current_namerspace to NS. */
6062 static int
6063 push_inline_namespaces (tree ns)
6065 int count = 0;
6066 if (ns != current_namespace)
6068 gcc_assert (ns != global_namespace);
6069 count += push_inline_namespaces (CP_DECL_CONTEXT (ns));
6070 resume_scope (NAMESPACE_LEVEL (ns));
6071 current_namespace = ns;
6072 count++;
6074 return count;
6077 /* Push into the scope of the NAME namespace. If NAME is NULL_TREE,
6078 then we enter an anonymous namespace. If MAKE_INLINE is true, then
6079 we create an inline namespace (it is up to the caller to check upon
6080 redefinition). Return the number of namespaces entered. */
6083 push_namespace (tree name, bool make_inline)
6085 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6086 int count = 0;
6088 /* We should not get here if the global_namespace is not yet constructed
6089 nor if NAME designates the global namespace: The global scope is
6090 constructed elsewhere. */
6091 gcc_assert (global_namespace != NULL && name != global_identifier);
6093 if (!name)
6094 name = anon_identifier;
6096 tree ns = NULL_TREE;
6098 name_lookup lookup (name, 0);
6099 if (!lookup.search_qualified (current_namespace, /*usings=*/false))
6101 else if (TREE_CODE (lookup.value) != NAMESPACE_DECL)
6103 else if (tree dna = DECL_NAMESPACE_ALIAS (lookup.value))
6105 /* A namespace alias is not allowed here, but if the alias
6106 is for a namespace also inside the current scope,
6107 accept it with a diagnostic. That's better than dying
6108 horribly. */
6109 if (is_nested_namespace (current_namespace, CP_DECL_CONTEXT (dna)))
6111 error ("namespace alias %qD not allowed here, "
6112 "assuming %qD", lookup.value, dna);
6113 ns = dna;
6116 else
6117 ns = lookup.value;
6120 bool new_ns = false;
6121 if (ns)
6122 /* DR2061. NS might be a member of an inline namespace. We
6123 need to push into those namespaces. */
6124 count += push_inline_namespaces (CP_DECL_CONTEXT (ns));
6125 else
6127 ns = build_lang_decl (NAMESPACE_DECL, name, void_type_node);
6128 SCOPE_DEPTH (ns) = SCOPE_DEPTH (current_namespace) + 1;
6129 if (!SCOPE_DEPTH (ns))
6130 /* We only allow depth 255. */
6131 sorry ("cannot nest more than %d namespaces",
6132 SCOPE_DEPTH (current_namespace));
6133 DECL_CONTEXT (ns) = FROB_CONTEXT (current_namespace);
6134 new_ns = true;
6136 if (pushdecl (ns) == error_mark_node)
6137 ns = NULL_TREE;
6138 else
6140 if (name == anon_identifier)
6142 /* Clear DECL_NAME for the benefit of debugging back ends. */
6143 SET_DECL_ASSEMBLER_NAME (ns, name);
6144 DECL_NAME (ns) = NULL_TREE;
6146 if (!make_inline)
6147 add_using_namespace (DECL_NAMESPACE_USING (current_namespace),
6148 ns);
6150 else if (TREE_PUBLIC (current_namespace))
6151 TREE_PUBLIC (ns) = 1;
6153 if (name == anon_identifier || make_inline)
6154 emit_debug_info_using_namespace (current_namespace, ns);
6156 if (make_inline)
6158 DECL_NAMESPACE_INLINE_P (ns) = true;
6159 vec_safe_push (DECL_NAMESPACE_INLINEES (current_namespace), ns);
6164 if (ns)
6166 if (make_inline && !DECL_NAMESPACE_INLINE_P (ns))
6168 error ("inline namespace must be specified at initial definition");
6169 inform (DECL_SOURCE_LOCATION (ns), "%qD defined here", ns);
6171 if (new_ns)
6172 begin_scope (sk_namespace, ns);
6173 else
6174 resume_scope (NAMESPACE_LEVEL (ns));
6175 current_namespace = ns;
6176 count++;
6179 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6180 return count;
6183 /* Pop from the scope of the current namespace. */
6185 void
6186 pop_namespace (void)
6188 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6190 gcc_assert (current_namespace != global_namespace);
6191 current_namespace = CP_DECL_CONTEXT (current_namespace);
6192 /* The binding level is not popped, as it might be re-opened later. */
6193 leave_scope ();
6195 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6198 /* External entry points for do_{push_to/pop_from}_top_level. */
6200 void
6201 push_to_top_level (void)
6203 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6204 do_push_to_top_level ();
6205 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6208 void
6209 pop_from_top_level (void)
6211 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6212 do_pop_from_top_level ();
6213 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6216 /* External entry points for do_{push,pop}_nested_namespace. */
6218 void
6219 push_nested_namespace (tree ns)
6221 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6222 do_push_nested_namespace (ns);
6223 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6226 void
6227 pop_nested_namespace (tree ns)
6229 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6230 gcc_assert (current_namespace == ns);
6231 do_pop_nested_namespace (ns);
6232 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6235 /* Pop off extraneous binding levels left over due to syntax errors.
6236 We don't pop past namespaces, as they might be valid. */
6238 void
6239 pop_everything (void)
6241 if (ENABLE_SCOPE_CHECKING)
6242 verbatim ("XXX entering pop_everything ()\n");
6243 while (!namespace_bindings_p ())
6245 if (current_binding_level->kind == sk_class)
6246 pop_nested_class ();
6247 else
6248 poplevel (0, 0, 0);
6250 if (ENABLE_SCOPE_CHECKING)
6251 verbatim ("XXX leaving pop_everything ()\n");
6254 /* Emit debugging information for using declarations and directives.
6255 If input tree is overloaded fn then emit debug info for all
6256 candidates. */
6258 void
6259 cp_emit_debug_info_for_using (tree t, tree context)
6261 /* Don't try to emit any debug information if we have errors. */
6262 if (seen_error ())
6263 return;
6265 /* Ignore this FUNCTION_DECL if it refers to a builtin declaration
6266 of a builtin function. */
6267 if (TREE_CODE (t) == FUNCTION_DECL
6268 && DECL_EXTERNAL (t)
6269 && DECL_BUILT_IN (t))
6270 return;
6272 /* Do not supply context to imported_module_or_decl, if
6273 it is a global namespace. */
6274 if (context == global_namespace)
6275 context = NULL_TREE;
6277 t = MAYBE_BASELINK_FUNCTIONS (t);
6279 /* FIXME: Handle TEMPLATE_DECLs. */
6280 for (lkp_iterator iter (t); iter; ++iter)
6282 tree fn = *iter;
6283 if (TREE_CODE (fn) != TEMPLATE_DECL)
6285 if (building_stmt_list_p ())
6286 add_stmt (build_stmt (input_location, USING_STMT, fn));
6287 else
6288 debug_hooks->imported_module_or_decl (fn,
6289 NULL_TREE, context, false);
6294 #include "gt-cp-name-lookup.h"