C++: simplify output from suggest_alternatives_for
[official-gcc.git] / gcc / cp / name-lookup.c
blob5b026da59e0bf9f34eb146a3d3ffa15e974849cb
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 name_hint maybe_suggest_missing_std_header (location_t location,
45 tree name);
46 static name_hint suggest_alternatives_for_1 (location_t location, tree name,
47 bool suggest_misspellings);
49 /* Create an overload suitable for recording an artificial TYPE_DECL
50 and another decl. We use this machanism to implement the struct
51 stat hack within a namespace. It'd be nice to use it everywhere. */
53 #define STAT_HACK_P(N) ((N) && TREE_CODE (N) == OVERLOAD && OVL_LOOKUP_P (N))
54 #define STAT_TYPE(N) TREE_TYPE (N)
55 #define STAT_DECL(N) OVL_FUNCTION (N)
56 #define MAYBE_STAT_DECL(N) (STAT_HACK_P (N) ? STAT_DECL (N) : N)
57 #define MAYBE_STAT_TYPE(N) (STAT_HACK_P (N) ? STAT_TYPE (N) : NULL_TREE)
59 /* Create a STAT_HACK node with DECL as the value binding and TYPE as
60 the type binding. */
62 static tree
63 stat_hack (tree decl = NULL_TREE, tree type = NULL_TREE)
65 tree result = make_node (OVERLOAD);
67 /* Mark this as a lookup, so we can tell this is a stat hack. */
68 OVL_LOOKUP_P (result) = true;
69 STAT_DECL (result) = decl;
70 STAT_TYPE (result) = type;
71 return result;
74 /* Create a local binding level for NAME. */
76 static cxx_binding *
77 create_local_binding (cp_binding_level *level, tree name)
79 cxx_binding *binding = cxx_binding_make (NULL, NULL);
81 INHERITED_VALUE_BINDING_P (binding) = false;
82 LOCAL_BINDING_P (binding) = true;
83 binding->scope = level;
84 binding->previous = IDENTIFIER_BINDING (name);
86 IDENTIFIER_BINDING (name) = binding;
88 return binding;
91 /* Find the binding for NAME in namespace NS. If CREATE_P is true,
92 make an empty binding if there wasn't one. */
94 static tree *
95 find_namespace_slot (tree ns, tree name, bool create_p = false)
97 tree *slot = DECL_NAMESPACE_BINDINGS (ns)
98 ->find_slot_with_hash (name, name ? IDENTIFIER_HASH_VALUE (name) : 0,
99 create_p ? INSERT : NO_INSERT);
100 return slot;
103 static tree
104 find_namespace_value (tree ns, tree name)
106 tree *b = find_namespace_slot (ns, name);
108 return b ? MAYBE_STAT_DECL (*b) : NULL_TREE;
111 /* Add DECL to the list of things declared in B. */
113 static void
114 add_decl_to_level (cp_binding_level *b, tree decl)
116 gcc_assert (b->kind != sk_class);
118 /* Make sure we don't create a circular list. xref_tag can end
119 up pushing the same artificial decl more than once. We
120 should have already detected that in update_binding. */
121 gcc_assert (b->names != decl);
123 /* We build up the list in reverse order, and reverse it later if
124 necessary. */
125 TREE_CHAIN (decl) = b->names;
126 b->names = decl;
128 /* If appropriate, add decl to separate list of statics. We
129 include extern variables because they might turn out to be
130 static later. It's OK for this list to contain a few false
131 positives. */
132 if (b->kind == sk_namespace
133 && ((VAR_P (decl)
134 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
135 || (TREE_CODE (decl) == FUNCTION_DECL
136 && (!TREE_PUBLIC (decl)
137 || decl_anon_ns_mem_p (decl)
138 || DECL_DECLARED_INLINE_P (decl)))))
139 vec_safe_push (static_decls, decl);
142 /* Find the binding for NAME in the local binding level B. */
144 static cxx_binding *
145 find_local_binding (cp_binding_level *b, tree name)
147 if (cxx_binding *binding = IDENTIFIER_BINDING (name))
148 for (;; b = b->level_chain)
150 if (binding->scope == b)
151 return binding;
153 /* Cleanup contours are transparent to the language. */
154 if (b->kind != sk_cleanup)
155 break;
157 return NULL;
160 struct name_lookup
162 public:
163 typedef std::pair<tree, tree> using_pair;
164 typedef vec<using_pair, va_heap, vl_embed> using_queue;
166 public:
167 tree name; /* The identifier being looked for. */
168 tree value; /* A (possibly ambiguous) set of things found. */
169 tree type; /* A type that has been found. */
170 int flags; /* Lookup flags. */
171 bool deduping; /* Full deduping is needed because using declarations
172 are in play. */
173 vec<tree, va_heap, vl_embed> *scopes;
174 name_lookup *previous; /* Previously active lookup. */
176 protected:
177 /* Marked scope stack for outermost name lookup. */
178 static vec<tree, va_heap, vl_embed> *shared_scopes;
179 /* Currently active lookup. */
180 static name_lookup *active;
182 public:
183 name_lookup (tree n, int f = 0)
184 : name (n), value (NULL_TREE), type (NULL_TREE), flags (f),
185 deduping (false), scopes (NULL), previous (NULL)
187 preserve_state ();
189 ~name_lookup ()
191 restore_state ();
194 private: /* Uncopyable, unmovable, unassignable. I am a rock. */
195 name_lookup (const name_lookup &);
196 name_lookup &operator= (const name_lookup &);
198 protected:
199 static bool seen_p (tree scope)
201 return LOOKUP_SEEN_P (scope);
203 static bool found_p (tree scope)
205 return LOOKUP_FOUND_P (scope);
208 void mark_seen (tree scope); /* Mark and add to scope vector. */
209 static void mark_found (tree scope)
211 gcc_checking_assert (seen_p (scope));
212 LOOKUP_FOUND_P (scope) = true;
214 bool see_and_mark (tree scope)
216 bool ret = seen_p (scope);
217 if (!ret)
218 mark_seen (scope);
219 return ret;
221 bool find_and_mark (tree scope);
223 private:
224 void preserve_state ();
225 void restore_state ();
227 private:
228 static tree ambiguous (tree thing, tree current);
229 void add_overload (tree fns);
230 void add_value (tree new_val);
231 void add_type (tree new_type);
232 bool process_binding (tree val_bind, tree type_bind);
234 /* Look in only namespace. */
235 bool search_namespace_only (tree scope);
236 /* Look in namespace and its (recursive) inlines. Ignore using
237 directives. Return true if something found (inc dups). */
238 bool search_namespace (tree scope);
239 /* Look in the using directives of namespace + inlines using
240 qualified lookup rules. */
241 bool search_usings (tree scope);
243 private:
244 using_queue *queue_namespace (using_queue *queue, int depth, tree scope);
245 using_queue *do_queue_usings (using_queue *queue, int depth,
246 vec<tree, va_gc> *usings);
247 using_queue *queue_usings (using_queue *queue, int depth,
248 vec<tree, va_gc> *usings)
250 if (usings)
251 queue = do_queue_usings (queue, depth, usings);
252 return queue;
255 private:
256 void add_fns (tree);
258 void adl_expr (tree);
259 void adl_type (tree);
260 void adl_template_arg (tree);
261 void adl_class (tree);
262 void adl_bases (tree);
263 void adl_class_only (tree);
264 void adl_namespace (tree);
265 void adl_namespace_only (tree);
267 public:
268 /* Search namespace + inlines + maybe usings as qualified lookup. */
269 bool search_qualified (tree scope, bool usings = true);
271 /* Search namespace + inlines + usings as unqualified lookup. */
272 bool search_unqualified (tree scope, cp_binding_level *);
274 /* ADL lookup of ARGS. */
275 tree search_adl (tree fns, vec<tree, va_gc> *args);
278 /* Scope stack shared by all outermost lookups. This avoids us
279 allocating and freeing on every single lookup. */
280 vec<tree, va_heap, vl_embed> *name_lookup::shared_scopes;
282 /* Currently active lookup. */
283 name_lookup *name_lookup::active;
285 /* Name lookup is recursive, becase ADL can cause template
286 instatiation. This is of course a rare event, so we optimize for
287 it not happening. When we discover an active name-lookup, which
288 must be an ADL lookup, we need to unmark the marked scopes and also
289 unmark the lookup we might have been accumulating. */
291 void
292 name_lookup::preserve_state ()
294 previous = active;
295 if (previous)
297 unsigned length = vec_safe_length (previous->scopes);
298 vec_safe_reserve (previous->scopes, length * 2);
299 for (unsigned ix = length; ix--;)
301 tree decl = (*previous->scopes)[ix];
303 gcc_checking_assert (LOOKUP_SEEN_P (decl));
304 LOOKUP_SEEN_P (decl) = false;
306 /* Preserve the FOUND_P state on the interrupted lookup's
307 stack. */
308 if (LOOKUP_FOUND_P (decl))
310 LOOKUP_FOUND_P (decl) = false;
311 previous->scopes->quick_push (decl);
315 /* Unmark the outer partial lookup. */
316 if (previous->deduping)
317 lookup_mark (previous->value, false);
319 else
320 scopes = shared_scopes;
321 active = this;
324 /* Restore the marking state of a lookup we interrupted. */
326 void
327 name_lookup::restore_state ()
329 if (deduping)
330 lookup_mark (value, false);
332 /* Unmark and empty this lookup's scope stack. */
333 for (unsigned ix = vec_safe_length (scopes); ix--;)
335 tree decl = scopes->pop ();
336 gcc_checking_assert (LOOKUP_SEEN_P (decl));
337 LOOKUP_SEEN_P (decl) = false;
338 LOOKUP_FOUND_P (decl) = false;
341 active = previous;
342 if (previous)
344 free (scopes);
346 unsigned length = vec_safe_length (previous->scopes);
347 for (unsigned ix = 0; ix != length; ix++)
349 tree decl = (*previous->scopes)[ix];
350 if (LOOKUP_SEEN_P (decl))
352 /* The remainder of the scope stack must be recording
353 FOUND_P decls, which we want to pop off. */
356 tree decl = previous->scopes->pop ();
357 gcc_checking_assert (LOOKUP_SEEN_P (decl)
358 && !LOOKUP_FOUND_P (decl));
359 LOOKUP_FOUND_P (decl) = true;
361 while (++ix != length);
362 break;
365 gcc_checking_assert (!LOOKUP_FOUND_P (decl));
366 LOOKUP_SEEN_P (decl) = true;
369 /* Remark the outer partial lookup. */
370 if (previous->deduping)
371 lookup_mark (previous->value, true);
373 else
374 shared_scopes = scopes;
377 void
378 name_lookup::mark_seen (tree scope)
380 gcc_checking_assert (!seen_p (scope));
381 LOOKUP_SEEN_P (scope) = true;
382 vec_safe_push (scopes, scope);
385 bool
386 name_lookup::find_and_mark (tree scope)
388 bool result = LOOKUP_FOUND_P (scope);
389 if (!result)
391 LOOKUP_FOUND_P (scope) = true;
392 if (!LOOKUP_SEEN_P (scope))
393 vec_safe_push (scopes, scope);
396 return result;
399 /* THING and CURRENT are ambiguous, concatenate them. */
401 tree
402 name_lookup::ambiguous (tree thing, tree current)
404 if (TREE_CODE (current) != TREE_LIST)
406 current = build_tree_list (NULL_TREE, current);
407 TREE_TYPE (current) = error_mark_node;
409 current = tree_cons (NULL_TREE, thing, current);
410 TREE_TYPE (current) = error_mark_node;
412 return current;
415 /* FNS is a new overload set to add to the exising set. */
417 void
418 name_lookup::add_overload (tree fns)
420 if (!deduping && TREE_CODE (fns) == OVERLOAD)
422 tree probe = fns;
423 if (flags & LOOKUP_HIDDEN)
424 probe = ovl_skip_hidden (probe);
425 if (probe && TREE_CODE (probe) == OVERLOAD && OVL_USING_P (probe))
427 /* We're about to add something found by a using
428 declaration, so need to engage deduping mode. */
429 lookup_mark (value, true);
430 deduping = true;
434 value = lookup_maybe_add (fns, value, deduping);
437 /* Add a NEW_VAL, a found value binding into the current value binding. */
439 void
440 name_lookup::add_value (tree new_val)
442 if (OVL_P (new_val) && (!value || OVL_P (value)))
443 add_overload (new_val);
444 else if (!value)
445 value = new_val;
446 else if (value == new_val)
448 else if ((TREE_CODE (value) == TYPE_DECL
449 && TREE_CODE (new_val) == TYPE_DECL
450 && same_type_p (TREE_TYPE (value), TREE_TYPE (new_val))))
451 /* Typedefs to the same type. */;
452 else if (TREE_CODE (value) == NAMESPACE_DECL
453 && TREE_CODE (new_val) == NAMESPACE_DECL
454 && ORIGINAL_NAMESPACE (value) == ORIGINAL_NAMESPACE (new_val))
455 /* Namespace (possibly aliased) to the same namespace. Locate
456 the namespace*/
457 value = ORIGINAL_NAMESPACE (value);
458 else
460 if (deduping)
462 /* Disengage deduping mode. */
463 lookup_mark (value, false);
464 deduping = false;
466 value = ambiguous (new_val, value);
470 /* Add a NEW_TYPE, a found type binding into the current type binding. */
472 void
473 name_lookup::add_type (tree new_type)
475 if (!type)
476 type = new_type;
477 else if (TREE_CODE (type) == TREE_LIST
478 || !same_type_p (TREE_TYPE (type), TREE_TYPE (new_type)))
479 type = ambiguous (new_type, type);
482 /* Process a found binding containing NEW_VAL and NEW_TYPE. Returns
483 true if we actually found something noteworthy. */
485 bool
486 name_lookup::process_binding (tree new_val, tree new_type)
488 /* Did we really see a type? */
489 if (new_type
490 && (LOOKUP_NAMESPACES_ONLY (flags)
491 || (!(flags & LOOKUP_HIDDEN)
492 && DECL_LANG_SPECIFIC (new_type)
493 && DECL_ANTICIPATED (new_type))))
494 new_type = NULL_TREE;
496 if (new_val && !(flags & LOOKUP_HIDDEN))
497 new_val = ovl_skip_hidden (new_val);
499 /* Do we really see a value? */
500 if (new_val)
501 switch (TREE_CODE (new_val))
503 case TEMPLATE_DECL:
504 /* If we expect types or namespaces, and not templates,
505 or this is not a template class. */
506 if ((LOOKUP_QUALIFIERS_ONLY (flags)
507 && !DECL_TYPE_TEMPLATE_P (new_val)))
508 new_val = NULL_TREE;
509 break;
510 case TYPE_DECL:
511 if (LOOKUP_NAMESPACES_ONLY (flags)
512 || (new_type && (flags & LOOKUP_PREFER_TYPES)))
513 new_val = NULL_TREE;
514 break;
515 case NAMESPACE_DECL:
516 if (LOOKUP_TYPES_ONLY (flags))
517 new_val = NULL_TREE;
518 break;
519 default:
520 if (LOOKUP_QUALIFIERS_ONLY (flags))
521 new_val = NULL_TREE;
524 if (!new_val)
526 new_val = new_type;
527 new_type = NULL_TREE;
530 /* Merge into the lookup */
531 if (new_val)
532 add_value (new_val);
533 if (new_type)
534 add_type (new_type);
536 return new_val != NULL_TREE;
539 /* Look in exactly namespace SCOPE. */
541 bool
542 name_lookup::search_namespace_only (tree scope)
544 bool found = false;
546 if (tree *binding = find_namespace_slot (scope, name))
547 found |= process_binding (MAYBE_STAT_DECL (*binding),
548 MAYBE_STAT_TYPE (*binding));
550 return found;
553 /* Conditionally look in namespace SCOPE and inline children. */
555 bool
556 name_lookup::search_namespace (tree scope)
558 if (see_and_mark (scope))
559 /* We've visited this scope before. Return what we found then. */
560 return found_p (scope);
562 /* Look in exactly namespace. */
563 bool found = search_namespace_only (scope);
565 /* Don't look into inline children, if we're looking for an
566 anonymous name -- it must be in the current scope, if anywhere. */
567 if (name)
568 /* Recursively look in its inline children. */
569 if (vec<tree, va_gc> *inlinees = DECL_NAMESPACE_INLINEES (scope))
570 for (unsigned ix = inlinees->length (); ix--;)
571 found |= search_namespace ((*inlinees)[ix]);
573 if (found)
574 mark_found (scope);
576 return found;
579 /* Recursively follow using directives of SCOPE & its inline children.
580 Such following is essentially a flood-fill algorithm. */
582 bool
583 name_lookup::search_usings (tree scope)
585 /* We do not check seen_p here, as that was already set during the
586 namespace_only walk. */
587 if (found_p (scope))
588 return true;
590 bool found = false;
591 if (vec<tree, va_gc> *usings = DECL_NAMESPACE_USING (scope))
592 for (unsigned ix = usings->length (); ix--;)
593 found |= search_qualified ((*usings)[ix], true);
595 /* Look in its inline children. */
596 if (vec<tree, va_gc> *inlinees = DECL_NAMESPACE_INLINEES (scope))
597 for (unsigned ix = inlinees->length (); ix--;)
598 found |= search_usings ((*inlinees)[ix]);
600 if (found)
601 mark_found (scope);
603 return found;
606 /* Qualified namespace lookup in SCOPE.
607 1) Look in SCOPE (+inlines). If found, we're done.
608 2) Otherwise, if USINGS is true,
609 recurse for every using directive of SCOPE (+inlines).
611 Trickiness is (a) loops and (b) multiple paths to same namespace.
612 In both cases we want to not repeat any lookups, and know whether
613 to stop the caller's step #2. Do this via the FOUND_P marker. */
615 bool
616 name_lookup::search_qualified (tree scope, bool usings)
618 bool found = false;
620 if (seen_p (scope))
621 found = found_p (scope);
622 else
624 found = search_namespace (scope);
625 if (!found && usings)
626 found = search_usings (scope);
629 return found;
632 /* Add SCOPE to the unqualified search queue, recursively add its
633 inlines and those via using directives. */
635 name_lookup::using_queue *
636 name_lookup::queue_namespace (using_queue *queue, int depth, tree scope)
638 if (see_and_mark (scope))
639 return queue;
641 /* Record it. */
642 tree common = scope;
643 while (SCOPE_DEPTH (common) > depth)
644 common = CP_DECL_CONTEXT (common);
645 vec_safe_push (queue, using_pair (common, scope));
647 /* Queue its inline children. */
648 if (vec<tree, va_gc> *inlinees = DECL_NAMESPACE_INLINEES (scope))
649 for (unsigned ix = inlinees->length (); ix--;)
650 queue = queue_namespace (queue, depth, (*inlinees)[ix]);
652 /* Queue its using targets. */
653 queue = queue_usings (queue, depth, DECL_NAMESPACE_USING (scope));
655 return queue;
658 /* Add the namespaces in USINGS to the unqualified search queue. */
660 name_lookup::using_queue *
661 name_lookup::do_queue_usings (using_queue *queue, int depth,
662 vec<tree, va_gc> *usings)
664 for (unsigned ix = usings->length (); ix--;)
665 queue = queue_namespace (queue, depth, (*usings)[ix]);
667 return queue;
670 /* Unqualified namespace lookup in SCOPE.
671 1) add scope+inlins to worklist.
672 2) recursively add target of every using directive
673 3) for each worklist item where SCOPE is common ancestor, search it
674 4) if nothing find, scope=parent, goto 1. */
676 bool
677 name_lookup::search_unqualified (tree scope, cp_binding_level *level)
679 /* Make static to avoid continual reallocation. We're not
680 recursive. */
681 static using_queue *queue = NULL;
682 bool found = false;
683 int length = vec_safe_length (queue);
685 /* Queue local using-directives. */
686 for (; level->kind != sk_namespace; level = level->level_chain)
687 queue = queue_usings (queue, SCOPE_DEPTH (scope), level->using_directives);
689 for (; !found; scope = CP_DECL_CONTEXT (scope))
691 gcc_assert (!DECL_NAMESPACE_ALIAS (scope));
692 int depth = SCOPE_DEPTH (scope);
694 /* Queue namespaces reachable from SCOPE. */
695 queue = queue_namespace (queue, depth, scope);
697 /* Search every queued namespace where SCOPE is the common
698 ancestor. Adjust the others. */
699 unsigned ix = length;
702 using_pair &pair = (*queue)[ix];
703 while (pair.first == scope)
705 found |= search_namespace_only (pair.second);
706 pair = queue->pop ();
707 if (ix == queue->length ())
708 goto done;
710 /* The depth is the same as SCOPE, find the parent scope. */
711 if (SCOPE_DEPTH (pair.first) == depth)
712 pair.first = CP_DECL_CONTEXT (pair.first);
713 ix++;
715 while (ix < queue->length ());
716 done:;
717 if (scope == global_namespace)
718 break;
720 /* If looking for hidden names, we only look in the innermost
721 namespace scope. [namespace.memdef]/3 If a friend
722 declaration in a non-local class first declares a class,
723 function, class template or function template the friend is a
724 member of the innermost enclosing namespace. See also
725 [basic.lookup.unqual]/7 */
726 if (flags & LOOKUP_HIDDEN)
727 break;
730 vec_safe_truncate (queue, length);
732 return found;
735 /* FNS is a value binding. If it is a (set of overloaded) functions,
736 add them into the current value. */
738 void
739 name_lookup::add_fns (tree fns)
741 if (!fns)
742 return;
743 else if (TREE_CODE (fns) == OVERLOAD)
745 if (TREE_TYPE (fns) != unknown_type_node)
746 fns = OVL_FUNCTION (fns);
748 else if (!DECL_DECLARES_FUNCTION_P (fns))
749 return;
751 add_overload (fns);
754 /* Add functions of a namespace to the lookup structure. */
756 void
757 name_lookup::adl_namespace_only (tree scope)
759 mark_seen (scope);
761 /* Look down into inline namespaces. */
762 if (vec<tree, va_gc> *inlinees = DECL_NAMESPACE_INLINEES (scope))
763 for (unsigned ix = inlinees->length (); ix--;)
764 adl_namespace_only ((*inlinees)[ix]);
766 if (tree fns = find_namespace_value (scope, name))
767 add_fns (ovl_skip_hidden (fns));
770 /* Find the containing non-inlined namespace, add it and all its
771 inlinees. */
773 void
774 name_lookup::adl_namespace (tree scope)
776 if (seen_p (scope))
777 return;
779 /* Find the containing non-inline namespace. */
780 while (DECL_NAMESPACE_INLINE_P (scope))
781 scope = CP_DECL_CONTEXT (scope);
783 adl_namespace_only (scope);
786 /* Adds the class and its friends to the lookup structure. */
788 void
789 name_lookup::adl_class_only (tree type)
791 /* Backend-built structures, such as __builtin_va_list, aren't
792 affected by all this. */
793 if (!CLASS_TYPE_P (type))
794 return;
796 type = TYPE_MAIN_VARIANT (type);
798 if (see_and_mark (type))
799 return;
801 tree context = decl_namespace_context (type);
802 adl_namespace (context);
804 complete_type (type);
806 /* Add friends. */
807 for (tree list = DECL_FRIENDLIST (TYPE_MAIN_DECL (type)); list;
808 list = TREE_CHAIN (list))
809 if (name == FRIEND_NAME (list))
810 for (tree friends = FRIEND_DECLS (list); friends;
811 friends = TREE_CHAIN (friends))
813 tree fn = TREE_VALUE (friends);
815 /* Only interested in global functions with potentially hidden
816 (i.e. unqualified) declarations. */
817 if (CP_DECL_CONTEXT (fn) != context)
818 continue;
820 /* Only interested in anticipated friends. (Non-anticipated
821 ones will have been inserted during the namespace
822 adl.) */
823 if (!DECL_ANTICIPATED (fn))
824 continue;
826 /* Template specializations are never found by name lookup.
827 (Templates themselves can be found, but not template
828 specializations.) */
829 if (TREE_CODE (fn) == FUNCTION_DECL && DECL_USE_TEMPLATE (fn))
830 continue;
832 add_fns (fn);
836 /* Adds the class and its bases to the lookup structure.
837 Returns true on error. */
839 void
840 name_lookup::adl_bases (tree type)
842 adl_class_only (type);
844 /* Process baseclasses. */
845 if (tree binfo = TYPE_BINFO (type))
847 tree base_binfo;
848 int i;
850 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
851 adl_bases (BINFO_TYPE (base_binfo));
855 /* Adds everything associated with a class argument type to the lookup
856 structure. Returns true on error.
858 If T is a class type (including unions), its associated classes are: the
859 class itself; the class of which it is a member, if any; and its direct
860 and indirect base classes. Its associated namespaces are the namespaces
861 of which its associated classes are members. Furthermore, if T is a
862 class template specialization, its associated namespaces and classes
863 also include: the namespaces and classes associated with the types of
864 the template arguments provided for template type parameters (excluding
865 template template parameters); the namespaces of which any template
866 template arguments are members; and the classes of which any member
867 templates used as template template arguments are members. [ Note:
868 non-type template arguments do not contribute to the set of associated
869 namespaces. --end note] */
871 void
872 name_lookup::adl_class (tree type)
874 /* Backend build structures, such as __builtin_va_list, aren't
875 affected by all this. */
876 if (!CLASS_TYPE_P (type))
877 return;
879 type = TYPE_MAIN_VARIANT (type);
880 /* We don't set found here because we have to have set seen first,
881 which is done in the adl_bases walk. */
882 if (found_p (type))
883 return;
885 adl_bases (type);
886 mark_found (type);
888 if (TYPE_CLASS_SCOPE_P (type))
889 adl_class_only (TYPE_CONTEXT (type));
891 /* Process template arguments. */
892 if (CLASSTYPE_TEMPLATE_INFO (type)
893 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
895 tree list = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
896 for (int i = 0; i < TREE_VEC_LENGTH (list); ++i)
897 adl_template_arg (TREE_VEC_ELT (list, i));
901 void
902 name_lookup::adl_expr (tree expr)
904 if (!expr)
905 return;
907 gcc_assert (!TYPE_P (expr));
909 if (TREE_TYPE (expr) != unknown_type_node)
911 adl_type (TREE_TYPE (expr));
912 return;
915 if (TREE_CODE (expr) == ADDR_EXPR)
916 expr = TREE_OPERAND (expr, 0);
917 if (TREE_CODE (expr) == COMPONENT_REF
918 || TREE_CODE (expr) == OFFSET_REF)
919 expr = TREE_OPERAND (expr, 1);
920 expr = MAYBE_BASELINK_FUNCTIONS (expr);
922 if (OVL_P (expr))
923 for (lkp_iterator iter (expr); iter; ++iter)
924 adl_type (TREE_TYPE (*iter));
925 else if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
927 /* The working paper doesn't currently say how to handle
928 template-id arguments. The sensible thing would seem to be
929 to handle the list of template candidates like a normal
930 overload set, and handle the template arguments like we do
931 for class template specializations. */
933 /* First the templates. */
934 adl_expr (TREE_OPERAND (expr, 0));
936 /* Now the arguments. */
937 if (tree args = TREE_OPERAND (expr, 1))
938 for (int ix = TREE_VEC_LENGTH (args); ix--;)
939 adl_template_arg (TREE_VEC_ELT (args, ix));
943 void
944 name_lookup::adl_type (tree type)
946 if (!type)
947 return;
949 if (TYPE_PTRDATAMEM_P (type))
951 /* Pointer to member: associate class type and value type. */
952 adl_type (TYPE_PTRMEM_CLASS_TYPE (type));
953 adl_type (TYPE_PTRMEM_POINTED_TO_TYPE (type));
954 return;
957 switch (TREE_CODE (type))
959 case RECORD_TYPE:
960 if (TYPE_PTRMEMFUNC_P (type))
962 adl_type (TYPE_PTRMEMFUNC_FN_TYPE (type));
963 return;
965 /* FALLTHRU */
966 case UNION_TYPE:
967 adl_class (type);
968 return;
970 case METHOD_TYPE:
971 /* The basetype is referenced in the first arg type, so just
972 fall through. */
973 case FUNCTION_TYPE:
974 /* Associate the parameter types. */
975 for (tree args = TYPE_ARG_TYPES (type); args; args = TREE_CHAIN (args))
976 adl_type (TREE_VALUE (args));
977 /* FALLTHROUGH */
979 case POINTER_TYPE:
980 case REFERENCE_TYPE:
981 case ARRAY_TYPE:
982 adl_type (TREE_TYPE (type));
983 return;
985 case ENUMERAL_TYPE:
986 if (TYPE_CLASS_SCOPE_P (type))
987 adl_class_only (TYPE_CONTEXT (type));
988 adl_namespace (decl_namespace_context (type));
989 return;
991 case LANG_TYPE:
992 gcc_assert (type == unknown_type_node
993 || type == init_list_type_node);
994 return;
996 case TYPE_PACK_EXPANSION:
997 adl_type (PACK_EXPANSION_PATTERN (type));
998 return;
1000 default:
1001 break;
1005 /* Adds everything associated with a template argument to the lookup
1006 structure. */
1008 void
1009 name_lookup::adl_template_arg (tree arg)
1011 /* [basic.lookup.koenig]
1013 If T is a template-id, its associated namespaces and classes are
1014 ... the namespaces and classes associated with the types of the
1015 template arguments provided for template type parameters
1016 (excluding template template parameters); the namespaces in which
1017 any template template arguments are defined; and the classes in
1018 which any member templates used as template template arguments
1019 are defined. [Note: non-type template arguments do not
1020 contribute to the set of associated namespaces. ] */
1022 /* Consider first template template arguments. */
1023 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
1024 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
1026 else if (TREE_CODE (arg) == TEMPLATE_DECL)
1028 tree ctx = CP_DECL_CONTEXT (arg);
1030 /* It's not a member template. */
1031 if (TREE_CODE (ctx) == NAMESPACE_DECL)
1032 adl_namespace (ctx);
1033 /* Otherwise, it must be member template. */
1034 else
1035 adl_class_only (ctx);
1037 /* It's an argument pack; handle it recursively. */
1038 else if (ARGUMENT_PACK_P (arg))
1040 tree args = ARGUMENT_PACK_ARGS (arg);
1041 int i, len = TREE_VEC_LENGTH (args);
1042 for (i = 0; i < len; ++i)
1043 adl_template_arg (TREE_VEC_ELT (args, i));
1045 /* It's not a template template argument, but it is a type template
1046 argument. */
1047 else if (TYPE_P (arg))
1048 adl_type (arg);
1051 /* Perform ADL lookup. FNS is the existing lookup result and ARGS are
1052 the call arguments. */
1054 tree
1055 name_lookup::search_adl (tree fns, vec<tree, va_gc> *args)
1057 if (fns)
1059 deduping = true;
1060 lookup_mark (fns, true);
1062 value = fns;
1064 unsigned ix;
1065 tree arg;
1067 FOR_EACH_VEC_ELT_REVERSE (*args, ix, arg)
1068 /* OMP reduction operators put an ADL-significant type as the
1069 first arg. */
1070 if (TYPE_P (arg))
1071 adl_type (arg);
1072 else
1073 adl_expr (arg);
1075 fns = value;
1077 return fns;
1080 static bool qualified_namespace_lookup (tree, name_lookup *);
1081 static void consider_binding_level (tree name,
1082 best_match <tree, const char *> &bm,
1083 cp_binding_level *lvl,
1084 bool look_within_fields,
1085 enum lookup_name_fuzzy_kind kind);
1086 static void diagnose_name_conflict (tree, tree);
1088 /* ADL lookup of NAME. FNS is the result of regular lookup, and we
1089 don't add duplicates to it. ARGS is the vector of call
1090 arguments (which will not be empty). */
1092 tree
1093 lookup_arg_dependent (tree name, tree fns, vec<tree, va_gc> *args)
1095 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
1096 name_lookup lookup (name);
1097 fns = lookup.search_adl (fns, args);
1098 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
1099 return fns;
1102 /* FNS is an overload set of conversion functions. Return the
1103 overloads converting to TYPE. */
1105 static tree
1106 extract_conversion_operator (tree fns, tree type)
1108 tree convs = NULL_TREE;
1109 tree tpls = NULL_TREE;
1111 for (ovl_iterator iter (fns); iter; ++iter)
1113 if (same_type_p (DECL_CONV_FN_TYPE (*iter), type))
1114 convs = lookup_add (*iter, convs);
1116 if (TREE_CODE (*iter) == TEMPLATE_DECL)
1117 tpls = lookup_add (*iter, tpls);
1120 if (!convs)
1121 convs = tpls;
1123 return convs;
1126 /* Binary search of (ordered) MEMBER_VEC for NAME. */
1128 static tree
1129 member_vec_binary_search (vec<tree, va_gc> *member_vec, tree name)
1131 for (unsigned lo = 0, hi = member_vec->length (); lo < hi;)
1133 unsigned mid = (lo + hi) / 2;
1134 tree binding = (*member_vec)[mid];
1135 tree binding_name = OVL_NAME (binding);
1137 if (binding_name > name)
1138 hi = mid;
1139 else if (binding_name < name)
1140 lo = mid + 1;
1141 else
1142 return binding;
1145 return NULL_TREE;
1148 /* Linear search of (unordered) MEMBER_VEC for NAME. */
1150 static tree
1151 member_vec_linear_search (vec<tree, va_gc> *member_vec, tree name)
1153 for (int ix = member_vec->length (); ix--;)
1154 if (tree binding = (*member_vec)[ix])
1155 if (OVL_NAME (binding) == name)
1156 return binding;
1158 return NULL_TREE;
1161 /* Linear search of (partially ordered) fields of KLASS for NAME. */
1163 static tree
1164 fields_linear_search (tree klass, tree name, bool want_type)
1166 for (tree fields = TYPE_FIELDS (klass); fields; fields = DECL_CHAIN (fields))
1168 tree decl = fields;
1170 if (TREE_CODE (decl) == FIELD_DECL
1171 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
1173 if (tree temp = search_anon_aggr (TREE_TYPE (decl), name, want_type))
1174 return temp;
1177 if (DECL_NAME (decl) != name)
1178 continue;
1180 if (TREE_CODE (decl) == USING_DECL)
1182 decl = strip_using_decl (decl);
1183 if (is_overloaded_fn (decl))
1184 continue;
1187 if (DECL_DECLARES_FUNCTION_P (decl))
1188 /* Functions are found separately. */
1189 continue;
1191 if (!want_type || DECL_DECLARES_TYPE_P (decl))
1192 return decl;
1195 return NULL_TREE;
1198 /* Look for NAME member inside of anonymous aggregate ANON. Although
1199 such things should only contain FIELD_DECLs, we check that too
1200 late, and would give very confusing errors if we weren't
1201 permissive here. */
1203 tree
1204 search_anon_aggr (tree anon, tree name, bool want_type)
1206 gcc_assert (COMPLETE_TYPE_P (anon));
1207 tree ret = get_class_binding_direct (anon, name, want_type);
1208 return ret;
1211 /* Look for NAME as an immediate member of KLASS (including
1212 anon-members or unscoped enum member). TYPE_OR_FNS is zero for
1213 regular search. >0 to get a type binding (if there is one) and <0
1214 if you want (just) the member function binding.
1216 Use this if you do not want lazy member creation. */
1218 tree
1219 get_class_binding_direct (tree klass, tree name, int type_or_fns)
1221 gcc_checking_assert (RECORD_OR_UNION_TYPE_P (klass));
1223 /* Conversion operators can only be found by the marker conversion
1224 operator name. */
1225 bool conv_op = IDENTIFIER_CONV_OP_P (name);
1226 tree lookup = conv_op ? conv_op_identifier : name;
1227 tree val = NULL_TREE;
1228 vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (klass);
1230 if (COMPLETE_TYPE_P (klass) && member_vec)
1232 val = member_vec_binary_search (member_vec, lookup);
1233 if (!val)
1235 else if (type_or_fns > 0)
1237 if (STAT_HACK_P (val))
1238 val = STAT_TYPE (val);
1239 else if (!DECL_DECLARES_TYPE_P (val))
1240 val = NULL_TREE;
1242 else if (STAT_HACK_P (val))
1243 val = STAT_DECL (val);
1245 if (val && TREE_CODE (val) == OVERLOAD
1246 && TREE_CODE (OVL_FUNCTION (val)) == USING_DECL)
1248 /* An overload with a dependent USING_DECL. Does the caller
1249 want the USING_DECL or the functions? */
1250 if (type_or_fns < 0)
1251 val = OVL_CHAIN (val);
1252 else
1253 val = OVL_FUNCTION (val);
1256 else
1258 if (member_vec && type_or_fns <= 0)
1259 val = member_vec_linear_search (member_vec, lookup);
1261 if (type_or_fns < 0)
1262 /* Don't bother looking for field. We don't want it. */;
1263 else if (!val || (TREE_CODE (val) == OVERLOAD && OVL_USING_P (val)))
1264 /* Dependent using declarations are a 'field', make sure we
1265 return that even if we saw an overload already. */
1266 if (tree field_val = fields_linear_search (klass, lookup,
1267 type_or_fns > 0))
1268 if (!val || TREE_CODE (field_val) == USING_DECL)
1269 val = field_val;
1272 /* Extract the conversion operators asked for, unless the general
1273 conversion operator was requested. */
1274 if (val && conv_op)
1276 gcc_checking_assert (OVL_FUNCTION (val) == conv_op_marker);
1277 val = OVL_CHAIN (val);
1278 if (tree type = TREE_TYPE (name))
1279 val = extract_conversion_operator (val, type);
1282 return val;
1285 /* Look for NAME's binding in exactly KLASS. See
1286 get_class_binding_direct for argument description. Does lazy
1287 special function creation as necessary. */
1289 tree
1290 get_class_binding (tree klass, tree name, int type_or_fns)
1292 klass = complete_type (klass);
1294 if (COMPLETE_TYPE_P (klass))
1296 /* Lazily declare functions, if we're going to search these. */
1297 if (IDENTIFIER_CTOR_P (name))
1299 if (CLASSTYPE_LAZY_DEFAULT_CTOR (klass))
1300 lazily_declare_fn (sfk_constructor, klass);
1301 if (CLASSTYPE_LAZY_COPY_CTOR (klass))
1302 lazily_declare_fn (sfk_copy_constructor, klass);
1303 if (CLASSTYPE_LAZY_MOVE_CTOR (klass))
1304 lazily_declare_fn (sfk_move_constructor, klass);
1306 else if (IDENTIFIER_DTOR_P (name))
1308 if (CLASSTYPE_LAZY_DESTRUCTOR (klass))
1309 lazily_declare_fn (sfk_destructor, klass);
1311 else if (name == assign_op_identifier)
1313 if (CLASSTYPE_LAZY_COPY_ASSIGN (klass))
1314 lazily_declare_fn (sfk_copy_assignment, klass);
1315 if (CLASSTYPE_LAZY_MOVE_ASSIGN (klass))
1316 lazily_declare_fn (sfk_move_assignment, klass);
1320 return get_class_binding_direct (klass, name, type_or_fns);
1323 /* Find the slot containing overloads called 'NAME'. If there is no
1324 such slot and the class is complete, create an empty one, at the
1325 correct point in the sorted member vector. Otherwise return NULL.
1326 Deals with conv_op marker handling. */
1328 tree *
1329 find_member_slot (tree klass, tree name)
1331 bool complete_p = COMPLETE_TYPE_P (klass);
1333 vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (klass);
1334 if (!member_vec)
1336 vec_alloc (member_vec, 8);
1337 CLASSTYPE_MEMBER_VEC (klass) = member_vec;
1338 if (complete_p)
1340 /* If the class is complete but had no member_vec, we need
1341 to add the TYPE_FIELDS into it. We're also most likely
1342 to be adding ctors & dtors, so ask for 6 spare slots (the
1343 abstract cdtors and their clones). */
1344 set_class_bindings (klass, 6);
1345 member_vec = CLASSTYPE_MEMBER_VEC (klass);
1349 if (IDENTIFIER_CONV_OP_P (name))
1350 name = conv_op_identifier;
1352 unsigned ix, length = member_vec->length ();
1353 for (ix = 0; ix < length; ix++)
1355 tree *slot = &(*member_vec)[ix];
1356 tree fn_name = OVL_NAME (*slot);
1358 if (fn_name == name)
1360 /* If we found an existing slot, it must be a function set.
1361 Even with insertion after completion, because those only
1362 happen with artificial fns that have unspellable names.
1363 This means we do not have to deal with the stat hack
1364 either. */
1365 gcc_checking_assert (OVL_P (*slot));
1366 if (name == conv_op_identifier)
1368 gcc_checking_assert (OVL_FUNCTION (*slot) == conv_op_marker);
1369 /* Skip the conv-op marker. */
1370 slot = &OVL_CHAIN (*slot);
1372 return slot;
1375 if (complete_p && fn_name > name)
1376 break;
1379 /* No slot found, add one if the class is complete. */
1380 if (complete_p)
1382 /* Do exact allocation, as we don't expect to add many. */
1383 gcc_assert (name != conv_op_identifier);
1384 vec_safe_reserve_exact (member_vec, 1);
1385 CLASSTYPE_MEMBER_VEC (klass) = member_vec;
1386 member_vec->quick_insert (ix, NULL_TREE);
1387 return &(*member_vec)[ix];
1390 return NULL;
1393 /* KLASS is an incomplete class to which we're adding a method NAME.
1394 Add a slot and deal with conv_op marker handling. */
1396 tree *
1397 add_member_slot (tree klass, tree name)
1399 gcc_assert (!COMPLETE_TYPE_P (klass));
1401 vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (klass);
1402 vec_safe_push (member_vec, NULL_TREE);
1403 CLASSTYPE_MEMBER_VEC (klass) = member_vec;
1405 tree *slot = &member_vec->last ();
1406 if (IDENTIFIER_CONV_OP_P (name))
1408 /* Install the marker prefix. */
1409 *slot = ovl_make (conv_op_marker, NULL_TREE);
1410 slot = &OVL_CHAIN (*slot);
1413 return slot;
1416 /* Comparison function to compare two MEMBER_VEC entries by name.
1417 Because we can have duplicates during insertion of TYPE_FIELDS, we
1418 do extra checking so deduping doesn't have to deal with so many
1419 cases. */
1421 static int
1422 member_name_cmp (const void *a_p, const void *b_p)
1424 tree a = *(const tree *)a_p;
1425 tree b = *(const tree *)b_p;
1426 tree name_a = DECL_NAME (TREE_CODE (a) == OVERLOAD ? OVL_FUNCTION (a) : a);
1427 tree name_b = DECL_NAME (TREE_CODE (b) == OVERLOAD ? OVL_FUNCTION (b) : b);
1429 gcc_checking_assert (name_a && name_b);
1430 if (name_a != name_b)
1431 return name_a < name_b ? -1 : +1;
1433 if (name_a == conv_op_identifier)
1435 /* Strip the conv-op markers. */
1436 gcc_checking_assert (OVL_FUNCTION (a) == conv_op_marker
1437 && OVL_FUNCTION (b) == conv_op_marker);
1438 a = OVL_CHAIN (a);
1439 b = OVL_CHAIN (b);
1442 if (TREE_CODE (a) == OVERLOAD)
1443 a = OVL_FUNCTION (a);
1444 if (TREE_CODE (b) == OVERLOAD)
1445 b = OVL_FUNCTION (b);
1447 /* We're in STAT_HACK or USING_DECL territory (or possibly error-land). */
1448 if (TREE_CODE (a) != TREE_CODE (b))
1450 /* If one of them is a TYPE_DECL, it loses. */
1451 if (TREE_CODE (a) == TYPE_DECL)
1452 return +1;
1453 else if (TREE_CODE (b) == TYPE_DECL)
1454 return -1;
1456 /* If one of them is a USING_DECL, it loses. */
1457 if (TREE_CODE (a) == USING_DECL)
1458 return +1;
1459 else if (TREE_CODE (b) == USING_DECL)
1460 return -1;
1462 /* There are no other cases with different kinds of decls, as
1463 duplicate detection should have kicked in earlier. However,
1464 some erroneous cases get though. */
1465 gcc_assert (errorcount);
1468 /* Using source location would be the best thing here, but we can
1469 get identically-located decls in the following circumstances:
1471 1) duplicate artificial type-decls for the same type.
1473 2) pack expansions of using-decls.
1475 We should not be doing #1, but in either case it doesn't matter
1476 how we order these. Use UID as a proxy for source ordering, so
1477 that identically-located decls still have a well-defined stable
1478 ordering. */
1479 if (DECL_UID (a) != DECL_UID (b))
1480 return DECL_UID (a) < DECL_UID (b) ? -1 : +1;
1481 gcc_assert (a == b);
1482 return 0;
1485 static struct {
1486 gt_pointer_operator new_value;
1487 void *cookie;
1488 } resort_data;
1490 /* This routine compares two fields like member_name_cmp but using the
1491 pointer operator in resort_field_decl_data. We don't have to deal
1492 with duplicates here. */
1494 static int
1495 resort_member_name_cmp (const void *a_p, const void *b_p)
1497 tree a = *(const tree *)a_p;
1498 tree b = *(const tree *)b_p;
1499 tree name_a = OVL_NAME (a);
1500 tree name_b = OVL_NAME (b);
1502 resort_data.new_value (&name_a, resort_data.cookie);
1503 resort_data.new_value (&name_b, resort_data.cookie);
1505 gcc_checking_assert (name_a != name_b);
1507 return name_a < name_b ? -1 : +1;
1510 /* Resort CLASSTYPE_MEMBER_VEC because pointers have been reordered. */
1512 void
1513 resort_type_member_vec (void *obj, void */*orig_obj*/,
1514 gt_pointer_operator new_value, void* cookie)
1516 if (vec<tree, va_gc> *member_vec = (vec<tree, va_gc> *) obj)
1518 resort_data.new_value = new_value;
1519 resort_data.cookie = cookie;
1520 member_vec->qsort (resort_member_name_cmp);
1524 /* Recursively count the number of fields in KLASS, including anonymous
1525 union members. */
1527 static unsigned
1528 count_class_fields (tree klass)
1530 unsigned n_fields = 0;
1532 for (tree fields = TYPE_FIELDS (klass); fields; fields = DECL_CHAIN (fields))
1533 if (DECL_DECLARES_FUNCTION_P (fields))
1534 /* Functions are dealt with separately. */;
1535 else if (TREE_CODE (fields) == FIELD_DECL
1536 && ANON_AGGR_TYPE_P (TREE_TYPE (fields)))
1537 n_fields += count_class_fields (TREE_TYPE (fields));
1538 else if (DECL_NAME (fields))
1539 n_fields += 1;
1541 return n_fields;
1544 /* Append all the nonfunction members fields of KLASS to MEMBER_VEC.
1545 Recurse for anonymous members. MEMBER_VEC must have space. */
1547 static void
1548 member_vec_append_class_fields (vec<tree, va_gc> *member_vec, tree klass)
1550 for (tree fields = TYPE_FIELDS (klass); fields; fields = DECL_CHAIN (fields))
1551 if (DECL_DECLARES_FUNCTION_P (fields))
1552 /* Functions are handled separately. */;
1553 else if (TREE_CODE (fields) == FIELD_DECL
1554 && ANON_AGGR_TYPE_P (TREE_TYPE (fields)))
1555 member_vec_append_class_fields (member_vec, TREE_TYPE (fields));
1556 else if (DECL_NAME (fields))
1558 tree field = fields;
1559 /* Mark a conv-op USING_DECL with the conv-op-marker. */
1560 if (TREE_CODE (field) == USING_DECL
1561 && IDENTIFIER_CONV_OP_P (DECL_NAME (field)))
1562 field = ovl_make (conv_op_marker, field);
1563 member_vec->quick_push (field);
1567 /* Append all of the enum values of ENUMTYPE to MEMBER_VEC.
1568 MEMBER_VEC must have space. */
1570 static void
1571 member_vec_append_enum_values (vec<tree, va_gc> *member_vec, tree enumtype)
1573 for (tree values = TYPE_VALUES (enumtype);
1574 values; values = TREE_CHAIN (values))
1575 member_vec->quick_push (TREE_VALUE (values));
1578 /* MEMBER_VEC has just had new DECLs added to it, but is sorted.
1579 DeDup adjacent DECLS of the same name. We already dealt with
1580 conflict resolution when adding the fields or methods themselves.
1581 There are three cases (which could all be combined):
1582 1) a TYPE_DECL and non TYPE_DECL. Deploy STAT_HACK as appropriate.
1583 2) a USING_DECL and an overload. If the USING_DECL is dependent,
1584 it wins. Otherwise the OVERLOAD does.
1585 3) two USING_DECLS. ...
1587 member_name_cmp will have ordered duplicates as
1588 <fns><using><type> */
1590 static void
1591 member_vec_dedup (vec<tree, va_gc> *member_vec)
1593 unsigned len = member_vec->length ();
1594 unsigned store = 0;
1596 if (!len)
1597 return;
1599 tree name = OVL_NAME ((*member_vec)[0]);
1600 for (unsigned jx, ix = 0; ix < len; ix = jx)
1602 tree current = NULL_TREE;
1603 tree to_type = NULL_TREE;
1604 tree to_using = NULL_TREE;
1605 tree marker = NULL_TREE;
1607 for (jx = ix; jx < len; jx++)
1609 tree next = (*member_vec)[jx];
1610 if (jx != ix)
1612 tree next_name = OVL_NAME (next);
1613 if (next_name != name)
1615 name = next_name;
1616 break;
1620 if (IDENTIFIER_CONV_OP_P (name))
1622 marker = next;
1623 next = OVL_CHAIN (next);
1626 if (TREE_CODE (next) == USING_DECL)
1628 if (IDENTIFIER_CTOR_P (name))
1629 /* Dependent inherited ctor. */
1630 continue;
1632 next = strip_using_decl (next);
1633 if (TREE_CODE (next) == USING_DECL)
1635 to_using = next;
1636 continue;
1639 if (is_overloaded_fn (next))
1640 continue;
1643 if (DECL_DECLARES_TYPE_P (next))
1645 to_type = next;
1646 continue;
1649 if (!current)
1650 current = next;
1653 if (to_using)
1655 if (!current)
1656 current = to_using;
1657 else
1658 current = ovl_make (to_using, current);
1661 if (to_type)
1663 if (!current)
1664 current = to_type;
1665 else
1666 current = stat_hack (current, to_type);
1669 if (current)
1671 if (marker)
1673 OVL_CHAIN (marker) = current;
1674 current = marker;
1676 (*member_vec)[store++] = current;
1680 while (store++ < len)
1681 member_vec->pop ();
1684 /* Add the non-function members to CLASSTYPE_MEMBER_VEC. If there is
1685 no existing MEMBER_VEC and fewer than 8 fields, do nothing. We
1686 know there must be at least 1 field -- the self-reference
1687 TYPE_DECL, except for anon aggregates, which will have at least
1688 one field. */
1690 void
1691 set_class_bindings (tree klass, unsigned extra)
1693 unsigned n_fields = count_class_fields (klass);
1694 vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (klass);
1696 if (member_vec || n_fields >= 8)
1698 /* Append the new fields. */
1699 vec_safe_reserve_exact (member_vec, extra + n_fields);
1700 member_vec_append_class_fields (member_vec, klass);
1703 if (member_vec)
1705 CLASSTYPE_MEMBER_VEC (klass) = member_vec;
1706 member_vec->qsort (member_name_cmp);
1707 member_vec_dedup (member_vec);
1711 /* Insert lately defined enum ENUMTYPE into KLASS for the sorted case. */
1713 void
1714 insert_late_enum_def_bindings (tree klass, tree enumtype)
1716 int n_fields;
1717 vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (klass);
1719 /* The enum bindings will already be on the TYPE_FIELDS, so don't
1720 count them twice. */
1721 if (!member_vec)
1722 n_fields = count_class_fields (klass);
1723 else
1724 n_fields = list_length (TYPE_VALUES (enumtype));
1726 if (member_vec || n_fields >= 8)
1728 vec_safe_reserve_exact (member_vec, n_fields);
1729 if (CLASSTYPE_MEMBER_VEC (klass))
1730 member_vec_append_enum_values (member_vec, enumtype);
1731 else
1732 member_vec_append_class_fields (member_vec, klass);
1733 CLASSTYPE_MEMBER_VEC (klass) = member_vec;
1734 member_vec->qsort (member_name_cmp);
1735 member_vec_dedup (member_vec);
1739 /* Compute the chain index of a binding_entry given the HASH value of its
1740 name and the total COUNT of chains. COUNT is assumed to be a power
1741 of 2. */
1743 #define ENTRY_INDEX(HASH, COUNT) (((HASH) >> 3) & ((COUNT) - 1))
1745 /* A free list of "binding_entry"s awaiting for re-use. */
1747 static GTY((deletable)) binding_entry free_binding_entry = NULL;
1749 /* The binding oracle; see cp-tree.h. */
1751 cp_binding_oracle_function *cp_binding_oracle;
1753 /* If we have a binding oracle, ask it for all namespace-scoped
1754 definitions of NAME. */
1756 static inline void
1757 query_oracle (tree name)
1759 if (!cp_binding_oracle)
1760 return;
1762 /* LOOKED_UP holds the set of identifiers that we have already
1763 looked up with the oracle. */
1764 static hash_set<tree> looked_up;
1765 if (looked_up.add (name))
1766 return;
1768 cp_binding_oracle (CP_ORACLE_IDENTIFIER, name);
1771 /* Create a binding_entry object for (NAME, TYPE). */
1773 static inline binding_entry
1774 binding_entry_make (tree name, tree type)
1776 binding_entry entry;
1778 if (free_binding_entry)
1780 entry = free_binding_entry;
1781 free_binding_entry = entry->chain;
1783 else
1784 entry = ggc_alloc<binding_entry_s> ();
1786 entry->name = name;
1787 entry->type = type;
1788 entry->chain = NULL;
1790 return entry;
1793 /* Put ENTRY back on the free list. */
1794 #if 0
1795 static inline void
1796 binding_entry_free (binding_entry entry)
1798 entry->name = NULL;
1799 entry->type = NULL;
1800 entry->chain = free_binding_entry;
1801 free_binding_entry = entry;
1803 #endif
1805 /* The datatype used to implement the mapping from names to types at
1806 a given scope. */
1807 struct GTY(()) binding_table_s {
1808 /* Array of chains of "binding_entry"s */
1809 binding_entry * GTY((length ("%h.chain_count"))) chain;
1811 /* The number of chains in this table. This is the length of the
1812 member "chain" considered as an array. */
1813 size_t chain_count;
1815 /* Number of "binding_entry"s in this table. */
1816 size_t entry_count;
1819 /* Construct TABLE with an initial CHAIN_COUNT. */
1821 static inline void
1822 binding_table_construct (binding_table table, size_t chain_count)
1824 table->chain_count = chain_count;
1825 table->entry_count = 0;
1826 table->chain = ggc_cleared_vec_alloc<binding_entry> (table->chain_count);
1829 /* Make TABLE's entries ready for reuse. */
1830 #if 0
1831 static void
1832 binding_table_free (binding_table table)
1834 size_t i;
1835 size_t count;
1837 if (table == NULL)
1838 return;
1840 for (i = 0, count = table->chain_count; i < count; ++i)
1842 binding_entry temp = table->chain[i];
1843 while (temp != NULL)
1845 binding_entry entry = temp;
1846 temp = entry->chain;
1847 binding_entry_free (entry);
1849 table->chain[i] = NULL;
1851 table->entry_count = 0;
1853 #endif
1855 /* Allocate a table with CHAIN_COUNT, assumed to be a power of two. */
1857 static inline binding_table
1858 binding_table_new (size_t chain_count)
1860 binding_table table = ggc_alloc<binding_table_s> ();
1861 table->chain = NULL;
1862 binding_table_construct (table, chain_count);
1863 return table;
1866 /* Expand TABLE to twice its current chain_count. */
1868 static void
1869 binding_table_expand (binding_table table)
1871 const size_t old_chain_count = table->chain_count;
1872 const size_t old_entry_count = table->entry_count;
1873 const size_t new_chain_count = 2 * old_chain_count;
1874 binding_entry *old_chains = table->chain;
1875 size_t i;
1877 binding_table_construct (table, new_chain_count);
1878 for (i = 0; i < old_chain_count; ++i)
1880 binding_entry entry = old_chains[i];
1881 for (; entry != NULL; entry = old_chains[i])
1883 const unsigned int hash = IDENTIFIER_HASH_VALUE (entry->name);
1884 const size_t j = ENTRY_INDEX (hash, new_chain_count);
1886 old_chains[i] = entry->chain;
1887 entry->chain = table->chain[j];
1888 table->chain[j] = entry;
1891 table->entry_count = old_entry_count;
1894 /* Insert a binding for NAME to TYPE into TABLE. */
1896 static void
1897 binding_table_insert (binding_table table, tree name, tree type)
1899 const unsigned int hash = IDENTIFIER_HASH_VALUE (name);
1900 const size_t i = ENTRY_INDEX (hash, table->chain_count);
1901 binding_entry entry = binding_entry_make (name, type);
1903 entry->chain = table->chain[i];
1904 table->chain[i] = entry;
1905 ++table->entry_count;
1907 if (3 * table->chain_count < 5 * table->entry_count)
1908 binding_table_expand (table);
1911 /* Return the binding_entry, if any, that maps NAME. */
1913 binding_entry
1914 binding_table_find (binding_table table, tree name)
1916 const unsigned int hash = IDENTIFIER_HASH_VALUE (name);
1917 binding_entry entry = table->chain[ENTRY_INDEX (hash, table->chain_count)];
1919 while (entry != NULL && entry->name != name)
1920 entry = entry->chain;
1922 return entry;
1925 /* Apply PROC -- with DATA -- to all entries in TABLE. */
1927 void
1928 binding_table_foreach (binding_table table, bt_foreach_proc proc, void *data)
1930 size_t chain_count;
1931 size_t i;
1933 if (!table)
1934 return;
1936 chain_count = table->chain_count;
1937 for (i = 0; i < chain_count; ++i)
1939 binding_entry entry = table->chain[i];
1940 for (; entry != NULL; entry = entry->chain)
1941 proc (entry, data);
1945 #ifndef ENABLE_SCOPE_CHECKING
1946 # define ENABLE_SCOPE_CHECKING 0
1947 #else
1948 # define ENABLE_SCOPE_CHECKING 1
1949 #endif
1951 /* A free list of "cxx_binding"s, connected by their PREVIOUS. */
1953 static GTY((deletable)) cxx_binding *free_bindings;
1955 /* Initialize VALUE and TYPE field for BINDING, and set the PREVIOUS
1956 field to NULL. */
1958 static inline void
1959 cxx_binding_init (cxx_binding *binding, tree value, tree type)
1961 binding->value = value;
1962 binding->type = type;
1963 binding->previous = NULL;
1966 /* (GC)-allocate a binding object with VALUE and TYPE member initialized. */
1968 static cxx_binding *
1969 cxx_binding_make (tree value, tree type)
1971 cxx_binding *binding;
1972 if (free_bindings)
1974 binding = free_bindings;
1975 free_bindings = binding->previous;
1977 else
1978 binding = ggc_alloc<cxx_binding> ();
1980 cxx_binding_init (binding, value, type);
1982 return binding;
1985 /* Put BINDING back on the free list. */
1987 static inline void
1988 cxx_binding_free (cxx_binding *binding)
1990 binding->scope = NULL;
1991 binding->previous = free_bindings;
1992 free_bindings = binding;
1995 /* Create a new binding for NAME (with the indicated VALUE and TYPE
1996 bindings) in the class scope indicated by SCOPE. */
1998 static cxx_binding *
1999 new_class_binding (tree name, tree value, tree type, cp_binding_level *scope)
2001 cp_class_binding cb = {cxx_binding_make (value, type), name};
2002 cxx_binding *binding = cb.base;
2003 vec_safe_push (scope->class_shadowed, cb);
2004 binding->scope = scope;
2005 return binding;
2008 /* Make DECL the innermost binding for ID. The LEVEL is the binding
2009 level at which this declaration is being bound. */
2011 void
2012 push_binding (tree id, tree decl, cp_binding_level* level)
2014 cxx_binding *binding;
2016 if (level != class_binding_level)
2018 binding = cxx_binding_make (decl, NULL_TREE);
2019 binding->scope = level;
2021 else
2022 binding = new_class_binding (id, decl, /*type=*/NULL_TREE, level);
2024 /* Now, fill in the binding information. */
2025 binding->previous = IDENTIFIER_BINDING (id);
2026 INHERITED_VALUE_BINDING_P (binding) = 0;
2027 LOCAL_BINDING_P (binding) = (level != class_binding_level);
2029 /* And put it on the front of the list of bindings for ID. */
2030 IDENTIFIER_BINDING (id) = binding;
2033 /* Remove the binding for DECL which should be the innermost binding
2034 for ID. */
2036 void
2037 pop_local_binding (tree id, tree decl)
2039 cxx_binding *binding;
2041 if (id == NULL_TREE)
2042 /* It's easiest to write the loops that call this function without
2043 checking whether or not the entities involved have names. We
2044 get here for such an entity. */
2045 return;
2047 /* Get the innermost binding for ID. */
2048 binding = IDENTIFIER_BINDING (id);
2050 /* The name should be bound. */
2051 gcc_assert (binding != NULL);
2053 /* The DECL will be either the ordinary binding or the type
2054 binding for this identifier. Remove that binding. */
2055 if (binding->value == decl)
2056 binding->value = NULL_TREE;
2057 else
2059 gcc_assert (binding->type == decl);
2060 binding->type = NULL_TREE;
2063 if (!binding->value && !binding->type)
2065 /* We're completely done with the innermost binding for this
2066 identifier. Unhook it from the list of bindings. */
2067 IDENTIFIER_BINDING (id) = binding->previous;
2069 /* Add it to the free list. */
2070 cxx_binding_free (binding);
2074 /* Remove the bindings for the decls of the current level and leave
2075 the current scope. */
2077 void
2078 pop_bindings_and_leave_scope (void)
2080 for (tree t = get_local_decls (); t; t = DECL_CHAIN (t))
2082 tree decl = TREE_CODE (t) == TREE_LIST ? TREE_VALUE (t) : t;
2083 tree name = OVL_NAME (decl);
2085 pop_local_binding (name, decl);
2088 leave_scope ();
2091 /* Strip non dependent using declarations. If DECL is dependent,
2092 surreptitiously create a typename_type and return it. */
2094 tree
2095 strip_using_decl (tree decl)
2097 if (decl == NULL_TREE)
2098 return NULL_TREE;
2100 while (TREE_CODE (decl) == USING_DECL && !DECL_DEPENDENT_P (decl))
2101 decl = USING_DECL_DECLS (decl);
2103 if (TREE_CODE (decl) == USING_DECL && DECL_DEPENDENT_P (decl)
2104 && USING_DECL_TYPENAME_P (decl))
2106 /* We have found a type introduced by a using
2107 declaration at class scope that refers to a dependent
2108 type.
2110 using typename :: [opt] nested-name-specifier unqualified-id ;
2112 decl = make_typename_type (TREE_TYPE (decl),
2113 DECL_NAME (decl),
2114 typename_type, tf_error);
2115 if (decl != error_mark_node)
2116 decl = TYPE_NAME (decl);
2119 return decl;
2122 /* Return true if OVL is an overload for an anticipated builtin. */
2124 static bool
2125 anticipated_builtin_p (tree ovl)
2127 if (TREE_CODE (ovl) != OVERLOAD)
2128 return false;
2130 if (!OVL_HIDDEN_P (ovl))
2131 return false;
2133 tree fn = OVL_FUNCTION (ovl);
2134 gcc_checking_assert (DECL_ANTICIPATED (fn));
2136 if (DECL_HIDDEN_FRIEND_P (fn))
2137 return false;
2139 return true;
2142 /* BINDING records an existing declaration for a name in the current scope.
2143 But, DECL is another declaration for that same identifier in the
2144 same scope. This is the `struct stat' hack whereby a non-typedef
2145 class name or enum-name can be bound at the same level as some other
2146 kind of entity.
2147 3.3.7/1
2149 A class name (9.1) or enumeration name (7.2) can be hidden by the
2150 name of an object, function, or enumerator declared in the same scope.
2151 If a class or enumeration name and an object, function, or enumerator
2152 are declared in the same scope (in any order) with the same name, the
2153 class or enumeration name is hidden wherever the object, function, or
2154 enumerator name is visible.
2156 It's the responsibility of the caller to check that
2157 inserting this name is valid here. Returns nonzero if the new binding
2158 was successful. */
2160 static bool
2161 supplement_binding_1 (cxx_binding *binding, tree decl)
2163 tree bval = binding->value;
2164 bool ok = true;
2165 tree target_bval = strip_using_decl (bval);
2166 tree target_decl = strip_using_decl (decl);
2168 if (TREE_CODE (target_decl) == TYPE_DECL && DECL_ARTIFICIAL (target_decl)
2169 && target_decl != target_bval
2170 && (TREE_CODE (target_bval) != TYPE_DECL
2171 /* We allow pushing an enum multiple times in a class
2172 template in order to handle late matching of underlying
2173 type on an opaque-enum-declaration followed by an
2174 enum-specifier. */
2175 || (processing_template_decl
2176 && TREE_CODE (TREE_TYPE (target_decl)) == ENUMERAL_TYPE
2177 && TREE_CODE (TREE_TYPE (target_bval)) == ENUMERAL_TYPE
2178 && (dependent_type_p (ENUM_UNDERLYING_TYPE
2179 (TREE_TYPE (target_decl)))
2180 || dependent_type_p (ENUM_UNDERLYING_TYPE
2181 (TREE_TYPE (target_bval)))))))
2182 /* The new name is the type name. */
2183 binding->type = decl;
2184 else if (/* TARGET_BVAL is null when push_class_level_binding moves
2185 an inherited type-binding out of the way to make room
2186 for a new value binding. */
2187 !target_bval
2188 /* TARGET_BVAL is error_mark_node when TARGET_DECL's name
2189 has been used in a non-class scope prior declaration.
2190 In that case, we should have already issued a
2191 diagnostic; for graceful error recovery purpose, pretend
2192 this was the intended declaration for that name. */
2193 || target_bval == error_mark_node
2194 /* If TARGET_BVAL is anticipated but has not yet been
2195 declared, pretend it is not there at all. */
2196 || anticipated_builtin_p (target_bval))
2197 binding->value = decl;
2198 else if (TREE_CODE (target_bval) == TYPE_DECL
2199 && DECL_ARTIFICIAL (target_bval)
2200 && target_decl != target_bval
2201 && (TREE_CODE (target_decl) != TYPE_DECL
2202 || same_type_p (TREE_TYPE (target_decl),
2203 TREE_TYPE (target_bval))))
2205 /* The old binding was a type name. It was placed in
2206 VALUE field because it was thought, at the point it was
2207 declared, to be the only entity with such a name. Move the
2208 type name into the type slot; it is now hidden by the new
2209 binding. */
2210 binding->type = bval;
2211 binding->value = decl;
2212 binding->value_is_inherited = false;
2214 else if (TREE_CODE (target_bval) == TYPE_DECL
2215 && TREE_CODE (target_decl) == TYPE_DECL
2216 && DECL_NAME (target_decl) == DECL_NAME (target_bval)
2217 && binding->scope->kind != sk_class
2218 && (same_type_p (TREE_TYPE (target_decl), TREE_TYPE (target_bval))
2219 /* If either type involves template parameters, we must
2220 wait until instantiation. */
2221 || uses_template_parms (TREE_TYPE (target_decl))
2222 || uses_template_parms (TREE_TYPE (target_bval))))
2223 /* We have two typedef-names, both naming the same type to have
2224 the same name. In general, this is OK because of:
2226 [dcl.typedef]
2228 In a given scope, a typedef specifier can be used to redefine
2229 the name of any type declared in that scope to refer to the
2230 type to which it already refers.
2232 However, in class scopes, this rule does not apply due to the
2233 stricter language in [class.mem] prohibiting redeclarations of
2234 members. */
2235 ok = false;
2236 /* There can be two block-scope declarations of the same variable,
2237 so long as they are `extern' declarations. However, there cannot
2238 be two declarations of the same static data member:
2240 [class.mem]
2242 A member shall not be declared twice in the
2243 member-specification. */
2244 else if (VAR_P (target_decl)
2245 && VAR_P (target_bval)
2246 && DECL_EXTERNAL (target_decl) && DECL_EXTERNAL (target_bval)
2247 && !DECL_CLASS_SCOPE_P (target_decl))
2249 duplicate_decls (decl, binding->value, /*newdecl_is_friend=*/false);
2250 ok = false;
2252 else if (TREE_CODE (decl) == NAMESPACE_DECL
2253 && TREE_CODE (bval) == NAMESPACE_DECL
2254 && DECL_NAMESPACE_ALIAS (decl)
2255 && DECL_NAMESPACE_ALIAS (bval)
2256 && ORIGINAL_NAMESPACE (bval) == ORIGINAL_NAMESPACE (decl))
2257 /* [namespace.alias]
2259 In a declarative region, a namespace-alias-definition can be
2260 used to redefine a namespace-alias declared in that declarative
2261 region to refer only to the namespace to which it already
2262 refers. */
2263 ok = false;
2264 else
2266 if (!error_operand_p (bval))
2267 diagnose_name_conflict (decl, bval);
2268 ok = false;
2271 return ok;
2274 /* Diagnose a name conflict between DECL and BVAL. */
2276 static void
2277 diagnose_name_conflict (tree decl, tree bval)
2279 if (TREE_CODE (decl) == TREE_CODE (bval)
2280 && TREE_CODE (decl) != NAMESPACE_DECL
2281 && !DECL_DECLARES_FUNCTION_P (decl)
2282 && (TREE_CODE (decl) != TYPE_DECL
2283 || DECL_ARTIFICIAL (decl) == DECL_ARTIFICIAL (bval))
2284 && CP_DECL_CONTEXT (decl) == CP_DECL_CONTEXT (bval))
2285 error ("redeclaration of %q#D", decl);
2286 else
2287 error ("%q#D conflicts with a previous declaration", decl);
2289 inform (location_of (bval), "previous declaration %q#D", bval);
2292 /* Wrapper for supplement_binding_1. */
2294 static bool
2295 supplement_binding (cxx_binding *binding, tree decl)
2297 bool ret;
2298 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
2299 ret = supplement_binding_1 (binding, decl);
2300 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
2301 return ret;
2304 /* Replace BINDING's current value on its scope's name list with
2305 NEWVAL. */
2307 static void
2308 update_local_overload (cxx_binding *binding, tree newval)
2310 tree *d;
2312 for (d = &binding->scope->names; ; d = &TREE_CHAIN (*d))
2313 if (*d == binding->value)
2315 /* Stitch new list node in. */
2316 *d = tree_cons (NULL_TREE, NULL_TREE, TREE_CHAIN (*d));
2317 break;
2319 else if (TREE_CODE (*d) == TREE_LIST && TREE_VALUE (*d) == binding->value)
2320 break;
2322 TREE_VALUE (*d) = newval;
2325 /* Compares the parameter-type-lists of ONE and TWO and
2326 returns false if they are different. If the DECLs are template
2327 functions, the return types and the template parameter lists are
2328 compared too (DR 565). */
2330 static bool
2331 matching_fn_p (tree one, tree two)
2333 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (one)),
2334 TYPE_ARG_TYPES (TREE_TYPE (two))))
2335 return false;
2337 if (TREE_CODE (one) == TEMPLATE_DECL
2338 && TREE_CODE (two) == TEMPLATE_DECL)
2340 /* Compare template parms. */
2341 if (!comp_template_parms (DECL_TEMPLATE_PARMS (one),
2342 DECL_TEMPLATE_PARMS (two)))
2343 return false;
2345 /* And return type. */
2346 if (!same_type_p (TREE_TYPE (TREE_TYPE (one)),
2347 TREE_TYPE (TREE_TYPE (two))))
2348 return false;
2351 return true;
2354 /* Push DECL into nonclass LEVEL BINDING or SLOT. OLD is the current
2355 binding value (possibly with anticipated builtins stripped).
2356 Diagnose conflicts and return updated decl. */
2358 static tree
2359 update_binding (cp_binding_level *level, cxx_binding *binding, tree *slot,
2360 tree old, tree decl, bool is_friend)
2362 tree to_val = decl;
2363 tree old_type = slot ? MAYBE_STAT_TYPE (*slot) : binding->type;
2364 tree to_type = old_type;
2366 gcc_assert (level->kind == sk_namespace ? !binding
2367 : level->kind != sk_class && !slot);
2368 if (old == error_mark_node)
2369 old = NULL_TREE;
2371 if (TREE_CODE (decl) == TYPE_DECL && DECL_ARTIFICIAL (decl))
2373 tree other = to_type;
2375 if (old && TREE_CODE (old) == TYPE_DECL && DECL_ARTIFICIAL (old))
2376 other = old;
2378 /* Pushing an artificial typedef. See if this matches either
2379 the type slot or the old value slot. */
2380 if (!other)
2382 else if (same_type_p (TREE_TYPE (other), TREE_TYPE (decl)))
2383 /* Two artificial decls to same type. Do nothing. */
2384 return other;
2385 else
2386 goto conflict;
2388 if (old)
2390 /* Slide decl into the type slot, keep old unaltered */
2391 to_type = decl;
2392 to_val = old;
2393 goto done;
2397 if (old && TREE_CODE (old) == TYPE_DECL && DECL_ARTIFICIAL (old))
2399 /* Slide old into the type slot. */
2400 to_type = old;
2401 old = NULL_TREE;
2404 if (DECL_DECLARES_FUNCTION_P (decl))
2406 if (!old)
2408 else if (OVL_P (old))
2410 for (ovl_iterator iter (old); iter; ++iter)
2412 tree fn = *iter;
2414 if (iter.using_p () && matching_fn_p (fn, decl))
2416 /* If a function declaration in namespace scope or
2417 block scope has the same name and the same
2418 parameter-type- list (8.3.5) as a function
2419 introduced by a using-declaration, and the
2420 declarations do not declare the same function,
2421 the program is ill-formed. [namespace.udecl]/14 */
2422 if (tree match = duplicate_decls (decl, fn, is_friend))
2423 return match;
2424 else
2425 /* FIXME: To preserve existing error behavior, we
2426 still push the decl. This might change. */
2427 diagnose_name_conflict (decl, fn);
2431 else
2432 goto conflict;
2434 if (to_type != old_type
2435 && warn_shadow
2436 && MAYBE_CLASS_TYPE_P (TREE_TYPE (to_type))
2437 && !(DECL_IN_SYSTEM_HEADER (decl)
2438 && DECL_IN_SYSTEM_HEADER (to_type)))
2439 warning (OPT_Wshadow, "%q#D hides constructor for %q#D",
2440 decl, to_type);
2442 to_val = ovl_insert (decl, old);
2444 else if (!old)
2446 else if (TREE_CODE (old) != TREE_CODE (decl))
2447 /* Different kinds of decls conflict. */
2448 goto conflict;
2449 else if (TREE_CODE (old) == TYPE_DECL)
2451 if (same_type_p (TREE_TYPE (old), TREE_TYPE (decl)))
2452 /* Two type decls to the same type. Do nothing. */
2453 return old;
2454 else
2455 goto conflict;
2457 else if (TREE_CODE (old) == NAMESPACE_DECL)
2459 /* Two maybe-aliased namespaces. If they're to the same target
2460 namespace, that's ok. */
2461 if (ORIGINAL_NAMESPACE (old) != ORIGINAL_NAMESPACE (decl))
2462 goto conflict;
2464 /* The new one must be an alias at this point. */
2465 gcc_assert (DECL_NAMESPACE_ALIAS (decl));
2466 return old;
2468 else if (TREE_CODE (old) == VAR_DECL)
2470 /* There can be two block-scope declarations of the same
2471 variable, so long as they are `extern' declarations. */
2472 if (!DECL_EXTERNAL (old) || !DECL_EXTERNAL (decl))
2473 goto conflict;
2474 else if (tree match = duplicate_decls (decl, old, false))
2475 return match;
2476 else
2477 goto conflict;
2479 else
2481 conflict:
2482 diagnose_name_conflict (decl, old);
2483 to_val = NULL_TREE;
2486 done:
2487 if (to_val)
2489 if (level->kind == sk_namespace || to_type == decl || to_val == decl)
2490 add_decl_to_level (level, decl);
2491 else
2493 gcc_checking_assert (binding->value && OVL_P (binding->value));
2494 update_local_overload (binding, to_val);
2497 if (slot)
2499 if (STAT_HACK_P (*slot))
2501 STAT_TYPE (*slot) = to_type;
2502 STAT_DECL (*slot) = to_val;
2504 else if (to_type)
2505 *slot = stat_hack (to_val, to_type);
2506 else
2507 *slot = to_val;
2509 else
2511 binding->type = to_type;
2512 binding->value = to_val;
2516 return decl;
2519 /* Table of identifiers to extern C declarations (or LISTS thereof). */
2521 static GTY(()) hash_table<named_decl_hash> *extern_c_decls;
2523 /* DECL has C linkage. If we have an existing instance, make sure the
2524 new one is compatible. Make sure it has the same exception
2525 specification [7.5, 7.6]. Add DECL to the map. */
2527 static void
2528 check_extern_c_conflict (tree decl)
2530 /* Ignore artificial or system header decls. */
2531 if (DECL_ARTIFICIAL (decl) || DECL_IN_SYSTEM_HEADER (decl))
2532 return;
2534 /* This only applies to decls at namespace scope. */
2535 if (!DECL_NAMESPACE_SCOPE_P (decl))
2536 return;
2538 if (!extern_c_decls)
2539 extern_c_decls = hash_table<named_decl_hash>::create_ggc (127);
2541 tree *slot = extern_c_decls
2542 ->find_slot_with_hash (DECL_NAME (decl),
2543 IDENTIFIER_HASH_VALUE (DECL_NAME (decl)), INSERT);
2544 if (tree old = *slot)
2546 if (TREE_CODE (old) == OVERLOAD)
2547 old = OVL_FUNCTION (old);
2549 int mismatch = 0;
2550 if (DECL_CONTEXT (old) == DECL_CONTEXT (decl))
2551 ; /* If they're in the same context, we'll have already complained
2552 about a (possible) mismatch, when inserting the decl. */
2553 else if (!decls_match (decl, old))
2554 mismatch = 1;
2555 else if (TREE_CODE (decl) == FUNCTION_DECL
2556 && !comp_except_specs (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (old)),
2557 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl)),
2558 ce_normal))
2559 mismatch = -1;
2560 else if (DECL_ASSEMBLER_NAME_SET_P (old))
2561 SET_DECL_ASSEMBLER_NAME (decl, DECL_ASSEMBLER_NAME (old));
2563 if (mismatch)
2565 auto_diagnostic_group d;
2566 pedwarn (input_location, 0,
2567 "conflicting C language linkage declaration %q#D", decl);
2568 inform (DECL_SOURCE_LOCATION (old),
2569 "previous declaration %q#D", old);
2570 if (mismatch < 0)
2571 inform (input_location,
2572 "due to different exception specifications");
2574 else
2576 if (old == *slot)
2577 /* The hash table expects OVERLOADS, so construct one with
2578 OLD as both the function and the chain. This allocate
2579 an excess OVERLOAD node, but it's rare to have multiple
2580 extern "C" decls of the same name. And we save
2581 complicating the hash table logic (which is used
2582 elsewhere). */
2583 *slot = ovl_make (old, old);
2585 slot = &OVL_CHAIN (*slot);
2587 /* Chain it on for c_linkage_binding's use. */
2588 *slot = tree_cons (NULL_TREE, decl, *slot);
2591 else
2592 *slot = decl;
2595 /* Returns a list of C-linkage decls with the name NAME. Used in
2596 c-family/c-pragma.c to implement redefine_extname pragma. */
2598 tree
2599 c_linkage_bindings (tree name)
2601 if (extern_c_decls)
2602 if (tree *slot = extern_c_decls
2603 ->find_slot_with_hash (name, IDENTIFIER_HASH_VALUE (name), NO_INSERT))
2605 tree result = *slot;
2606 if (TREE_CODE (result) == OVERLOAD)
2607 result = OVL_CHAIN (result);
2608 return result;
2611 return NULL_TREE;
2614 /* Subroutine of check_local_shadow. */
2616 static void
2617 inform_shadowed (tree shadowed)
2619 inform (DECL_SOURCE_LOCATION (shadowed),
2620 "shadowed declaration is here");
2623 /* DECL is being declared at a local scope. Emit suitable shadow
2624 warnings. */
2626 static void
2627 check_local_shadow (tree decl)
2629 /* Don't complain about the parms we push and then pop
2630 while tentatively parsing a function declarator. */
2631 if (TREE_CODE (decl) == PARM_DECL && !DECL_CONTEXT (decl))
2632 return;
2634 /* External decls are something else. */
2635 if (DECL_EXTERNAL (decl))
2636 return;
2638 tree old = NULL_TREE;
2639 cp_binding_level *old_scope = NULL;
2640 if (cxx_binding *binding = outer_binding (DECL_NAME (decl), NULL, true))
2642 old = binding->value;
2643 old_scope = binding->scope;
2646 if (old
2647 && (TREE_CODE (old) == PARM_DECL
2648 || VAR_P (old)
2649 || (TREE_CODE (old) == TYPE_DECL
2650 && (!DECL_ARTIFICIAL (old)
2651 || TREE_CODE (decl) == TYPE_DECL)))
2652 && DECL_FUNCTION_SCOPE_P (old)
2653 && (!DECL_ARTIFICIAL (decl)
2654 || is_capture_proxy (decl)
2655 || DECL_IMPLICIT_TYPEDEF_P (decl)
2656 || (VAR_P (decl) && DECL_ANON_UNION_VAR_P (decl))))
2658 /* DECL shadows a local thing possibly of interest. */
2660 /* DR 2211: check that captures and parameters
2661 do not have the same name. */
2662 if (is_capture_proxy (decl))
2664 if (current_lambda_expr ()
2665 && DECL_CONTEXT (old) == lambda_function (current_lambda_expr ())
2666 && TREE_CODE (old) == PARM_DECL
2667 && DECL_NAME (decl) != this_identifier)
2669 error_at (DECL_SOURCE_LOCATION (old),
2670 "lambda parameter %qD "
2671 "previously declared as a capture", old);
2673 return;
2675 /* Don't complain if it's from an enclosing function. */
2676 else if (DECL_CONTEXT (old) == current_function_decl
2677 && TREE_CODE (decl) != PARM_DECL
2678 && TREE_CODE (old) == PARM_DECL)
2680 /* Go to where the parms should be and see if we find
2681 them there. */
2682 cp_binding_level *b = current_binding_level->level_chain;
2684 if (FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
2685 /* Skip the ctor/dtor cleanup level. */
2686 b = b->level_chain;
2688 /* ARM $8.3 */
2689 if (b->kind == sk_function_parms)
2691 error ("declaration of %q#D shadows a parameter", decl);
2692 return;
2696 /* The local structure or class can't use parameters of
2697 the containing function anyway. */
2698 if (DECL_CONTEXT (old) != current_function_decl)
2700 for (cp_binding_level *scope = current_binding_level;
2701 scope != old_scope; scope = scope->level_chain)
2702 if (scope->kind == sk_class
2703 && !LAMBDA_TYPE_P (scope->this_entity))
2704 return;
2706 /* Error if redeclaring a local declared in a
2707 init-statement or in the condition of an if or
2708 switch statement when the new declaration is in the
2709 outermost block of the controlled statement.
2710 Redeclaring a variable from a for or while condition is
2711 detected elsewhere. */
2712 else if (VAR_P (old)
2713 && old_scope == current_binding_level->level_chain
2714 && (old_scope->kind == sk_cond || old_scope->kind == sk_for))
2716 auto_diagnostic_group d;
2717 error ("redeclaration of %q#D", decl);
2718 inform (DECL_SOURCE_LOCATION (old),
2719 "%q#D previously declared here", old);
2720 return;
2722 /* C++11:
2723 3.3.3/3: The name declared in an exception-declaration (...)
2724 shall not be redeclared in the outermost block of the handler.
2725 3.3.3/2: A parameter name shall not be redeclared (...) in
2726 the outermost block of any handler associated with a
2727 function-try-block.
2728 3.4.1/15: The function parameter names shall not be redeclared
2729 in the exception-declaration nor in the outermost block of a
2730 handler for the function-try-block. */
2731 else if ((TREE_CODE (old) == VAR_DECL
2732 && old_scope == current_binding_level->level_chain
2733 && old_scope->kind == sk_catch)
2734 || (TREE_CODE (old) == PARM_DECL
2735 && (current_binding_level->kind == sk_catch
2736 || current_binding_level->level_chain->kind == sk_catch)
2737 && in_function_try_handler))
2739 auto_diagnostic_group d;
2740 if (permerror (input_location, "redeclaration of %q#D", decl))
2741 inform (DECL_SOURCE_LOCATION (old),
2742 "%q#D previously declared here", old);
2743 return;
2746 /* If '-Wshadow=compatible-local' is specified without other
2747 -Wshadow= flags, we will warn only when the type of the
2748 shadowing variable (DECL) can be converted to that of the
2749 shadowed parameter (OLD_LOCAL). The reason why we only check
2750 if DECL's type can be converted to OLD_LOCAL's type (but not the
2751 other way around) is because when users accidentally shadow a
2752 parameter, more than often they would use the variable
2753 thinking (mistakenly) it's still the parameter. It would be
2754 rare that users would use the variable in the place that
2755 expects the parameter but thinking it's a new decl. */
2757 enum opt_code warning_code;
2758 if (warn_shadow)
2759 warning_code = OPT_Wshadow;
2760 else if (warn_shadow_local)
2761 warning_code = OPT_Wshadow_local;
2762 else if (warn_shadow_compatible_local
2763 && (same_type_p (TREE_TYPE (old), TREE_TYPE (decl))
2764 || (!dependent_type_p (TREE_TYPE (decl))
2765 && !dependent_type_p (TREE_TYPE (old))
2766 /* If the new decl uses auto, we don't yet know
2767 its type (the old type cannot be using auto
2768 at this point, without also being
2769 dependent). This is an indication we're
2770 (now) doing the shadow checking too
2771 early. */
2772 && !type_uses_auto (TREE_TYPE (decl))
2773 && can_convert (TREE_TYPE (old), TREE_TYPE (decl),
2774 tf_none))))
2775 warning_code = OPT_Wshadow_compatible_local;
2776 else
2777 return;
2779 const char *msg;
2780 if (TREE_CODE (old) == PARM_DECL)
2781 msg = "declaration of %q#D shadows a parameter";
2782 else if (is_capture_proxy (old))
2783 msg = "declaration of %qD shadows a lambda capture";
2784 else
2785 msg = "declaration of %qD shadows a previous local";
2787 auto_diagnostic_group d;
2788 if (warning_at (input_location, warning_code, msg, decl))
2789 inform_shadowed (old);
2790 return;
2793 if (!warn_shadow)
2794 return;
2796 /* Don't warn for artificial things that are not implicit typedefs. */
2797 if (DECL_ARTIFICIAL (decl) && !DECL_IMPLICIT_TYPEDEF_P (decl))
2798 return;
2800 if (nonlambda_method_basetype ())
2801 if (tree member = lookup_member (current_nonlambda_class_type (),
2802 DECL_NAME (decl), /*protect=*/0,
2803 /*want_type=*/false, tf_warning_or_error))
2805 member = MAYBE_BASELINK_FUNCTIONS (member);
2807 /* Warn if a variable shadows a non-function, or the variable
2808 is a function or a pointer-to-function. */
2809 if (!OVL_P (member)
2810 || TREE_CODE (decl) == FUNCTION_DECL
2811 || TYPE_PTRFN_P (TREE_TYPE (decl))
2812 || TYPE_PTRMEMFUNC_P (TREE_TYPE (decl)))
2814 auto_diagnostic_group d;
2815 if (warning_at (input_location, OPT_Wshadow,
2816 "declaration of %qD shadows a member of %qT",
2817 decl, current_nonlambda_class_type ())
2818 && DECL_P (member))
2819 inform_shadowed (member);
2821 return;
2824 /* Now look for a namespace shadow. */
2825 old = find_namespace_value (current_namespace, DECL_NAME (decl));
2826 if (old
2827 && (VAR_P (old)
2828 || (TREE_CODE (old) == TYPE_DECL
2829 && (!DECL_ARTIFICIAL (old)
2830 || TREE_CODE (decl) == TYPE_DECL)))
2831 && !instantiating_current_function_p ())
2832 /* XXX shadow warnings in outer-more namespaces */
2834 auto_diagnostic_group d;
2835 if (warning_at (input_location, OPT_Wshadow,
2836 "declaration of %qD shadows a global declaration",
2837 decl))
2838 inform_shadowed (old);
2839 return;
2842 return;
2845 /* DECL is being pushed inside function CTX. Set its context, if
2846 needed. */
2848 static void
2849 set_decl_context_in_fn (tree ctx, tree decl)
2851 if (!DECL_CONTEXT (decl)
2852 /* A local declaration for a function doesn't constitute
2853 nesting. */
2854 && TREE_CODE (decl) != FUNCTION_DECL
2855 /* A local declaration for an `extern' variable is in the
2856 scope of the current namespace, not the current
2857 function. */
2858 && !(VAR_P (decl) && DECL_EXTERNAL (decl))
2859 /* When parsing the parameter list of a function declarator,
2860 don't set DECL_CONTEXT to an enclosing function. When we
2861 push the PARM_DECLs in order to process the function body,
2862 current_binding_level->this_entity will be set. */
2863 && !(TREE_CODE (decl) == PARM_DECL
2864 && current_binding_level->kind == sk_function_parms
2865 && current_binding_level->this_entity == NULL))
2866 DECL_CONTEXT (decl) = ctx;
2868 /* If this is the declaration for a namespace-scope function,
2869 but the declaration itself is in a local scope, mark the
2870 declaration. */
2871 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (decl))
2872 DECL_LOCAL_FUNCTION_P (decl) = 1;
2875 /* DECL is a local-scope decl with linkage. SHADOWED is true if the
2876 name is already bound at the current level.
2878 [basic.link] If there is a visible declaration of an entity with
2879 linkage having the same name and type, ignoring entities declared
2880 outside the innermost enclosing namespace scope, the block scope
2881 declaration declares that same entity and receives the linkage of
2882 the previous declaration.
2884 Also, make sure that this decl matches any existing external decl
2885 in the enclosing namespace. */
2887 static void
2888 set_local_extern_decl_linkage (tree decl, bool shadowed)
2890 tree ns_value = decl; /* Unique marker. */
2892 if (!shadowed)
2894 tree loc_value = innermost_non_namespace_value (DECL_NAME (decl));
2895 if (!loc_value)
2897 ns_value
2898 = find_namespace_value (current_namespace, DECL_NAME (decl));
2899 loc_value = ns_value;
2901 if (loc_value == error_mark_node
2902 /* An ambiguous lookup. */
2903 || (loc_value && TREE_CODE (loc_value) == TREE_LIST))
2904 loc_value = NULL_TREE;
2906 for (ovl_iterator iter (loc_value); iter; ++iter)
2907 if (!iter.hidden_p ()
2908 && (TREE_STATIC (*iter) || DECL_EXTERNAL (*iter))
2909 && decls_match (*iter, decl))
2911 /* The standard only says that the local extern inherits
2912 linkage from the previous decl; in particular, default
2913 args are not shared. Add the decl into a hash table to
2914 make sure only the previous decl in this case is seen
2915 by the middle end. */
2916 struct cxx_int_tree_map *h;
2918 /* We inherit the outer decl's linkage. But we're a
2919 different decl. */
2920 TREE_PUBLIC (decl) = TREE_PUBLIC (*iter);
2922 if (cp_function_chain->extern_decl_map == NULL)
2923 cp_function_chain->extern_decl_map
2924 = hash_table<cxx_int_tree_map_hasher>::create_ggc (20);
2926 h = ggc_alloc<cxx_int_tree_map> ();
2927 h->uid = DECL_UID (decl);
2928 h->to = *iter;
2929 cxx_int_tree_map **loc = cp_function_chain->extern_decl_map
2930 ->find_slot (h, INSERT);
2931 *loc = h;
2932 break;
2936 if (TREE_PUBLIC (decl))
2938 /* DECL is externally visible. Make sure it matches a matching
2939 decl in the namespace scope. We only really need to check
2940 this when inserting the decl, not when we find an existing
2941 match in the current scope. However, in practice we're
2942 going to be inserting a new decl in the majority of cases --
2943 who writes multiple extern decls for the same thing in the
2944 same local scope? Doing it here often avoids a duplicate
2945 namespace lookup. */
2947 /* Avoid repeating a lookup. */
2948 if (ns_value == decl)
2949 ns_value = find_namespace_value (current_namespace, DECL_NAME (decl));
2951 if (ns_value == error_mark_node
2952 || (ns_value && TREE_CODE (ns_value) == TREE_LIST))
2953 ns_value = NULL_TREE;
2955 for (ovl_iterator iter (ns_value); iter; ++iter)
2957 tree other = *iter;
2959 if (!(TREE_PUBLIC (other) || DECL_EXTERNAL (other)))
2960 ; /* Not externally visible. */
2961 else if (DECL_EXTERN_C_P (decl) && DECL_EXTERN_C_P (other))
2962 ; /* Both are extern "C", we'll check via that mechanism. */
2963 else if (TREE_CODE (other) != TREE_CODE (decl)
2964 || ((VAR_P (decl) || matching_fn_p (other, decl))
2965 && !comptypes (TREE_TYPE (decl), TREE_TYPE (other),
2966 COMPARE_REDECLARATION)))
2968 auto_diagnostic_group d;
2969 if (permerror (DECL_SOURCE_LOCATION (decl),
2970 "local external declaration %q#D", decl))
2971 inform (DECL_SOURCE_LOCATION (other),
2972 "does not match previous declaration %q#D", other);
2973 break;
2979 /* Record DECL as belonging to the current lexical scope. Check for
2980 errors (such as an incompatible declaration for the same name
2981 already seen in the same scope). IS_FRIEND is true if DECL is
2982 declared as a friend.
2984 Returns either DECL or an old decl for the same name. If an old
2985 decl is returned, it may have been smashed to agree with what DECL
2986 says. */
2988 static tree
2989 do_pushdecl (tree decl, bool is_friend)
2991 if (decl == error_mark_node)
2992 return error_mark_node;
2994 if (!DECL_TEMPLATE_PARM_P (decl) && current_function_decl)
2995 set_decl_context_in_fn (current_function_decl, decl);
2997 /* The binding level we will be pushing into. During local class
2998 pushing, we want to push to the containing scope. */
2999 cp_binding_level *level = current_binding_level;
3000 while (level->kind == sk_class)
3001 level = level->level_chain;
3003 /* An anonymous namespace has a NULL DECL_NAME, but we still want to
3004 insert it. Other NULL-named decls, not so much. */
3005 tree name = DECL_NAME (decl);
3006 if (name || TREE_CODE (decl) == NAMESPACE_DECL)
3008 cxx_binding *binding = NULL; /* Local scope binding. */
3009 tree ns = NULL_TREE; /* Searched namespace. */
3010 tree *slot = NULL; /* Binding slot in namespace. */
3011 tree old = NULL_TREE;
3013 if (level->kind == sk_namespace)
3015 /* We look in the decl's namespace for an existing
3016 declaration, even though we push into the current
3017 namespace. */
3018 ns = (DECL_NAMESPACE_SCOPE_P (decl)
3019 ? CP_DECL_CONTEXT (decl) : current_namespace);
3020 /* Create the binding, if this is current namespace, because
3021 that's where we'll be pushing anyway. */
3022 slot = find_namespace_slot (ns, name, ns == current_namespace);
3023 if (slot)
3024 old = MAYBE_STAT_DECL (*slot);
3026 else
3028 binding = find_local_binding (level, name);
3029 if (binding)
3030 old = binding->value;
3033 if (current_function_decl && VAR_OR_FUNCTION_DECL_P (decl)
3034 && DECL_EXTERNAL (decl))
3035 set_local_extern_decl_linkage (decl, old != NULL_TREE);
3037 if (old == error_mark_node)
3038 old = NULL_TREE;
3040 for (ovl_iterator iter (old); iter; ++iter)
3041 if (iter.using_p ())
3042 ; /* Ignore using decls here. */
3043 else if (tree match = duplicate_decls (decl, *iter, is_friend))
3045 if (match == error_mark_node)
3047 else if (TREE_CODE (match) == TYPE_DECL)
3048 /* The IDENTIFIER will have the type referring to the
3049 now-smashed TYPE_DECL, because ...? Reset it. */
3050 SET_IDENTIFIER_TYPE_VALUE (name, TREE_TYPE (match));
3051 else if (iter.hidden_p () && !DECL_HIDDEN_P (match))
3053 /* Unhiding a previously hidden decl. */
3054 tree head = iter.reveal_node (old);
3055 if (head != old)
3057 if (!ns)
3059 update_local_overload (binding, head);
3060 binding->value = head;
3062 else if (STAT_HACK_P (*slot))
3063 STAT_DECL (*slot) = head;
3064 else
3065 *slot = head;
3067 if (DECL_EXTERN_C_P (match))
3068 /* We need to check and register the decl now. */
3069 check_extern_c_conflict (match);
3071 return match;
3074 /* We are pushing a new decl. */
3076 /* Skip a hidden builtin we failed to match already. There can
3077 only be one. */
3078 if (old && anticipated_builtin_p (old))
3079 old = OVL_CHAIN (old);
3081 check_template_shadow (decl);
3083 if (DECL_DECLARES_FUNCTION_P (decl))
3085 check_default_args (decl);
3087 if (is_friend)
3089 if (level->kind != sk_namespace)
3091 /* In a local class, a friend function declaration must
3092 find a matching decl in the innermost non-class scope.
3093 [class.friend/11] */
3094 error ("friend declaration %qD in local class without "
3095 "prior local declaration", decl);
3096 /* Don't attempt to push it. */
3097 return error_mark_node;
3099 /* Hide it from ordinary lookup. */
3100 DECL_ANTICIPATED (decl) = DECL_HIDDEN_FRIEND_P (decl) = true;
3104 if (level->kind != sk_namespace)
3106 check_local_shadow (decl);
3108 if (TREE_CODE (decl) == NAMESPACE_DECL)
3109 /* A local namespace alias. */
3110 set_identifier_type_value (name, NULL_TREE);
3112 if (!binding)
3113 binding = create_local_binding (level, name);
3115 else if (!slot)
3117 ns = current_namespace;
3118 slot = find_namespace_slot (ns, name, true);
3119 /* Update OLD to reflect the namespace we're going to be
3120 pushing into. */
3121 old = MAYBE_STAT_DECL (*slot);
3124 old = update_binding (level, binding, slot, old, decl, is_friend);
3126 if (old != decl)
3127 /* An existing decl matched, use it. */
3128 decl = old;
3129 else if (TREE_CODE (decl) == TYPE_DECL)
3131 tree type = TREE_TYPE (decl);
3133 if (type != error_mark_node)
3135 if (TYPE_NAME (type) != decl)
3136 set_underlying_type (decl);
3138 if (!ns)
3139 set_identifier_type_value_with_scope (name, decl, level);
3140 else
3141 SET_IDENTIFIER_TYPE_VALUE (name, global_type_node);
3144 /* If this is a locally defined typedef in a function that
3145 is not a template instantation, record it to implement
3146 -Wunused-local-typedefs. */
3147 if (!instantiating_current_function_p ())
3148 record_locally_defined_typedef (decl);
3150 else if (VAR_P (decl))
3151 maybe_register_incomplete_var (decl);
3153 if ((VAR_P (decl) || TREE_CODE (decl) == FUNCTION_DECL)
3154 && DECL_EXTERN_C_P (decl))
3155 check_extern_c_conflict (decl);
3157 else
3158 add_decl_to_level (level, decl);
3160 return decl;
3163 /* Record a decl-node X as belonging to the current lexical scope.
3164 It's a friend if IS_FRIEND is true -- which affects exactly where
3165 we push it. */
3167 tree
3168 pushdecl (tree x, bool is_friend)
3170 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3171 tree ret = do_pushdecl (x, is_friend);
3172 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3173 return ret;
3176 /* Enter DECL into the symbol table, if that's appropriate. Returns
3177 DECL, or a modified version thereof. */
3179 tree
3180 maybe_push_decl (tree decl)
3182 tree type = TREE_TYPE (decl);
3184 /* Add this decl to the current binding level, but not if it comes
3185 from another scope, e.g. a static member variable. TEM may equal
3186 DECL or it may be a previous decl of the same name. */
3187 if (decl == error_mark_node
3188 || (TREE_CODE (decl) != PARM_DECL
3189 && DECL_CONTEXT (decl) != NULL_TREE
3190 /* Definitions of namespace members outside their namespace are
3191 possible. */
3192 && !DECL_NAMESPACE_SCOPE_P (decl))
3193 || (TREE_CODE (decl) == TEMPLATE_DECL && !namespace_bindings_p ())
3194 || type == unknown_type_node
3195 /* The declaration of a template specialization does not affect
3196 the functions available for overload resolution, so we do not
3197 call pushdecl. */
3198 || (TREE_CODE (decl) == FUNCTION_DECL
3199 && DECL_TEMPLATE_SPECIALIZATION (decl)))
3200 return decl;
3201 else
3202 return pushdecl (decl);
3205 /* Bind DECL to ID in the current_binding_level, assumed to be a local
3206 binding level. If IS_USING is true, DECL got here through a
3207 using-declaration. */
3209 static void
3210 push_local_binding (tree id, tree decl, bool is_using)
3212 /* Skip over any local classes. This makes sense if we call
3213 push_local_binding with a friend decl of a local class. */
3214 cp_binding_level *b = innermost_nonclass_level ();
3216 gcc_assert (b->kind != sk_namespace);
3217 if (find_local_binding (b, id))
3219 /* Supplement the existing binding. */
3220 if (!supplement_binding (IDENTIFIER_BINDING (id), decl))
3221 /* It didn't work. Something else must be bound at this
3222 level. Do not add DECL to the list of things to pop
3223 later. */
3224 return;
3226 else
3227 /* Create a new binding. */
3228 push_binding (id, decl, b);
3230 if (TREE_CODE (decl) == OVERLOAD || is_using)
3231 /* We must put the OVERLOAD or using into a TREE_LIST since we
3232 cannot use the decl's chain itself. */
3233 decl = build_tree_list (NULL_TREE, decl);
3235 /* And put DECL on the list of things declared by the current
3236 binding level. */
3237 add_decl_to_level (b, decl);
3241 /* true means unconditionally make a BLOCK for the next level pushed. */
3243 static bool keep_next_level_flag;
3245 static int binding_depth = 0;
3247 static void
3248 indent (int depth)
3250 int i;
3252 for (i = 0; i < depth * 2; i++)
3253 putc (' ', stderr);
3256 /* Return a string describing the kind of SCOPE we have. */
3257 static const char *
3258 cp_binding_level_descriptor (cp_binding_level *scope)
3260 /* The order of this table must match the "scope_kind"
3261 enumerators. */
3262 static const char* scope_kind_names[] = {
3263 "block-scope",
3264 "cleanup-scope",
3265 "try-scope",
3266 "catch-scope",
3267 "for-scope",
3268 "function-parameter-scope",
3269 "class-scope",
3270 "namespace-scope",
3271 "template-parameter-scope",
3272 "template-explicit-spec-scope"
3274 const scope_kind kind = scope->explicit_spec_p
3275 ? sk_template_spec : scope->kind;
3277 return scope_kind_names[kind];
3280 /* Output a debugging information about SCOPE when performing
3281 ACTION at LINE. */
3282 static void
3283 cp_binding_level_debug (cp_binding_level *scope, int line, const char *action)
3285 const char *desc = cp_binding_level_descriptor (scope);
3286 if (scope->this_entity)
3287 verbatim ("%s %<%s(%E)%> %p %d\n", action, desc,
3288 scope->this_entity, (void *) scope, line);
3289 else
3290 verbatim ("%s %s %p %d\n", action, desc, (void *) scope, line);
3293 /* A chain of binding_level structures awaiting reuse. */
3295 static GTY((deletable)) cp_binding_level *free_binding_level;
3297 /* Insert SCOPE as the innermost binding level. */
3299 void
3300 push_binding_level (cp_binding_level *scope)
3302 /* Add it to the front of currently active scopes stack. */
3303 scope->level_chain = current_binding_level;
3304 current_binding_level = scope;
3305 keep_next_level_flag = false;
3307 if (ENABLE_SCOPE_CHECKING)
3309 scope->binding_depth = binding_depth;
3310 indent (binding_depth);
3311 cp_binding_level_debug (scope, LOCATION_LINE (input_location),
3312 "push");
3313 binding_depth++;
3317 /* Create a new KIND scope and make it the top of the active scopes stack.
3318 ENTITY is the scope of the associated C++ entity (namespace, class,
3319 function, C++0x enumeration); it is NULL otherwise. */
3321 cp_binding_level *
3322 begin_scope (scope_kind kind, tree entity)
3324 cp_binding_level *scope;
3326 /* Reuse or create a struct for this binding level. */
3327 if (!ENABLE_SCOPE_CHECKING && free_binding_level)
3329 scope = free_binding_level;
3330 free_binding_level = scope->level_chain;
3331 memset (scope, 0, sizeof (cp_binding_level));
3333 else
3334 scope = ggc_cleared_alloc<cp_binding_level> ();
3336 scope->this_entity = entity;
3337 scope->more_cleanups_ok = true;
3338 switch (kind)
3340 case sk_cleanup:
3341 scope->keep = true;
3342 break;
3344 case sk_template_spec:
3345 scope->explicit_spec_p = true;
3346 kind = sk_template_parms;
3347 /* Fall through. */
3348 case sk_template_parms:
3349 case sk_block:
3350 case sk_try:
3351 case sk_catch:
3352 case sk_for:
3353 case sk_cond:
3354 case sk_class:
3355 case sk_scoped_enum:
3356 case sk_function_parms:
3357 case sk_transaction:
3358 case sk_omp:
3359 scope->keep = keep_next_level_flag;
3360 break;
3362 case sk_namespace:
3363 NAMESPACE_LEVEL (entity) = scope;
3364 break;
3366 default:
3367 /* Should not happen. */
3368 gcc_unreachable ();
3369 break;
3371 scope->kind = kind;
3373 push_binding_level (scope);
3375 return scope;
3378 /* We're about to leave current scope. Pop the top of the stack of
3379 currently active scopes. Return the enclosing scope, now active. */
3381 cp_binding_level *
3382 leave_scope (void)
3384 cp_binding_level *scope = current_binding_level;
3386 if (scope->kind == sk_namespace && class_binding_level)
3387 current_binding_level = class_binding_level;
3389 /* We cannot leave a scope, if there are none left. */
3390 if (NAMESPACE_LEVEL (global_namespace))
3391 gcc_assert (!global_scope_p (scope));
3393 if (ENABLE_SCOPE_CHECKING)
3395 indent (--binding_depth);
3396 cp_binding_level_debug (scope, LOCATION_LINE (input_location),
3397 "leave");
3400 /* Move one nesting level up. */
3401 current_binding_level = scope->level_chain;
3403 /* Namespace-scopes are left most probably temporarily, not
3404 completely; they can be reopened later, e.g. in namespace-extension
3405 or any name binding activity that requires us to resume a
3406 namespace. For classes, we cache some binding levels. For other
3407 scopes, we just make the structure available for reuse. */
3408 if (scope->kind != sk_namespace
3409 && scope->kind != sk_class)
3411 scope->level_chain = free_binding_level;
3412 gcc_assert (!ENABLE_SCOPE_CHECKING
3413 || scope->binding_depth == binding_depth);
3414 free_binding_level = scope;
3417 if (scope->kind == sk_class)
3419 /* Reset DEFINING_CLASS_P to allow for reuse of a
3420 class-defining scope in a non-defining context. */
3421 scope->defining_class_p = 0;
3423 /* Find the innermost enclosing class scope, and reset
3424 CLASS_BINDING_LEVEL appropriately. */
3425 class_binding_level = NULL;
3426 for (scope = current_binding_level; scope; scope = scope->level_chain)
3427 if (scope->kind == sk_class)
3429 class_binding_level = scope;
3430 break;
3434 return current_binding_level;
3437 static void
3438 resume_scope (cp_binding_level* b)
3440 /* Resuming binding levels is meant only for namespaces,
3441 and those cannot nest into classes. */
3442 gcc_assert (!class_binding_level);
3443 /* Also, resuming a non-directly nested namespace is a no-no. */
3444 gcc_assert (b->level_chain == current_binding_level);
3445 current_binding_level = b;
3446 if (ENABLE_SCOPE_CHECKING)
3448 b->binding_depth = binding_depth;
3449 indent (binding_depth);
3450 cp_binding_level_debug (b, LOCATION_LINE (input_location), "resume");
3451 binding_depth++;
3455 /* Return the innermost binding level that is not for a class scope. */
3457 static cp_binding_level *
3458 innermost_nonclass_level (void)
3460 cp_binding_level *b;
3462 b = current_binding_level;
3463 while (b->kind == sk_class)
3464 b = b->level_chain;
3466 return b;
3469 /* We're defining an object of type TYPE. If it needs a cleanup, but
3470 we're not allowed to add any more objects with cleanups to the current
3471 scope, create a new binding level. */
3473 void
3474 maybe_push_cleanup_level (tree type)
3476 if (type != error_mark_node
3477 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
3478 && current_binding_level->more_cleanups_ok == 0)
3480 begin_scope (sk_cleanup, NULL);
3481 current_binding_level->statement_list = push_stmt_list ();
3485 /* Return true if we are in the global binding level. */
3487 bool
3488 global_bindings_p (void)
3490 return global_scope_p (current_binding_level);
3493 /* True if we are currently in a toplevel binding level. This
3494 means either the global binding level or a namespace in a toplevel
3495 binding level. Since there are no non-toplevel namespace levels,
3496 this really means any namespace or template parameter level. We
3497 also include a class whose context is toplevel. */
3499 bool
3500 toplevel_bindings_p (void)
3502 cp_binding_level *b = innermost_nonclass_level ();
3504 return b->kind == sk_namespace || b->kind == sk_template_parms;
3507 /* True if this is a namespace scope, or if we are defining a class
3508 which is itself at namespace scope, or whose enclosing class is
3509 such a class, etc. */
3511 bool
3512 namespace_bindings_p (void)
3514 cp_binding_level *b = innermost_nonclass_level ();
3516 return b->kind == sk_namespace;
3519 /* True if the innermost non-class scope is a block scope. */
3521 bool
3522 local_bindings_p (void)
3524 cp_binding_level *b = innermost_nonclass_level ();
3525 return b->kind < sk_function_parms || b->kind == sk_omp;
3528 /* True if the current level needs to have a BLOCK made. */
3530 bool
3531 kept_level_p (void)
3533 return (current_binding_level->blocks != NULL_TREE
3534 || current_binding_level->keep
3535 || current_binding_level->kind == sk_cleanup
3536 || current_binding_level->names != NULL_TREE
3537 || current_binding_level->using_directives);
3540 /* Returns the kind of the innermost scope. */
3542 scope_kind
3543 innermost_scope_kind (void)
3545 return current_binding_level->kind;
3548 /* Returns true if this scope was created to store template parameters. */
3550 bool
3551 template_parm_scope_p (void)
3553 return innermost_scope_kind () == sk_template_parms;
3556 /* If KEEP is true, make a BLOCK node for the next binding level,
3557 unconditionally. Otherwise, use the normal logic to decide whether
3558 or not to create a BLOCK. */
3560 void
3561 keep_next_level (bool keep)
3563 keep_next_level_flag = keep;
3566 /* Return the list of declarations of the current local scope. */
3568 tree
3569 get_local_decls (void)
3571 gcc_assert (current_binding_level->kind != sk_namespace
3572 && current_binding_level->kind != sk_class);
3573 return current_binding_level->names;
3576 /* Return how many function prototypes we are currently nested inside. */
3579 function_parm_depth (void)
3581 int level = 0;
3582 cp_binding_level *b;
3584 for (b = current_binding_level;
3585 b->kind == sk_function_parms;
3586 b = b->level_chain)
3587 ++level;
3589 return level;
3592 /* For debugging. */
3593 static int no_print_functions = 0;
3594 static int no_print_builtins = 0;
3596 static void
3597 print_binding_level (cp_binding_level* lvl)
3599 tree t;
3600 int i = 0, len;
3601 fprintf (stderr, " blocks=%p", (void *) lvl->blocks);
3602 if (lvl->more_cleanups_ok)
3603 fprintf (stderr, " more-cleanups-ok");
3604 if (lvl->have_cleanups)
3605 fprintf (stderr, " have-cleanups");
3606 fprintf (stderr, "\n");
3607 if (lvl->names)
3609 fprintf (stderr, " names:\t");
3610 /* We can probably fit 3 names to a line? */
3611 for (t = lvl->names; t; t = TREE_CHAIN (t))
3613 if (no_print_functions && (TREE_CODE (t) == FUNCTION_DECL))
3614 continue;
3615 if (no_print_builtins
3616 && (TREE_CODE (t) == TYPE_DECL)
3617 && DECL_IS_BUILTIN (t))
3618 continue;
3620 /* Function decls tend to have longer names. */
3621 if (TREE_CODE (t) == FUNCTION_DECL)
3622 len = 3;
3623 else
3624 len = 2;
3625 i += len;
3626 if (i > 6)
3628 fprintf (stderr, "\n\t");
3629 i = len;
3631 print_node_brief (stderr, "", t, 0);
3632 if (t == error_mark_node)
3633 break;
3635 if (i)
3636 fprintf (stderr, "\n");
3638 if (vec_safe_length (lvl->class_shadowed))
3640 size_t i;
3641 cp_class_binding *b;
3642 fprintf (stderr, " class-shadowed:");
3643 FOR_EACH_VEC_ELT (*lvl->class_shadowed, i, b)
3644 fprintf (stderr, " %s ", IDENTIFIER_POINTER (b->identifier));
3645 fprintf (stderr, "\n");
3647 if (lvl->type_shadowed)
3649 fprintf (stderr, " type-shadowed:");
3650 for (t = lvl->type_shadowed; t; t = TREE_CHAIN (t))
3652 fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
3654 fprintf (stderr, "\n");
3658 DEBUG_FUNCTION void
3659 debug (cp_binding_level &ref)
3661 print_binding_level (&ref);
3664 DEBUG_FUNCTION void
3665 debug (cp_binding_level *ptr)
3667 if (ptr)
3668 debug (*ptr);
3669 else
3670 fprintf (stderr, "<nil>\n");
3674 static void
3675 print_other_binding_stack (cp_binding_level *stack)
3677 cp_binding_level *level;
3678 for (level = stack; !global_scope_p (level); level = level->level_chain)
3680 fprintf (stderr, "binding level %p\n", (void *) level);
3681 print_binding_level (level);
3685 void
3686 print_binding_stack (void)
3688 cp_binding_level *b;
3689 fprintf (stderr, "current_binding_level=%p\n"
3690 "class_binding_level=%p\n"
3691 "NAMESPACE_LEVEL (global_namespace)=%p\n",
3692 (void *) current_binding_level, (void *) class_binding_level,
3693 (void *) NAMESPACE_LEVEL (global_namespace));
3694 if (class_binding_level)
3696 for (b = class_binding_level; b; b = b->level_chain)
3697 if (b == current_binding_level)
3698 break;
3699 if (b)
3700 b = class_binding_level;
3701 else
3702 b = current_binding_level;
3704 else
3705 b = current_binding_level;
3706 print_other_binding_stack (b);
3707 fprintf (stderr, "global:\n");
3708 print_binding_level (NAMESPACE_LEVEL (global_namespace));
3711 /* Return the type associated with ID. */
3713 static tree
3714 identifier_type_value_1 (tree id)
3716 /* There is no type with that name, anywhere. */
3717 if (REAL_IDENTIFIER_TYPE_VALUE (id) == NULL_TREE)
3718 return NULL_TREE;
3719 /* This is not the type marker, but the real thing. */
3720 if (REAL_IDENTIFIER_TYPE_VALUE (id) != global_type_node)
3721 return REAL_IDENTIFIER_TYPE_VALUE (id);
3722 /* Have to search for it. It must be on the global level, now.
3723 Ask lookup_name not to return non-types. */
3724 id = lookup_name_real (id, 2, 1, /*block_p=*/true, 0, 0);
3725 if (id)
3726 return TREE_TYPE (id);
3727 return NULL_TREE;
3730 /* Wrapper for identifier_type_value_1. */
3732 tree
3733 identifier_type_value (tree id)
3735 tree ret;
3736 timevar_start (TV_NAME_LOOKUP);
3737 ret = identifier_type_value_1 (id);
3738 timevar_stop (TV_NAME_LOOKUP);
3739 return ret;
3742 /* Push a definition of struct, union or enum tag named ID. into
3743 binding_level B. DECL is a TYPE_DECL for the type. We assume that
3744 the tag ID is not already defined. */
3746 static void
3747 set_identifier_type_value_with_scope (tree id, tree decl, cp_binding_level *b)
3749 tree type;
3751 if (b->kind != sk_namespace)
3753 /* Shadow the marker, not the real thing, so that the marker
3754 gets restored later. */
3755 tree old_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
3756 b->type_shadowed
3757 = tree_cons (id, old_type_value, b->type_shadowed);
3758 type = decl ? TREE_TYPE (decl) : NULL_TREE;
3759 TREE_TYPE (b->type_shadowed) = type;
3761 else
3763 tree *slot = find_namespace_slot (current_namespace, id, true);
3764 gcc_assert (decl);
3765 update_binding (b, NULL, slot, MAYBE_STAT_DECL (*slot), decl, false);
3767 /* Store marker instead of real type. */
3768 type = global_type_node;
3770 SET_IDENTIFIER_TYPE_VALUE (id, type);
3773 /* As set_identifier_type_value_with_scope, but using
3774 current_binding_level. */
3776 void
3777 set_identifier_type_value (tree id, tree decl)
3779 set_identifier_type_value_with_scope (id, decl, current_binding_level);
3782 /* Return the name for the constructor (or destructor) for the
3783 specified class. */
3785 tree
3786 constructor_name (tree type)
3788 tree decl = TYPE_NAME (TYPE_MAIN_VARIANT (type));
3790 return decl ? DECL_NAME (decl) : NULL_TREE;
3793 /* Returns TRUE if NAME is the name for the constructor for TYPE,
3794 which must be a class type. */
3796 bool
3797 constructor_name_p (tree name, tree type)
3799 gcc_assert (MAYBE_CLASS_TYPE_P (type));
3801 /* These don't have names. */
3802 if (TREE_CODE (type) == DECLTYPE_TYPE
3803 || TREE_CODE (type) == TYPEOF_TYPE)
3804 return false;
3806 if (name && name == constructor_name (type))
3807 return true;
3809 return false;
3812 /* Counter used to create anonymous type names. */
3814 static GTY(()) int anon_cnt;
3816 /* Return an IDENTIFIER which can be used as a name for
3817 unnamed structs and unions. */
3819 tree
3820 make_anon_name (void)
3822 char buf[32];
3824 sprintf (buf, anon_aggrname_format (), anon_cnt++);
3825 return get_identifier (buf);
3828 /* This code is practically identical to that for creating
3829 anonymous names, but is just used for lambdas instead. This isn't really
3830 necessary, but it's convenient to avoid treating lambdas like other
3831 unnamed types. */
3833 static GTY(()) int lambda_cnt = 0;
3835 tree
3836 make_lambda_name (void)
3838 char buf[32];
3840 sprintf (buf, LAMBDANAME_FORMAT, lambda_cnt++);
3841 return get_identifier (buf);
3844 /* Insert another USING_DECL into the current binding level, returning
3845 this declaration. If this is a redeclaration, do nothing, and
3846 return NULL_TREE if this not in namespace scope (in namespace
3847 scope, a using decl might extend any previous bindings). */
3849 static tree
3850 push_using_decl_1 (tree scope, tree name)
3852 tree decl;
3854 gcc_assert (TREE_CODE (scope) == NAMESPACE_DECL);
3855 gcc_assert (identifier_p (name));
3856 for (decl = current_binding_level->usings; decl; decl = DECL_CHAIN (decl))
3857 if (USING_DECL_SCOPE (decl) == scope && DECL_NAME (decl) == name)
3858 break;
3859 if (decl)
3860 return namespace_bindings_p () ? decl : NULL_TREE;
3861 decl = build_lang_decl (USING_DECL, name, NULL_TREE);
3862 USING_DECL_SCOPE (decl) = scope;
3863 DECL_CHAIN (decl) = current_binding_level->usings;
3864 current_binding_level->usings = decl;
3865 return decl;
3868 /* Wrapper for push_using_decl_1. */
3870 static tree
3871 push_using_decl (tree scope, tree name)
3873 tree ret;
3874 timevar_start (TV_NAME_LOOKUP);
3875 ret = push_using_decl_1 (scope, name);
3876 timevar_stop (TV_NAME_LOOKUP);
3877 return ret;
3880 /* Same as pushdecl, but define X in binding-level LEVEL. We rely on the
3881 caller to set DECL_CONTEXT properly.
3883 Note that this must only be used when X will be the new innermost
3884 binding for its name, as we tack it onto the front of IDENTIFIER_BINDING
3885 without checking to see if the current IDENTIFIER_BINDING comes from a
3886 closer binding level than LEVEL. */
3888 static tree
3889 do_pushdecl_with_scope (tree x, cp_binding_level *level, bool is_friend)
3891 cp_binding_level *b;
3893 if (level->kind == sk_class)
3895 b = class_binding_level;
3896 class_binding_level = level;
3897 pushdecl_class_level (x);
3898 class_binding_level = b;
3900 else
3902 tree function_decl = current_function_decl;
3903 if (level->kind == sk_namespace)
3904 current_function_decl = NULL_TREE;
3905 b = current_binding_level;
3906 current_binding_level = level;
3907 x = pushdecl (x, is_friend);
3908 current_binding_level = b;
3909 current_function_decl = function_decl;
3911 return x;
3914 /* Inject X into the local scope just before the function parms. */
3916 tree
3917 pushdecl_outermost_localscope (tree x)
3919 cp_binding_level *b = NULL;
3920 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3922 /* Find the scope just inside the function parms. */
3923 for (cp_binding_level *n = current_binding_level;
3924 n->kind != sk_function_parms; n = b->level_chain)
3925 b = n;
3927 tree ret = b ? do_pushdecl_with_scope (x, b, false) : error_mark_node;
3928 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3930 return ret;
3933 /* Check a non-member using-declaration. Return the name and scope
3934 being used, and the USING_DECL, or NULL_TREE on failure. */
3936 static tree
3937 validate_nonmember_using_decl (tree decl, tree scope, tree name)
3939 /* [namespace.udecl]
3940 A using-declaration for a class member shall be a
3941 member-declaration. */
3942 if (TYPE_P (scope))
3944 error ("%qT is not a namespace or unscoped enum", scope);
3945 return NULL_TREE;
3947 else if (scope == error_mark_node)
3948 return NULL_TREE;
3950 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR)
3952 /* 7.3.3/5
3953 A using-declaration shall not name a template-id. */
3954 error ("a using-declaration cannot specify a template-id. "
3955 "Try %<using %D%>", name);
3956 return NULL_TREE;
3959 if (TREE_CODE (decl) == NAMESPACE_DECL)
3961 error ("namespace %qD not allowed in using-declaration", decl);
3962 return NULL_TREE;
3965 if (TREE_CODE (decl) == SCOPE_REF)
3967 /* It's a nested name with template parameter dependent scope.
3968 This can only be using-declaration for class member. */
3969 error ("%qT is not a namespace", TREE_OPERAND (decl, 0));
3970 return NULL_TREE;
3973 decl = OVL_FIRST (decl);
3975 /* Make a USING_DECL. */
3976 tree using_decl = push_using_decl (scope, name);
3978 if (using_decl == NULL_TREE
3979 && at_function_scope_p ()
3980 && VAR_P (decl))
3981 /* C++11 7.3.3/10. */
3982 error ("%qD is already declared in this scope", name);
3984 return using_decl;
3987 /* Process a local-scope or namespace-scope using declaration. SCOPE
3988 is the nominated scope to search for NAME. VALUE_P and TYPE_P
3989 point to the binding for NAME in the current scope and are
3990 updated. */
3992 static void
3993 do_nonmember_using_decl (tree scope, tree name, tree *value_p, tree *type_p)
3995 name_lookup lookup (name, 0);
3997 if (!qualified_namespace_lookup (scope, &lookup))
3999 error ("%qD not declared", name);
4000 return;
4002 else if (TREE_CODE (lookup.value) == TREE_LIST)
4004 error ("reference to %qD is ambiguous", name);
4005 print_candidates (lookup.value);
4006 lookup.value = NULL_TREE;
4009 if (lookup.type && TREE_CODE (lookup.type) == TREE_LIST)
4011 error ("reference to %qD is ambiguous", name);
4012 print_candidates (lookup.type);
4013 lookup.type = NULL_TREE;
4016 tree value = *value_p;
4017 tree type = *type_p;
4019 /* Shift the old and new bindings around so we're comparing class and
4020 enumeration names to each other. */
4021 if (value && DECL_IMPLICIT_TYPEDEF_P (value))
4023 type = value;
4024 value = NULL_TREE;
4027 if (lookup.value && DECL_IMPLICIT_TYPEDEF_P (lookup.value))
4029 lookup.type = lookup.value;
4030 lookup.value = NULL_TREE;
4033 if (lookup.value && lookup.value != value)
4035 /* Check for using functions. */
4036 if (OVL_P (lookup.value) && (!value || OVL_P (value)))
4038 for (lkp_iterator usings (lookup.value); usings; ++usings)
4040 tree new_fn = *usings;
4042 /* [namespace.udecl]
4044 If a function declaration in namespace scope or block
4045 scope has the same name and the same parameter types as a
4046 function introduced by a using declaration the program is
4047 ill-formed. */
4048 bool found = false;
4049 for (ovl_iterator old (value); !found && old; ++old)
4051 tree old_fn = *old;
4053 if (new_fn == old_fn)
4054 /* The function already exists in the current
4055 namespace. */
4056 found = true;
4057 else if (old.using_p ())
4058 continue; /* This is a using decl. */
4059 else if (old.hidden_p () && !DECL_HIDDEN_FRIEND_P (old_fn))
4060 continue; /* This is an anticipated builtin. */
4061 else if (!matching_fn_p (new_fn, old_fn))
4062 continue; /* Parameters do not match. */
4063 else if (decls_match (new_fn, old_fn))
4064 found = true;
4065 else
4067 diagnose_name_conflict (new_fn, old_fn);
4068 found = true;
4072 if (!found)
4073 /* Unlike the overload case we don't drop anticipated
4074 builtins here. They don't cause a problem, and
4075 we'd like to match them with a future
4076 declaration. */
4077 value = ovl_insert (new_fn, value, true);
4080 else if (value
4081 /* Ignore anticipated builtins. */
4082 && !anticipated_builtin_p (value)
4083 && !decls_match (lookup.value, value))
4084 diagnose_name_conflict (lookup.value, value);
4085 else
4086 value = lookup.value;
4089 if (lookup.type && lookup.type != type)
4091 if (type && !decls_match (lookup.type, type))
4092 diagnose_name_conflict (lookup.type, type);
4093 else
4094 type = lookup.type;
4097 /* If bind->value is empty, shift any class or enumeration name back. */
4098 if (!value)
4100 value = type;
4101 type = NULL_TREE;
4104 *value_p = value;
4105 *type_p = type;
4108 /* Returns true if ANCESTOR encloses DESCENDANT, including matching.
4109 Both are namespaces. */
4111 bool
4112 is_nested_namespace (tree ancestor, tree descendant, bool inline_only)
4114 int depth = SCOPE_DEPTH (ancestor);
4116 if (!depth && !inline_only)
4117 /* The global namespace encloses everything. */
4118 return true;
4120 while (SCOPE_DEPTH (descendant) > depth
4121 && (!inline_only || DECL_NAMESPACE_INLINE_P (descendant)))
4122 descendant = CP_DECL_CONTEXT (descendant);
4124 return ancestor == descendant;
4127 /* Returns true if ROOT (a namespace, class, or function) encloses
4128 CHILD. CHILD may be either a class type or a namespace. */
4130 bool
4131 is_ancestor (tree root, tree child)
4133 gcc_assert ((TREE_CODE (root) == NAMESPACE_DECL
4134 || TREE_CODE (root) == FUNCTION_DECL
4135 || CLASS_TYPE_P (root)));
4136 gcc_assert ((TREE_CODE (child) == NAMESPACE_DECL
4137 || CLASS_TYPE_P (child)));
4139 /* The global namespace encloses everything. */
4140 if (root == global_namespace)
4141 return true;
4143 /* Search until we reach namespace scope. */
4144 while (TREE_CODE (child) != NAMESPACE_DECL)
4146 /* If we've reached the ROOT, it encloses CHILD. */
4147 if (root == child)
4148 return true;
4149 /* Go out one level. */
4150 if (TYPE_P (child))
4151 child = TYPE_NAME (child);
4152 child = CP_DECL_CONTEXT (child);
4155 if (TREE_CODE (root) == NAMESPACE_DECL)
4156 return is_nested_namespace (root, child);
4158 return false;
4161 /* Enter the class or namespace scope indicated by T suitable for name
4162 lookup. T can be arbitrary scope, not necessary nested inside the
4163 current scope. Returns a non-null scope to pop iff pop_scope
4164 should be called later to exit this scope. */
4166 tree
4167 push_scope (tree t)
4169 if (TREE_CODE (t) == NAMESPACE_DECL)
4170 push_decl_namespace (t);
4171 else if (CLASS_TYPE_P (t))
4173 if (!at_class_scope_p ()
4174 || !same_type_p (current_class_type, t))
4175 push_nested_class (t);
4176 else
4177 /* T is the same as the current scope. There is therefore no
4178 need to re-enter the scope. Since we are not actually
4179 pushing a new scope, our caller should not call
4180 pop_scope. */
4181 t = NULL_TREE;
4184 return t;
4187 /* Leave scope pushed by push_scope. */
4189 void
4190 pop_scope (tree t)
4192 if (t == NULL_TREE)
4193 return;
4194 if (TREE_CODE (t) == NAMESPACE_DECL)
4195 pop_decl_namespace ();
4196 else if CLASS_TYPE_P (t)
4197 pop_nested_class ();
4200 /* Subroutine of push_inner_scope. */
4202 static void
4203 push_inner_scope_r (tree outer, tree inner)
4205 tree prev;
4207 if (outer == inner
4208 || (TREE_CODE (inner) != NAMESPACE_DECL && !CLASS_TYPE_P (inner)))
4209 return;
4211 prev = CP_DECL_CONTEXT (TREE_CODE (inner) == NAMESPACE_DECL ? inner : TYPE_NAME (inner));
4212 if (outer != prev)
4213 push_inner_scope_r (outer, prev);
4214 if (TREE_CODE (inner) == NAMESPACE_DECL)
4216 cp_binding_level *save_template_parm = 0;
4217 /* Temporary take out template parameter scopes. They are saved
4218 in reversed order in save_template_parm. */
4219 while (current_binding_level->kind == sk_template_parms)
4221 cp_binding_level *b = current_binding_level;
4222 current_binding_level = b->level_chain;
4223 b->level_chain = save_template_parm;
4224 save_template_parm = b;
4227 resume_scope (NAMESPACE_LEVEL (inner));
4228 current_namespace = inner;
4230 /* Restore template parameter scopes. */
4231 while (save_template_parm)
4233 cp_binding_level *b = save_template_parm;
4234 save_template_parm = b->level_chain;
4235 b->level_chain = current_binding_level;
4236 current_binding_level = b;
4239 else
4240 pushclass (inner);
4243 /* Enter the scope INNER from current scope. INNER must be a scope
4244 nested inside current scope. This works with both name lookup and
4245 pushing name into scope. In case a template parameter scope is present,
4246 namespace is pushed under the template parameter scope according to
4247 name lookup rule in 14.6.1/6.
4249 Return the former current scope suitable for pop_inner_scope. */
4251 tree
4252 push_inner_scope (tree inner)
4254 tree outer = current_scope ();
4255 if (!outer)
4256 outer = current_namespace;
4258 push_inner_scope_r (outer, inner);
4259 return outer;
4262 /* Exit the current scope INNER back to scope OUTER. */
4264 void
4265 pop_inner_scope (tree outer, tree inner)
4267 if (outer == inner
4268 || (TREE_CODE (inner) != NAMESPACE_DECL && !CLASS_TYPE_P (inner)))
4269 return;
4271 while (outer != inner)
4273 if (TREE_CODE (inner) == NAMESPACE_DECL)
4275 cp_binding_level *save_template_parm = 0;
4276 /* Temporary take out template parameter scopes. They are saved
4277 in reversed order in save_template_parm. */
4278 while (current_binding_level->kind == sk_template_parms)
4280 cp_binding_level *b = current_binding_level;
4281 current_binding_level = b->level_chain;
4282 b->level_chain = save_template_parm;
4283 save_template_parm = b;
4286 pop_namespace ();
4288 /* Restore template parameter scopes. */
4289 while (save_template_parm)
4291 cp_binding_level *b = save_template_parm;
4292 save_template_parm = b->level_chain;
4293 b->level_chain = current_binding_level;
4294 current_binding_level = b;
4297 else
4298 popclass ();
4300 inner = CP_DECL_CONTEXT (TREE_CODE (inner) == NAMESPACE_DECL ? inner : TYPE_NAME (inner));
4304 /* Do a pushlevel for class declarations. */
4306 void
4307 pushlevel_class (void)
4309 class_binding_level = begin_scope (sk_class, current_class_type);
4312 /* ...and a poplevel for class declarations. */
4314 void
4315 poplevel_class (void)
4317 cp_binding_level *level = class_binding_level;
4318 cp_class_binding *cb;
4319 size_t i;
4320 tree shadowed;
4322 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4323 gcc_assert (level != 0);
4325 /* If we're leaving a toplevel class, cache its binding level. */
4326 if (current_class_depth == 1)
4327 previous_class_level = level;
4328 for (shadowed = level->type_shadowed;
4329 shadowed;
4330 shadowed = TREE_CHAIN (shadowed))
4331 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (shadowed), TREE_VALUE (shadowed));
4333 /* Remove the bindings for all of the class-level declarations. */
4334 if (level->class_shadowed)
4336 FOR_EACH_VEC_ELT (*level->class_shadowed, i, cb)
4338 IDENTIFIER_BINDING (cb->identifier) = cb->base->previous;
4339 cxx_binding_free (cb->base);
4341 ggc_free (level->class_shadowed);
4342 level->class_shadowed = NULL;
4345 /* Now, pop out of the binding level which we created up in the
4346 `pushlevel_class' routine. */
4347 gcc_assert (current_binding_level == level);
4348 leave_scope ();
4349 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4352 /* Set INHERITED_VALUE_BINDING_P on BINDING to true or false, as
4353 appropriate. DECL is the value to which a name has just been
4354 bound. CLASS_TYPE is the class in which the lookup occurred. */
4356 static void
4357 set_inherited_value_binding_p (cxx_binding *binding, tree decl,
4358 tree class_type)
4360 if (binding->value == decl && TREE_CODE (decl) != TREE_LIST)
4362 tree context;
4364 if (TREE_CODE (decl) == OVERLOAD)
4365 context = ovl_scope (decl);
4366 else
4368 gcc_assert (DECL_P (decl));
4369 context = context_for_name_lookup (decl);
4372 if (is_properly_derived_from (class_type, context))
4373 INHERITED_VALUE_BINDING_P (binding) = 1;
4374 else
4375 INHERITED_VALUE_BINDING_P (binding) = 0;
4377 else if (binding->value == decl)
4378 /* We only encounter a TREE_LIST when there is an ambiguity in the
4379 base classes. Such an ambiguity can be overridden by a
4380 definition in this class. */
4381 INHERITED_VALUE_BINDING_P (binding) = 1;
4382 else
4383 INHERITED_VALUE_BINDING_P (binding) = 0;
4386 /* Make the declaration of X appear in CLASS scope. */
4388 bool
4389 pushdecl_class_level (tree x)
4391 bool is_valid = true;
4392 bool subtime;
4394 /* Do nothing if we're adding to an outer lambda closure type,
4395 outer_binding will add it later if it's needed. */
4396 if (current_class_type != class_binding_level->this_entity)
4397 return true;
4399 subtime = timevar_cond_start (TV_NAME_LOOKUP);
4400 /* Get the name of X. */
4401 tree name = OVL_NAME (x);
4403 if (name)
4405 is_valid = push_class_level_binding (name, x);
4406 if (TREE_CODE (x) == TYPE_DECL)
4407 set_identifier_type_value (name, x);
4409 else if (ANON_AGGR_TYPE_P (TREE_TYPE (x)))
4411 /* If X is an anonymous aggregate, all of its members are
4412 treated as if they were members of the class containing the
4413 aggregate, for naming purposes. */
4414 location_t save_location = input_location;
4415 tree anon = TREE_TYPE (x);
4416 if (vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (anon))
4417 for (unsigned ix = member_vec->length (); ix--;)
4419 tree binding = (*member_vec)[ix];
4420 if (STAT_HACK_P (binding))
4422 if (!pushdecl_class_level (STAT_TYPE (binding)))
4423 is_valid = false;
4424 binding = STAT_DECL (binding);
4426 if (!pushdecl_class_level (binding))
4427 is_valid = false;
4429 else
4430 for (tree f = TYPE_FIELDS (anon); f; f = DECL_CHAIN (f))
4431 if (TREE_CODE (f) == FIELD_DECL)
4433 input_location = DECL_SOURCE_LOCATION (f);
4434 if (!pushdecl_class_level (f))
4435 is_valid = false;
4437 input_location = save_location;
4439 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4440 return is_valid;
4443 /* Return the BINDING (if any) for NAME in SCOPE, which is a class
4444 scope. If the value returned is non-NULL, and the PREVIOUS field
4445 is not set, callers must set the PREVIOUS field explicitly. */
4447 static cxx_binding *
4448 get_class_binding (tree name, cp_binding_level *scope)
4450 tree class_type;
4451 tree type_binding;
4452 tree value_binding;
4453 cxx_binding *binding;
4455 class_type = scope->this_entity;
4457 /* Get the type binding. */
4458 type_binding = lookup_member (class_type, name,
4459 /*protect=*/2, /*want_type=*/true,
4460 tf_warning_or_error);
4461 /* Get the value binding. */
4462 value_binding = lookup_member (class_type, name,
4463 /*protect=*/2, /*want_type=*/false,
4464 tf_warning_or_error);
4466 if (value_binding
4467 && (TREE_CODE (value_binding) == TYPE_DECL
4468 || DECL_CLASS_TEMPLATE_P (value_binding)
4469 || (TREE_CODE (value_binding) == TREE_LIST
4470 && TREE_TYPE (value_binding) == error_mark_node
4471 && (TREE_CODE (TREE_VALUE (value_binding))
4472 == TYPE_DECL))))
4473 /* We found a type binding, even when looking for a non-type
4474 binding. This means that we already processed this binding
4475 above. */
4477 else if (value_binding)
4479 if (TREE_CODE (value_binding) == TREE_LIST
4480 && TREE_TYPE (value_binding) == error_mark_node)
4481 /* NAME is ambiguous. */
4483 else if (BASELINK_P (value_binding))
4484 /* NAME is some overloaded functions. */
4485 value_binding = BASELINK_FUNCTIONS (value_binding);
4488 /* If we found either a type binding or a value binding, create a
4489 new binding object. */
4490 if (type_binding || value_binding)
4492 binding = new_class_binding (name,
4493 value_binding,
4494 type_binding,
4495 scope);
4496 /* This is a class-scope binding, not a block-scope binding. */
4497 LOCAL_BINDING_P (binding) = 0;
4498 set_inherited_value_binding_p (binding, value_binding, class_type);
4500 else
4501 binding = NULL;
4503 return binding;
4506 /* Make the declaration(s) of X appear in CLASS scope under the name
4507 NAME. Returns true if the binding is valid. */
4509 static bool
4510 push_class_level_binding_1 (tree name, tree x)
4512 cxx_binding *binding;
4513 tree decl = x;
4514 bool ok;
4516 /* The class_binding_level will be NULL if x is a template
4517 parameter name in a member template. */
4518 if (!class_binding_level)
4519 return true;
4521 if (name == error_mark_node)
4522 return false;
4524 /* Can happen for an erroneous declaration (c++/60384). */
4525 if (!identifier_p (name))
4527 gcc_assert (errorcount || sorrycount);
4528 return false;
4531 /* Check for invalid member names. But don't worry about a default
4532 argument-scope lambda being pushed after the class is complete. */
4533 gcc_assert (TYPE_BEING_DEFINED (current_class_type)
4534 || LAMBDA_TYPE_P (TREE_TYPE (decl)));
4535 /* Check that we're pushing into the right binding level. */
4536 gcc_assert (current_class_type == class_binding_level->this_entity);
4538 /* We could have been passed a tree list if this is an ambiguous
4539 declaration. If so, pull the declaration out because
4540 check_template_shadow will not handle a TREE_LIST. */
4541 if (TREE_CODE (decl) == TREE_LIST
4542 && TREE_TYPE (decl) == error_mark_node)
4543 decl = TREE_VALUE (decl);
4545 if (!check_template_shadow (decl))
4546 return false;
4548 /* [class.mem]
4550 If T is the name of a class, then each of the following shall
4551 have a name different from T:
4553 -- every static data member of class T;
4555 -- every member of class T that is itself a type;
4557 -- every enumerator of every member of class T that is an
4558 enumerated type;
4560 -- every member of every anonymous union that is a member of
4561 class T.
4563 (Non-static data members were also forbidden to have the same
4564 name as T until TC1.) */
4565 if ((VAR_P (x)
4566 || TREE_CODE (x) == CONST_DECL
4567 || (TREE_CODE (x) == TYPE_DECL
4568 && !DECL_SELF_REFERENCE_P (x))
4569 /* A data member of an anonymous union. */
4570 || (TREE_CODE (x) == FIELD_DECL
4571 && DECL_CONTEXT (x) != current_class_type))
4572 && DECL_NAME (x) == DECL_NAME (TYPE_NAME (current_class_type)))
4574 tree scope = context_for_name_lookup (x);
4575 if (TYPE_P (scope) && same_type_p (scope, current_class_type))
4577 error ("%qD has the same name as the class in which it is "
4578 "declared",
4580 return false;
4584 /* Get the current binding for NAME in this class, if any. */
4585 binding = IDENTIFIER_BINDING (name);
4586 if (!binding || binding->scope != class_binding_level)
4588 binding = get_class_binding (name, class_binding_level);
4589 /* If a new binding was created, put it at the front of the
4590 IDENTIFIER_BINDING list. */
4591 if (binding)
4593 binding->previous = IDENTIFIER_BINDING (name);
4594 IDENTIFIER_BINDING (name) = binding;
4598 /* If there is already a binding, then we may need to update the
4599 current value. */
4600 if (binding && binding->value)
4602 tree bval = binding->value;
4603 tree old_decl = NULL_TREE;
4604 tree target_decl = strip_using_decl (decl);
4605 tree target_bval = strip_using_decl (bval);
4607 if (INHERITED_VALUE_BINDING_P (binding))
4609 /* If the old binding was from a base class, and was for a
4610 tag name, slide it over to make room for the new binding.
4611 The old binding is still visible if explicitly qualified
4612 with a class-key. */
4613 if (TREE_CODE (target_bval) == TYPE_DECL
4614 && DECL_ARTIFICIAL (target_bval)
4615 && !(TREE_CODE (target_decl) == TYPE_DECL
4616 && DECL_ARTIFICIAL (target_decl)))
4618 old_decl = binding->type;
4619 binding->type = bval;
4620 binding->value = NULL_TREE;
4621 INHERITED_VALUE_BINDING_P (binding) = 0;
4623 else
4625 old_decl = bval;
4626 /* Any inherited type declaration is hidden by the type
4627 declaration in the derived class. */
4628 if (TREE_CODE (target_decl) == TYPE_DECL
4629 && DECL_ARTIFICIAL (target_decl))
4630 binding->type = NULL_TREE;
4633 else if (TREE_CODE (target_decl) == OVERLOAD
4634 && OVL_P (target_bval))
4635 old_decl = bval;
4636 else if (TREE_CODE (decl) == USING_DECL
4637 && TREE_CODE (bval) == USING_DECL
4638 && same_type_p (USING_DECL_SCOPE (decl),
4639 USING_DECL_SCOPE (bval)))
4640 /* This is a using redeclaration that will be diagnosed later
4641 in supplement_binding */
4643 else if (TREE_CODE (decl) == USING_DECL
4644 && TREE_CODE (bval) == USING_DECL
4645 && DECL_DEPENDENT_P (decl)
4646 && DECL_DEPENDENT_P (bval))
4647 return true;
4648 else if (TREE_CODE (decl) == USING_DECL
4649 && OVL_P (target_bval))
4650 old_decl = bval;
4651 else if (TREE_CODE (bval) == USING_DECL
4652 && OVL_P (target_decl))
4653 return true;
4655 if (old_decl && binding->scope == class_binding_level)
4657 binding->value = x;
4658 /* It is always safe to clear INHERITED_VALUE_BINDING_P
4659 here. This function is only used to register bindings
4660 from with the class definition itself. */
4661 INHERITED_VALUE_BINDING_P (binding) = 0;
4662 return true;
4666 /* Note that we declared this value so that we can issue an error if
4667 this is an invalid redeclaration of a name already used for some
4668 other purpose. */
4669 note_name_declared_in_class (name, decl);
4671 /* If we didn't replace an existing binding, put the binding on the
4672 stack of bindings for the identifier, and update the shadowed
4673 list. */
4674 if (binding && binding->scope == class_binding_level)
4675 /* Supplement the existing binding. */
4676 ok = supplement_binding (binding, decl);
4677 else
4679 /* Create a new binding. */
4680 push_binding (name, decl, class_binding_level);
4681 ok = true;
4684 return ok;
4687 /* Wrapper for push_class_level_binding_1. */
4689 bool
4690 push_class_level_binding (tree name, tree x)
4692 bool ret;
4693 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4694 ret = push_class_level_binding_1 (name, x);
4695 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4696 return ret;
4699 /* Process "using SCOPE::NAME" in a class scope. Return the
4700 USING_DECL created. */
4702 tree
4703 do_class_using_decl (tree scope, tree name)
4705 if (name == error_mark_node)
4706 return NULL_TREE;
4708 if (!scope || !TYPE_P (scope))
4710 error ("using-declaration for non-member at class scope");
4711 return NULL_TREE;
4714 /* Make sure the name is not invalid */
4715 if (TREE_CODE (name) == BIT_NOT_EXPR)
4717 error ("%<%T::%D%> names destructor", scope, name);
4718 return NULL_TREE;
4721 /* Using T::T declares inheriting ctors, even if T is a typedef. */
4722 if (MAYBE_CLASS_TYPE_P (scope)
4723 && (name == TYPE_IDENTIFIER (scope)
4724 || constructor_name_p (name, scope)))
4726 maybe_warn_cpp0x (CPP0X_INHERITING_CTORS);
4727 name = ctor_identifier;
4728 CLASSTYPE_NON_AGGREGATE (current_class_type) = true;
4731 /* Cannot introduce a constructor name. */
4732 if (constructor_name_p (name, current_class_type))
4734 error ("%<%T::%D%> names constructor in %qT",
4735 scope, name, current_class_type);
4736 return NULL_TREE;
4739 /* From [namespace.udecl]:
4741 A using-declaration used as a member-declaration shall refer to a
4742 member of a base class of the class being defined.
4744 In general, we cannot check this constraint in a template because
4745 we do not know the entire set of base classes of the current
4746 class type. Morover, if SCOPE is dependent, it might match a
4747 non-dependent base. */
4749 tree decl = NULL_TREE;
4750 if (!dependent_scope_p (scope))
4752 base_kind b_kind;
4753 tree binfo = lookup_base (current_class_type, scope, ba_any, &b_kind,
4754 tf_warning_or_error);
4755 if (b_kind < bk_proper_base)
4757 /* If there are dependent bases, scope might resolve at
4758 instantiation time, even if it isn't exactly one of the
4759 dependent bases. */
4760 if (b_kind == bk_same_type || !any_dependent_bases_p ())
4762 error_not_base_type (scope, current_class_type);
4763 return NULL_TREE;
4766 else if (name == ctor_identifier && !binfo_direct_p (binfo))
4768 error ("cannot inherit constructors from indirect base %qT", scope);
4769 return NULL_TREE;
4771 else if (!IDENTIFIER_CONV_OP_P (name)
4772 || !dependent_type_p (TREE_TYPE (name)))
4774 decl = lookup_member (binfo, name, 0, false, tf_warning_or_error);
4775 if (!decl)
4777 error ("no members matching %<%T::%D%> in %q#T", scope, name,
4778 scope);
4779 return NULL_TREE;
4782 /* The binfo from which the functions came does not matter. */
4783 if (BASELINK_P (decl))
4784 decl = BASELINK_FUNCTIONS (decl);
4788 tree value = build_lang_decl (USING_DECL, name, NULL_TREE);
4789 USING_DECL_DECLS (value) = decl;
4790 USING_DECL_SCOPE (value) = scope;
4791 DECL_DEPENDENT_P (value) = !decl;
4793 return value;
4797 /* Return the binding for NAME in NS. If NS is NULL, look in
4798 global_namespace. */
4800 tree
4801 get_namespace_binding (tree ns, tree name)
4803 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4804 if (!ns)
4805 ns = global_namespace;
4806 gcc_checking_assert (!DECL_NAMESPACE_ALIAS (ns));
4807 tree ret = find_namespace_value (ns, name);
4808 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4809 return ret;
4812 /* Push internal DECL into the global namespace. Does not do the
4813 full overload fn handling and does not add it to the list of things
4814 in the namespace. */
4816 void
4817 set_global_binding (tree decl)
4819 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4821 tree *slot = find_namespace_slot (global_namespace, DECL_NAME (decl), true);
4823 if (*slot)
4824 /* The user's placed something in the implementor's namespace. */
4825 diagnose_name_conflict (decl, MAYBE_STAT_DECL (*slot));
4827 /* Force the binding, so compiler internals continue to work. */
4828 *slot = decl;
4830 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4833 /* Set the context of a declaration to scope. Complain if we are not
4834 outside scope. */
4836 void
4837 set_decl_namespace (tree decl, tree scope, bool friendp)
4839 /* Get rid of namespace aliases. */
4840 scope = ORIGINAL_NAMESPACE (scope);
4842 /* It is ok for friends to be qualified in parallel space. */
4843 if (!friendp && !is_nested_namespace (current_namespace, scope))
4844 error ("declaration of %qD not in a namespace surrounding %qD",
4845 decl, scope);
4846 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
4848 /* See whether this has been declared in the namespace or inline
4849 children. */
4850 tree old = NULL_TREE;
4852 name_lookup lookup (DECL_NAME (decl), LOOKUP_HIDDEN);
4853 if (!lookup.search_qualified (scope, /*usings=*/false))
4854 /* No old declaration at all. */
4855 goto not_found;
4856 old = lookup.value;
4859 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
4860 if (TREE_CODE (old) == TREE_LIST)
4862 ambiguous:
4863 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
4864 error ("reference to %qD is ambiguous", decl);
4865 print_candidates (old);
4866 return;
4869 if (!DECL_DECLARES_FUNCTION_P (decl))
4871 /* Don't compare non-function decls with decls_match here, since
4872 it can't check for the correct constness at this
4873 point. pushdecl will find those errors later. */
4875 /* We might have found it in an inline namespace child of SCOPE. */
4876 if (TREE_CODE (decl) == TREE_CODE (old))
4877 DECL_CONTEXT (decl) = DECL_CONTEXT (old);
4879 found:
4880 /* Writing "N::i" to declare something directly in "N" is invalid. */
4881 if (CP_DECL_CONTEXT (decl) == current_namespace
4882 && at_namespace_scope_p ())
4883 error ("explicit qualification in declaration of %qD", decl);
4884 return;
4887 /* Since decl is a function, old should contain a function decl. */
4888 if (!OVL_P (old))
4889 goto not_found;
4891 /* We handle these in check_explicit_instantiation_namespace. */
4892 if (processing_explicit_instantiation)
4893 return;
4894 if (processing_template_decl || processing_specialization)
4895 /* We have not yet called push_template_decl to turn a
4896 FUNCTION_DECL into a TEMPLATE_DECL, so the declarations won't
4897 match. But, we'll check later, when we construct the
4898 template. */
4899 return;
4900 /* Instantiations or specializations of templates may be declared as
4901 friends in any namespace. */
4902 if (friendp && DECL_USE_TEMPLATE (decl))
4903 return;
4905 tree found;
4906 found = NULL_TREE;
4908 for (lkp_iterator iter (old); iter; ++iter)
4910 if (iter.using_p ())
4911 continue;
4913 tree ofn = *iter;
4915 /* Adjust DECL_CONTEXT first so decls_match will return true
4916 if DECL will match a declaration in an inline namespace. */
4917 DECL_CONTEXT (decl) = DECL_CONTEXT (ofn);
4918 if (decls_match (decl, ofn))
4920 if (found)
4922 /* We found more than one matching declaration. */
4923 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
4924 goto ambiguous;
4926 found = ofn;
4930 if (found)
4932 if (DECL_HIDDEN_FRIEND_P (found))
4934 pedwarn (DECL_SOURCE_LOCATION (decl), 0,
4935 "%qD has not been declared within %qD", decl, scope);
4936 inform (DECL_SOURCE_LOCATION (found),
4937 "only here as a %<friend%>");
4939 DECL_CONTEXT (decl) = DECL_CONTEXT (found);
4940 goto found;
4943 not_found:
4944 /* It didn't work, go back to the explicit scope. */
4945 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
4946 error ("%qD should have been declared inside %qD", decl, scope);
4949 /* Return the namespace where the current declaration is declared. */
4951 tree
4952 current_decl_namespace (void)
4954 tree result;
4955 /* If we have been pushed into a different namespace, use it. */
4956 if (!vec_safe_is_empty (decl_namespace_list))
4957 return decl_namespace_list->last ();
4959 if (current_class_type)
4960 result = decl_namespace_context (current_class_type);
4961 else if (current_function_decl)
4962 result = decl_namespace_context (current_function_decl);
4963 else
4964 result = current_namespace;
4965 return result;
4968 /* Process any ATTRIBUTES on a namespace definition. Returns true if
4969 attribute visibility is seen. */
4971 bool
4972 handle_namespace_attrs (tree ns, tree attributes)
4974 tree d;
4975 bool saw_vis = false;
4977 if (attributes == error_mark_node)
4978 return false;
4980 for (d = attributes; d; d = TREE_CHAIN (d))
4982 tree name = get_attribute_name (d);
4983 tree args = TREE_VALUE (d);
4985 if (is_attribute_p ("visibility", name))
4987 /* attribute visibility is a property of the syntactic block
4988 rather than the namespace as a whole, so we don't touch the
4989 NAMESPACE_DECL at all. */
4990 tree x = args ? TREE_VALUE (args) : NULL_TREE;
4991 if (x == NULL_TREE || TREE_CODE (x) != STRING_CST || TREE_CHAIN (args))
4993 warning (OPT_Wattributes,
4994 "%qD attribute requires a single NTBS argument",
4995 name);
4996 continue;
4999 if (!TREE_PUBLIC (ns))
5000 warning (OPT_Wattributes,
5001 "%qD attribute is meaningless since members of the "
5002 "anonymous namespace get local symbols", name);
5004 push_visibility (TREE_STRING_POINTER (x), 1);
5005 saw_vis = true;
5007 else if (is_attribute_p ("abi_tag", name))
5009 if (!DECL_NAME (ns))
5011 warning (OPT_Wattributes, "ignoring %qD attribute on anonymous "
5012 "namespace", name);
5013 continue;
5015 if (!DECL_NAMESPACE_INLINE_P (ns))
5017 warning (OPT_Wattributes, "ignoring %qD attribute on non-inline "
5018 "namespace", name);
5019 continue;
5021 if (!args)
5023 tree dn = DECL_NAME (ns);
5024 args = build_string (IDENTIFIER_LENGTH (dn) + 1,
5025 IDENTIFIER_POINTER (dn));
5026 TREE_TYPE (args) = char_array_type_node;
5027 args = fix_string_type (args);
5028 args = build_tree_list (NULL_TREE, args);
5030 if (check_abi_tag_args (args, name))
5031 DECL_ATTRIBUTES (ns) = tree_cons (name, args,
5032 DECL_ATTRIBUTES (ns));
5034 else
5036 warning (OPT_Wattributes, "%qD attribute directive ignored",
5037 name);
5038 continue;
5042 return saw_vis;
5045 /* Temporarily set the namespace for the current declaration. */
5047 void
5048 push_decl_namespace (tree decl)
5050 if (TREE_CODE (decl) != NAMESPACE_DECL)
5051 decl = decl_namespace_context (decl);
5052 vec_safe_push (decl_namespace_list, ORIGINAL_NAMESPACE (decl));
5055 /* [namespace.memdef]/2 */
5057 void
5058 pop_decl_namespace (void)
5060 decl_namespace_list->pop ();
5063 /* Process a namespace-alias declaration. */
5065 void
5066 do_namespace_alias (tree alias, tree name_space)
5068 if (name_space == error_mark_node)
5069 return;
5071 gcc_assert (TREE_CODE (name_space) == NAMESPACE_DECL);
5073 name_space = ORIGINAL_NAMESPACE (name_space);
5075 /* Build the alias. */
5076 alias = build_lang_decl (NAMESPACE_DECL, alias, void_type_node);
5077 DECL_NAMESPACE_ALIAS (alias) = name_space;
5078 DECL_EXTERNAL (alias) = 1;
5079 DECL_CONTEXT (alias) = FROB_CONTEXT (current_scope ());
5080 pushdecl (alias);
5082 /* Emit debug info for namespace alias. */
5083 if (!building_stmt_list_p ())
5084 (*debug_hooks->early_global_decl) (alias);
5087 /* Like pushdecl, only it places X in the current namespace,
5088 if appropriate. */
5090 tree
5091 pushdecl_namespace_level (tree x, bool is_friend)
5093 cp_binding_level *b = current_binding_level;
5094 tree t;
5096 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
5097 t = do_pushdecl_with_scope
5098 (x, NAMESPACE_LEVEL (current_namespace), is_friend);
5100 /* Now, the type_shadowed stack may screw us. Munge it so it does
5101 what we want. */
5102 if (TREE_CODE (t) == TYPE_DECL)
5104 tree name = DECL_NAME (t);
5105 tree newval;
5106 tree *ptr = (tree *)0;
5107 for (; !global_scope_p (b); b = b->level_chain)
5109 tree shadowed = b->type_shadowed;
5110 for (; shadowed; shadowed = TREE_CHAIN (shadowed))
5111 if (TREE_PURPOSE (shadowed) == name)
5113 ptr = &TREE_VALUE (shadowed);
5114 /* Can't break out of the loop here because sometimes
5115 a binding level will have duplicate bindings for
5116 PT names. It's gross, but I haven't time to fix it. */
5119 newval = TREE_TYPE (t);
5120 if (ptr == (tree *)0)
5122 /* @@ This shouldn't be needed. My test case "zstring.cc" trips
5123 up here if this is changed to an assertion. --KR */
5124 SET_IDENTIFIER_TYPE_VALUE (name, t);
5126 else
5128 *ptr = newval;
5131 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
5132 return t;
5135 /* Process a using-declaration appearing in namespace scope. */
5137 void
5138 finish_namespace_using_decl (tree decl, tree scope, tree name)
5140 tree orig_decl = decl;
5142 gcc_checking_assert (current_binding_level->kind == sk_namespace
5143 && !processing_template_decl);
5144 decl = validate_nonmember_using_decl (decl, scope, name);
5145 if (decl == NULL_TREE)
5146 return;
5148 tree *slot = find_namespace_slot (current_namespace, name, true);
5149 tree val = slot ? MAYBE_STAT_DECL (*slot) : NULL_TREE;
5150 tree type = slot ? MAYBE_STAT_TYPE (*slot) : NULL_TREE;
5151 do_nonmember_using_decl (scope, name, &val, &type);
5152 if (STAT_HACK_P (*slot))
5154 STAT_DECL (*slot) = val;
5155 STAT_TYPE (*slot) = type;
5157 else if (type)
5158 *slot = stat_hack (val, type);
5159 else
5160 *slot = val;
5162 /* Emit debug info. */
5163 cp_emit_debug_info_for_using (orig_decl, current_namespace);
5166 /* Process a using-declaration at function scope. */
5168 void
5169 finish_local_using_decl (tree decl, tree scope, tree name)
5171 tree orig_decl = decl;
5173 gcc_checking_assert (current_binding_level->kind != sk_class
5174 && current_binding_level->kind != sk_namespace);
5175 decl = validate_nonmember_using_decl (decl, scope, name);
5176 if (decl == NULL_TREE)
5177 return;
5179 add_decl_expr (decl);
5181 cxx_binding *binding = find_local_binding (current_binding_level, name);
5182 tree value = binding ? binding->value : NULL_TREE;
5183 tree type = binding ? binding->type : NULL_TREE;
5185 do_nonmember_using_decl (scope, name, &value, &type);
5187 if (!value)
5189 else if (binding && value == binding->value)
5191 else if (binding && binding->value && TREE_CODE (value) == OVERLOAD)
5193 update_local_overload (IDENTIFIER_BINDING (name), value);
5194 IDENTIFIER_BINDING (name)->value = value;
5196 else
5197 /* Install the new binding. */
5198 push_local_binding (name, value, true);
5200 if (!type)
5202 else if (binding && type == binding->type)
5204 else
5206 push_local_binding (name, type, true);
5207 set_identifier_type_value (name, type);
5210 /* Emit debug info. */
5211 if (!processing_template_decl)
5212 cp_emit_debug_info_for_using (orig_decl, current_scope ());
5215 /* Return the declarations that are members of the namespace NS. */
5217 tree
5218 cp_namespace_decls (tree ns)
5220 return NAMESPACE_LEVEL (ns)->names;
5223 /* Combine prefer_type and namespaces_only into flags. */
5225 static int
5226 lookup_flags (int prefer_type, int namespaces_only)
5228 if (namespaces_only)
5229 return LOOKUP_PREFER_NAMESPACES;
5230 if (prefer_type > 1)
5231 return LOOKUP_PREFER_TYPES;
5232 if (prefer_type > 0)
5233 return LOOKUP_PREFER_BOTH;
5234 return 0;
5237 /* Given a lookup that returned VAL, use FLAGS to decide if we want to
5238 ignore it or not. Subroutine of lookup_name_real and
5239 lookup_type_scope. */
5241 static bool
5242 qualify_lookup (tree val, int flags)
5244 if (val == NULL_TREE)
5245 return false;
5246 if ((flags & LOOKUP_PREFER_NAMESPACES) && TREE_CODE (val) == NAMESPACE_DECL)
5247 return true;
5248 if (flags & LOOKUP_PREFER_TYPES)
5250 tree target_val = strip_using_decl (val);
5251 if (TREE_CODE (target_val) == TYPE_DECL
5252 || TREE_CODE (target_val) == TEMPLATE_DECL)
5253 return true;
5255 if (flags & (LOOKUP_PREFER_NAMESPACES | LOOKUP_PREFER_TYPES))
5256 return false;
5257 /* Look through lambda things that we shouldn't be able to see. */
5258 if (!(flags & LOOKUP_HIDDEN) && is_lambda_ignored_entity (val))
5259 return false;
5260 return true;
5263 /* Is there a "using namespace std;" directive within USINGS? */
5265 static bool
5266 using_directives_contain_std_p (vec<tree, va_gc> *usings)
5268 if (!usings)
5269 return false;
5271 for (unsigned ix = usings->length (); ix--;)
5272 if ((*usings)[ix] == std_node)
5273 return true;
5275 return false;
5278 /* Is there a "using namespace std;" directive within the current
5279 namespace (or its ancestors)?
5280 Compare with name_lookup::search_unqualified. */
5282 static bool
5283 has_using_namespace_std_directive_p ()
5285 /* Look at local using-directives. */
5286 for (cp_binding_level *level = current_binding_level;
5287 level->kind != sk_namespace;
5288 level = level->level_chain)
5289 if (using_directives_contain_std_p (level->using_directives))
5290 return true;
5292 /* Look at this namespace and its ancestors. */
5293 for (tree scope = current_namespace; scope; scope = CP_DECL_CONTEXT (scope))
5295 if (using_directives_contain_std_p (DECL_NAMESPACE_USING (scope)))
5296 return true;
5298 if (scope == global_namespace)
5299 break;
5302 return false;
5305 /* Subclass of deferred_diagnostic, for issuing a note when
5306 --param cxx-max-namespaces-for-diagnostic-help is reached.
5308 The note should be issued after the error, but before any other
5309 deferred diagnostics. This is handled by decorating a wrapped
5310 deferred_diagnostic, and emitting a note before that wrapped note is
5311 deleted. */
5313 class namespace_limit_reached : public deferred_diagnostic
5315 public:
5316 namespace_limit_reached (location_t loc, unsigned limit, tree name,
5317 gnu::unique_ptr<deferred_diagnostic> wrapped)
5318 : deferred_diagnostic (loc),
5319 m_limit (limit), m_name (name),
5320 m_wrapped (move (wrapped))
5324 ~namespace_limit_reached ()
5326 /* Unconditionally warn that the search was truncated. */
5327 inform (get_location (),
5328 "maximum limit of %d namespaces searched for %qE",
5329 m_limit, m_name);
5330 /* m_wrapped will be implicitly deleted after this, emitting any followup
5331 diagnostic after the above note. */
5334 private:
5335 unsigned m_limit;
5336 tree m_name;
5337 gnu::unique_ptr<deferred_diagnostic> m_wrapped;
5340 /* Subclass of deferred_diagnostic, for use when issuing a single suggestion.
5341 Emit a note showing the location of the declaration of the suggestion. */
5343 class show_candidate_location : public deferred_diagnostic
5345 public:
5346 show_candidate_location (location_t loc, tree candidate)
5347 : deferred_diagnostic (loc),
5348 m_candidate (candidate)
5352 ~show_candidate_location ()
5354 inform (location_of (m_candidate), "%qE declared here", m_candidate);
5357 private:
5358 tree m_candidate;
5361 /* Subclass of deferred_diagnostic, for use when there are multiple candidates
5362 to be suggested by suggest_alternatives_for.
5364 Emit a series of notes showing the various suggestions. */
5366 class suggest_alternatives : public deferred_diagnostic
5368 public:
5369 suggest_alternatives (location_t loc, vec<tree> candidates)
5370 : deferred_diagnostic (loc),
5371 m_candidates (candidates)
5375 ~suggest_alternatives ()
5377 if (m_candidates.length ())
5379 inform_n (get_location (), m_candidates.length (),
5380 "suggested alternative:",
5381 "suggested alternatives:");
5382 for (unsigned ix = 0; ix != m_candidates.length (); ix++)
5384 tree val = m_candidates[ix];
5386 inform (location_of (val), " %qE", val);
5389 m_candidates.release ();
5392 private:
5393 vec<tree> m_candidates;
5396 /* A class for encapsulating the result of a search across
5397 multiple namespaces for an unrecognized name seen at a
5398 given source location. */
5400 class namespace_hints
5402 public:
5403 namespace_hints (location_t loc, tree name);
5405 name_hint convert_candidates_to_name_hint ();
5406 name_hint maybe_decorate_with_limit (name_hint);
5408 private:
5409 location_t m_loc;
5410 tree m_name;
5411 vec<tree> m_candidates;
5413 /* Value of "--param cxx-max-namespaces-for-diagnostic-help". */
5414 unsigned m_limit;
5416 /* Was the limit reached? */
5417 bool m_limited;
5420 /* Constructor for namespace_hints. Search namespaces, looking for a match
5421 for unrecognized NAME seen at LOC. */
5423 namespace_hints::namespace_hints (location_t loc, tree name)
5424 : m_loc(loc), m_name (name)
5426 auto_vec<tree> worklist;
5428 m_candidates = vNULL;
5429 m_limited = false;
5430 m_limit = PARAM_VALUE (CXX_MAX_NAMESPACES_FOR_DIAGNOSTIC_HELP);
5432 /* Breadth-first search of namespaces. Up to limit namespaces
5433 searched (limit zero == unlimited). */
5434 worklist.safe_push (global_namespace);
5435 for (unsigned ix = 0; ix != worklist.length (); ix++)
5437 tree ns = worklist[ix];
5438 name_lookup lookup (name);
5440 if (lookup.search_qualified (ns, false))
5441 m_candidates.safe_push (lookup.value);
5443 if (!m_limited)
5445 /* Look for child namespaces. We have to do this
5446 indirectly because they are chained in reverse order,
5447 which is confusing to the user. */
5448 auto_vec<tree> children;
5450 for (tree decl = NAMESPACE_LEVEL (ns)->names;
5451 decl; decl = TREE_CHAIN (decl))
5452 if (TREE_CODE (decl) == NAMESPACE_DECL
5453 && !DECL_NAMESPACE_ALIAS (decl)
5454 && !DECL_NAMESPACE_INLINE_P (decl))
5455 children.safe_push (decl);
5457 while (!m_limited && !children.is_empty ())
5459 if (worklist.length () == m_limit)
5460 m_limited = true;
5461 else
5462 worklist.safe_push (children.pop ());
5468 /* Drop ownership of m_candidates, using it to generate a name_hint at m_loc
5469 for m_name, an IDENTIFIER_NODE for which name lookup failed.
5471 If m_candidates is non-empty, use it to generate a suggestion and/or
5472 a deferred diagnostic that lists the possible candidate(s).
5475 name_hint
5476 namespace_hints::convert_candidates_to_name_hint ()
5478 /* How many candidates do we have? */
5480 /* If we have just one candidate, issue a name_hint with it as a suggestion
5481 (so that consumers are able to suggest it within the error message and emit
5482 it as a fix-it hint), and with a note showing the candidate's location. */
5483 if (m_candidates.length () == 1)
5485 tree candidate = m_candidates[0];
5486 /* Clean up CANDIDATES. */
5487 m_candidates.release ();
5488 return name_hint (expr_to_string (candidate),
5489 new show_candidate_location (m_loc, candidate));
5491 else if (m_candidates.length () > 1)
5492 /* If we have more than one candidate, issue a name_hint without a single
5493 "suggestion", but with a deferred diagnostic that lists the
5494 various candidates. This takes ownership of m_candidates. */
5495 return name_hint (NULL, new suggest_alternatives (m_loc, m_candidates));
5497 /* Otherwise, m_candidates ought to be empty, so no cleanup is necessary. */
5498 gcc_assert (m_candidates.length () == 0);
5499 gcc_assert (m_candidates == vNULL);
5501 return name_hint ();
5504 /* If --param cxx-max-namespaces-for-diagnostic-help was reached,
5505 then we want to emit a note about after the error, but before
5506 any other deferred diagnostics.
5508 Handle this by figuring out what hint is needed, then optionally
5509 decorating HINT with a namespace_limit_reached wrapper. */
5511 name_hint
5512 namespace_hints::maybe_decorate_with_limit (name_hint hint)
5514 if (m_limited)
5515 return name_hint (hint.suggestion (),
5516 new namespace_limit_reached (m_loc, m_limit,
5517 m_name,
5518 hint.take_deferred ()));
5519 else
5520 return hint;
5523 /* Generate a name_hint at LOCATION for NAME, an IDENTIFIER_NODE for which
5524 name lookup failed.
5526 Search through all available namespaces and generate a suggestion and/or
5527 a deferred diagnostic that lists possible candidate(s).
5529 If no exact matches are found, and SUGGEST_MISSPELLINGS is true, then also
5530 look for near-matches and suggest the best near-match, if there is one.
5532 If nothing is found, then an empty name_hint is returned. */
5534 name_hint
5535 suggest_alternatives_for (location_t location, tree name,
5536 bool suggest_misspellings)
5538 /* First, search for exact matches in other namespaces. */
5539 namespace_hints ns_hints (location, name);
5540 name_hint result = ns_hints.convert_candidates_to_name_hint ();
5542 /* Otherwise, try other approaches. */
5543 if (!result)
5544 result = suggest_alternatives_for_1 (location, name, suggest_misspellings);
5546 return ns_hints.maybe_decorate_with_limit (gnu::move (result));
5549 /* The second half of suggest_alternatives_for, for when no exact matches
5550 were found in other namespaces. */
5552 static name_hint
5553 suggest_alternatives_for_1 (location_t location, tree name,
5554 bool suggest_misspellings)
5556 /* No candidates were found in the available namespaces. */
5558 /* If there's a "using namespace std;" active, and this
5559 is one of the most common "std::" names, then it's probably a
5560 missing #include. */
5561 if (has_using_namespace_std_directive_p ())
5563 name_hint hint = maybe_suggest_missing_std_header (location, name);
5564 if (hint)
5565 return hint;
5568 /* Otherwise, consider misspellings. */
5569 if (!suggest_misspellings)
5570 return name_hint ();
5572 return lookup_name_fuzzy (name, FUZZY_LOOKUP_NAME, location);
5575 /* Generate a name_hint at LOCATION for NAME, an IDENTIFIER_NODE for which
5576 name lookup failed.
5578 Search through all available namespaces and generate a suggestion and/or
5579 a deferred diagnostic that lists possible candidate(s).
5581 This is similiar to suggest_alternatives_for, but doesn't fallback to
5582 the other approaches used by that function. */
5584 name_hint
5585 suggest_alternatives_in_other_namespaces (location_t location, tree name)
5587 namespace_hints ns_hints (location, name);
5589 name_hint result = ns_hints.convert_candidates_to_name_hint ();
5591 return ns_hints.maybe_decorate_with_limit (gnu::move (result));
5594 /* A well-known name within the C++ standard library, returned by
5595 get_std_name_hint. */
5597 struct std_name_hint
5599 /* A name within "std::". */
5600 const char *name;
5602 /* The header name defining it within the C++ Standard Library
5603 (with '<' and '>'). */
5604 const char *header;
5606 /* The dialect of C++ in which this was added. */
5607 enum cxx_dialect min_dialect;
5610 /* Subroutine of maybe_suggest_missing_header for handling unrecognized names
5611 for some of the most common names within "std::".
5612 Given non-NULL NAME, return the std_name_hint for it, or NULL. */
5614 static const std_name_hint *
5615 get_std_name_hint (const char *name)
5617 static const std_name_hint hints[] = {
5618 /* <any>. */
5619 {"any", "<any>", cxx17},
5620 {"any_cast", "<any>", cxx17},
5621 {"make_any", "<any>", cxx17},
5622 /* <array>. */
5623 {"array", "<array>", cxx11},
5624 /* <atomic>. */
5625 {"atomic", "<atomic>", cxx11},
5626 {"atomic_flag", "<atomic>", cxx11},
5627 /* <bitset>. */
5628 {"bitset", "<bitset>", cxx11},
5629 /* <complex>. */
5630 {"complex", "<complex>", cxx98},
5631 {"complex_literals", "<complex>", cxx98},
5632 /* <condition_variable>. */
5633 {"condition_variable", "<condition_variable>", cxx11},
5634 {"condition_variable_any", "<condition_variable>", cxx11},
5635 /* <deque>. */
5636 {"deque", "<deque>", cxx98},
5637 /* <forward_list>. */
5638 {"forward_list", "<forward_list>", cxx11},
5639 /* <fstream>. */
5640 {"basic_filebuf", "<fstream>", cxx98},
5641 {"basic_ifstream", "<fstream>", cxx98},
5642 {"basic_ofstream", "<fstream>", cxx98},
5643 {"basic_fstream", "<fstream>", cxx98},
5644 {"fstream", "<fstream>", cxx98},
5645 {"ifstream", "<fstream>", cxx98},
5646 {"ofstream", "<fstream>", cxx98},
5647 /* <functional>. */
5648 {"bind", "<functional>", cxx11},
5649 {"function", "<functional>", cxx11},
5650 {"hash", "<functional>", cxx11},
5651 {"mem_fn", "<functional>", cxx11},
5652 /* <future>. */
5653 {"async", "<future>", cxx11},
5654 {"future", "<future>", cxx11},
5655 {"packaged_task", "<future>", cxx11},
5656 {"promise", "<future>", cxx11},
5657 /* <iostream>. */
5658 {"cin", "<iostream>", cxx98},
5659 {"cout", "<iostream>", cxx98},
5660 {"cerr", "<iostream>", cxx98},
5661 {"clog", "<iostream>", cxx98},
5662 {"wcin", "<iostream>", cxx98},
5663 {"wcout", "<iostream>", cxx98},
5664 {"wclog", "<iostream>", cxx98},
5665 /* <istream>. */
5666 {"istream", "<istream>", cxx98},
5667 /* <iterator>. */
5668 {"advance", "<iterator>", cxx98},
5669 {"back_inserter", "<iterator>", cxx98},
5670 {"begin", "<iterator>", cxx11},
5671 {"distance", "<iterator>", cxx98},
5672 {"end", "<iterator>", cxx11},
5673 {"front_inserter", "<iterator>", cxx98},
5674 {"inserter", "<iterator>", cxx98},
5675 {"istream_iterator", "<iterator>", cxx98},
5676 {"istreambuf_iterator", "<iterator>", cxx98},
5677 {"iterator_traits", "<iterator>", cxx98},
5678 {"move_iterator", "<iterator>", cxx11},
5679 {"next", "<iterator>", cxx11},
5680 {"ostream_iterator", "<iterator>", cxx98},
5681 {"ostreambuf_iterator", "<iterator>", cxx98},
5682 {"prev", "<iterator>", cxx11},
5683 {"reverse_iterator", "<iterator>", cxx98},
5684 /* <ostream>. */
5685 {"ostream", "<ostream>", cxx98},
5686 /* <list>. */
5687 {"list", "<list>", cxx98},
5688 /* <map>. */
5689 {"map", "<map>", cxx98},
5690 {"multimap", "<map>", cxx98},
5691 /* <memory>. */
5692 {"make_shared", "<memory>", cxx11},
5693 {"make_unique", "<memory>", cxx11},
5694 {"shared_ptr", "<memory>", cxx11},
5695 {"unique_ptr", "<memory>", cxx11},
5696 {"weak_ptr", "<memory>", cxx11},
5697 /* <mutex>. */
5698 {"mutex", "<mutex>", cxx11},
5699 {"timed_mutex", "<mutex>", cxx11},
5700 {"recursive_mutex", "<mutex>", cxx11},
5701 {"recursive_timed_mutex", "<mutex>", cxx11},
5702 {"once_flag", "<mutex>", cxx11},
5703 {"call_once,", "<mutex>", cxx11},
5704 {"lock", "<mutex>", cxx11},
5705 {"scoped_lock", "<mutex>", cxx17},
5706 {"try_lock", "<mutex>", cxx11},
5707 {"lock_guard", "<mutex>", cxx11},
5708 {"unique_lock", "<mutex>", cxx11},
5709 /* <optional>. */
5710 {"optional", "<optional>", cxx17},
5711 {"make_optional", "<optional>", cxx17},
5712 /* <ostream>. */
5713 {"ostream", "<ostream>", cxx98},
5714 {"wostream", "<ostream>", cxx98},
5715 {"ends", "<ostream>", cxx98},
5716 {"flush", "<ostream>", cxx98},
5717 {"endl", "<ostream>", cxx98},
5718 /* <queue>. */
5719 {"queue", "<queue>", cxx98},
5720 {"priority_queue", "<queue>", cxx98},
5721 /* <set>. */
5722 {"set", "<set>", cxx98},
5723 {"multiset", "<set>", cxx98},
5724 /* <shared_mutex>. */
5725 {"shared_lock", "<shared_mutex>", cxx14},
5726 {"shared_mutex", "<shared_mutex>", cxx17},
5727 {"shared_timed_mutex", "<shared_mutex>", cxx14},
5728 /* <sstream>. */
5729 {"basic_stringbuf", "<sstream>", cxx98},
5730 {"basic_istringstream", "<sstream>", cxx98},
5731 {"basic_ostringstream", "<sstream>", cxx98},
5732 {"basic_stringstream", "<sstream>", cxx98},
5733 {"istringstream", "<sstream>", cxx98},
5734 {"ostringstream", "<sstream>", cxx98},
5735 {"stringstream", "<sstream>", cxx98},
5736 /* <stack>. */
5737 {"stack", "<stack>", cxx98},
5738 /* <string>. */
5739 {"basic_string", "<string>", cxx98},
5740 {"string", "<string>", cxx98},
5741 {"wstring", "<string>", cxx98},
5742 {"u16string", "<string>", cxx11},
5743 {"u32string", "<string>", cxx11},
5744 /* <string_view>. */
5745 {"string_view", "<string_view>", cxx17},
5746 /* <thread>. */
5747 {"thread", "<thread>", cxx11},
5748 /* <tuple>. */
5749 {"make_tuple", "<tuple>", cxx11},
5750 {"tuple", "<tuple>", cxx11},
5751 {"tuple_element", "<tuple>", cxx11},
5752 {"tuple_size", "<tuple>", cxx11},
5753 /* <unordered_map>. */
5754 {"unordered_map", "<unordered_map>", cxx11},
5755 {"unordered_multimap", "<unordered_map>", cxx11},
5756 /* <unordered_set>. */
5757 {"unordered_set", "<unordered_set>", cxx11},
5758 {"unordered_multiset", "<unordered_set>", cxx11},
5759 /* <utility>. */
5760 {"declval", "<utility>", cxx11},
5761 {"forward", "<utility>", cxx11},
5762 {"make_pair", "<utility>", cxx98},
5763 {"move", "<utility>", cxx11},
5764 {"pair", "<utility>", cxx98},
5765 /* <variant>. */
5766 {"variant", "<variant>", cxx17},
5767 {"visit", "<variant>", cxx17},
5768 /* <vector>. */
5769 {"vector", "<vector>", cxx98},
5771 const size_t num_hints = sizeof (hints) / sizeof (hints[0]);
5772 for (size_t i = 0; i < num_hints; i++)
5774 if (strcmp (name, hints[i].name) == 0)
5775 return &hints[i];
5777 return NULL;
5780 /* Describe DIALECT. */
5782 static const char *
5783 get_cxx_dialect_name (enum cxx_dialect dialect)
5785 switch (dialect)
5787 default:
5788 gcc_unreachable ();
5789 case cxx98:
5790 return "C++98";
5791 case cxx11:
5792 return "C++11";
5793 case cxx14:
5794 return "C++14";
5795 case cxx17:
5796 return "C++17";
5797 case cxx2a:
5798 return "C++2a";
5802 /* Subclass of deferred_diagnostic for use for names in the "std" namespace
5803 that weren't recognized, but for which we know which header it ought to be
5806 Emit a note either suggesting the header to be included, or noting that
5807 the current dialect is too early for the given name. */
5809 class missing_std_header : public deferred_diagnostic
5811 public:
5812 missing_std_header (location_t loc,
5813 const char *name_str,
5814 const std_name_hint *header_hint)
5815 : deferred_diagnostic (loc),
5816 m_name_str (name_str),
5817 m_header_hint (header_hint)
5819 ~missing_std_header ()
5821 gcc_rich_location richloc (get_location ());
5822 if (cxx_dialect >= m_header_hint->min_dialect)
5824 const char *header = m_header_hint->header;
5825 maybe_add_include_fixit (&richloc, header, true);
5826 inform (&richloc,
5827 "%<std::%s%> is defined in header %qs;"
5828 " did you forget to %<#include %s%>?",
5829 m_name_str, header, header);
5831 else
5832 inform (&richloc,
5833 "%<std::%s%> is only available from %s onwards",
5834 m_name_str, get_cxx_dialect_name (m_header_hint->min_dialect));
5837 private:
5838 const char *m_name_str;
5839 const std_name_hint *m_header_hint;
5842 /* Attempt to generate a name_hint that suggests pertinent header files
5843 for NAME at LOCATION, for common names within the "std" namespace,
5844 or an empty name_hint if this isn't applicable. */
5846 static name_hint
5847 maybe_suggest_missing_std_header (location_t location, tree name)
5849 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
5851 const char *name_str = IDENTIFIER_POINTER (name);
5852 const std_name_hint *header_hint = get_std_name_hint (name_str);
5853 if (!header_hint)
5854 return name_hint ();
5856 return name_hint (NULL, new missing_std_header (location, name_str,
5857 header_hint));
5860 /* Attempt to generate a name_hint that suggests a missing header file
5861 for NAME within SCOPE at LOCATION, or an empty name_hint if this isn't
5862 applicable. */
5864 static name_hint
5865 maybe_suggest_missing_header (location_t location, tree name, tree scope)
5867 if (scope == NULL_TREE)
5868 return name_hint ();
5869 if (TREE_CODE (scope) != NAMESPACE_DECL)
5870 return name_hint ();
5871 /* We only offer suggestions for the "std" namespace. */
5872 if (scope != std_node)
5873 return name_hint ();
5874 return maybe_suggest_missing_std_header (location, name);
5877 /* Generate a name_hint at LOCATION for NAME, an IDENTIFIER_NODE for which name
5878 lookup failed within the explicitly provided SCOPE.
5880 Suggest the the best meaningful candidates (if any), otherwise
5881 an empty name_hint is returned. */
5883 name_hint
5884 suggest_alternative_in_explicit_scope (location_t location, tree name,
5885 tree scope)
5887 /* Something went very wrong; don't suggest anything. */
5888 if (name == error_mark_node)
5889 return name_hint ();
5891 /* Resolve any namespace aliases. */
5892 scope = ORIGINAL_NAMESPACE (scope);
5894 name_hint hint = maybe_suggest_missing_header (location, name, scope);
5895 if (hint)
5896 return hint;
5898 cp_binding_level *level = NAMESPACE_LEVEL (scope);
5900 best_match <tree, const char *> bm (name);
5901 consider_binding_level (name, bm, level, false, FUZZY_LOOKUP_NAME);
5903 /* See if we have a good suggesion for the user. */
5904 const char *fuzzy_name = bm.get_best_meaningful_candidate ();
5905 if (fuzzy_name)
5906 return name_hint (fuzzy_name, NULL);
5908 return name_hint ();
5911 /* Look up NAME (an IDENTIFIER_NODE) in SCOPE (either a NAMESPACE_DECL
5912 or a class TYPE).
5914 If PREFER_TYPE is > 0, we only return TYPE_DECLs or namespaces.
5915 If PREFER_TYPE is > 1, we only return TYPE_DECLs.
5917 Returns a DECL (or OVERLOAD, or BASELINK) representing the
5918 declaration found. If no suitable declaration can be found,
5919 ERROR_MARK_NODE is returned. If COMPLAIN is true and SCOPE is
5920 neither a class-type nor a namespace a diagnostic is issued. */
5922 tree
5923 lookup_qualified_name (tree scope, tree name, int prefer_type, bool complain,
5924 bool find_hidden)
5926 tree t = NULL_TREE;
5928 if (TREE_CODE (scope) == NAMESPACE_DECL)
5930 int flags = lookup_flags (prefer_type, /*namespaces_only*/false);
5931 if (find_hidden)
5932 flags |= LOOKUP_HIDDEN;
5933 name_lookup lookup (name, flags);
5935 if (qualified_namespace_lookup (scope, &lookup))
5936 t = lookup.value;
5938 else if (cxx_dialect != cxx98 && TREE_CODE (scope) == ENUMERAL_TYPE)
5939 t = lookup_enumerator (scope, name);
5940 else if (is_class_type (scope, complain))
5941 t = lookup_member (scope, name, 2, prefer_type, tf_warning_or_error);
5943 if (!t)
5944 return error_mark_node;
5945 return t;
5948 /* [namespace.qual]
5949 Accepts the NAME to lookup and its qualifying SCOPE.
5950 Returns the name/type pair found into the cxx_binding *RESULT,
5951 or false on error. */
5953 static bool
5954 qualified_namespace_lookup (tree scope, name_lookup *lookup)
5956 timevar_start (TV_NAME_LOOKUP);
5957 query_oracle (lookup->name);
5958 bool found = lookup->search_qualified (ORIGINAL_NAMESPACE (scope));
5959 timevar_stop (TV_NAME_LOOKUP);
5960 return found;
5963 /* Helper function for lookup_name_fuzzy.
5964 Traverse binding level LVL, looking for good name matches for NAME
5965 (and BM). */
5966 static void
5967 consider_binding_level (tree name, best_match <tree, const char *> &bm,
5968 cp_binding_level *lvl, bool look_within_fields,
5969 enum lookup_name_fuzzy_kind kind)
5971 if (look_within_fields)
5972 if (lvl->this_entity && TREE_CODE (lvl->this_entity) == RECORD_TYPE)
5974 tree type = lvl->this_entity;
5975 bool want_type_p = (kind == FUZZY_LOOKUP_TYPENAME);
5976 tree best_matching_field
5977 = lookup_member_fuzzy (type, name, want_type_p);
5978 if (best_matching_field)
5979 bm.consider (IDENTIFIER_POINTER (best_matching_field));
5982 /* Only suggest names reserved for the implementation if NAME begins
5983 with an underscore. */
5984 bool consider_implementation_names = (IDENTIFIER_POINTER (name)[0] == '_');
5986 for (tree t = lvl->names; t; t = TREE_CHAIN (t))
5988 tree d = t;
5990 /* OVERLOADs or decls from using declaration are wrapped into
5991 TREE_LIST. */
5992 if (TREE_CODE (d) == TREE_LIST)
5993 d = OVL_FIRST (TREE_VALUE (d));
5995 /* Don't use bindings from implicitly declared functions,
5996 as they were likely misspellings themselves. */
5997 if (TREE_TYPE (d) == error_mark_node)
5998 continue;
6000 /* Skip anticipated decls of builtin functions. */
6001 if (TREE_CODE (d) == FUNCTION_DECL
6002 && fndecl_built_in_p (d)
6003 && DECL_ANTICIPATED (d))
6004 continue;
6006 /* Skip compiler-generated variables (e.g. __for_begin/__for_end
6007 within range for). */
6008 if (TREE_CODE (d) == VAR_DECL
6009 && DECL_ARTIFICIAL (d))
6010 continue;
6012 tree suggestion = DECL_NAME (d);
6013 if (!suggestion)
6014 continue;
6016 /* Don't suggest names that are for anonymous aggregate types, as
6017 they are an implementation detail generated by the compiler. */
6018 if (anon_aggrname_p (suggestion))
6019 continue;
6021 const char *suggestion_str = IDENTIFIER_POINTER (suggestion);
6023 /* Ignore internal names with spaces in them. */
6024 if (strchr (suggestion_str, ' '))
6025 continue;
6027 /* Don't suggest names that are reserved for use by the
6028 implementation, unless NAME began with an underscore. */
6029 if (name_reserved_for_implementation_p (suggestion_str)
6030 && !consider_implementation_names)
6031 continue;
6033 bm.consider (suggestion_str);
6037 /* Subclass of deferred_diagnostic. Notify the user that the
6038 given macro was used before it was defined.
6039 This can be done in the C++ frontend since tokenization happens
6040 upfront. */
6042 class macro_use_before_def : public deferred_diagnostic
6044 public:
6045 /* Factory function. Return a new macro_use_before_def instance if
6046 appropriate, or return NULL. */
6047 static macro_use_before_def *
6048 maybe_make (location_t use_loc, cpp_hashnode *macro)
6050 source_location def_loc = cpp_macro_definition_location (macro);
6051 if (def_loc == UNKNOWN_LOCATION)
6052 return NULL;
6054 /* We only want to issue a note if the macro was used *before* it was
6055 defined.
6056 We don't want to issue a note for cases where a macro was incorrectly
6057 used, leaving it unexpanded (e.g. by using the wrong argument
6058 count). */
6059 if (!linemap_location_before_p (line_table, use_loc, def_loc))
6060 return NULL;
6062 return new macro_use_before_def (use_loc, macro);
6065 private:
6066 /* Ctor. LOC is the location of the usage. MACRO is the
6067 macro that was used. */
6068 macro_use_before_def (location_t loc, cpp_hashnode *macro)
6069 : deferred_diagnostic (loc), m_macro (macro)
6071 gcc_assert (macro);
6074 ~macro_use_before_def ()
6076 if (is_suppressed_p ())
6077 return;
6079 inform (get_location (), "the macro %qs had not yet been defined",
6080 (const char *)m_macro->ident.str);
6081 inform (cpp_macro_definition_location (m_macro),
6082 "it was later defined here");
6085 private:
6086 cpp_hashnode *m_macro;
6089 /* Determine if it can ever make sense to offer RID as a suggestion for
6090 a misspelling.
6092 Subroutine of lookup_name_fuzzy. */
6094 static bool
6095 suggest_rid_p (enum rid rid)
6097 switch (rid)
6099 /* Support suggesting function-like keywords. */
6100 case RID_STATIC_ASSERT:
6101 return true;
6103 default:
6104 /* Support suggesting the various decl-specifier words, to handle
6105 e.g. "singed" vs "signed" typos. */
6106 if (cp_keyword_starts_decl_specifier_p (rid))
6107 return true;
6109 /* Otherwise, don't offer it. This avoids suggesting e.g. "if"
6110 and "do" for short misspellings, which are likely to lead to
6111 nonsensical results. */
6112 return false;
6116 /* Search for near-matches for NAME within the current bindings, and within
6117 macro names, returning the best match as a const char *, or NULL if
6118 no reasonable match is found.
6120 Use LOC for any deferred diagnostics. */
6122 name_hint
6123 lookup_name_fuzzy (tree name, enum lookup_name_fuzzy_kind kind, location_t loc)
6125 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
6127 /* First, try some well-known names in the C++ standard library, in case
6128 the user forgot a #include. */
6129 const char *header_hint
6130 = get_cp_stdlib_header_for_name (IDENTIFIER_POINTER (name));
6131 if (header_hint)
6132 return name_hint (NULL,
6133 new suggest_missing_header (loc,
6134 IDENTIFIER_POINTER (name),
6135 header_hint));
6137 best_match <tree, const char *> bm (name);
6139 cp_binding_level *lvl;
6140 for (lvl = scope_chain->class_bindings; lvl; lvl = lvl->level_chain)
6141 consider_binding_level (name, bm, lvl, true, kind);
6143 for (lvl = current_binding_level; lvl; lvl = lvl->level_chain)
6144 consider_binding_level (name, bm, lvl, false, kind);
6146 /* Consider macros: if the user misspelled a macro name e.g. "SOME_MACRO"
6148 x = SOME_OTHER_MACRO (y);
6149 then "SOME_OTHER_MACRO" will survive to the frontend and show up
6150 as a misspelled identifier.
6152 Use the best distance so far so that a candidate is only set if
6153 a macro is better than anything so far. This allows early rejection
6154 (without calculating the edit distance) of macro names that must have
6155 distance >= bm.get_best_distance (), and means that we only get a
6156 non-NULL result for best_macro_match if it's better than any of
6157 the identifiers already checked. */
6158 best_macro_match bmm (name, bm.get_best_distance (), parse_in);
6159 cpp_hashnode *best_macro = bmm.get_best_meaningful_candidate ();
6160 /* If a macro is the closest so far to NAME, consider it. */
6161 if (best_macro)
6162 bm.consider ((const char *)best_macro->ident.str);
6163 else if (bmm.get_best_distance () == 0)
6165 /* If we have an exact match for a macro name, then either the
6166 macro was used with the wrong argument count, or the macro
6167 has been used before it was defined. */
6168 if (cpp_hashnode *macro = bmm.blithely_get_best_candidate ())
6169 if (cpp_user_macro_p (macro))
6170 return name_hint (NULL,
6171 macro_use_before_def::maybe_make (loc, macro));
6174 /* Try the "starts_decl_specifier_p" keywords to detect
6175 "singed" vs "signed" typos. */
6176 for (unsigned i = 0; i < num_c_common_reswords; i++)
6178 const c_common_resword *resword = &c_common_reswords[i];
6180 if (!suggest_rid_p (resword->rid))
6181 continue;
6183 tree resword_identifier = ridpointers [resword->rid];
6184 if (!resword_identifier)
6185 continue;
6186 gcc_assert (TREE_CODE (resword_identifier) == IDENTIFIER_NODE);
6188 /* Only consider reserved words that survived the
6189 filtering in init_reswords (e.g. for -std). */
6190 if (!IDENTIFIER_KEYWORD_P (resword_identifier))
6191 continue;
6193 bm.consider (IDENTIFIER_POINTER (resword_identifier));
6196 return name_hint (bm.get_best_meaningful_candidate (), NULL);
6199 /* Subroutine of outer_binding.
6201 Returns TRUE if BINDING is a binding to a template parameter of
6202 SCOPE. In that case SCOPE is the scope of a primary template
6203 parameter -- in the sense of G++, i.e, a template that has its own
6204 template header.
6206 Returns FALSE otherwise. */
6208 static bool
6209 binding_to_template_parms_of_scope_p (cxx_binding *binding,
6210 cp_binding_level *scope)
6212 tree binding_value, tmpl, tinfo;
6213 int level;
6215 if (!binding || !scope || !scope->this_entity)
6216 return false;
6218 binding_value = binding->value ? binding->value : binding->type;
6219 tinfo = get_template_info (scope->this_entity);
6221 /* BINDING_VALUE must be a template parm. */
6222 if (binding_value == NULL_TREE
6223 || (!DECL_P (binding_value)
6224 || !DECL_TEMPLATE_PARM_P (binding_value)))
6225 return false;
6227 /* The level of BINDING_VALUE. */
6228 level =
6229 template_type_parameter_p (binding_value)
6230 ? TEMPLATE_PARM_LEVEL (TEMPLATE_TYPE_PARM_INDEX
6231 (TREE_TYPE (binding_value)))
6232 : TEMPLATE_PARM_LEVEL (DECL_INITIAL (binding_value));
6234 /* The template of the current scope, iff said scope is a primary
6235 template. */
6236 tmpl = (tinfo
6237 && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
6238 ? TI_TEMPLATE (tinfo)
6239 : NULL_TREE);
6241 /* If the level of the parm BINDING_VALUE equals the depth of TMPL,
6242 then BINDING_VALUE is a parameter of TMPL. */
6243 return (tmpl && level == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)));
6246 /* Return the innermost non-namespace binding for NAME from a scope
6247 containing BINDING, or, if BINDING is NULL, the current scope.
6248 Please note that for a given template, the template parameters are
6249 considered to be in the scope containing the current scope.
6250 If CLASS_P is false, then class bindings are ignored. */
6252 cxx_binding *
6253 outer_binding (tree name,
6254 cxx_binding *binding,
6255 bool class_p)
6257 cxx_binding *outer;
6258 cp_binding_level *scope;
6259 cp_binding_level *outer_scope;
6261 if (binding)
6263 scope = binding->scope->level_chain;
6264 outer = binding->previous;
6266 else
6268 scope = current_binding_level;
6269 outer = IDENTIFIER_BINDING (name);
6271 outer_scope = outer ? outer->scope : NULL;
6273 /* Because we create class bindings lazily, we might be missing a
6274 class binding for NAME. If there are any class binding levels
6275 between the LAST_BINDING_LEVEL and the scope in which OUTER was
6276 declared, we must lookup NAME in those class scopes. */
6277 if (class_p)
6278 while (scope && scope != outer_scope && scope->kind != sk_namespace)
6280 if (scope->kind == sk_class)
6282 cxx_binding *class_binding;
6284 class_binding = get_class_binding (name, scope);
6285 if (class_binding)
6287 /* Thread this new class-scope binding onto the
6288 IDENTIFIER_BINDING list so that future lookups
6289 find it quickly. */
6290 class_binding->previous = outer;
6291 if (binding)
6292 binding->previous = class_binding;
6293 else
6294 IDENTIFIER_BINDING (name) = class_binding;
6295 return class_binding;
6298 /* If we are in a member template, the template parms of the member
6299 template are considered to be inside the scope of the containing
6300 class, but within G++ the class bindings are all pushed between the
6301 template parms and the function body. So if the outer binding is
6302 a template parm for the current scope, return it now rather than
6303 look for a class binding. */
6304 if (outer_scope && outer_scope->kind == sk_template_parms
6305 && binding_to_template_parms_of_scope_p (outer, scope))
6306 return outer;
6308 scope = scope->level_chain;
6311 return outer;
6314 /* Return the innermost block-scope or class-scope value binding for
6315 NAME, or NULL_TREE if there is no such binding. */
6317 tree
6318 innermost_non_namespace_value (tree name)
6320 cxx_binding *binding;
6321 binding = outer_binding (name, /*binding=*/NULL, /*class_p=*/true);
6322 return binding ? binding->value : NULL_TREE;
6325 /* Look up NAME in the current binding level and its superiors in the
6326 namespace of variables, functions and typedefs. Return a ..._DECL
6327 node of some kind representing its definition if there is only one
6328 such declaration, or return a TREE_LIST with all the overloaded
6329 definitions if there are many, or return 0 if it is undefined.
6330 Hidden name, either friend declaration or built-in function, are
6331 not ignored.
6333 If PREFER_TYPE is > 0, we prefer TYPE_DECLs or namespaces.
6334 If PREFER_TYPE is > 1, we reject non-type decls (e.g. namespaces).
6335 Otherwise we prefer non-TYPE_DECLs.
6337 If NONCLASS is nonzero, bindings in class scopes are ignored. If
6338 BLOCK_P is false, bindings in block scopes are ignored. */
6340 static tree
6341 lookup_name_real_1 (tree name, int prefer_type, int nonclass, bool block_p,
6342 int namespaces_only, int flags)
6344 cxx_binding *iter;
6345 tree val = NULL_TREE;
6347 query_oracle (name);
6349 /* Conversion operators are handled specially because ordinary
6350 unqualified name lookup will not find template conversion
6351 operators. */
6352 if (IDENTIFIER_CONV_OP_P (name))
6354 cp_binding_level *level;
6356 for (level = current_binding_level;
6357 level && level->kind != sk_namespace;
6358 level = level->level_chain)
6360 tree class_type;
6361 tree operators;
6363 /* A conversion operator can only be declared in a class
6364 scope. */
6365 if (level->kind != sk_class)
6366 continue;
6368 /* Lookup the conversion operator in the class. */
6369 class_type = level->this_entity;
6370 operators = lookup_fnfields (class_type, name, /*protect=*/0);
6371 if (operators)
6372 return operators;
6375 return NULL_TREE;
6378 flags |= lookup_flags (prefer_type, namespaces_only);
6380 /* First, look in non-namespace scopes. */
6382 if (current_class_type == NULL_TREE)
6383 nonclass = 1;
6385 if (block_p || !nonclass)
6386 for (iter = outer_binding (name, NULL, !nonclass);
6387 iter;
6388 iter = outer_binding (name, iter, !nonclass))
6390 tree binding;
6392 /* Skip entities we don't want. */
6393 if (LOCAL_BINDING_P (iter) ? !block_p : nonclass)
6394 continue;
6396 /* If this is the kind of thing we're looking for, we're done. */
6397 if (qualify_lookup (iter->value, flags))
6398 binding = iter->value;
6399 else if ((flags & LOOKUP_PREFER_TYPES)
6400 && qualify_lookup (iter->type, flags))
6401 binding = iter->type;
6402 else
6403 binding = NULL_TREE;
6405 if (binding)
6407 if (TREE_CODE (binding) == TYPE_DECL && DECL_HIDDEN_P (binding))
6409 /* A non namespace-scope binding can only be hidden in the
6410 presence of a local class, due to friend declarations.
6412 In particular, consider:
6414 struct C;
6415 void f() {
6416 struct A {
6417 friend struct B;
6418 friend struct C;
6419 void g() {
6420 B* b; // error: B is hidden
6421 C* c; // OK, finds ::C
6424 B *b; // error: B is hidden
6425 C *c; // OK, finds ::C
6426 struct B {};
6427 B *bb; // OK
6430 The standard says that "B" is a local class in "f"
6431 (but not nested within "A") -- but that name lookup
6432 for "B" does not find this declaration until it is
6433 declared directly with "f".
6435 In particular:
6437 [class.friend]
6439 If a friend declaration appears in a local class and
6440 the name specified is an unqualified name, a prior
6441 declaration is looked up without considering scopes
6442 that are outside the innermost enclosing non-class
6443 scope. For a friend function declaration, if there is
6444 no prior declaration, the program is ill-formed. For a
6445 friend class declaration, if there is no prior
6446 declaration, the class that is specified belongs to the
6447 innermost enclosing non-class scope, but if it is
6448 subsequently referenced, its name is not found by name
6449 lookup until a matching declaration is provided in the
6450 innermost enclosing nonclass scope.
6452 So just keep looking for a non-hidden binding.
6454 gcc_assert (TREE_CODE (binding) == TYPE_DECL);
6455 continue;
6457 val = binding;
6458 break;
6462 /* Now lookup in namespace scopes. */
6463 if (!val)
6465 name_lookup lookup (name, flags);
6466 if (lookup.search_unqualified
6467 (current_decl_namespace (), current_binding_level))
6468 val = lookup.value;
6471 /* If we have a single function from a using decl, pull it out. */
6472 if (val && TREE_CODE (val) == OVERLOAD && !really_overloaded_fn (val))
6473 val = OVL_FUNCTION (val);
6475 return val;
6478 /* Wrapper for lookup_name_real_1. */
6480 tree
6481 lookup_name_real (tree name, int prefer_type, int nonclass, bool block_p,
6482 int namespaces_only, int flags)
6484 tree ret;
6485 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6486 ret = lookup_name_real_1 (name, prefer_type, nonclass, block_p,
6487 namespaces_only, flags);
6488 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6489 return ret;
6492 tree
6493 lookup_name_nonclass (tree name)
6495 return lookup_name_real (name, 0, 1, /*block_p=*/true, 0, 0);
6498 tree
6499 lookup_name (tree name)
6501 return lookup_name_real (name, 0, 0, /*block_p=*/true, 0, 0);
6504 tree
6505 lookup_name_prefer_type (tree name, int prefer_type)
6507 return lookup_name_real (name, prefer_type, 0, /*block_p=*/true, 0, 0);
6510 /* Look up NAME for type used in elaborated name specifier in
6511 the scopes given by SCOPE. SCOPE can be either TS_CURRENT or
6512 TS_WITHIN_ENCLOSING_NON_CLASS. Although not implied by the
6513 name, more scopes are checked if cleanup or template parameter
6514 scope is encountered.
6516 Unlike lookup_name_real, we make sure that NAME is actually
6517 declared in the desired scope, not from inheritance, nor using
6518 directive. For using declaration, there is DR138 still waiting
6519 to be resolved. Hidden name coming from an earlier friend
6520 declaration is also returned.
6522 A TYPE_DECL best matching the NAME is returned. Catching error
6523 and issuing diagnostics are caller's responsibility. */
6525 static tree
6526 lookup_type_scope_1 (tree name, tag_scope scope)
6528 cxx_binding *iter = NULL;
6529 tree val = NULL_TREE;
6530 cp_binding_level *level = NULL;
6532 /* Look in non-namespace scope first. */
6533 if (current_binding_level->kind != sk_namespace)
6534 iter = outer_binding (name, NULL, /*class_p=*/ true);
6535 for (; iter; iter = outer_binding (name, iter, /*class_p=*/ true))
6537 /* Check if this is the kind of thing we're looking for.
6538 If SCOPE is TS_CURRENT, also make sure it doesn't come from
6539 base class. For ITER->VALUE, we can simply use
6540 INHERITED_VALUE_BINDING_P. For ITER->TYPE, we have to use
6541 our own check.
6543 We check ITER->TYPE before ITER->VALUE in order to handle
6544 typedef struct C {} C;
6545 correctly. */
6547 if (qualify_lookup (iter->type, LOOKUP_PREFER_TYPES)
6548 && (scope != ts_current
6549 || LOCAL_BINDING_P (iter)
6550 || DECL_CONTEXT (iter->type) == iter->scope->this_entity))
6551 val = iter->type;
6552 else if ((scope != ts_current
6553 || !INHERITED_VALUE_BINDING_P (iter))
6554 && qualify_lookup (iter->value, LOOKUP_PREFER_TYPES))
6555 val = iter->value;
6557 if (val)
6558 break;
6561 /* Look in namespace scope. */
6562 if (val)
6563 level = iter->scope;
6564 else
6566 tree ns = current_decl_namespace ();
6568 if (tree *slot = find_namespace_slot (ns, name))
6570 /* If this is the kind of thing we're looking for, we're done. */
6571 if (tree type = MAYBE_STAT_TYPE (*slot))
6572 if (qualify_lookup (type, LOOKUP_PREFER_TYPES))
6573 val = type;
6574 if (!val)
6576 if (tree decl = MAYBE_STAT_DECL (*slot))
6577 if (qualify_lookup (decl, LOOKUP_PREFER_TYPES))
6578 val = decl;
6580 level = NAMESPACE_LEVEL (ns);
6584 /* Type found, check if it is in the allowed scopes, ignoring cleanup
6585 and template parameter scopes. */
6586 if (val)
6588 cp_binding_level *b = current_binding_level;
6589 while (b)
6591 if (level == b)
6592 return val;
6594 if (b->kind == sk_cleanup || b->kind == sk_template_parms
6595 || b->kind == sk_function_parms)
6596 b = b->level_chain;
6597 else if (b->kind == sk_class
6598 && scope == ts_within_enclosing_non_class)
6599 b = b->level_chain;
6600 else
6601 break;
6605 return NULL_TREE;
6608 /* Wrapper for lookup_type_scope_1. */
6610 tree
6611 lookup_type_scope (tree name, tag_scope scope)
6613 tree ret;
6614 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6615 ret = lookup_type_scope_1 (name, scope);
6616 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6617 return ret;
6620 /* Returns true iff DECL is a block-scope extern declaration of a function
6621 or variable. */
6623 bool
6624 is_local_extern (tree decl)
6626 cxx_binding *binding;
6628 /* For functions, this is easy. */
6629 if (TREE_CODE (decl) == FUNCTION_DECL)
6630 return DECL_LOCAL_FUNCTION_P (decl);
6632 if (!VAR_P (decl))
6633 return false;
6634 if (!current_function_decl)
6635 return false;
6637 /* For variables, this is not easy. We need to look at the binding stack
6638 for the identifier to see whether the decl we have is a local. */
6639 for (binding = IDENTIFIER_BINDING (DECL_NAME (decl));
6640 binding && binding->scope->kind != sk_namespace;
6641 binding = binding->previous)
6642 if (binding->value == decl)
6643 return LOCAL_BINDING_P (binding);
6645 return false;
6648 /* The type TYPE is being declared. If it is a class template, or a
6649 specialization of a class template, do any processing required and
6650 perform error-checking. If IS_FRIEND is nonzero, this TYPE is
6651 being declared a friend. B is the binding level at which this TYPE
6652 should be bound.
6654 Returns the TYPE_DECL for TYPE, which may have been altered by this
6655 processing. */
6657 static tree
6658 maybe_process_template_type_declaration (tree type, int is_friend,
6659 cp_binding_level *b)
6661 tree decl = TYPE_NAME (type);
6663 if (processing_template_parmlist)
6664 /* You can't declare a new template type in a template parameter
6665 list. But, you can declare a non-template type:
6667 template <class A*> struct S;
6669 is a forward-declaration of `A'. */
6671 else if (b->kind == sk_namespace
6672 && current_binding_level->kind != sk_namespace)
6673 /* If this new type is being injected into a containing scope,
6674 then it's not a template type. */
6676 else
6678 gcc_assert (MAYBE_CLASS_TYPE_P (type)
6679 || TREE_CODE (type) == ENUMERAL_TYPE);
6681 if (processing_template_decl)
6683 /* This may change after the call to
6684 push_template_decl_real, but we want the original value. */
6685 tree name = DECL_NAME (decl);
6687 decl = push_template_decl_real (decl, is_friend);
6688 if (decl == error_mark_node)
6689 return error_mark_node;
6691 /* If the current binding level is the binding level for the
6692 template parameters (see the comment in
6693 begin_template_parm_list) and the enclosing level is a class
6694 scope, and we're not looking at a friend, push the
6695 declaration of the member class into the class scope. In the
6696 friend case, push_template_decl will already have put the
6697 friend into global scope, if appropriate. */
6698 if (TREE_CODE (type) != ENUMERAL_TYPE
6699 && !is_friend && b->kind == sk_template_parms
6700 && b->level_chain->kind == sk_class)
6702 finish_member_declaration (CLASSTYPE_TI_TEMPLATE (type));
6704 if (!COMPLETE_TYPE_P (current_class_type))
6706 maybe_add_class_template_decl_list (current_class_type,
6707 type, /*friend_p=*/0);
6708 /* Put this UTD in the table of UTDs for the class. */
6709 if (CLASSTYPE_NESTED_UTDS (current_class_type) == NULL)
6710 CLASSTYPE_NESTED_UTDS (current_class_type) =
6711 binding_table_new (SCOPE_DEFAULT_HT_SIZE);
6713 binding_table_insert
6714 (CLASSTYPE_NESTED_UTDS (current_class_type), name, type);
6720 return decl;
6723 /* Push a tag name NAME for struct/class/union/enum type TYPE. In case
6724 that the NAME is a class template, the tag is processed but not pushed.
6726 The pushed scope depend on the SCOPE parameter:
6727 - When SCOPE is TS_CURRENT, put it into the inner-most non-sk_cleanup
6728 scope.
6729 - When SCOPE is TS_GLOBAL, put it in the inner-most non-class and
6730 non-template-parameter scope. This case is needed for forward
6731 declarations.
6732 - When SCOPE is TS_WITHIN_ENCLOSING_NON_CLASS, this is similar to
6733 TS_GLOBAL case except that names within template-parameter scopes
6734 are not pushed at all.
6736 Returns TYPE upon success and ERROR_MARK_NODE otherwise. */
6738 static tree
6739 do_pushtag (tree name, tree type, tag_scope scope)
6741 tree decl;
6743 cp_binding_level *b = current_binding_level;
6744 while (true)
6746 if (/* Cleanup scopes are not scopes from the point of view of
6747 the language. */
6748 b->kind == sk_cleanup
6749 /* Neither are function parameter scopes. */
6750 || b->kind == sk_function_parms
6751 /* Neither are the scopes used to hold template parameters
6752 for an explicit specialization. For an ordinary template
6753 declaration, these scopes are not scopes from the point of
6754 view of the language. */
6755 || (b->kind == sk_template_parms
6756 && (b->explicit_spec_p || scope == ts_global)))
6757 b = b->level_chain;
6758 else if (b->kind == sk_class
6759 && scope != ts_current)
6761 b = b->level_chain;
6762 if (b->kind == sk_template_parms)
6763 b = b->level_chain;
6765 else
6766 break;
6769 gcc_assert (identifier_p (name));
6771 /* Do C++ gratuitous typedefing. */
6772 if (identifier_type_value_1 (name) != type)
6774 tree tdef;
6775 int in_class = 0;
6776 tree context = TYPE_CONTEXT (type);
6778 if (! context)
6780 cp_binding_level *cb = b;
6781 while (cb->kind != sk_namespace
6782 && cb->kind != sk_class
6783 && (cb->kind != sk_function_parms
6784 || !cb->this_entity))
6785 cb = cb->level_chain;
6786 tree cs = cb->this_entity;
6788 gcc_checking_assert (TREE_CODE (cs) == FUNCTION_DECL
6789 ? cs == current_function_decl
6790 : TYPE_P (cs) ? cs == current_class_type
6791 : cs == current_namespace);
6793 if (scope == ts_current
6794 || (cs && TREE_CODE (cs) == FUNCTION_DECL))
6795 context = cs;
6796 else if (cs && TYPE_P (cs))
6797 /* When declaring a friend class of a local class, we want
6798 to inject the newly named class into the scope
6799 containing the local class, not the namespace
6800 scope. */
6801 context = decl_function_context (get_type_decl (cs));
6803 if (!context)
6804 context = current_namespace;
6806 if (b->kind == sk_class
6807 || (b->kind == sk_template_parms
6808 && b->level_chain->kind == sk_class))
6809 in_class = 1;
6811 tdef = create_implicit_typedef (name, type);
6812 DECL_CONTEXT (tdef) = FROB_CONTEXT (context);
6813 if (scope == ts_within_enclosing_non_class)
6815 /* This is a friend. Make this TYPE_DECL node hidden from
6816 ordinary name lookup. Its corresponding TEMPLATE_DECL
6817 will be marked in push_template_decl_real. */
6818 retrofit_lang_decl (tdef);
6819 DECL_ANTICIPATED (tdef) = 1;
6820 DECL_FRIEND_P (tdef) = 1;
6823 decl = maybe_process_template_type_declaration
6824 (type, scope == ts_within_enclosing_non_class, b);
6825 if (decl == error_mark_node)
6826 return decl;
6828 if (b->kind == sk_class)
6830 if (!TYPE_BEING_DEFINED (current_class_type))
6831 /* Don't push anywhere if the class is complete; a lambda in an
6832 NSDMI is not a member of the class. */
6834 else if (!PROCESSING_REAL_TEMPLATE_DECL_P ())
6835 /* Put this TYPE_DECL on the TYPE_FIELDS list for the
6836 class. But if it's a member template class, we want
6837 the TEMPLATE_DECL, not the TYPE_DECL, so this is done
6838 later. */
6839 finish_member_declaration (decl);
6840 else
6841 pushdecl_class_level (decl);
6843 else if (b->kind != sk_template_parms)
6845 decl = do_pushdecl_with_scope (decl, b, /*is_friend=*/false);
6846 if (decl == error_mark_node)
6847 return decl;
6849 if (DECL_CONTEXT (decl) == std_node
6850 && init_list_identifier == DECL_NAME (TYPE_NAME (type))
6851 && !CLASSTYPE_TEMPLATE_INFO (type))
6853 error ("declaration of %<std::initializer_list%> does not match "
6854 "%<#include <initializer_list>%>, isn't a template");
6855 return error_mark_node;
6859 if (! in_class)
6860 set_identifier_type_value_with_scope (name, tdef, b);
6862 TYPE_CONTEXT (type) = DECL_CONTEXT (decl);
6864 /* If this is a local class, keep track of it. We need this
6865 information for name-mangling, and so that it is possible to
6866 find all function definitions in a translation unit in a
6867 convenient way. (It's otherwise tricky to find a member
6868 function definition it's only pointed to from within a local
6869 class.) */
6870 if (TYPE_FUNCTION_SCOPE_P (type))
6872 if (processing_template_decl)
6874 /* Push a DECL_EXPR so we call pushtag at the right time in
6875 template instantiation rather than in some nested context. */
6876 add_decl_expr (decl);
6878 /* Lambdas use LAMBDA_EXPR_DISCRIMINATOR instead. */
6879 else if (!LAMBDA_TYPE_P (type))
6880 vec_safe_push (local_classes, type);
6884 if (b->kind == sk_class
6885 && !COMPLETE_TYPE_P (current_class_type))
6887 maybe_add_class_template_decl_list (current_class_type,
6888 type, /*friend_p=*/0);
6890 if (CLASSTYPE_NESTED_UTDS (current_class_type) == NULL)
6891 CLASSTYPE_NESTED_UTDS (current_class_type)
6892 = binding_table_new (SCOPE_DEFAULT_HT_SIZE);
6894 binding_table_insert
6895 (CLASSTYPE_NESTED_UTDS (current_class_type), name, type);
6898 decl = TYPE_NAME (type);
6899 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
6901 /* Set type visibility now if this is a forward declaration. */
6902 TREE_PUBLIC (decl) = 1;
6903 determine_visibility (decl);
6905 return type;
6908 /* Wrapper for do_pushtag. */
6910 tree
6911 pushtag (tree name, tree type, tag_scope scope)
6913 tree ret;
6914 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6915 ret = do_pushtag (name, type, scope);
6916 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6917 return ret;
6921 /* Subroutines for reverting temporarily to top-level for instantiation
6922 of templates and such. We actually need to clear out the class- and
6923 local-value slots of all identifiers, so that only the global values
6924 are at all visible. Simply setting current_binding_level to the global
6925 scope isn't enough, because more binding levels may be pushed. */
6926 struct saved_scope *scope_chain;
6928 /* Return true if ID has not already been marked. */
6930 static inline bool
6931 store_binding_p (tree id)
6933 if (!id || !IDENTIFIER_BINDING (id))
6934 return false;
6936 if (IDENTIFIER_MARKED (id))
6937 return false;
6939 return true;
6942 /* Add an appropriate binding to *OLD_BINDINGS which needs to already
6943 have enough space reserved. */
6945 static void
6946 store_binding (tree id, vec<cxx_saved_binding, va_gc> **old_bindings)
6948 cxx_saved_binding saved;
6950 gcc_checking_assert (store_binding_p (id));
6952 IDENTIFIER_MARKED (id) = 1;
6954 saved.identifier = id;
6955 saved.binding = IDENTIFIER_BINDING (id);
6956 saved.real_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
6957 (*old_bindings)->quick_push (saved);
6958 IDENTIFIER_BINDING (id) = NULL;
6961 static void
6962 store_bindings (tree names, vec<cxx_saved_binding, va_gc> **old_bindings)
6964 static vec<tree> bindings_need_stored;
6965 tree t, id;
6966 size_t i;
6968 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6969 for (t = names; t; t = TREE_CHAIN (t))
6971 if (TREE_CODE (t) == TREE_LIST)
6972 id = TREE_PURPOSE (t);
6973 else
6974 id = DECL_NAME (t);
6976 if (store_binding_p (id))
6977 bindings_need_stored.safe_push (id);
6979 if (!bindings_need_stored.is_empty ())
6981 vec_safe_reserve_exact (*old_bindings, bindings_need_stored.length ());
6982 for (i = 0; bindings_need_stored.iterate (i, &id); ++i)
6984 /* We can apparently have duplicates in NAMES. */
6985 if (store_binding_p (id))
6986 store_binding (id, old_bindings);
6988 bindings_need_stored.truncate (0);
6990 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6993 /* Like store_bindings, but NAMES is a vector of cp_class_binding
6994 objects, rather than a TREE_LIST. */
6996 static void
6997 store_class_bindings (vec<cp_class_binding, va_gc> *names,
6998 vec<cxx_saved_binding, va_gc> **old_bindings)
7000 static vec<tree> bindings_need_stored;
7001 size_t i;
7002 cp_class_binding *cb;
7004 for (i = 0; vec_safe_iterate (names, i, &cb); ++i)
7005 if (store_binding_p (cb->identifier))
7006 bindings_need_stored.safe_push (cb->identifier);
7007 if (!bindings_need_stored.is_empty ())
7009 tree id;
7010 vec_safe_reserve_exact (*old_bindings, bindings_need_stored.length ());
7011 for (i = 0; bindings_need_stored.iterate (i, &id); ++i)
7012 store_binding (id, old_bindings);
7013 bindings_need_stored.truncate (0);
7017 /* A chain of saved_scope structures awaiting reuse. */
7019 static GTY((deletable)) struct saved_scope *free_saved_scope;
7021 static void
7022 do_push_to_top_level (void)
7024 struct saved_scope *s;
7025 cp_binding_level *b;
7026 cxx_saved_binding *sb;
7027 size_t i;
7028 bool need_pop;
7030 /* Reuse or create a new structure for this saved scope. */
7031 if (free_saved_scope != NULL)
7033 s = free_saved_scope;
7034 free_saved_scope = s->prev;
7036 vec<cxx_saved_binding, va_gc> *old_bindings = s->old_bindings;
7037 memset (s, 0, sizeof (*s));
7038 /* Also reuse the structure's old_bindings vector. */
7039 vec_safe_truncate (old_bindings, 0);
7040 s->old_bindings = old_bindings;
7042 else
7043 s = ggc_cleared_alloc<saved_scope> ();
7045 b = scope_chain ? current_binding_level : 0;
7047 /* If we're in the middle of some function, save our state. */
7048 if (cfun)
7050 need_pop = true;
7051 push_function_context ();
7053 else
7054 need_pop = false;
7056 if (scope_chain && previous_class_level)
7057 store_class_bindings (previous_class_level->class_shadowed,
7058 &s->old_bindings);
7060 /* Have to include the global scope, because class-scope decls
7061 aren't listed anywhere useful. */
7062 for (; b; b = b->level_chain)
7064 tree t;
7066 /* Template IDs are inserted into the global level. If they were
7067 inserted into namespace level, finish_file wouldn't find them
7068 when doing pending instantiations. Therefore, don't stop at
7069 namespace level, but continue until :: . */
7070 if (global_scope_p (b))
7071 break;
7073 store_bindings (b->names, &s->old_bindings);
7074 /* We also need to check class_shadowed to save class-level type
7075 bindings, since pushclass doesn't fill in b->names. */
7076 if (b->kind == sk_class)
7077 store_class_bindings (b->class_shadowed, &s->old_bindings);
7079 /* Unwind type-value slots back to top level. */
7080 for (t = b->type_shadowed; t; t = TREE_CHAIN (t))
7081 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (t), TREE_VALUE (t));
7084 FOR_EACH_VEC_SAFE_ELT (s->old_bindings, i, sb)
7085 IDENTIFIER_MARKED (sb->identifier) = 0;
7087 s->prev = scope_chain;
7088 s->bindings = b;
7089 s->need_pop_function_context = need_pop;
7090 s->function_decl = current_function_decl;
7091 s->unevaluated_operand = cp_unevaluated_operand;
7092 s->inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
7093 s->x_stmt_tree.stmts_are_full_exprs_p = true;
7095 scope_chain = s;
7096 current_function_decl = NULL_TREE;
7097 current_lang_base = NULL;
7098 current_lang_name = lang_name_cplusplus;
7099 current_namespace = global_namespace;
7100 push_class_stack ();
7101 cp_unevaluated_operand = 0;
7102 c_inhibit_evaluation_warnings = 0;
7105 static void
7106 do_pop_from_top_level (void)
7108 struct saved_scope *s = scope_chain;
7109 cxx_saved_binding *saved;
7110 size_t i;
7112 /* Clear out class-level bindings cache. */
7113 if (previous_class_level)
7114 invalidate_class_lookup_cache ();
7115 pop_class_stack ();
7117 release_tree_vector (current_lang_base);
7119 scope_chain = s->prev;
7120 FOR_EACH_VEC_SAFE_ELT (s->old_bindings, i, saved)
7122 tree id = saved->identifier;
7124 IDENTIFIER_BINDING (id) = saved->binding;
7125 SET_IDENTIFIER_TYPE_VALUE (id, saved->real_type_value);
7128 /* If we were in the middle of compiling a function, restore our
7129 state. */
7130 if (s->need_pop_function_context)
7131 pop_function_context ();
7132 current_function_decl = s->function_decl;
7133 cp_unevaluated_operand = s->unevaluated_operand;
7134 c_inhibit_evaluation_warnings = s->inhibit_evaluation_warnings;
7136 /* Make this saved_scope structure available for reuse by
7137 push_to_top_level. */
7138 s->prev = free_saved_scope;
7139 free_saved_scope = s;
7142 /* Push into the scope of the namespace NS, even if it is deeply
7143 nested within another namespace. */
7145 static void
7146 do_push_nested_namespace (tree ns)
7148 if (ns == global_namespace)
7149 do_push_to_top_level ();
7150 else
7152 do_push_nested_namespace (CP_DECL_CONTEXT (ns));
7153 gcc_checking_assert
7154 (find_namespace_value (current_namespace, DECL_NAME (ns)) == ns);
7155 resume_scope (NAMESPACE_LEVEL (ns));
7156 current_namespace = ns;
7160 /* Pop back from the scope of the namespace NS, which was previously
7161 entered with push_nested_namespace. */
7163 static void
7164 do_pop_nested_namespace (tree ns)
7166 while (ns != global_namespace)
7168 ns = CP_DECL_CONTEXT (ns);
7169 current_namespace = ns;
7170 leave_scope ();
7173 do_pop_from_top_level ();
7176 /* Add TARGET to USINGS, if it does not already exist there.
7177 We used to build the complete graph of usings at this point, from
7178 the POV of the source namespaces. Now we build that as we perform
7179 the unqualified search. */
7181 static void
7182 add_using_namespace (vec<tree, va_gc> *&usings, tree target)
7184 if (usings)
7185 for (unsigned ix = usings->length (); ix--;)
7186 if ((*usings)[ix] == target)
7187 return;
7189 vec_safe_push (usings, target);
7192 /* Tell the debug system of a using directive. */
7194 static void
7195 emit_debug_info_using_namespace (tree from, tree target, bool implicit)
7197 /* Emit debugging info. */
7198 tree context = from != global_namespace ? from : NULL_TREE;
7199 debug_hooks->imported_module_or_decl (target, NULL_TREE, context, false,
7200 implicit);
7203 /* Process a namespace-scope using directive. */
7205 void
7206 finish_namespace_using_directive (tree target, tree attribs)
7208 gcc_checking_assert (namespace_bindings_p ());
7209 if (target == error_mark_node)
7210 return;
7212 add_using_namespace (DECL_NAMESPACE_USING (current_namespace),
7213 ORIGINAL_NAMESPACE (target));
7214 emit_debug_info_using_namespace (current_namespace,
7215 ORIGINAL_NAMESPACE (target), false);
7217 if (attribs == error_mark_node)
7218 return;
7220 for (tree a = attribs; a; a = TREE_CHAIN (a))
7222 tree name = get_attribute_name (a);
7223 if (is_attribute_p ("strong", name))
7225 warning (0, "strong using directive no longer supported");
7226 if (CP_DECL_CONTEXT (target) == current_namespace)
7227 inform (DECL_SOURCE_LOCATION (target),
7228 "you may use an inline namespace instead");
7230 else
7231 warning (OPT_Wattributes, "%qD attribute directive ignored", name);
7235 /* Process a function-scope using-directive. */
7237 void
7238 finish_local_using_directive (tree target, tree attribs)
7240 gcc_checking_assert (local_bindings_p ());
7241 if (target == error_mark_node)
7242 return;
7244 if (attribs)
7245 warning (OPT_Wattributes, "attributes ignored on local using directive");
7247 add_stmt (build_stmt (input_location, USING_STMT, target));
7249 add_using_namespace (current_binding_level->using_directives,
7250 ORIGINAL_NAMESPACE (target));
7253 /* Pushes X into the global namespace. */
7255 tree
7256 pushdecl_top_level (tree x, bool is_friend)
7258 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
7259 do_push_to_top_level ();
7260 x = pushdecl_namespace_level (x, is_friend);
7261 do_pop_from_top_level ();
7262 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
7263 return x;
7266 /* Pushes X into the global namespace and calls cp_finish_decl to
7267 register the variable, initializing it with INIT. */
7269 tree
7270 pushdecl_top_level_and_finish (tree x, tree init)
7272 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
7273 do_push_to_top_level ();
7274 x = pushdecl_namespace_level (x, false);
7275 cp_finish_decl (x, init, false, NULL_TREE, 0);
7276 do_pop_from_top_level ();
7277 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
7278 return x;
7281 /* Enter the namespaces from current_namerspace to NS. */
7283 static int
7284 push_inline_namespaces (tree ns)
7286 int count = 0;
7287 if (ns != current_namespace)
7289 gcc_assert (ns != global_namespace);
7290 count += push_inline_namespaces (CP_DECL_CONTEXT (ns));
7291 resume_scope (NAMESPACE_LEVEL (ns));
7292 current_namespace = ns;
7293 count++;
7295 return count;
7298 /* Push into the scope of the NAME namespace. If NAME is NULL_TREE,
7299 then we enter an anonymous namespace. If MAKE_INLINE is true, then
7300 we create an inline namespace (it is up to the caller to check upon
7301 redefinition). Return the number of namespaces entered. */
7304 push_namespace (tree name, bool make_inline)
7306 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
7307 int count = 0;
7309 /* We should not get here if the global_namespace is not yet constructed
7310 nor if NAME designates the global namespace: The global scope is
7311 constructed elsewhere. */
7312 gcc_checking_assert (global_namespace != NULL && name != global_identifier);
7314 tree ns = NULL_TREE;
7316 name_lookup lookup (name, 0);
7317 if (!lookup.search_qualified (current_namespace, /*usings=*/false))
7319 else if (TREE_CODE (lookup.value) != NAMESPACE_DECL)
7321 else if (tree dna = DECL_NAMESPACE_ALIAS (lookup.value))
7323 /* A namespace alias is not allowed here, but if the alias
7324 is for a namespace also inside the current scope,
7325 accept it with a diagnostic. That's better than dying
7326 horribly. */
7327 if (is_nested_namespace (current_namespace, CP_DECL_CONTEXT (dna)))
7329 error ("namespace alias %qD not allowed here, "
7330 "assuming %qD", lookup.value, dna);
7331 ns = dna;
7334 else
7335 ns = lookup.value;
7338 bool new_ns = false;
7339 if (ns)
7340 /* DR2061. NS might be a member of an inline namespace. We
7341 need to push into those namespaces. */
7342 count += push_inline_namespaces (CP_DECL_CONTEXT (ns));
7343 else
7345 ns = build_lang_decl (NAMESPACE_DECL, name, void_type_node);
7346 SCOPE_DEPTH (ns) = SCOPE_DEPTH (current_namespace) + 1;
7347 if (!SCOPE_DEPTH (ns))
7348 /* We only allow depth 255. */
7349 sorry ("cannot nest more than %d namespaces",
7350 SCOPE_DEPTH (current_namespace));
7351 DECL_CONTEXT (ns) = FROB_CONTEXT (current_namespace);
7352 new_ns = true;
7354 if (pushdecl (ns) == error_mark_node)
7355 ns = NULL_TREE;
7356 else
7358 if (!name)
7360 SET_DECL_ASSEMBLER_NAME (ns, anon_identifier);
7362 if (!make_inline)
7363 add_using_namespace (DECL_NAMESPACE_USING (current_namespace),
7364 ns);
7366 else if (TREE_PUBLIC (current_namespace))
7367 TREE_PUBLIC (ns) = 1;
7369 if (make_inline)
7371 DECL_NAMESPACE_INLINE_P (ns) = true;
7372 vec_safe_push (DECL_NAMESPACE_INLINEES (current_namespace), ns);
7375 if (!name || make_inline)
7376 emit_debug_info_using_namespace (current_namespace, ns, true);
7380 if (ns)
7382 if (make_inline && !DECL_NAMESPACE_INLINE_P (ns))
7384 error ("inline namespace must be specified at initial definition");
7385 inform (DECL_SOURCE_LOCATION (ns), "%qD defined here", ns);
7387 if (new_ns)
7388 begin_scope (sk_namespace, ns);
7389 else
7390 resume_scope (NAMESPACE_LEVEL (ns));
7391 current_namespace = ns;
7392 count++;
7395 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
7396 return count;
7399 /* Pop from the scope of the current namespace. */
7401 void
7402 pop_namespace (void)
7404 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
7406 gcc_assert (current_namespace != global_namespace);
7407 current_namespace = CP_DECL_CONTEXT (current_namespace);
7408 /* The binding level is not popped, as it might be re-opened later. */
7409 leave_scope ();
7411 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
7414 /* External entry points for do_{push_to/pop_from}_top_level. */
7416 void
7417 push_to_top_level (void)
7419 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
7420 do_push_to_top_level ();
7421 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
7424 void
7425 pop_from_top_level (void)
7427 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
7428 do_pop_from_top_level ();
7429 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
7432 /* External entry points for do_{push,pop}_nested_namespace. */
7434 void
7435 push_nested_namespace (tree ns)
7437 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
7438 do_push_nested_namespace (ns);
7439 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
7442 void
7443 pop_nested_namespace (tree ns)
7445 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
7446 gcc_assert (current_namespace == ns);
7447 do_pop_nested_namespace (ns);
7448 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
7451 /* Pop off extraneous binding levels left over due to syntax errors.
7452 We don't pop past namespaces, as they might be valid. */
7454 void
7455 pop_everything (void)
7457 if (ENABLE_SCOPE_CHECKING)
7458 verbatim ("XXX entering pop_everything ()\n");
7459 while (!namespace_bindings_p ())
7461 if (current_binding_level->kind == sk_class)
7462 pop_nested_class ();
7463 else
7464 poplevel (0, 0, 0);
7466 if (ENABLE_SCOPE_CHECKING)
7467 verbatim ("XXX leaving pop_everything ()\n");
7470 /* Emit debugging information for using declarations and directives.
7471 If input tree is overloaded fn then emit debug info for all
7472 candidates. */
7474 void
7475 cp_emit_debug_info_for_using (tree t, tree context)
7477 /* Don't try to emit any debug information if we have errors. */
7478 if (seen_error ())
7479 return;
7481 /* Ignore this FUNCTION_DECL if it refers to a builtin declaration
7482 of a builtin function. */
7483 if (TREE_CODE (t) == FUNCTION_DECL
7484 && DECL_EXTERNAL (t)
7485 && fndecl_built_in_p (t))
7486 return;
7488 /* Do not supply context to imported_module_or_decl, if
7489 it is a global namespace. */
7490 if (context == global_namespace)
7491 context = NULL_TREE;
7493 t = MAYBE_BASELINK_FUNCTIONS (t);
7495 /* FIXME: Handle TEMPLATE_DECLs. */
7496 for (lkp_iterator iter (t); iter; ++iter)
7498 tree fn = *iter;
7499 if (TREE_CODE (fn) != TEMPLATE_DECL)
7501 if (building_stmt_list_p ())
7502 add_stmt (build_stmt (input_location, USING_STMT, fn));
7503 else
7504 debug_hooks->imported_module_or_decl (fn, NULL_TREE, context,
7505 false, false);
7510 #include "gt-cp-name-lookup.h"