Add support for grouping of related diagnostics (PR other/84889)
[official-gcc.git] / gcc / cp / name-lookup.c
blob8c7f68522da492220550683c45ae8f1676b1cd0d
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 /* Recursively look in its inline children. */
563 if (vec<tree, va_gc> *inlinees = DECL_NAMESPACE_INLINEES (scope))
564 for (unsigned ix = inlinees->length (); ix--;)
565 found |= search_namespace ((*inlinees)[ix]);
567 if (found)
568 mark_found (scope);
570 return found;
573 /* Recursively follow using directives of SCOPE & its inline children.
574 Such following is essentially a flood-fill algorithm. */
576 bool
577 name_lookup::search_usings (tree scope)
579 /* We do not check seen_p here, as that was already set during the
580 namespace_only walk. */
581 if (found_p (scope))
582 return true;
584 bool found = false;
585 if (vec<tree, va_gc> *usings = DECL_NAMESPACE_USING (scope))
586 for (unsigned ix = usings->length (); ix--;)
587 found |= search_qualified ((*usings)[ix], true);
589 /* Look in its inline children. */
590 if (vec<tree, va_gc> *inlinees = DECL_NAMESPACE_INLINEES (scope))
591 for (unsigned ix = inlinees->length (); ix--;)
592 found |= search_usings ((*inlinees)[ix]);
594 if (found)
595 mark_found (scope);
597 return found;
600 /* Qualified namespace lookup in SCOPE.
601 1) Look in SCOPE (+inlines). If found, we're done.
602 2) Otherwise, if USINGS is true,
603 recurse for every using directive of SCOPE (+inlines).
605 Trickiness is (a) loops and (b) multiple paths to same namespace.
606 In both cases we want to not repeat any lookups, and know whether
607 to stop the caller's step #2. Do this via the FOUND_P marker. */
609 bool
610 name_lookup::search_qualified (tree scope, bool usings)
612 bool found = false;
614 if (seen_p (scope))
615 found = found_p (scope);
616 else
618 found = search_namespace (scope);
619 if (!found && usings)
620 found = search_usings (scope);
623 return found;
626 /* Add SCOPE to the unqualified search queue, recursively add its
627 inlines and those via using directives. */
629 name_lookup::using_queue *
630 name_lookup::queue_namespace (using_queue *queue, int depth, tree scope)
632 if (see_and_mark (scope))
633 return queue;
635 /* Record it. */
636 tree common = scope;
637 while (SCOPE_DEPTH (common) > depth)
638 common = CP_DECL_CONTEXT (common);
639 vec_safe_push (queue, using_pair (common, scope));
641 /* Queue its inline children. */
642 if (vec<tree, va_gc> *inlinees = DECL_NAMESPACE_INLINEES (scope))
643 for (unsigned ix = inlinees->length (); ix--;)
644 queue = queue_namespace (queue, depth, (*inlinees)[ix]);
646 /* Queue its using targets. */
647 queue = queue_usings (queue, depth, DECL_NAMESPACE_USING (scope));
649 return queue;
652 /* Add the namespaces in USINGS to the unqualified search queue. */
654 name_lookup::using_queue *
655 name_lookup::do_queue_usings (using_queue *queue, int depth,
656 vec<tree, va_gc> *usings)
658 for (unsigned ix = usings->length (); ix--;)
659 queue = queue_namespace (queue, depth, (*usings)[ix]);
661 return queue;
664 /* Unqualified namespace lookup in SCOPE.
665 1) add scope+inlins to worklist.
666 2) recursively add target of every using directive
667 3) for each worklist item where SCOPE is common ancestor, search it
668 4) if nothing find, scope=parent, goto 1. */
670 bool
671 name_lookup::search_unqualified (tree scope, cp_binding_level *level)
673 /* Make static to avoid continual reallocation. We're not
674 recursive. */
675 static using_queue *queue = NULL;
676 bool found = false;
677 int length = vec_safe_length (queue);
679 /* Queue local using-directives. */
680 for (; level->kind != sk_namespace; level = level->level_chain)
681 queue = queue_usings (queue, SCOPE_DEPTH (scope), level->using_directives);
683 for (; !found; scope = CP_DECL_CONTEXT (scope))
685 gcc_assert (!DECL_NAMESPACE_ALIAS (scope));
686 int depth = SCOPE_DEPTH (scope);
688 /* Queue namespaces reachable from SCOPE. */
689 queue = queue_namespace (queue, depth, scope);
691 /* Search every queued namespace where SCOPE is the common
692 ancestor. Adjust the others. */
693 unsigned ix = length;
696 using_pair &pair = (*queue)[ix];
697 while (pair.first == scope)
699 found |= search_namespace_only (pair.second);
700 pair = queue->pop ();
701 if (ix == queue->length ())
702 goto done;
704 /* The depth is the same as SCOPE, find the parent scope. */
705 if (SCOPE_DEPTH (pair.first) == depth)
706 pair.first = CP_DECL_CONTEXT (pair.first);
707 ix++;
709 while (ix < queue->length ());
710 done:;
711 if (scope == global_namespace)
712 break;
714 /* If looking for hidden names, we only look in the innermost
715 namespace scope. [namespace.memdef]/3 If a friend
716 declaration in a non-local class first declares a class,
717 function, class template or function template the friend is a
718 member of the innermost enclosing namespace. See also
719 [basic.lookup.unqual]/7 */
720 if (flags & LOOKUP_HIDDEN)
721 break;
724 vec_safe_truncate (queue, length);
726 return found;
729 /* FNS is a value binding. If it is a (set of overloaded) functions,
730 add them into the current value. */
732 void
733 name_lookup::add_fns (tree fns)
735 if (!fns)
736 return;
737 else if (TREE_CODE (fns) == OVERLOAD)
739 if (TREE_TYPE (fns) != unknown_type_node)
740 fns = OVL_FUNCTION (fns);
742 else if (!DECL_DECLARES_FUNCTION_P (fns))
743 return;
745 add_overload (fns);
748 /* Add functions of a namespace to the lookup structure. */
750 void
751 name_lookup::adl_namespace_only (tree scope)
753 mark_seen (scope);
755 /* Look down into inline namespaces. */
756 if (vec<tree, va_gc> *inlinees = DECL_NAMESPACE_INLINEES (scope))
757 for (unsigned ix = inlinees->length (); ix--;)
758 adl_namespace_only ((*inlinees)[ix]);
760 if (tree fns = find_namespace_value (scope, name))
761 add_fns (ovl_skip_hidden (fns));
764 /* Find the containing non-inlined namespace, add it and all its
765 inlinees. */
767 void
768 name_lookup::adl_namespace (tree scope)
770 if (seen_p (scope))
771 return;
773 /* Find the containing non-inline namespace. */
774 while (DECL_NAMESPACE_INLINE_P (scope))
775 scope = CP_DECL_CONTEXT (scope);
777 adl_namespace_only (scope);
780 /* Adds the class and its friends to the lookup structure. */
782 void
783 name_lookup::adl_class_only (tree type)
785 /* Backend-built structures, such as __builtin_va_list, aren't
786 affected by all this. */
787 if (!CLASS_TYPE_P (type))
788 return;
790 type = TYPE_MAIN_VARIANT (type);
792 if (see_and_mark (type))
793 return;
795 tree context = decl_namespace_context (type);
796 adl_namespace (context);
798 complete_type (type);
800 /* Add friends. */
801 for (tree list = DECL_FRIENDLIST (TYPE_MAIN_DECL (type)); list;
802 list = TREE_CHAIN (list))
803 if (name == FRIEND_NAME (list))
804 for (tree friends = FRIEND_DECLS (list); friends;
805 friends = TREE_CHAIN (friends))
807 tree fn = TREE_VALUE (friends);
809 /* Only interested in global functions with potentially hidden
810 (i.e. unqualified) declarations. */
811 if (CP_DECL_CONTEXT (fn) != context)
812 continue;
814 /* Only interested in anticipated friends. (Non-anticipated
815 ones will have been inserted during the namespace
816 adl.) */
817 if (!DECL_ANTICIPATED (fn))
818 continue;
820 /* Template specializations are never found by name lookup.
821 (Templates themselves can be found, but not template
822 specializations.) */
823 if (TREE_CODE (fn) == FUNCTION_DECL && DECL_USE_TEMPLATE (fn))
824 continue;
826 add_fns (fn);
830 /* Adds the class and its bases to the lookup structure.
831 Returns true on error. */
833 void
834 name_lookup::adl_bases (tree type)
836 adl_class_only (type);
838 /* Process baseclasses. */
839 if (tree binfo = TYPE_BINFO (type))
841 tree base_binfo;
842 int i;
844 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
845 adl_bases (BINFO_TYPE (base_binfo));
849 /* Adds everything associated with a class argument type to the lookup
850 structure. Returns true on error.
852 If T is a class type (including unions), its associated classes are: the
853 class itself; the class of which it is a member, if any; and its direct
854 and indirect base classes. Its associated namespaces are the namespaces
855 of which its associated classes are members. Furthermore, if T is a
856 class template specialization, its associated namespaces and classes
857 also include: the namespaces and classes associated with the types of
858 the template arguments provided for template type parameters (excluding
859 template template parameters); the namespaces of which any template
860 template arguments are members; and the classes of which any member
861 templates used as template template arguments are members. [ Note:
862 non-type template arguments do not contribute to the set of associated
863 namespaces. --end note] */
865 void
866 name_lookup::adl_class (tree type)
868 /* Backend build structures, such as __builtin_va_list, aren't
869 affected by all this. */
870 if (!CLASS_TYPE_P (type))
871 return;
873 type = TYPE_MAIN_VARIANT (type);
874 /* We don't set found here because we have to have set seen first,
875 which is done in the adl_bases walk. */
876 if (found_p (type))
877 return;
879 adl_bases (type);
880 mark_found (type);
882 if (TYPE_CLASS_SCOPE_P (type))
883 adl_class_only (TYPE_CONTEXT (type));
885 /* Process template arguments. */
886 if (CLASSTYPE_TEMPLATE_INFO (type)
887 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
889 tree list = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
890 for (int i = 0; i < TREE_VEC_LENGTH (list); ++i)
891 adl_template_arg (TREE_VEC_ELT (list, i));
895 void
896 name_lookup::adl_expr (tree expr)
898 if (!expr)
899 return;
901 gcc_assert (!TYPE_P (expr));
903 if (TREE_TYPE (expr) != unknown_type_node)
905 adl_type (TREE_TYPE (expr));
906 return;
909 if (TREE_CODE (expr) == ADDR_EXPR)
910 expr = TREE_OPERAND (expr, 0);
911 if (TREE_CODE (expr) == COMPONENT_REF
912 || TREE_CODE (expr) == OFFSET_REF)
913 expr = TREE_OPERAND (expr, 1);
914 expr = MAYBE_BASELINK_FUNCTIONS (expr);
916 if (OVL_P (expr))
917 for (lkp_iterator iter (expr); iter; ++iter)
918 adl_type (TREE_TYPE (*iter));
919 else if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
921 /* The working paper doesn't currently say how to handle
922 template-id arguments. The sensible thing would seem to be
923 to handle the list of template candidates like a normal
924 overload set, and handle the template arguments like we do
925 for class template specializations. */
927 /* First the templates. */
928 adl_expr (TREE_OPERAND (expr, 0));
930 /* Now the arguments. */
931 if (tree args = TREE_OPERAND (expr, 1))
932 for (int ix = TREE_VEC_LENGTH (args); ix--;)
933 adl_template_arg (TREE_VEC_ELT (args, ix));
937 void
938 name_lookup::adl_type (tree type)
940 if (!type)
941 return;
943 if (TYPE_PTRDATAMEM_P (type))
945 /* Pointer to member: associate class type and value type. */
946 adl_type (TYPE_PTRMEM_CLASS_TYPE (type));
947 adl_type (TYPE_PTRMEM_POINTED_TO_TYPE (type));
948 return;
951 switch (TREE_CODE (type))
953 case RECORD_TYPE:
954 if (TYPE_PTRMEMFUNC_P (type))
956 adl_type (TYPE_PTRMEMFUNC_FN_TYPE (type));
957 return;
959 /* FALLTHRU */
960 case UNION_TYPE:
961 adl_class (type);
962 return;
964 case METHOD_TYPE:
965 /* The basetype is referenced in the first arg type, so just
966 fall through. */
967 case FUNCTION_TYPE:
968 /* Associate the parameter types. */
969 for (tree args = TYPE_ARG_TYPES (type); args; args = TREE_CHAIN (args))
970 adl_type (TREE_VALUE (args));
971 /* FALLTHROUGH */
973 case POINTER_TYPE:
974 case REFERENCE_TYPE:
975 case ARRAY_TYPE:
976 adl_type (TREE_TYPE (type));
977 return;
979 case ENUMERAL_TYPE:
980 if (TYPE_CLASS_SCOPE_P (type))
981 adl_class_only (TYPE_CONTEXT (type));
982 adl_namespace (decl_namespace_context (type));
983 return;
985 case LANG_TYPE:
986 gcc_assert (type == unknown_type_node
987 || type == init_list_type_node);
988 return;
990 case TYPE_PACK_EXPANSION:
991 adl_type (PACK_EXPANSION_PATTERN (type));
992 return;
994 default:
995 break;
999 /* Adds everything associated with a template argument to the lookup
1000 structure. */
1002 void
1003 name_lookup::adl_template_arg (tree arg)
1005 /* [basic.lookup.koenig]
1007 If T is a template-id, its associated namespaces and classes are
1008 ... the namespaces and classes associated with the types of the
1009 template arguments provided for template type parameters
1010 (excluding template template parameters); the namespaces in which
1011 any template template arguments are defined; and the classes in
1012 which any member templates used as template template arguments
1013 are defined. [Note: non-type template arguments do not
1014 contribute to the set of associated namespaces. ] */
1016 /* Consider first template template arguments. */
1017 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
1018 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
1020 else if (TREE_CODE (arg) == TEMPLATE_DECL)
1022 tree ctx = CP_DECL_CONTEXT (arg);
1024 /* It's not a member template. */
1025 if (TREE_CODE (ctx) == NAMESPACE_DECL)
1026 adl_namespace (ctx);
1027 /* Otherwise, it must be member template. */
1028 else
1029 adl_class_only (ctx);
1031 /* It's an argument pack; handle it recursively. */
1032 else if (ARGUMENT_PACK_P (arg))
1034 tree args = ARGUMENT_PACK_ARGS (arg);
1035 int i, len = TREE_VEC_LENGTH (args);
1036 for (i = 0; i < len; ++i)
1037 adl_template_arg (TREE_VEC_ELT (args, i));
1039 /* It's not a template template argument, but it is a type template
1040 argument. */
1041 else if (TYPE_P (arg))
1042 adl_type (arg);
1045 /* Perform ADL lookup. FNS is the existing lookup result and ARGS are
1046 the call arguments. */
1048 tree
1049 name_lookup::search_adl (tree fns, vec<tree, va_gc> *args)
1051 if (fns)
1053 deduping = true;
1054 lookup_mark (fns, true);
1056 value = fns;
1058 unsigned ix;
1059 tree arg;
1061 FOR_EACH_VEC_ELT_REVERSE (*args, ix, arg)
1062 /* OMP reduction operators put an ADL-significant type as the
1063 first arg. */
1064 if (TYPE_P (arg))
1065 adl_type (arg);
1066 else
1067 adl_expr (arg);
1069 fns = value;
1071 return fns;
1074 static bool qualified_namespace_lookup (tree, name_lookup *);
1075 static void consider_binding_level (tree name,
1076 best_match <tree, const char *> &bm,
1077 cp_binding_level *lvl,
1078 bool look_within_fields,
1079 enum lookup_name_fuzzy_kind kind);
1080 static void diagnose_name_conflict (tree, tree);
1082 /* ADL lookup of NAME. FNS is the result of regular lookup, and we
1083 don't add duplicates to it. ARGS is the vector of call
1084 arguments (which will not be empty). */
1086 tree
1087 lookup_arg_dependent (tree name, tree fns, vec<tree, va_gc> *args)
1089 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
1090 name_lookup lookup (name);
1091 fns = lookup.search_adl (fns, args);
1092 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
1093 return fns;
1096 /* FNS is an overload set of conversion functions. Return the
1097 overloads converting to TYPE. */
1099 static tree
1100 extract_conversion_operator (tree fns, tree type)
1102 tree convs = NULL_TREE;
1103 tree tpls = NULL_TREE;
1105 for (ovl_iterator iter (fns); iter; ++iter)
1107 if (same_type_p (DECL_CONV_FN_TYPE (*iter), type))
1108 convs = lookup_add (*iter, convs);
1110 if (TREE_CODE (*iter) == TEMPLATE_DECL)
1111 tpls = lookup_add (*iter, tpls);
1114 if (!convs)
1115 convs = tpls;
1117 return convs;
1120 /* Binary search of (ordered) MEMBER_VEC for NAME. */
1122 static tree
1123 member_vec_binary_search (vec<tree, va_gc> *member_vec, tree name)
1125 for (unsigned lo = 0, hi = member_vec->length (); lo < hi;)
1127 unsigned mid = (lo + hi) / 2;
1128 tree binding = (*member_vec)[mid];
1129 tree binding_name = OVL_NAME (binding);
1131 if (binding_name > name)
1132 hi = mid;
1133 else if (binding_name < name)
1134 lo = mid + 1;
1135 else
1136 return binding;
1139 return NULL_TREE;
1142 /* Linear search of (unordered) MEMBER_VEC for NAME. */
1144 static tree
1145 member_vec_linear_search (vec<tree, va_gc> *member_vec, tree name)
1147 for (int ix = member_vec->length (); ix--;)
1148 if (tree binding = (*member_vec)[ix])
1149 if (OVL_NAME (binding) == name)
1150 return binding;
1152 return NULL_TREE;
1155 /* Linear search of (partially ordered) fields of KLASS for NAME. */
1157 static tree
1158 fields_linear_search (tree klass, tree name, bool want_type)
1160 for (tree fields = TYPE_FIELDS (klass); fields; fields = DECL_CHAIN (fields))
1162 tree decl = fields;
1164 if (TREE_CODE (decl) == FIELD_DECL
1165 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
1167 if (tree temp = search_anon_aggr (TREE_TYPE (decl), name, want_type))
1168 return temp;
1171 if (DECL_NAME (decl) != name)
1172 continue;
1174 if (TREE_CODE (decl) == USING_DECL)
1176 decl = strip_using_decl (decl);
1177 if (is_overloaded_fn (decl))
1178 continue;
1181 if (DECL_DECLARES_FUNCTION_P (decl))
1182 /* Functions are found separately. */
1183 continue;
1185 if (!want_type || DECL_DECLARES_TYPE_P (decl))
1186 return decl;
1189 return NULL_TREE;
1192 /* Look for NAME member inside of anonymous aggregate ANON. Although
1193 such things should only contain FIELD_DECLs, we check that too
1194 late, and would give very confusing errors if we weren't
1195 permissive here. */
1197 tree
1198 search_anon_aggr (tree anon, tree name, bool want_type)
1200 gcc_assert (COMPLETE_TYPE_P (anon));
1201 tree ret = get_class_binding_direct (anon, name, want_type);
1202 return ret;
1205 /* Look for NAME as an immediate member of KLASS (including
1206 anon-members or unscoped enum member). TYPE_OR_FNS is zero for
1207 regular search. >0 to get a type binding (if there is one) and <0
1208 if you want (just) the member function binding.
1210 Use this if you do not want lazy member creation. */
1212 tree
1213 get_class_binding_direct (tree klass, tree name, int type_or_fns)
1215 gcc_checking_assert (RECORD_OR_UNION_TYPE_P (klass));
1217 /* Conversion operators can only be found by the marker conversion
1218 operator name. */
1219 bool conv_op = IDENTIFIER_CONV_OP_P (name);
1220 tree lookup = conv_op ? conv_op_identifier : name;
1221 tree val = NULL_TREE;
1222 vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (klass);
1224 if (COMPLETE_TYPE_P (klass) && member_vec)
1226 val = member_vec_binary_search (member_vec, lookup);
1227 if (!val)
1229 else if (type_or_fns > 0)
1231 if (STAT_HACK_P (val))
1232 val = STAT_TYPE (val);
1233 else if (!DECL_DECLARES_TYPE_P (val))
1234 val = NULL_TREE;
1236 else if (STAT_HACK_P (val))
1237 val = STAT_DECL (val);
1239 if (val && TREE_CODE (val) == OVERLOAD
1240 && TREE_CODE (OVL_FUNCTION (val)) == USING_DECL)
1242 /* An overload with a dependent USING_DECL. Does the caller
1243 want the USING_DECL or the functions? */
1244 if (type_or_fns < 0)
1245 val = OVL_CHAIN (val);
1246 else
1247 val = OVL_FUNCTION (val);
1250 else
1252 if (member_vec && type_or_fns <= 0)
1253 val = member_vec_linear_search (member_vec, lookup);
1255 if (type_or_fns < 0)
1256 /* Don't bother looking for field. We don't want it. */;
1257 else if (!val || (TREE_CODE (val) == OVERLOAD && OVL_USING_P (val)))
1258 /* Dependent using declarations are a 'field', make sure we
1259 return that even if we saw an overload already. */
1260 if (tree field_val = fields_linear_search (klass, lookup,
1261 type_or_fns > 0))
1262 if (!val || TREE_CODE (field_val) == USING_DECL)
1263 val = field_val;
1266 /* Extract the conversion operators asked for, unless the general
1267 conversion operator was requested. */
1268 if (val && conv_op)
1270 gcc_checking_assert (OVL_FUNCTION (val) == conv_op_marker);
1271 val = OVL_CHAIN (val);
1272 if (tree type = TREE_TYPE (name))
1273 val = extract_conversion_operator (val, type);
1276 return val;
1279 /* Look for NAME's binding in exactly KLASS. See
1280 get_class_binding_direct for argument description. Does lazy
1281 special function creation as necessary. */
1283 tree
1284 get_class_binding (tree klass, tree name, int type_or_fns)
1286 klass = complete_type (klass);
1288 if (COMPLETE_TYPE_P (klass))
1290 /* Lazily declare functions, if we're going to search these. */
1291 if (IDENTIFIER_CTOR_P (name))
1293 if (CLASSTYPE_LAZY_DEFAULT_CTOR (klass))
1294 lazily_declare_fn (sfk_constructor, klass);
1295 if (CLASSTYPE_LAZY_COPY_CTOR (klass))
1296 lazily_declare_fn (sfk_copy_constructor, klass);
1297 if (CLASSTYPE_LAZY_MOVE_CTOR (klass))
1298 lazily_declare_fn (sfk_move_constructor, klass);
1300 else if (IDENTIFIER_DTOR_P (name))
1302 if (CLASSTYPE_LAZY_DESTRUCTOR (klass))
1303 lazily_declare_fn (sfk_destructor, klass);
1305 else if (name == assign_op_identifier)
1307 if (CLASSTYPE_LAZY_COPY_ASSIGN (klass))
1308 lazily_declare_fn (sfk_copy_assignment, klass);
1309 if (CLASSTYPE_LAZY_MOVE_ASSIGN (klass))
1310 lazily_declare_fn (sfk_move_assignment, klass);
1314 return get_class_binding_direct (klass, name, type_or_fns);
1317 /* Find the slot containing overloads called 'NAME'. If there is no
1318 such slot and the class is complete, create an empty one, at the
1319 correct point in the sorted member vector. Otherwise return NULL.
1320 Deals with conv_op marker handling. */
1322 tree *
1323 find_member_slot (tree klass, tree name)
1325 bool complete_p = COMPLETE_TYPE_P (klass);
1327 vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (klass);
1328 if (!member_vec)
1330 vec_alloc (member_vec, 8);
1331 CLASSTYPE_MEMBER_VEC (klass) = member_vec;
1332 if (complete_p)
1334 /* If the class is complete but had no member_vec, we need
1335 to add the TYPE_FIELDS into it. We're also most likely
1336 to be adding ctors & dtors, so ask for 6 spare slots (the
1337 abstract cdtors and their clones). */
1338 set_class_bindings (klass, 6);
1339 member_vec = CLASSTYPE_MEMBER_VEC (klass);
1343 if (IDENTIFIER_CONV_OP_P (name))
1344 name = conv_op_identifier;
1346 unsigned ix, length = member_vec->length ();
1347 for (ix = 0; ix < length; ix++)
1349 tree *slot = &(*member_vec)[ix];
1350 tree fn_name = OVL_NAME (*slot);
1352 if (fn_name == name)
1354 /* If we found an existing slot, it must be a function set.
1355 Even with insertion after completion, because those only
1356 happen with artificial fns that have unspellable names.
1357 This means we do not have to deal with the stat hack
1358 either. */
1359 gcc_checking_assert (OVL_P (*slot));
1360 if (name == conv_op_identifier)
1362 gcc_checking_assert (OVL_FUNCTION (*slot) == conv_op_marker);
1363 /* Skip the conv-op marker. */
1364 slot = &OVL_CHAIN (*slot);
1366 return slot;
1369 if (complete_p && fn_name > name)
1370 break;
1373 /* No slot found, add one if the class is complete. */
1374 if (complete_p)
1376 /* Do exact allocation, as we don't expect to add many. */
1377 gcc_assert (name != conv_op_identifier);
1378 vec_safe_reserve_exact (member_vec, 1);
1379 CLASSTYPE_MEMBER_VEC (klass) = member_vec;
1380 member_vec->quick_insert (ix, NULL_TREE);
1381 return &(*member_vec)[ix];
1384 return NULL;
1387 /* KLASS is an incomplete class to which we're adding a method NAME.
1388 Add a slot and deal with conv_op marker handling. */
1390 tree *
1391 add_member_slot (tree klass, tree name)
1393 gcc_assert (!COMPLETE_TYPE_P (klass));
1395 vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (klass);
1396 vec_safe_push (member_vec, NULL_TREE);
1397 CLASSTYPE_MEMBER_VEC (klass) = member_vec;
1399 tree *slot = &member_vec->last ();
1400 if (IDENTIFIER_CONV_OP_P (name))
1402 /* Install the marker prefix. */
1403 *slot = ovl_make (conv_op_marker, NULL_TREE);
1404 slot = &OVL_CHAIN (*slot);
1407 return slot;
1410 /* Comparison function to compare two MEMBER_VEC entries by name.
1411 Because we can have duplicates during insertion of TYPE_FIELDS, we
1412 do extra checking so deduping doesn't have to deal with so many
1413 cases. */
1415 static int
1416 member_name_cmp (const void *a_p, const void *b_p)
1418 tree a = *(const tree *)a_p;
1419 tree b = *(const tree *)b_p;
1420 tree name_a = DECL_NAME (TREE_CODE (a) == OVERLOAD ? OVL_FUNCTION (a) : a);
1421 tree name_b = DECL_NAME (TREE_CODE (b) == OVERLOAD ? OVL_FUNCTION (b) : b);
1423 gcc_checking_assert (name_a && name_b);
1424 if (name_a != name_b)
1425 return name_a < name_b ? -1 : +1;
1427 if (name_a == conv_op_identifier)
1429 /* Strip the conv-op markers. */
1430 gcc_checking_assert (OVL_FUNCTION (a) == conv_op_marker
1431 && OVL_FUNCTION (b) == conv_op_marker);
1432 a = OVL_CHAIN (a);
1433 b = OVL_CHAIN (b);
1436 if (TREE_CODE (a) == OVERLOAD)
1437 a = OVL_FUNCTION (a);
1438 if (TREE_CODE (b) == OVERLOAD)
1439 b = OVL_FUNCTION (b);
1441 /* We're in STAT_HACK or USING_DECL territory (or possibly error-land). */
1442 if (TREE_CODE (a) != TREE_CODE (b))
1444 /* If one of them is a TYPE_DECL, it loses. */
1445 if (TREE_CODE (a) == TYPE_DECL)
1446 return +1;
1447 else if (TREE_CODE (b) == TYPE_DECL)
1448 return -1;
1450 /* If one of them is a USING_DECL, it loses. */
1451 if (TREE_CODE (a) == USING_DECL)
1452 return +1;
1453 else if (TREE_CODE (b) == USING_DECL)
1454 return -1;
1456 /* There are no other cases with different kinds of decls, as
1457 duplicate detection should have kicked in earlier. However,
1458 some erroneous cases get though. */
1459 gcc_assert (errorcount);
1462 /* Using source location would be the best thing here, but we can
1463 get identically-located decls in the following circumstances:
1465 1) duplicate artificial type-decls for the same type.
1467 2) pack expansions of using-decls.
1469 We should not be doing #1, but in either case it doesn't matter
1470 how we order these. Use UID as a proxy for source ordering, so
1471 that identically-located decls still have a well-defined stable
1472 ordering. */
1473 if (DECL_UID (a) != DECL_UID (b))
1474 return DECL_UID (a) < DECL_UID (b) ? -1 : +1;
1475 gcc_assert (a == b);
1476 return 0;
1479 static struct {
1480 gt_pointer_operator new_value;
1481 void *cookie;
1482 } resort_data;
1484 /* This routine compares two fields like member_name_cmp but using the
1485 pointer operator in resort_field_decl_data. We don't have to deal
1486 with duplicates here. */
1488 static int
1489 resort_member_name_cmp (const void *a_p, const void *b_p)
1491 tree a = *(const tree *)a_p;
1492 tree b = *(const tree *)b_p;
1493 tree name_a = OVL_NAME (a);
1494 tree name_b = OVL_NAME (b);
1496 resort_data.new_value (&name_a, resort_data.cookie);
1497 resort_data.new_value (&name_b, resort_data.cookie);
1499 gcc_checking_assert (name_a != name_b);
1501 return name_a < name_b ? -1 : +1;
1504 /* Resort CLASSTYPE_MEMBER_VEC because pointers have been reordered. */
1506 void
1507 resort_type_member_vec (void *obj, void */*orig_obj*/,
1508 gt_pointer_operator new_value, void* cookie)
1510 if (vec<tree, va_gc> *member_vec = (vec<tree, va_gc> *) obj)
1512 resort_data.new_value = new_value;
1513 resort_data.cookie = cookie;
1514 member_vec->qsort (resort_member_name_cmp);
1518 /* Recursively count the number of fields in KLASS, including anonymous
1519 union members. */
1521 static unsigned
1522 count_class_fields (tree klass)
1524 unsigned n_fields = 0;
1526 for (tree fields = TYPE_FIELDS (klass); fields; fields = DECL_CHAIN (fields))
1527 if (DECL_DECLARES_FUNCTION_P (fields))
1528 /* Functions are dealt with separately. */;
1529 else if (TREE_CODE (fields) == FIELD_DECL
1530 && ANON_AGGR_TYPE_P (TREE_TYPE (fields)))
1531 n_fields += count_class_fields (TREE_TYPE (fields));
1532 else if (DECL_NAME (fields))
1533 n_fields += 1;
1535 return n_fields;
1538 /* Append all the nonfunction members fields of KLASS to MEMBER_VEC.
1539 Recurse for anonymous members. MEMBER_VEC must have space. */
1541 static void
1542 member_vec_append_class_fields (vec<tree, va_gc> *member_vec, tree klass)
1544 for (tree fields = TYPE_FIELDS (klass); fields; fields = DECL_CHAIN (fields))
1545 if (DECL_DECLARES_FUNCTION_P (fields))
1546 /* Functions are handled separately. */;
1547 else if (TREE_CODE (fields) == FIELD_DECL
1548 && ANON_AGGR_TYPE_P (TREE_TYPE (fields)))
1549 member_vec_append_class_fields (member_vec, TREE_TYPE (fields));
1550 else if (DECL_NAME (fields))
1552 tree field = fields;
1553 /* Mark a conv-op USING_DECL with the conv-op-marker. */
1554 if (TREE_CODE (field) == USING_DECL
1555 && IDENTIFIER_CONV_OP_P (DECL_NAME (field)))
1556 field = ovl_make (conv_op_marker, field);
1557 member_vec->quick_push (field);
1561 /* Append all of the enum values of ENUMTYPE to MEMBER_VEC.
1562 MEMBER_VEC must have space. */
1564 static void
1565 member_vec_append_enum_values (vec<tree, va_gc> *member_vec, tree enumtype)
1567 for (tree values = TYPE_VALUES (enumtype);
1568 values; values = TREE_CHAIN (values))
1569 member_vec->quick_push (TREE_VALUE (values));
1572 /* MEMBER_VEC has just had new DECLs added to it, but is sorted.
1573 DeDup adjacent DECLS of the same name. We already dealt with
1574 conflict resolution when adding the fields or methods themselves.
1575 There are three cases (which could all be combined):
1576 1) a TYPE_DECL and non TYPE_DECL. Deploy STAT_HACK as appropriate.
1577 2) a USING_DECL and an overload. If the USING_DECL is dependent,
1578 it wins. Otherwise the OVERLOAD does.
1579 3) two USING_DECLS. ...
1581 member_name_cmp will have ordered duplicates as
1582 <fns><using><type> */
1584 static void
1585 member_vec_dedup (vec<tree, va_gc> *member_vec)
1587 unsigned len = member_vec->length ();
1588 unsigned store = 0;
1590 if (!len)
1591 return;
1593 tree name = OVL_NAME ((*member_vec)[0]);
1594 for (unsigned jx, ix = 0; ix < len; ix = jx)
1596 tree current = NULL_TREE;
1597 tree to_type = NULL_TREE;
1598 tree to_using = NULL_TREE;
1599 tree marker = NULL_TREE;
1601 for (jx = ix; jx < len; jx++)
1603 tree next = (*member_vec)[jx];
1604 if (jx != ix)
1606 tree next_name = OVL_NAME (next);
1607 if (next_name != name)
1609 name = next_name;
1610 break;
1614 if (IDENTIFIER_CONV_OP_P (name))
1616 marker = next;
1617 next = OVL_CHAIN (next);
1620 if (TREE_CODE (next) == USING_DECL)
1622 if (IDENTIFIER_CTOR_P (name))
1623 /* Dependent inherited ctor. */
1624 continue;
1626 next = strip_using_decl (next);
1627 if (TREE_CODE (next) == USING_DECL)
1629 to_using = next;
1630 continue;
1633 if (is_overloaded_fn (next))
1634 continue;
1637 if (DECL_DECLARES_TYPE_P (next))
1639 to_type = next;
1640 continue;
1643 if (!current)
1644 current = next;
1647 if (to_using)
1649 if (!current)
1650 current = to_using;
1651 else
1652 current = ovl_make (to_using, current);
1655 if (to_type)
1657 if (!current)
1658 current = to_type;
1659 else
1660 current = stat_hack (current, to_type);
1663 if (current)
1665 if (marker)
1667 OVL_CHAIN (marker) = current;
1668 current = marker;
1670 (*member_vec)[store++] = current;
1674 while (store++ < len)
1675 member_vec->pop ();
1678 /* Add the non-function members to CLASSTYPE_MEMBER_VEC. If there is
1679 no existing MEMBER_VEC and fewer than 8 fields, do nothing. We
1680 know there must be at least 1 field -- the self-reference
1681 TYPE_DECL, except for anon aggregates, which will have at least
1682 one field. */
1684 void
1685 set_class_bindings (tree klass, unsigned extra)
1687 unsigned n_fields = count_class_fields (klass);
1688 vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (klass);
1690 if (member_vec || n_fields >= 8)
1692 /* Append the new fields. */
1693 vec_safe_reserve_exact (member_vec, extra + n_fields);
1694 member_vec_append_class_fields (member_vec, klass);
1697 if (member_vec)
1699 CLASSTYPE_MEMBER_VEC (klass) = member_vec;
1700 member_vec->qsort (member_name_cmp);
1701 member_vec_dedup (member_vec);
1705 /* Insert lately defined enum ENUMTYPE into KLASS for the sorted case. */
1707 void
1708 insert_late_enum_def_bindings (tree klass, tree enumtype)
1710 int n_fields;
1711 vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (klass);
1713 /* The enum bindings will already be on the TYPE_FIELDS, so don't
1714 count them twice. */
1715 if (!member_vec)
1716 n_fields = count_class_fields (klass);
1717 else
1718 n_fields = list_length (TYPE_VALUES (enumtype));
1720 if (member_vec || n_fields >= 8)
1722 vec_safe_reserve_exact (member_vec, n_fields);
1723 if (CLASSTYPE_MEMBER_VEC (klass))
1724 member_vec_append_enum_values (member_vec, enumtype);
1725 else
1726 member_vec_append_class_fields (member_vec, klass);
1727 CLASSTYPE_MEMBER_VEC (klass) = member_vec;
1728 member_vec->qsort (member_name_cmp);
1729 member_vec_dedup (member_vec);
1733 /* Compute the chain index of a binding_entry given the HASH value of its
1734 name and the total COUNT of chains. COUNT is assumed to be a power
1735 of 2. */
1737 #define ENTRY_INDEX(HASH, COUNT) (((HASH) >> 3) & ((COUNT) - 1))
1739 /* A free list of "binding_entry"s awaiting for re-use. */
1741 static GTY((deletable)) binding_entry free_binding_entry = NULL;
1743 /* The binding oracle; see cp-tree.h. */
1745 cp_binding_oracle_function *cp_binding_oracle;
1747 /* If we have a binding oracle, ask it for all namespace-scoped
1748 definitions of NAME. */
1750 static inline void
1751 query_oracle (tree name)
1753 if (!cp_binding_oracle)
1754 return;
1756 /* LOOKED_UP holds the set of identifiers that we have already
1757 looked up with the oracle. */
1758 static hash_set<tree> looked_up;
1759 if (looked_up.add (name))
1760 return;
1762 cp_binding_oracle (CP_ORACLE_IDENTIFIER, name);
1765 /* Create a binding_entry object for (NAME, TYPE). */
1767 static inline binding_entry
1768 binding_entry_make (tree name, tree type)
1770 binding_entry entry;
1772 if (free_binding_entry)
1774 entry = free_binding_entry;
1775 free_binding_entry = entry->chain;
1777 else
1778 entry = ggc_alloc<binding_entry_s> ();
1780 entry->name = name;
1781 entry->type = type;
1782 entry->chain = NULL;
1784 return entry;
1787 /* Put ENTRY back on the free list. */
1788 #if 0
1789 static inline void
1790 binding_entry_free (binding_entry entry)
1792 entry->name = NULL;
1793 entry->type = NULL;
1794 entry->chain = free_binding_entry;
1795 free_binding_entry = entry;
1797 #endif
1799 /* The datatype used to implement the mapping from names to types at
1800 a given scope. */
1801 struct GTY(()) binding_table_s {
1802 /* Array of chains of "binding_entry"s */
1803 binding_entry * GTY((length ("%h.chain_count"))) chain;
1805 /* The number of chains in this table. This is the length of the
1806 member "chain" considered as an array. */
1807 size_t chain_count;
1809 /* Number of "binding_entry"s in this table. */
1810 size_t entry_count;
1813 /* Construct TABLE with an initial CHAIN_COUNT. */
1815 static inline void
1816 binding_table_construct (binding_table table, size_t chain_count)
1818 table->chain_count = chain_count;
1819 table->entry_count = 0;
1820 table->chain = ggc_cleared_vec_alloc<binding_entry> (table->chain_count);
1823 /* Make TABLE's entries ready for reuse. */
1824 #if 0
1825 static void
1826 binding_table_free (binding_table table)
1828 size_t i;
1829 size_t count;
1831 if (table == NULL)
1832 return;
1834 for (i = 0, count = table->chain_count; i < count; ++i)
1836 binding_entry temp = table->chain[i];
1837 while (temp != NULL)
1839 binding_entry entry = temp;
1840 temp = entry->chain;
1841 binding_entry_free (entry);
1843 table->chain[i] = NULL;
1845 table->entry_count = 0;
1847 #endif
1849 /* Allocate a table with CHAIN_COUNT, assumed to be a power of two. */
1851 static inline binding_table
1852 binding_table_new (size_t chain_count)
1854 binding_table table = ggc_alloc<binding_table_s> ();
1855 table->chain = NULL;
1856 binding_table_construct (table, chain_count);
1857 return table;
1860 /* Expand TABLE to twice its current chain_count. */
1862 static void
1863 binding_table_expand (binding_table table)
1865 const size_t old_chain_count = table->chain_count;
1866 const size_t old_entry_count = table->entry_count;
1867 const size_t new_chain_count = 2 * old_chain_count;
1868 binding_entry *old_chains = table->chain;
1869 size_t i;
1871 binding_table_construct (table, new_chain_count);
1872 for (i = 0; i < old_chain_count; ++i)
1874 binding_entry entry = old_chains[i];
1875 for (; entry != NULL; entry = old_chains[i])
1877 const unsigned int hash = IDENTIFIER_HASH_VALUE (entry->name);
1878 const size_t j = ENTRY_INDEX (hash, new_chain_count);
1880 old_chains[i] = entry->chain;
1881 entry->chain = table->chain[j];
1882 table->chain[j] = entry;
1885 table->entry_count = old_entry_count;
1888 /* Insert a binding for NAME to TYPE into TABLE. */
1890 static void
1891 binding_table_insert (binding_table table, tree name, tree type)
1893 const unsigned int hash = IDENTIFIER_HASH_VALUE (name);
1894 const size_t i = ENTRY_INDEX (hash, table->chain_count);
1895 binding_entry entry = binding_entry_make (name, type);
1897 entry->chain = table->chain[i];
1898 table->chain[i] = entry;
1899 ++table->entry_count;
1901 if (3 * table->chain_count < 5 * table->entry_count)
1902 binding_table_expand (table);
1905 /* Return the binding_entry, if any, that maps NAME. */
1907 binding_entry
1908 binding_table_find (binding_table table, tree name)
1910 const unsigned int hash = IDENTIFIER_HASH_VALUE (name);
1911 binding_entry entry = table->chain[ENTRY_INDEX (hash, table->chain_count)];
1913 while (entry != NULL && entry->name != name)
1914 entry = entry->chain;
1916 return entry;
1919 /* Apply PROC -- with DATA -- to all entries in TABLE. */
1921 void
1922 binding_table_foreach (binding_table table, bt_foreach_proc proc, void *data)
1924 size_t chain_count;
1925 size_t i;
1927 if (!table)
1928 return;
1930 chain_count = table->chain_count;
1931 for (i = 0; i < chain_count; ++i)
1933 binding_entry entry = table->chain[i];
1934 for (; entry != NULL; entry = entry->chain)
1935 proc (entry, data);
1939 #ifndef ENABLE_SCOPE_CHECKING
1940 # define ENABLE_SCOPE_CHECKING 0
1941 #else
1942 # define ENABLE_SCOPE_CHECKING 1
1943 #endif
1945 /* A free list of "cxx_binding"s, connected by their PREVIOUS. */
1947 static GTY((deletable)) cxx_binding *free_bindings;
1949 /* Initialize VALUE and TYPE field for BINDING, and set the PREVIOUS
1950 field to NULL. */
1952 static inline void
1953 cxx_binding_init (cxx_binding *binding, tree value, tree type)
1955 binding->value = value;
1956 binding->type = type;
1957 binding->previous = NULL;
1960 /* (GC)-allocate a binding object with VALUE and TYPE member initialized. */
1962 static cxx_binding *
1963 cxx_binding_make (tree value, tree type)
1965 cxx_binding *binding;
1966 if (free_bindings)
1968 binding = free_bindings;
1969 free_bindings = binding->previous;
1971 else
1972 binding = ggc_alloc<cxx_binding> ();
1974 cxx_binding_init (binding, value, type);
1976 return binding;
1979 /* Put BINDING back on the free list. */
1981 static inline void
1982 cxx_binding_free (cxx_binding *binding)
1984 binding->scope = NULL;
1985 binding->previous = free_bindings;
1986 free_bindings = binding;
1989 /* Create a new binding for NAME (with the indicated VALUE and TYPE
1990 bindings) in the class scope indicated by SCOPE. */
1992 static cxx_binding *
1993 new_class_binding (tree name, tree value, tree type, cp_binding_level *scope)
1995 cp_class_binding cb = {cxx_binding_make (value, type), name};
1996 cxx_binding *binding = cb.base;
1997 vec_safe_push (scope->class_shadowed, cb);
1998 binding->scope = scope;
1999 return binding;
2002 /* Make DECL the innermost binding for ID. The LEVEL is the binding
2003 level at which this declaration is being bound. */
2005 void
2006 push_binding (tree id, tree decl, cp_binding_level* level)
2008 cxx_binding *binding;
2010 if (level != class_binding_level)
2012 binding = cxx_binding_make (decl, NULL_TREE);
2013 binding->scope = level;
2015 else
2016 binding = new_class_binding (id, decl, /*type=*/NULL_TREE, level);
2018 /* Now, fill in the binding information. */
2019 binding->previous = IDENTIFIER_BINDING (id);
2020 INHERITED_VALUE_BINDING_P (binding) = 0;
2021 LOCAL_BINDING_P (binding) = (level != class_binding_level);
2023 /* And put it on the front of the list of bindings for ID. */
2024 IDENTIFIER_BINDING (id) = binding;
2027 /* Remove the binding for DECL which should be the innermost binding
2028 for ID. */
2030 void
2031 pop_local_binding (tree id, tree decl)
2033 cxx_binding *binding;
2035 if (id == NULL_TREE)
2036 /* It's easiest to write the loops that call this function without
2037 checking whether or not the entities involved have names. We
2038 get here for such an entity. */
2039 return;
2041 /* Get the innermost binding for ID. */
2042 binding = IDENTIFIER_BINDING (id);
2044 /* The name should be bound. */
2045 gcc_assert (binding != NULL);
2047 /* The DECL will be either the ordinary binding or the type
2048 binding for this identifier. Remove that binding. */
2049 if (binding->value == decl)
2050 binding->value = NULL_TREE;
2051 else
2053 gcc_assert (binding->type == decl);
2054 binding->type = NULL_TREE;
2057 if (!binding->value && !binding->type)
2059 /* We're completely done with the innermost binding for this
2060 identifier. Unhook it from the list of bindings. */
2061 IDENTIFIER_BINDING (id) = binding->previous;
2063 /* Add it to the free list. */
2064 cxx_binding_free (binding);
2068 /* Remove the bindings for the decls of the current level and leave
2069 the current scope. */
2071 void
2072 pop_bindings_and_leave_scope (void)
2074 for (tree t = get_local_decls (); t; t = DECL_CHAIN (t))
2076 tree decl = TREE_CODE (t) == TREE_LIST ? TREE_VALUE (t) : t;
2077 tree name = OVL_NAME (decl);
2079 pop_local_binding (name, decl);
2082 leave_scope ();
2085 /* Strip non dependent using declarations. If DECL is dependent,
2086 surreptitiously create a typename_type and return it. */
2088 tree
2089 strip_using_decl (tree decl)
2091 if (decl == NULL_TREE)
2092 return NULL_TREE;
2094 while (TREE_CODE (decl) == USING_DECL && !DECL_DEPENDENT_P (decl))
2095 decl = USING_DECL_DECLS (decl);
2097 if (TREE_CODE (decl) == USING_DECL && DECL_DEPENDENT_P (decl)
2098 && USING_DECL_TYPENAME_P (decl))
2100 /* We have found a type introduced by a using
2101 declaration at class scope that refers to a dependent
2102 type.
2104 using typename :: [opt] nested-name-specifier unqualified-id ;
2106 decl = make_typename_type (TREE_TYPE (decl),
2107 DECL_NAME (decl),
2108 typename_type, tf_error);
2109 if (decl != error_mark_node)
2110 decl = TYPE_NAME (decl);
2113 return decl;
2116 /* Return true if OVL is an overload for an anticipated builtin. */
2118 static bool
2119 anticipated_builtin_p (tree ovl)
2121 if (TREE_CODE (ovl) != OVERLOAD)
2122 return false;
2124 if (!OVL_HIDDEN_P (ovl))
2125 return false;
2127 tree fn = OVL_FUNCTION (ovl);
2128 gcc_checking_assert (DECL_ANTICIPATED (fn));
2130 if (DECL_HIDDEN_FRIEND_P (fn))
2131 return false;
2133 return true;
2136 /* BINDING records an existing declaration for a name in the current scope.
2137 But, DECL is another declaration for that same identifier in the
2138 same scope. This is the `struct stat' hack whereby a non-typedef
2139 class name or enum-name can be bound at the same level as some other
2140 kind of entity.
2141 3.3.7/1
2143 A class name (9.1) or enumeration name (7.2) can be hidden by the
2144 name of an object, function, or enumerator declared in the same scope.
2145 If a class or enumeration name and an object, function, or enumerator
2146 are declared in the same scope (in any order) with the same name, the
2147 class or enumeration name is hidden wherever the object, function, or
2148 enumerator name is visible.
2150 It's the responsibility of the caller to check that
2151 inserting this name is valid here. Returns nonzero if the new binding
2152 was successful. */
2154 static bool
2155 supplement_binding_1 (cxx_binding *binding, tree decl)
2157 tree bval = binding->value;
2158 bool ok = true;
2159 tree target_bval = strip_using_decl (bval);
2160 tree target_decl = strip_using_decl (decl);
2162 if (TREE_CODE (target_decl) == TYPE_DECL && DECL_ARTIFICIAL (target_decl)
2163 && target_decl != target_bval
2164 && (TREE_CODE (target_bval) != TYPE_DECL
2165 /* We allow pushing an enum multiple times in a class
2166 template in order to handle late matching of underlying
2167 type on an opaque-enum-declaration followed by an
2168 enum-specifier. */
2169 || (processing_template_decl
2170 && TREE_CODE (TREE_TYPE (target_decl)) == ENUMERAL_TYPE
2171 && TREE_CODE (TREE_TYPE (target_bval)) == ENUMERAL_TYPE
2172 && (dependent_type_p (ENUM_UNDERLYING_TYPE
2173 (TREE_TYPE (target_decl)))
2174 || dependent_type_p (ENUM_UNDERLYING_TYPE
2175 (TREE_TYPE (target_bval)))))))
2176 /* The new name is the type name. */
2177 binding->type = decl;
2178 else if (/* TARGET_BVAL is null when push_class_level_binding moves
2179 an inherited type-binding out of the way to make room
2180 for a new value binding. */
2181 !target_bval
2182 /* TARGET_BVAL is error_mark_node when TARGET_DECL's name
2183 has been used in a non-class scope prior declaration.
2184 In that case, we should have already issued a
2185 diagnostic; for graceful error recovery purpose, pretend
2186 this was the intended declaration for that name. */
2187 || target_bval == error_mark_node
2188 /* If TARGET_BVAL is anticipated but has not yet been
2189 declared, pretend it is not there at all. */
2190 || anticipated_builtin_p (target_bval))
2191 binding->value = decl;
2192 else if (TREE_CODE (target_bval) == TYPE_DECL
2193 && DECL_ARTIFICIAL (target_bval)
2194 && target_decl != target_bval
2195 && (TREE_CODE (target_decl) != TYPE_DECL
2196 || same_type_p (TREE_TYPE (target_decl),
2197 TREE_TYPE (target_bval))))
2199 /* The old binding was a type name. It was placed in
2200 VALUE field because it was thought, at the point it was
2201 declared, to be the only entity with such a name. Move the
2202 type name into the type slot; it is now hidden by the new
2203 binding. */
2204 binding->type = bval;
2205 binding->value = decl;
2206 binding->value_is_inherited = false;
2208 else if (TREE_CODE (target_bval) == TYPE_DECL
2209 && TREE_CODE (target_decl) == TYPE_DECL
2210 && DECL_NAME (target_decl) == DECL_NAME (target_bval)
2211 && binding->scope->kind != sk_class
2212 && (same_type_p (TREE_TYPE (target_decl), TREE_TYPE (target_bval))
2213 /* If either type involves template parameters, we must
2214 wait until instantiation. */
2215 || uses_template_parms (TREE_TYPE (target_decl))
2216 || uses_template_parms (TREE_TYPE (target_bval))))
2217 /* We have two typedef-names, both naming the same type to have
2218 the same name. In general, this is OK because of:
2220 [dcl.typedef]
2222 In a given scope, a typedef specifier can be used to redefine
2223 the name of any type declared in that scope to refer to the
2224 type to which it already refers.
2226 However, in class scopes, this rule does not apply due to the
2227 stricter language in [class.mem] prohibiting redeclarations of
2228 members. */
2229 ok = false;
2230 /* There can be two block-scope declarations of the same variable,
2231 so long as they are `extern' declarations. However, there cannot
2232 be two declarations of the same static data member:
2234 [class.mem]
2236 A member shall not be declared twice in the
2237 member-specification. */
2238 else if (VAR_P (target_decl)
2239 && VAR_P (target_bval)
2240 && DECL_EXTERNAL (target_decl) && DECL_EXTERNAL (target_bval)
2241 && !DECL_CLASS_SCOPE_P (target_decl))
2243 duplicate_decls (decl, binding->value, /*newdecl_is_friend=*/false);
2244 ok = false;
2246 else if (TREE_CODE (decl) == NAMESPACE_DECL
2247 && TREE_CODE (bval) == NAMESPACE_DECL
2248 && DECL_NAMESPACE_ALIAS (decl)
2249 && DECL_NAMESPACE_ALIAS (bval)
2250 && ORIGINAL_NAMESPACE (bval) == ORIGINAL_NAMESPACE (decl))
2251 /* [namespace.alias]
2253 In a declarative region, a namespace-alias-definition can be
2254 used to redefine a namespace-alias declared in that declarative
2255 region to refer only to the namespace to which it already
2256 refers. */
2257 ok = false;
2258 else
2260 if (!error_operand_p (bval))
2261 diagnose_name_conflict (decl, bval);
2262 ok = false;
2265 return ok;
2268 /* Diagnose a name conflict between DECL and BVAL. */
2270 static void
2271 diagnose_name_conflict (tree decl, tree bval)
2273 if (TREE_CODE (decl) == TREE_CODE (bval)
2274 && TREE_CODE (decl) != NAMESPACE_DECL
2275 && !DECL_DECLARES_FUNCTION_P (decl)
2276 && (TREE_CODE (decl) != TYPE_DECL
2277 || DECL_ARTIFICIAL (decl) == DECL_ARTIFICIAL (bval))
2278 && CP_DECL_CONTEXT (decl) == CP_DECL_CONTEXT (bval))
2279 error ("redeclaration of %q#D", decl);
2280 else
2281 error ("%q#D conflicts with a previous declaration", decl);
2283 inform (location_of (bval), "previous declaration %q#D", bval);
2286 /* Wrapper for supplement_binding_1. */
2288 static bool
2289 supplement_binding (cxx_binding *binding, tree decl)
2291 bool ret;
2292 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
2293 ret = supplement_binding_1 (binding, decl);
2294 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
2295 return ret;
2298 /* Replace BINDING's current value on its scope's name list with
2299 NEWVAL. */
2301 static void
2302 update_local_overload (cxx_binding *binding, tree newval)
2304 tree *d;
2306 for (d = &binding->scope->names; ; d = &TREE_CHAIN (*d))
2307 if (*d == binding->value)
2309 /* Stitch new list node in. */
2310 *d = tree_cons (NULL_TREE, NULL_TREE, TREE_CHAIN (*d));
2311 break;
2313 else if (TREE_CODE (*d) == TREE_LIST && TREE_VALUE (*d) == binding->value)
2314 break;
2316 TREE_VALUE (*d) = newval;
2319 /* Compares the parameter-type-lists of ONE and TWO and
2320 returns false if they are different. If the DECLs are template
2321 functions, the return types and the template parameter lists are
2322 compared too (DR 565). */
2324 static bool
2325 matching_fn_p (tree one, tree two)
2327 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (one)),
2328 TYPE_ARG_TYPES (TREE_TYPE (two))))
2329 return false;
2331 if (TREE_CODE (one) == TEMPLATE_DECL
2332 && TREE_CODE (two) == TEMPLATE_DECL)
2334 /* Compare template parms. */
2335 if (!comp_template_parms (DECL_TEMPLATE_PARMS (one),
2336 DECL_TEMPLATE_PARMS (two)))
2337 return false;
2339 /* And return type. */
2340 if (!same_type_p (TREE_TYPE (TREE_TYPE (one)),
2341 TREE_TYPE (TREE_TYPE (two))))
2342 return false;
2345 return true;
2348 /* Push DECL into nonclass LEVEL BINDING or SLOT. OLD is the current
2349 binding value (possibly with anticipated builtins stripped).
2350 Diagnose conflicts and return updated decl. */
2352 static tree
2353 update_binding (cp_binding_level *level, cxx_binding *binding, tree *slot,
2354 tree old, tree decl, bool is_friend)
2356 tree to_val = decl;
2357 tree old_type = slot ? MAYBE_STAT_TYPE (*slot) : binding->type;
2358 tree to_type = old_type;
2360 gcc_assert (level->kind == sk_namespace ? !binding
2361 : level->kind != sk_class && !slot);
2362 if (old == error_mark_node)
2363 old = NULL_TREE;
2365 if (TREE_CODE (decl) == TYPE_DECL && DECL_ARTIFICIAL (decl))
2367 tree other = to_type;
2369 if (old && TREE_CODE (old) == TYPE_DECL && DECL_ARTIFICIAL (old))
2370 other = old;
2372 /* Pushing an artificial typedef. See if this matches either
2373 the type slot or the old value slot. */
2374 if (!other)
2376 else if (same_type_p (TREE_TYPE (other), TREE_TYPE (decl)))
2377 /* Two artificial decls to same type. Do nothing. */
2378 return other;
2379 else
2380 goto conflict;
2382 if (old)
2384 /* Slide decl into the type slot, keep old unaltered */
2385 to_type = decl;
2386 to_val = old;
2387 goto done;
2391 if (old && TREE_CODE (old) == TYPE_DECL && DECL_ARTIFICIAL (old))
2393 /* Slide old into the type slot. */
2394 to_type = old;
2395 old = NULL_TREE;
2398 if (DECL_DECLARES_FUNCTION_P (decl))
2400 if (!old)
2402 else if (OVL_P (old))
2404 for (ovl_iterator iter (old); iter; ++iter)
2406 tree fn = *iter;
2408 if (iter.using_p () && matching_fn_p (fn, decl))
2410 /* If a function declaration in namespace scope or
2411 block scope has the same name and the same
2412 parameter-type- list (8.3.5) as a function
2413 introduced by a using-declaration, and the
2414 declarations do not declare the same function,
2415 the program is ill-formed. [namespace.udecl]/14 */
2416 if (tree match = duplicate_decls (decl, fn, is_friend))
2417 return match;
2418 else
2419 /* FIXME: To preserve existing error behavior, we
2420 still push the decl. This might change. */
2421 diagnose_name_conflict (decl, fn);
2425 else
2426 goto conflict;
2428 if (to_type != old_type
2429 && warn_shadow
2430 && MAYBE_CLASS_TYPE_P (TREE_TYPE (to_type))
2431 && !(DECL_IN_SYSTEM_HEADER (decl)
2432 && DECL_IN_SYSTEM_HEADER (to_type)))
2433 warning (OPT_Wshadow, "%q#D hides constructor for %q#D",
2434 decl, to_type);
2436 to_val = ovl_insert (decl, old);
2438 else if (!old)
2440 else if (TREE_CODE (old) != TREE_CODE (decl))
2441 /* Different kinds of decls conflict. */
2442 goto conflict;
2443 else if (TREE_CODE (old) == TYPE_DECL)
2445 if (same_type_p (TREE_TYPE (old), TREE_TYPE (decl)))
2446 /* Two type decls to the same type. Do nothing. */
2447 return old;
2448 else
2449 goto conflict;
2451 else if (TREE_CODE (old) == NAMESPACE_DECL)
2453 /* Two maybe-aliased namespaces. If they're to the same target
2454 namespace, that's ok. */
2455 if (ORIGINAL_NAMESPACE (old) != ORIGINAL_NAMESPACE (decl))
2456 goto conflict;
2458 /* The new one must be an alias at this point. */
2459 gcc_assert (DECL_NAMESPACE_ALIAS (decl));
2460 return old;
2462 else if (TREE_CODE (old) == VAR_DECL)
2464 /* There can be two block-scope declarations of the same
2465 variable, so long as they are `extern' declarations. */
2466 if (!DECL_EXTERNAL (old) || !DECL_EXTERNAL (decl))
2467 goto conflict;
2468 else if (tree match = duplicate_decls (decl, old, false))
2469 return match;
2470 else
2471 goto conflict;
2473 else
2475 conflict:
2476 diagnose_name_conflict (decl, old);
2477 to_val = NULL_TREE;
2480 done:
2481 if (to_val)
2483 if (level->kind == sk_namespace || to_type == decl || to_val == decl)
2484 add_decl_to_level (level, decl);
2485 else
2487 gcc_checking_assert (binding->value && OVL_P (binding->value));
2488 update_local_overload (binding, to_val);
2491 if (slot)
2493 if (STAT_HACK_P (*slot))
2495 STAT_TYPE (*slot) = to_type;
2496 STAT_DECL (*slot) = to_val;
2498 else if (to_type)
2499 *slot = stat_hack (to_val, to_type);
2500 else
2501 *slot = to_val;
2503 else
2505 binding->type = to_type;
2506 binding->value = to_val;
2510 return decl;
2513 /* Table of identifiers to extern C declarations (or LISTS thereof). */
2515 static GTY(()) hash_table<named_decl_hash> *extern_c_decls;
2517 /* DECL has C linkage. If we have an existing instance, make sure the
2518 new one is compatible. Make sure it has the same exception
2519 specification [7.5, 7.6]. Add DECL to the map. */
2521 static void
2522 check_extern_c_conflict (tree decl)
2524 /* Ignore artificial or system header decls. */
2525 if (DECL_ARTIFICIAL (decl) || DECL_IN_SYSTEM_HEADER (decl))
2526 return;
2528 /* This only applies to decls at namespace scope. */
2529 if (!DECL_NAMESPACE_SCOPE_P (decl))
2530 return;
2532 if (!extern_c_decls)
2533 extern_c_decls = hash_table<named_decl_hash>::create_ggc (127);
2535 tree *slot = extern_c_decls
2536 ->find_slot_with_hash (DECL_NAME (decl),
2537 IDENTIFIER_HASH_VALUE (DECL_NAME (decl)), INSERT);
2538 if (tree old = *slot)
2540 if (TREE_CODE (old) == OVERLOAD)
2541 old = OVL_FUNCTION (old);
2543 int mismatch = 0;
2544 if (DECL_CONTEXT (old) == DECL_CONTEXT (decl))
2545 ; /* If they're in the same context, we'll have already complained
2546 about a (possible) mismatch, when inserting the decl. */
2547 else if (!decls_match (decl, old))
2548 mismatch = 1;
2549 else if (TREE_CODE (decl) == FUNCTION_DECL
2550 && !comp_except_specs (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (old)),
2551 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl)),
2552 ce_normal))
2553 mismatch = -1;
2554 else if (DECL_ASSEMBLER_NAME_SET_P (old))
2555 SET_DECL_ASSEMBLER_NAME (decl, DECL_ASSEMBLER_NAME (old));
2557 if (mismatch)
2559 auto_diagnostic_group d;
2560 pedwarn (input_location, 0,
2561 "conflicting C language linkage declaration %q#D", decl);
2562 inform (DECL_SOURCE_LOCATION (old),
2563 "previous declaration %q#D", old);
2564 if (mismatch < 0)
2565 inform (input_location,
2566 "due to different exception specifications");
2568 else
2570 if (old == *slot)
2571 /* The hash table expects OVERLOADS, so construct one with
2572 OLD as both the function and the chain. This allocate
2573 an excess OVERLOAD node, but it's rare to have multiple
2574 extern "C" decls of the same name. And we save
2575 complicating the hash table logic (which is used
2576 elsewhere). */
2577 *slot = ovl_make (old, old);
2579 slot = &OVL_CHAIN (*slot);
2581 /* Chain it on for c_linkage_binding's use. */
2582 *slot = tree_cons (NULL_TREE, decl, *slot);
2585 else
2586 *slot = decl;
2589 /* Returns a list of C-linkage decls with the name NAME. Used in
2590 c-family/c-pragma.c to implement redefine_extname pragma. */
2592 tree
2593 c_linkage_bindings (tree name)
2595 if (extern_c_decls)
2596 if (tree *slot = extern_c_decls
2597 ->find_slot_with_hash (name, IDENTIFIER_HASH_VALUE (name), NO_INSERT))
2599 tree result = *slot;
2600 if (TREE_CODE (result) == OVERLOAD)
2601 result = OVL_CHAIN (result);
2602 return result;
2605 return NULL_TREE;
2608 /* Subroutine of check_local_shadow. */
2610 static void
2611 inform_shadowed (tree shadowed)
2613 inform (DECL_SOURCE_LOCATION (shadowed),
2614 "shadowed declaration is here");
2617 /* DECL is being declared at a local scope. Emit suitable shadow
2618 warnings. */
2620 static void
2621 check_local_shadow (tree decl)
2623 /* Don't complain about the parms we push and then pop
2624 while tentatively parsing a function declarator. */
2625 if (TREE_CODE (decl) == PARM_DECL && !DECL_CONTEXT (decl))
2626 return;
2628 /* Inline decls shadow nothing. */
2629 if (DECL_FROM_INLINE (decl))
2630 return;
2632 /* External decls are something else. */
2633 if (DECL_EXTERNAL (decl))
2634 return;
2636 tree old = NULL_TREE;
2637 cp_binding_level *old_scope = NULL;
2638 if (cxx_binding *binding = outer_binding (DECL_NAME (decl), NULL, true))
2640 old = binding->value;
2641 old_scope = binding->scope;
2644 if (old
2645 && (TREE_CODE (old) == PARM_DECL
2646 || VAR_P (old)
2647 || (TREE_CODE (old) == TYPE_DECL
2648 && (!DECL_ARTIFICIAL (old)
2649 || TREE_CODE (decl) == TYPE_DECL)))
2650 && DECL_FUNCTION_SCOPE_P (old)
2651 && (!DECL_ARTIFICIAL (decl)
2652 || is_capture_proxy (decl)
2653 || DECL_IMPLICIT_TYPEDEF_P (decl)
2654 || (VAR_P (decl) && DECL_ANON_UNION_VAR_P (decl))))
2656 /* DECL shadows a local thing possibly of interest. */
2658 /* DR 2211: check that captures and parameters
2659 do not have the same name. */
2660 if (is_capture_proxy (decl))
2662 if (current_lambda_expr ()
2663 && DECL_CONTEXT (old) == lambda_function (current_lambda_expr ())
2664 && TREE_CODE (old) == PARM_DECL
2665 && DECL_NAME (decl) != this_identifier)
2667 error_at (DECL_SOURCE_LOCATION (old),
2668 "lambda parameter %qD "
2669 "previously declared as a capture", old);
2671 return;
2673 /* Don't complain if it's from an enclosing function. */
2674 else if (DECL_CONTEXT (old) == current_function_decl
2675 && TREE_CODE (decl) != PARM_DECL
2676 && TREE_CODE (old) == PARM_DECL)
2678 /* Go to where the parms should be and see if we find
2679 them there. */
2680 cp_binding_level *b = current_binding_level->level_chain;
2682 if (FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
2683 /* Skip the ctor/dtor cleanup level. */
2684 b = b->level_chain;
2686 /* ARM $8.3 */
2687 if (b->kind == sk_function_parms)
2689 error ("declaration of %q#D shadows a parameter", decl);
2690 return;
2694 /* The local structure or class can't use parameters of
2695 the containing function anyway. */
2696 if (DECL_CONTEXT (old) != current_function_decl)
2698 for (cp_binding_level *scope = current_binding_level;
2699 scope != old_scope; scope = scope->level_chain)
2700 if (scope->kind == sk_class
2701 && !LAMBDA_TYPE_P (scope->this_entity))
2702 return;
2704 /* Error if redeclaring a local declared in a
2705 init-statement or in the condition of an if or
2706 switch statement when the new declaration is in the
2707 outermost block of the controlled statement.
2708 Redeclaring a variable from a for or while condition is
2709 detected elsewhere. */
2710 else if (VAR_P (old)
2711 && old_scope == current_binding_level->level_chain
2712 && (old_scope->kind == sk_cond || old_scope->kind == sk_for))
2714 auto_diagnostic_group d;
2715 error ("redeclaration of %q#D", decl);
2716 inform (DECL_SOURCE_LOCATION (old),
2717 "%q#D previously declared here", old);
2718 return;
2720 /* C++11:
2721 3.3.3/3: The name declared in an exception-declaration (...)
2722 shall not be redeclared in the outermost block of the handler.
2723 3.3.3/2: A parameter name shall not be redeclared (...) in
2724 the outermost block of any handler associated with a
2725 function-try-block.
2726 3.4.1/15: The function parameter names shall not be redeclared
2727 in the exception-declaration nor in the outermost block of a
2728 handler for the function-try-block. */
2729 else if ((TREE_CODE (old) == VAR_DECL
2730 && old_scope == current_binding_level->level_chain
2731 && old_scope->kind == sk_catch)
2732 || (TREE_CODE (old) == PARM_DECL
2733 && (current_binding_level->kind == sk_catch
2734 || current_binding_level->level_chain->kind == sk_catch)
2735 && in_function_try_handler))
2737 auto_diagnostic_group d;
2738 if (permerror (input_location, "redeclaration of %q#D", decl))
2739 inform (DECL_SOURCE_LOCATION (old),
2740 "%q#D previously declared here", old);
2741 return;
2744 /* If '-Wshadow=compatible-local' is specified without other
2745 -Wshadow= flags, we will warn only when the type of the
2746 shadowing variable (DECL) can be converted to that of the
2747 shadowed parameter (OLD_LOCAL). The reason why we only check
2748 if DECL's type can be converted to OLD_LOCAL's type (but not the
2749 other way around) is because when users accidentally shadow a
2750 parameter, more than often they would use the variable
2751 thinking (mistakenly) it's still the parameter. It would be
2752 rare that users would use the variable in the place that
2753 expects the parameter but thinking it's a new decl. */
2755 enum opt_code warning_code;
2756 if (warn_shadow)
2757 warning_code = OPT_Wshadow;
2758 else if (warn_shadow_local)
2759 warning_code = OPT_Wshadow_local;
2760 else if (warn_shadow_compatible_local
2761 && (same_type_p (TREE_TYPE (old), TREE_TYPE (decl))
2762 || (!dependent_type_p (TREE_TYPE (decl))
2763 && !dependent_type_p (TREE_TYPE (old))
2764 && can_convert (TREE_TYPE (old), TREE_TYPE (decl),
2765 tf_none))))
2766 warning_code = OPT_Wshadow_compatible_local;
2767 else
2768 return;
2770 const char *msg;
2771 if (TREE_CODE (old) == PARM_DECL)
2772 msg = "declaration of %q#D shadows a parameter";
2773 else if (is_capture_proxy (old))
2774 msg = "declaration of %qD shadows a lambda capture";
2775 else
2776 msg = "declaration of %qD shadows a previous local";
2778 auto_diagnostic_group d;
2779 if (warning_at (input_location, warning_code, msg, decl))
2780 inform_shadowed (old);
2781 return;
2784 if (!warn_shadow)
2785 return;
2787 /* Don't warn for artificial things that are not implicit typedefs. */
2788 if (DECL_ARTIFICIAL (decl) && !DECL_IMPLICIT_TYPEDEF_P (decl))
2789 return;
2791 if (nonlambda_method_basetype ())
2792 if (tree member = lookup_member (current_nonlambda_class_type (),
2793 DECL_NAME (decl), /*protect=*/0,
2794 /*want_type=*/false, tf_warning_or_error))
2796 member = MAYBE_BASELINK_FUNCTIONS (member);
2798 /* Warn if a variable shadows a non-function, or the variable
2799 is a function or a pointer-to-function. */
2800 if (!OVL_P (member)
2801 || TREE_CODE (decl) == FUNCTION_DECL
2802 || TYPE_PTRFN_P (TREE_TYPE (decl))
2803 || TYPE_PTRMEMFUNC_P (TREE_TYPE (decl)))
2805 auto_diagnostic_group d;
2806 if (warning_at (input_location, OPT_Wshadow,
2807 "declaration of %qD shadows a member of %qT",
2808 decl, current_nonlambda_class_type ())
2809 && DECL_P (member))
2810 inform_shadowed (member);
2812 return;
2815 /* Now look for a namespace shadow. */
2816 old = find_namespace_value (current_namespace, DECL_NAME (decl));
2817 if (old
2818 && (VAR_P (old)
2819 || (TREE_CODE (old) == TYPE_DECL
2820 && (!DECL_ARTIFICIAL (old)
2821 || TREE_CODE (decl) == TYPE_DECL)))
2822 && !instantiating_current_function_p ())
2823 /* XXX shadow warnings in outer-more namespaces */
2825 auto_diagnostic_group d;
2826 if (warning_at (input_location, OPT_Wshadow,
2827 "declaration of %qD shadows a global declaration",
2828 decl))
2829 inform_shadowed (old);
2830 return;
2833 return;
2836 /* DECL is being pushed inside function CTX. Set its context, if
2837 needed. */
2839 static void
2840 set_decl_context_in_fn (tree ctx, tree decl)
2842 if (!DECL_CONTEXT (decl)
2843 /* A local declaration for a function doesn't constitute
2844 nesting. */
2845 && TREE_CODE (decl) != FUNCTION_DECL
2846 /* A local declaration for an `extern' variable is in the
2847 scope of the current namespace, not the current
2848 function. */
2849 && !(VAR_P (decl) && DECL_EXTERNAL (decl))
2850 /* When parsing the parameter list of a function declarator,
2851 don't set DECL_CONTEXT to an enclosing function. When we
2852 push the PARM_DECLs in order to process the function body,
2853 current_binding_level->this_entity will be set. */
2854 && !(TREE_CODE (decl) == PARM_DECL
2855 && current_binding_level->kind == sk_function_parms
2856 && current_binding_level->this_entity == NULL))
2857 DECL_CONTEXT (decl) = ctx;
2859 /* If this is the declaration for a namespace-scope function,
2860 but the declaration itself is in a local scope, mark the
2861 declaration. */
2862 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (decl))
2863 DECL_LOCAL_FUNCTION_P (decl) = 1;
2866 /* DECL is a local-scope decl with linkage. SHADOWED is true if the
2867 name is already bound at the current level.
2869 [basic.link] If there is a visible declaration of an entity with
2870 linkage having the same name and type, ignoring entities declared
2871 outside the innermost enclosing namespace scope, the block scope
2872 declaration declares that same entity and receives the linkage of
2873 the previous declaration.
2875 Also, make sure that this decl matches any existing external decl
2876 in the enclosing namespace. */
2878 static void
2879 set_local_extern_decl_linkage (tree decl, bool shadowed)
2881 tree ns_value = decl; /* Unique marker. */
2883 if (!shadowed)
2885 tree loc_value = innermost_non_namespace_value (DECL_NAME (decl));
2886 if (!loc_value)
2888 ns_value
2889 = find_namespace_value (current_namespace, DECL_NAME (decl));
2890 loc_value = ns_value;
2892 if (loc_value == error_mark_node
2893 /* An ambiguous lookup. */
2894 || (loc_value && TREE_CODE (loc_value) == TREE_LIST))
2895 loc_value = NULL_TREE;
2897 for (ovl_iterator iter (loc_value); iter; ++iter)
2898 if (!iter.hidden_p ()
2899 && (TREE_STATIC (*iter) || DECL_EXTERNAL (*iter))
2900 && decls_match (*iter, decl))
2902 /* The standard only says that the local extern inherits
2903 linkage from the previous decl; in particular, default
2904 args are not shared. Add the decl into a hash table to
2905 make sure only the previous decl in this case is seen
2906 by the middle end. */
2907 struct cxx_int_tree_map *h;
2909 /* We inherit the outer decl's linkage. But we're a
2910 different decl. */
2911 TREE_PUBLIC (decl) = TREE_PUBLIC (*iter);
2913 if (cp_function_chain->extern_decl_map == NULL)
2914 cp_function_chain->extern_decl_map
2915 = hash_table<cxx_int_tree_map_hasher>::create_ggc (20);
2917 h = ggc_alloc<cxx_int_tree_map> ();
2918 h->uid = DECL_UID (decl);
2919 h->to = *iter;
2920 cxx_int_tree_map **loc = cp_function_chain->extern_decl_map
2921 ->find_slot (h, INSERT);
2922 *loc = h;
2923 break;
2927 if (TREE_PUBLIC (decl))
2929 /* DECL is externally visible. Make sure it matches a matching
2930 decl in the namespace scope. We only really need to check
2931 this when inserting the decl, not when we find an existing
2932 match in the current scope. However, in practice we're
2933 going to be inserting a new decl in the majority of cases --
2934 who writes multiple extern decls for the same thing in the
2935 same local scope? Doing it here often avoids a duplicate
2936 namespace lookup. */
2938 /* Avoid repeating a lookup. */
2939 if (ns_value == decl)
2940 ns_value = find_namespace_value (current_namespace, DECL_NAME (decl));
2942 if (ns_value == error_mark_node
2943 || (ns_value && TREE_CODE (ns_value) == TREE_LIST))
2944 ns_value = NULL_TREE;
2946 for (ovl_iterator iter (ns_value); iter; ++iter)
2948 tree other = *iter;
2950 if (!(TREE_PUBLIC (other) || DECL_EXTERNAL (other)))
2951 ; /* Not externally visible. */
2952 else if (DECL_EXTERN_C_P (decl) && DECL_EXTERN_C_P (other))
2953 ; /* Both are extern "C", we'll check via that mechanism. */
2954 else if (TREE_CODE (other) != TREE_CODE (decl)
2955 || ((VAR_P (decl) || matching_fn_p (other, decl))
2956 && !comptypes (TREE_TYPE (decl), TREE_TYPE (other),
2957 COMPARE_REDECLARATION)))
2959 auto_diagnostic_group d;
2960 if (permerror (DECL_SOURCE_LOCATION (decl),
2961 "local external declaration %q#D", decl))
2962 inform (DECL_SOURCE_LOCATION (other),
2963 "does not match previous declaration %q#D", other);
2964 break;
2970 /* Record DECL as belonging to the current lexical scope. Check for
2971 errors (such as an incompatible declaration for the same name
2972 already seen in the same scope). IS_FRIEND is true if DECL is
2973 declared as a friend.
2975 Returns either DECL or an old decl for the same name. If an old
2976 decl is returned, it may have been smashed to agree with what DECL
2977 says. */
2979 static tree
2980 do_pushdecl (tree decl, bool is_friend)
2982 if (decl == error_mark_node)
2983 return error_mark_node;
2985 if (!DECL_TEMPLATE_PARM_P (decl) && current_function_decl)
2986 set_decl_context_in_fn (current_function_decl, decl);
2988 /* The binding level we will be pushing into. During local class
2989 pushing, we want to push to the containing scope. */
2990 cp_binding_level *level = current_binding_level;
2991 while (level->kind == sk_class)
2992 level = level->level_chain;
2994 /* An anonymous namespace has a NULL DECL_NAME, but we still want to
2995 insert it. Other NULL-named decls, not so much. */
2996 tree name = DECL_NAME (decl);
2997 if (name || TREE_CODE (decl) == NAMESPACE_DECL)
2999 cxx_binding *binding = NULL; /* Local scope binding. */
3000 tree ns = NULL_TREE; /* Searched namespace. */
3001 tree *slot = NULL; /* Binding slot in namespace. */
3002 tree old = NULL_TREE;
3004 if (level->kind == sk_namespace)
3006 /* We look in the decl's namespace for an existing
3007 declaration, even though we push into the current
3008 namespace. */
3009 ns = (DECL_NAMESPACE_SCOPE_P (decl)
3010 ? CP_DECL_CONTEXT (decl) : current_namespace);
3011 /* Create the binding, if this is current namespace, because
3012 that's where we'll be pushing anyway. */
3013 slot = find_namespace_slot (ns, name, ns == current_namespace);
3014 if (slot)
3015 old = MAYBE_STAT_DECL (*slot);
3017 else
3019 binding = find_local_binding (level, name);
3020 if (binding)
3021 old = binding->value;
3024 if (current_function_decl && VAR_OR_FUNCTION_DECL_P (decl)
3025 && DECL_EXTERNAL (decl))
3026 set_local_extern_decl_linkage (decl, old != NULL_TREE);
3028 if (old == error_mark_node)
3029 old = NULL_TREE;
3031 for (ovl_iterator iter (old); iter; ++iter)
3032 if (iter.using_p ())
3033 ; /* Ignore using decls here. */
3034 else if (tree match = duplicate_decls (decl, *iter, is_friend))
3036 if (match == error_mark_node)
3038 else if (TREE_CODE (match) == TYPE_DECL)
3039 /* The IDENTIFIER will have the type referring to the
3040 now-smashed TYPE_DECL, because ...? Reset it. */
3041 SET_IDENTIFIER_TYPE_VALUE (name, TREE_TYPE (match));
3042 else if (iter.hidden_p () && !DECL_HIDDEN_P (match))
3044 /* Unhiding a previously hidden decl. */
3045 tree head = iter.reveal_node (old);
3046 if (head != old)
3048 if (!ns)
3050 update_local_overload (binding, head);
3051 binding->value = head;
3053 else if (STAT_HACK_P (*slot))
3054 STAT_DECL (*slot) = head;
3055 else
3056 *slot = head;
3058 if (DECL_EXTERN_C_P (match))
3059 /* We need to check and register the decl now. */
3060 check_extern_c_conflict (match);
3062 return match;
3065 /* We are pushing a new decl. */
3067 /* Skip a hidden builtin we failed to match already. There can
3068 only be one. */
3069 if (old && anticipated_builtin_p (old))
3070 old = OVL_CHAIN (old);
3072 check_template_shadow (decl);
3074 if (DECL_DECLARES_FUNCTION_P (decl))
3076 check_default_args (decl);
3078 if (is_friend)
3080 if (level->kind != sk_namespace)
3082 /* In a local class, a friend function declaration must
3083 find a matching decl in the innermost non-class scope.
3084 [class.friend/11] */
3085 error ("friend declaration %qD in local class without "
3086 "prior local declaration", decl);
3087 /* Don't attempt to push it. */
3088 return error_mark_node;
3090 /* Hide it from ordinary lookup. */
3091 DECL_ANTICIPATED (decl) = DECL_HIDDEN_FRIEND_P (decl) = true;
3095 if (level->kind != sk_namespace)
3097 check_local_shadow (decl);
3099 if (TREE_CODE (decl) == NAMESPACE_DECL)
3100 /* A local namespace alias. */
3101 set_identifier_type_value (name, NULL_TREE);
3103 if (!binding)
3104 binding = create_local_binding (level, name);
3106 else if (!slot)
3108 ns = current_namespace;
3109 slot = find_namespace_slot (ns, name, true);
3110 /* Update OLD to reflect the namespace we're going to be
3111 pushing into. */
3112 old = MAYBE_STAT_DECL (*slot);
3115 old = update_binding (level, binding, slot, old, decl, is_friend);
3117 if (old != decl)
3118 /* An existing decl matched, use it. */
3119 decl = old;
3120 else if (TREE_CODE (decl) == TYPE_DECL)
3122 tree type = TREE_TYPE (decl);
3124 if (type != error_mark_node)
3126 if (TYPE_NAME (type) != decl)
3127 set_underlying_type (decl);
3129 if (!ns)
3130 set_identifier_type_value_with_scope (name, decl, level);
3131 else
3132 SET_IDENTIFIER_TYPE_VALUE (name, global_type_node);
3135 /* If this is a locally defined typedef in a function that
3136 is not a template instantation, record it to implement
3137 -Wunused-local-typedefs. */
3138 if (!instantiating_current_function_p ())
3139 record_locally_defined_typedef (decl);
3141 else if (VAR_P (decl))
3142 maybe_register_incomplete_var (decl);
3144 if ((VAR_P (decl) || TREE_CODE (decl) == FUNCTION_DECL)
3145 && DECL_EXTERN_C_P (decl))
3146 check_extern_c_conflict (decl);
3148 else
3149 add_decl_to_level (level, decl);
3151 return decl;
3154 /* Record a decl-node X as belonging to the current lexical scope.
3155 It's a friend if IS_FRIEND is true -- which affects exactly where
3156 we push it. */
3158 tree
3159 pushdecl (tree x, bool is_friend)
3161 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3162 tree ret = do_pushdecl (x, is_friend);
3163 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3164 return ret;
3167 /* Enter DECL into the symbol table, if that's appropriate. Returns
3168 DECL, or a modified version thereof. */
3170 tree
3171 maybe_push_decl (tree decl)
3173 tree type = TREE_TYPE (decl);
3175 /* Add this decl to the current binding level, but not if it comes
3176 from another scope, e.g. a static member variable. TEM may equal
3177 DECL or it may be a previous decl of the same name. */
3178 if (decl == error_mark_node
3179 || (TREE_CODE (decl) != PARM_DECL
3180 && DECL_CONTEXT (decl) != NULL_TREE
3181 /* Definitions of namespace members outside their namespace are
3182 possible. */
3183 && !DECL_NAMESPACE_SCOPE_P (decl))
3184 || (TREE_CODE (decl) == TEMPLATE_DECL && !namespace_bindings_p ())
3185 || type == unknown_type_node
3186 /* The declaration of a template specialization does not affect
3187 the functions available for overload resolution, so we do not
3188 call pushdecl. */
3189 || (TREE_CODE (decl) == FUNCTION_DECL
3190 && DECL_TEMPLATE_SPECIALIZATION (decl)))
3191 return decl;
3192 else
3193 return pushdecl (decl);
3196 /* Bind DECL to ID in the current_binding_level, assumed to be a local
3197 binding level. If IS_USING is true, DECL got here through a
3198 using-declaration. */
3200 static void
3201 push_local_binding (tree id, tree decl, bool is_using)
3203 /* Skip over any local classes. This makes sense if we call
3204 push_local_binding with a friend decl of a local class. */
3205 cp_binding_level *b = innermost_nonclass_level ();
3207 gcc_assert (b->kind != sk_namespace);
3208 if (find_local_binding (b, id))
3210 /* Supplement the existing binding. */
3211 if (!supplement_binding (IDENTIFIER_BINDING (id), decl))
3212 /* It didn't work. Something else must be bound at this
3213 level. Do not add DECL to the list of things to pop
3214 later. */
3215 return;
3217 else
3218 /* Create a new binding. */
3219 push_binding (id, decl, b);
3221 if (TREE_CODE (decl) == OVERLOAD || is_using)
3222 /* We must put the OVERLOAD or using into a TREE_LIST since we
3223 cannot use the decl's chain itself. */
3224 decl = build_tree_list (NULL_TREE, decl);
3226 /* And put DECL on the list of things declared by the current
3227 binding level. */
3228 add_decl_to_level (b, decl);
3232 /* true means unconditionally make a BLOCK for the next level pushed. */
3234 static bool keep_next_level_flag;
3236 static int binding_depth = 0;
3238 static void
3239 indent (int depth)
3241 int i;
3243 for (i = 0; i < depth * 2; i++)
3244 putc (' ', stderr);
3247 /* Return a string describing the kind of SCOPE we have. */
3248 static const char *
3249 cp_binding_level_descriptor (cp_binding_level *scope)
3251 /* The order of this table must match the "scope_kind"
3252 enumerators. */
3253 static const char* scope_kind_names[] = {
3254 "block-scope",
3255 "cleanup-scope",
3256 "try-scope",
3257 "catch-scope",
3258 "for-scope",
3259 "function-parameter-scope",
3260 "class-scope",
3261 "namespace-scope",
3262 "template-parameter-scope",
3263 "template-explicit-spec-scope"
3265 const scope_kind kind = scope->explicit_spec_p
3266 ? sk_template_spec : scope->kind;
3268 return scope_kind_names[kind];
3271 /* Output a debugging information about SCOPE when performing
3272 ACTION at LINE. */
3273 static void
3274 cp_binding_level_debug (cp_binding_level *scope, int line, const char *action)
3276 const char *desc = cp_binding_level_descriptor (scope);
3277 if (scope->this_entity)
3278 verbatim ("%s %<%s(%E)%> %p %d\n", action, desc,
3279 scope->this_entity, (void *) scope, line);
3280 else
3281 verbatim ("%s %s %p %d\n", action, desc, (void *) scope, line);
3284 /* Return the estimated initial size of the hashtable of a NAMESPACE
3285 scope. */
3287 static inline size_t
3288 namespace_scope_ht_size (tree ns)
3290 tree name = DECL_NAME (ns);
3292 return name == std_identifier
3293 ? NAMESPACE_STD_HT_SIZE
3294 : (name == global_identifier
3295 ? GLOBAL_SCOPE_HT_SIZE
3296 : NAMESPACE_ORDINARY_HT_SIZE);
3299 /* A chain of binding_level structures awaiting reuse. */
3301 static GTY((deletable)) cp_binding_level *free_binding_level;
3303 /* Insert SCOPE as the innermost binding level. */
3305 void
3306 push_binding_level (cp_binding_level *scope)
3308 /* Add it to the front of currently active scopes stack. */
3309 scope->level_chain = current_binding_level;
3310 current_binding_level = scope;
3311 keep_next_level_flag = false;
3313 if (ENABLE_SCOPE_CHECKING)
3315 scope->binding_depth = binding_depth;
3316 indent (binding_depth);
3317 cp_binding_level_debug (scope, LOCATION_LINE (input_location),
3318 "push");
3319 binding_depth++;
3323 /* Create a new KIND scope and make it the top of the active scopes stack.
3324 ENTITY is the scope of the associated C++ entity (namespace, class,
3325 function, C++0x enumeration); it is NULL otherwise. */
3327 cp_binding_level *
3328 begin_scope (scope_kind kind, tree entity)
3330 cp_binding_level *scope;
3332 /* Reuse or create a struct for this binding level. */
3333 if (!ENABLE_SCOPE_CHECKING && free_binding_level)
3335 scope = free_binding_level;
3336 free_binding_level = scope->level_chain;
3337 memset (scope, 0, sizeof (cp_binding_level));
3339 else
3340 scope = ggc_cleared_alloc<cp_binding_level> ();
3342 scope->this_entity = entity;
3343 scope->more_cleanups_ok = true;
3344 switch (kind)
3346 case sk_cleanup:
3347 scope->keep = true;
3348 break;
3350 case sk_template_spec:
3351 scope->explicit_spec_p = true;
3352 kind = sk_template_parms;
3353 /* Fall through. */
3354 case sk_template_parms:
3355 case sk_block:
3356 case sk_try:
3357 case sk_catch:
3358 case sk_for:
3359 case sk_cond:
3360 case sk_class:
3361 case sk_scoped_enum:
3362 case sk_function_parms:
3363 case sk_transaction:
3364 case sk_omp:
3365 scope->keep = keep_next_level_flag;
3366 break;
3368 case sk_namespace:
3369 NAMESPACE_LEVEL (entity) = scope;
3370 break;
3372 default:
3373 /* Should not happen. */
3374 gcc_unreachable ();
3375 break;
3377 scope->kind = kind;
3379 push_binding_level (scope);
3381 return scope;
3384 /* We're about to leave current scope. Pop the top of the stack of
3385 currently active scopes. Return the enclosing scope, now active. */
3387 cp_binding_level *
3388 leave_scope (void)
3390 cp_binding_level *scope = current_binding_level;
3392 if (scope->kind == sk_namespace && class_binding_level)
3393 current_binding_level = class_binding_level;
3395 /* We cannot leave a scope, if there are none left. */
3396 if (NAMESPACE_LEVEL (global_namespace))
3397 gcc_assert (!global_scope_p (scope));
3399 if (ENABLE_SCOPE_CHECKING)
3401 indent (--binding_depth);
3402 cp_binding_level_debug (scope, LOCATION_LINE (input_location),
3403 "leave");
3406 /* Move one nesting level up. */
3407 current_binding_level = scope->level_chain;
3409 /* Namespace-scopes are left most probably temporarily, not
3410 completely; they can be reopened later, e.g. in namespace-extension
3411 or any name binding activity that requires us to resume a
3412 namespace. For classes, we cache some binding levels. For other
3413 scopes, we just make the structure available for reuse. */
3414 if (scope->kind != sk_namespace
3415 && scope->kind != sk_class)
3417 scope->level_chain = free_binding_level;
3418 gcc_assert (!ENABLE_SCOPE_CHECKING
3419 || scope->binding_depth == binding_depth);
3420 free_binding_level = scope;
3423 if (scope->kind == sk_class)
3425 /* Reset DEFINING_CLASS_P to allow for reuse of a
3426 class-defining scope in a non-defining context. */
3427 scope->defining_class_p = 0;
3429 /* Find the innermost enclosing class scope, and reset
3430 CLASS_BINDING_LEVEL appropriately. */
3431 class_binding_level = NULL;
3432 for (scope = current_binding_level; scope; scope = scope->level_chain)
3433 if (scope->kind == sk_class)
3435 class_binding_level = scope;
3436 break;
3440 return current_binding_level;
3443 static void
3444 resume_scope (cp_binding_level* b)
3446 /* Resuming binding levels is meant only for namespaces,
3447 and those cannot nest into classes. */
3448 gcc_assert (!class_binding_level);
3449 /* Also, resuming a non-directly nested namespace is a no-no. */
3450 gcc_assert (b->level_chain == current_binding_level);
3451 current_binding_level = b;
3452 if (ENABLE_SCOPE_CHECKING)
3454 b->binding_depth = binding_depth;
3455 indent (binding_depth);
3456 cp_binding_level_debug (b, LOCATION_LINE (input_location), "resume");
3457 binding_depth++;
3461 /* Return the innermost binding level that is not for a class scope. */
3463 static cp_binding_level *
3464 innermost_nonclass_level (void)
3466 cp_binding_level *b;
3468 b = current_binding_level;
3469 while (b->kind == sk_class)
3470 b = b->level_chain;
3472 return b;
3475 /* We're defining an object of type TYPE. If it needs a cleanup, but
3476 we're not allowed to add any more objects with cleanups to the current
3477 scope, create a new binding level. */
3479 void
3480 maybe_push_cleanup_level (tree type)
3482 if (type != error_mark_node
3483 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
3484 && current_binding_level->more_cleanups_ok == 0)
3486 begin_scope (sk_cleanup, NULL);
3487 current_binding_level->statement_list = push_stmt_list ();
3491 /* Return true if we are in the global binding level. */
3493 bool
3494 global_bindings_p (void)
3496 return global_scope_p (current_binding_level);
3499 /* True if we are currently in a toplevel binding level. This
3500 means either the global binding level or a namespace in a toplevel
3501 binding level. Since there are no non-toplevel namespace levels,
3502 this really means any namespace or template parameter level. We
3503 also include a class whose context is toplevel. */
3505 bool
3506 toplevel_bindings_p (void)
3508 cp_binding_level *b = innermost_nonclass_level ();
3510 return b->kind == sk_namespace || b->kind == sk_template_parms;
3513 /* True if this is a namespace scope, or if we are defining a class
3514 which is itself at namespace scope, or whose enclosing class is
3515 such a class, etc. */
3517 bool
3518 namespace_bindings_p (void)
3520 cp_binding_level *b = innermost_nonclass_level ();
3522 return b->kind == sk_namespace;
3525 /* True if the innermost non-class scope is a block scope. */
3527 bool
3528 local_bindings_p (void)
3530 cp_binding_level *b = innermost_nonclass_level ();
3531 return b->kind < sk_function_parms || b->kind == sk_omp;
3534 /* True if the current level needs to have a BLOCK made. */
3536 bool
3537 kept_level_p (void)
3539 return (current_binding_level->blocks != NULL_TREE
3540 || current_binding_level->keep
3541 || current_binding_level->kind == sk_cleanup
3542 || current_binding_level->names != NULL_TREE
3543 || current_binding_level->using_directives);
3546 /* Returns the kind of the innermost scope. */
3548 scope_kind
3549 innermost_scope_kind (void)
3551 return current_binding_level->kind;
3554 /* Returns true if this scope was created to store template parameters. */
3556 bool
3557 template_parm_scope_p (void)
3559 return innermost_scope_kind () == sk_template_parms;
3562 /* If KEEP is true, make a BLOCK node for the next binding level,
3563 unconditionally. Otherwise, use the normal logic to decide whether
3564 or not to create a BLOCK. */
3566 void
3567 keep_next_level (bool keep)
3569 keep_next_level_flag = keep;
3572 /* Return the list of declarations of the current local scope. */
3574 tree
3575 get_local_decls (void)
3577 gcc_assert (current_binding_level->kind != sk_namespace
3578 && current_binding_level->kind != sk_class);
3579 return current_binding_level->names;
3582 /* Return how many function prototypes we are currently nested inside. */
3585 function_parm_depth (void)
3587 int level = 0;
3588 cp_binding_level *b;
3590 for (b = current_binding_level;
3591 b->kind == sk_function_parms;
3592 b = b->level_chain)
3593 ++level;
3595 return level;
3598 /* For debugging. */
3599 static int no_print_functions = 0;
3600 static int no_print_builtins = 0;
3602 static void
3603 print_binding_level (cp_binding_level* lvl)
3605 tree t;
3606 int i = 0, len;
3607 fprintf (stderr, " blocks=%p", (void *) lvl->blocks);
3608 if (lvl->more_cleanups_ok)
3609 fprintf (stderr, " more-cleanups-ok");
3610 if (lvl->have_cleanups)
3611 fprintf (stderr, " have-cleanups");
3612 fprintf (stderr, "\n");
3613 if (lvl->names)
3615 fprintf (stderr, " names:\t");
3616 /* We can probably fit 3 names to a line? */
3617 for (t = lvl->names; t; t = TREE_CHAIN (t))
3619 if (no_print_functions && (TREE_CODE (t) == FUNCTION_DECL))
3620 continue;
3621 if (no_print_builtins
3622 && (TREE_CODE (t) == TYPE_DECL)
3623 && DECL_IS_BUILTIN (t))
3624 continue;
3626 /* Function decls tend to have longer names. */
3627 if (TREE_CODE (t) == FUNCTION_DECL)
3628 len = 3;
3629 else
3630 len = 2;
3631 i += len;
3632 if (i > 6)
3634 fprintf (stderr, "\n\t");
3635 i = len;
3637 print_node_brief (stderr, "", t, 0);
3638 if (t == error_mark_node)
3639 break;
3641 if (i)
3642 fprintf (stderr, "\n");
3644 if (vec_safe_length (lvl->class_shadowed))
3646 size_t i;
3647 cp_class_binding *b;
3648 fprintf (stderr, " class-shadowed:");
3649 FOR_EACH_VEC_ELT (*lvl->class_shadowed, i, b)
3650 fprintf (stderr, " %s ", IDENTIFIER_POINTER (b->identifier));
3651 fprintf (stderr, "\n");
3653 if (lvl->type_shadowed)
3655 fprintf (stderr, " type-shadowed:");
3656 for (t = lvl->type_shadowed; t; t = TREE_CHAIN (t))
3658 fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
3660 fprintf (stderr, "\n");
3664 DEBUG_FUNCTION void
3665 debug (cp_binding_level &ref)
3667 print_binding_level (&ref);
3670 DEBUG_FUNCTION void
3671 debug (cp_binding_level *ptr)
3673 if (ptr)
3674 debug (*ptr);
3675 else
3676 fprintf (stderr, "<nil>\n");
3680 static void
3681 print_other_binding_stack (cp_binding_level *stack)
3683 cp_binding_level *level;
3684 for (level = stack; !global_scope_p (level); level = level->level_chain)
3686 fprintf (stderr, "binding level %p\n", (void *) level);
3687 print_binding_level (level);
3691 void
3692 print_binding_stack (void)
3694 cp_binding_level *b;
3695 fprintf (stderr, "current_binding_level=%p\n"
3696 "class_binding_level=%p\n"
3697 "NAMESPACE_LEVEL (global_namespace)=%p\n",
3698 (void *) current_binding_level, (void *) class_binding_level,
3699 (void *) NAMESPACE_LEVEL (global_namespace));
3700 if (class_binding_level)
3702 for (b = class_binding_level; b; b = b->level_chain)
3703 if (b == current_binding_level)
3704 break;
3705 if (b)
3706 b = class_binding_level;
3707 else
3708 b = current_binding_level;
3710 else
3711 b = current_binding_level;
3712 print_other_binding_stack (b);
3713 fprintf (stderr, "global:\n");
3714 print_binding_level (NAMESPACE_LEVEL (global_namespace));
3717 /* Return the type associated with ID. */
3719 static tree
3720 identifier_type_value_1 (tree id)
3722 /* There is no type with that name, anywhere. */
3723 if (REAL_IDENTIFIER_TYPE_VALUE (id) == NULL_TREE)
3724 return NULL_TREE;
3725 /* This is not the type marker, but the real thing. */
3726 if (REAL_IDENTIFIER_TYPE_VALUE (id) != global_type_node)
3727 return REAL_IDENTIFIER_TYPE_VALUE (id);
3728 /* Have to search for it. It must be on the global level, now.
3729 Ask lookup_name not to return non-types. */
3730 id = lookup_name_real (id, 2, 1, /*block_p=*/true, 0, 0);
3731 if (id)
3732 return TREE_TYPE (id);
3733 return NULL_TREE;
3736 /* Wrapper for identifier_type_value_1. */
3738 tree
3739 identifier_type_value (tree id)
3741 tree ret;
3742 timevar_start (TV_NAME_LOOKUP);
3743 ret = identifier_type_value_1 (id);
3744 timevar_stop (TV_NAME_LOOKUP);
3745 return ret;
3748 /* Push a definition of struct, union or enum tag named ID. into
3749 binding_level B. DECL is a TYPE_DECL for the type. We assume that
3750 the tag ID is not already defined. */
3752 static void
3753 set_identifier_type_value_with_scope (tree id, tree decl, cp_binding_level *b)
3755 tree type;
3757 if (b->kind != sk_namespace)
3759 /* Shadow the marker, not the real thing, so that the marker
3760 gets restored later. */
3761 tree old_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
3762 b->type_shadowed
3763 = tree_cons (id, old_type_value, b->type_shadowed);
3764 type = decl ? TREE_TYPE (decl) : NULL_TREE;
3765 TREE_TYPE (b->type_shadowed) = type;
3767 else
3769 tree *slot = find_namespace_slot (current_namespace, id, true);
3770 gcc_assert (decl);
3771 update_binding (b, NULL, slot, MAYBE_STAT_DECL (*slot), decl, false);
3773 /* Store marker instead of real type. */
3774 type = global_type_node;
3776 SET_IDENTIFIER_TYPE_VALUE (id, type);
3779 /* As set_identifier_type_value_with_scope, but using
3780 current_binding_level. */
3782 void
3783 set_identifier_type_value (tree id, tree decl)
3785 set_identifier_type_value_with_scope (id, decl, current_binding_level);
3788 /* Return the name for the constructor (or destructor) for the
3789 specified class. */
3791 tree
3792 constructor_name (tree type)
3794 tree decl = TYPE_NAME (TYPE_MAIN_VARIANT (type));
3796 return decl ? DECL_NAME (decl) : NULL_TREE;
3799 /* Returns TRUE if NAME is the name for the constructor for TYPE,
3800 which must be a class type. */
3802 bool
3803 constructor_name_p (tree name, tree type)
3805 gcc_assert (MAYBE_CLASS_TYPE_P (type));
3807 /* These don't have names. */
3808 if (TREE_CODE (type) == DECLTYPE_TYPE
3809 || TREE_CODE (type) == TYPEOF_TYPE)
3810 return false;
3812 if (name && name == constructor_name (type))
3813 return true;
3815 return false;
3818 /* Counter used to create anonymous type names. */
3820 static GTY(()) int anon_cnt;
3822 /* Return an IDENTIFIER which can be used as a name for
3823 unnamed structs and unions. */
3825 tree
3826 make_anon_name (void)
3828 char buf[32];
3830 sprintf (buf, anon_aggrname_format (), anon_cnt++);
3831 return get_identifier (buf);
3834 /* This code is practically identical to that for creating
3835 anonymous names, but is just used for lambdas instead. This isn't really
3836 necessary, but it's convenient to avoid treating lambdas like other
3837 unnamed types. */
3839 static GTY(()) int lambda_cnt = 0;
3841 tree
3842 make_lambda_name (void)
3844 char buf[32];
3846 sprintf (buf, LAMBDANAME_FORMAT, lambda_cnt++);
3847 return get_identifier (buf);
3850 /* Insert another USING_DECL into the current binding level, returning
3851 this declaration. If this is a redeclaration, do nothing, and
3852 return NULL_TREE if this not in namespace scope (in namespace
3853 scope, a using decl might extend any previous bindings). */
3855 static tree
3856 push_using_decl_1 (tree scope, tree name)
3858 tree decl;
3860 gcc_assert (TREE_CODE (scope) == NAMESPACE_DECL);
3861 gcc_assert (identifier_p (name));
3862 for (decl = current_binding_level->usings; decl; decl = DECL_CHAIN (decl))
3863 if (USING_DECL_SCOPE (decl) == scope && DECL_NAME (decl) == name)
3864 break;
3865 if (decl)
3866 return namespace_bindings_p () ? decl : NULL_TREE;
3867 decl = build_lang_decl (USING_DECL, name, NULL_TREE);
3868 USING_DECL_SCOPE (decl) = scope;
3869 DECL_CHAIN (decl) = current_binding_level->usings;
3870 current_binding_level->usings = decl;
3871 return decl;
3874 /* Wrapper for push_using_decl_1. */
3876 static tree
3877 push_using_decl (tree scope, tree name)
3879 tree ret;
3880 timevar_start (TV_NAME_LOOKUP);
3881 ret = push_using_decl_1 (scope, name);
3882 timevar_stop (TV_NAME_LOOKUP);
3883 return ret;
3886 /* Same as pushdecl, but define X in binding-level LEVEL. We rely on the
3887 caller to set DECL_CONTEXT properly.
3889 Note that this must only be used when X will be the new innermost
3890 binding for its name, as we tack it onto the front of IDENTIFIER_BINDING
3891 without checking to see if the current IDENTIFIER_BINDING comes from a
3892 closer binding level than LEVEL. */
3894 static tree
3895 do_pushdecl_with_scope (tree x, cp_binding_level *level, bool is_friend)
3897 cp_binding_level *b;
3899 if (level->kind == sk_class)
3901 b = class_binding_level;
3902 class_binding_level = level;
3903 pushdecl_class_level (x);
3904 class_binding_level = b;
3906 else
3908 tree function_decl = current_function_decl;
3909 if (level->kind == sk_namespace)
3910 current_function_decl = NULL_TREE;
3911 b = current_binding_level;
3912 current_binding_level = level;
3913 x = pushdecl (x, is_friend);
3914 current_binding_level = b;
3915 current_function_decl = function_decl;
3917 return x;
3920 /* Inject X into the local scope just before the function parms. */
3922 tree
3923 pushdecl_outermost_localscope (tree x)
3925 cp_binding_level *b = NULL;
3926 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3928 /* Find the scope just inside the function parms. */
3929 for (cp_binding_level *n = current_binding_level;
3930 n->kind != sk_function_parms; n = b->level_chain)
3931 b = n;
3933 tree ret = b ? do_pushdecl_with_scope (x, b, false) : error_mark_node;
3934 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3936 return ret;
3939 /* Check a non-member using-declaration. Return the name and scope
3940 being used, and the USING_DECL, or NULL_TREE on failure. */
3942 static tree
3943 validate_nonmember_using_decl (tree decl, tree scope, tree name)
3945 /* [namespace.udecl]
3946 A using-declaration for a class member shall be a
3947 member-declaration. */
3948 if (TYPE_P (scope))
3950 error ("%qT is not a namespace or unscoped enum", scope);
3951 return NULL_TREE;
3953 else if (scope == error_mark_node)
3954 return NULL_TREE;
3956 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR)
3958 /* 7.3.3/5
3959 A using-declaration shall not name a template-id. */
3960 error ("a using-declaration cannot specify a template-id. "
3961 "Try %<using %D%>", name);
3962 return NULL_TREE;
3965 if (TREE_CODE (decl) == NAMESPACE_DECL)
3967 error ("namespace %qD not allowed in using-declaration", decl);
3968 return NULL_TREE;
3971 if (TREE_CODE (decl) == SCOPE_REF)
3973 /* It's a nested name with template parameter dependent scope.
3974 This can only be using-declaration for class member. */
3975 error ("%qT is not a namespace", TREE_OPERAND (decl, 0));
3976 return NULL_TREE;
3979 decl = OVL_FIRST (decl);
3981 /* Make a USING_DECL. */
3982 tree using_decl = push_using_decl (scope, name);
3984 if (using_decl == NULL_TREE
3985 && at_function_scope_p ()
3986 && VAR_P (decl))
3987 /* C++11 7.3.3/10. */
3988 error ("%qD is already declared in this scope", name);
3990 return using_decl;
3993 /* Process a local-scope or namespace-scope using declaration. SCOPE
3994 is the nominated scope to search for NAME. VALUE_P and TYPE_P
3995 point to the binding for NAME in the current scope and are
3996 updated. */
3998 static void
3999 do_nonmember_using_decl (tree scope, tree name, tree *value_p, tree *type_p)
4001 name_lookup lookup (name, 0);
4003 if (!qualified_namespace_lookup (scope, &lookup))
4005 error ("%qD not declared", name);
4006 return;
4008 else if (TREE_CODE (lookup.value) == TREE_LIST)
4010 error ("reference to %qD is ambiguous", name);
4011 print_candidates (lookup.value);
4012 lookup.value = NULL_TREE;
4015 if (lookup.type && TREE_CODE (lookup.type) == TREE_LIST)
4017 error ("reference to %qD is ambiguous", name);
4018 print_candidates (lookup.type);
4019 lookup.type = NULL_TREE;
4022 tree value = *value_p;
4023 tree type = *type_p;
4025 /* Shift the old and new bindings around so we're comparing class and
4026 enumeration names to each other. */
4027 if (value && DECL_IMPLICIT_TYPEDEF_P (value))
4029 type = value;
4030 value = NULL_TREE;
4033 if (lookup.value && DECL_IMPLICIT_TYPEDEF_P (lookup.value))
4035 lookup.type = lookup.value;
4036 lookup.value = NULL_TREE;
4039 if (lookup.value && lookup.value != value)
4041 /* Check for using functions. */
4042 if (OVL_P (lookup.value) && (!value || OVL_P (value)))
4044 for (lkp_iterator usings (lookup.value); usings; ++usings)
4046 tree new_fn = *usings;
4048 /* [namespace.udecl]
4050 If a function declaration in namespace scope or block
4051 scope has the same name and the same parameter types as a
4052 function introduced by a using declaration the program is
4053 ill-formed. */
4054 bool found = false;
4055 for (ovl_iterator old (value); !found && old; ++old)
4057 tree old_fn = *old;
4059 if (new_fn == old_fn)
4060 /* The function already exists in the current
4061 namespace. */
4062 found = true;
4063 else if (old.using_p ())
4064 continue; /* This is a using decl. */
4065 else if (old.hidden_p () && !DECL_HIDDEN_FRIEND_P (old_fn))
4066 continue; /* This is an anticipated builtin. */
4067 else if (!matching_fn_p (new_fn, old_fn))
4068 continue; /* Parameters do not match. */
4069 else if (decls_match (new_fn, old_fn))
4070 found = true;
4071 else
4073 diagnose_name_conflict (new_fn, old_fn);
4074 found = true;
4078 if (!found)
4079 /* Unlike the overload case we don't drop anticipated
4080 builtins here. They don't cause a problem, and
4081 we'd like to match them with a future
4082 declaration. */
4083 value = ovl_insert (new_fn, value, true);
4086 else if (value
4087 /* Ignore anticipated builtins. */
4088 && !anticipated_builtin_p (value)
4089 && !decls_match (lookup.value, value))
4090 diagnose_name_conflict (lookup.value, value);
4091 else
4092 value = lookup.value;
4095 if (lookup.type && lookup.type != type)
4097 if (type && !decls_match (lookup.type, type))
4098 diagnose_name_conflict (lookup.type, type);
4099 else
4100 type = lookup.type;
4103 /* If bind->value is empty, shift any class or enumeration name back. */
4104 if (!value)
4106 value = type;
4107 type = NULL_TREE;
4110 *value_p = value;
4111 *type_p = type;
4114 /* Returns true if ANCESTOR encloses DESCENDANT, including matching.
4115 Both are namespaces. */
4117 bool
4118 is_nested_namespace (tree ancestor, tree descendant, bool inline_only)
4120 int depth = SCOPE_DEPTH (ancestor);
4122 if (!depth && !inline_only)
4123 /* The global namespace encloses everything. */
4124 return true;
4126 while (SCOPE_DEPTH (descendant) > depth
4127 && (!inline_only || DECL_NAMESPACE_INLINE_P (descendant)))
4128 descendant = CP_DECL_CONTEXT (descendant);
4130 return ancestor == descendant;
4133 /* Returns true if ROOT (a namespace, class, or function) encloses
4134 CHILD. CHILD may be either a class type or a namespace. */
4136 bool
4137 is_ancestor (tree root, tree child)
4139 gcc_assert ((TREE_CODE (root) == NAMESPACE_DECL
4140 || TREE_CODE (root) == FUNCTION_DECL
4141 || CLASS_TYPE_P (root)));
4142 gcc_assert ((TREE_CODE (child) == NAMESPACE_DECL
4143 || CLASS_TYPE_P (child)));
4145 /* The global namespace encloses everything. */
4146 if (root == global_namespace)
4147 return true;
4149 /* Search until we reach namespace scope. */
4150 while (TREE_CODE (child) != NAMESPACE_DECL)
4152 /* If we've reached the ROOT, it encloses CHILD. */
4153 if (root == child)
4154 return true;
4155 /* Go out one level. */
4156 if (TYPE_P (child))
4157 child = TYPE_NAME (child);
4158 child = CP_DECL_CONTEXT (child);
4161 if (TREE_CODE (root) == NAMESPACE_DECL)
4162 return is_nested_namespace (root, child);
4164 return false;
4167 /* Enter the class or namespace scope indicated by T suitable for name
4168 lookup. T can be arbitrary scope, not necessary nested inside the
4169 current scope. Returns a non-null scope to pop iff pop_scope
4170 should be called later to exit this scope. */
4172 tree
4173 push_scope (tree t)
4175 if (TREE_CODE (t) == NAMESPACE_DECL)
4176 push_decl_namespace (t);
4177 else if (CLASS_TYPE_P (t))
4179 if (!at_class_scope_p ()
4180 || !same_type_p (current_class_type, t))
4181 push_nested_class (t);
4182 else
4183 /* T is the same as the current scope. There is therefore no
4184 need to re-enter the scope. Since we are not actually
4185 pushing a new scope, our caller should not call
4186 pop_scope. */
4187 t = NULL_TREE;
4190 return t;
4193 /* Leave scope pushed by push_scope. */
4195 void
4196 pop_scope (tree t)
4198 if (t == NULL_TREE)
4199 return;
4200 if (TREE_CODE (t) == NAMESPACE_DECL)
4201 pop_decl_namespace ();
4202 else if CLASS_TYPE_P (t)
4203 pop_nested_class ();
4206 /* Subroutine of push_inner_scope. */
4208 static void
4209 push_inner_scope_r (tree outer, tree inner)
4211 tree prev;
4213 if (outer == inner
4214 || (TREE_CODE (inner) != NAMESPACE_DECL && !CLASS_TYPE_P (inner)))
4215 return;
4217 prev = CP_DECL_CONTEXT (TREE_CODE (inner) == NAMESPACE_DECL ? inner : TYPE_NAME (inner));
4218 if (outer != prev)
4219 push_inner_scope_r (outer, prev);
4220 if (TREE_CODE (inner) == NAMESPACE_DECL)
4222 cp_binding_level *save_template_parm = 0;
4223 /* Temporary take out template parameter scopes. They are saved
4224 in reversed order in save_template_parm. */
4225 while (current_binding_level->kind == sk_template_parms)
4227 cp_binding_level *b = current_binding_level;
4228 current_binding_level = b->level_chain;
4229 b->level_chain = save_template_parm;
4230 save_template_parm = b;
4233 resume_scope (NAMESPACE_LEVEL (inner));
4234 current_namespace = inner;
4236 /* Restore template parameter scopes. */
4237 while (save_template_parm)
4239 cp_binding_level *b = save_template_parm;
4240 save_template_parm = b->level_chain;
4241 b->level_chain = current_binding_level;
4242 current_binding_level = b;
4245 else
4246 pushclass (inner);
4249 /* Enter the scope INNER from current scope. INNER must be a scope
4250 nested inside current scope. This works with both name lookup and
4251 pushing name into scope. In case a template parameter scope is present,
4252 namespace is pushed under the template parameter scope according to
4253 name lookup rule in 14.6.1/6.
4255 Return the former current scope suitable for pop_inner_scope. */
4257 tree
4258 push_inner_scope (tree inner)
4260 tree outer = current_scope ();
4261 if (!outer)
4262 outer = current_namespace;
4264 push_inner_scope_r (outer, inner);
4265 return outer;
4268 /* Exit the current scope INNER back to scope OUTER. */
4270 void
4271 pop_inner_scope (tree outer, tree inner)
4273 if (outer == inner
4274 || (TREE_CODE (inner) != NAMESPACE_DECL && !CLASS_TYPE_P (inner)))
4275 return;
4277 while (outer != inner)
4279 if (TREE_CODE (inner) == NAMESPACE_DECL)
4281 cp_binding_level *save_template_parm = 0;
4282 /* Temporary take out template parameter scopes. They are saved
4283 in reversed order in save_template_parm. */
4284 while (current_binding_level->kind == sk_template_parms)
4286 cp_binding_level *b = current_binding_level;
4287 current_binding_level = b->level_chain;
4288 b->level_chain = save_template_parm;
4289 save_template_parm = b;
4292 pop_namespace ();
4294 /* Restore template parameter scopes. */
4295 while (save_template_parm)
4297 cp_binding_level *b = save_template_parm;
4298 save_template_parm = b->level_chain;
4299 b->level_chain = current_binding_level;
4300 current_binding_level = b;
4303 else
4304 popclass ();
4306 inner = CP_DECL_CONTEXT (TREE_CODE (inner) == NAMESPACE_DECL ? inner : TYPE_NAME (inner));
4310 /* Do a pushlevel for class declarations. */
4312 void
4313 pushlevel_class (void)
4315 class_binding_level = begin_scope (sk_class, current_class_type);
4318 /* ...and a poplevel for class declarations. */
4320 void
4321 poplevel_class (void)
4323 cp_binding_level *level = class_binding_level;
4324 cp_class_binding *cb;
4325 size_t i;
4326 tree shadowed;
4328 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4329 gcc_assert (level != 0);
4331 /* If we're leaving a toplevel class, cache its binding level. */
4332 if (current_class_depth == 1)
4333 previous_class_level = level;
4334 for (shadowed = level->type_shadowed;
4335 shadowed;
4336 shadowed = TREE_CHAIN (shadowed))
4337 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (shadowed), TREE_VALUE (shadowed));
4339 /* Remove the bindings for all of the class-level declarations. */
4340 if (level->class_shadowed)
4342 FOR_EACH_VEC_ELT (*level->class_shadowed, i, cb)
4344 IDENTIFIER_BINDING (cb->identifier) = cb->base->previous;
4345 cxx_binding_free (cb->base);
4347 ggc_free (level->class_shadowed);
4348 level->class_shadowed = NULL;
4351 /* Now, pop out of the binding level which we created up in the
4352 `pushlevel_class' routine. */
4353 gcc_assert (current_binding_level == level);
4354 leave_scope ();
4355 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4358 /* Set INHERITED_VALUE_BINDING_P on BINDING to true or false, as
4359 appropriate. DECL is the value to which a name has just been
4360 bound. CLASS_TYPE is the class in which the lookup occurred. */
4362 static void
4363 set_inherited_value_binding_p (cxx_binding *binding, tree decl,
4364 tree class_type)
4366 if (binding->value == decl && TREE_CODE (decl) != TREE_LIST)
4368 tree context;
4370 if (TREE_CODE (decl) == OVERLOAD)
4371 context = ovl_scope (decl);
4372 else
4374 gcc_assert (DECL_P (decl));
4375 context = context_for_name_lookup (decl);
4378 if (is_properly_derived_from (class_type, context))
4379 INHERITED_VALUE_BINDING_P (binding) = 1;
4380 else
4381 INHERITED_VALUE_BINDING_P (binding) = 0;
4383 else if (binding->value == decl)
4384 /* We only encounter a TREE_LIST when there is an ambiguity in the
4385 base classes. Such an ambiguity can be overridden by a
4386 definition in this class. */
4387 INHERITED_VALUE_BINDING_P (binding) = 1;
4388 else
4389 INHERITED_VALUE_BINDING_P (binding) = 0;
4392 /* Make the declaration of X appear in CLASS scope. */
4394 bool
4395 pushdecl_class_level (tree x)
4397 bool is_valid = true;
4398 bool subtime;
4400 /* Do nothing if we're adding to an outer lambda closure type,
4401 outer_binding will add it later if it's needed. */
4402 if (current_class_type != class_binding_level->this_entity)
4403 return true;
4405 subtime = timevar_cond_start (TV_NAME_LOOKUP);
4406 /* Get the name of X. */
4407 tree name = OVL_NAME (x);
4409 if (name)
4411 is_valid = push_class_level_binding (name, x);
4412 if (TREE_CODE (x) == TYPE_DECL)
4413 set_identifier_type_value (name, x);
4415 else if (ANON_AGGR_TYPE_P (TREE_TYPE (x)))
4417 /* If X is an anonymous aggregate, all of its members are
4418 treated as if they were members of the class containing the
4419 aggregate, for naming purposes. */
4420 location_t save_location = input_location;
4421 tree anon = TREE_TYPE (x);
4422 if (vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (anon))
4423 for (unsigned ix = member_vec->length (); ix--;)
4425 tree binding = (*member_vec)[ix];
4426 if (STAT_HACK_P (binding))
4428 if (!pushdecl_class_level (STAT_TYPE (binding)))
4429 is_valid = false;
4430 binding = STAT_DECL (binding);
4432 if (!pushdecl_class_level (binding))
4433 is_valid = false;
4435 else
4436 for (tree f = TYPE_FIELDS (anon); f; f = DECL_CHAIN (f))
4437 if (TREE_CODE (f) == FIELD_DECL)
4439 input_location = DECL_SOURCE_LOCATION (f);
4440 if (!pushdecl_class_level (f))
4441 is_valid = false;
4443 input_location = save_location;
4445 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4446 return is_valid;
4449 /* Return the BINDING (if any) for NAME in SCOPE, which is a class
4450 scope. If the value returned is non-NULL, and the PREVIOUS field
4451 is not set, callers must set the PREVIOUS field explicitly. */
4453 static cxx_binding *
4454 get_class_binding (tree name, cp_binding_level *scope)
4456 tree class_type;
4457 tree type_binding;
4458 tree value_binding;
4459 cxx_binding *binding;
4461 class_type = scope->this_entity;
4463 /* Get the type binding. */
4464 type_binding = lookup_member (class_type, name,
4465 /*protect=*/2, /*want_type=*/true,
4466 tf_warning_or_error);
4467 /* Get the value binding. */
4468 value_binding = lookup_member (class_type, name,
4469 /*protect=*/2, /*want_type=*/false,
4470 tf_warning_or_error);
4472 if (value_binding
4473 && (TREE_CODE (value_binding) == TYPE_DECL
4474 || DECL_CLASS_TEMPLATE_P (value_binding)
4475 || (TREE_CODE (value_binding) == TREE_LIST
4476 && TREE_TYPE (value_binding) == error_mark_node
4477 && (TREE_CODE (TREE_VALUE (value_binding))
4478 == TYPE_DECL))))
4479 /* We found a type binding, even when looking for a non-type
4480 binding. This means that we already processed this binding
4481 above. */
4483 else if (value_binding)
4485 if (TREE_CODE (value_binding) == TREE_LIST
4486 && TREE_TYPE (value_binding) == error_mark_node)
4487 /* NAME is ambiguous. */
4489 else if (BASELINK_P (value_binding))
4490 /* NAME is some overloaded functions. */
4491 value_binding = BASELINK_FUNCTIONS (value_binding);
4494 /* If we found either a type binding or a value binding, create a
4495 new binding object. */
4496 if (type_binding || value_binding)
4498 binding = new_class_binding (name,
4499 value_binding,
4500 type_binding,
4501 scope);
4502 /* This is a class-scope binding, not a block-scope binding. */
4503 LOCAL_BINDING_P (binding) = 0;
4504 set_inherited_value_binding_p (binding, value_binding, class_type);
4506 else
4507 binding = NULL;
4509 return binding;
4512 /* Make the declaration(s) of X appear in CLASS scope under the name
4513 NAME. Returns true if the binding is valid. */
4515 static bool
4516 push_class_level_binding_1 (tree name, tree x)
4518 cxx_binding *binding;
4519 tree decl = x;
4520 bool ok;
4522 /* The class_binding_level will be NULL if x is a template
4523 parameter name in a member template. */
4524 if (!class_binding_level)
4525 return true;
4527 if (name == error_mark_node)
4528 return false;
4530 /* Can happen for an erroneous declaration (c++/60384). */
4531 if (!identifier_p (name))
4533 gcc_assert (errorcount || sorrycount);
4534 return false;
4537 /* Check for invalid member names. But don't worry about a default
4538 argument-scope lambda being pushed after the class is complete. */
4539 gcc_assert (TYPE_BEING_DEFINED (current_class_type)
4540 || LAMBDA_TYPE_P (TREE_TYPE (decl)));
4541 /* Check that we're pushing into the right binding level. */
4542 gcc_assert (current_class_type == class_binding_level->this_entity);
4544 /* We could have been passed a tree list if this is an ambiguous
4545 declaration. If so, pull the declaration out because
4546 check_template_shadow will not handle a TREE_LIST. */
4547 if (TREE_CODE (decl) == TREE_LIST
4548 && TREE_TYPE (decl) == error_mark_node)
4549 decl = TREE_VALUE (decl);
4551 if (!check_template_shadow (decl))
4552 return false;
4554 /* [class.mem]
4556 If T is the name of a class, then each of the following shall
4557 have a name different from T:
4559 -- every static data member of class T;
4561 -- every member of class T that is itself a type;
4563 -- every enumerator of every member of class T that is an
4564 enumerated type;
4566 -- every member of every anonymous union that is a member of
4567 class T.
4569 (Non-static data members were also forbidden to have the same
4570 name as T until TC1.) */
4571 if ((VAR_P (x)
4572 || TREE_CODE (x) == CONST_DECL
4573 || (TREE_CODE (x) == TYPE_DECL
4574 && !DECL_SELF_REFERENCE_P (x))
4575 /* A data member of an anonymous union. */
4576 || (TREE_CODE (x) == FIELD_DECL
4577 && DECL_CONTEXT (x) != current_class_type))
4578 && DECL_NAME (x) == DECL_NAME (TYPE_NAME (current_class_type)))
4580 tree scope = context_for_name_lookup (x);
4581 if (TYPE_P (scope) && same_type_p (scope, current_class_type))
4583 error ("%qD has the same name as the class in which it is "
4584 "declared",
4586 return false;
4590 /* Get the current binding for NAME in this class, if any. */
4591 binding = IDENTIFIER_BINDING (name);
4592 if (!binding || binding->scope != class_binding_level)
4594 binding = get_class_binding (name, class_binding_level);
4595 /* If a new binding was created, put it at the front of the
4596 IDENTIFIER_BINDING list. */
4597 if (binding)
4599 binding->previous = IDENTIFIER_BINDING (name);
4600 IDENTIFIER_BINDING (name) = binding;
4604 /* If there is already a binding, then we may need to update the
4605 current value. */
4606 if (binding && binding->value)
4608 tree bval = binding->value;
4609 tree old_decl = NULL_TREE;
4610 tree target_decl = strip_using_decl (decl);
4611 tree target_bval = strip_using_decl (bval);
4613 if (INHERITED_VALUE_BINDING_P (binding))
4615 /* If the old binding was from a base class, and was for a
4616 tag name, slide it over to make room for the new binding.
4617 The old binding is still visible if explicitly qualified
4618 with a class-key. */
4619 if (TREE_CODE (target_bval) == TYPE_DECL
4620 && DECL_ARTIFICIAL (target_bval)
4621 && !(TREE_CODE (target_decl) == TYPE_DECL
4622 && DECL_ARTIFICIAL (target_decl)))
4624 old_decl = binding->type;
4625 binding->type = bval;
4626 binding->value = NULL_TREE;
4627 INHERITED_VALUE_BINDING_P (binding) = 0;
4629 else
4631 old_decl = bval;
4632 /* Any inherited type declaration is hidden by the type
4633 declaration in the derived class. */
4634 if (TREE_CODE (target_decl) == TYPE_DECL
4635 && DECL_ARTIFICIAL (target_decl))
4636 binding->type = NULL_TREE;
4639 else if (TREE_CODE (target_decl) == OVERLOAD
4640 && OVL_P (target_bval))
4641 old_decl = bval;
4642 else if (TREE_CODE (decl) == USING_DECL
4643 && TREE_CODE (bval) == USING_DECL
4644 && same_type_p (USING_DECL_SCOPE (decl),
4645 USING_DECL_SCOPE (bval)))
4646 /* This is a using redeclaration that will be diagnosed later
4647 in supplement_binding */
4649 else if (TREE_CODE (decl) == USING_DECL
4650 && TREE_CODE (bval) == USING_DECL
4651 && DECL_DEPENDENT_P (decl)
4652 && DECL_DEPENDENT_P (bval))
4653 return true;
4654 else if (TREE_CODE (decl) == USING_DECL
4655 && OVL_P (target_bval))
4656 old_decl = bval;
4657 else if (TREE_CODE (bval) == USING_DECL
4658 && OVL_P (target_decl))
4659 return true;
4661 if (old_decl && binding->scope == class_binding_level)
4663 binding->value = x;
4664 /* It is always safe to clear INHERITED_VALUE_BINDING_P
4665 here. This function is only used to register bindings
4666 from with the class definition itself. */
4667 INHERITED_VALUE_BINDING_P (binding) = 0;
4668 return true;
4672 /* Note that we declared this value so that we can issue an error if
4673 this is an invalid redeclaration of a name already used for some
4674 other purpose. */
4675 note_name_declared_in_class (name, decl);
4677 /* If we didn't replace an existing binding, put the binding on the
4678 stack of bindings for the identifier, and update the shadowed
4679 list. */
4680 if (binding && binding->scope == class_binding_level)
4681 /* Supplement the existing binding. */
4682 ok = supplement_binding (binding, decl);
4683 else
4685 /* Create a new binding. */
4686 push_binding (name, decl, class_binding_level);
4687 ok = true;
4690 return ok;
4693 /* Wrapper for push_class_level_binding_1. */
4695 bool
4696 push_class_level_binding (tree name, tree x)
4698 bool ret;
4699 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4700 ret = push_class_level_binding_1 (name, x);
4701 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4702 return ret;
4705 /* Process "using SCOPE::NAME" in a class scope. Return the
4706 USING_DECL created. */
4708 tree
4709 do_class_using_decl (tree scope, tree name)
4711 if (name == error_mark_node)
4712 return NULL_TREE;
4714 if (!scope || !TYPE_P (scope))
4716 error ("using-declaration for non-member at class scope");
4717 return NULL_TREE;
4720 /* Make sure the name is not invalid */
4721 if (TREE_CODE (name) == BIT_NOT_EXPR)
4723 error ("%<%T::%D%> names destructor", scope, name);
4724 return NULL_TREE;
4727 /* Using T::T declares inheriting ctors, even if T is a typedef. */
4728 if (MAYBE_CLASS_TYPE_P (scope)
4729 && (name == TYPE_IDENTIFIER (scope)
4730 || constructor_name_p (name, scope)))
4732 maybe_warn_cpp0x (CPP0X_INHERITING_CTORS);
4733 name = ctor_identifier;
4734 CLASSTYPE_NON_AGGREGATE (current_class_type) = true;
4737 /* Cannot introduce a constructor name. */
4738 if (constructor_name_p (name, current_class_type))
4740 error ("%<%T::%D%> names constructor in %qT",
4741 scope, name, current_class_type);
4742 return NULL_TREE;
4745 /* From [namespace.udecl]:
4747 A using-declaration used as a member-declaration shall refer to a
4748 member of a base class of the class being defined.
4750 In general, we cannot check this constraint in a template because
4751 we do not know the entire set of base classes of the current
4752 class type. Morover, if SCOPE is dependent, it might match a
4753 non-dependent base. */
4755 tree decl = NULL_TREE;
4756 if (!dependent_scope_p (scope))
4758 base_kind b_kind;
4759 tree binfo = lookup_base (current_class_type, scope, ba_any, &b_kind,
4760 tf_warning_or_error);
4761 if (b_kind < bk_proper_base)
4763 /* If there are dependent bases, scope might resolve at
4764 instantiation time, even if it isn't exactly one of the
4765 dependent bases. */
4766 if (b_kind == bk_same_type || !any_dependent_bases_p ())
4768 error_not_base_type (scope, current_class_type);
4769 return NULL_TREE;
4772 else if (name == ctor_identifier && !binfo_direct_p (binfo))
4774 error ("cannot inherit constructors from indirect base %qT", scope);
4775 return NULL_TREE;
4777 else if (!IDENTIFIER_CONV_OP_P (name)
4778 || !dependent_type_p (TREE_TYPE (name)))
4780 decl = lookup_member (binfo, name, 0, false, tf_warning_or_error);
4781 if (!decl)
4783 error ("no members matching %<%T::%D%> in %q#T", scope, name,
4784 scope);
4785 return NULL_TREE;
4788 /* The binfo from which the functions came does not matter. */
4789 if (BASELINK_P (decl))
4790 decl = BASELINK_FUNCTIONS (decl);
4794 tree value = build_lang_decl (USING_DECL, name, NULL_TREE);
4795 USING_DECL_DECLS (value) = decl;
4796 USING_DECL_SCOPE (value) = scope;
4797 DECL_DEPENDENT_P (value) = !decl;
4799 return value;
4803 /* Return the binding for NAME in NS. If NS is NULL, look in
4804 global_namespace. */
4806 tree
4807 get_namespace_binding (tree ns, tree name)
4809 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4810 if (!ns)
4811 ns = global_namespace;
4812 gcc_checking_assert (!DECL_NAMESPACE_ALIAS (ns));
4813 tree ret = find_namespace_value (ns, name);
4814 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4815 return ret;
4818 /* Push internal DECL into the global namespace. Does not do the
4819 full overload fn handling and does not add it to the list of things
4820 in the namespace. */
4822 void
4823 set_global_binding (tree decl)
4825 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4827 tree *slot = find_namespace_slot (global_namespace, DECL_NAME (decl), true);
4829 if (*slot)
4830 /* The user's placed something in the implementor's namespace. */
4831 diagnose_name_conflict (decl, MAYBE_STAT_DECL (*slot));
4833 /* Force the binding, so compiler internals continue to work. */
4834 *slot = decl;
4836 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4839 /* Set the context of a declaration to scope. Complain if we are not
4840 outside scope. */
4842 void
4843 set_decl_namespace (tree decl, tree scope, bool friendp)
4845 /* Get rid of namespace aliases. */
4846 scope = ORIGINAL_NAMESPACE (scope);
4848 /* It is ok for friends to be qualified in parallel space. */
4849 if (!friendp && !is_nested_namespace (current_namespace, scope))
4850 error ("declaration of %qD not in a namespace surrounding %qD",
4851 decl, scope);
4852 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
4854 /* See whether this has been declared in the namespace or inline
4855 children. */
4856 tree old = NULL_TREE;
4858 name_lookup lookup (DECL_NAME (decl), LOOKUP_HIDDEN);
4859 if (!lookup.search_qualified (scope, /*usings=*/false))
4860 /* No old declaration at all. */
4861 goto not_found;
4862 old = lookup.value;
4865 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
4866 if (TREE_CODE (old) == TREE_LIST)
4868 ambiguous:
4869 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
4870 error ("reference to %qD is ambiguous", decl);
4871 print_candidates (old);
4872 return;
4875 if (!DECL_DECLARES_FUNCTION_P (decl))
4877 /* Don't compare non-function decls with decls_match here, since
4878 it can't check for the correct constness at this
4879 point. pushdecl will find those errors later. */
4881 /* We might have found it in an inline namespace child of SCOPE. */
4882 if (TREE_CODE (decl) == TREE_CODE (old))
4883 DECL_CONTEXT (decl) = DECL_CONTEXT (old);
4885 found:
4886 /* Writing "N::i" to declare something directly in "N" is invalid. */
4887 if (CP_DECL_CONTEXT (decl) == current_namespace
4888 && at_namespace_scope_p ())
4889 error ("explicit qualification in declaration of %qD", decl);
4890 return;
4893 /* Since decl is a function, old should contain a function decl. */
4894 if (!OVL_P (old))
4895 goto not_found;
4897 /* We handle these in check_explicit_instantiation_namespace. */
4898 if (processing_explicit_instantiation)
4899 return;
4900 if (processing_template_decl || processing_specialization)
4901 /* We have not yet called push_template_decl to turn a
4902 FUNCTION_DECL into a TEMPLATE_DECL, so the declarations won't
4903 match. But, we'll check later, when we construct the
4904 template. */
4905 return;
4906 /* Instantiations or specializations of templates may be declared as
4907 friends in any namespace. */
4908 if (friendp && DECL_USE_TEMPLATE (decl))
4909 return;
4911 tree found;
4912 found = NULL_TREE;
4914 for (lkp_iterator iter (old); iter; ++iter)
4916 if (iter.using_p ())
4917 continue;
4919 tree ofn = *iter;
4921 /* Adjust DECL_CONTEXT first so decls_match will return true
4922 if DECL will match a declaration in an inline namespace. */
4923 DECL_CONTEXT (decl) = DECL_CONTEXT (ofn);
4924 if (decls_match (decl, ofn))
4926 if (found)
4928 /* We found more than one matching declaration. */
4929 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
4930 goto ambiguous;
4932 found = ofn;
4936 if (found)
4938 if (DECL_HIDDEN_FRIEND_P (found))
4940 pedwarn (DECL_SOURCE_LOCATION (decl), 0,
4941 "%qD has not been declared within %qD", decl, scope);
4942 inform (DECL_SOURCE_LOCATION (found),
4943 "only here as a %<friend%>");
4945 DECL_CONTEXT (decl) = DECL_CONTEXT (found);
4946 goto found;
4949 not_found:
4950 /* It didn't work, go back to the explicit scope. */
4951 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
4952 error ("%qD should have been declared inside %qD", decl, scope);
4955 /* Return the namespace where the current declaration is declared. */
4957 tree
4958 current_decl_namespace (void)
4960 tree result;
4961 /* If we have been pushed into a different namespace, use it. */
4962 if (!vec_safe_is_empty (decl_namespace_list))
4963 return decl_namespace_list->last ();
4965 if (current_class_type)
4966 result = decl_namespace_context (current_class_type);
4967 else if (current_function_decl)
4968 result = decl_namespace_context (current_function_decl);
4969 else
4970 result = current_namespace;
4971 return result;
4974 /* Process any ATTRIBUTES on a namespace definition. Returns true if
4975 attribute visibility is seen. */
4977 bool
4978 handle_namespace_attrs (tree ns, tree attributes)
4980 tree d;
4981 bool saw_vis = false;
4983 if (attributes == error_mark_node)
4984 return false;
4986 for (d = attributes; d; d = TREE_CHAIN (d))
4988 tree name = get_attribute_name (d);
4989 tree args = TREE_VALUE (d);
4991 if (is_attribute_p ("visibility", name))
4993 /* attribute visibility is a property of the syntactic block
4994 rather than the namespace as a whole, so we don't touch the
4995 NAMESPACE_DECL at all. */
4996 tree x = args ? TREE_VALUE (args) : NULL_TREE;
4997 if (x == NULL_TREE || TREE_CODE (x) != STRING_CST || TREE_CHAIN (args))
4999 warning (OPT_Wattributes,
5000 "%qD attribute requires a single NTBS argument",
5001 name);
5002 continue;
5005 if (!TREE_PUBLIC (ns))
5006 warning (OPT_Wattributes,
5007 "%qD attribute is meaningless since members of the "
5008 "anonymous namespace get local symbols", name);
5010 push_visibility (TREE_STRING_POINTER (x), 1);
5011 saw_vis = true;
5013 else if (is_attribute_p ("abi_tag", name))
5015 if (!DECL_NAME (ns))
5017 warning (OPT_Wattributes, "ignoring %qD attribute on anonymous "
5018 "namespace", name);
5019 continue;
5021 if (!DECL_NAMESPACE_INLINE_P (ns))
5023 warning (OPT_Wattributes, "ignoring %qD attribute on non-inline "
5024 "namespace", name);
5025 continue;
5027 if (!args)
5029 tree dn = DECL_NAME (ns);
5030 args = build_string (IDENTIFIER_LENGTH (dn) + 1,
5031 IDENTIFIER_POINTER (dn));
5032 TREE_TYPE (args) = char_array_type_node;
5033 args = fix_string_type (args);
5034 args = build_tree_list (NULL_TREE, args);
5036 if (check_abi_tag_args (args, name))
5037 DECL_ATTRIBUTES (ns) = tree_cons (name, args,
5038 DECL_ATTRIBUTES (ns));
5040 else
5042 warning (OPT_Wattributes, "%qD attribute directive ignored",
5043 name);
5044 continue;
5048 return saw_vis;
5051 /* Temporarily set the namespace for the current declaration. */
5053 void
5054 push_decl_namespace (tree decl)
5056 if (TREE_CODE (decl) != NAMESPACE_DECL)
5057 decl = decl_namespace_context (decl);
5058 vec_safe_push (decl_namespace_list, ORIGINAL_NAMESPACE (decl));
5061 /* [namespace.memdef]/2 */
5063 void
5064 pop_decl_namespace (void)
5066 decl_namespace_list->pop ();
5069 /* Process a namespace-alias declaration. */
5071 void
5072 do_namespace_alias (tree alias, tree name_space)
5074 if (name_space == error_mark_node)
5075 return;
5077 gcc_assert (TREE_CODE (name_space) == NAMESPACE_DECL);
5079 name_space = ORIGINAL_NAMESPACE (name_space);
5081 /* Build the alias. */
5082 alias = build_lang_decl (NAMESPACE_DECL, alias, void_type_node);
5083 DECL_NAMESPACE_ALIAS (alias) = name_space;
5084 DECL_EXTERNAL (alias) = 1;
5085 DECL_CONTEXT (alias) = FROB_CONTEXT (current_scope ());
5086 pushdecl (alias);
5088 /* Emit debug info for namespace alias. */
5089 if (!building_stmt_list_p ())
5090 (*debug_hooks->early_global_decl) (alias);
5093 /* Like pushdecl, only it places X in the current namespace,
5094 if appropriate. */
5096 tree
5097 pushdecl_namespace_level (tree x, bool is_friend)
5099 cp_binding_level *b = current_binding_level;
5100 tree t;
5102 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
5103 t = do_pushdecl_with_scope
5104 (x, NAMESPACE_LEVEL (current_namespace), is_friend);
5106 /* Now, the type_shadowed stack may screw us. Munge it so it does
5107 what we want. */
5108 if (TREE_CODE (t) == TYPE_DECL)
5110 tree name = DECL_NAME (t);
5111 tree newval;
5112 tree *ptr = (tree *)0;
5113 for (; !global_scope_p (b); b = b->level_chain)
5115 tree shadowed = b->type_shadowed;
5116 for (; shadowed; shadowed = TREE_CHAIN (shadowed))
5117 if (TREE_PURPOSE (shadowed) == name)
5119 ptr = &TREE_VALUE (shadowed);
5120 /* Can't break out of the loop here because sometimes
5121 a binding level will have duplicate bindings for
5122 PT names. It's gross, but I haven't time to fix it. */
5125 newval = TREE_TYPE (t);
5126 if (ptr == (tree *)0)
5128 /* @@ This shouldn't be needed. My test case "zstring.cc" trips
5129 up here if this is changed to an assertion. --KR */
5130 SET_IDENTIFIER_TYPE_VALUE (name, t);
5132 else
5134 *ptr = newval;
5137 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
5138 return t;
5141 /* Process a using-declaration appearing in namespace scope. */
5143 void
5144 finish_namespace_using_decl (tree decl, tree scope, tree name)
5146 tree orig_decl = decl;
5148 gcc_checking_assert (current_binding_level->kind == sk_namespace
5149 && !processing_template_decl);
5150 decl = validate_nonmember_using_decl (decl, scope, name);
5151 if (decl == NULL_TREE)
5152 return;
5154 tree *slot = find_namespace_slot (current_namespace, name, true);
5155 tree val = slot ? MAYBE_STAT_DECL (*slot) : NULL_TREE;
5156 tree type = slot ? MAYBE_STAT_TYPE (*slot) : NULL_TREE;
5157 do_nonmember_using_decl (scope, name, &val, &type);
5158 if (STAT_HACK_P (*slot))
5160 STAT_DECL (*slot) = val;
5161 STAT_TYPE (*slot) = type;
5163 else if (type)
5164 *slot = stat_hack (val, type);
5165 else
5166 *slot = val;
5168 /* Emit debug info. */
5169 cp_emit_debug_info_for_using (orig_decl, current_namespace);
5172 /* Process a using-declaration at function scope. */
5174 void
5175 finish_local_using_decl (tree decl, tree scope, tree name)
5177 tree orig_decl = decl;
5179 gcc_checking_assert (current_binding_level->kind != sk_class
5180 && current_binding_level->kind != sk_namespace);
5181 decl = validate_nonmember_using_decl (decl, scope, name);
5182 if (decl == NULL_TREE)
5183 return;
5185 add_decl_expr (decl);
5187 cxx_binding *binding = find_local_binding (current_binding_level, name);
5188 tree value = binding ? binding->value : NULL_TREE;
5189 tree type = binding ? binding->type : NULL_TREE;
5191 do_nonmember_using_decl (scope, name, &value, &type);
5193 if (!value)
5195 else if (binding && value == binding->value)
5197 else if (binding && binding->value && TREE_CODE (value) == OVERLOAD)
5199 update_local_overload (IDENTIFIER_BINDING (name), value);
5200 IDENTIFIER_BINDING (name)->value = value;
5202 else
5203 /* Install the new binding. */
5204 push_local_binding (name, value, true);
5206 if (!type)
5208 else if (binding && type == binding->type)
5210 else
5212 push_local_binding (name, type, true);
5213 set_identifier_type_value (name, type);
5216 /* Emit debug info. */
5217 if (!processing_template_decl)
5218 cp_emit_debug_info_for_using (orig_decl, current_scope ());
5221 /* Return the declarations that are members of the namespace NS. */
5223 tree
5224 cp_namespace_decls (tree ns)
5226 return NAMESPACE_LEVEL (ns)->names;
5229 /* Combine prefer_type and namespaces_only into flags. */
5231 static int
5232 lookup_flags (int prefer_type, int namespaces_only)
5234 if (namespaces_only)
5235 return LOOKUP_PREFER_NAMESPACES;
5236 if (prefer_type > 1)
5237 return LOOKUP_PREFER_TYPES;
5238 if (prefer_type > 0)
5239 return LOOKUP_PREFER_BOTH;
5240 return 0;
5243 /* Given a lookup that returned VAL, use FLAGS to decide if we want to
5244 ignore it or not. Subroutine of lookup_name_real and
5245 lookup_type_scope. */
5247 static bool
5248 qualify_lookup (tree val, int flags)
5250 if (val == NULL_TREE)
5251 return false;
5252 if ((flags & LOOKUP_PREFER_NAMESPACES) && TREE_CODE (val) == NAMESPACE_DECL)
5253 return true;
5254 if (flags & LOOKUP_PREFER_TYPES)
5256 tree target_val = strip_using_decl (val);
5257 if (TREE_CODE (target_val) == TYPE_DECL
5258 || TREE_CODE (target_val) == TEMPLATE_DECL)
5259 return true;
5261 if (flags & (LOOKUP_PREFER_NAMESPACES | LOOKUP_PREFER_TYPES))
5262 return false;
5263 /* Look through lambda things that we shouldn't be able to see. */
5264 if (!(flags & LOOKUP_HIDDEN) && is_lambda_ignored_entity (val))
5265 return false;
5266 return true;
5269 /* Is there a "using namespace std;" directive within USINGS? */
5271 static bool
5272 using_directives_contain_std_p (vec<tree, va_gc> *usings)
5274 if (!usings)
5275 return false;
5277 for (unsigned ix = usings->length (); ix--;)
5278 if ((*usings)[ix] == std_node)
5279 return true;
5281 return false;
5284 /* Is there a "using namespace std;" directive within the current
5285 namespace (or its ancestors)?
5286 Compare with name_lookup::search_unqualified. */
5288 static bool
5289 has_using_namespace_std_directive_p ()
5291 /* Look at local using-directives. */
5292 for (cp_binding_level *level = current_binding_level;
5293 level->kind != sk_namespace;
5294 level = level->level_chain)
5295 if (using_directives_contain_std_p (level->using_directives))
5296 return true;
5298 /* Look at this namespace and its ancestors. */
5299 for (tree scope = current_namespace; scope; scope = CP_DECL_CONTEXT (scope))
5301 if (using_directives_contain_std_p (DECL_NAMESPACE_USING (scope)))
5302 return true;
5304 if (scope == global_namespace)
5305 break;
5308 return false;
5311 /* Suggest alternatives for NAME, an IDENTIFIER_NODE for which name
5312 lookup failed. Search through all available namespaces and print out
5313 possible candidates. If no exact matches are found, and
5314 SUGGEST_MISSPELLINGS is true, then also look for near-matches and
5315 suggest the best near-match, if there is one. */
5317 void
5318 suggest_alternatives_for (location_t location, tree name,
5319 bool suggest_misspellings)
5321 vec<tree> candidates = vNULL;
5322 vec<tree> worklist = vNULL;
5323 unsigned limit = PARAM_VALUE (CXX_MAX_NAMESPACES_FOR_DIAGNOSTIC_HELP);
5324 bool limited = false;
5326 /* Breadth-first search of namespaces. Up to limit namespaces
5327 searched (limit zero == unlimited). */
5328 worklist.safe_push (global_namespace);
5329 for (unsigned ix = 0; ix != worklist.length (); ix++)
5331 tree ns = worklist[ix];
5332 name_lookup lookup (name);
5334 if (lookup.search_qualified (ns, false))
5335 candidates.safe_push (lookup.value);
5337 if (!limited)
5339 /* Look for child namespaces. We have to do this
5340 indirectly because they are chained in reverse order,
5341 which is confusing to the user. */
5342 vec<tree> children = vNULL;
5344 for (tree decl = NAMESPACE_LEVEL (ns)->names;
5345 decl; decl = TREE_CHAIN (decl))
5346 if (TREE_CODE (decl) == NAMESPACE_DECL
5347 && !DECL_NAMESPACE_ALIAS (decl)
5348 && !DECL_NAMESPACE_INLINE_P (decl))
5349 children.safe_push (decl);
5351 while (!limited && !children.is_empty ())
5353 if (worklist.length () == limit)
5355 /* Unconditionally warn that the search was truncated. */
5356 inform (location,
5357 "maximum limit of %d namespaces searched for %qE",
5358 limit, name);
5359 limited = true;
5361 else
5362 worklist.safe_push (children.pop ());
5364 children.release ();
5367 worklist.release ();
5369 if (candidates.length ())
5371 inform_n (location, candidates.length (),
5372 "suggested alternative:",
5373 "suggested alternatives:");
5374 for (unsigned ix = 0; ix != candidates.length (); ix++)
5376 tree val = candidates[ix];
5378 inform (location_of (val), " %qE", val);
5380 candidates.release ();
5381 return;
5384 /* No candidates were found in the available namespaces. */
5386 /* If there's a "using namespace std;" active, and this
5387 is one of the most common "std::" names, then it's probably a
5388 missing #include. */
5389 if (has_using_namespace_std_directive_p ())
5390 if (maybe_suggest_missing_std_header (location, name))
5391 return;
5393 /* Otherwise, consider misspellings. */
5394 if (!suggest_misspellings)
5395 return;
5396 if (name_hint hint = lookup_name_fuzzy (name, FUZZY_LOOKUP_NAME,
5397 location))
5399 /* Show a spelling correction. */
5400 gcc_rich_location richloc (location);
5402 richloc.add_fixit_replace (hint.suggestion ());
5403 inform (&richloc, "suggested alternative: %qs", hint.suggestion ());
5407 /* A well-known name within the C++ standard library, returned by
5408 get_std_name_hint. */
5410 struct std_name_hint
5412 /* A name within "std::". */
5413 const char *name;
5415 /* The header name defining it within the C++ Standard Library
5416 (with '<' and '>'). */
5417 const char *header;
5419 /* The dialect of C++ in which this was added. */
5420 enum cxx_dialect min_dialect;
5423 /* Subroutine of maybe_suggest_missing_header for handling unrecognized names
5424 for some of the most common names within "std::".
5425 Given non-NULL NAME, return the std_name_hint for it, or NULL. */
5427 static const std_name_hint *
5428 get_std_name_hint (const char *name)
5430 static const std_name_hint hints[] = {
5431 /* <any>. */
5432 {"any", "<any>", cxx17},
5433 {"any_cast", "<any>", cxx17},
5434 {"make_any", "<any>", cxx17},
5435 /* <array>. */
5436 {"array", "<array>", cxx11},
5437 /* <atomic>. */
5438 {"atomic", "<atomic>", cxx11},
5439 {"atomic_flag", "<atomic>", cxx11},
5440 /* <bitset>. */
5441 {"bitset", "<bitset>", cxx11},
5442 /* <complex>. */
5443 {"complex", "<complex>", cxx98},
5444 {"complex_literals", "<complex>", cxx98},
5445 /* <condition_variable>. */
5446 {"condition_variable", "<condition_variable>", cxx11},
5447 {"condition_variable_any", "<condition_variable>", cxx11},
5448 /* <deque>. */
5449 {"deque", "<deque>", cxx98},
5450 /* <forward_list>. */
5451 {"forward_list", "<forward_list>", cxx11},
5452 /* <fstream>. */
5453 {"basic_filebuf", "<fstream>", cxx98},
5454 {"basic_ifstream", "<fstream>", cxx98},
5455 {"basic_ofstream", "<fstream>", cxx98},
5456 {"basic_fstream", "<fstream>", cxx98},
5457 {"fstream", "<fstream>", cxx98},
5458 {"ifstream", "<fstream>", cxx98},
5459 {"ofstream", "<fstream>", cxx98},
5460 /* <functional>. */
5461 {"bind", "<functional>", cxx11},
5462 {"function", "<functional>", cxx11},
5463 {"hash", "<functional>", cxx11},
5464 {"mem_fn", "<functional>", cxx11},
5465 /* <future>. */
5466 {"async", "<future>", cxx11},
5467 {"future", "<future>", cxx11},
5468 {"packaged_task", "<future>", cxx11},
5469 {"promise", "<future>", cxx11},
5470 /* <iostream>. */
5471 {"cin", "<iostream>", cxx98},
5472 {"cout", "<iostream>", cxx98},
5473 {"cerr", "<iostream>", cxx98},
5474 {"clog", "<iostream>", cxx98},
5475 {"wcin", "<iostream>", cxx98},
5476 {"wcout", "<iostream>", cxx98},
5477 {"wclog", "<iostream>", cxx98},
5478 /* <istream>. */
5479 {"istream", "<istream>", cxx98},
5480 /* <iterator>. */
5481 {"advance", "<iterator>", cxx98},
5482 {"back_inserter", "<iterator>", cxx98},
5483 {"begin", "<iterator>", cxx11},
5484 {"distance", "<iterator>", cxx98},
5485 {"end", "<iterator>", cxx11},
5486 {"front_inserter", "<iterator>", cxx98},
5487 {"inserter", "<iterator>", cxx98},
5488 {"istream_iterator", "<iterator>", cxx98},
5489 {"istreambuf_iterator", "<iterator>", cxx98},
5490 {"iterator_traits", "<iterator>", cxx98},
5491 {"move_iterator", "<iterator>", cxx11},
5492 {"next", "<iterator>", cxx11},
5493 {"ostream_iterator", "<iterator>", cxx98},
5494 {"ostreambuf_iterator", "<iterator>", cxx98},
5495 {"prev", "<iterator>", cxx11},
5496 {"reverse_iterator", "<iterator>", cxx98},
5497 /* <ostream>. */
5498 {"ostream", "<ostream>", cxx98},
5499 /* <list>. */
5500 {"list", "<list>", cxx98},
5501 /* <map>. */
5502 {"map", "<map>", cxx98},
5503 {"multimap", "<map>", cxx98},
5504 /* <memory>. */
5505 {"make_shared", "<memory>", cxx11},
5506 {"make_unique", "<memory>", cxx11},
5507 {"shared_ptr", "<memory>", cxx11},
5508 {"unique_ptr", "<memory>", cxx11},
5509 {"weak_ptr", "<memory>", cxx11},
5510 /* <mutex>. */
5511 {"mutex", "<mutex>", cxx11},
5512 {"timed_mutex", "<mutex>", cxx11},
5513 {"recursive_mutex", "<mutex>", cxx11},
5514 {"recursive_timed_mutex", "<mutex>", cxx11},
5515 {"once_flag", "<mutex>", cxx11},
5516 {"call_once,", "<mutex>", cxx11},
5517 {"lock", "<mutex>", cxx11},
5518 {"scoped_lock", "<mutex>", cxx17},
5519 {"try_lock", "<mutex>", cxx11},
5520 {"lock_guard", "<mutex>", cxx11},
5521 {"unique_lock", "<mutex>", cxx11},
5522 /* <optional>. */
5523 {"optional", "<optional>", cxx17},
5524 {"make_optional", "<optional>", cxx17},
5525 /* <ostream>. */
5526 {"ostream", "<ostream>", cxx98},
5527 {"wostream", "<ostream>", cxx98},
5528 {"ends", "<ostream>", cxx98},
5529 {"flush", "<ostream>", cxx98},
5530 {"endl", "<ostream>", cxx98},
5531 /* <queue>. */
5532 {"queue", "<queue>", cxx98},
5533 {"priority_queue", "<queue>", cxx98},
5534 /* <set>. */
5535 {"set", "<set>", cxx98},
5536 {"multiset", "<set>", cxx98},
5537 /* <shared_mutex>. */
5538 {"shared_lock", "<shared_mutex>", cxx14},
5539 {"shared_mutex", "<shared_mutex>", cxx17},
5540 {"shared_timed_mutex", "<shared_mutex>", cxx14},
5541 /* <sstream>. */
5542 {"basic_stringbuf", "<sstream>", cxx98},
5543 {"basic_istringstream", "<sstream>", cxx98},
5544 {"basic_ostringstream", "<sstream>", cxx98},
5545 {"basic_stringstream", "<sstream>", cxx98},
5546 {"istringstream", "<sstream>", cxx98},
5547 {"ostringstream", "<sstream>", cxx98},
5548 {"stringstream", "<sstream>", cxx98},
5549 /* <stack>. */
5550 {"stack", "<stack>", cxx98},
5551 /* <string>. */
5552 {"basic_string", "<string>", cxx98},
5553 {"string", "<string>", cxx98},
5554 {"wstring", "<string>", cxx98},
5555 {"u16string", "<string>", cxx11},
5556 {"u32string", "<string>", cxx11},
5557 /* <string_view>. */
5558 {"string_view", "<string_view>", cxx17},
5559 /* <thread>. */
5560 {"thread", "<thread>", cxx11},
5561 /* <tuple>. */
5562 {"make_tuple", "<tuple>", cxx11},
5563 {"tuple", "<tuple>", cxx11},
5564 {"tuple_element", "<tuple>", cxx11},
5565 {"tuple_size", "<tuple>", cxx11},
5566 /* <unordered_map>. */
5567 {"unordered_map", "<unordered_map>", cxx11},
5568 {"unordered_multimap", "<unordered_map>", cxx11},
5569 /* <unordered_set>. */
5570 {"unordered_set", "<unordered_set>", cxx11},
5571 {"unordered_multiset", "<unordered_set>", cxx11},
5572 /* <utility>. */
5573 {"declval", "<utility>", cxx11},
5574 {"forward", "<utility>", cxx11},
5575 {"make_pair", "<utility>", cxx98},
5576 {"move", "<utility>", cxx11},
5577 {"pair", "<utility>", cxx98},
5578 /* <variant>. */
5579 {"variant", "<variant>", cxx17},
5580 {"visit", "<variant>", cxx17},
5581 /* <vector>. */
5582 {"vector", "<vector>", cxx98},
5584 const size_t num_hints = sizeof (hints) / sizeof (hints[0]);
5585 for (size_t i = 0; i < num_hints; i++)
5587 if (strcmp (name, hints[i].name) == 0)
5588 return &hints[i];
5590 return NULL;
5593 /* Describe DIALECT. */
5595 static const char *
5596 get_cxx_dialect_name (enum cxx_dialect dialect)
5598 switch (dialect)
5600 default:
5601 gcc_unreachable ();
5602 case cxx98:
5603 return "C++98";
5604 case cxx11:
5605 return "C++11";
5606 case cxx14:
5607 return "C++14";
5608 case cxx17:
5609 return "C++17";
5610 case cxx2a:
5611 return "C++2a";
5615 /* Suggest pertinent header files for NAME at LOCATION, for common
5616 names within the "std" namespace.
5617 Return true iff a suggestion was offered. */
5619 static bool
5620 maybe_suggest_missing_std_header (location_t location, tree name)
5622 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
5624 const char *name_str = IDENTIFIER_POINTER (name);
5625 const std_name_hint *header_hint = get_std_name_hint (name_str);
5626 if (!header_hint)
5627 return false;
5629 gcc_rich_location richloc (location);
5630 if (cxx_dialect >= header_hint->min_dialect)
5632 const char *header = header_hint->header;
5633 maybe_add_include_fixit (&richloc, header);
5634 inform (&richloc,
5635 "%<std::%s%> is defined in header %qs;"
5636 " did you forget to %<#include %s%>?",
5637 name_str, header, header);
5639 else
5641 inform (&richloc,
5642 "%<std::%s%> is only available from %s onwards",
5643 name_str, get_cxx_dialect_name (header_hint->min_dialect));
5645 return true;
5648 /* If SCOPE is the "std" namespace, then suggest pertinent header
5649 files for NAME at LOCATION.
5650 Return true iff a suggestion was offered. */
5652 static bool
5653 maybe_suggest_missing_header (location_t location, tree name, tree scope)
5655 if (scope == NULL_TREE)
5656 return false;
5657 if (TREE_CODE (scope) != NAMESPACE_DECL)
5658 return false;
5659 /* We only offer suggestions for the "std" namespace. */
5660 if (scope != std_node)
5661 return false;
5662 return maybe_suggest_missing_std_header (location, name);
5665 /* Look for alternatives for NAME, an IDENTIFIER_NODE for which name
5666 lookup failed within the explicitly provided SCOPE. Suggest the
5667 the best meaningful candidates (if any) as a fix-it hint.
5668 Return true iff a suggestion was provided. */
5670 bool
5671 suggest_alternative_in_explicit_scope (location_t location, tree name,
5672 tree scope)
5674 /* Something went very wrong; don't suggest anything. */
5675 if (name == error_mark_node)
5676 return false;
5678 /* Resolve any namespace aliases. */
5679 scope = ORIGINAL_NAMESPACE (scope);
5681 if (maybe_suggest_missing_header (location, name, scope))
5682 return true;
5684 cp_binding_level *level = NAMESPACE_LEVEL (scope);
5686 best_match <tree, const char *> bm (name);
5687 consider_binding_level (name, bm, level, false, FUZZY_LOOKUP_NAME);
5689 /* See if we have a good suggesion for the user. */
5690 const char *fuzzy_name = bm.get_best_meaningful_candidate ();
5691 if (fuzzy_name)
5693 gcc_rich_location richloc (location);
5694 richloc.add_fixit_replace (fuzzy_name);
5695 inform (&richloc, "suggested alternative: %qs",
5696 fuzzy_name);
5697 return true;
5700 return false;
5703 /* Look up NAME (an IDENTIFIER_NODE) in SCOPE (either a NAMESPACE_DECL
5704 or a class TYPE).
5706 If PREFER_TYPE is > 0, we only return TYPE_DECLs or namespaces.
5707 If PREFER_TYPE is > 1, we only return TYPE_DECLs.
5709 Returns a DECL (or OVERLOAD, or BASELINK) representing the
5710 declaration found. If no suitable declaration can be found,
5711 ERROR_MARK_NODE is returned. If COMPLAIN is true and SCOPE is
5712 neither a class-type nor a namespace a diagnostic is issued. */
5714 tree
5715 lookup_qualified_name (tree scope, tree name, int prefer_type, bool complain,
5716 bool find_hidden)
5718 tree t = NULL_TREE;
5720 if (TREE_CODE (scope) == NAMESPACE_DECL)
5722 int flags = lookup_flags (prefer_type, /*namespaces_only*/false);
5723 if (find_hidden)
5724 flags |= LOOKUP_HIDDEN;
5725 name_lookup lookup (name, flags);
5727 if (qualified_namespace_lookup (scope, &lookup))
5728 t = lookup.value;
5730 else if (cxx_dialect != cxx98 && TREE_CODE (scope) == ENUMERAL_TYPE)
5731 t = lookup_enumerator (scope, name);
5732 else if (is_class_type (scope, complain))
5733 t = lookup_member (scope, name, 2, prefer_type, tf_warning_or_error);
5735 if (!t)
5736 return error_mark_node;
5737 return t;
5740 /* [namespace.qual]
5741 Accepts the NAME to lookup and its qualifying SCOPE.
5742 Returns the name/type pair found into the cxx_binding *RESULT,
5743 or false on error. */
5745 static bool
5746 qualified_namespace_lookup (tree scope, name_lookup *lookup)
5748 timevar_start (TV_NAME_LOOKUP);
5749 query_oracle (lookup->name);
5750 bool found = lookup->search_qualified (ORIGINAL_NAMESPACE (scope));
5751 timevar_stop (TV_NAME_LOOKUP);
5752 return found;
5755 /* Helper function for lookup_name_fuzzy.
5756 Traverse binding level LVL, looking for good name matches for NAME
5757 (and BM). */
5758 static void
5759 consider_binding_level (tree name, best_match <tree, const char *> &bm,
5760 cp_binding_level *lvl, bool look_within_fields,
5761 enum lookup_name_fuzzy_kind kind)
5763 if (look_within_fields)
5764 if (lvl->this_entity && TREE_CODE (lvl->this_entity) == RECORD_TYPE)
5766 tree type = lvl->this_entity;
5767 bool want_type_p = (kind == FUZZY_LOOKUP_TYPENAME);
5768 tree best_matching_field
5769 = lookup_member_fuzzy (type, name, want_type_p);
5770 if (best_matching_field)
5771 bm.consider (IDENTIFIER_POINTER (best_matching_field));
5774 /* Only suggest names reserved for the implementation if NAME begins
5775 with an underscore. */
5776 bool consider_implementation_names = (IDENTIFIER_POINTER (name)[0] == '_');
5778 for (tree t = lvl->names; t; t = TREE_CHAIN (t))
5780 tree d = t;
5782 /* OVERLOADs or decls from using declaration are wrapped into
5783 TREE_LIST. */
5784 if (TREE_CODE (d) == TREE_LIST)
5785 d = OVL_FIRST (TREE_VALUE (d));
5787 /* Don't use bindings from implicitly declared functions,
5788 as they were likely misspellings themselves. */
5789 if (TREE_TYPE (d) == error_mark_node)
5790 continue;
5792 /* Skip anticipated decls of builtin functions. */
5793 if (TREE_CODE (d) == FUNCTION_DECL
5794 && DECL_BUILT_IN (d)
5795 && DECL_ANTICIPATED (d))
5796 continue;
5798 /* Skip compiler-generated variables (e.g. __for_begin/__for_end
5799 within range for). */
5800 if (TREE_CODE (d) == VAR_DECL
5801 && DECL_ARTIFICIAL (d))
5802 continue;
5804 tree suggestion = DECL_NAME (d);
5805 if (!suggestion)
5806 continue;
5808 /* Don't suggest names that are for anonymous aggregate types, as
5809 they are an implementation detail generated by the compiler. */
5810 if (anon_aggrname_p (suggestion))
5811 continue;
5813 const char *suggestion_str = IDENTIFIER_POINTER (suggestion);
5815 /* Ignore internal names with spaces in them. */
5816 if (strchr (suggestion_str, ' '))
5817 continue;
5819 /* Don't suggest names that are reserved for use by the
5820 implementation, unless NAME began with an underscore. */
5821 if (name_reserved_for_implementation_p (suggestion_str)
5822 && !consider_implementation_names)
5823 continue;
5825 bm.consider (suggestion_str);
5829 /* Subclass of deferred_diagnostic. Notify the user that the
5830 given macro was used before it was defined.
5831 This can be done in the C++ frontend since tokenization happens
5832 upfront. */
5834 class macro_use_before_def : public deferred_diagnostic
5836 public:
5837 /* Factory function. Return a new macro_use_before_def instance if
5838 appropriate, or return NULL. */
5839 static macro_use_before_def *
5840 maybe_make (location_t use_loc, cpp_hashnode *macro)
5842 source_location def_loc = cpp_macro_definition_location (macro);
5843 if (def_loc == UNKNOWN_LOCATION)
5844 return NULL;
5846 /* We only want to issue a note if the macro was used *before* it was
5847 defined.
5848 We don't want to issue a note for cases where a macro was incorrectly
5849 used, leaving it unexpanded (e.g. by using the wrong argument
5850 count). */
5851 if (!linemap_location_before_p (line_table, use_loc, def_loc))
5852 return NULL;
5854 return new macro_use_before_def (use_loc, macro);
5857 private:
5858 /* Ctor. LOC is the location of the usage. MACRO is the
5859 macro that was used. */
5860 macro_use_before_def (location_t loc, cpp_hashnode *macro)
5861 : deferred_diagnostic (loc), m_macro (macro)
5863 gcc_assert (macro);
5866 ~macro_use_before_def ()
5868 if (is_suppressed_p ())
5869 return;
5871 inform (get_location (), "the macro %qs had not yet been defined",
5872 (const char *)m_macro->ident.str);
5873 inform (cpp_macro_definition_location (m_macro),
5874 "it was later defined here");
5877 private:
5878 cpp_hashnode *m_macro;
5881 /* Determine if it can ever make sense to offer RID as a suggestion for
5882 a misspelling.
5884 Subroutine of lookup_name_fuzzy. */
5886 static bool
5887 suggest_rid_p (enum rid rid)
5889 switch (rid)
5891 /* Support suggesting function-like keywords. */
5892 case RID_STATIC_ASSERT:
5893 return true;
5895 default:
5896 /* Support suggesting the various decl-specifier words, to handle
5897 e.g. "singed" vs "signed" typos. */
5898 if (cp_keyword_starts_decl_specifier_p (rid))
5899 return true;
5901 /* Otherwise, don't offer it. This avoids suggesting e.g. "if"
5902 and "do" for short misspellings, which are likely to lead to
5903 nonsensical results. */
5904 return false;
5908 /* Search for near-matches for NAME within the current bindings, and within
5909 macro names, returning the best match as a const char *, or NULL if
5910 no reasonable match is found.
5912 Use LOC for any deferred diagnostics. */
5914 name_hint
5915 lookup_name_fuzzy (tree name, enum lookup_name_fuzzy_kind kind, location_t loc)
5917 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
5919 /* First, try some well-known names in the C++ standard library, in case
5920 the user forgot a #include. */
5921 const char *header_hint
5922 = get_cp_stdlib_header_for_name (IDENTIFIER_POINTER (name));
5923 if (header_hint)
5924 return name_hint (NULL,
5925 new suggest_missing_header (loc,
5926 IDENTIFIER_POINTER (name),
5927 header_hint));
5929 best_match <tree, const char *> bm (name);
5931 cp_binding_level *lvl;
5932 for (lvl = scope_chain->class_bindings; lvl; lvl = lvl->level_chain)
5933 consider_binding_level (name, bm, lvl, true, kind);
5935 for (lvl = current_binding_level; lvl; lvl = lvl->level_chain)
5936 consider_binding_level (name, bm, lvl, false, kind);
5938 /* Consider macros: if the user misspelled a macro name e.g. "SOME_MACRO"
5940 x = SOME_OTHER_MACRO (y);
5941 then "SOME_OTHER_MACRO" will survive to the frontend and show up
5942 as a misspelled identifier.
5944 Use the best distance so far so that a candidate is only set if
5945 a macro is better than anything so far. This allows early rejection
5946 (without calculating the edit distance) of macro names that must have
5947 distance >= bm.get_best_distance (), and means that we only get a
5948 non-NULL result for best_macro_match if it's better than any of
5949 the identifiers already checked. */
5950 best_macro_match bmm (name, bm.get_best_distance (), parse_in);
5951 cpp_hashnode *best_macro = bmm.get_best_meaningful_candidate ();
5952 /* If a macro is the closest so far to NAME, consider it. */
5953 if (best_macro)
5954 bm.consider ((const char *)best_macro->ident.str);
5955 else if (bmm.get_best_distance () == 0)
5957 /* If we have an exact match for a macro name, then either the
5958 macro was used with the wrong argument count, or the macro
5959 has been used before it was defined. */
5960 if (cpp_hashnode *macro = bmm.blithely_get_best_candidate ())
5961 if (cpp_user_macro_p (macro))
5962 return name_hint (NULL,
5963 macro_use_before_def::maybe_make (loc, macro));
5966 /* Try the "starts_decl_specifier_p" keywords to detect
5967 "singed" vs "signed" typos. */
5968 for (unsigned i = 0; i < num_c_common_reswords; i++)
5970 const c_common_resword *resword = &c_common_reswords[i];
5972 if (!suggest_rid_p (resword->rid))
5973 continue;
5975 tree resword_identifier = ridpointers [resword->rid];
5976 if (!resword_identifier)
5977 continue;
5978 gcc_assert (TREE_CODE (resword_identifier) == IDENTIFIER_NODE);
5980 /* Only consider reserved words that survived the
5981 filtering in init_reswords (e.g. for -std). */
5982 if (!IDENTIFIER_KEYWORD_P (resword_identifier))
5983 continue;
5985 bm.consider (IDENTIFIER_POINTER (resword_identifier));
5988 return name_hint (bm.get_best_meaningful_candidate (), NULL);
5991 /* Subroutine of outer_binding.
5993 Returns TRUE if BINDING is a binding to a template parameter of
5994 SCOPE. In that case SCOPE is the scope of a primary template
5995 parameter -- in the sense of G++, i.e, a template that has its own
5996 template header.
5998 Returns FALSE otherwise. */
6000 static bool
6001 binding_to_template_parms_of_scope_p (cxx_binding *binding,
6002 cp_binding_level *scope)
6004 tree binding_value, tmpl, tinfo;
6005 int level;
6007 if (!binding || !scope || !scope->this_entity)
6008 return false;
6010 binding_value = binding->value ? binding->value : binding->type;
6011 tinfo = get_template_info (scope->this_entity);
6013 /* BINDING_VALUE must be a template parm. */
6014 if (binding_value == NULL_TREE
6015 || (!DECL_P (binding_value)
6016 || !DECL_TEMPLATE_PARM_P (binding_value)))
6017 return false;
6019 /* The level of BINDING_VALUE. */
6020 level =
6021 template_type_parameter_p (binding_value)
6022 ? TEMPLATE_PARM_LEVEL (TEMPLATE_TYPE_PARM_INDEX
6023 (TREE_TYPE (binding_value)))
6024 : TEMPLATE_PARM_LEVEL (DECL_INITIAL (binding_value));
6026 /* The template of the current scope, iff said scope is a primary
6027 template. */
6028 tmpl = (tinfo
6029 && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
6030 ? TI_TEMPLATE (tinfo)
6031 : NULL_TREE);
6033 /* If the level of the parm BINDING_VALUE equals the depth of TMPL,
6034 then BINDING_VALUE is a parameter of TMPL. */
6035 return (tmpl && level == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)));
6038 /* Return the innermost non-namespace binding for NAME from a scope
6039 containing BINDING, or, if BINDING is NULL, the current scope.
6040 Please note that for a given template, the template parameters are
6041 considered to be in the scope containing the current scope.
6042 If CLASS_P is false, then class bindings are ignored. */
6044 cxx_binding *
6045 outer_binding (tree name,
6046 cxx_binding *binding,
6047 bool class_p)
6049 cxx_binding *outer;
6050 cp_binding_level *scope;
6051 cp_binding_level *outer_scope;
6053 if (binding)
6055 scope = binding->scope->level_chain;
6056 outer = binding->previous;
6058 else
6060 scope = current_binding_level;
6061 outer = IDENTIFIER_BINDING (name);
6063 outer_scope = outer ? outer->scope : NULL;
6065 /* Because we create class bindings lazily, we might be missing a
6066 class binding for NAME. If there are any class binding levels
6067 between the LAST_BINDING_LEVEL and the scope in which OUTER was
6068 declared, we must lookup NAME in those class scopes. */
6069 if (class_p)
6070 while (scope && scope != outer_scope && scope->kind != sk_namespace)
6072 if (scope->kind == sk_class)
6074 cxx_binding *class_binding;
6076 class_binding = get_class_binding (name, scope);
6077 if (class_binding)
6079 /* Thread this new class-scope binding onto the
6080 IDENTIFIER_BINDING list so that future lookups
6081 find it quickly. */
6082 class_binding->previous = outer;
6083 if (binding)
6084 binding->previous = class_binding;
6085 else
6086 IDENTIFIER_BINDING (name) = class_binding;
6087 return class_binding;
6090 /* If we are in a member template, the template parms of the member
6091 template are considered to be inside the scope of the containing
6092 class, but within G++ the class bindings are all pushed between the
6093 template parms and the function body. So if the outer binding is
6094 a template parm for the current scope, return it now rather than
6095 look for a class binding. */
6096 if (outer_scope && outer_scope->kind == sk_template_parms
6097 && binding_to_template_parms_of_scope_p (outer, scope))
6098 return outer;
6100 scope = scope->level_chain;
6103 return outer;
6106 /* Return the innermost block-scope or class-scope value binding for
6107 NAME, or NULL_TREE if there is no such binding. */
6109 tree
6110 innermost_non_namespace_value (tree name)
6112 cxx_binding *binding;
6113 binding = outer_binding (name, /*binding=*/NULL, /*class_p=*/true);
6114 return binding ? binding->value : NULL_TREE;
6117 /* Look up NAME in the current binding level and its superiors in the
6118 namespace of variables, functions and typedefs. Return a ..._DECL
6119 node of some kind representing its definition if there is only one
6120 such declaration, or return a TREE_LIST with all the overloaded
6121 definitions if there are many, or return 0 if it is undefined.
6122 Hidden name, either friend declaration or built-in function, are
6123 not ignored.
6125 If PREFER_TYPE is > 0, we prefer TYPE_DECLs or namespaces.
6126 If PREFER_TYPE is > 1, we reject non-type decls (e.g. namespaces).
6127 Otherwise we prefer non-TYPE_DECLs.
6129 If NONCLASS is nonzero, bindings in class scopes are ignored. If
6130 BLOCK_P is false, bindings in block scopes are ignored. */
6132 static tree
6133 lookup_name_real_1 (tree name, int prefer_type, int nonclass, bool block_p,
6134 int namespaces_only, int flags)
6136 cxx_binding *iter;
6137 tree val = NULL_TREE;
6139 query_oracle (name);
6141 /* Conversion operators are handled specially because ordinary
6142 unqualified name lookup will not find template conversion
6143 operators. */
6144 if (IDENTIFIER_CONV_OP_P (name))
6146 cp_binding_level *level;
6148 for (level = current_binding_level;
6149 level && level->kind != sk_namespace;
6150 level = level->level_chain)
6152 tree class_type;
6153 tree operators;
6155 /* A conversion operator can only be declared in a class
6156 scope. */
6157 if (level->kind != sk_class)
6158 continue;
6160 /* Lookup the conversion operator in the class. */
6161 class_type = level->this_entity;
6162 operators = lookup_fnfields (class_type, name, /*protect=*/0);
6163 if (operators)
6164 return operators;
6167 return NULL_TREE;
6170 flags |= lookup_flags (prefer_type, namespaces_only);
6172 /* First, look in non-namespace scopes. */
6174 if (current_class_type == NULL_TREE)
6175 nonclass = 1;
6177 if (block_p || !nonclass)
6178 for (iter = outer_binding (name, NULL, !nonclass);
6179 iter;
6180 iter = outer_binding (name, iter, !nonclass))
6182 tree binding;
6184 /* Skip entities we don't want. */
6185 if (LOCAL_BINDING_P (iter) ? !block_p : nonclass)
6186 continue;
6188 /* If this is the kind of thing we're looking for, we're done. */
6189 if (qualify_lookup (iter->value, flags))
6190 binding = iter->value;
6191 else if ((flags & LOOKUP_PREFER_TYPES)
6192 && qualify_lookup (iter->type, flags))
6193 binding = iter->type;
6194 else
6195 binding = NULL_TREE;
6197 if (binding)
6199 if (TREE_CODE (binding) == TYPE_DECL && DECL_HIDDEN_P (binding))
6201 /* A non namespace-scope binding can only be hidden in the
6202 presence of a local class, due to friend declarations.
6204 In particular, consider:
6206 struct C;
6207 void f() {
6208 struct A {
6209 friend struct B;
6210 friend struct C;
6211 void g() {
6212 B* b; // error: B is hidden
6213 C* c; // OK, finds ::C
6216 B *b; // error: B is hidden
6217 C *c; // OK, finds ::C
6218 struct B {};
6219 B *bb; // OK
6222 The standard says that "B" is a local class in "f"
6223 (but not nested within "A") -- but that name lookup
6224 for "B" does not find this declaration until it is
6225 declared directly with "f".
6227 In particular:
6229 [class.friend]
6231 If a friend declaration appears in a local class and
6232 the name specified is an unqualified name, a prior
6233 declaration is looked up without considering scopes
6234 that are outside the innermost enclosing non-class
6235 scope. For a friend function declaration, if there is
6236 no prior declaration, the program is ill-formed. For a
6237 friend class declaration, if there is no prior
6238 declaration, the class that is specified belongs to the
6239 innermost enclosing non-class scope, but if it is
6240 subsequently referenced, its name is not found by name
6241 lookup until a matching declaration is provided in the
6242 innermost enclosing nonclass scope.
6244 So just keep looking for a non-hidden binding.
6246 gcc_assert (TREE_CODE (binding) == TYPE_DECL);
6247 continue;
6249 val = binding;
6250 break;
6254 /* Now lookup in namespace scopes. */
6255 if (!val)
6257 name_lookup lookup (name, flags);
6258 if (lookup.search_unqualified
6259 (current_decl_namespace (), current_binding_level))
6260 val = lookup.value;
6263 /* If we have a single function from a using decl, pull it out. */
6264 if (val && TREE_CODE (val) == OVERLOAD && !really_overloaded_fn (val))
6265 val = OVL_FUNCTION (val);
6267 return val;
6270 /* Wrapper for lookup_name_real_1. */
6272 tree
6273 lookup_name_real (tree name, int prefer_type, int nonclass, bool block_p,
6274 int namespaces_only, int flags)
6276 tree ret;
6277 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6278 ret = lookup_name_real_1 (name, prefer_type, nonclass, block_p,
6279 namespaces_only, flags);
6280 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6281 return ret;
6284 tree
6285 lookup_name_nonclass (tree name)
6287 return lookup_name_real (name, 0, 1, /*block_p=*/true, 0, 0);
6290 tree
6291 lookup_name (tree name)
6293 return lookup_name_real (name, 0, 0, /*block_p=*/true, 0, 0);
6296 tree
6297 lookup_name_prefer_type (tree name, int prefer_type)
6299 return lookup_name_real (name, prefer_type, 0, /*block_p=*/true, 0, 0);
6302 /* Look up NAME for type used in elaborated name specifier in
6303 the scopes given by SCOPE. SCOPE can be either TS_CURRENT or
6304 TS_WITHIN_ENCLOSING_NON_CLASS. Although not implied by the
6305 name, more scopes are checked if cleanup or template parameter
6306 scope is encountered.
6308 Unlike lookup_name_real, we make sure that NAME is actually
6309 declared in the desired scope, not from inheritance, nor using
6310 directive. For using declaration, there is DR138 still waiting
6311 to be resolved. Hidden name coming from an earlier friend
6312 declaration is also returned.
6314 A TYPE_DECL best matching the NAME is returned. Catching error
6315 and issuing diagnostics are caller's responsibility. */
6317 static tree
6318 lookup_type_scope_1 (tree name, tag_scope scope)
6320 cxx_binding *iter = NULL;
6321 tree val = NULL_TREE;
6322 cp_binding_level *level = NULL;
6324 /* Look in non-namespace scope first. */
6325 if (current_binding_level->kind != sk_namespace)
6326 iter = outer_binding (name, NULL, /*class_p=*/ true);
6327 for (; iter; iter = outer_binding (name, iter, /*class_p=*/ true))
6329 /* Check if this is the kind of thing we're looking for.
6330 If SCOPE is TS_CURRENT, also make sure it doesn't come from
6331 base class. For ITER->VALUE, we can simply use
6332 INHERITED_VALUE_BINDING_P. For ITER->TYPE, we have to use
6333 our own check.
6335 We check ITER->TYPE before ITER->VALUE in order to handle
6336 typedef struct C {} C;
6337 correctly. */
6339 if (qualify_lookup (iter->type, LOOKUP_PREFER_TYPES)
6340 && (scope != ts_current
6341 || LOCAL_BINDING_P (iter)
6342 || DECL_CONTEXT (iter->type) == iter->scope->this_entity))
6343 val = iter->type;
6344 else if ((scope != ts_current
6345 || !INHERITED_VALUE_BINDING_P (iter))
6346 && qualify_lookup (iter->value, LOOKUP_PREFER_TYPES))
6347 val = iter->value;
6349 if (val)
6350 break;
6353 /* Look in namespace scope. */
6354 if (val)
6355 level = iter->scope;
6356 else
6358 tree ns = current_decl_namespace ();
6360 if (tree *slot = find_namespace_slot (ns, name))
6362 /* If this is the kind of thing we're looking for, we're done. */
6363 if (tree type = MAYBE_STAT_TYPE (*slot))
6364 if (qualify_lookup (type, LOOKUP_PREFER_TYPES))
6365 val = type;
6366 if (!val)
6368 if (tree decl = MAYBE_STAT_DECL (*slot))
6369 if (qualify_lookup (decl, LOOKUP_PREFER_TYPES))
6370 val = decl;
6372 level = NAMESPACE_LEVEL (ns);
6376 /* Type found, check if it is in the allowed scopes, ignoring cleanup
6377 and template parameter scopes. */
6378 if (val)
6380 cp_binding_level *b = current_binding_level;
6381 while (b)
6383 if (level == b)
6384 return val;
6386 if (b->kind == sk_cleanup || b->kind == sk_template_parms
6387 || b->kind == sk_function_parms)
6388 b = b->level_chain;
6389 else if (b->kind == sk_class
6390 && scope == ts_within_enclosing_non_class)
6391 b = b->level_chain;
6392 else
6393 break;
6397 return NULL_TREE;
6400 /* Wrapper for lookup_type_scope_1. */
6402 tree
6403 lookup_type_scope (tree name, tag_scope scope)
6405 tree ret;
6406 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6407 ret = lookup_type_scope_1 (name, scope);
6408 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6409 return ret;
6412 /* Returns true iff DECL is a block-scope extern declaration of a function
6413 or variable. */
6415 bool
6416 is_local_extern (tree decl)
6418 cxx_binding *binding;
6420 /* For functions, this is easy. */
6421 if (TREE_CODE (decl) == FUNCTION_DECL)
6422 return DECL_LOCAL_FUNCTION_P (decl);
6424 if (!VAR_P (decl))
6425 return false;
6426 if (!current_function_decl)
6427 return false;
6429 /* For variables, this is not easy. We need to look at the binding stack
6430 for the identifier to see whether the decl we have is a local. */
6431 for (binding = IDENTIFIER_BINDING (DECL_NAME (decl));
6432 binding && binding->scope->kind != sk_namespace;
6433 binding = binding->previous)
6434 if (binding->value == decl)
6435 return LOCAL_BINDING_P (binding);
6437 return false;
6440 /* The type TYPE is being declared. If it is a class template, or a
6441 specialization of a class template, do any processing required and
6442 perform error-checking. If IS_FRIEND is nonzero, this TYPE is
6443 being declared a friend. B is the binding level at which this TYPE
6444 should be bound.
6446 Returns the TYPE_DECL for TYPE, which may have been altered by this
6447 processing. */
6449 static tree
6450 maybe_process_template_type_declaration (tree type, int is_friend,
6451 cp_binding_level *b)
6453 tree decl = TYPE_NAME (type);
6455 if (processing_template_parmlist)
6456 /* You can't declare a new template type in a template parameter
6457 list. But, you can declare a non-template type:
6459 template <class A*> struct S;
6461 is a forward-declaration of `A'. */
6463 else if (b->kind == sk_namespace
6464 && current_binding_level->kind != sk_namespace)
6465 /* If this new type is being injected into a containing scope,
6466 then it's not a template type. */
6468 else
6470 gcc_assert (MAYBE_CLASS_TYPE_P (type)
6471 || TREE_CODE (type) == ENUMERAL_TYPE);
6473 if (processing_template_decl)
6475 /* This may change after the call to
6476 push_template_decl_real, but we want the original value. */
6477 tree name = DECL_NAME (decl);
6479 decl = push_template_decl_real (decl, is_friend);
6480 if (decl == error_mark_node)
6481 return error_mark_node;
6483 /* If the current binding level is the binding level for the
6484 template parameters (see the comment in
6485 begin_template_parm_list) and the enclosing level is a class
6486 scope, and we're not looking at a friend, push the
6487 declaration of the member class into the class scope. In the
6488 friend case, push_template_decl will already have put the
6489 friend into global scope, if appropriate. */
6490 if (TREE_CODE (type) != ENUMERAL_TYPE
6491 && !is_friend && b->kind == sk_template_parms
6492 && b->level_chain->kind == sk_class)
6494 finish_member_declaration (CLASSTYPE_TI_TEMPLATE (type));
6496 if (!COMPLETE_TYPE_P (current_class_type))
6498 maybe_add_class_template_decl_list (current_class_type,
6499 type, /*friend_p=*/0);
6500 /* Put this UTD in the table of UTDs for the class. */
6501 if (CLASSTYPE_NESTED_UTDS (current_class_type) == NULL)
6502 CLASSTYPE_NESTED_UTDS (current_class_type) =
6503 binding_table_new (SCOPE_DEFAULT_HT_SIZE);
6505 binding_table_insert
6506 (CLASSTYPE_NESTED_UTDS (current_class_type), name, type);
6512 return decl;
6515 /* Push a tag name NAME for struct/class/union/enum type TYPE. In case
6516 that the NAME is a class template, the tag is processed but not pushed.
6518 The pushed scope depend on the SCOPE parameter:
6519 - When SCOPE is TS_CURRENT, put it into the inner-most non-sk_cleanup
6520 scope.
6521 - When SCOPE is TS_GLOBAL, put it in the inner-most non-class and
6522 non-template-parameter scope. This case is needed for forward
6523 declarations.
6524 - When SCOPE is TS_WITHIN_ENCLOSING_NON_CLASS, this is similar to
6525 TS_GLOBAL case except that names within template-parameter scopes
6526 are not pushed at all.
6528 Returns TYPE upon success and ERROR_MARK_NODE otherwise. */
6530 static tree
6531 do_pushtag (tree name, tree type, tag_scope scope)
6533 tree decl;
6535 cp_binding_level *b = current_binding_level;
6536 while (true)
6538 if (/* Cleanup scopes are not scopes from the point of view of
6539 the language. */
6540 b->kind == sk_cleanup
6541 /* Neither are function parameter scopes. */
6542 || b->kind == sk_function_parms
6543 /* Neither are the scopes used to hold template parameters
6544 for an explicit specialization. For an ordinary template
6545 declaration, these scopes are not scopes from the point of
6546 view of the language. */
6547 || (b->kind == sk_template_parms
6548 && (b->explicit_spec_p || scope == ts_global)))
6549 b = b->level_chain;
6550 else if (b->kind == sk_class
6551 && scope != ts_current)
6553 b = b->level_chain;
6554 if (b->kind == sk_template_parms)
6555 b = b->level_chain;
6557 else
6558 break;
6561 gcc_assert (identifier_p (name));
6563 /* Do C++ gratuitous typedefing. */
6564 if (identifier_type_value_1 (name) != type)
6566 tree tdef;
6567 int in_class = 0;
6568 tree context = TYPE_CONTEXT (type);
6570 if (! context)
6572 cp_binding_level *cb = b;
6573 while (cb->kind != sk_namespace
6574 && cb->kind != sk_class
6575 && (cb->kind != sk_function_parms
6576 || !cb->this_entity))
6577 cb = cb->level_chain;
6578 tree cs = cb->this_entity;
6580 gcc_checking_assert (TREE_CODE (cs) == FUNCTION_DECL
6581 ? cs == current_function_decl
6582 : TYPE_P (cs) ? cs == current_class_type
6583 : cs == current_namespace);
6585 if (scope == ts_current
6586 || (cs && TREE_CODE (cs) == FUNCTION_DECL))
6587 context = cs;
6588 else if (cs && TYPE_P (cs))
6589 /* When declaring a friend class of a local class, we want
6590 to inject the newly named class into the scope
6591 containing the local class, not the namespace
6592 scope. */
6593 context = decl_function_context (get_type_decl (cs));
6595 if (!context)
6596 context = current_namespace;
6598 if (b->kind == sk_class
6599 || (b->kind == sk_template_parms
6600 && b->level_chain->kind == sk_class))
6601 in_class = 1;
6603 tdef = create_implicit_typedef (name, type);
6604 DECL_CONTEXT (tdef) = FROB_CONTEXT (context);
6605 if (scope == ts_within_enclosing_non_class)
6607 /* This is a friend. Make this TYPE_DECL node hidden from
6608 ordinary name lookup. Its corresponding TEMPLATE_DECL
6609 will be marked in push_template_decl_real. */
6610 retrofit_lang_decl (tdef);
6611 DECL_ANTICIPATED (tdef) = 1;
6612 DECL_FRIEND_P (tdef) = 1;
6615 decl = maybe_process_template_type_declaration
6616 (type, scope == ts_within_enclosing_non_class, b);
6617 if (decl == error_mark_node)
6618 return decl;
6620 if (b->kind == sk_class)
6622 if (!TYPE_BEING_DEFINED (current_class_type))
6623 /* Don't push anywhere if the class is complete; a lambda in an
6624 NSDMI is not a member of the class. */
6626 else if (!PROCESSING_REAL_TEMPLATE_DECL_P ())
6627 /* Put this TYPE_DECL on the TYPE_FIELDS list for the
6628 class. But if it's a member template class, we want
6629 the TEMPLATE_DECL, not the TYPE_DECL, so this is done
6630 later. */
6631 finish_member_declaration (decl);
6632 else
6633 pushdecl_class_level (decl);
6635 else if (b->kind != sk_template_parms)
6637 decl = do_pushdecl_with_scope (decl, b, /*is_friend=*/false);
6638 if (decl == error_mark_node)
6639 return decl;
6641 if (DECL_CONTEXT (decl) == std_node
6642 && init_list_identifier == DECL_NAME (TYPE_NAME (type))
6643 && !CLASSTYPE_TEMPLATE_INFO (type))
6645 error ("declaration of %<std::initializer_list%> does not match "
6646 "%<#include <initializer_list>%>, isn't a template");
6647 return error_mark_node;
6651 if (! in_class)
6652 set_identifier_type_value_with_scope (name, tdef, b);
6654 TYPE_CONTEXT (type) = DECL_CONTEXT (decl);
6656 /* If this is a local class, keep track of it. We need this
6657 information for name-mangling, and so that it is possible to
6658 find all function definitions in a translation unit in a
6659 convenient way. (It's otherwise tricky to find a member
6660 function definition it's only pointed to from within a local
6661 class.) */
6662 if (TYPE_FUNCTION_SCOPE_P (type))
6664 if (processing_template_decl)
6666 /* Push a DECL_EXPR so we call pushtag at the right time in
6667 template instantiation rather than in some nested context. */
6668 add_decl_expr (decl);
6670 /* Lambdas use LAMBDA_EXPR_DISCRIMINATOR instead. */
6671 else if (!LAMBDA_TYPE_P (type))
6672 vec_safe_push (local_classes, type);
6676 if (b->kind == sk_class
6677 && !COMPLETE_TYPE_P (current_class_type))
6679 maybe_add_class_template_decl_list (current_class_type,
6680 type, /*friend_p=*/0);
6682 if (CLASSTYPE_NESTED_UTDS (current_class_type) == NULL)
6683 CLASSTYPE_NESTED_UTDS (current_class_type)
6684 = binding_table_new (SCOPE_DEFAULT_HT_SIZE);
6686 binding_table_insert
6687 (CLASSTYPE_NESTED_UTDS (current_class_type), name, type);
6690 decl = TYPE_NAME (type);
6691 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
6693 /* Set type visibility now if this is a forward declaration. */
6694 TREE_PUBLIC (decl) = 1;
6695 determine_visibility (decl);
6697 return type;
6700 /* Wrapper for do_pushtag. */
6702 tree
6703 pushtag (tree name, tree type, tag_scope scope)
6705 tree ret;
6706 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6707 ret = do_pushtag (name, type, scope);
6708 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6709 return ret;
6713 /* Subroutines for reverting temporarily to top-level for instantiation
6714 of templates and such. We actually need to clear out the class- and
6715 local-value slots of all identifiers, so that only the global values
6716 are at all visible. Simply setting current_binding_level to the global
6717 scope isn't enough, because more binding levels may be pushed. */
6718 struct saved_scope *scope_chain;
6720 /* Return true if ID has not already been marked. */
6722 static inline bool
6723 store_binding_p (tree id)
6725 if (!id || !IDENTIFIER_BINDING (id))
6726 return false;
6728 if (IDENTIFIER_MARKED (id))
6729 return false;
6731 return true;
6734 /* Add an appropriate binding to *OLD_BINDINGS which needs to already
6735 have enough space reserved. */
6737 static void
6738 store_binding (tree id, vec<cxx_saved_binding, va_gc> **old_bindings)
6740 cxx_saved_binding saved;
6742 gcc_checking_assert (store_binding_p (id));
6744 IDENTIFIER_MARKED (id) = 1;
6746 saved.identifier = id;
6747 saved.binding = IDENTIFIER_BINDING (id);
6748 saved.real_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
6749 (*old_bindings)->quick_push (saved);
6750 IDENTIFIER_BINDING (id) = NULL;
6753 static void
6754 store_bindings (tree names, vec<cxx_saved_binding, va_gc> **old_bindings)
6756 static vec<tree> bindings_need_stored;
6757 tree t, id;
6758 size_t i;
6760 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6761 for (t = names; t; t = TREE_CHAIN (t))
6763 if (TREE_CODE (t) == TREE_LIST)
6764 id = TREE_PURPOSE (t);
6765 else
6766 id = DECL_NAME (t);
6768 if (store_binding_p (id))
6769 bindings_need_stored.safe_push (id);
6771 if (!bindings_need_stored.is_empty ())
6773 vec_safe_reserve_exact (*old_bindings, bindings_need_stored.length ());
6774 for (i = 0; bindings_need_stored.iterate (i, &id); ++i)
6776 /* We can apparently have duplicates in NAMES. */
6777 if (store_binding_p (id))
6778 store_binding (id, old_bindings);
6780 bindings_need_stored.truncate (0);
6782 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6785 /* Like store_bindings, but NAMES is a vector of cp_class_binding
6786 objects, rather than a TREE_LIST. */
6788 static void
6789 store_class_bindings (vec<cp_class_binding, va_gc> *names,
6790 vec<cxx_saved_binding, va_gc> **old_bindings)
6792 static vec<tree> bindings_need_stored;
6793 size_t i;
6794 cp_class_binding *cb;
6796 for (i = 0; vec_safe_iterate (names, i, &cb); ++i)
6797 if (store_binding_p (cb->identifier))
6798 bindings_need_stored.safe_push (cb->identifier);
6799 if (!bindings_need_stored.is_empty ())
6801 tree id;
6802 vec_safe_reserve_exact (*old_bindings, bindings_need_stored.length ());
6803 for (i = 0; bindings_need_stored.iterate (i, &id); ++i)
6804 store_binding (id, old_bindings);
6805 bindings_need_stored.truncate (0);
6809 /* A chain of saved_scope structures awaiting reuse. */
6811 static GTY((deletable)) struct saved_scope *free_saved_scope;
6813 static void
6814 do_push_to_top_level (void)
6816 struct saved_scope *s;
6817 cp_binding_level *b;
6818 cxx_saved_binding *sb;
6819 size_t i;
6820 bool need_pop;
6822 /* Reuse or create a new structure for this saved scope. */
6823 if (free_saved_scope != NULL)
6825 s = free_saved_scope;
6826 free_saved_scope = s->prev;
6828 vec<cxx_saved_binding, va_gc> *old_bindings = s->old_bindings;
6829 memset (s, 0, sizeof (*s));
6830 /* Also reuse the structure's old_bindings vector. */
6831 vec_safe_truncate (old_bindings, 0);
6832 s->old_bindings = old_bindings;
6834 else
6835 s = ggc_cleared_alloc<saved_scope> ();
6837 b = scope_chain ? current_binding_level : 0;
6839 /* If we're in the middle of some function, save our state. */
6840 if (cfun)
6842 need_pop = true;
6843 push_function_context ();
6845 else
6846 need_pop = false;
6848 if (scope_chain && previous_class_level)
6849 store_class_bindings (previous_class_level->class_shadowed,
6850 &s->old_bindings);
6852 /* Have to include the global scope, because class-scope decls
6853 aren't listed anywhere useful. */
6854 for (; b; b = b->level_chain)
6856 tree t;
6858 /* Template IDs are inserted into the global level. If they were
6859 inserted into namespace level, finish_file wouldn't find them
6860 when doing pending instantiations. Therefore, don't stop at
6861 namespace level, but continue until :: . */
6862 if (global_scope_p (b))
6863 break;
6865 store_bindings (b->names, &s->old_bindings);
6866 /* We also need to check class_shadowed to save class-level type
6867 bindings, since pushclass doesn't fill in b->names. */
6868 if (b->kind == sk_class)
6869 store_class_bindings (b->class_shadowed, &s->old_bindings);
6871 /* Unwind type-value slots back to top level. */
6872 for (t = b->type_shadowed; t; t = TREE_CHAIN (t))
6873 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (t), TREE_VALUE (t));
6876 FOR_EACH_VEC_SAFE_ELT (s->old_bindings, i, sb)
6877 IDENTIFIER_MARKED (sb->identifier) = 0;
6879 s->prev = scope_chain;
6880 s->bindings = b;
6881 s->need_pop_function_context = need_pop;
6882 s->function_decl = current_function_decl;
6883 s->unevaluated_operand = cp_unevaluated_operand;
6884 s->inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
6885 s->x_stmt_tree.stmts_are_full_exprs_p = true;
6887 scope_chain = s;
6888 current_function_decl = NULL_TREE;
6889 current_lang_base = NULL;
6890 current_lang_name = lang_name_cplusplus;
6891 current_namespace = global_namespace;
6892 push_class_stack ();
6893 cp_unevaluated_operand = 0;
6894 c_inhibit_evaluation_warnings = 0;
6897 static void
6898 do_pop_from_top_level (void)
6900 struct saved_scope *s = scope_chain;
6901 cxx_saved_binding *saved;
6902 size_t i;
6904 /* Clear out class-level bindings cache. */
6905 if (previous_class_level)
6906 invalidate_class_lookup_cache ();
6907 pop_class_stack ();
6909 release_tree_vector (current_lang_base);
6911 scope_chain = s->prev;
6912 FOR_EACH_VEC_SAFE_ELT (s->old_bindings, i, saved)
6914 tree id = saved->identifier;
6916 IDENTIFIER_BINDING (id) = saved->binding;
6917 SET_IDENTIFIER_TYPE_VALUE (id, saved->real_type_value);
6920 /* If we were in the middle of compiling a function, restore our
6921 state. */
6922 if (s->need_pop_function_context)
6923 pop_function_context ();
6924 current_function_decl = s->function_decl;
6925 cp_unevaluated_operand = s->unevaluated_operand;
6926 c_inhibit_evaluation_warnings = s->inhibit_evaluation_warnings;
6928 /* Make this saved_scope structure available for reuse by
6929 push_to_top_level. */
6930 s->prev = free_saved_scope;
6931 free_saved_scope = s;
6934 /* Push into the scope of the namespace NS, even if it is deeply
6935 nested within another namespace. */
6937 static void
6938 do_push_nested_namespace (tree ns)
6940 if (ns == global_namespace)
6941 do_push_to_top_level ();
6942 else
6944 do_push_nested_namespace (CP_DECL_CONTEXT (ns));
6945 gcc_checking_assert
6946 (find_namespace_value (current_namespace, DECL_NAME (ns)) == ns);
6947 resume_scope (NAMESPACE_LEVEL (ns));
6948 current_namespace = ns;
6952 /* Pop back from the scope of the namespace NS, which was previously
6953 entered with push_nested_namespace. */
6955 static void
6956 do_pop_nested_namespace (tree ns)
6958 while (ns != global_namespace)
6960 ns = CP_DECL_CONTEXT (ns);
6961 current_namespace = ns;
6962 leave_scope ();
6965 do_pop_from_top_level ();
6968 /* Add TARGET to USINGS, if it does not already exist there.
6969 We used to build the complete graph of usings at this point, from
6970 the POV of the source namespaces. Now we build that as we perform
6971 the unqualified search. */
6973 static void
6974 add_using_namespace (vec<tree, va_gc> *&usings, tree target)
6976 if (usings)
6977 for (unsigned ix = usings->length (); ix--;)
6978 if ((*usings)[ix] == target)
6979 return;
6981 vec_safe_push (usings, target);
6984 /* Tell the debug system of a using directive. */
6986 static void
6987 emit_debug_info_using_namespace (tree from, tree target, bool implicit)
6989 /* Emit debugging info. */
6990 tree context = from != global_namespace ? from : NULL_TREE;
6991 debug_hooks->imported_module_or_decl (target, NULL_TREE, context, false,
6992 implicit);
6995 /* Process a namespace-scope using directive. */
6997 void
6998 finish_namespace_using_directive (tree target, tree attribs)
7000 gcc_checking_assert (namespace_bindings_p ());
7001 if (target == error_mark_node)
7002 return;
7004 add_using_namespace (DECL_NAMESPACE_USING (current_namespace),
7005 ORIGINAL_NAMESPACE (target));
7006 emit_debug_info_using_namespace (current_namespace,
7007 ORIGINAL_NAMESPACE (target), false);
7009 if (attribs == error_mark_node)
7010 return;
7012 for (tree a = attribs; a; a = TREE_CHAIN (a))
7014 tree name = get_attribute_name (a);
7015 if (is_attribute_p ("strong", name))
7017 warning (0, "strong using directive no longer supported");
7018 if (CP_DECL_CONTEXT (target) == current_namespace)
7019 inform (DECL_SOURCE_LOCATION (target),
7020 "you may use an inline namespace instead");
7022 else
7023 warning (OPT_Wattributes, "%qD attribute directive ignored", name);
7027 /* Process a function-scope using-directive. */
7029 void
7030 finish_local_using_directive (tree target, tree attribs)
7032 gcc_checking_assert (local_bindings_p ());
7033 if (target == error_mark_node)
7034 return;
7036 if (attribs)
7037 warning (OPT_Wattributes, "attributes ignored on local using directive");
7039 add_stmt (build_stmt (input_location, USING_STMT, target));
7041 add_using_namespace (current_binding_level->using_directives,
7042 ORIGINAL_NAMESPACE (target));
7045 /* Pushes X into the global namespace. */
7047 tree
7048 pushdecl_top_level (tree x, bool is_friend)
7050 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
7051 do_push_to_top_level ();
7052 x = pushdecl_namespace_level (x, is_friend);
7053 do_pop_from_top_level ();
7054 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
7055 return x;
7058 /* Pushes X into the global namespace and calls cp_finish_decl to
7059 register the variable, initializing it with INIT. */
7061 tree
7062 pushdecl_top_level_and_finish (tree x, tree init)
7064 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
7065 do_push_to_top_level ();
7066 x = pushdecl_namespace_level (x, false);
7067 cp_finish_decl (x, init, false, NULL_TREE, 0);
7068 do_pop_from_top_level ();
7069 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
7070 return x;
7073 /* Enter the namespaces from current_namerspace to NS. */
7075 static int
7076 push_inline_namespaces (tree ns)
7078 int count = 0;
7079 if (ns != current_namespace)
7081 gcc_assert (ns != global_namespace);
7082 count += push_inline_namespaces (CP_DECL_CONTEXT (ns));
7083 resume_scope (NAMESPACE_LEVEL (ns));
7084 current_namespace = ns;
7085 count++;
7087 return count;
7090 /* Push into the scope of the NAME namespace. If NAME is NULL_TREE,
7091 then we enter an anonymous namespace. If MAKE_INLINE is true, then
7092 we create an inline namespace (it is up to the caller to check upon
7093 redefinition). Return the number of namespaces entered. */
7096 push_namespace (tree name, bool make_inline)
7098 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
7099 int count = 0;
7101 /* We should not get here if the global_namespace is not yet constructed
7102 nor if NAME designates the global namespace: The global scope is
7103 constructed elsewhere. */
7104 gcc_checking_assert (global_namespace != NULL && name != global_identifier);
7106 tree ns = NULL_TREE;
7108 name_lookup lookup (name, 0);
7109 if (!lookup.search_qualified (current_namespace, /*usings=*/false))
7111 else if (TREE_CODE (lookup.value) != NAMESPACE_DECL)
7113 else if (tree dna = DECL_NAMESPACE_ALIAS (lookup.value))
7115 /* A namespace alias is not allowed here, but if the alias
7116 is for a namespace also inside the current scope,
7117 accept it with a diagnostic. That's better than dying
7118 horribly. */
7119 if (is_nested_namespace (current_namespace, CP_DECL_CONTEXT (dna)))
7121 error ("namespace alias %qD not allowed here, "
7122 "assuming %qD", lookup.value, dna);
7123 ns = dna;
7126 else
7127 ns = lookup.value;
7130 bool new_ns = false;
7131 if (ns)
7132 /* DR2061. NS might be a member of an inline namespace. We
7133 need to push into those namespaces. */
7134 count += push_inline_namespaces (CP_DECL_CONTEXT (ns));
7135 else
7137 ns = build_lang_decl (NAMESPACE_DECL, name, void_type_node);
7138 SCOPE_DEPTH (ns) = SCOPE_DEPTH (current_namespace) + 1;
7139 if (!SCOPE_DEPTH (ns))
7140 /* We only allow depth 255. */
7141 sorry ("cannot nest more than %d namespaces",
7142 SCOPE_DEPTH (current_namespace));
7143 DECL_CONTEXT (ns) = FROB_CONTEXT (current_namespace);
7144 new_ns = true;
7146 if (pushdecl (ns) == error_mark_node)
7147 ns = NULL_TREE;
7148 else
7150 if (!name)
7152 SET_DECL_ASSEMBLER_NAME (ns, anon_identifier);
7154 if (!make_inline)
7155 add_using_namespace (DECL_NAMESPACE_USING (current_namespace),
7156 ns);
7158 else if (TREE_PUBLIC (current_namespace))
7159 TREE_PUBLIC (ns) = 1;
7161 if (make_inline)
7163 DECL_NAMESPACE_INLINE_P (ns) = true;
7164 vec_safe_push (DECL_NAMESPACE_INLINEES (current_namespace), ns);
7167 if (!name || make_inline)
7168 emit_debug_info_using_namespace (current_namespace, ns, true);
7172 if (ns)
7174 if (make_inline && !DECL_NAMESPACE_INLINE_P (ns))
7176 error ("inline namespace must be specified at initial definition");
7177 inform (DECL_SOURCE_LOCATION (ns), "%qD defined here", ns);
7179 if (new_ns)
7180 begin_scope (sk_namespace, ns);
7181 else
7182 resume_scope (NAMESPACE_LEVEL (ns));
7183 current_namespace = ns;
7184 count++;
7187 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
7188 return count;
7191 /* Pop from the scope of the current namespace. */
7193 void
7194 pop_namespace (void)
7196 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
7198 gcc_assert (current_namespace != global_namespace);
7199 current_namespace = CP_DECL_CONTEXT (current_namespace);
7200 /* The binding level is not popped, as it might be re-opened later. */
7201 leave_scope ();
7203 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
7206 /* External entry points for do_{push_to/pop_from}_top_level. */
7208 void
7209 push_to_top_level (void)
7211 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
7212 do_push_to_top_level ();
7213 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
7216 void
7217 pop_from_top_level (void)
7219 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
7220 do_pop_from_top_level ();
7221 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
7224 /* External entry points for do_{push,pop}_nested_namespace. */
7226 void
7227 push_nested_namespace (tree ns)
7229 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
7230 do_push_nested_namespace (ns);
7231 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
7234 void
7235 pop_nested_namespace (tree ns)
7237 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
7238 gcc_assert (current_namespace == ns);
7239 do_pop_nested_namespace (ns);
7240 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
7243 /* Pop off extraneous binding levels left over due to syntax errors.
7244 We don't pop past namespaces, as they might be valid. */
7246 void
7247 pop_everything (void)
7249 if (ENABLE_SCOPE_CHECKING)
7250 verbatim ("XXX entering pop_everything ()\n");
7251 while (!namespace_bindings_p ())
7253 if (current_binding_level->kind == sk_class)
7254 pop_nested_class ();
7255 else
7256 poplevel (0, 0, 0);
7258 if (ENABLE_SCOPE_CHECKING)
7259 verbatim ("XXX leaving pop_everything ()\n");
7262 /* Emit debugging information for using declarations and directives.
7263 If input tree is overloaded fn then emit debug info for all
7264 candidates. */
7266 void
7267 cp_emit_debug_info_for_using (tree t, tree context)
7269 /* Don't try to emit any debug information if we have errors. */
7270 if (seen_error ())
7271 return;
7273 /* Ignore this FUNCTION_DECL if it refers to a builtin declaration
7274 of a builtin function. */
7275 if (TREE_CODE (t) == FUNCTION_DECL
7276 && DECL_EXTERNAL (t)
7277 && DECL_BUILT_IN (t))
7278 return;
7280 /* Do not supply context to imported_module_or_decl, if
7281 it is a global namespace. */
7282 if (context == global_namespace)
7283 context = NULL_TREE;
7285 t = MAYBE_BASELINK_FUNCTIONS (t);
7287 /* FIXME: Handle TEMPLATE_DECLs. */
7288 for (lkp_iterator iter (t); iter; ++iter)
7290 tree fn = *iter;
7291 if (TREE_CODE (fn) != TEMPLATE_DECL)
7293 if (building_stmt_list_p ())
7294 add_stmt (build_stmt (input_location, USING_STMT, fn));
7295 else
7296 debug_hooks->imported_module_or_decl (fn, NULL_TREE, context,
7297 false, false);
7302 #include "gt-cp-name-lookup.h"