C++: don't offer bogus "._0" suggestions (PR c++/86329)
[official-gcc.git] / gcc / cp / name-lookup.c
blob3aafb0f0ccf8190fa3416fdb30458c14f2e480a7
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 pedwarn (input_location, 0,
2560 "conflicting C language linkage declaration %q#D", decl);
2561 inform (DECL_SOURCE_LOCATION (old),
2562 "previous declaration %q#D", old);
2563 if (mismatch < 0)
2564 inform (input_location,
2565 "due to different exception specifications");
2567 else
2569 if (old == *slot)
2570 /* The hash table expects OVERLOADS, so construct one with
2571 OLD as both the function and the chain. This allocate
2572 an excess OVERLOAD node, but it's rare to have multiple
2573 extern "C" decls of the same name. And we save
2574 complicating the hash table logic (which is used
2575 elsewhere). */
2576 *slot = ovl_make (old, old);
2578 slot = &OVL_CHAIN (*slot);
2580 /* Chain it on for c_linkage_binding's use. */
2581 *slot = tree_cons (NULL_TREE, decl, *slot);
2584 else
2585 *slot = decl;
2588 /* Returns a list of C-linkage decls with the name NAME. Used in
2589 c-family/c-pragma.c to implement redefine_extname pragma. */
2591 tree
2592 c_linkage_bindings (tree name)
2594 if (extern_c_decls)
2595 if (tree *slot = extern_c_decls
2596 ->find_slot_with_hash (name, IDENTIFIER_HASH_VALUE (name), NO_INSERT))
2598 tree result = *slot;
2599 if (TREE_CODE (result) == OVERLOAD)
2600 result = OVL_CHAIN (result);
2601 return result;
2604 return NULL_TREE;
2607 /* DECL is being declared at a local scope. Emit suitable shadow
2608 warnings. */
2610 static void
2611 check_local_shadow (tree decl)
2613 /* Don't complain about the parms we push and then pop
2614 while tentatively parsing a function declarator. */
2615 if (TREE_CODE (decl) == PARM_DECL && !DECL_CONTEXT (decl))
2616 return;
2618 /* Inline decls shadow nothing. */
2619 if (DECL_FROM_INLINE (decl))
2620 return;
2622 /* External decls are something else. */
2623 if (DECL_EXTERNAL (decl))
2624 return;
2626 tree old = NULL_TREE;
2627 cp_binding_level *old_scope = NULL;
2628 if (cxx_binding *binding = outer_binding (DECL_NAME (decl), NULL, true))
2630 old = binding->value;
2631 old_scope = binding->scope;
2634 tree shadowed = NULL_TREE;
2635 if (old
2636 && (TREE_CODE (old) == PARM_DECL
2637 || VAR_P (old)
2638 || (TREE_CODE (old) == TYPE_DECL
2639 && (!DECL_ARTIFICIAL (old)
2640 || TREE_CODE (decl) == TYPE_DECL)))
2641 && DECL_FUNCTION_SCOPE_P (old)
2642 && (!DECL_ARTIFICIAL (decl)
2643 || DECL_IMPLICIT_TYPEDEF_P (decl)
2644 || (VAR_P (decl) && DECL_ANON_UNION_VAR_P (decl))))
2646 /* DECL shadows a local thing possibly of interest. */
2648 /* Don't complain if it's from an enclosing function. */
2649 if (DECL_CONTEXT (old) == current_function_decl
2650 && TREE_CODE (decl) != PARM_DECL
2651 && TREE_CODE (old) == PARM_DECL)
2653 /* Go to where the parms should be and see if we find
2654 them there. */
2655 cp_binding_level *b = current_binding_level->level_chain;
2657 if (FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
2658 /* Skip the ctor/dtor cleanup level. */
2659 b = b->level_chain;
2661 /* ARM $8.3 */
2662 if (b->kind == sk_function_parms)
2664 error ("declaration of %q#D shadows a parameter", decl);
2665 return;
2669 /* The local structure or class can't use parameters of
2670 the containing function anyway. */
2671 if (DECL_CONTEXT (old) != current_function_decl)
2673 for (cp_binding_level *scope = current_binding_level;
2674 scope != old_scope; scope = scope->level_chain)
2675 if (scope->kind == sk_class
2676 && !LAMBDA_TYPE_P (scope->this_entity))
2677 return;
2679 /* Error if redeclaring a local declared in a
2680 init-statement or in the condition of an if or
2681 switch statement when the new declaration is in the
2682 outermost block of the controlled statement.
2683 Redeclaring a variable from a for or while condition is
2684 detected elsewhere. */
2685 else if (VAR_P (old)
2686 && old_scope == current_binding_level->level_chain
2687 && (old_scope->kind == sk_cond || old_scope->kind == sk_for))
2689 error ("redeclaration of %q#D", decl);
2690 inform (DECL_SOURCE_LOCATION (old),
2691 "%q#D previously declared here", old);
2692 return;
2694 /* C++11:
2695 3.3.3/3: The name declared in an exception-declaration (...)
2696 shall not be redeclared in the outermost block of the handler.
2697 3.3.3/2: A parameter name shall not be redeclared (...) in
2698 the outermost block of any handler associated with a
2699 function-try-block.
2700 3.4.1/15: The function parameter names shall not be redeclared
2701 in the exception-declaration nor in the outermost block of a
2702 handler for the function-try-block. */
2703 else if ((TREE_CODE (old) == VAR_DECL
2704 && old_scope == current_binding_level->level_chain
2705 && old_scope->kind == sk_catch)
2706 || (TREE_CODE (old) == PARM_DECL
2707 && (current_binding_level->kind == sk_catch
2708 || current_binding_level->level_chain->kind == sk_catch)
2709 && in_function_try_handler))
2711 if (permerror (input_location, "redeclaration of %q#D", decl))
2712 inform (DECL_SOURCE_LOCATION (old),
2713 "%q#D previously declared here", old);
2714 return;
2717 /* If '-Wshadow=compatible-local' is specified without other
2718 -Wshadow= flags, we will warn only when the type of the
2719 shadowing variable (DECL) can be converted to that of the
2720 shadowed parameter (OLD_LOCAL). The reason why we only check
2721 if DECL's type can be converted to OLD_LOCAL's type (but not the
2722 other way around) is because when users accidentally shadow a
2723 parameter, more than often they would use the variable
2724 thinking (mistakenly) it's still the parameter. It would be
2725 rare that users would use the variable in the place that
2726 expects the parameter but thinking it's a new decl. */
2728 enum opt_code warning_code;
2729 if (warn_shadow)
2730 warning_code = OPT_Wshadow;
2731 else if (warn_shadow_local)
2732 warning_code = OPT_Wshadow_local;
2733 else if (warn_shadow_compatible_local
2734 && (same_type_p (TREE_TYPE (old), TREE_TYPE (decl))
2735 || (!dependent_type_p (TREE_TYPE (decl))
2736 && !dependent_type_p (TREE_TYPE (old))
2737 && can_convert (TREE_TYPE (old), TREE_TYPE (decl),
2738 tf_none))))
2739 warning_code = OPT_Wshadow_compatible_local;
2740 else
2741 return;
2743 const char *msg;
2744 if (TREE_CODE (old) == PARM_DECL)
2745 msg = "declaration of %q#D shadows a parameter";
2746 else if (is_capture_proxy (old))
2747 msg = "declaration of %qD shadows a lambda capture";
2748 else
2749 msg = "declaration of %qD shadows a previous local";
2751 if (warning_at (input_location, warning_code, msg, decl))
2753 shadowed = old;
2754 goto inform_shadowed;
2756 return;
2759 if (!warn_shadow)
2760 return;
2762 /* Don't warn for artificial things that are not implicit typedefs. */
2763 if (DECL_ARTIFICIAL (decl) && !DECL_IMPLICIT_TYPEDEF_P (decl))
2764 return;
2766 if (nonlambda_method_basetype ())
2767 if (tree member = lookup_member (current_nonlambda_class_type (),
2768 DECL_NAME (decl), /*protect=*/0,
2769 /*want_type=*/false, tf_warning_or_error))
2771 member = MAYBE_BASELINK_FUNCTIONS (member);
2773 /* Warn if a variable shadows a non-function, or the variable
2774 is a function or a pointer-to-function. */
2775 if (!OVL_P (member)
2776 || TREE_CODE (decl) == FUNCTION_DECL
2777 || TYPE_PTRFN_P (TREE_TYPE (decl))
2778 || TYPE_PTRMEMFUNC_P (TREE_TYPE (decl)))
2780 if (warning_at (input_location, OPT_Wshadow,
2781 "declaration of %qD shadows a member of %qT",
2782 decl, current_nonlambda_class_type ())
2783 && DECL_P (member))
2785 shadowed = member;
2786 goto inform_shadowed;
2789 return;
2792 /* Now look for a namespace shadow. */
2793 old = find_namespace_value (current_namespace, DECL_NAME (decl));
2794 if (old
2795 && (VAR_P (old)
2796 || (TREE_CODE (old) == TYPE_DECL
2797 && (!DECL_ARTIFICIAL (old)
2798 || TREE_CODE (decl) == TYPE_DECL)))
2799 && !instantiating_current_function_p ())
2800 /* XXX shadow warnings in outer-more namespaces */
2802 if (warning_at (input_location, OPT_Wshadow,
2803 "declaration of %qD shadows a global declaration",
2804 decl))
2806 shadowed = old;
2807 goto inform_shadowed;
2809 return;
2812 return;
2814 inform_shadowed:
2815 inform (DECL_SOURCE_LOCATION (shadowed), "shadowed declaration is here");
2818 /* DECL is being pushed inside function CTX. Set its context, if
2819 needed. */
2821 static void
2822 set_decl_context_in_fn (tree ctx, tree decl)
2824 if (!DECL_CONTEXT (decl)
2825 /* A local declaration for a function doesn't constitute
2826 nesting. */
2827 && TREE_CODE (decl) != FUNCTION_DECL
2828 /* A local declaration for an `extern' variable is in the
2829 scope of the current namespace, not the current
2830 function. */
2831 && !(VAR_P (decl) && DECL_EXTERNAL (decl))
2832 /* When parsing the parameter list of a function declarator,
2833 don't set DECL_CONTEXT to an enclosing function. When we
2834 push the PARM_DECLs in order to process the function body,
2835 current_binding_level->this_entity will be set. */
2836 && !(TREE_CODE (decl) == PARM_DECL
2837 && current_binding_level->kind == sk_function_parms
2838 && current_binding_level->this_entity == NULL))
2839 DECL_CONTEXT (decl) = ctx;
2841 /* If this is the declaration for a namespace-scope function,
2842 but the declaration itself is in a local scope, mark the
2843 declaration. */
2844 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (decl))
2845 DECL_LOCAL_FUNCTION_P (decl) = 1;
2848 /* DECL is a local-scope decl with linkage. SHADOWED is true if the
2849 name is already bound at the current level.
2851 [basic.link] If there is a visible declaration of an entity with
2852 linkage having the same name and type, ignoring entities declared
2853 outside the innermost enclosing namespace scope, the block scope
2854 declaration declares that same entity and receives the linkage of
2855 the previous declaration.
2857 Also, make sure that this decl matches any existing external decl
2858 in the enclosing namespace. */
2860 static void
2861 set_local_extern_decl_linkage (tree decl, bool shadowed)
2863 tree ns_value = decl; /* Unique marker. */
2865 if (!shadowed)
2867 tree loc_value = innermost_non_namespace_value (DECL_NAME (decl));
2868 if (!loc_value)
2870 ns_value
2871 = find_namespace_value (current_namespace, DECL_NAME (decl));
2872 loc_value = ns_value;
2874 if (loc_value == error_mark_node
2875 /* An ambiguous lookup. */
2876 || (loc_value && TREE_CODE (loc_value) == TREE_LIST))
2877 loc_value = NULL_TREE;
2879 for (ovl_iterator iter (loc_value); iter; ++iter)
2880 if (!iter.hidden_p ()
2881 && (TREE_STATIC (*iter) || DECL_EXTERNAL (*iter))
2882 && decls_match (*iter, decl))
2884 /* The standard only says that the local extern inherits
2885 linkage from the previous decl; in particular, default
2886 args are not shared. Add the decl into a hash table to
2887 make sure only the previous decl in this case is seen
2888 by the middle end. */
2889 struct cxx_int_tree_map *h;
2891 /* We inherit the outer decl's linkage. But we're a
2892 different decl. */
2893 TREE_PUBLIC (decl) = TREE_PUBLIC (*iter);
2895 if (cp_function_chain->extern_decl_map == NULL)
2896 cp_function_chain->extern_decl_map
2897 = hash_table<cxx_int_tree_map_hasher>::create_ggc (20);
2899 h = ggc_alloc<cxx_int_tree_map> ();
2900 h->uid = DECL_UID (decl);
2901 h->to = *iter;
2902 cxx_int_tree_map **loc = cp_function_chain->extern_decl_map
2903 ->find_slot (h, INSERT);
2904 *loc = h;
2905 break;
2909 if (TREE_PUBLIC (decl))
2911 /* DECL is externally visible. Make sure it matches a matching
2912 decl in the namespace scope. We only really need to check
2913 this when inserting the decl, not when we find an existing
2914 match in the current scope. However, in practice we're
2915 going to be inserting a new decl in the majority of cases --
2916 who writes multiple extern decls for the same thing in the
2917 same local scope? Doing it here often avoids a duplicate
2918 namespace lookup. */
2920 /* Avoid repeating a lookup. */
2921 if (ns_value == decl)
2922 ns_value = find_namespace_value (current_namespace, DECL_NAME (decl));
2924 if (ns_value == error_mark_node
2925 || (ns_value && TREE_CODE (ns_value) == TREE_LIST))
2926 ns_value = NULL_TREE;
2928 for (ovl_iterator iter (ns_value); iter; ++iter)
2930 tree other = *iter;
2932 if (!(TREE_PUBLIC (other) || DECL_EXTERNAL (other)))
2933 ; /* Not externally visible. */
2934 else if (DECL_EXTERN_C_P (decl) && DECL_EXTERN_C_P (other))
2935 ; /* Both are extern "C", we'll check via that mechanism. */
2936 else if (TREE_CODE (other) != TREE_CODE (decl)
2937 || ((VAR_P (decl) || matching_fn_p (other, decl))
2938 && !comptypes (TREE_TYPE (decl), TREE_TYPE (other),
2939 COMPARE_REDECLARATION)))
2941 if (permerror (DECL_SOURCE_LOCATION (decl),
2942 "local external declaration %q#D", decl))
2943 inform (DECL_SOURCE_LOCATION (other),
2944 "does not match previous declaration %q#D", other);
2945 break;
2951 /* Record DECL as belonging to the current lexical scope. Check for
2952 errors (such as an incompatible declaration for the same name
2953 already seen in the same scope). IS_FRIEND is true if DECL is
2954 declared as a friend.
2956 Returns either DECL or an old decl for the same name. If an old
2957 decl is returned, it may have been smashed to agree with what DECL
2958 says. */
2960 static tree
2961 do_pushdecl (tree decl, bool is_friend)
2963 if (decl == error_mark_node)
2964 return error_mark_node;
2966 if (!DECL_TEMPLATE_PARM_P (decl) && current_function_decl)
2967 set_decl_context_in_fn (current_function_decl, decl);
2969 /* The binding level we will be pushing into. During local class
2970 pushing, we want to push to the containing scope. */
2971 cp_binding_level *level = current_binding_level;
2972 while (level->kind == sk_class)
2973 level = level->level_chain;
2975 /* An anonymous namespace has a NULL DECL_NAME, but we still want to
2976 insert it. Other NULL-named decls, not so much. */
2977 tree name = DECL_NAME (decl);
2978 if (name || TREE_CODE (decl) == NAMESPACE_DECL)
2980 cxx_binding *binding = NULL; /* Local scope binding. */
2981 tree ns = NULL_TREE; /* Searched namespace. */
2982 tree *slot = NULL; /* Binding slot in namespace. */
2983 tree old = NULL_TREE;
2985 if (level->kind == sk_namespace)
2987 /* We look in the decl's namespace for an existing
2988 declaration, even though we push into the current
2989 namespace. */
2990 ns = (DECL_NAMESPACE_SCOPE_P (decl)
2991 ? CP_DECL_CONTEXT (decl) : current_namespace);
2992 /* Create the binding, if this is current namespace, because
2993 that's where we'll be pushing anyway. */
2994 slot = find_namespace_slot (ns, name, ns == current_namespace);
2995 if (slot)
2996 old = MAYBE_STAT_DECL (*slot);
2998 else
3000 binding = find_local_binding (level, name);
3001 if (binding)
3002 old = binding->value;
3005 if (current_function_decl && VAR_OR_FUNCTION_DECL_P (decl)
3006 && DECL_EXTERNAL (decl))
3007 set_local_extern_decl_linkage (decl, old != NULL_TREE);
3009 if (old == error_mark_node)
3010 old = NULL_TREE;
3012 for (ovl_iterator iter (old); iter; ++iter)
3013 if (iter.using_p ())
3014 ; /* Ignore using decls here. */
3015 else if (tree match = duplicate_decls (decl, *iter, is_friend))
3017 if (match == error_mark_node)
3019 else if (TREE_CODE (match) == TYPE_DECL)
3020 /* The IDENTIFIER will have the type referring to the
3021 now-smashed TYPE_DECL, because ...? Reset it. */
3022 SET_IDENTIFIER_TYPE_VALUE (name, TREE_TYPE (match));
3023 else if (iter.hidden_p () && !DECL_HIDDEN_P (match))
3025 /* Unhiding a previously hidden decl. */
3026 tree head = iter.reveal_node (old);
3027 if (head != old)
3029 if (!ns)
3031 update_local_overload (binding, head);
3032 binding->value = head;
3034 else if (STAT_HACK_P (*slot))
3035 STAT_DECL (*slot) = head;
3036 else
3037 *slot = head;
3039 if (DECL_EXTERN_C_P (match))
3040 /* We need to check and register the decl now. */
3041 check_extern_c_conflict (match);
3043 return match;
3046 /* We are pushing a new decl. */
3048 /* Skip a hidden builtin we failed to match already. There can
3049 only be one. */
3050 if (old && anticipated_builtin_p (old))
3051 old = OVL_CHAIN (old);
3053 check_template_shadow (decl);
3055 if (DECL_DECLARES_FUNCTION_P (decl))
3057 check_default_args (decl);
3059 if (is_friend)
3061 if (level->kind != sk_namespace)
3063 /* In a local class, a friend function declaration must
3064 find a matching decl in the innermost non-class scope.
3065 [class.friend/11] */
3066 error ("friend declaration %qD in local class without "
3067 "prior local declaration", decl);
3068 /* Don't attempt to push it. */
3069 return error_mark_node;
3071 /* Hide it from ordinary lookup. */
3072 DECL_ANTICIPATED (decl) = DECL_HIDDEN_FRIEND_P (decl) = true;
3076 if (level->kind != sk_namespace)
3078 check_local_shadow (decl);
3080 if (TREE_CODE (decl) == NAMESPACE_DECL)
3081 /* A local namespace alias. */
3082 set_identifier_type_value (name, NULL_TREE);
3084 if (!binding)
3085 binding = create_local_binding (level, name);
3087 else if (!slot)
3089 ns = current_namespace;
3090 slot = find_namespace_slot (ns, name, true);
3091 /* Update OLD to reflect the namespace we're going to be
3092 pushing into. */
3093 old = MAYBE_STAT_DECL (*slot);
3096 old = update_binding (level, binding, slot, old, decl, is_friend);
3098 if (old != decl)
3099 /* An existing decl matched, use it. */
3100 decl = old;
3101 else if (TREE_CODE (decl) == TYPE_DECL)
3103 tree type = TREE_TYPE (decl);
3105 if (type != error_mark_node)
3107 if (TYPE_NAME (type) != decl)
3108 set_underlying_type (decl);
3110 if (!ns)
3111 set_identifier_type_value_with_scope (name, decl, level);
3112 else
3113 SET_IDENTIFIER_TYPE_VALUE (name, global_type_node);
3116 /* If this is a locally defined typedef in a function that
3117 is not a template instantation, record it to implement
3118 -Wunused-local-typedefs. */
3119 if (!instantiating_current_function_p ())
3120 record_locally_defined_typedef (decl);
3122 else if (VAR_P (decl))
3123 maybe_register_incomplete_var (decl);
3125 if ((VAR_P (decl) || TREE_CODE (decl) == FUNCTION_DECL)
3126 && DECL_EXTERN_C_P (decl))
3127 check_extern_c_conflict (decl);
3129 else
3130 add_decl_to_level (level, decl);
3132 return decl;
3135 /* Record a decl-node X as belonging to the current lexical scope.
3136 It's a friend if IS_FRIEND is true -- which affects exactly where
3137 we push it. */
3139 tree
3140 pushdecl (tree x, bool is_friend)
3142 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3143 tree ret = do_pushdecl (x, is_friend);
3144 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3145 return ret;
3148 /* Enter DECL into the symbol table, if that's appropriate. Returns
3149 DECL, or a modified version thereof. */
3151 tree
3152 maybe_push_decl (tree decl)
3154 tree type = TREE_TYPE (decl);
3156 /* Add this decl to the current binding level, but not if it comes
3157 from another scope, e.g. a static member variable. TEM may equal
3158 DECL or it may be a previous decl of the same name. */
3159 if (decl == error_mark_node
3160 || (TREE_CODE (decl) != PARM_DECL
3161 && DECL_CONTEXT (decl) != NULL_TREE
3162 /* Definitions of namespace members outside their namespace are
3163 possible. */
3164 && !DECL_NAMESPACE_SCOPE_P (decl))
3165 || (TREE_CODE (decl) == TEMPLATE_DECL && !namespace_bindings_p ())
3166 || type == unknown_type_node
3167 /* The declaration of a template specialization does not affect
3168 the functions available for overload resolution, so we do not
3169 call pushdecl. */
3170 || (TREE_CODE (decl) == FUNCTION_DECL
3171 && DECL_TEMPLATE_SPECIALIZATION (decl)))
3172 return decl;
3173 else
3174 return pushdecl (decl);
3177 /* Bind DECL to ID in the current_binding_level, assumed to be a local
3178 binding level. If IS_USING is true, DECL got here through a
3179 using-declaration. */
3181 static void
3182 push_local_binding (tree id, tree decl, bool is_using)
3184 /* Skip over any local classes. This makes sense if we call
3185 push_local_binding with a friend decl of a local class. */
3186 cp_binding_level *b = innermost_nonclass_level ();
3188 gcc_assert (b->kind != sk_namespace);
3189 if (find_local_binding (b, id))
3191 /* Supplement the existing binding. */
3192 if (!supplement_binding (IDENTIFIER_BINDING (id), decl))
3193 /* It didn't work. Something else must be bound at this
3194 level. Do not add DECL to the list of things to pop
3195 later. */
3196 return;
3198 else
3199 /* Create a new binding. */
3200 push_binding (id, decl, b);
3202 if (TREE_CODE (decl) == OVERLOAD || is_using)
3203 /* We must put the OVERLOAD or using into a TREE_LIST since we
3204 cannot use the decl's chain itself. */
3205 decl = build_tree_list (NULL_TREE, decl);
3207 /* And put DECL on the list of things declared by the current
3208 binding level. */
3209 add_decl_to_level (b, decl);
3213 /* true means unconditionally make a BLOCK for the next level pushed. */
3215 static bool keep_next_level_flag;
3217 static int binding_depth = 0;
3219 static void
3220 indent (int depth)
3222 int i;
3224 for (i = 0; i < depth * 2; i++)
3225 putc (' ', stderr);
3228 /* Return a string describing the kind of SCOPE we have. */
3229 static const char *
3230 cp_binding_level_descriptor (cp_binding_level *scope)
3232 /* The order of this table must match the "scope_kind"
3233 enumerators. */
3234 static const char* scope_kind_names[] = {
3235 "block-scope",
3236 "cleanup-scope",
3237 "try-scope",
3238 "catch-scope",
3239 "for-scope",
3240 "function-parameter-scope",
3241 "class-scope",
3242 "namespace-scope",
3243 "template-parameter-scope",
3244 "template-explicit-spec-scope"
3246 const scope_kind kind = scope->explicit_spec_p
3247 ? sk_template_spec : scope->kind;
3249 return scope_kind_names[kind];
3252 /* Output a debugging information about SCOPE when performing
3253 ACTION at LINE. */
3254 static void
3255 cp_binding_level_debug (cp_binding_level *scope, int line, const char *action)
3257 const char *desc = cp_binding_level_descriptor (scope);
3258 if (scope->this_entity)
3259 verbatim ("%s %<%s(%E)%> %p %d\n", action, desc,
3260 scope->this_entity, (void *) scope, line);
3261 else
3262 verbatim ("%s %s %p %d\n", action, desc, (void *) scope, line);
3265 /* Return the estimated initial size of the hashtable of a NAMESPACE
3266 scope. */
3268 static inline size_t
3269 namespace_scope_ht_size (tree ns)
3271 tree name = DECL_NAME (ns);
3273 return name == std_identifier
3274 ? NAMESPACE_STD_HT_SIZE
3275 : (name == global_identifier
3276 ? GLOBAL_SCOPE_HT_SIZE
3277 : NAMESPACE_ORDINARY_HT_SIZE);
3280 /* A chain of binding_level structures awaiting reuse. */
3282 static GTY((deletable)) cp_binding_level *free_binding_level;
3284 /* Insert SCOPE as the innermost binding level. */
3286 void
3287 push_binding_level (cp_binding_level *scope)
3289 /* Add it to the front of currently active scopes stack. */
3290 scope->level_chain = current_binding_level;
3291 current_binding_level = scope;
3292 keep_next_level_flag = false;
3294 if (ENABLE_SCOPE_CHECKING)
3296 scope->binding_depth = binding_depth;
3297 indent (binding_depth);
3298 cp_binding_level_debug (scope, LOCATION_LINE (input_location),
3299 "push");
3300 binding_depth++;
3304 /* Create a new KIND scope and make it the top of the active scopes stack.
3305 ENTITY is the scope of the associated C++ entity (namespace, class,
3306 function, C++0x enumeration); it is NULL otherwise. */
3308 cp_binding_level *
3309 begin_scope (scope_kind kind, tree entity)
3311 cp_binding_level *scope;
3313 /* Reuse or create a struct for this binding level. */
3314 if (!ENABLE_SCOPE_CHECKING && free_binding_level)
3316 scope = free_binding_level;
3317 free_binding_level = scope->level_chain;
3318 memset (scope, 0, sizeof (cp_binding_level));
3320 else
3321 scope = ggc_cleared_alloc<cp_binding_level> ();
3323 scope->this_entity = entity;
3324 scope->more_cleanups_ok = true;
3325 switch (kind)
3327 case sk_cleanup:
3328 scope->keep = true;
3329 break;
3331 case sk_template_spec:
3332 scope->explicit_spec_p = true;
3333 kind = sk_template_parms;
3334 /* Fall through. */
3335 case sk_template_parms:
3336 case sk_block:
3337 case sk_try:
3338 case sk_catch:
3339 case sk_for:
3340 case sk_cond:
3341 case sk_class:
3342 case sk_scoped_enum:
3343 case sk_function_parms:
3344 case sk_transaction:
3345 case sk_omp:
3346 scope->keep = keep_next_level_flag;
3347 break;
3349 case sk_namespace:
3350 NAMESPACE_LEVEL (entity) = scope;
3351 break;
3353 default:
3354 /* Should not happen. */
3355 gcc_unreachable ();
3356 break;
3358 scope->kind = kind;
3360 push_binding_level (scope);
3362 return scope;
3365 /* We're about to leave current scope. Pop the top of the stack of
3366 currently active scopes. Return the enclosing scope, now active. */
3368 cp_binding_level *
3369 leave_scope (void)
3371 cp_binding_level *scope = current_binding_level;
3373 if (scope->kind == sk_namespace && class_binding_level)
3374 current_binding_level = class_binding_level;
3376 /* We cannot leave a scope, if there are none left. */
3377 if (NAMESPACE_LEVEL (global_namespace))
3378 gcc_assert (!global_scope_p (scope));
3380 if (ENABLE_SCOPE_CHECKING)
3382 indent (--binding_depth);
3383 cp_binding_level_debug (scope, LOCATION_LINE (input_location),
3384 "leave");
3387 /* Move one nesting level up. */
3388 current_binding_level = scope->level_chain;
3390 /* Namespace-scopes are left most probably temporarily, not
3391 completely; they can be reopened later, e.g. in namespace-extension
3392 or any name binding activity that requires us to resume a
3393 namespace. For classes, we cache some binding levels. For other
3394 scopes, we just make the structure available for reuse. */
3395 if (scope->kind != sk_namespace
3396 && scope->kind != sk_class)
3398 scope->level_chain = free_binding_level;
3399 gcc_assert (!ENABLE_SCOPE_CHECKING
3400 || scope->binding_depth == binding_depth);
3401 free_binding_level = scope;
3404 if (scope->kind == sk_class)
3406 /* Reset DEFINING_CLASS_P to allow for reuse of a
3407 class-defining scope in a non-defining context. */
3408 scope->defining_class_p = 0;
3410 /* Find the innermost enclosing class scope, and reset
3411 CLASS_BINDING_LEVEL appropriately. */
3412 class_binding_level = NULL;
3413 for (scope = current_binding_level; scope; scope = scope->level_chain)
3414 if (scope->kind == sk_class)
3416 class_binding_level = scope;
3417 break;
3421 return current_binding_level;
3424 static void
3425 resume_scope (cp_binding_level* b)
3427 /* Resuming binding levels is meant only for namespaces,
3428 and those cannot nest into classes. */
3429 gcc_assert (!class_binding_level);
3430 /* Also, resuming a non-directly nested namespace is a no-no. */
3431 gcc_assert (b->level_chain == current_binding_level);
3432 current_binding_level = b;
3433 if (ENABLE_SCOPE_CHECKING)
3435 b->binding_depth = binding_depth;
3436 indent (binding_depth);
3437 cp_binding_level_debug (b, LOCATION_LINE (input_location), "resume");
3438 binding_depth++;
3442 /* Return the innermost binding level that is not for a class scope. */
3444 static cp_binding_level *
3445 innermost_nonclass_level (void)
3447 cp_binding_level *b;
3449 b = current_binding_level;
3450 while (b->kind == sk_class)
3451 b = b->level_chain;
3453 return b;
3456 /* We're defining an object of type TYPE. If it needs a cleanup, but
3457 we're not allowed to add any more objects with cleanups to the current
3458 scope, create a new binding level. */
3460 void
3461 maybe_push_cleanup_level (tree type)
3463 if (type != error_mark_node
3464 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
3465 && current_binding_level->more_cleanups_ok == 0)
3467 begin_scope (sk_cleanup, NULL);
3468 current_binding_level->statement_list = push_stmt_list ();
3472 /* Return true if we are in the global binding level. */
3474 bool
3475 global_bindings_p (void)
3477 return global_scope_p (current_binding_level);
3480 /* True if we are currently in a toplevel binding level. This
3481 means either the global binding level or a namespace in a toplevel
3482 binding level. Since there are no non-toplevel namespace levels,
3483 this really means any namespace or template parameter level. We
3484 also include a class whose context is toplevel. */
3486 bool
3487 toplevel_bindings_p (void)
3489 cp_binding_level *b = innermost_nonclass_level ();
3491 return b->kind == sk_namespace || b->kind == sk_template_parms;
3494 /* True if this is a namespace scope, or if we are defining a class
3495 which is itself at namespace scope, or whose enclosing class is
3496 such a class, etc. */
3498 bool
3499 namespace_bindings_p (void)
3501 cp_binding_level *b = innermost_nonclass_level ();
3503 return b->kind == sk_namespace;
3506 /* True if the innermost non-class scope is a block scope. */
3508 bool
3509 local_bindings_p (void)
3511 cp_binding_level *b = innermost_nonclass_level ();
3512 return b->kind < sk_function_parms || b->kind == sk_omp;
3515 /* True if the current level needs to have a BLOCK made. */
3517 bool
3518 kept_level_p (void)
3520 return (current_binding_level->blocks != NULL_TREE
3521 || current_binding_level->keep
3522 || current_binding_level->kind == sk_cleanup
3523 || current_binding_level->names != NULL_TREE
3524 || current_binding_level->using_directives);
3527 /* Returns the kind of the innermost scope. */
3529 scope_kind
3530 innermost_scope_kind (void)
3532 return current_binding_level->kind;
3535 /* Returns true if this scope was created to store template parameters. */
3537 bool
3538 template_parm_scope_p (void)
3540 return innermost_scope_kind () == sk_template_parms;
3543 /* If KEEP is true, make a BLOCK node for the next binding level,
3544 unconditionally. Otherwise, use the normal logic to decide whether
3545 or not to create a BLOCK. */
3547 void
3548 keep_next_level (bool keep)
3550 keep_next_level_flag = keep;
3553 /* Return the list of declarations of the current local scope. */
3555 tree
3556 get_local_decls (void)
3558 gcc_assert (current_binding_level->kind != sk_namespace
3559 && current_binding_level->kind != sk_class);
3560 return current_binding_level->names;
3563 /* Return how many function prototypes we are currently nested inside. */
3566 function_parm_depth (void)
3568 int level = 0;
3569 cp_binding_level *b;
3571 for (b = current_binding_level;
3572 b->kind == sk_function_parms;
3573 b = b->level_chain)
3574 ++level;
3576 return level;
3579 /* For debugging. */
3580 static int no_print_functions = 0;
3581 static int no_print_builtins = 0;
3583 static void
3584 print_binding_level (cp_binding_level* lvl)
3586 tree t;
3587 int i = 0, len;
3588 fprintf (stderr, " blocks=%p", (void *) lvl->blocks);
3589 if (lvl->more_cleanups_ok)
3590 fprintf (stderr, " more-cleanups-ok");
3591 if (lvl->have_cleanups)
3592 fprintf (stderr, " have-cleanups");
3593 fprintf (stderr, "\n");
3594 if (lvl->names)
3596 fprintf (stderr, " names:\t");
3597 /* We can probably fit 3 names to a line? */
3598 for (t = lvl->names; t; t = TREE_CHAIN (t))
3600 if (no_print_functions && (TREE_CODE (t) == FUNCTION_DECL))
3601 continue;
3602 if (no_print_builtins
3603 && (TREE_CODE (t) == TYPE_DECL)
3604 && DECL_IS_BUILTIN (t))
3605 continue;
3607 /* Function decls tend to have longer names. */
3608 if (TREE_CODE (t) == FUNCTION_DECL)
3609 len = 3;
3610 else
3611 len = 2;
3612 i += len;
3613 if (i > 6)
3615 fprintf (stderr, "\n\t");
3616 i = len;
3618 print_node_brief (stderr, "", t, 0);
3619 if (t == error_mark_node)
3620 break;
3622 if (i)
3623 fprintf (stderr, "\n");
3625 if (vec_safe_length (lvl->class_shadowed))
3627 size_t i;
3628 cp_class_binding *b;
3629 fprintf (stderr, " class-shadowed:");
3630 FOR_EACH_VEC_ELT (*lvl->class_shadowed, i, b)
3631 fprintf (stderr, " %s ", IDENTIFIER_POINTER (b->identifier));
3632 fprintf (stderr, "\n");
3634 if (lvl->type_shadowed)
3636 fprintf (stderr, " type-shadowed:");
3637 for (t = lvl->type_shadowed; t; t = TREE_CHAIN (t))
3639 fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
3641 fprintf (stderr, "\n");
3645 DEBUG_FUNCTION void
3646 debug (cp_binding_level &ref)
3648 print_binding_level (&ref);
3651 DEBUG_FUNCTION void
3652 debug (cp_binding_level *ptr)
3654 if (ptr)
3655 debug (*ptr);
3656 else
3657 fprintf (stderr, "<nil>\n");
3661 static void
3662 print_other_binding_stack (cp_binding_level *stack)
3664 cp_binding_level *level;
3665 for (level = stack; !global_scope_p (level); level = level->level_chain)
3667 fprintf (stderr, "binding level %p\n", (void *) level);
3668 print_binding_level (level);
3672 void
3673 print_binding_stack (void)
3675 cp_binding_level *b;
3676 fprintf (stderr, "current_binding_level=%p\n"
3677 "class_binding_level=%p\n"
3678 "NAMESPACE_LEVEL (global_namespace)=%p\n",
3679 (void *) current_binding_level, (void *) class_binding_level,
3680 (void *) NAMESPACE_LEVEL (global_namespace));
3681 if (class_binding_level)
3683 for (b = class_binding_level; b; b = b->level_chain)
3684 if (b == current_binding_level)
3685 break;
3686 if (b)
3687 b = class_binding_level;
3688 else
3689 b = current_binding_level;
3691 else
3692 b = current_binding_level;
3693 print_other_binding_stack (b);
3694 fprintf (stderr, "global:\n");
3695 print_binding_level (NAMESPACE_LEVEL (global_namespace));
3698 /* Return the type associated with ID. */
3700 static tree
3701 identifier_type_value_1 (tree id)
3703 /* There is no type with that name, anywhere. */
3704 if (REAL_IDENTIFIER_TYPE_VALUE (id) == NULL_TREE)
3705 return NULL_TREE;
3706 /* This is not the type marker, but the real thing. */
3707 if (REAL_IDENTIFIER_TYPE_VALUE (id) != global_type_node)
3708 return REAL_IDENTIFIER_TYPE_VALUE (id);
3709 /* Have to search for it. It must be on the global level, now.
3710 Ask lookup_name not to return non-types. */
3711 id = lookup_name_real (id, 2, 1, /*block_p=*/true, 0, 0);
3712 if (id)
3713 return TREE_TYPE (id);
3714 return NULL_TREE;
3717 /* Wrapper for identifier_type_value_1. */
3719 tree
3720 identifier_type_value (tree id)
3722 tree ret;
3723 timevar_start (TV_NAME_LOOKUP);
3724 ret = identifier_type_value_1 (id);
3725 timevar_stop (TV_NAME_LOOKUP);
3726 return ret;
3729 /* Push a definition of struct, union or enum tag named ID. into
3730 binding_level B. DECL is a TYPE_DECL for the type. We assume that
3731 the tag ID is not already defined. */
3733 static void
3734 set_identifier_type_value_with_scope (tree id, tree decl, cp_binding_level *b)
3736 tree type;
3738 if (b->kind != sk_namespace)
3740 /* Shadow the marker, not the real thing, so that the marker
3741 gets restored later. */
3742 tree old_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
3743 b->type_shadowed
3744 = tree_cons (id, old_type_value, b->type_shadowed);
3745 type = decl ? TREE_TYPE (decl) : NULL_TREE;
3746 TREE_TYPE (b->type_shadowed) = type;
3748 else
3750 tree *slot = find_namespace_slot (current_namespace, id, true);
3751 gcc_assert (decl);
3752 update_binding (b, NULL, slot, MAYBE_STAT_DECL (*slot), decl, false);
3754 /* Store marker instead of real type. */
3755 type = global_type_node;
3757 SET_IDENTIFIER_TYPE_VALUE (id, type);
3760 /* As set_identifier_type_value_with_scope, but using
3761 current_binding_level. */
3763 void
3764 set_identifier_type_value (tree id, tree decl)
3766 set_identifier_type_value_with_scope (id, decl, current_binding_level);
3769 /* Return the name for the constructor (or destructor) for the
3770 specified class. */
3772 tree
3773 constructor_name (tree type)
3775 tree decl = TYPE_NAME (TYPE_MAIN_VARIANT (type));
3777 return decl ? DECL_NAME (decl) : NULL_TREE;
3780 /* Returns TRUE if NAME is the name for the constructor for TYPE,
3781 which must be a class type. */
3783 bool
3784 constructor_name_p (tree name, tree type)
3786 gcc_assert (MAYBE_CLASS_TYPE_P (type));
3788 /* These don't have names. */
3789 if (TREE_CODE (type) == DECLTYPE_TYPE
3790 || TREE_CODE (type) == TYPEOF_TYPE)
3791 return false;
3793 if (name && name == constructor_name (type))
3794 return true;
3796 return false;
3799 /* Counter used to create anonymous type names. */
3801 static GTY(()) int anon_cnt;
3803 /* Return an IDENTIFIER which can be used as a name for
3804 unnamed structs and unions. */
3806 tree
3807 make_anon_name (void)
3809 char buf[32];
3811 sprintf (buf, anon_aggrname_format (), anon_cnt++);
3812 return get_identifier (buf);
3815 /* This code is practically identical to that for creating
3816 anonymous names, but is just used for lambdas instead. This isn't really
3817 necessary, but it's convenient to avoid treating lambdas like other
3818 unnamed types. */
3820 static GTY(()) int lambda_cnt = 0;
3822 tree
3823 make_lambda_name (void)
3825 char buf[32];
3827 sprintf (buf, LAMBDANAME_FORMAT, lambda_cnt++);
3828 return get_identifier (buf);
3831 /* Insert another USING_DECL into the current binding level, returning
3832 this declaration. If this is a redeclaration, do nothing, and
3833 return NULL_TREE if this not in namespace scope (in namespace
3834 scope, a using decl might extend any previous bindings). */
3836 static tree
3837 push_using_decl_1 (tree scope, tree name)
3839 tree decl;
3841 gcc_assert (TREE_CODE (scope) == NAMESPACE_DECL);
3842 gcc_assert (identifier_p (name));
3843 for (decl = current_binding_level->usings; decl; decl = DECL_CHAIN (decl))
3844 if (USING_DECL_SCOPE (decl) == scope && DECL_NAME (decl) == name)
3845 break;
3846 if (decl)
3847 return namespace_bindings_p () ? decl : NULL_TREE;
3848 decl = build_lang_decl (USING_DECL, name, NULL_TREE);
3849 USING_DECL_SCOPE (decl) = scope;
3850 DECL_CHAIN (decl) = current_binding_level->usings;
3851 current_binding_level->usings = decl;
3852 return decl;
3855 /* Wrapper for push_using_decl_1. */
3857 static tree
3858 push_using_decl (tree scope, tree name)
3860 tree ret;
3861 timevar_start (TV_NAME_LOOKUP);
3862 ret = push_using_decl_1 (scope, name);
3863 timevar_stop (TV_NAME_LOOKUP);
3864 return ret;
3867 /* Same as pushdecl, but define X in binding-level LEVEL. We rely on the
3868 caller to set DECL_CONTEXT properly.
3870 Note that this must only be used when X will be the new innermost
3871 binding for its name, as we tack it onto the front of IDENTIFIER_BINDING
3872 without checking to see if the current IDENTIFIER_BINDING comes from a
3873 closer binding level than LEVEL. */
3875 static tree
3876 do_pushdecl_with_scope (tree x, cp_binding_level *level, bool is_friend)
3878 cp_binding_level *b;
3880 if (level->kind == sk_class)
3882 b = class_binding_level;
3883 class_binding_level = level;
3884 pushdecl_class_level (x);
3885 class_binding_level = b;
3887 else
3889 tree function_decl = current_function_decl;
3890 if (level->kind == sk_namespace)
3891 current_function_decl = NULL_TREE;
3892 b = current_binding_level;
3893 current_binding_level = level;
3894 x = pushdecl (x, is_friend);
3895 current_binding_level = b;
3896 current_function_decl = function_decl;
3898 return x;
3901 /* Inject X into the local scope just before the function parms. */
3903 tree
3904 pushdecl_outermost_localscope (tree x)
3906 cp_binding_level *b = NULL;
3907 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3909 /* Find the scope just inside the function parms. */
3910 for (cp_binding_level *n = current_binding_level;
3911 n->kind != sk_function_parms; n = b->level_chain)
3912 b = n;
3914 tree ret = b ? do_pushdecl_with_scope (x, b, false) : error_mark_node;
3915 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3917 return ret;
3920 /* Check a non-member using-declaration. Return the name and scope
3921 being used, and the USING_DECL, or NULL_TREE on failure. */
3923 static tree
3924 validate_nonmember_using_decl (tree decl, tree scope, tree name)
3926 /* [namespace.udecl]
3927 A using-declaration for a class member shall be a
3928 member-declaration. */
3929 if (TYPE_P (scope))
3931 error ("%qT is not a namespace or unscoped enum", scope);
3932 return NULL_TREE;
3934 else if (scope == error_mark_node)
3935 return NULL_TREE;
3937 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR)
3939 /* 7.3.3/5
3940 A using-declaration shall not name a template-id. */
3941 error ("a using-declaration cannot specify a template-id. "
3942 "Try %<using %D%>", name);
3943 return NULL_TREE;
3946 if (TREE_CODE (decl) == NAMESPACE_DECL)
3948 error ("namespace %qD not allowed in using-declaration", decl);
3949 return NULL_TREE;
3952 if (TREE_CODE (decl) == SCOPE_REF)
3954 /* It's a nested name with template parameter dependent scope.
3955 This can only be using-declaration for class member. */
3956 error ("%qT is not a namespace", TREE_OPERAND (decl, 0));
3957 return NULL_TREE;
3960 decl = OVL_FIRST (decl);
3962 /* Make a USING_DECL. */
3963 tree using_decl = push_using_decl (scope, name);
3965 if (using_decl == NULL_TREE
3966 && at_function_scope_p ()
3967 && VAR_P (decl))
3968 /* C++11 7.3.3/10. */
3969 error ("%qD is already declared in this scope", name);
3971 return using_decl;
3974 /* Process a local-scope or namespace-scope using declaration. SCOPE
3975 is the nominated scope to search for NAME. VALUE_P and TYPE_P
3976 point to the binding for NAME in the current scope and are
3977 updated. */
3979 static void
3980 do_nonmember_using_decl (tree scope, tree name, tree *value_p, tree *type_p)
3982 name_lookup lookup (name, 0);
3984 if (!qualified_namespace_lookup (scope, &lookup))
3986 error ("%qD not declared", name);
3987 return;
3989 else if (TREE_CODE (lookup.value) == TREE_LIST)
3991 error ("reference to %qD is ambiguous", name);
3992 print_candidates (lookup.value);
3993 lookup.value = NULL_TREE;
3996 if (lookup.type && TREE_CODE (lookup.type) == TREE_LIST)
3998 error ("reference to %qD is ambiguous", name);
3999 print_candidates (lookup.type);
4000 lookup.type = NULL_TREE;
4003 tree value = *value_p;
4004 tree type = *type_p;
4006 /* Shift the old and new bindings around so we're comparing class and
4007 enumeration names to each other. */
4008 if (value && DECL_IMPLICIT_TYPEDEF_P (value))
4010 type = value;
4011 value = NULL_TREE;
4014 if (lookup.value && DECL_IMPLICIT_TYPEDEF_P (lookup.value))
4016 lookup.type = lookup.value;
4017 lookup.value = NULL_TREE;
4020 if (lookup.value && lookup.value != value)
4022 /* Check for using functions. */
4023 if (OVL_P (lookup.value) && (!value || OVL_P (value)))
4025 for (lkp_iterator usings (lookup.value); usings; ++usings)
4027 tree new_fn = *usings;
4029 /* [namespace.udecl]
4031 If a function declaration in namespace scope or block
4032 scope has the same name and the same parameter types as a
4033 function introduced by a using declaration the program is
4034 ill-formed. */
4035 bool found = false;
4036 for (ovl_iterator old (value); !found && old; ++old)
4038 tree old_fn = *old;
4040 if (new_fn == old_fn)
4041 /* The function already exists in the current
4042 namespace. */
4043 found = true;
4044 else if (old.using_p ())
4045 continue; /* This is a using decl. */
4046 else if (old.hidden_p () && !DECL_HIDDEN_FRIEND_P (old_fn))
4047 continue; /* This is an anticipated builtin. */
4048 else if (!matching_fn_p (new_fn, old_fn))
4049 continue; /* Parameters do not match. */
4050 else if (decls_match (new_fn, old_fn))
4051 found = true;
4052 else
4054 diagnose_name_conflict (new_fn, old_fn);
4055 found = true;
4059 if (!found)
4060 /* Unlike the overload case we don't drop anticipated
4061 builtins here. They don't cause a problem, and
4062 we'd like to match them with a future
4063 declaration. */
4064 value = ovl_insert (new_fn, value, true);
4067 else if (value
4068 /* Ignore anticipated builtins. */
4069 && !anticipated_builtin_p (value)
4070 && !decls_match (lookup.value, value))
4071 diagnose_name_conflict (lookup.value, value);
4072 else
4073 value = lookup.value;
4076 if (lookup.type && lookup.type != type)
4078 if (type && !decls_match (lookup.type, type))
4079 diagnose_name_conflict (lookup.type, type);
4080 else
4081 type = lookup.type;
4084 /* If bind->value is empty, shift any class or enumeration name back. */
4085 if (!value)
4087 value = type;
4088 type = NULL_TREE;
4091 *value_p = value;
4092 *type_p = type;
4095 /* Returns true if ANCESTOR encloses DESCENDANT, including matching.
4096 Both are namespaces. */
4098 bool
4099 is_nested_namespace (tree ancestor, tree descendant, bool inline_only)
4101 int depth = SCOPE_DEPTH (ancestor);
4103 if (!depth && !inline_only)
4104 /* The global namespace encloses everything. */
4105 return true;
4107 while (SCOPE_DEPTH (descendant) > depth
4108 && (!inline_only || DECL_NAMESPACE_INLINE_P (descendant)))
4109 descendant = CP_DECL_CONTEXT (descendant);
4111 return ancestor == descendant;
4114 /* Returns true if ROOT (a namespace, class, or function) encloses
4115 CHILD. CHILD may be either a class type or a namespace. */
4117 bool
4118 is_ancestor (tree root, tree child)
4120 gcc_assert ((TREE_CODE (root) == NAMESPACE_DECL
4121 || TREE_CODE (root) == FUNCTION_DECL
4122 || CLASS_TYPE_P (root)));
4123 gcc_assert ((TREE_CODE (child) == NAMESPACE_DECL
4124 || CLASS_TYPE_P (child)));
4126 /* The global namespace encloses everything. */
4127 if (root == global_namespace)
4128 return true;
4130 /* Search until we reach namespace scope. */
4131 while (TREE_CODE (child) != NAMESPACE_DECL)
4133 /* If we've reached the ROOT, it encloses CHILD. */
4134 if (root == child)
4135 return true;
4136 /* Go out one level. */
4137 if (TYPE_P (child))
4138 child = TYPE_NAME (child);
4139 child = CP_DECL_CONTEXT (child);
4142 if (TREE_CODE (root) == NAMESPACE_DECL)
4143 return is_nested_namespace (root, child);
4145 return false;
4148 /* Enter the class or namespace scope indicated by T suitable for name
4149 lookup. T can be arbitrary scope, not necessary nested inside the
4150 current scope. Returns a non-null scope to pop iff pop_scope
4151 should be called later to exit this scope. */
4153 tree
4154 push_scope (tree t)
4156 if (TREE_CODE (t) == NAMESPACE_DECL)
4157 push_decl_namespace (t);
4158 else if (CLASS_TYPE_P (t))
4160 if (!at_class_scope_p ()
4161 || !same_type_p (current_class_type, t))
4162 push_nested_class (t);
4163 else
4164 /* T is the same as the current scope. There is therefore no
4165 need to re-enter the scope. Since we are not actually
4166 pushing a new scope, our caller should not call
4167 pop_scope. */
4168 t = NULL_TREE;
4171 return t;
4174 /* Leave scope pushed by push_scope. */
4176 void
4177 pop_scope (tree t)
4179 if (t == NULL_TREE)
4180 return;
4181 if (TREE_CODE (t) == NAMESPACE_DECL)
4182 pop_decl_namespace ();
4183 else if CLASS_TYPE_P (t)
4184 pop_nested_class ();
4187 /* Subroutine of push_inner_scope. */
4189 static void
4190 push_inner_scope_r (tree outer, tree inner)
4192 tree prev;
4194 if (outer == inner
4195 || (TREE_CODE (inner) != NAMESPACE_DECL && !CLASS_TYPE_P (inner)))
4196 return;
4198 prev = CP_DECL_CONTEXT (TREE_CODE (inner) == NAMESPACE_DECL ? inner : TYPE_NAME (inner));
4199 if (outer != prev)
4200 push_inner_scope_r (outer, prev);
4201 if (TREE_CODE (inner) == NAMESPACE_DECL)
4203 cp_binding_level *save_template_parm = 0;
4204 /* Temporary take out template parameter scopes. They are saved
4205 in reversed order in save_template_parm. */
4206 while (current_binding_level->kind == sk_template_parms)
4208 cp_binding_level *b = current_binding_level;
4209 current_binding_level = b->level_chain;
4210 b->level_chain = save_template_parm;
4211 save_template_parm = b;
4214 resume_scope (NAMESPACE_LEVEL (inner));
4215 current_namespace = inner;
4217 /* Restore template parameter scopes. */
4218 while (save_template_parm)
4220 cp_binding_level *b = save_template_parm;
4221 save_template_parm = b->level_chain;
4222 b->level_chain = current_binding_level;
4223 current_binding_level = b;
4226 else
4227 pushclass (inner);
4230 /* Enter the scope INNER from current scope. INNER must be a scope
4231 nested inside current scope. This works with both name lookup and
4232 pushing name into scope. In case a template parameter scope is present,
4233 namespace is pushed under the template parameter scope according to
4234 name lookup rule in 14.6.1/6.
4236 Return the former current scope suitable for pop_inner_scope. */
4238 tree
4239 push_inner_scope (tree inner)
4241 tree outer = current_scope ();
4242 if (!outer)
4243 outer = current_namespace;
4245 push_inner_scope_r (outer, inner);
4246 return outer;
4249 /* Exit the current scope INNER back to scope OUTER. */
4251 void
4252 pop_inner_scope (tree outer, tree inner)
4254 if (outer == inner
4255 || (TREE_CODE (inner) != NAMESPACE_DECL && !CLASS_TYPE_P (inner)))
4256 return;
4258 while (outer != inner)
4260 if (TREE_CODE (inner) == NAMESPACE_DECL)
4262 cp_binding_level *save_template_parm = 0;
4263 /* Temporary take out template parameter scopes. They are saved
4264 in reversed order in save_template_parm. */
4265 while (current_binding_level->kind == sk_template_parms)
4267 cp_binding_level *b = current_binding_level;
4268 current_binding_level = b->level_chain;
4269 b->level_chain = save_template_parm;
4270 save_template_parm = b;
4273 pop_namespace ();
4275 /* Restore template parameter scopes. */
4276 while (save_template_parm)
4278 cp_binding_level *b = save_template_parm;
4279 save_template_parm = b->level_chain;
4280 b->level_chain = current_binding_level;
4281 current_binding_level = b;
4284 else
4285 popclass ();
4287 inner = CP_DECL_CONTEXT (TREE_CODE (inner) == NAMESPACE_DECL ? inner : TYPE_NAME (inner));
4291 /* Do a pushlevel for class declarations. */
4293 void
4294 pushlevel_class (void)
4296 class_binding_level = begin_scope (sk_class, current_class_type);
4299 /* ...and a poplevel for class declarations. */
4301 void
4302 poplevel_class (void)
4304 cp_binding_level *level = class_binding_level;
4305 cp_class_binding *cb;
4306 size_t i;
4307 tree shadowed;
4309 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4310 gcc_assert (level != 0);
4312 /* If we're leaving a toplevel class, cache its binding level. */
4313 if (current_class_depth == 1)
4314 previous_class_level = level;
4315 for (shadowed = level->type_shadowed;
4316 shadowed;
4317 shadowed = TREE_CHAIN (shadowed))
4318 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (shadowed), TREE_VALUE (shadowed));
4320 /* Remove the bindings for all of the class-level declarations. */
4321 if (level->class_shadowed)
4323 FOR_EACH_VEC_ELT (*level->class_shadowed, i, cb)
4325 IDENTIFIER_BINDING (cb->identifier) = cb->base->previous;
4326 cxx_binding_free (cb->base);
4328 ggc_free (level->class_shadowed);
4329 level->class_shadowed = NULL;
4332 /* Now, pop out of the binding level which we created up in the
4333 `pushlevel_class' routine. */
4334 gcc_assert (current_binding_level == level);
4335 leave_scope ();
4336 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4339 /* Set INHERITED_VALUE_BINDING_P on BINDING to true or false, as
4340 appropriate. DECL is the value to which a name has just been
4341 bound. CLASS_TYPE is the class in which the lookup occurred. */
4343 static void
4344 set_inherited_value_binding_p (cxx_binding *binding, tree decl,
4345 tree class_type)
4347 if (binding->value == decl && TREE_CODE (decl) != TREE_LIST)
4349 tree context;
4351 if (TREE_CODE (decl) == OVERLOAD)
4352 context = ovl_scope (decl);
4353 else
4355 gcc_assert (DECL_P (decl));
4356 context = context_for_name_lookup (decl);
4359 if (is_properly_derived_from (class_type, context))
4360 INHERITED_VALUE_BINDING_P (binding) = 1;
4361 else
4362 INHERITED_VALUE_BINDING_P (binding) = 0;
4364 else if (binding->value == decl)
4365 /* We only encounter a TREE_LIST when there is an ambiguity in the
4366 base classes. Such an ambiguity can be overridden by a
4367 definition in this class. */
4368 INHERITED_VALUE_BINDING_P (binding) = 1;
4369 else
4370 INHERITED_VALUE_BINDING_P (binding) = 0;
4373 /* Make the declaration of X appear in CLASS scope. */
4375 bool
4376 pushdecl_class_level (tree x)
4378 bool is_valid = true;
4379 bool subtime;
4381 /* Do nothing if we're adding to an outer lambda closure type,
4382 outer_binding will add it later if it's needed. */
4383 if (current_class_type != class_binding_level->this_entity)
4384 return true;
4386 subtime = timevar_cond_start (TV_NAME_LOOKUP);
4387 /* Get the name of X. */
4388 tree name = OVL_NAME (x);
4390 if (name)
4392 is_valid = push_class_level_binding (name, x);
4393 if (TREE_CODE (x) == TYPE_DECL)
4394 set_identifier_type_value (name, x);
4396 else if (ANON_AGGR_TYPE_P (TREE_TYPE (x)))
4398 /* If X is an anonymous aggregate, all of its members are
4399 treated as if they were members of the class containing the
4400 aggregate, for naming purposes. */
4401 location_t save_location = input_location;
4402 tree anon = TREE_TYPE (x);
4403 if (vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (anon))
4404 for (unsigned ix = member_vec->length (); ix--;)
4406 tree binding = (*member_vec)[ix];
4407 if (STAT_HACK_P (binding))
4409 if (!pushdecl_class_level (STAT_TYPE (binding)))
4410 is_valid = false;
4411 binding = STAT_DECL (binding);
4413 if (!pushdecl_class_level (binding))
4414 is_valid = false;
4416 else
4417 for (tree f = TYPE_FIELDS (anon); f; f = DECL_CHAIN (f))
4418 if (TREE_CODE (f) == FIELD_DECL)
4420 input_location = DECL_SOURCE_LOCATION (f);
4421 if (!pushdecl_class_level (f))
4422 is_valid = false;
4424 input_location = save_location;
4426 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4427 return is_valid;
4430 /* Return the BINDING (if any) for NAME in SCOPE, which is a class
4431 scope. If the value returned is non-NULL, and the PREVIOUS field
4432 is not set, callers must set the PREVIOUS field explicitly. */
4434 static cxx_binding *
4435 get_class_binding (tree name, cp_binding_level *scope)
4437 tree class_type;
4438 tree type_binding;
4439 tree value_binding;
4440 cxx_binding *binding;
4442 class_type = scope->this_entity;
4444 /* Get the type binding. */
4445 type_binding = lookup_member (class_type, name,
4446 /*protect=*/2, /*want_type=*/true,
4447 tf_warning_or_error);
4448 /* Get the value binding. */
4449 value_binding = lookup_member (class_type, name,
4450 /*protect=*/2, /*want_type=*/false,
4451 tf_warning_or_error);
4453 if (value_binding
4454 && (TREE_CODE (value_binding) == TYPE_DECL
4455 || DECL_CLASS_TEMPLATE_P (value_binding)
4456 || (TREE_CODE (value_binding) == TREE_LIST
4457 && TREE_TYPE (value_binding) == error_mark_node
4458 && (TREE_CODE (TREE_VALUE (value_binding))
4459 == TYPE_DECL))))
4460 /* We found a type binding, even when looking for a non-type
4461 binding. This means that we already processed this binding
4462 above. */
4464 else if (value_binding)
4466 if (TREE_CODE (value_binding) == TREE_LIST
4467 && TREE_TYPE (value_binding) == error_mark_node)
4468 /* NAME is ambiguous. */
4470 else if (BASELINK_P (value_binding))
4471 /* NAME is some overloaded functions. */
4472 value_binding = BASELINK_FUNCTIONS (value_binding);
4475 /* If we found either a type binding or a value binding, create a
4476 new binding object. */
4477 if (type_binding || value_binding)
4479 binding = new_class_binding (name,
4480 value_binding,
4481 type_binding,
4482 scope);
4483 /* This is a class-scope binding, not a block-scope binding. */
4484 LOCAL_BINDING_P (binding) = 0;
4485 set_inherited_value_binding_p (binding, value_binding, class_type);
4487 else
4488 binding = NULL;
4490 return binding;
4493 /* Make the declaration(s) of X appear in CLASS scope under the name
4494 NAME. Returns true if the binding is valid. */
4496 static bool
4497 push_class_level_binding_1 (tree name, tree x)
4499 cxx_binding *binding;
4500 tree decl = x;
4501 bool ok;
4503 /* The class_binding_level will be NULL if x is a template
4504 parameter name in a member template. */
4505 if (!class_binding_level)
4506 return true;
4508 if (name == error_mark_node)
4509 return false;
4511 /* Can happen for an erroneous declaration (c++/60384). */
4512 if (!identifier_p (name))
4514 gcc_assert (errorcount || sorrycount);
4515 return false;
4518 /* Check for invalid member names. But don't worry about a default
4519 argument-scope lambda being pushed after the class is complete. */
4520 gcc_assert (TYPE_BEING_DEFINED (current_class_type)
4521 || LAMBDA_TYPE_P (TREE_TYPE (decl)));
4522 /* Check that we're pushing into the right binding level. */
4523 gcc_assert (current_class_type == class_binding_level->this_entity);
4525 /* We could have been passed a tree list if this is an ambiguous
4526 declaration. If so, pull the declaration out because
4527 check_template_shadow will not handle a TREE_LIST. */
4528 if (TREE_CODE (decl) == TREE_LIST
4529 && TREE_TYPE (decl) == error_mark_node)
4530 decl = TREE_VALUE (decl);
4532 if (!check_template_shadow (decl))
4533 return false;
4535 /* [class.mem]
4537 If T is the name of a class, then each of the following shall
4538 have a name different from T:
4540 -- every static data member of class T;
4542 -- every member of class T that is itself a type;
4544 -- every enumerator of every member of class T that is an
4545 enumerated type;
4547 -- every member of every anonymous union that is a member of
4548 class T.
4550 (Non-static data members were also forbidden to have the same
4551 name as T until TC1.) */
4552 if ((VAR_P (x)
4553 || TREE_CODE (x) == CONST_DECL
4554 || (TREE_CODE (x) == TYPE_DECL
4555 && !DECL_SELF_REFERENCE_P (x))
4556 /* A data member of an anonymous union. */
4557 || (TREE_CODE (x) == FIELD_DECL
4558 && DECL_CONTEXT (x) != current_class_type))
4559 && DECL_NAME (x) == DECL_NAME (TYPE_NAME (current_class_type)))
4561 tree scope = context_for_name_lookup (x);
4562 if (TYPE_P (scope) && same_type_p (scope, current_class_type))
4564 error ("%qD has the same name as the class in which it is "
4565 "declared",
4567 return false;
4571 /* Get the current binding for NAME in this class, if any. */
4572 binding = IDENTIFIER_BINDING (name);
4573 if (!binding || binding->scope != class_binding_level)
4575 binding = get_class_binding (name, class_binding_level);
4576 /* If a new binding was created, put it at the front of the
4577 IDENTIFIER_BINDING list. */
4578 if (binding)
4580 binding->previous = IDENTIFIER_BINDING (name);
4581 IDENTIFIER_BINDING (name) = binding;
4585 /* If there is already a binding, then we may need to update the
4586 current value. */
4587 if (binding && binding->value)
4589 tree bval = binding->value;
4590 tree old_decl = NULL_TREE;
4591 tree target_decl = strip_using_decl (decl);
4592 tree target_bval = strip_using_decl (bval);
4594 if (INHERITED_VALUE_BINDING_P (binding))
4596 /* If the old binding was from a base class, and was for a
4597 tag name, slide it over to make room for the new binding.
4598 The old binding is still visible if explicitly qualified
4599 with a class-key. */
4600 if (TREE_CODE (target_bval) == TYPE_DECL
4601 && DECL_ARTIFICIAL (target_bval)
4602 && !(TREE_CODE (target_decl) == TYPE_DECL
4603 && DECL_ARTIFICIAL (target_decl)))
4605 old_decl = binding->type;
4606 binding->type = bval;
4607 binding->value = NULL_TREE;
4608 INHERITED_VALUE_BINDING_P (binding) = 0;
4610 else
4612 old_decl = bval;
4613 /* Any inherited type declaration is hidden by the type
4614 declaration in the derived class. */
4615 if (TREE_CODE (target_decl) == TYPE_DECL
4616 && DECL_ARTIFICIAL (target_decl))
4617 binding->type = NULL_TREE;
4620 else if (TREE_CODE (target_decl) == OVERLOAD
4621 && OVL_P (target_bval))
4622 old_decl = bval;
4623 else if (TREE_CODE (decl) == USING_DECL
4624 && TREE_CODE (bval) == USING_DECL
4625 && same_type_p (USING_DECL_SCOPE (decl),
4626 USING_DECL_SCOPE (bval)))
4627 /* This is a using redeclaration that will be diagnosed later
4628 in supplement_binding */
4630 else if (TREE_CODE (decl) == USING_DECL
4631 && TREE_CODE (bval) == USING_DECL
4632 && DECL_DEPENDENT_P (decl)
4633 && DECL_DEPENDENT_P (bval))
4634 return true;
4635 else if (TREE_CODE (decl) == USING_DECL
4636 && OVL_P (target_bval))
4637 old_decl = bval;
4638 else if (TREE_CODE (bval) == USING_DECL
4639 && OVL_P (target_decl))
4640 return true;
4642 if (old_decl && binding->scope == class_binding_level)
4644 binding->value = x;
4645 /* It is always safe to clear INHERITED_VALUE_BINDING_P
4646 here. This function is only used to register bindings
4647 from with the class definition itself. */
4648 INHERITED_VALUE_BINDING_P (binding) = 0;
4649 return true;
4653 /* Note that we declared this value so that we can issue an error if
4654 this is an invalid redeclaration of a name already used for some
4655 other purpose. */
4656 note_name_declared_in_class (name, decl);
4658 /* If we didn't replace an existing binding, put the binding on the
4659 stack of bindings for the identifier, and update the shadowed
4660 list. */
4661 if (binding && binding->scope == class_binding_level)
4662 /* Supplement the existing binding. */
4663 ok = supplement_binding (binding, decl);
4664 else
4666 /* Create a new binding. */
4667 push_binding (name, decl, class_binding_level);
4668 ok = true;
4671 return ok;
4674 /* Wrapper for push_class_level_binding_1. */
4676 bool
4677 push_class_level_binding (tree name, tree x)
4679 bool ret;
4680 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4681 ret = push_class_level_binding_1 (name, x);
4682 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4683 return ret;
4686 /* Process "using SCOPE::NAME" in a class scope. Return the
4687 USING_DECL created. */
4689 tree
4690 do_class_using_decl (tree scope, tree name)
4692 if (name == error_mark_node)
4693 return NULL_TREE;
4695 if (!scope || !TYPE_P (scope))
4697 error ("using-declaration for non-member at class scope");
4698 return NULL_TREE;
4701 /* Make sure the name is not invalid */
4702 if (TREE_CODE (name) == BIT_NOT_EXPR)
4704 error ("%<%T::%D%> names destructor", scope, name);
4705 return NULL_TREE;
4708 /* Using T::T declares inheriting ctors, even if T is a typedef. */
4709 if (MAYBE_CLASS_TYPE_P (scope)
4710 && (name == TYPE_IDENTIFIER (scope)
4711 || constructor_name_p (name, scope)))
4713 maybe_warn_cpp0x (CPP0X_INHERITING_CTORS);
4714 name = ctor_identifier;
4715 CLASSTYPE_NON_AGGREGATE (current_class_type) = true;
4718 /* Cannot introduce a constructor name. */
4719 if (constructor_name_p (name, current_class_type))
4721 error ("%<%T::%D%> names constructor in %qT",
4722 scope, name, current_class_type);
4723 return NULL_TREE;
4726 /* From [namespace.udecl]:
4728 A using-declaration used as a member-declaration shall refer to a
4729 member of a base class of the class being defined.
4731 In general, we cannot check this constraint in a template because
4732 we do not know the entire set of base classes of the current
4733 class type. Morover, if SCOPE is dependent, it might match a
4734 non-dependent base. */
4736 tree decl = NULL_TREE;
4737 if (!dependent_scope_p (scope))
4739 base_kind b_kind;
4740 tree binfo = lookup_base (current_class_type, scope, ba_any, &b_kind,
4741 tf_warning_or_error);
4742 if (b_kind < bk_proper_base)
4744 /* If there are dependent bases, scope might resolve at
4745 instantiation time, even if it isn't exactly one of the
4746 dependent bases. */
4747 if (b_kind == bk_same_type || !any_dependent_bases_p ())
4749 error_not_base_type (scope, current_class_type);
4750 return NULL_TREE;
4753 else if (name == ctor_identifier && !binfo_direct_p (binfo))
4755 error ("cannot inherit constructors from indirect base %qT", scope);
4756 return NULL_TREE;
4758 else if (!IDENTIFIER_CONV_OP_P (name)
4759 || !dependent_type_p (TREE_TYPE (name)))
4761 decl = lookup_member (binfo, name, 0, false, tf_warning_or_error);
4762 if (!decl)
4764 error ("no members matching %<%T::%D%> in %q#T", scope, name,
4765 scope);
4766 return NULL_TREE;
4769 /* The binfo from which the functions came does not matter. */
4770 if (BASELINK_P (decl))
4771 decl = BASELINK_FUNCTIONS (decl);
4775 tree value = build_lang_decl (USING_DECL, name, NULL_TREE);
4776 USING_DECL_DECLS (value) = decl;
4777 USING_DECL_SCOPE (value) = scope;
4778 DECL_DEPENDENT_P (value) = !decl;
4780 return value;
4784 /* Return the binding for NAME in NS. If NS is NULL, look in
4785 global_namespace. */
4787 tree
4788 get_namespace_binding (tree ns, tree name)
4790 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4791 if (!ns)
4792 ns = global_namespace;
4793 gcc_checking_assert (!DECL_NAMESPACE_ALIAS (ns));
4794 tree ret = find_namespace_value (ns, name);
4795 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4796 return ret;
4799 /* Push internal DECL into the global namespace. Does not do the
4800 full overload fn handling and does not add it to the list of things
4801 in the namespace. */
4803 void
4804 set_global_binding (tree decl)
4806 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4808 tree *slot = find_namespace_slot (global_namespace, DECL_NAME (decl), true);
4810 if (*slot)
4811 /* The user's placed something in the implementor's namespace. */
4812 diagnose_name_conflict (decl, MAYBE_STAT_DECL (*slot));
4814 /* Force the binding, so compiler internals continue to work. */
4815 *slot = decl;
4817 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4820 /* Set the context of a declaration to scope. Complain if we are not
4821 outside scope. */
4823 void
4824 set_decl_namespace (tree decl, tree scope, bool friendp)
4826 /* Get rid of namespace aliases. */
4827 scope = ORIGINAL_NAMESPACE (scope);
4829 /* It is ok for friends to be qualified in parallel space. */
4830 if (!friendp && !is_nested_namespace (current_namespace, scope))
4831 error ("declaration of %qD not in a namespace surrounding %qD",
4832 decl, scope);
4833 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
4835 /* See whether this has been declared in the namespace or inline
4836 children. */
4837 tree old = NULL_TREE;
4839 name_lookup lookup (DECL_NAME (decl), LOOKUP_HIDDEN);
4840 if (!lookup.search_qualified (scope, /*usings=*/false))
4841 /* No old declaration at all. */
4842 goto not_found;
4843 old = lookup.value;
4846 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
4847 if (TREE_CODE (old) == TREE_LIST)
4849 ambiguous:
4850 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
4851 error ("reference to %qD is ambiguous", decl);
4852 print_candidates (old);
4853 return;
4856 if (!DECL_DECLARES_FUNCTION_P (decl))
4858 /* Don't compare non-function decls with decls_match here, since
4859 it can't check for the correct constness at this
4860 point. pushdecl will find those errors later. */
4862 /* We might have found it in an inline namespace child of SCOPE. */
4863 if (TREE_CODE (decl) == TREE_CODE (old))
4864 DECL_CONTEXT (decl) = DECL_CONTEXT (old);
4866 found:
4867 /* Writing "N::i" to declare something directly in "N" is invalid. */
4868 if (CP_DECL_CONTEXT (decl) == current_namespace
4869 && at_namespace_scope_p ())
4870 error ("explicit qualification in declaration of %qD", decl);
4871 return;
4874 /* Since decl is a function, old should contain a function decl. */
4875 if (!OVL_P (old))
4876 goto not_found;
4878 /* We handle these in check_explicit_instantiation_namespace. */
4879 if (processing_explicit_instantiation)
4880 return;
4881 if (processing_template_decl || processing_specialization)
4882 /* We have not yet called push_template_decl to turn a
4883 FUNCTION_DECL into a TEMPLATE_DECL, so the declarations won't
4884 match. But, we'll check later, when we construct the
4885 template. */
4886 return;
4887 /* Instantiations or specializations of templates may be declared as
4888 friends in any namespace. */
4889 if (friendp && DECL_USE_TEMPLATE (decl))
4890 return;
4892 tree found;
4893 found = NULL_TREE;
4895 for (lkp_iterator iter (old); iter; ++iter)
4897 if (iter.using_p ())
4898 continue;
4900 tree ofn = *iter;
4902 /* Adjust DECL_CONTEXT first so decls_match will return true
4903 if DECL will match a declaration in an inline namespace. */
4904 DECL_CONTEXT (decl) = DECL_CONTEXT (ofn);
4905 if (decls_match (decl, ofn))
4907 if (found)
4909 /* We found more than one matching declaration. */
4910 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
4911 goto ambiguous;
4913 found = ofn;
4917 if (found)
4919 if (DECL_HIDDEN_FRIEND_P (found))
4921 pedwarn (DECL_SOURCE_LOCATION (decl), 0,
4922 "%qD has not been declared within %qD", decl, scope);
4923 inform (DECL_SOURCE_LOCATION (found),
4924 "only here as a %<friend%>");
4926 DECL_CONTEXT (decl) = DECL_CONTEXT (found);
4927 goto found;
4930 not_found:
4931 /* It didn't work, go back to the explicit scope. */
4932 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
4933 error ("%qD should have been declared inside %qD", decl, scope);
4936 /* Return the namespace where the current declaration is declared. */
4938 tree
4939 current_decl_namespace (void)
4941 tree result;
4942 /* If we have been pushed into a different namespace, use it. */
4943 if (!vec_safe_is_empty (decl_namespace_list))
4944 return decl_namespace_list->last ();
4946 if (current_class_type)
4947 result = decl_namespace_context (current_class_type);
4948 else if (current_function_decl)
4949 result = decl_namespace_context (current_function_decl);
4950 else
4951 result = current_namespace;
4952 return result;
4955 /* Process any ATTRIBUTES on a namespace definition. Returns true if
4956 attribute visibility is seen. */
4958 bool
4959 handle_namespace_attrs (tree ns, tree attributes)
4961 tree d;
4962 bool saw_vis = false;
4964 if (attributes == error_mark_node)
4965 return false;
4967 for (d = attributes; d; d = TREE_CHAIN (d))
4969 tree name = get_attribute_name (d);
4970 tree args = TREE_VALUE (d);
4972 if (is_attribute_p ("visibility", name))
4974 /* attribute visibility is a property of the syntactic block
4975 rather than the namespace as a whole, so we don't touch the
4976 NAMESPACE_DECL at all. */
4977 tree x = args ? TREE_VALUE (args) : NULL_TREE;
4978 if (x == NULL_TREE || TREE_CODE (x) != STRING_CST || TREE_CHAIN (args))
4980 warning (OPT_Wattributes,
4981 "%qD attribute requires a single NTBS argument",
4982 name);
4983 continue;
4986 if (!TREE_PUBLIC (ns))
4987 warning (OPT_Wattributes,
4988 "%qD attribute is meaningless since members of the "
4989 "anonymous namespace get local symbols", name);
4991 push_visibility (TREE_STRING_POINTER (x), 1);
4992 saw_vis = true;
4994 else if (is_attribute_p ("abi_tag", name))
4996 if (!DECL_NAME (ns))
4998 warning (OPT_Wattributes, "ignoring %qD attribute on anonymous "
4999 "namespace", name);
5000 continue;
5002 if (!DECL_NAMESPACE_INLINE_P (ns))
5004 warning (OPT_Wattributes, "ignoring %qD attribute on non-inline "
5005 "namespace", name);
5006 continue;
5008 if (!args)
5010 tree dn = DECL_NAME (ns);
5011 args = build_string (IDENTIFIER_LENGTH (dn) + 1,
5012 IDENTIFIER_POINTER (dn));
5013 TREE_TYPE (args) = char_array_type_node;
5014 args = fix_string_type (args);
5015 args = build_tree_list (NULL_TREE, args);
5017 if (check_abi_tag_args (args, name))
5018 DECL_ATTRIBUTES (ns) = tree_cons (name, args,
5019 DECL_ATTRIBUTES (ns));
5021 else
5023 warning (OPT_Wattributes, "%qD attribute directive ignored",
5024 name);
5025 continue;
5029 return saw_vis;
5032 /* Temporarily set the namespace for the current declaration. */
5034 void
5035 push_decl_namespace (tree decl)
5037 if (TREE_CODE (decl) != NAMESPACE_DECL)
5038 decl = decl_namespace_context (decl);
5039 vec_safe_push (decl_namespace_list, ORIGINAL_NAMESPACE (decl));
5042 /* [namespace.memdef]/2 */
5044 void
5045 pop_decl_namespace (void)
5047 decl_namespace_list->pop ();
5050 /* Process a namespace-alias declaration. */
5052 void
5053 do_namespace_alias (tree alias, tree name_space)
5055 if (name_space == error_mark_node)
5056 return;
5058 gcc_assert (TREE_CODE (name_space) == NAMESPACE_DECL);
5060 name_space = ORIGINAL_NAMESPACE (name_space);
5062 /* Build the alias. */
5063 alias = build_lang_decl (NAMESPACE_DECL, alias, void_type_node);
5064 DECL_NAMESPACE_ALIAS (alias) = name_space;
5065 DECL_EXTERNAL (alias) = 1;
5066 DECL_CONTEXT (alias) = FROB_CONTEXT (current_scope ());
5067 pushdecl (alias);
5069 /* Emit debug info for namespace alias. */
5070 if (!building_stmt_list_p ())
5071 (*debug_hooks->early_global_decl) (alias);
5074 /* Like pushdecl, only it places X in the current namespace,
5075 if appropriate. */
5077 tree
5078 pushdecl_namespace_level (tree x, bool is_friend)
5080 cp_binding_level *b = current_binding_level;
5081 tree t;
5083 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
5084 t = do_pushdecl_with_scope
5085 (x, NAMESPACE_LEVEL (current_namespace), is_friend);
5087 /* Now, the type_shadowed stack may screw us. Munge it so it does
5088 what we want. */
5089 if (TREE_CODE (t) == TYPE_DECL)
5091 tree name = DECL_NAME (t);
5092 tree newval;
5093 tree *ptr = (tree *)0;
5094 for (; !global_scope_p (b); b = b->level_chain)
5096 tree shadowed = b->type_shadowed;
5097 for (; shadowed; shadowed = TREE_CHAIN (shadowed))
5098 if (TREE_PURPOSE (shadowed) == name)
5100 ptr = &TREE_VALUE (shadowed);
5101 /* Can't break out of the loop here because sometimes
5102 a binding level will have duplicate bindings for
5103 PT names. It's gross, but I haven't time to fix it. */
5106 newval = TREE_TYPE (t);
5107 if (ptr == (tree *)0)
5109 /* @@ This shouldn't be needed. My test case "zstring.cc" trips
5110 up here if this is changed to an assertion. --KR */
5111 SET_IDENTIFIER_TYPE_VALUE (name, t);
5113 else
5115 *ptr = newval;
5118 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
5119 return t;
5122 /* Process a using-declaration appearing in namespace scope. */
5124 void
5125 finish_namespace_using_decl (tree decl, tree scope, tree name)
5127 tree orig_decl = decl;
5129 gcc_checking_assert (current_binding_level->kind == sk_namespace
5130 && !processing_template_decl);
5131 decl = validate_nonmember_using_decl (decl, scope, name);
5132 if (decl == NULL_TREE)
5133 return;
5135 tree *slot = find_namespace_slot (current_namespace, name, true);
5136 tree val = slot ? MAYBE_STAT_DECL (*slot) : NULL_TREE;
5137 tree type = slot ? MAYBE_STAT_TYPE (*slot) : NULL_TREE;
5138 do_nonmember_using_decl (scope, name, &val, &type);
5139 if (STAT_HACK_P (*slot))
5141 STAT_DECL (*slot) = val;
5142 STAT_TYPE (*slot) = type;
5144 else if (type)
5145 *slot = stat_hack (val, type);
5146 else
5147 *slot = val;
5149 /* Emit debug info. */
5150 cp_emit_debug_info_for_using (orig_decl, current_namespace);
5153 /* Process a using-declaration at function scope. */
5155 void
5156 finish_local_using_decl (tree decl, tree scope, tree name)
5158 tree orig_decl = decl;
5160 gcc_checking_assert (current_binding_level->kind != sk_class
5161 && current_binding_level->kind != sk_namespace);
5162 decl = validate_nonmember_using_decl (decl, scope, name);
5163 if (decl == NULL_TREE)
5164 return;
5166 add_decl_expr (decl);
5168 cxx_binding *binding = find_local_binding (current_binding_level, name);
5169 tree value = binding ? binding->value : NULL_TREE;
5170 tree type = binding ? binding->type : NULL_TREE;
5172 do_nonmember_using_decl (scope, name, &value, &type);
5174 if (!value)
5176 else if (binding && value == binding->value)
5178 else if (binding && binding->value && TREE_CODE (value) == OVERLOAD)
5180 update_local_overload (IDENTIFIER_BINDING (name), value);
5181 IDENTIFIER_BINDING (name)->value = value;
5183 else
5184 /* Install the new binding. */
5185 push_local_binding (name, value, true);
5187 if (!type)
5189 else if (binding && type == binding->type)
5191 else
5193 push_local_binding (name, type, true);
5194 set_identifier_type_value (name, type);
5197 /* Emit debug info. */
5198 if (!processing_template_decl)
5199 cp_emit_debug_info_for_using (orig_decl, current_scope ());
5202 /* Return the declarations that are members of the namespace NS. */
5204 tree
5205 cp_namespace_decls (tree ns)
5207 return NAMESPACE_LEVEL (ns)->names;
5210 /* Combine prefer_type and namespaces_only into flags. */
5212 static int
5213 lookup_flags (int prefer_type, int namespaces_only)
5215 if (namespaces_only)
5216 return LOOKUP_PREFER_NAMESPACES;
5217 if (prefer_type > 1)
5218 return LOOKUP_PREFER_TYPES;
5219 if (prefer_type > 0)
5220 return LOOKUP_PREFER_BOTH;
5221 return 0;
5224 /* Given a lookup that returned VAL, use FLAGS to decide if we want to
5225 ignore it or not. Subroutine of lookup_name_real and
5226 lookup_type_scope. */
5228 static bool
5229 qualify_lookup (tree val, int flags)
5231 if (val == NULL_TREE)
5232 return false;
5233 if ((flags & LOOKUP_PREFER_NAMESPACES) && TREE_CODE (val) == NAMESPACE_DECL)
5234 return true;
5235 if (flags & LOOKUP_PREFER_TYPES)
5237 tree target_val = strip_using_decl (val);
5238 if (TREE_CODE (target_val) == TYPE_DECL
5239 || TREE_CODE (target_val) == TEMPLATE_DECL)
5240 return true;
5242 if (flags & (LOOKUP_PREFER_NAMESPACES | LOOKUP_PREFER_TYPES))
5243 return false;
5244 /* Look through lambda things that we shouldn't be able to see. */
5245 if (!(flags & LOOKUP_HIDDEN) && is_lambda_ignored_entity (val))
5246 return false;
5247 return true;
5250 /* Is there a "using namespace std;" directive within USINGS? */
5252 static bool
5253 using_directives_contain_std_p (vec<tree, va_gc> *usings)
5255 if (!usings)
5256 return false;
5258 for (unsigned ix = usings->length (); ix--;)
5259 if ((*usings)[ix] == std_node)
5260 return true;
5262 return false;
5265 /* Is there a "using namespace std;" directive within the current
5266 namespace (or its ancestors)?
5267 Compare with name_lookup::search_unqualified. */
5269 static bool
5270 has_using_namespace_std_directive_p ()
5272 /* Look at local using-directives. */
5273 for (cp_binding_level *level = current_binding_level;
5274 level->kind != sk_namespace;
5275 level = level->level_chain)
5276 if (using_directives_contain_std_p (level->using_directives))
5277 return true;
5279 /* Look at this namespace and its ancestors. */
5280 for (tree scope = current_namespace; scope; scope = CP_DECL_CONTEXT (scope))
5282 if (using_directives_contain_std_p (DECL_NAMESPACE_USING (scope)))
5283 return true;
5285 if (scope == global_namespace)
5286 break;
5289 return false;
5292 /* Suggest alternatives for NAME, an IDENTIFIER_NODE for which name
5293 lookup failed. Search through all available namespaces and print out
5294 possible candidates. If no exact matches are found, and
5295 SUGGEST_MISSPELLINGS is true, then also look for near-matches and
5296 suggest the best near-match, if there is one. */
5298 void
5299 suggest_alternatives_for (location_t location, tree name,
5300 bool suggest_misspellings)
5302 vec<tree> candidates = vNULL;
5303 vec<tree> worklist = vNULL;
5304 unsigned limit = PARAM_VALUE (CXX_MAX_NAMESPACES_FOR_DIAGNOSTIC_HELP);
5305 bool limited = false;
5307 /* Breadth-first search of namespaces. Up to limit namespaces
5308 searched (limit zero == unlimited). */
5309 worklist.safe_push (global_namespace);
5310 for (unsigned ix = 0; ix != worklist.length (); ix++)
5312 tree ns = worklist[ix];
5313 name_lookup lookup (name);
5315 if (lookup.search_qualified (ns, false))
5316 candidates.safe_push (lookup.value);
5318 if (!limited)
5320 /* Look for child namespaces. We have to do this
5321 indirectly because they are chained in reverse order,
5322 which is confusing to the user. */
5323 vec<tree> children = vNULL;
5325 for (tree decl = NAMESPACE_LEVEL (ns)->names;
5326 decl; decl = TREE_CHAIN (decl))
5327 if (TREE_CODE (decl) == NAMESPACE_DECL
5328 && !DECL_NAMESPACE_ALIAS (decl)
5329 && !DECL_NAMESPACE_INLINE_P (decl))
5330 children.safe_push (decl);
5332 while (!limited && !children.is_empty ())
5334 if (worklist.length () == limit)
5336 /* Unconditionally warn that the search was truncated. */
5337 inform (location,
5338 "maximum limit of %d namespaces searched for %qE",
5339 limit, name);
5340 limited = true;
5342 else
5343 worklist.safe_push (children.pop ());
5345 children.release ();
5348 worklist.release ();
5350 if (candidates.length ())
5352 inform_n (location, candidates.length (),
5353 "suggested alternative:",
5354 "suggested alternatives:");
5355 for (unsigned ix = 0; ix != candidates.length (); ix++)
5357 tree val = candidates[ix];
5359 inform (location_of (val), " %qE", val);
5361 candidates.release ();
5362 return;
5365 /* No candidates were found in the available namespaces. */
5367 /* If there's a "using namespace std;" active, and this
5368 is one of the most common "std::" names, then it's probably a
5369 missing #include. */
5370 if (has_using_namespace_std_directive_p ())
5371 if (maybe_suggest_missing_std_header (location, name))
5372 return;
5374 /* Otherwise, consider misspellings. */
5375 if (!suggest_misspellings)
5376 return;
5377 if (name_hint hint = lookup_name_fuzzy (name, FUZZY_LOOKUP_NAME,
5378 location))
5380 /* Show a spelling correction. */
5381 gcc_rich_location richloc (location);
5383 richloc.add_fixit_replace (hint.suggestion ());
5384 inform (&richloc, "suggested alternative: %qs", hint.suggestion ());
5388 /* A well-known name within the C++ standard library, returned by
5389 get_std_name_hint. */
5391 struct std_name_hint
5393 /* A name within "std::". */
5394 const char *name;
5396 /* The header name defining it within the C++ Standard Library
5397 (with '<' and '>'). */
5398 const char *header;
5400 /* The dialect of C++ in which this was added. */
5401 enum cxx_dialect min_dialect;
5404 /* Subroutine of maybe_suggest_missing_header for handling unrecognized names
5405 for some of the most common names within "std::".
5406 Given non-NULL NAME, return the std_name_hint for it, or NULL. */
5408 static const std_name_hint *
5409 get_std_name_hint (const char *name)
5411 static const std_name_hint hints[] = {
5412 /* <any>. */
5413 {"any", "<any>", cxx17},
5414 {"any_cast", "<any>", cxx17},
5415 {"make_any", "<any>", cxx17},
5416 /* <array>. */
5417 {"array", "<array>", cxx11},
5418 /* <atomic>. */
5419 {"atomic", "<atomic>", cxx11},
5420 {"atomic_flag", "<atomic>", cxx11},
5421 /* <bitset>. */
5422 {"bitset", "<bitset>", cxx11},
5423 /* <complex>. */
5424 {"complex", "<complex>", cxx98},
5425 {"complex_literals", "<complex>", cxx98},
5426 /* <condition_variable>. */
5427 {"condition_variable", "<condition_variable>", cxx11},
5428 {"condition_variable_any", "<condition_variable>", cxx11},
5429 /* <deque>. */
5430 {"deque", "<deque>", cxx98},
5431 /* <forward_list>. */
5432 {"forward_list", "<forward_list>", cxx11},
5433 /* <fstream>. */
5434 {"basic_filebuf", "<fstream>", cxx98},
5435 {"basic_ifstream", "<fstream>", cxx98},
5436 {"basic_ofstream", "<fstream>", cxx98},
5437 {"basic_fstream", "<fstream>", cxx98},
5438 {"fstream", "<fstream>", cxx98},
5439 {"ifstream", "<fstream>", cxx98},
5440 {"ofstream", "<fstream>", cxx98},
5441 /* <functional>. */
5442 {"bind", "<functional>", cxx11},
5443 {"function", "<functional>", cxx11},
5444 {"hash", "<functional>", cxx11},
5445 {"mem_fn", "<functional>", cxx11},
5446 /* <future>. */
5447 {"async", "<future>", cxx11},
5448 {"future", "<future>", cxx11},
5449 {"packaged_task", "<future>", cxx11},
5450 {"promise", "<future>", cxx11},
5451 /* <iostream>. */
5452 {"cin", "<iostream>", cxx98},
5453 {"cout", "<iostream>", cxx98},
5454 {"cerr", "<iostream>", cxx98},
5455 {"clog", "<iostream>", cxx98},
5456 {"wcin", "<iostream>", cxx98},
5457 {"wcout", "<iostream>", cxx98},
5458 {"wclog", "<iostream>", cxx98},
5459 /* <istream>. */
5460 {"istream", "<istream>", cxx98},
5461 /* <iterator>. */
5462 {"advance", "<iterator>", cxx98},
5463 {"back_inserter", "<iterator>", cxx98},
5464 {"begin", "<iterator>", cxx11},
5465 {"distance", "<iterator>", cxx98},
5466 {"end", "<iterator>", cxx11},
5467 {"front_inserter", "<iterator>", cxx98},
5468 {"inserter", "<iterator>", cxx98},
5469 {"istream_iterator", "<iterator>", cxx98},
5470 {"istreambuf_iterator", "<iterator>", cxx98},
5471 {"iterator_traits", "<iterator>", cxx98},
5472 {"move_iterator", "<iterator>", cxx11},
5473 {"next", "<iterator>", cxx11},
5474 {"ostream_iterator", "<iterator>", cxx98},
5475 {"ostreambuf_iterator", "<iterator>", cxx98},
5476 {"prev", "<iterator>", cxx11},
5477 {"reverse_iterator", "<iterator>", cxx98},
5478 /* <ostream>. */
5479 {"ostream", "<ostream>", cxx98},
5480 /* <list>. */
5481 {"list", "<list>", cxx98},
5482 /* <map>. */
5483 {"map", "<map>", cxx98},
5484 {"multimap", "<map>", cxx98},
5485 /* <memory>. */
5486 {"make_shared", "<memory>", cxx11},
5487 {"make_unique", "<memory>", cxx11},
5488 {"shared_ptr", "<memory>", cxx11},
5489 {"unique_ptr", "<memory>", cxx11},
5490 {"weak_ptr", "<memory>", cxx11},
5491 /* <mutex>. */
5492 {"mutex", "<mutex>", cxx11},
5493 {"timed_mutex", "<mutex>", cxx11},
5494 {"recursive_mutex", "<mutex>", cxx11},
5495 {"recursive_timed_mutex", "<mutex>", cxx11},
5496 {"once_flag", "<mutex>", cxx11},
5497 {"call_once,", "<mutex>", cxx11},
5498 {"lock", "<mutex>", cxx11},
5499 {"scoped_lock", "<mutex>", cxx17},
5500 {"try_lock", "<mutex>", cxx11},
5501 {"lock_guard", "<mutex>", cxx11},
5502 {"unique_lock", "<mutex>", cxx11},
5503 /* <optional>. */
5504 {"optional", "<optional>", cxx17},
5505 {"make_optional", "<optional>", cxx17},
5506 /* <ostream>. */
5507 {"ostream", "<ostream>", cxx98},
5508 {"wostream", "<ostream>", cxx98},
5509 {"ends", "<ostream>", cxx98},
5510 {"flush", "<ostream>", cxx98},
5511 {"endl", "<ostream>", cxx98},
5512 /* <queue>. */
5513 {"queue", "<queue>", cxx98},
5514 {"priority_queue", "<queue>", cxx98},
5515 /* <set>. */
5516 {"set", "<set>", cxx98},
5517 {"multiset", "<set>", cxx98},
5518 /* <shared_mutex>. */
5519 {"shared_lock", "<shared_mutex>", cxx14},
5520 {"shared_mutex", "<shared_mutex>", cxx17},
5521 {"shared_timed_mutex", "<shared_mutex>", cxx14},
5522 /* <sstream>. */
5523 {"basic_stringbuf", "<sstream>", cxx98},
5524 {"basic_istringstream", "<sstream>", cxx98},
5525 {"basic_ostringstream", "<sstream>", cxx98},
5526 {"basic_stringstream", "<sstream>", cxx98},
5527 {"istringstream", "<sstream>", cxx98},
5528 {"ostringstream", "<sstream>", cxx98},
5529 {"stringstream", "<sstream>", cxx98},
5530 /* <stack>. */
5531 {"stack", "<stack>", cxx98},
5532 /* <string>. */
5533 {"basic_string", "<string>", cxx98},
5534 {"string", "<string>", cxx98},
5535 {"wstring", "<string>", cxx98},
5536 {"u16string", "<string>", cxx11},
5537 {"u32string", "<string>", cxx11},
5538 /* <string_view>. */
5539 {"string_view", "<string_view>", cxx17},
5540 /* <thread>. */
5541 {"thread", "<thread>", cxx11},
5542 /* <tuple>. */
5543 {"make_tuple", "<tuple>", cxx11},
5544 {"tuple", "<tuple>", cxx11},
5545 {"tuple_element", "<tuple>", cxx11},
5546 {"tuple_size", "<tuple>", cxx11},
5547 /* <unordered_map>. */
5548 {"unordered_map", "<unordered_map>", cxx11},
5549 {"unordered_multimap", "<unordered_map>", cxx11},
5550 /* <unordered_set>. */
5551 {"unordered_set", "<unordered_set>", cxx11},
5552 {"unordered_multiset", "<unordered_set>", cxx11},
5553 /* <utility>. */
5554 {"declval", "<utility>", cxx11},
5555 {"forward", "<utility>", cxx11},
5556 {"make_pair", "<utility>", cxx98},
5557 {"move", "<utility>", cxx11},
5558 {"pair", "<utility>", cxx98},
5559 /* <variant>. */
5560 {"variant", "<variant>", cxx17},
5561 {"visit", "<variant>", cxx17},
5562 /* <vector>. */
5563 {"vector", "<vector>", cxx98},
5565 const size_t num_hints = sizeof (hints) / sizeof (hints[0]);
5566 for (size_t i = 0; i < num_hints; i++)
5568 if (strcmp (name, hints[i].name) == 0)
5569 return &hints[i];
5571 return NULL;
5574 /* Describe DIALECT. */
5576 static const char *
5577 get_cxx_dialect_name (enum cxx_dialect dialect)
5579 switch (dialect)
5581 default:
5582 gcc_unreachable ();
5583 case cxx98:
5584 return "C++98";
5585 case cxx11:
5586 return "C++11";
5587 case cxx14:
5588 return "C++14";
5589 case cxx17:
5590 return "C++17";
5591 case cxx2a:
5592 return "C++2a";
5596 /* Suggest pertinent header files for NAME at LOCATION, for common
5597 names within the "std" namespace.
5598 Return true iff a suggestion was offered. */
5600 static bool
5601 maybe_suggest_missing_std_header (location_t location, tree name)
5603 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
5605 const char *name_str = IDENTIFIER_POINTER (name);
5606 const std_name_hint *header_hint = get_std_name_hint (name_str);
5607 if (!header_hint)
5608 return false;
5610 gcc_rich_location richloc (location);
5611 if (cxx_dialect >= header_hint->min_dialect)
5613 const char *header = header_hint->header;
5614 maybe_add_include_fixit (&richloc, header);
5615 inform (&richloc,
5616 "%<std::%s%> is defined in header %qs;"
5617 " did you forget to %<#include %s%>?",
5618 name_str, header, header);
5620 else
5622 inform (&richloc,
5623 "%<std::%s%> is only available from %s onwards",
5624 name_str, get_cxx_dialect_name (header_hint->min_dialect));
5626 return true;
5629 /* If SCOPE is the "std" namespace, then suggest pertinent header
5630 files for NAME at LOCATION.
5631 Return true iff a suggestion was offered. */
5633 static bool
5634 maybe_suggest_missing_header (location_t location, tree name, tree scope)
5636 if (scope == NULL_TREE)
5637 return false;
5638 if (TREE_CODE (scope) != NAMESPACE_DECL)
5639 return false;
5640 /* We only offer suggestions for the "std" namespace. */
5641 if (scope != std_node)
5642 return false;
5643 return maybe_suggest_missing_std_header (location, name);
5646 /* Look for alternatives for NAME, an IDENTIFIER_NODE for which name
5647 lookup failed within the explicitly provided SCOPE. Suggest the
5648 the best meaningful candidates (if any) as a fix-it hint.
5649 Return true iff a suggestion was provided. */
5651 bool
5652 suggest_alternative_in_explicit_scope (location_t location, tree name,
5653 tree scope)
5655 /* Something went very wrong; don't suggest anything. */
5656 if (name == error_mark_node)
5657 return false;
5659 /* Resolve any namespace aliases. */
5660 scope = ORIGINAL_NAMESPACE (scope);
5662 if (maybe_suggest_missing_header (location, name, scope))
5663 return true;
5665 cp_binding_level *level = NAMESPACE_LEVEL (scope);
5667 best_match <tree, const char *> bm (name);
5668 consider_binding_level (name, bm, level, false, FUZZY_LOOKUP_NAME);
5670 /* See if we have a good suggesion for the user. */
5671 const char *fuzzy_name = bm.get_best_meaningful_candidate ();
5672 if (fuzzy_name)
5674 gcc_rich_location richloc (location);
5675 richloc.add_fixit_replace (fuzzy_name);
5676 inform (&richloc, "suggested alternative: %qs",
5677 fuzzy_name);
5678 return true;
5681 return false;
5684 /* Look up NAME (an IDENTIFIER_NODE) in SCOPE (either a NAMESPACE_DECL
5685 or a class TYPE).
5687 If PREFER_TYPE is > 0, we only return TYPE_DECLs or namespaces.
5688 If PREFER_TYPE is > 1, we only return TYPE_DECLs.
5690 Returns a DECL (or OVERLOAD, or BASELINK) representing the
5691 declaration found. If no suitable declaration can be found,
5692 ERROR_MARK_NODE is returned. If COMPLAIN is true and SCOPE is
5693 neither a class-type nor a namespace a diagnostic is issued. */
5695 tree
5696 lookup_qualified_name (tree scope, tree name, int prefer_type, bool complain,
5697 bool find_hidden)
5699 tree t = NULL_TREE;
5701 if (TREE_CODE (scope) == NAMESPACE_DECL)
5703 int flags = lookup_flags (prefer_type, /*namespaces_only*/false);
5704 if (find_hidden)
5705 flags |= LOOKUP_HIDDEN;
5706 name_lookup lookup (name, flags);
5708 if (qualified_namespace_lookup (scope, &lookup))
5709 t = lookup.value;
5711 else if (cxx_dialect != cxx98 && TREE_CODE (scope) == ENUMERAL_TYPE)
5712 t = lookup_enumerator (scope, name);
5713 else if (is_class_type (scope, complain))
5714 t = lookup_member (scope, name, 2, prefer_type, tf_warning_or_error);
5716 if (!t)
5717 return error_mark_node;
5718 return t;
5721 /* [namespace.qual]
5722 Accepts the NAME to lookup and its qualifying SCOPE.
5723 Returns the name/type pair found into the cxx_binding *RESULT,
5724 or false on error. */
5726 static bool
5727 qualified_namespace_lookup (tree scope, name_lookup *lookup)
5729 timevar_start (TV_NAME_LOOKUP);
5730 query_oracle (lookup->name);
5731 bool found = lookup->search_qualified (ORIGINAL_NAMESPACE (scope));
5732 timevar_stop (TV_NAME_LOOKUP);
5733 return found;
5736 /* Helper function for lookup_name_fuzzy.
5737 Traverse binding level LVL, looking for good name matches for NAME
5738 (and BM). */
5739 static void
5740 consider_binding_level (tree name, best_match <tree, const char *> &bm,
5741 cp_binding_level *lvl, bool look_within_fields,
5742 enum lookup_name_fuzzy_kind kind)
5744 if (look_within_fields)
5745 if (lvl->this_entity && TREE_CODE (lvl->this_entity) == RECORD_TYPE)
5747 tree type = lvl->this_entity;
5748 bool want_type_p = (kind == FUZZY_LOOKUP_TYPENAME);
5749 tree best_matching_field
5750 = lookup_member_fuzzy (type, name, want_type_p);
5751 if (best_matching_field)
5752 bm.consider (IDENTIFIER_POINTER (best_matching_field));
5755 /* Only suggest names reserved for the implementation if NAME begins
5756 with an underscore. */
5757 bool consider_implementation_names = (IDENTIFIER_POINTER (name)[0] == '_');
5759 for (tree t = lvl->names; t; t = TREE_CHAIN (t))
5761 tree d = t;
5763 /* OVERLOADs or decls from using declaration are wrapped into
5764 TREE_LIST. */
5765 if (TREE_CODE (d) == TREE_LIST)
5766 d = OVL_FIRST (TREE_VALUE (d));
5768 /* Don't use bindings from implicitly declared functions,
5769 as they were likely misspellings themselves. */
5770 if (TREE_TYPE (d) == error_mark_node)
5771 continue;
5773 /* Skip anticipated decls of builtin functions. */
5774 if (TREE_CODE (d) == FUNCTION_DECL
5775 && DECL_BUILT_IN (d)
5776 && DECL_ANTICIPATED (d))
5777 continue;
5779 /* Skip compiler-generated variables (e.g. __for_begin/__for_end
5780 within range for). */
5781 if (TREE_CODE (d) == VAR_DECL
5782 && DECL_ARTIFICIAL (d))
5783 continue;
5785 tree suggestion = DECL_NAME (d);
5786 if (!suggestion)
5787 continue;
5789 /* Don't suggest names that are for anonymous aggregate types, as
5790 they are an implementation detail generated by the compiler. */
5791 if (anon_aggrname_p (suggestion))
5792 continue;
5794 const char *suggestion_str = IDENTIFIER_POINTER (suggestion);
5796 /* Ignore internal names with spaces in them. */
5797 if (strchr (suggestion_str, ' '))
5798 continue;
5800 /* Don't suggest names that are reserved for use by the
5801 implementation, unless NAME began with an underscore. */
5802 if (name_reserved_for_implementation_p (suggestion_str)
5803 && !consider_implementation_names)
5804 continue;
5806 bm.consider (suggestion_str);
5810 /* Subclass of deferred_diagnostic. Notify the user that the
5811 given macro was used before it was defined.
5812 This can be done in the C++ frontend since tokenization happens
5813 upfront. */
5815 class macro_use_before_def : public deferred_diagnostic
5817 public:
5818 /* Factory function. Return a new macro_use_before_def instance if
5819 appropriate, or return NULL. */
5820 static macro_use_before_def *
5821 maybe_make (location_t use_loc, cpp_hashnode *macro)
5823 source_location def_loc = cpp_macro_definition_location (macro);
5824 if (def_loc == UNKNOWN_LOCATION)
5825 return NULL;
5827 /* We only want to issue a note if the macro was used *before* it was
5828 defined.
5829 We don't want to issue a note for cases where a macro was incorrectly
5830 used, leaving it unexpanded (e.g. by using the wrong argument
5831 count). */
5832 if (!linemap_location_before_p (line_table, use_loc, def_loc))
5833 return NULL;
5835 return new macro_use_before_def (use_loc, macro);
5838 private:
5839 /* Ctor. LOC is the location of the usage. MACRO is the
5840 macro that was used. */
5841 macro_use_before_def (location_t loc, cpp_hashnode *macro)
5842 : deferred_diagnostic (loc), m_macro (macro)
5844 gcc_assert (macro);
5847 ~macro_use_before_def ()
5849 if (is_suppressed_p ())
5850 return;
5852 inform (get_location (), "the macro %qs had not yet been defined",
5853 (const char *)m_macro->ident.str);
5854 inform (cpp_macro_definition_location (m_macro),
5855 "it was later defined here");
5858 private:
5859 cpp_hashnode *m_macro;
5862 /* Determine if it can ever make sense to offer RID as a suggestion for
5863 a misspelling.
5865 Subroutine of lookup_name_fuzzy. */
5867 static bool
5868 suggest_rid_p (enum rid rid)
5870 switch (rid)
5872 /* Support suggesting function-like keywords. */
5873 case RID_STATIC_ASSERT:
5874 return true;
5876 default:
5877 /* Support suggesting the various decl-specifier words, to handle
5878 e.g. "singed" vs "signed" typos. */
5879 if (cp_keyword_starts_decl_specifier_p (rid))
5880 return true;
5882 /* Otherwise, don't offer it. This avoids suggesting e.g. "if"
5883 and "do" for short misspellings, which are likely to lead to
5884 nonsensical results. */
5885 return false;
5889 /* Search for near-matches for NAME within the current bindings, and within
5890 macro names, returning the best match as a const char *, or NULL if
5891 no reasonable match is found.
5893 Use LOC for any deferred diagnostics. */
5895 name_hint
5896 lookup_name_fuzzy (tree name, enum lookup_name_fuzzy_kind kind, location_t loc)
5898 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
5900 /* First, try some well-known names in the C++ standard library, in case
5901 the user forgot a #include. */
5902 const char *header_hint
5903 = get_cp_stdlib_header_for_name (IDENTIFIER_POINTER (name));
5904 if (header_hint)
5905 return name_hint (NULL,
5906 new suggest_missing_header (loc,
5907 IDENTIFIER_POINTER (name),
5908 header_hint));
5910 best_match <tree, const char *> bm (name);
5912 cp_binding_level *lvl;
5913 for (lvl = scope_chain->class_bindings; lvl; lvl = lvl->level_chain)
5914 consider_binding_level (name, bm, lvl, true, kind);
5916 for (lvl = current_binding_level; lvl; lvl = lvl->level_chain)
5917 consider_binding_level (name, bm, lvl, false, kind);
5919 /* Consider macros: if the user misspelled a macro name e.g. "SOME_MACRO"
5921 x = SOME_OTHER_MACRO (y);
5922 then "SOME_OTHER_MACRO" will survive to the frontend and show up
5923 as a misspelled identifier.
5925 Use the best distance so far so that a candidate is only set if
5926 a macro is better than anything so far. This allows early rejection
5927 (without calculating the edit distance) of macro names that must have
5928 distance >= bm.get_best_distance (), and means that we only get a
5929 non-NULL result for best_macro_match if it's better than any of
5930 the identifiers already checked. */
5931 best_macro_match bmm (name, bm.get_best_distance (), parse_in);
5932 cpp_hashnode *best_macro = bmm.get_best_meaningful_candidate ();
5933 /* If a macro is the closest so far to NAME, consider it. */
5934 if (best_macro)
5935 bm.consider ((const char *)best_macro->ident.str);
5936 else if (bmm.get_best_distance () == 0)
5938 /* If we have an exact match for a macro name, then either the
5939 macro was used with the wrong argument count, or the macro
5940 has been used before it was defined. */
5941 cpp_hashnode *macro = bmm.blithely_get_best_candidate ();
5942 if (macro && (macro->flags & NODE_BUILTIN) == 0)
5943 return name_hint (NULL,
5944 macro_use_before_def::maybe_make (loc, macro));
5947 /* Try the "starts_decl_specifier_p" keywords to detect
5948 "singed" vs "signed" typos. */
5949 for (unsigned i = 0; i < num_c_common_reswords; i++)
5951 const c_common_resword *resword = &c_common_reswords[i];
5953 if (!suggest_rid_p (resword->rid))
5954 continue;
5956 tree resword_identifier = ridpointers [resword->rid];
5957 if (!resword_identifier)
5958 continue;
5959 gcc_assert (TREE_CODE (resword_identifier) == IDENTIFIER_NODE);
5961 /* Only consider reserved words that survived the
5962 filtering in init_reswords (e.g. for -std). */
5963 if (!IDENTIFIER_KEYWORD_P (resword_identifier))
5964 continue;
5966 bm.consider (IDENTIFIER_POINTER (resword_identifier));
5969 return name_hint (bm.get_best_meaningful_candidate (), NULL);
5972 /* Subroutine of outer_binding.
5974 Returns TRUE if BINDING is a binding to a template parameter of
5975 SCOPE. In that case SCOPE is the scope of a primary template
5976 parameter -- in the sense of G++, i.e, a template that has its own
5977 template header.
5979 Returns FALSE otherwise. */
5981 static bool
5982 binding_to_template_parms_of_scope_p (cxx_binding *binding,
5983 cp_binding_level *scope)
5985 tree binding_value, tmpl, tinfo;
5986 int level;
5988 if (!binding || !scope || !scope->this_entity)
5989 return false;
5991 binding_value = binding->value ? binding->value : binding->type;
5992 tinfo = get_template_info (scope->this_entity);
5994 /* BINDING_VALUE must be a template parm. */
5995 if (binding_value == NULL_TREE
5996 || (!DECL_P (binding_value)
5997 || !DECL_TEMPLATE_PARM_P (binding_value)))
5998 return false;
6000 /* The level of BINDING_VALUE. */
6001 level =
6002 template_type_parameter_p (binding_value)
6003 ? TEMPLATE_PARM_LEVEL (TEMPLATE_TYPE_PARM_INDEX
6004 (TREE_TYPE (binding_value)))
6005 : TEMPLATE_PARM_LEVEL (DECL_INITIAL (binding_value));
6007 /* The template of the current scope, iff said scope is a primary
6008 template. */
6009 tmpl = (tinfo
6010 && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
6011 ? TI_TEMPLATE (tinfo)
6012 : NULL_TREE);
6014 /* If the level of the parm BINDING_VALUE equals the depth of TMPL,
6015 then BINDING_VALUE is a parameter of TMPL. */
6016 return (tmpl && level == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)));
6019 /* Return the innermost non-namespace binding for NAME from a scope
6020 containing BINDING, or, if BINDING is NULL, the current scope.
6021 Please note that for a given template, the template parameters are
6022 considered to be in the scope containing the current scope.
6023 If CLASS_P is false, then class bindings are ignored. */
6025 cxx_binding *
6026 outer_binding (tree name,
6027 cxx_binding *binding,
6028 bool class_p)
6030 cxx_binding *outer;
6031 cp_binding_level *scope;
6032 cp_binding_level *outer_scope;
6034 if (binding)
6036 scope = binding->scope->level_chain;
6037 outer = binding->previous;
6039 else
6041 scope = current_binding_level;
6042 outer = IDENTIFIER_BINDING (name);
6044 outer_scope = outer ? outer->scope : NULL;
6046 /* Because we create class bindings lazily, we might be missing a
6047 class binding for NAME. If there are any class binding levels
6048 between the LAST_BINDING_LEVEL and the scope in which OUTER was
6049 declared, we must lookup NAME in those class scopes. */
6050 if (class_p)
6051 while (scope && scope != outer_scope && scope->kind != sk_namespace)
6053 if (scope->kind == sk_class)
6055 cxx_binding *class_binding;
6057 class_binding = get_class_binding (name, scope);
6058 if (class_binding)
6060 /* Thread this new class-scope binding onto the
6061 IDENTIFIER_BINDING list so that future lookups
6062 find it quickly. */
6063 class_binding->previous = outer;
6064 if (binding)
6065 binding->previous = class_binding;
6066 else
6067 IDENTIFIER_BINDING (name) = class_binding;
6068 return class_binding;
6071 /* If we are in a member template, the template parms of the member
6072 template are considered to be inside the scope of the containing
6073 class, but within G++ the class bindings are all pushed between the
6074 template parms and the function body. So if the outer binding is
6075 a template parm for the current scope, return it now rather than
6076 look for a class binding. */
6077 if (outer_scope && outer_scope->kind == sk_template_parms
6078 && binding_to_template_parms_of_scope_p (outer, scope))
6079 return outer;
6081 scope = scope->level_chain;
6084 return outer;
6087 /* Return the innermost block-scope or class-scope value binding for
6088 NAME, or NULL_TREE if there is no such binding. */
6090 tree
6091 innermost_non_namespace_value (tree name)
6093 cxx_binding *binding;
6094 binding = outer_binding (name, /*binding=*/NULL, /*class_p=*/true);
6095 return binding ? binding->value : NULL_TREE;
6098 /* Look up NAME in the current binding level and its superiors in the
6099 namespace of variables, functions and typedefs. Return a ..._DECL
6100 node of some kind representing its definition if there is only one
6101 such declaration, or return a TREE_LIST with all the overloaded
6102 definitions if there are many, or return 0 if it is undefined.
6103 Hidden name, either friend declaration or built-in function, are
6104 not ignored.
6106 If PREFER_TYPE is > 0, we prefer TYPE_DECLs or namespaces.
6107 If PREFER_TYPE is > 1, we reject non-type decls (e.g. namespaces).
6108 Otherwise we prefer non-TYPE_DECLs.
6110 If NONCLASS is nonzero, bindings in class scopes are ignored. If
6111 BLOCK_P is false, bindings in block scopes are ignored. */
6113 static tree
6114 lookup_name_real_1 (tree name, int prefer_type, int nonclass, bool block_p,
6115 int namespaces_only, int flags)
6117 cxx_binding *iter;
6118 tree val = NULL_TREE;
6120 query_oracle (name);
6122 /* Conversion operators are handled specially because ordinary
6123 unqualified name lookup will not find template conversion
6124 operators. */
6125 if (IDENTIFIER_CONV_OP_P (name))
6127 cp_binding_level *level;
6129 for (level = current_binding_level;
6130 level && level->kind != sk_namespace;
6131 level = level->level_chain)
6133 tree class_type;
6134 tree operators;
6136 /* A conversion operator can only be declared in a class
6137 scope. */
6138 if (level->kind != sk_class)
6139 continue;
6141 /* Lookup the conversion operator in the class. */
6142 class_type = level->this_entity;
6143 operators = lookup_fnfields (class_type, name, /*protect=*/0);
6144 if (operators)
6145 return operators;
6148 return NULL_TREE;
6151 flags |= lookup_flags (prefer_type, namespaces_only);
6153 /* First, look in non-namespace scopes. */
6155 if (current_class_type == NULL_TREE)
6156 nonclass = 1;
6158 if (block_p || !nonclass)
6159 for (iter = outer_binding (name, NULL, !nonclass);
6160 iter;
6161 iter = outer_binding (name, iter, !nonclass))
6163 tree binding;
6165 /* Skip entities we don't want. */
6166 if (LOCAL_BINDING_P (iter) ? !block_p : nonclass)
6167 continue;
6169 /* If this is the kind of thing we're looking for, we're done. */
6170 if (qualify_lookup (iter->value, flags))
6171 binding = iter->value;
6172 else if ((flags & LOOKUP_PREFER_TYPES)
6173 && qualify_lookup (iter->type, flags))
6174 binding = iter->type;
6175 else
6176 binding = NULL_TREE;
6178 if (binding)
6180 if (TREE_CODE (binding) == TYPE_DECL && DECL_HIDDEN_P (binding))
6182 /* A non namespace-scope binding can only be hidden in the
6183 presence of a local class, due to friend declarations.
6185 In particular, consider:
6187 struct C;
6188 void f() {
6189 struct A {
6190 friend struct B;
6191 friend struct C;
6192 void g() {
6193 B* b; // error: B is hidden
6194 C* c; // OK, finds ::C
6197 B *b; // error: B is hidden
6198 C *c; // OK, finds ::C
6199 struct B {};
6200 B *bb; // OK
6203 The standard says that "B" is a local class in "f"
6204 (but not nested within "A") -- but that name lookup
6205 for "B" does not find this declaration until it is
6206 declared directly with "f".
6208 In particular:
6210 [class.friend]
6212 If a friend declaration appears in a local class and
6213 the name specified is an unqualified name, a prior
6214 declaration is looked up without considering scopes
6215 that are outside the innermost enclosing non-class
6216 scope. For a friend function declaration, if there is
6217 no prior declaration, the program is ill-formed. For a
6218 friend class declaration, if there is no prior
6219 declaration, the class that is specified belongs to the
6220 innermost enclosing non-class scope, but if it is
6221 subsequently referenced, its name is not found by name
6222 lookup until a matching declaration is provided in the
6223 innermost enclosing nonclass scope.
6225 So just keep looking for a non-hidden binding.
6227 gcc_assert (TREE_CODE (binding) == TYPE_DECL);
6228 continue;
6230 val = binding;
6231 break;
6235 /* Now lookup in namespace scopes. */
6236 if (!val)
6238 name_lookup lookup (name, flags);
6239 if (lookup.search_unqualified
6240 (current_decl_namespace (), current_binding_level))
6241 val = lookup.value;
6244 /* If we have a single function from a using decl, pull it out. */
6245 if (val && TREE_CODE (val) == OVERLOAD && !really_overloaded_fn (val))
6246 val = OVL_FUNCTION (val);
6248 return val;
6251 /* Wrapper for lookup_name_real_1. */
6253 tree
6254 lookup_name_real (tree name, int prefer_type, int nonclass, bool block_p,
6255 int namespaces_only, int flags)
6257 tree ret;
6258 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6259 ret = lookup_name_real_1 (name, prefer_type, nonclass, block_p,
6260 namespaces_only, flags);
6261 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6262 return ret;
6265 tree
6266 lookup_name_nonclass (tree name)
6268 return lookup_name_real (name, 0, 1, /*block_p=*/true, 0, 0);
6271 tree
6272 lookup_name (tree name)
6274 return lookup_name_real (name, 0, 0, /*block_p=*/true, 0, 0);
6277 tree
6278 lookup_name_prefer_type (tree name, int prefer_type)
6280 return lookup_name_real (name, prefer_type, 0, /*block_p=*/true, 0, 0);
6283 /* Look up NAME for type used in elaborated name specifier in
6284 the scopes given by SCOPE. SCOPE can be either TS_CURRENT or
6285 TS_WITHIN_ENCLOSING_NON_CLASS. Although not implied by the
6286 name, more scopes are checked if cleanup or template parameter
6287 scope is encountered.
6289 Unlike lookup_name_real, we make sure that NAME is actually
6290 declared in the desired scope, not from inheritance, nor using
6291 directive. For using declaration, there is DR138 still waiting
6292 to be resolved. Hidden name coming from an earlier friend
6293 declaration is also returned.
6295 A TYPE_DECL best matching the NAME is returned. Catching error
6296 and issuing diagnostics are caller's responsibility. */
6298 static tree
6299 lookup_type_scope_1 (tree name, tag_scope scope)
6301 cxx_binding *iter = NULL;
6302 tree val = NULL_TREE;
6303 cp_binding_level *level = NULL;
6305 /* Look in non-namespace scope first. */
6306 if (current_binding_level->kind != sk_namespace)
6307 iter = outer_binding (name, NULL, /*class_p=*/ true);
6308 for (; iter; iter = outer_binding (name, iter, /*class_p=*/ true))
6310 /* Check if this is the kind of thing we're looking for.
6311 If SCOPE is TS_CURRENT, also make sure it doesn't come from
6312 base class. For ITER->VALUE, we can simply use
6313 INHERITED_VALUE_BINDING_P. For ITER->TYPE, we have to use
6314 our own check.
6316 We check ITER->TYPE before ITER->VALUE in order to handle
6317 typedef struct C {} C;
6318 correctly. */
6320 if (qualify_lookup (iter->type, LOOKUP_PREFER_TYPES)
6321 && (scope != ts_current
6322 || LOCAL_BINDING_P (iter)
6323 || DECL_CONTEXT (iter->type) == iter->scope->this_entity))
6324 val = iter->type;
6325 else if ((scope != ts_current
6326 || !INHERITED_VALUE_BINDING_P (iter))
6327 && qualify_lookup (iter->value, LOOKUP_PREFER_TYPES))
6328 val = iter->value;
6330 if (val)
6331 break;
6334 /* Look in namespace scope. */
6335 if (val)
6336 level = iter->scope;
6337 else
6339 tree ns = current_decl_namespace ();
6341 if (tree *slot = find_namespace_slot (ns, name))
6343 /* If this is the kind of thing we're looking for, we're done. */
6344 if (tree type = MAYBE_STAT_TYPE (*slot))
6345 if (qualify_lookup (type, LOOKUP_PREFER_TYPES))
6346 val = type;
6347 if (!val)
6349 if (tree decl = MAYBE_STAT_DECL (*slot))
6350 if (qualify_lookup (decl, LOOKUP_PREFER_TYPES))
6351 val = decl;
6353 level = NAMESPACE_LEVEL (ns);
6357 /* Type found, check if it is in the allowed scopes, ignoring cleanup
6358 and template parameter scopes. */
6359 if (val)
6361 cp_binding_level *b = current_binding_level;
6362 while (b)
6364 if (level == b)
6365 return val;
6367 if (b->kind == sk_cleanup || b->kind == sk_template_parms
6368 || b->kind == sk_function_parms)
6369 b = b->level_chain;
6370 else if (b->kind == sk_class
6371 && scope == ts_within_enclosing_non_class)
6372 b = b->level_chain;
6373 else
6374 break;
6378 return NULL_TREE;
6381 /* Wrapper for lookup_type_scope_1. */
6383 tree
6384 lookup_type_scope (tree name, tag_scope scope)
6386 tree ret;
6387 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6388 ret = lookup_type_scope_1 (name, scope);
6389 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6390 return ret;
6393 /* Returns true iff DECL is a block-scope extern declaration of a function
6394 or variable. */
6396 bool
6397 is_local_extern (tree decl)
6399 cxx_binding *binding;
6401 /* For functions, this is easy. */
6402 if (TREE_CODE (decl) == FUNCTION_DECL)
6403 return DECL_LOCAL_FUNCTION_P (decl);
6405 if (!VAR_P (decl))
6406 return false;
6407 if (!current_function_decl)
6408 return false;
6410 /* For variables, this is not easy. We need to look at the binding stack
6411 for the identifier to see whether the decl we have is a local. */
6412 for (binding = IDENTIFIER_BINDING (DECL_NAME (decl));
6413 binding && binding->scope->kind != sk_namespace;
6414 binding = binding->previous)
6415 if (binding->value == decl)
6416 return LOCAL_BINDING_P (binding);
6418 return false;
6421 /* The type TYPE is being declared. If it is a class template, or a
6422 specialization of a class template, do any processing required and
6423 perform error-checking. If IS_FRIEND is nonzero, this TYPE is
6424 being declared a friend. B is the binding level at which this TYPE
6425 should be bound.
6427 Returns the TYPE_DECL for TYPE, which may have been altered by this
6428 processing. */
6430 static tree
6431 maybe_process_template_type_declaration (tree type, int is_friend,
6432 cp_binding_level *b)
6434 tree decl = TYPE_NAME (type);
6436 if (processing_template_parmlist)
6437 /* You can't declare a new template type in a template parameter
6438 list. But, you can declare a non-template type:
6440 template <class A*> struct S;
6442 is a forward-declaration of `A'. */
6444 else if (b->kind == sk_namespace
6445 && current_binding_level->kind != sk_namespace)
6446 /* If this new type is being injected into a containing scope,
6447 then it's not a template type. */
6449 else
6451 gcc_assert (MAYBE_CLASS_TYPE_P (type)
6452 || TREE_CODE (type) == ENUMERAL_TYPE);
6454 if (processing_template_decl)
6456 /* This may change after the call to
6457 push_template_decl_real, but we want the original value. */
6458 tree name = DECL_NAME (decl);
6460 decl = push_template_decl_real (decl, is_friend);
6461 if (decl == error_mark_node)
6462 return error_mark_node;
6464 /* If the current binding level is the binding level for the
6465 template parameters (see the comment in
6466 begin_template_parm_list) and the enclosing level is a class
6467 scope, and we're not looking at a friend, push the
6468 declaration of the member class into the class scope. In the
6469 friend case, push_template_decl will already have put the
6470 friend into global scope, if appropriate. */
6471 if (TREE_CODE (type) != ENUMERAL_TYPE
6472 && !is_friend && b->kind == sk_template_parms
6473 && b->level_chain->kind == sk_class)
6475 finish_member_declaration (CLASSTYPE_TI_TEMPLATE (type));
6477 if (!COMPLETE_TYPE_P (current_class_type))
6479 maybe_add_class_template_decl_list (current_class_type,
6480 type, /*friend_p=*/0);
6481 /* Put this UTD in the table of UTDs for the class. */
6482 if (CLASSTYPE_NESTED_UTDS (current_class_type) == NULL)
6483 CLASSTYPE_NESTED_UTDS (current_class_type) =
6484 binding_table_new (SCOPE_DEFAULT_HT_SIZE);
6486 binding_table_insert
6487 (CLASSTYPE_NESTED_UTDS (current_class_type), name, type);
6493 return decl;
6496 /* Push a tag name NAME for struct/class/union/enum type TYPE. In case
6497 that the NAME is a class template, the tag is processed but not pushed.
6499 The pushed scope depend on the SCOPE parameter:
6500 - When SCOPE is TS_CURRENT, put it into the inner-most non-sk_cleanup
6501 scope.
6502 - When SCOPE is TS_GLOBAL, put it in the inner-most non-class and
6503 non-template-parameter scope. This case is needed for forward
6504 declarations.
6505 - When SCOPE is TS_WITHIN_ENCLOSING_NON_CLASS, this is similar to
6506 TS_GLOBAL case except that names within template-parameter scopes
6507 are not pushed at all.
6509 Returns TYPE upon success and ERROR_MARK_NODE otherwise. */
6511 static tree
6512 do_pushtag (tree name, tree type, tag_scope scope)
6514 tree decl;
6516 cp_binding_level *b = current_binding_level;
6517 while (true)
6519 if (/* Cleanup scopes are not scopes from the point of view of
6520 the language. */
6521 b->kind == sk_cleanup
6522 /* Neither are function parameter scopes. */
6523 || b->kind == sk_function_parms
6524 /* Neither are the scopes used to hold template parameters
6525 for an explicit specialization. For an ordinary template
6526 declaration, these scopes are not scopes from the point of
6527 view of the language. */
6528 || (b->kind == sk_template_parms
6529 && (b->explicit_spec_p || scope == ts_global)))
6530 b = b->level_chain;
6531 else if (b->kind == sk_class
6532 && scope != ts_current)
6534 b = b->level_chain;
6535 if (b->kind == sk_template_parms)
6536 b = b->level_chain;
6538 else
6539 break;
6542 gcc_assert (identifier_p (name));
6544 /* Do C++ gratuitous typedefing. */
6545 if (identifier_type_value_1 (name) != type)
6547 tree tdef;
6548 int in_class = 0;
6549 tree context = TYPE_CONTEXT (type);
6551 if (! context)
6553 cp_binding_level *cb = b;
6554 while (cb->kind != sk_namespace
6555 && cb->kind != sk_class
6556 && (cb->kind != sk_function_parms
6557 || !cb->this_entity))
6558 cb = cb->level_chain;
6559 tree cs = cb->this_entity;
6561 gcc_checking_assert (TREE_CODE (cs) == FUNCTION_DECL
6562 ? cs == current_function_decl
6563 : TYPE_P (cs) ? cs == current_class_type
6564 : cs == current_namespace);
6566 if (scope == ts_current
6567 || (cs && TREE_CODE (cs) == FUNCTION_DECL))
6568 context = cs;
6569 else if (cs && TYPE_P (cs))
6570 /* When declaring a friend class of a local class, we want
6571 to inject the newly named class into the scope
6572 containing the local class, not the namespace
6573 scope. */
6574 context = decl_function_context (get_type_decl (cs));
6576 if (!context)
6577 context = current_namespace;
6579 if (b->kind == sk_class
6580 || (b->kind == sk_template_parms
6581 && b->level_chain->kind == sk_class))
6582 in_class = 1;
6584 tdef = create_implicit_typedef (name, type);
6585 DECL_CONTEXT (tdef) = FROB_CONTEXT (context);
6586 if (scope == ts_within_enclosing_non_class)
6588 /* This is a friend. Make this TYPE_DECL node hidden from
6589 ordinary name lookup. Its corresponding TEMPLATE_DECL
6590 will be marked in push_template_decl_real. */
6591 retrofit_lang_decl (tdef);
6592 DECL_ANTICIPATED (tdef) = 1;
6593 DECL_FRIEND_P (tdef) = 1;
6596 decl = maybe_process_template_type_declaration
6597 (type, scope == ts_within_enclosing_non_class, b);
6598 if (decl == error_mark_node)
6599 return decl;
6601 if (b->kind == sk_class)
6603 if (!TYPE_BEING_DEFINED (current_class_type))
6604 /* Don't push anywhere if the class is complete; a lambda in an
6605 NSDMI is not a member of the class. */
6607 else if (!PROCESSING_REAL_TEMPLATE_DECL_P ())
6608 /* Put this TYPE_DECL on the TYPE_FIELDS list for the
6609 class. But if it's a member template class, we want
6610 the TEMPLATE_DECL, not the TYPE_DECL, so this is done
6611 later. */
6612 finish_member_declaration (decl);
6613 else
6614 pushdecl_class_level (decl);
6616 else if (b->kind != sk_template_parms)
6618 decl = do_pushdecl_with_scope (decl, b, /*is_friend=*/false);
6619 if (decl == error_mark_node)
6620 return decl;
6622 if (DECL_CONTEXT (decl) == std_node
6623 && init_list_identifier == DECL_NAME (TYPE_NAME (type))
6624 && !CLASSTYPE_TEMPLATE_INFO (type))
6626 error ("declaration of %<std::initializer_list%> does not match "
6627 "%<#include <initializer_list>%>, isn't a template");
6628 return error_mark_node;
6632 if (! in_class)
6633 set_identifier_type_value_with_scope (name, tdef, b);
6635 TYPE_CONTEXT (type) = DECL_CONTEXT (decl);
6637 /* If this is a local class, keep track of it. We need this
6638 information for name-mangling, and so that it is possible to
6639 find all function definitions in a translation unit in a
6640 convenient way. (It's otherwise tricky to find a member
6641 function definition it's only pointed to from within a local
6642 class.) */
6643 if (TYPE_FUNCTION_SCOPE_P (type))
6645 if (processing_template_decl)
6647 /* Push a DECL_EXPR so we call pushtag at the right time in
6648 template instantiation rather than in some nested context. */
6649 add_decl_expr (decl);
6651 /* Lambdas use LAMBDA_EXPR_DISCRIMINATOR instead. */
6652 else if (!LAMBDA_TYPE_P (type))
6653 vec_safe_push (local_classes, type);
6657 if (b->kind == sk_class
6658 && !COMPLETE_TYPE_P (current_class_type))
6660 maybe_add_class_template_decl_list (current_class_type,
6661 type, /*friend_p=*/0);
6663 if (CLASSTYPE_NESTED_UTDS (current_class_type) == NULL)
6664 CLASSTYPE_NESTED_UTDS (current_class_type)
6665 = binding_table_new (SCOPE_DEFAULT_HT_SIZE);
6667 binding_table_insert
6668 (CLASSTYPE_NESTED_UTDS (current_class_type), name, type);
6671 decl = TYPE_NAME (type);
6672 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
6674 /* Set type visibility now if this is a forward declaration. */
6675 TREE_PUBLIC (decl) = 1;
6676 determine_visibility (decl);
6678 return type;
6681 /* Wrapper for do_pushtag. */
6683 tree
6684 pushtag (tree name, tree type, tag_scope scope)
6686 tree ret;
6687 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6688 ret = do_pushtag (name, type, scope);
6689 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6690 return ret;
6694 /* Subroutines for reverting temporarily to top-level for instantiation
6695 of templates and such. We actually need to clear out the class- and
6696 local-value slots of all identifiers, so that only the global values
6697 are at all visible. Simply setting current_binding_level to the global
6698 scope isn't enough, because more binding levels may be pushed. */
6699 struct saved_scope *scope_chain;
6701 /* Return true if ID has not already been marked. */
6703 static inline bool
6704 store_binding_p (tree id)
6706 if (!id || !IDENTIFIER_BINDING (id))
6707 return false;
6709 if (IDENTIFIER_MARKED (id))
6710 return false;
6712 return true;
6715 /* Add an appropriate binding to *OLD_BINDINGS which needs to already
6716 have enough space reserved. */
6718 static void
6719 store_binding (tree id, vec<cxx_saved_binding, va_gc> **old_bindings)
6721 cxx_saved_binding saved;
6723 gcc_checking_assert (store_binding_p (id));
6725 IDENTIFIER_MARKED (id) = 1;
6727 saved.identifier = id;
6728 saved.binding = IDENTIFIER_BINDING (id);
6729 saved.real_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
6730 (*old_bindings)->quick_push (saved);
6731 IDENTIFIER_BINDING (id) = NULL;
6734 static void
6735 store_bindings (tree names, vec<cxx_saved_binding, va_gc> **old_bindings)
6737 static vec<tree> bindings_need_stored;
6738 tree t, id;
6739 size_t i;
6741 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6742 for (t = names; t; t = TREE_CHAIN (t))
6744 if (TREE_CODE (t) == TREE_LIST)
6745 id = TREE_PURPOSE (t);
6746 else
6747 id = DECL_NAME (t);
6749 if (store_binding_p (id))
6750 bindings_need_stored.safe_push (id);
6752 if (!bindings_need_stored.is_empty ())
6754 vec_safe_reserve_exact (*old_bindings, bindings_need_stored.length ());
6755 for (i = 0; bindings_need_stored.iterate (i, &id); ++i)
6757 /* We can apparently have duplicates in NAMES. */
6758 if (store_binding_p (id))
6759 store_binding (id, old_bindings);
6761 bindings_need_stored.truncate (0);
6763 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6766 /* Like store_bindings, but NAMES is a vector of cp_class_binding
6767 objects, rather than a TREE_LIST. */
6769 static void
6770 store_class_bindings (vec<cp_class_binding, va_gc> *names,
6771 vec<cxx_saved_binding, va_gc> **old_bindings)
6773 static vec<tree> bindings_need_stored;
6774 size_t i;
6775 cp_class_binding *cb;
6777 for (i = 0; vec_safe_iterate (names, i, &cb); ++i)
6778 if (store_binding_p (cb->identifier))
6779 bindings_need_stored.safe_push (cb->identifier);
6780 if (!bindings_need_stored.is_empty ())
6782 tree id;
6783 vec_safe_reserve_exact (*old_bindings, bindings_need_stored.length ());
6784 for (i = 0; bindings_need_stored.iterate (i, &id); ++i)
6785 store_binding (id, old_bindings);
6786 bindings_need_stored.truncate (0);
6790 /* A chain of saved_scope structures awaiting reuse. */
6792 static GTY((deletable)) struct saved_scope *free_saved_scope;
6794 static void
6795 do_push_to_top_level (void)
6797 struct saved_scope *s;
6798 cp_binding_level *b;
6799 cxx_saved_binding *sb;
6800 size_t i;
6801 bool need_pop;
6803 /* Reuse or create a new structure for this saved scope. */
6804 if (free_saved_scope != NULL)
6806 s = free_saved_scope;
6807 free_saved_scope = s->prev;
6809 vec<cxx_saved_binding, va_gc> *old_bindings = s->old_bindings;
6810 memset (s, 0, sizeof (*s));
6811 /* Also reuse the structure's old_bindings vector. */
6812 vec_safe_truncate (old_bindings, 0);
6813 s->old_bindings = old_bindings;
6815 else
6816 s = ggc_cleared_alloc<saved_scope> ();
6818 b = scope_chain ? current_binding_level : 0;
6820 /* If we're in the middle of some function, save our state. */
6821 if (cfun)
6823 need_pop = true;
6824 push_function_context ();
6826 else
6827 need_pop = false;
6829 if (scope_chain && previous_class_level)
6830 store_class_bindings (previous_class_level->class_shadowed,
6831 &s->old_bindings);
6833 /* Have to include the global scope, because class-scope decls
6834 aren't listed anywhere useful. */
6835 for (; b; b = b->level_chain)
6837 tree t;
6839 /* Template IDs are inserted into the global level. If they were
6840 inserted into namespace level, finish_file wouldn't find them
6841 when doing pending instantiations. Therefore, don't stop at
6842 namespace level, but continue until :: . */
6843 if (global_scope_p (b))
6844 break;
6846 store_bindings (b->names, &s->old_bindings);
6847 /* We also need to check class_shadowed to save class-level type
6848 bindings, since pushclass doesn't fill in b->names. */
6849 if (b->kind == sk_class)
6850 store_class_bindings (b->class_shadowed, &s->old_bindings);
6852 /* Unwind type-value slots back to top level. */
6853 for (t = b->type_shadowed; t; t = TREE_CHAIN (t))
6854 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (t), TREE_VALUE (t));
6857 FOR_EACH_VEC_SAFE_ELT (s->old_bindings, i, sb)
6858 IDENTIFIER_MARKED (sb->identifier) = 0;
6860 s->prev = scope_chain;
6861 s->bindings = b;
6862 s->need_pop_function_context = need_pop;
6863 s->function_decl = current_function_decl;
6864 s->unevaluated_operand = cp_unevaluated_operand;
6865 s->inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
6866 s->x_stmt_tree.stmts_are_full_exprs_p = true;
6868 scope_chain = s;
6869 current_function_decl = NULL_TREE;
6870 current_lang_base = NULL;
6871 current_lang_name = lang_name_cplusplus;
6872 current_namespace = global_namespace;
6873 push_class_stack ();
6874 cp_unevaluated_operand = 0;
6875 c_inhibit_evaluation_warnings = 0;
6878 static void
6879 do_pop_from_top_level (void)
6881 struct saved_scope *s = scope_chain;
6882 cxx_saved_binding *saved;
6883 size_t i;
6885 /* Clear out class-level bindings cache. */
6886 if (previous_class_level)
6887 invalidate_class_lookup_cache ();
6888 pop_class_stack ();
6890 release_tree_vector (current_lang_base);
6892 scope_chain = s->prev;
6893 FOR_EACH_VEC_SAFE_ELT (s->old_bindings, i, saved)
6895 tree id = saved->identifier;
6897 IDENTIFIER_BINDING (id) = saved->binding;
6898 SET_IDENTIFIER_TYPE_VALUE (id, saved->real_type_value);
6901 /* If we were in the middle of compiling a function, restore our
6902 state. */
6903 if (s->need_pop_function_context)
6904 pop_function_context ();
6905 current_function_decl = s->function_decl;
6906 cp_unevaluated_operand = s->unevaluated_operand;
6907 c_inhibit_evaluation_warnings = s->inhibit_evaluation_warnings;
6909 /* Make this saved_scope structure available for reuse by
6910 push_to_top_level. */
6911 s->prev = free_saved_scope;
6912 free_saved_scope = s;
6915 /* Push into the scope of the namespace NS, even if it is deeply
6916 nested within another namespace. */
6918 static void
6919 do_push_nested_namespace (tree ns)
6921 if (ns == global_namespace)
6922 do_push_to_top_level ();
6923 else
6925 do_push_nested_namespace (CP_DECL_CONTEXT (ns));
6926 gcc_checking_assert
6927 (find_namespace_value (current_namespace, DECL_NAME (ns)) == ns);
6928 resume_scope (NAMESPACE_LEVEL (ns));
6929 current_namespace = ns;
6933 /* Pop back from the scope of the namespace NS, which was previously
6934 entered with push_nested_namespace. */
6936 static void
6937 do_pop_nested_namespace (tree ns)
6939 while (ns != global_namespace)
6941 ns = CP_DECL_CONTEXT (ns);
6942 current_namespace = ns;
6943 leave_scope ();
6946 do_pop_from_top_level ();
6949 /* Add TARGET to USINGS, if it does not already exist there.
6950 We used to build the complete graph of usings at this point, from
6951 the POV of the source namespaces. Now we build that as we perform
6952 the unqualified search. */
6954 static void
6955 add_using_namespace (vec<tree, va_gc> *&usings, tree target)
6957 if (usings)
6958 for (unsigned ix = usings->length (); ix--;)
6959 if ((*usings)[ix] == target)
6960 return;
6962 vec_safe_push (usings, target);
6965 /* Tell the debug system of a using directive. */
6967 static void
6968 emit_debug_info_using_namespace (tree from, tree target, bool implicit)
6970 /* Emit debugging info. */
6971 tree context = from != global_namespace ? from : NULL_TREE;
6972 debug_hooks->imported_module_or_decl (target, NULL_TREE, context, false,
6973 implicit);
6976 /* Process a namespace-scope using directive. */
6978 void
6979 finish_namespace_using_directive (tree target, tree attribs)
6981 gcc_checking_assert (namespace_bindings_p ());
6982 if (target == error_mark_node)
6983 return;
6985 add_using_namespace (DECL_NAMESPACE_USING (current_namespace),
6986 ORIGINAL_NAMESPACE (target));
6987 emit_debug_info_using_namespace (current_namespace,
6988 ORIGINAL_NAMESPACE (target), false);
6990 if (attribs == error_mark_node)
6991 return;
6993 for (tree a = attribs; a; a = TREE_CHAIN (a))
6995 tree name = get_attribute_name (a);
6996 if (is_attribute_p ("strong", name))
6998 warning (0, "strong using directive no longer supported");
6999 if (CP_DECL_CONTEXT (target) == current_namespace)
7000 inform (DECL_SOURCE_LOCATION (target),
7001 "you may use an inline namespace instead");
7003 else
7004 warning (OPT_Wattributes, "%qD attribute directive ignored", name);
7008 /* Process a function-scope using-directive. */
7010 void
7011 finish_local_using_directive (tree target, tree attribs)
7013 gcc_checking_assert (local_bindings_p ());
7014 if (target == error_mark_node)
7015 return;
7017 if (attribs)
7018 warning (OPT_Wattributes, "attributes ignored on local using directive");
7020 add_stmt (build_stmt (input_location, USING_STMT, target));
7022 add_using_namespace (current_binding_level->using_directives,
7023 ORIGINAL_NAMESPACE (target));
7026 /* Pushes X into the global namespace. */
7028 tree
7029 pushdecl_top_level (tree x, bool is_friend)
7031 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
7032 do_push_to_top_level ();
7033 x = pushdecl_namespace_level (x, is_friend);
7034 do_pop_from_top_level ();
7035 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
7036 return x;
7039 /* Pushes X into the global namespace and calls cp_finish_decl to
7040 register the variable, initializing it with INIT. */
7042 tree
7043 pushdecl_top_level_and_finish (tree x, tree init)
7045 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
7046 do_push_to_top_level ();
7047 x = pushdecl_namespace_level (x, false);
7048 cp_finish_decl (x, init, false, NULL_TREE, 0);
7049 do_pop_from_top_level ();
7050 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
7051 return x;
7054 /* Enter the namespaces from current_namerspace to NS. */
7056 static int
7057 push_inline_namespaces (tree ns)
7059 int count = 0;
7060 if (ns != current_namespace)
7062 gcc_assert (ns != global_namespace);
7063 count += push_inline_namespaces (CP_DECL_CONTEXT (ns));
7064 resume_scope (NAMESPACE_LEVEL (ns));
7065 current_namespace = ns;
7066 count++;
7068 return count;
7071 /* Push into the scope of the NAME namespace. If NAME is NULL_TREE,
7072 then we enter an anonymous namespace. If MAKE_INLINE is true, then
7073 we create an inline namespace (it is up to the caller to check upon
7074 redefinition). Return the number of namespaces entered. */
7077 push_namespace (tree name, bool make_inline)
7079 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
7080 int count = 0;
7082 /* We should not get here if the global_namespace is not yet constructed
7083 nor if NAME designates the global namespace: The global scope is
7084 constructed elsewhere. */
7085 gcc_checking_assert (global_namespace != NULL && name != global_identifier);
7087 tree ns = NULL_TREE;
7089 name_lookup lookup (name, 0);
7090 if (!lookup.search_qualified (current_namespace, /*usings=*/false))
7092 else if (TREE_CODE (lookup.value) != NAMESPACE_DECL)
7094 else if (tree dna = DECL_NAMESPACE_ALIAS (lookup.value))
7096 /* A namespace alias is not allowed here, but if the alias
7097 is for a namespace also inside the current scope,
7098 accept it with a diagnostic. That's better than dying
7099 horribly. */
7100 if (is_nested_namespace (current_namespace, CP_DECL_CONTEXT (dna)))
7102 error ("namespace alias %qD not allowed here, "
7103 "assuming %qD", lookup.value, dna);
7104 ns = dna;
7107 else
7108 ns = lookup.value;
7111 bool new_ns = false;
7112 if (ns)
7113 /* DR2061. NS might be a member of an inline namespace. We
7114 need to push into those namespaces. */
7115 count += push_inline_namespaces (CP_DECL_CONTEXT (ns));
7116 else
7118 ns = build_lang_decl (NAMESPACE_DECL, name, void_type_node);
7119 SCOPE_DEPTH (ns) = SCOPE_DEPTH (current_namespace) + 1;
7120 if (!SCOPE_DEPTH (ns))
7121 /* We only allow depth 255. */
7122 sorry ("cannot nest more than %d namespaces",
7123 SCOPE_DEPTH (current_namespace));
7124 DECL_CONTEXT (ns) = FROB_CONTEXT (current_namespace);
7125 new_ns = true;
7127 if (pushdecl (ns) == error_mark_node)
7128 ns = NULL_TREE;
7129 else
7131 if (!name)
7133 SET_DECL_ASSEMBLER_NAME (ns, anon_identifier);
7135 if (!make_inline)
7136 add_using_namespace (DECL_NAMESPACE_USING (current_namespace),
7137 ns);
7139 else if (TREE_PUBLIC (current_namespace))
7140 TREE_PUBLIC (ns) = 1;
7142 if (make_inline)
7144 DECL_NAMESPACE_INLINE_P (ns) = true;
7145 vec_safe_push (DECL_NAMESPACE_INLINEES (current_namespace), ns);
7148 if (!name || make_inline)
7149 emit_debug_info_using_namespace (current_namespace, ns, true);
7153 if (ns)
7155 if (make_inline && !DECL_NAMESPACE_INLINE_P (ns))
7157 error ("inline namespace must be specified at initial definition");
7158 inform (DECL_SOURCE_LOCATION (ns), "%qD defined here", ns);
7160 if (new_ns)
7161 begin_scope (sk_namespace, ns);
7162 else
7163 resume_scope (NAMESPACE_LEVEL (ns));
7164 current_namespace = ns;
7165 count++;
7168 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
7169 return count;
7172 /* Pop from the scope of the current namespace. */
7174 void
7175 pop_namespace (void)
7177 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
7179 gcc_assert (current_namespace != global_namespace);
7180 current_namespace = CP_DECL_CONTEXT (current_namespace);
7181 /* The binding level is not popped, as it might be re-opened later. */
7182 leave_scope ();
7184 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
7187 /* External entry points for do_{push_to/pop_from}_top_level. */
7189 void
7190 push_to_top_level (void)
7192 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
7193 do_push_to_top_level ();
7194 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
7197 void
7198 pop_from_top_level (void)
7200 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
7201 do_pop_from_top_level ();
7202 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
7205 /* External entry points for do_{push,pop}_nested_namespace. */
7207 void
7208 push_nested_namespace (tree ns)
7210 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
7211 do_push_nested_namespace (ns);
7212 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
7215 void
7216 pop_nested_namespace (tree ns)
7218 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
7219 gcc_assert (current_namespace == ns);
7220 do_pop_nested_namespace (ns);
7221 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
7224 /* Pop off extraneous binding levels left over due to syntax errors.
7225 We don't pop past namespaces, as they might be valid. */
7227 void
7228 pop_everything (void)
7230 if (ENABLE_SCOPE_CHECKING)
7231 verbatim ("XXX entering pop_everything ()\n");
7232 while (!namespace_bindings_p ())
7234 if (current_binding_level->kind == sk_class)
7235 pop_nested_class ();
7236 else
7237 poplevel (0, 0, 0);
7239 if (ENABLE_SCOPE_CHECKING)
7240 verbatim ("XXX leaving pop_everything ()\n");
7243 /* Emit debugging information for using declarations and directives.
7244 If input tree is overloaded fn then emit debug info for all
7245 candidates. */
7247 void
7248 cp_emit_debug_info_for_using (tree t, tree context)
7250 /* Don't try to emit any debug information if we have errors. */
7251 if (seen_error ())
7252 return;
7254 /* Ignore this FUNCTION_DECL if it refers to a builtin declaration
7255 of a builtin function. */
7256 if (TREE_CODE (t) == FUNCTION_DECL
7257 && DECL_EXTERNAL (t)
7258 && DECL_BUILT_IN (t))
7259 return;
7261 /* Do not supply context to imported_module_or_decl, if
7262 it is a global namespace. */
7263 if (context == global_namespace)
7264 context = NULL_TREE;
7266 t = MAYBE_BASELINK_FUNCTIONS (t);
7268 /* FIXME: Handle TEMPLATE_DECLs. */
7269 for (lkp_iterator iter (t); iter; ++iter)
7271 tree fn = *iter;
7272 if (TREE_CODE (fn) != TEMPLATE_DECL)
7274 if (building_stmt_list_p ())
7275 add_stmt (build_stmt (input_location, USING_STMT, fn));
7276 else
7277 debug_hooks->imported_module_or_decl (fn, NULL_TREE, context,
7278 false, false);
7283 #include "gt-cp-name-lookup.h"