[PATCH c++/86881] -Wshadow-local-compatible ICE
[official-gcc.git] / gcc / cp / name-lookup.c
blobc56bfe58a1999c106a38382f848e8a39fb3f35c5
1 /* Definitions for C++ name lookup routines.
2 Copyright (C) 2003-2018 Free Software Foundation, Inc.
3 Contributed by Gabriel Dos Reis <gdr@integrable-solutions.net>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #define INCLUDE_UNIQUE_PTR
23 #include "system.h"
24 #include "coretypes.h"
25 #include "cp-tree.h"
26 #include "timevar.h"
27 #include "stringpool.h"
28 #include "print-tree.h"
29 #include "attribs.h"
30 #include "debug.h"
31 #include "c-family/c-pragma.h"
32 #include "params.h"
33 #include "gcc-rich-location.h"
34 #include "spellcheck-tree.h"
35 #include "parser.h"
36 #include "c-family/name-hint.h"
37 #include "c-family/known-headers.h"
38 #include "c-family/c-spellcheck.h"
40 static cxx_binding *cxx_binding_make (tree value, tree type);
41 static cp_binding_level *innermost_nonclass_level (void);
42 static void set_identifier_type_value_with_scope (tree id, tree decl,
43 cp_binding_level *b);
44 static bool maybe_suggest_missing_std_header (location_t location, tree name);
46 /* Create an overload suitable for recording an artificial TYPE_DECL
47 and another decl. We use this machanism to implement the struct
48 stat hack within a namespace. It'd be nice to use it everywhere. */
50 #define STAT_HACK_P(N) ((N) && TREE_CODE (N) == OVERLOAD && OVL_LOOKUP_P (N))
51 #define STAT_TYPE(N) TREE_TYPE (N)
52 #define STAT_DECL(N) OVL_FUNCTION (N)
53 #define MAYBE_STAT_DECL(N) (STAT_HACK_P (N) ? STAT_DECL (N) : N)
54 #define MAYBE_STAT_TYPE(N) (STAT_HACK_P (N) ? STAT_TYPE (N) : NULL_TREE)
56 /* Create a STAT_HACK node with DECL as the value binding and TYPE as
57 the type binding. */
59 static tree
60 stat_hack (tree decl = NULL_TREE, tree type = NULL_TREE)
62 tree result = make_node (OVERLOAD);
64 /* Mark this as a lookup, so we can tell this is a stat hack. */
65 OVL_LOOKUP_P (result) = true;
66 STAT_DECL (result) = decl;
67 STAT_TYPE (result) = type;
68 return result;
71 /* Create a local binding level for NAME. */
73 static cxx_binding *
74 create_local_binding (cp_binding_level *level, tree name)
76 cxx_binding *binding = cxx_binding_make (NULL, NULL);
78 INHERITED_VALUE_BINDING_P (binding) = false;
79 LOCAL_BINDING_P (binding) = true;
80 binding->scope = level;
81 binding->previous = IDENTIFIER_BINDING (name);
83 IDENTIFIER_BINDING (name) = binding;
85 return binding;
88 /* Find the binding for NAME in namespace NS. If CREATE_P is true,
89 make an empty binding if there wasn't one. */
91 static tree *
92 find_namespace_slot (tree ns, tree name, bool create_p = false)
94 tree *slot = DECL_NAMESPACE_BINDINGS (ns)
95 ->find_slot_with_hash (name, name ? IDENTIFIER_HASH_VALUE (name) : 0,
96 create_p ? INSERT : NO_INSERT);
97 return slot;
100 static tree
101 find_namespace_value (tree ns, tree name)
103 tree *b = find_namespace_slot (ns, name);
105 return b ? MAYBE_STAT_DECL (*b) : NULL_TREE;
108 /* Add DECL to the list of things declared in B. */
110 static void
111 add_decl_to_level (cp_binding_level *b, tree decl)
113 gcc_assert (b->kind != sk_class);
115 /* Make sure we don't create a circular list. xref_tag can end
116 up pushing the same artificial decl more than once. We
117 should have already detected that in update_binding. */
118 gcc_assert (b->names != decl);
120 /* We build up the list in reverse order, and reverse it later if
121 necessary. */
122 TREE_CHAIN (decl) = b->names;
123 b->names = decl;
125 /* If appropriate, add decl to separate list of statics. We
126 include extern variables because they might turn out to be
127 static later. It's OK for this list to contain a few false
128 positives. */
129 if (b->kind == sk_namespace
130 && ((VAR_P (decl)
131 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
132 || (TREE_CODE (decl) == FUNCTION_DECL
133 && (!TREE_PUBLIC (decl)
134 || decl_anon_ns_mem_p (decl)
135 || DECL_DECLARED_INLINE_P (decl)))))
136 vec_safe_push (static_decls, decl);
139 /* Find the binding for NAME in the local binding level B. */
141 static cxx_binding *
142 find_local_binding (cp_binding_level *b, tree name)
144 if (cxx_binding *binding = IDENTIFIER_BINDING (name))
145 for (;; b = b->level_chain)
147 if (binding->scope == b)
148 return binding;
150 /* Cleanup contours are transparent to the language. */
151 if (b->kind != sk_cleanup)
152 break;
154 return NULL;
157 struct name_lookup
159 public:
160 typedef std::pair<tree, tree> using_pair;
161 typedef vec<using_pair, va_heap, vl_embed> using_queue;
163 public:
164 tree name; /* The identifier being looked for. */
165 tree value; /* A (possibly ambiguous) set of things found. */
166 tree type; /* A type that has been found. */
167 int flags; /* Lookup flags. */
168 bool deduping; /* Full deduping is needed because using declarations
169 are in play. */
170 vec<tree, va_heap, vl_embed> *scopes;
171 name_lookup *previous; /* Previously active lookup. */
173 protected:
174 /* Marked scope stack for outermost name lookup. */
175 static vec<tree, va_heap, vl_embed> *shared_scopes;
176 /* Currently active lookup. */
177 static name_lookup *active;
179 public:
180 name_lookup (tree n, int f = 0)
181 : name (n), value (NULL_TREE), type (NULL_TREE), flags (f),
182 deduping (false), scopes (NULL), previous (NULL)
184 preserve_state ();
186 ~name_lookup ()
188 restore_state ();
191 private: /* Uncopyable, unmovable, unassignable. I am a rock. */
192 name_lookup (const name_lookup &);
193 name_lookup &operator= (const name_lookup &);
195 protected:
196 static bool seen_p (tree scope)
198 return LOOKUP_SEEN_P (scope);
200 static bool found_p (tree scope)
202 return LOOKUP_FOUND_P (scope);
205 void mark_seen (tree scope); /* Mark and add to scope vector. */
206 static void mark_found (tree scope)
208 gcc_checking_assert (seen_p (scope));
209 LOOKUP_FOUND_P (scope) = true;
211 bool see_and_mark (tree scope)
213 bool ret = seen_p (scope);
214 if (!ret)
215 mark_seen (scope);
216 return ret;
218 bool find_and_mark (tree scope);
220 private:
221 void preserve_state ();
222 void restore_state ();
224 private:
225 static tree ambiguous (tree thing, tree current);
226 void add_overload (tree fns);
227 void add_value (tree new_val);
228 void add_type (tree new_type);
229 bool process_binding (tree val_bind, tree type_bind);
231 /* Look in only namespace. */
232 bool search_namespace_only (tree scope);
233 /* Look in namespace and its (recursive) inlines. Ignore using
234 directives. Return true if something found (inc dups). */
235 bool search_namespace (tree scope);
236 /* Look in the using directives of namespace + inlines using
237 qualified lookup rules. */
238 bool search_usings (tree scope);
240 private:
241 using_queue *queue_namespace (using_queue *queue, int depth, tree scope);
242 using_queue *do_queue_usings (using_queue *queue, int depth,
243 vec<tree, va_gc> *usings);
244 using_queue *queue_usings (using_queue *queue, int depth,
245 vec<tree, va_gc> *usings)
247 if (usings)
248 queue = do_queue_usings (queue, depth, usings);
249 return queue;
252 private:
253 void add_fns (tree);
255 void adl_expr (tree);
256 void adl_type (tree);
257 void adl_template_arg (tree);
258 void adl_class (tree);
259 void adl_bases (tree);
260 void adl_class_only (tree);
261 void adl_namespace (tree);
262 void adl_namespace_only (tree);
264 public:
265 /* Search namespace + inlines + maybe usings as qualified lookup. */
266 bool search_qualified (tree scope, bool usings = true);
268 /* Search namespace + inlines + usings as unqualified lookup. */
269 bool search_unqualified (tree scope, cp_binding_level *);
271 /* ADL lookup of ARGS. */
272 tree search_adl (tree fns, vec<tree, va_gc> *args);
275 /* Scope stack shared by all outermost lookups. This avoids us
276 allocating and freeing on every single lookup. */
277 vec<tree, va_heap, vl_embed> *name_lookup::shared_scopes;
279 /* Currently active lookup. */
280 name_lookup *name_lookup::active;
282 /* Name lookup is recursive, becase ADL can cause template
283 instatiation. This is of course a rare event, so we optimize for
284 it not happening. When we discover an active name-lookup, which
285 must be an ADL lookup, we need to unmark the marked scopes and also
286 unmark the lookup we might have been accumulating. */
288 void
289 name_lookup::preserve_state ()
291 previous = active;
292 if (previous)
294 unsigned length = vec_safe_length (previous->scopes);
295 vec_safe_reserve (previous->scopes, length * 2);
296 for (unsigned ix = length; ix--;)
298 tree decl = (*previous->scopes)[ix];
300 gcc_checking_assert (LOOKUP_SEEN_P (decl));
301 LOOKUP_SEEN_P (decl) = false;
303 /* Preserve the FOUND_P state on the interrupted lookup's
304 stack. */
305 if (LOOKUP_FOUND_P (decl))
307 LOOKUP_FOUND_P (decl) = false;
308 previous->scopes->quick_push (decl);
312 /* Unmark the outer partial lookup. */
313 if (previous->deduping)
314 lookup_mark (previous->value, false);
316 else
317 scopes = shared_scopes;
318 active = this;
321 /* Restore the marking state of a lookup we interrupted. */
323 void
324 name_lookup::restore_state ()
326 if (deduping)
327 lookup_mark (value, false);
329 /* Unmark and empty this lookup's scope stack. */
330 for (unsigned ix = vec_safe_length (scopes); ix--;)
332 tree decl = scopes->pop ();
333 gcc_checking_assert (LOOKUP_SEEN_P (decl));
334 LOOKUP_SEEN_P (decl) = false;
335 LOOKUP_FOUND_P (decl) = false;
338 active = previous;
339 if (previous)
341 free (scopes);
343 unsigned length = vec_safe_length (previous->scopes);
344 for (unsigned ix = 0; ix != length; ix++)
346 tree decl = (*previous->scopes)[ix];
347 if (LOOKUP_SEEN_P (decl))
349 /* The remainder of the scope stack must be recording
350 FOUND_P decls, which we want to pop off. */
353 tree decl = previous->scopes->pop ();
354 gcc_checking_assert (LOOKUP_SEEN_P (decl)
355 && !LOOKUP_FOUND_P (decl));
356 LOOKUP_FOUND_P (decl) = true;
358 while (++ix != length);
359 break;
362 gcc_checking_assert (!LOOKUP_FOUND_P (decl));
363 LOOKUP_SEEN_P (decl) = true;
366 /* Remark the outer partial lookup. */
367 if (previous->deduping)
368 lookup_mark (previous->value, true);
370 else
371 shared_scopes = scopes;
374 void
375 name_lookup::mark_seen (tree scope)
377 gcc_checking_assert (!seen_p (scope));
378 LOOKUP_SEEN_P (scope) = true;
379 vec_safe_push (scopes, scope);
382 bool
383 name_lookup::find_and_mark (tree scope)
385 bool result = LOOKUP_FOUND_P (scope);
386 if (!result)
388 LOOKUP_FOUND_P (scope) = true;
389 if (!LOOKUP_SEEN_P (scope))
390 vec_safe_push (scopes, scope);
393 return result;
396 /* THING and CURRENT are ambiguous, concatenate them. */
398 tree
399 name_lookup::ambiguous (tree thing, tree current)
401 if (TREE_CODE (current) != TREE_LIST)
403 current = build_tree_list (NULL_TREE, current);
404 TREE_TYPE (current) = error_mark_node;
406 current = tree_cons (NULL_TREE, thing, current);
407 TREE_TYPE (current) = error_mark_node;
409 return current;
412 /* FNS is a new overload set to add to the exising set. */
414 void
415 name_lookup::add_overload (tree fns)
417 if (!deduping && TREE_CODE (fns) == OVERLOAD)
419 tree probe = fns;
420 if (flags & LOOKUP_HIDDEN)
421 probe = ovl_skip_hidden (probe);
422 if (probe && TREE_CODE (probe) == OVERLOAD && OVL_USING_P (probe))
424 /* We're about to add something found by a using
425 declaration, so need to engage deduping mode. */
426 lookup_mark (value, true);
427 deduping = true;
431 value = lookup_maybe_add (fns, value, deduping);
434 /* Add a NEW_VAL, a found value binding into the current value binding. */
436 void
437 name_lookup::add_value (tree new_val)
439 if (OVL_P (new_val) && (!value || OVL_P (value)))
440 add_overload (new_val);
441 else if (!value)
442 value = new_val;
443 else if (value == new_val)
445 else if ((TREE_CODE (value) == TYPE_DECL
446 && TREE_CODE (new_val) == TYPE_DECL
447 && same_type_p (TREE_TYPE (value), TREE_TYPE (new_val))))
448 /* Typedefs to the same type. */;
449 else if (TREE_CODE (value) == NAMESPACE_DECL
450 && TREE_CODE (new_val) == NAMESPACE_DECL
451 && ORIGINAL_NAMESPACE (value) == ORIGINAL_NAMESPACE (new_val))
452 /* Namespace (possibly aliased) to the same namespace. Locate
453 the namespace*/
454 value = ORIGINAL_NAMESPACE (value);
455 else
457 if (deduping)
459 /* Disengage deduping mode. */
460 lookup_mark (value, false);
461 deduping = false;
463 value = ambiguous (new_val, value);
467 /* Add a NEW_TYPE, a found type binding into the current type binding. */
469 void
470 name_lookup::add_type (tree new_type)
472 if (!type)
473 type = new_type;
474 else if (TREE_CODE (type) == TREE_LIST
475 || !same_type_p (TREE_TYPE (type), TREE_TYPE (new_type)))
476 type = ambiguous (new_type, type);
479 /* Process a found binding containing NEW_VAL and NEW_TYPE. Returns
480 true if we actually found something noteworthy. */
482 bool
483 name_lookup::process_binding (tree new_val, tree new_type)
485 /* Did we really see a type? */
486 if (new_type
487 && (LOOKUP_NAMESPACES_ONLY (flags)
488 || (!(flags & LOOKUP_HIDDEN)
489 && DECL_LANG_SPECIFIC (new_type)
490 && DECL_ANTICIPATED (new_type))))
491 new_type = NULL_TREE;
493 if (new_val && !(flags & LOOKUP_HIDDEN))
494 new_val = ovl_skip_hidden (new_val);
496 /* Do we really see a value? */
497 if (new_val)
498 switch (TREE_CODE (new_val))
500 case TEMPLATE_DECL:
501 /* If we expect types or namespaces, and not templates,
502 or this is not a template class. */
503 if ((LOOKUP_QUALIFIERS_ONLY (flags)
504 && !DECL_TYPE_TEMPLATE_P (new_val)))
505 new_val = NULL_TREE;
506 break;
507 case TYPE_DECL:
508 if (LOOKUP_NAMESPACES_ONLY (flags)
509 || (new_type && (flags & LOOKUP_PREFER_TYPES)))
510 new_val = NULL_TREE;
511 break;
512 case NAMESPACE_DECL:
513 if (LOOKUP_TYPES_ONLY (flags))
514 new_val = NULL_TREE;
515 break;
516 default:
517 if (LOOKUP_QUALIFIERS_ONLY (flags))
518 new_val = NULL_TREE;
521 if (!new_val)
523 new_val = new_type;
524 new_type = NULL_TREE;
527 /* Merge into the lookup */
528 if (new_val)
529 add_value (new_val);
530 if (new_type)
531 add_type (new_type);
533 return new_val != NULL_TREE;
536 /* Look in exactly namespace SCOPE. */
538 bool
539 name_lookup::search_namespace_only (tree scope)
541 bool found = false;
543 if (tree *binding = find_namespace_slot (scope, name))
544 found |= process_binding (MAYBE_STAT_DECL (*binding),
545 MAYBE_STAT_TYPE (*binding));
547 return found;
550 /* Conditionally look in namespace SCOPE and inline children. */
552 bool
553 name_lookup::search_namespace (tree scope)
555 if (see_and_mark (scope))
556 /* We've visited this scope before. Return what we found then. */
557 return found_p (scope);
559 /* Look in exactly namespace. */
560 bool found = search_namespace_only (scope);
562 /* Don't look into inline children, if we're looking for an
563 anonymous name -- it must be in the current scope, if anywhere. */
564 if (name)
565 /* Recursively look in its inline children. */
566 if (vec<tree, va_gc> *inlinees = DECL_NAMESPACE_INLINEES (scope))
567 for (unsigned ix = inlinees->length (); ix--;)
568 found |= search_namespace ((*inlinees)[ix]);
570 if (found)
571 mark_found (scope);
573 return found;
576 /* Recursively follow using directives of SCOPE & its inline children.
577 Such following is essentially a flood-fill algorithm. */
579 bool
580 name_lookup::search_usings (tree scope)
582 /* We do not check seen_p here, as that was already set during the
583 namespace_only walk. */
584 if (found_p (scope))
585 return true;
587 bool found = false;
588 if (vec<tree, va_gc> *usings = DECL_NAMESPACE_USING (scope))
589 for (unsigned ix = usings->length (); ix--;)
590 found |= search_qualified ((*usings)[ix], true);
592 /* Look in its inline children. */
593 if (vec<tree, va_gc> *inlinees = DECL_NAMESPACE_INLINEES (scope))
594 for (unsigned ix = inlinees->length (); ix--;)
595 found |= search_usings ((*inlinees)[ix]);
597 if (found)
598 mark_found (scope);
600 return found;
603 /* Qualified namespace lookup in SCOPE.
604 1) Look in SCOPE (+inlines). If found, we're done.
605 2) Otherwise, if USINGS is true,
606 recurse for every using directive of SCOPE (+inlines).
608 Trickiness is (a) loops and (b) multiple paths to same namespace.
609 In both cases we want to not repeat any lookups, and know whether
610 to stop the caller's step #2. Do this via the FOUND_P marker. */
612 bool
613 name_lookup::search_qualified (tree scope, bool usings)
615 bool found = false;
617 if (seen_p (scope))
618 found = found_p (scope);
619 else
621 found = search_namespace (scope);
622 if (!found && usings)
623 found = search_usings (scope);
626 return found;
629 /* Add SCOPE to the unqualified search queue, recursively add its
630 inlines and those via using directives. */
632 name_lookup::using_queue *
633 name_lookup::queue_namespace (using_queue *queue, int depth, tree scope)
635 if (see_and_mark (scope))
636 return queue;
638 /* Record it. */
639 tree common = scope;
640 while (SCOPE_DEPTH (common) > depth)
641 common = CP_DECL_CONTEXT (common);
642 vec_safe_push (queue, using_pair (common, scope));
644 /* Queue its inline children. */
645 if (vec<tree, va_gc> *inlinees = DECL_NAMESPACE_INLINEES (scope))
646 for (unsigned ix = inlinees->length (); ix--;)
647 queue = queue_namespace (queue, depth, (*inlinees)[ix]);
649 /* Queue its using targets. */
650 queue = queue_usings (queue, depth, DECL_NAMESPACE_USING (scope));
652 return queue;
655 /* Add the namespaces in USINGS to the unqualified search queue. */
657 name_lookup::using_queue *
658 name_lookup::do_queue_usings (using_queue *queue, int depth,
659 vec<tree, va_gc> *usings)
661 for (unsigned ix = usings->length (); ix--;)
662 queue = queue_namespace (queue, depth, (*usings)[ix]);
664 return queue;
667 /* Unqualified namespace lookup in SCOPE.
668 1) add scope+inlins to worklist.
669 2) recursively add target of every using directive
670 3) for each worklist item where SCOPE is common ancestor, search it
671 4) if nothing find, scope=parent, goto 1. */
673 bool
674 name_lookup::search_unqualified (tree scope, cp_binding_level *level)
676 /* Make static to avoid continual reallocation. We're not
677 recursive. */
678 static using_queue *queue = NULL;
679 bool found = false;
680 int length = vec_safe_length (queue);
682 /* Queue local using-directives. */
683 for (; level->kind != sk_namespace; level = level->level_chain)
684 queue = queue_usings (queue, SCOPE_DEPTH (scope), level->using_directives);
686 for (; !found; scope = CP_DECL_CONTEXT (scope))
688 gcc_assert (!DECL_NAMESPACE_ALIAS (scope));
689 int depth = SCOPE_DEPTH (scope);
691 /* Queue namespaces reachable from SCOPE. */
692 queue = queue_namespace (queue, depth, scope);
694 /* Search every queued namespace where SCOPE is the common
695 ancestor. Adjust the others. */
696 unsigned ix = length;
699 using_pair &pair = (*queue)[ix];
700 while (pair.first == scope)
702 found |= search_namespace_only (pair.second);
703 pair = queue->pop ();
704 if (ix == queue->length ())
705 goto done;
707 /* The depth is the same as SCOPE, find the parent scope. */
708 if (SCOPE_DEPTH (pair.first) == depth)
709 pair.first = CP_DECL_CONTEXT (pair.first);
710 ix++;
712 while (ix < queue->length ());
713 done:;
714 if (scope == global_namespace)
715 break;
717 /* If looking for hidden names, we only look in the innermost
718 namespace scope. [namespace.memdef]/3 If a friend
719 declaration in a non-local class first declares a class,
720 function, class template or function template the friend is a
721 member of the innermost enclosing namespace. See also
722 [basic.lookup.unqual]/7 */
723 if (flags & LOOKUP_HIDDEN)
724 break;
727 vec_safe_truncate (queue, length);
729 return found;
732 /* FNS is a value binding. If it is a (set of overloaded) functions,
733 add them into the current value. */
735 void
736 name_lookup::add_fns (tree fns)
738 if (!fns)
739 return;
740 else if (TREE_CODE (fns) == OVERLOAD)
742 if (TREE_TYPE (fns) != unknown_type_node)
743 fns = OVL_FUNCTION (fns);
745 else if (!DECL_DECLARES_FUNCTION_P (fns))
746 return;
748 add_overload (fns);
751 /* Add functions of a namespace to the lookup structure. */
753 void
754 name_lookup::adl_namespace_only (tree scope)
756 mark_seen (scope);
758 /* Look down into inline namespaces. */
759 if (vec<tree, va_gc> *inlinees = DECL_NAMESPACE_INLINEES (scope))
760 for (unsigned ix = inlinees->length (); ix--;)
761 adl_namespace_only ((*inlinees)[ix]);
763 if (tree fns = find_namespace_value (scope, name))
764 add_fns (ovl_skip_hidden (fns));
767 /* Find the containing non-inlined namespace, add it and all its
768 inlinees. */
770 void
771 name_lookup::adl_namespace (tree scope)
773 if (seen_p (scope))
774 return;
776 /* Find the containing non-inline namespace. */
777 while (DECL_NAMESPACE_INLINE_P (scope))
778 scope = CP_DECL_CONTEXT (scope);
780 adl_namespace_only (scope);
783 /* Adds the class and its friends to the lookup structure. */
785 void
786 name_lookup::adl_class_only (tree type)
788 /* Backend-built structures, such as __builtin_va_list, aren't
789 affected by all this. */
790 if (!CLASS_TYPE_P (type))
791 return;
793 type = TYPE_MAIN_VARIANT (type);
795 if (see_and_mark (type))
796 return;
798 tree context = decl_namespace_context (type);
799 adl_namespace (context);
801 complete_type (type);
803 /* Add friends. */
804 for (tree list = DECL_FRIENDLIST (TYPE_MAIN_DECL (type)); list;
805 list = TREE_CHAIN (list))
806 if (name == FRIEND_NAME (list))
807 for (tree friends = FRIEND_DECLS (list); friends;
808 friends = TREE_CHAIN (friends))
810 tree fn = TREE_VALUE (friends);
812 /* Only interested in global functions with potentially hidden
813 (i.e. unqualified) declarations. */
814 if (CP_DECL_CONTEXT (fn) != context)
815 continue;
817 /* Only interested in anticipated friends. (Non-anticipated
818 ones will have been inserted during the namespace
819 adl.) */
820 if (!DECL_ANTICIPATED (fn))
821 continue;
823 /* Template specializations are never found by name lookup.
824 (Templates themselves can be found, but not template
825 specializations.) */
826 if (TREE_CODE (fn) == FUNCTION_DECL && DECL_USE_TEMPLATE (fn))
827 continue;
829 add_fns (fn);
833 /* Adds the class and its bases to the lookup structure.
834 Returns true on error. */
836 void
837 name_lookup::adl_bases (tree type)
839 adl_class_only (type);
841 /* Process baseclasses. */
842 if (tree binfo = TYPE_BINFO (type))
844 tree base_binfo;
845 int i;
847 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
848 adl_bases (BINFO_TYPE (base_binfo));
852 /* Adds everything associated with a class argument type to the lookup
853 structure. Returns true on error.
855 If T is a class type (including unions), its associated classes are: the
856 class itself; the class of which it is a member, if any; and its direct
857 and indirect base classes. Its associated namespaces are the namespaces
858 of which its associated classes are members. Furthermore, if T is a
859 class template specialization, its associated namespaces and classes
860 also include: the namespaces and classes associated with the types of
861 the template arguments provided for template type parameters (excluding
862 template template parameters); the namespaces of which any template
863 template arguments are members; and the classes of which any member
864 templates used as template template arguments are members. [ Note:
865 non-type template arguments do not contribute to the set of associated
866 namespaces. --end note] */
868 void
869 name_lookup::adl_class (tree type)
871 /* Backend build structures, such as __builtin_va_list, aren't
872 affected by all this. */
873 if (!CLASS_TYPE_P (type))
874 return;
876 type = TYPE_MAIN_VARIANT (type);
877 /* We don't set found here because we have to have set seen first,
878 which is done in the adl_bases walk. */
879 if (found_p (type))
880 return;
882 adl_bases (type);
883 mark_found (type);
885 if (TYPE_CLASS_SCOPE_P (type))
886 adl_class_only (TYPE_CONTEXT (type));
888 /* Process template arguments. */
889 if (CLASSTYPE_TEMPLATE_INFO (type)
890 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
892 tree list = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
893 for (int i = 0; i < TREE_VEC_LENGTH (list); ++i)
894 adl_template_arg (TREE_VEC_ELT (list, i));
898 void
899 name_lookup::adl_expr (tree expr)
901 if (!expr)
902 return;
904 gcc_assert (!TYPE_P (expr));
906 if (TREE_TYPE (expr) != unknown_type_node)
908 adl_type (TREE_TYPE (expr));
909 return;
912 if (TREE_CODE (expr) == ADDR_EXPR)
913 expr = TREE_OPERAND (expr, 0);
914 if (TREE_CODE (expr) == COMPONENT_REF
915 || TREE_CODE (expr) == OFFSET_REF)
916 expr = TREE_OPERAND (expr, 1);
917 expr = MAYBE_BASELINK_FUNCTIONS (expr);
919 if (OVL_P (expr))
920 for (lkp_iterator iter (expr); iter; ++iter)
921 adl_type (TREE_TYPE (*iter));
922 else if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
924 /* The working paper doesn't currently say how to handle
925 template-id arguments. The sensible thing would seem to be
926 to handle the list of template candidates like a normal
927 overload set, and handle the template arguments like we do
928 for class template specializations. */
930 /* First the templates. */
931 adl_expr (TREE_OPERAND (expr, 0));
933 /* Now the arguments. */
934 if (tree args = TREE_OPERAND (expr, 1))
935 for (int ix = TREE_VEC_LENGTH (args); ix--;)
936 adl_template_arg (TREE_VEC_ELT (args, ix));
940 void
941 name_lookup::adl_type (tree type)
943 if (!type)
944 return;
946 if (TYPE_PTRDATAMEM_P (type))
948 /* Pointer to member: associate class type and value type. */
949 adl_type (TYPE_PTRMEM_CLASS_TYPE (type));
950 adl_type (TYPE_PTRMEM_POINTED_TO_TYPE (type));
951 return;
954 switch (TREE_CODE (type))
956 case RECORD_TYPE:
957 if (TYPE_PTRMEMFUNC_P (type))
959 adl_type (TYPE_PTRMEMFUNC_FN_TYPE (type));
960 return;
962 /* FALLTHRU */
963 case UNION_TYPE:
964 adl_class (type);
965 return;
967 case METHOD_TYPE:
968 /* The basetype is referenced in the first arg type, so just
969 fall through. */
970 case FUNCTION_TYPE:
971 /* Associate the parameter types. */
972 for (tree args = TYPE_ARG_TYPES (type); args; args = TREE_CHAIN (args))
973 adl_type (TREE_VALUE (args));
974 /* FALLTHROUGH */
976 case POINTER_TYPE:
977 case REFERENCE_TYPE:
978 case ARRAY_TYPE:
979 adl_type (TREE_TYPE (type));
980 return;
982 case ENUMERAL_TYPE:
983 if (TYPE_CLASS_SCOPE_P (type))
984 adl_class_only (TYPE_CONTEXT (type));
985 adl_namespace (decl_namespace_context (type));
986 return;
988 case LANG_TYPE:
989 gcc_assert (type == unknown_type_node
990 || type == init_list_type_node);
991 return;
993 case TYPE_PACK_EXPANSION:
994 adl_type (PACK_EXPANSION_PATTERN (type));
995 return;
997 default:
998 break;
1002 /* Adds everything associated with a template argument to the lookup
1003 structure. */
1005 void
1006 name_lookup::adl_template_arg (tree arg)
1008 /* [basic.lookup.koenig]
1010 If T is a template-id, its associated namespaces and classes are
1011 ... the namespaces and classes associated with the types of the
1012 template arguments provided for template type parameters
1013 (excluding template template parameters); the namespaces in which
1014 any template template arguments are defined; and the classes in
1015 which any member templates used as template template arguments
1016 are defined. [Note: non-type template arguments do not
1017 contribute to the set of associated namespaces. ] */
1019 /* Consider first template template arguments. */
1020 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
1021 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
1023 else if (TREE_CODE (arg) == TEMPLATE_DECL)
1025 tree ctx = CP_DECL_CONTEXT (arg);
1027 /* It's not a member template. */
1028 if (TREE_CODE (ctx) == NAMESPACE_DECL)
1029 adl_namespace (ctx);
1030 /* Otherwise, it must be member template. */
1031 else
1032 adl_class_only (ctx);
1034 /* It's an argument pack; handle it recursively. */
1035 else if (ARGUMENT_PACK_P (arg))
1037 tree args = ARGUMENT_PACK_ARGS (arg);
1038 int i, len = TREE_VEC_LENGTH (args);
1039 for (i = 0; i < len; ++i)
1040 adl_template_arg (TREE_VEC_ELT (args, i));
1042 /* It's not a template template argument, but it is a type template
1043 argument. */
1044 else if (TYPE_P (arg))
1045 adl_type (arg);
1048 /* Perform ADL lookup. FNS is the existing lookup result and ARGS are
1049 the call arguments. */
1051 tree
1052 name_lookup::search_adl (tree fns, vec<tree, va_gc> *args)
1054 if (fns)
1056 deduping = true;
1057 lookup_mark (fns, true);
1059 value = fns;
1061 unsigned ix;
1062 tree arg;
1064 FOR_EACH_VEC_ELT_REVERSE (*args, ix, arg)
1065 /* OMP reduction operators put an ADL-significant type as the
1066 first arg. */
1067 if (TYPE_P (arg))
1068 adl_type (arg);
1069 else
1070 adl_expr (arg);
1072 fns = value;
1074 return fns;
1077 static bool qualified_namespace_lookup (tree, name_lookup *);
1078 static void consider_binding_level (tree name,
1079 best_match <tree, const char *> &bm,
1080 cp_binding_level *lvl,
1081 bool look_within_fields,
1082 enum lookup_name_fuzzy_kind kind);
1083 static void diagnose_name_conflict (tree, tree);
1085 /* ADL lookup of NAME. FNS is the result of regular lookup, and we
1086 don't add duplicates to it. ARGS is the vector of call
1087 arguments (which will not be empty). */
1089 tree
1090 lookup_arg_dependent (tree name, tree fns, vec<tree, va_gc> *args)
1092 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
1093 name_lookup lookup (name);
1094 fns = lookup.search_adl (fns, args);
1095 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
1096 return fns;
1099 /* FNS is an overload set of conversion functions. Return the
1100 overloads converting to TYPE. */
1102 static tree
1103 extract_conversion_operator (tree fns, tree type)
1105 tree convs = NULL_TREE;
1106 tree tpls = NULL_TREE;
1108 for (ovl_iterator iter (fns); iter; ++iter)
1110 if (same_type_p (DECL_CONV_FN_TYPE (*iter), type))
1111 convs = lookup_add (*iter, convs);
1113 if (TREE_CODE (*iter) == TEMPLATE_DECL)
1114 tpls = lookup_add (*iter, tpls);
1117 if (!convs)
1118 convs = tpls;
1120 return convs;
1123 /* Binary search of (ordered) MEMBER_VEC for NAME. */
1125 static tree
1126 member_vec_binary_search (vec<tree, va_gc> *member_vec, tree name)
1128 for (unsigned lo = 0, hi = member_vec->length (); lo < hi;)
1130 unsigned mid = (lo + hi) / 2;
1131 tree binding = (*member_vec)[mid];
1132 tree binding_name = OVL_NAME (binding);
1134 if (binding_name > name)
1135 hi = mid;
1136 else if (binding_name < name)
1137 lo = mid + 1;
1138 else
1139 return binding;
1142 return NULL_TREE;
1145 /* Linear search of (unordered) MEMBER_VEC for NAME. */
1147 static tree
1148 member_vec_linear_search (vec<tree, va_gc> *member_vec, tree name)
1150 for (int ix = member_vec->length (); ix--;)
1151 if (tree binding = (*member_vec)[ix])
1152 if (OVL_NAME (binding) == name)
1153 return binding;
1155 return NULL_TREE;
1158 /* Linear search of (partially ordered) fields of KLASS for NAME. */
1160 static tree
1161 fields_linear_search (tree klass, tree name, bool want_type)
1163 for (tree fields = TYPE_FIELDS (klass); fields; fields = DECL_CHAIN (fields))
1165 tree decl = fields;
1167 if (TREE_CODE (decl) == FIELD_DECL
1168 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
1170 if (tree temp = search_anon_aggr (TREE_TYPE (decl), name, want_type))
1171 return temp;
1174 if (DECL_NAME (decl) != name)
1175 continue;
1177 if (TREE_CODE (decl) == USING_DECL)
1179 decl = strip_using_decl (decl);
1180 if (is_overloaded_fn (decl))
1181 continue;
1184 if (DECL_DECLARES_FUNCTION_P (decl))
1185 /* Functions are found separately. */
1186 continue;
1188 if (!want_type || DECL_DECLARES_TYPE_P (decl))
1189 return decl;
1192 return NULL_TREE;
1195 /* Look for NAME member inside of anonymous aggregate ANON. Although
1196 such things should only contain FIELD_DECLs, we check that too
1197 late, and would give very confusing errors if we weren't
1198 permissive here. */
1200 tree
1201 search_anon_aggr (tree anon, tree name, bool want_type)
1203 gcc_assert (COMPLETE_TYPE_P (anon));
1204 tree ret = get_class_binding_direct (anon, name, want_type);
1205 return ret;
1208 /* Look for NAME as an immediate member of KLASS (including
1209 anon-members or unscoped enum member). TYPE_OR_FNS is zero for
1210 regular search. >0 to get a type binding (if there is one) and <0
1211 if you want (just) the member function binding.
1213 Use this if you do not want lazy member creation. */
1215 tree
1216 get_class_binding_direct (tree klass, tree name, int type_or_fns)
1218 gcc_checking_assert (RECORD_OR_UNION_TYPE_P (klass));
1220 /* Conversion operators can only be found by the marker conversion
1221 operator name. */
1222 bool conv_op = IDENTIFIER_CONV_OP_P (name);
1223 tree lookup = conv_op ? conv_op_identifier : name;
1224 tree val = NULL_TREE;
1225 vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (klass);
1227 if (COMPLETE_TYPE_P (klass) && member_vec)
1229 val = member_vec_binary_search (member_vec, lookup);
1230 if (!val)
1232 else if (type_or_fns > 0)
1234 if (STAT_HACK_P (val))
1235 val = STAT_TYPE (val);
1236 else if (!DECL_DECLARES_TYPE_P (val))
1237 val = NULL_TREE;
1239 else if (STAT_HACK_P (val))
1240 val = STAT_DECL (val);
1242 if (val && TREE_CODE (val) == OVERLOAD
1243 && TREE_CODE (OVL_FUNCTION (val)) == USING_DECL)
1245 /* An overload with a dependent USING_DECL. Does the caller
1246 want the USING_DECL or the functions? */
1247 if (type_or_fns < 0)
1248 val = OVL_CHAIN (val);
1249 else
1250 val = OVL_FUNCTION (val);
1253 else
1255 if (member_vec && type_or_fns <= 0)
1256 val = member_vec_linear_search (member_vec, lookup);
1258 if (type_or_fns < 0)
1259 /* Don't bother looking for field. We don't want it. */;
1260 else if (!val || (TREE_CODE (val) == OVERLOAD && OVL_USING_P (val)))
1261 /* Dependent using declarations are a 'field', make sure we
1262 return that even if we saw an overload already. */
1263 if (tree field_val = fields_linear_search (klass, lookup,
1264 type_or_fns > 0))
1265 if (!val || TREE_CODE (field_val) == USING_DECL)
1266 val = field_val;
1269 /* Extract the conversion operators asked for, unless the general
1270 conversion operator was requested. */
1271 if (val && conv_op)
1273 gcc_checking_assert (OVL_FUNCTION (val) == conv_op_marker);
1274 val = OVL_CHAIN (val);
1275 if (tree type = TREE_TYPE (name))
1276 val = extract_conversion_operator (val, type);
1279 return val;
1282 /* Look for NAME's binding in exactly KLASS. See
1283 get_class_binding_direct for argument description. Does lazy
1284 special function creation as necessary. */
1286 tree
1287 get_class_binding (tree klass, tree name, int type_or_fns)
1289 klass = complete_type (klass);
1291 if (COMPLETE_TYPE_P (klass))
1293 /* Lazily declare functions, if we're going to search these. */
1294 if (IDENTIFIER_CTOR_P (name))
1296 if (CLASSTYPE_LAZY_DEFAULT_CTOR (klass))
1297 lazily_declare_fn (sfk_constructor, klass);
1298 if (CLASSTYPE_LAZY_COPY_CTOR (klass))
1299 lazily_declare_fn (sfk_copy_constructor, klass);
1300 if (CLASSTYPE_LAZY_MOVE_CTOR (klass))
1301 lazily_declare_fn (sfk_move_constructor, klass);
1303 else if (IDENTIFIER_DTOR_P (name))
1305 if (CLASSTYPE_LAZY_DESTRUCTOR (klass))
1306 lazily_declare_fn (sfk_destructor, klass);
1308 else if (name == assign_op_identifier)
1310 if (CLASSTYPE_LAZY_COPY_ASSIGN (klass))
1311 lazily_declare_fn (sfk_copy_assignment, klass);
1312 if (CLASSTYPE_LAZY_MOVE_ASSIGN (klass))
1313 lazily_declare_fn (sfk_move_assignment, klass);
1317 return get_class_binding_direct (klass, name, type_or_fns);
1320 /* Find the slot containing overloads called 'NAME'. If there is no
1321 such slot and the class is complete, create an empty one, at the
1322 correct point in the sorted member vector. Otherwise return NULL.
1323 Deals with conv_op marker handling. */
1325 tree *
1326 find_member_slot (tree klass, tree name)
1328 bool complete_p = COMPLETE_TYPE_P (klass);
1330 vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (klass);
1331 if (!member_vec)
1333 vec_alloc (member_vec, 8);
1334 CLASSTYPE_MEMBER_VEC (klass) = member_vec;
1335 if (complete_p)
1337 /* If the class is complete but had no member_vec, we need
1338 to add the TYPE_FIELDS into it. We're also most likely
1339 to be adding ctors & dtors, so ask for 6 spare slots (the
1340 abstract cdtors and their clones). */
1341 set_class_bindings (klass, 6);
1342 member_vec = CLASSTYPE_MEMBER_VEC (klass);
1346 if (IDENTIFIER_CONV_OP_P (name))
1347 name = conv_op_identifier;
1349 unsigned ix, length = member_vec->length ();
1350 for (ix = 0; ix < length; ix++)
1352 tree *slot = &(*member_vec)[ix];
1353 tree fn_name = OVL_NAME (*slot);
1355 if (fn_name == name)
1357 /* If we found an existing slot, it must be a function set.
1358 Even with insertion after completion, because those only
1359 happen with artificial fns that have unspellable names.
1360 This means we do not have to deal with the stat hack
1361 either. */
1362 gcc_checking_assert (OVL_P (*slot));
1363 if (name == conv_op_identifier)
1365 gcc_checking_assert (OVL_FUNCTION (*slot) == conv_op_marker);
1366 /* Skip the conv-op marker. */
1367 slot = &OVL_CHAIN (*slot);
1369 return slot;
1372 if (complete_p && fn_name > name)
1373 break;
1376 /* No slot found, add one if the class is complete. */
1377 if (complete_p)
1379 /* Do exact allocation, as we don't expect to add many. */
1380 gcc_assert (name != conv_op_identifier);
1381 vec_safe_reserve_exact (member_vec, 1);
1382 CLASSTYPE_MEMBER_VEC (klass) = member_vec;
1383 member_vec->quick_insert (ix, NULL_TREE);
1384 return &(*member_vec)[ix];
1387 return NULL;
1390 /* KLASS is an incomplete class to which we're adding a method NAME.
1391 Add a slot and deal with conv_op marker handling. */
1393 tree *
1394 add_member_slot (tree klass, tree name)
1396 gcc_assert (!COMPLETE_TYPE_P (klass));
1398 vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (klass);
1399 vec_safe_push (member_vec, NULL_TREE);
1400 CLASSTYPE_MEMBER_VEC (klass) = member_vec;
1402 tree *slot = &member_vec->last ();
1403 if (IDENTIFIER_CONV_OP_P (name))
1405 /* Install the marker prefix. */
1406 *slot = ovl_make (conv_op_marker, NULL_TREE);
1407 slot = &OVL_CHAIN (*slot);
1410 return slot;
1413 /* Comparison function to compare two MEMBER_VEC entries by name.
1414 Because we can have duplicates during insertion of TYPE_FIELDS, we
1415 do extra checking so deduping doesn't have to deal with so many
1416 cases. */
1418 static int
1419 member_name_cmp (const void *a_p, const void *b_p)
1421 tree a = *(const tree *)a_p;
1422 tree b = *(const tree *)b_p;
1423 tree name_a = DECL_NAME (TREE_CODE (a) == OVERLOAD ? OVL_FUNCTION (a) : a);
1424 tree name_b = DECL_NAME (TREE_CODE (b) == OVERLOAD ? OVL_FUNCTION (b) : b);
1426 gcc_checking_assert (name_a && name_b);
1427 if (name_a != name_b)
1428 return name_a < name_b ? -1 : +1;
1430 if (name_a == conv_op_identifier)
1432 /* Strip the conv-op markers. */
1433 gcc_checking_assert (OVL_FUNCTION (a) == conv_op_marker
1434 && OVL_FUNCTION (b) == conv_op_marker);
1435 a = OVL_CHAIN (a);
1436 b = OVL_CHAIN (b);
1439 if (TREE_CODE (a) == OVERLOAD)
1440 a = OVL_FUNCTION (a);
1441 if (TREE_CODE (b) == OVERLOAD)
1442 b = OVL_FUNCTION (b);
1444 /* We're in STAT_HACK or USING_DECL territory (or possibly error-land). */
1445 if (TREE_CODE (a) != TREE_CODE (b))
1447 /* If one of them is a TYPE_DECL, it loses. */
1448 if (TREE_CODE (a) == TYPE_DECL)
1449 return +1;
1450 else if (TREE_CODE (b) == TYPE_DECL)
1451 return -1;
1453 /* If one of them is a USING_DECL, it loses. */
1454 if (TREE_CODE (a) == USING_DECL)
1455 return +1;
1456 else if (TREE_CODE (b) == USING_DECL)
1457 return -1;
1459 /* There are no other cases with different kinds of decls, as
1460 duplicate detection should have kicked in earlier. However,
1461 some erroneous cases get though. */
1462 gcc_assert (errorcount);
1465 /* Using source location would be the best thing here, but we can
1466 get identically-located decls in the following circumstances:
1468 1) duplicate artificial type-decls for the same type.
1470 2) pack expansions of using-decls.
1472 We should not be doing #1, but in either case it doesn't matter
1473 how we order these. Use UID as a proxy for source ordering, so
1474 that identically-located decls still have a well-defined stable
1475 ordering. */
1476 if (DECL_UID (a) != DECL_UID (b))
1477 return DECL_UID (a) < DECL_UID (b) ? -1 : +1;
1478 gcc_assert (a == b);
1479 return 0;
1482 static struct {
1483 gt_pointer_operator new_value;
1484 void *cookie;
1485 } resort_data;
1487 /* This routine compares two fields like member_name_cmp but using the
1488 pointer operator in resort_field_decl_data. We don't have to deal
1489 with duplicates here. */
1491 static int
1492 resort_member_name_cmp (const void *a_p, const void *b_p)
1494 tree a = *(const tree *)a_p;
1495 tree b = *(const tree *)b_p;
1496 tree name_a = OVL_NAME (a);
1497 tree name_b = OVL_NAME (b);
1499 resort_data.new_value (&name_a, resort_data.cookie);
1500 resort_data.new_value (&name_b, resort_data.cookie);
1502 gcc_checking_assert (name_a != name_b);
1504 return name_a < name_b ? -1 : +1;
1507 /* Resort CLASSTYPE_MEMBER_VEC because pointers have been reordered. */
1509 void
1510 resort_type_member_vec (void *obj, void */*orig_obj*/,
1511 gt_pointer_operator new_value, void* cookie)
1513 if (vec<tree, va_gc> *member_vec = (vec<tree, va_gc> *) obj)
1515 resort_data.new_value = new_value;
1516 resort_data.cookie = cookie;
1517 member_vec->qsort (resort_member_name_cmp);
1521 /* Recursively count the number of fields in KLASS, including anonymous
1522 union members. */
1524 static unsigned
1525 count_class_fields (tree klass)
1527 unsigned n_fields = 0;
1529 for (tree fields = TYPE_FIELDS (klass); fields; fields = DECL_CHAIN (fields))
1530 if (DECL_DECLARES_FUNCTION_P (fields))
1531 /* Functions are dealt with separately. */;
1532 else if (TREE_CODE (fields) == FIELD_DECL
1533 && ANON_AGGR_TYPE_P (TREE_TYPE (fields)))
1534 n_fields += count_class_fields (TREE_TYPE (fields));
1535 else if (DECL_NAME (fields))
1536 n_fields += 1;
1538 return n_fields;
1541 /* Append all the nonfunction members fields of KLASS to MEMBER_VEC.
1542 Recurse for anonymous members. MEMBER_VEC must have space. */
1544 static void
1545 member_vec_append_class_fields (vec<tree, va_gc> *member_vec, tree klass)
1547 for (tree fields = TYPE_FIELDS (klass); fields; fields = DECL_CHAIN (fields))
1548 if (DECL_DECLARES_FUNCTION_P (fields))
1549 /* Functions are handled separately. */;
1550 else if (TREE_CODE (fields) == FIELD_DECL
1551 && ANON_AGGR_TYPE_P (TREE_TYPE (fields)))
1552 member_vec_append_class_fields (member_vec, TREE_TYPE (fields));
1553 else if (DECL_NAME (fields))
1555 tree field = fields;
1556 /* Mark a conv-op USING_DECL with the conv-op-marker. */
1557 if (TREE_CODE (field) == USING_DECL
1558 && IDENTIFIER_CONV_OP_P (DECL_NAME (field)))
1559 field = ovl_make (conv_op_marker, field);
1560 member_vec->quick_push (field);
1564 /* Append all of the enum values of ENUMTYPE to MEMBER_VEC.
1565 MEMBER_VEC must have space. */
1567 static void
1568 member_vec_append_enum_values (vec<tree, va_gc> *member_vec, tree enumtype)
1570 for (tree values = TYPE_VALUES (enumtype);
1571 values; values = TREE_CHAIN (values))
1572 member_vec->quick_push (TREE_VALUE (values));
1575 /* MEMBER_VEC has just had new DECLs added to it, but is sorted.
1576 DeDup adjacent DECLS of the same name. We already dealt with
1577 conflict resolution when adding the fields or methods themselves.
1578 There are three cases (which could all be combined):
1579 1) a TYPE_DECL and non TYPE_DECL. Deploy STAT_HACK as appropriate.
1580 2) a USING_DECL and an overload. If the USING_DECL is dependent,
1581 it wins. Otherwise the OVERLOAD does.
1582 3) two USING_DECLS. ...
1584 member_name_cmp will have ordered duplicates as
1585 <fns><using><type> */
1587 static void
1588 member_vec_dedup (vec<tree, va_gc> *member_vec)
1590 unsigned len = member_vec->length ();
1591 unsigned store = 0;
1593 if (!len)
1594 return;
1596 tree name = OVL_NAME ((*member_vec)[0]);
1597 for (unsigned jx, ix = 0; ix < len; ix = jx)
1599 tree current = NULL_TREE;
1600 tree to_type = NULL_TREE;
1601 tree to_using = NULL_TREE;
1602 tree marker = NULL_TREE;
1604 for (jx = ix; jx < len; jx++)
1606 tree next = (*member_vec)[jx];
1607 if (jx != ix)
1609 tree next_name = OVL_NAME (next);
1610 if (next_name != name)
1612 name = next_name;
1613 break;
1617 if (IDENTIFIER_CONV_OP_P (name))
1619 marker = next;
1620 next = OVL_CHAIN (next);
1623 if (TREE_CODE (next) == USING_DECL)
1625 if (IDENTIFIER_CTOR_P (name))
1626 /* Dependent inherited ctor. */
1627 continue;
1629 next = strip_using_decl (next);
1630 if (TREE_CODE (next) == USING_DECL)
1632 to_using = next;
1633 continue;
1636 if (is_overloaded_fn (next))
1637 continue;
1640 if (DECL_DECLARES_TYPE_P (next))
1642 to_type = next;
1643 continue;
1646 if (!current)
1647 current = next;
1650 if (to_using)
1652 if (!current)
1653 current = to_using;
1654 else
1655 current = ovl_make (to_using, current);
1658 if (to_type)
1660 if (!current)
1661 current = to_type;
1662 else
1663 current = stat_hack (current, to_type);
1666 if (current)
1668 if (marker)
1670 OVL_CHAIN (marker) = current;
1671 current = marker;
1673 (*member_vec)[store++] = current;
1677 while (store++ < len)
1678 member_vec->pop ();
1681 /* Add the non-function members to CLASSTYPE_MEMBER_VEC. If there is
1682 no existing MEMBER_VEC and fewer than 8 fields, do nothing. We
1683 know there must be at least 1 field -- the self-reference
1684 TYPE_DECL, except for anon aggregates, which will have at least
1685 one field. */
1687 void
1688 set_class_bindings (tree klass, unsigned extra)
1690 unsigned n_fields = count_class_fields (klass);
1691 vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (klass);
1693 if (member_vec || n_fields >= 8)
1695 /* Append the new fields. */
1696 vec_safe_reserve_exact (member_vec, extra + n_fields);
1697 member_vec_append_class_fields (member_vec, klass);
1700 if (member_vec)
1702 CLASSTYPE_MEMBER_VEC (klass) = member_vec;
1703 member_vec->qsort (member_name_cmp);
1704 member_vec_dedup (member_vec);
1708 /* Insert lately defined enum ENUMTYPE into KLASS for the sorted case. */
1710 void
1711 insert_late_enum_def_bindings (tree klass, tree enumtype)
1713 int n_fields;
1714 vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (klass);
1716 /* The enum bindings will already be on the TYPE_FIELDS, so don't
1717 count them twice. */
1718 if (!member_vec)
1719 n_fields = count_class_fields (klass);
1720 else
1721 n_fields = list_length (TYPE_VALUES (enumtype));
1723 if (member_vec || n_fields >= 8)
1725 vec_safe_reserve_exact (member_vec, n_fields);
1726 if (CLASSTYPE_MEMBER_VEC (klass))
1727 member_vec_append_enum_values (member_vec, enumtype);
1728 else
1729 member_vec_append_class_fields (member_vec, klass);
1730 CLASSTYPE_MEMBER_VEC (klass) = member_vec;
1731 member_vec->qsort (member_name_cmp);
1732 member_vec_dedup (member_vec);
1736 /* Compute the chain index of a binding_entry given the HASH value of its
1737 name and the total COUNT of chains. COUNT is assumed to be a power
1738 of 2. */
1740 #define ENTRY_INDEX(HASH, COUNT) (((HASH) >> 3) & ((COUNT) - 1))
1742 /* A free list of "binding_entry"s awaiting for re-use. */
1744 static GTY((deletable)) binding_entry free_binding_entry = NULL;
1746 /* The binding oracle; see cp-tree.h. */
1748 cp_binding_oracle_function *cp_binding_oracle;
1750 /* If we have a binding oracle, ask it for all namespace-scoped
1751 definitions of NAME. */
1753 static inline void
1754 query_oracle (tree name)
1756 if (!cp_binding_oracle)
1757 return;
1759 /* LOOKED_UP holds the set of identifiers that we have already
1760 looked up with the oracle. */
1761 static hash_set<tree> looked_up;
1762 if (looked_up.add (name))
1763 return;
1765 cp_binding_oracle (CP_ORACLE_IDENTIFIER, name);
1768 /* Create a binding_entry object for (NAME, TYPE). */
1770 static inline binding_entry
1771 binding_entry_make (tree name, tree type)
1773 binding_entry entry;
1775 if (free_binding_entry)
1777 entry = free_binding_entry;
1778 free_binding_entry = entry->chain;
1780 else
1781 entry = ggc_alloc<binding_entry_s> ();
1783 entry->name = name;
1784 entry->type = type;
1785 entry->chain = NULL;
1787 return entry;
1790 /* Put ENTRY back on the free list. */
1791 #if 0
1792 static inline void
1793 binding_entry_free (binding_entry entry)
1795 entry->name = NULL;
1796 entry->type = NULL;
1797 entry->chain = free_binding_entry;
1798 free_binding_entry = entry;
1800 #endif
1802 /* The datatype used to implement the mapping from names to types at
1803 a given scope. */
1804 struct GTY(()) binding_table_s {
1805 /* Array of chains of "binding_entry"s */
1806 binding_entry * GTY((length ("%h.chain_count"))) chain;
1808 /* The number of chains in this table. This is the length of the
1809 member "chain" considered as an array. */
1810 size_t chain_count;
1812 /* Number of "binding_entry"s in this table. */
1813 size_t entry_count;
1816 /* Construct TABLE with an initial CHAIN_COUNT. */
1818 static inline void
1819 binding_table_construct (binding_table table, size_t chain_count)
1821 table->chain_count = chain_count;
1822 table->entry_count = 0;
1823 table->chain = ggc_cleared_vec_alloc<binding_entry> (table->chain_count);
1826 /* Make TABLE's entries ready for reuse. */
1827 #if 0
1828 static void
1829 binding_table_free (binding_table table)
1831 size_t i;
1832 size_t count;
1834 if (table == NULL)
1835 return;
1837 for (i = 0, count = table->chain_count; i < count; ++i)
1839 binding_entry temp = table->chain[i];
1840 while (temp != NULL)
1842 binding_entry entry = temp;
1843 temp = entry->chain;
1844 binding_entry_free (entry);
1846 table->chain[i] = NULL;
1848 table->entry_count = 0;
1850 #endif
1852 /* Allocate a table with CHAIN_COUNT, assumed to be a power of two. */
1854 static inline binding_table
1855 binding_table_new (size_t chain_count)
1857 binding_table table = ggc_alloc<binding_table_s> ();
1858 table->chain = NULL;
1859 binding_table_construct (table, chain_count);
1860 return table;
1863 /* Expand TABLE to twice its current chain_count. */
1865 static void
1866 binding_table_expand (binding_table table)
1868 const size_t old_chain_count = table->chain_count;
1869 const size_t old_entry_count = table->entry_count;
1870 const size_t new_chain_count = 2 * old_chain_count;
1871 binding_entry *old_chains = table->chain;
1872 size_t i;
1874 binding_table_construct (table, new_chain_count);
1875 for (i = 0; i < old_chain_count; ++i)
1877 binding_entry entry = old_chains[i];
1878 for (; entry != NULL; entry = old_chains[i])
1880 const unsigned int hash = IDENTIFIER_HASH_VALUE (entry->name);
1881 const size_t j = ENTRY_INDEX (hash, new_chain_count);
1883 old_chains[i] = entry->chain;
1884 entry->chain = table->chain[j];
1885 table->chain[j] = entry;
1888 table->entry_count = old_entry_count;
1891 /* Insert a binding for NAME to TYPE into TABLE. */
1893 static void
1894 binding_table_insert (binding_table table, tree name, tree type)
1896 const unsigned int hash = IDENTIFIER_HASH_VALUE (name);
1897 const size_t i = ENTRY_INDEX (hash, table->chain_count);
1898 binding_entry entry = binding_entry_make (name, type);
1900 entry->chain = table->chain[i];
1901 table->chain[i] = entry;
1902 ++table->entry_count;
1904 if (3 * table->chain_count < 5 * table->entry_count)
1905 binding_table_expand (table);
1908 /* Return the binding_entry, if any, that maps NAME. */
1910 binding_entry
1911 binding_table_find (binding_table table, tree name)
1913 const unsigned int hash = IDENTIFIER_HASH_VALUE (name);
1914 binding_entry entry = table->chain[ENTRY_INDEX (hash, table->chain_count)];
1916 while (entry != NULL && entry->name != name)
1917 entry = entry->chain;
1919 return entry;
1922 /* Apply PROC -- with DATA -- to all entries in TABLE. */
1924 void
1925 binding_table_foreach (binding_table table, bt_foreach_proc proc, void *data)
1927 size_t chain_count;
1928 size_t i;
1930 if (!table)
1931 return;
1933 chain_count = table->chain_count;
1934 for (i = 0; i < chain_count; ++i)
1936 binding_entry entry = table->chain[i];
1937 for (; entry != NULL; entry = entry->chain)
1938 proc (entry, data);
1942 #ifndef ENABLE_SCOPE_CHECKING
1943 # define ENABLE_SCOPE_CHECKING 0
1944 #else
1945 # define ENABLE_SCOPE_CHECKING 1
1946 #endif
1948 /* A free list of "cxx_binding"s, connected by their PREVIOUS. */
1950 static GTY((deletable)) cxx_binding *free_bindings;
1952 /* Initialize VALUE and TYPE field for BINDING, and set the PREVIOUS
1953 field to NULL. */
1955 static inline void
1956 cxx_binding_init (cxx_binding *binding, tree value, tree type)
1958 binding->value = value;
1959 binding->type = type;
1960 binding->previous = NULL;
1963 /* (GC)-allocate a binding object with VALUE and TYPE member initialized. */
1965 static cxx_binding *
1966 cxx_binding_make (tree value, tree type)
1968 cxx_binding *binding;
1969 if (free_bindings)
1971 binding = free_bindings;
1972 free_bindings = binding->previous;
1974 else
1975 binding = ggc_alloc<cxx_binding> ();
1977 cxx_binding_init (binding, value, type);
1979 return binding;
1982 /* Put BINDING back on the free list. */
1984 static inline void
1985 cxx_binding_free (cxx_binding *binding)
1987 binding->scope = NULL;
1988 binding->previous = free_bindings;
1989 free_bindings = binding;
1992 /* Create a new binding for NAME (with the indicated VALUE and TYPE
1993 bindings) in the class scope indicated by SCOPE. */
1995 static cxx_binding *
1996 new_class_binding (tree name, tree value, tree type, cp_binding_level *scope)
1998 cp_class_binding cb = {cxx_binding_make (value, type), name};
1999 cxx_binding *binding = cb.base;
2000 vec_safe_push (scope->class_shadowed, cb);
2001 binding->scope = scope;
2002 return binding;
2005 /* Make DECL the innermost binding for ID. The LEVEL is the binding
2006 level at which this declaration is being bound. */
2008 void
2009 push_binding (tree id, tree decl, cp_binding_level* level)
2011 cxx_binding *binding;
2013 if (level != class_binding_level)
2015 binding = cxx_binding_make (decl, NULL_TREE);
2016 binding->scope = level;
2018 else
2019 binding = new_class_binding (id, decl, /*type=*/NULL_TREE, level);
2021 /* Now, fill in the binding information. */
2022 binding->previous = IDENTIFIER_BINDING (id);
2023 INHERITED_VALUE_BINDING_P (binding) = 0;
2024 LOCAL_BINDING_P (binding) = (level != class_binding_level);
2026 /* And put it on the front of the list of bindings for ID. */
2027 IDENTIFIER_BINDING (id) = binding;
2030 /* Remove the binding for DECL which should be the innermost binding
2031 for ID. */
2033 void
2034 pop_local_binding (tree id, tree decl)
2036 cxx_binding *binding;
2038 if (id == NULL_TREE)
2039 /* It's easiest to write the loops that call this function without
2040 checking whether or not the entities involved have names. We
2041 get here for such an entity. */
2042 return;
2044 /* Get the innermost binding for ID. */
2045 binding = IDENTIFIER_BINDING (id);
2047 /* The name should be bound. */
2048 gcc_assert (binding != NULL);
2050 /* The DECL will be either the ordinary binding or the type
2051 binding for this identifier. Remove that binding. */
2052 if (binding->value == decl)
2053 binding->value = NULL_TREE;
2054 else
2056 gcc_assert (binding->type == decl);
2057 binding->type = NULL_TREE;
2060 if (!binding->value && !binding->type)
2062 /* We're completely done with the innermost binding for this
2063 identifier. Unhook it from the list of bindings. */
2064 IDENTIFIER_BINDING (id) = binding->previous;
2066 /* Add it to the free list. */
2067 cxx_binding_free (binding);
2071 /* Remove the bindings for the decls of the current level and leave
2072 the current scope. */
2074 void
2075 pop_bindings_and_leave_scope (void)
2077 for (tree t = get_local_decls (); t; t = DECL_CHAIN (t))
2079 tree decl = TREE_CODE (t) == TREE_LIST ? TREE_VALUE (t) : t;
2080 tree name = OVL_NAME (decl);
2082 pop_local_binding (name, decl);
2085 leave_scope ();
2088 /* Strip non dependent using declarations. If DECL is dependent,
2089 surreptitiously create a typename_type and return it. */
2091 tree
2092 strip_using_decl (tree decl)
2094 if (decl == NULL_TREE)
2095 return NULL_TREE;
2097 while (TREE_CODE (decl) == USING_DECL && !DECL_DEPENDENT_P (decl))
2098 decl = USING_DECL_DECLS (decl);
2100 if (TREE_CODE (decl) == USING_DECL && DECL_DEPENDENT_P (decl)
2101 && USING_DECL_TYPENAME_P (decl))
2103 /* We have found a type introduced by a using
2104 declaration at class scope that refers to a dependent
2105 type.
2107 using typename :: [opt] nested-name-specifier unqualified-id ;
2109 decl = make_typename_type (TREE_TYPE (decl),
2110 DECL_NAME (decl),
2111 typename_type, tf_error);
2112 if (decl != error_mark_node)
2113 decl = TYPE_NAME (decl);
2116 return decl;
2119 /* Return true if OVL is an overload for an anticipated builtin. */
2121 static bool
2122 anticipated_builtin_p (tree ovl)
2124 if (TREE_CODE (ovl) != OVERLOAD)
2125 return false;
2127 if (!OVL_HIDDEN_P (ovl))
2128 return false;
2130 tree fn = OVL_FUNCTION (ovl);
2131 gcc_checking_assert (DECL_ANTICIPATED (fn));
2133 if (DECL_HIDDEN_FRIEND_P (fn))
2134 return false;
2136 return true;
2139 /* BINDING records an existing declaration for a name in the current scope.
2140 But, DECL is another declaration for that same identifier in the
2141 same scope. This is the `struct stat' hack whereby a non-typedef
2142 class name or enum-name can be bound at the same level as some other
2143 kind of entity.
2144 3.3.7/1
2146 A class name (9.1) or enumeration name (7.2) can be hidden by the
2147 name of an object, function, or enumerator declared in the same scope.
2148 If a class or enumeration name and an object, function, or enumerator
2149 are declared in the same scope (in any order) with the same name, the
2150 class or enumeration name is hidden wherever the object, function, or
2151 enumerator name is visible.
2153 It's the responsibility of the caller to check that
2154 inserting this name is valid here. Returns nonzero if the new binding
2155 was successful. */
2157 static bool
2158 supplement_binding_1 (cxx_binding *binding, tree decl)
2160 tree bval = binding->value;
2161 bool ok = true;
2162 tree target_bval = strip_using_decl (bval);
2163 tree target_decl = strip_using_decl (decl);
2165 if (TREE_CODE (target_decl) == TYPE_DECL && DECL_ARTIFICIAL (target_decl)
2166 && target_decl != target_bval
2167 && (TREE_CODE (target_bval) != TYPE_DECL
2168 /* We allow pushing an enum multiple times in a class
2169 template in order to handle late matching of underlying
2170 type on an opaque-enum-declaration followed by an
2171 enum-specifier. */
2172 || (processing_template_decl
2173 && TREE_CODE (TREE_TYPE (target_decl)) == ENUMERAL_TYPE
2174 && TREE_CODE (TREE_TYPE (target_bval)) == ENUMERAL_TYPE
2175 && (dependent_type_p (ENUM_UNDERLYING_TYPE
2176 (TREE_TYPE (target_decl)))
2177 || dependent_type_p (ENUM_UNDERLYING_TYPE
2178 (TREE_TYPE (target_bval)))))))
2179 /* The new name is the type name. */
2180 binding->type = decl;
2181 else if (/* TARGET_BVAL is null when push_class_level_binding moves
2182 an inherited type-binding out of the way to make room
2183 for a new value binding. */
2184 !target_bval
2185 /* TARGET_BVAL is error_mark_node when TARGET_DECL's name
2186 has been used in a non-class scope prior declaration.
2187 In that case, we should have already issued a
2188 diagnostic; for graceful error recovery purpose, pretend
2189 this was the intended declaration for that name. */
2190 || target_bval == error_mark_node
2191 /* If TARGET_BVAL is anticipated but has not yet been
2192 declared, pretend it is not there at all. */
2193 || anticipated_builtin_p (target_bval))
2194 binding->value = decl;
2195 else if (TREE_CODE (target_bval) == TYPE_DECL
2196 && DECL_ARTIFICIAL (target_bval)
2197 && target_decl != target_bval
2198 && (TREE_CODE (target_decl) != TYPE_DECL
2199 || same_type_p (TREE_TYPE (target_decl),
2200 TREE_TYPE (target_bval))))
2202 /* The old binding was a type name. It was placed in
2203 VALUE field because it was thought, at the point it was
2204 declared, to be the only entity with such a name. Move the
2205 type name into the type slot; it is now hidden by the new
2206 binding. */
2207 binding->type = bval;
2208 binding->value = decl;
2209 binding->value_is_inherited = false;
2211 else if (TREE_CODE (target_bval) == TYPE_DECL
2212 && TREE_CODE (target_decl) == TYPE_DECL
2213 && DECL_NAME (target_decl) == DECL_NAME (target_bval)
2214 && binding->scope->kind != sk_class
2215 && (same_type_p (TREE_TYPE (target_decl), TREE_TYPE (target_bval))
2216 /* If either type involves template parameters, we must
2217 wait until instantiation. */
2218 || uses_template_parms (TREE_TYPE (target_decl))
2219 || uses_template_parms (TREE_TYPE (target_bval))))
2220 /* We have two typedef-names, both naming the same type to have
2221 the same name. In general, this is OK because of:
2223 [dcl.typedef]
2225 In a given scope, a typedef specifier can be used to redefine
2226 the name of any type declared in that scope to refer to the
2227 type to which it already refers.
2229 However, in class scopes, this rule does not apply due to the
2230 stricter language in [class.mem] prohibiting redeclarations of
2231 members. */
2232 ok = false;
2233 /* There can be two block-scope declarations of the same variable,
2234 so long as they are `extern' declarations. However, there cannot
2235 be two declarations of the same static data member:
2237 [class.mem]
2239 A member shall not be declared twice in the
2240 member-specification. */
2241 else if (VAR_P (target_decl)
2242 && VAR_P (target_bval)
2243 && DECL_EXTERNAL (target_decl) && DECL_EXTERNAL (target_bval)
2244 && !DECL_CLASS_SCOPE_P (target_decl))
2246 duplicate_decls (decl, binding->value, /*newdecl_is_friend=*/false);
2247 ok = false;
2249 else if (TREE_CODE (decl) == NAMESPACE_DECL
2250 && TREE_CODE (bval) == NAMESPACE_DECL
2251 && DECL_NAMESPACE_ALIAS (decl)
2252 && DECL_NAMESPACE_ALIAS (bval)
2253 && ORIGINAL_NAMESPACE (bval) == ORIGINAL_NAMESPACE (decl))
2254 /* [namespace.alias]
2256 In a declarative region, a namespace-alias-definition can be
2257 used to redefine a namespace-alias declared in that declarative
2258 region to refer only to the namespace to which it already
2259 refers. */
2260 ok = false;
2261 else
2263 if (!error_operand_p (bval))
2264 diagnose_name_conflict (decl, bval);
2265 ok = false;
2268 return ok;
2271 /* Diagnose a name conflict between DECL and BVAL. */
2273 static void
2274 diagnose_name_conflict (tree decl, tree bval)
2276 if (TREE_CODE (decl) == TREE_CODE (bval)
2277 && TREE_CODE (decl) != NAMESPACE_DECL
2278 && !DECL_DECLARES_FUNCTION_P (decl)
2279 && (TREE_CODE (decl) != TYPE_DECL
2280 || DECL_ARTIFICIAL (decl) == DECL_ARTIFICIAL (bval))
2281 && CP_DECL_CONTEXT (decl) == CP_DECL_CONTEXT (bval))
2282 error ("redeclaration of %q#D", decl);
2283 else
2284 error ("%q#D conflicts with a previous declaration", decl);
2286 inform (location_of (bval), "previous declaration %q#D", bval);
2289 /* Wrapper for supplement_binding_1. */
2291 static bool
2292 supplement_binding (cxx_binding *binding, tree decl)
2294 bool ret;
2295 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
2296 ret = supplement_binding_1 (binding, decl);
2297 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
2298 return ret;
2301 /* Replace BINDING's current value on its scope's name list with
2302 NEWVAL. */
2304 static void
2305 update_local_overload (cxx_binding *binding, tree newval)
2307 tree *d;
2309 for (d = &binding->scope->names; ; d = &TREE_CHAIN (*d))
2310 if (*d == binding->value)
2312 /* Stitch new list node in. */
2313 *d = tree_cons (NULL_TREE, NULL_TREE, TREE_CHAIN (*d));
2314 break;
2316 else if (TREE_CODE (*d) == TREE_LIST && TREE_VALUE (*d) == binding->value)
2317 break;
2319 TREE_VALUE (*d) = newval;
2322 /* Compares the parameter-type-lists of ONE and TWO and
2323 returns false if they are different. If the DECLs are template
2324 functions, the return types and the template parameter lists are
2325 compared too (DR 565). */
2327 static bool
2328 matching_fn_p (tree one, tree two)
2330 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (one)),
2331 TYPE_ARG_TYPES (TREE_TYPE (two))))
2332 return false;
2334 if (TREE_CODE (one) == TEMPLATE_DECL
2335 && TREE_CODE (two) == TEMPLATE_DECL)
2337 /* Compare template parms. */
2338 if (!comp_template_parms (DECL_TEMPLATE_PARMS (one),
2339 DECL_TEMPLATE_PARMS (two)))
2340 return false;
2342 /* And return type. */
2343 if (!same_type_p (TREE_TYPE (TREE_TYPE (one)),
2344 TREE_TYPE (TREE_TYPE (two))))
2345 return false;
2348 return true;
2351 /* Push DECL into nonclass LEVEL BINDING or SLOT. OLD is the current
2352 binding value (possibly with anticipated builtins stripped).
2353 Diagnose conflicts and return updated decl. */
2355 static tree
2356 update_binding (cp_binding_level *level, cxx_binding *binding, tree *slot,
2357 tree old, tree decl, bool is_friend)
2359 tree to_val = decl;
2360 tree old_type = slot ? MAYBE_STAT_TYPE (*slot) : binding->type;
2361 tree to_type = old_type;
2363 gcc_assert (level->kind == sk_namespace ? !binding
2364 : level->kind != sk_class && !slot);
2365 if (old == error_mark_node)
2366 old = NULL_TREE;
2368 if (TREE_CODE (decl) == TYPE_DECL && DECL_ARTIFICIAL (decl))
2370 tree other = to_type;
2372 if (old && TREE_CODE (old) == TYPE_DECL && DECL_ARTIFICIAL (old))
2373 other = old;
2375 /* Pushing an artificial typedef. See if this matches either
2376 the type slot or the old value slot. */
2377 if (!other)
2379 else if (same_type_p (TREE_TYPE (other), TREE_TYPE (decl)))
2380 /* Two artificial decls to same type. Do nothing. */
2381 return other;
2382 else
2383 goto conflict;
2385 if (old)
2387 /* Slide decl into the type slot, keep old unaltered */
2388 to_type = decl;
2389 to_val = old;
2390 goto done;
2394 if (old && TREE_CODE (old) == TYPE_DECL && DECL_ARTIFICIAL (old))
2396 /* Slide old into the type slot. */
2397 to_type = old;
2398 old = NULL_TREE;
2401 if (DECL_DECLARES_FUNCTION_P (decl))
2403 if (!old)
2405 else if (OVL_P (old))
2407 for (ovl_iterator iter (old); iter; ++iter)
2409 tree fn = *iter;
2411 if (iter.using_p () && matching_fn_p (fn, decl))
2413 /* If a function declaration in namespace scope or
2414 block scope has the same name and the same
2415 parameter-type- list (8.3.5) as a function
2416 introduced by a using-declaration, and the
2417 declarations do not declare the same function,
2418 the program is ill-formed. [namespace.udecl]/14 */
2419 if (tree match = duplicate_decls (decl, fn, is_friend))
2420 return match;
2421 else
2422 /* FIXME: To preserve existing error behavior, we
2423 still push the decl. This might change. */
2424 diagnose_name_conflict (decl, fn);
2428 else
2429 goto conflict;
2431 if (to_type != old_type
2432 && warn_shadow
2433 && MAYBE_CLASS_TYPE_P (TREE_TYPE (to_type))
2434 && !(DECL_IN_SYSTEM_HEADER (decl)
2435 && DECL_IN_SYSTEM_HEADER (to_type)))
2436 warning (OPT_Wshadow, "%q#D hides constructor for %q#D",
2437 decl, to_type);
2439 to_val = ovl_insert (decl, old);
2441 else if (!old)
2443 else if (TREE_CODE (old) != TREE_CODE (decl))
2444 /* Different kinds of decls conflict. */
2445 goto conflict;
2446 else if (TREE_CODE (old) == TYPE_DECL)
2448 if (same_type_p (TREE_TYPE (old), TREE_TYPE (decl)))
2449 /* Two type decls to the same type. Do nothing. */
2450 return old;
2451 else
2452 goto conflict;
2454 else if (TREE_CODE (old) == NAMESPACE_DECL)
2456 /* Two maybe-aliased namespaces. If they're to the same target
2457 namespace, that's ok. */
2458 if (ORIGINAL_NAMESPACE (old) != ORIGINAL_NAMESPACE (decl))
2459 goto conflict;
2461 /* The new one must be an alias at this point. */
2462 gcc_assert (DECL_NAMESPACE_ALIAS (decl));
2463 return old;
2465 else if (TREE_CODE (old) == VAR_DECL)
2467 /* There can be two block-scope declarations of the same
2468 variable, so long as they are `extern' declarations. */
2469 if (!DECL_EXTERNAL (old) || !DECL_EXTERNAL (decl))
2470 goto conflict;
2471 else if (tree match = duplicate_decls (decl, old, false))
2472 return match;
2473 else
2474 goto conflict;
2476 else
2478 conflict:
2479 diagnose_name_conflict (decl, old);
2480 to_val = NULL_TREE;
2483 done:
2484 if (to_val)
2486 if (level->kind == sk_namespace || to_type == decl || to_val == decl)
2487 add_decl_to_level (level, decl);
2488 else
2490 gcc_checking_assert (binding->value && OVL_P (binding->value));
2491 update_local_overload (binding, to_val);
2494 if (slot)
2496 if (STAT_HACK_P (*slot))
2498 STAT_TYPE (*slot) = to_type;
2499 STAT_DECL (*slot) = to_val;
2501 else if (to_type)
2502 *slot = stat_hack (to_val, to_type);
2503 else
2504 *slot = to_val;
2506 else
2508 binding->type = to_type;
2509 binding->value = to_val;
2513 return decl;
2516 /* Table of identifiers to extern C declarations (or LISTS thereof). */
2518 static GTY(()) hash_table<named_decl_hash> *extern_c_decls;
2520 /* DECL has C linkage. If we have an existing instance, make sure the
2521 new one is compatible. Make sure it has the same exception
2522 specification [7.5, 7.6]. Add DECL to the map. */
2524 static void
2525 check_extern_c_conflict (tree decl)
2527 /* Ignore artificial or system header decls. */
2528 if (DECL_ARTIFICIAL (decl) || DECL_IN_SYSTEM_HEADER (decl))
2529 return;
2531 /* This only applies to decls at namespace scope. */
2532 if (!DECL_NAMESPACE_SCOPE_P (decl))
2533 return;
2535 if (!extern_c_decls)
2536 extern_c_decls = hash_table<named_decl_hash>::create_ggc (127);
2538 tree *slot = extern_c_decls
2539 ->find_slot_with_hash (DECL_NAME (decl),
2540 IDENTIFIER_HASH_VALUE (DECL_NAME (decl)), INSERT);
2541 if (tree old = *slot)
2543 if (TREE_CODE (old) == OVERLOAD)
2544 old = OVL_FUNCTION (old);
2546 int mismatch = 0;
2547 if (DECL_CONTEXT (old) == DECL_CONTEXT (decl))
2548 ; /* If they're in the same context, we'll have already complained
2549 about a (possible) mismatch, when inserting the decl. */
2550 else if (!decls_match (decl, old))
2551 mismatch = 1;
2552 else if (TREE_CODE (decl) == FUNCTION_DECL
2553 && !comp_except_specs (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (old)),
2554 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl)),
2555 ce_normal))
2556 mismatch = -1;
2557 else if (DECL_ASSEMBLER_NAME_SET_P (old))
2558 SET_DECL_ASSEMBLER_NAME (decl, DECL_ASSEMBLER_NAME (old));
2560 if (mismatch)
2562 auto_diagnostic_group d;
2563 pedwarn (input_location, 0,
2564 "conflicting C language linkage declaration %q#D", decl);
2565 inform (DECL_SOURCE_LOCATION (old),
2566 "previous declaration %q#D", old);
2567 if (mismatch < 0)
2568 inform (input_location,
2569 "due to different exception specifications");
2571 else
2573 if (old == *slot)
2574 /* The hash table expects OVERLOADS, so construct one with
2575 OLD as both the function and the chain. This allocate
2576 an excess OVERLOAD node, but it's rare to have multiple
2577 extern "C" decls of the same name. And we save
2578 complicating the hash table logic (which is used
2579 elsewhere). */
2580 *slot = ovl_make (old, old);
2582 slot = &OVL_CHAIN (*slot);
2584 /* Chain it on for c_linkage_binding's use. */
2585 *slot = tree_cons (NULL_TREE, decl, *slot);
2588 else
2589 *slot = decl;
2592 /* Returns a list of C-linkage decls with the name NAME. Used in
2593 c-family/c-pragma.c to implement redefine_extname pragma. */
2595 tree
2596 c_linkage_bindings (tree name)
2598 if (extern_c_decls)
2599 if (tree *slot = extern_c_decls
2600 ->find_slot_with_hash (name, IDENTIFIER_HASH_VALUE (name), NO_INSERT))
2602 tree result = *slot;
2603 if (TREE_CODE (result) == OVERLOAD)
2604 result = OVL_CHAIN (result);
2605 return result;
2608 return NULL_TREE;
2611 /* Subroutine of check_local_shadow. */
2613 static void
2614 inform_shadowed (tree shadowed)
2616 inform (DECL_SOURCE_LOCATION (shadowed),
2617 "shadowed declaration is here");
2620 /* DECL is being declared at a local scope. Emit suitable shadow
2621 warnings. */
2623 static void
2624 check_local_shadow (tree decl)
2626 /* Don't complain about the parms we push and then pop
2627 while tentatively parsing a function declarator. */
2628 if (TREE_CODE (decl) == PARM_DECL && !DECL_CONTEXT (decl))
2629 return;
2631 /* Inline decls shadow nothing. */
2632 if (DECL_FROM_INLINE (decl))
2633 return;
2635 /* External decls are something else. */
2636 if (DECL_EXTERNAL (decl))
2637 return;
2639 tree old = NULL_TREE;
2640 cp_binding_level *old_scope = NULL;
2641 if (cxx_binding *binding = outer_binding (DECL_NAME (decl), NULL, true))
2643 old = binding->value;
2644 old_scope = binding->scope;
2647 if (old
2648 && (TREE_CODE (old) == PARM_DECL
2649 || VAR_P (old)
2650 || (TREE_CODE (old) == TYPE_DECL
2651 && (!DECL_ARTIFICIAL (old)
2652 || TREE_CODE (decl) == TYPE_DECL)))
2653 && DECL_FUNCTION_SCOPE_P (old)
2654 && (!DECL_ARTIFICIAL (decl)
2655 || is_capture_proxy (decl)
2656 || DECL_IMPLICIT_TYPEDEF_P (decl)
2657 || (VAR_P (decl) && DECL_ANON_UNION_VAR_P (decl))))
2659 /* DECL shadows a local thing possibly of interest. */
2661 /* DR 2211: check that captures and parameters
2662 do not have the same name. */
2663 if (is_capture_proxy (decl))
2665 if (current_lambda_expr ()
2666 && DECL_CONTEXT (old) == lambda_function (current_lambda_expr ())
2667 && TREE_CODE (old) == PARM_DECL
2668 && DECL_NAME (decl) != this_identifier)
2670 error_at (DECL_SOURCE_LOCATION (old),
2671 "lambda parameter %qD "
2672 "previously declared as a capture", old);
2674 return;
2676 /* Don't complain if it's from an enclosing function. */
2677 else if (DECL_CONTEXT (old) == current_function_decl
2678 && TREE_CODE (decl) != PARM_DECL
2679 && TREE_CODE (old) == PARM_DECL)
2681 /* Go to where the parms should be and see if we find
2682 them there. */
2683 cp_binding_level *b = current_binding_level->level_chain;
2685 if (FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
2686 /* Skip the ctor/dtor cleanup level. */
2687 b = b->level_chain;
2689 /* ARM $8.3 */
2690 if (b->kind == sk_function_parms)
2692 error ("declaration of %q#D shadows a parameter", decl);
2693 return;
2697 /* The local structure or class can't use parameters of
2698 the containing function anyway. */
2699 if (DECL_CONTEXT (old) != current_function_decl)
2701 for (cp_binding_level *scope = current_binding_level;
2702 scope != old_scope; scope = scope->level_chain)
2703 if (scope->kind == sk_class
2704 && !LAMBDA_TYPE_P (scope->this_entity))
2705 return;
2707 /* Error if redeclaring a local declared in a
2708 init-statement or in the condition of an if or
2709 switch statement when the new declaration is in the
2710 outermost block of the controlled statement.
2711 Redeclaring a variable from a for or while condition is
2712 detected elsewhere. */
2713 else if (VAR_P (old)
2714 && old_scope == current_binding_level->level_chain
2715 && (old_scope->kind == sk_cond || old_scope->kind == sk_for))
2717 auto_diagnostic_group d;
2718 error ("redeclaration of %q#D", decl);
2719 inform (DECL_SOURCE_LOCATION (old),
2720 "%q#D previously declared here", old);
2721 return;
2723 /* C++11:
2724 3.3.3/3: The name declared in an exception-declaration (...)
2725 shall not be redeclared in the outermost block of the handler.
2726 3.3.3/2: A parameter name shall not be redeclared (...) in
2727 the outermost block of any handler associated with a
2728 function-try-block.
2729 3.4.1/15: The function parameter names shall not be redeclared
2730 in the exception-declaration nor in the outermost block of a
2731 handler for the function-try-block. */
2732 else if ((TREE_CODE (old) == VAR_DECL
2733 && old_scope == current_binding_level->level_chain
2734 && old_scope->kind == sk_catch)
2735 || (TREE_CODE (old) == PARM_DECL
2736 && (current_binding_level->kind == sk_catch
2737 || current_binding_level->level_chain->kind == sk_catch)
2738 && in_function_try_handler))
2740 auto_diagnostic_group d;
2741 if (permerror (input_location, "redeclaration of %q#D", decl))
2742 inform (DECL_SOURCE_LOCATION (old),
2743 "%q#D previously declared here", old);
2744 return;
2747 /* If '-Wshadow=compatible-local' is specified without other
2748 -Wshadow= flags, we will warn only when the type of the
2749 shadowing variable (DECL) can be converted to that of the
2750 shadowed parameter (OLD_LOCAL). The reason why we only check
2751 if DECL's type can be converted to OLD_LOCAL's type (but not the
2752 other way around) is because when users accidentally shadow a
2753 parameter, more than often they would use the variable
2754 thinking (mistakenly) it's still the parameter. It would be
2755 rare that users would use the variable in the place that
2756 expects the parameter but thinking it's a new decl. */
2758 enum opt_code warning_code;
2759 if (warn_shadow)
2760 warning_code = OPT_Wshadow;
2761 else if (warn_shadow_local)
2762 warning_code = OPT_Wshadow_local;
2763 else if (warn_shadow_compatible_local
2764 && (same_type_p (TREE_TYPE (old), TREE_TYPE (decl))
2765 || (!dependent_type_p (TREE_TYPE (decl))
2766 && !dependent_type_p (TREE_TYPE (old))
2767 /* If the new decl uses auto, we don't yet know
2768 its type (the old type cannot be using auto
2769 at this point, without also being
2770 dependent). This is an indication we're
2771 (now) doing the shadow checking too
2772 early. */
2773 && !type_uses_auto (TREE_TYPE (decl))
2774 && can_convert (TREE_TYPE (old), TREE_TYPE (decl),
2775 tf_none))))
2776 warning_code = OPT_Wshadow_compatible_local;
2777 else
2778 return;
2780 const char *msg;
2781 if (TREE_CODE (old) == PARM_DECL)
2782 msg = "declaration of %q#D shadows a parameter";
2783 else if (is_capture_proxy (old))
2784 msg = "declaration of %qD shadows a lambda capture";
2785 else
2786 msg = "declaration of %qD shadows a previous local";
2788 auto_diagnostic_group d;
2789 if (warning_at (input_location, warning_code, msg, decl))
2790 inform_shadowed (old);
2791 return;
2794 if (!warn_shadow)
2795 return;
2797 /* Don't warn for artificial things that are not implicit typedefs. */
2798 if (DECL_ARTIFICIAL (decl) && !DECL_IMPLICIT_TYPEDEF_P (decl))
2799 return;
2801 if (nonlambda_method_basetype ())
2802 if (tree member = lookup_member (current_nonlambda_class_type (),
2803 DECL_NAME (decl), /*protect=*/0,
2804 /*want_type=*/false, tf_warning_or_error))
2806 member = MAYBE_BASELINK_FUNCTIONS (member);
2808 /* Warn if a variable shadows a non-function, or the variable
2809 is a function or a pointer-to-function. */
2810 if (!OVL_P (member)
2811 || TREE_CODE (decl) == FUNCTION_DECL
2812 || TYPE_PTRFN_P (TREE_TYPE (decl))
2813 || TYPE_PTRMEMFUNC_P (TREE_TYPE (decl)))
2815 auto_diagnostic_group d;
2816 if (warning_at (input_location, OPT_Wshadow,
2817 "declaration of %qD shadows a member of %qT",
2818 decl, current_nonlambda_class_type ())
2819 && DECL_P (member))
2820 inform_shadowed (member);
2822 return;
2825 /* Now look for a namespace shadow. */
2826 old = find_namespace_value (current_namespace, DECL_NAME (decl));
2827 if (old
2828 && (VAR_P (old)
2829 || (TREE_CODE (old) == TYPE_DECL
2830 && (!DECL_ARTIFICIAL (old)
2831 || TREE_CODE (decl) == TYPE_DECL)))
2832 && !instantiating_current_function_p ())
2833 /* XXX shadow warnings in outer-more namespaces */
2835 auto_diagnostic_group d;
2836 if (warning_at (input_location, OPT_Wshadow,
2837 "declaration of %qD shadows a global declaration",
2838 decl))
2839 inform_shadowed (old);
2840 return;
2843 return;
2846 /* DECL is being pushed inside function CTX. Set its context, if
2847 needed. */
2849 static void
2850 set_decl_context_in_fn (tree ctx, tree decl)
2852 if (!DECL_CONTEXT (decl)
2853 /* A local declaration for a function doesn't constitute
2854 nesting. */
2855 && TREE_CODE (decl) != FUNCTION_DECL
2856 /* A local declaration for an `extern' variable is in the
2857 scope of the current namespace, not the current
2858 function. */
2859 && !(VAR_P (decl) && DECL_EXTERNAL (decl))
2860 /* When parsing the parameter list of a function declarator,
2861 don't set DECL_CONTEXT to an enclosing function. When we
2862 push the PARM_DECLs in order to process the function body,
2863 current_binding_level->this_entity will be set. */
2864 && !(TREE_CODE (decl) == PARM_DECL
2865 && current_binding_level->kind == sk_function_parms
2866 && current_binding_level->this_entity == NULL))
2867 DECL_CONTEXT (decl) = ctx;
2869 /* If this is the declaration for a namespace-scope function,
2870 but the declaration itself is in a local scope, mark the
2871 declaration. */
2872 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (decl))
2873 DECL_LOCAL_FUNCTION_P (decl) = 1;
2876 /* DECL is a local-scope decl with linkage. SHADOWED is true if the
2877 name is already bound at the current level.
2879 [basic.link] If there is a visible declaration of an entity with
2880 linkage having the same name and type, ignoring entities declared
2881 outside the innermost enclosing namespace scope, the block scope
2882 declaration declares that same entity and receives the linkage of
2883 the previous declaration.
2885 Also, make sure that this decl matches any existing external decl
2886 in the enclosing namespace. */
2888 static void
2889 set_local_extern_decl_linkage (tree decl, bool shadowed)
2891 tree ns_value = decl; /* Unique marker. */
2893 if (!shadowed)
2895 tree loc_value = innermost_non_namespace_value (DECL_NAME (decl));
2896 if (!loc_value)
2898 ns_value
2899 = find_namespace_value (current_namespace, DECL_NAME (decl));
2900 loc_value = ns_value;
2902 if (loc_value == error_mark_node
2903 /* An ambiguous lookup. */
2904 || (loc_value && TREE_CODE (loc_value) == TREE_LIST))
2905 loc_value = NULL_TREE;
2907 for (ovl_iterator iter (loc_value); iter; ++iter)
2908 if (!iter.hidden_p ()
2909 && (TREE_STATIC (*iter) || DECL_EXTERNAL (*iter))
2910 && decls_match (*iter, decl))
2912 /* The standard only says that the local extern inherits
2913 linkage from the previous decl; in particular, default
2914 args are not shared. Add the decl into a hash table to
2915 make sure only the previous decl in this case is seen
2916 by the middle end. */
2917 struct cxx_int_tree_map *h;
2919 /* We inherit the outer decl's linkage. But we're a
2920 different decl. */
2921 TREE_PUBLIC (decl) = TREE_PUBLIC (*iter);
2923 if (cp_function_chain->extern_decl_map == NULL)
2924 cp_function_chain->extern_decl_map
2925 = hash_table<cxx_int_tree_map_hasher>::create_ggc (20);
2927 h = ggc_alloc<cxx_int_tree_map> ();
2928 h->uid = DECL_UID (decl);
2929 h->to = *iter;
2930 cxx_int_tree_map **loc = cp_function_chain->extern_decl_map
2931 ->find_slot (h, INSERT);
2932 *loc = h;
2933 break;
2937 if (TREE_PUBLIC (decl))
2939 /* DECL is externally visible. Make sure it matches a matching
2940 decl in the namespace scope. We only really need to check
2941 this when inserting the decl, not when we find an existing
2942 match in the current scope. However, in practice we're
2943 going to be inserting a new decl in the majority of cases --
2944 who writes multiple extern decls for the same thing in the
2945 same local scope? Doing it here often avoids a duplicate
2946 namespace lookup. */
2948 /* Avoid repeating a lookup. */
2949 if (ns_value == decl)
2950 ns_value = find_namespace_value (current_namespace, DECL_NAME (decl));
2952 if (ns_value == error_mark_node
2953 || (ns_value && TREE_CODE (ns_value) == TREE_LIST))
2954 ns_value = NULL_TREE;
2956 for (ovl_iterator iter (ns_value); iter; ++iter)
2958 tree other = *iter;
2960 if (!(TREE_PUBLIC (other) || DECL_EXTERNAL (other)))
2961 ; /* Not externally visible. */
2962 else if (DECL_EXTERN_C_P (decl) && DECL_EXTERN_C_P (other))
2963 ; /* Both are extern "C", we'll check via that mechanism. */
2964 else if (TREE_CODE (other) != TREE_CODE (decl)
2965 || ((VAR_P (decl) || matching_fn_p (other, decl))
2966 && !comptypes (TREE_TYPE (decl), TREE_TYPE (other),
2967 COMPARE_REDECLARATION)))
2969 auto_diagnostic_group d;
2970 if (permerror (DECL_SOURCE_LOCATION (decl),
2971 "local external declaration %q#D", decl))
2972 inform (DECL_SOURCE_LOCATION (other),
2973 "does not match previous declaration %q#D", other);
2974 break;
2980 /* Record DECL as belonging to the current lexical scope. Check for
2981 errors (such as an incompatible declaration for the same name
2982 already seen in the same scope). IS_FRIEND is true if DECL is
2983 declared as a friend.
2985 Returns either DECL or an old decl for the same name. If an old
2986 decl is returned, it may have been smashed to agree with what DECL
2987 says. */
2989 static tree
2990 do_pushdecl (tree decl, bool is_friend)
2992 if (decl == error_mark_node)
2993 return error_mark_node;
2995 if (!DECL_TEMPLATE_PARM_P (decl) && current_function_decl)
2996 set_decl_context_in_fn (current_function_decl, decl);
2998 /* The binding level we will be pushing into. During local class
2999 pushing, we want to push to the containing scope. */
3000 cp_binding_level *level = current_binding_level;
3001 while (level->kind == sk_class)
3002 level = level->level_chain;
3004 /* An anonymous namespace has a NULL DECL_NAME, but we still want to
3005 insert it. Other NULL-named decls, not so much. */
3006 tree name = DECL_NAME (decl);
3007 if (name || TREE_CODE (decl) == NAMESPACE_DECL)
3009 cxx_binding *binding = NULL; /* Local scope binding. */
3010 tree ns = NULL_TREE; /* Searched namespace. */
3011 tree *slot = NULL; /* Binding slot in namespace. */
3012 tree old = NULL_TREE;
3014 if (level->kind == sk_namespace)
3016 /* We look in the decl's namespace for an existing
3017 declaration, even though we push into the current
3018 namespace. */
3019 ns = (DECL_NAMESPACE_SCOPE_P (decl)
3020 ? CP_DECL_CONTEXT (decl) : current_namespace);
3021 /* Create the binding, if this is current namespace, because
3022 that's where we'll be pushing anyway. */
3023 slot = find_namespace_slot (ns, name, ns == current_namespace);
3024 if (slot)
3025 old = MAYBE_STAT_DECL (*slot);
3027 else
3029 binding = find_local_binding (level, name);
3030 if (binding)
3031 old = binding->value;
3034 if (current_function_decl && VAR_OR_FUNCTION_DECL_P (decl)
3035 && DECL_EXTERNAL (decl))
3036 set_local_extern_decl_linkage (decl, old != NULL_TREE);
3038 if (old == error_mark_node)
3039 old = NULL_TREE;
3041 for (ovl_iterator iter (old); iter; ++iter)
3042 if (iter.using_p ())
3043 ; /* Ignore using decls here. */
3044 else if (tree match = duplicate_decls (decl, *iter, is_friend))
3046 if (match == error_mark_node)
3048 else if (TREE_CODE (match) == TYPE_DECL)
3049 /* The IDENTIFIER will have the type referring to the
3050 now-smashed TYPE_DECL, because ...? Reset it. */
3051 SET_IDENTIFIER_TYPE_VALUE (name, TREE_TYPE (match));
3052 else if (iter.hidden_p () && !DECL_HIDDEN_P (match))
3054 /* Unhiding a previously hidden decl. */
3055 tree head = iter.reveal_node (old);
3056 if (head != old)
3058 if (!ns)
3060 update_local_overload (binding, head);
3061 binding->value = head;
3063 else if (STAT_HACK_P (*slot))
3064 STAT_DECL (*slot) = head;
3065 else
3066 *slot = head;
3068 if (DECL_EXTERN_C_P (match))
3069 /* We need to check and register the decl now. */
3070 check_extern_c_conflict (match);
3072 return match;
3075 /* We are pushing a new decl. */
3077 /* Skip a hidden builtin we failed to match already. There can
3078 only be one. */
3079 if (old && anticipated_builtin_p (old))
3080 old = OVL_CHAIN (old);
3082 check_template_shadow (decl);
3084 if (DECL_DECLARES_FUNCTION_P (decl))
3086 check_default_args (decl);
3088 if (is_friend)
3090 if (level->kind != sk_namespace)
3092 /* In a local class, a friend function declaration must
3093 find a matching decl in the innermost non-class scope.
3094 [class.friend/11] */
3095 error ("friend declaration %qD in local class without "
3096 "prior local declaration", decl);
3097 /* Don't attempt to push it. */
3098 return error_mark_node;
3100 /* Hide it from ordinary lookup. */
3101 DECL_ANTICIPATED (decl) = DECL_HIDDEN_FRIEND_P (decl) = true;
3105 if (level->kind != sk_namespace)
3107 check_local_shadow (decl);
3109 if (TREE_CODE (decl) == NAMESPACE_DECL)
3110 /* A local namespace alias. */
3111 set_identifier_type_value (name, NULL_TREE);
3113 if (!binding)
3114 binding = create_local_binding (level, name);
3116 else if (!slot)
3118 ns = current_namespace;
3119 slot = find_namespace_slot (ns, name, true);
3120 /* Update OLD to reflect the namespace we're going to be
3121 pushing into. */
3122 old = MAYBE_STAT_DECL (*slot);
3125 old = update_binding (level, binding, slot, old, decl, is_friend);
3127 if (old != decl)
3128 /* An existing decl matched, use it. */
3129 decl = old;
3130 else if (TREE_CODE (decl) == TYPE_DECL)
3132 tree type = TREE_TYPE (decl);
3134 if (type != error_mark_node)
3136 if (TYPE_NAME (type) != decl)
3137 set_underlying_type (decl);
3139 if (!ns)
3140 set_identifier_type_value_with_scope (name, decl, level);
3141 else
3142 SET_IDENTIFIER_TYPE_VALUE (name, global_type_node);
3145 /* If this is a locally defined typedef in a function that
3146 is not a template instantation, record it to implement
3147 -Wunused-local-typedefs. */
3148 if (!instantiating_current_function_p ())
3149 record_locally_defined_typedef (decl);
3151 else if (VAR_P (decl))
3152 maybe_register_incomplete_var (decl);
3154 if ((VAR_P (decl) || TREE_CODE (decl) == FUNCTION_DECL)
3155 && DECL_EXTERN_C_P (decl))
3156 check_extern_c_conflict (decl);
3158 else
3159 add_decl_to_level (level, decl);
3161 return decl;
3164 /* Record a decl-node X as belonging to the current lexical scope.
3165 It's a friend if IS_FRIEND is true -- which affects exactly where
3166 we push it. */
3168 tree
3169 pushdecl (tree x, bool is_friend)
3171 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3172 tree ret = do_pushdecl (x, is_friend);
3173 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3174 return ret;
3177 /* Enter DECL into the symbol table, if that's appropriate. Returns
3178 DECL, or a modified version thereof. */
3180 tree
3181 maybe_push_decl (tree decl)
3183 tree type = TREE_TYPE (decl);
3185 /* Add this decl to the current binding level, but not if it comes
3186 from another scope, e.g. a static member variable. TEM may equal
3187 DECL or it may be a previous decl of the same name. */
3188 if (decl == error_mark_node
3189 || (TREE_CODE (decl) != PARM_DECL
3190 && DECL_CONTEXT (decl) != NULL_TREE
3191 /* Definitions of namespace members outside their namespace are
3192 possible. */
3193 && !DECL_NAMESPACE_SCOPE_P (decl))
3194 || (TREE_CODE (decl) == TEMPLATE_DECL && !namespace_bindings_p ())
3195 || type == unknown_type_node
3196 /* The declaration of a template specialization does not affect
3197 the functions available for overload resolution, so we do not
3198 call pushdecl. */
3199 || (TREE_CODE (decl) == FUNCTION_DECL
3200 && DECL_TEMPLATE_SPECIALIZATION (decl)))
3201 return decl;
3202 else
3203 return pushdecl (decl);
3206 /* Bind DECL to ID in the current_binding_level, assumed to be a local
3207 binding level. If IS_USING is true, DECL got here through a
3208 using-declaration. */
3210 static void
3211 push_local_binding (tree id, tree decl, bool is_using)
3213 /* Skip over any local classes. This makes sense if we call
3214 push_local_binding with a friend decl of a local class. */
3215 cp_binding_level *b = innermost_nonclass_level ();
3217 gcc_assert (b->kind != sk_namespace);
3218 if (find_local_binding (b, id))
3220 /* Supplement the existing binding. */
3221 if (!supplement_binding (IDENTIFIER_BINDING (id), decl))
3222 /* It didn't work. Something else must be bound at this
3223 level. Do not add DECL to the list of things to pop
3224 later. */
3225 return;
3227 else
3228 /* Create a new binding. */
3229 push_binding (id, decl, b);
3231 if (TREE_CODE (decl) == OVERLOAD || is_using)
3232 /* We must put the OVERLOAD or using into a TREE_LIST since we
3233 cannot use the decl's chain itself. */
3234 decl = build_tree_list (NULL_TREE, decl);
3236 /* And put DECL on the list of things declared by the current
3237 binding level. */
3238 add_decl_to_level (b, decl);
3242 /* true means unconditionally make a BLOCK for the next level pushed. */
3244 static bool keep_next_level_flag;
3246 static int binding_depth = 0;
3248 static void
3249 indent (int depth)
3251 int i;
3253 for (i = 0; i < depth * 2; i++)
3254 putc (' ', stderr);
3257 /* Return a string describing the kind of SCOPE we have. */
3258 static const char *
3259 cp_binding_level_descriptor (cp_binding_level *scope)
3261 /* The order of this table must match the "scope_kind"
3262 enumerators. */
3263 static const char* scope_kind_names[] = {
3264 "block-scope",
3265 "cleanup-scope",
3266 "try-scope",
3267 "catch-scope",
3268 "for-scope",
3269 "function-parameter-scope",
3270 "class-scope",
3271 "namespace-scope",
3272 "template-parameter-scope",
3273 "template-explicit-spec-scope"
3275 const scope_kind kind = scope->explicit_spec_p
3276 ? sk_template_spec : scope->kind;
3278 return scope_kind_names[kind];
3281 /* Output a debugging information about SCOPE when performing
3282 ACTION at LINE. */
3283 static void
3284 cp_binding_level_debug (cp_binding_level *scope, int line, const char *action)
3286 const char *desc = cp_binding_level_descriptor (scope);
3287 if (scope->this_entity)
3288 verbatim ("%s %<%s(%E)%> %p %d\n", action, desc,
3289 scope->this_entity, (void *) scope, line);
3290 else
3291 verbatim ("%s %s %p %d\n", action, desc, (void *) scope, line);
3294 /* Return the estimated initial size of the hashtable of a NAMESPACE
3295 scope. */
3297 static inline size_t
3298 namespace_scope_ht_size (tree ns)
3300 tree name = DECL_NAME (ns);
3302 return name == std_identifier
3303 ? NAMESPACE_STD_HT_SIZE
3304 : (name == global_identifier
3305 ? GLOBAL_SCOPE_HT_SIZE
3306 : NAMESPACE_ORDINARY_HT_SIZE);
3309 /* A chain of binding_level structures awaiting reuse. */
3311 static GTY((deletable)) cp_binding_level *free_binding_level;
3313 /* Insert SCOPE as the innermost binding level. */
3315 void
3316 push_binding_level (cp_binding_level *scope)
3318 /* Add it to the front of currently active scopes stack. */
3319 scope->level_chain = current_binding_level;
3320 current_binding_level = scope;
3321 keep_next_level_flag = false;
3323 if (ENABLE_SCOPE_CHECKING)
3325 scope->binding_depth = binding_depth;
3326 indent (binding_depth);
3327 cp_binding_level_debug (scope, LOCATION_LINE (input_location),
3328 "push");
3329 binding_depth++;
3333 /* Create a new KIND scope and make it the top of the active scopes stack.
3334 ENTITY is the scope of the associated C++ entity (namespace, class,
3335 function, C++0x enumeration); it is NULL otherwise. */
3337 cp_binding_level *
3338 begin_scope (scope_kind kind, tree entity)
3340 cp_binding_level *scope;
3342 /* Reuse or create a struct for this binding level. */
3343 if (!ENABLE_SCOPE_CHECKING && free_binding_level)
3345 scope = free_binding_level;
3346 free_binding_level = scope->level_chain;
3347 memset (scope, 0, sizeof (cp_binding_level));
3349 else
3350 scope = ggc_cleared_alloc<cp_binding_level> ();
3352 scope->this_entity = entity;
3353 scope->more_cleanups_ok = true;
3354 switch (kind)
3356 case sk_cleanup:
3357 scope->keep = true;
3358 break;
3360 case sk_template_spec:
3361 scope->explicit_spec_p = true;
3362 kind = sk_template_parms;
3363 /* Fall through. */
3364 case sk_template_parms:
3365 case sk_block:
3366 case sk_try:
3367 case sk_catch:
3368 case sk_for:
3369 case sk_cond:
3370 case sk_class:
3371 case sk_scoped_enum:
3372 case sk_function_parms:
3373 case sk_transaction:
3374 case sk_omp:
3375 scope->keep = keep_next_level_flag;
3376 break;
3378 case sk_namespace:
3379 NAMESPACE_LEVEL (entity) = scope;
3380 break;
3382 default:
3383 /* Should not happen. */
3384 gcc_unreachable ();
3385 break;
3387 scope->kind = kind;
3389 push_binding_level (scope);
3391 return scope;
3394 /* We're about to leave current scope. Pop the top of the stack of
3395 currently active scopes. Return the enclosing scope, now active. */
3397 cp_binding_level *
3398 leave_scope (void)
3400 cp_binding_level *scope = current_binding_level;
3402 if (scope->kind == sk_namespace && class_binding_level)
3403 current_binding_level = class_binding_level;
3405 /* We cannot leave a scope, if there are none left. */
3406 if (NAMESPACE_LEVEL (global_namespace))
3407 gcc_assert (!global_scope_p (scope));
3409 if (ENABLE_SCOPE_CHECKING)
3411 indent (--binding_depth);
3412 cp_binding_level_debug (scope, LOCATION_LINE (input_location),
3413 "leave");
3416 /* Move one nesting level up. */
3417 current_binding_level = scope->level_chain;
3419 /* Namespace-scopes are left most probably temporarily, not
3420 completely; they can be reopened later, e.g. in namespace-extension
3421 or any name binding activity that requires us to resume a
3422 namespace. For classes, we cache some binding levels. For other
3423 scopes, we just make the structure available for reuse. */
3424 if (scope->kind != sk_namespace
3425 && scope->kind != sk_class)
3427 scope->level_chain = free_binding_level;
3428 gcc_assert (!ENABLE_SCOPE_CHECKING
3429 || scope->binding_depth == binding_depth);
3430 free_binding_level = scope;
3433 if (scope->kind == sk_class)
3435 /* Reset DEFINING_CLASS_P to allow for reuse of a
3436 class-defining scope in a non-defining context. */
3437 scope->defining_class_p = 0;
3439 /* Find the innermost enclosing class scope, and reset
3440 CLASS_BINDING_LEVEL appropriately. */
3441 class_binding_level = NULL;
3442 for (scope = current_binding_level; scope; scope = scope->level_chain)
3443 if (scope->kind == sk_class)
3445 class_binding_level = scope;
3446 break;
3450 return current_binding_level;
3453 static void
3454 resume_scope (cp_binding_level* b)
3456 /* Resuming binding levels is meant only for namespaces,
3457 and those cannot nest into classes. */
3458 gcc_assert (!class_binding_level);
3459 /* Also, resuming a non-directly nested namespace is a no-no. */
3460 gcc_assert (b->level_chain == current_binding_level);
3461 current_binding_level = b;
3462 if (ENABLE_SCOPE_CHECKING)
3464 b->binding_depth = binding_depth;
3465 indent (binding_depth);
3466 cp_binding_level_debug (b, LOCATION_LINE (input_location), "resume");
3467 binding_depth++;
3471 /* Return the innermost binding level that is not for a class scope. */
3473 static cp_binding_level *
3474 innermost_nonclass_level (void)
3476 cp_binding_level *b;
3478 b = current_binding_level;
3479 while (b->kind == sk_class)
3480 b = b->level_chain;
3482 return b;
3485 /* We're defining an object of type TYPE. If it needs a cleanup, but
3486 we're not allowed to add any more objects with cleanups to the current
3487 scope, create a new binding level. */
3489 void
3490 maybe_push_cleanup_level (tree type)
3492 if (type != error_mark_node
3493 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
3494 && current_binding_level->more_cleanups_ok == 0)
3496 begin_scope (sk_cleanup, NULL);
3497 current_binding_level->statement_list = push_stmt_list ();
3501 /* Return true if we are in the global binding level. */
3503 bool
3504 global_bindings_p (void)
3506 return global_scope_p (current_binding_level);
3509 /* True if we are currently in a toplevel binding level. This
3510 means either the global binding level or a namespace in a toplevel
3511 binding level. Since there are no non-toplevel namespace levels,
3512 this really means any namespace or template parameter level. We
3513 also include a class whose context is toplevel. */
3515 bool
3516 toplevel_bindings_p (void)
3518 cp_binding_level *b = innermost_nonclass_level ();
3520 return b->kind == sk_namespace || b->kind == sk_template_parms;
3523 /* True if this is a namespace scope, or if we are defining a class
3524 which is itself at namespace scope, or whose enclosing class is
3525 such a class, etc. */
3527 bool
3528 namespace_bindings_p (void)
3530 cp_binding_level *b = innermost_nonclass_level ();
3532 return b->kind == sk_namespace;
3535 /* True if the innermost non-class scope is a block scope. */
3537 bool
3538 local_bindings_p (void)
3540 cp_binding_level *b = innermost_nonclass_level ();
3541 return b->kind < sk_function_parms || b->kind == sk_omp;
3544 /* True if the current level needs to have a BLOCK made. */
3546 bool
3547 kept_level_p (void)
3549 return (current_binding_level->blocks != NULL_TREE
3550 || current_binding_level->keep
3551 || current_binding_level->kind == sk_cleanup
3552 || current_binding_level->names != NULL_TREE
3553 || current_binding_level->using_directives);
3556 /* Returns the kind of the innermost scope. */
3558 scope_kind
3559 innermost_scope_kind (void)
3561 return current_binding_level->kind;
3564 /* Returns true if this scope was created to store template parameters. */
3566 bool
3567 template_parm_scope_p (void)
3569 return innermost_scope_kind () == sk_template_parms;
3572 /* If KEEP is true, make a BLOCK node for the next binding level,
3573 unconditionally. Otherwise, use the normal logic to decide whether
3574 or not to create a BLOCK. */
3576 void
3577 keep_next_level (bool keep)
3579 keep_next_level_flag = keep;
3582 /* Return the list of declarations of the current local scope. */
3584 tree
3585 get_local_decls (void)
3587 gcc_assert (current_binding_level->kind != sk_namespace
3588 && current_binding_level->kind != sk_class);
3589 return current_binding_level->names;
3592 /* Return how many function prototypes we are currently nested inside. */
3595 function_parm_depth (void)
3597 int level = 0;
3598 cp_binding_level *b;
3600 for (b = current_binding_level;
3601 b->kind == sk_function_parms;
3602 b = b->level_chain)
3603 ++level;
3605 return level;
3608 /* For debugging. */
3609 static int no_print_functions = 0;
3610 static int no_print_builtins = 0;
3612 static void
3613 print_binding_level (cp_binding_level* lvl)
3615 tree t;
3616 int i = 0, len;
3617 fprintf (stderr, " blocks=%p", (void *) lvl->blocks);
3618 if (lvl->more_cleanups_ok)
3619 fprintf (stderr, " more-cleanups-ok");
3620 if (lvl->have_cleanups)
3621 fprintf (stderr, " have-cleanups");
3622 fprintf (stderr, "\n");
3623 if (lvl->names)
3625 fprintf (stderr, " names:\t");
3626 /* We can probably fit 3 names to a line? */
3627 for (t = lvl->names; t; t = TREE_CHAIN (t))
3629 if (no_print_functions && (TREE_CODE (t) == FUNCTION_DECL))
3630 continue;
3631 if (no_print_builtins
3632 && (TREE_CODE (t) == TYPE_DECL)
3633 && DECL_IS_BUILTIN (t))
3634 continue;
3636 /* Function decls tend to have longer names. */
3637 if (TREE_CODE (t) == FUNCTION_DECL)
3638 len = 3;
3639 else
3640 len = 2;
3641 i += len;
3642 if (i > 6)
3644 fprintf (stderr, "\n\t");
3645 i = len;
3647 print_node_brief (stderr, "", t, 0);
3648 if (t == error_mark_node)
3649 break;
3651 if (i)
3652 fprintf (stderr, "\n");
3654 if (vec_safe_length (lvl->class_shadowed))
3656 size_t i;
3657 cp_class_binding *b;
3658 fprintf (stderr, " class-shadowed:");
3659 FOR_EACH_VEC_ELT (*lvl->class_shadowed, i, b)
3660 fprintf (stderr, " %s ", IDENTIFIER_POINTER (b->identifier));
3661 fprintf (stderr, "\n");
3663 if (lvl->type_shadowed)
3665 fprintf (stderr, " type-shadowed:");
3666 for (t = lvl->type_shadowed; t; t = TREE_CHAIN (t))
3668 fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
3670 fprintf (stderr, "\n");
3674 DEBUG_FUNCTION void
3675 debug (cp_binding_level &ref)
3677 print_binding_level (&ref);
3680 DEBUG_FUNCTION void
3681 debug (cp_binding_level *ptr)
3683 if (ptr)
3684 debug (*ptr);
3685 else
3686 fprintf (stderr, "<nil>\n");
3690 static void
3691 print_other_binding_stack (cp_binding_level *stack)
3693 cp_binding_level *level;
3694 for (level = stack; !global_scope_p (level); level = level->level_chain)
3696 fprintf (stderr, "binding level %p\n", (void *) level);
3697 print_binding_level (level);
3701 void
3702 print_binding_stack (void)
3704 cp_binding_level *b;
3705 fprintf (stderr, "current_binding_level=%p\n"
3706 "class_binding_level=%p\n"
3707 "NAMESPACE_LEVEL (global_namespace)=%p\n",
3708 (void *) current_binding_level, (void *) class_binding_level,
3709 (void *) NAMESPACE_LEVEL (global_namespace));
3710 if (class_binding_level)
3712 for (b = class_binding_level; b; b = b->level_chain)
3713 if (b == current_binding_level)
3714 break;
3715 if (b)
3716 b = class_binding_level;
3717 else
3718 b = current_binding_level;
3720 else
3721 b = current_binding_level;
3722 print_other_binding_stack (b);
3723 fprintf (stderr, "global:\n");
3724 print_binding_level (NAMESPACE_LEVEL (global_namespace));
3727 /* Return the type associated with ID. */
3729 static tree
3730 identifier_type_value_1 (tree id)
3732 /* There is no type with that name, anywhere. */
3733 if (REAL_IDENTIFIER_TYPE_VALUE (id) == NULL_TREE)
3734 return NULL_TREE;
3735 /* This is not the type marker, but the real thing. */
3736 if (REAL_IDENTIFIER_TYPE_VALUE (id) != global_type_node)
3737 return REAL_IDENTIFIER_TYPE_VALUE (id);
3738 /* Have to search for it. It must be on the global level, now.
3739 Ask lookup_name not to return non-types. */
3740 id = lookup_name_real (id, 2, 1, /*block_p=*/true, 0, 0);
3741 if (id)
3742 return TREE_TYPE (id);
3743 return NULL_TREE;
3746 /* Wrapper for identifier_type_value_1. */
3748 tree
3749 identifier_type_value (tree id)
3751 tree ret;
3752 timevar_start (TV_NAME_LOOKUP);
3753 ret = identifier_type_value_1 (id);
3754 timevar_stop (TV_NAME_LOOKUP);
3755 return ret;
3758 /* Push a definition of struct, union or enum tag named ID. into
3759 binding_level B. DECL is a TYPE_DECL for the type. We assume that
3760 the tag ID is not already defined. */
3762 static void
3763 set_identifier_type_value_with_scope (tree id, tree decl, cp_binding_level *b)
3765 tree type;
3767 if (b->kind != sk_namespace)
3769 /* Shadow the marker, not the real thing, so that the marker
3770 gets restored later. */
3771 tree old_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
3772 b->type_shadowed
3773 = tree_cons (id, old_type_value, b->type_shadowed);
3774 type = decl ? TREE_TYPE (decl) : NULL_TREE;
3775 TREE_TYPE (b->type_shadowed) = type;
3777 else
3779 tree *slot = find_namespace_slot (current_namespace, id, true);
3780 gcc_assert (decl);
3781 update_binding (b, NULL, slot, MAYBE_STAT_DECL (*slot), decl, false);
3783 /* Store marker instead of real type. */
3784 type = global_type_node;
3786 SET_IDENTIFIER_TYPE_VALUE (id, type);
3789 /* As set_identifier_type_value_with_scope, but using
3790 current_binding_level. */
3792 void
3793 set_identifier_type_value (tree id, tree decl)
3795 set_identifier_type_value_with_scope (id, decl, current_binding_level);
3798 /* Return the name for the constructor (or destructor) for the
3799 specified class. */
3801 tree
3802 constructor_name (tree type)
3804 tree decl = TYPE_NAME (TYPE_MAIN_VARIANT (type));
3806 return decl ? DECL_NAME (decl) : NULL_TREE;
3809 /* Returns TRUE if NAME is the name for the constructor for TYPE,
3810 which must be a class type. */
3812 bool
3813 constructor_name_p (tree name, tree type)
3815 gcc_assert (MAYBE_CLASS_TYPE_P (type));
3817 /* These don't have names. */
3818 if (TREE_CODE (type) == DECLTYPE_TYPE
3819 || TREE_CODE (type) == TYPEOF_TYPE)
3820 return false;
3822 if (name && name == constructor_name (type))
3823 return true;
3825 return false;
3828 /* Counter used to create anonymous type names. */
3830 static GTY(()) int anon_cnt;
3832 /* Return an IDENTIFIER which can be used as a name for
3833 unnamed structs and unions. */
3835 tree
3836 make_anon_name (void)
3838 char buf[32];
3840 sprintf (buf, anon_aggrname_format (), anon_cnt++);
3841 return get_identifier (buf);
3844 /* This code is practically identical to that for creating
3845 anonymous names, but is just used for lambdas instead. This isn't really
3846 necessary, but it's convenient to avoid treating lambdas like other
3847 unnamed types. */
3849 static GTY(()) int lambda_cnt = 0;
3851 tree
3852 make_lambda_name (void)
3854 char buf[32];
3856 sprintf (buf, LAMBDANAME_FORMAT, lambda_cnt++);
3857 return get_identifier (buf);
3860 /* Insert another USING_DECL into the current binding level, returning
3861 this declaration. If this is a redeclaration, do nothing, and
3862 return NULL_TREE if this not in namespace scope (in namespace
3863 scope, a using decl might extend any previous bindings). */
3865 static tree
3866 push_using_decl_1 (tree scope, tree name)
3868 tree decl;
3870 gcc_assert (TREE_CODE (scope) == NAMESPACE_DECL);
3871 gcc_assert (identifier_p (name));
3872 for (decl = current_binding_level->usings; decl; decl = DECL_CHAIN (decl))
3873 if (USING_DECL_SCOPE (decl) == scope && DECL_NAME (decl) == name)
3874 break;
3875 if (decl)
3876 return namespace_bindings_p () ? decl : NULL_TREE;
3877 decl = build_lang_decl (USING_DECL, name, NULL_TREE);
3878 USING_DECL_SCOPE (decl) = scope;
3879 DECL_CHAIN (decl) = current_binding_level->usings;
3880 current_binding_level->usings = decl;
3881 return decl;
3884 /* Wrapper for push_using_decl_1. */
3886 static tree
3887 push_using_decl (tree scope, tree name)
3889 tree ret;
3890 timevar_start (TV_NAME_LOOKUP);
3891 ret = push_using_decl_1 (scope, name);
3892 timevar_stop (TV_NAME_LOOKUP);
3893 return ret;
3896 /* Same as pushdecl, but define X in binding-level LEVEL. We rely on the
3897 caller to set DECL_CONTEXT properly.
3899 Note that this must only be used when X will be the new innermost
3900 binding for its name, as we tack it onto the front of IDENTIFIER_BINDING
3901 without checking to see if the current IDENTIFIER_BINDING comes from a
3902 closer binding level than LEVEL. */
3904 static tree
3905 do_pushdecl_with_scope (tree x, cp_binding_level *level, bool is_friend)
3907 cp_binding_level *b;
3909 if (level->kind == sk_class)
3911 b = class_binding_level;
3912 class_binding_level = level;
3913 pushdecl_class_level (x);
3914 class_binding_level = b;
3916 else
3918 tree function_decl = current_function_decl;
3919 if (level->kind == sk_namespace)
3920 current_function_decl = NULL_TREE;
3921 b = current_binding_level;
3922 current_binding_level = level;
3923 x = pushdecl (x, is_friend);
3924 current_binding_level = b;
3925 current_function_decl = function_decl;
3927 return x;
3930 /* Inject X into the local scope just before the function parms. */
3932 tree
3933 pushdecl_outermost_localscope (tree x)
3935 cp_binding_level *b = NULL;
3936 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3938 /* Find the scope just inside the function parms. */
3939 for (cp_binding_level *n = current_binding_level;
3940 n->kind != sk_function_parms; n = b->level_chain)
3941 b = n;
3943 tree ret = b ? do_pushdecl_with_scope (x, b, false) : error_mark_node;
3944 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3946 return ret;
3949 /* Check a non-member using-declaration. Return the name and scope
3950 being used, and the USING_DECL, or NULL_TREE on failure. */
3952 static tree
3953 validate_nonmember_using_decl (tree decl, tree scope, tree name)
3955 /* [namespace.udecl]
3956 A using-declaration for a class member shall be a
3957 member-declaration. */
3958 if (TYPE_P (scope))
3960 error ("%qT is not a namespace or unscoped enum", scope);
3961 return NULL_TREE;
3963 else if (scope == error_mark_node)
3964 return NULL_TREE;
3966 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR)
3968 /* 7.3.3/5
3969 A using-declaration shall not name a template-id. */
3970 error ("a using-declaration cannot specify a template-id. "
3971 "Try %<using %D%>", name);
3972 return NULL_TREE;
3975 if (TREE_CODE (decl) == NAMESPACE_DECL)
3977 error ("namespace %qD not allowed in using-declaration", decl);
3978 return NULL_TREE;
3981 if (TREE_CODE (decl) == SCOPE_REF)
3983 /* It's a nested name with template parameter dependent scope.
3984 This can only be using-declaration for class member. */
3985 error ("%qT is not a namespace", TREE_OPERAND (decl, 0));
3986 return NULL_TREE;
3989 decl = OVL_FIRST (decl);
3991 /* Make a USING_DECL. */
3992 tree using_decl = push_using_decl (scope, name);
3994 if (using_decl == NULL_TREE
3995 && at_function_scope_p ()
3996 && VAR_P (decl))
3997 /* C++11 7.3.3/10. */
3998 error ("%qD is already declared in this scope", name);
4000 return using_decl;
4003 /* Process a local-scope or namespace-scope using declaration. SCOPE
4004 is the nominated scope to search for NAME. VALUE_P and TYPE_P
4005 point to the binding for NAME in the current scope and are
4006 updated. */
4008 static void
4009 do_nonmember_using_decl (tree scope, tree name, tree *value_p, tree *type_p)
4011 name_lookup lookup (name, 0);
4013 if (!qualified_namespace_lookup (scope, &lookup))
4015 error ("%qD not declared", name);
4016 return;
4018 else if (TREE_CODE (lookup.value) == TREE_LIST)
4020 error ("reference to %qD is ambiguous", name);
4021 print_candidates (lookup.value);
4022 lookup.value = NULL_TREE;
4025 if (lookup.type && TREE_CODE (lookup.type) == TREE_LIST)
4027 error ("reference to %qD is ambiguous", name);
4028 print_candidates (lookup.type);
4029 lookup.type = NULL_TREE;
4032 tree value = *value_p;
4033 tree type = *type_p;
4035 /* Shift the old and new bindings around so we're comparing class and
4036 enumeration names to each other. */
4037 if (value && DECL_IMPLICIT_TYPEDEF_P (value))
4039 type = value;
4040 value = NULL_TREE;
4043 if (lookup.value && DECL_IMPLICIT_TYPEDEF_P (lookup.value))
4045 lookup.type = lookup.value;
4046 lookup.value = NULL_TREE;
4049 if (lookup.value && lookup.value != value)
4051 /* Check for using functions. */
4052 if (OVL_P (lookup.value) && (!value || OVL_P (value)))
4054 for (lkp_iterator usings (lookup.value); usings; ++usings)
4056 tree new_fn = *usings;
4058 /* [namespace.udecl]
4060 If a function declaration in namespace scope or block
4061 scope has the same name and the same parameter types as a
4062 function introduced by a using declaration the program is
4063 ill-formed. */
4064 bool found = false;
4065 for (ovl_iterator old (value); !found && old; ++old)
4067 tree old_fn = *old;
4069 if (new_fn == old_fn)
4070 /* The function already exists in the current
4071 namespace. */
4072 found = true;
4073 else if (old.using_p ())
4074 continue; /* This is a using decl. */
4075 else if (old.hidden_p () && !DECL_HIDDEN_FRIEND_P (old_fn))
4076 continue; /* This is an anticipated builtin. */
4077 else if (!matching_fn_p (new_fn, old_fn))
4078 continue; /* Parameters do not match. */
4079 else if (decls_match (new_fn, old_fn))
4080 found = true;
4081 else
4083 diagnose_name_conflict (new_fn, old_fn);
4084 found = true;
4088 if (!found)
4089 /* Unlike the overload case we don't drop anticipated
4090 builtins here. They don't cause a problem, and
4091 we'd like to match them with a future
4092 declaration. */
4093 value = ovl_insert (new_fn, value, true);
4096 else if (value
4097 /* Ignore anticipated builtins. */
4098 && !anticipated_builtin_p (value)
4099 && !decls_match (lookup.value, value))
4100 diagnose_name_conflict (lookup.value, value);
4101 else
4102 value = lookup.value;
4105 if (lookup.type && lookup.type != type)
4107 if (type && !decls_match (lookup.type, type))
4108 diagnose_name_conflict (lookup.type, type);
4109 else
4110 type = lookup.type;
4113 /* If bind->value is empty, shift any class or enumeration name back. */
4114 if (!value)
4116 value = type;
4117 type = NULL_TREE;
4120 *value_p = value;
4121 *type_p = type;
4124 /* Returns true if ANCESTOR encloses DESCENDANT, including matching.
4125 Both are namespaces. */
4127 bool
4128 is_nested_namespace (tree ancestor, tree descendant, bool inline_only)
4130 int depth = SCOPE_DEPTH (ancestor);
4132 if (!depth && !inline_only)
4133 /* The global namespace encloses everything. */
4134 return true;
4136 while (SCOPE_DEPTH (descendant) > depth
4137 && (!inline_only || DECL_NAMESPACE_INLINE_P (descendant)))
4138 descendant = CP_DECL_CONTEXT (descendant);
4140 return ancestor == descendant;
4143 /* Returns true if ROOT (a namespace, class, or function) encloses
4144 CHILD. CHILD may be either a class type or a namespace. */
4146 bool
4147 is_ancestor (tree root, tree child)
4149 gcc_assert ((TREE_CODE (root) == NAMESPACE_DECL
4150 || TREE_CODE (root) == FUNCTION_DECL
4151 || CLASS_TYPE_P (root)));
4152 gcc_assert ((TREE_CODE (child) == NAMESPACE_DECL
4153 || CLASS_TYPE_P (child)));
4155 /* The global namespace encloses everything. */
4156 if (root == global_namespace)
4157 return true;
4159 /* Search until we reach namespace scope. */
4160 while (TREE_CODE (child) != NAMESPACE_DECL)
4162 /* If we've reached the ROOT, it encloses CHILD. */
4163 if (root == child)
4164 return true;
4165 /* Go out one level. */
4166 if (TYPE_P (child))
4167 child = TYPE_NAME (child);
4168 child = CP_DECL_CONTEXT (child);
4171 if (TREE_CODE (root) == NAMESPACE_DECL)
4172 return is_nested_namespace (root, child);
4174 return false;
4177 /* Enter the class or namespace scope indicated by T suitable for name
4178 lookup. T can be arbitrary scope, not necessary nested inside the
4179 current scope. Returns a non-null scope to pop iff pop_scope
4180 should be called later to exit this scope. */
4182 tree
4183 push_scope (tree t)
4185 if (TREE_CODE (t) == NAMESPACE_DECL)
4186 push_decl_namespace (t);
4187 else if (CLASS_TYPE_P (t))
4189 if (!at_class_scope_p ()
4190 || !same_type_p (current_class_type, t))
4191 push_nested_class (t);
4192 else
4193 /* T is the same as the current scope. There is therefore no
4194 need to re-enter the scope. Since we are not actually
4195 pushing a new scope, our caller should not call
4196 pop_scope. */
4197 t = NULL_TREE;
4200 return t;
4203 /* Leave scope pushed by push_scope. */
4205 void
4206 pop_scope (tree t)
4208 if (t == NULL_TREE)
4209 return;
4210 if (TREE_CODE (t) == NAMESPACE_DECL)
4211 pop_decl_namespace ();
4212 else if CLASS_TYPE_P (t)
4213 pop_nested_class ();
4216 /* Subroutine of push_inner_scope. */
4218 static void
4219 push_inner_scope_r (tree outer, tree inner)
4221 tree prev;
4223 if (outer == inner
4224 || (TREE_CODE (inner) != NAMESPACE_DECL && !CLASS_TYPE_P (inner)))
4225 return;
4227 prev = CP_DECL_CONTEXT (TREE_CODE (inner) == NAMESPACE_DECL ? inner : TYPE_NAME (inner));
4228 if (outer != prev)
4229 push_inner_scope_r (outer, prev);
4230 if (TREE_CODE (inner) == NAMESPACE_DECL)
4232 cp_binding_level *save_template_parm = 0;
4233 /* Temporary take out template parameter scopes. They are saved
4234 in reversed order in save_template_parm. */
4235 while (current_binding_level->kind == sk_template_parms)
4237 cp_binding_level *b = current_binding_level;
4238 current_binding_level = b->level_chain;
4239 b->level_chain = save_template_parm;
4240 save_template_parm = b;
4243 resume_scope (NAMESPACE_LEVEL (inner));
4244 current_namespace = inner;
4246 /* Restore template parameter scopes. */
4247 while (save_template_parm)
4249 cp_binding_level *b = save_template_parm;
4250 save_template_parm = b->level_chain;
4251 b->level_chain = current_binding_level;
4252 current_binding_level = b;
4255 else
4256 pushclass (inner);
4259 /* Enter the scope INNER from current scope. INNER must be a scope
4260 nested inside current scope. This works with both name lookup and
4261 pushing name into scope. In case a template parameter scope is present,
4262 namespace is pushed under the template parameter scope according to
4263 name lookup rule in 14.6.1/6.
4265 Return the former current scope suitable for pop_inner_scope. */
4267 tree
4268 push_inner_scope (tree inner)
4270 tree outer = current_scope ();
4271 if (!outer)
4272 outer = current_namespace;
4274 push_inner_scope_r (outer, inner);
4275 return outer;
4278 /* Exit the current scope INNER back to scope OUTER. */
4280 void
4281 pop_inner_scope (tree outer, tree inner)
4283 if (outer == inner
4284 || (TREE_CODE (inner) != NAMESPACE_DECL && !CLASS_TYPE_P (inner)))
4285 return;
4287 while (outer != inner)
4289 if (TREE_CODE (inner) == NAMESPACE_DECL)
4291 cp_binding_level *save_template_parm = 0;
4292 /* Temporary take out template parameter scopes. They are saved
4293 in reversed order in save_template_parm. */
4294 while (current_binding_level->kind == sk_template_parms)
4296 cp_binding_level *b = current_binding_level;
4297 current_binding_level = b->level_chain;
4298 b->level_chain = save_template_parm;
4299 save_template_parm = b;
4302 pop_namespace ();
4304 /* Restore template parameter scopes. */
4305 while (save_template_parm)
4307 cp_binding_level *b = save_template_parm;
4308 save_template_parm = b->level_chain;
4309 b->level_chain = current_binding_level;
4310 current_binding_level = b;
4313 else
4314 popclass ();
4316 inner = CP_DECL_CONTEXT (TREE_CODE (inner) == NAMESPACE_DECL ? inner : TYPE_NAME (inner));
4320 /* Do a pushlevel for class declarations. */
4322 void
4323 pushlevel_class (void)
4325 class_binding_level = begin_scope (sk_class, current_class_type);
4328 /* ...and a poplevel for class declarations. */
4330 void
4331 poplevel_class (void)
4333 cp_binding_level *level = class_binding_level;
4334 cp_class_binding *cb;
4335 size_t i;
4336 tree shadowed;
4338 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4339 gcc_assert (level != 0);
4341 /* If we're leaving a toplevel class, cache its binding level. */
4342 if (current_class_depth == 1)
4343 previous_class_level = level;
4344 for (shadowed = level->type_shadowed;
4345 shadowed;
4346 shadowed = TREE_CHAIN (shadowed))
4347 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (shadowed), TREE_VALUE (shadowed));
4349 /* Remove the bindings for all of the class-level declarations. */
4350 if (level->class_shadowed)
4352 FOR_EACH_VEC_ELT (*level->class_shadowed, i, cb)
4354 IDENTIFIER_BINDING (cb->identifier) = cb->base->previous;
4355 cxx_binding_free (cb->base);
4357 ggc_free (level->class_shadowed);
4358 level->class_shadowed = NULL;
4361 /* Now, pop out of the binding level which we created up in the
4362 `pushlevel_class' routine. */
4363 gcc_assert (current_binding_level == level);
4364 leave_scope ();
4365 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4368 /* Set INHERITED_VALUE_BINDING_P on BINDING to true or false, as
4369 appropriate. DECL is the value to which a name has just been
4370 bound. CLASS_TYPE is the class in which the lookup occurred. */
4372 static void
4373 set_inherited_value_binding_p (cxx_binding *binding, tree decl,
4374 tree class_type)
4376 if (binding->value == decl && TREE_CODE (decl) != TREE_LIST)
4378 tree context;
4380 if (TREE_CODE (decl) == OVERLOAD)
4381 context = ovl_scope (decl);
4382 else
4384 gcc_assert (DECL_P (decl));
4385 context = context_for_name_lookup (decl);
4388 if (is_properly_derived_from (class_type, context))
4389 INHERITED_VALUE_BINDING_P (binding) = 1;
4390 else
4391 INHERITED_VALUE_BINDING_P (binding) = 0;
4393 else if (binding->value == decl)
4394 /* We only encounter a TREE_LIST when there is an ambiguity in the
4395 base classes. Such an ambiguity can be overridden by a
4396 definition in this class. */
4397 INHERITED_VALUE_BINDING_P (binding) = 1;
4398 else
4399 INHERITED_VALUE_BINDING_P (binding) = 0;
4402 /* Make the declaration of X appear in CLASS scope. */
4404 bool
4405 pushdecl_class_level (tree x)
4407 bool is_valid = true;
4408 bool subtime;
4410 /* Do nothing if we're adding to an outer lambda closure type,
4411 outer_binding will add it later if it's needed. */
4412 if (current_class_type != class_binding_level->this_entity)
4413 return true;
4415 subtime = timevar_cond_start (TV_NAME_LOOKUP);
4416 /* Get the name of X. */
4417 tree name = OVL_NAME (x);
4419 if (name)
4421 is_valid = push_class_level_binding (name, x);
4422 if (TREE_CODE (x) == TYPE_DECL)
4423 set_identifier_type_value (name, x);
4425 else if (ANON_AGGR_TYPE_P (TREE_TYPE (x)))
4427 /* If X is an anonymous aggregate, all of its members are
4428 treated as if they were members of the class containing the
4429 aggregate, for naming purposes. */
4430 location_t save_location = input_location;
4431 tree anon = TREE_TYPE (x);
4432 if (vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (anon))
4433 for (unsigned ix = member_vec->length (); ix--;)
4435 tree binding = (*member_vec)[ix];
4436 if (STAT_HACK_P (binding))
4438 if (!pushdecl_class_level (STAT_TYPE (binding)))
4439 is_valid = false;
4440 binding = STAT_DECL (binding);
4442 if (!pushdecl_class_level (binding))
4443 is_valid = false;
4445 else
4446 for (tree f = TYPE_FIELDS (anon); f; f = DECL_CHAIN (f))
4447 if (TREE_CODE (f) == FIELD_DECL)
4449 input_location = DECL_SOURCE_LOCATION (f);
4450 if (!pushdecl_class_level (f))
4451 is_valid = false;
4453 input_location = save_location;
4455 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4456 return is_valid;
4459 /* Return the BINDING (if any) for NAME in SCOPE, which is a class
4460 scope. If the value returned is non-NULL, and the PREVIOUS field
4461 is not set, callers must set the PREVIOUS field explicitly. */
4463 static cxx_binding *
4464 get_class_binding (tree name, cp_binding_level *scope)
4466 tree class_type;
4467 tree type_binding;
4468 tree value_binding;
4469 cxx_binding *binding;
4471 class_type = scope->this_entity;
4473 /* Get the type binding. */
4474 type_binding = lookup_member (class_type, name,
4475 /*protect=*/2, /*want_type=*/true,
4476 tf_warning_or_error);
4477 /* Get the value binding. */
4478 value_binding = lookup_member (class_type, name,
4479 /*protect=*/2, /*want_type=*/false,
4480 tf_warning_or_error);
4482 if (value_binding
4483 && (TREE_CODE (value_binding) == TYPE_DECL
4484 || DECL_CLASS_TEMPLATE_P (value_binding)
4485 || (TREE_CODE (value_binding) == TREE_LIST
4486 && TREE_TYPE (value_binding) == error_mark_node
4487 && (TREE_CODE (TREE_VALUE (value_binding))
4488 == TYPE_DECL))))
4489 /* We found a type binding, even when looking for a non-type
4490 binding. This means that we already processed this binding
4491 above. */
4493 else if (value_binding)
4495 if (TREE_CODE (value_binding) == TREE_LIST
4496 && TREE_TYPE (value_binding) == error_mark_node)
4497 /* NAME is ambiguous. */
4499 else if (BASELINK_P (value_binding))
4500 /* NAME is some overloaded functions. */
4501 value_binding = BASELINK_FUNCTIONS (value_binding);
4504 /* If we found either a type binding or a value binding, create a
4505 new binding object. */
4506 if (type_binding || value_binding)
4508 binding = new_class_binding (name,
4509 value_binding,
4510 type_binding,
4511 scope);
4512 /* This is a class-scope binding, not a block-scope binding. */
4513 LOCAL_BINDING_P (binding) = 0;
4514 set_inherited_value_binding_p (binding, value_binding, class_type);
4516 else
4517 binding = NULL;
4519 return binding;
4522 /* Make the declaration(s) of X appear in CLASS scope under the name
4523 NAME. Returns true if the binding is valid. */
4525 static bool
4526 push_class_level_binding_1 (tree name, tree x)
4528 cxx_binding *binding;
4529 tree decl = x;
4530 bool ok;
4532 /* The class_binding_level will be NULL if x is a template
4533 parameter name in a member template. */
4534 if (!class_binding_level)
4535 return true;
4537 if (name == error_mark_node)
4538 return false;
4540 /* Can happen for an erroneous declaration (c++/60384). */
4541 if (!identifier_p (name))
4543 gcc_assert (errorcount || sorrycount);
4544 return false;
4547 /* Check for invalid member names. But don't worry about a default
4548 argument-scope lambda being pushed after the class is complete. */
4549 gcc_assert (TYPE_BEING_DEFINED (current_class_type)
4550 || LAMBDA_TYPE_P (TREE_TYPE (decl)));
4551 /* Check that we're pushing into the right binding level. */
4552 gcc_assert (current_class_type == class_binding_level->this_entity);
4554 /* We could have been passed a tree list if this is an ambiguous
4555 declaration. If so, pull the declaration out because
4556 check_template_shadow will not handle a TREE_LIST. */
4557 if (TREE_CODE (decl) == TREE_LIST
4558 && TREE_TYPE (decl) == error_mark_node)
4559 decl = TREE_VALUE (decl);
4561 if (!check_template_shadow (decl))
4562 return false;
4564 /* [class.mem]
4566 If T is the name of a class, then each of the following shall
4567 have a name different from T:
4569 -- every static data member of class T;
4571 -- every member of class T that is itself a type;
4573 -- every enumerator of every member of class T that is an
4574 enumerated type;
4576 -- every member of every anonymous union that is a member of
4577 class T.
4579 (Non-static data members were also forbidden to have the same
4580 name as T until TC1.) */
4581 if ((VAR_P (x)
4582 || TREE_CODE (x) == CONST_DECL
4583 || (TREE_CODE (x) == TYPE_DECL
4584 && !DECL_SELF_REFERENCE_P (x))
4585 /* A data member of an anonymous union. */
4586 || (TREE_CODE (x) == FIELD_DECL
4587 && DECL_CONTEXT (x) != current_class_type))
4588 && DECL_NAME (x) == DECL_NAME (TYPE_NAME (current_class_type)))
4590 tree scope = context_for_name_lookup (x);
4591 if (TYPE_P (scope) && same_type_p (scope, current_class_type))
4593 error ("%qD has the same name as the class in which it is "
4594 "declared",
4596 return false;
4600 /* Get the current binding for NAME in this class, if any. */
4601 binding = IDENTIFIER_BINDING (name);
4602 if (!binding || binding->scope != class_binding_level)
4604 binding = get_class_binding (name, class_binding_level);
4605 /* If a new binding was created, put it at the front of the
4606 IDENTIFIER_BINDING list. */
4607 if (binding)
4609 binding->previous = IDENTIFIER_BINDING (name);
4610 IDENTIFIER_BINDING (name) = binding;
4614 /* If there is already a binding, then we may need to update the
4615 current value. */
4616 if (binding && binding->value)
4618 tree bval = binding->value;
4619 tree old_decl = NULL_TREE;
4620 tree target_decl = strip_using_decl (decl);
4621 tree target_bval = strip_using_decl (bval);
4623 if (INHERITED_VALUE_BINDING_P (binding))
4625 /* If the old binding was from a base class, and was for a
4626 tag name, slide it over to make room for the new binding.
4627 The old binding is still visible if explicitly qualified
4628 with a class-key. */
4629 if (TREE_CODE (target_bval) == TYPE_DECL
4630 && DECL_ARTIFICIAL (target_bval)
4631 && !(TREE_CODE (target_decl) == TYPE_DECL
4632 && DECL_ARTIFICIAL (target_decl)))
4634 old_decl = binding->type;
4635 binding->type = bval;
4636 binding->value = NULL_TREE;
4637 INHERITED_VALUE_BINDING_P (binding) = 0;
4639 else
4641 old_decl = bval;
4642 /* Any inherited type declaration is hidden by the type
4643 declaration in the derived class. */
4644 if (TREE_CODE (target_decl) == TYPE_DECL
4645 && DECL_ARTIFICIAL (target_decl))
4646 binding->type = NULL_TREE;
4649 else if (TREE_CODE (target_decl) == OVERLOAD
4650 && OVL_P (target_bval))
4651 old_decl = bval;
4652 else if (TREE_CODE (decl) == USING_DECL
4653 && TREE_CODE (bval) == USING_DECL
4654 && same_type_p (USING_DECL_SCOPE (decl),
4655 USING_DECL_SCOPE (bval)))
4656 /* This is a using redeclaration that will be diagnosed later
4657 in supplement_binding */
4659 else if (TREE_CODE (decl) == USING_DECL
4660 && TREE_CODE (bval) == USING_DECL
4661 && DECL_DEPENDENT_P (decl)
4662 && DECL_DEPENDENT_P (bval))
4663 return true;
4664 else if (TREE_CODE (decl) == USING_DECL
4665 && OVL_P (target_bval))
4666 old_decl = bval;
4667 else if (TREE_CODE (bval) == USING_DECL
4668 && OVL_P (target_decl))
4669 return true;
4671 if (old_decl && binding->scope == class_binding_level)
4673 binding->value = x;
4674 /* It is always safe to clear INHERITED_VALUE_BINDING_P
4675 here. This function is only used to register bindings
4676 from with the class definition itself. */
4677 INHERITED_VALUE_BINDING_P (binding) = 0;
4678 return true;
4682 /* Note that we declared this value so that we can issue an error if
4683 this is an invalid redeclaration of a name already used for some
4684 other purpose. */
4685 note_name_declared_in_class (name, decl);
4687 /* If we didn't replace an existing binding, put the binding on the
4688 stack of bindings for the identifier, and update the shadowed
4689 list. */
4690 if (binding && binding->scope == class_binding_level)
4691 /* Supplement the existing binding. */
4692 ok = supplement_binding (binding, decl);
4693 else
4695 /* Create a new binding. */
4696 push_binding (name, decl, class_binding_level);
4697 ok = true;
4700 return ok;
4703 /* Wrapper for push_class_level_binding_1. */
4705 bool
4706 push_class_level_binding (tree name, tree x)
4708 bool ret;
4709 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4710 ret = push_class_level_binding_1 (name, x);
4711 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4712 return ret;
4715 /* Process "using SCOPE::NAME" in a class scope. Return the
4716 USING_DECL created. */
4718 tree
4719 do_class_using_decl (tree scope, tree name)
4721 if (name == error_mark_node)
4722 return NULL_TREE;
4724 if (!scope || !TYPE_P (scope))
4726 error ("using-declaration for non-member at class scope");
4727 return NULL_TREE;
4730 /* Make sure the name is not invalid */
4731 if (TREE_CODE (name) == BIT_NOT_EXPR)
4733 error ("%<%T::%D%> names destructor", scope, name);
4734 return NULL_TREE;
4737 /* Using T::T declares inheriting ctors, even if T is a typedef. */
4738 if (MAYBE_CLASS_TYPE_P (scope)
4739 && (name == TYPE_IDENTIFIER (scope)
4740 || constructor_name_p (name, scope)))
4742 maybe_warn_cpp0x (CPP0X_INHERITING_CTORS);
4743 name = ctor_identifier;
4744 CLASSTYPE_NON_AGGREGATE (current_class_type) = true;
4747 /* Cannot introduce a constructor name. */
4748 if (constructor_name_p (name, current_class_type))
4750 error ("%<%T::%D%> names constructor in %qT",
4751 scope, name, current_class_type);
4752 return NULL_TREE;
4755 /* From [namespace.udecl]:
4757 A using-declaration used as a member-declaration shall refer to a
4758 member of a base class of the class being defined.
4760 In general, we cannot check this constraint in a template because
4761 we do not know the entire set of base classes of the current
4762 class type. Morover, if SCOPE is dependent, it might match a
4763 non-dependent base. */
4765 tree decl = NULL_TREE;
4766 if (!dependent_scope_p (scope))
4768 base_kind b_kind;
4769 tree binfo = lookup_base (current_class_type, scope, ba_any, &b_kind,
4770 tf_warning_or_error);
4771 if (b_kind < bk_proper_base)
4773 /* If there are dependent bases, scope might resolve at
4774 instantiation time, even if it isn't exactly one of the
4775 dependent bases. */
4776 if (b_kind == bk_same_type || !any_dependent_bases_p ())
4778 error_not_base_type (scope, current_class_type);
4779 return NULL_TREE;
4782 else if (name == ctor_identifier && !binfo_direct_p (binfo))
4784 error ("cannot inherit constructors from indirect base %qT", scope);
4785 return NULL_TREE;
4787 else if (!IDENTIFIER_CONV_OP_P (name)
4788 || !dependent_type_p (TREE_TYPE (name)))
4790 decl = lookup_member (binfo, name, 0, false, tf_warning_or_error);
4791 if (!decl)
4793 error ("no members matching %<%T::%D%> in %q#T", scope, name,
4794 scope);
4795 return NULL_TREE;
4798 /* The binfo from which the functions came does not matter. */
4799 if (BASELINK_P (decl))
4800 decl = BASELINK_FUNCTIONS (decl);
4804 tree value = build_lang_decl (USING_DECL, name, NULL_TREE);
4805 USING_DECL_DECLS (value) = decl;
4806 USING_DECL_SCOPE (value) = scope;
4807 DECL_DEPENDENT_P (value) = !decl;
4809 return value;
4813 /* Return the binding for NAME in NS. If NS is NULL, look in
4814 global_namespace. */
4816 tree
4817 get_namespace_binding (tree ns, tree name)
4819 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4820 if (!ns)
4821 ns = global_namespace;
4822 gcc_checking_assert (!DECL_NAMESPACE_ALIAS (ns));
4823 tree ret = find_namespace_value (ns, name);
4824 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4825 return ret;
4828 /* Push internal DECL into the global namespace. Does not do the
4829 full overload fn handling and does not add it to the list of things
4830 in the namespace. */
4832 void
4833 set_global_binding (tree decl)
4835 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4837 tree *slot = find_namespace_slot (global_namespace, DECL_NAME (decl), true);
4839 if (*slot)
4840 /* The user's placed something in the implementor's namespace. */
4841 diagnose_name_conflict (decl, MAYBE_STAT_DECL (*slot));
4843 /* Force the binding, so compiler internals continue to work. */
4844 *slot = decl;
4846 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4849 /* Set the context of a declaration to scope. Complain if we are not
4850 outside scope. */
4852 void
4853 set_decl_namespace (tree decl, tree scope, bool friendp)
4855 /* Get rid of namespace aliases. */
4856 scope = ORIGINAL_NAMESPACE (scope);
4858 /* It is ok for friends to be qualified in parallel space. */
4859 if (!friendp && !is_nested_namespace (current_namespace, scope))
4860 error ("declaration of %qD not in a namespace surrounding %qD",
4861 decl, scope);
4862 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
4864 /* See whether this has been declared in the namespace or inline
4865 children. */
4866 tree old = NULL_TREE;
4868 name_lookup lookup (DECL_NAME (decl), LOOKUP_HIDDEN);
4869 if (!lookup.search_qualified (scope, /*usings=*/false))
4870 /* No old declaration at all. */
4871 goto not_found;
4872 old = lookup.value;
4875 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
4876 if (TREE_CODE (old) == TREE_LIST)
4878 ambiguous:
4879 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
4880 error ("reference to %qD is ambiguous", decl);
4881 print_candidates (old);
4882 return;
4885 if (!DECL_DECLARES_FUNCTION_P (decl))
4887 /* Don't compare non-function decls with decls_match here, since
4888 it can't check for the correct constness at this
4889 point. pushdecl will find those errors later. */
4891 /* We might have found it in an inline namespace child of SCOPE. */
4892 if (TREE_CODE (decl) == TREE_CODE (old))
4893 DECL_CONTEXT (decl) = DECL_CONTEXT (old);
4895 found:
4896 /* Writing "N::i" to declare something directly in "N" is invalid. */
4897 if (CP_DECL_CONTEXT (decl) == current_namespace
4898 && at_namespace_scope_p ())
4899 error ("explicit qualification in declaration of %qD", decl);
4900 return;
4903 /* Since decl is a function, old should contain a function decl. */
4904 if (!OVL_P (old))
4905 goto not_found;
4907 /* We handle these in check_explicit_instantiation_namespace. */
4908 if (processing_explicit_instantiation)
4909 return;
4910 if (processing_template_decl || processing_specialization)
4911 /* We have not yet called push_template_decl to turn a
4912 FUNCTION_DECL into a TEMPLATE_DECL, so the declarations won't
4913 match. But, we'll check later, when we construct the
4914 template. */
4915 return;
4916 /* Instantiations or specializations of templates may be declared as
4917 friends in any namespace. */
4918 if (friendp && DECL_USE_TEMPLATE (decl))
4919 return;
4921 tree found;
4922 found = NULL_TREE;
4924 for (lkp_iterator iter (old); iter; ++iter)
4926 if (iter.using_p ())
4927 continue;
4929 tree ofn = *iter;
4931 /* Adjust DECL_CONTEXT first so decls_match will return true
4932 if DECL will match a declaration in an inline namespace. */
4933 DECL_CONTEXT (decl) = DECL_CONTEXT (ofn);
4934 if (decls_match (decl, ofn))
4936 if (found)
4938 /* We found more than one matching declaration. */
4939 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
4940 goto ambiguous;
4942 found = ofn;
4946 if (found)
4948 if (DECL_HIDDEN_FRIEND_P (found))
4950 pedwarn (DECL_SOURCE_LOCATION (decl), 0,
4951 "%qD has not been declared within %qD", decl, scope);
4952 inform (DECL_SOURCE_LOCATION (found),
4953 "only here as a %<friend%>");
4955 DECL_CONTEXT (decl) = DECL_CONTEXT (found);
4956 goto found;
4959 not_found:
4960 /* It didn't work, go back to the explicit scope. */
4961 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
4962 error ("%qD should have been declared inside %qD", decl, scope);
4965 /* Return the namespace where the current declaration is declared. */
4967 tree
4968 current_decl_namespace (void)
4970 tree result;
4971 /* If we have been pushed into a different namespace, use it. */
4972 if (!vec_safe_is_empty (decl_namespace_list))
4973 return decl_namespace_list->last ();
4975 if (current_class_type)
4976 result = decl_namespace_context (current_class_type);
4977 else if (current_function_decl)
4978 result = decl_namespace_context (current_function_decl);
4979 else
4980 result = current_namespace;
4981 return result;
4984 /* Process any ATTRIBUTES on a namespace definition. Returns true if
4985 attribute visibility is seen. */
4987 bool
4988 handle_namespace_attrs (tree ns, tree attributes)
4990 tree d;
4991 bool saw_vis = false;
4993 if (attributes == error_mark_node)
4994 return false;
4996 for (d = attributes; d; d = TREE_CHAIN (d))
4998 tree name = get_attribute_name (d);
4999 tree args = TREE_VALUE (d);
5001 if (is_attribute_p ("visibility", name))
5003 /* attribute visibility is a property of the syntactic block
5004 rather than the namespace as a whole, so we don't touch the
5005 NAMESPACE_DECL at all. */
5006 tree x = args ? TREE_VALUE (args) : NULL_TREE;
5007 if (x == NULL_TREE || TREE_CODE (x) != STRING_CST || TREE_CHAIN (args))
5009 warning (OPT_Wattributes,
5010 "%qD attribute requires a single NTBS argument",
5011 name);
5012 continue;
5015 if (!TREE_PUBLIC (ns))
5016 warning (OPT_Wattributes,
5017 "%qD attribute is meaningless since members of the "
5018 "anonymous namespace get local symbols", name);
5020 push_visibility (TREE_STRING_POINTER (x), 1);
5021 saw_vis = true;
5023 else if (is_attribute_p ("abi_tag", name))
5025 if (!DECL_NAME (ns))
5027 warning (OPT_Wattributes, "ignoring %qD attribute on anonymous "
5028 "namespace", name);
5029 continue;
5031 if (!DECL_NAMESPACE_INLINE_P (ns))
5033 warning (OPT_Wattributes, "ignoring %qD attribute on non-inline "
5034 "namespace", name);
5035 continue;
5037 if (!args)
5039 tree dn = DECL_NAME (ns);
5040 args = build_string (IDENTIFIER_LENGTH (dn) + 1,
5041 IDENTIFIER_POINTER (dn));
5042 TREE_TYPE (args) = char_array_type_node;
5043 args = fix_string_type (args);
5044 args = build_tree_list (NULL_TREE, args);
5046 if (check_abi_tag_args (args, name))
5047 DECL_ATTRIBUTES (ns) = tree_cons (name, args,
5048 DECL_ATTRIBUTES (ns));
5050 else
5052 warning (OPT_Wattributes, "%qD attribute directive ignored",
5053 name);
5054 continue;
5058 return saw_vis;
5061 /* Temporarily set the namespace for the current declaration. */
5063 void
5064 push_decl_namespace (tree decl)
5066 if (TREE_CODE (decl) != NAMESPACE_DECL)
5067 decl = decl_namespace_context (decl);
5068 vec_safe_push (decl_namespace_list, ORIGINAL_NAMESPACE (decl));
5071 /* [namespace.memdef]/2 */
5073 void
5074 pop_decl_namespace (void)
5076 decl_namespace_list->pop ();
5079 /* Process a namespace-alias declaration. */
5081 void
5082 do_namespace_alias (tree alias, tree name_space)
5084 if (name_space == error_mark_node)
5085 return;
5087 gcc_assert (TREE_CODE (name_space) == NAMESPACE_DECL);
5089 name_space = ORIGINAL_NAMESPACE (name_space);
5091 /* Build the alias. */
5092 alias = build_lang_decl (NAMESPACE_DECL, alias, void_type_node);
5093 DECL_NAMESPACE_ALIAS (alias) = name_space;
5094 DECL_EXTERNAL (alias) = 1;
5095 DECL_CONTEXT (alias) = FROB_CONTEXT (current_scope ());
5096 pushdecl (alias);
5098 /* Emit debug info for namespace alias. */
5099 if (!building_stmt_list_p ())
5100 (*debug_hooks->early_global_decl) (alias);
5103 /* Like pushdecl, only it places X in the current namespace,
5104 if appropriate. */
5106 tree
5107 pushdecl_namespace_level (tree x, bool is_friend)
5109 cp_binding_level *b = current_binding_level;
5110 tree t;
5112 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
5113 t = do_pushdecl_with_scope
5114 (x, NAMESPACE_LEVEL (current_namespace), is_friend);
5116 /* Now, the type_shadowed stack may screw us. Munge it so it does
5117 what we want. */
5118 if (TREE_CODE (t) == TYPE_DECL)
5120 tree name = DECL_NAME (t);
5121 tree newval;
5122 tree *ptr = (tree *)0;
5123 for (; !global_scope_p (b); b = b->level_chain)
5125 tree shadowed = b->type_shadowed;
5126 for (; shadowed; shadowed = TREE_CHAIN (shadowed))
5127 if (TREE_PURPOSE (shadowed) == name)
5129 ptr = &TREE_VALUE (shadowed);
5130 /* Can't break out of the loop here because sometimes
5131 a binding level will have duplicate bindings for
5132 PT names. It's gross, but I haven't time to fix it. */
5135 newval = TREE_TYPE (t);
5136 if (ptr == (tree *)0)
5138 /* @@ This shouldn't be needed. My test case "zstring.cc" trips
5139 up here if this is changed to an assertion. --KR */
5140 SET_IDENTIFIER_TYPE_VALUE (name, t);
5142 else
5144 *ptr = newval;
5147 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
5148 return t;
5151 /* Process a using-declaration appearing in namespace scope. */
5153 void
5154 finish_namespace_using_decl (tree decl, tree scope, tree name)
5156 tree orig_decl = decl;
5158 gcc_checking_assert (current_binding_level->kind == sk_namespace
5159 && !processing_template_decl);
5160 decl = validate_nonmember_using_decl (decl, scope, name);
5161 if (decl == NULL_TREE)
5162 return;
5164 tree *slot = find_namespace_slot (current_namespace, name, true);
5165 tree val = slot ? MAYBE_STAT_DECL (*slot) : NULL_TREE;
5166 tree type = slot ? MAYBE_STAT_TYPE (*slot) : NULL_TREE;
5167 do_nonmember_using_decl (scope, name, &val, &type);
5168 if (STAT_HACK_P (*slot))
5170 STAT_DECL (*slot) = val;
5171 STAT_TYPE (*slot) = type;
5173 else if (type)
5174 *slot = stat_hack (val, type);
5175 else
5176 *slot = val;
5178 /* Emit debug info. */
5179 cp_emit_debug_info_for_using (orig_decl, current_namespace);
5182 /* Process a using-declaration at function scope. */
5184 void
5185 finish_local_using_decl (tree decl, tree scope, tree name)
5187 tree orig_decl = decl;
5189 gcc_checking_assert (current_binding_level->kind != sk_class
5190 && current_binding_level->kind != sk_namespace);
5191 decl = validate_nonmember_using_decl (decl, scope, name);
5192 if (decl == NULL_TREE)
5193 return;
5195 add_decl_expr (decl);
5197 cxx_binding *binding = find_local_binding (current_binding_level, name);
5198 tree value = binding ? binding->value : NULL_TREE;
5199 tree type = binding ? binding->type : NULL_TREE;
5201 do_nonmember_using_decl (scope, name, &value, &type);
5203 if (!value)
5205 else if (binding && value == binding->value)
5207 else if (binding && binding->value && TREE_CODE (value) == OVERLOAD)
5209 update_local_overload (IDENTIFIER_BINDING (name), value);
5210 IDENTIFIER_BINDING (name)->value = value;
5212 else
5213 /* Install the new binding. */
5214 push_local_binding (name, value, true);
5216 if (!type)
5218 else if (binding && type == binding->type)
5220 else
5222 push_local_binding (name, type, true);
5223 set_identifier_type_value (name, type);
5226 /* Emit debug info. */
5227 if (!processing_template_decl)
5228 cp_emit_debug_info_for_using (orig_decl, current_scope ());
5231 /* Return the declarations that are members of the namespace NS. */
5233 tree
5234 cp_namespace_decls (tree ns)
5236 return NAMESPACE_LEVEL (ns)->names;
5239 /* Combine prefer_type and namespaces_only into flags. */
5241 static int
5242 lookup_flags (int prefer_type, int namespaces_only)
5244 if (namespaces_only)
5245 return LOOKUP_PREFER_NAMESPACES;
5246 if (prefer_type > 1)
5247 return LOOKUP_PREFER_TYPES;
5248 if (prefer_type > 0)
5249 return LOOKUP_PREFER_BOTH;
5250 return 0;
5253 /* Given a lookup that returned VAL, use FLAGS to decide if we want to
5254 ignore it or not. Subroutine of lookup_name_real and
5255 lookup_type_scope. */
5257 static bool
5258 qualify_lookup (tree val, int flags)
5260 if (val == NULL_TREE)
5261 return false;
5262 if ((flags & LOOKUP_PREFER_NAMESPACES) && TREE_CODE (val) == NAMESPACE_DECL)
5263 return true;
5264 if (flags & LOOKUP_PREFER_TYPES)
5266 tree target_val = strip_using_decl (val);
5267 if (TREE_CODE (target_val) == TYPE_DECL
5268 || TREE_CODE (target_val) == TEMPLATE_DECL)
5269 return true;
5271 if (flags & (LOOKUP_PREFER_NAMESPACES | LOOKUP_PREFER_TYPES))
5272 return false;
5273 /* Look through lambda things that we shouldn't be able to see. */
5274 if (!(flags & LOOKUP_HIDDEN) && is_lambda_ignored_entity (val))
5275 return false;
5276 return true;
5279 /* Is there a "using namespace std;" directive within USINGS? */
5281 static bool
5282 using_directives_contain_std_p (vec<tree, va_gc> *usings)
5284 if (!usings)
5285 return false;
5287 for (unsigned ix = usings->length (); ix--;)
5288 if ((*usings)[ix] == std_node)
5289 return true;
5291 return false;
5294 /* Is there a "using namespace std;" directive within the current
5295 namespace (or its ancestors)?
5296 Compare with name_lookup::search_unqualified. */
5298 static bool
5299 has_using_namespace_std_directive_p ()
5301 /* Look at local using-directives. */
5302 for (cp_binding_level *level = current_binding_level;
5303 level->kind != sk_namespace;
5304 level = level->level_chain)
5305 if (using_directives_contain_std_p (level->using_directives))
5306 return true;
5308 /* Look at this namespace and its ancestors. */
5309 for (tree scope = current_namespace; scope; scope = CP_DECL_CONTEXT (scope))
5311 if (using_directives_contain_std_p (DECL_NAMESPACE_USING (scope)))
5312 return true;
5314 if (scope == global_namespace)
5315 break;
5318 return false;
5321 /* Suggest alternatives for NAME, an IDENTIFIER_NODE for which name
5322 lookup failed. Search through all available namespaces and print out
5323 possible candidates. If no exact matches are found, and
5324 SUGGEST_MISSPELLINGS is true, then also look for near-matches and
5325 suggest the best near-match, if there is one. */
5327 void
5328 suggest_alternatives_for (location_t location, tree name,
5329 bool suggest_misspellings)
5331 vec<tree> candidates = vNULL;
5332 vec<tree> worklist = vNULL;
5333 unsigned limit = PARAM_VALUE (CXX_MAX_NAMESPACES_FOR_DIAGNOSTIC_HELP);
5334 bool limited = false;
5336 /* Breadth-first search of namespaces. Up to limit namespaces
5337 searched (limit zero == unlimited). */
5338 worklist.safe_push (global_namespace);
5339 for (unsigned ix = 0; ix != worklist.length (); ix++)
5341 tree ns = worklist[ix];
5342 name_lookup lookup (name);
5344 if (lookup.search_qualified (ns, false))
5345 candidates.safe_push (lookup.value);
5347 if (!limited)
5349 /* Look for child namespaces. We have to do this
5350 indirectly because they are chained in reverse order,
5351 which is confusing to the user. */
5352 vec<tree> children = vNULL;
5354 for (tree decl = NAMESPACE_LEVEL (ns)->names;
5355 decl; decl = TREE_CHAIN (decl))
5356 if (TREE_CODE (decl) == NAMESPACE_DECL
5357 && !DECL_NAMESPACE_ALIAS (decl)
5358 && !DECL_NAMESPACE_INLINE_P (decl))
5359 children.safe_push (decl);
5361 while (!limited && !children.is_empty ())
5363 if (worklist.length () == limit)
5365 /* Unconditionally warn that the search was truncated. */
5366 inform (location,
5367 "maximum limit of %d namespaces searched for %qE",
5368 limit, name);
5369 limited = true;
5371 else
5372 worklist.safe_push (children.pop ());
5374 children.release ();
5377 worklist.release ();
5379 if (candidates.length ())
5381 inform_n (location, candidates.length (),
5382 "suggested alternative:",
5383 "suggested alternatives:");
5384 for (unsigned ix = 0; ix != candidates.length (); ix++)
5386 tree val = candidates[ix];
5388 inform (location_of (val), " %qE", val);
5390 candidates.release ();
5391 return;
5394 /* No candidates were found in the available namespaces. */
5396 /* If there's a "using namespace std;" active, and this
5397 is one of the most common "std::" names, then it's probably a
5398 missing #include. */
5399 if (has_using_namespace_std_directive_p ())
5400 if (maybe_suggest_missing_std_header (location, name))
5401 return;
5403 /* Otherwise, consider misspellings. */
5404 if (!suggest_misspellings)
5405 return;
5406 if (name_hint hint = lookup_name_fuzzy (name, FUZZY_LOOKUP_NAME,
5407 location))
5409 /* Show a spelling correction. */
5410 gcc_rich_location richloc (location);
5412 richloc.add_fixit_replace (hint.suggestion ());
5413 inform (&richloc, "suggested alternative: %qs", hint.suggestion ());
5417 /* A well-known name within the C++ standard library, returned by
5418 get_std_name_hint. */
5420 struct std_name_hint
5422 /* A name within "std::". */
5423 const char *name;
5425 /* The header name defining it within the C++ Standard Library
5426 (with '<' and '>'). */
5427 const char *header;
5429 /* The dialect of C++ in which this was added. */
5430 enum cxx_dialect min_dialect;
5433 /* Subroutine of maybe_suggest_missing_header for handling unrecognized names
5434 for some of the most common names within "std::".
5435 Given non-NULL NAME, return the std_name_hint for it, or NULL. */
5437 static const std_name_hint *
5438 get_std_name_hint (const char *name)
5440 static const std_name_hint hints[] = {
5441 /* <any>. */
5442 {"any", "<any>", cxx17},
5443 {"any_cast", "<any>", cxx17},
5444 {"make_any", "<any>", cxx17},
5445 /* <array>. */
5446 {"array", "<array>", cxx11},
5447 /* <atomic>. */
5448 {"atomic", "<atomic>", cxx11},
5449 {"atomic_flag", "<atomic>", cxx11},
5450 /* <bitset>. */
5451 {"bitset", "<bitset>", cxx11},
5452 /* <complex>. */
5453 {"complex", "<complex>", cxx98},
5454 {"complex_literals", "<complex>", cxx98},
5455 /* <condition_variable>. */
5456 {"condition_variable", "<condition_variable>", cxx11},
5457 {"condition_variable_any", "<condition_variable>", cxx11},
5458 /* <deque>. */
5459 {"deque", "<deque>", cxx98},
5460 /* <forward_list>. */
5461 {"forward_list", "<forward_list>", cxx11},
5462 /* <fstream>. */
5463 {"basic_filebuf", "<fstream>", cxx98},
5464 {"basic_ifstream", "<fstream>", cxx98},
5465 {"basic_ofstream", "<fstream>", cxx98},
5466 {"basic_fstream", "<fstream>", cxx98},
5467 {"fstream", "<fstream>", cxx98},
5468 {"ifstream", "<fstream>", cxx98},
5469 {"ofstream", "<fstream>", cxx98},
5470 /* <functional>. */
5471 {"bind", "<functional>", cxx11},
5472 {"function", "<functional>", cxx11},
5473 {"hash", "<functional>", cxx11},
5474 {"mem_fn", "<functional>", cxx11},
5475 /* <future>. */
5476 {"async", "<future>", cxx11},
5477 {"future", "<future>", cxx11},
5478 {"packaged_task", "<future>", cxx11},
5479 {"promise", "<future>", cxx11},
5480 /* <iostream>. */
5481 {"cin", "<iostream>", cxx98},
5482 {"cout", "<iostream>", cxx98},
5483 {"cerr", "<iostream>", cxx98},
5484 {"clog", "<iostream>", cxx98},
5485 {"wcin", "<iostream>", cxx98},
5486 {"wcout", "<iostream>", cxx98},
5487 {"wclog", "<iostream>", cxx98},
5488 /* <istream>. */
5489 {"istream", "<istream>", cxx98},
5490 /* <iterator>. */
5491 {"advance", "<iterator>", cxx98},
5492 {"back_inserter", "<iterator>", cxx98},
5493 {"begin", "<iterator>", cxx11},
5494 {"distance", "<iterator>", cxx98},
5495 {"end", "<iterator>", cxx11},
5496 {"front_inserter", "<iterator>", cxx98},
5497 {"inserter", "<iterator>", cxx98},
5498 {"istream_iterator", "<iterator>", cxx98},
5499 {"istreambuf_iterator", "<iterator>", cxx98},
5500 {"iterator_traits", "<iterator>", cxx98},
5501 {"move_iterator", "<iterator>", cxx11},
5502 {"next", "<iterator>", cxx11},
5503 {"ostream_iterator", "<iterator>", cxx98},
5504 {"ostreambuf_iterator", "<iterator>", cxx98},
5505 {"prev", "<iterator>", cxx11},
5506 {"reverse_iterator", "<iterator>", cxx98},
5507 /* <ostream>. */
5508 {"ostream", "<ostream>", cxx98},
5509 /* <list>. */
5510 {"list", "<list>", cxx98},
5511 /* <map>. */
5512 {"map", "<map>", cxx98},
5513 {"multimap", "<map>", cxx98},
5514 /* <memory>. */
5515 {"make_shared", "<memory>", cxx11},
5516 {"make_unique", "<memory>", cxx11},
5517 {"shared_ptr", "<memory>", cxx11},
5518 {"unique_ptr", "<memory>", cxx11},
5519 {"weak_ptr", "<memory>", cxx11},
5520 /* <mutex>. */
5521 {"mutex", "<mutex>", cxx11},
5522 {"timed_mutex", "<mutex>", cxx11},
5523 {"recursive_mutex", "<mutex>", cxx11},
5524 {"recursive_timed_mutex", "<mutex>", cxx11},
5525 {"once_flag", "<mutex>", cxx11},
5526 {"call_once,", "<mutex>", cxx11},
5527 {"lock", "<mutex>", cxx11},
5528 {"scoped_lock", "<mutex>", cxx17},
5529 {"try_lock", "<mutex>", cxx11},
5530 {"lock_guard", "<mutex>", cxx11},
5531 {"unique_lock", "<mutex>", cxx11},
5532 /* <optional>. */
5533 {"optional", "<optional>", cxx17},
5534 {"make_optional", "<optional>", cxx17},
5535 /* <ostream>. */
5536 {"ostream", "<ostream>", cxx98},
5537 {"wostream", "<ostream>", cxx98},
5538 {"ends", "<ostream>", cxx98},
5539 {"flush", "<ostream>", cxx98},
5540 {"endl", "<ostream>", cxx98},
5541 /* <queue>. */
5542 {"queue", "<queue>", cxx98},
5543 {"priority_queue", "<queue>", cxx98},
5544 /* <set>. */
5545 {"set", "<set>", cxx98},
5546 {"multiset", "<set>", cxx98},
5547 /* <shared_mutex>. */
5548 {"shared_lock", "<shared_mutex>", cxx14},
5549 {"shared_mutex", "<shared_mutex>", cxx17},
5550 {"shared_timed_mutex", "<shared_mutex>", cxx14},
5551 /* <sstream>. */
5552 {"basic_stringbuf", "<sstream>", cxx98},
5553 {"basic_istringstream", "<sstream>", cxx98},
5554 {"basic_ostringstream", "<sstream>", cxx98},
5555 {"basic_stringstream", "<sstream>", cxx98},
5556 {"istringstream", "<sstream>", cxx98},
5557 {"ostringstream", "<sstream>", cxx98},
5558 {"stringstream", "<sstream>", cxx98},
5559 /* <stack>. */
5560 {"stack", "<stack>", cxx98},
5561 /* <string>. */
5562 {"basic_string", "<string>", cxx98},
5563 {"string", "<string>", cxx98},
5564 {"wstring", "<string>", cxx98},
5565 {"u16string", "<string>", cxx11},
5566 {"u32string", "<string>", cxx11},
5567 /* <string_view>. */
5568 {"string_view", "<string_view>", cxx17},
5569 /* <thread>. */
5570 {"thread", "<thread>", cxx11},
5571 /* <tuple>. */
5572 {"make_tuple", "<tuple>", cxx11},
5573 {"tuple", "<tuple>", cxx11},
5574 {"tuple_element", "<tuple>", cxx11},
5575 {"tuple_size", "<tuple>", cxx11},
5576 /* <unordered_map>. */
5577 {"unordered_map", "<unordered_map>", cxx11},
5578 {"unordered_multimap", "<unordered_map>", cxx11},
5579 /* <unordered_set>. */
5580 {"unordered_set", "<unordered_set>", cxx11},
5581 {"unordered_multiset", "<unordered_set>", cxx11},
5582 /* <utility>. */
5583 {"declval", "<utility>", cxx11},
5584 {"forward", "<utility>", cxx11},
5585 {"make_pair", "<utility>", cxx98},
5586 {"move", "<utility>", cxx11},
5587 {"pair", "<utility>", cxx98},
5588 /* <variant>. */
5589 {"variant", "<variant>", cxx17},
5590 {"visit", "<variant>", cxx17},
5591 /* <vector>. */
5592 {"vector", "<vector>", cxx98},
5594 const size_t num_hints = sizeof (hints) / sizeof (hints[0]);
5595 for (size_t i = 0; i < num_hints; i++)
5597 if (strcmp (name, hints[i].name) == 0)
5598 return &hints[i];
5600 return NULL;
5603 /* Describe DIALECT. */
5605 static const char *
5606 get_cxx_dialect_name (enum cxx_dialect dialect)
5608 switch (dialect)
5610 default:
5611 gcc_unreachable ();
5612 case cxx98:
5613 return "C++98";
5614 case cxx11:
5615 return "C++11";
5616 case cxx14:
5617 return "C++14";
5618 case cxx17:
5619 return "C++17";
5620 case cxx2a:
5621 return "C++2a";
5625 /* Suggest pertinent header files for NAME at LOCATION, for common
5626 names within the "std" namespace.
5627 Return true iff a suggestion was offered. */
5629 static bool
5630 maybe_suggest_missing_std_header (location_t location, tree name)
5632 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
5634 const char *name_str = IDENTIFIER_POINTER (name);
5635 const std_name_hint *header_hint = get_std_name_hint (name_str);
5636 if (!header_hint)
5637 return false;
5639 gcc_rich_location richloc (location);
5640 if (cxx_dialect >= header_hint->min_dialect)
5642 const char *header = header_hint->header;
5643 maybe_add_include_fixit (&richloc, header, true);
5644 inform (&richloc,
5645 "%<std::%s%> is defined in header %qs;"
5646 " did you forget to %<#include %s%>?",
5647 name_str, header, header);
5649 else
5651 inform (&richloc,
5652 "%<std::%s%> is only available from %s onwards",
5653 name_str, get_cxx_dialect_name (header_hint->min_dialect));
5655 return true;
5658 /* If SCOPE is the "std" namespace, then suggest pertinent header
5659 files for NAME at LOCATION.
5660 Return true iff a suggestion was offered. */
5662 static bool
5663 maybe_suggest_missing_header (location_t location, tree name, tree scope)
5665 if (scope == NULL_TREE)
5666 return false;
5667 if (TREE_CODE (scope) != NAMESPACE_DECL)
5668 return false;
5669 /* We only offer suggestions for the "std" namespace. */
5670 if (scope != std_node)
5671 return false;
5672 return maybe_suggest_missing_std_header (location, name);
5675 /* Look for alternatives for NAME, an IDENTIFIER_NODE for which name
5676 lookup failed within the explicitly provided SCOPE. Suggest the
5677 the best meaningful candidates (if any) as a fix-it hint.
5678 Return true iff a suggestion was provided. */
5680 bool
5681 suggest_alternative_in_explicit_scope (location_t location, tree name,
5682 tree scope)
5684 /* Something went very wrong; don't suggest anything. */
5685 if (name == error_mark_node)
5686 return false;
5688 /* Resolve any namespace aliases. */
5689 scope = ORIGINAL_NAMESPACE (scope);
5691 if (maybe_suggest_missing_header (location, name, scope))
5692 return true;
5694 cp_binding_level *level = NAMESPACE_LEVEL (scope);
5696 best_match <tree, const char *> bm (name);
5697 consider_binding_level (name, bm, level, false, FUZZY_LOOKUP_NAME);
5699 /* See if we have a good suggesion for the user. */
5700 const char *fuzzy_name = bm.get_best_meaningful_candidate ();
5701 if (fuzzy_name)
5703 gcc_rich_location richloc (location);
5704 richloc.add_fixit_replace (fuzzy_name);
5705 inform (&richloc, "suggested alternative: %qs",
5706 fuzzy_name);
5707 return true;
5710 return false;
5713 /* Look up NAME (an IDENTIFIER_NODE) in SCOPE (either a NAMESPACE_DECL
5714 or a class TYPE).
5716 If PREFER_TYPE is > 0, we only return TYPE_DECLs or namespaces.
5717 If PREFER_TYPE is > 1, we only return TYPE_DECLs.
5719 Returns a DECL (or OVERLOAD, or BASELINK) representing the
5720 declaration found. If no suitable declaration can be found,
5721 ERROR_MARK_NODE is returned. If COMPLAIN is true and SCOPE is
5722 neither a class-type nor a namespace a diagnostic is issued. */
5724 tree
5725 lookup_qualified_name (tree scope, tree name, int prefer_type, bool complain,
5726 bool find_hidden)
5728 tree t = NULL_TREE;
5730 if (TREE_CODE (scope) == NAMESPACE_DECL)
5732 int flags = lookup_flags (prefer_type, /*namespaces_only*/false);
5733 if (find_hidden)
5734 flags |= LOOKUP_HIDDEN;
5735 name_lookup lookup (name, flags);
5737 if (qualified_namespace_lookup (scope, &lookup))
5738 t = lookup.value;
5740 else if (cxx_dialect != cxx98 && TREE_CODE (scope) == ENUMERAL_TYPE)
5741 t = lookup_enumerator (scope, name);
5742 else if (is_class_type (scope, complain))
5743 t = lookup_member (scope, name, 2, prefer_type, tf_warning_or_error);
5745 if (!t)
5746 return error_mark_node;
5747 return t;
5750 /* [namespace.qual]
5751 Accepts the NAME to lookup and its qualifying SCOPE.
5752 Returns the name/type pair found into the cxx_binding *RESULT,
5753 or false on error. */
5755 static bool
5756 qualified_namespace_lookup (tree scope, name_lookup *lookup)
5758 timevar_start (TV_NAME_LOOKUP);
5759 query_oracle (lookup->name);
5760 bool found = lookup->search_qualified (ORIGINAL_NAMESPACE (scope));
5761 timevar_stop (TV_NAME_LOOKUP);
5762 return found;
5765 /* Helper function for lookup_name_fuzzy.
5766 Traverse binding level LVL, looking for good name matches for NAME
5767 (and BM). */
5768 static void
5769 consider_binding_level (tree name, best_match <tree, const char *> &bm,
5770 cp_binding_level *lvl, bool look_within_fields,
5771 enum lookup_name_fuzzy_kind kind)
5773 if (look_within_fields)
5774 if (lvl->this_entity && TREE_CODE (lvl->this_entity) == RECORD_TYPE)
5776 tree type = lvl->this_entity;
5777 bool want_type_p = (kind == FUZZY_LOOKUP_TYPENAME);
5778 tree best_matching_field
5779 = lookup_member_fuzzy (type, name, want_type_p);
5780 if (best_matching_field)
5781 bm.consider (IDENTIFIER_POINTER (best_matching_field));
5784 /* Only suggest names reserved for the implementation if NAME begins
5785 with an underscore. */
5786 bool consider_implementation_names = (IDENTIFIER_POINTER (name)[0] == '_');
5788 for (tree t = lvl->names; t; t = TREE_CHAIN (t))
5790 tree d = t;
5792 /* OVERLOADs or decls from using declaration are wrapped into
5793 TREE_LIST. */
5794 if (TREE_CODE (d) == TREE_LIST)
5795 d = OVL_FIRST (TREE_VALUE (d));
5797 /* Don't use bindings from implicitly declared functions,
5798 as they were likely misspellings themselves. */
5799 if (TREE_TYPE (d) == error_mark_node)
5800 continue;
5802 /* Skip anticipated decls of builtin functions. */
5803 if (TREE_CODE (d) == FUNCTION_DECL
5804 && fndecl_built_in_p (d)
5805 && DECL_ANTICIPATED (d))
5806 continue;
5808 /* Skip compiler-generated variables (e.g. __for_begin/__for_end
5809 within range for). */
5810 if (TREE_CODE (d) == VAR_DECL
5811 && DECL_ARTIFICIAL (d))
5812 continue;
5814 tree suggestion = DECL_NAME (d);
5815 if (!suggestion)
5816 continue;
5818 /* Don't suggest names that are for anonymous aggregate types, as
5819 they are an implementation detail generated by the compiler. */
5820 if (anon_aggrname_p (suggestion))
5821 continue;
5823 const char *suggestion_str = IDENTIFIER_POINTER (suggestion);
5825 /* Ignore internal names with spaces in them. */
5826 if (strchr (suggestion_str, ' '))
5827 continue;
5829 /* Don't suggest names that are reserved for use by the
5830 implementation, unless NAME began with an underscore. */
5831 if (name_reserved_for_implementation_p (suggestion_str)
5832 && !consider_implementation_names)
5833 continue;
5835 bm.consider (suggestion_str);
5839 /* Subclass of deferred_diagnostic. Notify the user that the
5840 given macro was used before it was defined.
5841 This can be done in the C++ frontend since tokenization happens
5842 upfront. */
5844 class macro_use_before_def : public deferred_diagnostic
5846 public:
5847 /* Factory function. Return a new macro_use_before_def instance if
5848 appropriate, or return NULL. */
5849 static macro_use_before_def *
5850 maybe_make (location_t use_loc, cpp_hashnode *macro)
5852 source_location def_loc = cpp_macro_definition_location (macro);
5853 if (def_loc == UNKNOWN_LOCATION)
5854 return NULL;
5856 /* We only want to issue a note if the macro was used *before* it was
5857 defined.
5858 We don't want to issue a note for cases where a macro was incorrectly
5859 used, leaving it unexpanded (e.g. by using the wrong argument
5860 count). */
5861 if (!linemap_location_before_p (line_table, use_loc, def_loc))
5862 return NULL;
5864 return new macro_use_before_def (use_loc, macro);
5867 private:
5868 /* Ctor. LOC is the location of the usage. MACRO is the
5869 macro that was used. */
5870 macro_use_before_def (location_t loc, cpp_hashnode *macro)
5871 : deferred_diagnostic (loc), m_macro (macro)
5873 gcc_assert (macro);
5876 ~macro_use_before_def ()
5878 if (is_suppressed_p ())
5879 return;
5881 inform (get_location (), "the macro %qs had not yet been defined",
5882 (const char *)m_macro->ident.str);
5883 inform (cpp_macro_definition_location (m_macro),
5884 "it was later defined here");
5887 private:
5888 cpp_hashnode *m_macro;
5891 /* Determine if it can ever make sense to offer RID as a suggestion for
5892 a misspelling.
5894 Subroutine of lookup_name_fuzzy. */
5896 static bool
5897 suggest_rid_p (enum rid rid)
5899 switch (rid)
5901 /* Support suggesting function-like keywords. */
5902 case RID_STATIC_ASSERT:
5903 return true;
5905 default:
5906 /* Support suggesting the various decl-specifier words, to handle
5907 e.g. "singed" vs "signed" typos. */
5908 if (cp_keyword_starts_decl_specifier_p (rid))
5909 return true;
5911 /* Otherwise, don't offer it. This avoids suggesting e.g. "if"
5912 and "do" for short misspellings, which are likely to lead to
5913 nonsensical results. */
5914 return false;
5918 /* Search for near-matches for NAME within the current bindings, and within
5919 macro names, returning the best match as a const char *, or NULL if
5920 no reasonable match is found.
5922 Use LOC for any deferred diagnostics. */
5924 name_hint
5925 lookup_name_fuzzy (tree name, enum lookup_name_fuzzy_kind kind, location_t loc)
5927 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
5929 /* First, try some well-known names in the C++ standard library, in case
5930 the user forgot a #include. */
5931 const char *header_hint
5932 = get_cp_stdlib_header_for_name (IDENTIFIER_POINTER (name));
5933 if (header_hint)
5934 return name_hint (NULL,
5935 new suggest_missing_header (loc,
5936 IDENTIFIER_POINTER (name),
5937 header_hint));
5939 best_match <tree, const char *> bm (name);
5941 cp_binding_level *lvl;
5942 for (lvl = scope_chain->class_bindings; lvl; lvl = lvl->level_chain)
5943 consider_binding_level (name, bm, lvl, true, kind);
5945 for (lvl = current_binding_level; lvl; lvl = lvl->level_chain)
5946 consider_binding_level (name, bm, lvl, false, kind);
5948 /* Consider macros: if the user misspelled a macro name e.g. "SOME_MACRO"
5950 x = SOME_OTHER_MACRO (y);
5951 then "SOME_OTHER_MACRO" will survive to the frontend and show up
5952 as a misspelled identifier.
5954 Use the best distance so far so that a candidate is only set if
5955 a macro is better than anything so far. This allows early rejection
5956 (without calculating the edit distance) of macro names that must have
5957 distance >= bm.get_best_distance (), and means that we only get a
5958 non-NULL result for best_macro_match if it's better than any of
5959 the identifiers already checked. */
5960 best_macro_match bmm (name, bm.get_best_distance (), parse_in);
5961 cpp_hashnode *best_macro = bmm.get_best_meaningful_candidate ();
5962 /* If a macro is the closest so far to NAME, consider it. */
5963 if (best_macro)
5964 bm.consider ((const char *)best_macro->ident.str);
5965 else if (bmm.get_best_distance () == 0)
5967 /* If we have an exact match for a macro name, then either the
5968 macro was used with the wrong argument count, or the macro
5969 has been used before it was defined. */
5970 if (cpp_hashnode *macro = bmm.blithely_get_best_candidate ())
5971 if (cpp_user_macro_p (macro))
5972 return name_hint (NULL,
5973 macro_use_before_def::maybe_make (loc, macro));
5976 /* Try the "starts_decl_specifier_p" keywords to detect
5977 "singed" vs "signed" typos. */
5978 for (unsigned i = 0; i < num_c_common_reswords; i++)
5980 const c_common_resword *resword = &c_common_reswords[i];
5982 if (!suggest_rid_p (resword->rid))
5983 continue;
5985 tree resword_identifier = ridpointers [resword->rid];
5986 if (!resword_identifier)
5987 continue;
5988 gcc_assert (TREE_CODE (resword_identifier) == IDENTIFIER_NODE);
5990 /* Only consider reserved words that survived the
5991 filtering in init_reswords (e.g. for -std). */
5992 if (!IDENTIFIER_KEYWORD_P (resword_identifier))
5993 continue;
5995 bm.consider (IDENTIFIER_POINTER (resword_identifier));
5998 return name_hint (bm.get_best_meaningful_candidate (), NULL);
6001 /* Subroutine of outer_binding.
6003 Returns TRUE if BINDING is a binding to a template parameter of
6004 SCOPE. In that case SCOPE is the scope of a primary template
6005 parameter -- in the sense of G++, i.e, a template that has its own
6006 template header.
6008 Returns FALSE otherwise. */
6010 static bool
6011 binding_to_template_parms_of_scope_p (cxx_binding *binding,
6012 cp_binding_level *scope)
6014 tree binding_value, tmpl, tinfo;
6015 int level;
6017 if (!binding || !scope || !scope->this_entity)
6018 return false;
6020 binding_value = binding->value ? binding->value : binding->type;
6021 tinfo = get_template_info (scope->this_entity);
6023 /* BINDING_VALUE must be a template parm. */
6024 if (binding_value == NULL_TREE
6025 || (!DECL_P (binding_value)
6026 || !DECL_TEMPLATE_PARM_P (binding_value)))
6027 return false;
6029 /* The level of BINDING_VALUE. */
6030 level =
6031 template_type_parameter_p (binding_value)
6032 ? TEMPLATE_PARM_LEVEL (TEMPLATE_TYPE_PARM_INDEX
6033 (TREE_TYPE (binding_value)))
6034 : TEMPLATE_PARM_LEVEL (DECL_INITIAL (binding_value));
6036 /* The template of the current scope, iff said scope is a primary
6037 template. */
6038 tmpl = (tinfo
6039 && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
6040 ? TI_TEMPLATE (tinfo)
6041 : NULL_TREE);
6043 /* If the level of the parm BINDING_VALUE equals the depth of TMPL,
6044 then BINDING_VALUE is a parameter of TMPL. */
6045 return (tmpl && level == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)));
6048 /* Return the innermost non-namespace binding for NAME from a scope
6049 containing BINDING, or, if BINDING is NULL, the current scope.
6050 Please note that for a given template, the template parameters are
6051 considered to be in the scope containing the current scope.
6052 If CLASS_P is false, then class bindings are ignored. */
6054 cxx_binding *
6055 outer_binding (tree name,
6056 cxx_binding *binding,
6057 bool class_p)
6059 cxx_binding *outer;
6060 cp_binding_level *scope;
6061 cp_binding_level *outer_scope;
6063 if (binding)
6065 scope = binding->scope->level_chain;
6066 outer = binding->previous;
6068 else
6070 scope = current_binding_level;
6071 outer = IDENTIFIER_BINDING (name);
6073 outer_scope = outer ? outer->scope : NULL;
6075 /* Because we create class bindings lazily, we might be missing a
6076 class binding for NAME. If there are any class binding levels
6077 between the LAST_BINDING_LEVEL and the scope in which OUTER was
6078 declared, we must lookup NAME in those class scopes. */
6079 if (class_p)
6080 while (scope && scope != outer_scope && scope->kind != sk_namespace)
6082 if (scope->kind == sk_class)
6084 cxx_binding *class_binding;
6086 class_binding = get_class_binding (name, scope);
6087 if (class_binding)
6089 /* Thread this new class-scope binding onto the
6090 IDENTIFIER_BINDING list so that future lookups
6091 find it quickly. */
6092 class_binding->previous = outer;
6093 if (binding)
6094 binding->previous = class_binding;
6095 else
6096 IDENTIFIER_BINDING (name) = class_binding;
6097 return class_binding;
6100 /* If we are in a member template, the template parms of the member
6101 template are considered to be inside the scope of the containing
6102 class, but within G++ the class bindings are all pushed between the
6103 template parms and the function body. So if the outer binding is
6104 a template parm for the current scope, return it now rather than
6105 look for a class binding. */
6106 if (outer_scope && outer_scope->kind == sk_template_parms
6107 && binding_to_template_parms_of_scope_p (outer, scope))
6108 return outer;
6110 scope = scope->level_chain;
6113 return outer;
6116 /* Return the innermost block-scope or class-scope value binding for
6117 NAME, or NULL_TREE if there is no such binding. */
6119 tree
6120 innermost_non_namespace_value (tree name)
6122 cxx_binding *binding;
6123 binding = outer_binding (name, /*binding=*/NULL, /*class_p=*/true);
6124 return binding ? binding->value : NULL_TREE;
6127 /* Look up NAME in the current binding level and its superiors in the
6128 namespace of variables, functions and typedefs. Return a ..._DECL
6129 node of some kind representing its definition if there is only one
6130 such declaration, or return a TREE_LIST with all the overloaded
6131 definitions if there are many, or return 0 if it is undefined.
6132 Hidden name, either friend declaration or built-in function, are
6133 not ignored.
6135 If PREFER_TYPE is > 0, we prefer TYPE_DECLs or namespaces.
6136 If PREFER_TYPE is > 1, we reject non-type decls (e.g. namespaces).
6137 Otherwise we prefer non-TYPE_DECLs.
6139 If NONCLASS is nonzero, bindings in class scopes are ignored. If
6140 BLOCK_P is false, bindings in block scopes are ignored. */
6142 static tree
6143 lookup_name_real_1 (tree name, int prefer_type, int nonclass, bool block_p,
6144 int namespaces_only, int flags)
6146 cxx_binding *iter;
6147 tree val = NULL_TREE;
6149 query_oracle (name);
6151 /* Conversion operators are handled specially because ordinary
6152 unqualified name lookup will not find template conversion
6153 operators. */
6154 if (IDENTIFIER_CONV_OP_P (name))
6156 cp_binding_level *level;
6158 for (level = current_binding_level;
6159 level && level->kind != sk_namespace;
6160 level = level->level_chain)
6162 tree class_type;
6163 tree operators;
6165 /* A conversion operator can only be declared in a class
6166 scope. */
6167 if (level->kind != sk_class)
6168 continue;
6170 /* Lookup the conversion operator in the class. */
6171 class_type = level->this_entity;
6172 operators = lookup_fnfields (class_type, name, /*protect=*/0);
6173 if (operators)
6174 return operators;
6177 return NULL_TREE;
6180 flags |= lookup_flags (prefer_type, namespaces_only);
6182 /* First, look in non-namespace scopes. */
6184 if (current_class_type == NULL_TREE)
6185 nonclass = 1;
6187 if (block_p || !nonclass)
6188 for (iter = outer_binding (name, NULL, !nonclass);
6189 iter;
6190 iter = outer_binding (name, iter, !nonclass))
6192 tree binding;
6194 /* Skip entities we don't want. */
6195 if (LOCAL_BINDING_P (iter) ? !block_p : nonclass)
6196 continue;
6198 /* If this is the kind of thing we're looking for, we're done. */
6199 if (qualify_lookup (iter->value, flags))
6200 binding = iter->value;
6201 else if ((flags & LOOKUP_PREFER_TYPES)
6202 && qualify_lookup (iter->type, flags))
6203 binding = iter->type;
6204 else
6205 binding = NULL_TREE;
6207 if (binding)
6209 if (TREE_CODE (binding) == TYPE_DECL && DECL_HIDDEN_P (binding))
6211 /* A non namespace-scope binding can only be hidden in the
6212 presence of a local class, due to friend declarations.
6214 In particular, consider:
6216 struct C;
6217 void f() {
6218 struct A {
6219 friend struct B;
6220 friend struct C;
6221 void g() {
6222 B* b; // error: B is hidden
6223 C* c; // OK, finds ::C
6226 B *b; // error: B is hidden
6227 C *c; // OK, finds ::C
6228 struct B {};
6229 B *bb; // OK
6232 The standard says that "B" is a local class in "f"
6233 (but not nested within "A") -- but that name lookup
6234 for "B" does not find this declaration until it is
6235 declared directly with "f".
6237 In particular:
6239 [class.friend]
6241 If a friend declaration appears in a local class and
6242 the name specified is an unqualified name, a prior
6243 declaration is looked up without considering scopes
6244 that are outside the innermost enclosing non-class
6245 scope. For a friend function declaration, if there is
6246 no prior declaration, the program is ill-formed. For a
6247 friend class declaration, if there is no prior
6248 declaration, the class that is specified belongs to the
6249 innermost enclosing non-class scope, but if it is
6250 subsequently referenced, its name is not found by name
6251 lookup until a matching declaration is provided in the
6252 innermost enclosing nonclass scope.
6254 So just keep looking for a non-hidden binding.
6256 gcc_assert (TREE_CODE (binding) == TYPE_DECL);
6257 continue;
6259 val = binding;
6260 break;
6264 /* Now lookup in namespace scopes. */
6265 if (!val)
6267 name_lookup lookup (name, flags);
6268 if (lookup.search_unqualified
6269 (current_decl_namespace (), current_binding_level))
6270 val = lookup.value;
6273 /* If we have a single function from a using decl, pull it out. */
6274 if (val && TREE_CODE (val) == OVERLOAD && !really_overloaded_fn (val))
6275 val = OVL_FUNCTION (val);
6277 return val;
6280 /* Wrapper for lookup_name_real_1. */
6282 tree
6283 lookup_name_real (tree name, int prefer_type, int nonclass, bool block_p,
6284 int namespaces_only, int flags)
6286 tree ret;
6287 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6288 ret = lookup_name_real_1 (name, prefer_type, nonclass, block_p,
6289 namespaces_only, flags);
6290 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6291 return ret;
6294 tree
6295 lookup_name_nonclass (tree name)
6297 return lookup_name_real (name, 0, 1, /*block_p=*/true, 0, 0);
6300 tree
6301 lookup_name (tree name)
6303 return lookup_name_real (name, 0, 0, /*block_p=*/true, 0, 0);
6306 tree
6307 lookup_name_prefer_type (tree name, int prefer_type)
6309 return lookup_name_real (name, prefer_type, 0, /*block_p=*/true, 0, 0);
6312 /* Look up NAME for type used in elaborated name specifier in
6313 the scopes given by SCOPE. SCOPE can be either TS_CURRENT or
6314 TS_WITHIN_ENCLOSING_NON_CLASS. Although not implied by the
6315 name, more scopes are checked if cleanup or template parameter
6316 scope is encountered.
6318 Unlike lookup_name_real, we make sure that NAME is actually
6319 declared in the desired scope, not from inheritance, nor using
6320 directive. For using declaration, there is DR138 still waiting
6321 to be resolved. Hidden name coming from an earlier friend
6322 declaration is also returned.
6324 A TYPE_DECL best matching the NAME is returned. Catching error
6325 and issuing diagnostics are caller's responsibility. */
6327 static tree
6328 lookup_type_scope_1 (tree name, tag_scope scope)
6330 cxx_binding *iter = NULL;
6331 tree val = NULL_TREE;
6332 cp_binding_level *level = NULL;
6334 /* Look in non-namespace scope first. */
6335 if (current_binding_level->kind != sk_namespace)
6336 iter = outer_binding (name, NULL, /*class_p=*/ true);
6337 for (; iter; iter = outer_binding (name, iter, /*class_p=*/ true))
6339 /* Check if this is the kind of thing we're looking for.
6340 If SCOPE is TS_CURRENT, also make sure it doesn't come from
6341 base class. For ITER->VALUE, we can simply use
6342 INHERITED_VALUE_BINDING_P. For ITER->TYPE, we have to use
6343 our own check.
6345 We check ITER->TYPE before ITER->VALUE in order to handle
6346 typedef struct C {} C;
6347 correctly. */
6349 if (qualify_lookup (iter->type, LOOKUP_PREFER_TYPES)
6350 && (scope != ts_current
6351 || LOCAL_BINDING_P (iter)
6352 || DECL_CONTEXT (iter->type) == iter->scope->this_entity))
6353 val = iter->type;
6354 else if ((scope != ts_current
6355 || !INHERITED_VALUE_BINDING_P (iter))
6356 && qualify_lookup (iter->value, LOOKUP_PREFER_TYPES))
6357 val = iter->value;
6359 if (val)
6360 break;
6363 /* Look in namespace scope. */
6364 if (val)
6365 level = iter->scope;
6366 else
6368 tree ns = current_decl_namespace ();
6370 if (tree *slot = find_namespace_slot (ns, name))
6372 /* If this is the kind of thing we're looking for, we're done. */
6373 if (tree type = MAYBE_STAT_TYPE (*slot))
6374 if (qualify_lookup (type, LOOKUP_PREFER_TYPES))
6375 val = type;
6376 if (!val)
6378 if (tree decl = MAYBE_STAT_DECL (*slot))
6379 if (qualify_lookup (decl, LOOKUP_PREFER_TYPES))
6380 val = decl;
6382 level = NAMESPACE_LEVEL (ns);
6386 /* Type found, check if it is in the allowed scopes, ignoring cleanup
6387 and template parameter scopes. */
6388 if (val)
6390 cp_binding_level *b = current_binding_level;
6391 while (b)
6393 if (level == b)
6394 return val;
6396 if (b->kind == sk_cleanup || b->kind == sk_template_parms
6397 || b->kind == sk_function_parms)
6398 b = b->level_chain;
6399 else if (b->kind == sk_class
6400 && scope == ts_within_enclosing_non_class)
6401 b = b->level_chain;
6402 else
6403 break;
6407 return NULL_TREE;
6410 /* Wrapper for lookup_type_scope_1. */
6412 tree
6413 lookup_type_scope (tree name, tag_scope scope)
6415 tree ret;
6416 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6417 ret = lookup_type_scope_1 (name, scope);
6418 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6419 return ret;
6422 /* Returns true iff DECL is a block-scope extern declaration of a function
6423 or variable. */
6425 bool
6426 is_local_extern (tree decl)
6428 cxx_binding *binding;
6430 /* For functions, this is easy. */
6431 if (TREE_CODE (decl) == FUNCTION_DECL)
6432 return DECL_LOCAL_FUNCTION_P (decl);
6434 if (!VAR_P (decl))
6435 return false;
6436 if (!current_function_decl)
6437 return false;
6439 /* For variables, this is not easy. We need to look at the binding stack
6440 for the identifier to see whether the decl we have is a local. */
6441 for (binding = IDENTIFIER_BINDING (DECL_NAME (decl));
6442 binding && binding->scope->kind != sk_namespace;
6443 binding = binding->previous)
6444 if (binding->value == decl)
6445 return LOCAL_BINDING_P (binding);
6447 return false;
6450 /* The type TYPE is being declared. If it is a class template, or a
6451 specialization of a class template, do any processing required and
6452 perform error-checking. If IS_FRIEND is nonzero, this TYPE is
6453 being declared a friend. B is the binding level at which this TYPE
6454 should be bound.
6456 Returns the TYPE_DECL for TYPE, which may have been altered by this
6457 processing. */
6459 static tree
6460 maybe_process_template_type_declaration (tree type, int is_friend,
6461 cp_binding_level *b)
6463 tree decl = TYPE_NAME (type);
6465 if (processing_template_parmlist)
6466 /* You can't declare a new template type in a template parameter
6467 list. But, you can declare a non-template type:
6469 template <class A*> struct S;
6471 is a forward-declaration of `A'. */
6473 else if (b->kind == sk_namespace
6474 && current_binding_level->kind != sk_namespace)
6475 /* If this new type is being injected into a containing scope,
6476 then it's not a template type. */
6478 else
6480 gcc_assert (MAYBE_CLASS_TYPE_P (type)
6481 || TREE_CODE (type) == ENUMERAL_TYPE);
6483 if (processing_template_decl)
6485 /* This may change after the call to
6486 push_template_decl_real, but we want the original value. */
6487 tree name = DECL_NAME (decl);
6489 decl = push_template_decl_real (decl, is_friend);
6490 if (decl == error_mark_node)
6491 return error_mark_node;
6493 /* If the current binding level is the binding level for the
6494 template parameters (see the comment in
6495 begin_template_parm_list) and the enclosing level is a class
6496 scope, and we're not looking at a friend, push the
6497 declaration of the member class into the class scope. In the
6498 friend case, push_template_decl will already have put the
6499 friend into global scope, if appropriate. */
6500 if (TREE_CODE (type) != ENUMERAL_TYPE
6501 && !is_friend && b->kind == sk_template_parms
6502 && b->level_chain->kind == sk_class)
6504 finish_member_declaration (CLASSTYPE_TI_TEMPLATE (type));
6506 if (!COMPLETE_TYPE_P (current_class_type))
6508 maybe_add_class_template_decl_list (current_class_type,
6509 type, /*friend_p=*/0);
6510 /* Put this UTD in the table of UTDs for the class. */
6511 if (CLASSTYPE_NESTED_UTDS (current_class_type) == NULL)
6512 CLASSTYPE_NESTED_UTDS (current_class_type) =
6513 binding_table_new (SCOPE_DEFAULT_HT_SIZE);
6515 binding_table_insert
6516 (CLASSTYPE_NESTED_UTDS (current_class_type), name, type);
6522 return decl;
6525 /* Push a tag name NAME for struct/class/union/enum type TYPE. In case
6526 that the NAME is a class template, the tag is processed but not pushed.
6528 The pushed scope depend on the SCOPE parameter:
6529 - When SCOPE is TS_CURRENT, put it into the inner-most non-sk_cleanup
6530 scope.
6531 - When SCOPE is TS_GLOBAL, put it in the inner-most non-class and
6532 non-template-parameter scope. This case is needed for forward
6533 declarations.
6534 - When SCOPE is TS_WITHIN_ENCLOSING_NON_CLASS, this is similar to
6535 TS_GLOBAL case except that names within template-parameter scopes
6536 are not pushed at all.
6538 Returns TYPE upon success and ERROR_MARK_NODE otherwise. */
6540 static tree
6541 do_pushtag (tree name, tree type, tag_scope scope)
6543 tree decl;
6545 cp_binding_level *b = current_binding_level;
6546 while (true)
6548 if (/* Cleanup scopes are not scopes from the point of view of
6549 the language. */
6550 b->kind == sk_cleanup
6551 /* Neither are function parameter scopes. */
6552 || b->kind == sk_function_parms
6553 /* Neither are the scopes used to hold template parameters
6554 for an explicit specialization. For an ordinary template
6555 declaration, these scopes are not scopes from the point of
6556 view of the language. */
6557 || (b->kind == sk_template_parms
6558 && (b->explicit_spec_p || scope == ts_global)))
6559 b = b->level_chain;
6560 else if (b->kind == sk_class
6561 && scope != ts_current)
6563 b = b->level_chain;
6564 if (b->kind == sk_template_parms)
6565 b = b->level_chain;
6567 else
6568 break;
6571 gcc_assert (identifier_p (name));
6573 /* Do C++ gratuitous typedefing. */
6574 if (identifier_type_value_1 (name) != type)
6576 tree tdef;
6577 int in_class = 0;
6578 tree context = TYPE_CONTEXT (type);
6580 if (! context)
6582 cp_binding_level *cb = b;
6583 while (cb->kind != sk_namespace
6584 && cb->kind != sk_class
6585 && (cb->kind != sk_function_parms
6586 || !cb->this_entity))
6587 cb = cb->level_chain;
6588 tree cs = cb->this_entity;
6590 gcc_checking_assert (TREE_CODE (cs) == FUNCTION_DECL
6591 ? cs == current_function_decl
6592 : TYPE_P (cs) ? cs == current_class_type
6593 : cs == current_namespace);
6595 if (scope == ts_current
6596 || (cs && TREE_CODE (cs) == FUNCTION_DECL))
6597 context = cs;
6598 else if (cs && TYPE_P (cs))
6599 /* When declaring a friend class of a local class, we want
6600 to inject the newly named class into the scope
6601 containing the local class, not the namespace
6602 scope. */
6603 context = decl_function_context (get_type_decl (cs));
6605 if (!context)
6606 context = current_namespace;
6608 if (b->kind == sk_class
6609 || (b->kind == sk_template_parms
6610 && b->level_chain->kind == sk_class))
6611 in_class = 1;
6613 tdef = create_implicit_typedef (name, type);
6614 DECL_CONTEXT (tdef) = FROB_CONTEXT (context);
6615 if (scope == ts_within_enclosing_non_class)
6617 /* This is a friend. Make this TYPE_DECL node hidden from
6618 ordinary name lookup. Its corresponding TEMPLATE_DECL
6619 will be marked in push_template_decl_real. */
6620 retrofit_lang_decl (tdef);
6621 DECL_ANTICIPATED (tdef) = 1;
6622 DECL_FRIEND_P (tdef) = 1;
6625 decl = maybe_process_template_type_declaration
6626 (type, scope == ts_within_enclosing_non_class, b);
6627 if (decl == error_mark_node)
6628 return decl;
6630 if (b->kind == sk_class)
6632 if (!TYPE_BEING_DEFINED (current_class_type))
6633 /* Don't push anywhere if the class is complete; a lambda in an
6634 NSDMI is not a member of the class. */
6636 else if (!PROCESSING_REAL_TEMPLATE_DECL_P ())
6637 /* Put this TYPE_DECL on the TYPE_FIELDS list for the
6638 class. But if it's a member template class, we want
6639 the TEMPLATE_DECL, not the TYPE_DECL, so this is done
6640 later. */
6641 finish_member_declaration (decl);
6642 else
6643 pushdecl_class_level (decl);
6645 else if (b->kind != sk_template_parms)
6647 decl = do_pushdecl_with_scope (decl, b, /*is_friend=*/false);
6648 if (decl == error_mark_node)
6649 return decl;
6651 if (DECL_CONTEXT (decl) == std_node
6652 && init_list_identifier == DECL_NAME (TYPE_NAME (type))
6653 && !CLASSTYPE_TEMPLATE_INFO (type))
6655 error ("declaration of %<std::initializer_list%> does not match "
6656 "%<#include <initializer_list>%>, isn't a template");
6657 return error_mark_node;
6661 if (! in_class)
6662 set_identifier_type_value_with_scope (name, tdef, b);
6664 TYPE_CONTEXT (type) = DECL_CONTEXT (decl);
6666 /* If this is a local class, keep track of it. We need this
6667 information for name-mangling, and so that it is possible to
6668 find all function definitions in a translation unit in a
6669 convenient way. (It's otherwise tricky to find a member
6670 function definition it's only pointed to from within a local
6671 class.) */
6672 if (TYPE_FUNCTION_SCOPE_P (type))
6674 if (processing_template_decl)
6676 /* Push a DECL_EXPR so we call pushtag at the right time in
6677 template instantiation rather than in some nested context. */
6678 add_decl_expr (decl);
6680 /* Lambdas use LAMBDA_EXPR_DISCRIMINATOR instead. */
6681 else if (!LAMBDA_TYPE_P (type))
6682 vec_safe_push (local_classes, type);
6686 if (b->kind == sk_class
6687 && !COMPLETE_TYPE_P (current_class_type))
6689 maybe_add_class_template_decl_list (current_class_type,
6690 type, /*friend_p=*/0);
6692 if (CLASSTYPE_NESTED_UTDS (current_class_type) == NULL)
6693 CLASSTYPE_NESTED_UTDS (current_class_type)
6694 = binding_table_new (SCOPE_DEFAULT_HT_SIZE);
6696 binding_table_insert
6697 (CLASSTYPE_NESTED_UTDS (current_class_type), name, type);
6700 decl = TYPE_NAME (type);
6701 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
6703 /* Set type visibility now if this is a forward declaration. */
6704 TREE_PUBLIC (decl) = 1;
6705 determine_visibility (decl);
6707 return type;
6710 /* Wrapper for do_pushtag. */
6712 tree
6713 pushtag (tree name, tree type, tag_scope scope)
6715 tree ret;
6716 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6717 ret = do_pushtag (name, type, scope);
6718 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6719 return ret;
6723 /* Subroutines for reverting temporarily to top-level for instantiation
6724 of templates and such. We actually need to clear out the class- and
6725 local-value slots of all identifiers, so that only the global values
6726 are at all visible. Simply setting current_binding_level to the global
6727 scope isn't enough, because more binding levels may be pushed. */
6728 struct saved_scope *scope_chain;
6730 /* Return true if ID has not already been marked. */
6732 static inline bool
6733 store_binding_p (tree id)
6735 if (!id || !IDENTIFIER_BINDING (id))
6736 return false;
6738 if (IDENTIFIER_MARKED (id))
6739 return false;
6741 return true;
6744 /* Add an appropriate binding to *OLD_BINDINGS which needs to already
6745 have enough space reserved. */
6747 static void
6748 store_binding (tree id, vec<cxx_saved_binding, va_gc> **old_bindings)
6750 cxx_saved_binding saved;
6752 gcc_checking_assert (store_binding_p (id));
6754 IDENTIFIER_MARKED (id) = 1;
6756 saved.identifier = id;
6757 saved.binding = IDENTIFIER_BINDING (id);
6758 saved.real_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
6759 (*old_bindings)->quick_push (saved);
6760 IDENTIFIER_BINDING (id) = NULL;
6763 static void
6764 store_bindings (tree names, vec<cxx_saved_binding, va_gc> **old_bindings)
6766 static vec<tree> bindings_need_stored;
6767 tree t, id;
6768 size_t i;
6770 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6771 for (t = names; t; t = TREE_CHAIN (t))
6773 if (TREE_CODE (t) == TREE_LIST)
6774 id = TREE_PURPOSE (t);
6775 else
6776 id = DECL_NAME (t);
6778 if (store_binding_p (id))
6779 bindings_need_stored.safe_push (id);
6781 if (!bindings_need_stored.is_empty ())
6783 vec_safe_reserve_exact (*old_bindings, bindings_need_stored.length ());
6784 for (i = 0; bindings_need_stored.iterate (i, &id); ++i)
6786 /* We can apparently have duplicates in NAMES. */
6787 if (store_binding_p (id))
6788 store_binding (id, old_bindings);
6790 bindings_need_stored.truncate (0);
6792 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6795 /* Like store_bindings, but NAMES is a vector of cp_class_binding
6796 objects, rather than a TREE_LIST. */
6798 static void
6799 store_class_bindings (vec<cp_class_binding, va_gc> *names,
6800 vec<cxx_saved_binding, va_gc> **old_bindings)
6802 static vec<tree> bindings_need_stored;
6803 size_t i;
6804 cp_class_binding *cb;
6806 for (i = 0; vec_safe_iterate (names, i, &cb); ++i)
6807 if (store_binding_p (cb->identifier))
6808 bindings_need_stored.safe_push (cb->identifier);
6809 if (!bindings_need_stored.is_empty ())
6811 tree id;
6812 vec_safe_reserve_exact (*old_bindings, bindings_need_stored.length ());
6813 for (i = 0; bindings_need_stored.iterate (i, &id); ++i)
6814 store_binding (id, old_bindings);
6815 bindings_need_stored.truncate (0);
6819 /* A chain of saved_scope structures awaiting reuse. */
6821 static GTY((deletable)) struct saved_scope *free_saved_scope;
6823 static void
6824 do_push_to_top_level (void)
6826 struct saved_scope *s;
6827 cp_binding_level *b;
6828 cxx_saved_binding *sb;
6829 size_t i;
6830 bool need_pop;
6832 /* Reuse or create a new structure for this saved scope. */
6833 if (free_saved_scope != NULL)
6835 s = free_saved_scope;
6836 free_saved_scope = s->prev;
6838 vec<cxx_saved_binding, va_gc> *old_bindings = s->old_bindings;
6839 memset (s, 0, sizeof (*s));
6840 /* Also reuse the structure's old_bindings vector. */
6841 vec_safe_truncate (old_bindings, 0);
6842 s->old_bindings = old_bindings;
6844 else
6845 s = ggc_cleared_alloc<saved_scope> ();
6847 b = scope_chain ? current_binding_level : 0;
6849 /* If we're in the middle of some function, save our state. */
6850 if (cfun)
6852 need_pop = true;
6853 push_function_context ();
6855 else
6856 need_pop = false;
6858 if (scope_chain && previous_class_level)
6859 store_class_bindings (previous_class_level->class_shadowed,
6860 &s->old_bindings);
6862 /* Have to include the global scope, because class-scope decls
6863 aren't listed anywhere useful. */
6864 for (; b; b = b->level_chain)
6866 tree t;
6868 /* Template IDs are inserted into the global level. If they were
6869 inserted into namespace level, finish_file wouldn't find them
6870 when doing pending instantiations. Therefore, don't stop at
6871 namespace level, but continue until :: . */
6872 if (global_scope_p (b))
6873 break;
6875 store_bindings (b->names, &s->old_bindings);
6876 /* We also need to check class_shadowed to save class-level type
6877 bindings, since pushclass doesn't fill in b->names. */
6878 if (b->kind == sk_class)
6879 store_class_bindings (b->class_shadowed, &s->old_bindings);
6881 /* Unwind type-value slots back to top level. */
6882 for (t = b->type_shadowed; t; t = TREE_CHAIN (t))
6883 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (t), TREE_VALUE (t));
6886 FOR_EACH_VEC_SAFE_ELT (s->old_bindings, i, sb)
6887 IDENTIFIER_MARKED (sb->identifier) = 0;
6889 s->prev = scope_chain;
6890 s->bindings = b;
6891 s->need_pop_function_context = need_pop;
6892 s->function_decl = current_function_decl;
6893 s->unevaluated_operand = cp_unevaluated_operand;
6894 s->inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
6895 s->x_stmt_tree.stmts_are_full_exprs_p = true;
6897 scope_chain = s;
6898 current_function_decl = NULL_TREE;
6899 current_lang_base = NULL;
6900 current_lang_name = lang_name_cplusplus;
6901 current_namespace = global_namespace;
6902 push_class_stack ();
6903 cp_unevaluated_operand = 0;
6904 c_inhibit_evaluation_warnings = 0;
6907 static void
6908 do_pop_from_top_level (void)
6910 struct saved_scope *s = scope_chain;
6911 cxx_saved_binding *saved;
6912 size_t i;
6914 /* Clear out class-level bindings cache. */
6915 if (previous_class_level)
6916 invalidate_class_lookup_cache ();
6917 pop_class_stack ();
6919 release_tree_vector (current_lang_base);
6921 scope_chain = s->prev;
6922 FOR_EACH_VEC_SAFE_ELT (s->old_bindings, i, saved)
6924 tree id = saved->identifier;
6926 IDENTIFIER_BINDING (id) = saved->binding;
6927 SET_IDENTIFIER_TYPE_VALUE (id, saved->real_type_value);
6930 /* If we were in the middle of compiling a function, restore our
6931 state. */
6932 if (s->need_pop_function_context)
6933 pop_function_context ();
6934 current_function_decl = s->function_decl;
6935 cp_unevaluated_operand = s->unevaluated_operand;
6936 c_inhibit_evaluation_warnings = s->inhibit_evaluation_warnings;
6938 /* Make this saved_scope structure available for reuse by
6939 push_to_top_level. */
6940 s->prev = free_saved_scope;
6941 free_saved_scope = s;
6944 /* Push into the scope of the namespace NS, even if it is deeply
6945 nested within another namespace. */
6947 static void
6948 do_push_nested_namespace (tree ns)
6950 if (ns == global_namespace)
6951 do_push_to_top_level ();
6952 else
6954 do_push_nested_namespace (CP_DECL_CONTEXT (ns));
6955 gcc_checking_assert
6956 (find_namespace_value (current_namespace, DECL_NAME (ns)) == ns);
6957 resume_scope (NAMESPACE_LEVEL (ns));
6958 current_namespace = ns;
6962 /* Pop back from the scope of the namespace NS, which was previously
6963 entered with push_nested_namespace. */
6965 static void
6966 do_pop_nested_namespace (tree ns)
6968 while (ns != global_namespace)
6970 ns = CP_DECL_CONTEXT (ns);
6971 current_namespace = ns;
6972 leave_scope ();
6975 do_pop_from_top_level ();
6978 /* Add TARGET to USINGS, if it does not already exist there.
6979 We used to build the complete graph of usings at this point, from
6980 the POV of the source namespaces. Now we build that as we perform
6981 the unqualified search. */
6983 static void
6984 add_using_namespace (vec<tree, va_gc> *&usings, tree target)
6986 if (usings)
6987 for (unsigned ix = usings->length (); ix--;)
6988 if ((*usings)[ix] == target)
6989 return;
6991 vec_safe_push (usings, target);
6994 /* Tell the debug system of a using directive. */
6996 static void
6997 emit_debug_info_using_namespace (tree from, tree target, bool implicit)
6999 /* Emit debugging info. */
7000 tree context = from != global_namespace ? from : NULL_TREE;
7001 debug_hooks->imported_module_or_decl (target, NULL_TREE, context, false,
7002 implicit);
7005 /* Process a namespace-scope using directive. */
7007 void
7008 finish_namespace_using_directive (tree target, tree attribs)
7010 gcc_checking_assert (namespace_bindings_p ());
7011 if (target == error_mark_node)
7012 return;
7014 add_using_namespace (DECL_NAMESPACE_USING (current_namespace),
7015 ORIGINAL_NAMESPACE (target));
7016 emit_debug_info_using_namespace (current_namespace,
7017 ORIGINAL_NAMESPACE (target), false);
7019 if (attribs == error_mark_node)
7020 return;
7022 for (tree a = attribs; a; a = TREE_CHAIN (a))
7024 tree name = get_attribute_name (a);
7025 if (is_attribute_p ("strong", name))
7027 warning (0, "strong using directive no longer supported");
7028 if (CP_DECL_CONTEXT (target) == current_namespace)
7029 inform (DECL_SOURCE_LOCATION (target),
7030 "you may use an inline namespace instead");
7032 else
7033 warning (OPT_Wattributes, "%qD attribute directive ignored", name);
7037 /* Process a function-scope using-directive. */
7039 void
7040 finish_local_using_directive (tree target, tree attribs)
7042 gcc_checking_assert (local_bindings_p ());
7043 if (target == error_mark_node)
7044 return;
7046 if (attribs)
7047 warning (OPT_Wattributes, "attributes ignored on local using directive");
7049 add_stmt (build_stmt (input_location, USING_STMT, target));
7051 add_using_namespace (current_binding_level->using_directives,
7052 ORIGINAL_NAMESPACE (target));
7055 /* Pushes X into the global namespace. */
7057 tree
7058 pushdecl_top_level (tree x, bool is_friend)
7060 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
7061 do_push_to_top_level ();
7062 x = pushdecl_namespace_level (x, is_friend);
7063 do_pop_from_top_level ();
7064 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
7065 return x;
7068 /* Pushes X into the global namespace and calls cp_finish_decl to
7069 register the variable, initializing it with INIT. */
7071 tree
7072 pushdecl_top_level_and_finish (tree x, tree init)
7074 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
7075 do_push_to_top_level ();
7076 x = pushdecl_namespace_level (x, false);
7077 cp_finish_decl (x, init, false, NULL_TREE, 0);
7078 do_pop_from_top_level ();
7079 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
7080 return x;
7083 /* Enter the namespaces from current_namerspace to NS. */
7085 static int
7086 push_inline_namespaces (tree ns)
7088 int count = 0;
7089 if (ns != current_namespace)
7091 gcc_assert (ns != global_namespace);
7092 count += push_inline_namespaces (CP_DECL_CONTEXT (ns));
7093 resume_scope (NAMESPACE_LEVEL (ns));
7094 current_namespace = ns;
7095 count++;
7097 return count;
7100 /* Push into the scope of the NAME namespace. If NAME is NULL_TREE,
7101 then we enter an anonymous namespace. If MAKE_INLINE is true, then
7102 we create an inline namespace (it is up to the caller to check upon
7103 redefinition). Return the number of namespaces entered. */
7106 push_namespace (tree name, bool make_inline)
7108 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
7109 int count = 0;
7111 /* We should not get here if the global_namespace is not yet constructed
7112 nor if NAME designates the global namespace: The global scope is
7113 constructed elsewhere. */
7114 gcc_checking_assert (global_namespace != NULL && name != global_identifier);
7116 tree ns = NULL_TREE;
7118 name_lookup lookup (name, 0);
7119 if (!lookup.search_qualified (current_namespace, /*usings=*/false))
7121 else if (TREE_CODE (lookup.value) != NAMESPACE_DECL)
7123 else if (tree dna = DECL_NAMESPACE_ALIAS (lookup.value))
7125 /* A namespace alias is not allowed here, but if the alias
7126 is for a namespace also inside the current scope,
7127 accept it with a diagnostic. That's better than dying
7128 horribly. */
7129 if (is_nested_namespace (current_namespace, CP_DECL_CONTEXT (dna)))
7131 error ("namespace alias %qD not allowed here, "
7132 "assuming %qD", lookup.value, dna);
7133 ns = dna;
7136 else
7137 ns = lookup.value;
7140 bool new_ns = false;
7141 if (ns)
7142 /* DR2061. NS might be a member of an inline namespace. We
7143 need to push into those namespaces. */
7144 count += push_inline_namespaces (CP_DECL_CONTEXT (ns));
7145 else
7147 ns = build_lang_decl (NAMESPACE_DECL, name, void_type_node);
7148 SCOPE_DEPTH (ns) = SCOPE_DEPTH (current_namespace) + 1;
7149 if (!SCOPE_DEPTH (ns))
7150 /* We only allow depth 255. */
7151 sorry ("cannot nest more than %d namespaces",
7152 SCOPE_DEPTH (current_namespace));
7153 DECL_CONTEXT (ns) = FROB_CONTEXT (current_namespace);
7154 new_ns = true;
7156 if (pushdecl (ns) == error_mark_node)
7157 ns = NULL_TREE;
7158 else
7160 if (!name)
7162 SET_DECL_ASSEMBLER_NAME (ns, anon_identifier);
7164 if (!make_inline)
7165 add_using_namespace (DECL_NAMESPACE_USING (current_namespace),
7166 ns);
7168 else if (TREE_PUBLIC (current_namespace))
7169 TREE_PUBLIC (ns) = 1;
7171 if (make_inline)
7173 DECL_NAMESPACE_INLINE_P (ns) = true;
7174 vec_safe_push (DECL_NAMESPACE_INLINEES (current_namespace), ns);
7177 if (!name || make_inline)
7178 emit_debug_info_using_namespace (current_namespace, ns, true);
7182 if (ns)
7184 if (make_inline && !DECL_NAMESPACE_INLINE_P (ns))
7186 error ("inline namespace must be specified at initial definition");
7187 inform (DECL_SOURCE_LOCATION (ns), "%qD defined here", ns);
7189 if (new_ns)
7190 begin_scope (sk_namespace, ns);
7191 else
7192 resume_scope (NAMESPACE_LEVEL (ns));
7193 current_namespace = ns;
7194 count++;
7197 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
7198 return count;
7201 /* Pop from the scope of the current namespace. */
7203 void
7204 pop_namespace (void)
7206 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
7208 gcc_assert (current_namespace != global_namespace);
7209 current_namespace = CP_DECL_CONTEXT (current_namespace);
7210 /* The binding level is not popped, as it might be re-opened later. */
7211 leave_scope ();
7213 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
7216 /* External entry points for do_{push_to/pop_from}_top_level. */
7218 void
7219 push_to_top_level (void)
7221 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
7222 do_push_to_top_level ();
7223 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
7226 void
7227 pop_from_top_level (void)
7229 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
7230 do_pop_from_top_level ();
7231 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
7234 /* External entry points for do_{push,pop}_nested_namespace. */
7236 void
7237 push_nested_namespace (tree ns)
7239 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
7240 do_push_nested_namespace (ns);
7241 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
7244 void
7245 pop_nested_namespace (tree ns)
7247 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
7248 gcc_assert (current_namespace == ns);
7249 do_pop_nested_namespace (ns);
7250 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
7253 /* Pop off extraneous binding levels left over due to syntax errors.
7254 We don't pop past namespaces, as they might be valid. */
7256 void
7257 pop_everything (void)
7259 if (ENABLE_SCOPE_CHECKING)
7260 verbatim ("XXX entering pop_everything ()\n");
7261 while (!namespace_bindings_p ())
7263 if (current_binding_level->kind == sk_class)
7264 pop_nested_class ();
7265 else
7266 poplevel (0, 0, 0);
7268 if (ENABLE_SCOPE_CHECKING)
7269 verbatim ("XXX leaving pop_everything ()\n");
7272 /* Emit debugging information for using declarations and directives.
7273 If input tree is overloaded fn then emit debug info for all
7274 candidates. */
7276 void
7277 cp_emit_debug_info_for_using (tree t, tree context)
7279 /* Don't try to emit any debug information if we have errors. */
7280 if (seen_error ())
7281 return;
7283 /* Ignore this FUNCTION_DECL if it refers to a builtin declaration
7284 of a builtin function. */
7285 if (TREE_CODE (t) == FUNCTION_DECL
7286 && DECL_EXTERNAL (t)
7287 && fndecl_built_in_p (t))
7288 return;
7290 /* Do not supply context to imported_module_or_decl, if
7291 it is a global namespace. */
7292 if (context == global_namespace)
7293 context = NULL_TREE;
7295 t = MAYBE_BASELINK_FUNCTIONS (t);
7297 /* FIXME: Handle TEMPLATE_DECLs. */
7298 for (lkp_iterator iter (t); iter; ++iter)
7300 tree fn = *iter;
7301 if (TREE_CODE (fn) != TEMPLATE_DECL)
7303 if (building_stmt_list_p ())
7304 add_stmt (build_stmt (input_location, USING_STMT, fn));
7305 else
7306 debug_hooks->imported_module_or_decl (fn, NULL_TREE, context,
7307 false, false);
7312 #include "gt-cp-name-lookup.h"