PR rtl-optimization/82913
[official-gcc.git] / gcc / cp / name-lookup.c
blobb4976d8b7ccf420d3da5d2b9cd5f8dd21721d2b1
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 = DECL_NAMESPACE_BINDINGS (ns)
90 ->find_slot_with_hash (name, name ? IDENTIFIER_HASH_VALUE (name) : 0,
91 create_p ? INSERT : NO_INSERT);
92 return slot;
95 static tree
96 find_namespace_value (tree ns, tree name)
98 tree *b = find_namespace_slot (ns, name);
100 return b ? MAYBE_STAT_DECL (*b) : NULL_TREE;
103 /* Add DECL to the list of things declared in B. */
105 static void
106 add_decl_to_level (cp_binding_level *b, tree decl)
108 gcc_assert (b->kind != sk_class);
110 /* Make sure we don't create a circular list. xref_tag can end
111 up pushing the same artificial decl more than once. We
112 should have already detected that in update_binding. */
113 gcc_assert (b->names != decl);
115 /* We build up the list in reverse order, and reverse it later if
116 necessary. */
117 TREE_CHAIN (decl) = b->names;
118 b->names = decl;
120 /* If appropriate, add decl to separate list of statics. We
121 include extern variables because they might turn out to be
122 static later. It's OK for this list to contain a few false
123 positives. */
124 if (b->kind == sk_namespace
125 && ((VAR_P (decl)
126 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
127 || (TREE_CODE (decl) == FUNCTION_DECL
128 && (!TREE_PUBLIC (decl)
129 || decl_anon_ns_mem_p (decl)
130 || DECL_DECLARED_INLINE_P (decl)))))
131 vec_safe_push (static_decls, decl);
134 /* Find the binding for NAME in the local binding level B. */
136 static cxx_binding *
137 find_local_binding (cp_binding_level *b, tree name)
139 if (cxx_binding *binding = IDENTIFIER_BINDING (name))
140 for (;; b = b->level_chain)
142 if (binding->scope == b
143 && !(VAR_P (binding->value)
144 && DECL_DEAD_FOR_LOCAL (binding->value)))
145 return binding;
147 /* Cleanup contours are transparent to the language. */
148 if (b->kind != sk_cleanup)
149 break;
151 return NULL;
154 struct name_lookup
156 public:
157 typedef std::pair<tree, tree> using_pair;
158 typedef vec<using_pair, va_heap, vl_embed> using_queue;
160 public:
161 tree name; /* The identifier being looked for. */
162 tree value; /* A (possibly ambiguous) set of things found. */
163 tree type; /* A type that has been found. */
164 int flags; /* Lookup flags. */
165 bool deduping; /* Full deduping is needed because using declarations
166 are in play. */
167 vec<tree, va_heap, vl_embed> *scopes;
168 name_lookup *previous; /* Previously active lookup. */
170 protected:
171 /* Marked scope stack for outermost name lookup. */
172 static vec<tree, va_heap, vl_embed> *shared_scopes;
173 /* Currently active lookup. */
174 static name_lookup *active;
176 public:
177 name_lookup (tree n, int f = 0)
178 : name (n), value (NULL_TREE), type (NULL_TREE), flags (f),
179 deduping (false), scopes (NULL), previous (NULL)
181 preserve_state ();
183 ~name_lookup ()
185 restore_state ();
188 private: /* Uncopyable, unmovable, unassignable. I am a rock. */
189 name_lookup (const name_lookup &);
190 name_lookup &operator= (const name_lookup &);
192 protected:
193 static bool seen_p (tree scope)
195 return LOOKUP_SEEN_P (scope);
197 static bool found_p (tree scope)
199 return LOOKUP_FOUND_P (scope);
202 void mark_seen (tree scope); /* Mark and add to scope vector. */
203 static void mark_found (tree scope)
205 gcc_checking_assert (seen_p (scope));
206 LOOKUP_FOUND_P (scope) = true;
208 bool see_and_mark (tree scope)
210 bool ret = seen_p (scope);
211 if (!ret)
212 mark_seen (scope);
213 return ret;
215 bool find_and_mark (tree scope);
217 private:
218 void preserve_state ();
219 void restore_state ();
221 private:
222 static tree ambiguous (tree thing, tree current);
223 void add_overload (tree fns);
224 void add_value (tree new_val);
225 void add_type (tree new_type);
226 bool process_binding (tree val_bind, tree type_bind);
228 /* Look in only namespace. */
229 bool search_namespace_only (tree scope);
230 /* Look in namespace and its (recursive) inlines. Ignore using
231 directives. Return true if something found (inc dups). */
232 bool search_namespace (tree scope);
233 /* Look in the using directives of namespace + inlines using
234 qualified lookup rules. */
235 bool search_usings (tree scope);
237 private:
238 using_queue *queue_namespace (using_queue *queue, int depth, tree scope);
239 using_queue *do_queue_usings (using_queue *queue, int depth,
240 vec<tree, va_gc> *usings);
241 using_queue *queue_usings (using_queue *queue, int depth,
242 vec<tree, va_gc> *usings)
244 if (usings)
245 queue = do_queue_usings (queue, depth, usings);
246 return queue;
249 private:
250 void add_fns (tree);
252 void adl_expr (tree);
253 void adl_type (tree);
254 void adl_template_arg (tree);
255 void adl_class (tree);
256 void adl_bases (tree);
257 void adl_class_only (tree);
258 void adl_namespace (tree);
259 void adl_namespace_only (tree);
261 public:
262 /* Search namespace + inlines + maybe usings as qualified lookup. */
263 bool search_qualified (tree scope, bool usings = true);
265 /* Search namespace + inlines + usings as unqualified lookup. */
266 bool search_unqualified (tree scope, cp_binding_level *);
268 /* ADL lookup of ARGS. */
269 tree search_adl (tree fns, vec<tree, va_gc> *args);
272 /* Scope stack shared by all outermost lookups. This avoids us
273 allocating and freeing on every single lookup. */
274 vec<tree, va_heap, vl_embed> *name_lookup::shared_scopes;
276 /* Currently active lookup. */
277 name_lookup *name_lookup::active;
279 /* Name lookup is recursive, becase ADL can cause template
280 instatiation. This is of course a rare event, so we optimize for
281 it not happening. When we discover an active name-lookup, which
282 must be an ADL lookup, we need to unmark the marked scopes and also
283 unmark the lookup we might have been accumulating. */
285 void
286 name_lookup::preserve_state ()
288 previous = active;
289 if (previous)
291 unsigned length = vec_safe_length (previous->scopes);
292 vec_safe_reserve (previous->scopes, length * 2);
293 for (unsigned ix = length; ix--;)
295 tree decl = (*previous->scopes)[ix];
297 gcc_checking_assert (LOOKUP_SEEN_P (decl));
298 LOOKUP_SEEN_P (decl) = false;
300 /* Preserve the FOUND_P state on the interrupted lookup's
301 stack. */
302 if (LOOKUP_FOUND_P (decl))
304 LOOKUP_FOUND_P (decl) = false;
305 previous->scopes->quick_push (decl);
309 /* Unmark the outer partial lookup. */
310 if (previous->deduping)
311 lookup_mark (previous->value, false);
313 else
314 scopes = shared_scopes;
315 active = this;
318 /* Restore the marking state of a lookup we interrupted. */
320 void
321 name_lookup::restore_state ()
323 if (deduping)
324 lookup_mark (value, false);
326 /* Unmark and empty this lookup's scope stack. */
327 for (unsigned ix = vec_safe_length (scopes); ix--;)
329 tree decl = scopes->pop ();
330 gcc_checking_assert (LOOKUP_SEEN_P (decl));
331 LOOKUP_SEEN_P (decl) = false;
332 LOOKUP_FOUND_P (decl) = false;
335 active = previous;
336 if (previous)
338 free (scopes);
340 unsigned length = vec_safe_length (previous->scopes);
341 for (unsigned ix = 0; ix != length; ix++)
343 tree decl = (*previous->scopes)[ix];
344 if (LOOKUP_SEEN_P (decl))
346 /* The remainder of the scope stack must be recording
347 FOUND_P decls, which we want to pop off. */
350 tree decl = previous->scopes->pop ();
351 gcc_checking_assert (LOOKUP_SEEN_P (decl)
352 && !LOOKUP_FOUND_P (decl));
353 LOOKUP_FOUND_P (decl) = true;
355 while (++ix != length);
356 break;
359 gcc_checking_assert (!LOOKUP_FOUND_P (decl));
360 LOOKUP_SEEN_P (decl) = true;
363 /* Remark the outer partial lookup. */
364 if (previous->deduping)
365 lookup_mark (previous->value, true);
367 else
368 shared_scopes = scopes;
371 void
372 name_lookup::mark_seen (tree scope)
374 gcc_checking_assert (!seen_p (scope));
375 LOOKUP_SEEN_P (scope) = true;
376 vec_safe_push (scopes, scope);
379 bool
380 name_lookup::find_and_mark (tree scope)
382 bool result = LOOKUP_FOUND_P (scope);
383 if (!result)
385 LOOKUP_FOUND_P (scope) = true;
386 if (!LOOKUP_SEEN_P (scope))
387 vec_safe_push (scopes, scope);
390 return result;
393 /* THING and CURRENT are ambiguous, concatenate them. */
395 tree
396 name_lookup::ambiguous (tree thing, tree current)
398 if (TREE_CODE (current) != TREE_LIST)
400 current = build_tree_list (NULL_TREE, current);
401 TREE_TYPE (current) = error_mark_node;
403 current = tree_cons (NULL_TREE, thing, current);
404 TREE_TYPE (current) = error_mark_node;
406 return current;
409 /* FNS is a new overload set to add to the exising set. */
411 void
412 name_lookup::add_overload (tree fns)
414 if (!deduping && TREE_CODE (fns) == OVERLOAD)
416 tree probe = fns;
417 if (flags & LOOKUP_HIDDEN)
418 probe = ovl_skip_hidden (probe);
419 if (probe && TREE_CODE (probe) == OVERLOAD && OVL_USING_P (probe))
421 /* We're about to add something found by a using
422 declaration, so need to engage deduping mode. */
423 lookup_mark (value, true);
424 deduping = true;
428 value = lookup_maybe_add (fns, value, deduping);
431 /* Add a NEW_VAL, a found value binding into the current value binding. */
433 void
434 name_lookup::add_value (tree new_val)
436 if (OVL_P (new_val) && (!value || OVL_P (value)))
437 add_overload (new_val);
438 else if (!value)
439 value = new_val;
440 else if (value == new_val)
442 else if ((TREE_CODE (value) == TYPE_DECL
443 && TREE_CODE (new_val) == TYPE_DECL
444 && same_type_p (TREE_TYPE (value), TREE_TYPE (new_val))))
445 /* Typedefs to the same type. */;
446 else if (TREE_CODE (value) == NAMESPACE_DECL
447 && TREE_CODE (new_val) == NAMESPACE_DECL
448 && ORIGINAL_NAMESPACE (value) == ORIGINAL_NAMESPACE (new_val))
449 /* Namespace (possibly aliased) to the same namespace. Locate
450 the namespace*/
451 value = ORIGINAL_NAMESPACE (value);
452 else
454 if (deduping)
456 /* Disengage deduping mode. */
457 lookup_mark (value, false);
458 deduping = false;
460 value = ambiguous (new_val, value);
464 /* Add a NEW_TYPE, a found type binding into the current type binding. */
466 void
467 name_lookup::add_type (tree new_type)
469 if (!type)
470 type = new_type;
471 else if (TREE_CODE (type) == TREE_LIST
472 || !same_type_p (TREE_TYPE (type), TREE_TYPE (new_type)))
473 type = ambiguous (new_type, type);
476 /* Process a found binding containing NEW_VAL and NEW_TYPE. Returns
477 true if we actually found something noteworthy. */
479 bool
480 name_lookup::process_binding (tree new_val, tree new_type)
482 /* Did we really see a type? */
483 if (new_type
484 && (LOOKUP_NAMESPACES_ONLY (flags)
485 || (!(flags & LOOKUP_HIDDEN)
486 && DECL_LANG_SPECIFIC (new_type)
487 && DECL_ANTICIPATED (new_type))))
488 new_type = NULL_TREE;
490 if (new_val && !(flags & LOOKUP_HIDDEN))
491 new_val = ovl_skip_hidden (new_val);
493 /* Do we really see a value? */
494 if (new_val)
495 switch (TREE_CODE (new_val))
497 case TEMPLATE_DECL:
498 /* If we expect types or namespaces, and not templates,
499 or this is not a template class. */
500 if ((LOOKUP_QUALIFIERS_ONLY (flags)
501 && !DECL_TYPE_TEMPLATE_P (new_val)))
502 new_val = NULL_TREE;
503 break;
504 case TYPE_DECL:
505 if (LOOKUP_NAMESPACES_ONLY (flags)
506 || (new_type && (flags & LOOKUP_PREFER_TYPES)))
507 new_val = NULL_TREE;
508 break;
509 case NAMESPACE_DECL:
510 if (LOOKUP_TYPES_ONLY (flags))
511 new_val = NULL_TREE;
512 break;
513 default:
514 if (LOOKUP_QUALIFIERS_ONLY (flags))
515 new_val = NULL_TREE;
518 if (!new_val)
520 new_val = new_type;
521 new_type = NULL_TREE;
524 /* Merge into the lookup */
525 if (new_val)
526 add_value (new_val);
527 if (new_type)
528 add_type (new_type);
530 return new_val != NULL_TREE;
533 /* Look in exactly namespace SCOPE. */
535 bool
536 name_lookup::search_namespace_only (tree scope)
538 bool found = false;
540 if (tree *binding = find_namespace_slot (scope, name))
541 found |= process_binding (MAYBE_STAT_DECL (*binding),
542 MAYBE_STAT_TYPE (*binding));
544 return found;
547 /* Conditionally look in namespace SCOPE and inline children. */
549 bool
550 name_lookup::search_namespace (tree scope)
552 if (see_and_mark (scope))
553 /* We've visited this scope before. Return what we found then. */
554 return found_p (scope);
556 /* Look in exactly namespace. */
557 bool found = search_namespace_only (scope);
559 /* Recursively look in its inline children. */
560 if (vec<tree, va_gc> *inlinees = DECL_NAMESPACE_INLINEES (scope))
561 for (unsigned ix = inlinees->length (); ix--;)
562 found |= search_namespace ((*inlinees)[ix]);
564 if (found)
565 mark_found (scope);
567 return found;
570 /* Recursively follow using directives of SCOPE & its inline children.
571 Such following is essentially a flood-fill algorithm. */
573 bool
574 name_lookup::search_usings (tree scope)
576 /* We do not check seen_p here, as that was already set during the
577 namespace_only walk. */
578 if (found_p (scope))
579 return true;
581 bool found = false;
582 if (vec<tree, va_gc> *usings = DECL_NAMESPACE_USING (scope))
583 for (unsigned ix = usings->length (); ix--;)
584 found |= search_qualified ((*usings)[ix], true);
586 /* Look in its inline children. */
587 if (vec<tree, va_gc> *inlinees = DECL_NAMESPACE_INLINEES (scope))
588 for (unsigned ix = inlinees->length (); ix--;)
589 found |= search_usings ((*inlinees)[ix]);
591 if (found)
592 mark_found (scope);
594 return found;
597 /* Qualified namespace lookup in SCOPE.
598 1) Look in SCOPE (+inlines). If found, we're done.
599 2) Otherwise, if USINGS is true,
600 recurse for every using directive of SCOPE (+inlines).
602 Trickiness is (a) loops and (b) multiple paths to same namespace.
603 In both cases we want to not repeat any lookups, and know whether
604 to stop the caller's step #2. Do this via the FOUND_P marker. */
606 bool
607 name_lookup::search_qualified (tree scope, bool usings)
609 bool found = false;
611 if (seen_p (scope))
612 found = found_p (scope);
613 else
615 found = search_namespace (scope);
616 if (!found && usings)
617 found = search_usings (scope);
620 return found;
623 /* Add SCOPE to the unqualified search queue, recursively add its
624 inlines and those via using directives. */
626 name_lookup::using_queue *
627 name_lookup::queue_namespace (using_queue *queue, int depth, tree scope)
629 if (see_and_mark (scope))
630 return queue;
632 /* Record it. */
633 tree common = scope;
634 while (SCOPE_DEPTH (common) > depth)
635 common = CP_DECL_CONTEXT (common);
636 vec_safe_push (queue, using_pair (common, scope));
638 /* Queue its inline children. */
639 if (vec<tree, va_gc> *inlinees = DECL_NAMESPACE_INLINEES (scope))
640 for (unsigned ix = inlinees->length (); ix--;)
641 queue = queue_namespace (queue, depth, (*inlinees)[ix]);
643 /* Queue its using targets. */
644 queue = queue_usings (queue, depth, DECL_NAMESPACE_USING (scope));
646 return queue;
649 /* Add the namespaces in USINGS to the unqualified search queue. */
651 name_lookup::using_queue *
652 name_lookup::do_queue_usings (using_queue *queue, int depth,
653 vec<tree, va_gc> *usings)
655 for (unsigned ix = usings->length (); ix--;)
656 queue = queue_namespace (queue, depth, (*usings)[ix]);
658 return queue;
661 /* Unqualified namespace lookup in SCOPE.
662 1) add scope+inlins to worklist.
663 2) recursively add target of every using directive
664 3) for each worklist item where SCOPE is common ancestor, search it
665 4) if nothing find, scope=parent, goto 1. */
667 bool
668 name_lookup::search_unqualified (tree scope, cp_binding_level *level)
670 /* Make static to avoid continual reallocation. We're not
671 recursive. */
672 static using_queue *queue = NULL;
673 bool found = false;
674 int length = vec_safe_length (queue);
676 /* Queue local using-directives. */
677 for (; level->kind != sk_namespace; level = level->level_chain)
678 queue = queue_usings (queue, SCOPE_DEPTH (scope), level->using_directives);
680 for (; !found; scope = CP_DECL_CONTEXT (scope))
682 gcc_assert (!DECL_NAMESPACE_ALIAS (scope));
683 int depth = SCOPE_DEPTH (scope);
685 /* Queue namespaces reachable from SCOPE. */
686 queue = queue_namespace (queue, depth, scope);
688 /* Search every queued namespace where SCOPE is the common
689 ancestor. Adjust the others. */
690 unsigned ix = length;
693 using_pair &pair = (*queue)[ix];
694 while (pair.first == scope)
696 found |= search_namespace_only (pair.second);
697 pair = queue->pop ();
698 if (ix == queue->length ())
699 goto done;
701 /* The depth is the same as SCOPE, find the parent scope. */
702 if (SCOPE_DEPTH (pair.first) == depth)
703 pair.first = CP_DECL_CONTEXT (pair.first);
704 ix++;
706 while (ix < queue->length ());
707 done:;
708 if (scope == global_namespace)
709 break;
712 vec_safe_truncate (queue, length);
714 return found;
717 /* FNS is a value binding. If it is a (set of overloaded) functions,
718 add them into the current value. */
720 void
721 name_lookup::add_fns (tree fns)
723 if (!fns)
724 return;
725 else if (TREE_CODE (fns) == OVERLOAD)
727 if (TREE_TYPE (fns) != unknown_type_node)
728 fns = OVL_FUNCTION (fns);
730 else if (!DECL_DECLARES_FUNCTION_P (fns))
731 return;
733 add_overload (fns);
736 /* Add functions of a namespace to the lookup structure. */
738 void
739 name_lookup::adl_namespace_only (tree scope)
741 mark_seen (scope);
743 /* Look down into inline namespaces. */
744 if (vec<tree, va_gc> *inlinees = DECL_NAMESPACE_INLINEES (scope))
745 for (unsigned ix = inlinees->length (); ix--;)
746 adl_namespace_only ((*inlinees)[ix]);
748 if (tree fns = find_namespace_value (scope, name))
749 add_fns (ovl_skip_hidden (fns));
752 /* Find the containing non-inlined namespace, add it and all its
753 inlinees. */
755 void
756 name_lookup::adl_namespace (tree scope)
758 if (seen_p (scope))
759 return;
761 /* Find the containing non-inline namespace. */
762 while (DECL_NAMESPACE_INLINE_P (scope))
763 scope = CP_DECL_CONTEXT (scope);
765 adl_namespace_only (scope);
768 /* Adds the class and its friends to the lookup structure. */
770 void
771 name_lookup::adl_class_only (tree type)
773 /* Backend-built structures, such as __builtin_va_list, aren't
774 affected by all this. */
775 if (!CLASS_TYPE_P (type))
776 return;
778 type = TYPE_MAIN_VARIANT (type);
780 if (see_and_mark (type))
781 return;
783 tree context = decl_namespace_context (type);
784 adl_namespace (context);
786 complete_type (type);
788 /* Add friends. */
789 for (tree list = DECL_FRIENDLIST (TYPE_MAIN_DECL (type)); list;
790 list = TREE_CHAIN (list))
791 if (name == FRIEND_NAME (list))
792 for (tree friends = FRIEND_DECLS (list); friends;
793 friends = TREE_CHAIN (friends))
795 tree fn = TREE_VALUE (friends);
797 /* Only interested in global functions with potentially hidden
798 (i.e. unqualified) declarations. */
799 if (CP_DECL_CONTEXT (fn) != context)
800 continue;
802 /* Only interested in anticipated friends. (Non-anticipated
803 ones will have been inserted during the namespace
804 adl.) */
805 if (!DECL_ANTICIPATED (fn))
806 continue;
808 /* Template specializations are never found by name lookup.
809 (Templates themselves can be found, but not template
810 specializations.) */
811 if (TREE_CODE (fn) == FUNCTION_DECL && DECL_USE_TEMPLATE (fn))
812 continue;
814 add_fns (fn);
818 /* Adds the class and its bases to the lookup structure.
819 Returns true on error. */
821 void
822 name_lookup::adl_bases (tree type)
824 adl_class_only (type);
826 /* Process baseclasses. */
827 if (tree binfo = TYPE_BINFO (type))
829 tree base_binfo;
830 int i;
832 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
833 adl_bases (BINFO_TYPE (base_binfo));
837 /* Adds everything associated with a class argument type to the lookup
838 structure. Returns true on error.
840 If T is a class type (including unions), its associated classes are: the
841 class itself; the class of which it is a member, if any; and its direct
842 and indirect base classes. Its associated namespaces are the namespaces
843 of which its associated classes are members. Furthermore, if T is a
844 class template specialization, its associated namespaces and classes
845 also include: the namespaces and classes associated with the types of
846 the template arguments provided for template type parameters (excluding
847 template template parameters); the namespaces of which any template
848 template arguments are members; and the classes of which any member
849 templates used as template template arguments are members. [ Note:
850 non-type template arguments do not contribute to the set of associated
851 namespaces. --end note] */
853 void
854 name_lookup::adl_class (tree type)
856 /* Backend build structures, such as __builtin_va_list, aren't
857 affected by all this. */
858 if (!CLASS_TYPE_P (type))
859 return;
861 type = TYPE_MAIN_VARIANT (type);
862 /* We don't set found here because we have to have set seen first,
863 which is done in the adl_bases walk. */
864 if (found_p (type))
865 return;
867 adl_bases (type);
868 mark_found (type);
870 if (TYPE_CLASS_SCOPE_P (type))
871 adl_class_only (TYPE_CONTEXT (type));
873 /* Process template arguments. */
874 if (CLASSTYPE_TEMPLATE_INFO (type)
875 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
877 tree list = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
878 for (int i = 0; i < TREE_VEC_LENGTH (list); ++i)
879 adl_template_arg (TREE_VEC_ELT (list, i));
883 void
884 name_lookup::adl_expr (tree expr)
886 if (!expr)
887 return;
889 gcc_assert (!TYPE_P (expr));
891 if (TREE_TYPE (expr) != unknown_type_node)
893 adl_type (TREE_TYPE (expr));
894 return;
897 if (TREE_CODE (expr) == ADDR_EXPR)
898 expr = TREE_OPERAND (expr, 0);
899 if (TREE_CODE (expr) == COMPONENT_REF
900 || TREE_CODE (expr) == OFFSET_REF)
901 expr = TREE_OPERAND (expr, 1);
902 expr = MAYBE_BASELINK_FUNCTIONS (expr);
904 if (OVL_P (expr))
905 for (lkp_iterator iter (expr); iter; ++iter)
906 adl_type (TREE_TYPE (*iter));
907 else if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
909 /* The working paper doesn't currently say how to handle
910 template-id arguments. The sensible thing would seem to be
911 to handle the list of template candidates like a normal
912 overload set, and handle the template arguments like we do
913 for class template specializations. */
915 /* First the templates. */
916 adl_expr (TREE_OPERAND (expr, 0));
918 /* Now the arguments. */
919 if (tree args = TREE_OPERAND (expr, 1))
920 for (int ix = TREE_VEC_LENGTH (args); ix--;)
921 adl_template_arg (TREE_VEC_ELT (args, ix));
925 void
926 name_lookup::adl_type (tree type)
928 if (!type)
929 return;
931 if (TYPE_PTRDATAMEM_P (type))
933 /* Pointer to member: associate class type and value type. */
934 adl_type (TYPE_PTRMEM_CLASS_TYPE (type));
935 adl_type (TYPE_PTRMEM_POINTED_TO_TYPE (type));
936 return;
939 switch (TREE_CODE (type))
941 case RECORD_TYPE:
942 if (TYPE_PTRMEMFUNC_P (type))
944 adl_type (TYPE_PTRMEMFUNC_FN_TYPE (type));
945 return;
947 /* FALLTHRU */
948 case UNION_TYPE:
949 adl_class (type);
950 return;
952 case METHOD_TYPE:
953 /* The basetype is referenced in the first arg type, so just
954 fall through. */
955 case FUNCTION_TYPE:
956 /* Associate the parameter types. */
957 for (tree args = TYPE_ARG_TYPES (type); args; args = TREE_CHAIN (args))
958 adl_type (TREE_VALUE (args));
959 /* FALLTHROUGH */
961 case POINTER_TYPE:
962 case REFERENCE_TYPE:
963 case ARRAY_TYPE:
964 adl_type (TREE_TYPE (type));
965 return;
967 case ENUMERAL_TYPE:
968 if (TYPE_CLASS_SCOPE_P (type))
969 adl_class_only (TYPE_CONTEXT (type));
970 adl_namespace (decl_namespace_context (type));
971 return;
973 case LANG_TYPE:
974 gcc_assert (type == unknown_type_node
975 || type == init_list_type_node);
976 return;
978 case TYPE_PACK_EXPANSION:
979 adl_type (PACK_EXPANSION_PATTERN (type));
980 return;
982 default:
983 break;
987 /* Adds everything associated with a template argument to the lookup
988 structure. */
990 void
991 name_lookup::adl_template_arg (tree arg)
993 /* [basic.lookup.koenig]
995 If T is a template-id, its associated namespaces and classes are
996 ... the namespaces and classes associated with the types of the
997 template arguments provided for template type parameters
998 (excluding template template parameters); the namespaces in which
999 any template template arguments are defined; and the classes in
1000 which any member templates used as template template arguments
1001 are defined. [Note: non-type template arguments do not
1002 contribute to the set of associated namespaces. ] */
1004 /* Consider first template template arguments. */
1005 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
1006 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
1008 else if (TREE_CODE (arg) == TEMPLATE_DECL)
1010 tree ctx = CP_DECL_CONTEXT (arg);
1012 /* It's not a member template. */
1013 if (TREE_CODE (ctx) == NAMESPACE_DECL)
1014 adl_namespace (ctx);
1015 /* Otherwise, it must be member template. */
1016 else
1017 adl_class_only (ctx);
1019 /* It's an argument pack; handle it recursively. */
1020 else if (ARGUMENT_PACK_P (arg))
1022 tree args = ARGUMENT_PACK_ARGS (arg);
1023 int i, len = TREE_VEC_LENGTH (args);
1024 for (i = 0; i < len; ++i)
1025 adl_template_arg (TREE_VEC_ELT (args, i));
1027 /* It's not a template template argument, but it is a type template
1028 argument. */
1029 else if (TYPE_P (arg))
1030 adl_type (arg);
1033 /* Perform ADL lookup. FNS is the existing lookup result and ARGS are
1034 the call arguments. */
1036 tree
1037 name_lookup::search_adl (tree fns, vec<tree, va_gc> *args)
1039 if (fns)
1041 deduping = true;
1042 lookup_mark (fns, true);
1044 value = fns;
1046 unsigned ix;
1047 tree arg;
1049 FOR_EACH_VEC_ELT_REVERSE (*args, ix, arg)
1050 /* OMP reduction operators put an ADL-significant type as the
1051 first arg. */
1052 if (TYPE_P (arg))
1053 adl_type (arg);
1054 else
1055 adl_expr (arg);
1057 fns = value;
1059 return fns;
1062 static bool qualified_namespace_lookup (tree, name_lookup *);
1063 static void consider_binding_level (tree name,
1064 best_match <tree, const char *> &bm,
1065 cp_binding_level *lvl,
1066 bool look_within_fields,
1067 enum lookup_name_fuzzy_kind kind);
1068 static void diagnose_name_conflict (tree, tree);
1070 /* ADL lookup of NAME. FNS is the result of regular lookup, and we
1071 don't add duplicates to it. ARGS is the vector of call
1072 arguments (which will not be empty). */
1074 tree
1075 lookup_arg_dependent (tree name, tree fns, vec<tree, va_gc> *args)
1077 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
1078 name_lookup lookup (name);
1079 fns = lookup.search_adl (fns, args);
1080 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
1081 return fns;
1084 /* FNS is an overload set of conversion functions. Return the
1085 overloads converting to TYPE. */
1087 static tree
1088 extract_conversion_operator (tree fns, tree type)
1090 tree convs = NULL_TREE;
1091 tree tpls = NULL_TREE;
1093 for (ovl_iterator iter (fns); iter; ++iter)
1095 if (same_type_p (DECL_CONV_FN_TYPE (*iter), type))
1096 convs = lookup_add (*iter, convs);
1098 if (TREE_CODE (*iter) == TEMPLATE_DECL)
1099 tpls = lookup_add (*iter, tpls);
1102 if (!convs)
1103 convs = tpls;
1105 return convs;
1108 /* Binary search of (ordered) MEMBER_VEC for NAME. */
1110 static tree
1111 member_vec_binary_search (vec<tree, va_gc> *member_vec, tree name)
1113 for (unsigned lo = 0, hi = member_vec->length (); lo < hi;)
1115 unsigned mid = (lo + hi) / 2;
1116 tree binding = (*member_vec)[mid];
1117 tree binding_name = OVL_NAME (binding);
1119 if (binding_name > name)
1120 hi = mid;
1121 else if (binding_name < name)
1122 lo = mid + 1;
1123 else
1124 return binding;
1127 return NULL_TREE;
1130 /* Linear search of (unordered) MEMBER_VEC for NAME. */
1132 static tree
1133 member_vec_linear_search (vec<tree, va_gc> *member_vec, tree name)
1135 for (int ix = member_vec->length (); ix--;)
1136 /* We can get a NULL binding during insertion of a new method
1137 name, because the identifier_binding machinery performs a
1138 lookup. If we find such a NULL slot, that's the thing we were
1139 looking for, so we might as well bail out immediately. */
1140 if (tree binding = (*member_vec)[ix])
1142 if (OVL_NAME (binding) == name)
1143 return binding;
1145 else
1146 break;
1148 return NULL_TREE;
1151 /* Linear search of (partially ordered) fields of KLASS for NAME. */
1153 static tree
1154 fields_linear_search (tree klass, tree name, bool want_type)
1156 for (tree fields = TYPE_FIELDS (klass); fields; fields = DECL_CHAIN (fields))
1158 tree decl = fields;
1160 if (!want_type
1161 && TREE_CODE (decl) == FIELD_DECL
1162 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
1164 tree anon = TREE_TYPE (decl);
1165 gcc_assert (COMPLETE_TYPE_P (anon));
1166 tree temp;
1168 if (vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (anon))
1169 temp = member_vec_linear_search (member_vec, name);
1170 else
1171 temp = fields_linear_search (anon, name, want_type);
1173 if (temp)
1175 /* Anon members can only contain fields. */
1176 gcc_assert (!STAT_HACK_P (temp) && !DECL_DECLARES_TYPE_P (temp));
1177 return temp;
1181 if (DECL_NAME (decl) != name)
1182 continue;
1184 if (TREE_CODE (decl) == USING_DECL)
1186 decl = strip_using_decl (decl);
1187 if (is_overloaded_fn (decl))
1188 continue;
1191 if (DECL_DECLARES_FUNCTION_P (decl))
1192 /* Functions are found separately. */
1193 continue;
1195 if (!want_type || DECL_DECLARES_TYPE_P (decl))
1196 return decl;
1199 return NULL_TREE;
1202 /* Look for NAME as an immediate member of KLASS (including
1203 anon-members or unscoped enum member). TYPE_OR_FNS is zero for
1204 regular search. >0 to get a type binding (if there is one) and <0
1205 if you want (just) the member function binding.
1207 Use this if you do not want lazy member creation. */
1209 tree
1210 get_class_binding_direct (tree klass, tree name, int type_or_fns)
1212 gcc_checking_assert (RECORD_OR_UNION_TYPE_P (klass));
1214 /* Conversion operators can only be found by the marker conversion
1215 operator name. */
1216 bool conv_op = IDENTIFIER_CONV_OP_P (name);
1217 tree lookup = conv_op ? conv_op_identifier : name;
1218 tree val = NULL_TREE;
1219 vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (klass);
1221 if (COMPLETE_TYPE_P (klass) && member_vec)
1223 val = member_vec_binary_search (member_vec, lookup);
1224 if (!val)
1226 else if (type_or_fns > 0)
1228 if (STAT_HACK_P (val))
1229 val = STAT_TYPE (val);
1230 else if (!DECL_DECLARES_TYPE_P (val))
1231 val = NULL_TREE;
1233 else if (STAT_HACK_P (val))
1234 val = STAT_DECL (val);
1236 if (val && TREE_CODE (val) == OVERLOAD
1237 && TREE_CODE (OVL_FUNCTION (val)) == USING_DECL)
1239 /* An overload with a dependent USING_DECL. Does the caller
1240 want the USING_DECL or the functions? */
1241 if (type_or_fns < 0)
1242 val = OVL_CHAIN (val);
1243 else
1244 val = OVL_FUNCTION (val);
1247 else
1249 if (member_vec && type_or_fns <= 0)
1250 val = member_vec_linear_search (member_vec, lookup);
1252 if (type_or_fns < 0)
1253 /* Don't bother looking for field. We don't want it. */;
1254 else if (!val || (TREE_CODE (val) == OVERLOAD && OVL_USING_P (val)))
1255 /* Dependent using declarations are a 'field', make sure we
1256 return that even if we saw an overload already. */
1257 if (tree field_val = fields_linear_search (klass, lookup,
1258 type_or_fns > 0))
1259 if (!val || TREE_CODE (field_val) == USING_DECL)
1260 val = field_val;
1263 /* Extract the conversion operators asked for, unless the general
1264 conversion operator was requested. */
1265 if (val && conv_op)
1267 gcc_checking_assert (OVL_FUNCTION (val) == conv_op_marker);
1268 val = OVL_CHAIN (val);
1269 if (tree type = TREE_TYPE (name))
1270 val = extract_conversion_operator (val, type);
1273 return val;
1276 /* Look for NAME's binding in exactly KLASS. See
1277 get_class_binding_direct for argument description. Does lazy
1278 special function creation as necessary. */
1280 tree
1281 get_class_binding (tree klass, tree name, int type_or_fns)
1283 klass = complete_type (klass);
1285 if (COMPLETE_TYPE_P (klass))
1287 /* Lazily declare functions, if we're going to search these. */
1288 if (IDENTIFIER_CTOR_P (name))
1290 if (CLASSTYPE_LAZY_DEFAULT_CTOR (klass))
1291 lazily_declare_fn (sfk_constructor, klass);
1292 if (CLASSTYPE_LAZY_COPY_CTOR (klass))
1293 lazily_declare_fn (sfk_copy_constructor, klass);
1294 if (CLASSTYPE_LAZY_MOVE_CTOR (klass))
1295 lazily_declare_fn (sfk_move_constructor, klass);
1297 else if (IDENTIFIER_DTOR_P (name))
1299 if (CLASSTYPE_LAZY_DESTRUCTOR (klass))
1300 lazily_declare_fn (sfk_destructor, klass);
1302 else if (name == assign_op_identifier)
1304 if (CLASSTYPE_LAZY_COPY_ASSIGN (klass))
1305 lazily_declare_fn (sfk_copy_assignment, klass);
1306 if (CLASSTYPE_LAZY_MOVE_ASSIGN (klass))
1307 lazily_declare_fn (sfk_move_assignment, klass);
1311 return get_class_binding_direct (klass, name, type_or_fns);
1314 /* Find the slot containing overloads called 'NAME'. If there is no
1315 such slot, create an empty one. KLASS might be complete at this
1316 point, in which case we need to preserve ordering. Deals with
1317 conv_op marker handling. */
1319 tree *
1320 get_member_slot (tree klass, tree name)
1322 bool complete_p = COMPLETE_TYPE_P (klass);
1324 vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (klass);
1325 if (!member_vec)
1327 vec_alloc (member_vec, 8);
1328 CLASSTYPE_MEMBER_VEC (klass) = member_vec;
1329 if (complete_p)
1331 /* If the class is complete but had no member_vec, we need
1332 to add the TYPE_FIELDS into it. We're also most likely
1333 to be adding ctors & dtors, so ask for 6 spare slots (the
1334 abstract cdtors and their clones). */
1335 set_class_bindings (klass, 6);
1336 member_vec = CLASSTYPE_MEMBER_VEC (klass);
1340 if (IDENTIFIER_CONV_OP_P (name))
1341 name = conv_op_identifier;
1343 unsigned ix, length = member_vec->length ();
1344 for (ix = 0; ix < length; ix++)
1346 tree *slot = &(*member_vec)[ix];
1347 tree fn_name = OVL_NAME (*slot);
1349 if (fn_name == name)
1351 /* If we found an existing slot, it must be a function set.
1352 Even with insertion after completion, because those only
1353 happen with artificial fns that have unspellable names.
1354 This means we do not have to deal with the stat hack
1355 either. */
1356 gcc_checking_assert (OVL_P (*slot));
1357 if (name == conv_op_identifier)
1359 gcc_checking_assert (OVL_FUNCTION (*slot) == conv_op_marker);
1360 /* Skip the conv-op marker. */
1361 slot = &OVL_CHAIN (*slot);
1363 return slot;
1366 if (complete_p && fn_name > name)
1367 break;
1370 /* No slot found. Create one at IX. We know in this case that our
1371 caller will succeed in adding the function. */
1372 if (complete_p)
1374 /* Do exact allocation when complete, as we don't expect to add
1375 many. */
1376 vec_safe_reserve_exact (member_vec, 1);
1377 member_vec->quick_insert (ix, NULL_TREE);
1379 else
1381 gcc_checking_assert (ix == length);
1382 vec_safe_push (member_vec, NULL_TREE);
1384 CLASSTYPE_MEMBER_VEC (klass) = member_vec;
1386 tree *slot = &(*member_vec)[ix];
1387 if (name == conv_op_identifier)
1389 /* Install the marker prefix. */
1390 *slot = ovl_make (conv_op_marker, NULL_TREE);
1391 slot = &OVL_CHAIN (*slot);
1394 return slot;
1397 /* Comparison function to compare two MEMBER_VEC entries by name.
1398 Because we can have duplicates during insertion of TYPE_FIELDS, we
1399 do extra checking so deduping doesn't have to deal with so many
1400 cases. */
1402 static int
1403 member_name_cmp (const void *a_p, const void *b_p)
1405 tree a = *(const tree *)a_p;
1406 tree b = *(const tree *)b_p;
1407 tree name_a = DECL_NAME (TREE_CODE (a) == OVERLOAD ? OVL_FUNCTION (a) : a);
1408 tree name_b = DECL_NAME (TREE_CODE (b) == OVERLOAD ? OVL_FUNCTION (b) : b);
1410 gcc_checking_assert (name_a && name_b);
1411 if (name_a != name_b)
1412 return name_a < name_b ? -1 : +1;
1414 if (name_a == conv_op_identifier)
1416 /* Strip the conv-op markers. */
1417 gcc_checking_assert (OVL_FUNCTION (a) == conv_op_marker
1418 && OVL_FUNCTION (b) == conv_op_marker);
1419 a = OVL_CHAIN (a);
1420 b = OVL_CHAIN (b);
1423 if (TREE_CODE (a) == OVERLOAD)
1424 a = OVL_FUNCTION (a);
1425 if (TREE_CODE (b) == OVERLOAD)
1426 b = OVL_FUNCTION (b);
1428 /* We're in STAT_HACK or USING_DECL territory (or possibly error-land). */
1429 if (TREE_CODE (a) != TREE_CODE (b))
1431 /* If one of them is a TYPE_DECL, it loses. */
1432 if (TREE_CODE (a) == TYPE_DECL)
1433 return +1;
1434 else if (TREE_CODE (b) == TYPE_DECL)
1435 return -1;
1437 /* If one of them is a USING_DECL, it loses. */
1438 if (TREE_CODE (a) == USING_DECL)
1439 return +1;
1440 else if (TREE_CODE (b) == USING_DECL)
1441 return -1;
1443 /* There are no other cases with different kinds of decls, as
1444 duplicate detection should have kicked in earlier. However,
1445 some erroneous cases get though. */
1446 gcc_assert (errorcount);
1449 /* Using source location would be the best thing here, but we can
1450 get identically-located decls in the following circumstances:
1452 1) duplicate artificial type-decls for the same type.
1454 2) pack expansions of using-decls.
1456 We should not be doing #1, but in either case it doesn't matter
1457 how we order these. Use UID as a proxy for source ordering, so
1458 that identically-located decls still have a well-defined stable
1459 ordering. */
1460 return DECL_UID (a) < DECL_UID (b) ? -1 : +1;
1463 static struct {
1464 gt_pointer_operator new_value;
1465 void *cookie;
1466 } resort_data;
1468 /* This routine compares two fields like member_name_cmp but using the
1469 pointer operator in resort_field_decl_data. We don't have to deal
1470 with duplicates here. */
1472 static int
1473 resort_member_name_cmp (const void *a_p, const void *b_p)
1475 tree a = *(const tree *)a_p;
1476 tree b = *(const tree *)b_p;
1477 tree name_a = OVL_NAME (a);
1478 tree name_b = OVL_NAME (b);
1480 resort_data.new_value (&name_a, resort_data.cookie);
1481 resort_data.new_value (&name_b, resort_data.cookie);
1483 gcc_checking_assert (name_a != name_b);
1485 return name_a < name_b ? -1 : +1;
1488 /* Resort CLASSTYPE_MEMBER_VEC because pointers have been reordered. */
1490 void
1491 resort_type_member_vec (void *obj, void */*orig_obj*/,
1492 gt_pointer_operator new_value, void* cookie)
1494 if (vec<tree, va_gc> *member_vec = (vec<tree, va_gc> *) obj)
1496 resort_data.new_value = new_value;
1497 resort_data.cookie = cookie;
1498 qsort (member_vec->address (), member_vec->length (),
1499 sizeof (tree), resort_member_name_cmp);
1503 /* Recursively count the number of fields in KLASS, including anonymous
1504 union members. */
1506 static unsigned
1507 count_class_fields (tree klass)
1509 unsigned n_fields = 0;
1511 for (tree fields = TYPE_FIELDS (klass); fields; fields = DECL_CHAIN (fields))
1512 if (DECL_DECLARES_FUNCTION_P (fields))
1513 /* Functions are dealt with separately. */;
1514 else if (TREE_CODE (fields) == FIELD_DECL
1515 && ANON_AGGR_TYPE_P (TREE_TYPE (fields)))
1516 n_fields += count_class_fields (TREE_TYPE (fields));
1517 else if (DECL_NAME (fields))
1518 n_fields += 1;
1520 return n_fields;
1523 /* Append all the nonfunction members fields of KLASS to MEMBER_VEC.
1524 Recurse for anonymous members. MEMBER_VEC must have space. */
1526 static void
1527 member_vec_append_class_fields (vec<tree, va_gc> *member_vec, tree klass)
1529 for (tree fields = TYPE_FIELDS (klass); fields; fields = DECL_CHAIN (fields))
1530 if (DECL_DECLARES_FUNCTION_P (fields))
1531 /* Functions are handled separately. */;
1532 else if (TREE_CODE (fields) == FIELD_DECL
1533 && ANON_AGGR_TYPE_P (TREE_TYPE (fields)))
1534 member_vec_append_class_fields (member_vec, TREE_TYPE (fields));
1535 else if (DECL_NAME (fields))
1537 tree field = fields;
1538 /* Mark a conv-op USING_DECL with the conv-op-marker. */
1539 if (TREE_CODE (field) == USING_DECL
1540 && IDENTIFIER_CONV_OP_P (DECL_NAME (field)))
1541 field = ovl_make (conv_op_marker, field);
1542 member_vec->quick_push (field);
1546 /* Append all of the enum values of ENUMTYPE to MEMBER_VEC.
1547 MEMBER_VEC must have space. */
1549 static void
1550 member_vec_append_enum_values (vec<tree, va_gc> *member_vec, tree enumtype)
1552 for (tree values = TYPE_VALUES (enumtype);
1553 values; values = TREE_CHAIN (values))
1554 member_vec->quick_push (TREE_VALUE (values));
1557 /* MEMBER_VEC has just had new DECLs added to it, but is sorted.
1558 DeDup adjacent DECLS of the same name. We already dealt with
1559 conflict resolution when adding the fields or methods themselves.
1560 There are three cases (which could all be combined):
1561 1) a TYPE_DECL and non TYPE_DECL. Deploy STAT_HACK as appropriate.
1562 2) a USING_DECL and an overload. If the USING_DECL is dependent,
1563 it wins. Otherwise the OVERLOAD does.
1564 3) two USING_DECLS. ...
1566 member_name_cmp will have ordered duplicates as
1567 <fns><using><type> */
1569 static void
1570 member_vec_dedup (vec<tree, va_gc> *member_vec)
1572 unsigned len = member_vec->length ();
1573 unsigned store = 0;
1575 tree current = (*member_vec)[0], name = OVL_NAME (current);
1576 tree next = NULL_TREE, next_name = NULL_TREE;
1577 for (unsigned jx, ix = 0; ix < len;
1578 ix = jx, current = next, name = next_name)
1580 tree to_type = NULL_TREE;
1581 tree to_using = NULL_TREE;
1582 tree marker = NULL_TREE;
1583 if (IDENTIFIER_CONV_OP_P (name))
1585 marker = current;
1586 current = OVL_CHAIN (current);
1587 name = DECL_NAME (OVL_FUNCTION (marker));
1588 gcc_checking_assert (name == conv_op_identifier);
1591 if (TREE_CODE (current) == USING_DECL)
1593 current = strip_using_decl (current);
1594 if (is_overloaded_fn (current))
1595 current = NULL_TREE;
1596 else if (TREE_CODE (current) == USING_DECL)
1598 to_using = current;
1599 current = NULL_TREE;
1603 if (current && DECL_DECLARES_TYPE_P (current))
1605 to_type = current;
1606 current = NULL_TREE;
1609 for (jx = ix + 1; jx < len; jx++)
1611 next = (*member_vec)[jx];
1612 next_name = OVL_NAME (next);
1613 if (next_name != name)
1614 break;
1616 if (marker)
1618 gcc_checking_assert (OVL_FUNCTION (marker)
1619 == OVL_FUNCTION (next));
1620 next = OVL_CHAIN (next);
1623 if (TREE_CODE (next) == USING_DECL)
1625 next = strip_using_decl (next);
1626 if (is_overloaded_fn (next))
1627 next = NULL_TREE;
1628 else if (TREE_CODE (next) == USING_DECL)
1630 to_using = next;
1631 next = NULL_TREE;
1635 if (next && DECL_DECLARES_TYPE_P (next))
1636 to_type = next;
1639 if (to_using)
1641 if (!current)
1642 current = to_using;
1643 else
1644 current = ovl_make (to_using, current);
1647 if (to_type)
1649 if (!current)
1650 current = to_type;
1651 else
1652 current = stat_hack (current, to_type);
1655 gcc_assert (current);
1656 if (marker)
1658 OVL_CHAIN (marker) = current;
1659 current = marker;
1661 (*member_vec)[store++] = current;
1664 while (store++ < len)
1665 member_vec->pop ();
1668 /* Add the non-function members to CLASSTYPE_MEMBER_VEC. If there is
1669 no existing MEMBER_VEC and fewer than 8 fields, do nothing. We
1670 know there must be at least 1 field -- the self-reference
1671 TYPE_DECL, except for anon aggregates, which will have at least
1672 one field. */
1674 void
1675 set_class_bindings (tree klass, unsigned extra)
1677 unsigned n_fields = count_class_fields (klass);
1678 vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (klass);
1680 if (member_vec || n_fields >= 8)
1682 /* Append the new fields. */
1683 vec_safe_reserve_exact (member_vec, extra + n_fields);
1684 member_vec_append_class_fields (member_vec, klass);
1687 if (member_vec)
1689 CLASSTYPE_MEMBER_VEC (klass) = member_vec;
1690 qsort (member_vec->address (), member_vec->length (),
1691 sizeof (tree), member_name_cmp);
1692 member_vec_dedup (member_vec);
1696 /* Insert lately defined enum ENUMTYPE into KLASS for the sorted case. */
1698 void
1699 insert_late_enum_def_bindings (tree klass, tree enumtype)
1701 int n_fields;
1702 vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (klass);
1704 /* The enum bindings will already be on the TYPE_FIELDS, so don't
1705 count them twice. */
1706 if (!member_vec)
1707 n_fields = count_class_fields (klass);
1708 else
1709 n_fields = list_length (TYPE_VALUES (enumtype));
1711 if (member_vec || n_fields >= 8)
1713 vec_safe_reserve_exact (member_vec, n_fields);
1714 if (CLASSTYPE_MEMBER_VEC (klass))
1715 member_vec_append_enum_values (member_vec, enumtype);
1716 else
1717 member_vec_append_class_fields (member_vec, klass);
1718 CLASSTYPE_MEMBER_VEC (klass) = member_vec;
1719 qsort (member_vec->address (), member_vec->length (),
1720 sizeof (tree), member_name_cmp);
1721 member_vec_dedup (member_vec);
1725 /* Compute the chain index of a binding_entry given the HASH value of its
1726 name and the total COUNT of chains. COUNT is assumed to be a power
1727 of 2. */
1729 #define ENTRY_INDEX(HASH, COUNT) (((HASH) >> 3) & ((COUNT) - 1))
1731 /* A free list of "binding_entry"s awaiting for re-use. */
1733 static GTY((deletable)) binding_entry free_binding_entry = NULL;
1735 /* The binding oracle; see cp-tree.h. */
1737 cp_binding_oracle_function *cp_binding_oracle;
1739 /* If we have a binding oracle, ask it for all namespace-scoped
1740 definitions of NAME. */
1742 static inline void
1743 query_oracle (tree name)
1745 if (!cp_binding_oracle)
1746 return;
1748 /* LOOKED_UP holds the set of identifiers that we have already
1749 looked up with the oracle. */
1750 static hash_set<tree> looked_up;
1751 if (looked_up.add (name))
1752 return;
1754 cp_binding_oracle (CP_ORACLE_IDENTIFIER, name);
1757 /* Create a binding_entry object for (NAME, TYPE). */
1759 static inline binding_entry
1760 binding_entry_make (tree name, tree type)
1762 binding_entry entry;
1764 if (free_binding_entry)
1766 entry = free_binding_entry;
1767 free_binding_entry = entry->chain;
1769 else
1770 entry = ggc_alloc<binding_entry_s> ();
1772 entry->name = name;
1773 entry->type = type;
1774 entry->chain = NULL;
1776 return entry;
1779 /* Put ENTRY back on the free list. */
1780 #if 0
1781 static inline void
1782 binding_entry_free (binding_entry entry)
1784 entry->name = NULL;
1785 entry->type = NULL;
1786 entry->chain = free_binding_entry;
1787 free_binding_entry = entry;
1789 #endif
1791 /* The datatype used to implement the mapping from names to types at
1792 a given scope. */
1793 struct GTY(()) binding_table_s {
1794 /* Array of chains of "binding_entry"s */
1795 binding_entry * GTY((length ("%h.chain_count"))) chain;
1797 /* The number of chains in this table. This is the length of the
1798 member "chain" considered as an array. */
1799 size_t chain_count;
1801 /* Number of "binding_entry"s in this table. */
1802 size_t entry_count;
1805 /* Construct TABLE with an initial CHAIN_COUNT. */
1807 static inline void
1808 binding_table_construct (binding_table table, size_t chain_count)
1810 table->chain_count = chain_count;
1811 table->entry_count = 0;
1812 table->chain = ggc_cleared_vec_alloc<binding_entry> (table->chain_count);
1815 /* Make TABLE's entries ready for reuse. */
1816 #if 0
1817 static void
1818 binding_table_free (binding_table table)
1820 size_t i;
1821 size_t count;
1823 if (table == NULL)
1824 return;
1826 for (i = 0, count = table->chain_count; i < count; ++i)
1828 binding_entry temp = table->chain[i];
1829 while (temp != NULL)
1831 binding_entry entry = temp;
1832 temp = entry->chain;
1833 binding_entry_free (entry);
1835 table->chain[i] = NULL;
1837 table->entry_count = 0;
1839 #endif
1841 /* Allocate a table with CHAIN_COUNT, assumed to be a power of two. */
1843 static inline binding_table
1844 binding_table_new (size_t chain_count)
1846 binding_table table = ggc_alloc<binding_table_s> ();
1847 table->chain = NULL;
1848 binding_table_construct (table, chain_count);
1849 return table;
1852 /* Expand TABLE to twice its current chain_count. */
1854 static void
1855 binding_table_expand (binding_table table)
1857 const size_t old_chain_count = table->chain_count;
1858 const size_t old_entry_count = table->entry_count;
1859 const size_t new_chain_count = 2 * old_chain_count;
1860 binding_entry *old_chains = table->chain;
1861 size_t i;
1863 binding_table_construct (table, new_chain_count);
1864 for (i = 0; i < old_chain_count; ++i)
1866 binding_entry entry = old_chains[i];
1867 for (; entry != NULL; entry = old_chains[i])
1869 const unsigned int hash = IDENTIFIER_HASH_VALUE (entry->name);
1870 const size_t j = ENTRY_INDEX (hash, new_chain_count);
1872 old_chains[i] = entry->chain;
1873 entry->chain = table->chain[j];
1874 table->chain[j] = entry;
1877 table->entry_count = old_entry_count;
1880 /* Insert a binding for NAME to TYPE into TABLE. */
1882 static void
1883 binding_table_insert (binding_table table, tree name, tree type)
1885 const unsigned int hash = IDENTIFIER_HASH_VALUE (name);
1886 const size_t i = ENTRY_INDEX (hash, table->chain_count);
1887 binding_entry entry = binding_entry_make (name, type);
1889 entry->chain = table->chain[i];
1890 table->chain[i] = entry;
1891 ++table->entry_count;
1893 if (3 * table->chain_count < 5 * table->entry_count)
1894 binding_table_expand (table);
1897 /* Return the binding_entry, if any, that maps NAME. */
1899 binding_entry
1900 binding_table_find (binding_table table, tree name)
1902 const unsigned int hash = IDENTIFIER_HASH_VALUE (name);
1903 binding_entry entry = table->chain[ENTRY_INDEX (hash, table->chain_count)];
1905 while (entry != NULL && entry->name != name)
1906 entry = entry->chain;
1908 return entry;
1911 /* Apply PROC -- with DATA -- to all entries in TABLE. */
1913 void
1914 binding_table_foreach (binding_table table, bt_foreach_proc proc, void *data)
1916 size_t chain_count;
1917 size_t i;
1919 if (!table)
1920 return;
1922 chain_count = table->chain_count;
1923 for (i = 0; i < chain_count; ++i)
1925 binding_entry entry = table->chain[i];
1926 for (; entry != NULL; entry = entry->chain)
1927 proc (entry, data);
1931 #ifndef ENABLE_SCOPE_CHECKING
1932 # define ENABLE_SCOPE_CHECKING 0
1933 #else
1934 # define ENABLE_SCOPE_CHECKING 1
1935 #endif
1937 /* A free list of "cxx_binding"s, connected by their PREVIOUS. */
1939 static GTY((deletable)) cxx_binding *free_bindings;
1941 /* Initialize VALUE and TYPE field for BINDING, and set the PREVIOUS
1942 field to NULL. */
1944 static inline void
1945 cxx_binding_init (cxx_binding *binding, tree value, tree type)
1947 binding->value = value;
1948 binding->type = type;
1949 binding->previous = NULL;
1952 /* (GC)-allocate a binding object with VALUE and TYPE member initialized. */
1954 static cxx_binding *
1955 cxx_binding_make (tree value, tree type)
1957 cxx_binding *binding;
1958 if (free_bindings)
1960 binding = free_bindings;
1961 free_bindings = binding->previous;
1963 else
1964 binding = ggc_alloc<cxx_binding> ();
1966 cxx_binding_init (binding, value, type);
1968 return binding;
1971 /* Put BINDING back on the free list. */
1973 static inline void
1974 cxx_binding_free (cxx_binding *binding)
1976 binding->scope = NULL;
1977 binding->previous = free_bindings;
1978 free_bindings = binding;
1981 /* Create a new binding for NAME (with the indicated VALUE and TYPE
1982 bindings) in the class scope indicated by SCOPE. */
1984 static cxx_binding *
1985 new_class_binding (tree name, tree value, tree type, cp_binding_level *scope)
1987 cp_class_binding cb = {cxx_binding_make (value, type), name};
1988 cxx_binding *binding = cb.base;
1989 vec_safe_push (scope->class_shadowed, cb);
1990 binding->scope = scope;
1991 return binding;
1994 /* Make DECL the innermost binding for ID. The LEVEL is the binding
1995 level at which this declaration is being bound. */
1997 void
1998 push_binding (tree id, tree decl, cp_binding_level* level)
2000 cxx_binding *binding;
2002 if (level != class_binding_level)
2004 binding = cxx_binding_make (decl, NULL_TREE);
2005 binding->scope = level;
2007 else
2008 binding = new_class_binding (id, decl, /*type=*/NULL_TREE, level);
2010 /* Now, fill in the binding information. */
2011 binding->previous = IDENTIFIER_BINDING (id);
2012 INHERITED_VALUE_BINDING_P (binding) = 0;
2013 LOCAL_BINDING_P (binding) = (level != class_binding_level);
2015 /* And put it on the front of the list of bindings for ID. */
2016 IDENTIFIER_BINDING (id) = binding;
2019 /* Remove the binding for DECL which should be the innermost binding
2020 for ID. */
2022 void
2023 pop_local_binding (tree id, tree decl)
2025 cxx_binding *binding;
2027 if (id == NULL_TREE)
2028 /* It's easiest to write the loops that call this function without
2029 checking whether or not the entities involved have names. We
2030 get here for such an entity. */
2031 return;
2033 /* Get the innermost binding for ID. */
2034 binding = IDENTIFIER_BINDING (id);
2036 /* The name should be bound. */
2037 gcc_assert (binding != NULL);
2039 /* The DECL will be either the ordinary binding or the type
2040 binding for this identifier. Remove that binding. */
2041 if (binding->value == decl)
2042 binding->value = NULL_TREE;
2043 else
2045 gcc_assert (binding->type == decl);
2046 binding->type = NULL_TREE;
2049 if (!binding->value && !binding->type)
2051 /* We're completely done with the innermost binding for this
2052 identifier. Unhook it from the list of bindings. */
2053 IDENTIFIER_BINDING (id) = binding->previous;
2055 /* Add it to the free list. */
2056 cxx_binding_free (binding);
2060 /* Remove the bindings for the decls of the current level and leave
2061 the current scope. */
2063 void
2064 pop_bindings_and_leave_scope (void)
2066 for (tree t = get_local_decls (); t; t = DECL_CHAIN (t))
2068 tree decl = TREE_CODE (t) == TREE_LIST ? TREE_VALUE (t) : t;
2069 tree name = OVL_NAME (decl);
2071 pop_local_binding (name, decl);
2074 leave_scope ();
2077 /* Strip non dependent using declarations. If DECL is dependent,
2078 surreptitiously create a typename_type and return it. */
2080 tree
2081 strip_using_decl (tree decl)
2083 if (decl == NULL_TREE)
2084 return NULL_TREE;
2086 while (TREE_CODE (decl) == USING_DECL && !DECL_DEPENDENT_P (decl))
2087 decl = USING_DECL_DECLS (decl);
2089 if (TREE_CODE (decl) == USING_DECL && DECL_DEPENDENT_P (decl)
2090 && USING_DECL_TYPENAME_P (decl))
2092 /* We have found a type introduced by a using
2093 declaration at class scope that refers to a dependent
2094 type.
2096 using typename :: [opt] nested-name-specifier unqualified-id ;
2098 decl = make_typename_type (TREE_TYPE (decl),
2099 DECL_NAME (decl),
2100 typename_type, tf_error);
2101 if (decl != error_mark_node)
2102 decl = TYPE_NAME (decl);
2105 return decl;
2108 /* Return true if OVL is an overload for an anticipated builtin. */
2110 static bool
2111 anticipated_builtin_p (tree ovl)
2113 if (TREE_CODE (ovl) != OVERLOAD)
2114 return false;
2116 if (!OVL_HIDDEN_P (ovl))
2117 return false;
2119 tree fn = OVL_FUNCTION (ovl);
2120 gcc_checking_assert (DECL_ANTICIPATED (fn));
2122 if (DECL_HIDDEN_FRIEND_P (fn))
2123 return false;
2125 return true;
2128 /* BINDING records an existing declaration for a name in the current scope.
2129 But, DECL is another declaration for that same identifier in the
2130 same scope. This is the `struct stat' hack whereby a non-typedef
2131 class name or enum-name can be bound at the same level as some other
2132 kind of entity.
2133 3.3.7/1
2135 A class name (9.1) or enumeration name (7.2) can be hidden by the
2136 name of an object, function, or enumerator declared in the same scope.
2137 If a class or enumeration name and an object, function, or enumerator
2138 are declared in the same scope (in any order) with the same name, the
2139 class or enumeration name is hidden wherever the object, function, or
2140 enumerator name is visible.
2142 It's the responsibility of the caller to check that
2143 inserting this name is valid here. Returns nonzero if the new binding
2144 was successful. */
2146 static bool
2147 supplement_binding_1 (cxx_binding *binding, tree decl)
2149 tree bval = binding->value;
2150 bool ok = true;
2151 tree target_bval = strip_using_decl (bval);
2152 tree target_decl = strip_using_decl (decl);
2154 if (TREE_CODE (target_decl) == TYPE_DECL && DECL_ARTIFICIAL (target_decl)
2155 && target_decl != target_bval
2156 && (TREE_CODE (target_bval) != TYPE_DECL
2157 /* We allow pushing an enum multiple times in a class
2158 template in order to handle late matching of underlying
2159 type on an opaque-enum-declaration followed by an
2160 enum-specifier. */
2161 || (processing_template_decl
2162 && TREE_CODE (TREE_TYPE (target_decl)) == ENUMERAL_TYPE
2163 && TREE_CODE (TREE_TYPE (target_bval)) == ENUMERAL_TYPE
2164 && (dependent_type_p (ENUM_UNDERLYING_TYPE
2165 (TREE_TYPE (target_decl)))
2166 || dependent_type_p (ENUM_UNDERLYING_TYPE
2167 (TREE_TYPE (target_bval)))))))
2168 /* The new name is the type name. */
2169 binding->type = decl;
2170 else if (/* TARGET_BVAL is null when push_class_level_binding moves
2171 an inherited type-binding out of the way to make room
2172 for a new value binding. */
2173 !target_bval
2174 /* TARGET_BVAL is error_mark_node when TARGET_DECL's name
2175 has been used in a non-class scope prior declaration.
2176 In that case, we should have already issued a
2177 diagnostic; for graceful error recovery purpose, pretend
2178 this was the intended declaration for that name. */
2179 || target_bval == error_mark_node
2180 /* If TARGET_BVAL is anticipated but has not yet been
2181 declared, pretend it is not there at all. */
2182 || anticipated_builtin_p (target_bval))
2183 binding->value = decl;
2184 else if (TREE_CODE (target_bval) == TYPE_DECL
2185 && DECL_ARTIFICIAL (target_bval)
2186 && target_decl != target_bval
2187 && (TREE_CODE (target_decl) != TYPE_DECL
2188 || same_type_p (TREE_TYPE (target_decl),
2189 TREE_TYPE (target_bval))))
2191 /* The old binding was a type name. It was placed in
2192 VALUE field because it was thought, at the point it was
2193 declared, to be the only entity with such a name. Move the
2194 type name into the type slot; it is now hidden by the new
2195 binding. */
2196 binding->type = bval;
2197 binding->value = decl;
2198 binding->value_is_inherited = false;
2200 else if (TREE_CODE (target_bval) == TYPE_DECL
2201 && TREE_CODE (target_decl) == TYPE_DECL
2202 && DECL_NAME (target_decl) == DECL_NAME (target_bval)
2203 && binding->scope->kind != sk_class
2204 && (same_type_p (TREE_TYPE (target_decl), TREE_TYPE (target_bval))
2205 /* If either type involves template parameters, we must
2206 wait until instantiation. */
2207 || uses_template_parms (TREE_TYPE (target_decl))
2208 || uses_template_parms (TREE_TYPE (target_bval))))
2209 /* We have two typedef-names, both naming the same type to have
2210 the same name. In general, this is OK because of:
2212 [dcl.typedef]
2214 In a given scope, a typedef specifier can be used to redefine
2215 the name of any type declared in that scope to refer to the
2216 type to which it already refers.
2218 However, in class scopes, this rule does not apply due to the
2219 stricter language in [class.mem] prohibiting redeclarations of
2220 members. */
2221 ok = false;
2222 /* There can be two block-scope declarations of the same variable,
2223 so long as they are `extern' declarations. However, there cannot
2224 be two declarations of the same static data member:
2226 [class.mem]
2228 A member shall not be declared twice in the
2229 member-specification. */
2230 else if (VAR_P (target_decl)
2231 && VAR_P (target_bval)
2232 && DECL_EXTERNAL (target_decl) && DECL_EXTERNAL (target_bval)
2233 && !DECL_CLASS_SCOPE_P (target_decl))
2235 duplicate_decls (decl, binding->value, /*newdecl_is_friend=*/false);
2236 ok = false;
2238 else if (TREE_CODE (decl) == NAMESPACE_DECL
2239 && TREE_CODE (bval) == NAMESPACE_DECL
2240 && DECL_NAMESPACE_ALIAS (decl)
2241 && DECL_NAMESPACE_ALIAS (bval)
2242 && ORIGINAL_NAMESPACE (bval) == ORIGINAL_NAMESPACE (decl))
2243 /* [namespace.alias]
2245 In a declarative region, a namespace-alias-definition can be
2246 used to redefine a namespace-alias declared in that declarative
2247 region to refer only to the namespace to which it already
2248 refers. */
2249 ok = false;
2250 else
2252 if (!error_operand_p (bval))
2253 diagnose_name_conflict (decl, bval);
2254 ok = false;
2257 return ok;
2260 /* Diagnose a name conflict between DECL and BVAL. */
2262 static void
2263 diagnose_name_conflict (tree decl, tree bval)
2265 if (TREE_CODE (decl) == TREE_CODE (bval)
2266 && TREE_CODE (decl) != NAMESPACE_DECL
2267 && !DECL_DECLARES_FUNCTION_P (decl)
2268 && (TREE_CODE (decl) != TYPE_DECL
2269 || DECL_ARTIFICIAL (decl) == DECL_ARTIFICIAL (bval))
2270 && CP_DECL_CONTEXT (decl) == CP_DECL_CONTEXT (bval))
2271 error ("redeclaration of %q#D", decl);
2272 else
2273 error ("%q#D conflicts with a previous declaration", decl);
2275 inform (location_of (bval), "previous declaration %q#D", bval);
2278 /* Wrapper for supplement_binding_1. */
2280 static bool
2281 supplement_binding (cxx_binding *binding, tree decl)
2283 bool ret;
2284 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
2285 ret = supplement_binding_1 (binding, decl);
2286 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
2287 return ret;
2290 /* Replace BINDING's current value on its scope's name list with
2291 NEWVAL. */
2293 static void
2294 update_local_overload (cxx_binding *binding, tree newval)
2296 tree *d;
2298 for (d = &binding->scope->names; ; d = &TREE_CHAIN (*d))
2299 if (*d == binding->value)
2301 /* Stitch new list node in. */
2302 *d = tree_cons (NULL_TREE, NULL_TREE, TREE_CHAIN (*d));
2303 break;
2305 else if (TREE_CODE (*d) == TREE_LIST && TREE_VALUE (*d) == binding->value)
2306 break;
2308 TREE_VALUE (*d) = newval;
2311 /* Compares the parameter-type-lists of ONE and TWO and
2312 returns false if they are different. If the DECLs are template
2313 functions, the return types and the template parameter lists are
2314 compared too (DR 565). */
2316 static bool
2317 matching_fn_p (tree one, tree two)
2319 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (one)),
2320 TYPE_ARG_TYPES (TREE_TYPE (two))))
2321 return false;
2323 if (TREE_CODE (one) == TEMPLATE_DECL
2324 && TREE_CODE (two) == TEMPLATE_DECL)
2326 /* Compare template parms. */
2327 if (!comp_template_parms (DECL_TEMPLATE_PARMS (one),
2328 DECL_TEMPLATE_PARMS (two)))
2329 return false;
2331 /* And return type. */
2332 if (!same_type_p (TREE_TYPE (TREE_TYPE (one)),
2333 TREE_TYPE (TREE_TYPE (two))))
2334 return false;
2337 return true;
2340 /* Push DECL into nonclass LEVEL BINDING or SLOT. OLD is the current
2341 binding value (possibly with anticipated builtins stripped).
2342 Diagnose conflicts and return updated decl. */
2344 static tree
2345 update_binding (cp_binding_level *level, cxx_binding *binding, tree *slot,
2346 tree old, tree decl, bool is_friend)
2348 tree to_val = decl;
2349 tree old_type = slot ? MAYBE_STAT_TYPE (*slot) : binding->type;
2350 tree to_type = old_type;
2352 gcc_assert (level->kind == sk_namespace ? !binding
2353 : level->kind != sk_class && !slot);
2354 if (old == error_mark_node)
2355 old = NULL_TREE;
2357 if (TREE_CODE (decl) == TYPE_DECL && DECL_ARTIFICIAL (decl))
2359 tree other = to_type;
2361 if (old && TREE_CODE (old) == TYPE_DECL && DECL_ARTIFICIAL (old))
2362 other = old;
2364 /* Pushing an artificial typedef. See if this matches either
2365 the type slot or the old value slot. */
2366 if (!other)
2368 else if (same_type_p (TREE_TYPE (other), TREE_TYPE (decl)))
2369 /* Two artificial decls to same type. Do nothing. */
2370 return other;
2371 else
2372 goto conflict;
2374 if (old)
2376 /* Slide decl into the type slot, keep old unaltered */
2377 to_type = decl;
2378 to_val = old;
2379 goto done;
2383 if (old && TREE_CODE (old) == TYPE_DECL && DECL_ARTIFICIAL (old))
2385 /* Slide old into the type slot. */
2386 to_type = old;
2387 old = NULL_TREE;
2390 if (DECL_DECLARES_FUNCTION_P (decl))
2392 if (!old)
2394 else if (OVL_P (old))
2396 for (ovl_iterator iter (old); iter; ++iter)
2398 tree fn = *iter;
2400 if (iter.using_p () && matching_fn_p (fn, decl))
2402 /* If a function declaration in namespace scope or
2403 block scope has the same name and the same
2404 parameter-type- list (8.3.5) as a function
2405 introduced by a using-declaration, and the
2406 declarations do not declare the same function,
2407 the program is ill-formed. [namespace.udecl]/14 */
2408 if (tree match = duplicate_decls (decl, fn, is_friend))
2409 return match;
2410 else
2411 /* FIXME: To preserve existing error behavior, we
2412 still push the decl. This might change. */
2413 diagnose_name_conflict (decl, fn);
2417 else
2418 goto conflict;
2420 if (to_type != old_type
2421 && warn_shadow
2422 && MAYBE_CLASS_TYPE_P (TREE_TYPE (to_type))
2423 && !(DECL_IN_SYSTEM_HEADER (decl)
2424 && DECL_IN_SYSTEM_HEADER (to_type)))
2425 warning (OPT_Wshadow, "%q#D hides constructor for %q#D",
2426 decl, to_type);
2428 to_val = ovl_insert (decl, old);
2430 else if (!old)
2432 else if (TREE_CODE (old) != TREE_CODE (decl))
2433 /* Different kinds of decls conflict. */
2434 goto conflict;
2435 else if (TREE_CODE (old) == TYPE_DECL)
2437 if (same_type_p (TREE_TYPE (old), TREE_TYPE (decl)))
2438 /* Two type decls to the same type. Do nothing. */
2439 return old;
2440 else
2441 goto conflict;
2443 else if (TREE_CODE (old) == NAMESPACE_DECL)
2445 /* Two maybe-aliased namespaces. If they're to the same target
2446 namespace, that's ok. */
2447 if (ORIGINAL_NAMESPACE (old) != ORIGINAL_NAMESPACE (decl))
2448 goto conflict;
2450 /* The new one must be an alias at this point. */
2451 gcc_assert (DECL_NAMESPACE_ALIAS (decl));
2452 return old;
2454 else if (TREE_CODE (old) == VAR_DECL)
2456 /* There can be two block-scope declarations of the same
2457 variable, so long as they are `extern' declarations. */
2458 if (!DECL_EXTERNAL (old) || !DECL_EXTERNAL (decl))
2459 goto conflict;
2460 else if (tree match = duplicate_decls (decl, old, false))
2461 return match;
2462 else
2463 goto conflict;
2465 else
2467 conflict:
2468 diagnose_name_conflict (decl, old);
2469 to_val = NULL_TREE;
2472 done:
2473 if (to_val)
2475 if (level->kind != sk_namespace
2476 && !to_type && binding->value && OVL_P (to_val))
2477 update_local_overload (binding, to_val);
2478 else
2480 tree to_add = to_val;
2482 if (level->kind == sk_namespace)
2483 to_add = decl;
2484 else if (to_type == decl)
2485 to_add = decl;
2486 else if (TREE_CODE (to_add) == OVERLOAD)
2487 to_add = build_tree_list (NULL_TREE, to_add);
2489 add_decl_to_level (level, to_add);
2492 if (slot)
2494 if (STAT_HACK_P (*slot))
2496 STAT_TYPE (*slot) = to_type;
2497 STAT_DECL (*slot) = to_val;
2499 else if (to_type)
2500 *slot = stat_hack (to_val, to_type);
2501 else
2502 *slot = to_val;
2504 else
2506 binding->type = to_type;
2507 binding->value = to_val;
2511 return decl;
2514 /* Table of identifiers to extern C declarations (or LISTS thereof). */
2516 static GTY(()) hash_table<named_decl_hash> *extern_c_decls;
2518 /* DECL has C linkage. If we have an existing instance, make sure the
2519 new one is compatible. Make sure it has the same exception
2520 specification [7.5, 7.6]. Add DECL to the map. */
2522 static void
2523 check_extern_c_conflict (tree decl)
2525 /* Ignore artificial or system header decls. */
2526 if (DECL_ARTIFICIAL (decl) || DECL_IN_SYSTEM_HEADER (decl))
2527 return;
2529 if (!extern_c_decls)
2530 extern_c_decls = hash_table<named_decl_hash>::create_ggc (127);
2532 tree *slot = extern_c_decls
2533 ->find_slot_with_hash (DECL_NAME (decl),
2534 IDENTIFIER_HASH_VALUE (DECL_NAME (decl)), INSERT);
2535 if (tree old = *slot)
2537 if (TREE_CODE (old) == OVERLOAD)
2538 old = OVL_FUNCTION (old);
2540 int mismatch = 0;
2541 if (DECL_CONTEXT (old) == DECL_CONTEXT (decl))
2542 ; /* If they're in the same context, we'll have already complained
2543 about a (possible) mismatch, when inserting the decl. */
2544 else if (!decls_match (decl, old))
2545 mismatch = 1;
2546 else if (TREE_CODE (decl) == FUNCTION_DECL
2547 && !comp_except_specs (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (old)),
2548 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl)),
2549 ce_normal))
2550 mismatch = -1;
2551 else if (DECL_ASSEMBLER_NAME_SET_P (old))
2552 SET_DECL_ASSEMBLER_NAME (decl, DECL_ASSEMBLER_NAME (old));
2554 if (mismatch)
2556 pedwarn (input_location, 0,
2557 "conflicting C language linkage declaration %q#D", decl);
2558 inform (DECL_SOURCE_LOCATION (old),
2559 "previous declaration %q#D", old);
2560 if (mismatch < 0)
2561 inform (input_location,
2562 "due to different exception specifications");
2564 else
2566 if (old == *slot)
2567 /* The hash table expects OVERLOADS, so construct one with
2568 OLD as both the function and the chain. This allocate
2569 an excess OVERLOAD node, but it's rare to have multiple
2570 extern "C" decls of the same name. And we save
2571 complicating the hash table logic (which is used
2572 elsewhere). */
2573 *slot = ovl_make (old, old);
2575 slot = &OVL_CHAIN (*slot);
2577 /* Chain it on for c_linkage_binding's use. */
2578 *slot = tree_cons (NULL_TREE, decl, *slot);
2581 else
2582 *slot = decl;
2585 /* Returns a list of C-linkage decls with the name NAME. Used in
2586 c-family/c-pragma.c to implement redefine_extname pragma. */
2588 tree
2589 c_linkage_bindings (tree name)
2591 if (extern_c_decls)
2592 if (tree *slot = extern_c_decls
2593 ->find_slot_with_hash (name, IDENTIFIER_HASH_VALUE (name), NO_INSERT))
2595 tree result = *slot;
2596 if (TREE_CODE (result) == OVERLOAD)
2597 result = OVL_CHAIN (result);
2598 return result;
2601 return NULL_TREE;
2604 /* DECL is being declared at a local scope. Emit suitable shadow
2605 warnings. */
2607 static void
2608 check_local_shadow (tree decl)
2610 /* Don't complain about the parms we push and then pop
2611 while tentatively parsing a function declarator. */
2612 if (TREE_CODE (decl) == PARM_DECL && !DECL_CONTEXT (decl))
2613 return;
2615 /* Inline decls shadow nothing. */
2616 if (DECL_FROM_INLINE (decl))
2617 return;
2619 /* External decls are something else. */
2620 if (DECL_EXTERNAL (decl))
2621 return;
2623 tree old = NULL_TREE;
2624 cp_binding_level *old_scope = NULL;
2625 if (cxx_binding *binding = outer_binding (DECL_NAME (decl), NULL, true))
2627 old = binding->value;
2628 old_scope = binding->scope;
2630 while (old && VAR_P (old) && DECL_DEAD_FOR_LOCAL (old))
2631 old = DECL_SHADOWED_FOR_VAR (old);
2633 tree shadowed = NULL_TREE;
2634 if (old
2635 && (TREE_CODE (old) == PARM_DECL
2636 || VAR_P (old)
2637 || (TREE_CODE (old) == TYPE_DECL
2638 && (!DECL_ARTIFICIAL (old)
2639 || TREE_CODE (decl) == TYPE_DECL)))
2640 && (!DECL_ARTIFICIAL (decl)
2641 || DECL_IMPLICIT_TYPEDEF_P (decl)
2642 || (VAR_P (decl) && DECL_ANON_UNION_VAR_P (decl))))
2644 /* DECL shadows a local thing possibly of interest. */
2646 /* Don't complain if it's from an enclosing function. */
2647 if (DECL_CONTEXT (old) == current_function_decl
2648 && TREE_CODE (decl) != PARM_DECL
2649 && TREE_CODE (old) == PARM_DECL)
2651 /* Go to where the parms should be and see if we find
2652 them there. */
2653 cp_binding_level *b = current_binding_level->level_chain;
2655 if (FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
2656 /* Skip the ctor/dtor cleanup level. */
2657 b = b->level_chain;
2659 /* ARM $8.3 */
2660 if (b->kind == sk_function_parms)
2662 error ("declaration of %q#D shadows a parameter", decl);
2663 return;
2667 /* The local structure or class can't use parameters of
2668 the containing function anyway. */
2669 if (DECL_CONTEXT (old) != current_function_decl)
2671 for (cp_binding_level *scope = current_binding_level;
2672 scope != old_scope; scope = scope->level_chain)
2673 if (scope->kind == sk_class
2674 && !LAMBDA_TYPE_P (scope->this_entity))
2675 return;
2677 /* Error if redeclaring a local declared in a
2678 init-statement or in the condition of an if or
2679 switch statement when the new declaration is in the
2680 outermost block of the controlled statement.
2681 Redeclaring a variable from a for or while condition is
2682 detected elsewhere. */
2683 else if (VAR_P (old)
2684 && old_scope == current_binding_level->level_chain
2685 && (old_scope->kind == sk_cond || old_scope->kind == sk_for))
2687 error ("redeclaration of %q#D", decl);
2688 inform (DECL_SOURCE_LOCATION (old),
2689 "%q#D previously declared here", old);
2690 return;
2692 /* C++11:
2693 3.3.3/3: The name declared in an exception-declaration (...)
2694 shall not be redeclared in the outermost block of the handler.
2695 3.3.3/2: A parameter name shall not be redeclared (...) in
2696 the outermost block of any handler associated with a
2697 function-try-block.
2698 3.4.1/15: The function parameter names shall not be redeclared
2699 in the exception-declaration nor in the outermost block of a
2700 handler for the function-try-block. */
2701 else if ((TREE_CODE (old) == VAR_DECL
2702 && old_scope == current_binding_level->level_chain
2703 && old_scope->kind == sk_catch)
2704 || (TREE_CODE (old) == PARM_DECL
2705 && (current_binding_level->kind == sk_catch
2706 || current_binding_level->level_chain->kind == sk_catch)
2707 && in_function_try_handler))
2709 if (permerror (input_location, "redeclaration of %q#D", decl))
2710 inform (DECL_SOURCE_LOCATION (old),
2711 "%q#D previously declared here", old);
2712 return;
2715 /* If '-Wshadow=compatible-local' is specified without other
2716 -Wshadow= flags, we will warn only when the type of the
2717 shadowing variable (DECL) can be converted to that of the
2718 shadowed parameter (OLD_LOCAL). The reason why we only check
2719 if DECL's type can be converted to OLD_LOCAL's type (but not the
2720 other way around) is because when users accidentally shadow a
2721 parameter, more than often they would use the variable
2722 thinking (mistakenly) it's still the parameter. It would be
2723 rare that users would use the variable in the place that
2724 expects the parameter but thinking it's a new decl. */
2726 enum opt_code warning_code;
2727 if (warn_shadow)
2728 warning_code = OPT_Wshadow;
2729 else if (warn_shadow_local)
2730 warning_code = OPT_Wshadow_local;
2731 else if (warn_shadow_compatible_local
2732 && (same_type_p (TREE_TYPE (old), TREE_TYPE (decl))
2733 || (!dependent_type_p (TREE_TYPE (decl))
2734 && !dependent_type_p (TREE_TYPE (old))
2735 && can_convert (TREE_TYPE (old), TREE_TYPE (decl),
2736 tf_none))))
2737 warning_code = OPT_Wshadow_compatible_local;
2738 else
2739 return;
2741 const char *msg;
2742 if (TREE_CODE (old) == PARM_DECL)
2743 msg = "declaration of %q#D shadows a parameter";
2744 else if (is_capture_proxy (old))
2745 msg = "declaration of %qD shadows a lambda capture";
2746 else
2747 msg = "declaration of %qD shadows a previous local";
2749 if (warning_at (input_location, warning_code, msg, decl))
2751 shadowed = old;
2752 goto inform_shadowed;
2754 return;
2757 if (!warn_shadow)
2758 return;
2760 /* Don't warn for artificial things that are not implicit typedefs. */
2761 if (DECL_ARTIFICIAL (decl) && !DECL_IMPLICIT_TYPEDEF_P (decl))
2762 return;
2764 if (nonlambda_method_basetype ())
2765 if (tree member = lookup_member (current_nonlambda_class_type (),
2766 DECL_NAME (decl), /*protect=*/0,
2767 /*want_type=*/false, tf_warning_or_error))
2769 member = MAYBE_BASELINK_FUNCTIONS (member);
2771 /* Warn if a variable shadows a non-function, or the variable
2772 is a function or a pointer-to-function. */
2773 if (!OVL_P (member)
2774 || TREE_CODE (decl) == FUNCTION_DECL
2775 || TYPE_PTRFN_P (TREE_TYPE (decl))
2776 || TYPE_PTRMEMFUNC_P (TREE_TYPE (decl)))
2778 if (warning_at (input_location, OPT_Wshadow,
2779 "declaration of %qD shadows a member of %qT",
2780 decl, current_nonlambda_class_type ())
2781 && DECL_P (member))
2783 shadowed = member;
2784 goto inform_shadowed;
2787 return;
2790 /* Now look for a namespace shadow. */
2791 old = find_namespace_value (current_namespace, DECL_NAME (decl));
2792 if (old
2793 && (VAR_P (old)
2794 || (TREE_CODE (old) == TYPE_DECL
2795 && (!DECL_ARTIFICIAL (old)
2796 || TREE_CODE (decl) == TYPE_DECL)))
2797 && !instantiating_current_function_p ())
2798 /* XXX shadow warnings in outer-more namespaces */
2800 if (warning_at (input_location, OPT_Wshadow,
2801 "declaration of %qD shadows a global declaration",
2802 decl))
2804 shadowed = old;
2805 goto inform_shadowed;
2807 return;
2810 return;
2812 inform_shadowed:
2813 inform (DECL_SOURCE_LOCATION (shadowed), "shadowed declaration is here");
2816 /* DECL is being pushed inside function CTX. Set its context, if
2817 needed. */
2819 static void
2820 set_decl_context_in_fn (tree ctx, tree decl)
2822 if (!DECL_CONTEXT (decl)
2823 /* A local declaration for a function doesn't constitute
2824 nesting. */
2825 && TREE_CODE (decl) != FUNCTION_DECL
2826 /* A local declaration for an `extern' variable is in the
2827 scope of the current namespace, not the current
2828 function. */
2829 && !(VAR_P (decl) && DECL_EXTERNAL (decl))
2830 /* When parsing the parameter list of a function declarator,
2831 don't set DECL_CONTEXT to an enclosing function. When we
2832 push the PARM_DECLs in order to process the function body,
2833 current_binding_level->this_entity will be set. */
2834 && !(TREE_CODE (decl) == PARM_DECL
2835 && current_binding_level->kind == sk_function_parms
2836 && current_binding_level->this_entity == NULL))
2837 DECL_CONTEXT (decl) = ctx;
2839 /* If this is the declaration for a namespace-scope function,
2840 but the declaration itself is in a local scope, mark the
2841 declaration. */
2842 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (decl))
2843 DECL_LOCAL_FUNCTION_P (decl) = 1;
2846 /* DECL is a local-scope decl with linkage. SHADOWED is true if the
2847 name is already bound at the current level.
2849 [basic.link] If there is a visible declaration of an entity with
2850 linkage having the same name and type, ignoring entities declared
2851 outside the innermost enclosing namespace scope, the block scope
2852 declaration declares that same entity and receives the linkage of
2853 the previous declaration.
2855 Also, make sure that this decl matches any existing external decl
2856 in the enclosing namespace. */
2858 static void
2859 set_local_extern_decl_linkage (tree decl, bool shadowed)
2861 tree ns_value = decl; /* Unique marker. */
2863 if (!shadowed)
2865 tree loc_value = innermost_non_namespace_value (DECL_NAME (decl));
2866 if (!loc_value)
2868 ns_value
2869 = find_namespace_value (current_namespace, DECL_NAME (decl));
2870 loc_value = ns_value;
2872 if (loc_value == error_mark_node)
2873 loc_value = NULL_TREE;
2875 for (ovl_iterator iter (loc_value); iter; ++iter)
2876 if (!iter.hidden_p ()
2877 && (TREE_STATIC (*iter) || DECL_EXTERNAL (*iter))
2878 && decls_match (*iter, decl))
2880 /* The standard only says that the local extern inherits
2881 linkage from the previous decl; in particular, default
2882 args are not shared. Add the decl into a hash table to
2883 make sure only the previous decl in this case is seen
2884 by the middle end. */
2885 struct cxx_int_tree_map *h;
2887 /* We inherit the outer decl's linkage. But we're a
2888 different decl. */
2889 TREE_PUBLIC (decl) = TREE_PUBLIC (*iter);
2891 if (cp_function_chain->extern_decl_map == NULL)
2892 cp_function_chain->extern_decl_map
2893 = hash_table<cxx_int_tree_map_hasher>::create_ggc (20);
2895 h = ggc_alloc<cxx_int_tree_map> ();
2896 h->uid = DECL_UID (decl);
2897 h->to = *iter;
2898 cxx_int_tree_map **loc = cp_function_chain->extern_decl_map
2899 ->find_slot (h, INSERT);
2900 *loc = h;
2901 break;
2905 if (TREE_PUBLIC (decl))
2907 /* DECL is externally visible. Make sure it matches a matching
2908 decl in the namespace scope. We only really need to check
2909 this when inserting the decl, not when we find an existing
2910 match in the current scope. However, in practice we're
2911 going to be inserting a new decl in the majority of cases --
2912 who writes multiple extern decls for the same thing in the
2913 same local scope? Doing it here often avoids a duplicate
2914 namespace lookup. */
2916 /* Avoid repeating a lookup. */
2917 if (ns_value == decl)
2918 ns_value = find_namespace_value (current_namespace, DECL_NAME (decl));
2920 if (ns_value == error_mark_node)
2921 ns_value = NULL_TREE;
2923 for (ovl_iterator iter (ns_value); iter; ++iter)
2925 tree other = *iter;
2927 if (!(TREE_PUBLIC (other) || DECL_EXTERNAL (other)))
2928 ; /* Not externally visible. */
2929 else if (DECL_EXTERN_C_P (decl) && DECL_EXTERN_C_P (other))
2930 ; /* Both are extern "C", we'll check via that mechanism. */
2931 else if (TREE_CODE (other) != TREE_CODE (decl)
2932 || ((VAR_P (decl) || matching_fn_p (other, decl))
2933 && !comptypes (TREE_TYPE (decl), TREE_TYPE (other),
2934 COMPARE_REDECLARATION)))
2936 if (permerror (DECL_SOURCE_LOCATION (decl),
2937 "local external declaration %q#D", decl))
2938 inform (DECL_SOURCE_LOCATION (other),
2939 "does not match previous declaration %q#D", other);
2940 break;
2946 /* Record DECL as belonging to the current lexical scope. Check for
2947 errors (such as an incompatible declaration for the same name
2948 already seen in the same scope). IS_FRIEND is true if DECL is
2949 declared as a friend.
2951 Returns either DECL or an old decl for the same name. If an old
2952 decl is returned, it may have been smashed to agree with what DECL
2953 says. */
2955 static tree
2956 do_pushdecl (tree decl, bool is_friend)
2958 if (decl == error_mark_node)
2959 return error_mark_node;
2961 if (!DECL_TEMPLATE_PARM_P (decl) && current_function_decl)
2962 set_decl_context_in_fn (current_function_decl, decl);
2964 /* The binding level we will be pushing into. During local class
2965 pushing, we want to push to the containing scope. */
2966 cp_binding_level *level = current_binding_level;
2967 while (level->kind == sk_class)
2968 level = level->level_chain;
2970 /* An anonymous namespace has a NULL DECL_NAME, but we still want to
2971 insert it. Other NULL-named decls, not so much. */
2972 tree name = DECL_NAME (decl);
2973 if (name || TREE_CODE (decl) == NAMESPACE_DECL)
2975 cxx_binding *binding = NULL; /* Local scope binding. */
2976 tree ns = NULL_TREE; /* Searched namespace. */
2977 tree *slot = NULL; /* Binding slot in namespace. */
2978 tree old = NULL_TREE;
2980 if (level->kind == sk_namespace)
2982 /* We look in the decl's namespace for an existing
2983 declaration, even though we push into the current
2984 namespace. */
2985 ns = (DECL_NAMESPACE_SCOPE_P (decl)
2986 ? CP_DECL_CONTEXT (decl) : current_namespace);
2987 /* Create the binding, if this is current namespace, because
2988 that's where we'll be pushing anyway. */
2989 slot = find_namespace_slot (ns, name, ns == current_namespace);
2990 if (slot)
2991 old = MAYBE_STAT_DECL (*slot);
2993 else
2995 binding = find_local_binding (level, name);
2996 if (binding)
2997 old = binding->value;
3000 if (current_function_decl && VAR_OR_FUNCTION_DECL_P (decl)
3001 && DECL_EXTERNAL (decl))
3002 set_local_extern_decl_linkage (decl, old != NULL_TREE);
3004 if (old == error_mark_node)
3005 old = NULL_TREE;
3007 for (ovl_iterator iter (old); iter; ++iter)
3008 if (iter.using_p ())
3009 ; /* Ignore using decls here. */
3010 else if (tree match = duplicate_decls (decl, *iter, is_friend))
3012 if (match == error_mark_node)
3014 else if (TREE_CODE (match) == TYPE_DECL)
3015 /* The IDENTIFIER will have the type referring to the
3016 now-smashed TYPE_DECL, because ...? Reset it. */
3017 SET_IDENTIFIER_TYPE_VALUE (name, TREE_TYPE (match));
3018 else if (iter.hidden_p () && !DECL_HIDDEN_P (match))
3020 /* Unhiding a previously hidden decl. */
3021 tree head = iter.reveal_node (old);
3022 if (head != old)
3024 if (!ns)
3026 update_local_overload (binding, head);
3027 binding->value = head;
3029 else if (STAT_HACK_P (*slot))
3030 STAT_DECL (*slot) = head;
3031 else
3032 *slot = head;
3034 if (DECL_EXTERN_C_P (match))
3035 /* We need to check and register the decl now. */
3036 check_extern_c_conflict (match);
3038 return match;
3041 /* We are pushing a new decl. */
3043 /* Skip a hidden builtin we failed to match already. There can
3044 only be one. */
3045 if (old && anticipated_builtin_p (old))
3046 old = OVL_CHAIN (old);
3048 check_template_shadow (decl);
3050 if (DECL_DECLARES_FUNCTION_P (decl))
3052 check_default_args (decl);
3054 if (is_friend)
3056 if (level->kind != sk_namespace)
3057 /* In a local class, a friend function declaration must
3058 find a matching decl in the innermost non-class scope.
3059 [class.friend/11] */
3060 error ("friend declaration %qD in local class without "
3061 "prior local declaration", decl);
3062 else if (!flag_friend_injection)
3063 /* Hide it from ordinary lookup. */
3064 DECL_ANTICIPATED (decl) = DECL_HIDDEN_FRIEND_P (decl) = true;
3068 if (level->kind != sk_namespace)
3070 check_local_shadow (decl);
3072 if (TREE_CODE (decl) == NAMESPACE_DECL)
3073 /* A local namespace alias. */
3074 set_identifier_type_value (name, NULL_TREE);
3076 if (!binding)
3077 binding = create_local_binding (level, name);
3079 else if (!slot)
3081 ns = current_namespace;
3082 slot = find_namespace_slot (ns, name, true);
3083 /* Update OLD to reflect the namespace we're going to be
3084 pushing into. */
3085 old = MAYBE_STAT_DECL (*slot);
3088 old = update_binding (level, binding, slot, old, decl, is_friend);
3090 if (old != decl)
3091 /* An existing decl matched, use it. */
3092 decl = old;
3093 else if (TREE_CODE (decl) == TYPE_DECL)
3095 tree type = TREE_TYPE (decl);
3097 if (type != error_mark_node)
3099 if (TYPE_NAME (type) != decl)
3100 set_underlying_type (decl);
3102 if (!ns)
3103 set_identifier_type_value_with_scope (name, decl, level);
3104 else
3105 SET_IDENTIFIER_TYPE_VALUE (name, global_type_node);
3108 /* If this is a locally defined typedef in a function that
3109 is not a template instantation, record it to implement
3110 -Wunused-local-typedefs. */
3111 if (!instantiating_current_function_p ())
3112 record_locally_defined_typedef (decl);
3114 else if (VAR_P (decl))
3115 maybe_register_incomplete_var (decl);
3117 if ((VAR_P (decl) || TREE_CODE (decl) == FUNCTION_DECL)
3118 && DECL_EXTERN_C_P (decl))
3119 check_extern_c_conflict (decl);
3121 else
3122 add_decl_to_level (level, decl);
3124 return decl;
3127 /* Record a decl-node X as belonging to the current lexical scope.
3128 It's a friend if IS_FRIEND is true -- which affects exactly where
3129 we push it. */
3131 tree
3132 pushdecl (tree x, bool is_friend)
3134 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3135 tree ret = do_pushdecl (x, is_friend);
3136 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3137 return ret;
3140 /* Enter DECL into the symbol table, if that's appropriate. Returns
3141 DECL, or a modified version thereof. */
3143 tree
3144 maybe_push_decl (tree decl)
3146 tree type = TREE_TYPE (decl);
3148 /* Add this decl to the current binding level, but not if it comes
3149 from another scope, e.g. a static member variable. TEM may equal
3150 DECL or it may be a previous decl of the same name. */
3151 if (decl == error_mark_node
3152 || (TREE_CODE (decl) != PARM_DECL
3153 && DECL_CONTEXT (decl) != NULL_TREE
3154 /* Definitions of namespace members outside their namespace are
3155 possible. */
3156 && !DECL_NAMESPACE_SCOPE_P (decl))
3157 || (TREE_CODE (decl) == TEMPLATE_DECL && !namespace_bindings_p ())
3158 || type == unknown_type_node
3159 /* The declaration of a template specialization does not affect
3160 the functions available for overload resolution, so we do not
3161 call pushdecl. */
3162 || (TREE_CODE (decl) == FUNCTION_DECL
3163 && DECL_TEMPLATE_SPECIALIZATION (decl)))
3164 return decl;
3165 else
3166 return pushdecl (decl);
3169 /* Bind DECL to ID in the current_binding_level, assumed to be a local
3170 binding level. If IS_USING is true, DECL got here through a
3171 using-declaration. */
3173 static void
3174 push_local_binding (tree id, tree decl, bool is_using)
3176 /* Skip over any local classes. This makes sense if we call
3177 push_local_binding with a friend decl of a local class. */
3178 cp_binding_level *b = innermost_nonclass_level ();
3180 gcc_assert (b->kind != sk_namespace);
3181 if (find_local_binding (b, id))
3183 /* Supplement the existing binding. */
3184 if (!supplement_binding (IDENTIFIER_BINDING (id), decl))
3185 /* It didn't work. Something else must be bound at this
3186 level. Do not add DECL to the list of things to pop
3187 later. */
3188 return;
3190 else
3191 /* Create a new binding. */
3192 push_binding (id, decl, b);
3194 if (TREE_CODE (decl) == OVERLOAD || is_using)
3195 /* We must put the OVERLOAD or using into a TREE_LIST since we
3196 cannot use the decl's chain itself. */
3197 decl = build_tree_list (NULL_TREE, decl);
3199 /* And put DECL on the list of things declared by the current
3200 binding level. */
3201 add_decl_to_level (b, decl);
3204 /* Check to see whether or not DECL is a variable that would have been
3205 in scope under the ARM, but is not in scope under the ANSI/ISO
3206 standard. If so, issue an error message. If name lookup would
3207 work in both cases, but return a different result, this function
3208 returns the result of ANSI/ISO lookup. Otherwise, it returns
3209 DECL. */
3211 tree
3212 check_for_out_of_scope_variable (tree decl)
3214 tree shadowed;
3216 /* We only care about out of scope variables. */
3217 if (!(VAR_P (decl) && DECL_DEAD_FOR_LOCAL (decl)))
3218 return decl;
3220 shadowed = DECL_HAS_SHADOWED_FOR_VAR_P (decl)
3221 ? DECL_SHADOWED_FOR_VAR (decl) : NULL_TREE ;
3222 while (shadowed != NULL_TREE && VAR_P (shadowed)
3223 && DECL_DEAD_FOR_LOCAL (shadowed))
3224 shadowed = DECL_HAS_SHADOWED_FOR_VAR_P (shadowed)
3225 ? DECL_SHADOWED_FOR_VAR (shadowed) : NULL_TREE;
3226 if (!shadowed)
3227 shadowed = find_namespace_value (current_namespace, DECL_NAME (decl));
3228 if (shadowed)
3230 if (!DECL_ERROR_REPORTED (decl))
3232 warning (0, "name lookup of %qD changed", DECL_NAME (decl));
3233 warning_at (DECL_SOURCE_LOCATION (shadowed), 0,
3234 " matches this %qD under ISO standard rules",
3235 shadowed);
3236 warning_at (DECL_SOURCE_LOCATION (decl), 0,
3237 " matches this %qD under old rules", decl);
3238 DECL_ERROR_REPORTED (decl) = 1;
3240 return shadowed;
3243 /* If we have already complained about this declaration, there's no
3244 need to do it again. */
3245 if (DECL_ERROR_REPORTED (decl))
3246 return decl;
3248 DECL_ERROR_REPORTED (decl) = 1;
3250 if (TREE_TYPE (decl) == error_mark_node)
3251 return decl;
3253 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
3255 error ("name lookup of %qD changed for ISO %<for%> scoping",
3256 DECL_NAME (decl));
3257 error (" cannot use obsolete binding at %q+D because "
3258 "it has a destructor", decl);
3259 return error_mark_node;
3261 else
3263 permerror (input_location, "name lookup of %qD changed for ISO %<for%> scoping",
3264 DECL_NAME (decl));
3265 if (flag_permissive)
3266 permerror (DECL_SOURCE_LOCATION (decl),
3267 " using obsolete binding at %qD", decl);
3268 else
3270 static bool hint;
3271 if (!hint)
3273 inform (input_location, "(if you use %<-fpermissive%> G++ will accept your code)");
3274 hint = true;
3279 return decl;
3282 /* true means unconditionally make a BLOCK for the next level pushed. */
3284 static bool keep_next_level_flag;
3286 static int binding_depth = 0;
3288 static void
3289 indent (int depth)
3291 int i;
3293 for (i = 0; i < depth * 2; i++)
3294 putc (' ', stderr);
3297 /* Return a string describing the kind of SCOPE we have. */
3298 static const char *
3299 cp_binding_level_descriptor (cp_binding_level *scope)
3301 /* The order of this table must match the "scope_kind"
3302 enumerators. */
3303 static const char* scope_kind_names[] = {
3304 "block-scope",
3305 "cleanup-scope",
3306 "try-scope",
3307 "catch-scope",
3308 "for-scope",
3309 "function-parameter-scope",
3310 "class-scope",
3311 "namespace-scope",
3312 "template-parameter-scope",
3313 "template-explicit-spec-scope"
3315 const scope_kind kind = scope->explicit_spec_p
3316 ? sk_template_spec : scope->kind;
3318 return scope_kind_names[kind];
3321 /* Output a debugging information about SCOPE when performing
3322 ACTION at LINE. */
3323 static void
3324 cp_binding_level_debug (cp_binding_level *scope, int line, const char *action)
3326 const char *desc = cp_binding_level_descriptor (scope);
3327 if (scope->this_entity)
3328 verbatim ("%s %<%s(%E)%> %p %d\n", action, desc,
3329 scope->this_entity, (void *) scope, line);
3330 else
3331 verbatim ("%s %s %p %d\n", action, desc, (void *) scope, line);
3334 /* Return the estimated initial size of the hashtable of a NAMESPACE
3335 scope. */
3337 static inline size_t
3338 namespace_scope_ht_size (tree ns)
3340 tree name = DECL_NAME (ns);
3342 return name == std_identifier
3343 ? NAMESPACE_STD_HT_SIZE
3344 : (name == global_identifier
3345 ? GLOBAL_SCOPE_HT_SIZE
3346 : NAMESPACE_ORDINARY_HT_SIZE);
3349 /* A chain of binding_level structures awaiting reuse. */
3351 static GTY((deletable)) cp_binding_level *free_binding_level;
3353 /* Insert SCOPE as the innermost binding level. */
3355 void
3356 push_binding_level (cp_binding_level *scope)
3358 /* Add it to the front of currently active scopes stack. */
3359 scope->level_chain = current_binding_level;
3360 current_binding_level = scope;
3361 keep_next_level_flag = false;
3363 if (ENABLE_SCOPE_CHECKING)
3365 scope->binding_depth = binding_depth;
3366 indent (binding_depth);
3367 cp_binding_level_debug (scope, LOCATION_LINE (input_location),
3368 "push");
3369 binding_depth++;
3373 /* Create a new KIND scope and make it the top of the active scopes stack.
3374 ENTITY is the scope of the associated C++ entity (namespace, class,
3375 function, C++0x enumeration); it is NULL otherwise. */
3377 cp_binding_level *
3378 begin_scope (scope_kind kind, tree entity)
3380 cp_binding_level *scope;
3382 /* Reuse or create a struct for this binding level. */
3383 if (!ENABLE_SCOPE_CHECKING && free_binding_level)
3385 scope = free_binding_level;
3386 free_binding_level = scope->level_chain;
3387 memset (scope, 0, sizeof (cp_binding_level));
3389 else
3390 scope = ggc_cleared_alloc<cp_binding_level> ();
3392 scope->this_entity = entity;
3393 scope->more_cleanups_ok = true;
3394 switch (kind)
3396 case sk_cleanup:
3397 scope->keep = true;
3398 break;
3400 case sk_template_spec:
3401 scope->explicit_spec_p = true;
3402 kind = sk_template_parms;
3403 /* Fall through. */
3404 case sk_template_parms:
3405 case sk_block:
3406 case sk_try:
3407 case sk_catch:
3408 case sk_for:
3409 case sk_cond:
3410 case sk_class:
3411 case sk_scoped_enum:
3412 case sk_function_parms:
3413 case sk_transaction:
3414 case sk_omp:
3415 scope->keep = keep_next_level_flag;
3416 break;
3418 case sk_namespace:
3419 NAMESPACE_LEVEL (entity) = scope;
3420 break;
3422 default:
3423 /* Should not happen. */
3424 gcc_unreachable ();
3425 break;
3427 scope->kind = kind;
3429 push_binding_level (scope);
3431 return scope;
3434 /* We're about to leave current scope. Pop the top of the stack of
3435 currently active scopes. Return the enclosing scope, now active. */
3437 cp_binding_level *
3438 leave_scope (void)
3440 cp_binding_level *scope = current_binding_level;
3442 if (scope->kind == sk_namespace && class_binding_level)
3443 current_binding_level = class_binding_level;
3445 /* We cannot leave a scope, if there are none left. */
3446 if (NAMESPACE_LEVEL (global_namespace))
3447 gcc_assert (!global_scope_p (scope));
3449 if (ENABLE_SCOPE_CHECKING)
3451 indent (--binding_depth);
3452 cp_binding_level_debug (scope, LOCATION_LINE (input_location),
3453 "leave");
3456 /* Move one nesting level up. */
3457 current_binding_level = scope->level_chain;
3459 /* Namespace-scopes are left most probably temporarily, not
3460 completely; they can be reopened later, e.g. in namespace-extension
3461 or any name binding activity that requires us to resume a
3462 namespace. For classes, we cache some binding levels. For other
3463 scopes, we just make the structure available for reuse. */
3464 if (scope->kind != sk_namespace
3465 && scope->kind != sk_class)
3467 scope->level_chain = free_binding_level;
3468 gcc_assert (!ENABLE_SCOPE_CHECKING
3469 || scope->binding_depth == binding_depth);
3470 free_binding_level = scope;
3473 if (scope->kind == sk_class)
3475 /* Reset DEFINING_CLASS_P to allow for reuse of a
3476 class-defining scope in a non-defining context. */
3477 scope->defining_class_p = 0;
3479 /* Find the innermost enclosing class scope, and reset
3480 CLASS_BINDING_LEVEL appropriately. */
3481 class_binding_level = NULL;
3482 for (scope = current_binding_level; scope; scope = scope->level_chain)
3483 if (scope->kind == sk_class)
3485 class_binding_level = scope;
3486 break;
3490 return current_binding_level;
3493 static void
3494 resume_scope (cp_binding_level* b)
3496 /* Resuming binding levels is meant only for namespaces,
3497 and those cannot nest into classes. */
3498 gcc_assert (!class_binding_level);
3499 /* Also, resuming a non-directly nested namespace is a no-no. */
3500 gcc_assert (b->level_chain == current_binding_level);
3501 current_binding_level = b;
3502 if (ENABLE_SCOPE_CHECKING)
3504 b->binding_depth = binding_depth;
3505 indent (binding_depth);
3506 cp_binding_level_debug (b, LOCATION_LINE (input_location), "resume");
3507 binding_depth++;
3511 /* Return the innermost binding level that is not for a class scope. */
3513 static cp_binding_level *
3514 innermost_nonclass_level (void)
3516 cp_binding_level *b;
3518 b = current_binding_level;
3519 while (b->kind == sk_class)
3520 b = b->level_chain;
3522 return b;
3525 /* We're defining an object of type TYPE. If it needs a cleanup, but
3526 we're not allowed to add any more objects with cleanups to the current
3527 scope, create a new binding level. */
3529 void
3530 maybe_push_cleanup_level (tree type)
3532 if (type != error_mark_node
3533 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
3534 && current_binding_level->more_cleanups_ok == 0)
3536 begin_scope (sk_cleanup, NULL);
3537 current_binding_level->statement_list = push_stmt_list ();
3541 /* Return true if we are in the global binding level. */
3543 bool
3544 global_bindings_p (void)
3546 return global_scope_p (current_binding_level);
3549 /* True if we are currently in a toplevel binding level. This
3550 means either the global binding level or a namespace in a toplevel
3551 binding level. Since there are no non-toplevel namespace levels,
3552 this really means any namespace or template parameter level. We
3553 also include a class whose context is toplevel. */
3555 bool
3556 toplevel_bindings_p (void)
3558 cp_binding_level *b = innermost_nonclass_level ();
3560 return b->kind == sk_namespace || b->kind == sk_template_parms;
3563 /* True if this is a namespace scope, or if we are defining a class
3564 which is itself at namespace scope, or whose enclosing class is
3565 such a class, etc. */
3567 bool
3568 namespace_bindings_p (void)
3570 cp_binding_level *b = innermost_nonclass_level ();
3572 return b->kind == sk_namespace;
3575 /* True if the innermost non-class scope is a block scope. */
3577 bool
3578 local_bindings_p (void)
3580 cp_binding_level *b = innermost_nonclass_level ();
3581 return b->kind < sk_function_parms || b->kind == sk_omp;
3584 /* True if the current level needs to have a BLOCK made. */
3586 bool
3587 kept_level_p (void)
3589 return (current_binding_level->blocks != NULL_TREE
3590 || current_binding_level->keep
3591 || current_binding_level->kind == sk_cleanup
3592 || current_binding_level->names != NULL_TREE
3593 || current_binding_level->using_directives);
3596 /* Returns the kind of the innermost scope. */
3598 scope_kind
3599 innermost_scope_kind (void)
3601 return current_binding_level->kind;
3604 /* Returns true if this scope was created to store template parameters. */
3606 bool
3607 template_parm_scope_p (void)
3609 return innermost_scope_kind () == sk_template_parms;
3612 /* If KEEP is true, make a BLOCK node for the next binding level,
3613 unconditionally. Otherwise, use the normal logic to decide whether
3614 or not to create a BLOCK. */
3616 void
3617 keep_next_level (bool keep)
3619 keep_next_level_flag = keep;
3622 /* Return the list of declarations of the current local scope. */
3624 tree
3625 get_local_decls (void)
3627 gcc_assert (current_binding_level->kind != sk_namespace
3628 && current_binding_level->kind != sk_class);
3629 return current_binding_level->names;
3632 /* Return how many function prototypes we are currently nested inside. */
3635 function_parm_depth (void)
3637 int level = 0;
3638 cp_binding_level *b;
3640 for (b = current_binding_level;
3641 b->kind == sk_function_parms;
3642 b = b->level_chain)
3643 ++level;
3645 return level;
3648 /* For debugging. */
3649 static int no_print_functions = 0;
3650 static int no_print_builtins = 0;
3652 static void
3653 print_binding_level (cp_binding_level* lvl)
3655 tree t;
3656 int i = 0, len;
3657 fprintf (stderr, " blocks=%p", (void *) lvl->blocks);
3658 if (lvl->more_cleanups_ok)
3659 fprintf (stderr, " more-cleanups-ok");
3660 if (lvl->have_cleanups)
3661 fprintf (stderr, " have-cleanups");
3662 fprintf (stderr, "\n");
3663 if (lvl->names)
3665 fprintf (stderr, " names:\t");
3666 /* We can probably fit 3 names to a line? */
3667 for (t = lvl->names; t; t = TREE_CHAIN (t))
3669 if (no_print_functions && (TREE_CODE (t) == FUNCTION_DECL))
3670 continue;
3671 if (no_print_builtins
3672 && (TREE_CODE (t) == TYPE_DECL)
3673 && DECL_IS_BUILTIN (t))
3674 continue;
3676 /* Function decls tend to have longer names. */
3677 if (TREE_CODE (t) == FUNCTION_DECL)
3678 len = 3;
3679 else
3680 len = 2;
3681 i += len;
3682 if (i > 6)
3684 fprintf (stderr, "\n\t");
3685 i = len;
3687 print_node_brief (stderr, "", t, 0);
3688 if (t == error_mark_node)
3689 break;
3691 if (i)
3692 fprintf (stderr, "\n");
3694 if (vec_safe_length (lvl->class_shadowed))
3696 size_t i;
3697 cp_class_binding *b;
3698 fprintf (stderr, " class-shadowed:");
3699 FOR_EACH_VEC_ELT (*lvl->class_shadowed, i, b)
3700 fprintf (stderr, " %s ", IDENTIFIER_POINTER (b->identifier));
3701 fprintf (stderr, "\n");
3703 if (lvl->type_shadowed)
3705 fprintf (stderr, " type-shadowed:");
3706 for (t = lvl->type_shadowed; t; t = TREE_CHAIN (t))
3708 fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
3710 fprintf (stderr, "\n");
3714 DEBUG_FUNCTION void
3715 debug (cp_binding_level &ref)
3717 print_binding_level (&ref);
3720 DEBUG_FUNCTION void
3721 debug (cp_binding_level *ptr)
3723 if (ptr)
3724 debug (*ptr);
3725 else
3726 fprintf (stderr, "<nil>\n");
3730 void
3731 print_other_binding_stack (cp_binding_level *stack)
3733 cp_binding_level *level;
3734 for (level = stack; !global_scope_p (level); level = level->level_chain)
3736 fprintf (stderr, "binding level %p\n", (void *) level);
3737 print_binding_level (level);
3741 void
3742 print_binding_stack (void)
3744 cp_binding_level *b;
3745 fprintf (stderr, "current_binding_level=%p\n"
3746 "class_binding_level=%p\n"
3747 "NAMESPACE_LEVEL (global_namespace)=%p\n",
3748 (void *) current_binding_level, (void *) class_binding_level,
3749 (void *) NAMESPACE_LEVEL (global_namespace));
3750 if (class_binding_level)
3752 for (b = class_binding_level; b; b = b->level_chain)
3753 if (b == current_binding_level)
3754 break;
3755 if (b)
3756 b = class_binding_level;
3757 else
3758 b = current_binding_level;
3760 else
3761 b = current_binding_level;
3762 print_other_binding_stack (b);
3763 fprintf (stderr, "global:\n");
3764 print_binding_level (NAMESPACE_LEVEL (global_namespace));
3767 /* Return the type associated with ID. */
3769 static tree
3770 identifier_type_value_1 (tree id)
3772 /* There is no type with that name, anywhere. */
3773 if (REAL_IDENTIFIER_TYPE_VALUE (id) == NULL_TREE)
3774 return NULL_TREE;
3775 /* This is not the type marker, but the real thing. */
3776 if (REAL_IDENTIFIER_TYPE_VALUE (id) != global_type_node)
3777 return REAL_IDENTIFIER_TYPE_VALUE (id);
3778 /* Have to search for it. It must be on the global level, now.
3779 Ask lookup_name not to return non-types. */
3780 id = lookup_name_real (id, 2, 1, /*block_p=*/true, 0, 0);
3781 if (id)
3782 return TREE_TYPE (id);
3783 return NULL_TREE;
3786 /* Wrapper for identifier_type_value_1. */
3788 tree
3789 identifier_type_value (tree id)
3791 tree ret;
3792 timevar_start (TV_NAME_LOOKUP);
3793 ret = identifier_type_value_1 (id);
3794 timevar_stop (TV_NAME_LOOKUP);
3795 return ret;
3798 /* Push a definition of struct, union or enum tag named ID. into
3799 binding_level B. DECL is a TYPE_DECL for the type. We assume that
3800 the tag ID is not already defined. */
3802 static void
3803 set_identifier_type_value_with_scope (tree id, tree decl, cp_binding_level *b)
3805 tree type;
3807 if (b->kind != sk_namespace)
3809 /* Shadow the marker, not the real thing, so that the marker
3810 gets restored later. */
3811 tree old_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
3812 b->type_shadowed
3813 = tree_cons (id, old_type_value, b->type_shadowed);
3814 type = decl ? TREE_TYPE (decl) : NULL_TREE;
3815 TREE_TYPE (b->type_shadowed) = type;
3817 else
3819 tree *slot = find_namespace_slot (current_namespace, id, true);
3820 gcc_assert (decl);
3821 update_binding (b, NULL, slot, MAYBE_STAT_DECL (*slot), decl, false);
3823 /* Store marker instead of real type. */
3824 type = global_type_node;
3826 SET_IDENTIFIER_TYPE_VALUE (id, type);
3829 /* As set_identifier_type_value_with_scope, but using
3830 current_binding_level. */
3832 void
3833 set_identifier_type_value (tree id, tree decl)
3835 set_identifier_type_value_with_scope (id, decl, current_binding_level);
3838 /* Return the name for the constructor (or destructor) for the
3839 specified class. */
3841 tree
3842 constructor_name (tree type)
3844 tree decl = TYPE_NAME (TYPE_MAIN_VARIANT (type));
3846 return decl ? DECL_NAME (decl) : NULL_TREE;
3849 /* Returns TRUE if NAME is the name for the constructor for TYPE,
3850 which must be a class type. */
3852 bool
3853 constructor_name_p (tree name, tree type)
3855 gcc_assert (MAYBE_CLASS_TYPE_P (type));
3857 /* These don't have names. */
3858 if (TREE_CODE (type) == DECLTYPE_TYPE
3859 || TREE_CODE (type) == TYPEOF_TYPE)
3860 return false;
3862 if (name && name == constructor_name (type))
3863 return true;
3865 return false;
3868 /* Counter used to create anonymous type names. */
3870 static GTY(()) int anon_cnt;
3872 /* Return an IDENTIFIER which can be used as a name for
3873 unnamed structs and unions. */
3875 tree
3876 make_anon_name (void)
3878 char buf[32];
3880 sprintf (buf, anon_aggrname_format (), anon_cnt++);
3881 return get_identifier (buf);
3884 /* This code is practically identical to that for creating
3885 anonymous names, but is just used for lambdas instead. This isn't really
3886 necessary, but it's convenient to avoid treating lambdas like other
3887 unnamed types. */
3889 static GTY(()) int lambda_cnt = 0;
3891 tree
3892 make_lambda_name (void)
3894 char buf[32];
3896 sprintf (buf, LAMBDANAME_FORMAT, lambda_cnt++);
3897 return get_identifier (buf);
3900 /* Insert another USING_DECL into the current binding level, returning
3901 this declaration. If this is a redeclaration, do nothing, and
3902 return NULL_TREE if this not in namespace scope (in namespace
3903 scope, a using decl might extend any previous bindings). */
3905 static tree
3906 push_using_decl_1 (tree scope, tree name)
3908 tree decl;
3910 gcc_assert (TREE_CODE (scope) == NAMESPACE_DECL);
3911 gcc_assert (identifier_p (name));
3912 for (decl = current_binding_level->usings; decl; decl = DECL_CHAIN (decl))
3913 if (USING_DECL_SCOPE (decl) == scope && DECL_NAME (decl) == name)
3914 break;
3915 if (decl)
3916 return namespace_bindings_p () ? decl : NULL_TREE;
3917 decl = build_lang_decl (USING_DECL, name, NULL_TREE);
3918 USING_DECL_SCOPE (decl) = scope;
3919 DECL_CHAIN (decl) = current_binding_level->usings;
3920 current_binding_level->usings = decl;
3921 return decl;
3924 /* Wrapper for push_using_decl_1. */
3926 static tree
3927 push_using_decl (tree scope, tree name)
3929 tree ret;
3930 timevar_start (TV_NAME_LOOKUP);
3931 ret = push_using_decl_1 (scope, name);
3932 timevar_stop (TV_NAME_LOOKUP);
3933 return ret;
3936 /* Same as pushdecl, but define X in binding-level LEVEL. We rely on the
3937 caller to set DECL_CONTEXT properly.
3939 Note that this must only be used when X will be the new innermost
3940 binding for its name, as we tack it onto the front of IDENTIFIER_BINDING
3941 without checking to see if the current IDENTIFIER_BINDING comes from a
3942 closer binding level than LEVEL. */
3944 static tree
3945 do_pushdecl_with_scope (tree x, cp_binding_level *level, bool is_friend)
3947 cp_binding_level *b;
3948 tree function_decl = current_function_decl;
3950 current_function_decl = NULL_TREE;
3951 if (level->kind == sk_class)
3953 b = class_binding_level;
3954 class_binding_level = level;
3955 pushdecl_class_level (x);
3956 class_binding_level = b;
3958 else
3960 b = current_binding_level;
3961 current_binding_level = level;
3962 x = pushdecl (x, is_friend);
3963 current_binding_level = b;
3965 current_function_decl = function_decl;
3966 return x;
3969 /* Inject X into the local scope just before the function parms. */
3971 tree
3972 pushdecl_outermost_localscope (tree x)
3974 cp_binding_level *b = NULL;
3975 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3977 /* Find the scope just inside the function parms. */
3978 for (cp_binding_level *n = current_binding_level;
3979 n->kind != sk_function_parms; n = b->level_chain)
3980 b = n;
3982 tree ret = b ? do_pushdecl_with_scope (x, b, false) : error_mark_node;
3983 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3985 return ret;
3988 /* Check a non-member using-declaration. Return the name and scope
3989 being used, and the USING_DECL, or NULL_TREE on failure. */
3991 static tree
3992 validate_nonmember_using_decl (tree decl, tree scope, tree name)
3994 /* [namespace.udecl]
3995 A using-declaration for a class member shall be a
3996 member-declaration. */
3997 if (TYPE_P (scope))
3999 error ("%qT is not a namespace or unscoped enum", scope);
4000 return NULL_TREE;
4002 else if (scope == error_mark_node)
4003 return NULL_TREE;
4005 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR)
4007 /* 7.3.3/5
4008 A using-declaration shall not name a template-id. */
4009 error ("a using-declaration cannot specify a template-id. "
4010 "Try %<using %D%>", name);
4011 return NULL_TREE;
4014 if (TREE_CODE (decl) == NAMESPACE_DECL)
4016 error ("namespace %qD not allowed in using-declaration", decl);
4017 return NULL_TREE;
4020 if (TREE_CODE (decl) == SCOPE_REF)
4022 /* It's a nested name with template parameter dependent scope.
4023 This can only be using-declaration for class member. */
4024 error ("%qT is not a namespace", TREE_OPERAND (decl, 0));
4025 return NULL_TREE;
4028 decl = OVL_FIRST (decl);
4030 /* Make a USING_DECL. */
4031 tree using_decl = push_using_decl (scope, name);
4033 if (using_decl == NULL_TREE
4034 && at_function_scope_p ()
4035 && VAR_P (decl))
4036 /* C++11 7.3.3/10. */
4037 error ("%qD is already declared in this scope", name);
4039 return using_decl;
4042 /* Process a local-scope or namespace-scope using declaration. SCOPE
4043 is the nominated scope to search for NAME. VALUE_P and TYPE_P
4044 point to the binding for NAME in the current scope and are
4045 updated. */
4047 static void
4048 do_nonmember_using_decl (tree scope, tree name, tree *value_p, tree *type_p)
4050 name_lookup lookup (name, 0);
4052 if (!qualified_namespace_lookup (scope, &lookup))
4054 error ("%qD not declared", name);
4055 return;
4057 else if (TREE_CODE (lookup.value) == TREE_LIST)
4059 error ("reference to %qD is ambiguous", name);
4060 print_candidates (lookup.value);
4061 lookup.value = NULL_TREE;
4064 if (lookup.type && TREE_CODE (lookup.type) == TREE_LIST)
4066 error ("reference to %qD is ambiguous", name);
4067 print_candidates (lookup.type);
4068 lookup.type = NULL_TREE;
4071 tree value = *value_p;
4072 tree type = *type_p;
4074 /* Shift the old and new bindings around so we're comparing class and
4075 enumeration names to each other. */
4076 if (value && DECL_IMPLICIT_TYPEDEF_P (value))
4078 type = value;
4079 value = NULL_TREE;
4082 if (lookup.value && DECL_IMPLICIT_TYPEDEF_P (lookup.value))
4084 lookup.type = lookup.value;
4085 lookup.value = NULL_TREE;
4088 if (lookup.value && lookup.value != value)
4090 /* Check for using functions. */
4091 if (OVL_P (lookup.value) && (!value || OVL_P (value)))
4093 for (lkp_iterator usings (lookup.value); usings; ++usings)
4095 tree new_fn = *usings;
4097 /* [namespace.udecl]
4099 If a function declaration in namespace scope or block
4100 scope has the same name and the same parameter types as a
4101 function introduced by a using declaration the program is
4102 ill-formed. */
4103 bool found = false;
4104 for (ovl_iterator old (value); !found && old; ++old)
4106 tree old_fn = *old;
4108 if (new_fn == old_fn)
4109 /* The function already exists in the current
4110 namespace. */
4111 found = true;
4112 else if (old.using_p ())
4113 continue; /* This is a using decl. */
4114 else if (old.hidden_p () && !DECL_HIDDEN_FRIEND_P (old_fn))
4115 continue; /* This is an anticipated builtin. */
4116 else if (!matching_fn_p (new_fn, old_fn))
4117 continue; /* Parameters do not match. */
4118 else if (decls_match (new_fn, old_fn))
4119 found = true;
4120 else
4122 diagnose_name_conflict (new_fn, old_fn);
4123 found = true;
4127 if (!found)
4128 /* Unlike the overload case we don't drop anticipated
4129 builtins here. They don't cause a problem, and
4130 we'd like to match them with a future
4131 declaration. */
4132 value = ovl_insert (new_fn, value, true);
4135 else if (value
4136 /* Ignore anticipated builtins. */
4137 && !anticipated_builtin_p (value)
4138 && !decls_match (lookup.value, value))
4139 diagnose_name_conflict (lookup.value, value);
4140 else
4141 value = lookup.value;
4144 if (lookup.type && lookup.type != type)
4146 if (type && !decls_match (lookup.type, type))
4147 diagnose_name_conflict (lookup.type, type);
4148 else
4149 type = lookup.type;
4152 /* If bind->value is empty, shift any class or enumeration name back. */
4153 if (!value)
4155 value = type;
4156 type = NULL_TREE;
4159 *value_p = value;
4160 *type_p = type;
4163 /* Returns true if ANCESTOR encloses DESCENDANT, including matching.
4164 Both are namespaces. */
4166 bool
4167 is_nested_namespace (tree ancestor, tree descendant, bool inline_only)
4169 int depth = SCOPE_DEPTH (ancestor);
4171 if (!depth && !inline_only)
4172 /* The global namespace encloses everything. */
4173 return true;
4175 while (SCOPE_DEPTH (descendant) > depth
4176 && (!inline_only || DECL_NAMESPACE_INLINE_P (descendant)))
4177 descendant = CP_DECL_CONTEXT (descendant);
4179 return ancestor == descendant;
4182 /* Returns true if ROOT (a namespace, class, or function) encloses
4183 CHILD. CHILD may be either a class type or a namespace. */
4185 bool
4186 is_ancestor (tree root, tree child)
4188 gcc_assert ((TREE_CODE (root) == NAMESPACE_DECL
4189 || TREE_CODE (root) == FUNCTION_DECL
4190 || CLASS_TYPE_P (root)));
4191 gcc_assert ((TREE_CODE (child) == NAMESPACE_DECL
4192 || CLASS_TYPE_P (child)));
4194 /* The global namespace encloses everything. */
4195 if (root == global_namespace)
4196 return true;
4198 /* Search until we reach namespace scope. */
4199 while (TREE_CODE (child) != NAMESPACE_DECL)
4201 /* If we've reached the ROOT, it encloses CHILD. */
4202 if (root == child)
4203 return true;
4204 /* Go out one level. */
4205 if (TYPE_P (child))
4206 child = TYPE_NAME (child);
4207 child = CP_DECL_CONTEXT (child);
4210 if (TREE_CODE (root) == NAMESPACE_DECL)
4211 return is_nested_namespace (root, child);
4213 return false;
4216 /* Enter the class or namespace scope indicated by T suitable for name
4217 lookup. T can be arbitrary scope, not necessary nested inside the
4218 current scope. Returns a non-null scope to pop iff pop_scope
4219 should be called later to exit this scope. */
4221 tree
4222 push_scope (tree t)
4224 if (TREE_CODE (t) == NAMESPACE_DECL)
4225 push_decl_namespace (t);
4226 else if (CLASS_TYPE_P (t))
4228 if (!at_class_scope_p ()
4229 || !same_type_p (current_class_type, t))
4230 push_nested_class (t);
4231 else
4232 /* T is the same as the current scope. There is therefore no
4233 need to re-enter the scope. Since we are not actually
4234 pushing a new scope, our caller should not call
4235 pop_scope. */
4236 t = NULL_TREE;
4239 return t;
4242 /* Leave scope pushed by push_scope. */
4244 void
4245 pop_scope (tree t)
4247 if (t == NULL_TREE)
4248 return;
4249 if (TREE_CODE (t) == NAMESPACE_DECL)
4250 pop_decl_namespace ();
4251 else if CLASS_TYPE_P (t)
4252 pop_nested_class ();
4255 /* Subroutine of push_inner_scope. */
4257 static void
4258 push_inner_scope_r (tree outer, tree inner)
4260 tree prev;
4262 if (outer == inner
4263 || (TREE_CODE (inner) != NAMESPACE_DECL && !CLASS_TYPE_P (inner)))
4264 return;
4266 prev = CP_DECL_CONTEXT (TREE_CODE (inner) == NAMESPACE_DECL ? inner : TYPE_NAME (inner));
4267 if (outer != prev)
4268 push_inner_scope_r (outer, prev);
4269 if (TREE_CODE (inner) == NAMESPACE_DECL)
4271 cp_binding_level *save_template_parm = 0;
4272 /* Temporary take out template parameter scopes. They are saved
4273 in reversed order in save_template_parm. */
4274 while (current_binding_level->kind == sk_template_parms)
4276 cp_binding_level *b = current_binding_level;
4277 current_binding_level = b->level_chain;
4278 b->level_chain = save_template_parm;
4279 save_template_parm = b;
4282 resume_scope (NAMESPACE_LEVEL (inner));
4283 current_namespace = inner;
4285 /* Restore template parameter scopes. */
4286 while (save_template_parm)
4288 cp_binding_level *b = save_template_parm;
4289 save_template_parm = b->level_chain;
4290 b->level_chain = current_binding_level;
4291 current_binding_level = b;
4294 else
4295 pushclass (inner);
4298 /* Enter the scope INNER from current scope. INNER must be a scope
4299 nested inside current scope. This works with both name lookup and
4300 pushing name into scope. In case a template parameter scope is present,
4301 namespace is pushed under the template parameter scope according to
4302 name lookup rule in 14.6.1/6.
4304 Return the former current scope suitable for pop_inner_scope. */
4306 tree
4307 push_inner_scope (tree inner)
4309 tree outer = current_scope ();
4310 if (!outer)
4311 outer = current_namespace;
4313 push_inner_scope_r (outer, inner);
4314 return outer;
4317 /* Exit the current scope INNER back to scope OUTER. */
4319 void
4320 pop_inner_scope (tree outer, tree inner)
4322 if (outer == inner
4323 || (TREE_CODE (inner) != NAMESPACE_DECL && !CLASS_TYPE_P (inner)))
4324 return;
4326 while (outer != inner)
4328 if (TREE_CODE (inner) == NAMESPACE_DECL)
4330 cp_binding_level *save_template_parm = 0;
4331 /* Temporary take out template parameter scopes. They are saved
4332 in reversed order in save_template_parm. */
4333 while (current_binding_level->kind == sk_template_parms)
4335 cp_binding_level *b = current_binding_level;
4336 current_binding_level = b->level_chain;
4337 b->level_chain = save_template_parm;
4338 save_template_parm = b;
4341 pop_namespace ();
4343 /* Restore template parameter scopes. */
4344 while (save_template_parm)
4346 cp_binding_level *b = save_template_parm;
4347 save_template_parm = b->level_chain;
4348 b->level_chain = current_binding_level;
4349 current_binding_level = b;
4352 else
4353 popclass ();
4355 inner = CP_DECL_CONTEXT (TREE_CODE (inner) == NAMESPACE_DECL ? inner : TYPE_NAME (inner));
4359 /* Do a pushlevel for class declarations. */
4361 void
4362 pushlevel_class (void)
4364 class_binding_level = begin_scope (sk_class, current_class_type);
4367 /* ...and a poplevel for class declarations. */
4369 void
4370 poplevel_class (void)
4372 cp_binding_level *level = class_binding_level;
4373 cp_class_binding *cb;
4374 size_t i;
4375 tree shadowed;
4377 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4378 gcc_assert (level != 0);
4380 /* If we're leaving a toplevel class, cache its binding level. */
4381 if (current_class_depth == 1)
4382 previous_class_level = level;
4383 for (shadowed = level->type_shadowed;
4384 shadowed;
4385 shadowed = TREE_CHAIN (shadowed))
4386 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (shadowed), TREE_VALUE (shadowed));
4388 /* Remove the bindings for all of the class-level declarations. */
4389 if (level->class_shadowed)
4391 FOR_EACH_VEC_ELT (*level->class_shadowed, i, cb)
4393 IDENTIFIER_BINDING (cb->identifier) = cb->base->previous;
4394 cxx_binding_free (cb->base);
4396 ggc_free (level->class_shadowed);
4397 level->class_shadowed = NULL;
4400 /* Now, pop out of the binding level which we created up in the
4401 `pushlevel_class' routine. */
4402 gcc_assert (current_binding_level == level);
4403 leave_scope ();
4404 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4407 /* Set INHERITED_VALUE_BINDING_P on BINDING to true or false, as
4408 appropriate. DECL is the value to which a name has just been
4409 bound. CLASS_TYPE is the class in which the lookup occurred. */
4411 static void
4412 set_inherited_value_binding_p (cxx_binding *binding, tree decl,
4413 tree class_type)
4415 if (binding->value == decl && TREE_CODE (decl) != TREE_LIST)
4417 tree context;
4419 if (TREE_CODE (decl) == OVERLOAD)
4420 context = ovl_scope (decl);
4421 else
4423 gcc_assert (DECL_P (decl));
4424 context = context_for_name_lookup (decl);
4427 if (is_properly_derived_from (class_type, context))
4428 INHERITED_VALUE_BINDING_P (binding) = 1;
4429 else
4430 INHERITED_VALUE_BINDING_P (binding) = 0;
4432 else if (binding->value == decl)
4433 /* We only encounter a TREE_LIST when there is an ambiguity in the
4434 base classes. Such an ambiguity can be overridden by a
4435 definition in this class. */
4436 INHERITED_VALUE_BINDING_P (binding) = 1;
4437 else
4438 INHERITED_VALUE_BINDING_P (binding) = 0;
4441 /* Make the declaration of X appear in CLASS scope. */
4443 bool
4444 pushdecl_class_level (tree x)
4446 bool is_valid = true;
4447 bool subtime;
4449 /* Do nothing if we're adding to an outer lambda closure type,
4450 outer_binding will add it later if it's needed. */
4451 if (current_class_type != class_binding_level->this_entity)
4452 return true;
4454 subtime = timevar_cond_start (TV_NAME_LOOKUP);
4455 /* Get the name of X. */
4456 tree name = OVL_NAME (x);
4458 if (name)
4460 is_valid = push_class_level_binding (name, x);
4461 if (TREE_CODE (x) == TYPE_DECL)
4462 set_identifier_type_value (name, x);
4464 else if (ANON_AGGR_TYPE_P (TREE_TYPE (x)))
4466 /* If X is an anonymous aggregate, all of its members are
4467 treated as if they were members of the class containing the
4468 aggregate, for naming purposes. */
4469 tree f;
4471 for (f = TYPE_FIELDS (TREE_TYPE (x)); f; f = DECL_CHAIN (f))
4473 location_t save_location = input_location;
4474 input_location = DECL_SOURCE_LOCATION (f);
4475 if (!pushdecl_class_level (f))
4476 is_valid = false;
4477 input_location = save_location;
4480 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4481 return is_valid;
4484 /* Return the BINDING (if any) for NAME in SCOPE, which is a class
4485 scope. If the value returned is non-NULL, and the PREVIOUS field
4486 is not set, callers must set the PREVIOUS field explicitly. */
4488 static cxx_binding *
4489 get_class_binding (tree name, cp_binding_level *scope)
4491 tree class_type;
4492 tree type_binding;
4493 tree value_binding;
4494 cxx_binding *binding;
4496 class_type = scope->this_entity;
4498 /* Get the type binding. */
4499 type_binding = lookup_member (class_type, name,
4500 /*protect=*/2, /*want_type=*/true,
4501 tf_warning_or_error);
4502 /* Get the value binding. */
4503 value_binding = lookup_member (class_type, name,
4504 /*protect=*/2, /*want_type=*/false,
4505 tf_warning_or_error);
4507 if (value_binding
4508 && (TREE_CODE (value_binding) == TYPE_DECL
4509 || DECL_CLASS_TEMPLATE_P (value_binding)
4510 || (TREE_CODE (value_binding) == TREE_LIST
4511 && TREE_TYPE (value_binding) == error_mark_node
4512 && (TREE_CODE (TREE_VALUE (value_binding))
4513 == TYPE_DECL))))
4514 /* We found a type binding, even when looking for a non-type
4515 binding. This means that we already processed this binding
4516 above. */
4518 else if (value_binding)
4520 if (TREE_CODE (value_binding) == TREE_LIST
4521 && TREE_TYPE (value_binding) == error_mark_node)
4522 /* NAME is ambiguous. */
4524 else if (BASELINK_P (value_binding))
4525 /* NAME is some overloaded functions. */
4526 value_binding = BASELINK_FUNCTIONS (value_binding);
4529 /* If we found either a type binding or a value binding, create a
4530 new binding object. */
4531 if (type_binding || value_binding)
4533 binding = new_class_binding (name,
4534 value_binding,
4535 type_binding,
4536 scope);
4537 /* This is a class-scope binding, not a block-scope binding. */
4538 LOCAL_BINDING_P (binding) = 0;
4539 set_inherited_value_binding_p (binding, value_binding, class_type);
4541 else
4542 binding = NULL;
4544 return binding;
4547 /* Make the declaration(s) of X appear in CLASS scope under the name
4548 NAME. Returns true if the binding is valid. */
4550 static bool
4551 push_class_level_binding_1 (tree name, tree x)
4553 cxx_binding *binding;
4554 tree decl = x;
4555 bool ok;
4557 /* The class_binding_level will be NULL if x is a template
4558 parameter name in a member template. */
4559 if (!class_binding_level)
4560 return true;
4562 if (name == error_mark_node)
4563 return false;
4565 /* Can happen for an erroneous declaration (c++/60384). */
4566 if (!identifier_p (name))
4568 gcc_assert (errorcount || sorrycount);
4569 return false;
4572 /* Check for invalid member names. But don't worry about a default
4573 argument-scope lambda being pushed after the class is complete. */
4574 gcc_assert (TYPE_BEING_DEFINED (current_class_type)
4575 || LAMBDA_TYPE_P (TREE_TYPE (decl)));
4576 /* Check that we're pushing into the right binding level. */
4577 gcc_assert (current_class_type == class_binding_level->this_entity);
4579 /* We could have been passed a tree list if this is an ambiguous
4580 declaration. If so, pull the declaration out because
4581 check_template_shadow will not handle a TREE_LIST. */
4582 if (TREE_CODE (decl) == TREE_LIST
4583 && TREE_TYPE (decl) == error_mark_node)
4584 decl = TREE_VALUE (decl);
4586 if (!check_template_shadow (decl))
4587 return false;
4589 /* [class.mem]
4591 If T is the name of a class, then each of the following shall
4592 have a name different from T:
4594 -- every static data member of class T;
4596 -- every member of class T that is itself a type;
4598 -- every enumerator of every member of class T that is an
4599 enumerated type;
4601 -- every member of every anonymous union that is a member of
4602 class T.
4604 (Non-static data members were also forbidden to have the same
4605 name as T until TC1.) */
4606 if ((VAR_P (x)
4607 || TREE_CODE (x) == CONST_DECL
4608 || (TREE_CODE (x) == TYPE_DECL
4609 && !DECL_SELF_REFERENCE_P (x))
4610 /* A data member of an anonymous union. */
4611 || (TREE_CODE (x) == FIELD_DECL
4612 && DECL_CONTEXT (x) != current_class_type))
4613 && DECL_NAME (x) == DECL_NAME (TYPE_NAME (current_class_type)))
4615 tree scope = context_for_name_lookup (x);
4616 if (TYPE_P (scope) && same_type_p (scope, current_class_type))
4618 error ("%qD has the same name as the class in which it is "
4619 "declared",
4621 return false;
4625 /* Get the current binding for NAME in this class, if any. */
4626 binding = IDENTIFIER_BINDING (name);
4627 if (!binding || binding->scope != class_binding_level)
4629 binding = get_class_binding (name, class_binding_level);
4630 /* If a new binding was created, put it at the front of the
4631 IDENTIFIER_BINDING list. */
4632 if (binding)
4634 binding->previous = IDENTIFIER_BINDING (name);
4635 IDENTIFIER_BINDING (name) = binding;
4639 /* If there is already a binding, then we may need to update the
4640 current value. */
4641 if (binding && binding->value)
4643 tree bval = binding->value;
4644 tree old_decl = NULL_TREE;
4645 tree target_decl = strip_using_decl (decl);
4646 tree target_bval = strip_using_decl (bval);
4648 if (INHERITED_VALUE_BINDING_P (binding))
4650 /* If the old binding was from a base class, and was for a
4651 tag name, slide it over to make room for the new binding.
4652 The old binding is still visible if explicitly qualified
4653 with a class-key. */
4654 if (TREE_CODE (target_bval) == TYPE_DECL
4655 && DECL_ARTIFICIAL (target_bval)
4656 && !(TREE_CODE (target_decl) == TYPE_DECL
4657 && DECL_ARTIFICIAL (target_decl)))
4659 old_decl = binding->type;
4660 binding->type = bval;
4661 binding->value = NULL_TREE;
4662 INHERITED_VALUE_BINDING_P (binding) = 0;
4664 else
4666 old_decl = bval;
4667 /* Any inherited type declaration is hidden by the type
4668 declaration in the derived class. */
4669 if (TREE_CODE (target_decl) == TYPE_DECL
4670 && DECL_ARTIFICIAL (target_decl))
4671 binding->type = NULL_TREE;
4674 else if (TREE_CODE (target_decl) == OVERLOAD
4675 && OVL_P (target_bval))
4676 old_decl = bval;
4677 else if (TREE_CODE (decl) == USING_DECL
4678 && TREE_CODE (bval) == USING_DECL
4679 && same_type_p (USING_DECL_SCOPE (decl),
4680 USING_DECL_SCOPE (bval)))
4681 /* This is a using redeclaration that will be diagnosed later
4682 in supplement_binding */
4684 else if (TREE_CODE (decl) == USING_DECL
4685 && TREE_CODE (bval) == USING_DECL
4686 && DECL_DEPENDENT_P (decl)
4687 && DECL_DEPENDENT_P (bval))
4688 return true;
4689 else if (TREE_CODE (decl) == USING_DECL
4690 && OVL_P (target_bval))
4691 old_decl = bval;
4692 else if (TREE_CODE (bval) == USING_DECL
4693 && OVL_P (target_decl))
4694 return true;
4696 if (old_decl && binding->scope == class_binding_level)
4698 binding->value = x;
4699 /* It is always safe to clear INHERITED_VALUE_BINDING_P
4700 here. This function is only used to register bindings
4701 from with the class definition itself. */
4702 INHERITED_VALUE_BINDING_P (binding) = 0;
4703 return true;
4707 /* Note that we declared this value so that we can issue an error if
4708 this is an invalid redeclaration of a name already used for some
4709 other purpose. */
4710 note_name_declared_in_class (name, decl);
4712 /* If we didn't replace an existing binding, put the binding on the
4713 stack of bindings for the identifier, and update the shadowed
4714 list. */
4715 if (binding && binding->scope == class_binding_level)
4716 /* Supplement the existing binding. */
4717 ok = supplement_binding (binding, decl);
4718 else
4720 /* Create a new binding. */
4721 push_binding (name, decl, class_binding_level);
4722 ok = true;
4725 return ok;
4728 /* Wrapper for push_class_level_binding_1. */
4730 bool
4731 push_class_level_binding (tree name, tree x)
4733 bool ret;
4734 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4735 ret = push_class_level_binding_1 (name, x);
4736 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4737 return ret;
4740 /* Process "using SCOPE::NAME" in a class scope. Return the
4741 USING_DECL created. */
4743 tree
4744 do_class_using_decl (tree scope, tree name)
4746 if (name == error_mark_node)
4747 return NULL_TREE;
4749 if (!scope || !TYPE_P (scope))
4751 error ("using-declaration for non-member at class scope");
4752 return NULL_TREE;
4755 /* Make sure the name is not invalid */
4756 if (TREE_CODE (name) == BIT_NOT_EXPR)
4758 error ("%<%T::%D%> names destructor", scope, name);
4759 return NULL_TREE;
4762 /* Using T::T declares inheriting ctors, even if T is a typedef. */
4763 if (MAYBE_CLASS_TYPE_P (scope)
4764 && (name == TYPE_IDENTIFIER (scope)
4765 || constructor_name_p (name, scope)))
4767 maybe_warn_cpp0x (CPP0X_INHERITING_CTORS);
4768 name = ctor_identifier;
4769 CLASSTYPE_NON_AGGREGATE (current_class_type) = true;
4772 /* Cannot introduce a constructor name. */
4773 if (constructor_name_p (name, current_class_type))
4775 error ("%<%T::%D%> names constructor in %qT",
4776 scope, name, current_class_type);
4777 return NULL_TREE;
4780 /* From [namespace.udecl]:
4782 A using-declaration used as a member-declaration shall refer to a
4783 member of a base class of the class being defined.
4785 In general, we cannot check this constraint in a template because
4786 we do not know the entire set of base classes of the current
4787 class type. Morover, if SCOPE is dependent, it might match a
4788 non-dependent base. */
4790 tree decl = NULL_TREE;
4791 if (!dependent_scope_p (scope))
4793 base_kind b_kind;
4794 tree binfo = lookup_base (current_class_type, scope, ba_any, &b_kind,
4795 tf_warning_or_error);
4796 if (b_kind < bk_proper_base)
4798 /* If there are dependent bases, scope might resolve at
4799 instantiation time, even if it isn't exactly one of the
4800 dependent bases. */
4801 if (b_kind == bk_same_type || !any_dependent_bases_p ())
4803 error_not_base_type (scope, current_class_type);
4804 return NULL_TREE;
4807 else if (name == ctor_identifier && !binfo_direct_p (binfo))
4809 error ("cannot inherit constructors from indirect base %qT", scope);
4810 return NULL_TREE;
4812 else if (!IDENTIFIER_CONV_OP_P (name)
4813 || !dependent_type_p (TREE_TYPE (name)))
4815 decl = lookup_member (binfo, name, 0, false, tf_warning_or_error);
4816 if (!decl)
4818 error ("no members matching %<%T::%D%> in %q#T", scope, name,
4819 scope);
4820 return NULL_TREE;
4823 /* The binfo from which the functions came does not matter. */
4824 if (BASELINK_P (decl))
4825 decl = BASELINK_FUNCTIONS (decl);
4829 tree value = build_lang_decl (USING_DECL, name, NULL_TREE);
4830 USING_DECL_DECLS (value) = decl;
4831 USING_DECL_SCOPE (value) = scope;
4832 DECL_DEPENDENT_P (value) = !decl;
4834 return value;
4838 /* Return the binding for NAME in NS. If NS is NULL, look in
4839 global_namespace. */
4841 tree
4842 get_namespace_binding (tree ns, tree name)
4844 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4845 if (!ns)
4846 ns = global_namespace;
4847 gcc_checking_assert (!DECL_NAMESPACE_ALIAS (ns));
4848 tree ret = find_namespace_value (ns, name);
4849 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4850 return ret;
4853 /* Push internal DECL into the global namespace. Does not do the
4854 full overload fn handling and does not add it to the list of things
4855 in the namespace. */
4857 void
4858 set_global_binding (tree decl)
4860 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4862 tree *slot = find_namespace_slot (global_namespace, DECL_NAME (decl), true);
4864 if (*slot)
4865 /* The user's placed something in the implementor's namespace. */
4866 diagnose_name_conflict (decl, MAYBE_STAT_DECL (*slot));
4868 /* Force the binding, so compiler internals continue to work. */
4869 *slot = decl;
4871 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4874 /* Set the context of a declaration to scope. Complain if we are not
4875 outside scope. */
4877 void
4878 set_decl_namespace (tree decl, tree scope, bool friendp)
4880 /* Get rid of namespace aliases. */
4881 scope = ORIGINAL_NAMESPACE (scope);
4883 /* It is ok for friends to be qualified in parallel space. */
4884 if (!friendp && !is_nested_namespace (current_namespace, scope))
4885 error ("declaration of %qD not in a namespace surrounding %qD",
4886 decl, scope);
4887 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
4889 /* See whether this has been declared in the namespace or inline
4890 children. */
4891 tree old = NULL_TREE;
4893 name_lookup lookup (DECL_NAME (decl), LOOKUP_HIDDEN);
4894 if (!lookup.search_qualified (scope, /*usings=*/false))
4895 /* No old declaration at all. */
4896 goto not_found;
4897 old = lookup.value;
4900 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
4901 if (TREE_CODE (old) == TREE_LIST)
4903 ambiguous:
4904 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
4905 error ("reference to %qD is ambiguous", decl);
4906 print_candidates (old);
4907 return;
4910 if (!DECL_DECLARES_FUNCTION_P (decl))
4912 /* Don't compare non-function decls with decls_match here, since
4913 it can't check for the correct constness at this
4914 point. pushdecl will find those errors later. */
4916 /* We might have found it in an inline namespace child of SCOPE. */
4917 if (TREE_CODE (decl) == TREE_CODE (old))
4918 DECL_CONTEXT (decl) = DECL_CONTEXT (old);
4920 found:
4921 /* Writing "N::i" to declare something directly in "N" is invalid. */
4922 if (CP_DECL_CONTEXT (decl) == current_namespace
4923 && at_namespace_scope_p ())
4924 error ("explicit qualification in declaration of %qD", decl);
4925 return;
4928 /* Since decl is a function, old should contain a function decl. */
4929 if (!OVL_P (old))
4930 goto not_found;
4932 /* We handle these in check_explicit_instantiation_namespace. */
4933 if (processing_explicit_instantiation)
4934 return;
4935 if (processing_template_decl || processing_specialization)
4936 /* We have not yet called push_template_decl to turn a
4937 FUNCTION_DECL into a TEMPLATE_DECL, so the declarations won't
4938 match. But, we'll check later, when we construct the
4939 template. */
4940 return;
4941 /* Instantiations or specializations of templates may be declared as
4942 friends in any namespace. */
4943 if (friendp && DECL_USE_TEMPLATE (decl))
4944 return;
4946 tree found;
4947 found = NULL_TREE;
4949 for (lkp_iterator iter (old); iter; ++iter)
4951 if (iter.using_p ())
4952 continue;
4954 tree ofn = *iter;
4956 /* Adjust DECL_CONTEXT first so decls_match will return true
4957 if DECL will match a declaration in an inline namespace. */
4958 DECL_CONTEXT (decl) = DECL_CONTEXT (ofn);
4959 if (decls_match (decl, ofn))
4961 if (found)
4963 /* We found more than one matching declaration. */
4964 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
4965 goto ambiguous;
4967 found = ofn;
4971 if (found)
4973 if (DECL_HIDDEN_FRIEND_P (found))
4975 pedwarn (DECL_SOURCE_LOCATION (decl), 0,
4976 "%qD has not been declared within %qD", decl, scope);
4977 inform (DECL_SOURCE_LOCATION (found),
4978 "only here as a %<friend%>");
4980 DECL_CONTEXT (decl) = DECL_CONTEXT (found);
4981 goto found;
4984 not_found:
4985 /* It didn't work, go back to the explicit scope. */
4986 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
4987 error ("%qD should have been declared inside %qD", decl, scope);
4990 /* Return the namespace where the current declaration is declared. */
4992 tree
4993 current_decl_namespace (void)
4995 tree result;
4996 /* If we have been pushed into a different namespace, use it. */
4997 if (!vec_safe_is_empty (decl_namespace_list))
4998 return decl_namespace_list->last ();
5000 if (current_class_type)
5001 result = decl_namespace_context (current_class_type);
5002 else if (current_function_decl)
5003 result = decl_namespace_context (current_function_decl);
5004 else
5005 result = current_namespace;
5006 return result;
5009 /* Process any ATTRIBUTES on a namespace definition. Returns true if
5010 attribute visibility is seen. */
5012 bool
5013 handle_namespace_attrs (tree ns, tree attributes)
5015 tree d;
5016 bool saw_vis = false;
5018 for (d = attributes; d; d = TREE_CHAIN (d))
5020 tree name = get_attribute_name (d);
5021 tree args = TREE_VALUE (d);
5023 if (is_attribute_p ("visibility", name))
5025 /* attribute visibility is a property of the syntactic block
5026 rather than the namespace as a whole, so we don't touch the
5027 NAMESPACE_DECL at all. */
5028 tree x = args ? TREE_VALUE (args) : NULL_TREE;
5029 if (x == NULL_TREE || TREE_CODE (x) != STRING_CST || TREE_CHAIN (args))
5031 warning (OPT_Wattributes,
5032 "%qD attribute requires a single NTBS argument",
5033 name);
5034 continue;
5037 if (!TREE_PUBLIC (ns))
5038 warning (OPT_Wattributes,
5039 "%qD attribute is meaningless since members of the "
5040 "anonymous namespace get local symbols", name);
5042 push_visibility (TREE_STRING_POINTER (x), 1);
5043 saw_vis = true;
5045 else if (is_attribute_p ("abi_tag", name))
5047 if (!DECL_NAME (ns))
5049 warning (OPT_Wattributes, "ignoring %qD attribute on anonymous "
5050 "namespace", name);
5051 continue;
5053 if (!DECL_NAMESPACE_INLINE_P (ns))
5055 warning (OPT_Wattributes, "ignoring %qD attribute on non-inline "
5056 "namespace", name);
5057 continue;
5059 if (!args)
5061 tree dn = DECL_NAME (ns);
5062 args = build_string (IDENTIFIER_LENGTH (dn) + 1,
5063 IDENTIFIER_POINTER (dn));
5064 TREE_TYPE (args) = char_array_type_node;
5065 args = fix_string_type (args);
5066 args = build_tree_list (NULL_TREE, args);
5068 if (check_abi_tag_args (args, name))
5069 DECL_ATTRIBUTES (ns) = tree_cons (name, args,
5070 DECL_ATTRIBUTES (ns));
5072 else
5074 warning (OPT_Wattributes, "%qD attribute directive ignored",
5075 name);
5076 continue;
5080 return saw_vis;
5083 /* Temporarily set the namespace for the current declaration. */
5085 void
5086 push_decl_namespace (tree decl)
5088 if (TREE_CODE (decl) != NAMESPACE_DECL)
5089 decl = decl_namespace_context (decl);
5090 vec_safe_push (decl_namespace_list, ORIGINAL_NAMESPACE (decl));
5093 /* [namespace.memdef]/2 */
5095 void
5096 pop_decl_namespace (void)
5098 decl_namespace_list->pop ();
5101 /* Process a namespace-alias declaration. */
5103 void
5104 do_namespace_alias (tree alias, tree name_space)
5106 if (name_space == error_mark_node)
5107 return;
5109 gcc_assert (TREE_CODE (name_space) == NAMESPACE_DECL);
5111 name_space = ORIGINAL_NAMESPACE (name_space);
5113 /* Build the alias. */
5114 alias = build_lang_decl (NAMESPACE_DECL, alias, void_type_node);
5115 DECL_NAMESPACE_ALIAS (alias) = name_space;
5116 DECL_EXTERNAL (alias) = 1;
5117 DECL_CONTEXT (alias) = FROB_CONTEXT (current_scope ());
5118 pushdecl (alias);
5120 /* Emit debug info for namespace alias. */
5121 if (!building_stmt_list_p ())
5122 (*debug_hooks->early_global_decl) (alias);
5125 /* Like pushdecl, only it places X in the current namespace,
5126 if appropriate. */
5128 tree
5129 pushdecl_namespace_level (tree x, bool is_friend)
5131 cp_binding_level *b = current_binding_level;
5132 tree t;
5134 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
5135 t = do_pushdecl_with_scope
5136 (x, NAMESPACE_LEVEL (current_namespace), is_friend);
5138 /* Now, the type_shadowed stack may screw us. Munge it so it does
5139 what we want. */
5140 if (TREE_CODE (t) == TYPE_DECL)
5142 tree name = DECL_NAME (t);
5143 tree newval;
5144 tree *ptr = (tree *)0;
5145 for (; !global_scope_p (b); b = b->level_chain)
5147 tree shadowed = b->type_shadowed;
5148 for (; shadowed; shadowed = TREE_CHAIN (shadowed))
5149 if (TREE_PURPOSE (shadowed) == name)
5151 ptr = &TREE_VALUE (shadowed);
5152 /* Can't break out of the loop here because sometimes
5153 a binding level will have duplicate bindings for
5154 PT names. It's gross, but I haven't time to fix it. */
5157 newval = TREE_TYPE (t);
5158 if (ptr == (tree *)0)
5160 /* @@ This shouldn't be needed. My test case "zstring.cc" trips
5161 up here if this is changed to an assertion. --KR */
5162 SET_IDENTIFIER_TYPE_VALUE (name, t);
5164 else
5166 *ptr = newval;
5169 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
5170 return t;
5173 /* Process a using-declaration appearing in namespace scope. */
5175 void
5176 finish_namespace_using_decl (tree decl, tree scope, tree name)
5178 tree orig_decl = decl;
5180 gcc_checking_assert (current_binding_level->kind == sk_namespace
5181 && !processing_template_decl);
5182 decl = validate_nonmember_using_decl (decl, scope, name);
5183 if (decl == NULL_TREE)
5184 return;
5186 tree *slot = find_namespace_slot (current_namespace, name, true);
5187 tree val = slot ? MAYBE_STAT_DECL (*slot) : NULL_TREE;
5188 tree type = slot ? MAYBE_STAT_TYPE (*slot) : NULL_TREE;
5189 do_nonmember_using_decl (scope, name, &val, &type);
5190 if (STAT_HACK_P (*slot))
5192 STAT_DECL (*slot) = val;
5193 STAT_TYPE (*slot) = type;
5195 else if (type)
5196 *slot = stat_hack (val, type);
5197 else
5198 *slot = val;
5200 /* Emit debug info. */
5201 cp_emit_debug_info_for_using (orig_decl, current_namespace);
5204 /* Process a using-declaration at function scope. */
5206 void
5207 finish_local_using_decl (tree decl, tree scope, tree name)
5209 tree orig_decl = decl;
5211 gcc_checking_assert (current_binding_level->kind != sk_class
5212 && current_binding_level->kind != sk_namespace);
5213 decl = validate_nonmember_using_decl (decl, scope, name);
5214 if (decl == NULL_TREE)
5215 return;
5217 add_decl_expr (decl);
5219 cxx_binding *binding = find_local_binding (current_binding_level, name);
5220 tree value = binding ? binding->value : NULL_TREE;
5221 tree type = binding ? binding->type : NULL_TREE;
5223 do_nonmember_using_decl (scope, name, &value, &type);
5225 if (!value)
5227 else if (binding && value == binding->value)
5229 else if (binding && binding->value && TREE_CODE (value) == OVERLOAD)
5231 update_local_overload (IDENTIFIER_BINDING (name), value);
5232 IDENTIFIER_BINDING (name)->value = value;
5234 else
5235 /* Install the new binding. */
5236 push_local_binding (name, value, true);
5238 if (!type)
5240 else if (binding && type == binding->type)
5242 else
5244 push_local_binding (name, type, true);
5245 set_identifier_type_value (name, type);
5248 /* Emit debug info. */
5249 if (!processing_template_decl)
5250 cp_emit_debug_info_for_using (orig_decl, current_scope ());
5253 /* Return the declarations that are members of the namespace NS. */
5255 tree
5256 cp_namespace_decls (tree ns)
5258 return NAMESPACE_LEVEL (ns)->names;
5261 /* Combine prefer_type and namespaces_only into flags. */
5263 static int
5264 lookup_flags (int prefer_type, int namespaces_only)
5266 if (namespaces_only)
5267 return LOOKUP_PREFER_NAMESPACES;
5268 if (prefer_type > 1)
5269 return LOOKUP_PREFER_TYPES;
5270 if (prefer_type > 0)
5271 return LOOKUP_PREFER_BOTH;
5272 return 0;
5275 /* Given a lookup that returned VAL, use FLAGS to decide if we want to
5276 ignore it or not. Subroutine of lookup_name_real and
5277 lookup_type_scope. */
5279 static bool
5280 qualify_lookup (tree val, int flags)
5282 if (val == NULL_TREE)
5283 return false;
5284 if ((flags & LOOKUP_PREFER_NAMESPACES) && TREE_CODE (val) == NAMESPACE_DECL)
5285 return true;
5286 if (flags & LOOKUP_PREFER_TYPES)
5288 tree target_val = strip_using_decl (val);
5289 if (TREE_CODE (target_val) == TYPE_DECL
5290 || TREE_CODE (target_val) == TEMPLATE_DECL)
5291 return true;
5293 if (flags & (LOOKUP_PREFER_NAMESPACES | LOOKUP_PREFER_TYPES))
5294 return false;
5295 /* Look through lambda things that we shouldn't be able to see. */
5296 if (!(flags & LOOKUP_HIDDEN) && is_lambda_ignored_entity (val))
5297 return false;
5298 return true;
5301 /* Suggest alternatives for NAME, an IDENTIFIER_NODE for which name
5302 lookup failed. Search through all available namespaces and print out
5303 possible candidates. If no exact matches are found, and
5304 SUGGEST_MISSPELLINGS is true, then also look for near-matches and
5305 suggest the best near-match, if there is one. */
5307 void
5308 suggest_alternatives_for (location_t location, tree name,
5309 bool suggest_misspellings)
5311 vec<tree> candidates = vNULL;
5312 vec<tree> worklist = vNULL;
5313 unsigned limit = PARAM_VALUE (CXX_MAX_NAMESPACES_FOR_DIAGNOSTIC_HELP);
5314 bool limited = false;
5316 /* Breadth-first search of namespaces. Up to limit namespaces
5317 searched (limit zero == unlimited). */
5318 worklist.safe_push (global_namespace);
5319 for (unsigned ix = 0; ix != worklist.length (); ix++)
5321 tree ns = worklist[ix];
5322 name_lookup lookup (name);
5324 if (lookup.search_qualified (ns, false))
5325 candidates.safe_push (lookup.value);
5327 if (!limited)
5329 /* Look for child namespaces. We have to do this
5330 indirectly because they are chained in reverse order,
5331 which is confusing to the user. */
5332 vec<tree> children = vNULL;
5334 for (tree decl = NAMESPACE_LEVEL (ns)->names;
5335 decl; decl = TREE_CHAIN (decl))
5336 if (TREE_CODE (decl) == NAMESPACE_DECL
5337 && !DECL_NAMESPACE_ALIAS (decl)
5338 && !DECL_NAMESPACE_INLINE_P (decl))
5339 children.safe_push (decl);
5341 while (!limited && !children.is_empty ())
5343 if (worklist.length () == limit)
5345 /* Unconditionally warn that the search was truncated. */
5346 inform (location,
5347 "maximum limit of %d namespaces searched for %qE",
5348 limit, name);
5349 limited = true;
5351 else
5352 worklist.safe_push (children.pop ());
5354 children.release ();
5357 worklist.release ();
5359 if (candidates.length ())
5361 inform_n (location, candidates.length (),
5362 "suggested alternative:",
5363 "suggested alternatives:");
5364 for (unsigned ix = 0; ix != candidates.length (); ix++)
5366 tree val = candidates[ix];
5368 inform (location_of (val), " %qE", val);
5370 candidates.release ();
5372 else if (!suggest_misspellings)
5374 else if (const char *fuzzy = lookup_name_fuzzy (name, FUZZY_LOOKUP_NAME))
5376 /* Show a spelling correction. */
5377 gcc_rich_location richloc (location);
5379 richloc.add_fixit_replace (fuzzy);
5380 inform (&richloc, "suggested alternative: %qs", fuzzy);
5384 /* Subroutine of maybe_suggest_missing_header for handling unrecognized names
5385 for some of the most common names within "std::".
5386 Given non-NULL NAME, a name for lookup within "std::", return the header
5387 name defining it within the C++ Standard Library (with '<' and '>'),
5388 or NULL. */
5390 static const char *
5391 get_std_name_hint (const char *name)
5393 struct std_name_hint
5395 const char *name;
5396 const char *header;
5398 static const std_name_hint hints[] = {
5399 /* <array>. */
5400 {"array", "<array>"}, // C++11
5401 /* <deque>. */
5402 {"deque", "<deque>"},
5403 /* <forward_list>. */
5404 {"forward_list", "<forward_list>"}, // C++11
5405 /* <fstream>. */
5406 {"basic_filebuf", "<fstream>"},
5407 {"basic_ifstream", "<fstream>"},
5408 {"basic_ofstream", "<fstream>"},
5409 {"basic_fstream", "<fstream>"},
5410 /* <iostream>. */
5411 {"cin", "<iostream>"},
5412 {"cout", "<iostream>"},
5413 {"cerr", "<iostream>"},
5414 {"clog", "<iostream>"},
5415 {"wcin", "<iostream>"},
5416 {"wcout", "<iostream>"},
5417 {"wclog", "<iostream>"},
5418 /* <list>. */
5419 {"list", "<list>"},
5420 /* <map>. */
5421 {"map", "<map>"},
5422 {"multimap", "<map>"},
5423 /* <queue>. */
5424 {"queue", "<queue>"},
5425 {"priority_queue", "<queue>"},
5426 /* <ostream>. */
5427 {"ostream", "<ostream>"},
5428 {"wostream", "<ostream>"},
5429 {"ends", "<ostream>"},
5430 {"flush", "<ostream>"},
5431 {"endl", "<ostream>"},
5432 /* <set>. */
5433 {"set", "<set>"},
5434 {"multiset", "<set>"},
5435 /* <sstream>. */
5436 {"basic_stringbuf", "<sstream>"},
5437 {"basic_istringstream", "<sstream>"},
5438 {"basic_ostringstream", "<sstream>"},
5439 {"basic_stringstream", "<sstream>"},
5440 /* <stack>. */
5441 {"stack", "<stack>"},
5442 /* <string>. */
5443 {"string", "<string>"},
5444 {"wstring", "<string>"},
5445 {"u16string", "<string>"},
5446 {"u32string", "<string>"},
5447 /* <unordered_map>. */
5448 {"unordered_map", "<unordered_map>"}, // C++11
5449 {"unordered_multimap", "<unordered_map>"}, // C++11
5450 /* <unordered_set>. */
5451 {"unordered_set", "<unordered_set>"}, // C++11
5452 {"unordered_multiset", "<unordered_set>"}, // C++11
5453 /* <vector>. */
5454 {"vector", "<vector>"},
5456 const size_t num_hints = sizeof (hints) / sizeof (hints[0]);
5457 for (size_t i = 0; i < num_hints; i++)
5459 if (0 == strcmp (name, hints[i].name))
5460 return hints[i].header;
5462 return NULL;
5465 /* If SCOPE is the "std" namespace, then suggest pertinent header
5466 files for NAME at LOCATION.
5467 Return true iff a suggestion was offered. */
5469 static bool
5470 maybe_suggest_missing_header (location_t location, tree name, tree scope)
5472 if (scope == NULL_TREE)
5473 return false;
5474 if (TREE_CODE (scope) != NAMESPACE_DECL)
5475 return false;
5476 /* We only offer suggestions for the "std" namespace. */
5477 if (scope != std_node)
5478 return false;
5479 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
5481 const char *name_str = IDENTIFIER_POINTER (name);
5482 const char *header_hint = get_std_name_hint (name_str);
5483 if (!header_hint)
5484 return false;
5486 gcc_rich_location richloc (location);
5487 maybe_add_include_fixit (&richloc, header_hint);
5488 inform (&richloc,
5489 "%<std::%s%> is defined in header %qs;"
5490 " did you forget to %<#include %s%>?",
5491 name_str, header_hint, header_hint);
5492 return true;
5495 /* Look for alternatives for NAME, an IDENTIFIER_NODE for which name
5496 lookup failed within the explicitly provided SCOPE. Suggest the
5497 the best meaningful candidates (if any) as a fix-it hint.
5498 Return true iff a suggestion was provided. */
5500 bool
5501 suggest_alternative_in_explicit_scope (location_t location, tree name,
5502 tree scope)
5504 /* Resolve any namespace aliases. */
5505 scope = ORIGINAL_NAMESPACE (scope);
5507 if (maybe_suggest_missing_header (location, name, scope))
5508 return true;
5510 cp_binding_level *level = NAMESPACE_LEVEL (scope);
5512 best_match <tree, const char *> bm (name);
5513 consider_binding_level (name, bm, level, false, FUZZY_LOOKUP_NAME);
5515 /* See if we have a good suggesion for the user. */
5516 const char *fuzzy_name = bm.get_best_meaningful_candidate ();
5517 if (fuzzy_name)
5519 gcc_rich_location richloc (location);
5520 richloc.add_fixit_replace (fuzzy_name);
5521 inform (&richloc, "suggested alternative: %qs",
5522 fuzzy_name);
5523 return true;
5526 return false;
5529 /* Look up NAME (an IDENTIFIER_NODE) in SCOPE (either a NAMESPACE_DECL
5530 or a class TYPE).
5532 If PREFER_TYPE is > 0, we only return TYPE_DECLs or namespaces.
5533 If PREFER_TYPE is > 1, we only return TYPE_DECLs.
5535 Returns a DECL (or OVERLOAD, or BASELINK) representing the
5536 declaration found. If no suitable declaration can be found,
5537 ERROR_MARK_NODE is returned. If COMPLAIN is true and SCOPE is
5538 neither a class-type nor a namespace a diagnostic is issued. */
5540 tree
5541 lookup_qualified_name (tree scope, tree name, int prefer_type, bool complain,
5542 bool find_hidden)
5544 tree t = NULL_TREE;
5546 if (TREE_CODE (scope) == NAMESPACE_DECL)
5548 int flags = lookup_flags (prefer_type, /*namespaces_only*/false);
5549 if (find_hidden)
5550 flags |= LOOKUP_HIDDEN;
5551 name_lookup lookup (name, flags);
5553 if (qualified_namespace_lookup (scope, &lookup))
5554 t = lookup.value;
5556 else if (cxx_dialect != cxx98 && TREE_CODE (scope) == ENUMERAL_TYPE)
5557 t = lookup_enumerator (scope, name);
5558 else if (is_class_type (scope, complain))
5559 t = lookup_member (scope, name, 2, prefer_type, tf_warning_or_error);
5561 if (!t)
5562 return error_mark_node;
5563 return t;
5566 /* [namespace.qual]
5567 Accepts the NAME to lookup and its qualifying SCOPE.
5568 Returns the name/type pair found into the cxx_binding *RESULT,
5569 or false on error. */
5571 static bool
5572 qualified_namespace_lookup (tree scope, name_lookup *lookup)
5574 timevar_start (TV_NAME_LOOKUP);
5575 query_oracle (lookup->name);
5576 bool found = lookup->search_qualified (ORIGINAL_NAMESPACE (scope));
5577 timevar_stop (TV_NAME_LOOKUP);
5578 return found;
5581 /* Helper function for lookup_name_fuzzy.
5582 Traverse binding level LVL, looking for good name matches for NAME
5583 (and BM). */
5584 static void
5585 consider_binding_level (tree name, best_match <tree, const char *> &bm,
5586 cp_binding_level *lvl, bool look_within_fields,
5587 enum lookup_name_fuzzy_kind kind)
5589 if (look_within_fields)
5590 if (lvl->this_entity && TREE_CODE (lvl->this_entity) == RECORD_TYPE)
5592 tree type = lvl->this_entity;
5593 bool want_type_p = (kind == FUZZY_LOOKUP_TYPENAME);
5594 tree best_matching_field
5595 = lookup_member_fuzzy (type, name, want_type_p);
5596 if (best_matching_field)
5597 bm.consider (IDENTIFIER_POINTER (best_matching_field));
5600 for (tree t = lvl->names; t; t = TREE_CHAIN (t))
5602 tree d = t;
5604 /* OVERLOADs or decls from using declaration are wrapped into
5605 TREE_LIST. */
5606 if (TREE_CODE (d) == TREE_LIST)
5607 d = OVL_FIRST (TREE_VALUE (d));
5609 /* Don't use bindings from implicitly declared functions,
5610 as they were likely misspellings themselves. */
5611 if (TREE_TYPE (d) == error_mark_node)
5612 continue;
5614 /* Skip anticipated decls of builtin functions. */
5615 if (TREE_CODE (d) == FUNCTION_DECL
5616 && DECL_BUILT_IN (d)
5617 && DECL_ANTICIPATED (d))
5618 continue;
5620 if (tree name = DECL_NAME (d))
5621 /* Ignore internal names with spaces in them. */
5622 if (!strchr (IDENTIFIER_POINTER (name), ' '))
5623 bm.consider (IDENTIFIER_POINTER (name));
5627 /* Search for near-matches for NAME within the current bindings, and within
5628 macro names, returning the best match as a const char *, or NULL if
5629 no reasonable match is found. */
5631 const char *
5632 lookup_name_fuzzy (tree name, enum lookup_name_fuzzy_kind kind)
5634 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
5636 best_match <tree, const char *> bm (name);
5638 cp_binding_level *lvl;
5639 for (lvl = scope_chain->class_bindings; lvl; lvl = lvl->level_chain)
5640 consider_binding_level (name, bm, lvl, true, kind);
5642 for (lvl = current_binding_level; lvl; lvl = lvl->level_chain)
5643 consider_binding_level (name, bm, lvl, false, kind);
5645 /* Consider macros: if the user misspelled a macro name e.g. "SOME_MACRO"
5647 x = SOME_OTHER_MACRO (y);
5648 then "SOME_OTHER_MACRO" will survive to the frontend and show up
5649 as a misspelled identifier.
5651 Use the best distance so far so that a candidate is only set if
5652 a macro is better than anything so far. This allows early rejection
5653 (without calculating the edit distance) of macro names that must have
5654 distance >= bm.get_best_distance (), and means that we only get a
5655 non-NULL result for best_macro_match if it's better than any of
5656 the identifiers already checked. */
5657 best_macro_match bmm (name, bm.get_best_distance (), parse_in);
5658 cpp_hashnode *best_macro = bmm.get_best_meaningful_candidate ();
5659 /* If a macro is the closest so far to NAME, consider it. */
5660 if (best_macro)
5661 bm.consider ((const char *)best_macro->ident.str);
5663 /* Try the "starts_decl_specifier_p" keywords to detect
5664 "singed" vs "signed" typos. */
5665 for (unsigned i = 0; i < num_c_common_reswords; i++)
5667 const c_common_resword *resword = &c_common_reswords[i];
5669 if (kind == FUZZY_LOOKUP_TYPENAME)
5670 if (!cp_keyword_starts_decl_specifier_p (resword->rid))
5671 continue;
5673 tree resword_identifier = ridpointers [resword->rid];
5674 if (!resword_identifier)
5675 continue;
5676 gcc_assert (TREE_CODE (resword_identifier) == IDENTIFIER_NODE);
5678 /* Only consider reserved words that survived the
5679 filtering in init_reswords (e.g. for -std). */
5680 if (!IDENTIFIER_KEYWORD_P (resword_identifier))
5681 continue;
5683 bm.consider (IDENTIFIER_POINTER (resword_identifier));
5686 return bm.get_best_meaningful_candidate ();
5689 /* Subroutine of outer_binding.
5691 Returns TRUE if BINDING is a binding to a template parameter of
5692 SCOPE. In that case SCOPE is the scope of a primary template
5693 parameter -- in the sense of G++, i.e, a template that has its own
5694 template header.
5696 Returns FALSE otherwise. */
5698 static bool
5699 binding_to_template_parms_of_scope_p (cxx_binding *binding,
5700 cp_binding_level *scope)
5702 tree binding_value, tmpl, tinfo;
5703 int level;
5705 if (!binding || !scope || !scope->this_entity)
5706 return false;
5708 binding_value = binding->value ? binding->value : binding->type;
5709 tinfo = get_template_info (scope->this_entity);
5711 /* BINDING_VALUE must be a template parm. */
5712 if (binding_value == NULL_TREE
5713 || (!DECL_P (binding_value)
5714 || !DECL_TEMPLATE_PARM_P (binding_value)))
5715 return false;
5717 /* The level of BINDING_VALUE. */
5718 level =
5719 template_type_parameter_p (binding_value)
5720 ? TEMPLATE_PARM_LEVEL (TEMPLATE_TYPE_PARM_INDEX
5721 (TREE_TYPE (binding_value)))
5722 : TEMPLATE_PARM_LEVEL (DECL_INITIAL (binding_value));
5724 /* The template of the current scope, iff said scope is a primary
5725 template. */
5726 tmpl = (tinfo
5727 && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
5728 ? TI_TEMPLATE (tinfo)
5729 : NULL_TREE);
5731 /* If the level of the parm BINDING_VALUE equals the depth of TMPL,
5732 then BINDING_VALUE is a parameter of TMPL. */
5733 return (tmpl && level == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)));
5736 /* Return the innermost non-namespace binding for NAME from a scope
5737 containing BINDING, or, if BINDING is NULL, the current scope.
5738 Please note that for a given template, the template parameters are
5739 considered to be in the scope containing the current scope.
5740 If CLASS_P is false, then class bindings are ignored. */
5742 cxx_binding *
5743 outer_binding (tree name,
5744 cxx_binding *binding,
5745 bool class_p)
5747 cxx_binding *outer;
5748 cp_binding_level *scope;
5749 cp_binding_level *outer_scope;
5751 if (binding)
5753 scope = binding->scope->level_chain;
5754 outer = binding->previous;
5756 else
5758 scope = current_binding_level;
5759 outer = IDENTIFIER_BINDING (name);
5761 outer_scope = outer ? outer->scope : NULL;
5763 /* Because we create class bindings lazily, we might be missing a
5764 class binding for NAME. If there are any class binding levels
5765 between the LAST_BINDING_LEVEL and the scope in which OUTER was
5766 declared, we must lookup NAME in those class scopes. */
5767 if (class_p)
5768 while (scope && scope != outer_scope && scope->kind != sk_namespace)
5770 if (scope->kind == sk_class)
5772 cxx_binding *class_binding;
5774 class_binding = get_class_binding (name, scope);
5775 if (class_binding)
5777 /* Thread this new class-scope binding onto the
5778 IDENTIFIER_BINDING list so that future lookups
5779 find it quickly. */
5780 class_binding->previous = outer;
5781 if (binding)
5782 binding->previous = class_binding;
5783 else
5784 IDENTIFIER_BINDING (name) = class_binding;
5785 return class_binding;
5788 /* If we are in a member template, the template parms of the member
5789 template are considered to be inside the scope of the containing
5790 class, but within G++ the class bindings are all pushed between the
5791 template parms and the function body. So if the outer binding is
5792 a template parm for the current scope, return it now rather than
5793 look for a class binding. */
5794 if (outer_scope && outer_scope->kind == sk_template_parms
5795 && binding_to_template_parms_of_scope_p (outer, scope))
5796 return outer;
5798 scope = scope->level_chain;
5801 return outer;
5804 /* Return the innermost block-scope or class-scope value binding for
5805 NAME, or NULL_TREE if there is no such binding. */
5807 tree
5808 innermost_non_namespace_value (tree name)
5810 cxx_binding *binding;
5811 binding = outer_binding (name, /*binding=*/NULL, /*class_p=*/true);
5812 return binding ? binding->value : NULL_TREE;
5815 /* Look up NAME in the current binding level and its superiors in the
5816 namespace of variables, functions and typedefs. Return a ..._DECL
5817 node of some kind representing its definition if there is only one
5818 such declaration, or return a TREE_LIST with all the overloaded
5819 definitions if there are many, or return 0 if it is undefined.
5820 Hidden name, either friend declaration or built-in function, are
5821 not ignored.
5823 If PREFER_TYPE is > 0, we prefer TYPE_DECLs or namespaces.
5824 If PREFER_TYPE is > 1, we reject non-type decls (e.g. namespaces).
5825 Otherwise we prefer non-TYPE_DECLs.
5827 If NONCLASS is nonzero, bindings in class scopes are ignored. If
5828 BLOCK_P is false, bindings in block scopes are ignored. */
5830 static tree
5831 lookup_name_real_1 (tree name, int prefer_type, int nonclass, bool block_p,
5832 int namespaces_only, int flags)
5834 cxx_binding *iter;
5835 tree val = NULL_TREE;
5837 query_oracle (name);
5839 /* Conversion operators are handled specially because ordinary
5840 unqualified name lookup will not find template conversion
5841 operators. */
5842 if (IDENTIFIER_CONV_OP_P (name))
5844 cp_binding_level *level;
5846 for (level = current_binding_level;
5847 level && level->kind != sk_namespace;
5848 level = level->level_chain)
5850 tree class_type;
5851 tree operators;
5853 /* A conversion operator can only be declared in a class
5854 scope. */
5855 if (level->kind != sk_class)
5856 continue;
5858 /* Lookup the conversion operator in the class. */
5859 class_type = level->this_entity;
5860 operators = lookup_fnfields (class_type, name, /*protect=*/0);
5861 if (operators)
5862 return operators;
5865 return NULL_TREE;
5868 flags |= lookup_flags (prefer_type, namespaces_only);
5870 /* First, look in non-namespace scopes. */
5872 if (current_class_type == NULL_TREE)
5873 nonclass = 1;
5875 if (block_p || !nonclass)
5876 for (iter = outer_binding (name, NULL, !nonclass);
5877 iter;
5878 iter = outer_binding (name, iter, !nonclass))
5880 tree binding;
5882 /* Skip entities we don't want. */
5883 if (LOCAL_BINDING_P (iter) ? !block_p : nonclass)
5884 continue;
5886 /* If this is the kind of thing we're looking for, we're done. */
5887 if (qualify_lookup (iter->value, flags))
5888 binding = iter->value;
5889 else if ((flags & LOOKUP_PREFER_TYPES)
5890 && qualify_lookup (iter->type, flags))
5891 binding = iter->type;
5892 else
5893 binding = NULL_TREE;
5895 if (binding)
5897 if (TREE_CODE (binding) == TYPE_DECL && DECL_HIDDEN_P (binding))
5899 /* A non namespace-scope binding can only be hidden in the
5900 presence of a local class, due to friend declarations.
5902 In particular, consider:
5904 struct C;
5905 void f() {
5906 struct A {
5907 friend struct B;
5908 friend struct C;
5909 void g() {
5910 B* b; // error: B is hidden
5911 C* c; // OK, finds ::C
5914 B *b; // error: B is hidden
5915 C *c; // OK, finds ::C
5916 struct B {};
5917 B *bb; // OK
5920 The standard says that "B" is a local class in "f"
5921 (but not nested within "A") -- but that name lookup
5922 for "B" does not find this declaration until it is
5923 declared directly with "f".
5925 In particular:
5927 [class.friend]
5929 If a friend declaration appears in a local class and
5930 the name specified is an unqualified name, a prior
5931 declaration is looked up without considering scopes
5932 that are outside the innermost enclosing non-class
5933 scope. For a friend function declaration, if there is
5934 no prior declaration, the program is ill-formed. For a
5935 friend class declaration, if there is no prior
5936 declaration, the class that is specified belongs to the
5937 innermost enclosing non-class scope, but if it is
5938 subsequently referenced, its name is not found by name
5939 lookup until a matching declaration is provided in the
5940 innermost enclosing nonclass scope.
5942 So just keep looking for a non-hidden binding.
5944 gcc_assert (TREE_CODE (binding) == TYPE_DECL);
5945 continue;
5947 val = binding;
5948 break;
5952 /* Now lookup in namespace scopes. */
5953 if (!val)
5955 name_lookup lookup (name, flags);
5956 if (lookup.search_unqualified
5957 (current_decl_namespace (), current_binding_level))
5958 val = lookup.value;
5961 /* If we have a single function from a using decl, pull it out. */
5962 if (val && TREE_CODE (val) == OVERLOAD && !really_overloaded_fn (val))
5963 val = OVL_FUNCTION (val);
5965 return val;
5968 /* Wrapper for lookup_name_real_1. */
5970 tree
5971 lookup_name_real (tree name, int prefer_type, int nonclass, bool block_p,
5972 int namespaces_only, int flags)
5974 tree ret;
5975 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
5976 ret = lookup_name_real_1 (name, prefer_type, nonclass, block_p,
5977 namespaces_only, flags);
5978 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
5979 return ret;
5982 tree
5983 lookup_name_nonclass (tree name)
5985 return lookup_name_real (name, 0, 1, /*block_p=*/true, 0, 0);
5988 tree
5989 lookup_name (tree name)
5991 return lookup_name_real (name, 0, 0, /*block_p=*/true, 0, 0);
5994 tree
5995 lookup_name_prefer_type (tree name, int prefer_type)
5997 return lookup_name_real (name, prefer_type, 0, /*block_p=*/true, 0, 0);
6000 /* Look up NAME for type used in elaborated name specifier in
6001 the scopes given by SCOPE. SCOPE can be either TS_CURRENT or
6002 TS_WITHIN_ENCLOSING_NON_CLASS. Although not implied by the
6003 name, more scopes are checked if cleanup or template parameter
6004 scope is encountered.
6006 Unlike lookup_name_real, we make sure that NAME is actually
6007 declared in the desired scope, not from inheritance, nor using
6008 directive. For using declaration, there is DR138 still waiting
6009 to be resolved. Hidden name coming from an earlier friend
6010 declaration is also returned.
6012 A TYPE_DECL best matching the NAME is returned. Catching error
6013 and issuing diagnostics are caller's responsibility. */
6015 static tree
6016 lookup_type_scope_1 (tree name, tag_scope scope)
6018 cxx_binding *iter = NULL;
6019 tree val = NULL_TREE;
6020 cp_binding_level *level = NULL;
6022 /* Look in non-namespace scope first. */
6023 if (current_binding_level->kind != sk_namespace)
6024 iter = outer_binding (name, NULL, /*class_p=*/ true);
6025 for (; iter; iter = outer_binding (name, iter, /*class_p=*/ true))
6027 /* Check if this is the kind of thing we're looking for.
6028 If SCOPE is TS_CURRENT, also make sure it doesn't come from
6029 base class. For ITER->VALUE, we can simply use
6030 INHERITED_VALUE_BINDING_P. For ITER->TYPE, we have to use
6031 our own check.
6033 We check ITER->TYPE before ITER->VALUE in order to handle
6034 typedef struct C {} C;
6035 correctly. */
6037 if (qualify_lookup (iter->type, LOOKUP_PREFER_TYPES)
6038 && (scope != ts_current
6039 || LOCAL_BINDING_P (iter)
6040 || DECL_CONTEXT (iter->type) == iter->scope->this_entity))
6041 val = iter->type;
6042 else if ((scope != ts_current
6043 || !INHERITED_VALUE_BINDING_P (iter))
6044 && qualify_lookup (iter->value, LOOKUP_PREFER_TYPES))
6045 val = iter->value;
6047 if (val)
6048 break;
6051 /* Look in namespace scope. */
6052 if (val)
6053 level = iter->scope;
6054 else
6056 tree ns = current_decl_namespace ();
6058 if (tree *slot = find_namespace_slot (ns, name))
6060 /* If this is the kind of thing we're looking for, we're done. */
6061 if (tree type = MAYBE_STAT_TYPE (*slot))
6062 if (qualify_lookup (type, LOOKUP_PREFER_TYPES))
6063 val = type;
6064 if (!val)
6066 if (tree decl = MAYBE_STAT_DECL (*slot))
6067 if (qualify_lookup (decl, LOOKUP_PREFER_TYPES))
6068 val = decl;
6070 level = NAMESPACE_LEVEL (ns);
6074 /* Type found, check if it is in the allowed scopes, ignoring cleanup
6075 and template parameter scopes. */
6076 if (val)
6078 cp_binding_level *b = current_binding_level;
6079 while (b)
6081 if (level == b)
6082 return val;
6084 if (b->kind == sk_cleanup || b->kind == sk_template_parms
6085 || b->kind == sk_function_parms)
6086 b = b->level_chain;
6087 else if (b->kind == sk_class
6088 && scope == ts_within_enclosing_non_class)
6089 b = b->level_chain;
6090 else
6091 break;
6095 return NULL_TREE;
6098 /* Wrapper for lookup_type_scope_1. */
6100 tree
6101 lookup_type_scope (tree name, tag_scope scope)
6103 tree ret;
6104 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6105 ret = lookup_type_scope_1 (name, scope);
6106 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6107 return ret;
6110 /* Returns true iff DECL is a block-scope extern declaration of a function
6111 or variable. */
6113 bool
6114 is_local_extern (tree decl)
6116 cxx_binding *binding;
6118 /* For functions, this is easy. */
6119 if (TREE_CODE (decl) == FUNCTION_DECL)
6120 return DECL_LOCAL_FUNCTION_P (decl);
6122 if (!VAR_P (decl))
6123 return false;
6124 if (!current_function_decl)
6125 return false;
6127 /* For variables, this is not easy. We need to look at the binding stack
6128 for the identifier to see whether the decl we have is a local. */
6129 for (binding = IDENTIFIER_BINDING (DECL_NAME (decl));
6130 binding && binding->scope->kind != sk_namespace;
6131 binding = binding->previous)
6132 if (binding->value == decl)
6133 return LOCAL_BINDING_P (binding);
6135 return false;
6138 /* The type TYPE is being declared. If it is a class template, or a
6139 specialization of a class template, do any processing required and
6140 perform error-checking. If IS_FRIEND is nonzero, this TYPE is
6141 being declared a friend. B is the binding level at which this TYPE
6142 should be bound.
6144 Returns the TYPE_DECL for TYPE, which may have been altered by this
6145 processing. */
6147 static tree
6148 maybe_process_template_type_declaration (tree type, int is_friend,
6149 cp_binding_level *b)
6151 tree decl = TYPE_NAME (type);
6153 if (processing_template_parmlist)
6154 /* You can't declare a new template type in a template parameter
6155 list. But, you can declare a non-template type:
6157 template <class A*> struct S;
6159 is a forward-declaration of `A'. */
6161 else if (b->kind == sk_namespace
6162 && current_binding_level->kind != sk_namespace)
6163 /* If this new type is being injected into a containing scope,
6164 then it's not a template type. */
6166 else
6168 gcc_assert (MAYBE_CLASS_TYPE_P (type)
6169 || TREE_CODE (type) == ENUMERAL_TYPE);
6171 if (processing_template_decl)
6173 /* This may change after the call to
6174 push_template_decl_real, but we want the original value. */
6175 tree name = DECL_NAME (decl);
6177 decl = push_template_decl_real (decl, is_friend);
6178 if (decl == error_mark_node)
6179 return error_mark_node;
6181 /* If the current binding level is the binding level for the
6182 template parameters (see the comment in
6183 begin_template_parm_list) and the enclosing level is a class
6184 scope, and we're not looking at a friend, push the
6185 declaration of the member class into the class scope. In the
6186 friend case, push_template_decl will already have put the
6187 friend into global scope, if appropriate. */
6188 if (TREE_CODE (type) != ENUMERAL_TYPE
6189 && !is_friend && b->kind == sk_template_parms
6190 && b->level_chain->kind == sk_class)
6192 finish_member_declaration (CLASSTYPE_TI_TEMPLATE (type));
6194 if (!COMPLETE_TYPE_P (current_class_type))
6196 maybe_add_class_template_decl_list (current_class_type,
6197 type, /*friend_p=*/0);
6198 /* Put this UTD in the table of UTDs for the class. */
6199 if (CLASSTYPE_NESTED_UTDS (current_class_type) == NULL)
6200 CLASSTYPE_NESTED_UTDS (current_class_type) =
6201 binding_table_new (SCOPE_DEFAULT_HT_SIZE);
6203 binding_table_insert
6204 (CLASSTYPE_NESTED_UTDS (current_class_type), name, type);
6210 return decl;
6213 /* Push a tag name NAME for struct/class/union/enum type TYPE. In case
6214 that the NAME is a class template, the tag is processed but not pushed.
6216 The pushed scope depend on the SCOPE parameter:
6217 - When SCOPE is TS_CURRENT, put it into the inner-most non-sk_cleanup
6218 scope.
6219 - When SCOPE is TS_GLOBAL, put it in the inner-most non-class and
6220 non-template-parameter scope. This case is needed for forward
6221 declarations.
6222 - When SCOPE is TS_WITHIN_ENCLOSING_NON_CLASS, this is similar to
6223 TS_GLOBAL case except that names within template-parameter scopes
6224 are not pushed at all.
6226 Returns TYPE upon success and ERROR_MARK_NODE otherwise. */
6228 static tree
6229 do_pushtag (tree name, tree type, tag_scope scope)
6231 tree decl;
6233 cp_binding_level *b = current_binding_level;
6234 while (/* Cleanup scopes are not scopes from the point of view of
6235 the language. */
6236 b->kind == sk_cleanup
6237 /* Neither are function parameter scopes. */
6238 || b->kind == sk_function_parms
6239 /* Neither are the scopes used to hold template parameters
6240 for an explicit specialization. For an ordinary template
6241 declaration, these scopes are not scopes from the point of
6242 view of the language. */
6243 || (b->kind == sk_template_parms
6244 && (b->explicit_spec_p || scope == ts_global))
6245 /* Pushing into a class is ok for lambdas or when we want current */
6246 || (b->kind == sk_class
6247 && scope != ts_lambda
6248 && (scope != ts_current
6249 /* We may be defining a new type in the initializer
6250 of a static member variable. We allow this when
6251 not pedantic, and it is particularly useful for
6252 type punning via an anonymous union. */
6253 || COMPLETE_TYPE_P (b->this_entity))))
6254 b = b->level_chain;
6256 gcc_assert (identifier_p (name));
6258 /* Do C++ gratuitous typedefing. */
6259 if (identifier_type_value_1 (name) != type)
6261 tree tdef;
6262 int in_class = 0;
6263 tree context = TYPE_CONTEXT (type);
6265 if (! context)
6267 tree cs = current_scope ();
6269 if (scope == ts_current
6270 || scope == ts_lambda
6271 || (cs && TREE_CODE (cs) == FUNCTION_DECL))
6272 context = cs;
6273 else if (cs && TYPE_P (cs))
6274 /* When declaring a friend class of a local class, we want
6275 to inject the newly named class into the scope
6276 containing the local class, not the namespace
6277 scope. */
6278 context = decl_function_context (get_type_decl (cs));
6280 if (!context)
6281 context = current_namespace;
6283 if (b->kind == sk_class
6284 || (b->kind == sk_template_parms
6285 && b->level_chain->kind == sk_class))
6286 in_class = 1;
6288 tdef = create_implicit_typedef (name, type);
6289 DECL_CONTEXT (tdef) = FROB_CONTEXT (context);
6290 if (scope == ts_within_enclosing_non_class)
6292 /* This is a friend. Make this TYPE_DECL node hidden from
6293 ordinary name lookup. Its corresponding TEMPLATE_DECL
6294 will be marked in push_template_decl_real. */
6295 retrofit_lang_decl (tdef);
6296 DECL_ANTICIPATED (tdef) = 1;
6297 DECL_FRIEND_P (tdef) = 1;
6300 decl = maybe_process_template_type_declaration
6301 (type, scope == ts_within_enclosing_non_class, b);
6302 if (decl == error_mark_node)
6303 return decl;
6305 if (b->kind == sk_class)
6307 if (!TYPE_BEING_DEFINED (current_class_type)
6308 && scope != ts_lambda)
6309 return error_mark_node;
6311 if (!PROCESSING_REAL_TEMPLATE_DECL_P ())
6312 /* Put this TYPE_DECL on the TYPE_FIELDS list for the
6313 class. But if it's a member template class, we want
6314 the TEMPLATE_DECL, not the TYPE_DECL, so this is done
6315 later. */
6316 finish_member_declaration (decl);
6317 else
6318 pushdecl_class_level (decl);
6320 else if (b->kind != sk_template_parms)
6322 decl = do_pushdecl_with_scope (decl, b, /*is_friend=*/false);
6323 if (decl == error_mark_node)
6324 return decl;
6326 if (DECL_CONTEXT (decl) == std_node
6327 && init_list_identifier == DECL_NAME (TYPE_NAME (type))
6328 && !CLASSTYPE_TEMPLATE_INFO (type))
6330 error ("declaration of std::initializer_list does not match "
6331 "#include <initializer_list>, isn't a template");
6332 return error_mark_node;
6336 if (! in_class)
6337 set_identifier_type_value_with_scope (name, tdef, b);
6339 TYPE_CONTEXT (type) = DECL_CONTEXT (decl);
6341 /* If this is a local class, keep track of it. We need this
6342 information for name-mangling, and so that it is possible to
6343 find all function definitions in a translation unit in a
6344 convenient way. (It's otherwise tricky to find a member
6345 function definition it's only pointed to from within a local
6346 class.) */
6347 if (TYPE_FUNCTION_SCOPE_P (type))
6349 if (processing_template_decl)
6351 /* Push a DECL_EXPR so we call pushtag at the right time in
6352 template instantiation rather than in some nested context. */
6353 add_decl_expr (decl);
6355 else
6356 vec_safe_push (local_classes, type);
6360 if (b->kind == sk_class
6361 && !COMPLETE_TYPE_P (current_class_type))
6363 maybe_add_class_template_decl_list (current_class_type,
6364 type, /*friend_p=*/0);
6366 if (CLASSTYPE_NESTED_UTDS (current_class_type) == NULL)
6367 CLASSTYPE_NESTED_UTDS (current_class_type)
6368 = binding_table_new (SCOPE_DEFAULT_HT_SIZE);
6370 binding_table_insert
6371 (CLASSTYPE_NESTED_UTDS (current_class_type), name, type);
6374 decl = TYPE_NAME (type);
6375 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
6377 /* Set type visibility now if this is a forward declaration. */
6378 TREE_PUBLIC (decl) = 1;
6379 determine_visibility (decl);
6381 return type;
6384 /* Wrapper for do_pushtag. */
6386 tree
6387 pushtag (tree name, tree type, tag_scope scope)
6389 tree ret;
6390 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6391 ret = do_pushtag (name, type, scope);
6392 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6393 return ret;
6397 /* Subroutines for reverting temporarily to top-level for instantiation
6398 of templates and such. We actually need to clear out the class- and
6399 local-value slots of all identifiers, so that only the global values
6400 are at all visible. Simply setting current_binding_level to the global
6401 scope isn't enough, because more binding levels may be pushed. */
6402 struct saved_scope *scope_chain;
6404 /* Return true if ID has not already been marked. */
6406 static inline bool
6407 store_binding_p (tree id)
6409 if (!id || !IDENTIFIER_BINDING (id))
6410 return false;
6412 if (IDENTIFIER_MARKED (id))
6413 return false;
6415 return true;
6418 /* Add an appropriate binding to *OLD_BINDINGS which needs to already
6419 have enough space reserved. */
6421 static void
6422 store_binding (tree id, vec<cxx_saved_binding, va_gc> **old_bindings)
6424 cxx_saved_binding saved;
6426 gcc_checking_assert (store_binding_p (id));
6428 IDENTIFIER_MARKED (id) = 1;
6430 saved.identifier = id;
6431 saved.binding = IDENTIFIER_BINDING (id);
6432 saved.real_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
6433 (*old_bindings)->quick_push (saved);
6434 IDENTIFIER_BINDING (id) = NULL;
6437 static void
6438 store_bindings (tree names, vec<cxx_saved_binding, va_gc> **old_bindings)
6440 static vec<tree> bindings_need_stored;
6441 tree t, id;
6442 size_t i;
6444 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6445 for (t = names; t; t = TREE_CHAIN (t))
6447 if (TREE_CODE (t) == TREE_LIST)
6448 id = TREE_PURPOSE (t);
6449 else
6450 id = DECL_NAME (t);
6452 if (store_binding_p (id))
6453 bindings_need_stored.safe_push (id);
6455 if (!bindings_need_stored.is_empty ())
6457 vec_safe_reserve_exact (*old_bindings, bindings_need_stored.length ());
6458 for (i = 0; bindings_need_stored.iterate (i, &id); ++i)
6460 /* We can apparently have duplicates in NAMES. */
6461 if (store_binding_p (id))
6462 store_binding (id, old_bindings);
6464 bindings_need_stored.truncate (0);
6466 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6469 /* Like store_bindings, but NAMES is a vector of cp_class_binding
6470 objects, rather than a TREE_LIST. */
6472 static void
6473 store_class_bindings (vec<cp_class_binding, va_gc> *names,
6474 vec<cxx_saved_binding, va_gc> **old_bindings)
6476 static vec<tree> bindings_need_stored;
6477 size_t i;
6478 cp_class_binding *cb;
6480 for (i = 0; vec_safe_iterate (names, i, &cb); ++i)
6481 if (store_binding_p (cb->identifier))
6482 bindings_need_stored.safe_push (cb->identifier);
6483 if (!bindings_need_stored.is_empty ())
6485 tree id;
6486 vec_safe_reserve_exact (*old_bindings, bindings_need_stored.length ());
6487 for (i = 0; bindings_need_stored.iterate (i, &id); ++i)
6488 store_binding (id, old_bindings);
6489 bindings_need_stored.truncate (0);
6493 /* A chain of saved_scope structures awaiting reuse. */
6495 static GTY((deletable)) struct saved_scope *free_saved_scope;
6497 static void
6498 do_push_to_top_level (void)
6500 struct saved_scope *s;
6501 cp_binding_level *b;
6502 cxx_saved_binding *sb;
6503 size_t i;
6504 bool need_pop;
6506 /* Reuse or create a new structure for this saved scope. */
6507 if (free_saved_scope != NULL)
6509 s = free_saved_scope;
6510 free_saved_scope = s->prev;
6512 vec<cxx_saved_binding, va_gc> *old_bindings = s->old_bindings;
6513 memset (s, 0, sizeof (*s));
6514 /* Also reuse the structure's old_bindings vector. */
6515 vec_safe_truncate (old_bindings, 0);
6516 s->old_bindings = old_bindings;
6518 else
6519 s = ggc_cleared_alloc<saved_scope> ();
6521 b = scope_chain ? current_binding_level : 0;
6523 /* If we're in the middle of some function, save our state. */
6524 if (cfun)
6526 need_pop = true;
6527 push_function_context ();
6529 else
6530 need_pop = false;
6532 if (scope_chain && previous_class_level)
6533 store_class_bindings (previous_class_level->class_shadowed,
6534 &s->old_bindings);
6536 /* Have to include the global scope, because class-scope decls
6537 aren't listed anywhere useful. */
6538 for (; b; b = b->level_chain)
6540 tree t;
6542 /* Template IDs are inserted into the global level. If they were
6543 inserted into namespace level, finish_file wouldn't find them
6544 when doing pending instantiations. Therefore, don't stop at
6545 namespace level, but continue until :: . */
6546 if (global_scope_p (b))
6547 break;
6549 store_bindings (b->names, &s->old_bindings);
6550 /* We also need to check class_shadowed to save class-level type
6551 bindings, since pushclass doesn't fill in b->names. */
6552 if (b->kind == sk_class)
6553 store_class_bindings (b->class_shadowed, &s->old_bindings);
6555 /* Unwind type-value slots back to top level. */
6556 for (t = b->type_shadowed; t; t = TREE_CHAIN (t))
6557 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (t), TREE_VALUE (t));
6560 FOR_EACH_VEC_SAFE_ELT (s->old_bindings, i, sb)
6561 IDENTIFIER_MARKED (sb->identifier) = 0;
6563 s->prev = scope_chain;
6564 s->bindings = b;
6565 s->need_pop_function_context = need_pop;
6566 s->function_decl = current_function_decl;
6567 s->unevaluated_operand = cp_unevaluated_operand;
6568 s->inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
6569 s->x_stmt_tree.stmts_are_full_exprs_p = true;
6571 scope_chain = s;
6572 current_function_decl = NULL_TREE;
6573 vec_alloc (current_lang_base, 10);
6574 current_lang_name = lang_name_cplusplus;
6575 current_namespace = global_namespace;
6576 push_class_stack ();
6577 cp_unevaluated_operand = 0;
6578 c_inhibit_evaluation_warnings = 0;
6581 static void
6582 do_pop_from_top_level (void)
6584 struct saved_scope *s = scope_chain;
6585 cxx_saved_binding *saved;
6586 size_t i;
6588 /* Clear out class-level bindings cache. */
6589 if (previous_class_level)
6590 invalidate_class_lookup_cache ();
6591 pop_class_stack ();
6593 current_lang_base = 0;
6595 scope_chain = s->prev;
6596 FOR_EACH_VEC_SAFE_ELT (s->old_bindings, i, saved)
6598 tree id = saved->identifier;
6600 IDENTIFIER_BINDING (id) = saved->binding;
6601 SET_IDENTIFIER_TYPE_VALUE (id, saved->real_type_value);
6604 /* If we were in the middle of compiling a function, restore our
6605 state. */
6606 if (s->need_pop_function_context)
6607 pop_function_context ();
6608 current_function_decl = s->function_decl;
6609 cp_unevaluated_operand = s->unevaluated_operand;
6610 c_inhibit_evaluation_warnings = s->inhibit_evaluation_warnings;
6612 /* Make this saved_scope structure available for reuse by
6613 push_to_top_level. */
6614 s->prev = free_saved_scope;
6615 free_saved_scope = s;
6618 /* Push into the scope of the namespace NS, even if it is deeply
6619 nested within another namespace. */
6621 static void
6622 do_push_nested_namespace (tree ns)
6624 if (ns == global_namespace)
6625 do_push_to_top_level ();
6626 else
6628 do_push_nested_namespace (CP_DECL_CONTEXT (ns));
6629 gcc_checking_assert
6630 (find_namespace_value (current_namespace, DECL_NAME (ns)) == ns);
6631 resume_scope (NAMESPACE_LEVEL (ns));
6632 current_namespace = ns;
6636 /* Pop back from the scope of the namespace NS, which was previously
6637 entered with push_nested_namespace. */
6639 static void
6640 do_pop_nested_namespace (tree ns)
6642 while (ns != global_namespace)
6644 ns = CP_DECL_CONTEXT (ns);
6645 current_namespace = ns;
6646 leave_scope ();
6649 do_pop_from_top_level ();
6652 /* Add TARGET to USINGS, if it does not already exist there.
6653 We used to build the complete graph of usings at this point, from
6654 the POV of the source namespaces. Now we build that as we perform
6655 the unqualified search. */
6657 static void
6658 add_using_namespace (vec<tree, va_gc> *&usings, tree target)
6660 if (usings)
6661 for (unsigned ix = usings->length (); ix--;)
6662 if ((*usings)[ix] == target)
6663 return;
6665 vec_safe_push (usings, target);
6668 /* Tell the debug system of a using directive. */
6670 static void
6671 emit_debug_info_using_namespace (tree from, tree target, bool implicit)
6673 /* Emit debugging info. */
6674 tree context = from != global_namespace ? from : NULL_TREE;
6675 debug_hooks->imported_module_or_decl (target, NULL_TREE, context, false,
6676 implicit);
6679 /* Process a namespace-scope using directive. */
6681 void
6682 finish_namespace_using_directive (tree target, tree attribs)
6684 gcc_checking_assert (namespace_bindings_p ());
6685 if (target == error_mark_node)
6686 return;
6688 add_using_namespace (DECL_NAMESPACE_USING (current_namespace),
6689 ORIGINAL_NAMESPACE (target));
6690 emit_debug_info_using_namespace (current_namespace,
6691 ORIGINAL_NAMESPACE (target), false);
6693 if (attribs == error_mark_node)
6694 return;
6696 for (tree a = attribs; a; a = TREE_CHAIN (a))
6698 tree name = get_attribute_name (a);
6699 if (is_attribute_p ("strong", name))
6701 warning (0, "strong using directive no longer supported");
6702 if (CP_DECL_CONTEXT (target) == current_namespace)
6703 inform (DECL_SOURCE_LOCATION (target),
6704 "you may use an inline namespace instead");
6706 else
6707 warning (OPT_Wattributes, "%qD attribute directive ignored", name);
6711 /* Process a function-scope using-directive. */
6713 void
6714 finish_local_using_directive (tree target, tree attribs)
6716 gcc_checking_assert (local_bindings_p ());
6717 if (target == error_mark_node)
6718 return;
6720 if (attribs)
6721 warning (OPT_Wattributes, "attributes ignored on local using directive");
6723 add_stmt (build_stmt (input_location, USING_STMT, target));
6725 add_using_namespace (current_binding_level->using_directives,
6726 ORIGINAL_NAMESPACE (target));
6729 /* Pushes X into the global namespace. */
6731 tree
6732 pushdecl_top_level (tree x, bool is_friend)
6734 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6735 do_push_to_top_level ();
6736 x = pushdecl_namespace_level (x, is_friend);
6737 do_pop_from_top_level ();
6738 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6739 return x;
6742 /* Pushes X into the global namespace and calls cp_finish_decl to
6743 register the variable, initializing it with INIT. */
6745 tree
6746 pushdecl_top_level_and_finish (tree x, tree init)
6748 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6749 do_push_to_top_level ();
6750 x = pushdecl_namespace_level (x, false);
6751 cp_finish_decl (x, init, false, NULL_TREE, 0);
6752 do_pop_from_top_level ();
6753 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6754 return x;
6757 /* Enter the namespaces from current_namerspace to NS. */
6759 static int
6760 push_inline_namespaces (tree ns)
6762 int count = 0;
6763 if (ns != current_namespace)
6765 gcc_assert (ns != global_namespace);
6766 count += push_inline_namespaces (CP_DECL_CONTEXT (ns));
6767 resume_scope (NAMESPACE_LEVEL (ns));
6768 current_namespace = ns;
6769 count++;
6771 return count;
6774 /* Push into the scope of the NAME namespace. If NAME is NULL_TREE,
6775 then we enter an anonymous namespace. If MAKE_INLINE is true, then
6776 we create an inline namespace (it is up to the caller to check upon
6777 redefinition). Return the number of namespaces entered. */
6780 push_namespace (tree name, bool make_inline)
6782 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6783 int count = 0;
6785 /* We should not get here if the global_namespace is not yet constructed
6786 nor if NAME designates the global namespace: The global scope is
6787 constructed elsewhere. */
6788 gcc_checking_assert (global_namespace != NULL && name != global_identifier);
6790 tree ns = NULL_TREE;
6792 name_lookup lookup (name, 0);
6793 if (!lookup.search_qualified (current_namespace, /*usings=*/false))
6795 else if (TREE_CODE (lookup.value) != NAMESPACE_DECL)
6797 else if (tree dna = DECL_NAMESPACE_ALIAS (lookup.value))
6799 /* A namespace alias is not allowed here, but if the alias
6800 is for a namespace also inside the current scope,
6801 accept it with a diagnostic. That's better than dying
6802 horribly. */
6803 if (is_nested_namespace (current_namespace, CP_DECL_CONTEXT (dna)))
6805 error ("namespace alias %qD not allowed here, "
6806 "assuming %qD", lookup.value, dna);
6807 ns = dna;
6810 else
6811 ns = lookup.value;
6814 bool new_ns = false;
6815 if (ns)
6816 /* DR2061. NS might be a member of an inline namespace. We
6817 need to push into those namespaces. */
6818 count += push_inline_namespaces (CP_DECL_CONTEXT (ns));
6819 else
6821 ns = build_lang_decl (NAMESPACE_DECL, name, void_type_node);
6822 SCOPE_DEPTH (ns) = SCOPE_DEPTH (current_namespace) + 1;
6823 if (!SCOPE_DEPTH (ns))
6824 /* We only allow depth 255. */
6825 sorry ("cannot nest more than %d namespaces",
6826 SCOPE_DEPTH (current_namespace));
6827 DECL_CONTEXT (ns) = FROB_CONTEXT (current_namespace);
6828 new_ns = true;
6830 if (pushdecl (ns) == error_mark_node)
6831 ns = NULL_TREE;
6832 else
6834 if (!name)
6836 SET_DECL_ASSEMBLER_NAME (ns, anon_identifier);
6838 if (!make_inline)
6839 add_using_namespace (DECL_NAMESPACE_USING (current_namespace),
6840 ns);
6842 else if (TREE_PUBLIC (current_namespace))
6843 TREE_PUBLIC (ns) = 1;
6845 if (make_inline)
6847 DECL_NAMESPACE_INLINE_P (ns) = true;
6848 vec_safe_push (DECL_NAMESPACE_INLINEES (current_namespace), ns);
6851 if (!name || make_inline)
6852 emit_debug_info_using_namespace (current_namespace, ns, true);
6856 if (ns)
6858 if (make_inline && !DECL_NAMESPACE_INLINE_P (ns))
6860 error ("inline namespace must be specified at initial definition");
6861 inform (DECL_SOURCE_LOCATION (ns), "%qD defined here", ns);
6863 if (new_ns)
6864 begin_scope (sk_namespace, ns);
6865 else
6866 resume_scope (NAMESPACE_LEVEL (ns));
6867 current_namespace = ns;
6868 count++;
6871 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6872 return count;
6875 /* Pop from the scope of the current namespace. */
6877 void
6878 pop_namespace (void)
6880 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6882 gcc_assert (current_namespace != global_namespace);
6883 current_namespace = CP_DECL_CONTEXT (current_namespace);
6884 /* The binding level is not popped, as it might be re-opened later. */
6885 leave_scope ();
6887 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6890 /* External entry points for do_{push_to/pop_from}_top_level. */
6892 void
6893 push_to_top_level (void)
6895 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6896 do_push_to_top_level ();
6897 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6900 void
6901 pop_from_top_level (void)
6903 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6904 do_pop_from_top_level ();
6905 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6908 /* External entry points for do_{push,pop}_nested_namespace. */
6910 void
6911 push_nested_namespace (tree ns)
6913 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6914 do_push_nested_namespace (ns);
6915 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6918 void
6919 pop_nested_namespace (tree ns)
6921 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6922 gcc_assert (current_namespace == ns);
6923 do_pop_nested_namespace (ns);
6924 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6927 /* Pop off extraneous binding levels left over due to syntax errors.
6928 We don't pop past namespaces, as they might be valid. */
6930 void
6931 pop_everything (void)
6933 if (ENABLE_SCOPE_CHECKING)
6934 verbatim ("XXX entering pop_everything ()\n");
6935 while (!namespace_bindings_p ())
6937 if (current_binding_level->kind == sk_class)
6938 pop_nested_class ();
6939 else
6940 poplevel (0, 0, 0);
6942 if (ENABLE_SCOPE_CHECKING)
6943 verbatim ("XXX leaving pop_everything ()\n");
6946 /* Emit debugging information for using declarations and directives.
6947 If input tree is overloaded fn then emit debug info for all
6948 candidates. */
6950 void
6951 cp_emit_debug_info_for_using (tree t, tree context)
6953 /* Don't try to emit any debug information if we have errors. */
6954 if (seen_error ())
6955 return;
6957 /* Ignore this FUNCTION_DECL if it refers to a builtin declaration
6958 of a builtin function. */
6959 if (TREE_CODE (t) == FUNCTION_DECL
6960 && DECL_EXTERNAL (t)
6961 && DECL_BUILT_IN (t))
6962 return;
6964 /* Do not supply context to imported_module_or_decl, if
6965 it is a global namespace. */
6966 if (context == global_namespace)
6967 context = NULL_TREE;
6969 t = MAYBE_BASELINK_FUNCTIONS (t);
6971 /* FIXME: Handle TEMPLATE_DECLs. */
6972 for (lkp_iterator iter (t); iter; ++iter)
6974 tree fn = *iter;
6975 if (TREE_CODE (fn) != TEMPLATE_DECL)
6977 if (building_stmt_list_p ())
6978 add_stmt (build_stmt (input_location, USING_STMT, fn));
6979 else
6980 debug_hooks->imported_module_or_decl (fn, NULL_TREE, context,
6981 false, false);
6986 #include "gt-cp-name-lookup.h"