[C++ PATCH] Move mangling alias out of ::
[official-gcc.git] / gcc / cp / name-lookup.c
blob0111d8de899c646a922dee0e8c247c4259af094e
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 an overload suitable for recording an artificial TYPE_DECL
42 and another decl. We use this machanism to implement the struct
43 stat hack within a namespace. It'd be nice to use it everywhere. */
45 #define STAT_HACK_P(N) ((N) && TREE_CODE (N) == OVERLOAD && OVL_LOOKUP_P (N))
46 #define STAT_TYPE(N) TREE_TYPE (N)
47 #define STAT_DECL(N) OVL_FUNCTION (N)
48 #define MAYBE_STAT_DECL(N) (STAT_HACK_P (N) ? STAT_DECL (N) : N)
49 #define MAYBE_STAT_TYPE(N) (STAT_HACK_P (N) ? STAT_TYPE (N) : NULL_TREE)
51 /* Create a STAT_HACK node with DECL as the value binding and TYPE as
52 the type binding. */
54 static tree
55 stat_hack (tree decl = NULL_TREE, tree type = NULL_TREE)
57 tree result = make_node (OVERLOAD);
59 /* Mark this as a lookup, so we can tell this is a stat hack. */
60 OVL_LOOKUP_P (result) = true;
61 STAT_DECL (result) = decl;
62 STAT_TYPE (result) = type;
63 return result;
66 /* Create a local binding level for NAME. */
68 static cxx_binding *
69 create_local_binding (cp_binding_level *level, tree name)
71 cxx_binding *binding = cxx_binding_make (NULL, NULL);
73 INHERITED_VALUE_BINDING_P (binding) = false;
74 LOCAL_BINDING_P (binding) = true;
75 binding->scope = level;
76 binding->previous = IDENTIFIER_BINDING (name);
78 IDENTIFIER_BINDING (name) = binding;
80 return binding;
83 /* Find the binding for NAME in namespace NS. If CREATE_P is true,
84 make an empty binding if there wasn't one. */
86 static tree *
87 find_namespace_slot (tree ns, tree name, bool create_p = false)
89 tree *slot;
91 if (create_p)
93 bool existed;
94 slot = &DECL_NAMESPACE_BINDINGS (ns)->get_or_insert (name, &existed);
95 if (!existed)
96 *slot = NULL_TREE;
98 else
99 slot = DECL_NAMESPACE_BINDINGS (ns)->get (name);
100 return slot;
103 static tree
104 find_namespace_value (tree ns, tree name)
106 tree *b = find_namespace_slot (ns, name);
108 return b ? MAYBE_STAT_DECL (*b) : NULL_TREE;
111 /* Add DECL to the list of things declared in B. */
113 static void
114 add_decl_to_level (cp_binding_level *b, tree decl)
116 gcc_assert (b->kind != sk_class);
118 /* Make sure we don't create a circular list. xref_tag can end
119 up pushing the same artificial decl more than once. We
120 should have already detected that in update_binding. */
121 gcc_assert (b->names != decl);
123 /* We build up the list in reverse order, and reverse it later if
124 necessary. */
125 TREE_CHAIN (decl) = b->names;
126 b->names = decl;
128 /* If appropriate, add decl to separate list of statics. We
129 include extern variables because they might turn out to be
130 static later. It's OK for this list to contain a few false
131 positives. */
132 if (b->kind == sk_namespace
133 && ((VAR_P (decl)
134 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
135 || (TREE_CODE (decl) == FUNCTION_DECL
136 && (!TREE_PUBLIC (decl)
137 || decl_anon_ns_mem_p (decl)
138 || DECL_DECLARED_INLINE_P (decl)))))
139 vec_safe_push (static_decls, decl);
142 /* Find the binding for NAME in the local binding level B. */
144 static cxx_binding *
145 find_local_binding (cp_binding_level *b, tree name)
147 if (cxx_binding *binding = IDENTIFIER_BINDING (name))
148 for (;; b = b->level_chain)
150 if (binding->scope == b
151 && !(VAR_P (binding->value)
152 && DECL_DEAD_FOR_LOCAL (binding->value)))
153 return binding;
155 /* Cleanup contours are transparent to the language. */
156 if (b->kind != sk_cleanup)
157 break;
159 return NULL;
162 struct name_lookup
164 public:
165 typedef std::pair<tree, tree> using_pair;
166 typedef vec<using_pair, va_heap, vl_embed> using_queue;
168 public:
169 tree name; /* The identifier being looked for. */
170 tree value; /* A (possibly ambiguous) set of things found. */
171 tree type; /* A type that has been found. */
172 int flags; /* Lookup flags. */
173 bool deduping; /* Full deduping is needed because using declarations
174 are in play. */
175 vec<tree, va_heap, vl_embed> *scopes;
176 name_lookup *previous; /* Previously active lookup. */
178 protected:
179 /* Marked scope stack for outermost name lookup. */
180 static vec<tree, va_heap, vl_embed> *shared_scopes;
181 /* Currently active lookup. */
182 static name_lookup *active;
184 public:
185 name_lookup (tree n, int f = 0)
186 : name (n), value (NULL_TREE), type (NULL_TREE), flags (f),
187 deduping (false), scopes (NULL), previous (NULL)
189 preserve_state ();
191 ~name_lookup ()
193 restore_state ();
196 private: /* Uncopyable, unmovable, unassignable. I am a rock. */
197 name_lookup (const name_lookup &);
198 name_lookup &operator= (const name_lookup &);
200 protected:
201 static bool seen_p (tree scope)
203 return LOOKUP_SEEN_P (scope);
205 static bool found_p (tree scope)
207 return LOOKUP_FOUND_P (scope);
210 void mark_seen (tree scope); /* Mark and add to scope vector. */
211 static void mark_found (tree scope)
213 gcc_checking_assert (seen_p (scope));
214 LOOKUP_FOUND_P (scope) = true;
216 bool see_and_mark (tree scope)
218 bool ret = seen_p (scope);
219 if (!ret)
220 mark_seen (scope);
221 return ret;
223 bool find_and_mark (tree scope);
225 private:
226 void preserve_state ();
227 void restore_state ();
229 private:
230 static tree ambiguous (tree thing, tree current);
231 void add_overload (tree fns);
232 void add_value (tree new_val);
233 void add_type (tree new_type);
234 bool process_binding (tree val_bind, tree type_bind);
236 /* Look in only namespace. */
237 bool search_namespace_only (tree scope);
238 /* Look in namespace and its (recursive) inlines. Ignore using
239 directives. Return true if something found (inc dups). */
240 bool search_namespace (tree scope);
241 /* Look in the using directives of namespace + inlines using
242 qualified lookup rules. */
243 bool search_usings (tree scope);
245 private:
246 using_queue *queue_namespace (using_queue *queue, int depth, tree scope);
247 using_queue *do_queue_usings (using_queue *queue, int depth,
248 vec<tree, va_gc> *usings);
249 using_queue *queue_usings (using_queue *queue, int depth,
250 vec<tree, va_gc> *usings)
252 if (usings)
253 queue = do_queue_usings (queue, depth, usings);
254 return queue;
257 private:
258 void add_fns (tree);
260 void adl_expr (tree);
261 void adl_type (tree);
262 void adl_template_arg (tree);
263 void adl_class (tree);
264 void adl_bases (tree);
265 void adl_class_only (tree);
266 void adl_namespace (tree);
267 void adl_namespace_only (tree);
269 public:
270 /* Search namespace + inlines + maybe usings as qualified lookup. */
271 bool search_qualified (tree scope, bool usings = true);
273 /* Search namespace + inlines + usings as unqualified lookup. */
274 bool search_unqualified (tree scope, cp_binding_level *);
276 /* ADL lookup of ARGS. */
277 tree search_adl (tree fns, vec<tree, va_gc> *args);
280 /* Scope stack shared by all outermost lookups. This avoids us
281 allocating and freeing on every single lookup. */
282 vec<tree, va_heap, vl_embed> *name_lookup::shared_scopes;
284 /* Currently active lookup. */
285 name_lookup *name_lookup::active;
287 /* Name lookup is recursive, becase ADL can cause template
288 instatiation. This is of course a rare event, so we optimize for
289 it not happening. When we discover an active name-lookup, which
290 must be an ADL lookup, we need to unmark the marked scopes and also
291 unmark the lookup we might have been accumulating. */
293 void
294 name_lookup::preserve_state ()
296 previous = active;
297 if (previous)
299 unsigned length = vec_safe_length (previous->scopes);
300 vec_safe_reserve (previous->scopes, length * 2);
301 for (unsigned ix = length; ix--;)
303 tree decl = (*previous->scopes)[ix];
305 gcc_checking_assert (LOOKUP_SEEN_P (decl));
306 LOOKUP_SEEN_P (decl) = false;
308 /* Preserve the FOUND_P state on the interrupted lookup's
309 stack. */
310 if (LOOKUP_FOUND_P (decl))
312 LOOKUP_FOUND_P (decl) = false;
313 previous->scopes->quick_push (decl);
317 /* Unmark the outer partial lookup. */
318 if (previous->deduping)
319 lookup_mark (previous->value, false);
321 else
322 scopes = shared_scopes;
323 active = this;
326 /* Restore the marking state of a lookup we interrupted. */
328 void
329 name_lookup::restore_state ()
331 if (deduping)
332 lookup_mark (value, false);
334 /* Unmark and empty this lookup's scope stack. */
335 for (unsigned ix = vec_safe_length (scopes); ix--;)
337 tree decl = scopes->pop ();
338 gcc_checking_assert (LOOKUP_SEEN_P (decl));
339 LOOKUP_SEEN_P (decl) = false;
340 LOOKUP_FOUND_P (decl) = false;
343 active = previous;
344 if (previous)
346 free (scopes);
348 unsigned length = vec_safe_length (previous->scopes);
349 for (unsigned ix = 0; ix != length; ix++)
351 tree decl = (*previous->scopes)[ix];
352 if (LOOKUP_SEEN_P (decl))
354 /* The remainder of the scope stack must be recording
355 FOUND_P decls, which we want to pop off. */
358 tree decl = previous->scopes->pop ();
359 gcc_checking_assert (LOOKUP_SEEN_P (decl)
360 && !LOOKUP_FOUND_P (decl));
361 LOOKUP_FOUND_P (decl) = true;
363 while (++ix != length);
364 break;
367 gcc_checking_assert (!LOOKUP_FOUND_P (decl));
368 LOOKUP_SEEN_P (decl) = true;
371 /* Remark the outer partial lookup. */
372 if (previous->deduping)
373 lookup_mark (previous->value, true);
375 else
376 shared_scopes = scopes;
379 void
380 name_lookup::mark_seen (tree scope)
382 gcc_checking_assert (!seen_p (scope));
383 LOOKUP_SEEN_P (scope) = true;
384 vec_safe_push (scopes, scope);
387 bool
388 name_lookup::find_and_mark (tree scope)
390 bool result = LOOKUP_FOUND_P (scope);
391 if (!result)
393 LOOKUP_FOUND_P (scope) = true;
394 if (!LOOKUP_SEEN_P (scope))
395 vec_safe_push (scopes, scope);
398 return result;
401 /* THING and CURRENT are ambiguous, concatenate them. */
403 tree
404 name_lookup::ambiguous (tree thing, tree current)
406 if (TREE_CODE (current) != TREE_LIST)
408 current = build_tree_list (NULL_TREE, current);
409 TREE_TYPE (current) = error_mark_node;
411 current = tree_cons (NULL_TREE, thing, current);
412 TREE_TYPE (current) = error_mark_node;
414 return current;
417 /* FNS is a new overload set to add to the exising set. */
419 void
420 name_lookup::add_overload (tree fns)
422 if (!deduping && TREE_CODE (fns) == OVERLOAD)
424 tree probe = fns;
425 if (flags & LOOKUP_HIDDEN)
426 probe = ovl_skip_hidden (probe);
427 if (probe && TREE_CODE (probe) == OVERLOAD && OVL_USING_P (probe))
429 /* We're about to add something found by a using
430 declaration, so need to engage deduping mode. */
431 lookup_mark (value, true);
432 deduping = true;
436 value = lookup_maybe_add (fns, value, deduping);
439 /* Add a NEW_VAL, a found value binding into the current value binding. */
441 void
442 name_lookup::add_value (tree new_val)
444 if (OVL_P (new_val) && (!value || OVL_P (value)))
445 add_overload (new_val);
446 else if (!value)
447 value = new_val;
448 else if (value == new_val)
450 else if ((TREE_CODE (value) == TYPE_DECL
451 && TREE_CODE (new_val) == TYPE_DECL
452 && same_type_p (TREE_TYPE (value), TREE_TYPE (new_val))))
453 /* Typedefs to the same type. */;
454 else if (TREE_CODE (value) == NAMESPACE_DECL
455 && TREE_CODE (new_val) == NAMESPACE_DECL
456 && ORIGINAL_NAMESPACE (value) == ORIGINAL_NAMESPACE (new_val))
457 /* Namespace (possibly aliased) to the same namespace. Locate
458 the namespace*/
459 value = ORIGINAL_NAMESPACE (value);
460 else
462 if (deduping)
464 /* Disengage deduping mode. */
465 lookup_mark (value, false);
466 deduping = false;
468 value = ambiguous (new_val, value);
472 /* Add a NEW_TYPE, a found type binding into the current type binding. */
474 void
475 name_lookup::add_type (tree new_type)
477 if (!type)
478 type = new_type;
479 else if (TREE_CODE (type) == TREE_LIST
480 || !same_type_p (TREE_TYPE (type), TREE_TYPE (new_type)))
481 type = ambiguous (new_type, type);
484 /* Process a found binding containing NEW_VAL and NEW_TYPE. Returns
485 true if we actually found something noteworthy. */
487 bool
488 name_lookup::process_binding (tree new_val, tree new_type)
490 /* Did we really see a type? */
491 if (new_type
492 && (LOOKUP_NAMESPACES_ONLY (flags)
493 || (!(flags & LOOKUP_HIDDEN)
494 && DECL_LANG_SPECIFIC (new_type)
495 && DECL_ANTICIPATED (new_type))))
496 new_type = NULL_TREE;
498 if (new_val && !(flags & LOOKUP_HIDDEN))
499 new_val = ovl_skip_hidden (new_val);
501 /* Do we really see a value? */
502 if (new_val)
503 switch (TREE_CODE (new_val))
505 case TEMPLATE_DECL:
506 /* If we expect types or namespaces, and not templates,
507 or this is not a template class. */
508 if ((LOOKUP_QUALIFIERS_ONLY (flags)
509 && !DECL_TYPE_TEMPLATE_P (new_val)))
510 new_val = NULL_TREE;
511 break;
512 case TYPE_DECL:
513 if (LOOKUP_NAMESPACES_ONLY (flags)
514 || (new_type && (flags & LOOKUP_PREFER_TYPES)))
515 new_val = NULL_TREE;
516 break;
517 case NAMESPACE_DECL:
518 if (LOOKUP_TYPES_ONLY (flags))
519 new_val = NULL_TREE;
520 break;
521 default:
522 if (LOOKUP_QUALIFIERS_ONLY (flags))
523 new_val = NULL_TREE;
526 if (!new_val)
528 new_val = new_type;
529 new_type = NULL_TREE;
532 /* Merge into the lookup */
533 if (new_val)
534 add_value (new_val);
535 if (new_type)
536 add_type (new_type);
538 return new_val != NULL_TREE;
541 /* Look in exactly namespace SCOPE. */
543 bool
544 name_lookup::search_namespace_only (tree scope)
546 bool found = false;
548 if (tree *binding = find_namespace_slot (scope, name))
549 found |= process_binding (MAYBE_STAT_DECL (*binding),
550 MAYBE_STAT_TYPE (*binding));
552 return found;
555 /* Conditionally look in namespace SCOPE and inline children. */
557 bool
558 name_lookup::search_namespace (tree scope)
560 if (see_and_mark (scope))
561 /* We've visited this scope before. Return what we found then. */
562 return found_p (scope);
564 /* Look in exactly namespace. */
565 bool found = search_namespace_only (scope);
567 /* Recursively look in its inline children. */
568 if (vec<tree, va_gc> *inlinees = DECL_NAMESPACE_INLINEES (scope))
569 for (unsigned ix = inlinees->length (); ix--;)
570 found |= search_namespace ((*inlinees)[ix]);
572 if (found)
573 mark_found (scope);
575 return found;
578 /* Recursively follow using directives of SCOPE & its inline children.
579 Such following is essentially a flood-fill algorithm. */
581 bool
582 name_lookup::search_usings (tree scope)
584 /* We do not check seen_p here, as that was already set during the
585 namespace_only walk. */
586 if (found_p (scope))
587 return true;
589 bool found = false;
590 if (vec<tree, va_gc> *usings = DECL_NAMESPACE_USING (scope))
591 for (unsigned ix = usings->length (); ix--;)
592 found |= search_qualified ((*usings)[ix], true);
594 /* Look in its inline children. */
595 if (vec<tree, va_gc> *inlinees = DECL_NAMESPACE_INLINEES (scope))
596 for (unsigned ix = inlinees->length (); ix--;)
597 found |= search_usings ((*inlinees)[ix]);
599 if (found)
600 mark_found (scope);
602 return found;
605 /* Qualified namespace lookup in SCOPE.
606 1) Look in SCOPE (+inlines). If found, we're done.
607 2) Otherwise, if USINGS is true,
608 recurse for every using directive of SCOPE (+inlines).
610 Trickiness is (a) loops and (b) multiple paths to same namespace.
611 In both cases we want to not repeat any lookups, and know whether
612 to stop the caller's step #2. Do this via the FOUND_P marker. */
614 bool
615 name_lookup::search_qualified (tree scope, bool usings)
617 bool found = false;
619 if (seen_p (scope))
620 found = found_p (scope);
621 else
623 found = search_namespace (scope);
624 if (!found && usings)
625 found = search_usings (scope);
628 return found;
631 /* Add SCOPE to the unqualified search queue, recursively add its
632 inlines and those via using directives. */
634 name_lookup::using_queue *
635 name_lookup::queue_namespace (using_queue *queue, int depth, tree scope)
637 if (see_and_mark (scope))
638 return queue;
640 /* Record it. */
641 tree common = scope;
642 while (SCOPE_DEPTH (common) > depth)
643 common = CP_DECL_CONTEXT (common);
644 vec_safe_push (queue, using_pair (common, scope));
646 /* Queue its inline children. */
647 if (vec<tree, va_gc> *inlinees = DECL_NAMESPACE_INLINEES (scope))
648 for (unsigned ix = inlinees->length (); ix--;)
649 queue = queue_namespace (queue, depth, (*inlinees)[ix]);
651 /* Queue its using targets. */
652 queue = queue_usings (queue, depth, DECL_NAMESPACE_USING (scope));
654 return queue;
657 /* Add the namespaces in USINGS to the unqualified search queue. */
659 name_lookup::using_queue *
660 name_lookup::do_queue_usings (using_queue *queue, int depth,
661 vec<tree, va_gc> *usings)
663 for (unsigned ix = usings->length (); ix--;)
664 queue = queue_namespace (queue, depth, (*usings)[ix]);
666 return queue;
669 /* Unqualified namespace lookup in SCOPE.
670 1) add scope+inlins to worklist.
671 2) recursively add target of every using directive
672 3) for each worklist item where SCOPE is common ancestor, search it
673 4) if nothing find, scope=parent, goto 1. */
675 bool
676 name_lookup::search_unqualified (tree scope, cp_binding_level *level)
678 /* Make static to avoid continual reallocation. We're not
679 recursive. */
680 static using_queue *queue = NULL;
681 bool found = false;
682 int length = vec_safe_length (queue);
684 /* Queue local using-directives. */
685 for (; level->kind != sk_namespace; level = level->level_chain)
686 queue = queue_usings (queue, SCOPE_DEPTH (scope), level->using_directives);
688 for (; !found; scope = CP_DECL_CONTEXT (scope))
690 gcc_assert (!DECL_NAMESPACE_ALIAS (scope));
691 int depth = SCOPE_DEPTH (scope);
693 /* Queue namespaces reachable from SCOPE. */
694 queue = queue_namespace (queue, depth, scope);
696 /* Search every queued namespace where SCOPE is the common
697 ancestor. Adjust the others. */
698 unsigned ix = length;
701 using_pair &pair = (*queue)[ix];
702 while (pair.first == scope)
704 found |= search_namespace_only (pair.second);
705 pair = queue->pop ();
706 if (ix == queue->length ())
707 goto done;
709 /* The depth is the same as SCOPE, find the parent scope. */
710 if (SCOPE_DEPTH (pair.first) == depth)
711 pair.first = CP_DECL_CONTEXT (pair.first);
712 ix++;
714 while (ix < queue->length ());
715 done:;
716 if (scope == global_namespace)
717 break;
720 vec_safe_truncate (queue, length);
722 return found;
725 /* FNS is a value binding. If it is a (set of overloaded) functions,
726 add them into the current value. */
728 void
729 name_lookup::add_fns (tree fns)
731 if (!fns)
732 return;
733 else if (TREE_CODE (fns) == OVERLOAD)
735 if (TREE_TYPE (fns) != unknown_type_node)
736 fns = OVL_FUNCTION (fns);
738 else if (!DECL_DECLARES_FUNCTION_P (fns))
739 return;
741 add_overload (fns);
744 /* Add functions of a namespace to the lookup structure. */
746 void
747 name_lookup::adl_namespace_only (tree scope)
749 mark_seen (scope);
751 /* Look down into inline namespaces. */
752 if (vec<tree, va_gc> *inlinees = DECL_NAMESPACE_INLINEES (scope))
753 for (unsigned ix = inlinees->length (); ix--;)
754 adl_namespace_only ((*inlinees)[ix]);
756 if (tree fns = find_namespace_value (scope, name))
757 add_fns (ovl_skip_hidden (fns));
760 /* Find the containing non-inlined namespace, add it and all its
761 inlinees. */
763 void
764 name_lookup::adl_namespace (tree scope)
766 if (seen_p (scope))
767 return;
769 /* Find the containing non-inline namespace. */
770 while (DECL_NAMESPACE_INLINE_P (scope))
771 scope = CP_DECL_CONTEXT (scope);
773 adl_namespace_only (scope);
776 /* Adds the class and its friends to the lookup structure. */
778 void
779 name_lookup::adl_class_only (tree type)
781 /* Backend-built structures, such as __builtin_va_list, aren't
782 affected by all this. */
783 if (!CLASS_TYPE_P (type))
784 return;
786 type = TYPE_MAIN_VARIANT (type);
788 if (see_and_mark (type))
789 return;
791 tree context = decl_namespace_context (type);
792 adl_namespace (context);
794 complete_type (type);
796 /* Add friends. */
797 for (tree list = DECL_FRIENDLIST (TYPE_MAIN_DECL (type)); list;
798 list = TREE_CHAIN (list))
799 if (name == FRIEND_NAME (list))
800 for (tree friends = FRIEND_DECLS (list); friends;
801 friends = TREE_CHAIN (friends))
803 tree fn = TREE_VALUE (friends);
805 /* Only interested in global functions with potentially hidden
806 (i.e. unqualified) declarations. */
807 if (CP_DECL_CONTEXT (fn) != context)
808 continue;
810 /* Only interested in anticipated friends. (Non-anticipated
811 ones will have been inserted during the namespace
812 adl.) */
813 if (!DECL_ANTICIPATED (fn))
814 continue;
816 /* Template specializations are never found by name lookup.
817 (Templates themselves can be found, but not template
818 specializations.) */
819 if (TREE_CODE (fn) == FUNCTION_DECL && DECL_USE_TEMPLATE (fn))
820 continue;
822 add_fns (fn);
826 /* Adds the class and its bases to the lookup structure.
827 Returns true on error. */
829 void
830 name_lookup::adl_bases (tree type)
832 adl_class_only (type);
834 /* Process baseclasses. */
835 if (tree binfo = TYPE_BINFO (type))
837 tree base_binfo;
838 int i;
840 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
841 adl_bases (BINFO_TYPE (base_binfo));
845 /* Adds everything associated with a class argument type to the lookup
846 structure. Returns true on error.
848 If T is a class type (including unions), its associated classes are: the
849 class itself; the class of which it is a member, if any; and its direct
850 and indirect base classes. Its associated namespaces are the namespaces
851 of which its associated classes are members. Furthermore, if T is a
852 class template specialization, its associated namespaces and classes
853 also include: the namespaces and classes associated with the types of
854 the template arguments provided for template type parameters (excluding
855 template template parameters); the namespaces of which any template
856 template arguments are members; and the classes of which any member
857 templates used as template template arguments are members. [ Note:
858 non-type template arguments do not contribute to the set of associated
859 namespaces. --end note] */
861 void
862 name_lookup::adl_class (tree type)
864 /* Backend build structures, such as __builtin_va_list, aren't
865 affected by all this. */
866 if (!CLASS_TYPE_P (type))
867 return;
869 type = TYPE_MAIN_VARIANT (type);
870 /* We don't set found here because we have to have set seen first,
871 which is done in the adl_bases walk. */
872 if (found_p (type))
873 return;
875 adl_bases (type);
876 mark_found (type);
878 if (TYPE_CLASS_SCOPE_P (type))
879 adl_class_only (TYPE_CONTEXT (type));
881 /* Process template arguments. */
882 if (CLASSTYPE_TEMPLATE_INFO (type)
883 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
885 tree list = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
886 for (int i = 0; i < TREE_VEC_LENGTH (list); ++i)
887 adl_template_arg (TREE_VEC_ELT (list, i));
891 void
892 name_lookup::adl_expr (tree expr)
894 if (!expr)
895 return;
897 gcc_assert (!TYPE_P (expr));
899 if (TREE_TYPE (expr) != unknown_type_node)
901 adl_type (TREE_TYPE (expr));
902 return;
905 if (TREE_CODE (expr) == ADDR_EXPR)
906 expr = TREE_OPERAND (expr, 0);
907 if (TREE_CODE (expr) == COMPONENT_REF
908 || TREE_CODE (expr) == OFFSET_REF)
909 expr = TREE_OPERAND (expr, 1);
910 expr = MAYBE_BASELINK_FUNCTIONS (expr);
912 if (OVL_P (expr))
913 for (lkp_iterator iter (expr); iter; ++iter)
914 adl_type (TREE_TYPE (*iter));
915 else if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
917 /* The working paper doesn't currently say how to handle
918 template-id arguments. The sensible thing would seem to be
919 to handle the list of template candidates like a normal
920 overload set, and handle the template arguments like we do
921 for class template specializations. */
923 /* First the templates. */
924 adl_expr (TREE_OPERAND (expr, 0));
926 /* Now the arguments. */
927 if (tree args = TREE_OPERAND (expr, 1))
928 for (int ix = TREE_VEC_LENGTH (args); ix--;)
929 adl_template_arg (TREE_VEC_ELT (args, ix));
933 void
934 name_lookup::adl_type (tree type)
936 if (!type)
937 return;
939 if (TYPE_PTRDATAMEM_P (type))
941 /* Pointer to member: associate class type and value type. */
942 adl_type (TYPE_PTRMEM_CLASS_TYPE (type));
943 adl_type (TYPE_PTRMEM_POINTED_TO_TYPE (type));
944 return;
947 switch (TREE_CODE (type))
949 case RECORD_TYPE:
950 if (TYPE_PTRMEMFUNC_P (type))
952 adl_type (TYPE_PTRMEMFUNC_FN_TYPE (type));
953 return;
955 /* FALLTHRU */
956 case UNION_TYPE:
957 adl_class (type);
958 return;
960 case METHOD_TYPE:
961 /* The basetype is referenced in the first arg type, so just
962 fall through. */
963 case FUNCTION_TYPE:
964 /* Associate the parameter types. */
965 for (tree args = TYPE_ARG_TYPES (type); args; args = TREE_CHAIN (args))
966 adl_type (TREE_VALUE (args));
967 /* FALLTHROUGH */
969 case POINTER_TYPE:
970 case REFERENCE_TYPE:
971 case ARRAY_TYPE:
972 adl_type (TREE_TYPE (type));
973 return;
975 case ENUMERAL_TYPE:
976 if (TYPE_CLASS_SCOPE_P (type))
977 adl_class_only (TYPE_CONTEXT (type));
978 adl_namespace (decl_namespace_context (type));
979 return;
981 case LANG_TYPE:
982 gcc_assert (type == unknown_type_node
983 || type == init_list_type_node);
984 return;
986 case TYPE_PACK_EXPANSION:
987 adl_type (PACK_EXPANSION_PATTERN (type));
988 return;
990 default:
991 break;
995 /* Adds everything associated with a template argument to the lookup
996 structure. */
998 void
999 name_lookup::adl_template_arg (tree arg)
1001 /* [basic.lookup.koenig]
1003 If T is a template-id, its associated namespaces and classes are
1004 ... the namespaces and classes associated with the types of the
1005 template arguments provided for template type parameters
1006 (excluding template template parameters); the namespaces in which
1007 any template template arguments are defined; and the classes in
1008 which any member templates used as template template arguments
1009 are defined. [Note: non-type template arguments do not
1010 contribute to the set of associated namespaces. ] */
1012 /* Consider first template template arguments. */
1013 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
1014 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
1016 else if (TREE_CODE (arg) == TEMPLATE_DECL)
1018 tree ctx = CP_DECL_CONTEXT (arg);
1020 /* It's not a member template. */
1021 if (TREE_CODE (ctx) == NAMESPACE_DECL)
1022 adl_namespace (ctx);
1023 /* Otherwise, it must be member template. */
1024 else
1025 adl_class_only (ctx);
1027 /* It's an argument pack; handle it recursively. */
1028 else if (ARGUMENT_PACK_P (arg))
1030 tree args = ARGUMENT_PACK_ARGS (arg);
1031 int i, len = TREE_VEC_LENGTH (args);
1032 for (i = 0; i < len; ++i)
1033 adl_template_arg (TREE_VEC_ELT (args, i));
1035 /* It's not a template template argument, but it is a type template
1036 argument. */
1037 else if (TYPE_P (arg))
1038 adl_type (arg);
1041 /* Perform ADL lookup. FNS is the existing lookup result and ARGS are
1042 the call arguments. */
1044 tree
1045 name_lookup::search_adl (tree fns, vec<tree, va_gc> *args)
1047 if (fns)
1049 deduping = true;
1050 lookup_mark (fns, true);
1052 value = fns;
1054 unsigned ix;
1055 tree arg;
1057 FOR_EACH_VEC_ELT_REVERSE (*args, ix, arg)
1058 /* OMP reduction operators put an ADL-significant type as the
1059 first arg. */
1060 if (TYPE_P (arg))
1061 adl_type (arg);
1062 else
1063 adl_expr (arg);
1065 fns = value;
1067 return fns;
1070 static bool qualified_namespace_lookup (tree, name_lookup *);
1071 static void consider_binding_level (tree name,
1072 best_match <tree, const char *> &bm,
1073 cp_binding_level *lvl,
1074 bool look_within_fields,
1075 enum lookup_name_fuzzy_kind kind);
1076 static void diagnose_name_conflict (tree, tree);
1078 /* ADL lookup of NAME. FNS is the result of regular lookup, and we
1079 don't add duplicates to it. ARGS is the vector of call
1080 arguments (which will not be empty). */
1082 tree
1083 lookup_arg_dependent (tree name, tree fns, vec<tree, va_gc> *args)
1085 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
1086 name_lookup lookup (name);
1087 fns = lookup.search_adl (fns, args);
1088 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
1089 return fns;
1092 /* FNS is an overload set of conversion functions. Return the
1093 overloads converting to TYPE. */
1095 static tree
1096 extract_conversion_operator (tree fns, tree type)
1098 tree convs = NULL_TREE;
1099 tree tpls = NULL_TREE;
1101 for (ovl_iterator iter (fns); iter; ++iter)
1103 if (same_type_p (DECL_CONV_FN_TYPE (*iter), type))
1104 convs = lookup_add (*iter, convs);
1106 if (TREE_CODE (*iter) == TEMPLATE_DECL)
1107 tpls = lookup_add (*iter, tpls);
1110 if (!convs)
1111 convs = tpls;
1113 return convs;
1116 /* Binary search of (ordered) MEMBER_VEC for NAME. */
1118 static tree
1119 member_vec_binary_search (vec<tree, va_gc> *member_vec, tree name)
1121 for (unsigned lo = 0, hi = member_vec->length (); lo < hi;)
1123 unsigned mid = (lo + hi) / 2;
1124 tree binding = (*member_vec)[mid];
1125 tree binding_name = OVL_NAME (binding);
1127 if (binding_name > name)
1128 hi = mid;
1129 else if (binding_name < name)
1130 lo = mid + 1;
1131 else
1132 return binding;
1135 return NULL_TREE;
1138 /* Linear search of (unordered) MEMBER_VEC for NAME. */
1140 static tree
1141 member_vec_linear_search (vec<tree, va_gc> *member_vec, tree name)
1143 for (int ix = member_vec->length (); ix--;)
1144 /* We can get a NULL binding during insertion of a new method
1145 name, because the identifier_binding machinery performs a
1146 lookup. If we find such a NULL slot, that's the thing we were
1147 looking for, so we might as well bail out immediately. */
1148 if (tree binding = (*member_vec)[ix])
1150 if (OVL_NAME (binding) == name)
1151 return binding;
1153 else
1154 break;
1156 return NULL_TREE;
1159 /* Linear search of (partially ordered) fields of KLASS for NAME. */
1161 static tree
1162 fields_linear_search (tree klass, tree name, bool want_type)
1164 for (tree fields = TYPE_FIELDS (klass); fields; fields = DECL_CHAIN (fields))
1166 tree decl = fields;
1168 if (!want_type
1169 && TREE_CODE (decl) == FIELD_DECL
1170 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
1172 tree anon = TREE_TYPE (decl);
1173 gcc_assert (COMPLETE_TYPE_P (anon));
1174 tree temp;
1176 if (vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (anon))
1177 temp = member_vec_linear_search (member_vec, name);
1178 else
1179 temp = fields_linear_search (anon, name, want_type);
1181 if (temp)
1183 /* Anon members can only contain fields. */
1184 gcc_assert (!STAT_HACK_P (temp) && !DECL_DECLARES_TYPE_P (temp));
1185 return temp;
1189 if (DECL_NAME (decl) != name)
1190 continue;
1192 if (TREE_CODE (decl) == USING_DECL)
1194 decl = strip_using_decl (decl);
1195 if (is_overloaded_fn (decl))
1196 continue;
1199 if (DECL_DECLARES_FUNCTION_P (decl))
1200 /* Functions are found separately. */
1201 continue;
1203 if (!want_type || DECL_DECLARES_TYPE_P (decl))
1204 return decl;
1207 return NULL_TREE;
1210 /* Look for NAME as an immediate member of KLASS (including
1211 anon-members or unscoped enum member). TYPE_OR_FNS is zero for
1212 regular search. >0 to get a type binding (if there is one) and <0
1213 if you want (just) the member function binding.
1215 Use this if you do not want lazy member creation. */
1217 tree
1218 get_class_binding_direct (tree klass, tree name, int type_or_fns)
1220 gcc_checking_assert (RECORD_OR_UNION_TYPE_P (klass));
1222 /* Conversion operators can only be found by the marker conversion
1223 operator name. */
1224 bool conv_op = IDENTIFIER_CONV_OP_P (name);
1225 tree lookup = conv_op ? conv_op_identifier : name;
1226 tree val = NULL_TREE;
1227 vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (klass);
1229 if (COMPLETE_TYPE_P (klass) && member_vec)
1231 val = member_vec_binary_search (member_vec, lookup);
1232 if (!val)
1234 else if (type_or_fns > 0)
1236 if (STAT_HACK_P (val))
1237 val = STAT_TYPE (val);
1238 else if (!DECL_DECLARES_TYPE_P (val))
1239 val = NULL_TREE;
1241 else if (STAT_HACK_P (val))
1242 val = STAT_DECL (val);
1244 if (val && TREE_CODE (val) == OVERLOAD
1245 && TREE_CODE (OVL_FUNCTION (val)) == USING_DECL)
1247 /* An overload with a dependent USING_DECL. Does the caller
1248 want the USING_DECL or the functions? */
1249 if (type_or_fns < 0)
1250 val = OVL_CHAIN (val);
1251 else
1252 val = OVL_FUNCTION (val);
1255 else
1257 if (member_vec && type_or_fns <= 0)
1258 val = member_vec_linear_search (member_vec, lookup);
1260 if (type_or_fns < 0)
1261 /* Don't bother looking for field. We don't want it. */;
1262 else if (!val || (TREE_CODE (val) == OVERLOAD && OVL_USING_P (val)))
1263 /* Dependent using declarations are a 'field', make sure we
1264 return that even if we saw an overload already. */
1265 if (tree field_val = fields_linear_search (klass, lookup,
1266 type_or_fns > 0))
1267 if (!val || TREE_CODE (field_val) == USING_DECL)
1268 val = field_val;
1271 /* Extract the conversion operators asked for, unless the general
1272 conversion operator was requested. */
1273 if (val && conv_op)
1275 gcc_checking_assert (OVL_FUNCTION (val) == conv_op_marker);
1276 val = OVL_CHAIN (val);
1277 if (tree type = TREE_TYPE (name))
1278 val = extract_conversion_operator (val, type);
1281 return val;
1284 /* Look for NAME's binding in exactly KLASS. See
1285 get_class_binding_direct for argument description. Does lazy
1286 special function creation as necessary. */
1288 tree
1289 get_class_binding (tree klass, tree name, int type_or_fns)
1291 klass = complete_type (klass);
1293 if (COMPLETE_TYPE_P (klass))
1295 /* Lazily declare functions, if we're going to search these. */
1296 if (IDENTIFIER_CTOR_P (name))
1298 if (CLASSTYPE_LAZY_DEFAULT_CTOR (klass))
1299 lazily_declare_fn (sfk_constructor, klass);
1300 if (CLASSTYPE_LAZY_COPY_CTOR (klass))
1301 lazily_declare_fn (sfk_copy_constructor, klass);
1302 if (CLASSTYPE_LAZY_MOVE_CTOR (klass))
1303 lazily_declare_fn (sfk_move_constructor, klass);
1305 else if (IDENTIFIER_DTOR_P (name))
1307 if (CLASSTYPE_LAZY_DESTRUCTOR (klass))
1308 lazily_declare_fn (sfk_destructor, klass);
1310 else if (name == cp_assignment_operator_id (NOP_EXPR))
1312 if (CLASSTYPE_LAZY_COPY_ASSIGN (klass))
1313 lazily_declare_fn (sfk_copy_assignment, klass);
1314 if (CLASSTYPE_LAZY_MOVE_ASSIGN (klass))
1315 lazily_declare_fn (sfk_move_assignment, klass);
1319 return get_class_binding_direct (klass, name, type_or_fns);
1322 /* Find the slot containing overloads called 'NAME'. If there is no
1323 such slot, create an empty one. KLASS might be complete at this
1324 point, in which case we need to preserve ordering. Deals with
1325 conv_op marker handling. */
1327 tree *
1328 get_member_slot (tree klass, tree name)
1330 bool complete_p = COMPLETE_TYPE_P (klass);
1332 vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (klass);
1333 if (!member_vec)
1335 vec_alloc (member_vec, 8);
1336 CLASSTYPE_MEMBER_VEC (klass) = member_vec;
1337 if (complete_p)
1339 /* If the class is complete but had no member_vec, we need
1340 to add the TYPE_FIELDS into it. We're also most likely
1341 to be adding ctors & dtors, so ask for 6 spare slots (the
1342 abstract cdtors and their clones). */
1343 set_class_bindings (klass, 6);
1344 member_vec = CLASSTYPE_MEMBER_VEC (klass);
1348 if (IDENTIFIER_CONV_OP_P (name))
1349 name = conv_op_identifier;
1351 unsigned ix, length = member_vec->length ();
1352 for (ix = 0; ix < length; ix++)
1354 tree *slot = &(*member_vec)[ix];
1355 tree fn_name = OVL_NAME (*slot);
1357 if (fn_name == name)
1359 /* If we found an existing slot, it must be a function set.
1360 Even with insertion after completion, because those only
1361 happen with artificial fns that have unspellable names.
1362 This means we do not have to deal with the stat hack
1363 either. */
1364 gcc_checking_assert (OVL_P (*slot));
1365 if (name == conv_op_identifier)
1367 gcc_checking_assert (OVL_FUNCTION (*slot) == conv_op_marker);
1368 /* Skip the conv-op marker. */
1369 slot = &OVL_CHAIN (*slot);
1371 return slot;
1374 if (complete_p && fn_name > name)
1375 break;
1378 /* No slot found. Create one at IX. We know in this case that our
1379 caller will succeed in adding the function. */
1380 if (complete_p)
1382 /* Do exact allocation when complete, as we don't expect to add
1383 many. */
1384 vec_safe_reserve_exact (member_vec, 1);
1385 member_vec->quick_insert (ix, NULL_TREE);
1387 else
1389 gcc_checking_assert (ix == length);
1390 vec_safe_push (member_vec, NULL_TREE);
1392 CLASSTYPE_MEMBER_VEC (klass) = member_vec;
1394 tree *slot = &(*member_vec)[ix];
1395 if (name == conv_op_identifier)
1397 /* Install the marker prefix. */
1398 *slot = ovl_make (conv_op_marker, NULL_TREE);
1399 slot = &OVL_CHAIN (*slot);
1402 return slot;
1405 /* Comparison function to compare two MEMBER_VEC entries by name.
1406 Because we can have duplicates during insertion of TYPE_FIELDS, we
1407 do extra checking so deduping doesn't have to deal with so many
1408 cases. */
1410 static int
1411 member_name_cmp (const void *a_p, const void *b_p)
1413 tree a = *(const tree *)a_p;
1414 tree b = *(const tree *)b_p;
1415 tree name_a = DECL_NAME (TREE_CODE (a) == OVERLOAD ? OVL_FUNCTION (a) : a);
1416 tree name_b = DECL_NAME (TREE_CODE (b) == OVERLOAD ? OVL_FUNCTION (b) : b);
1418 gcc_checking_assert (name_a && name_b);
1419 if (name_a != name_b)
1420 return name_a < name_b ? -1 : +1;
1422 if (name_a == conv_op_identifier)
1424 /* Strip the conv-op markers. */
1425 gcc_checking_assert (OVL_FUNCTION (a) == conv_op_marker
1426 && OVL_FUNCTION (b) == conv_op_marker);
1427 a = OVL_CHAIN (a);
1428 b = OVL_CHAIN (b);
1431 if (TREE_CODE (a) == OVERLOAD)
1432 a = OVL_FUNCTION (a);
1433 if (TREE_CODE (b) == OVERLOAD)
1434 b = OVL_FUNCTION (b);
1436 /* We're in STAT_HACK or USING_DECL territory (or possibly error-land). */
1437 if (TREE_CODE (a) != TREE_CODE (b))
1439 /* If one of them is a TYPE_DECL, it loses. */
1440 if (TREE_CODE (a) == TYPE_DECL)
1441 return +1;
1442 else if (TREE_CODE (b) == TYPE_DECL)
1443 return -1;
1445 /* If one of them is a USING_DECL, it loses. */
1446 if (TREE_CODE (a) == USING_DECL)
1447 return +1;
1448 else if (TREE_CODE (b) == USING_DECL)
1449 return -1;
1451 /* There are no other cases with different kinds of decls, as
1452 duplicate detection should have kicked in earlier. However,
1453 some erroneous cases get though. */
1454 gcc_assert (errorcount);
1457 /* Using source location would be the best thing here, but we can
1458 get identically-located decls in the following circumstances:
1460 1) duplicate artificial type-decls for the same type.
1462 2) pack expansions of using-decls.
1464 We should not be doing #1, but in either case it doesn't matter
1465 how we order these. Use UID as a proxy for source ordering, so
1466 that identically-located decls still have a well-defined stable
1467 ordering. */
1468 return DECL_UID (a) < DECL_UID (b) ? -1 : +1;
1471 static struct {
1472 gt_pointer_operator new_value;
1473 void *cookie;
1474 } resort_data;
1476 /* This routine compares two fields like member_name_cmp but using the
1477 pointer operator in resort_field_decl_data. We don't have to deal
1478 with duplicates here. */
1480 static int
1481 resort_member_name_cmp (const void *a_p, const void *b_p)
1483 tree a = *(const tree *)a_p;
1484 tree b = *(const tree *)b_p;
1485 tree name_a = OVL_NAME (a);
1486 tree name_b = OVL_NAME (b);
1488 resort_data.new_value (&name_a, resort_data.cookie);
1489 resort_data.new_value (&name_b, resort_data.cookie);
1491 gcc_checking_assert (name_a != name_b);
1493 return name_a < name_b ? -1 : +1;
1496 /* Resort CLASSTYPE_MEMBER_VEC because pointers have been reordered. */
1498 void
1499 resort_type_member_vec (void *obj, void */*orig_obj*/,
1500 gt_pointer_operator new_value, void* cookie)
1502 if (vec<tree, va_gc> *member_vec = (vec<tree, va_gc> *) obj)
1504 resort_data.new_value = new_value;
1505 resort_data.cookie = cookie;
1506 qsort (member_vec->address (), member_vec->length (),
1507 sizeof (tree), resort_member_name_cmp);
1511 /* Recursively count the number of fields in KLASS, including anonymous
1512 union members. */
1514 static unsigned
1515 count_class_fields (tree klass)
1517 unsigned n_fields = 0;
1519 for (tree fields = TYPE_FIELDS (klass); fields; fields = DECL_CHAIN (fields))
1520 if (DECL_DECLARES_FUNCTION_P (fields))
1521 /* Functions are dealt with separately. */;
1522 else if (TREE_CODE (fields) == FIELD_DECL
1523 && ANON_AGGR_TYPE_P (TREE_TYPE (fields)))
1524 n_fields += count_class_fields (TREE_TYPE (fields));
1525 else if (DECL_NAME (fields))
1526 n_fields += 1;
1528 return n_fields;
1531 /* Append all the nonfunction members fields of KLASS to MEMBER_VEC.
1532 Recurse for anonymous members. MEMBER_VEC must have space. */
1534 static void
1535 member_vec_append_class_fields (vec<tree, va_gc> *member_vec, tree klass)
1537 for (tree fields = TYPE_FIELDS (klass); fields; fields = DECL_CHAIN (fields))
1538 if (DECL_DECLARES_FUNCTION_P (fields))
1539 /* Functions are handled separately. */;
1540 else if (TREE_CODE (fields) == FIELD_DECL
1541 && ANON_AGGR_TYPE_P (TREE_TYPE (fields)))
1542 member_vec_append_class_fields (member_vec, TREE_TYPE (fields));
1543 else if (DECL_NAME (fields))
1545 tree field = fields;
1546 /* Mark a conv-op USING_DECL with the conv-op-marker. */
1547 if (TREE_CODE (field) == USING_DECL
1548 && IDENTIFIER_CONV_OP_P (DECL_NAME (field)))
1549 field = ovl_make (conv_op_marker, field);
1550 member_vec->quick_push (field);
1554 /* Append all of the enum values of ENUMTYPE to MEMBER_VEC.
1555 MEMBER_VEC must have space. */
1557 static void
1558 member_vec_append_enum_values (vec<tree, va_gc> *member_vec, tree enumtype)
1560 for (tree values = TYPE_VALUES (enumtype);
1561 values; values = TREE_CHAIN (values))
1562 member_vec->quick_push (TREE_VALUE (values));
1565 /* MEMBER_VEC has just had new DECLs added to it, but is sorted.
1566 DeDup adjacent DECLS of the same name. We already dealt with
1567 conflict resolution when adding the fields or methods themselves.
1568 There are three cases (which could all be combined):
1569 1) a TYPE_DECL and non TYPE_DECL. Deploy STAT_HACK as appropriate.
1570 2) a USING_DECL and an overload. If the USING_DECL is dependent,
1571 it wins. Otherwise the OVERLOAD does.
1572 3) two USING_DECLS. ...
1574 member_name_cmp will have ordered duplicates as
1575 <fns><using><type> */
1577 static void
1578 member_vec_dedup (vec<tree, va_gc> *member_vec)
1580 unsigned len = member_vec->length ();
1581 unsigned store = 0;
1583 tree current = (*member_vec)[0], name = OVL_NAME (current);
1584 tree next = NULL_TREE, next_name = NULL_TREE;
1585 for (unsigned jx, ix = 0; ix < len;
1586 ix = jx, current = next, name = next_name)
1588 tree to_type = NULL_TREE;
1589 tree to_using = NULL_TREE;
1590 tree marker = NULL_TREE;
1591 if (IDENTIFIER_CONV_OP_P (name))
1593 marker = current;
1594 current = OVL_CHAIN (current);
1595 name = DECL_NAME (OVL_FUNCTION (marker));
1596 gcc_checking_assert (name == conv_op_identifier);
1599 if (TREE_CODE (current) == USING_DECL)
1601 current = strip_using_decl (current);
1602 if (is_overloaded_fn (current))
1603 current = NULL_TREE;
1604 else if (TREE_CODE (current) == USING_DECL)
1606 to_using = current;
1607 current = NULL_TREE;
1611 if (current && DECL_DECLARES_TYPE_P (current))
1613 to_type = current;
1614 current = NULL_TREE;
1617 for (jx = ix + 1; jx < len; jx++)
1619 next = (*member_vec)[jx];
1620 next_name = OVL_NAME (next);
1621 if (next_name != name)
1622 break;
1624 if (marker)
1626 gcc_checking_assert (OVL_FUNCTION (marker)
1627 == OVL_FUNCTION (next));
1628 next = OVL_CHAIN (next);
1631 if (TREE_CODE (next) == USING_DECL)
1633 next = strip_using_decl (next);
1634 if (is_overloaded_fn (next))
1635 next = NULL_TREE;
1636 else if (TREE_CODE (next) == USING_DECL)
1638 to_using = next;
1639 next = NULL_TREE;
1643 if (next && DECL_DECLARES_TYPE_P (next))
1644 to_type = next;
1647 if (to_using)
1649 if (!current)
1650 current = to_using;
1651 else
1652 current = ovl_make (to_using, current);
1655 if (to_type)
1657 if (!current)
1658 current = to_type;
1659 else
1660 current = stat_hack (current, to_type);
1663 gcc_assert (current);
1664 if (marker)
1666 OVL_CHAIN (marker) = current;
1667 current = marker;
1669 (*member_vec)[store++] = current;
1672 while (store++ < len)
1673 member_vec->pop ();
1676 /* Add the non-function members to CLASSTYPE_MEMBER_VEC. If there is
1677 no existing MEMBER_VEC and fewer than 8 fields, do nothing. We
1678 know there must be at least 1 field -- the self-reference
1679 TYPE_DECL, except for anon aggregates, which will have at least
1680 one field. */
1682 void
1683 set_class_bindings (tree klass, unsigned extra)
1685 unsigned n_fields = count_class_fields (klass);
1686 vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (klass);
1688 if (member_vec || n_fields >= 8)
1690 /* Append the new fields. */
1691 vec_safe_reserve_exact (member_vec, extra + n_fields);
1692 member_vec_append_class_fields (member_vec, klass);
1695 if (member_vec)
1697 CLASSTYPE_MEMBER_VEC (klass) = member_vec;
1698 qsort (member_vec->address (), member_vec->length (),
1699 sizeof (tree), member_name_cmp);
1700 member_vec_dedup (member_vec);
1704 /* Insert lately defined enum ENUMTYPE into KLASS for the sorted case. */
1706 void
1707 insert_late_enum_def_bindings (tree klass, tree enumtype)
1709 int n_fields;
1710 vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (klass);
1712 /* The enum bindings will already be on the TYPE_FIELDS, so don't
1713 count them twice. */
1714 if (!member_vec)
1715 n_fields = count_class_fields (klass);
1716 else
1717 n_fields = list_length (TYPE_VALUES (enumtype));
1719 if (member_vec || n_fields >= 8)
1721 vec_safe_reserve_exact (member_vec, n_fields);
1722 if (CLASSTYPE_MEMBER_VEC (klass))
1723 member_vec_append_enum_values (member_vec, enumtype);
1724 else
1725 member_vec_append_class_fields (member_vec, klass);
1726 CLASSTYPE_MEMBER_VEC (klass) = member_vec;
1727 qsort (member_vec->address (), member_vec->length (),
1728 sizeof (tree), member_name_cmp);
1729 member_vec_dedup (member_vec);
1733 /* Compute the chain index of a binding_entry given the HASH value of its
1734 name and the total COUNT of chains. COUNT is assumed to be a power
1735 of 2. */
1737 #define ENTRY_INDEX(HASH, COUNT) (((HASH) >> 3) & ((COUNT) - 1))
1739 /* A free list of "binding_entry"s awaiting for re-use. */
1741 static GTY((deletable)) binding_entry free_binding_entry = NULL;
1743 /* The binding oracle; see cp-tree.h. */
1745 cp_binding_oracle_function *cp_binding_oracle;
1747 /* If we have a binding oracle, ask it for all namespace-scoped
1748 definitions of NAME. */
1750 static inline void
1751 query_oracle (tree name)
1753 if (!cp_binding_oracle)
1754 return;
1756 /* LOOKED_UP holds the set of identifiers that we have already
1757 looked up with the oracle. */
1758 static hash_set<tree> looked_up;
1759 if (looked_up.add (name))
1760 return;
1762 cp_binding_oracle (CP_ORACLE_IDENTIFIER, name);
1765 /* Create a binding_entry object for (NAME, TYPE). */
1767 static inline binding_entry
1768 binding_entry_make (tree name, tree type)
1770 binding_entry entry;
1772 if (free_binding_entry)
1774 entry = free_binding_entry;
1775 free_binding_entry = entry->chain;
1777 else
1778 entry = ggc_alloc<binding_entry_s> ();
1780 entry->name = name;
1781 entry->type = type;
1782 entry->chain = NULL;
1784 return entry;
1787 /* Put ENTRY back on the free list. */
1788 #if 0
1789 static inline void
1790 binding_entry_free (binding_entry entry)
1792 entry->name = NULL;
1793 entry->type = NULL;
1794 entry->chain = free_binding_entry;
1795 free_binding_entry = entry;
1797 #endif
1799 /* The datatype used to implement the mapping from names to types at
1800 a given scope. */
1801 struct GTY(()) binding_table_s {
1802 /* Array of chains of "binding_entry"s */
1803 binding_entry * GTY((length ("%h.chain_count"))) chain;
1805 /* The number of chains in this table. This is the length of the
1806 member "chain" considered as an array. */
1807 size_t chain_count;
1809 /* Number of "binding_entry"s in this table. */
1810 size_t entry_count;
1813 /* Construct TABLE with an initial CHAIN_COUNT. */
1815 static inline void
1816 binding_table_construct (binding_table table, size_t chain_count)
1818 table->chain_count = chain_count;
1819 table->entry_count = 0;
1820 table->chain = ggc_cleared_vec_alloc<binding_entry> (table->chain_count);
1823 /* Make TABLE's entries ready for reuse. */
1824 #if 0
1825 static void
1826 binding_table_free (binding_table table)
1828 size_t i;
1829 size_t count;
1831 if (table == NULL)
1832 return;
1834 for (i = 0, count = table->chain_count; i < count; ++i)
1836 binding_entry temp = table->chain[i];
1837 while (temp != NULL)
1839 binding_entry entry = temp;
1840 temp = entry->chain;
1841 binding_entry_free (entry);
1843 table->chain[i] = NULL;
1845 table->entry_count = 0;
1847 #endif
1849 /* Allocate a table with CHAIN_COUNT, assumed to be a power of two. */
1851 static inline binding_table
1852 binding_table_new (size_t chain_count)
1854 binding_table table = ggc_alloc<binding_table_s> ();
1855 table->chain = NULL;
1856 binding_table_construct (table, chain_count);
1857 return table;
1860 /* Expand TABLE to twice its current chain_count. */
1862 static void
1863 binding_table_expand (binding_table table)
1865 const size_t old_chain_count = table->chain_count;
1866 const size_t old_entry_count = table->entry_count;
1867 const size_t new_chain_count = 2 * old_chain_count;
1868 binding_entry *old_chains = table->chain;
1869 size_t i;
1871 binding_table_construct (table, new_chain_count);
1872 for (i = 0; i < old_chain_count; ++i)
1874 binding_entry entry = old_chains[i];
1875 for (; entry != NULL; entry = old_chains[i])
1877 const unsigned int hash = IDENTIFIER_HASH_VALUE (entry->name);
1878 const size_t j = ENTRY_INDEX (hash, new_chain_count);
1880 old_chains[i] = entry->chain;
1881 entry->chain = table->chain[j];
1882 table->chain[j] = entry;
1885 table->entry_count = old_entry_count;
1888 /* Insert a binding for NAME to TYPE into TABLE. */
1890 static void
1891 binding_table_insert (binding_table table, tree name, tree type)
1893 const unsigned int hash = IDENTIFIER_HASH_VALUE (name);
1894 const size_t i = ENTRY_INDEX (hash, table->chain_count);
1895 binding_entry entry = binding_entry_make (name, type);
1897 entry->chain = table->chain[i];
1898 table->chain[i] = entry;
1899 ++table->entry_count;
1901 if (3 * table->chain_count < 5 * table->entry_count)
1902 binding_table_expand (table);
1905 /* Return the binding_entry, if any, that maps NAME. */
1907 binding_entry
1908 binding_table_find (binding_table table, tree name)
1910 const unsigned int hash = IDENTIFIER_HASH_VALUE (name);
1911 binding_entry entry = table->chain[ENTRY_INDEX (hash, table->chain_count)];
1913 while (entry != NULL && entry->name != name)
1914 entry = entry->chain;
1916 return entry;
1919 /* Apply PROC -- with DATA -- to all entries in TABLE. */
1921 void
1922 binding_table_foreach (binding_table table, bt_foreach_proc proc, void *data)
1924 size_t chain_count;
1925 size_t i;
1927 if (!table)
1928 return;
1930 chain_count = table->chain_count;
1931 for (i = 0; i < chain_count; ++i)
1933 binding_entry entry = table->chain[i];
1934 for (; entry != NULL; entry = entry->chain)
1935 proc (entry, data);
1939 #ifndef ENABLE_SCOPE_CHECKING
1940 # define ENABLE_SCOPE_CHECKING 0
1941 #else
1942 # define ENABLE_SCOPE_CHECKING 1
1943 #endif
1945 /* A free list of "cxx_binding"s, connected by their PREVIOUS. */
1947 static GTY((deletable)) cxx_binding *free_bindings;
1949 /* Initialize VALUE and TYPE field for BINDING, and set the PREVIOUS
1950 field to NULL. */
1952 static inline void
1953 cxx_binding_init (cxx_binding *binding, tree value, tree type)
1955 binding->value = value;
1956 binding->type = type;
1957 binding->previous = NULL;
1960 /* (GC)-allocate a binding object with VALUE and TYPE member initialized. */
1962 static cxx_binding *
1963 cxx_binding_make (tree value, tree type)
1965 cxx_binding *binding;
1966 if (free_bindings)
1968 binding = free_bindings;
1969 free_bindings = binding->previous;
1971 else
1972 binding = ggc_alloc<cxx_binding> ();
1974 cxx_binding_init (binding, value, type);
1976 return binding;
1979 /* Put BINDING back on the free list. */
1981 static inline void
1982 cxx_binding_free (cxx_binding *binding)
1984 binding->scope = NULL;
1985 binding->previous = free_bindings;
1986 free_bindings = binding;
1989 /* Create a new binding for NAME (with the indicated VALUE and TYPE
1990 bindings) in the class scope indicated by SCOPE. */
1992 static cxx_binding *
1993 new_class_binding (tree name, tree value, tree type, cp_binding_level *scope)
1995 cp_class_binding cb = {cxx_binding_make (value, type), name};
1996 cxx_binding *binding = cb.base;
1997 vec_safe_push (scope->class_shadowed, cb);
1998 binding->scope = scope;
1999 return binding;
2002 /* Make DECL the innermost binding for ID. The LEVEL is the binding
2003 level at which this declaration is being bound. */
2005 void
2006 push_binding (tree id, tree decl, cp_binding_level* level)
2008 cxx_binding *binding;
2010 if (level != class_binding_level)
2012 binding = cxx_binding_make (decl, NULL_TREE);
2013 binding->scope = level;
2015 else
2016 binding = new_class_binding (id, decl, /*type=*/NULL_TREE, level);
2018 /* Now, fill in the binding information. */
2019 binding->previous = IDENTIFIER_BINDING (id);
2020 INHERITED_VALUE_BINDING_P (binding) = 0;
2021 LOCAL_BINDING_P (binding) = (level != class_binding_level);
2023 /* And put it on the front of the list of bindings for ID. */
2024 IDENTIFIER_BINDING (id) = binding;
2027 /* Remove the binding for DECL which should be the innermost binding
2028 for ID. */
2030 void
2031 pop_local_binding (tree id, tree decl)
2033 cxx_binding *binding;
2035 if (id == NULL_TREE)
2036 /* It's easiest to write the loops that call this function without
2037 checking whether or not the entities involved have names. We
2038 get here for such an entity. */
2039 return;
2041 /* Get the innermost binding for ID. */
2042 binding = IDENTIFIER_BINDING (id);
2044 /* The name should be bound. */
2045 gcc_assert (binding != NULL);
2047 /* The DECL will be either the ordinary binding or the type
2048 binding for this identifier. Remove that binding. */
2049 if (binding->value == decl)
2050 binding->value = NULL_TREE;
2051 else
2053 gcc_assert (binding->type == decl);
2054 binding->type = NULL_TREE;
2057 if (!binding->value && !binding->type)
2059 /* We're completely done with the innermost binding for this
2060 identifier. Unhook it from the list of bindings. */
2061 IDENTIFIER_BINDING (id) = binding->previous;
2063 /* Add it to the free list. */
2064 cxx_binding_free (binding);
2068 /* Remove the bindings for the decls of the current level and leave
2069 the current scope. */
2071 void
2072 pop_bindings_and_leave_scope (void)
2074 for (tree t = get_local_decls (); t; t = DECL_CHAIN (t))
2076 tree decl = TREE_CODE (t) == TREE_LIST ? TREE_VALUE (t) : t;
2077 tree name = OVL_NAME (decl);
2079 pop_local_binding (name, decl);
2082 leave_scope ();
2085 /* Strip non dependent using declarations. If DECL is dependent,
2086 surreptitiously create a typename_type and return it. */
2088 tree
2089 strip_using_decl (tree decl)
2091 if (decl == NULL_TREE)
2092 return NULL_TREE;
2094 while (TREE_CODE (decl) == USING_DECL && !DECL_DEPENDENT_P (decl))
2095 decl = USING_DECL_DECLS (decl);
2097 if (TREE_CODE (decl) == USING_DECL && DECL_DEPENDENT_P (decl)
2098 && USING_DECL_TYPENAME_P (decl))
2100 /* We have found a type introduced by a using
2101 declaration at class scope that refers to a dependent
2102 type.
2104 using typename :: [opt] nested-name-specifier unqualified-id ;
2106 decl = make_typename_type (TREE_TYPE (decl),
2107 DECL_NAME (decl),
2108 typename_type, tf_error);
2109 if (decl != error_mark_node)
2110 decl = TYPE_NAME (decl);
2113 return decl;
2116 /* Return true if OVL is an overload for an anticipated builtin. */
2118 static bool
2119 anticipated_builtin_p (tree ovl)
2121 if (TREE_CODE (ovl) != OVERLOAD)
2122 return false;
2124 if (!OVL_HIDDEN_P (ovl))
2125 return false;
2127 tree fn = OVL_FUNCTION (ovl);
2128 gcc_checking_assert (DECL_ANTICIPATED (fn));
2130 if (DECL_HIDDEN_FRIEND_P (fn))
2131 return false;
2133 return true;
2136 /* BINDING records an existing declaration for a name in the current scope.
2137 But, DECL is another declaration for that same identifier in the
2138 same scope. This is the `struct stat' hack whereby a non-typedef
2139 class name or enum-name can be bound at the same level as some other
2140 kind of entity.
2141 3.3.7/1
2143 A class name (9.1) or enumeration name (7.2) can be hidden by the
2144 name of an object, function, or enumerator declared in the same scope.
2145 If a class or enumeration name and an object, function, or enumerator
2146 are declared in the same scope (in any order) with the same name, the
2147 class or enumeration name is hidden wherever the object, function, or
2148 enumerator name is visible.
2150 It's the responsibility of the caller to check that
2151 inserting this name is valid here. Returns nonzero if the new binding
2152 was successful. */
2154 static bool
2155 supplement_binding_1 (cxx_binding *binding, tree decl)
2157 tree bval = binding->value;
2158 bool ok = true;
2159 tree target_bval = strip_using_decl (bval);
2160 tree target_decl = strip_using_decl (decl);
2162 if (TREE_CODE (target_decl) == TYPE_DECL && DECL_ARTIFICIAL (target_decl)
2163 && target_decl != target_bval
2164 && (TREE_CODE (target_bval) != TYPE_DECL
2165 /* We allow pushing an enum multiple times in a class
2166 template in order to handle late matching of underlying
2167 type on an opaque-enum-declaration followed by an
2168 enum-specifier. */
2169 || (processing_template_decl
2170 && TREE_CODE (TREE_TYPE (target_decl)) == ENUMERAL_TYPE
2171 && TREE_CODE (TREE_TYPE (target_bval)) == ENUMERAL_TYPE
2172 && (dependent_type_p (ENUM_UNDERLYING_TYPE
2173 (TREE_TYPE (target_decl)))
2174 || dependent_type_p (ENUM_UNDERLYING_TYPE
2175 (TREE_TYPE (target_bval)))))))
2176 /* The new name is the type name. */
2177 binding->type = decl;
2178 else if (/* TARGET_BVAL is null when push_class_level_binding moves
2179 an inherited type-binding out of the way to make room
2180 for a new value binding. */
2181 !target_bval
2182 /* TARGET_BVAL is error_mark_node when TARGET_DECL's name
2183 has been used in a non-class scope prior declaration.
2184 In that case, we should have already issued a
2185 diagnostic; for graceful error recovery purpose, pretend
2186 this was the intended declaration for that name. */
2187 || target_bval == error_mark_node
2188 /* If TARGET_BVAL is anticipated but has not yet been
2189 declared, pretend it is not there at all. */
2190 || anticipated_builtin_p (target_bval))
2191 binding->value = decl;
2192 else if (TREE_CODE (target_bval) == TYPE_DECL
2193 && DECL_ARTIFICIAL (target_bval)
2194 && target_decl != target_bval
2195 && (TREE_CODE (target_decl) != TYPE_DECL
2196 || same_type_p (TREE_TYPE (target_decl),
2197 TREE_TYPE (target_bval))))
2199 /* The old binding was a type name. It was placed in
2200 VALUE field because it was thought, at the point it was
2201 declared, to be the only entity with such a name. Move the
2202 type name into the type slot; it is now hidden by the new
2203 binding. */
2204 binding->type = bval;
2205 binding->value = decl;
2206 binding->value_is_inherited = false;
2208 else if (TREE_CODE (target_bval) == TYPE_DECL
2209 && TREE_CODE (target_decl) == TYPE_DECL
2210 && DECL_NAME (target_decl) == DECL_NAME (target_bval)
2211 && binding->scope->kind != sk_class
2212 && (same_type_p (TREE_TYPE (target_decl), TREE_TYPE (target_bval))
2213 /* If either type involves template parameters, we must
2214 wait until instantiation. */
2215 || uses_template_parms (TREE_TYPE (target_decl))
2216 || uses_template_parms (TREE_TYPE (target_bval))))
2217 /* We have two typedef-names, both naming the same type to have
2218 the same name. In general, this is OK because of:
2220 [dcl.typedef]
2222 In a given scope, a typedef specifier can be used to redefine
2223 the name of any type declared in that scope to refer to the
2224 type to which it already refers.
2226 However, in class scopes, this rule does not apply due to the
2227 stricter language in [class.mem] prohibiting redeclarations of
2228 members. */
2229 ok = false;
2230 /* There can be two block-scope declarations of the same variable,
2231 so long as they are `extern' declarations. However, there cannot
2232 be two declarations of the same static data member:
2234 [class.mem]
2236 A member shall not be declared twice in the
2237 member-specification. */
2238 else if (VAR_P (target_decl)
2239 && VAR_P (target_bval)
2240 && DECL_EXTERNAL (target_decl) && DECL_EXTERNAL (target_bval)
2241 && !DECL_CLASS_SCOPE_P (target_decl))
2243 duplicate_decls (decl, binding->value, /*newdecl_is_friend=*/false);
2244 ok = false;
2246 else if (TREE_CODE (decl) == NAMESPACE_DECL
2247 && TREE_CODE (bval) == NAMESPACE_DECL
2248 && DECL_NAMESPACE_ALIAS (decl)
2249 && DECL_NAMESPACE_ALIAS (bval)
2250 && ORIGINAL_NAMESPACE (bval) == ORIGINAL_NAMESPACE (decl))
2251 /* [namespace.alias]
2253 In a declarative region, a namespace-alias-definition can be
2254 used to redefine a namespace-alias declared in that declarative
2255 region to refer only to the namespace to which it already
2256 refers. */
2257 ok = false;
2258 else
2260 if (!error_operand_p (bval))
2261 diagnose_name_conflict (decl, bval);
2262 ok = false;
2265 return ok;
2268 /* Diagnose a name conflict between DECL and BVAL. */
2270 static void
2271 diagnose_name_conflict (tree decl, tree bval)
2273 if (TREE_CODE (decl) == TREE_CODE (bval)
2274 && TREE_CODE (decl) != NAMESPACE_DECL
2275 && !DECL_DECLARES_FUNCTION_P (decl)
2276 && (TREE_CODE (decl) != TYPE_DECL
2277 || DECL_ARTIFICIAL (decl) == DECL_ARTIFICIAL (bval))
2278 && CP_DECL_CONTEXT (decl) == CP_DECL_CONTEXT (bval))
2279 error ("redeclaration of %q#D", decl);
2280 else
2281 error ("%q#D conflicts with a previous declaration", decl);
2283 inform (location_of (bval), "previous declaration %q#D", bval);
2286 /* Wrapper for supplement_binding_1. */
2288 static bool
2289 supplement_binding (cxx_binding *binding, tree decl)
2291 bool ret;
2292 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
2293 ret = supplement_binding_1 (binding, decl);
2294 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
2295 return ret;
2298 /* Replace BINDING's current value on its scope's name list with
2299 NEWVAL. */
2301 static void
2302 update_local_overload (cxx_binding *binding, tree newval)
2304 tree *d;
2306 for (d = &binding->scope->names; ; d = &TREE_CHAIN (*d))
2307 if (*d == binding->value)
2309 /* Stitch new list node in. */
2310 *d = tree_cons (NULL_TREE, NULL_TREE, TREE_CHAIN (*d));
2311 break;
2313 else if (TREE_CODE (*d) == TREE_LIST && TREE_VALUE (*d) == binding->value)
2314 break;
2316 TREE_VALUE (*d) = newval;
2319 /* Compares the parameter-type-lists of ONE and TWO and
2320 returns false if they are different. If the DECLs are template
2321 functions, the return types and the template parameter lists are
2322 compared too (DR 565). */
2324 static bool
2325 matching_fn_p (tree one, tree two)
2327 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (one)),
2328 TYPE_ARG_TYPES (TREE_TYPE (two))))
2329 return false;
2331 if (TREE_CODE (one) == TEMPLATE_DECL
2332 && TREE_CODE (two) == TEMPLATE_DECL)
2334 /* Compare template parms. */
2335 if (!comp_template_parms (DECL_TEMPLATE_PARMS (one),
2336 DECL_TEMPLATE_PARMS (two)))
2337 return false;
2339 /* And return type. */
2340 if (!same_type_p (TREE_TYPE (TREE_TYPE (one)),
2341 TREE_TYPE (TREE_TYPE (two))))
2342 return false;
2345 return true;
2348 /* Push DECL into nonclass LEVEL BINDING or SLOT. OLD is the current
2349 binding value (possibly with anticipated builtins stripped).
2350 Diagnose conflicts and return updated decl. */
2352 static tree
2353 update_binding (cp_binding_level *level, cxx_binding *binding, tree *slot,
2354 tree old, tree decl, bool is_friend)
2356 tree to_val = decl;
2357 tree old_type = slot ? MAYBE_STAT_TYPE (*slot) : binding->type;
2358 tree to_type = old_type;
2360 gcc_assert (level->kind == sk_namespace ? !binding
2361 : level->kind != sk_class && !slot);
2362 if (old == error_mark_node)
2363 old = NULL_TREE;
2365 if (TREE_CODE (decl) == TYPE_DECL && DECL_ARTIFICIAL (decl))
2367 tree other = to_type;
2369 if (old && TREE_CODE (old) == TYPE_DECL && DECL_ARTIFICIAL (old))
2370 other = old;
2372 /* Pushing an artificial typedef. See if this matches either
2373 the type slot or the old value slot. */
2374 if (!other)
2376 else if (same_type_p (TREE_TYPE (other), TREE_TYPE (decl)))
2377 /* Two artificial decls to same type. Do nothing. */
2378 return other;
2379 else
2380 goto conflict;
2382 if (old)
2384 /* Slide decl into the type slot, keep old unaltered */
2385 to_type = decl;
2386 to_val = old;
2387 goto done;
2391 if (old && TREE_CODE (old) == TYPE_DECL && DECL_ARTIFICIAL (old))
2393 /* Slide old into the type slot. */
2394 to_type = old;
2395 old = NULL_TREE;
2398 if (DECL_DECLARES_FUNCTION_P (decl))
2400 if (!old)
2402 else if (OVL_P (old))
2404 for (ovl_iterator iter (old); iter; ++iter)
2406 tree fn = *iter;
2408 if (iter.using_p () && matching_fn_p (fn, decl))
2410 /* If a function declaration in namespace scope or
2411 block scope has the same name and the same
2412 parameter-type- list (8.3.5) as a function
2413 introduced by a using-declaration, and the
2414 declarations do not declare the same function,
2415 the program is ill-formed. [namespace.udecl]/14 */
2416 if (tree match = duplicate_decls (decl, fn, is_friend))
2417 return match;
2418 else
2419 /* FIXME: To preserve existing error behavior, we
2420 still push the decl. This might change. */
2421 diagnose_name_conflict (decl, fn);
2425 else
2426 goto conflict;
2428 if (to_type != old_type
2429 && warn_shadow
2430 && MAYBE_CLASS_TYPE_P (TREE_TYPE (to_type))
2431 && !(DECL_IN_SYSTEM_HEADER (decl)
2432 && DECL_IN_SYSTEM_HEADER (to_type)))
2433 warning (OPT_Wshadow, "%q#D hides constructor for %q#D",
2434 decl, to_type);
2436 to_val = ovl_insert (decl, old);
2438 else if (!old)
2440 else if (TREE_CODE (old) != TREE_CODE (decl))
2441 /* Different kinds of decls conflict. */
2442 goto conflict;
2443 else if (TREE_CODE (old) == TYPE_DECL)
2445 if (same_type_p (TREE_TYPE (old), TREE_TYPE (decl)))
2446 /* Two type decls to the same type. Do nothing. */
2447 return old;
2448 else
2449 goto conflict;
2451 else if (TREE_CODE (old) == NAMESPACE_DECL)
2453 /* Two maybe-aliased namespaces. If they're to the same target
2454 namespace, that's ok. */
2455 if (ORIGINAL_NAMESPACE (old) != ORIGINAL_NAMESPACE (decl))
2456 goto conflict;
2458 /* The new one must be an alias at this point. */
2459 gcc_assert (DECL_NAMESPACE_ALIAS (decl));
2460 return old;
2462 else if (TREE_CODE (old) == VAR_DECL)
2464 /* There can be two block-scope declarations of the same
2465 variable, so long as they are `extern' declarations. */
2466 if (!DECL_EXTERNAL (old) || !DECL_EXTERNAL (decl))
2467 goto conflict;
2468 else if (tree match = duplicate_decls (decl, old, false))
2469 return match;
2470 else
2471 goto conflict;
2473 else
2475 conflict:
2476 diagnose_name_conflict (decl, old);
2477 to_val = NULL_TREE;
2480 done:
2481 if (to_val)
2483 if (level->kind != sk_namespace
2484 && !to_type && binding->value && OVL_P (to_val))
2485 update_local_overload (binding, to_val);
2486 else
2488 tree to_add = to_val;
2490 if (level->kind == sk_namespace)
2491 to_add = decl;
2492 else if (to_type == decl)
2493 to_add = decl;
2494 else if (TREE_CODE (to_add) == OVERLOAD)
2495 to_add = build_tree_list (NULL_TREE, to_add);
2497 add_decl_to_level (level, to_add);
2500 if (slot)
2502 if (STAT_HACK_P (*slot))
2504 STAT_TYPE (*slot) = to_type;
2505 STAT_DECL (*slot) = to_val;
2507 else if (to_type)
2508 *slot = stat_hack (to_val, to_type);
2509 else
2510 *slot = to_val;
2512 else
2514 binding->type = to_type;
2515 binding->value = to_val;
2519 return decl;
2522 /* Map of identifiers to extern C functions (or LISTS thereof). */
2524 static GTY(()) hash_map<lang_identifier *, tree> *extern_c_fns;
2526 /* DECL has C linkage. If we have an existing instance, make sure it
2527 has the same exception specification [7.5, 7.6]. If there's no
2528 instance, add DECL to the map. */
2530 static void
2531 check_extern_c_conflict (tree decl)
2533 /* Ignore artificial or system header decls. */
2534 if (DECL_ARTIFICIAL (decl) || DECL_IN_SYSTEM_HEADER (decl))
2535 return;
2537 if (!extern_c_fns)
2538 extern_c_fns = hash_map<lang_identifier *,tree>::create_ggc (127);
2540 bool existed;
2541 tree *slot = &extern_c_fns->get_or_insert (DECL_NAME (decl), &existed);
2542 if (!existed)
2543 *slot = decl;
2544 else
2546 tree old = *slot;
2547 if (TREE_CODE (old) == TREE_LIST)
2548 old = TREE_VALUE (old);
2550 int mismatch = 0;
2551 if (DECL_CONTEXT (old) == DECL_CONTEXT (decl))
2552 ; /* If they're in the same context, we'll have already complained
2553 about a (possible) mismatch, when inserting the decl. */
2554 else if (!decls_match (decl, old))
2555 mismatch = 1;
2556 else if (!comp_except_specs (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (old)),
2557 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl)),
2558 ce_normal))
2559 mismatch = -1;
2560 else if (DECL_ASSEMBLER_NAME_SET_P (old))
2561 SET_DECL_ASSEMBLER_NAME (decl, DECL_ASSEMBLER_NAME (old));
2563 if (mismatch)
2565 pedwarn (input_location, 0,
2566 "declaration of %q#D with C language linkage", decl);
2567 pedwarn (DECL_SOURCE_LOCATION (old), 0,
2568 "conflicts with previous declaration %q#D", old);
2569 if (mismatch < 0)
2570 pedwarn (input_location, 0,
2571 "due to different exception specifications");
2573 else
2574 /* Chain it on for c_linkage_binding's use. */
2575 *slot = tree_cons (NULL_TREE, decl, *slot);
2579 /* Returns a list of C-linkage decls with the name NAME. Used in
2580 c-family/c-pragma.c to implement redefine_extname pragma. */
2582 tree
2583 c_linkage_bindings (tree name)
2585 if (extern_c_fns)
2586 if (tree *slot = extern_c_fns->get (name))
2587 return *slot;
2588 return NULL_TREE;
2591 /* DECL is being declared at a local scope. Emit suitable shadow
2592 warnings. */
2594 static void
2595 check_local_shadow (tree decl)
2597 /* Don't complain about the parms we push and then pop
2598 while tentatively parsing a function declarator. */
2599 if (TREE_CODE (decl) == PARM_DECL && !DECL_CONTEXT (decl))
2600 return;
2602 /* Inline decls shadow nothing. */
2603 if (DECL_FROM_INLINE (decl))
2604 return;
2606 /* External decls are something else. */
2607 if (DECL_EXTERNAL (decl))
2608 return;
2610 tree old = NULL_TREE;
2611 cp_binding_level *old_scope = NULL;
2612 if (cxx_binding *binding = outer_binding (DECL_NAME (decl), NULL, true))
2614 old = binding->value;
2615 old_scope = binding->scope;
2617 while (old && VAR_P (old) && DECL_DEAD_FOR_LOCAL (old))
2618 old = DECL_SHADOWED_FOR_VAR (old);
2620 tree shadowed = NULL_TREE;
2621 if (old
2622 && (TREE_CODE (old) == PARM_DECL
2623 || VAR_P (old)
2624 || (TREE_CODE (old) == TYPE_DECL
2625 && (!DECL_ARTIFICIAL (old)
2626 || TREE_CODE (decl) == TYPE_DECL)))
2627 && (!DECL_ARTIFICIAL (decl)
2628 || DECL_IMPLICIT_TYPEDEF_P (decl)
2629 || (VAR_P (decl) && DECL_ANON_UNION_VAR_P (decl))))
2631 /* DECL shadows a local thing possibly of interest. */
2633 /* Don't complain if it's from an enclosing function. */
2634 if (DECL_CONTEXT (old) == current_function_decl
2635 && TREE_CODE (decl) != PARM_DECL
2636 && TREE_CODE (old) == PARM_DECL)
2638 /* Go to where the parms should be and see if we find
2639 them there. */
2640 cp_binding_level *b = current_binding_level->level_chain;
2642 if (FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
2643 /* Skip the ctor/dtor cleanup level. */
2644 b = b->level_chain;
2646 /* ARM $8.3 */
2647 if (b->kind == sk_function_parms)
2649 error ("declaration of %q#D shadows a parameter", decl);
2650 return;
2654 /* The local structure or class can't use parameters of
2655 the containing function anyway. */
2656 if (DECL_CONTEXT (old) != current_function_decl)
2658 for (cp_binding_level *scope = current_binding_level;
2659 scope != old_scope; scope = scope->level_chain)
2660 if (scope->kind == sk_class
2661 && !LAMBDA_TYPE_P (scope->this_entity))
2662 return;
2664 /* Error if redeclaring a local declared in a
2665 init-statement or in the condition of an if or
2666 switch statement when the new declaration is in the
2667 outermost block of the controlled statement.
2668 Redeclaring a variable from a for or while condition is
2669 detected elsewhere. */
2670 else if (VAR_P (old)
2671 && old_scope == current_binding_level->level_chain
2672 && (old_scope->kind == sk_cond || old_scope->kind == sk_for))
2674 error ("redeclaration of %q#D", decl);
2675 inform (DECL_SOURCE_LOCATION (old),
2676 "%q#D previously declared here", old);
2677 return;
2679 /* C++11:
2680 3.3.3/3: The name declared in an exception-declaration (...)
2681 shall not be redeclared in the outermost block of the handler.
2682 3.3.3/2: A parameter name shall not be redeclared (...) in
2683 the outermost block of any handler associated with a
2684 function-try-block.
2685 3.4.1/15: The function parameter names shall not be redeclared
2686 in the exception-declaration nor in the outermost block of a
2687 handler for the function-try-block. */
2688 else if ((TREE_CODE (old) == VAR_DECL
2689 && old_scope == current_binding_level->level_chain
2690 && old_scope->kind == sk_catch)
2691 || (TREE_CODE (old) == PARM_DECL
2692 && (current_binding_level->kind == sk_catch
2693 || current_binding_level->level_chain->kind == sk_catch)
2694 && in_function_try_handler))
2696 if (permerror (input_location, "redeclaration of %q#D", decl))
2697 inform (DECL_SOURCE_LOCATION (old),
2698 "%q#D previously declared here", old);
2699 return;
2702 /* If '-Wshadow=compatible-local' is specified without other
2703 -Wshadow= flags, we will warn only when the type of the
2704 shadowing variable (DECL) can be converted to that of the
2705 shadowed parameter (OLD_LOCAL). The reason why we only check
2706 if DECL's type can be converted to OLD_LOCAL's type (but not the
2707 other way around) is because when users accidentally shadow a
2708 parameter, more than often they would use the variable
2709 thinking (mistakenly) it's still the parameter. It would be
2710 rare that users would use the variable in the place that
2711 expects the parameter but thinking it's a new decl. */
2713 enum opt_code warning_code;
2714 if (warn_shadow)
2715 warning_code = OPT_Wshadow;
2716 else if (warn_shadow_local)
2717 warning_code = OPT_Wshadow_local;
2718 else if (warn_shadow_compatible_local
2719 && can_convert (TREE_TYPE (old), TREE_TYPE (decl), tf_none))
2720 warning_code = OPT_Wshadow_compatible_local;
2721 else
2722 return;
2724 const char *msg;
2725 if (TREE_CODE (old) == PARM_DECL)
2726 msg = "declaration of %q#D shadows a parameter";
2727 else if (is_capture_proxy (old))
2728 msg = "declaration of %qD shadows a lambda capture";
2729 else
2730 msg = "declaration of %qD shadows a previous local";
2732 if (warning_at (input_location, warning_code, msg, decl))
2734 shadowed = old;
2735 goto inform_shadowed;
2737 return;
2740 if (!warn_shadow)
2741 return;
2743 /* Don't warn for artificial things that are not implicit typedefs. */
2744 if (DECL_ARTIFICIAL (decl) && !DECL_IMPLICIT_TYPEDEF_P (decl))
2745 return;
2747 if (nonlambda_method_basetype ())
2748 if (tree member = lookup_member (current_nonlambda_class_type (),
2749 DECL_NAME (decl), /*protect=*/0,
2750 /*want_type=*/false, tf_warning_or_error))
2752 member = MAYBE_BASELINK_FUNCTIONS (member);
2754 /* Warn if a variable shadows a non-function, or the variable
2755 is a function or a pointer-to-function. */
2756 if (!OVL_P (member)
2757 || TREE_CODE (decl) == FUNCTION_DECL
2758 || TYPE_PTRFN_P (TREE_TYPE (decl))
2759 || TYPE_PTRMEMFUNC_P (TREE_TYPE (decl)))
2761 if (warning_at (input_location, OPT_Wshadow,
2762 "declaration of %qD shadows a member of %qT",
2763 decl, current_nonlambda_class_type ())
2764 && DECL_P (member))
2766 shadowed = member;
2767 goto inform_shadowed;
2770 return;
2773 /* Now look for a namespace shadow. */
2774 old = find_namespace_value (current_namespace, DECL_NAME (decl));
2775 if (old
2776 && (VAR_P (old)
2777 || (TREE_CODE (old) == TYPE_DECL
2778 && (!DECL_ARTIFICIAL (old)
2779 || TREE_CODE (decl) == TYPE_DECL)))
2780 && !instantiating_current_function_p ())
2781 /* XXX shadow warnings in outer-more namespaces */
2783 if (warning_at (input_location, OPT_Wshadow,
2784 "declaration of %qD shadows a global declaration",
2785 decl))
2787 shadowed = old;
2788 goto inform_shadowed;
2790 return;
2793 return;
2795 inform_shadowed:
2796 inform (DECL_SOURCE_LOCATION (shadowed), "shadowed declaration is here");
2799 /* DECL is being pushed inside function CTX. Set its context, if
2800 needed. */
2802 static void
2803 set_decl_context_in_fn (tree ctx, tree decl)
2805 if (!DECL_CONTEXT (decl)
2806 /* A local declaration for a function doesn't constitute
2807 nesting. */
2808 && TREE_CODE (decl) != FUNCTION_DECL
2809 /* A local declaration for an `extern' variable is in the
2810 scope of the current namespace, not the current
2811 function. */
2812 && !(VAR_P (decl) && DECL_EXTERNAL (decl))
2813 /* When parsing the parameter list of a function declarator,
2814 don't set DECL_CONTEXT to an enclosing function. When we
2815 push the PARM_DECLs in order to process the function body,
2816 current_binding_level->this_entity will be set. */
2817 && !(TREE_CODE (decl) == PARM_DECL
2818 && current_binding_level->kind == sk_function_parms
2819 && current_binding_level->this_entity == NULL))
2820 DECL_CONTEXT (decl) = ctx;
2822 /* If this is the declaration for a namespace-scope function,
2823 but the declaration itself is in a local scope, mark the
2824 declaration. */
2825 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (decl))
2826 DECL_LOCAL_FUNCTION_P (decl) = 1;
2829 /* DECL is a local-scope decl with linkage. SHADOWED is true if the
2830 name is already bound at the current level.
2832 [basic.link] If there is a visible declaration of an entity with
2833 linkage having the same name and type, ignoring entities declared
2834 outside the innermost enclosing namespace scope, the block scope
2835 declaration declares that same entity and receives the linkage of
2836 the previous declaration.
2838 Also, make sure that this decl matches any existing external decl
2839 in the enclosing namespace. */
2841 static void
2842 set_local_extern_decl_linkage (tree decl, bool shadowed)
2844 tree ns_value = decl; /* Unique marker. */
2846 if (!shadowed)
2848 tree loc_value = innermost_non_namespace_value (DECL_NAME (decl));
2849 if (!loc_value)
2851 ns_value
2852 = find_namespace_value (current_namespace, DECL_NAME (decl));
2853 loc_value = ns_value;
2855 if (loc_value == error_mark_node)
2856 loc_value = NULL_TREE;
2858 for (ovl_iterator iter (loc_value); iter; ++iter)
2859 if (!iter.hidden_p ()
2860 && (TREE_STATIC (*iter) || DECL_EXTERNAL (*iter))
2861 && decls_match (*iter, decl))
2863 /* The standard only says that the local extern inherits
2864 linkage from the previous decl; in particular, default
2865 args are not shared. Add the decl into a hash table to
2866 make sure only the previous decl in this case is seen
2867 by the middle end. */
2868 struct cxx_int_tree_map *h;
2870 /* We inherit the outer decl's linkage. But we're a
2871 different decl. */
2872 TREE_PUBLIC (decl) = TREE_PUBLIC (*iter);
2874 if (cp_function_chain->extern_decl_map == NULL)
2875 cp_function_chain->extern_decl_map
2876 = hash_table<cxx_int_tree_map_hasher>::create_ggc (20);
2878 h = ggc_alloc<cxx_int_tree_map> ();
2879 h->uid = DECL_UID (decl);
2880 h->to = *iter;
2881 cxx_int_tree_map **loc = cp_function_chain->extern_decl_map
2882 ->find_slot (h, INSERT);
2883 *loc = h;
2884 break;
2888 if (TREE_PUBLIC (decl))
2890 /* DECL is externally visible. Make sure it matches a matching
2891 decl in the namespace scope. We only really need to check
2892 this when inserting the decl, not when we find an existing
2893 match in the current scope. However, in practice we're
2894 going to be inserting a new decl in the majority of cases --
2895 who writes multiple extern decls for the same thing in the
2896 same local scope? Doing it here often avoids a duplicate
2897 namespace lookup. */
2899 /* Avoid repeating a lookup. */
2900 if (ns_value == decl)
2901 ns_value = find_namespace_value (current_namespace, DECL_NAME (decl));
2903 if (ns_value == error_mark_node)
2904 ns_value = NULL_TREE;
2906 for (ovl_iterator iter (ns_value); iter; ++iter)
2908 tree other = *iter;
2910 if (!(TREE_PUBLIC (other) || DECL_EXTERNAL (other)))
2911 ; /* Not externally visible. */
2912 else if (DECL_EXTERN_C_P (decl) && DECL_EXTERN_C_P (other))
2913 ; /* Both are extern "C", we'll check via that mechanism. */
2914 else if (TREE_CODE (other) != TREE_CODE (decl)
2915 || ((VAR_P (decl) || matching_fn_p (other, decl))
2916 && !comptypes (TREE_TYPE (decl), TREE_TYPE (other),
2917 COMPARE_REDECLARATION)))
2919 if (permerror (DECL_SOURCE_LOCATION (decl),
2920 "local external declaration %q#D", decl))
2921 inform (DECL_SOURCE_LOCATION (other),
2922 "does not match previous declaration %q#D", other);
2923 break;
2929 /* Record DECL as belonging to the current lexical scope. Check for
2930 errors (such as an incompatible declaration for the same name
2931 already seen in the same scope). IS_FRIEND is true if DECL is
2932 declared as a friend.
2934 Returns either DECL or an old decl for the same name. If an old
2935 decl is returned, it may have been smashed to agree with what DECL
2936 says. */
2938 static tree
2939 do_pushdecl (tree decl, bool is_friend)
2941 if (decl == error_mark_node)
2942 return error_mark_node;
2944 if (!DECL_TEMPLATE_PARM_P (decl) && current_function_decl)
2945 set_decl_context_in_fn (current_function_decl, decl);
2947 /* The binding level we will be pushing into. During local class
2948 pushing, we want to push to the containing scope. */
2949 cp_binding_level *level = current_binding_level;
2950 while (level->kind == sk_class)
2951 level = level->level_chain;
2953 if (tree name = DECL_NAME (decl))
2955 cxx_binding *binding = NULL; /* Local scope binding. */
2956 tree ns = NULL_TREE; /* Searched namespace. */
2957 tree *slot = NULL; /* Binding slot in namespace. */
2958 tree old = NULL_TREE;
2960 if (level->kind == sk_namespace)
2962 /* We look in the decl's namespace for an existing
2963 declaration, even though we push into the current
2964 namespace. */
2965 ns = (DECL_NAMESPACE_SCOPE_P (decl)
2966 ? CP_DECL_CONTEXT (decl) : current_namespace);
2967 /* Create the binding, if this is current namespace, because
2968 that's where we'll be pushing anyway. */
2969 slot = find_namespace_slot (ns, name, ns == current_namespace);
2970 if (slot)
2971 old = MAYBE_STAT_DECL (*slot);
2973 else
2975 binding = find_local_binding (level, name);
2976 if (binding)
2977 old = binding->value;
2980 if (current_function_decl && VAR_OR_FUNCTION_DECL_P (decl)
2981 && DECL_EXTERNAL (decl))
2982 set_local_extern_decl_linkage (decl, old != NULL_TREE);
2984 if (old == error_mark_node)
2985 old = NULL_TREE;
2987 for (ovl_iterator iter (old); iter; ++iter)
2988 if (iter.using_p ())
2989 ; /* Ignore using decls here. */
2990 else if (tree match = duplicate_decls (decl, *iter, is_friend))
2992 if (match == error_mark_node)
2994 else if (TREE_CODE (match) == TYPE_DECL)
2995 /* The IDENTIFIER will have the type referring to the
2996 now-smashed TYPE_DECL, because ...? Reset it. */
2997 SET_IDENTIFIER_TYPE_VALUE (name, TREE_TYPE (match));
2998 else if (iter.hidden_p () && !DECL_HIDDEN_P (match))
3000 /* Unhiding a previously hidden decl. */
3001 tree head = iter.reveal_node (old);
3002 if (head != old)
3004 if (!ns)
3006 update_local_overload (binding, head);
3007 binding->value = head;
3009 else if (STAT_HACK_P (*slot))
3010 STAT_DECL (*slot) = head;
3011 else
3012 *slot = head;
3014 if (TREE_CODE (match) == FUNCTION_DECL
3015 && DECL_EXTERN_C_P (match))
3016 /* We need to check and register the fn now. */
3017 check_extern_c_conflict (match);
3019 return match;
3022 /* We are pushing a new decl. */
3024 /* Skip a hidden builtin we failed to match already. There can
3025 only be one. */
3026 if (old && anticipated_builtin_p (old))
3027 old = OVL_CHAIN (old);
3029 check_template_shadow (decl);
3031 if (DECL_DECLARES_FUNCTION_P (decl))
3033 check_default_args (decl);
3035 if (is_friend)
3037 if (level->kind != sk_namespace)
3038 /* In a local class, a friend function declaration must
3039 find a matching decl in the innermost non-class scope.
3040 [class.friend/11] */
3041 error ("friend declaration %qD in local class without "
3042 "prior local declaration", decl);
3043 else if (!flag_friend_injection)
3044 /* Hide it from ordinary lookup. */
3045 DECL_ANTICIPATED (decl) = DECL_HIDDEN_FRIEND_P (decl) = true;
3049 if (level->kind != sk_namespace)
3051 check_local_shadow (decl);
3053 if (TREE_CODE (decl) == NAMESPACE_DECL)
3054 /* A local namespace alias. */
3055 set_identifier_type_value (name, NULL_TREE);
3057 if (!binding)
3058 binding = create_local_binding (level, name);
3060 else if (!slot)
3062 ns = current_namespace;
3063 slot = find_namespace_slot (ns, name, true);
3064 /* Update OLD to reflect the namespace we're going to be
3065 pushing into. */
3066 old = MAYBE_STAT_DECL (*slot);
3069 old = update_binding (level, binding, slot, old, decl, is_friend);
3071 if (old != decl)
3072 /* An existing decl matched, use it. */
3073 decl = old;
3074 else if (TREE_CODE (decl) == TYPE_DECL)
3076 tree type = TREE_TYPE (decl);
3078 if (type != error_mark_node)
3080 if (TYPE_NAME (type) != decl)
3081 set_underlying_type (decl);
3083 if (!ns)
3084 set_identifier_type_value_with_scope (name, decl, level);
3085 else
3086 SET_IDENTIFIER_TYPE_VALUE (name, global_type_node);
3089 /* If this is a locally defined typedef in a function that
3090 is not a template instantation, record it to implement
3091 -Wunused-local-typedefs. */
3092 if (!instantiating_current_function_p ())
3093 record_locally_defined_typedef (decl);
3095 else if (VAR_P (decl))
3096 maybe_register_incomplete_var (decl);
3097 else if (TREE_CODE (decl) == FUNCTION_DECL && DECL_EXTERN_C_P (decl))
3098 check_extern_c_conflict (decl);
3100 else
3101 add_decl_to_level (level, decl);
3103 return decl;
3106 /* Record a decl-node X as belonging to the current lexical scope.
3107 It's a friend if IS_FRIEND is true -- which affects exactly where
3108 we push it. */
3110 tree
3111 pushdecl (tree x, bool is_friend)
3113 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3114 tree ret = do_pushdecl (x, is_friend);
3115 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3116 return ret;
3119 /* Enter DECL into the symbol table, if that's appropriate. Returns
3120 DECL, or a modified version thereof. */
3122 tree
3123 maybe_push_decl (tree decl)
3125 tree type = TREE_TYPE (decl);
3127 /* Add this decl to the current binding level, but not if it comes
3128 from another scope, e.g. a static member variable. TEM may equal
3129 DECL or it may be a previous decl of the same name. */
3130 if (decl == error_mark_node
3131 || (TREE_CODE (decl) != PARM_DECL
3132 && DECL_CONTEXT (decl) != NULL_TREE
3133 /* Definitions of namespace members outside their namespace are
3134 possible. */
3135 && !DECL_NAMESPACE_SCOPE_P (decl))
3136 || (TREE_CODE (decl) == TEMPLATE_DECL && !namespace_bindings_p ())
3137 || type == unknown_type_node
3138 /* The declaration of a template specialization does not affect
3139 the functions available for overload resolution, so we do not
3140 call pushdecl. */
3141 || (TREE_CODE (decl) == FUNCTION_DECL
3142 && DECL_TEMPLATE_SPECIALIZATION (decl)))
3143 return decl;
3144 else
3145 return pushdecl (decl);
3148 /* Bind DECL to ID in the current_binding_level, assumed to be a local
3149 binding level. If IS_USING is true, DECL got here through a
3150 using-declaration. */
3152 static void
3153 push_local_binding (tree id, tree decl, bool is_using)
3155 /* Skip over any local classes. This makes sense if we call
3156 push_local_binding with a friend decl of a local class. */
3157 cp_binding_level *b = innermost_nonclass_level ();
3159 gcc_assert (b->kind != sk_namespace);
3160 if (find_local_binding (b, id))
3162 /* Supplement the existing binding. */
3163 if (!supplement_binding (IDENTIFIER_BINDING (id), decl))
3164 /* It didn't work. Something else must be bound at this
3165 level. Do not add DECL to the list of things to pop
3166 later. */
3167 return;
3169 else
3170 /* Create a new binding. */
3171 push_binding (id, decl, b);
3173 if (TREE_CODE (decl) == OVERLOAD || is_using)
3174 /* We must put the OVERLOAD or using into a TREE_LIST since we
3175 cannot use the decl's chain itself. */
3176 decl = build_tree_list (NULL_TREE, decl);
3178 /* And put DECL on the list of things declared by the current
3179 binding level. */
3180 add_decl_to_level (b, decl);
3183 /* Check to see whether or not DECL is a variable that would have been
3184 in scope under the ARM, but is not in scope under the ANSI/ISO
3185 standard. If so, issue an error message. If name lookup would
3186 work in both cases, but return a different result, this function
3187 returns the result of ANSI/ISO lookup. Otherwise, it returns
3188 DECL. */
3190 tree
3191 check_for_out_of_scope_variable (tree decl)
3193 tree shadowed;
3195 /* We only care about out of scope variables. */
3196 if (!(VAR_P (decl) && DECL_DEAD_FOR_LOCAL (decl)))
3197 return decl;
3199 shadowed = DECL_HAS_SHADOWED_FOR_VAR_P (decl)
3200 ? DECL_SHADOWED_FOR_VAR (decl) : NULL_TREE ;
3201 while (shadowed != NULL_TREE && VAR_P (shadowed)
3202 && DECL_DEAD_FOR_LOCAL (shadowed))
3203 shadowed = DECL_HAS_SHADOWED_FOR_VAR_P (shadowed)
3204 ? DECL_SHADOWED_FOR_VAR (shadowed) : NULL_TREE;
3205 if (!shadowed)
3206 shadowed = find_namespace_value (current_namespace, DECL_NAME (decl));
3207 if (shadowed)
3209 if (!DECL_ERROR_REPORTED (decl))
3211 warning (0, "name lookup of %qD changed", DECL_NAME (decl));
3212 warning_at (DECL_SOURCE_LOCATION (shadowed), 0,
3213 " matches this %qD under ISO standard rules",
3214 shadowed);
3215 warning_at (DECL_SOURCE_LOCATION (decl), 0,
3216 " matches this %qD under old rules", decl);
3217 DECL_ERROR_REPORTED (decl) = 1;
3219 return shadowed;
3222 /* If we have already complained about this declaration, there's no
3223 need to do it again. */
3224 if (DECL_ERROR_REPORTED (decl))
3225 return decl;
3227 DECL_ERROR_REPORTED (decl) = 1;
3229 if (TREE_TYPE (decl) == error_mark_node)
3230 return decl;
3232 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
3234 error ("name lookup of %qD changed for ISO %<for%> scoping",
3235 DECL_NAME (decl));
3236 error (" cannot use obsolete binding at %q+D because "
3237 "it has a destructor", decl);
3238 return error_mark_node;
3240 else
3242 permerror (input_location, "name lookup of %qD changed for ISO %<for%> scoping",
3243 DECL_NAME (decl));
3244 if (flag_permissive)
3245 permerror (DECL_SOURCE_LOCATION (decl),
3246 " using obsolete binding at %qD", decl);
3247 else
3249 static bool hint;
3250 if (!hint)
3252 inform (input_location, "(if you use %<-fpermissive%> G++ will accept your code)");
3253 hint = true;
3258 return decl;
3261 /* true means unconditionally make a BLOCK for the next level pushed. */
3263 static bool keep_next_level_flag;
3265 static int binding_depth = 0;
3267 static void
3268 indent (int depth)
3270 int i;
3272 for (i = 0; i < depth * 2; i++)
3273 putc (' ', stderr);
3276 /* Return a string describing the kind of SCOPE we have. */
3277 static const char *
3278 cp_binding_level_descriptor (cp_binding_level *scope)
3280 /* The order of this table must match the "scope_kind"
3281 enumerators. */
3282 static const char* scope_kind_names[] = {
3283 "block-scope",
3284 "cleanup-scope",
3285 "try-scope",
3286 "catch-scope",
3287 "for-scope",
3288 "function-parameter-scope",
3289 "class-scope",
3290 "namespace-scope",
3291 "template-parameter-scope",
3292 "template-explicit-spec-scope"
3294 const scope_kind kind = scope->explicit_spec_p
3295 ? sk_template_spec : scope->kind;
3297 return scope_kind_names[kind];
3300 /* Output a debugging information about SCOPE when performing
3301 ACTION at LINE. */
3302 static void
3303 cp_binding_level_debug (cp_binding_level *scope, int line, const char *action)
3305 const char *desc = cp_binding_level_descriptor (scope);
3306 if (scope->this_entity)
3307 verbatim ("%s %<%s(%E)%> %p %d\n", action, desc,
3308 scope->this_entity, (void *) scope, line);
3309 else
3310 verbatim ("%s %s %p %d\n", action, desc, (void *) scope, line);
3313 /* Return the estimated initial size of the hashtable of a NAMESPACE
3314 scope. */
3316 static inline size_t
3317 namespace_scope_ht_size (tree ns)
3319 tree name = DECL_NAME (ns);
3321 return name == std_identifier
3322 ? NAMESPACE_STD_HT_SIZE
3323 : (name == global_identifier
3324 ? GLOBAL_SCOPE_HT_SIZE
3325 : NAMESPACE_ORDINARY_HT_SIZE);
3328 /* A chain of binding_level structures awaiting reuse. */
3330 static GTY((deletable)) cp_binding_level *free_binding_level;
3332 /* Insert SCOPE as the innermost binding level. */
3334 void
3335 push_binding_level (cp_binding_level *scope)
3337 /* Add it to the front of currently active scopes stack. */
3338 scope->level_chain = current_binding_level;
3339 current_binding_level = scope;
3340 keep_next_level_flag = false;
3342 if (ENABLE_SCOPE_CHECKING)
3344 scope->binding_depth = binding_depth;
3345 indent (binding_depth);
3346 cp_binding_level_debug (scope, LOCATION_LINE (input_location),
3347 "push");
3348 binding_depth++;
3352 /* Create a new KIND scope and make it the top of the active scopes stack.
3353 ENTITY is the scope of the associated C++ entity (namespace, class,
3354 function, C++0x enumeration); it is NULL otherwise. */
3356 cp_binding_level *
3357 begin_scope (scope_kind kind, tree entity)
3359 cp_binding_level *scope;
3361 /* Reuse or create a struct for this binding level. */
3362 if (!ENABLE_SCOPE_CHECKING && free_binding_level)
3364 scope = free_binding_level;
3365 free_binding_level = scope->level_chain;
3366 memset (scope, 0, sizeof (cp_binding_level));
3368 else
3369 scope = ggc_cleared_alloc<cp_binding_level> ();
3371 scope->this_entity = entity;
3372 scope->more_cleanups_ok = true;
3373 switch (kind)
3375 case sk_cleanup:
3376 scope->keep = true;
3377 break;
3379 case sk_template_spec:
3380 scope->explicit_spec_p = true;
3381 kind = sk_template_parms;
3382 /* Fall through. */
3383 case sk_template_parms:
3384 case sk_block:
3385 case sk_try:
3386 case sk_catch:
3387 case sk_for:
3388 case sk_cond:
3389 case sk_class:
3390 case sk_scoped_enum:
3391 case sk_function_parms:
3392 case sk_transaction:
3393 case sk_omp:
3394 scope->keep = keep_next_level_flag;
3395 break;
3397 case sk_namespace:
3398 NAMESPACE_LEVEL (entity) = scope;
3399 break;
3401 default:
3402 /* Should not happen. */
3403 gcc_unreachable ();
3404 break;
3406 scope->kind = kind;
3408 push_binding_level (scope);
3410 return scope;
3413 /* We're about to leave current scope. Pop the top of the stack of
3414 currently active scopes. Return the enclosing scope, now active. */
3416 cp_binding_level *
3417 leave_scope (void)
3419 cp_binding_level *scope = current_binding_level;
3421 if (scope->kind == sk_namespace && class_binding_level)
3422 current_binding_level = class_binding_level;
3424 /* We cannot leave a scope, if there are none left. */
3425 if (NAMESPACE_LEVEL (global_namespace))
3426 gcc_assert (!global_scope_p (scope));
3428 if (ENABLE_SCOPE_CHECKING)
3430 indent (--binding_depth);
3431 cp_binding_level_debug (scope, LOCATION_LINE (input_location),
3432 "leave");
3435 /* Move one nesting level up. */
3436 current_binding_level = scope->level_chain;
3438 /* Namespace-scopes are left most probably temporarily, not
3439 completely; they can be reopened later, e.g. in namespace-extension
3440 or any name binding activity that requires us to resume a
3441 namespace. For classes, we cache some binding levels. For other
3442 scopes, we just make the structure available for reuse. */
3443 if (scope->kind != sk_namespace
3444 && scope->kind != sk_class)
3446 scope->level_chain = free_binding_level;
3447 gcc_assert (!ENABLE_SCOPE_CHECKING
3448 || scope->binding_depth == binding_depth);
3449 free_binding_level = scope;
3452 if (scope->kind == sk_class)
3454 /* Reset DEFINING_CLASS_P to allow for reuse of a
3455 class-defining scope in a non-defining context. */
3456 scope->defining_class_p = 0;
3458 /* Find the innermost enclosing class scope, and reset
3459 CLASS_BINDING_LEVEL appropriately. */
3460 class_binding_level = NULL;
3461 for (scope = current_binding_level; scope; scope = scope->level_chain)
3462 if (scope->kind == sk_class)
3464 class_binding_level = scope;
3465 break;
3469 return current_binding_level;
3472 static void
3473 resume_scope (cp_binding_level* b)
3475 /* Resuming binding levels is meant only for namespaces,
3476 and those cannot nest into classes. */
3477 gcc_assert (!class_binding_level);
3478 /* Also, resuming a non-directly nested namespace is a no-no. */
3479 gcc_assert (b->level_chain == current_binding_level);
3480 current_binding_level = b;
3481 if (ENABLE_SCOPE_CHECKING)
3483 b->binding_depth = binding_depth;
3484 indent (binding_depth);
3485 cp_binding_level_debug (b, LOCATION_LINE (input_location), "resume");
3486 binding_depth++;
3490 /* Return the innermost binding level that is not for a class scope. */
3492 static cp_binding_level *
3493 innermost_nonclass_level (void)
3495 cp_binding_level *b;
3497 b = current_binding_level;
3498 while (b->kind == sk_class)
3499 b = b->level_chain;
3501 return b;
3504 /* We're defining an object of type TYPE. If it needs a cleanup, but
3505 we're not allowed to add any more objects with cleanups to the current
3506 scope, create a new binding level. */
3508 void
3509 maybe_push_cleanup_level (tree type)
3511 if (type != error_mark_node
3512 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
3513 && current_binding_level->more_cleanups_ok == 0)
3515 begin_scope (sk_cleanup, NULL);
3516 current_binding_level->statement_list = push_stmt_list ();
3520 /* Return true if we are in the global binding level. */
3522 bool
3523 global_bindings_p (void)
3525 return global_scope_p (current_binding_level);
3528 /* True if we are currently in a toplevel binding level. This
3529 means either the global binding level or a namespace in a toplevel
3530 binding level. Since there are no non-toplevel namespace levels,
3531 this really means any namespace or template parameter level. We
3532 also include a class whose context is toplevel. */
3534 bool
3535 toplevel_bindings_p (void)
3537 cp_binding_level *b = innermost_nonclass_level ();
3539 return b->kind == sk_namespace || b->kind == sk_template_parms;
3542 /* True if this is a namespace scope, or if we are defining a class
3543 which is itself at namespace scope, or whose enclosing class is
3544 such a class, etc. */
3546 bool
3547 namespace_bindings_p (void)
3549 cp_binding_level *b = innermost_nonclass_level ();
3551 return b->kind == sk_namespace;
3554 /* True if the innermost non-class scope is a block scope. */
3556 bool
3557 local_bindings_p (void)
3559 cp_binding_level *b = innermost_nonclass_level ();
3560 return b->kind < sk_function_parms || b->kind == sk_omp;
3563 /* True if the current level needs to have a BLOCK made. */
3565 bool
3566 kept_level_p (void)
3568 return (current_binding_level->blocks != NULL_TREE
3569 || current_binding_level->keep
3570 || current_binding_level->kind == sk_cleanup
3571 || current_binding_level->names != NULL_TREE
3572 || current_binding_level->using_directives);
3575 /* Returns the kind of the innermost scope. */
3577 scope_kind
3578 innermost_scope_kind (void)
3580 return current_binding_level->kind;
3583 /* Returns true if this scope was created to store template parameters. */
3585 bool
3586 template_parm_scope_p (void)
3588 return innermost_scope_kind () == sk_template_parms;
3591 /* If KEEP is true, make a BLOCK node for the next binding level,
3592 unconditionally. Otherwise, use the normal logic to decide whether
3593 or not to create a BLOCK. */
3595 void
3596 keep_next_level (bool keep)
3598 keep_next_level_flag = keep;
3601 /* Return the list of declarations of the current local scope. */
3603 tree
3604 get_local_decls (void)
3606 gcc_assert (current_binding_level->kind != sk_namespace
3607 && current_binding_level->kind != sk_class);
3608 return current_binding_level->names;
3611 /* Return how many function prototypes we are currently nested inside. */
3614 function_parm_depth (void)
3616 int level = 0;
3617 cp_binding_level *b;
3619 for (b = current_binding_level;
3620 b->kind == sk_function_parms;
3621 b = b->level_chain)
3622 ++level;
3624 return level;
3627 /* For debugging. */
3628 static int no_print_functions = 0;
3629 static int no_print_builtins = 0;
3631 static void
3632 print_binding_level (cp_binding_level* lvl)
3634 tree t;
3635 int i = 0, len;
3636 fprintf (stderr, " blocks=%p", (void *) lvl->blocks);
3637 if (lvl->more_cleanups_ok)
3638 fprintf (stderr, " more-cleanups-ok");
3639 if (lvl->have_cleanups)
3640 fprintf (stderr, " have-cleanups");
3641 fprintf (stderr, "\n");
3642 if (lvl->names)
3644 fprintf (stderr, " names:\t");
3645 /* We can probably fit 3 names to a line? */
3646 for (t = lvl->names; t; t = TREE_CHAIN (t))
3648 if (no_print_functions && (TREE_CODE (t) == FUNCTION_DECL))
3649 continue;
3650 if (no_print_builtins
3651 && (TREE_CODE (t) == TYPE_DECL)
3652 && DECL_IS_BUILTIN (t))
3653 continue;
3655 /* Function decls tend to have longer names. */
3656 if (TREE_CODE (t) == FUNCTION_DECL)
3657 len = 3;
3658 else
3659 len = 2;
3660 i += len;
3661 if (i > 6)
3663 fprintf (stderr, "\n\t");
3664 i = len;
3666 print_node_brief (stderr, "", t, 0);
3667 if (t == error_mark_node)
3668 break;
3670 if (i)
3671 fprintf (stderr, "\n");
3673 if (vec_safe_length (lvl->class_shadowed))
3675 size_t i;
3676 cp_class_binding *b;
3677 fprintf (stderr, " class-shadowed:");
3678 FOR_EACH_VEC_ELT (*lvl->class_shadowed, i, b)
3679 fprintf (stderr, " %s ", IDENTIFIER_POINTER (b->identifier));
3680 fprintf (stderr, "\n");
3682 if (lvl->type_shadowed)
3684 fprintf (stderr, " type-shadowed:");
3685 for (t = lvl->type_shadowed; t; t = TREE_CHAIN (t))
3687 fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
3689 fprintf (stderr, "\n");
3693 DEBUG_FUNCTION void
3694 debug (cp_binding_level &ref)
3696 print_binding_level (&ref);
3699 DEBUG_FUNCTION void
3700 debug (cp_binding_level *ptr)
3702 if (ptr)
3703 debug (*ptr);
3704 else
3705 fprintf (stderr, "<nil>\n");
3709 void
3710 print_other_binding_stack (cp_binding_level *stack)
3712 cp_binding_level *level;
3713 for (level = stack; !global_scope_p (level); level = level->level_chain)
3715 fprintf (stderr, "binding level %p\n", (void *) level);
3716 print_binding_level (level);
3720 void
3721 print_binding_stack (void)
3723 cp_binding_level *b;
3724 fprintf (stderr, "current_binding_level=%p\n"
3725 "class_binding_level=%p\n"
3726 "NAMESPACE_LEVEL (global_namespace)=%p\n",
3727 (void *) current_binding_level, (void *) class_binding_level,
3728 (void *) NAMESPACE_LEVEL (global_namespace));
3729 if (class_binding_level)
3731 for (b = class_binding_level; b; b = b->level_chain)
3732 if (b == current_binding_level)
3733 break;
3734 if (b)
3735 b = class_binding_level;
3736 else
3737 b = current_binding_level;
3739 else
3740 b = current_binding_level;
3741 print_other_binding_stack (b);
3742 fprintf (stderr, "global:\n");
3743 print_binding_level (NAMESPACE_LEVEL (global_namespace));
3746 /* Return the type associated with ID. */
3748 static tree
3749 identifier_type_value_1 (tree id)
3751 /* There is no type with that name, anywhere. */
3752 if (REAL_IDENTIFIER_TYPE_VALUE (id) == NULL_TREE)
3753 return NULL_TREE;
3754 /* This is not the type marker, but the real thing. */
3755 if (REAL_IDENTIFIER_TYPE_VALUE (id) != global_type_node)
3756 return REAL_IDENTIFIER_TYPE_VALUE (id);
3757 /* Have to search for it. It must be on the global level, now.
3758 Ask lookup_name not to return non-types. */
3759 id = lookup_name_real (id, 2, 1, /*block_p=*/true, 0, 0);
3760 if (id)
3761 return TREE_TYPE (id);
3762 return NULL_TREE;
3765 /* Wrapper for identifier_type_value_1. */
3767 tree
3768 identifier_type_value (tree id)
3770 tree ret;
3771 timevar_start (TV_NAME_LOOKUP);
3772 ret = identifier_type_value_1 (id);
3773 timevar_stop (TV_NAME_LOOKUP);
3774 return ret;
3778 /* Return the IDENTIFIER_GLOBAL_VALUE of T, for use in common code, since
3779 the definition of IDENTIFIER_GLOBAL_VALUE is different for C and C++. */
3781 tree
3782 identifier_global_value (tree t)
3784 return IDENTIFIER_GLOBAL_VALUE (t);
3787 /* Push a definition of struct, union or enum tag named ID. into
3788 binding_level B. DECL is a TYPE_DECL for the type. We assume that
3789 the tag ID is not already defined. */
3791 static void
3792 set_identifier_type_value_with_scope (tree id, tree decl, cp_binding_level *b)
3794 tree type;
3796 if (b->kind != sk_namespace)
3798 /* Shadow the marker, not the real thing, so that the marker
3799 gets restored later. */
3800 tree old_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
3801 b->type_shadowed
3802 = tree_cons (id, old_type_value, b->type_shadowed);
3803 type = decl ? TREE_TYPE (decl) : NULL_TREE;
3804 TREE_TYPE (b->type_shadowed) = type;
3806 else
3808 tree *slot = find_namespace_slot (current_namespace, id, true);
3809 gcc_assert (decl);
3810 update_binding (b, NULL, slot, MAYBE_STAT_DECL (*slot), decl, false);
3812 /* Store marker instead of real type. */
3813 type = global_type_node;
3815 SET_IDENTIFIER_TYPE_VALUE (id, type);
3818 /* As set_identifier_type_value_with_scope, but using
3819 current_binding_level. */
3821 void
3822 set_identifier_type_value (tree id, tree decl)
3824 set_identifier_type_value_with_scope (id, decl, current_binding_level);
3827 /* Return the name for the constructor (or destructor) for the
3828 specified class. */
3830 tree
3831 constructor_name (tree type)
3833 tree decl = TYPE_NAME (TYPE_MAIN_VARIANT (type));
3835 return decl ? DECL_NAME (decl) : NULL_TREE;
3838 /* Returns TRUE if NAME is the name for the constructor for TYPE,
3839 which must be a class type. */
3841 bool
3842 constructor_name_p (tree name, tree type)
3844 gcc_assert (MAYBE_CLASS_TYPE_P (type));
3846 /* These don't have names. */
3847 if (TREE_CODE (type) == DECLTYPE_TYPE
3848 || TREE_CODE (type) == TYPEOF_TYPE)
3849 return false;
3851 if (name && name == constructor_name (type))
3852 return true;
3854 return false;
3857 /* Counter used to create anonymous type names. */
3859 static GTY(()) int anon_cnt;
3861 /* Return an IDENTIFIER which can be used as a name for
3862 unnamed structs and unions. */
3864 tree
3865 make_anon_name (void)
3867 char buf[32];
3869 sprintf (buf, anon_aggrname_format (), anon_cnt++);
3870 return get_identifier (buf);
3873 /* This code is practically identical to that for creating
3874 anonymous names, but is just used for lambdas instead. This isn't really
3875 necessary, but it's convenient to avoid treating lambdas like other
3876 unnamed types. */
3878 static GTY(()) int lambda_cnt = 0;
3880 tree
3881 make_lambda_name (void)
3883 char buf[32];
3885 sprintf (buf, LAMBDANAME_FORMAT, lambda_cnt++);
3886 return get_identifier (buf);
3889 /* Insert another USING_DECL into the current binding level, returning
3890 this declaration. If this is a redeclaration, do nothing, and
3891 return NULL_TREE if this not in namespace scope (in namespace
3892 scope, a using decl might extend any previous bindings). */
3894 static tree
3895 push_using_decl_1 (tree scope, tree name)
3897 tree decl;
3899 gcc_assert (TREE_CODE (scope) == NAMESPACE_DECL);
3900 gcc_assert (identifier_p (name));
3901 for (decl = current_binding_level->usings; decl; decl = DECL_CHAIN (decl))
3902 if (USING_DECL_SCOPE (decl) == scope && DECL_NAME (decl) == name)
3903 break;
3904 if (decl)
3905 return namespace_bindings_p () ? decl : NULL_TREE;
3906 decl = build_lang_decl (USING_DECL, name, NULL_TREE);
3907 USING_DECL_SCOPE (decl) = scope;
3908 DECL_CHAIN (decl) = current_binding_level->usings;
3909 current_binding_level->usings = decl;
3910 return decl;
3913 /* Wrapper for push_using_decl_1. */
3915 static tree
3916 push_using_decl (tree scope, tree name)
3918 tree ret;
3919 timevar_start (TV_NAME_LOOKUP);
3920 ret = push_using_decl_1 (scope, name);
3921 timevar_stop (TV_NAME_LOOKUP);
3922 return ret;
3925 /* Same as pushdecl, but define X in binding-level LEVEL. We rely on the
3926 caller to set DECL_CONTEXT properly.
3928 Note that this must only be used when X will be the new innermost
3929 binding for its name, as we tack it onto the front of IDENTIFIER_BINDING
3930 without checking to see if the current IDENTIFIER_BINDING comes from a
3931 closer binding level than LEVEL. */
3933 static tree
3934 do_pushdecl_with_scope (tree x, cp_binding_level *level, bool is_friend)
3936 cp_binding_level *b;
3937 tree function_decl = current_function_decl;
3939 current_function_decl = NULL_TREE;
3940 if (level->kind == sk_class)
3942 b = class_binding_level;
3943 class_binding_level = level;
3944 pushdecl_class_level (x);
3945 class_binding_level = b;
3947 else
3949 b = current_binding_level;
3950 current_binding_level = level;
3951 x = pushdecl (x, is_friend);
3952 current_binding_level = b;
3954 current_function_decl = function_decl;
3955 return x;
3958 /* Inject X into the local scope just before the function parms. */
3960 tree
3961 pushdecl_outermost_localscope (tree x)
3963 cp_binding_level *b = NULL;
3964 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3966 /* Find the scope just inside the function parms. */
3967 for (cp_binding_level *n = current_binding_level;
3968 n->kind != sk_function_parms; n = b->level_chain)
3969 b = n;
3971 tree ret = b ? do_pushdecl_with_scope (x, b, false) : error_mark_node;
3972 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3974 return ret;
3977 /* Check a non-member using-declaration. Return the name and scope
3978 being used, and the USING_DECL, or NULL_TREE on failure. */
3980 static tree
3981 validate_nonmember_using_decl (tree decl, tree scope, tree name)
3983 /* [namespace.udecl]
3984 A using-declaration for a class member shall be a
3985 member-declaration. */
3986 if (TYPE_P (scope))
3988 error ("%qT is not a namespace or unscoped enum", scope);
3989 return NULL_TREE;
3991 else if (scope == error_mark_node)
3992 return NULL_TREE;
3994 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR)
3996 /* 7.3.3/5
3997 A using-declaration shall not name a template-id. */
3998 error ("a using-declaration cannot specify a template-id. "
3999 "Try %<using %D%>", name);
4000 return NULL_TREE;
4003 if (TREE_CODE (decl) == NAMESPACE_DECL)
4005 error ("namespace %qD not allowed in using-declaration", decl);
4006 return NULL_TREE;
4009 if (TREE_CODE (decl) == SCOPE_REF)
4011 /* It's a nested name with template parameter dependent scope.
4012 This can only be using-declaration for class member. */
4013 error ("%qT is not a namespace", TREE_OPERAND (decl, 0));
4014 return NULL_TREE;
4017 decl = OVL_FIRST (decl);
4019 /* Make a USING_DECL. */
4020 tree using_decl = push_using_decl (scope, name);
4022 if (using_decl == NULL_TREE
4023 && at_function_scope_p ()
4024 && VAR_P (decl))
4025 /* C++11 7.3.3/10. */
4026 error ("%qD is already declared in this scope", name);
4028 return using_decl;
4031 /* Process a local-scope or namespace-scope using declaration. SCOPE
4032 is the nominated scope to search for NAME. VALUE_P and TYPE_P
4033 point to the binding for NAME in the current scope and are
4034 updated. */
4036 static void
4037 do_nonmember_using_decl (tree scope, tree name, tree *value_p, tree *type_p)
4039 name_lookup lookup (name, 0);
4041 if (!qualified_namespace_lookup (scope, &lookup))
4043 error ("%qD not declared", name);
4044 return;
4046 else if (TREE_CODE (lookup.value) == TREE_LIST)
4048 error ("reference to %qD is ambiguous", name);
4049 print_candidates (lookup.value);
4050 lookup.value = NULL_TREE;
4053 if (lookup.type && TREE_CODE (lookup.type) == TREE_LIST)
4055 error ("reference to %qD is ambiguous", name);
4056 print_candidates (lookup.type);
4057 lookup.type = NULL_TREE;
4060 tree value = *value_p;
4061 tree type = *type_p;
4063 /* Shift the old and new bindings around so we're comparing class and
4064 enumeration names to each other. */
4065 if (value && DECL_IMPLICIT_TYPEDEF_P (value))
4067 type = value;
4068 value = NULL_TREE;
4071 if (lookup.value && DECL_IMPLICIT_TYPEDEF_P (lookup.value))
4073 lookup.type = lookup.value;
4074 lookup.value = NULL_TREE;
4077 if (lookup.value && lookup.value != value)
4079 /* Check for using functions. */
4080 if (OVL_P (lookup.value) && (!value || OVL_P (value)))
4082 for (lkp_iterator usings (lookup.value); usings; ++usings)
4084 tree new_fn = *usings;
4086 /* [namespace.udecl]
4088 If a function declaration in namespace scope or block
4089 scope has the same name and the same parameter types as a
4090 function introduced by a using declaration the program is
4091 ill-formed. */
4092 bool found = false;
4093 for (ovl_iterator old (value); !found && old; ++old)
4095 tree old_fn = *old;
4097 if (new_fn == old_fn)
4098 /* The function already exists in the current
4099 namespace. */
4100 found = true;
4101 else if (old.using_p ())
4102 continue; /* This is a using decl. */
4103 else if (old.hidden_p () && !DECL_HIDDEN_FRIEND_P (old_fn))
4104 continue; /* This is an anticipated builtin. */
4105 else if (!matching_fn_p (new_fn, old_fn))
4106 continue; /* Parameters do not match. */
4107 else if (decls_match (new_fn, old_fn))
4108 found = true;
4109 else
4111 diagnose_name_conflict (new_fn, old_fn);
4112 found = true;
4116 if (!found)
4117 /* Unlike the overload case we don't drop anticipated
4118 builtins here. They don't cause a problem, and
4119 we'd like to match them with a future
4120 declaration. */
4121 value = ovl_insert (new_fn, value, true);
4124 else if (value
4125 /* Ignore anticipated builtins. */
4126 && !anticipated_builtin_p (value)
4127 && !decls_match (lookup.value, value))
4128 diagnose_name_conflict (lookup.value, value);
4129 else
4130 value = lookup.value;
4133 if (lookup.type && lookup.type != type)
4135 if (type && !decls_match (lookup.type, type))
4136 diagnose_name_conflict (lookup.type, type);
4137 else
4138 type = lookup.type;
4141 /* If bind->value is empty, shift any class or enumeration name back. */
4142 if (!value)
4144 value = type;
4145 type = NULL_TREE;
4148 *value_p = value;
4149 *type_p = type;
4152 /* Returns true if ANCESTOR encloses DESCENDANT, including matching.
4153 Both are namespaces. */
4155 bool
4156 is_nested_namespace (tree ancestor, tree descendant, bool inline_only)
4158 int depth = SCOPE_DEPTH (ancestor);
4160 if (!depth && !inline_only)
4161 /* The global namespace encloses everything. */
4162 return true;
4164 while (SCOPE_DEPTH (descendant) > depth
4165 && (!inline_only || DECL_NAMESPACE_INLINE_P (descendant)))
4166 descendant = CP_DECL_CONTEXT (descendant);
4168 return ancestor == descendant;
4171 /* Returns true if ROOT (a namespace, class, or function) encloses
4172 CHILD. CHILD may be either a class type or a namespace. */
4174 bool
4175 is_ancestor (tree root, tree child)
4177 gcc_assert ((TREE_CODE (root) == NAMESPACE_DECL
4178 || TREE_CODE (root) == FUNCTION_DECL
4179 || CLASS_TYPE_P (root)));
4180 gcc_assert ((TREE_CODE (child) == NAMESPACE_DECL
4181 || CLASS_TYPE_P (child)));
4183 /* The global namespace encloses everything. */
4184 if (root == global_namespace)
4185 return true;
4187 /* Search until we reach namespace scope. */
4188 while (TREE_CODE (child) != NAMESPACE_DECL)
4190 /* If we've reached the ROOT, it encloses CHILD. */
4191 if (root == child)
4192 return true;
4193 /* Go out one level. */
4194 if (TYPE_P (child))
4195 child = TYPE_NAME (child);
4196 child = CP_DECL_CONTEXT (child);
4199 if (TREE_CODE (root) == NAMESPACE_DECL)
4200 return is_nested_namespace (root, child);
4202 return false;
4205 /* Enter the class or namespace scope indicated by T suitable for name
4206 lookup. T can be arbitrary scope, not necessary nested inside the
4207 current scope. Returns a non-null scope to pop iff pop_scope
4208 should be called later to exit this scope. */
4210 tree
4211 push_scope (tree t)
4213 if (TREE_CODE (t) == NAMESPACE_DECL)
4214 push_decl_namespace (t);
4215 else if (CLASS_TYPE_P (t))
4217 if (!at_class_scope_p ()
4218 || !same_type_p (current_class_type, t))
4219 push_nested_class (t);
4220 else
4221 /* T is the same as the current scope. There is therefore no
4222 need to re-enter the scope. Since we are not actually
4223 pushing a new scope, our caller should not call
4224 pop_scope. */
4225 t = NULL_TREE;
4228 return t;
4231 /* Leave scope pushed by push_scope. */
4233 void
4234 pop_scope (tree t)
4236 if (t == NULL_TREE)
4237 return;
4238 if (TREE_CODE (t) == NAMESPACE_DECL)
4239 pop_decl_namespace ();
4240 else if CLASS_TYPE_P (t)
4241 pop_nested_class ();
4244 /* Subroutine of push_inner_scope. */
4246 static void
4247 push_inner_scope_r (tree outer, tree inner)
4249 tree prev;
4251 if (outer == inner
4252 || (TREE_CODE (inner) != NAMESPACE_DECL && !CLASS_TYPE_P (inner)))
4253 return;
4255 prev = CP_DECL_CONTEXT (TREE_CODE (inner) == NAMESPACE_DECL ? inner : TYPE_NAME (inner));
4256 if (outer != prev)
4257 push_inner_scope_r (outer, prev);
4258 if (TREE_CODE (inner) == NAMESPACE_DECL)
4260 cp_binding_level *save_template_parm = 0;
4261 /* Temporary take out template parameter scopes. They are saved
4262 in reversed order in save_template_parm. */
4263 while (current_binding_level->kind == sk_template_parms)
4265 cp_binding_level *b = current_binding_level;
4266 current_binding_level = b->level_chain;
4267 b->level_chain = save_template_parm;
4268 save_template_parm = b;
4271 resume_scope (NAMESPACE_LEVEL (inner));
4272 current_namespace = inner;
4274 /* Restore template parameter scopes. */
4275 while (save_template_parm)
4277 cp_binding_level *b = save_template_parm;
4278 save_template_parm = b->level_chain;
4279 b->level_chain = current_binding_level;
4280 current_binding_level = b;
4283 else
4284 pushclass (inner);
4287 /* Enter the scope INNER from current scope. INNER must be a scope
4288 nested inside current scope. This works with both name lookup and
4289 pushing name into scope. In case a template parameter scope is present,
4290 namespace is pushed under the template parameter scope according to
4291 name lookup rule in 14.6.1/6.
4293 Return the former current scope suitable for pop_inner_scope. */
4295 tree
4296 push_inner_scope (tree inner)
4298 tree outer = current_scope ();
4299 if (!outer)
4300 outer = current_namespace;
4302 push_inner_scope_r (outer, inner);
4303 return outer;
4306 /* Exit the current scope INNER back to scope OUTER. */
4308 void
4309 pop_inner_scope (tree outer, tree inner)
4311 if (outer == inner
4312 || (TREE_CODE (inner) != NAMESPACE_DECL && !CLASS_TYPE_P (inner)))
4313 return;
4315 while (outer != inner)
4317 if (TREE_CODE (inner) == NAMESPACE_DECL)
4319 cp_binding_level *save_template_parm = 0;
4320 /* Temporary take out template parameter scopes. They are saved
4321 in reversed order in save_template_parm. */
4322 while (current_binding_level->kind == sk_template_parms)
4324 cp_binding_level *b = current_binding_level;
4325 current_binding_level = b->level_chain;
4326 b->level_chain = save_template_parm;
4327 save_template_parm = b;
4330 pop_namespace ();
4332 /* Restore template parameter scopes. */
4333 while (save_template_parm)
4335 cp_binding_level *b = save_template_parm;
4336 save_template_parm = b->level_chain;
4337 b->level_chain = current_binding_level;
4338 current_binding_level = b;
4341 else
4342 popclass ();
4344 inner = CP_DECL_CONTEXT (TREE_CODE (inner) == NAMESPACE_DECL ? inner : TYPE_NAME (inner));
4348 /* Do a pushlevel for class declarations. */
4350 void
4351 pushlevel_class (void)
4353 class_binding_level = begin_scope (sk_class, current_class_type);
4356 /* ...and a poplevel for class declarations. */
4358 void
4359 poplevel_class (void)
4361 cp_binding_level *level = class_binding_level;
4362 cp_class_binding *cb;
4363 size_t i;
4364 tree shadowed;
4366 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4367 gcc_assert (level != 0);
4369 /* If we're leaving a toplevel class, cache its binding level. */
4370 if (current_class_depth == 1)
4371 previous_class_level = level;
4372 for (shadowed = level->type_shadowed;
4373 shadowed;
4374 shadowed = TREE_CHAIN (shadowed))
4375 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (shadowed), TREE_VALUE (shadowed));
4377 /* Remove the bindings for all of the class-level declarations. */
4378 if (level->class_shadowed)
4380 FOR_EACH_VEC_ELT (*level->class_shadowed, i, cb)
4382 IDENTIFIER_BINDING (cb->identifier) = cb->base->previous;
4383 cxx_binding_free (cb->base);
4385 ggc_free (level->class_shadowed);
4386 level->class_shadowed = NULL;
4389 /* Now, pop out of the binding level which we created up in the
4390 `pushlevel_class' routine. */
4391 gcc_assert (current_binding_level == level);
4392 leave_scope ();
4393 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4396 /* Set INHERITED_VALUE_BINDING_P on BINDING to true or false, as
4397 appropriate. DECL is the value to which a name has just been
4398 bound. CLASS_TYPE is the class in which the lookup occurred. */
4400 static void
4401 set_inherited_value_binding_p (cxx_binding *binding, tree decl,
4402 tree class_type)
4404 if (binding->value == decl && TREE_CODE (decl) != TREE_LIST)
4406 tree context;
4408 if (TREE_CODE (decl) == OVERLOAD)
4409 context = ovl_scope (decl);
4410 else
4412 gcc_assert (DECL_P (decl));
4413 context = context_for_name_lookup (decl);
4416 if (is_properly_derived_from (class_type, context))
4417 INHERITED_VALUE_BINDING_P (binding) = 1;
4418 else
4419 INHERITED_VALUE_BINDING_P (binding) = 0;
4421 else if (binding->value == decl)
4422 /* We only encounter a TREE_LIST when there is an ambiguity in the
4423 base classes. Such an ambiguity can be overridden by a
4424 definition in this class. */
4425 INHERITED_VALUE_BINDING_P (binding) = 1;
4426 else
4427 INHERITED_VALUE_BINDING_P (binding) = 0;
4430 /* Make the declaration of X appear in CLASS scope. */
4432 bool
4433 pushdecl_class_level (tree x)
4435 bool is_valid = true;
4436 bool subtime;
4438 /* Do nothing if we're adding to an outer lambda closure type,
4439 outer_binding will add it later if it's needed. */
4440 if (current_class_type != class_binding_level->this_entity)
4441 return true;
4443 subtime = timevar_cond_start (TV_NAME_LOOKUP);
4444 /* Get the name of X. */
4445 tree name = OVL_NAME (x);
4447 if (name)
4449 is_valid = push_class_level_binding (name, x);
4450 if (TREE_CODE (x) == TYPE_DECL)
4451 set_identifier_type_value (name, x);
4453 else if (ANON_AGGR_TYPE_P (TREE_TYPE (x)))
4455 /* If X is an anonymous aggregate, all of its members are
4456 treated as if they were members of the class containing the
4457 aggregate, for naming purposes. */
4458 tree f;
4460 for (f = TYPE_FIELDS (TREE_TYPE (x)); f; f = DECL_CHAIN (f))
4462 location_t save_location = input_location;
4463 input_location = DECL_SOURCE_LOCATION (f);
4464 if (!pushdecl_class_level (f))
4465 is_valid = false;
4466 input_location = save_location;
4469 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4470 return is_valid;
4473 /* Return the BINDING (if any) for NAME in SCOPE, which is a class
4474 scope. If the value returned is non-NULL, and the PREVIOUS field
4475 is not set, callers must set the PREVIOUS field explicitly. */
4477 static cxx_binding *
4478 get_class_binding (tree name, cp_binding_level *scope)
4480 tree class_type;
4481 tree type_binding;
4482 tree value_binding;
4483 cxx_binding *binding;
4485 class_type = scope->this_entity;
4487 /* Get the type binding. */
4488 type_binding = lookup_member (class_type, name,
4489 /*protect=*/2, /*want_type=*/true,
4490 tf_warning_or_error);
4491 /* Get the value binding. */
4492 value_binding = lookup_member (class_type, name,
4493 /*protect=*/2, /*want_type=*/false,
4494 tf_warning_or_error);
4496 if (value_binding
4497 && (TREE_CODE (value_binding) == TYPE_DECL
4498 || DECL_CLASS_TEMPLATE_P (value_binding)
4499 || (TREE_CODE (value_binding) == TREE_LIST
4500 && TREE_TYPE (value_binding) == error_mark_node
4501 && (TREE_CODE (TREE_VALUE (value_binding))
4502 == TYPE_DECL))))
4503 /* We found a type binding, even when looking for a non-type
4504 binding. This means that we already processed this binding
4505 above. */
4507 else if (value_binding)
4509 if (TREE_CODE (value_binding) == TREE_LIST
4510 && TREE_TYPE (value_binding) == error_mark_node)
4511 /* NAME is ambiguous. */
4513 else if (BASELINK_P (value_binding))
4514 /* NAME is some overloaded functions. */
4515 value_binding = BASELINK_FUNCTIONS (value_binding);
4518 /* If we found either a type binding or a value binding, create a
4519 new binding object. */
4520 if (type_binding || value_binding)
4522 binding = new_class_binding (name,
4523 value_binding,
4524 type_binding,
4525 scope);
4526 /* This is a class-scope binding, not a block-scope binding. */
4527 LOCAL_BINDING_P (binding) = 0;
4528 set_inherited_value_binding_p (binding, value_binding, class_type);
4530 else
4531 binding = NULL;
4533 return binding;
4536 /* Make the declaration(s) of X appear in CLASS scope under the name
4537 NAME. Returns true if the binding is valid. */
4539 static bool
4540 push_class_level_binding_1 (tree name, tree x)
4542 cxx_binding *binding;
4543 tree decl = x;
4544 bool ok;
4546 /* The class_binding_level will be NULL if x is a template
4547 parameter name in a member template. */
4548 if (!class_binding_level)
4549 return true;
4551 if (name == error_mark_node)
4552 return false;
4554 /* Can happen for an erroneous declaration (c++/60384). */
4555 if (!identifier_p (name))
4557 gcc_assert (errorcount || sorrycount);
4558 return false;
4561 /* Check for invalid member names. But don't worry about a default
4562 argument-scope lambda being pushed after the class is complete. */
4563 gcc_assert (TYPE_BEING_DEFINED (current_class_type)
4564 || LAMBDA_TYPE_P (TREE_TYPE (decl)));
4565 /* Check that we're pushing into the right binding level. */
4566 gcc_assert (current_class_type == class_binding_level->this_entity);
4568 /* We could have been passed a tree list if this is an ambiguous
4569 declaration. If so, pull the declaration out because
4570 check_template_shadow will not handle a TREE_LIST. */
4571 if (TREE_CODE (decl) == TREE_LIST
4572 && TREE_TYPE (decl) == error_mark_node)
4573 decl = TREE_VALUE (decl);
4575 if (!check_template_shadow (decl))
4576 return false;
4578 /* [class.mem]
4580 If T is the name of a class, then each of the following shall
4581 have a name different from T:
4583 -- every static data member of class T;
4585 -- every member of class T that is itself a type;
4587 -- every enumerator of every member of class T that is an
4588 enumerated type;
4590 -- every member of every anonymous union that is a member of
4591 class T.
4593 (Non-static data members were also forbidden to have the same
4594 name as T until TC1.) */
4595 if ((VAR_P (x)
4596 || TREE_CODE (x) == CONST_DECL
4597 || (TREE_CODE (x) == TYPE_DECL
4598 && !DECL_SELF_REFERENCE_P (x))
4599 /* A data member of an anonymous union. */
4600 || (TREE_CODE (x) == FIELD_DECL
4601 && DECL_CONTEXT (x) != current_class_type))
4602 && DECL_NAME (x) == DECL_NAME (TYPE_NAME (current_class_type)))
4604 tree scope = context_for_name_lookup (x);
4605 if (TYPE_P (scope) && same_type_p (scope, current_class_type))
4607 error ("%qD has the same name as the class in which it is "
4608 "declared",
4610 return false;
4614 /* Get the current binding for NAME in this class, if any. */
4615 binding = IDENTIFIER_BINDING (name);
4616 if (!binding || binding->scope != class_binding_level)
4618 binding = get_class_binding (name, class_binding_level);
4619 /* If a new binding was created, put it at the front of the
4620 IDENTIFIER_BINDING list. */
4621 if (binding)
4623 binding->previous = IDENTIFIER_BINDING (name);
4624 IDENTIFIER_BINDING (name) = binding;
4628 /* If there is already a binding, then we may need to update the
4629 current value. */
4630 if (binding && binding->value)
4632 tree bval = binding->value;
4633 tree old_decl = NULL_TREE;
4634 tree target_decl = strip_using_decl (decl);
4635 tree target_bval = strip_using_decl (bval);
4637 if (INHERITED_VALUE_BINDING_P (binding))
4639 /* If the old binding was from a base class, and was for a
4640 tag name, slide it over to make room for the new binding.
4641 The old binding is still visible if explicitly qualified
4642 with a class-key. */
4643 if (TREE_CODE (target_bval) == TYPE_DECL
4644 && DECL_ARTIFICIAL (target_bval)
4645 && !(TREE_CODE (target_decl) == TYPE_DECL
4646 && DECL_ARTIFICIAL (target_decl)))
4648 old_decl = binding->type;
4649 binding->type = bval;
4650 binding->value = NULL_TREE;
4651 INHERITED_VALUE_BINDING_P (binding) = 0;
4653 else
4655 old_decl = bval;
4656 /* Any inherited type declaration is hidden by the type
4657 declaration in the derived class. */
4658 if (TREE_CODE (target_decl) == TYPE_DECL
4659 && DECL_ARTIFICIAL (target_decl))
4660 binding->type = NULL_TREE;
4663 else if (TREE_CODE (target_decl) == OVERLOAD
4664 && OVL_P (target_bval))
4665 old_decl = bval;
4666 else if (TREE_CODE (decl) == USING_DECL
4667 && TREE_CODE (bval) == USING_DECL
4668 && same_type_p (USING_DECL_SCOPE (decl),
4669 USING_DECL_SCOPE (bval)))
4670 /* This is a using redeclaration that will be diagnosed later
4671 in supplement_binding */
4673 else if (TREE_CODE (decl) == USING_DECL
4674 && TREE_CODE (bval) == USING_DECL
4675 && DECL_DEPENDENT_P (decl)
4676 && DECL_DEPENDENT_P (bval))
4677 return true;
4678 else if (TREE_CODE (decl) == USING_DECL
4679 && OVL_P (target_bval))
4680 old_decl = bval;
4681 else if (TREE_CODE (bval) == USING_DECL
4682 && OVL_P (target_decl))
4683 return true;
4685 if (old_decl && binding->scope == class_binding_level)
4687 binding->value = x;
4688 /* It is always safe to clear INHERITED_VALUE_BINDING_P
4689 here. This function is only used to register bindings
4690 from with the class definition itself. */
4691 INHERITED_VALUE_BINDING_P (binding) = 0;
4692 return true;
4696 /* Note that we declared this value so that we can issue an error if
4697 this is an invalid redeclaration of a name already used for some
4698 other purpose. */
4699 note_name_declared_in_class (name, decl);
4701 /* If we didn't replace an existing binding, put the binding on the
4702 stack of bindings for the identifier, and update the shadowed
4703 list. */
4704 if (binding && binding->scope == class_binding_level)
4705 /* Supplement the existing binding. */
4706 ok = supplement_binding (binding, decl);
4707 else
4709 /* Create a new binding. */
4710 push_binding (name, decl, class_binding_level);
4711 ok = true;
4714 return ok;
4717 /* Wrapper for push_class_level_binding_1. */
4719 bool
4720 push_class_level_binding (tree name, tree x)
4722 bool ret;
4723 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4724 ret = push_class_level_binding_1 (name, x);
4725 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4726 return ret;
4729 /* Process "using SCOPE::NAME" in a class scope. Return the
4730 USING_DECL created. */
4732 tree
4733 do_class_using_decl (tree scope, tree name)
4735 if (name == error_mark_node)
4736 return NULL_TREE;
4738 if (!scope || !TYPE_P (scope))
4740 error ("using-declaration for non-member at class scope");
4741 return NULL_TREE;
4744 /* Make sure the name is not invalid */
4745 if (TREE_CODE (name) == BIT_NOT_EXPR)
4747 error ("%<%T::%D%> names destructor", scope, name);
4748 return NULL_TREE;
4751 /* Using T::T declares inheriting ctors, even if T is a typedef. */
4752 if (MAYBE_CLASS_TYPE_P (scope)
4753 && (name == TYPE_IDENTIFIER (scope)
4754 || constructor_name_p (name, scope)))
4756 maybe_warn_cpp0x (CPP0X_INHERITING_CTORS);
4757 name = ctor_identifier;
4758 CLASSTYPE_NON_AGGREGATE (current_class_type) = true;
4761 /* Cannot introduce a constructor name. */
4762 if (constructor_name_p (name, current_class_type))
4764 error ("%<%T::%D%> names constructor in %qT",
4765 scope, name, current_class_type);
4766 return NULL_TREE;
4769 /* From [namespace.udecl]:
4771 A using-declaration used as a member-declaration shall refer to a
4772 member of a base class of the class being defined.
4774 In general, we cannot check this constraint in a template because
4775 we do not know the entire set of base classes of the current
4776 class type. Morover, if SCOPE is dependent, it might match a
4777 non-dependent base. */
4779 tree decl = NULL_TREE;
4780 if (!dependent_scope_p (scope))
4782 base_kind b_kind;
4783 tree binfo = lookup_base (current_class_type, scope, ba_any, &b_kind,
4784 tf_warning_or_error);
4785 if (b_kind < bk_proper_base)
4787 /* If there are dependent bases, scope might resolve at
4788 instantiation time, even if it isn't exactly one of the
4789 dependent bases. */
4790 if (b_kind == bk_same_type || !any_dependent_bases_p ())
4792 error_not_base_type (scope, current_class_type);
4793 return NULL_TREE;
4796 else if (name == ctor_identifier && !binfo_direct_p (binfo))
4798 error ("cannot inherit constructors from indirect base %qT", scope);
4799 return NULL_TREE;
4801 else if (!IDENTIFIER_CONV_OP_P (name)
4802 || !dependent_type_p (TREE_TYPE (name)))
4804 decl = lookup_member (binfo, name, 0, false, tf_warning_or_error);
4805 if (!decl)
4807 error ("no members matching %<%T::%D%> in %q#T", scope, name,
4808 scope);
4809 return NULL_TREE;
4812 /* The binfo from which the functions came does not matter. */
4813 if (BASELINK_P (decl))
4814 decl = BASELINK_FUNCTIONS (decl);
4818 tree value = build_lang_decl (USING_DECL, name, NULL_TREE);
4819 USING_DECL_DECLS (value) = decl;
4820 USING_DECL_SCOPE (value) = scope;
4821 DECL_DEPENDENT_P (value) = !decl;
4823 return value;
4827 /* Return the binding for NAME in NS. If NS is NULL, look in
4828 global_namespace. */
4830 tree
4831 get_namespace_binding (tree ns, tree name)
4833 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4834 if (!ns)
4835 ns = global_namespace;
4836 gcc_checking_assert (!DECL_NAMESPACE_ALIAS (ns));
4837 tree ret = find_namespace_value (ns, name);
4838 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4839 return ret;
4842 /* Set value binding of NAME in the global namespace to VAL. Does not
4843 add it to the list of things in the namespace. */
4845 void
4846 set_global_binding (tree name, tree val)
4848 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4850 tree *slot = find_namespace_slot (global_namespace, name, true);
4851 tree old = MAYBE_STAT_DECL (*slot);
4853 if (!old)
4854 *slot = val;
4855 else if (old == val)
4857 else if (!STAT_HACK_P (*slot)
4858 && TREE_CODE (val) == TYPE_DECL && DECL_ARTIFICIAL (val))
4859 *slot = stat_hack (old, val);
4860 else if (!STAT_HACK_P (*slot)
4861 && TREE_CODE (old) == TYPE_DECL && DECL_ARTIFICIAL (old))
4862 *slot = stat_hack (val, old);
4863 else
4864 /* The user's placed something in the implementor's
4865 namespace. */
4866 diagnose_name_conflict (val, old);
4868 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4871 /* Set the context of a declaration to scope. Complain if we are not
4872 outside scope. */
4874 void
4875 set_decl_namespace (tree decl, tree scope, bool friendp)
4877 /* Get rid of namespace aliases. */
4878 scope = ORIGINAL_NAMESPACE (scope);
4880 /* It is ok for friends to be qualified in parallel space. */
4881 if (!friendp && !is_nested_namespace (current_namespace, scope))
4882 error ("declaration of %qD not in a namespace surrounding %qD",
4883 decl, scope);
4884 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
4886 /* See whether this has been declared in the namespace or inline
4887 children. */
4888 tree old = NULL_TREE;
4890 name_lookup lookup (DECL_NAME (decl), LOOKUP_HIDDEN);
4891 if (!lookup.search_qualified (scope, /*usings=*/false))
4892 /* No old declaration at all. */
4893 goto not_found;
4894 old = lookup.value;
4897 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
4898 if (TREE_CODE (old) == TREE_LIST)
4900 ambiguous:
4901 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
4902 error ("reference to %qD is ambiguous", decl);
4903 print_candidates (old);
4904 return;
4907 if (!DECL_DECLARES_FUNCTION_P (decl))
4909 /* Don't compare non-function decls with decls_match here, since
4910 it can't check for the correct constness at this
4911 point. pushdecl will find those errors later. */
4913 /* We might have found it in an inline namespace child of SCOPE. */
4914 if (TREE_CODE (decl) == TREE_CODE (old))
4915 DECL_CONTEXT (decl) = DECL_CONTEXT (old);
4917 found:
4918 /* Writing "N::i" to declare something directly in "N" is invalid. */
4919 if (CP_DECL_CONTEXT (decl) == current_namespace
4920 && at_namespace_scope_p ())
4921 error ("explicit qualification in declaration of %qD", decl);
4922 return;
4925 /* Since decl is a function, old should contain a function decl. */
4926 if (!OVL_P (old))
4927 goto not_found;
4929 /* We handle these in check_explicit_instantiation_namespace. */
4930 if (processing_explicit_instantiation)
4931 return;
4932 if (processing_template_decl || processing_specialization)
4933 /* We have not yet called push_template_decl to turn a
4934 FUNCTION_DECL into a TEMPLATE_DECL, so the declarations won't
4935 match. But, we'll check later, when we construct the
4936 template. */
4937 return;
4938 /* Instantiations or specializations of templates may be declared as
4939 friends in any namespace. */
4940 if (friendp && DECL_USE_TEMPLATE (decl))
4941 return;
4943 tree found;
4944 found = NULL_TREE;
4946 for (lkp_iterator iter (old); iter; ++iter)
4948 if (iter.using_p ())
4949 continue;
4951 tree ofn = *iter;
4953 /* Adjust DECL_CONTEXT first so decls_match will return true
4954 if DECL will match a declaration in an inline namespace. */
4955 DECL_CONTEXT (decl) = DECL_CONTEXT (ofn);
4956 if (decls_match (decl, ofn))
4958 if (found)
4960 /* We found more than one matching declaration. */
4961 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
4962 goto ambiguous;
4964 found = ofn;
4968 if (found)
4970 if (DECL_HIDDEN_FRIEND_P (found))
4972 pedwarn (DECL_SOURCE_LOCATION (decl), 0,
4973 "%qD has not been declared within %qD", decl, scope);
4974 inform (DECL_SOURCE_LOCATION (found),
4975 "only here as a %<friend%>");
4977 DECL_CONTEXT (decl) = DECL_CONTEXT (found);
4978 goto found;
4981 not_found:
4982 /* It didn't work, go back to the explicit scope. */
4983 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
4984 error ("%qD should have been declared inside %qD", decl, scope);
4987 /* Return the namespace where the current declaration is declared. */
4989 tree
4990 current_decl_namespace (void)
4992 tree result;
4993 /* If we have been pushed into a different namespace, use it. */
4994 if (!vec_safe_is_empty (decl_namespace_list))
4995 return decl_namespace_list->last ();
4997 if (current_class_type)
4998 result = decl_namespace_context (current_class_type);
4999 else if (current_function_decl)
5000 result = decl_namespace_context (current_function_decl);
5001 else
5002 result = current_namespace;
5003 return result;
5006 /* Process any ATTRIBUTES on a namespace definition. Returns true if
5007 attribute visibility is seen. */
5009 bool
5010 handle_namespace_attrs (tree ns, tree attributes)
5012 tree d;
5013 bool saw_vis = false;
5015 for (d = attributes; d; d = TREE_CHAIN (d))
5017 tree name = get_attribute_name (d);
5018 tree args = TREE_VALUE (d);
5020 if (is_attribute_p ("visibility", name))
5022 /* attribute visibility is a property of the syntactic block
5023 rather than the namespace as a whole, so we don't touch the
5024 NAMESPACE_DECL at all. */
5025 tree x = args ? TREE_VALUE (args) : NULL_TREE;
5026 if (x == NULL_TREE || TREE_CODE (x) != STRING_CST || TREE_CHAIN (args))
5028 warning (OPT_Wattributes,
5029 "%qD attribute requires a single NTBS argument",
5030 name);
5031 continue;
5034 if (!TREE_PUBLIC (ns))
5035 warning (OPT_Wattributes,
5036 "%qD attribute is meaningless since members of the "
5037 "anonymous namespace get local symbols", name);
5039 push_visibility (TREE_STRING_POINTER (x), 1);
5040 saw_vis = true;
5042 else if (is_attribute_p ("abi_tag", name))
5044 if (!DECL_NAME (ns))
5046 warning (OPT_Wattributes, "ignoring %qD attribute on anonymous "
5047 "namespace", name);
5048 continue;
5050 if (!DECL_NAMESPACE_INLINE_P (ns))
5052 warning (OPT_Wattributes, "ignoring %qD attribute on non-inline "
5053 "namespace", name);
5054 continue;
5056 if (!args)
5058 tree dn = DECL_NAME (ns);
5059 args = build_string (IDENTIFIER_LENGTH (dn) + 1,
5060 IDENTIFIER_POINTER (dn));
5061 TREE_TYPE (args) = char_array_type_node;
5062 args = fix_string_type (args);
5063 args = build_tree_list (NULL_TREE, args);
5065 if (check_abi_tag_args (args, name))
5066 DECL_ATTRIBUTES (ns) = tree_cons (name, args,
5067 DECL_ATTRIBUTES (ns));
5069 else
5071 warning (OPT_Wattributes, "%qD attribute directive ignored",
5072 name);
5073 continue;
5077 return saw_vis;
5080 /* Temporarily set the namespace for the current declaration. */
5082 void
5083 push_decl_namespace (tree decl)
5085 if (TREE_CODE (decl) != NAMESPACE_DECL)
5086 decl = decl_namespace_context (decl);
5087 vec_safe_push (decl_namespace_list, ORIGINAL_NAMESPACE (decl));
5090 /* [namespace.memdef]/2 */
5092 void
5093 pop_decl_namespace (void)
5095 decl_namespace_list->pop ();
5098 /* Process a namespace-alias declaration. */
5100 void
5101 do_namespace_alias (tree alias, tree name_space)
5103 if (name_space == error_mark_node)
5104 return;
5106 gcc_assert (TREE_CODE (name_space) == NAMESPACE_DECL);
5108 name_space = ORIGINAL_NAMESPACE (name_space);
5110 /* Build the alias. */
5111 alias = build_lang_decl (NAMESPACE_DECL, alias, void_type_node);
5112 DECL_NAMESPACE_ALIAS (alias) = name_space;
5113 DECL_EXTERNAL (alias) = 1;
5114 DECL_CONTEXT (alias) = FROB_CONTEXT (current_scope ());
5115 pushdecl (alias);
5117 /* Emit debug info for namespace alias. */
5118 if (!building_stmt_list_p ())
5119 (*debug_hooks->early_global_decl) (alias);
5122 /* Like pushdecl, only it places X in the current namespace,
5123 if appropriate. */
5125 tree
5126 pushdecl_namespace_level (tree x, bool is_friend)
5128 cp_binding_level *b = current_binding_level;
5129 tree t;
5131 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
5132 t = do_pushdecl_with_scope
5133 (x, NAMESPACE_LEVEL (current_namespace), is_friend);
5135 /* Now, the type_shadowed stack may screw us. Munge it so it does
5136 what we want. */
5137 if (TREE_CODE (t) == TYPE_DECL)
5139 tree name = DECL_NAME (t);
5140 tree newval;
5141 tree *ptr = (tree *)0;
5142 for (; !global_scope_p (b); b = b->level_chain)
5144 tree shadowed = b->type_shadowed;
5145 for (; shadowed; shadowed = TREE_CHAIN (shadowed))
5146 if (TREE_PURPOSE (shadowed) == name)
5148 ptr = &TREE_VALUE (shadowed);
5149 /* Can't break out of the loop here because sometimes
5150 a binding level will have duplicate bindings for
5151 PT names. It's gross, but I haven't time to fix it. */
5154 newval = TREE_TYPE (t);
5155 if (ptr == (tree *)0)
5157 /* @@ This shouldn't be needed. My test case "zstring.cc" trips
5158 up here if this is changed to an assertion. --KR */
5159 SET_IDENTIFIER_TYPE_VALUE (name, t);
5161 else
5163 *ptr = newval;
5166 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
5167 return t;
5170 /* Process a using-declaration appearing in namespace scope. */
5172 void
5173 finish_namespace_using_decl (tree decl, tree scope, tree name)
5175 tree orig_decl = decl;
5177 gcc_checking_assert (current_binding_level->kind == sk_namespace
5178 && !processing_template_decl);
5179 decl = validate_nonmember_using_decl (decl, scope, name);
5180 if (decl == NULL_TREE)
5181 return;
5183 tree *slot = find_namespace_slot (current_namespace, name, true);
5184 tree val = slot ? MAYBE_STAT_DECL (*slot) : NULL_TREE;
5185 tree type = slot ? MAYBE_STAT_TYPE (*slot) : NULL_TREE;
5186 do_nonmember_using_decl (scope, name, &val, &type);
5187 if (STAT_HACK_P (*slot))
5189 STAT_DECL (*slot) = val;
5190 STAT_TYPE (*slot) = type;
5192 else if (type)
5193 *slot = stat_hack (val, type);
5194 else
5195 *slot = val;
5197 /* Emit debug info. */
5198 cp_emit_debug_info_for_using (orig_decl, current_namespace);
5201 /* Process a using-declaration at function scope. */
5203 void
5204 finish_local_using_decl (tree decl, tree scope, tree name)
5206 tree orig_decl = decl;
5208 gcc_checking_assert (current_binding_level->kind != sk_class
5209 && current_binding_level->kind != sk_namespace);
5210 decl = validate_nonmember_using_decl (decl, scope, name);
5211 if (decl == NULL_TREE)
5212 return;
5214 add_decl_expr (decl);
5216 cxx_binding *binding = find_local_binding (current_binding_level, name);
5217 tree value = binding ? binding->value : NULL_TREE;
5218 tree type = binding ? binding->type : NULL_TREE;
5220 do_nonmember_using_decl (scope, name, &value, &type);
5222 if (!value)
5224 else if (binding && value == binding->value)
5226 else if (binding && binding->value && TREE_CODE (value) == OVERLOAD)
5228 update_local_overload (IDENTIFIER_BINDING (name), value);
5229 IDENTIFIER_BINDING (name)->value = value;
5231 else
5232 /* Install the new binding. */
5233 push_local_binding (name, value, true);
5235 if (!type)
5237 else if (binding && type == binding->type)
5239 else
5241 push_local_binding (name, type, true);
5242 set_identifier_type_value (name, type);
5245 /* Emit debug info. */
5246 if (!processing_template_decl)
5247 cp_emit_debug_info_for_using (orig_decl, current_scope ());
5250 /* Return the declarations that are members of the namespace NS. */
5252 tree
5253 cp_namespace_decls (tree ns)
5255 return NAMESPACE_LEVEL (ns)->names;
5258 /* Combine prefer_type and namespaces_only into flags. */
5260 static int
5261 lookup_flags (int prefer_type, int namespaces_only)
5263 if (namespaces_only)
5264 return LOOKUP_PREFER_NAMESPACES;
5265 if (prefer_type > 1)
5266 return LOOKUP_PREFER_TYPES;
5267 if (prefer_type > 0)
5268 return LOOKUP_PREFER_BOTH;
5269 return 0;
5272 /* Given a lookup that returned VAL, use FLAGS to decide if we want to
5273 ignore it or not. Subroutine of lookup_name_real and
5274 lookup_type_scope. */
5276 static bool
5277 qualify_lookup (tree val, int flags)
5279 if (val == NULL_TREE)
5280 return false;
5281 if ((flags & LOOKUP_PREFER_NAMESPACES) && TREE_CODE (val) == NAMESPACE_DECL)
5282 return true;
5283 if (flags & LOOKUP_PREFER_TYPES)
5285 tree target_val = strip_using_decl (val);
5286 if (TREE_CODE (target_val) == TYPE_DECL
5287 || TREE_CODE (target_val) == TEMPLATE_DECL)
5288 return true;
5290 if (flags & (LOOKUP_PREFER_NAMESPACES | LOOKUP_PREFER_TYPES))
5291 return false;
5292 /* Look through lambda things that we shouldn't be able to see. */
5293 if (!(flags & LOOKUP_HIDDEN) && is_lambda_ignored_entity (val))
5294 return false;
5295 return true;
5298 /* Suggest alternatives for NAME, an IDENTIFIER_NODE for which name
5299 lookup failed. Search through all available namespaces and print out
5300 possible candidates. If no exact matches are found, and
5301 SUGGEST_MISSPELLINGS is true, then also look for near-matches and
5302 suggest the best near-match, if there is one. */
5304 void
5305 suggest_alternatives_for (location_t location, tree name,
5306 bool suggest_misspellings)
5308 vec<tree> candidates = vNULL;
5309 vec<tree> worklist = vNULL;
5310 unsigned limit = PARAM_VALUE (CXX_MAX_NAMESPACES_FOR_DIAGNOSTIC_HELP);
5311 bool limited = false;
5313 /* Breadth-first search of namespaces. Up to limit namespaces
5314 searched (limit zero == unlimited). */
5315 worklist.safe_push (global_namespace);
5316 for (unsigned ix = 0; ix != worklist.length (); ix++)
5318 tree ns = worklist[ix];
5319 name_lookup lookup (name);
5321 if (lookup.search_qualified (ns, false))
5322 candidates.safe_push (lookup.value);
5324 if (!limited)
5326 /* Look for child namespaces. We have to do this
5327 indirectly because they are chained in reverse order,
5328 which is confusing to the user. */
5329 vec<tree> children = vNULL;
5331 for (tree decl = NAMESPACE_LEVEL (ns)->names;
5332 decl; decl = TREE_CHAIN (decl))
5333 if (TREE_CODE (decl) == NAMESPACE_DECL
5334 && !DECL_NAMESPACE_ALIAS (decl)
5335 && !DECL_NAMESPACE_INLINE_P (decl))
5336 children.safe_push (decl);
5338 while (!limited && !children.is_empty ())
5340 if (worklist.length () == limit)
5342 /* Unconditionally warn that the search was truncated. */
5343 inform (location,
5344 "maximum limit of %d namespaces searched for %qE",
5345 limit, name);
5346 limited = true;
5348 else
5349 worklist.safe_push (children.pop ());
5351 children.release ();
5354 worklist.release ();
5356 if (candidates.length ())
5358 inform_n (location, candidates.length (),
5359 "suggested alternative:",
5360 "suggested alternatives:");
5361 for (unsigned ix = 0; ix != candidates.length (); ix++)
5363 tree val = candidates[ix];
5365 inform (location_of (val), " %qE", val);
5367 candidates.release ();
5369 else if (!suggest_misspellings)
5371 else if (const char *fuzzy = lookup_name_fuzzy (name, FUZZY_LOOKUP_NAME))
5373 /* Show a spelling correction. */
5374 gcc_rich_location richloc (location);
5376 richloc.add_fixit_replace (fuzzy);
5377 inform_at_rich_loc (&richloc, "suggested alternative: %qs", fuzzy);
5381 /* Subroutine of maybe_suggest_missing_header for handling unrecognized names
5382 for some of the most common names within "std::".
5383 Given non-NULL NAME, a name for lookup within "std::", return the header
5384 name defining it within the C++ Standard Library (with '<' and '>'),
5385 or NULL. */
5387 static const char *
5388 get_std_name_hint (const char *name)
5390 struct std_name_hint
5392 const char *name;
5393 const char *header;
5395 static const std_name_hint hints[] = {
5396 /* <array>. */
5397 {"array", "<array>"}, // C++11
5398 /* <deque>. */
5399 {"deque", "<deque>"},
5400 /* <forward_list>. */
5401 {"forward_list", "<forward_list>"}, // C++11
5402 /* <fstream>. */
5403 {"basic_filebuf", "<fstream>"},
5404 {"basic_ifstream", "<fstream>"},
5405 {"basic_ofstream", "<fstream>"},
5406 {"basic_fstream", "<fstream>"},
5407 /* <iostream>. */
5408 {"cin", "<iostream>"},
5409 {"cout", "<iostream>"},
5410 {"cerr", "<iostream>"},
5411 {"clog", "<iostream>"},
5412 {"wcin", "<iostream>"},
5413 {"wcout", "<iostream>"},
5414 {"wclog", "<iostream>"},
5415 /* <list>. */
5416 {"list", "<list>"},
5417 /* <map>. */
5418 {"map", "<map>"},
5419 {"multimap", "<map>"},
5420 /* <queue>. */
5421 {"queue", "<queue>"},
5422 {"priority_queue", "<queue>"},
5423 /* <ostream>. */
5424 {"ostream", "<ostream>"},
5425 {"wostream", "<ostream>"},
5426 {"ends", "<ostream>"},
5427 {"flush", "<ostream>"},
5428 {"endl", "<ostream>"},
5429 /* <set>. */
5430 {"set", "<set>"},
5431 {"multiset", "<set>"},
5432 /* <sstream>. */
5433 {"basic_stringbuf", "<sstream>"},
5434 {"basic_istringstream", "<sstream>"},
5435 {"basic_ostringstream", "<sstream>"},
5436 {"basic_stringstream", "<sstream>"},
5437 /* <stack>. */
5438 {"stack", "<stack>"},
5439 /* <string>. */
5440 {"string", "<string>"},
5441 {"wstring", "<string>"},
5442 {"u16string", "<string>"},
5443 {"u32string", "<string>"},
5444 /* <unordered_map>. */
5445 {"unordered_map", "<unordered_map>"}, // C++11
5446 {"unordered_multimap", "<unordered_map>"}, // C++11
5447 /* <unordered_set>. */
5448 {"unordered_set", "<unordered_set>"}, // C++11
5449 {"unordered_multiset", "<unordered_set>"}, // C++11
5450 /* <vector>. */
5451 {"vector", "<vector>"},
5453 const size_t num_hints = sizeof (hints) / sizeof (hints[0]);
5454 for (size_t i = 0; i < num_hints; i++)
5456 if (0 == strcmp (name, hints[i].name))
5457 return hints[i].header;
5459 return NULL;
5462 /* If SCOPE is the "std" namespace, then suggest pertinent header
5463 files for NAME at LOCATION.
5464 Return true iff a suggestion was offered. */
5466 static bool
5467 maybe_suggest_missing_header (location_t location, tree name, tree scope)
5469 if (scope == NULL_TREE)
5470 return false;
5471 if (TREE_CODE (scope) != NAMESPACE_DECL)
5472 return false;
5473 /* We only offer suggestions for the "std" namespace. */
5474 if (scope != std_node)
5475 return false;
5476 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
5478 const char *name_str = IDENTIFIER_POINTER (name);
5479 const char *header_hint = get_std_name_hint (name_str);
5480 if (!header_hint)
5481 return false;
5483 gcc_rich_location richloc (location);
5484 maybe_add_include_fixit (&richloc, header_hint);
5485 inform_at_rich_loc (&richloc,
5486 "%<std::%s%> is defined in header %qs;"
5487 " did you forget to %<#include %s%>?",
5488 name_str, header_hint, header_hint);
5489 return true;
5492 /* Look for alternatives for NAME, an IDENTIFIER_NODE for which name
5493 lookup failed within the explicitly provided SCOPE. Suggest the
5494 the best meaningful candidates (if any) as a fix-it hint.
5495 Return true iff a suggestion was provided. */
5497 bool
5498 suggest_alternative_in_explicit_scope (location_t location, tree name,
5499 tree scope)
5501 /* Resolve any namespace aliases. */
5502 scope = ORIGINAL_NAMESPACE (scope);
5504 if (maybe_suggest_missing_header (location, name, scope))
5505 return true;
5507 cp_binding_level *level = NAMESPACE_LEVEL (scope);
5509 best_match <tree, const char *> bm (name);
5510 consider_binding_level (name, bm, level, false, FUZZY_LOOKUP_NAME);
5512 /* See if we have a good suggesion for the user. */
5513 const char *fuzzy_name = bm.get_best_meaningful_candidate ();
5514 if (fuzzy_name)
5516 gcc_rich_location richloc (location);
5517 richloc.add_fixit_replace (fuzzy_name);
5518 inform_at_rich_loc (&richloc, "suggested alternative: %qs",
5519 fuzzy_name);
5520 return true;
5523 return false;
5526 /* Look up NAME (an IDENTIFIER_NODE) in SCOPE (either a NAMESPACE_DECL
5527 or a class TYPE).
5529 If PREFER_TYPE is > 0, we only return TYPE_DECLs or namespaces.
5530 If PREFER_TYPE is > 1, we only return TYPE_DECLs.
5532 Returns a DECL (or OVERLOAD, or BASELINK) representing the
5533 declaration found. If no suitable declaration can be found,
5534 ERROR_MARK_NODE is returned. If COMPLAIN is true and SCOPE is
5535 neither a class-type nor a namespace a diagnostic is issued. */
5537 tree
5538 lookup_qualified_name (tree scope, tree name, int prefer_type, bool complain,
5539 bool find_hidden)
5541 tree t = NULL_TREE;
5543 if (TREE_CODE (scope) == NAMESPACE_DECL)
5545 int flags = lookup_flags (prefer_type, /*namespaces_only*/false);
5546 if (find_hidden)
5547 flags |= LOOKUP_HIDDEN;
5548 name_lookup lookup (name, flags);
5550 if (qualified_namespace_lookup (scope, &lookup))
5551 t = lookup.value;
5553 else if (cxx_dialect != cxx98 && TREE_CODE (scope) == ENUMERAL_TYPE)
5554 t = lookup_enumerator (scope, name);
5555 else if (is_class_type (scope, complain))
5556 t = lookup_member (scope, name, 2, prefer_type, tf_warning_or_error);
5558 if (!t)
5559 return error_mark_node;
5560 return t;
5563 /* [namespace.qual]
5564 Accepts the NAME to lookup and its qualifying SCOPE.
5565 Returns the name/type pair found into the cxx_binding *RESULT,
5566 or false on error. */
5568 static bool
5569 qualified_namespace_lookup (tree scope, name_lookup *lookup)
5571 timevar_start (TV_NAME_LOOKUP);
5572 query_oracle (lookup->name);
5573 bool found = lookup->search_qualified (ORIGINAL_NAMESPACE (scope));
5574 timevar_stop (TV_NAME_LOOKUP);
5575 return found;
5578 /* Helper function for lookup_name_fuzzy.
5579 Traverse binding level LVL, looking for good name matches for NAME
5580 (and BM). */
5581 static void
5582 consider_binding_level (tree name, best_match <tree, const char *> &bm,
5583 cp_binding_level *lvl, bool look_within_fields,
5584 enum lookup_name_fuzzy_kind kind)
5586 if (look_within_fields)
5587 if (lvl->this_entity && TREE_CODE (lvl->this_entity) == RECORD_TYPE)
5589 tree type = lvl->this_entity;
5590 bool want_type_p = (kind == FUZZY_LOOKUP_TYPENAME);
5591 tree best_matching_field
5592 = lookup_member_fuzzy (type, name, want_type_p);
5593 if (best_matching_field)
5594 bm.consider (IDENTIFIER_POINTER (best_matching_field));
5597 for (tree t = lvl->names; t; t = TREE_CHAIN (t))
5599 tree d = t;
5601 /* OVERLOADs or decls from using declaration are wrapped into
5602 TREE_LIST. */
5603 if (TREE_CODE (d) == TREE_LIST)
5604 d = OVL_FIRST (TREE_VALUE (d));
5606 /* Don't use bindings from implicitly declared functions,
5607 as they were likely misspellings themselves. */
5608 if (TREE_TYPE (d) == error_mark_node)
5609 continue;
5611 /* Skip anticipated decls of builtin functions. */
5612 if (TREE_CODE (d) == FUNCTION_DECL
5613 && DECL_BUILT_IN (d)
5614 && DECL_ANTICIPATED (d))
5615 continue;
5617 if (tree name = DECL_NAME (d))
5618 /* Ignore internal names with spaces in them. */
5619 if (!strchr (IDENTIFIER_POINTER (name), ' '))
5620 bm.consider (IDENTIFIER_POINTER (name));
5624 /* Search for near-matches for NAME within the current bindings, and within
5625 macro names, returning the best match as a const char *, or NULL if
5626 no reasonable match is found. */
5628 const char *
5629 lookup_name_fuzzy (tree name, enum lookup_name_fuzzy_kind kind)
5631 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
5633 best_match <tree, const char *> bm (name);
5635 cp_binding_level *lvl;
5636 for (lvl = scope_chain->class_bindings; lvl; lvl = lvl->level_chain)
5637 consider_binding_level (name, bm, lvl, true, kind);
5639 for (lvl = current_binding_level; lvl; lvl = lvl->level_chain)
5640 consider_binding_level (name, bm, lvl, false, kind);
5642 /* Consider macros: if the user misspelled a macro name e.g. "SOME_MACRO"
5644 x = SOME_OTHER_MACRO (y);
5645 then "SOME_OTHER_MACRO" will survive to the frontend and show up
5646 as a misspelled identifier.
5648 Use the best distance so far so that a candidate is only set if
5649 a macro is better than anything so far. This allows early rejection
5650 (without calculating the edit distance) of macro names that must have
5651 distance >= bm.get_best_distance (), and means that we only get a
5652 non-NULL result for best_macro_match if it's better than any of
5653 the identifiers already checked. */
5654 best_macro_match bmm (name, bm.get_best_distance (), parse_in);
5655 cpp_hashnode *best_macro = bmm.get_best_meaningful_candidate ();
5656 /* If a macro is the closest so far to NAME, consider it. */
5657 if (best_macro)
5658 bm.consider ((const char *)best_macro->ident.str);
5660 /* Try the "starts_decl_specifier_p" keywords to detect
5661 "singed" vs "signed" typos. */
5662 for (unsigned i = 0; i < num_c_common_reswords; i++)
5664 const c_common_resword *resword = &c_common_reswords[i];
5666 if (kind == FUZZY_LOOKUP_TYPENAME)
5667 if (!cp_keyword_starts_decl_specifier_p (resword->rid))
5668 continue;
5670 tree resword_identifier = ridpointers [resword->rid];
5671 if (!resword_identifier)
5672 continue;
5673 gcc_assert (TREE_CODE (resword_identifier) == IDENTIFIER_NODE);
5675 /* Only consider reserved words that survived the
5676 filtering in init_reswords (e.g. for -std). */
5677 if (!IDENTIFIER_KEYWORD_P (resword_identifier))
5678 continue;
5680 bm.consider (IDENTIFIER_POINTER (resword_identifier));
5683 return bm.get_best_meaningful_candidate ();
5686 /* Subroutine of outer_binding.
5688 Returns TRUE if BINDING is a binding to a template parameter of
5689 SCOPE. In that case SCOPE is the scope of a primary template
5690 parameter -- in the sense of G++, i.e, a template that has its own
5691 template header.
5693 Returns FALSE otherwise. */
5695 static bool
5696 binding_to_template_parms_of_scope_p (cxx_binding *binding,
5697 cp_binding_level *scope)
5699 tree binding_value, tmpl, tinfo;
5700 int level;
5702 if (!binding || !scope || !scope->this_entity)
5703 return false;
5705 binding_value = binding->value ? binding->value : binding->type;
5706 tinfo = get_template_info (scope->this_entity);
5708 /* BINDING_VALUE must be a template parm. */
5709 if (binding_value == NULL_TREE
5710 || (!DECL_P (binding_value)
5711 || !DECL_TEMPLATE_PARM_P (binding_value)))
5712 return false;
5714 /* The level of BINDING_VALUE. */
5715 level =
5716 template_type_parameter_p (binding_value)
5717 ? TEMPLATE_PARM_LEVEL (TEMPLATE_TYPE_PARM_INDEX
5718 (TREE_TYPE (binding_value)))
5719 : TEMPLATE_PARM_LEVEL (DECL_INITIAL (binding_value));
5721 /* The template of the current scope, iff said scope is a primary
5722 template. */
5723 tmpl = (tinfo
5724 && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
5725 ? TI_TEMPLATE (tinfo)
5726 : NULL_TREE);
5728 /* If the level of the parm BINDING_VALUE equals the depth of TMPL,
5729 then BINDING_VALUE is a parameter of TMPL. */
5730 return (tmpl && level == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)));
5733 /* Return the innermost non-namespace binding for NAME from a scope
5734 containing BINDING, or, if BINDING is NULL, the current scope.
5735 Please note that for a given template, the template parameters are
5736 considered to be in the scope containing the current scope.
5737 If CLASS_P is false, then class bindings are ignored. */
5739 cxx_binding *
5740 outer_binding (tree name,
5741 cxx_binding *binding,
5742 bool class_p)
5744 cxx_binding *outer;
5745 cp_binding_level *scope;
5746 cp_binding_level *outer_scope;
5748 if (binding)
5750 scope = binding->scope->level_chain;
5751 outer = binding->previous;
5753 else
5755 scope = current_binding_level;
5756 outer = IDENTIFIER_BINDING (name);
5758 outer_scope = outer ? outer->scope : NULL;
5760 /* Because we create class bindings lazily, we might be missing a
5761 class binding for NAME. If there are any class binding levels
5762 between the LAST_BINDING_LEVEL and the scope in which OUTER was
5763 declared, we must lookup NAME in those class scopes. */
5764 if (class_p)
5765 while (scope && scope != outer_scope && scope->kind != sk_namespace)
5767 if (scope->kind == sk_class)
5769 cxx_binding *class_binding;
5771 class_binding = get_class_binding (name, scope);
5772 if (class_binding)
5774 /* Thread this new class-scope binding onto the
5775 IDENTIFIER_BINDING list so that future lookups
5776 find it quickly. */
5777 class_binding->previous = outer;
5778 if (binding)
5779 binding->previous = class_binding;
5780 else
5781 IDENTIFIER_BINDING (name) = class_binding;
5782 return class_binding;
5785 /* If we are in a member template, the template parms of the member
5786 template are considered to be inside the scope of the containing
5787 class, but within G++ the class bindings are all pushed between the
5788 template parms and the function body. So if the outer binding is
5789 a template parm for the current scope, return it now rather than
5790 look for a class binding. */
5791 if (outer_scope && outer_scope->kind == sk_template_parms
5792 && binding_to_template_parms_of_scope_p (outer, scope))
5793 return outer;
5795 scope = scope->level_chain;
5798 return outer;
5801 /* Return the innermost block-scope or class-scope value binding for
5802 NAME, or NULL_TREE if there is no such binding. */
5804 tree
5805 innermost_non_namespace_value (tree name)
5807 cxx_binding *binding;
5808 binding = outer_binding (name, /*binding=*/NULL, /*class_p=*/true);
5809 return binding ? binding->value : NULL_TREE;
5812 /* Look up NAME in the current binding level and its superiors in the
5813 namespace of variables, functions and typedefs. Return a ..._DECL
5814 node of some kind representing its definition if there is only one
5815 such declaration, or return a TREE_LIST with all the overloaded
5816 definitions if there are many, or return 0 if it is undefined.
5817 Hidden name, either friend declaration or built-in function, are
5818 not ignored.
5820 If PREFER_TYPE is > 0, we prefer TYPE_DECLs or namespaces.
5821 If PREFER_TYPE is > 1, we reject non-type decls (e.g. namespaces).
5822 Otherwise we prefer non-TYPE_DECLs.
5824 If NONCLASS is nonzero, bindings in class scopes are ignored. If
5825 BLOCK_P is false, bindings in block scopes are ignored. */
5827 static tree
5828 lookup_name_real_1 (tree name, int prefer_type, int nonclass, bool block_p,
5829 int namespaces_only, int flags)
5831 cxx_binding *iter;
5832 tree val = NULL_TREE;
5834 query_oracle (name);
5836 /* Conversion operators are handled specially because ordinary
5837 unqualified name lookup will not find template conversion
5838 operators. */
5839 if (IDENTIFIER_CONV_OP_P (name))
5841 cp_binding_level *level;
5843 for (level = current_binding_level;
5844 level && level->kind != sk_namespace;
5845 level = level->level_chain)
5847 tree class_type;
5848 tree operators;
5850 /* A conversion operator can only be declared in a class
5851 scope. */
5852 if (level->kind != sk_class)
5853 continue;
5855 /* Lookup the conversion operator in the class. */
5856 class_type = level->this_entity;
5857 operators = lookup_fnfields (class_type, name, /*protect=*/0);
5858 if (operators)
5859 return operators;
5862 return NULL_TREE;
5865 flags |= lookup_flags (prefer_type, namespaces_only);
5867 /* First, look in non-namespace scopes. */
5869 if (current_class_type == NULL_TREE)
5870 nonclass = 1;
5872 if (block_p || !nonclass)
5873 for (iter = outer_binding (name, NULL, !nonclass);
5874 iter;
5875 iter = outer_binding (name, iter, !nonclass))
5877 tree binding;
5879 /* Skip entities we don't want. */
5880 if (LOCAL_BINDING_P (iter) ? !block_p : nonclass)
5881 continue;
5883 /* If this is the kind of thing we're looking for, we're done. */
5884 if (qualify_lookup (iter->value, flags))
5885 binding = iter->value;
5886 else if ((flags & LOOKUP_PREFER_TYPES)
5887 && qualify_lookup (iter->type, flags))
5888 binding = iter->type;
5889 else
5890 binding = NULL_TREE;
5892 if (binding)
5894 if (TREE_CODE (binding) == TYPE_DECL && DECL_HIDDEN_P (binding))
5896 /* A non namespace-scope binding can only be hidden in the
5897 presence of a local class, due to friend declarations.
5899 In particular, consider:
5901 struct C;
5902 void f() {
5903 struct A {
5904 friend struct B;
5905 friend struct C;
5906 void g() {
5907 B* b; // error: B is hidden
5908 C* c; // OK, finds ::C
5911 B *b; // error: B is hidden
5912 C *c; // OK, finds ::C
5913 struct B {};
5914 B *bb; // OK
5917 The standard says that "B" is a local class in "f"
5918 (but not nested within "A") -- but that name lookup
5919 for "B" does not find this declaration until it is
5920 declared directly with "f".
5922 In particular:
5924 [class.friend]
5926 If a friend declaration appears in a local class and
5927 the name specified is an unqualified name, a prior
5928 declaration is looked up without considering scopes
5929 that are outside the innermost enclosing non-class
5930 scope. For a friend function declaration, if there is
5931 no prior declaration, the program is ill-formed. For a
5932 friend class declaration, if there is no prior
5933 declaration, the class that is specified belongs to the
5934 innermost enclosing non-class scope, but if it is
5935 subsequently referenced, its name is not found by name
5936 lookup until a matching declaration is provided in the
5937 innermost enclosing nonclass scope.
5939 So just keep looking for a non-hidden binding.
5941 gcc_assert (TREE_CODE (binding) == TYPE_DECL);
5942 continue;
5944 val = binding;
5945 break;
5949 /* Now lookup in namespace scopes. */
5950 if (!val)
5952 name_lookup lookup (name, flags);
5953 if (lookup.search_unqualified
5954 (current_decl_namespace (), current_binding_level))
5955 val = lookup.value;
5958 /* If we have a single function from a using decl, pull it out. */
5959 if (val && TREE_CODE (val) == OVERLOAD && !really_overloaded_fn (val))
5960 val = OVL_FUNCTION (val);
5962 return val;
5965 /* Wrapper for lookup_name_real_1. */
5967 tree
5968 lookup_name_real (tree name, int prefer_type, int nonclass, bool block_p,
5969 int namespaces_only, int flags)
5971 tree ret;
5972 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
5973 ret = lookup_name_real_1 (name, prefer_type, nonclass, block_p,
5974 namespaces_only, flags);
5975 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
5976 return ret;
5979 tree
5980 lookup_name_nonclass (tree name)
5982 return lookup_name_real (name, 0, 1, /*block_p=*/true, 0, 0);
5985 tree
5986 lookup_name (tree name)
5988 return lookup_name_real (name, 0, 0, /*block_p=*/true, 0, 0);
5991 tree
5992 lookup_name_prefer_type (tree name, int prefer_type)
5994 return lookup_name_real (name, prefer_type, 0, /*block_p=*/true, 0, 0);
5997 /* Look up NAME for type used in elaborated name specifier in
5998 the scopes given by SCOPE. SCOPE can be either TS_CURRENT or
5999 TS_WITHIN_ENCLOSING_NON_CLASS. Although not implied by the
6000 name, more scopes are checked if cleanup or template parameter
6001 scope is encountered.
6003 Unlike lookup_name_real, we make sure that NAME is actually
6004 declared in the desired scope, not from inheritance, nor using
6005 directive. For using declaration, there is DR138 still waiting
6006 to be resolved. Hidden name coming from an earlier friend
6007 declaration is also returned.
6009 A TYPE_DECL best matching the NAME is returned. Catching error
6010 and issuing diagnostics are caller's responsibility. */
6012 static tree
6013 lookup_type_scope_1 (tree name, tag_scope scope)
6015 cxx_binding *iter = NULL;
6016 tree val = NULL_TREE;
6017 cp_binding_level *level = NULL;
6019 /* Look in non-namespace scope first. */
6020 if (current_binding_level->kind != sk_namespace)
6021 iter = outer_binding (name, NULL, /*class_p=*/ true);
6022 for (; iter; iter = outer_binding (name, iter, /*class_p=*/ true))
6024 /* Check if this is the kind of thing we're looking for.
6025 If SCOPE is TS_CURRENT, also make sure it doesn't come from
6026 base class. For ITER->VALUE, we can simply use
6027 INHERITED_VALUE_BINDING_P. For ITER->TYPE, we have to use
6028 our own check.
6030 We check ITER->TYPE before ITER->VALUE in order to handle
6031 typedef struct C {} C;
6032 correctly. */
6034 if (qualify_lookup (iter->type, LOOKUP_PREFER_TYPES)
6035 && (scope != ts_current
6036 || LOCAL_BINDING_P (iter)
6037 || DECL_CONTEXT (iter->type) == iter->scope->this_entity))
6038 val = iter->type;
6039 else if ((scope != ts_current
6040 || !INHERITED_VALUE_BINDING_P (iter))
6041 && qualify_lookup (iter->value, LOOKUP_PREFER_TYPES))
6042 val = iter->value;
6044 if (val)
6045 break;
6048 /* Look in namespace scope. */
6049 if (val)
6050 level = iter->scope;
6051 else
6053 tree ns = current_decl_namespace ();
6055 if (tree *slot = find_namespace_slot (ns, name))
6057 /* If this is the kind of thing we're looking for, we're done. */
6058 if (tree type = MAYBE_STAT_TYPE (*slot))
6059 if (qualify_lookup (type, LOOKUP_PREFER_TYPES))
6060 val = type;
6061 if (!val)
6063 if (tree decl = MAYBE_STAT_DECL (*slot))
6064 if (qualify_lookup (decl, LOOKUP_PREFER_TYPES))
6065 val = decl;
6067 level = NAMESPACE_LEVEL (ns);
6071 /* Type found, check if it is in the allowed scopes, ignoring cleanup
6072 and template parameter scopes. */
6073 if (val)
6075 cp_binding_level *b = current_binding_level;
6076 while (b)
6078 if (level == b)
6079 return val;
6081 if (b->kind == sk_cleanup || b->kind == sk_template_parms
6082 || b->kind == sk_function_parms)
6083 b = b->level_chain;
6084 else if (b->kind == sk_class
6085 && scope == ts_within_enclosing_non_class)
6086 b = b->level_chain;
6087 else
6088 break;
6092 return NULL_TREE;
6095 /* Wrapper for lookup_type_scope_1. */
6097 tree
6098 lookup_type_scope (tree name, tag_scope scope)
6100 tree ret;
6101 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6102 ret = lookup_type_scope_1 (name, scope);
6103 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6104 return ret;
6107 /* Returns true iff DECL is a block-scope extern declaration of a function
6108 or variable. */
6110 bool
6111 is_local_extern (tree decl)
6113 cxx_binding *binding;
6115 /* For functions, this is easy. */
6116 if (TREE_CODE (decl) == FUNCTION_DECL)
6117 return DECL_LOCAL_FUNCTION_P (decl);
6119 if (!VAR_P (decl))
6120 return false;
6121 if (!current_function_decl)
6122 return false;
6124 /* For variables, this is not easy. We need to look at the binding stack
6125 for the identifier to see whether the decl we have is a local. */
6126 for (binding = IDENTIFIER_BINDING (DECL_NAME (decl));
6127 binding && binding->scope->kind != sk_namespace;
6128 binding = binding->previous)
6129 if (binding->value == decl)
6130 return LOCAL_BINDING_P (binding);
6132 return false;
6135 /* The type TYPE is being declared. If it is a class template, or a
6136 specialization of a class template, do any processing required and
6137 perform error-checking. If IS_FRIEND is nonzero, this TYPE is
6138 being declared a friend. B is the binding level at which this TYPE
6139 should be bound.
6141 Returns the TYPE_DECL for TYPE, which may have been altered by this
6142 processing. */
6144 static tree
6145 maybe_process_template_type_declaration (tree type, int is_friend,
6146 cp_binding_level *b)
6148 tree decl = TYPE_NAME (type);
6150 if (processing_template_parmlist)
6151 /* You can't declare a new template type in a template parameter
6152 list. But, you can declare a non-template type:
6154 template <class A*> struct S;
6156 is a forward-declaration of `A'. */
6158 else if (b->kind == sk_namespace
6159 && current_binding_level->kind != sk_namespace)
6160 /* If this new type is being injected into a containing scope,
6161 then it's not a template type. */
6163 else
6165 gcc_assert (MAYBE_CLASS_TYPE_P (type)
6166 || TREE_CODE (type) == ENUMERAL_TYPE);
6168 if (processing_template_decl)
6170 /* This may change after the call to
6171 push_template_decl_real, but we want the original value. */
6172 tree name = DECL_NAME (decl);
6174 decl = push_template_decl_real (decl, is_friend);
6175 if (decl == error_mark_node)
6176 return error_mark_node;
6178 /* If the current binding level is the binding level for the
6179 template parameters (see the comment in
6180 begin_template_parm_list) and the enclosing level is a class
6181 scope, and we're not looking at a friend, push the
6182 declaration of the member class into the class scope. In the
6183 friend case, push_template_decl will already have put the
6184 friend into global scope, if appropriate. */
6185 if (TREE_CODE (type) != ENUMERAL_TYPE
6186 && !is_friend && b->kind == sk_template_parms
6187 && b->level_chain->kind == sk_class)
6189 finish_member_declaration (CLASSTYPE_TI_TEMPLATE (type));
6191 if (!COMPLETE_TYPE_P (current_class_type))
6193 maybe_add_class_template_decl_list (current_class_type,
6194 type, /*friend_p=*/0);
6195 /* Put this UTD in the table of UTDs for the class. */
6196 if (CLASSTYPE_NESTED_UTDS (current_class_type) == NULL)
6197 CLASSTYPE_NESTED_UTDS (current_class_type) =
6198 binding_table_new (SCOPE_DEFAULT_HT_SIZE);
6200 binding_table_insert
6201 (CLASSTYPE_NESTED_UTDS (current_class_type), name, type);
6207 return decl;
6210 /* Push a tag name NAME for struct/class/union/enum type TYPE. In case
6211 that the NAME is a class template, the tag is processed but not pushed.
6213 The pushed scope depend on the SCOPE parameter:
6214 - When SCOPE is TS_CURRENT, put it into the inner-most non-sk_cleanup
6215 scope.
6216 - When SCOPE is TS_GLOBAL, put it in the inner-most non-class and
6217 non-template-parameter scope. This case is needed for forward
6218 declarations.
6219 - When SCOPE is TS_WITHIN_ENCLOSING_NON_CLASS, this is similar to
6220 TS_GLOBAL case except that names within template-parameter scopes
6221 are not pushed at all.
6223 Returns TYPE upon success and ERROR_MARK_NODE otherwise. */
6225 static tree
6226 do_pushtag (tree name, tree type, tag_scope scope)
6228 tree decl;
6230 cp_binding_level *b = current_binding_level;
6231 while (/* Cleanup scopes are not scopes from the point of view of
6232 the language. */
6233 b->kind == sk_cleanup
6234 /* Neither are function parameter scopes. */
6235 || b->kind == sk_function_parms
6236 /* Neither are the scopes used to hold template parameters
6237 for an explicit specialization. For an ordinary template
6238 declaration, these scopes are not scopes from the point of
6239 view of the language. */
6240 || (b->kind == sk_template_parms
6241 && (b->explicit_spec_p || scope == ts_global))
6242 /* Pushing into a class is ok for lambdas or when we want current */
6243 || (b->kind == sk_class
6244 && scope != ts_lambda
6245 && (scope != ts_current
6246 /* We may be defining a new type in the initializer
6247 of a static member variable. We allow this when
6248 not pedantic, and it is particularly useful for
6249 type punning via an anonymous union. */
6250 || COMPLETE_TYPE_P (b->this_entity))))
6251 b = b->level_chain;
6253 gcc_assert (identifier_p (name));
6255 /* Do C++ gratuitous typedefing. */
6256 if (identifier_type_value_1 (name) != type)
6258 tree tdef;
6259 int in_class = 0;
6260 tree context = TYPE_CONTEXT (type);
6262 if (! context)
6264 tree cs = current_scope ();
6266 if (scope == ts_current
6267 || scope == ts_lambda
6268 || (cs && TREE_CODE (cs) == FUNCTION_DECL))
6269 context = cs;
6270 else if (cs && TYPE_P (cs))
6271 /* When declaring a friend class of a local class, we want
6272 to inject the newly named class into the scope
6273 containing the local class, not the namespace
6274 scope. */
6275 context = decl_function_context (get_type_decl (cs));
6277 if (!context)
6278 context = current_namespace;
6280 if (b->kind == sk_class
6281 || (b->kind == sk_template_parms
6282 && b->level_chain->kind == sk_class))
6283 in_class = 1;
6285 tdef = create_implicit_typedef (name, type);
6286 DECL_CONTEXT (tdef) = FROB_CONTEXT (context);
6287 if (scope == ts_within_enclosing_non_class)
6289 /* This is a friend. Make this TYPE_DECL node hidden from
6290 ordinary name lookup. Its corresponding TEMPLATE_DECL
6291 will be marked in push_template_decl_real. */
6292 retrofit_lang_decl (tdef);
6293 DECL_ANTICIPATED (tdef) = 1;
6294 DECL_FRIEND_P (tdef) = 1;
6297 decl = maybe_process_template_type_declaration
6298 (type, scope == ts_within_enclosing_non_class, b);
6299 if (decl == error_mark_node)
6300 return decl;
6302 if (b->kind == sk_class)
6304 if (!TYPE_BEING_DEFINED (current_class_type)
6305 && scope != ts_lambda)
6306 return error_mark_node;
6308 if (!PROCESSING_REAL_TEMPLATE_DECL_P ())
6309 /* Put this TYPE_DECL on the TYPE_FIELDS list for the
6310 class. But if it's a member template class, we want
6311 the TEMPLATE_DECL, not the TYPE_DECL, so this is done
6312 later. */
6313 finish_member_declaration (decl);
6314 else
6315 pushdecl_class_level (decl);
6317 else if (b->kind != sk_template_parms)
6319 decl = do_pushdecl_with_scope (decl, b, /*is_friend=*/false);
6320 if (decl == error_mark_node)
6321 return decl;
6323 if (DECL_CONTEXT (decl) == std_node
6324 && init_list_identifier == DECL_NAME (TYPE_NAME (type))
6325 && !CLASSTYPE_TEMPLATE_INFO (type))
6327 error ("declaration of std::initializer_list does not match "
6328 "#include <initializer_list>, isn't a template");
6329 return error_mark_node;
6333 if (! in_class)
6334 set_identifier_type_value_with_scope (name, tdef, b);
6336 TYPE_CONTEXT (type) = DECL_CONTEXT (decl);
6338 /* If this is a local class, keep track of it. We need this
6339 information for name-mangling, and so that it is possible to
6340 find all function definitions in a translation unit in a
6341 convenient way. (It's otherwise tricky to find a member
6342 function definition it's only pointed to from within a local
6343 class.) */
6344 if (TYPE_FUNCTION_SCOPE_P (type))
6346 if (processing_template_decl)
6348 /* Push a DECL_EXPR so we call pushtag at the right time in
6349 template instantiation rather than in some nested context. */
6350 add_decl_expr (decl);
6352 else
6353 vec_safe_push (local_classes, type);
6357 if (b->kind == sk_class
6358 && !COMPLETE_TYPE_P (current_class_type))
6360 maybe_add_class_template_decl_list (current_class_type,
6361 type, /*friend_p=*/0);
6363 if (CLASSTYPE_NESTED_UTDS (current_class_type) == NULL)
6364 CLASSTYPE_NESTED_UTDS (current_class_type)
6365 = binding_table_new (SCOPE_DEFAULT_HT_SIZE);
6367 binding_table_insert
6368 (CLASSTYPE_NESTED_UTDS (current_class_type), name, type);
6371 decl = TYPE_NAME (type);
6372 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
6374 /* Set type visibility now if this is a forward declaration. */
6375 TREE_PUBLIC (decl) = 1;
6376 determine_visibility (decl);
6378 return type;
6381 /* Wrapper for do_pushtag. */
6383 tree
6384 pushtag (tree name, tree type, tag_scope scope)
6386 tree ret;
6387 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6388 ret = do_pushtag (name, type, scope);
6389 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6390 return ret;
6394 /* Subroutines for reverting temporarily to top-level for instantiation
6395 of templates and such. We actually need to clear out the class- and
6396 local-value slots of all identifiers, so that only the global values
6397 are at all visible. Simply setting current_binding_level to the global
6398 scope isn't enough, because more binding levels may be pushed. */
6399 struct saved_scope *scope_chain;
6401 /* Return true if ID has not already been marked. */
6403 static inline bool
6404 store_binding_p (tree id)
6406 if (!id || !IDENTIFIER_BINDING (id))
6407 return false;
6409 if (IDENTIFIER_MARKED (id))
6410 return false;
6412 return true;
6415 /* Add an appropriate binding to *OLD_BINDINGS which needs to already
6416 have enough space reserved. */
6418 static void
6419 store_binding (tree id, vec<cxx_saved_binding, va_gc> **old_bindings)
6421 cxx_saved_binding saved;
6423 gcc_checking_assert (store_binding_p (id));
6425 IDENTIFIER_MARKED (id) = 1;
6427 saved.identifier = id;
6428 saved.binding = IDENTIFIER_BINDING (id);
6429 saved.real_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
6430 (*old_bindings)->quick_push (saved);
6431 IDENTIFIER_BINDING (id) = NULL;
6434 static void
6435 store_bindings (tree names, vec<cxx_saved_binding, va_gc> **old_bindings)
6437 static vec<tree> bindings_need_stored;
6438 tree t, id;
6439 size_t i;
6441 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6442 for (t = names; t; t = TREE_CHAIN (t))
6444 if (TREE_CODE (t) == TREE_LIST)
6445 id = TREE_PURPOSE (t);
6446 else
6447 id = DECL_NAME (t);
6449 if (store_binding_p (id))
6450 bindings_need_stored.safe_push (id);
6452 if (!bindings_need_stored.is_empty ())
6454 vec_safe_reserve_exact (*old_bindings, bindings_need_stored.length ());
6455 for (i = 0; bindings_need_stored.iterate (i, &id); ++i)
6457 /* We can apparently have duplicates in NAMES. */
6458 if (store_binding_p (id))
6459 store_binding (id, old_bindings);
6461 bindings_need_stored.truncate (0);
6463 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6466 /* Like store_bindings, but NAMES is a vector of cp_class_binding
6467 objects, rather than a TREE_LIST. */
6469 static void
6470 store_class_bindings (vec<cp_class_binding, va_gc> *names,
6471 vec<cxx_saved_binding, va_gc> **old_bindings)
6473 static vec<tree> bindings_need_stored;
6474 size_t i;
6475 cp_class_binding *cb;
6477 for (i = 0; vec_safe_iterate (names, i, &cb); ++i)
6478 if (store_binding_p (cb->identifier))
6479 bindings_need_stored.safe_push (cb->identifier);
6480 if (!bindings_need_stored.is_empty ())
6482 tree id;
6483 vec_safe_reserve_exact (*old_bindings, bindings_need_stored.length ());
6484 for (i = 0; bindings_need_stored.iterate (i, &id); ++i)
6485 store_binding (id, old_bindings);
6486 bindings_need_stored.truncate (0);
6490 /* A chain of saved_scope structures awaiting reuse. */
6492 static GTY((deletable)) struct saved_scope *free_saved_scope;
6494 static void
6495 do_push_to_top_level (void)
6497 struct saved_scope *s;
6498 cp_binding_level *b;
6499 cxx_saved_binding *sb;
6500 size_t i;
6501 bool need_pop;
6503 /* Reuse or create a new structure for this saved scope. */
6504 if (free_saved_scope != NULL)
6506 s = free_saved_scope;
6507 free_saved_scope = s->prev;
6509 vec<cxx_saved_binding, va_gc> *old_bindings = s->old_bindings;
6510 memset (s, 0, sizeof (*s));
6511 /* Also reuse the structure's old_bindings vector. */
6512 vec_safe_truncate (old_bindings, 0);
6513 s->old_bindings = old_bindings;
6515 else
6516 s = ggc_cleared_alloc<saved_scope> ();
6518 b = scope_chain ? current_binding_level : 0;
6520 /* If we're in the middle of some function, save our state. */
6521 if (cfun)
6523 need_pop = true;
6524 push_function_context ();
6526 else
6527 need_pop = false;
6529 if (scope_chain && previous_class_level)
6530 store_class_bindings (previous_class_level->class_shadowed,
6531 &s->old_bindings);
6533 /* Have to include the global scope, because class-scope decls
6534 aren't listed anywhere useful. */
6535 for (; b; b = b->level_chain)
6537 tree t;
6539 /* Template IDs are inserted into the global level. If they were
6540 inserted into namespace level, finish_file wouldn't find them
6541 when doing pending instantiations. Therefore, don't stop at
6542 namespace level, but continue until :: . */
6543 if (global_scope_p (b))
6544 break;
6546 store_bindings (b->names, &s->old_bindings);
6547 /* We also need to check class_shadowed to save class-level type
6548 bindings, since pushclass doesn't fill in b->names. */
6549 if (b->kind == sk_class)
6550 store_class_bindings (b->class_shadowed, &s->old_bindings);
6552 /* Unwind type-value slots back to top level. */
6553 for (t = b->type_shadowed; t; t = TREE_CHAIN (t))
6554 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (t), TREE_VALUE (t));
6557 FOR_EACH_VEC_SAFE_ELT (s->old_bindings, i, sb)
6558 IDENTIFIER_MARKED (sb->identifier) = 0;
6560 s->prev = scope_chain;
6561 s->bindings = b;
6562 s->need_pop_function_context = need_pop;
6563 s->function_decl = current_function_decl;
6564 s->unevaluated_operand = cp_unevaluated_operand;
6565 s->inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
6566 s->x_stmt_tree.stmts_are_full_exprs_p = true;
6568 scope_chain = s;
6569 current_function_decl = NULL_TREE;
6570 vec_alloc (current_lang_base, 10);
6571 current_lang_name = lang_name_cplusplus;
6572 current_namespace = global_namespace;
6573 push_class_stack ();
6574 cp_unevaluated_operand = 0;
6575 c_inhibit_evaluation_warnings = 0;
6578 static void
6579 do_pop_from_top_level (void)
6581 struct saved_scope *s = scope_chain;
6582 cxx_saved_binding *saved;
6583 size_t i;
6585 /* Clear out class-level bindings cache. */
6586 if (previous_class_level)
6587 invalidate_class_lookup_cache ();
6588 pop_class_stack ();
6590 current_lang_base = 0;
6592 scope_chain = s->prev;
6593 FOR_EACH_VEC_SAFE_ELT (s->old_bindings, i, saved)
6595 tree id = saved->identifier;
6597 IDENTIFIER_BINDING (id) = saved->binding;
6598 SET_IDENTIFIER_TYPE_VALUE (id, saved->real_type_value);
6601 /* If we were in the middle of compiling a function, restore our
6602 state. */
6603 if (s->need_pop_function_context)
6604 pop_function_context ();
6605 current_function_decl = s->function_decl;
6606 cp_unevaluated_operand = s->unevaluated_operand;
6607 c_inhibit_evaluation_warnings = s->inhibit_evaluation_warnings;
6609 /* Make this saved_scope structure available for reuse by
6610 push_to_top_level. */
6611 s->prev = free_saved_scope;
6612 free_saved_scope = s;
6615 /* Push into the scope of the namespace NS, even if it is deeply
6616 nested within another namespace. */
6618 static void
6619 do_push_nested_namespace (tree ns)
6621 if (ns == global_namespace)
6622 do_push_to_top_level ();
6623 else
6625 do_push_nested_namespace (CP_DECL_CONTEXT (ns));
6626 gcc_checking_assert
6627 (find_namespace_value (current_namespace,
6628 DECL_NAME (ns) ? DECL_NAME (ns)
6629 : anon_identifier) == ns);
6630 resume_scope (NAMESPACE_LEVEL (ns));
6631 current_namespace = ns;
6635 /* Pop back from the scope of the namespace NS, which was previously
6636 entered with push_nested_namespace. */
6638 static void
6639 do_pop_nested_namespace (tree ns)
6641 while (ns != global_namespace)
6643 ns = CP_DECL_CONTEXT (ns);
6644 current_namespace = ns;
6645 leave_scope ();
6648 do_pop_from_top_level ();
6651 /* Add TARGET to USINGS, if it does not already exist there.
6652 We used to build the complete graph of usings at this point, from
6653 the POV of the source namespaces. Now we build that as we perform
6654 the unqualified search. */
6656 static void
6657 add_using_namespace (vec<tree, va_gc> *&usings, tree target)
6659 if (usings)
6660 for (unsigned ix = usings->length (); ix--;)
6661 if ((*usings)[ix] == target)
6662 return;
6664 vec_safe_push (usings, target);
6667 /* Tell the debug system of a using directive. */
6669 static void
6670 emit_debug_info_using_namespace (tree from, tree target, bool implicit)
6672 /* Emit debugging info. */
6673 tree context = from != global_namespace ? from : NULL_TREE;
6674 debug_hooks->imported_module_or_decl (target, NULL_TREE, context, false,
6675 implicit);
6678 /* Process a namespace-scope using directive. */
6680 void
6681 finish_namespace_using_directive (tree target, tree attribs)
6683 gcc_checking_assert (namespace_bindings_p ());
6684 if (target == error_mark_node)
6685 return;
6687 add_using_namespace (DECL_NAMESPACE_USING (current_namespace),
6688 ORIGINAL_NAMESPACE (target));
6689 emit_debug_info_using_namespace (current_namespace,
6690 ORIGINAL_NAMESPACE (target), false);
6692 if (attribs == error_mark_node)
6693 return;
6695 for (tree a = attribs; a; a = TREE_CHAIN (a))
6697 tree name = get_attribute_name (a);
6698 if (is_attribute_p ("strong", name))
6700 warning (0, "strong using directive no longer supported");
6701 if (CP_DECL_CONTEXT (target) == current_namespace)
6702 inform (DECL_SOURCE_LOCATION (target),
6703 "you may use an inline namespace instead");
6705 else
6706 warning (OPT_Wattributes, "%qD attribute directive ignored", name);
6710 /* Process a function-scope using-directive. */
6712 void
6713 finish_local_using_directive (tree target, tree attribs)
6715 gcc_checking_assert (local_bindings_p ());
6716 if (target == error_mark_node)
6717 return;
6719 if (attribs)
6720 warning (OPT_Wattributes, "attributes ignored on local using directive");
6722 add_stmt (build_stmt (input_location, USING_STMT, target));
6724 add_using_namespace (current_binding_level->using_directives,
6725 ORIGINAL_NAMESPACE (target));
6728 /* Pushes X into the global namespace. */
6730 tree
6731 pushdecl_top_level (tree x, bool is_friend)
6733 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6734 do_push_to_top_level ();
6735 x = pushdecl_namespace_level (x, is_friend);
6736 do_pop_from_top_level ();
6737 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6738 return x;
6741 /* Pushes X into the global namespace and calls cp_finish_decl to
6742 register the variable, initializing it with INIT. */
6744 tree
6745 pushdecl_top_level_and_finish (tree x, tree init)
6747 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6748 do_push_to_top_level ();
6749 x = pushdecl_namespace_level (x, false);
6750 cp_finish_decl (x, init, false, NULL_TREE, 0);
6751 do_pop_from_top_level ();
6752 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6753 return x;
6756 /* Enter the namespaces from current_namerspace to NS. */
6758 static int
6759 push_inline_namespaces (tree ns)
6761 int count = 0;
6762 if (ns != current_namespace)
6764 gcc_assert (ns != global_namespace);
6765 count += push_inline_namespaces (CP_DECL_CONTEXT (ns));
6766 resume_scope (NAMESPACE_LEVEL (ns));
6767 current_namespace = ns;
6768 count++;
6770 return count;
6773 /* Push into the scope of the NAME namespace. If NAME is NULL_TREE,
6774 then we enter an anonymous namespace. If MAKE_INLINE is true, then
6775 we create an inline namespace (it is up to the caller to check upon
6776 redefinition). Return the number of namespaces entered. */
6779 push_namespace (tree name, bool make_inline)
6781 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6782 int count = 0;
6784 /* We should not get here if the global_namespace is not yet constructed
6785 nor if NAME designates the global namespace: The global scope is
6786 constructed elsewhere. */
6787 gcc_assert (global_namespace != NULL && name != global_identifier);
6789 if (!name)
6790 name = anon_identifier;
6792 tree ns = NULL_TREE;
6794 name_lookup lookup (name, 0);
6795 if (!lookup.search_qualified (current_namespace, /*usings=*/false))
6797 else if (TREE_CODE (lookup.value) != NAMESPACE_DECL)
6799 else if (tree dna = DECL_NAMESPACE_ALIAS (lookup.value))
6801 /* A namespace alias is not allowed here, but if the alias
6802 is for a namespace also inside the current scope,
6803 accept it with a diagnostic. That's better than dying
6804 horribly. */
6805 if (is_nested_namespace (current_namespace, CP_DECL_CONTEXT (dna)))
6807 error ("namespace alias %qD not allowed here, "
6808 "assuming %qD", lookup.value, dna);
6809 ns = dna;
6812 else
6813 ns = lookup.value;
6816 bool new_ns = false;
6817 if (ns)
6818 /* DR2061. NS might be a member of an inline namespace. We
6819 need to push into those namespaces. */
6820 count += push_inline_namespaces (CP_DECL_CONTEXT (ns));
6821 else
6823 ns = build_lang_decl (NAMESPACE_DECL, name, void_type_node);
6824 SCOPE_DEPTH (ns) = SCOPE_DEPTH (current_namespace) + 1;
6825 if (!SCOPE_DEPTH (ns))
6826 /* We only allow depth 255. */
6827 sorry ("cannot nest more than %d namespaces",
6828 SCOPE_DEPTH (current_namespace));
6829 DECL_CONTEXT (ns) = FROB_CONTEXT (current_namespace);
6830 new_ns = true;
6832 if (pushdecl (ns) == error_mark_node)
6833 ns = NULL_TREE;
6834 else
6836 if (name == anon_identifier)
6838 /* Clear DECL_NAME for the benefit of debugging back ends. */
6839 SET_DECL_ASSEMBLER_NAME (ns, name);
6840 DECL_NAME (ns) = NULL_TREE;
6842 if (!make_inline)
6843 add_using_namespace (DECL_NAMESPACE_USING (current_namespace),
6844 ns);
6846 else if (TREE_PUBLIC (current_namespace))
6847 TREE_PUBLIC (ns) = 1;
6849 if (make_inline)
6851 DECL_NAMESPACE_INLINE_P (ns) = true;
6852 vec_safe_push (DECL_NAMESPACE_INLINEES (current_namespace), ns);
6855 if (name == anon_identifier || make_inline)
6856 emit_debug_info_using_namespace (current_namespace, ns, true);
6860 if (ns)
6862 if (make_inline && !DECL_NAMESPACE_INLINE_P (ns))
6864 error ("inline namespace must be specified at initial definition");
6865 inform (DECL_SOURCE_LOCATION (ns), "%qD defined here", ns);
6867 if (new_ns)
6868 begin_scope (sk_namespace, ns);
6869 else
6870 resume_scope (NAMESPACE_LEVEL (ns));
6871 current_namespace = ns;
6872 count++;
6875 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6876 return count;
6879 /* Pop from the scope of the current namespace. */
6881 void
6882 pop_namespace (void)
6884 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6886 gcc_assert (current_namespace != global_namespace);
6887 current_namespace = CP_DECL_CONTEXT (current_namespace);
6888 /* The binding level is not popped, as it might be re-opened later. */
6889 leave_scope ();
6891 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6894 /* External entry points for do_{push_to/pop_from}_top_level. */
6896 void
6897 push_to_top_level (void)
6899 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6900 do_push_to_top_level ();
6901 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6904 void
6905 pop_from_top_level (void)
6907 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6908 do_pop_from_top_level ();
6909 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6912 /* External entry points for do_{push,pop}_nested_namespace. */
6914 void
6915 push_nested_namespace (tree ns)
6917 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6918 do_push_nested_namespace (ns);
6919 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6922 void
6923 pop_nested_namespace (tree ns)
6925 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6926 gcc_assert (current_namespace == ns);
6927 do_pop_nested_namespace (ns);
6928 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6931 /* Pop off extraneous binding levels left over due to syntax errors.
6932 We don't pop past namespaces, as they might be valid. */
6934 void
6935 pop_everything (void)
6937 if (ENABLE_SCOPE_CHECKING)
6938 verbatim ("XXX entering pop_everything ()\n");
6939 while (!namespace_bindings_p ())
6941 if (current_binding_level->kind == sk_class)
6942 pop_nested_class ();
6943 else
6944 poplevel (0, 0, 0);
6946 if (ENABLE_SCOPE_CHECKING)
6947 verbatim ("XXX leaving pop_everything ()\n");
6950 /* Emit debugging information for using declarations and directives.
6951 If input tree is overloaded fn then emit debug info for all
6952 candidates. */
6954 void
6955 cp_emit_debug_info_for_using (tree t, tree context)
6957 /* Don't try to emit any debug information if we have errors. */
6958 if (seen_error ())
6959 return;
6961 /* Ignore this FUNCTION_DECL if it refers to a builtin declaration
6962 of a builtin function. */
6963 if (TREE_CODE (t) == FUNCTION_DECL
6964 && DECL_EXTERNAL (t)
6965 && DECL_BUILT_IN (t))
6966 return;
6968 /* Do not supply context to imported_module_or_decl, if
6969 it is a global namespace. */
6970 if (context == global_namespace)
6971 context = NULL_TREE;
6973 t = MAYBE_BASELINK_FUNCTIONS (t);
6975 /* FIXME: Handle TEMPLATE_DECLs. */
6976 for (lkp_iterator iter (t); iter; ++iter)
6978 tree fn = *iter;
6979 if (TREE_CODE (fn) != TEMPLATE_DECL)
6981 if (building_stmt_list_p ())
6982 add_stmt (build_stmt (input_location, USING_STMT, fn));
6983 else
6984 debug_hooks->imported_module_or_decl (fn, NULL_TREE, context,
6985 false, false);
6990 #include "gt-cp-name-lookup.h"