* dwarf2out.c (DEBUG_LTO_DWO_INFO_SECTION): Reorder defines.
[official-gcc.git] / gcc / cp / name-lookup.c
blobd0aaf2b1d16e547442c580732d669290fe91fc7e
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))
1438 /* We can get two TYPE_DECLs or two USING_DECLs. Place in source
1439 order. */
1440 return DECL_SOURCE_LOCATION (a) < DECL_SOURCE_LOCATION (b) ? -1 : +1;
1442 /* If one of them is a TYPE_DECL, it loses. */
1443 if (TREE_CODE (a) == TYPE_DECL)
1444 return +1;
1445 else if (TREE_CODE (b) == TYPE_DECL)
1446 return -1;
1448 /* If one of them is a USING_DECL, it loses. */
1449 if (TREE_CODE (a) == USING_DECL)
1450 return +1;
1451 else if (TREE_CODE (b) == USING_DECL)
1452 return -1;
1454 /* There are no other cases, as duplicate detection should have
1455 kicked in earlier. However, some erroneous cases get though.
1456 Order by source location. We should really prevent this
1457 happening. */
1458 gcc_assert (errorcount);
1459 return DECL_SOURCE_LOCATION (a) < DECL_SOURCE_LOCATION (b) ? -1 : +1;
1462 static struct {
1463 gt_pointer_operator new_value;
1464 void *cookie;
1465 } resort_data;
1467 /* This routine compares two fields like member_name_cmp but using the
1468 pointer operator in resort_field_decl_data. We don't have to deal
1469 with duplicates here. */
1471 static int
1472 resort_member_name_cmp (const void *a_p, const void *b_p)
1474 tree a = *(const tree *)a_p;
1475 tree b = *(const tree *)b_p;
1476 tree name_a = OVL_NAME (a);
1477 tree name_b = OVL_NAME (b);
1479 resort_data.new_value (&name_a, resort_data.cookie);
1480 resort_data.new_value (&name_b, resort_data.cookie);
1482 gcc_checking_assert (name_a != name_b);
1484 return name_a < name_b ? -1 : +1;
1487 /* Resort CLASSTYPE_MEMBER_VEC because pointers have been reordered. */
1489 void
1490 resort_type_member_vec (void *obj, void */*orig_obj*/,
1491 gt_pointer_operator new_value, void* cookie)
1493 if (vec<tree, va_gc> *member_vec = (vec<tree, va_gc> *) obj)
1495 resort_data.new_value = new_value;
1496 resort_data.cookie = cookie;
1497 qsort (member_vec->address (), member_vec->length (),
1498 sizeof (tree), resort_member_name_cmp);
1502 /* Recursively count the number of fields in KLASS, including anonymous
1503 union members. */
1505 static unsigned
1506 count_class_fields (tree klass)
1508 unsigned n_fields = 0;
1510 for (tree fields = TYPE_FIELDS (klass); fields; fields = DECL_CHAIN (fields))
1511 if (DECL_DECLARES_FUNCTION_P (fields))
1512 /* Functions are dealt with separately. */;
1513 else if (TREE_CODE (fields) == FIELD_DECL
1514 && ANON_AGGR_TYPE_P (TREE_TYPE (fields)))
1515 n_fields += count_class_fields (TREE_TYPE (fields));
1516 else if (DECL_NAME (fields))
1517 n_fields += 1;
1519 return n_fields;
1522 /* Append all the nonfunction members fields of KLASS to MEMBER_VEC.
1523 Recurse for anonymous members. MEMBER_VEC must have space. */
1525 static void
1526 member_vec_append_class_fields (vec<tree, va_gc> *member_vec, tree klass)
1528 for (tree fields = TYPE_FIELDS (klass); fields; fields = DECL_CHAIN (fields))
1529 if (DECL_DECLARES_FUNCTION_P (fields))
1530 /* Functions are handled separately. */;
1531 else if (TREE_CODE (fields) == FIELD_DECL
1532 && ANON_AGGR_TYPE_P (TREE_TYPE (fields)))
1533 member_vec_append_class_fields (member_vec, TREE_TYPE (fields));
1534 else if (DECL_NAME (fields))
1536 tree field = fields;
1537 /* Mark a conv-op USING_DECL with the conv-op-marker. */
1538 if (TREE_CODE (field) == USING_DECL
1539 && IDENTIFIER_CONV_OP_P (DECL_NAME (field)))
1540 field = ovl_make (conv_op_marker, field);
1541 member_vec->quick_push (field);
1545 /* Append all of the enum values of ENUMTYPE to MEMBER_VEC.
1546 MEMBER_VEC must have space. */
1548 static void
1549 member_vec_append_enum_values (vec<tree, va_gc> *member_vec, tree enumtype)
1551 for (tree values = TYPE_VALUES (enumtype);
1552 values; values = TREE_CHAIN (values))
1553 member_vec->quick_push (TREE_VALUE (values));
1556 /* MEMBER_VEC has just had new DECLs added to it, but is sorted.
1557 DeDup adjacent DECLS of the same name. We already dealt with
1558 conflict resolution when adding the fields or methods themselves.
1559 There are three cases (which could all be combined):
1560 1) a TYPE_DECL and non TYPE_DECL. Deploy STAT_HACK as appropriate.
1561 2) a USING_DECL and an overload. If the USING_DECL is dependent,
1562 it wins. Otherwise the OVERLOAD does.
1563 3) two USING_DECLS. ...
1565 member_name_cmp will have ordered duplicates as
1566 <fns><using><type> */
1568 static void
1569 member_vec_dedup (vec<tree, va_gc> *member_vec)
1571 unsigned len = member_vec->length ();
1572 unsigned store = 0;
1574 tree current = (*member_vec)[0], name = OVL_NAME (current);
1575 tree next = NULL_TREE, next_name = NULL_TREE;
1576 for (unsigned jx, ix = 0; ix < len;
1577 ix = jx, current = next, name = next_name)
1579 tree to_type = NULL_TREE;
1580 tree to_using = NULL_TREE;
1581 tree marker = NULL_TREE;
1582 if (IDENTIFIER_CONV_OP_P (name))
1584 marker = current;
1585 current = OVL_CHAIN (current);
1586 name = DECL_NAME (OVL_FUNCTION (marker));
1587 gcc_checking_assert (name == conv_op_identifier);
1590 if (TREE_CODE (current) == USING_DECL)
1592 current = strip_using_decl (current);
1593 if (is_overloaded_fn (current))
1594 current = NULL_TREE;
1595 else if (TREE_CODE (current) == USING_DECL)
1597 to_using = current;
1598 current = NULL_TREE;
1602 if (current && DECL_DECLARES_TYPE_P (current))
1604 to_type = current;
1605 current = NULL_TREE;
1608 for (jx = ix + 1; jx < len; jx++)
1610 next = (*member_vec)[jx];
1611 next_name = OVL_NAME (next);
1612 if (next_name != name)
1613 break;
1615 if (marker)
1617 gcc_checking_assert (OVL_FUNCTION (marker)
1618 == OVL_FUNCTION (next));
1619 next = OVL_CHAIN (next);
1622 if (TREE_CODE (next) == USING_DECL)
1624 next = strip_using_decl (next);
1625 if (is_overloaded_fn (next))
1626 next = NULL_TREE;
1627 else if (TREE_CODE (next) == USING_DECL)
1629 to_using = next;
1630 next = NULL_TREE;
1634 if (next && DECL_DECLARES_TYPE_P (next))
1635 to_type = next;
1638 if (to_using)
1640 if (!current)
1641 current = to_using;
1642 else
1643 current = ovl_make (to_using, current);
1646 if (to_type)
1648 if (!current)
1649 current = to_type;
1650 else
1651 current = stat_hack (current, to_type);
1654 gcc_assert (current);
1655 if (marker)
1657 OVL_CHAIN (marker) = current;
1658 current = marker;
1660 (*member_vec)[store++] = current;
1663 while (store++ < len)
1664 member_vec->pop ();
1667 /* Add the non-function members to CLASSTYPE_MEMBER_VEC. If there is
1668 no existing MEMBER_VEC and fewer than 8 fields, do nothing. We
1669 know there must be at least 1 field -- the self-reference
1670 TYPE_DECL, except for anon aggregates, which will have at least
1671 one field. */
1673 void
1674 set_class_bindings (tree klass, unsigned extra)
1676 unsigned n_fields = count_class_fields (klass);
1677 vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (klass);
1679 if (member_vec || n_fields >= 8)
1681 /* Append the new fields. */
1682 vec_safe_reserve_exact (member_vec, extra + n_fields);
1683 member_vec_append_class_fields (member_vec, klass);
1686 if (member_vec)
1688 CLASSTYPE_MEMBER_VEC (klass) = member_vec;
1689 qsort (member_vec->address (), member_vec->length (),
1690 sizeof (tree), member_name_cmp);
1691 member_vec_dedup (member_vec);
1695 /* Insert lately defined enum ENUMTYPE into KLASS for the sorted case. */
1697 void
1698 insert_late_enum_def_bindings (tree klass, tree enumtype)
1700 int n_fields;
1701 vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (klass);
1703 /* The enum bindings will already be on the TYPE_FIELDS, so don't
1704 count them twice. */
1705 if (!member_vec)
1706 n_fields = count_class_fields (klass);
1707 else
1708 n_fields = list_length (TYPE_VALUES (enumtype));
1710 if (member_vec || n_fields >= 8)
1712 vec_safe_reserve_exact (member_vec, n_fields);
1713 if (CLASSTYPE_MEMBER_VEC (klass))
1714 member_vec_append_enum_values (member_vec, enumtype);
1715 else
1716 member_vec_append_class_fields (member_vec, klass);
1717 CLASSTYPE_MEMBER_VEC (klass) = member_vec;
1718 qsort (member_vec->address (), member_vec->length (),
1719 sizeof (tree), member_name_cmp);
1720 member_vec_dedup (member_vec);
1724 /* Compute the chain index of a binding_entry given the HASH value of its
1725 name and the total COUNT of chains. COUNT is assumed to be a power
1726 of 2. */
1728 #define ENTRY_INDEX(HASH, COUNT) (((HASH) >> 3) & ((COUNT) - 1))
1730 /* A free list of "binding_entry"s awaiting for re-use. */
1732 static GTY((deletable)) binding_entry free_binding_entry = NULL;
1734 /* The binding oracle; see cp-tree.h. */
1736 cp_binding_oracle_function *cp_binding_oracle;
1738 /* If we have a binding oracle, ask it for all namespace-scoped
1739 definitions of NAME. */
1741 static inline void
1742 query_oracle (tree name)
1744 if (!cp_binding_oracle)
1745 return;
1747 /* LOOKED_UP holds the set of identifiers that we have already
1748 looked up with the oracle. */
1749 static hash_set<tree> looked_up;
1750 if (looked_up.add (name))
1751 return;
1753 cp_binding_oracle (CP_ORACLE_IDENTIFIER, name);
1756 /* Create a binding_entry object for (NAME, TYPE). */
1758 static inline binding_entry
1759 binding_entry_make (tree name, tree type)
1761 binding_entry entry;
1763 if (free_binding_entry)
1765 entry = free_binding_entry;
1766 free_binding_entry = entry->chain;
1768 else
1769 entry = ggc_alloc<binding_entry_s> ();
1771 entry->name = name;
1772 entry->type = type;
1773 entry->chain = NULL;
1775 return entry;
1778 /* Put ENTRY back on the free list. */
1779 #if 0
1780 static inline void
1781 binding_entry_free (binding_entry entry)
1783 entry->name = NULL;
1784 entry->type = NULL;
1785 entry->chain = free_binding_entry;
1786 free_binding_entry = entry;
1788 #endif
1790 /* The datatype used to implement the mapping from names to types at
1791 a given scope. */
1792 struct GTY(()) binding_table_s {
1793 /* Array of chains of "binding_entry"s */
1794 binding_entry * GTY((length ("%h.chain_count"))) chain;
1796 /* The number of chains in this table. This is the length of the
1797 member "chain" considered as an array. */
1798 size_t chain_count;
1800 /* Number of "binding_entry"s in this table. */
1801 size_t entry_count;
1804 /* Construct TABLE with an initial CHAIN_COUNT. */
1806 static inline void
1807 binding_table_construct (binding_table table, size_t chain_count)
1809 table->chain_count = chain_count;
1810 table->entry_count = 0;
1811 table->chain = ggc_cleared_vec_alloc<binding_entry> (table->chain_count);
1814 /* Make TABLE's entries ready for reuse. */
1815 #if 0
1816 static void
1817 binding_table_free (binding_table table)
1819 size_t i;
1820 size_t count;
1822 if (table == NULL)
1823 return;
1825 for (i = 0, count = table->chain_count; i < count; ++i)
1827 binding_entry temp = table->chain[i];
1828 while (temp != NULL)
1830 binding_entry entry = temp;
1831 temp = entry->chain;
1832 binding_entry_free (entry);
1834 table->chain[i] = NULL;
1836 table->entry_count = 0;
1838 #endif
1840 /* Allocate a table with CHAIN_COUNT, assumed to be a power of two. */
1842 static inline binding_table
1843 binding_table_new (size_t chain_count)
1845 binding_table table = ggc_alloc<binding_table_s> ();
1846 table->chain = NULL;
1847 binding_table_construct (table, chain_count);
1848 return table;
1851 /* Expand TABLE to twice its current chain_count. */
1853 static void
1854 binding_table_expand (binding_table table)
1856 const size_t old_chain_count = table->chain_count;
1857 const size_t old_entry_count = table->entry_count;
1858 const size_t new_chain_count = 2 * old_chain_count;
1859 binding_entry *old_chains = table->chain;
1860 size_t i;
1862 binding_table_construct (table, new_chain_count);
1863 for (i = 0; i < old_chain_count; ++i)
1865 binding_entry entry = old_chains[i];
1866 for (; entry != NULL; entry = old_chains[i])
1868 const unsigned int hash = IDENTIFIER_HASH_VALUE (entry->name);
1869 const size_t j = ENTRY_INDEX (hash, new_chain_count);
1871 old_chains[i] = entry->chain;
1872 entry->chain = table->chain[j];
1873 table->chain[j] = entry;
1876 table->entry_count = old_entry_count;
1879 /* Insert a binding for NAME to TYPE into TABLE. */
1881 static void
1882 binding_table_insert (binding_table table, tree name, tree type)
1884 const unsigned int hash = IDENTIFIER_HASH_VALUE (name);
1885 const size_t i = ENTRY_INDEX (hash, table->chain_count);
1886 binding_entry entry = binding_entry_make (name, type);
1888 entry->chain = table->chain[i];
1889 table->chain[i] = entry;
1890 ++table->entry_count;
1892 if (3 * table->chain_count < 5 * table->entry_count)
1893 binding_table_expand (table);
1896 /* Return the binding_entry, if any, that maps NAME. */
1898 binding_entry
1899 binding_table_find (binding_table table, tree name)
1901 const unsigned int hash = IDENTIFIER_HASH_VALUE (name);
1902 binding_entry entry = table->chain[ENTRY_INDEX (hash, table->chain_count)];
1904 while (entry != NULL && entry->name != name)
1905 entry = entry->chain;
1907 return entry;
1910 /* Apply PROC -- with DATA -- to all entries in TABLE. */
1912 void
1913 binding_table_foreach (binding_table table, bt_foreach_proc proc, void *data)
1915 size_t chain_count;
1916 size_t i;
1918 if (!table)
1919 return;
1921 chain_count = table->chain_count;
1922 for (i = 0; i < chain_count; ++i)
1924 binding_entry entry = table->chain[i];
1925 for (; entry != NULL; entry = entry->chain)
1926 proc (entry, data);
1930 #ifndef ENABLE_SCOPE_CHECKING
1931 # define ENABLE_SCOPE_CHECKING 0
1932 #else
1933 # define ENABLE_SCOPE_CHECKING 1
1934 #endif
1936 /* A free list of "cxx_binding"s, connected by their PREVIOUS. */
1938 static GTY((deletable)) cxx_binding *free_bindings;
1940 /* Initialize VALUE and TYPE field for BINDING, and set the PREVIOUS
1941 field to NULL. */
1943 static inline void
1944 cxx_binding_init (cxx_binding *binding, tree value, tree type)
1946 binding->value = value;
1947 binding->type = type;
1948 binding->previous = NULL;
1951 /* (GC)-allocate a binding object with VALUE and TYPE member initialized. */
1953 static cxx_binding *
1954 cxx_binding_make (tree value, tree type)
1956 cxx_binding *binding;
1957 if (free_bindings)
1959 binding = free_bindings;
1960 free_bindings = binding->previous;
1962 else
1963 binding = ggc_alloc<cxx_binding> ();
1965 cxx_binding_init (binding, value, type);
1967 return binding;
1970 /* Put BINDING back on the free list. */
1972 static inline void
1973 cxx_binding_free (cxx_binding *binding)
1975 binding->scope = NULL;
1976 binding->previous = free_bindings;
1977 free_bindings = binding;
1980 /* Create a new binding for NAME (with the indicated VALUE and TYPE
1981 bindings) in the class scope indicated by SCOPE. */
1983 static cxx_binding *
1984 new_class_binding (tree name, tree value, tree type, cp_binding_level *scope)
1986 cp_class_binding cb = {cxx_binding_make (value, type), name};
1987 cxx_binding *binding = cb.base;
1988 vec_safe_push (scope->class_shadowed, cb);
1989 binding->scope = scope;
1990 return binding;
1993 /* Make DECL the innermost binding for ID. The LEVEL is the binding
1994 level at which this declaration is being bound. */
1996 void
1997 push_binding (tree id, tree decl, cp_binding_level* level)
1999 cxx_binding *binding;
2001 if (level != class_binding_level)
2003 binding = cxx_binding_make (decl, NULL_TREE);
2004 binding->scope = level;
2006 else
2007 binding = new_class_binding (id, decl, /*type=*/NULL_TREE, level);
2009 /* Now, fill in the binding information. */
2010 binding->previous = IDENTIFIER_BINDING (id);
2011 INHERITED_VALUE_BINDING_P (binding) = 0;
2012 LOCAL_BINDING_P (binding) = (level != class_binding_level);
2014 /* And put it on the front of the list of bindings for ID. */
2015 IDENTIFIER_BINDING (id) = binding;
2018 /* Remove the binding for DECL which should be the innermost binding
2019 for ID. */
2021 void
2022 pop_local_binding (tree id, tree decl)
2024 cxx_binding *binding;
2026 if (id == NULL_TREE)
2027 /* It's easiest to write the loops that call this function without
2028 checking whether or not the entities involved have names. We
2029 get here for such an entity. */
2030 return;
2032 /* Get the innermost binding for ID. */
2033 binding = IDENTIFIER_BINDING (id);
2035 /* The name should be bound. */
2036 gcc_assert (binding != NULL);
2038 /* The DECL will be either the ordinary binding or the type
2039 binding for this identifier. Remove that binding. */
2040 if (binding->value == decl)
2041 binding->value = NULL_TREE;
2042 else
2044 gcc_assert (binding->type == decl);
2045 binding->type = NULL_TREE;
2048 if (!binding->value && !binding->type)
2050 /* We're completely done with the innermost binding for this
2051 identifier. Unhook it from the list of bindings. */
2052 IDENTIFIER_BINDING (id) = binding->previous;
2054 /* Add it to the free list. */
2055 cxx_binding_free (binding);
2059 /* Remove the bindings for the decls of the current level and leave
2060 the current scope. */
2062 void
2063 pop_bindings_and_leave_scope (void)
2065 for (tree t = get_local_decls (); t; t = DECL_CHAIN (t))
2067 tree decl = TREE_CODE (t) == TREE_LIST ? TREE_VALUE (t) : t;
2068 tree name = OVL_NAME (decl);
2070 pop_local_binding (name, decl);
2073 leave_scope ();
2076 /* Strip non dependent using declarations. If DECL is dependent,
2077 surreptitiously create a typename_type and return it. */
2079 tree
2080 strip_using_decl (tree decl)
2082 if (decl == NULL_TREE)
2083 return NULL_TREE;
2085 while (TREE_CODE (decl) == USING_DECL && !DECL_DEPENDENT_P (decl))
2086 decl = USING_DECL_DECLS (decl);
2088 if (TREE_CODE (decl) == USING_DECL && DECL_DEPENDENT_P (decl)
2089 && USING_DECL_TYPENAME_P (decl))
2091 /* We have found a type introduced by a using
2092 declaration at class scope that refers to a dependent
2093 type.
2095 using typename :: [opt] nested-name-specifier unqualified-id ;
2097 decl = make_typename_type (TREE_TYPE (decl),
2098 DECL_NAME (decl),
2099 typename_type, tf_error);
2100 if (decl != error_mark_node)
2101 decl = TYPE_NAME (decl);
2104 return decl;
2107 /* Return true if OVL is an overload for an anticipated builtin. */
2109 static bool
2110 anticipated_builtin_p (tree ovl)
2112 if (TREE_CODE (ovl) != OVERLOAD)
2113 return false;
2115 if (!OVL_HIDDEN_P (ovl))
2116 return false;
2118 tree fn = OVL_FUNCTION (ovl);
2119 gcc_checking_assert (DECL_ANTICIPATED (fn));
2121 if (DECL_HIDDEN_FRIEND_P (fn))
2122 return false;
2124 return true;
2127 /* BINDING records an existing declaration for a name in the current scope.
2128 But, DECL is another declaration for that same identifier in the
2129 same scope. This is the `struct stat' hack whereby a non-typedef
2130 class name or enum-name can be bound at the same level as some other
2131 kind of entity.
2132 3.3.7/1
2134 A class name (9.1) or enumeration name (7.2) can be hidden by the
2135 name of an object, function, or enumerator declared in the same scope.
2136 If a class or enumeration name and an object, function, or enumerator
2137 are declared in the same scope (in any order) with the same name, the
2138 class or enumeration name is hidden wherever the object, function, or
2139 enumerator name is visible.
2141 It's the responsibility of the caller to check that
2142 inserting this name is valid here. Returns nonzero if the new binding
2143 was successful. */
2145 static bool
2146 supplement_binding_1 (cxx_binding *binding, tree decl)
2148 tree bval = binding->value;
2149 bool ok = true;
2150 tree target_bval = strip_using_decl (bval);
2151 tree target_decl = strip_using_decl (decl);
2153 if (TREE_CODE (target_decl) == TYPE_DECL && DECL_ARTIFICIAL (target_decl)
2154 && target_decl != target_bval
2155 && (TREE_CODE (target_bval) != TYPE_DECL
2156 /* We allow pushing an enum multiple times in a class
2157 template in order to handle late matching of underlying
2158 type on an opaque-enum-declaration followed by an
2159 enum-specifier. */
2160 || (processing_template_decl
2161 && TREE_CODE (TREE_TYPE (target_decl)) == ENUMERAL_TYPE
2162 && TREE_CODE (TREE_TYPE (target_bval)) == ENUMERAL_TYPE
2163 && (dependent_type_p (ENUM_UNDERLYING_TYPE
2164 (TREE_TYPE (target_decl)))
2165 || dependent_type_p (ENUM_UNDERLYING_TYPE
2166 (TREE_TYPE (target_bval)))))))
2167 /* The new name is the type name. */
2168 binding->type = decl;
2169 else if (/* TARGET_BVAL is null when push_class_level_binding moves
2170 an inherited type-binding out of the way to make room
2171 for a new value binding. */
2172 !target_bval
2173 /* TARGET_BVAL is error_mark_node when TARGET_DECL's name
2174 has been used in a non-class scope prior declaration.
2175 In that case, we should have already issued a
2176 diagnostic; for graceful error recovery purpose, pretend
2177 this was the intended declaration for that name. */
2178 || target_bval == error_mark_node
2179 /* If TARGET_BVAL is anticipated but has not yet been
2180 declared, pretend it is not there at all. */
2181 || anticipated_builtin_p (target_bval))
2182 binding->value = decl;
2183 else if (TREE_CODE (target_bval) == TYPE_DECL
2184 && DECL_ARTIFICIAL (target_bval)
2185 && target_decl != target_bval
2186 && (TREE_CODE (target_decl) != TYPE_DECL
2187 || same_type_p (TREE_TYPE (target_decl),
2188 TREE_TYPE (target_bval))))
2190 /* The old binding was a type name. It was placed in
2191 VALUE field because it was thought, at the point it was
2192 declared, to be the only entity with such a name. Move the
2193 type name into the type slot; it is now hidden by the new
2194 binding. */
2195 binding->type = bval;
2196 binding->value = decl;
2197 binding->value_is_inherited = false;
2199 else if (TREE_CODE (target_bval) == TYPE_DECL
2200 && TREE_CODE (target_decl) == TYPE_DECL
2201 && DECL_NAME (target_decl) == DECL_NAME (target_bval)
2202 && binding->scope->kind != sk_class
2203 && (same_type_p (TREE_TYPE (target_decl), TREE_TYPE (target_bval))
2204 /* If either type involves template parameters, we must
2205 wait until instantiation. */
2206 || uses_template_parms (TREE_TYPE (target_decl))
2207 || uses_template_parms (TREE_TYPE (target_bval))))
2208 /* We have two typedef-names, both naming the same type to have
2209 the same name. In general, this is OK because of:
2211 [dcl.typedef]
2213 In a given scope, a typedef specifier can be used to redefine
2214 the name of any type declared in that scope to refer to the
2215 type to which it already refers.
2217 However, in class scopes, this rule does not apply due to the
2218 stricter language in [class.mem] prohibiting redeclarations of
2219 members. */
2220 ok = false;
2221 /* There can be two block-scope declarations of the same variable,
2222 so long as they are `extern' declarations. However, there cannot
2223 be two declarations of the same static data member:
2225 [class.mem]
2227 A member shall not be declared twice in the
2228 member-specification. */
2229 else if (VAR_P (target_decl)
2230 && VAR_P (target_bval)
2231 && DECL_EXTERNAL (target_decl) && DECL_EXTERNAL (target_bval)
2232 && !DECL_CLASS_SCOPE_P (target_decl))
2234 duplicate_decls (decl, binding->value, /*newdecl_is_friend=*/false);
2235 ok = false;
2237 else if (TREE_CODE (decl) == NAMESPACE_DECL
2238 && TREE_CODE (bval) == NAMESPACE_DECL
2239 && DECL_NAMESPACE_ALIAS (decl)
2240 && DECL_NAMESPACE_ALIAS (bval)
2241 && ORIGINAL_NAMESPACE (bval) == ORIGINAL_NAMESPACE (decl))
2242 /* [namespace.alias]
2244 In a declarative region, a namespace-alias-definition can be
2245 used to redefine a namespace-alias declared in that declarative
2246 region to refer only to the namespace to which it already
2247 refers. */
2248 ok = false;
2249 else if (maybe_remove_implicit_alias (bval))
2251 /* There was a mangling compatibility alias using this mangled name,
2252 but now we have a real decl that wants to use it instead. */
2253 binding->value = decl;
2255 else
2257 if (!error_operand_p (bval))
2258 diagnose_name_conflict (decl, bval);
2259 ok = false;
2262 return ok;
2265 /* Diagnose a name conflict between DECL and BVAL. */
2267 static void
2268 diagnose_name_conflict (tree decl, tree bval)
2270 if (TREE_CODE (decl) == TREE_CODE (bval)
2271 && TREE_CODE (decl) != NAMESPACE_DECL
2272 && !DECL_DECLARES_FUNCTION_P (decl)
2273 && (TREE_CODE (decl) != TYPE_DECL
2274 || DECL_ARTIFICIAL (decl) == DECL_ARTIFICIAL (bval))
2275 && CP_DECL_CONTEXT (decl) == CP_DECL_CONTEXT (bval))
2276 error ("redeclaration of %q#D", decl);
2277 else
2278 error ("%q#D conflicts with a previous declaration", decl);
2280 inform (location_of (bval), "previous declaration %q#D", bval);
2283 /* Wrapper for supplement_binding_1. */
2285 static bool
2286 supplement_binding (cxx_binding *binding, tree decl)
2288 bool ret;
2289 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
2290 ret = supplement_binding_1 (binding, decl);
2291 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
2292 return ret;
2295 /* Replace BINDING's current value on its scope's name list with
2296 NEWVAL. */
2298 static void
2299 update_local_overload (cxx_binding *binding, tree newval)
2301 tree *d;
2303 for (d = &binding->scope->names; ; d = &TREE_CHAIN (*d))
2304 if (*d == binding->value)
2306 /* Stitch new list node in. */
2307 *d = tree_cons (NULL_TREE, NULL_TREE, TREE_CHAIN (*d));
2308 break;
2310 else if (TREE_CODE (*d) == TREE_LIST && TREE_VALUE (*d) == binding->value)
2311 break;
2313 TREE_VALUE (*d) = newval;
2316 /* Compares the parameter-type-lists of ONE and TWO and
2317 returns false if they are different. If the DECLs are template
2318 functions, the return types and the template parameter lists are
2319 compared too (DR 565). */
2321 static bool
2322 matching_fn_p (tree one, tree two)
2324 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (one)),
2325 TYPE_ARG_TYPES (TREE_TYPE (two))))
2326 return false;
2328 if (TREE_CODE (one) == TEMPLATE_DECL
2329 && TREE_CODE (two) == TEMPLATE_DECL)
2331 /* Compare template parms. */
2332 if (!comp_template_parms (DECL_TEMPLATE_PARMS (one),
2333 DECL_TEMPLATE_PARMS (two)))
2334 return false;
2336 /* And return type. */
2337 if (!same_type_p (TREE_TYPE (TREE_TYPE (one)),
2338 TREE_TYPE (TREE_TYPE (two))))
2339 return false;
2342 return true;
2345 /* Push DECL into nonclass LEVEL BINDING or SLOT. OLD is the current
2346 binding value (possibly with anticipated builtins stripped).
2347 Diagnose conflicts and return updated decl. */
2349 static tree
2350 update_binding (cp_binding_level *level, cxx_binding *binding, tree *slot,
2351 tree old, tree decl, bool is_friend)
2353 tree to_val = decl;
2354 tree old_type = slot ? MAYBE_STAT_TYPE (*slot) : binding->type;
2355 tree to_type = old_type;
2357 gcc_assert (level->kind == sk_namespace ? !binding
2358 : level->kind != sk_class && !slot);
2359 if (old == error_mark_node)
2360 old = NULL_TREE;
2362 if (TREE_CODE (decl) == TYPE_DECL && DECL_ARTIFICIAL (decl))
2364 tree other = to_type;
2366 if (old && TREE_CODE (old) == TYPE_DECL && DECL_ARTIFICIAL (old))
2367 other = old;
2369 /* Pushing an artificial typedef. See if this matches either
2370 the type slot or the old value slot. */
2371 if (!other)
2373 else if (same_type_p (TREE_TYPE (other), TREE_TYPE (decl)))
2374 /* Two artificial decls to same type. Do nothing. */
2375 return other;
2376 else
2377 goto conflict;
2379 if (old)
2381 /* Slide decl into the type slot, keep old unaltered */
2382 to_type = decl;
2383 to_val = old;
2384 goto done;
2388 if (old && TREE_CODE (old) == TYPE_DECL && DECL_ARTIFICIAL (old))
2390 /* Slide old into the type slot. */
2391 to_type = old;
2392 old = NULL_TREE;
2395 if (DECL_DECLARES_FUNCTION_P (decl))
2397 if (!old)
2399 else if (OVL_P (old))
2401 for (ovl_iterator iter (old); iter; ++iter)
2403 tree fn = *iter;
2405 if (iter.using_p () && matching_fn_p (fn, decl))
2407 /* If a function declaration in namespace scope or
2408 block scope has the same name and the same
2409 parameter-type- list (8.3.5) as a function
2410 introduced by a using-declaration, and the
2411 declarations do not declare the same function,
2412 the program is ill-formed. [namespace.udecl]/14 */
2413 if (tree match = duplicate_decls (decl, fn, is_friend))
2414 return match;
2415 else
2416 /* FIXME: To preserve existing error behavior, we
2417 still push the decl. This might change. */
2418 diagnose_name_conflict (decl, fn);
2422 else
2423 goto conflict;
2425 if (to_type != old_type
2426 && warn_shadow
2427 && MAYBE_CLASS_TYPE_P (TREE_TYPE (to_type))
2428 && !(DECL_IN_SYSTEM_HEADER (decl)
2429 && DECL_IN_SYSTEM_HEADER (to_type)))
2430 warning (OPT_Wshadow, "%q#D hides constructor for %q#D",
2431 decl, to_type);
2433 to_val = ovl_insert (decl, old);
2435 else if (!old)
2437 else if (TREE_CODE (old) != TREE_CODE (decl))
2438 /* Different kinds of decls conflict. */
2439 goto conflict;
2440 else if (TREE_CODE (old) == TYPE_DECL)
2442 if (same_type_p (TREE_TYPE (old), TREE_TYPE (decl)))
2443 /* Two type decls to the same type. Do nothing. */
2444 return old;
2445 else
2446 goto conflict;
2448 else if (TREE_CODE (old) == NAMESPACE_DECL)
2450 /* Two maybe-aliased namespaces. If they're to the same target
2451 namespace, that's ok. */
2452 if (ORIGINAL_NAMESPACE (old) != ORIGINAL_NAMESPACE (decl))
2453 goto conflict;
2455 /* The new one must be an alias at this point. */
2456 gcc_assert (DECL_NAMESPACE_ALIAS (decl));
2457 return old;
2459 else if (TREE_CODE (old) == VAR_DECL)
2461 /* There can be two block-scope declarations of the same
2462 variable, so long as they are `extern' declarations. */
2463 if (!DECL_EXTERNAL (old) || !DECL_EXTERNAL (decl))
2464 goto conflict;
2465 else if (tree match = duplicate_decls (decl, old, false))
2466 return match;
2467 else
2468 goto conflict;
2470 else
2472 conflict:
2473 diagnose_name_conflict (decl, old);
2474 to_val = NULL_TREE;
2477 done:
2478 if (to_val)
2480 if (level->kind != sk_namespace
2481 && !to_type && binding->value && OVL_P (to_val))
2482 update_local_overload (binding, to_val);
2483 else
2485 tree to_add = to_val;
2487 if (level->kind == sk_namespace)
2488 to_add = decl;
2489 else if (to_type == decl)
2490 to_add = decl;
2491 else if (TREE_CODE (to_add) == OVERLOAD)
2492 to_add = build_tree_list (NULL_TREE, to_add);
2494 add_decl_to_level (level, to_add);
2497 if (slot)
2499 if (STAT_HACK_P (*slot))
2501 STAT_TYPE (*slot) = to_type;
2502 STAT_DECL (*slot) = to_val;
2504 else if (to_type)
2505 *slot = stat_hack (to_val, to_type);
2506 else
2507 *slot = to_val;
2509 else
2511 binding->type = to_type;
2512 binding->value = to_val;
2516 return decl;
2519 /* Map of identifiers to extern C functions (or LISTS thereof). */
2521 static GTY(()) hash_map<lang_identifier *, tree> *extern_c_fns;
2523 /* DECL has C linkage. If we have an existing instance, make sure it
2524 has the same exception specification [7.5, 7.6]. If there's no
2525 instance, add DECL to the map. */
2527 static void
2528 check_extern_c_conflict (tree decl)
2530 /* Ignore artificial or system header decls. */
2531 if (DECL_ARTIFICIAL (decl) || DECL_IN_SYSTEM_HEADER (decl))
2532 return;
2534 if (!extern_c_fns)
2535 extern_c_fns = hash_map<lang_identifier *,tree>::create_ggc (127);
2537 bool existed;
2538 tree *slot = &extern_c_fns->get_or_insert (DECL_NAME (decl), &existed);
2539 if (!existed)
2540 *slot = decl;
2541 else
2543 tree old = *slot;
2544 if (TREE_CODE (old) == TREE_LIST)
2545 old = TREE_VALUE (old);
2547 int mismatch = 0;
2548 if (DECL_CONTEXT (old) == DECL_CONTEXT (decl))
2549 ; /* If they're in the same context, we'll have already complained
2550 about a (possible) mismatch, when inserting the decl. */
2551 else if (!decls_match (decl, old))
2552 mismatch = 1;
2553 else if (!comp_except_specs (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (old)),
2554 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl)),
2555 ce_normal))
2556 mismatch = -1;
2557 else if (DECL_ASSEMBLER_NAME_SET_P (old))
2558 SET_DECL_ASSEMBLER_NAME (decl, DECL_ASSEMBLER_NAME (old));
2560 if (mismatch)
2562 pedwarn (input_location, 0,
2563 "declaration of %q#D with C language linkage", decl);
2564 pedwarn (DECL_SOURCE_LOCATION (old), 0,
2565 "conflicts with previous declaration %q#D", old);
2566 if (mismatch < 0)
2567 pedwarn (input_location, 0,
2568 "due to different exception specifications");
2570 else
2571 /* Chain it on for c_linkage_binding's use. */
2572 *slot = tree_cons (NULL_TREE, decl, *slot);
2576 /* Returns a list of C-linkage decls with the name NAME. Used in
2577 c-family/c-pragma.c to implement redefine_extname pragma. */
2579 tree
2580 c_linkage_bindings (tree name)
2582 if (extern_c_fns)
2583 if (tree *slot = extern_c_fns->get (name))
2584 return *slot;
2585 return NULL_TREE;
2588 /* DECL is being declared at a local scope. Emit suitable shadow
2589 warnings. */
2591 static void
2592 check_local_shadow (tree decl)
2594 /* Don't complain about the parms we push and then pop
2595 while tentatively parsing a function declarator. */
2596 if (TREE_CODE (decl) == PARM_DECL && !DECL_CONTEXT (decl))
2597 return;
2599 /* Inline decls shadow nothing. */
2600 if (DECL_FROM_INLINE (decl))
2601 return;
2603 /* External decls are something else. */
2604 if (DECL_EXTERNAL (decl))
2605 return;
2607 tree old = NULL_TREE;
2608 cp_binding_level *old_scope = NULL;
2609 if (cxx_binding *binding = outer_binding (DECL_NAME (decl), NULL, true))
2611 old = binding->value;
2612 old_scope = binding->scope;
2614 while (old && VAR_P (old) && DECL_DEAD_FOR_LOCAL (old))
2615 old = DECL_SHADOWED_FOR_VAR (old);
2617 tree shadowed = NULL_TREE;
2618 if (old
2619 && (TREE_CODE (old) == PARM_DECL
2620 || VAR_P (old)
2621 || (TREE_CODE (old) == TYPE_DECL
2622 && (!DECL_ARTIFICIAL (old)
2623 || TREE_CODE (decl) == TYPE_DECL)))
2624 && (!DECL_ARTIFICIAL (decl)
2625 || DECL_IMPLICIT_TYPEDEF_P (decl)
2626 || (VAR_P (decl) && DECL_ANON_UNION_VAR_P (decl))))
2628 /* DECL shadows a local thing possibly of interest. */
2630 /* Don't complain if it's from an enclosing function. */
2631 if (DECL_CONTEXT (old) == current_function_decl
2632 && TREE_CODE (decl) != PARM_DECL
2633 && TREE_CODE (old) == PARM_DECL)
2635 /* Go to where the parms should be and see if we find
2636 them there. */
2637 cp_binding_level *b = current_binding_level->level_chain;
2639 if (FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
2640 /* Skip the ctor/dtor cleanup level. */
2641 b = b->level_chain;
2643 /* ARM $8.3 */
2644 if (b->kind == sk_function_parms)
2646 error ("declaration of %q#D shadows a parameter", decl);
2647 return;
2651 /* The local structure or class can't use parameters of
2652 the containing function anyway. */
2653 if (DECL_CONTEXT (old) != current_function_decl)
2655 for (cp_binding_level *scope = current_binding_level;
2656 scope != old_scope; scope = scope->level_chain)
2657 if (scope->kind == sk_class
2658 && !LAMBDA_TYPE_P (scope->this_entity))
2659 return;
2661 /* Error if redeclaring a local declared in a
2662 init-statement or in the condition of an if or
2663 switch statement when the new declaration is in the
2664 outermost block of the controlled statement.
2665 Redeclaring a variable from a for or while condition is
2666 detected elsewhere. */
2667 else if (VAR_P (old)
2668 && old_scope == current_binding_level->level_chain
2669 && (old_scope->kind == sk_cond || old_scope->kind == sk_for))
2671 error ("redeclaration of %q#D", decl);
2672 inform (DECL_SOURCE_LOCATION (old),
2673 "%q#D previously declared here", old);
2674 return;
2676 /* C++11:
2677 3.3.3/3: The name declared in an exception-declaration (...)
2678 shall not be redeclared in the outermost block of the handler.
2679 3.3.3/2: A parameter name shall not be redeclared (...) in
2680 the outermost block of any handler associated with a
2681 function-try-block.
2682 3.4.1/15: The function parameter names shall not be redeclared
2683 in the exception-declaration nor in the outermost block of a
2684 handler for the function-try-block. */
2685 else if ((TREE_CODE (old) == VAR_DECL
2686 && old_scope == current_binding_level->level_chain
2687 && old_scope->kind == sk_catch)
2688 || (TREE_CODE (old) == PARM_DECL
2689 && (current_binding_level->kind == sk_catch
2690 || current_binding_level->level_chain->kind == sk_catch)
2691 && in_function_try_handler))
2693 if (permerror (input_location, "redeclaration of %q#D", decl))
2694 inform (DECL_SOURCE_LOCATION (old),
2695 "%q#D previously declared here", old);
2696 return;
2699 /* If '-Wshadow=compatible-local' is specified without other
2700 -Wshadow= flags, we will warn only when the type of the
2701 shadowing variable (DECL) can be converted to that of the
2702 shadowed parameter (OLD_LOCAL). The reason why we only check
2703 if DECL's type can be converted to OLD_LOCAL's type (but not the
2704 other way around) is because when users accidentally shadow a
2705 parameter, more than often they would use the variable
2706 thinking (mistakenly) it's still the parameter. It would be
2707 rare that users would use the variable in the place that
2708 expects the parameter but thinking it's a new decl. */
2710 enum opt_code warning_code;
2711 if (warn_shadow)
2712 warning_code = OPT_Wshadow;
2713 else if (warn_shadow_local)
2714 warning_code = OPT_Wshadow_local;
2715 else if (warn_shadow_compatible_local
2716 && can_convert (TREE_TYPE (old), TREE_TYPE (decl), tf_none))
2717 warning_code = OPT_Wshadow_compatible_local;
2718 else
2719 return;
2721 const char *msg;
2722 if (TREE_CODE (old) == PARM_DECL)
2723 msg = "declaration of %q#D shadows a parameter";
2724 else if (is_capture_proxy (old))
2725 msg = "declaration of %qD shadows a lambda capture";
2726 else
2727 msg = "declaration of %qD shadows a previous local";
2729 if (warning_at (input_location, warning_code, msg, decl))
2731 shadowed = old;
2732 goto inform_shadowed;
2734 return;
2737 if (!warn_shadow)
2738 return;
2740 /* Don't warn for artificial things that are not implicit typedefs. */
2741 if (DECL_ARTIFICIAL (decl) && !DECL_IMPLICIT_TYPEDEF_P (decl))
2742 return;
2744 if (nonlambda_method_basetype ())
2745 if (tree member = lookup_member (current_nonlambda_class_type (),
2746 DECL_NAME (decl), /*protect=*/0,
2747 /*want_type=*/false, tf_warning_or_error))
2749 member = MAYBE_BASELINK_FUNCTIONS (member);
2751 /* Warn if a variable shadows a non-function, or the variable
2752 is a function or a pointer-to-function. */
2753 if (!OVL_P (member)
2754 || TREE_CODE (decl) == FUNCTION_DECL
2755 || TYPE_PTRFN_P (TREE_TYPE (decl))
2756 || TYPE_PTRMEMFUNC_P (TREE_TYPE (decl)))
2758 if (warning_at (input_location, OPT_Wshadow,
2759 "declaration of %qD shadows a member of %qT",
2760 decl, current_nonlambda_class_type ())
2761 && DECL_P (member))
2763 shadowed = member;
2764 goto inform_shadowed;
2767 return;
2770 /* Now look for a namespace shadow. */
2771 old = find_namespace_value (current_namespace, DECL_NAME (decl));
2772 if (old
2773 && (VAR_P (old)
2774 || (TREE_CODE (old) == TYPE_DECL
2775 && (!DECL_ARTIFICIAL (old)
2776 || TREE_CODE (decl) == TYPE_DECL)))
2777 && !instantiating_current_function_p ())
2778 /* XXX shadow warnings in outer-more namespaces */
2780 if (warning_at (input_location, OPT_Wshadow,
2781 "declaration of %qD shadows a global declaration",
2782 decl))
2784 shadowed = old;
2785 goto inform_shadowed;
2787 return;
2790 return;
2792 inform_shadowed:
2793 inform (DECL_SOURCE_LOCATION (shadowed), "shadowed declaration is here");
2796 /* DECL is being pushed inside function CTX. Set its context, if
2797 needed. */
2799 static void
2800 set_decl_context_in_fn (tree ctx, tree decl)
2802 if (!DECL_CONTEXT (decl)
2803 /* A local declaration for a function doesn't constitute
2804 nesting. */
2805 && TREE_CODE (decl) != FUNCTION_DECL
2806 /* A local declaration for an `extern' variable is in the
2807 scope of the current namespace, not the current
2808 function. */
2809 && !(VAR_P (decl) && DECL_EXTERNAL (decl))
2810 /* When parsing the parameter list of a function declarator,
2811 don't set DECL_CONTEXT to an enclosing function. When we
2812 push the PARM_DECLs in order to process the function body,
2813 current_binding_level->this_entity will be set. */
2814 && !(TREE_CODE (decl) == PARM_DECL
2815 && current_binding_level->kind == sk_function_parms
2816 && current_binding_level->this_entity == NULL))
2817 DECL_CONTEXT (decl) = ctx;
2819 /* If this is the declaration for a namespace-scope function,
2820 but the declaration itself is in a local scope, mark the
2821 declaration. */
2822 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (decl))
2823 DECL_LOCAL_FUNCTION_P (decl) = 1;
2826 /* DECL is a local-scope decl with linkage. SHADOWED is true if the
2827 name is already bound at the current level.
2829 [basic.link] If there is a visible declaration of an entity with
2830 linkage having the same name and type, ignoring entities declared
2831 outside the innermost enclosing namespace scope, the block scope
2832 declaration declares that same entity and receives the linkage of
2833 the previous declaration.
2835 Also, make sure that this decl matches any existing external decl
2836 in the enclosing namespace. */
2838 static void
2839 set_local_extern_decl_linkage (tree decl, bool shadowed)
2841 tree ns_value = decl; /* Unique marker. */
2843 if (!shadowed)
2845 tree loc_value = innermost_non_namespace_value (DECL_NAME (decl));
2846 if (!loc_value)
2848 ns_value
2849 = find_namespace_value (current_namespace, DECL_NAME (decl));
2850 loc_value = ns_value;
2852 if (loc_value == error_mark_node)
2853 loc_value = NULL_TREE;
2855 for (ovl_iterator iter (loc_value); iter; ++iter)
2856 if (!iter.hidden_p ()
2857 && (TREE_STATIC (*iter) || DECL_EXTERNAL (*iter))
2858 && decls_match (*iter, decl))
2860 /* The standard only says that the local extern inherits
2861 linkage from the previous decl; in particular, default
2862 args are not shared. Add the decl into a hash table to
2863 make sure only the previous decl in this case is seen
2864 by the middle end. */
2865 struct cxx_int_tree_map *h;
2867 /* We inherit the outer decl's linkage. But we're a
2868 different decl. */
2869 TREE_PUBLIC (decl) = TREE_PUBLIC (*iter);
2871 if (cp_function_chain->extern_decl_map == NULL)
2872 cp_function_chain->extern_decl_map
2873 = hash_table<cxx_int_tree_map_hasher>::create_ggc (20);
2875 h = ggc_alloc<cxx_int_tree_map> ();
2876 h->uid = DECL_UID (decl);
2877 h->to = *iter;
2878 cxx_int_tree_map **loc = cp_function_chain->extern_decl_map
2879 ->find_slot (h, INSERT);
2880 *loc = h;
2881 break;
2885 if (TREE_PUBLIC (decl))
2887 /* DECL is externally visible. Make sure it matches a matching
2888 decl in the namespace scope. We only really need to check
2889 this when inserting the decl, not when we find an existing
2890 match in the current scope. However, in practice we're
2891 going to be inserting a new decl in the majority of cases --
2892 who writes multiple extern decls for the same thing in the
2893 same local scope? Doing it here often avoids a duplicate
2894 namespace lookup. */
2896 /* Avoid repeating a lookup. */
2897 if (ns_value == decl)
2898 ns_value = find_namespace_value (current_namespace, DECL_NAME (decl));
2900 if (ns_value == error_mark_node)
2901 ns_value = NULL_TREE;
2903 for (ovl_iterator iter (ns_value); iter; ++iter)
2905 tree other = *iter;
2907 if (!(TREE_PUBLIC (other) || DECL_EXTERNAL (other)))
2908 ; /* Not externally visible. */
2909 else if (DECL_EXTERN_C_P (decl) && DECL_EXTERN_C_P (other))
2910 ; /* Both are extern "C", we'll check via that mechanism. */
2911 else if (TREE_CODE (other) != TREE_CODE (decl)
2912 || ((VAR_P (decl) || matching_fn_p (other, decl))
2913 && !comptypes (TREE_TYPE (decl), TREE_TYPE (other),
2914 COMPARE_REDECLARATION)))
2916 if (permerror (DECL_SOURCE_LOCATION (decl),
2917 "local external declaration %q#D", decl))
2918 inform (DECL_SOURCE_LOCATION (other),
2919 "does not match previous declaration %q#D", other);
2920 break;
2926 /* Record DECL as belonging to the current lexical scope. Check for
2927 errors (such as an incompatible declaration for the same name
2928 already seen in the same scope). IS_FRIEND is true if DECL is
2929 declared as a friend.
2931 Returns either DECL or an old decl for the same name. If an old
2932 decl is returned, it may have been smashed to agree with what DECL
2933 says. */
2935 static tree
2936 do_pushdecl (tree decl, bool is_friend)
2938 if (decl == error_mark_node)
2939 return error_mark_node;
2941 if (!DECL_TEMPLATE_PARM_P (decl) && current_function_decl)
2942 set_decl_context_in_fn (current_function_decl, decl);
2944 /* The binding level we will be pushing into. During local class
2945 pushing, we want to push to the containing scope. */
2946 cp_binding_level *level = current_binding_level;
2947 while (level->kind == sk_class)
2948 level = level->level_chain;
2950 if (tree name = DECL_NAME (decl))
2952 cxx_binding *binding = NULL; /* Local scope binding. */
2953 tree ns = NULL_TREE; /* Searched namespace. */
2954 tree *slot = NULL; /* Binding slot in namespace. */
2955 tree old = NULL_TREE;
2957 if (level->kind == sk_namespace)
2959 /* We look in the decl's namespace for an existing
2960 declaration, even though we push into the current
2961 namespace. */
2962 ns = (DECL_NAMESPACE_SCOPE_P (decl)
2963 ? CP_DECL_CONTEXT (decl) : current_namespace);
2964 /* Create the binding, if this is current namespace, because
2965 that's where we'll be pushing anyway. */
2966 slot = find_namespace_slot (ns, name, ns == current_namespace);
2967 if (slot)
2968 old = MAYBE_STAT_DECL (*slot);
2970 else
2972 binding = find_local_binding (level, name);
2973 if (binding)
2974 old = binding->value;
2977 if (current_function_decl && VAR_OR_FUNCTION_DECL_P (decl)
2978 && DECL_EXTERNAL (decl))
2979 set_local_extern_decl_linkage (decl, old != NULL_TREE);
2981 if (old == error_mark_node)
2982 old = NULL_TREE;
2984 for (ovl_iterator iter (old); iter; ++iter)
2985 if (iter.using_p ())
2986 ; /* Ignore using decls here. */
2987 else if (tree match = duplicate_decls (decl, *iter, is_friend))
2989 if (match == error_mark_node)
2991 else if (TREE_CODE (match) == TYPE_DECL)
2992 /* The IDENTIFIER will have the type referring to the
2993 now-smashed TYPE_DECL, because ...? Reset it. */
2994 SET_IDENTIFIER_TYPE_VALUE (name, TREE_TYPE (match));
2995 else if (iter.hidden_p () && !DECL_HIDDEN_P (match))
2997 /* Unhiding a previously hidden decl. */
2998 tree head = iter.reveal_node (old);
2999 if (head != old)
3001 if (!ns)
3003 update_local_overload (binding, head);
3004 binding->value = head;
3006 else if (STAT_HACK_P (*slot))
3007 STAT_DECL (*slot) = head;
3008 else
3009 *slot = head;
3011 if (TREE_CODE (match) == FUNCTION_DECL
3012 && DECL_EXTERN_C_P (match))
3013 /* We need to check and register the fn now. */
3014 check_extern_c_conflict (match);
3016 return match;
3019 /* We are pushing a new decl. */
3021 /* Skip a hidden builtin we failed to match already. There can
3022 only be one. */
3023 if (old && anticipated_builtin_p (old))
3024 old = OVL_CHAIN (old);
3026 check_template_shadow (decl);
3028 if (DECL_DECLARES_FUNCTION_P (decl))
3030 check_default_args (decl);
3032 if (is_friend)
3034 if (level->kind != sk_namespace)
3035 /* In a local class, a friend function declaration must
3036 find a matching decl in the innermost non-class scope.
3037 [class.friend/11] */
3038 error ("friend declaration %qD in local class without "
3039 "prior local declaration", decl);
3040 else if (!flag_friend_injection)
3041 /* Hide it from ordinary lookup. */
3042 DECL_ANTICIPATED (decl) = DECL_HIDDEN_FRIEND_P (decl) = true;
3046 if (level->kind != sk_namespace)
3048 check_local_shadow (decl);
3050 if (TREE_CODE (decl) == NAMESPACE_DECL)
3051 /* A local namespace alias. */
3052 set_identifier_type_value (name, NULL_TREE);
3054 if (!binding)
3055 binding = create_local_binding (level, name);
3057 else if (!slot)
3059 ns = current_namespace;
3060 slot = find_namespace_slot (ns, name, true);
3061 /* Update OLD to reflect the namespace we're going to be
3062 pushing into. */
3063 old = MAYBE_STAT_DECL (*slot);
3066 old = update_binding (level, binding, slot, old, decl, is_friend);
3068 if (old != decl)
3069 /* An existing decl matched, use it. */
3070 decl = old;
3071 else if (TREE_CODE (decl) == TYPE_DECL)
3073 tree type = TREE_TYPE (decl);
3075 if (type != error_mark_node)
3077 if (TYPE_NAME (type) != decl)
3078 set_underlying_type (decl);
3080 if (!ns)
3081 set_identifier_type_value_with_scope (name, decl, level);
3082 else
3083 SET_IDENTIFIER_TYPE_VALUE (name, global_type_node);
3086 /* If this is a locally defined typedef in a function that
3087 is not a template instantation, record it to implement
3088 -Wunused-local-typedefs. */
3089 if (!instantiating_current_function_p ())
3090 record_locally_defined_typedef (decl);
3092 else if (VAR_P (decl))
3093 maybe_register_incomplete_var (decl);
3094 else if (TREE_CODE (decl) == FUNCTION_DECL && DECL_EXTERN_C_P (decl))
3095 check_extern_c_conflict (decl);
3097 else
3098 add_decl_to_level (level, decl);
3100 return decl;
3103 /* Record a decl-node X as belonging to the current lexical scope.
3104 It's a friend if IS_FRIEND is true -- which affects exactly where
3105 we push it. */
3107 tree
3108 pushdecl (tree x, bool is_friend)
3110 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3111 tree ret = do_pushdecl (x, is_friend);
3112 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3113 return ret;
3116 /* Enter DECL into the symbol table, if that's appropriate. Returns
3117 DECL, or a modified version thereof. */
3119 tree
3120 maybe_push_decl (tree decl)
3122 tree type = TREE_TYPE (decl);
3124 /* Add this decl to the current binding level, but not if it comes
3125 from another scope, e.g. a static member variable. TEM may equal
3126 DECL or it may be a previous decl of the same name. */
3127 if (decl == error_mark_node
3128 || (TREE_CODE (decl) != PARM_DECL
3129 && DECL_CONTEXT (decl) != NULL_TREE
3130 /* Definitions of namespace members outside their namespace are
3131 possible. */
3132 && !DECL_NAMESPACE_SCOPE_P (decl))
3133 || (TREE_CODE (decl) == TEMPLATE_DECL && !namespace_bindings_p ())
3134 || type == unknown_type_node
3135 /* The declaration of a template specialization does not affect
3136 the functions available for overload resolution, so we do not
3137 call pushdecl. */
3138 || (TREE_CODE (decl) == FUNCTION_DECL
3139 && DECL_TEMPLATE_SPECIALIZATION (decl)))
3140 return decl;
3141 else
3142 return pushdecl (decl);
3145 /* Bind DECL to ID in the current_binding_level, assumed to be a local
3146 binding level. If IS_USING is true, DECL got here through a
3147 using-declaration. */
3149 static void
3150 push_local_binding (tree id, tree decl, bool is_using)
3152 /* Skip over any local classes. This makes sense if we call
3153 push_local_binding with a friend decl of a local class. */
3154 cp_binding_level *b = innermost_nonclass_level ();
3156 gcc_assert (b->kind != sk_namespace);
3157 if (find_local_binding (b, id))
3159 /* Supplement the existing binding. */
3160 if (!supplement_binding (IDENTIFIER_BINDING (id), decl))
3161 /* It didn't work. Something else must be bound at this
3162 level. Do not add DECL to the list of things to pop
3163 later. */
3164 return;
3166 else
3167 /* Create a new binding. */
3168 push_binding (id, decl, b);
3170 if (TREE_CODE (decl) == OVERLOAD || is_using)
3171 /* We must put the OVERLOAD or using into a TREE_LIST since we
3172 cannot use the decl's chain itself. */
3173 decl = build_tree_list (NULL_TREE, decl);
3175 /* And put DECL on the list of things declared by the current
3176 binding level. */
3177 add_decl_to_level (b, decl);
3180 /* Check to see whether or not DECL is a variable that would have been
3181 in scope under the ARM, but is not in scope under the ANSI/ISO
3182 standard. If so, issue an error message. If name lookup would
3183 work in both cases, but return a different result, this function
3184 returns the result of ANSI/ISO lookup. Otherwise, it returns
3185 DECL. */
3187 tree
3188 check_for_out_of_scope_variable (tree decl)
3190 tree shadowed;
3192 /* We only care about out of scope variables. */
3193 if (!(VAR_P (decl) && DECL_DEAD_FOR_LOCAL (decl)))
3194 return decl;
3196 shadowed = DECL_HAS_SHADOWED_FOR_VAR_P (decl)
3197 ? DECL_SHADOWED_FOR_VAR (decl) : NULL_TREE ;
3198 while (shadowed != NULL_TREE && VAR_P (shadowed)
3199 && DECL_DEAD_FOR_LOCAL (shadowed))
3200 shadowed = DECL_HAS_SHADOWED_FOR_VAR_P (shadowed)
3201 ? DECL_SHADOWED_FOR_VAR (shadowed) : NULL_TREE;
3202 if (!shadowed)
3203 shadowed = find_namespace_value (current_namespace, DECL_NAME (decl));
3204 if (shadowed)
3206 if (!DECL_ERROR_REPORTED (decl))
3208 warning (0, "name lookup of %qD changed", DECL_NAME (decl));
3209 warning_at (DECL_SOURCE_LOCATION (shadowed), 0,
3210 " matches this %qD under ISO standard rules",
3211 shadowed);
3212 warning_at (DECL_SOURCE_LOCATION (decl), 0,
3213 " matches this %qD under old rules", decl);
3214 DECL_ERROR_REPORTED (decl) = 1;
3216 return shadowed;
3219 /* If we have already complained about this declaration, there's no
3220 need to do it again. */
3221 if (DECL_ERROR_REPORTED (decl))
3222 return decl;
3224 DECL_ERROR_REPORTED (decl) = 1;
3226 if (TREE_TYPE (decl) == error_mark_node)
3227 return decl;
3229 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
3231 error ("name lookup of %qD changed for ISO %<for%> scoping",
3232 DECL_NAME (decl));
3233 error (" cannot use obsolete binding at %q+D because "
3234 "it has a destructor", decl);
3235 return error_mark_node;
3237 else
3239 permerror (input_location, "name lookup of %qD changed for ISO %<for%> scoping",
3240 DECL_NAME (decl));
3241 if (flag_permissive)
3242 permerror (DECL_SOURCE_LOCATION (decl),
3243 " using obsolete binding at %qD", decl);
3244 else
3246 static bool hint;
3247 if (!hint)
3249 inform (input_location, "(if you use %<-fpermissive%> G++ will accept your code)");
3250 hint = true;
3255 return decl;
3258 /* true means unconditionally make a BLOCK for the next level pushed. */
3260 static bool keep_next_level_flag;
3262 static int binding_depth = 0;
3264 static void
3265 indent (int depth)
3267 int i;
3269 for (i = 0; i < depth * 2; i++)
3270 putc (' ', stderr);
3273 /* Return a string describing the kind of SCOPE we have. */
3274 static const char *
3275 cp_binding_level_descriptor (cp_binding_level *scope)
3277 /* The order of this table must match the "scope_kind"
3278 enumerators. */
3279 static const char* scope_kind_names[] = {
3280 "block-scope",
3281 "cleanup-scope",
3282 "try-scope",
3283 "catch-scope",
3284 "for-scope",
3285 "function-parameter-scope",
3286 "class-scope",
3287 "namespace-scope",
3288 "template-parameter-scope",
3289 "template-explicit-spec-scope"
3291 const scope_kind kind = scope->explicit_spec_p
3292 ? sk_template_spec : scope->kind;
3294 return scope_kind_names[kind];
3297 /* Output a debugging information about SCOPE when performing
3298 ACTION at LINE. */
3299 static void
3300 cp_binding_level_debug (cp_binding_level *scope, int line, const char *action)
3302 const char *desc = cp_binding_level_descriptor (scope);
3303 if (scope->this_entity)
3304 verbatim ("%s %<%s(%E)%> %p %d\n", action, desc,
3305 scope->this_entity, (void *) scope, line);
3306 else
3307 verbatim ("%s %s %p %d\n", action, desc, (void *) scope, line);
3310 /* Return the estimated initial size of the hashtable of a NAMESPACE
3311 scope. */
3313 static inline size_t
3314 namespace_scope_ht_size (tree ns)
3316 tree name = DECL_NAME (ns);
3318 return name == std_identifier
3319 ? NAMESPACE_STD_HT_SIZE
3320 : (name == global_identifier
3321 ? GLOBAL_SCOPE_HT_SIZE
3322 : NAMESPACE_ORDINARY_HT_SIZE);
3325 /* A chain of binding_level structures awaiting reuse. */
3327 static GTY((deletable)) cp_binding_level *free_binding_level;
3329 /* Insert SCOPE as the innermost binding level. */
3331 void
3332 push_binding_level (cp_binding_level *scope)
3334 /* Add it to the front of currently active scopes stack. */
3335 scope->level_chain = current_binding_level;
3336 current_binding_level = scope;
3337 keep_next_level_flag = false;
3339 if (ENABLE_SCOPE_CHECKING)
3341 scope->binding_depth = binding_depth;
3342 indent (binding_depth);
3343 cp_binding_level_debug (scope, LOCATION_LINE (input_location),
3344 "push");
3345 binding_depth++;
3349 /* Create a new KIND scope and make it the top of the active scopes stack.
3350 ENTITY is the scope of the associated C++ entity (namespace, class,
3351 function, C++0x enumeration); it is NULL otherwise. */
3353 cp_binding_level *
3354 begin_scope (scope_kind kind, tree entity)
3356 cp_binding_level *scope;
3358 /* Reuse or create a struct for this binding level. */
3359 if (!ENABLE_SCOPE_CHECKING && free_binding_level)
3361 scope = free_binding_level;
3362 free_binding_level = scope->level_chain;
3363 memset (scope, 0, sizeof (cp_binding_level));
3365 else
3366 scope = ggc_cleared_alloc<cp_binding_level> ();
3368 scope->this_entity = entity;
3369 scope->more_cleanups_ok = true;
3370 switch (kind)
3372 case sk_cleanup:
3373 scope->keep = true;
3374 break;
3376 case sk_template_spec:
3377 scope->explicit_spec_p = true;
3378 kind = sk_template_parms;
3379 /* Fall through. */
3380 case sk_template_parms:
3381 case sk_block:
3382 case sk_try:
3383 case sk_catch:
3384 case sk_for:
3385 case sk_cond:
3386 case sk_class:
3387 case sk_scoped_enum:
3388 case sk_function_parms:
3389 case sk_transaction:
3390 case sk_omp:
3391 scope->keep = keep_next_level_flag;
3392 break;
3394 case sk_namespace:
3395 NAMESPACE_LEVEL (entity) = scope;
3396 break;
3398 default:
3399 /* Should not happen. */
3400 gcc_unreachable ();
3401 break;
3403 scope->kind = kind;
3405 push_binding_level (scope);
3407 return scope;
3410 /* We're about to leave current scope. Pop the top of the stack of
3411 currently active scopes. Return the enclosing scope, now active. */
3413 cp_binding_level *
3414 leave_scope (void)
3416 cp_binding_level *scope = current_binding_level;
3418 if (scope->kind == sk_namespace && class_binding_level)
3419 current_binding_level = class_binding_level;
3421 /* We cannot leave a scope, if there are none left. */
3422 if (NAMESPACE_LEVEL (global_namespace))
3423 gcc_assert (!global_scope_p (scope));
3425 if (ENABLE_SCOPE_CHECKING)
3427 indent (--binding_depth);
3428 cp_binding_level_debug (scope, LOCATION_LINE (input_location),
3429 "leave");
3432 /* Move one nesting level up. */
3433 current_binding_level = scope->level_chain;
3435 /* Namespace-scopes are left most probably temporarily, not
3436 completely; they can be reopened later, e.g. in namespace-extension
3437 or any name binding activity that requires us to resume a
3438 namespace. For classes, we cache some binding levels. For other
3439 scopes, we just make the structure available for reuse. */
3440 if (scope->kind != sk_namespace
3441 && scope->kind != sk_class)
3443 scope->level_chain = free_binding_level;
3444 gcc_assert (!ENABLE_SCOPE_CHECKING
3445 || scope->binding_depth == binding_depth);
3446 free_binding_level = scope;
3449 if (scope->kind == sk_class)
3451 /* Reset DEFINING_CLASS_P to allow for reuse of a
3452 class-defining scope in a non-defining context. */
3453 scope->defining_class_p = 0;
3455 /* Find the innermost enclosing class scope, and reset
3456 CLASS_BINDING_LEVEL appropriately. */
3457 class_binding_level = NULL;
3458 for (scope = current_binding_level; scope; scope = scope->level_chain)
3459 if (scope->kind == sk_class)
3461 class_binding_level = scope;
3462 break;
3466 return current_binding_level;
3469 static void
3470 resume_scope (cp_binding_level* b)
3472 /* Resuming binding levels is meant only for namespaces,
3473 and those cannot nest into classes. */
3474 gcc_assert (!class_binding_level);
3475 /* Also, resuming a non-directly nested namespace is a no-no. */
3476 gcc_assert (b->level_chain == current_binding_level);
3477 current_binding_level = b;
3478 if (ENABLE_SCOPE_CHECKING)
3480 b->binding_depth = binding_depth;
3481 indent (binding_depth);
3482 cp_binding_level_debug (b, LOCATION_LINE (input_location), "resume");
3483 binding_depth++;
3487 /* Return the innermost binding level that is not for a class scope. */
3489 static cp_binding_level *
3490 innermost_nonclass_level (void)
3492 cp_binding_level *b;
3494 b = current_binding_level;
3495 while (b->kind == sk_class)
3496 b = b->level_chain;
3498 return b;
3501 /* We're defining an object of type TYPE. If it needs a cleanup, but
3502 we're not allowed to add any more objects with cleanups to the current
3503 scope, create a new binding level. */
3505 void
3506 maybe_push_cleanup_level (tree type)
3508 if (type != error_mark_node
3509 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
3510 && current_binding_level->more_cleanups_ok == 0)
3512 begin_scope (sk_cleanup, NULL);
3513 current_binding_level->statement_list = push_stmt_list ();
3517 /* Return true if we are in the global binding level. */
3519 bool
3520 global_bindings_p (void)
3522 return global_scope_p (current_binding_level);
3525 /* True if we are currently in a toplevel binding level. This
3526 means either the global binding level or a namespace in a toplevel
3527 binding level. Since there are no non-toplevel namespace levels,
3528 this really means any namespace or template parameter level. We
3529 also include a class whose context is toplevel. */
3531 bool
3532 toplevel_bindings_p (void)
3534 cp_binding_level *b = innermost_nonclass_level ();
3536 return b->kind == sk_namespace || b->kind == sk_template_parms;
3539 /* True if this is a namespace scope, or if we are defining a class
3540 which is itself at namespace scope, or whose enclosing class is
3541 such a class, etc. */
3543 bool
3544 namespace_bindings_p (void)
3546 cp_binding_level *b = innermost_nonclass_level ();
3548 return b->kind == sk_namespace;
3551 /* True if the innermost non-class scope is a block scope. */
3553 bool
3554 local_bindings_p (void)
3556 cp_binding_level *b = innermost_nonclass_level ();
3557 return b->kind < sk_function_parms || b->kind == sk_omp;
3560 /* True if the current level needs to have a BLOCK made. */
3562 bool
3563 kept_level_p (void)
3565 return (current_binding_level->blocks != NULL_TREE
3566 || current_binding_level->keep
3567 || current_binding_level->kind == sk_cleanup
3568 || current_binding_level->names != NULL_TREE
3569 || current_binding_level->using_directives);
3572 /* Returns the kind of the innermost scope. */
3574 scope_kind
3575 innermost_scope_kind (void)
3577 return current_binding_level->kind;
3580 /* Returns true if this scope was created to store template parameters. */
3582 bool
3583 template_parm_scope_p (void)
3585 return innermost_scope_kind () == sk_template_parms;
3588 /* If KEEP is true, make a BLOCK node for the next binding level,
3589 unconditionally. Otherwise, use the normal logic to decide whether
3590 or not to create a BLOCK. */
3592 void
3593 keep_next_level (bool keep)
3595 keep_next_level_flag = keep;
3598 /* Return the list of declarations of the current local scope. */
3600 tree
3601 get_local_decls (void)
3603 gcc_assert (current_binding_level->kind != sk_namespace
3604 && current_binding_level->kind != sk_class);
3605 return current_binding_level->names;
3608 /* Return how many function prototypes we are currently nested inside. */
3611 function_parm_depth (void)
3613 int level = 0;
3614 cp_binding_level *b;
3616 for (b = current_binding_level;
3617 b->kind == sk_function_parms;
3618 b = b->level_chain)
3619 ++level;
3621 return level;
3624 /* For debugging. */
3625 static int no_print_functions = 0;
3626 static int no_print_builtins = 0;
3628 static void
3629 print_binding_level (cp_binding_level* lvl)
3631 tree t;
3632 int i = 0, len;
3633 fprintf (stderr, " blocks=%p", (void *) lvl->blocks);
3634 if (lvl->more_cleanups_ok)
3635 fprintf (stderr, " more-cleanups-ok");
3636 if (lvl->have_cleanups)
3637 fprintf (stderr, " have-cleanups");
3638 fprintf (stderr, "\n");
3639 if (lvl->names)
3641 fprintf (stderr, " names:\t");
3642 /* We can probably fit 3 names to a line? */
3643 for (t = lvl->names; t; t = TREE_CHAIN (t))
3645 if (no_print_functions && (TREE_CODE (t) == FUNCTION_DECL))
3646 continue;
3647 if (no_print_builtins
3648 && (TREE_CODE (t) == TYPE_DECL)
3649 && DECL_IS_BUILTIN (t))
3650 continue;
3652 /* Function decls tend to have longer names. */
3653 if (TREE_CODE (t) == FUNCTION_DECL)
3654 len = 3;
3655 else
3656 len = 2;
3657 i += len;
3658 if (i > 6)
3660 fprintf (stderr, "\n\t");
3661 i = len;
3663 print_node_brief (stderr, "", t, 0);
3664 if (t == error_mark_node)
3665 break;
3667 if (i)
3668 fprintf (stderr, "\n");
3670 if (vec_safe_length (lvl->class_shadowed))
3672 size_t i;
3673 cp_class_binding *b;
3674 fprintf (stderr, " class-shadowed:");
3675 FOR_EACH_VEC_ELT (*lvl->class_shadowed, i, b)
3676 fprintf (stderr, " %s ", IDENTIFIER_POINTER (b->identifier));
3677 fprintf (stderr, "\n");
3679 if (lvl->type_shadowed)
3681 fprintf (stderr, " type-shadowed:");
3682 for (t = lvl->type_shadowed; t; t = TREE_CHAIN (t))
3684 fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
3686 fprintf (stderr, "\n");
3690 DEBUG_FUNCTION void
3691 debug (cp_binding_level &ref)
3693 print_binding_level (&ref);
3696 DEBUG_FUNCTION void
3697 debug (cp_binding_level *ptr)
3699 if (ptr)
3700 debug (*ptr);
3701 else
3702 fprintf (stderr, "<nil>\n");
3706 void
3707 print_other_binding_stack (cp_binding_level *stack)
3709 cp_binding_level *level;
3710 for (level = stack; !global_scope_p (level); level = level->level_chain)
3712 fprintf (stderr, "binding level %p\n", (void *) level);
3713 print_binding_level (level);
3717 void
3718 print_binding_stack (void)
3720 cp_binding_level *b;
3721 fprintf (stderr, "current_binding_level=%p\n"
3722 "class_binding_level=%p\n"
3723 "NAMESPACE_LEVEL (global_namespace)=%p\n",
3724 (void *) current_binding_level, (void *) class_binding_level,
3725 (void *) NAMESPACE_LEVEL (global_namespace));
3726 if (class_binding_level)
3728 for (b = class_binding_level; b; b = b->level_chain)
3729 if (b == current_binding_level)
3730 break;
3731 if (b)
3732 b = class_binding_level;
3733 else
3734 b = current_binding_level;
3736 else
3737 b = current_binding_level;
3738 print_other_binding_stack (b);
3739 fprintf (stderr, "global:\n");
3740 print_binding_level (NAMESPACE_LEVEL (global_namespace));
3743 /* Return the type associated with ID. */
3745 static tree
3746 identifier_type_value_1 (tree id)
3748 /* There is no type with that name, anywhere. */
3749 if (REAL_IDENTIFIER_TYPE_VALUE (id) == NULL_TREE)
3750 return NULL_TREE;
3751 /* This is not the type marker, but the real thing. */
3752 if (REAL_IDENTIFIER_TYPE_VALUE (id) != global_type_node)
3753 return REAL_IDENTIFIER_TYPE_VALUE (id);
3754 /* Have to search for it. It must be on the global level, now.
3755 Ask lookup_name not to return non-types. */
3756 id = lookup_name_real (id, 2, 1, /*block_p=*/true, 0, 0);
3757 if (id)
3758 return TREE_TYPE (id);
3759 return NULL_TREE;
3762 /* Wrapper for identifier_type_value_1. */
3764 tree
3765 identifier_type_value (tree id)
3767 tree ret;
3768 timevar_start (TV_NAME_LOOKUP);
3769 ret = identifier_type_value_1 (id);
3770 timevar_stop (TV_NAME_LOOKUP);
3771 return ret;
3775 /* Return the IDENTIFIER_GLOBAL_VALUE of T, for use in common code, since
3776 the definition of IDENTIFIER_GLOBAL_VALUE is different for C and C++. */
3778 tree
3779 identifier_global_value (tree t)
3781 return IDENTIFIER_GLOBAL_VALUE (t);
3784 /* Push a definition of struct, union or enum tag named ID. into
3785 binding_level B. DECL is a TYPE_DECL for the type. We assume that
3786 the tag ID is not already defined. */
3788 static void
3789 set_identifier_type_value_with_scope (tree id, tree decl, cp_binding_level *b)
3791 tree type;
3793 if (b->kind != sk_namespace)
3795 /* Shadow the marker, not the real thing, so that the marker
3796 gets restored later. */
3797 tree old_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
3798 b->type_shadowed
3799 = tree_cons (id, old_type_value, b->type_shadowed);
3800 type = decl ? TREE_TYPE (decl) : NULL_TREE;
3801 TREE_TYPE (b->type_shadowed) = type;
3803 else
3805 tree *slot = find_namespace_slot (current_namespace, id, true);
3806 gcc_assert (decl);
3807 update_binding (b, NULL, slot, MAYBE_STAT_DECL (*slot), decl, false);
3809 /* Store marker instead of real type. */
3810 type = global_type_node;
3812 SET_IDENTIFIER_TYPE_VALUE (id, type);
3815 /* As set_identifier_type_value_with_scope, but using
3816 current_binding_level. */
3818 void
3819 set_identifier_type_value (tree id, tree decl)
3821 set_identifier_type_value_with_scope (id, decl, current_binding_level);
3824 /* Return the name for the constructor (or destructor) for the
3825 specified class. */
3827 tree
3828 constructor_name (tree type)
3830 tree decl = TYPE_NAME (TYPE_MAIN_VARIANT (type));
3832 return decl ? DECL_NAME (decl) : NULL_TREE;
3835 /* Returns TRUE if NAME is the name for the constructor for TYPE,
3836 which must be a class type. */
3838 bool
3839 constructor_name_p (tree name, tree type)
3841 gcc_assert (MAYBE_CLASS_TYPE_P (type));
3843 /* These don't have names. */
3844 if (TREE_CODE (type) == DECLTYPE_TYPE
3845 || TREE_CODE (type) == TYPEOF_TYPE)
3846 return false;
3848 if (name && name == constructor_name (type))
3849 return true;
3851 return false;
3854 /* Counter used to create anonymous type names. */
3856 static GTY(()) int anon_cnt;
3858 /* Return an IDENTIFIER which can be used as a name for
3859 unnamed structs and unions. */
3861 tree
3862 make_anon_name (void)
3864 char buf[32];
3866 sprintf (buf, anon_aggrname_format (), anon_cnt++);
3867 return get_identifier (buf);
3870 /* This code is practically identical to that for creating
3871 anonymous names, but is just used for lambdas instead. This isn't really
3872 necessary, but it's convenient to avoid treating lambdas like other
3873 unnamed types. */
3875 static GTY(()) int lambda_cnt = 0;
3877 tree
3878 make_lambda_name (void)
3880 char buf[32];
3882 sprintf (buf, LAMBDANAME_FORMAT, lambda_cnt++);
3883 return get_identifier (buf);
3886 /* Insert another USING_DECL into the current binding level, returning
3887 this declaration. If this is a redeclaration, do nothing, and
3888 return NULL_TREE if this not in namespace scope (in namespace
3889 scope, a using decl might extend any previous bindings). */
3891 static tree
3892 push_using_decl_1 (tree scope, tree name)
3894 tree decl;
3896 gcc_assert (TREE_CODE (scope) == NAMESPACE_DECL);
3897 gcc_assert (identifier_p (name));
3898 for (decl = current_binding_level->usings; decl; decl = DECL_CHAIN (decl))
3899 if (USING_DECL_SCOPE (decl) == scope && DECL_NAME (decl) == name)
3900 break;
3901 if (decl)
3902 return namespace_bindings_p () ? decl : NULL_TREE;
3903 decl = build_lang_decl (USING_DECL, name, NULL_TREE);
3904 USING_DECL_SCOPE (decl) = scope;
3905 DECL_CHAIN (decl) = current_binding_level->usings;
3906 current_binding_level->usings = decl;
3907 return decl;
3910 /* Wrapper for push_using_decl_1. */
3912 static tree
3913 push_using_decl (tree scope, tree name)
3915 tree ret;
3916 timevar_start (TV_NAME_LOOKUP);
3917 ret = push_using_decl_1 (scope, name);
3918 timevar_stop (TV_NAME_LOOKUP);
3919 return ret;
3922 /* Same as pushdecl, but define X in binding-level LEVEL. We rely on the
3923 caller to set DECL_CONTEXT properly.
3925 Note that this must only be used when X will be the new innermost
3926 binding for its name, as we tack it onto the front of IDENTIFIER_BINDING
3927 without checking to see if the current IDENTIFIER_BINDING comes from a
3928 closer binding level than LEVEL. */
3930 static tree
3931 do_pushdecl_with_scope (tree x, cp_binding_level *level, bool is_friend)
3933 cp_binding_level *b;
3934 tree function_decl = current_function_decl;
3936 current_function_decl = NULL_TREE;
3937 if (level->kind == sk_class)
3939 b = class_binding_level;
3940 class_binding_level = level;
3941 pushdecl_class_level (x);
3942 class_binding_level = b;
3944 else
3946 b = current_binding_level;
3947 current_binding_level = level;
3948 x = pushdecl (x, is_friend);
3949 current_binding_level = b;
3951 current_function_decl = function_decl;
3952 return x;
3955 /* Inject X into the local scope just before the function parms. */
3957 tree
3958 pushdecl_outermost_localscope (tree x)
3960 cp_binding_level *b = NULL;
3961 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3963 /* Find the scope just inside the function parms. */
3964 for (cp_binding_level *n = current_binding_level;
3965 n->kind != sk_function_parms; n = b->level_chain)
3966 b = n;
3968 tree ret = b ? do_pushdecl_with_scope (x, b, false) : error_mark_node;
3969 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3971 return ret;
3974 /* Check a non-member using-declaration. Return the name and scope
3975 being used, and the USING_DECL, or NULL_TREE on failure. */
3977 static tree
3978 validate_nonmember_using_decl (tree decl, tree scope, tree name)
3980 /* [namespace.udecl]
3981 A using-declaration for a class member shall be a
3982 member-declaration. */
3983 if (TYPE_P (scope))
3985 error ("%qT is not a namespace or unscoped enum", scope);
3986 return NULL_TREE;
3988 else if (scope == error_mark_node)
3989 return NULL_TREE;
3991 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR)
3993 /* 7.3.3/5
3994 A using-declaration shall not name a template-id. */
3995 error ("a using-declaration cannot specify a template-id. "
3996 "Try %<using %D%>", name);
3997 return NULL_TREE;
4000 if (TREE_CODE (decl) == NAMESPACE_DECL)
4002 error ("namespace %qD not allowed in using-declaration", decl);
4003 return NULL_TREE;
4006 if (TREE_CODE (decl) == SCOPE_REF)
4008 /* It's a nested name with template parameter dependent scope.
4009 This can only be using-declaration for class member. */
4010 error ("%qT is not a namespace", TREE_OPERAND (decl, 0));
4011 return NULL_TREE;
4014 decl = OVL_FIRST (decl);
4016 /* Make a USING_DECL. */
4017 tree using_decl = push_using_decl (scope, name);
4019 if (using_decl == NULL_TREE
4020 && at_function_scope_p ()
4021 && VAR_P (decl))
4022 /* C++11 7.3.3/10. */
4023 error ("%qD is already declared in this scope", name);
4025 return using_decl;
4028 /* Process a local-scope or namespace-scope using declaration. SCOPE
4029 is the nominated scope to search for NAME. VALUE_P and TYPE_P
4030 point to the binding for NAME in the current scope and are
4031 updated. */
4033 static void
4034 do_nonmember_using_decl (tree scope, tree name, tree *value_p, tree *type_p)
4036 name_lookup lookup (name, 0);
4038 if (!qualified_namespace_lookup (scope, &lookup))
4040 error ("%qD not declared", name);
4041 return;
4043 else if (TREE_CODE (lookup.value) == TREE_LIST)
4045 error ("reference to %qD is ambiguous", name);
4046 print_candidates (lookup.value);
4047 lookup.value = NULL_TREE;
4050 if (lookup.type && TREE_CODE (lookup.type) == TREE_LIST)
4052 error ("reference to %qD is ambiguous", name);
4053 print_candidates (lookup.type);
4054 lookup.type = NULL_TREE;
4057 tree value = *value_p;
4058 tree type = *type_p;
4060 /* Shift the old and new bindings around so we're comparing class and
4061 enumeration names to each other. */
4062 if (value && DECL_IMPLICIT_TYPEDEF_P (value))
4064 type = value;
4065 value = NULL_TREE;
4068 if (lookup.value && DECL_IMPLICIT_TYPEDEF_P (lookup.value))
4070 lookup.type = lookup.value;
4071 lookup.value = NULL_TREE;
4074 if (lookup.value && lookup.value != value)
4076 /* Check for using functions. */
4077 if (OVL_P (lookup.value) && (!value || OVL_P (value)))
4079 for (lkp_iterator usings (lookup.value); usings; ++usings)
4081 tree new_fn = *usings;
4083 /* [namespace.udecl]
4085 If a function declaration in namespace scope or block
4086 scope has the same name and the same parameter types as a
4087 function introduced by a using declaration the program is
4088 ill-formed. */
4089 bool found = false;
4090 for (ovl_iterator old (value); !found && old; ++old)
4092 tree old_fn = *old;
4094 if (new_fn == old_fn)
4095 /* The function already exists in the current
4096 namespace. */
4097 found = true;
4098 else if (old.using_p ())
4099 continue; /* This is a using decl. */
4100 else if (old.hidden_p () && !DECL_HIDDEN_FRIEND_P (old_fn))
4101 continue; /* This is an anticipated builtin. */
4102 else if (!matching_fn_p (new_fn, old_fn))
4103 continue; /* Parameters do not match. */
4104 else if (decls_match (new_fn, old_fn))
4105 found = true;
4106 else
4108 diagnose_name_conflict (new_fn, old_fn);
4109 found = true;
4113 if (!found)
4114 /* Unlike the overload case we don't drop anticipated
4115 builtins here. They don't cause a problem, and
4116 we'd like to match them with a future
4117 declaration. */
4118 value = ovl_insert (new_fn, value, true);
4121 else if (value
4122 /* Ignore anticipated builtins. */
4123 && !anticipated_builtin_p (value)
4124 && !decls_match (lookup.value, value))
4125 diagnose_name_conflict (lookup.value, value);
4126 else
4127 value = lookup.value;
4130 if (lookup.type && lookup.type != type)
4132 if (type && !decls_match (lookup.type, type))
4133 diagnose_name_conflict (lookup.type, type);
4134 else
4135 type = lookup.type;
4138 /* If bind->value is empty, shift any class or enumeration name back. */
4139 if (!value)
4141 value = type;
4142 type = NULL_TREE;
4145 *value_p = value;
4146 *type_p = type;
4149 /* Returns true if ANCESTOR encloses DESCENDANT, including matching.
4150 Both are namespaces. */
4152 bool
4153 is_nested_namespace (tree ancestor, tree descendant, bool inline_only)
4155 int depth = SCOPE_DEPTH (ancestor);
4157 if (!depth && !inline_only)
4158 /* The global namespace encloses everything. */
4159 return true;
4161 while (SCOPE_DEPTH (descendant) > depth
4162 && (!inline_only || DECL_NAMESPACE_INLINE_P (descendant)))
4163 descendant = CP_DECL_CONTEXT (descendant);
4165 return ancestor == descendant;
4168 /* Returns true if ROOT (a namespace, class, or function) encloses
4169 CHILD. CHILD may be either a class type or a namespace. */
4171 bool
4172 is_ancestor (tree root, tree child)
4174 gcc_assert ((TREE_CODE (root) == NAMESPACE_DECL
4175 || TREE_CODE (root) == FUNCTION_DECL
4176 || CLASS_TYPE_P (root)));
4177 gcc_assert ((TREE_CODE (child) == NAMESPACE_DECL
4178 || CLASS_TYPE_P (child)));
4180 /* The global namespace encloses everything. */
4181 if (root == global_namespace)
4182 return true;
4184 /* Search until we reach namespace scope. */
4185 while (TREE_CODE (child) != NAMESPACE_DECL)
4187 /* If we've reached the ROOT, it encloses CHILD. */
4188 if (root == child)
4189 return true;
4190 /* Go out one level. */
4191 if (TYPE_P (child))
4192 child = TYPE_NAME (child);
4193 child = CP_DECL_CONTEXT (child);
4196 if (TREE_CODE (root) == NAMESPACE_DECL)
4197 return is_nested_namespace (root, child);
4199 return false;
4202 /* Enter the class or namespace scope indicated by T suitable for name
4203 lookup. T can be arbitrary scope, not necessary nested inside the
4204 current scope. Returns a non-null scope to pop iff pop_scope
4205 should be called later to exit this scope. */
4207 tree
4208 push_scope (tree t)
4210 if (TREE_CODE (t) == NAMESPACE_DECL)
4211 push_decl_namespace (t);
4212 else if (CLASS_TYPE_P (t))
4214 if (!at_class_scope_p ()
4215 || !same_type_p (current_class_type, t))
4216 push_nested_class (t);
4217 else
4218 /* T is the same as the current scope. There is therefore no
4219 need to re-enter the scope. Since we are not actually
4220 pushing a new scope, our caller should not call
4221 pop_scope. */
4222 t = NULL_TREE;
4225 return t;
4228 /* Leave scope pushed by push_scope. */
4230 void
4231 pop_scope (tree t)
4233 if (t == NULL_TREE)
4234 return;
4235 if (TREE_CODE (t) == NAMESPACE_DECL)
4236 pop_decl_namespace ();
4237 else if CLASS_TYPE_P (t)
4238 pop_nested_class ();
4241 /* Subroutine of push_inner_scope. */
4243 static void
4244 push_inner_scope_r (tree outer, tree inner)
4246 tree prev;
4248 if (outer == inner
4249 || (TREE_CODE (inner) != NAMESPACE_DECL && !CLASS_TYPE_P (inner)))
4250 return;
4252 prev = CP_DECL_CONTEXT (TREE_CODE (inner) == NAMESPACE_DECL ? inner : TYPE_NAME (inner));
4253 if (outer != prev)
4254 push_inner_scope_r (outer, prev);
4255 if (TREE_CODE (inner) == NAMESPACE_DECL)
4257 cp_binding_level *save_template_parm = 0;
4258 /* Temporary take out template parameter scopes. They are saved
4259 in reversed order in save_template_parm. */
4260 while (current_binding_level->kind == sk_template_parms)
4262 cp_binding_level *b = current_binding_level;
4263 current_binding_level = b->level_chain;
4264 b->level_chain = save_template_parm;
4265 save_template_parm = b;
4268 resume_scope (NAMESPACE_LEVEL (inner));
4269 current_namespace = inner;
4271 /* Restore template parameter scopes. */
4272 while (save_template_parm)
4274 cp_binding_level *b = save_template_parm;
4275 save_template_parm = b->level_chain;
4276 b->level_chain = current_binding_level;
4277 current_binding_level = b;
4280 else
4281 pushclass (inner);
4284 /* Enter the scope INNER from current scope. INNER must be a scope
4285 nested inside current scope. This works with both name lookup and
4286 pushing name into scope. In case a template parameter scope is present,
4287 namespace is pushed under the template parameter scope according to
4288 name lookup rule in 14.6.1/6.
4290 Return the former current scope suitable for pop_inner_scope. */
4292 tree
4293 push_inner_scope (tree inner)
4295 tree outer = current_scope ();
4296 if (!outer)
4297 outer = current_namespace;
4299 push_inner_scope_r (outer, inner);
4300 return outer;
4303 /* Exit the current scope INNER back to scope OUTER. */
4305 void
4306 pop_inner_scope (tree outer, tree inner)
4308 if (outer == inner
4309 || (TREE_CODE (inner) != NAMESPACE_DECL && !CLASS_TYPE_P (inner)))
4310 return;
4312 while (outer != inner)
4314 if (TREE_CODE (inner) == NAMESPACE_DECL)
4316 cp_binding_level *save_template_parm = 0;
4317 /* Temporary take out template parameter scopes. They are saved
4318 in reversed order in save_template_parm. */
4319 while (current_binding_level->kind == sk_template_parms)
4321 cp_binding_level *b = current_binding_level;
4322 current_binding_level = b->level_chain;
4323 b->level_chain = save_template_parm;
4324 save_template_parm = b;
4327 pop_namespace ();
4329 /* Restore template parameter scopes. */
4330 while (save_template_parm)
4332 cp_binding_level *b = save_template_parm;
4333 save_template_parm = b->level_chain;
4334 b->level_chain = current_binding_level;
4335 current_binding_level = b;
4338 else
4339 popclass ();
4341 inner = CP_DECL_CONTEXT (TREE_CODE (inner) == NAMESPACE_DECL ? inner : TYPE_NAME (inner));
4345 /* Do a pushlevel for class declarations. */
4347 void
4348 pushlevel_class (void)
4350 class_binding_level = begin_scope (sk_class, current_class_type);
4353 /* ...and a poplevel for class declarations. */
4355 void
4356 poplevel_class (void)
4358 cp_binding_level *level = class_binding_level;
4359 cp_class_binding *cb;
4360 size_t i;
4361 tree shadowed;
4363 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4364 gcc_assert (level != 0);
4366 /* If we're leaving a toplevel class, cache its binding level. */
4367 if (current_class_depth == 1)
4368 previous_class_level = level;
4369 for (shadowed = level->type_shadowed;
4370 shadowed;
4371 shadowed = TREE_CHAIN (shadowed))
4372 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (shadowed), TREE_VALUE (shadowed));
4374 /* Remove the bindings for all of the class-level declarations. */
4375 if (level->class_shadowed)
4377 FOR_EACH_VEC_ELT (*level->class_shadowed, i, cb)
4379 IDENTIFIER_BINDING (cb->identifier) = cb->base->previous;
4380 cxx_binding_free (cb->base);
4382 ggc_free (level->class_shadowed);
4383 level->class_shadowed = NULL;
4386 /* Now, pop out of the binding level which we created up in the
4387 `pushlevel_class' routine. */
4388 gcc_assert (current_binding_level == level);
4389 leave_scope ();
4390 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4393 /* Set INHERITED_VALUE_BINDING_P on BINDING to true or false, as
4394 appropriate. DECL is the value to which a name has just been
4395 bound. CLASS_TYPE is the class in which the lookup occurred. */
4397 static void
4398 set_inherited_value_binding_p (cxx_binding *binding, tree decl,
4399 tree class_type)
4401 if (binding->value == decl && TREE_CODE (decl) != TREE_LIST)
4403 tree context;
4405 if (TREE_CODE (decl) == OVERLOAD)
4406 context = ovl_scope (decl);
4407 else
4409 gcc_assert (DECL_P (decl));
4410 context = context_for_name_lookup (decl);
4413 if (is_properly_derived_from (class_type, context))
4414 INHERITED_VALUE_BINDING_P (binding) = 1;
4415 else
4416 INHERITED_VALUE_BINDING_P (binding) = 0;
4418 else if (binding->value == decl)
4419 /* We only encounter a TREE_LIST when there is an ambiguity in the
4420 base classes. Such an ambiguity can be overridden by a
4421 definition in this class. */
4422 INHERITED_VALUE_BINDING_P (binding) = 1;
4423 else
4424 INHERITED_VALUE_BINDING_P (binding) = 0;
4427 /* Make the declaration of X appear in CLASS scope. */
4429 bool
4430 pushdecl_class_level (tree x)
4432 bool is_valid = true;
4433 bool subtime;
4435 /* Do nothing if we're adding to an outer lambda closure type,
4436 outer_binding will add it later if it's needed. */
4437 if (current_class_type != class_binding_level->this_entity)
4438 return true;
4440 subtime = timevar_cond_start (TV_NAME_LOOKUP);
4441 /* Get the name of X. */
4442 tree name = OVL_NAME (x);
4444 if (name)
4446 is_valid = push_class_level_binding (name, x);
4447 if (TREE_CODE (x) == TYPE_DECL)
4448 set_identifier_type_value (name, x);
4450 else if (ANON_AGGR_TYPE_P (TREE_TYPE (x)))
4452 /* If X is an anonymous aggregate, all of its members are
4453 treated as if they were members of the class containing the
4454 aggregate, for naming purposes. */
4455 tree f;
4457 for (f = TYPE_FIELDS (TREE_TYPE (x)); f; f = DECL_CHAIN (f))
4459 location_t save_location = input_location;
4460 input_location = DECL_SOURCE_LOCATION (f);
4461 if (!pushdecl_class_level (f))
4462 is_valid = false;
4463 input_location = save_location;
4466 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4467 return is_valid;
4470 /* Return the BINDING (if any) for NAME in SCOPE, which is a class
4471 scope. If the value returned is non-NULL, and the PREVIOUS field
4472 is not set, callers must set the PREVIOUS field explicitly. */
4474 static cxx_binding *
4475 get_class_binding (tree name, cp_binding_level *scope)
4477 tree class_type;
4478 tree type_binding;
4479 tree value_binding;
4480 cxx_binding *binding;
4482 class_type = scope->this_entity;
4484 /* Get the type binding. */
4485 type_binding = lookup_member (class_type, name,
4486 /*protect=*/2, /*want_type=*/true,
4487 tf_warning_or_error);
4488 /* Get the value binding. */
4489 value_binding = lookup_member (class_type, name,
4490 /*protect=*/2, /*want_type=*/false,
4491 tf_warning_or_error);
4493 if (value_binding
4494 && (TREE_CODE (value_binding) == TYPE_DECL
4495 || DECL_CLASS_TEMPLATE_P (value_binding)
4496 || (TREE_CODE (value_binding) == TREE_LIST
4497 && TREE_TYPE (value_binding) == error_mark_node
4498 && (TREE_CODE (TREE_VALUE (value_binding))
4499 == TYPE_DECL))))
4500 /* We found a type binding, even when looking for a non-type
4501 binding. This means that we already processed this binding
4502 above. */
4504 else if (value_binding)
4506 if (TREE_CODE (value_binding) == TREE_LIST
4507 && TREE_TYPE (value_binding) == error_mark_node)
4508 /* NAME is ambiguous. */
4510 else if (BASELINK_P (value_binding))
4511 /* NAME is some overloaded functions. */
4512 value_binding = BASELINK_FUNCTIONS (value_binding);
4515 /* If we found either a type binding or a value binding, create a
4516 new binding object. */
4517 if (type_binding || value_binding)
4519 binding = new_class_binding (name,
4520 value_binding,
4521 type_binding,
4522 scope);
4523 /* This is a class-scope binding, not a block-scope binding. */
4524 LOCAL_BINDING_P (binding) = 0;
4525 set_inherited_value_binding_p (binding, value_binding, class_type);
4527 else
4528 binding = NULL;
4530 return binding;
4533 /* Make the declaration(s) of X appear in CLASS scope under the name
4534 NAME. Returns true if the binding is valid. */
4536 static bool
4537 push_class_level_binding_1 (tree name, tree x)
4539 cxx_binding *binding;
4540 tree decl = x;
4541 bool ok;
4543 /* The class_binding_level will be NULL if x is a template
4544 parameter name in a member template. */
4545 if (!class_binding_level)
4546 return true;
4548 if (name == error_mark_node)
4549 return false;
4551 /* Can happen for an erroneous declaration (c++/60384). */
4552 if (!identifier_p (name))
4554 gcc_assert (errorcount || sorrycount);
4555 return false;
4558 /* Check for invalid member names. But don't worry about a default
4559 argument-scope lambda being pushed after the class is complete. */
4560 gcc_assert (TYPE_BEING_DEFINED (current_class_type)
4561 || LAMBDA_TYPE_P (TREE_TYPE (decl)));
4562 /* Check that we're pushing into the right binding level. */
4563 gcc_assert (current_class_type == class_binding_level->this_entity);
4565 /* We could have been passed a tree list if this is an ambiguous
4566 declaration. If so, pull the declaration out because
4567 check_template_shadow will not handle a TREE_LIST. */
4568 if (TREE_CODE (decl) == TREE_LIST
4569 && TREE_TYPE (decl) == error_mark_node)
4570 decl = TREE_VALUE (decl);
4572 if (!check_template_shadow (decl))
4573 return false;
4575 /* [class.mem]
4577 If T is the name of a class, then each of the following shall
4578 have a name different from T:
4580 -- every static data member of class T;
4582 -- every member of class T that is itself a type;
4584 -- every enumerator of every member of class T that is an
4585 enumerated type;
4587 -- every member of every anonymous union that is a member of
4588 class T.
4590 (Non-static data members were also forbidden to have the same
4591 name as T until TC1.) */
4592 if ((VAR_P (x)
4593 || TREE_CODE (x) == CONST_DECL
4594 || (TREE_CODE (x) == TYPE_DECL
4595 && !DECL_SELF_REFERENCE_P (x))
4596 /* A data member of an anonymous union. */
4597 || (TREE_CODE (x) == FIELD_DECL
4598 && DECL_CONTEXT (x) != current_class_type))
4599 && DECL_NAME (x) == DECL_NAME (TYPE_NAME (current_class_type)))
4601 tree scope = context_for_name_lookup (x);
4602 if (TYPE_P (scope) && same_type_p (scope, current_class_type))
4604 error ("%qD has the same name as the class in which it is "
4605 "declared",
4607 return false;
4611 /* Get the current binding for NAME in this class, if any. */
4612 binding = IDENTIFIER_BINDING (name);
4613 if (!binding || binding->scope != class_binding_level)
4615 binding = get_class_binding (name, class_binding_level);
4616 /* If a new binding was created, put it at the front of the
4617 IDENTIFIER_BINDING list. */
4618 if (binding)
4620 binding->previous = IDENTIFIER_BINDING (name);
4621 IDENTIFIER_BINDING (name) = binding;
4625 /* If there is already a binding, then we may need to update the
4626 current value. */
4627 if (binding && binding->value)
4629 tree bval = binding->value;
4630 tree old_decl = NULL_TREE;
4631 tree target_decl = strip_using_decl (decl);
4632 tree target_bval = strip_using_decl (bval);
4634 if (INHERITED_VALUE_BINDING_P (binding))
4636 /* If the old binding was from a base class, and was for a
4637 tag name, slide it over to make room for the new binding.
4638 The old binding is still visible if explicitly qualified
4639 with a class-key. */
4640 if (TREE_CODE (target_bval) == TYPE_DECL
4641 && DECL_ARTIFICIAL (target_bval)
4642 && !(TREE_CODE (target_decl) == TYPE_DECL
4643 && DECL_ARTIFICIAL (target_decl)))
4645 old_decl = binding->type;
4646 binding->type = bval;
4647 binding->value = NULL_TREE;
4648 INHERITED_VALUE_BINDING_P (binding) = 0;
4650 else
4652 old_decl = bval;
4653 /* Any inherited type declaration is hidden by the type
4654 declaration in the derived class. */
4655 if (TREE_CODE (target_decl) == TYPE_DECL
4656 && DECL_ARTIFICIAL (target_decl))
4657 binding->type = NULL_TREE;
4660 else if (TREE_CODE (target_decl) == OVERLOAD
4661 && OVL_P (target_bval))
4662 old_decl = bval;
4663 else if (TREE_CODE (decl) == USING_DECL
4664 && TREE_CODE (bval) == USING_DECL
4665 && same_type_p (USING_DECL_SCOPE (decl),
4666 USING_DECL_SCOPE (bval)))
4667 /* This is a using redeclaration that will be diagnosed later
4668 in supplement_binding */
4670 else if (TREE_CODE (decl) == USING_DECL
4671 && TREE_CODE (bval) == USING_DECL
4672 && DECL_DEPENDENT_P (decl)
4673 && DECL_DEPENDENT_P (bval))
4674 return true;
4675 else if (TREE_CODE (decl) == USING_DECL
4676 && OVL_P (target_bval))
4677 old_decl = bval;
4678 else if (TREE_CODE (bval) == USING_DECL
4679 && OVL_P (target_decl))
4680 return true;
4682 if (old_decl && binding->scope == class_binding_level)
4684 binding->value = x;
4685 /* It is always safe to clear INHERITED_VALUE_BINDING_P
4686 here. This function is only used to register bindings
4687 from with the class definition itself. */
4688 INHERITED_VALUE_BINDING_P (binding) = 0;
4689 return true;
4693 /* Note that we declared this value so that we can issue an error if
4694 this is an invalid redeclaration of a name already used for some
4695 other purpose. */
4696 note_name_declared_in_class (name, decl);
4698 /* If we didn't replace an existing binding, put the binding on the
4699 stack of bindings for the identifier, and update the shadowed
4700 list. */
4701 if (binding && binding->scope == class_binding_level)
4702 /* Supplement the existing binding. */
4703 ok = supplement_binding (binding, decl);
4704 else
4706 /* Create a new binding. */
4707 push_binding (name, decl, class_binding_level);
4708 ok = true;
4711 return ok;
4714 /* Wrapper for push_class_level_binding_1. */
4716 bool
4717 push_class_level_binding (tree name, tree x)
4719 bool ret;
4720 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4721 ret = push_class_level_binding_1 (name, x);
4722 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4723 return ret;
4726 /* Process "using SCOPE::NAME" in a class scope. Return the
4727 USING_DECL created. */
4729 tree
4730 do_class_using_decl (tree scope, tree name)
4732 if (name == error_mark_node)
4733 return NULL_TREE;
4735 if (!scope || !TYPE_P (scope))
4737 error ("using-declaration for non-member at class scope");
4738 return NULL_TREE;
4741 /* Make sure the name is not invalid */
4742 if (TREE_CODE (name) == BIT_NOT_EXPR)
4744 error ("%<%T::%D%> names destructor", scope, name);
4745 return NULL_TREE;
4748 /* Using T::T declares inheriting ctors, even if T is a typedef. */
4749 if (MAYBE_CLASS_TYPE_P (scope)
4750 && (name == TYPE_IDENTIFIER (scope)
4751 || constructor_name_p (name, scope)))
4753 maybe_warn_cpp0x (CPP0X_INHERITING_CTORS);
4754 name = ctor_identifier;
4755 CLASSTYPE_NON_AGGREGATE (current_class_type) = true;
4758 /* Cannot introduce a constructor name. */
4759 if (constructor_name_p (name, current_class_type))
4761 error ("%<%T::%D%> names constructor in %qT",
4762 scope, name, current_class_type);
4763 return NULL_TREE;
4766 /* From [namespace.udecl]:
4768 A using-declaration used as a member-declaration shall refer to a
4769 member of a base class of the class being defined.
4771 In general, we cannot check this constraint in a template because
4772 we do not know the entire set of base classes of the current
4773 class type. Morover, if SCOPE is dependent, it might match a
4774 non-dependent base. */
4776 tree decl = NULL_TREE;
4777 if (!dependent_scope_p (scope))
4779 base_kind b_kind;
4780 tree binfo = lookup_base (current_class_type, scope, ba_any, &b_kind,
4781 tf_warning_or_error);
4782 if (b_kind < bk_proper_base)
4784 /* If there are dependent bases, scope might resolve at
4785 instantiation time, even if it isn't exactly one of the
4786 dependent bases. */
4787 if (b_kind == bk_same_type || !any_dependent_bases_p ())
4789 error_not_base_type (scope, current_class_type);
4790 return NULL_TREE;
4793 else if (name == ctor_identifier && !binfo_direct_p (binfo))
4795 error ("cannot inherit constructors from indirect base %qT", scope);
4796 return NULL_TREE;
4798 else if (!IDENTIFIER_CONV_OP_P (name)
4799 || !dependent_type_p (TREE_TYPE (name)))
4801 decl = lookup_member (binfo, name, 0, false, tf_warning_or_error);
4802 if (!decl)
4804 error ("no members matching %<%T::%D%> in %q#T", scope, name,
4805 scope);
4806 return NULL_TREE;
4809 /* The binfo from which the functions came does not matter. */
4810 if (BASELINK_P (decl))
4811 decl = BASELINK_FUNCTIONS (decl);
4815 tree value = build_lang_decl (USING_DECL, name, NULL_TREE);
4816 USING_DECL_DECLS (value) = decl;
4817 USING_DECL_SCOPE (value) = scope;
4818 DECL_DEPENDENT_P (value) = !decl;
4820 return value;
4824 /* Return the binding for NAME in NS. If NS is NULL, look in
4825 global_namespace. */
4827 tree
4828 get_namespace_binding (tree ns, tree name)
4830 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4831 if (!ns)
4832 ns = global_namespace;
4833 gcc_checking_assert (!DECL_NAMESPACE_ALIAS (ns));
4834 tree ret = find_namespace_value (ns, name);
4835 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4836 return ret;
4839 /* Set value binding of NAME in the global namespace to VAL. Does not
4840 add it to the list of things in the namespace. */
4842 void
4843 set_global_binding (tree name, tree val)
4845 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4847 tree *slot = find_namespace_slot (global_namespace, name, true);
4848 tree old = MAYBE_STAT_DECL (*slot);
4850 if (!old)
4851 *slot = val;
4852 else if (old == val)
4854 else if (!STAT_HACK_P (*slot)
4855 && TREE_CODE (val) == TYPE_DECL && DECL_ARTIFICIAL (val))
4856 *slot = stat_hack (old, val);
4857 else if (!STAT_HACK_P (*slot)
4858 && TREE_CODE (old) == TYPE_DECL && DECL_ARTIFICIAL (old))
4859 *slot = stat_hack (val, old);
4860 else
4861 /* The user's placed something in the implementor's
4862 namespace. */
4863 diagnose_name_conflict (val, old);
4865 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4868 /* Set the context of a declaration to scope. Complain if we are not
4869 outside scope. */
4871 void
4872 set_decl_namespace (tree decl, tree scope, bool friendp)
4874 /* Get rid of namespace aliases. */
4875 scope = ORIGINAL_NAMESPACE (scope);
4877 /* It is ok for friends to be qualified in parallel space. */
4878 if (!friendp && !is_nested_namespace (current_namespace, scope))
4879 error ("declaration of %qD not in a namespace surrounding %qD",
4880 decl, scope);
4881 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
4883 /* See whether this has been declared in the namespace or inline
4884 children. */
4885 tree old = NULL_TREE;
4887 name_lookup lookup (DECL_NAME (decl), LOOKUP_HIDDEN);
4888 if (!lookup.search_qualified (scope, /*usings=*/false))
4889 /* No old declaration at all. */
4890 goto not_found;
4891 old = lookup.value;
4894 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
4895 if (TREE_CODE (old) == TREE_LIST)
4897 ambiguous:
4898 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
4899 error ("reference to %qD is ambiguous", decl);
4900 print_candidates (old);
4901 return;
4904 if (!DECL_DECLARES_FUNCTION_P (decl))
4906 /* Don't compare non-function decls with decls_match here, since
4907 it can't check for the correct constness at this
4908 point. pushdecl will find those errors later. */
4910 /* We might have found it in an inline namespace child of SCOPE. */
4911 if (TREE_CODE (decl) == TREE_CODE (old))
4912 DECL_CONTEXT (decl) = DECL_CONTEXT (old);
4914 found:
4915 /* Writing "N::i" to declare something directly in "N" is invalid. */
4916 if (CP_DECL_CONTEXT (decl) == current_namespace
4917 && at_namespace_scope_p ())
4918 error ("explicit qualification in declaration of %qD", decl);
4919 return;
4922 /* Since decl is a function, old should contain a function decl. */
4923 if (!OVL_P (old))
4924 goto not_found;
4926 /* We handle these in check_explicit_instantiation_namespace. */
4927 if (processing_explicit_instantiation)
4928 return;
4929 if (processing_template_decl || processing_specialization)
4930 /* We have not yet called push_template_decl to turn a
4931 FUNCTION_DECL into a TEMPLATE_DECL, so the declarations won't
4932 match. But, we'll check later, when we construct the
4933 template. */
4934 return;
4935 /* Instantiations or specializations of templates may be declared as
4936 friends in any namespace. */
4937 if (friendp && DECL_USE_TEMPLATE (decl))
4938 return;
4940 tree found;
4941 found = NULL_TREE;
4943 for (lkp_iterator iter (old); iter; ++iter)
4945 if (iter.using_p ())
4946 continue;
4948 tree ofn = *iter;
4950 /* Adjust DECL_CONTEXT first so decls_match will return true
4951 if DECL will match a declaration in an inline namespace. */
4952 DECL_CONTEXT (decl) = DECL_CONTEXT (ofn);
4953 if (decls_match (decl, ofn))
4955 if (found)
4957 /* We found more than one matching declaration. */
4958 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
4959 goto ambiguous;
4961 found = ofn;
4965 if (found)
4967 if (DECL_HIDDEN_FRIEND_P (found))
4969 pedwarn (DECL_SOURCE_LOCATION (decl), 0,
4970 "%qD has not been declared within %qD", decl, scope);
4971 inform (DECL_SOURCE_LOCATION (found),
4972 "only here as a %<friend%>");
4974 DECL_CONTEXT (decl) = DECL_CONTEXT (found);
4975 goto found;
4978 not_found:
4979 /* It didn't work, go back to the explicit scope. */
4980 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
4981 error ("%qD should have been declared inside %qD", decl, scope);
4984 /* Return the namespace where the current declaration is declared. */
4986 tree
4987 current_decl_namespace (void)
4989 tree result;
4990 /* If we have been pushed into a different namespace, use it. */
4991 if (!vec_safe_is_empty (decl_namespace_list))
4992 return decl_namespace_list->last ();
4994 if (current_class_type)
4995 result = decl_namespace_context (current_class_type);
4996 else if (current_function_decl)
4997 result = decl_namespace_context (current_function_decl);
4998 else
4999 result = current_namespace;
5000 return result;
5003 /* Process any ATTRIBUTES on a namespace definition. Returns true if
5004 attribute visibility is seen. */
5006 bool
5007 handle_namespace_attrs (tree ns, tree attributes)
5009 tree d;
5010 bool saw_vis = false;
5012 for (d = attributes; d; d = TREE_CHAIN (d))
5014 tree name = get_attribute_name (d);
5015 tree args = TREE_VALUE (d);
5017 if (is_attribute_p ("visibility", name))
5019 /* attribute visibility is a property of the syntactic block
5020 rather than the namespace as a whole, so we don't touch the
5021 NAMESPACE_DECL at all. */
5022 tree x = args ? TREE_VALUE (args) : NULL_TREE;
5023 if (x == NULL_TREE || TREE_CODE (x) != STRING_CST || TREE_CHAIN (args))
5025 warning (OPT_Wattributes,
5026 "%qD attribute requires a single NTBS argument",
5027 name);
5028 continue;
5031 if (!TREE_PUBLIC (ns))
5032 warning (OPT_Wattributes,
5033 "%qD attribute is meaningless since members of the "
5034 "anonymous namespace get local symbols", name);
5036 push_visibility (TREE_STRING_POINTER (x), 1);
5037 saw_vis = true;
5039 else if (is_attribute_p ("abi_tag", name))
5041 if (!DECL_NAME (ns))
5043 warning (OPT_Wattributes, "ignoring %qD attribute on anonymous "
5044 "namespace", name);
5045 continue;
5047 if (!DECL_NAMESPACE_INLINE_P (ns))
5049 warning (OPT_Wattributes, "ignoring %qD attribute on non-inline "
5050 "namespace", name);
5051 continue;
5053 if (!args)
5055 tree dn = DECL_NAME (ns);
5056 args = build_string (IDENTIFIER_LENGTH (dn) + 1,
5057 IDENTIFIER_POINTER (dn));
5058 TREE_TYPE (args) = char_array_type_node;
5059 args = fix_string_type (args);
5060 args = build_tree_list (NULL_TREE, args);
5062 if (check_abi_tag_args (args, name))
5063 DECL_ATTRIBUTES (ns) = tree_cons (name, args,
5064 DECL_ATTRIBUTES (ns));
5066 else
5068 warning (OPT_Wattributes, "%qD attribute directive ignored",
5069 name);
5070 continue;
5074 return saw_vis;
5077 /* Temporarily set the namespace for the current declaration. */
5079 void
5080 push_decl_namespace (tree decl)
5082 if (TREE_CODE (decl) != NAMESPACE_DECL)
5083 decl = decl_namespace_context (decl);
5084 vec_safe_push (decl_namespace_list, ORIGINAL_NAMESPACE (decl));
5087 /* [namespace.memdef]/2 */
5089 void
5090 pop_decl_namespace (void)
5092 decl_namespace_list->pop ();
5095 /* Process a namespace-alias declaration. */
5097 void
5098 do_namespace_alias (tree alias, tree name_space)
5100 if (name_space == error_mark_node)
5101 return;
5103 gcc_assert (TREE_CODE (name_space) == NAMESPACE_DECL);
5105 name_space = ORIGINAL_NAMESPACE (name_space);
5107 /* Build the alias. */
5108 alias = build_lang_decl (NAMESPACE_DECL, alias, void_type_node);
5109 DECL_NAMESPACE_ALIAS (alias) = name_space;
5110 DECL_EXTERNAL (alias) = 1;
5111 DECL_CONTEXT (alias) = FROB_CONTEXT (current_scope ());
5112 pushdecl (alias);
5114 /* Emit debug info for namespace alias. */
5115 if (!building_stmt_list_p ())
5116 (*debug_hooks->early_global_decl) (alias);
5119 /* Like pushdecl, only it places X in the current namespace,
5120 if appropriate. */
5122 tree
5123 pushdecl_namespace_level (tree x, bool is_friend)
5125 cp_binding_level *b = current_binding_level;
5126 tree t;
5128 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
5129 t = do_pushdecl_with_scope
5130 (x, NAMESPACE_LEVEL (current_namespace), is_friend);
5132 /* Now, the type_shadowed stack may screw us. Munge it so it does
5133 what we want. */
5134 if (TREE_CODE (t) == TYPE_DECL)
5136 tree name = DECL_NAME (t);
5137 tree newval;
5138 tree *ptr = (tree *)0;
5139 for (; !global_scope_p (b); b = b->level_chain)
5141 tree shadowed = b->type_shadowed;
5142 for (; shadowed; shadowed = TREE_CHAIN (shadowed))
5143 if (TREE_PURPOSE (shadowed) == name)
5145 ptr = &TREE_VALUE (shadowed);
5146 /* Can't break out of the loop here because sometimes
5147 a binding level will have duplicate bindings for
5148 PT names. It's gross, but I haven't time to fix it. */
5151 newval = TREE_TYPE (t);
5152 if (ptr == (tree *)0)
5154 /* @@ This shouldn't be needed. My test case "zstring.cc" trips
5155 up here if this is changed to an assertion. --KR */
5156 SET_IDENTIFIER_TYPE_VALUE (name, t);
5158 else
5160 *ptr = newval;
5163 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
5164 return t;
5167 /* Process a using-declaration appearing in namespace scope. */
5169 void
5170 finish_namespace_using_decl (tree decl, tree scope, tree name)
5172 tree orig_decl = decl;
5174 gcc_checking_assert (current_binding_level->kind == sk_namespace
5175 && !processing_template_decl);
5176 decl = validate_nonmember_using_decl (decl, scope, name);
5177 if (decl == NULL_TREE)
5178 return;
5180 tree *slot = find_namespace_slot (current_namespace, name, true);
5181 tree val = slot ? MAYBE_STAT_DECL (*slot) : NULL_TREE;
5182 tree type = slot ? MAYBE_STAT_TYPE (*slot) : NULL_TREE;
5183 do_nonmember_using_decl (scope, name, &val, &type);
5184 if (STAT_HACK_P (*slot))
5186 STAT_DECL (*slot) = val;
5187 STAT_TYPE (*slot) = type;
5189 else if (type)
5190 *slot = stat_hack (val, type);
5191 else
5192 *slot = val;
5194 /* Emit debug info. */
5195 cp_emit_debug_info_for_using (orig_decl, current_namespace);
5198 /* Process a using-declaration at function scope. */
5200 void
5201 finish_local_using_decl (tree decl, tree scope, tree name)
5203 tree orig_decl = decl;
5205 gcc_checking_assert (current_binding_level->kind != sk_class
5206 && current_binding_level->kind != sk_namespace);
5207 decl = validate_nonmember_using_decl (decl, scope, name);
5208 if (decl == NULL_TREE)
5209 return;
5211 add_decl_expr (decl);
5213 cxx_binding *binding = find_local_binding (current_binding_level, name);
5214 tree value = binding ? binding->value : NULL_TREE;
5215 tree type = binding ? binding->type : NULL_TREE;
5217 do_nonmember_using_decl (scope, name, &value, &type);
5219 if (!value)
5221 else if (binding && value == binding->value)
5223 else if (binding && binding->value && TREE_CODE (value) == OVERLOAD)
5225 update_local_overload (IDENTIFIER_BINDING (name), value);
5226 IDENTIFIER_BINDING (name)->value = value;
5228 else
5229 /* Install the new binding. */
5230 push_local_binding (name, value, true);
5232 if (!type)
5234 else if (binding && type == binding->type)
5236 else
5238 push_local_binding (name, type, true);
5239 set_identifier_type_value (name, type);
5242 /* Emit debug info. */
5243 if (!processing_template_decl)
5244 cp_emit_debug_info_for_using (orig_decl, current_scope ());
5247 /* Return the declarations that are members of the namespace NS. */
5249 tree
5250 cp_namespace_decls (tree ns)
5252 return NAMESPACE_LEVEL (ns)->names;
5255 /* Combine prefer_type and namespaces_only into flags. */
5257 static int
5258 lookup_flags (int prefer_type, int namespaces_only)
5260 if (namespaces_only)
5261 return LOOKUP_PREFER_NAMESPACES;
5262 if (prefer_type > 1)
5263 return LOOKUP_PREFER_TYPES;
5264 if (prefer_type > 0)
5265 return LOOKUP_PREFER_BOTH;
5266 return 0;
5269 /* Given a lookup that returned VAL, use FLAGS to decide if we want to
5270 ignore it or not. Subroutine of lookup_name_real and
5271 lookup_type_scope. */
5273 static bool
5274 qualify_lookup (tree val, int flags)
5276 if (val == NULL_TREE)
5277 return false;
5278 if ((flags & LOOKUP_PREFER_NAMESPACES) && TREE_CODE (val) == NAMESPACE_DECL)
5279 return true;
5280 if (flags & LOOKUP_PREFER_TYPES)
5282 tree target_val = strip_using_decl (val);
5283 if (TREE_CODE (target_val) == TYPE_DECL
5284 || TREE_CODE (target_val) == TEMPLATE_DECL)
5285 return true;
5287 if (flags & (LOOKUP_PREFER_NAMESPACES | LOOKUP_PREFER_TYPES))
5288 return false;
5289 /* Look through lambda things that we shouldn't be able to see. */
5290 if (is_lambda_ignored_entity (val))
5291 return false;
5292 return true;
5295 /* Suggest alternatives for NAME, an IDENTIFIER_NODE for which name
5296 lookup failed. Search through all available namespaces and print out
5297 possible candidates. If no exact matches are found, and
5298 SUGGEST_MISSPELLINGS is true, then also look for near-matches and
5299 suggest the best near-match, if there is one. */
5301 void
5302 suggest_alternatives_for (location_t location, tree name,
5303 bool suggest_misspellings)
5305 vec<tree> candidates = vNULL;
5306 vec<tree> worklist = vNULL;
5307 unsigned limit = PARAM_VALUE (CXX_MAX_NAMESPACES_FOR_DIAGNOSTIC_HELP);
5308 bool limited = false;
5310 /* Breadth-first search of namespaces. Up to limit namespaces
5311 searched (limit zero == unlimited). */
5312 worklist.safe_push (global_namespace);
5313 for (unsigned ix = 0; ix != worklist.length (); ix++)
5315 tree ns = worklist[ix];
5316 name_lookup lookup (name);
5318 if (lookup.search_qualified (ns, false))
5319 candidates.safe_push (lookup.value);
5321 if (!limited)
5323 /* Look for child namespaces. We have to do this
5324 indirectly because they are chained in reverse order,
5325 which is confusing to the user. */
5326 vec<tree> children = vNULL;
5328 for (tree decl = NAMESPACE_LEVEL (ns)->names;
5329 decl; decl = TREE_CHAIN (decl))
5330 if (TREE_CODE (decl) == NAMESPACE_DECL
5331 && !DECL_NAMESPACE_ALIAS (decl)
5332 && !DECL_NAMESPACE_INLINE_P (decl))
5333 children.safe_push (decl);
5335 while (!limited && !children.is_empty ())
5337 if (worklist.length () == limit)
5339 /* Unconditionally warn that the search was truncated. */
5340 inform (location,
5341 "maximum limit of %d namespaces searched for %qE",
5342 limit, name);
5343 limited = true;
5345 else
5346 worklist.safe_push (children.pop ());
5348 children.release ();
5351 worklist.release ();
5353 if (candidates.length ())
5355 inform_n (location, candidates.length (),
5356 "suggested alternative:",
5357 "suggested alternatives:");
5358 for (unsigned ix = 0; ix != candidates.length (); ix++)
5360 tree val = candidates[ix];
5362 inform (location_of (val), " %qE", val);
5364 candidates.release ();
5366 else if (!suggest_misspellings)
5368 else if (const char *fuzzy = lookup_name_fuzzy (name, FUZZY_LOOKUP_NAME))
5370 /* Show a spelling correction. */
5371 gcc_rich_location richloc (location);
5373 richloc.add_fixit_replace (fuzzy);
5374 inform_at_rich_loc (&richloc, "suggested alternative: %qs", fuzzy);
5378 /* Subroutine of maybe_suggest_missing_header for handling unrecognized names
5379 for some of the most common names within "std::".
5380 Given non-NULL NAME, a name for lookup within "std::", return the header
5381 name defining it within the C++ Standard Library (with '<' and '>'),
5382 or NULL. */
5384 static const char *
5385 get_std_name_hint (const char *name)
5387 struct std_name_hint
5389 const char *name;
5390 const char *header;
5392 static const std_name_hint hints[] = {
5393 /* <array>. */
5394 {"array", "<array>"}, // C++11
5395 /* <deque>. */
5396 {"deque", "<deque>"},
5397 /* <forward_list>. */
5398 {"forward_list", "<forward_list>"}, // C++11
5399 /* <fstream>. */
5400 {"basic_filebuf", "<fstream>"},
5401 {"basic_ifstream", "<fstream>"},
5402 {"basic_ofstream", "<fstream>"},
5403 {"basic_fstream", "<fstream>"},
5404 /* <iostream>. */
5405 {"cin", "<iostream>"},
5406 {"cout", "<iostream>"},
5407 {"cerr", "<iostream>"},
5408 {"clog", "<iostream>"},
5409 {"wcin", "<iostream>"},
5410 {"wcout", "<iostream>"},
5411 {"wclog", "<iostream>"},
5412 /* <list>. */
5413 {"list", "<list>"},
5414 /* <map>. */
5415 {"map", "<map>"},
5416 {"multimap", "<map>"},
5417 /* <queue>. */
5418 {"queue", "<queue>"},
5419 {"priority_queue", "<queue>"},
5420 /* <ostream>. */
5421 {"ostream", "<ostream>"},
5422 {"wostream", "<ostream>"},
5423 {"ends", "<ostream>"},
5424 {"flush", "<ostream>"},
5425 {"endl", "<ostream>"},
5426 /* <set>. */
5427 {"set", "<set>"},
5428 {"multiset", "<set>"},
5429 /* <sstream>. */
5430 {"basic_stringbuf", "<sstream>"},
5431 {"basic_istringstream", "<sstream>"},
5432 {"basic_ostringstream", "<sstream>"},
5433 {"basic_stringstream", "<sstream>"},
5434 /* <stack>. */
5435 {"stack", "<stack>"},
5436 /* <string>. */
5437 {"string", "<string>"},
5438 {"wstring", "<string>"},
5439 {"u16string", "<string>"},
5440 {"u32string", "<string>"},
5441 /* <unordered_map>. */
5442 {"unordered_map", "<unordered_map>"}, // C++11
5443 {"unordered_multimap", "<unordered_map>"}, // C++11
5444 /* <unordered_set>. */
5445 {"unordered_set", "<unordered_set>"}, // C++11
5446 {"unordered_multiset", "<unordered_set>"}, // C++11
5447 /* <vector>. */
5448 {"vector", "<vector>"},
5450 const size_t num_hints = sizeof (hints) / sizeof (hints[0]);
5451 for (size_t i = 0; i < num_hints; i++)
5453 if (0 == strcmp (name, hints[i].name))
5454 return hints[i].header;
5456 return NULL;
5459 /* If SCOPE is the "std" namespace, then suggest pertinent header
5460 files for NAME at LOCATION.
5461 Return true iff a suggestion was offered. */
5463 static bool
5464 maybe_suggest_missing_header (location_t location, tree name, tree scope)
5466 if (scope == NULL_TREE)
5467 return false;
5468 if (TREE_CODE (scope) != NAMESPACE_DECL)
5469 return false;
5470 /* We only offer suggestions for the "std" namespace. */
5471 if (scope != std_node)
5472 return false;
5473 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
5475 const char *name_str = IDENTIFIER_POINTER (name);
5476 const char *header_hint = get_std_name_hint (name_str);
5477 if (!header_hint)
5478 return false;
5480 gcc_rich_location richloc (location);
5481 maybe_add_include_fixit (&richloc, header_hint);
5482 inform_at_rich_loc (&richloc,
5483 "%<std::%s%> is defined in header %qs;"
5484 " did you forget to %<#include %s%>?",
5485 name_str, header_hint, header_hint);
5486 return true;
5489 /* Look for alternatives for NAME, an IDENTIFIER_NODE for which name
5490 lookup failed within the explicitly provided SCOPE. Suggest the
5491 the best meaningful candidates (if any) as a fix-it hint.
5492 Return true iff a suggestion was provided. */
5494 bool
5495 suggest_alternative_in_explicit_scope (location_t location, tree name,
5496 tree scope)
5498 /* Resolve any namespace aliases. */
5499 scope = ORIGINAL_NAMESPACE (scope);
5501 if (maybe_suggest_missing_header (location, name, scope))
5502 return true;
5504 cp_binding_level *level = NAMESPACE_LEVEL (scope);
5506 best_match <tree, const char *> bm (name);
5507 consider_binding_level (name, bm, level, false, FUZZY_LOOKUP_NAME);
5509 /* See if we have a good suggesion for the user. */
5510 const char *fuzzy_name = bm.get_best_meaningful_candidate ();
5511 if (fuzzy_name)
5513 gcc_rich_location richloc (location);
5514 richloc.add_fixit_replace (fuzzy_name);
5515 inform_at_rich_loc (&richloc, "suggested alternative: %qs",
5516 fuzzy_name);
5517 return true;
5520 return false;
5523 /* Look up NAME (an IDENTIFIER_NODE) in SCOPE (either a NAMESPACE_DECL
5524 or a class TYPE).
5526 If PREFER_TYPE is > 0, we only return TYPE_DECLs or namespaces.
5527 If PREFER_TYPE is > 1, we only return TYPE_DECLs.
5529 Returns a DECL (or OVERLOAD, or BASELINK) representing the
5530 declaration found. If no suitable declaration can be found,
5531 ERROR_MARK_NODE is returned. If COMPLAIN is true and SCOPE is
5532 neither a class-type nor a namespace a diagnostic is issued. */
5534 tree
5535 lookup_qualified_name (tree scope, tree name, int prefer_type, bool complain,
5536 bool find_hidden)
5538 tree t = NULL_TREE;
5540 if (TREE_CODE (scope) == NAMESPACE_DECL)
5542 int flags = lookup_flags (prefer_type, /*namespaces_only*/false);
5543 if (find_hidden)
5544 flags |= LOOKUP_HIDDEN;
5545 name_lookup lookup (name, flags);
5547 if (qualified_namespace_lookup (scope, &lookup))
5548 t = lookup.value;
5550 else if (cxx_dialect != cxx98 && TREE_CODE (scope) == ENUMERAL_TYPE)
5551 t = lookup_enumerator (scope, name);
5552 else if (is_class_type (scope, complain))
5553 t = lookup_member (scope, name, 2, prefer_type, tf_warning_or_error);
5555 if (!t)
5556 return error_mark_node;
5557 return t;
5560 /* [namespace.qual]
5561 Accepts the NAME to lookup and its qualifying SCOPE.
5562 Returns the name/type pair found into the cxx_binding *RESULT,
5563 or false on error. */
5565 static bool
5566 qualified_namespace_lookup (tree scope, name_lookup *lookup)
5568 timevar_start (TV_NAME_LOOKUP);
5569 query_oracle (lookup->name);
5570 bool found = lookup->search_qualified (ORIGINAL_NAMESPACE (scope));
5571 timevar_stop (TV_NAME_LOOKUP);
5572 return found;
5575 /* Helper function for lookup_name_fuzzy.
5576 Traverse binding level LVL, looking for good name matches for NAME
5577 (and BM). */
5578 static void
5579 consider_binding_level (tree name, best_match <tree, const char *> &bm,
5580 cp_binding_level *lvl, bool look_within_fields,
5581 enum lookup_name_fuzzy_kind kind)
5583 if (look_within_fields)
5584 if (lvl->this_entity && TREE_CODE (lvl->this_entity) == RECORD_TYPE)
5586 tree type = lvl->this_entity;
5587 bool want_type_p = (kind == FUZZY_LOOKUP_TYPENAME);
5588 tree best_matching_field
5589 = lookup_member_fuzzy (type, name, want_type_p);
5590 if (best_matching_field)
5591 bm.consider (IDENTIFIER_POINTER (best_matching_field));
5594 for (tree t = lvl->names; t; t = TREE_CHAIN (t))
5596 tree d = t;
5598 /* OVERLOADs or decls from using declaration are wrapped into
5599 TREE_LIST. */
5600 if (TREE_CODE (d) == TREE_LIST)
5601 d = OVL_FIRST (TREE_VALUE (d));
5603 /* Don't use bindings from implicitly declared functions,
5604 as they were likely misspellings themselves. */
5605 if (TREE_TYPE (d) == error_mark_node)
5606 continue;
5608 /* Skip anticipated decls of builtin functions. */
5609 if (TREE_CODE (d) == FUNCTION_DECL
5610 && DECL_BUILT_IN (d)
5611 && DECL_ANTICIPATED (d))
5612 continue;
5614 if (tree name = DECL_NAME (d))
5615 /* Ignore internal names with spaces in them. */
5616 if (!strchr (IDENTIFIER_POINTER (name), ' '))
5617 bm.consider (IDENTIFIER_POINTER (name));
5621 /* Search for near-matches for NAME within the current bindings, and within
5622 macro names, returning the best match as a const char *, or NULL if
5623 no reasonable match is found. */
5625 const char *
5626 lookup_name_fuzzy (tree name, enum lookup_name_fuzzy_kind kind)
5628 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
5630 best_match <tree, const char *> bm (name);
5632 cp_binding_level *lvl;
5633 for (lvl = scope_chain->class_bindings; lvl; lvl = lvl->level_chain)
5634 consider_binding_level (name, bm, lvl, true, kind);
5636 for (lvl = current_binding_level; lvl; lvl = lvl->level_chain)
5637 consider_binding_level (name, bm, lvl, false, kind);
5639 /* Consider macros: if the user misspelled a macro name e.g. "SOME_MACRO"
5641 x = SOME_OTHER_MACRO (y);
5642 then "SOME_OTHER_MACRO" will survive to the frontend and show up
5643 as a misspelled identifier.
5645 Use the best distance so far so that a candidate is only set if
5646 a macro is better than anything so far. This allows early rejection
5647 (without calculating the edit distance) of macro names that must have
5648 distance >= bm.get_best_distance (), and means that we only get a
5649 non-NULL result for best_macro_match if it's better than any of
5650 the identifiers already checked. */
5651 best_macro_match bmm (name, bm.get_best_distance (), parse_in);
5652 cpp_hashnode *best_macro = bmm.get_best_meaningful_candidate ();
5653 /* If a macro is the closest so far to NAME, consider it. */
5654 if (best_macro)
5655 bm.consider ((const char *)best_macro->ident.str);
5657 /* Try the "starts_decl_specifier_p" keywords to detect
5658 "singed" vs "signed" typos. */
5659 for (unsigned i = 0; i < num_c_common_reswords; i++)
5661 const c_common_resword *resword = &c_common_reswords[i];
5663 if (kind == FUZZY_LOOKUP_TYPENAME)
5664 if (!cp_keyword_starts_decl_specifier_p (resword->rid))
5665 continue;
5667 tree resword_identifier = ridpointers [resword->rid];
5668 if (!resword_identifier)
5669 continue;
5670 gcc_assert (TREE_CODE (resword_identifier) == IDENTIFIER_NODE);
5672 /* Only consider reserved words that survived the
5673 filtering in init_reswords (e.g. for -std). */
5674 if (!IDENTIFIER_KEYWORD_P (resword_identifier))
5675 continue;
5677 bm.consider (IDENTIFIER_POINTER (resword_identifier));
5680 return bm.get_best_meaningful_candidate ();
5683 /* Subroutine of outer_binding.
5685 Returns TRUE if BINDING is a binding to a template parameter of
5686 SCOPE. In that case SCOPE is the scope of a primary template
5687 parameter -- in the sense of G++, i.e, a template that has its own
5688 template header.
5690 Returns FALSE otherwise. */
5692 static bool
5693 binding_to_template_parms_of_scope_p (cxx_binding *binding,
5694 cp_binding_level *scope)
5696 tree binding_value, tmpl, tinfo;
5697 int level;
5699 if (!binding || !scope || !scope->this_entity)
5700 return false;
5702 binding_value = binding->value ? binding->value : binding->type;
5703 tinfo = get_template_info (scope->this_entity);
5705 /* BINDING_VALUE must be a template parm. */
5706 if (binding_value == NULL_TREE
5707 || (!DECL_P (binding_value)
5708 || !DECL_TEMPLATE_PARM_P (binding_value)))
5709 return false;
5711 /* The level of BINDING_VALUE. */
5712 level =
5713 template_type_parameter_p (binding_value)
5714 ? TEMPLATE_PARM_LEVEL (TEMPLATE_TYPE_PARM_INDEX
5715 (TREE_TYPE (binding_value)))
5716 : TEMPLATE_PARM_LEVEL (DECL_INITIAL (binding_value));
5718 /* The template of the current scope, iff said scope is a primary
5719 template. */
5720 tmpl = (tinfo
5721 && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
5722 ? TI_TEMPLATE (tinfo)
5723 : NULL_TREE);
5725 /* If the level of the parm BINDING_VALUE equals the depth of TMPL,
5726 then BINDING_VALUE is a parameter of TMPL. */
5727 return (tmpl && level == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)));
5730 /* Return the innermost non-namespace binding for NAME from a scope
5731 containing BINDING, or, if BINDING is NULL, the current scope.
5732 Please note that for a given template, the template parameters are
5733 considered to be in the scope containing the current scope.
5734 If CLASS_P is false, then class bindings are ignored. */
5736 cxx_binding *
5737 outer_binding (tree name,
5738 cxx_binding *binding,
5739 bool class_p)
5741 cxx_binding *outer;
5742 cp_binding_level *scope;
5743 cp_binding_level *outer_scope;
5745 if (binding)
5747 scope = binding->scope->level_chain;
5748 outer = binding->previous;
5750 else
5752 scope = current_binding_level;
5753 outer = IDENTIFIER_BINDING (name);
5755 outer_scope = outer ? outer->scope : NULL;
5757 /* Because we create class bindings lazily, we might be missing a
5758 class binding for NAME. If there are any class binding levels
5759 between the LAST_BINDING_LEVEL and the scope in which OUTER was
5760 declared, we must lookup NAME in those class scopes. */
5761 if (class_p)
5762 while (scope && scope != outer_scope && scope->kind != sk_namespace)
5764 if (scope->kind == sk_class)
5766 cxx_binding *class_binding;
5768 class_binding = get_class_binding (name, scope);
5769 if (class_binding)
5771 /* Thread this new class-scope binding onto the
5772 IDENTIFIER_BINDING list so that future lookups
5773 find it quickly. */
5774 class_binding->previous = outer;
5775 if (binding)
5776 binding->previous = class_binding;
5777 else
5778 IDENTIFIER_BINDING (name) = class_binding;
5779 return class_binding;
5782 /* If we are in a member template, the template parms of the member
5783 template are considered to be inside the scope of the containing
5784 class, but within G++ the class bindings are all pushed between the
5785 template parms and the function body. So if the outer binding is
5786 a template parm for the current scope, return it now rather than
5787 look for a class binding. */
5788 if (outer_scope && outer_scope->kind == sk_template_parms
5789 && binding_to_template_parms_of_scope_p (outer, scope))
5790 return outer;
5792 scope = scope->level_chain;
5795 return outer;
5798 /* Return the innermost block-scope or class-scope value binding for
5799 NAME, or NULL_TREE if there is no such binding. */
5801 tree
5802 innermost_non_namespace_value (tree name)
5804 cxx_binding *binding;
5805 binding = outer_binding (name, /*binding=*/NULL, /*class_p=*/true);
5806 return binding ? binding->value : NULL_TREE;
5809 /* Look up NAME in the current binding level and its superiors in the
5810 namespace of variables, functions and typedefs. Return a ..._DECL
5811 node of some kind representing its definition if there is only one
5812 such declaration, or return a TREE_LIST with all the overloaded
5813 definitions if there are many, or return 0 if it is undefined.
5814 Hidden name, either friend declaration or built-in function, are
5815 not ignored.
5817 If PREFER_TYPE is > 0, we prefer TYPE_DECLs or namespaces.
5818 If PREFER_TYPE is > 1, we reject non-type decls (e.g. namespaces).
5819 Otherwise we prefer non-TYPE_DECLs.
5821 If NONCLASS is nonzero, bindings in class scopes are ignored. If
5822 BLOCK_P is false, bindings in block scopes are ignored. */
5824 static tree
5825 lookup_name_real_1 (tree name, int prefer_type, int nonclass, bool block_p,
5826 int namespaces_only, int flags)
5828 cxx_binding *iter;
5829 tree val = NULL_TREE;
5831 query_oracle (name);
5833 /* Conversion operators are handled specially because ordinary
5834 unqualified name lookup will not find template conversion
5835 operators. */
5836 if (IDENTIFIER_CONV_OP_P (name))
5838 cp_binding_level *level;
5840 for (level = current_binding_level;
5841 level && level->kind != sk_namespace;
5842 level = level->level_chain)
5844 tree class_type;
5845 tree operators;
5847 /* A conversion operator can only be declared in a class
5848 scope. */
5849 if (level->kind != sk_class)
5850 continue;
5852 /* Lookup the conversion operator in the class. */
5853 class_type = level->this_entity;
5854 operators = lookup_fnfields (class_type, name, /*protect=*/0);
5855 if (operators)
5856 return operators;
5859 return NULL_TREE;
5862 flags |= lookup_flags (prefer_type, namespaces_only);
5864 /* First, look in non-namespace scopes. */
5866 if (current_class_type == NULL_TREE)
5867 nonclass = 1;
5869 if (block_p || !nonclass)
5870 for (iter = outer_binding (name, NULL, !nonclass);
5871 iter;
5872 iter = outer_binding (name, iter, !nonclass))
5874 tree binding;
5876 /* Skip entities we don't want. */
5877 if (LOCAL_BINDING_P (iter) ? !block_p : nonclass)
5878 continue;
5880 /* If this is the kind of thing we're looking for, we're done. */
5881 if (qualify_lookup (iter->value, flags))
5882 binding = iter->value;
5883 else if ((flags & LOOKUP_PREFER_TYPES)
5884 && qualify_lookup (iter->type, flags))
5885 binding = iter->type;
5886 else
5887 binding = NULL_TREE;
5889 if (binding)
5891 if (TREE_CODE (binding) == TYPE_DECL && DECL_HIDDEN_P (binding))
5893 /* A non namespace-scope binding can only be hidden in the
5894 presence of a local class, due to friend declarations.
5896 In particular, consider:
5898 struct C;
5899 void f() {
5900 struct A {
5901 friend struct B;
5902 friend struct C;
5903 void g() {
5904 B* b; // error: B is hidden
5905 C* c; // OK, finds ::C
5908 B *b; // error: B is hidden
5909 C *c; // OK, finds ::C
5910 struct B {};
5911 B *bb; // OK
5914 The standard says that "B" is a local class in "f"
5915 (but not nested within "A") -- but that name lookup
5916 for "B" does not find this declaration until it is
5917 declared directly with "f".
5919 In particular:
5921 [class.friend]
5923 If a friend declaration appears in a local class and
5924 the name specified is an unqualified name, a prior
5925 declaration is looked up without considering scopes
5926 that are outside the innermost enclosing non-class
5927 scope. For a friend function declaration, if there is
5928 no prior declaration, the program is ill-formed. For a
5929 friend class declaration, if there is no prior
5930 declaration, the class that is specified belongs to the
5931 innermost enclosing non-class scope, but if it is
5932 subsequently referenced, its name is not found by name
5933 lookup until a matching declaration is provided in the
5934 innermost enclosing nonclass scope.
5936 So just keep looking for a non-hidden binding.
5938 gcc_assert (TREE_CODE (binding) == TYPE_DECL);
5939 continue;
5941 val = binding;
5942 break;
5946 /* Now lookup in namespace scopes. */
5947 if (!val)
5949 name_lookup lookup (name, flags);
5950 if (lookup.search_unqualified
5951 (current_decl_namespace (), current_binding_level))
5952 val = lookup.value;
5955 /* If we have a single function from a using decl, pull it out. */
5956 if (val && TREE_CODE (val) == OVERLOAD && !really_overloaded_fn (val))
5957 val = OVL_FUNCTION (val);
5959 return val;
5962 /* Wrapper for lookup_name_real_1. */
5964 tree
5965 lookup_name_real (tree name, int prefer_type, int nonclass, bool block_p,
5966 int namespaces_only, int flags)
5968 tree ret;
5969 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
5970 ret = lookup_name_real_1 (name, prefer_type, nonclass, block_p,
5971 namespaces_only, flags);
5972 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
5973 return ret;
5976 tree
5977 lookup_name_nonclass (tree name)
5979 return lookup_name_real (name, 0, 1, /*block_p=*/true, 0, 0);
5982 tree
5983 lookup_name (tree name)
5985 return lookup_name_real (name, 0, 0, /*block_p=*/true, 0, 0);
5988 tree
5989 lookup_name_prefer_type (tree name, int prefer_type)
5991 return lookup_name_real (name, prefer_type, 0, /*block_p=*/true, 0, 0);
5994 /* Look up NAME for type used in elaborated name specifier in
5995 the scopes given by SCOPE. SCOPE can be either TS_CURRENT or
5996 TS_WITHIN_ENCLOSING_NON_CLASS. Although not implied by the
5997 name, more scopes are checked if cleanup or template parameter
5998 scope is encountered.
6000 Unlike lookup_name_real, we make sure that NAME is actually
6001 declared in the desired scope, not from inheritance, nor using
6002 directive. For using declaration, there is DR138 still waiting
6003 to be resolved. Hidden name coming from an earlier friend
6004 declaration is also returned.
6006 A TYPE_DECL best matching the NAME is returned. Catching error
6007 and issuing diagnostics are caller's responsibility. */
6009 static tree
6010 lookup_type_scope_1 (tree name, tag_scope scope)
6012 cxx_binding *iter = NULL;
6013 tree val = NULL_TREE;
6014 cp_binding_level *level = NULL;
6016 /* Look in non-namespace scope first. */
6017 if (current_binding_level->kind != sk_namespace)
6018 iter = outer_binding (name, NULL, /*class_p=*/ true);
6019 for (; iter; iter = outer_binding (name, iter, /*class_p=*/ true))
6021 /* Check if this is the kind of thing we're looking for.
6022 If SCOPE is TS_CURRENT, also make sure it doesn't come from
6023 base class. For ITER->VALUE, we can simply use
6024 INHERITED_VALUE_BINDING_P. For ITER->TYPE, we have to use
6025 our own check.
6027 We check ITER->TYPE before ITER->VALUE in order to handle
6028 typedef struct C {} C;
6029 correctly. */
6031 if (qualify_lookup (iter->type, LOOKUP_PREFER_TYPES)
6032 && (scope != ts_current
6033 || LOCAL_BINDING_P (iter)
6034 || DECL_CONTEXT (iter->type) == iter->scope->this_entity))
6035 val = iter->type;
6036 else if ((scope != ts_current
6037 || !INHERITED_VALUE_BINDING_P (iter))
6038 && qualify_lookup (iter->value, LOOKUP_PREFER_TYPES))
6039 val = iter->value;
6041 if (val)
6042 break;
6045 /* Look in namespace scope. */
6046 if (val)
6047 level = iter->scope;
6048 else
6050 tree ns = current_decl_namespace ();
6052 if (tree *slot = find_namespace_slot (ns, name))
6054 /* If this is the kind of thing we're looking for, we're done. */
6055 if (tree type = MAYBE_STAT_TYPE (*slot))
6056 if (qualify_lookup (type, LOOKUP_PREFER_TYPES))
6057 val = type;
6058 if (!val)
6060 if (tree decl = MAYBE_STAT_DECL (*slot))
6061 if (qualify_lookup (decl, LOOKUP_PREFER_TYPES))
6062 val = decl;
6064 level = NAMESPACE_LEVEL (ns);
6068 /* Type found, check if it is in the allowed scopes, ignoring cleanup
6069 and template parameter scopes. */
6070 if (val)
6072 cp_binding_level *b = current_binding_level;
6073 while (b)
6075 if (level == b)
6076 return val;
6078 if (b->kind == sk_cleanup || b->kind == sk_template_parms
6079 || b->kind == sk_function_parms)
6080 b = b->level_chain;
6081 else if (b->kind == sk_class
6082 && scope == ts_within_enclosing_non_class)
6083 b = b->level_chain;
6084 else
6085 break;
6089 return NULL_TREE;
6092 /* Wrapper for lookup_type_scope_1. */
6094 tree
6095 lookup_type_scope (tree name, tag_scope scope)
6097 tree ret;
6098 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6099 ret = lookup_type_scope_1 (name, scope);
6100 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6101 return ret;
6104 /* Returns true iff DECL is a block-scope extern declaration of a function
6105 or variable. */
6107 bool
6108 is_local_extern (tree decl)
6110 cxx_binding *binding;
6112 /* For functions, this is easy. */
6113 if (TREE_CODE (decl) == FUNCTION_DECL)
6114 return DECL_LOCAL_FUNCTION_P (decl);
6116 if (!VAR_P (decl))
6117 return false;
6118 if (!current_function_decl)
6119 return false;
6121 /* For variables, this is not easy. We need to look at the binding stack
6122 for the identifier to see whether the decl we have is a local. */
6123 for (binding = IDENTIFIER_BINDING (DECL_NAME (decl));
6124 binding && binding->scope->kind != sk_namespace;
6125 binding = binding->previous)
6126 if (binding->value == decl)
6127 return LOCAL_BINDING_P (binding);
6129 return false;
6132 /* The type TYPE is being declared. If it is a class template, or a
6133 specialization of a class template, do any processing required and
6134 perform error-checking. If IS_FRIEND is nonzero, this TYPE is
6135 being declared a friend. B is the binding level at which this TYPE
6136 should be bound.
6138 Returns the TYPE_DECL for TYPE, which may have been altered by this
6139 processing. */
6141 static tree
6142 maybe_process_template_type_declaration (tree type, int is_friend,
6143 cp_binding_level *b)
6145 tree decl = TYPE_NAME (type);
6147 if (processing_template_parmlist)
6148 /* You can't declare a new template type in a template parameter
6149 list. But, you can declare a non-template type:
6151 template <class A*> struct S;
6153 is a forward-declaration of `A'. */
6155 else if (b->kind == sk_namespace
6156 && current_binding_level->kind != sk_namespace)
6157 /* If this new type is being injected into a containing scope,
6158 then it's not a template type. */
6160 else
6162 gcc_assert (MAYBE_CLASS_TYPE_P (type)
6163 || TREE_CODE (type) == ENUMERAL_TYPE);
6165 if (processing_template_decl)
6167 /* This may change after the call to
6168 push_template_decl_real, but we want the original value. */
6169 tree name = DECL_NAME (decl);
6171 decl = push_template_decl_real (decl, is_friend);
6172 if (decl == error_mark_node)
6173 return error_mark_node;
6175 /* If the current binding level is the binding level for the
6176 template parameters (see the comment in
6177 begin_template_parm_list) and the enclosing level is a class
6178 scope, and we're not looking at a friend, push the
6179 declaration of the member class into the class scope. In the
6180 friend case, push_template_decl will already have put the
6181 friend into global scope, if appropriate. */
6182 if (TREE_CODE (type) != ENUMERAL_TYPE
6183 && !is_friend && b->kind == sk_template_parms
6184 && b->level_chain->kind == sk_class)
6186 finish_member_declaration (CLASSTYPE_TI_TEMPLATE (type));
6188 if (!COMPLETE_TYPE_P (current_class_type))
6190 maybe_add_class_template_decl_list (current_class_type,
6191 type, /*friend_p=*/0);
6192 /* Put this UTD in the table of UTDs for the class. */
6193 if (CLASSTYPE_NESTED_UTDS (current_class_type) == NULL)
6194 CLASSTYPE_NESTED_UTDS (current_class_type) =
6195 binding_table_new (SCOPE_DEFAULT_HT_SIZE);
6197 binding_table_insert
6198 (CLASSTYPE_NESTED_UTDS (current_class_type), name, type);
6204 return decl;
6207 /* Push a tag name NAME for struct/class/union/enum type TYPE. In case
6208 that the NAME is a class template, the tag is processed but not pushed.
6210 The pushed scope depend on the SCOPE parameter:
6211 - When SCOPE is TS_CURRENT, put it into the inner-most non-sk_cleanup
6212 scope.
6213 - When SCOPE is TS_GLOBAL, put it in the inner-most non-class and
6214 non-template-parameter scope. This case is needed for forward
6215 declarations.
6216 - When SCOPE is TS_WITHIN_ENCLOSING_NON_CLASS, this is similar to
6217 TS_GLOBAL case except that names within template-parameter scopes
6218 are not pushed at all.
6220 Returns TYPE upon success and ERROR_MARK_NODE otherwise. */
6222 static tree
6223 do_pushtag (tree name, tree type, tag_scope scope)
6225 tree decl;
6227 cp_binding_level *b = current_binding_level;
6228 while (/* Cleanup scopes are not scopes from the point of view of
6229 the language. */
6230 b->kind == sk_cleanup
6231 /* Neither are function parameter scopes. */
6232 || b->kind == sk_function_parms
6233 /* Neither are the scopes used to hold template parameters
6234 for an explicit specialization. For an ordinary template
6235 declaration, these scopes are not scopes from the point of
6236 view of the language. */
6237 || (b->kind == sk_template_parms
6238 && (b->explicit_spec_p || scope == ts_global))
6239 /* Pushing into a class is ok for lambdas or when we want current */
6240 || (b->kind == sk_class
6241 && scope != ts_lambda
6242 && (scope != ts_current
6243 /* We may be defining a new type in the initializer
6244 of a static member variable. We allow this when
6245 not pedantic, and it is particularly useful for
6246 type punning via an anonymous union. */
6247 || COMPLETE_TYPE_P (b->this_entity))))
6248 b = b->level_chain;
6250 gcc_assert (identifier_p (name));
6252 /* Do C++ gratuitous typedefing. */
6253 if (identifier_type_value_1 (name) != type)
6255 tree tdef;
6256 int in_class = 0;
6257 tree context = TYPE_CONTEXT (type);
6259 if (! context)
6261 tree cs = current_scope ();
6263 if (scope == ts_current
6264 || scope == ts_lambda
6265 || (cs && TREE_CODE (cs) == FUNCTION_DECL))
6266 context = cs;
6267 else if (cs && TYPE_P (cs))
6268 /* When declaring a friend class of a local class, we want
6269 to inject the newly named class into the scope
6270 containing the local class, not the namespace
6271 scope. */
6272 context = decl_function_context (get_type_decl (cs));
6274 if (!context)
6275 context = current_namespace;
6277 if (b->kind == sk_class
6278 || (b->kind == sk_template_parms
6279 && b->level_chain->kind == sk_class))
6280 in_class = 1;
6282 tdef = create_implicit_typedef (name, type);
6283 DECL_CONTEXT (tdef) = FROB_CONTEXT (context);
6284 if (scope == ts_within_enclosing_non_class)
6286 /* This is a friend. Make this TYPE_DECL node hidden from
6287 ordinary name lookup. Its corresponding TEMPLATE_DECL
6288 will be marked in push_template_decl_real. */
6289 retrofit_lang_decl (tdef);
6290 DECL_ANTICIPATED (tdef) = 1;
6291 DECL_FRIEND_P (tdef) = 1;
6294 decl = maybe_process_template_type_declaration
6295 (type, scope == ts_within_enclosing_non_class, b);
6296 if (decl == error_mark_node)
6297 return decl;
6299 if (b->kind == sk_class)
6301 if (!TYPE_BEING_DEFINED (current_class_type)
6302 && scope != ts_lambda)
6303 return error_mark_node;
6305 if (!PROCESSING_REAL_TEMPLATE_DECL_P ())
6306 /* Put this TYPE_DECL on the TYPE_FIELDS list for the
6307 class. But if it's a member template class, we want
6308 the TEMPLATE_DECL, not the TYPE_DECL, so this is done
6309 later. */
6310 finish_member_declaration (decl);
6311 else
6312 pushdecl_class_level (decl);
6314 else if (b->kind != sk_template_parms)
6316 decl = do_pushdecl_with_scope (decl, b, /*is_friend=*/false);
6317 if (decl == error_mark_node)
6318 return decl;
6320 if (DECL_CONTEXT (decl) == std_node
6321 && init_list_identifier == DECL_NAME (TYPE_NAME (type))
6322 && !CLASSTYPE_TEMPLATE_INFO (type))
6324 error ("declaration of std::initializer_list does not match "
6325 "#include <initializer_list>, isn't a template");
6326 return error_mark_node;
6330 if (! in_class)
6331 set_identifier_type_value_with_scope (name, tdef, b);
6333 TYPE_CONTEXT (type) = DECL_CONTEXT (decl);
6335 /* If this is a local class, keep track of it. We need this
6336 information for name-mangling, and so that it is possible to
6337 find all function definitions in a translation unit in a
6338 convenient way. (It's otherwise tricky to find a member
6339 function definition it's only pointed to from within a local
6340 class.) */
6341 if (TYPE_FUNCTION_SCOPE_P (type))
6343 if (processing_template_decl)
6345 /* Push a DECL_EXPR so we call pushtag at the right time in
6346 template instantiation rather than in some nested context. */
6347 add_decl_expr (decl);
6349 else
6350 vec_safe_push (local_classes, type);
6354 if (b->kind == sk_class
6355 && !COMPLETE_TYPE_P (current_class_type))
6357 maybe_add_class_template_decl_list (current_class_type,
6358 type, /*friend_p=*/0);
6360 if (CLASSTYPE_NESTED_UTDS (current_class_type) == NULL)
6361 CLASSTYPE_NESTED_UTDS (current_class_type)
6362 = binding_table_new (SCOPE_DEFAULT_HT_SIZE);
6364 binding_table_insert
6365 (CLASSTYPE_NESTED_UTDS (current_class_type), name, type);
6368 decl = TYPE_NAME (type);
6369 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
6371 /* Set type visibility now if this is a forward declaration. */
6372 TREE_PUBLIC (decl) = 1;
6373 determine_visibility (decl);
6375 return type;
6378 /* Wrapper for do_pushtag. */
6380 tree
6381 pushtag (tree name, tree type, tag_scope scope)
6383 tree ret;
6384 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6385 ret = do_pushtag (name, type, scope);
6386 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6387 return ret;
6391 /* Subroutines for reverting temporarily to top-level for instantiation
6392 of templates and such. We actually need to clear out the class- and
6393 local-value slots of all identifiers, so that only the global values
6394 are at all visible. Simply setting current_binding_level to the global
6395 scope isn't enough, because more binding levels may be pushed. */
6396 struct saved_scope *scope_chain;
6398 /* Return true if ID has not already been marked. */
6400 static inline bool
6401 store_binding_p (tree id)
6403 if (!id || !IDENTIFIER_BINDING (id))
6404 return false;
6406 if (IDENTIFIER_MARKED (id))
6407 return false;
6409 return true;
6412 /* Add an appropriate binding to *OLD_BINDINGS which needs to already
6413 have enough space reserved. */
6415 static void
6416 store_binding (tree id, vec<cxx_saved_binding, va_gc> **old_bindings)
6418 cxx_saved_binding saved;
6420 gcc_checking_assert (store_binding_p (id));
6422 IDENTIFIER_MARKED (id) = 1;
6424 saved.identifier = id;
6425 saved.binding = IDENTIFIER_BINDING (id);
6426 saved.real_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
6427 (*old_bindings)->quick_push (saved);
6428 IDENTIFIER_BINDING (id) = NULL;
6431 static void
6432 store_bindings (tree names, vec<cxx_saved_binding, va_gc> **old_bindings)
6434 static vec<tree> bindings_need_stored;
6435 tree t, id;
6436 size_t i;
6438 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6439 for (t = names; t; t = TREE_CHAIN (t))
6441 if (TREE_CODE (t) == TREE_LIST)
6442 id = TREE_PURPOSE (t);
6443 else
6444 id = DECL_NAME (t);
6446 if (store_binding_p (id))
6447 bindings_need_stored.safe_push (id);
6449 if (!bindings_need_stored.is_empty ())
6451 vec_safe_reserve_exact (*old_bindings, bindings_need_stored.length ());
6452 for (i = 0; bindings_need_stored.iterate (i, &id); ++i)
6454 /* We can apparently have duplicates in NAMES. */
6455 if (store_binding_p (id))
6456 store_binding (id, old_bindings);
6458 bindings_need_stored.truncate (0);
6460 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6463 /* Like store_bindings, but NAMES is a vector of cp_class_binding
6464 objects, rather than a TREE_LIST. */
6466 static void
6467 store_class_bindings (vec<cp_class_binding, va_gc> *names,
6468 vec<cxx_saved_binding, va_gc> **old_bindings)
6470 static vec<tree> bindings_need_stored;
6471 size_t i;
6472 cp_class_binding *cb;
6474 for (i = 0; vec_safe_iterate (names, i, &cb); ++i)
6475 if (store_binding_p (cb->identifier))
6476 bindings_need_stored.safe_push (cb->identifier);
6477 if (!bindings_need_stored.is_empty ())
6479 tree id;
6480 vec_safe_reserve_exact (*old_bindings, bindings_need_stored.length ());
6481 for (i = 0; bindings_need_stored.iterate (i, &id); ++i)
6482 store_binding (id, old_bindings);
6483 bindings_need_stored.truncate (0);
6487 /* A chain of saved_scope structures awaiting reuse. */
6489 static GTY((deletable)) struct saved_scope *free_saved_scope;
6491 static void
6492 do_push_to_top_level (void)
6494 struct saved_scope *s;
6495 cp_binding_level *b;
6496 cxx_saved_binding *sb;
6497 size_t i;
6498 bool need_pop;
6500 /* Reuse or create a new structure for this saved scope. */
6501 if (free_saved_scope != NULL)
6503 s = free_saved_scope;
6504 free_saved_scope = s->prev;
6506 vec<cxx_saved_binding, va_gc> *old_bindings = s->old_bindings;
6507 memset (s, 0, sizeof (*s));
6508 /* Also reuse the structure's old_bindings vector. */
6509 vec_safe_truncate (old_bindings, 0);
6510 s->old_bindings = old_bindings;
6512 else
6513 s = ggc_cleared_alloc<saved_scope> ();
6515 b = scope_chain ? current_binding_level : 0;
6517 /* If we're in the middle of some function, save our state. */
6518 if (cfun)
6520 need_pop = true;
6521 push_function_context ();
6523 else
6524 need_pop = false;
6526 if (scope_chain && previous_class_level)
6527 store_class_bindings (previous_class_level->class_shadowed,
6528 &s->old_bindings);
6530 /* Have to include the global scope, because class-scope decls
6531 aren't listed anywhere useful. */
6532 for (; b; b = b->level_chain)
6534 tree t;
6536 /* Template IDs are inserted into the global level. If they were
6537 inserted into namespace level, finish_file wouldn't find them
6538 when doing pending instantiations. Therefore, don't stop at
6539 namespace level, but continue until :: . */
6540 if (global_scope_p (b))
6541 break;
6543 store_bindings (b->names, &s->old_bindings);
6544 /* We also need to check class_shadowed to save class-level type
6545 bindings, since pushclass doesn't fill in b->names. */
6546 if (b->kind == sk_class)
6547 store_class_bindings (b->class_shadowed, &s->old_bindings);
6549 /* Unwind type-value slots back to top level. */
6550 for (t = b->type_shadowed; t; t = TREE_CHAIN (t))
6551 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (t), TREE_VALUE (t));
6554 FOR_EACH_VEC_SAFE_ELT (s->old_bindings, i, sb)
6555 IDENTIFIER_MARKED (sb->identifier) = 0;
6557 s->prev = scope_chain;
6558 s->bindings = b;
6559 s->need_pop_function_context = need_pop;
6560 s->function_decl = current_function_decl;
6561 s->unevaluated_operand = cp_unevaluated_operand;
6562 s->inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
6563 s->x_stmt_tree.stmts_are_full_exprs_p = true;
6565 scope_chain = s;
6566 current_function_decl = NULL_TREE;
6567 vec_alloc (current_lang_base, 10);
6568 current_lang_name = lang_name_cplusplus;
6569 current_namespace = global_namespace;
6570 push_class_stack ();
6571 cp_unevaluated_operand = 0;
6572 c_inhibit_evaluation_warnings = 0;
6575 static void
6576 do_pop_from_top_level (void)
6578 struct saved_scope *s = scope_chain;
6579 cxx_saved_binding *saved;
6580 size_t i;
6582 /* Clear out class-level bindings cache. */
6583 if (previous_class_level)
6584 invalidate_class_lookup_cache ();
6585 pop_class_stack ();
6587 current_lang_base = 0;
6589 scope_chain = s->prev;
6590 FOR_EACH_VEC_SAFE_ELT (s->old_bindings, i, saved)
6592 tree id = saved->identifier;
6594 IDENTIFIER_BINDING (id) = saved->binding;
6595 SET_IDENTIFIER_TYPE_VALUE (id, saved->real_type_value);
6598 /* If we were in the middle of compiling a function, restore our
6599 state. */
6600 if (s->need_pop_function_context)
6601 pop_function_context ();
6602 current_function_decl = s->function_decl;
6603 cp_unevaluated_operand = s->unevaluated_operand;
6604 c_inhibit_evaluation_warnings = s->inhibit_evaluation_warnings;
6606 /* Make this saved_scope structure available for reuse by
6607 push_to_top_level. */
6608 s->prev = free_saved_scope;
6609 free_saved_scope = s;
6612 /* Push into the scope of the namespace NS, even if it is deeply
6613 nested within another namespace. */
6615 static void
6616 do_push_nested_namespace (tree ns)
6618 if (ns == global_namespace)
6619 do_push_to_top_level ();
6620 else
6622 do_push_nested_namespace (CP_DECL_CONTEXT (ns));
6623 gcc_checking_assert
6624 (find_namespace_value (current_namespace,
6625 DECL_NAME (ns) ? DECL_NAME (ns)
6626 : anon_identifier) == ns);
6627 resume_scope (NAMESPACE_LEVEL (ns));
6628 current_namespace = ns;
6632 /* Pop back from the scope of the namespace NS, which was previously
6633 entered with push_nested_namespace. */
6635 static void
6636 do_pop_nested_namespace (tree ns)
6638 while (ns != global_namespace)
6640 ns = CP_DECL_CONTEXT (ns);
6641 current_namespace = ns;
6642 leave_scope ();
6645 do_pop_from_top_level ();
6648 /* Add TARGET to USINGS, if it does not already exist there.
6649 We used to build the complete graph of usings at this point, from
6650 the POV of the source namespaces. Now we build that as we perform
6651 the unqualified search. */
6653 static void
6654 add_using_namespace (vec<tree, va_gc> *&usings, tree target)
6656 if (usings)
6657 for (unsigned ix = usings->length (); ix--;)
6658 if ((*usings)[ix] == target)
6659 return;
6661 vec_safe_push (usings, target);
6664 /* Tell the debug system of a using directive. */
6666 static void
6667 emit_debug_info_using_namespace (tree from, tree target, bool implicit)
6669 /* Emit debugging info. */
6670 tree context = from != global_namespace ? from : NULL_TREE;
6671 debug_hooks->imported_module_or_decl (target, NULL_TREE, context, false,
6672 implicit);
6675 /* Process a namespace-scope using directive. */
6677 void
6678 finish_namespace_using_directive (tree target, tree attribs)
6680 gcc_checking_assert (namespace_bindings_p ());
6681 if (target == error_mark_node)
6682 return;
6684 add_using_namespace (DECL_NAMESPACE_USING (current_namespace),
6685 ORIGINAL_NAMESPACE (target));
6686 emit_debug_info_using_namespace (current_namespace,
6687 ORIGINAL_NAMESPACE (target), false);
6689 if (attribs == error_mark_node)
6690 return;
6692 for (tree a = attribs; a; a = TREE_CHAIN (a))
6694 tree name = get_attribute_name (a);
6695 if (is_attribute_p ("strong", name))
6697 warning (0, "strong using directive no longer supported");
6698 if (CP_DECL_CONTEXT (target) == current_namespace)
6699 inform (DECL_SOURCE_LOCATION (target),
6700 "you may use an inline namespace instead");
6702 else
6703 warning (OPT_Wattributes, "%qD attribute directive ignored", name);
6707 /* Process a function-scope using-directive. */
6709 void
6710 finish_local_using_directive (tree target, tree attribs)
6712 gcc_checking_assert (local_bindings_p ());
6713 if (target == error_mark_node)
6714 return;
6716 if (attribs)
6717 warning (OPT_Wattributes, "attributes ignored on local using directive");
6719 add_stmt (build_stmt (input_location, USING_STMT, target));
6721 add_using_namespace (current_binding_level->using_directives,
6722 ORIGINAL_NAMESPACE (target));
6725 /* Pushes X into the global namespace. */
6727 tree
6728 pushdecl_top_level (tree x, bool is_friend)
6730 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6731 do_push_to_top_level ();
6732 x = pushdecl_namespace_level (x, is_friend);
6733 do_pop_from_top_level ();
6734 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6735 return x;
6738 /* Pushes X into the global namespace and calls cp_finish_decl to
6739 register the variable, initializing it with INIT. */
6741 tree
6742 pushdecl_top_level_and_finish (tree x, tree init)
6744 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6745 do_push_to_top_level ();
6746 x = pushdecl_namespace_level (x, false);
6747 cp_finish_decl (x, init, false, NULL_TREE, 0);
6748 do_pop_from_top_level ();
6749 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6750 return x;
6753 /* Enter the namespaces from current_namerspace to NS. */
6755 static int
6756 push_inline_namespaces (tree ns)
6758 int count = 0;
6759 if (ns != current_namespace)
6761 gcc_assert (ns != global_namespace);
6762 count += push_inline_namespaces (CP_DECL_CONTEXT (ns));
6763 resume_scope (NAMESPACE_LEVEL (ns));
6764 current_namespace = ns;
6765 count++;
6767 return count;
6770 /* Push into the scope of the NAME namespace. If NAME is NULL_TREE,
6771 then we enter an anonymous namespace. If MAKE_INLINE is true, then
6772 we create an inline namespace (it is up to the caller to check upon
6773 redefinition). Return the number of namespaces entered. */
6776 push_namespace (tree name, bool make_inline)
6778 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6779 int count = 0;
6781 /* We should not get here if the global_namespace is not yet constructed
6782 nor if NAME designates the global namespace: The global scope is
6783 constructed elsewhere. */
6784 gcc_assert (global_namespace != NULL && name != global_identifier);
6786 if (!name)
6787 name = anon_identifier;
6789 tree ns = NULL_TREE;
6791 name_lookup lookup (name, 0);
6792 if (!lookup.search_qualified (current_namespace, /*usings=*/false))
6794 else if (TREE_CODE (lookup.value) != NAMESPACE_DECL)
6796 else if (tree dna = DECL_NAMESPACE_ALIAS (lookup.value))
6798 /* A namespace alias is not allowed here, but if the alias
6799 is for a namespace also inside the current scope,
6800 accept it with a diagnostic. That's better than dying
6801 horribly. */
6802 if (is_nested_namespace (current_namespace, CP_DECL_CONTEXT (dna)))
6804 error ("namespace alias %qD not allowed here, "
6805 "assuming %qD", lookup.value, dna);
6806 ns = dna;
6809 else
6810 ns = lookup.value;
6813 bool new_ns = false;
6814 if (ns)
6815 /* DR2061. NS might be a member of an inline namespace. We
6816 need to push into those namespaces. */
6817 count += push_inline_namespaces (CP_DECL_CONTEXT (ns));
6818 else
6820 ns = build_lang_decl (NAMESPACE_DECL, name, void_type_node);
6821 SCOPE_DEPTH (ns) = SCOPE_DEPTH (current_namespace) + 1;
6822 if (!SCOPE_DEPTH (ns))
6823 /* We only allow depth 255. */
6824 sorry ("cannot nest more than %d namespaces",
6825 SCOPE_DEPTH (current_namespace));
6826 DECL_CONTEXT (ns) = FROB_CONTEXT (current_namespace);
6827 new_ns = true;
6829 if (pushdecl (ns) == error_mark_node)
6830 ns = NULL_TREE;
6831 else
6833 if (name == anon_identifier)
6835 /* Clear DECL_NAME for the benefit of debugging back ends. */
6836 SET_DECL_ASSEMBLER_NAME (ns, name);
6837 DECL_NAME (ns) = NULL_TREE;
6839 if (!make_inline)
6840 add_using_namespace (DECL_NAMESPACE_USING (current_namespace),
6841 ns);
6843 else if (TREE_PUBLIC (current_namespace))
6844 TREE_PUBLIC (ns) = 1;
6846 if (make_inline)
6848 DECL_NAMESPACE_INLINE_P (ns) = true;
6849 vec_safe_push (DECL_NAMESPACE_INLINEES (current_namespace), ns);
6852 if (name == anon_identifier || make_inline)
6853 emit_debug_info_using_namespace (current_namespace, ns, true);
6857 if (ns)
6859 if (make_inline && !DECL_NAMESPACE_INLINE_P (ns))
6861 error ("inline namespace must be specified at initial definition");
6862 inform (DECL_SOURCE_LOCATION (ns), "%qD defined here", ns);
6864 if (new_ns)
6865 begin_scope (sk_namespace, ns);
6866 else
6867 resume_scope (NAMESPACE_LEVEL (ns));
6868 current_namespace = ns;
6869 count++;
6872 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6873 return count;
6876 /* Pop from the scope of the current namespace. */
6878 void
6879 pop_namespace (void)
6881 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6883 gcc_assert (current_namespace != global_namespace);
6884 current_namespace = CP_DECL_CONTEXT (current_namespace);
6885 /* The binding level is not popped, as it might be re-opened later. */
6886 leave_scope ();
6888 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6891 /* External entry points for do_{push_to/pop_from}_top_level. */
6893 void
6894 push_to_top_level (void)
6896 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6897 do_push_to_top_level ();
6898 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6901 void
6902 pop_from_top_level (void)
6904 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6905 do_pop_from_top_level ();
6906 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6909 /* External entry points for do_{push,pop}_nested_namespace. */
6911 void
6912 push_nested_namespace (tree ns)
6914 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6915 do_push_nested_namespace (ns);
6916 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6919 void
6920 pop_nested_namespace (tree ns)
6922 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6923 gcc_assert (current_namespace == ns);
6924 do_pop_nested_namespace (ns);
6925 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6928 /* Pop off extraneous binding levels left over due to syntax errors.
6929 We don't pop past namespaces, as they might be valid. */
6931 void
6932 pop_everything (void)
6934 if (ENABLE_SCOPE_CHECKING)
6935 verbatim ("XXX entering pop_everything ()\n");
6936 while (!namespace_bindings_p ())
6938 if (current_binding_level->kind == sk_class)
6939 pop_nested_class ();
6940 else
6941 poplevel (0, 0, 0);
6943 if (ENABLE_SCOPE_CHECKING)
6944 verbatim ("XXX leaving pop_everything ()\n");
6947 /* Emit debugging information for using declarations and directives.
6948 If input tree is overloaded fn then emit debug info for all
6949 candidates. */
6951 void
6952 cp_emit_debug_info_for_using (tree t, tree context)
6954 /* Don't try to emit any debug information if we have errors. */
6955 if (seen_error ())
6956 return;
6958 /* Ignore this FUNCTION_DECL if it refers to a builtin declaration
6959 of a builtin function. */
6960 if (TREE_CODE (t) == FUNCTION_DECL
6961 && DECL_EXTERNAL (t)
6962 && DECL_BUILT_IN (t))
6963 return;
6965 /* Do not supply context to imported_module_or_decl, if
6966 it is a global namespace. */
6967 if (context == global_namespace)
6968 context = NULL_TREE;
6970 t = MAYBE_BASELINK_FUNCTIONS (t);
6972 /* FIXME: Handle TEMPLATE_DECLs. */
6973 for (lkp_iterator iter (t); iter; ++iter)
6975 tree fn = *iter;
6976 if (TREE_CODE (fn) != TEMPLATE_DECL)
6978 if (building_stmt_list_p ())
6979 add_stmt (build_stmt (input_location, USING_STMT, fn));
6980 else
6981 debug_hooks->imported_module_or_decl (fn, NULL_TREE, context,
6982 false, false);
6987 #include "gt-cp-name-lookup.h"