Relocation (= move+destroy)
[official-gcc.git] / gcc / cp / name-lookup.c
blob08632c382b7b758432270edbd91fbfcda024940d
1 /* Definitions for C++ name lookup routines.
2 Copyright (C) 2003-2018 Free Software Foundation, Inc.
3 Contributed by Gabriel Dos Reis <gdr@integrable-solutions.net>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #define INCLUDE_UNIQUE_PTR
23 #include "system.h"
24 #include "coretypes.h"
25 #include "cp-tree.h"
26 #include "timevar.h"
27 #include "stringpool.h"
28 #include "print-tree.h"
29 #include "attribs.h"
30 #include "debug.h"
31 #include "c-family/c-pragma.h"
32 #include "params.h"
33 #include "gcc-rich-location.h"
34 #include "spellcheck-tree.h"
35 #include "parser.h"
36 #include "c-family/name-hint.h"
37 #include "c-family/known-headers.h"
38 #include "c-family/c-spellcheck.h"
40 static cxx_binding *cxx_binding_make (tree value, tree type);
41 static cp_binding_level *innermost_nonclass_level (void);
42 static void set_identifier_type_value_with_scope (tree id, tree decl,
43 cp_binding_level *b);
44 static bool maybe_suggest_missing_std_header (location_t location, tree name);
46 /* Create an overload suitable for recording an artificial TYPE_DECL
47 and another decl. We use this machanism to implement the struct
48 stat hack within a namespace. It'd be nice to use it everywhere. */
50 #define STAT_HACK_P(N) ((N) && TREE_CODE (N) == OVERLOAD && OVL_LOOKUP_P (N))
51 #define STAT_TYPE(N) TREE_TYPE (N)
52 #define STAT_DECL(N) OVL_FUNCTION (N)
53 #define MAYBE_STAT_DECL(N) (STAT_HACK_P (N) ? STAT_DECL (N) : N)
54 #define MAYBE_STAT_TYPE(N) (STAT_HACK_P (N) ? STAT_TYPE (N) : NULL_TREE)
56 /* Create a STAT_HACK node with DECL as the value binding and TYPE as
57 the type binding. */
59 static tree
60 stat_hack (tree decl = NULL_TREE, tree type = NULL_TREE)
62 tree result = make_node (OVERLOAD);
64 /* Mark this as a lookup, so we can tell this is a stat hack. */
65 OVL_LOOKUP_P (result) = true;
66 STAT_DECL (result) = decl;
67 STAT_TYPE (result) = type;
68 return result;
71 /* Create a local binding level for NAME. */
73 static cxx_binding *
74 create_local_binding (cp_binding_level *level, tree name)
76 cxx_binding *binding = cxx_binding_make (NULL, NULL);
78 INHERITED_VALUE_BINDING_P (binding) = false;
79 LOCAL_BINDING_P (binding) = true;
80 binding->scope = level;
81 binding->previous = IDENTIFIER_BINDING (name);
83 IDENTIFIER_BINDING (name) = binding;
85 return binding;
88 /* Find the binding for NAME in namespace NS. If CREATE_P is true,
89 make an empty binding if there wasn't one. */
91 static tree *
92 find_namespace_slot (tree ns, tree name, bool create_p = false)
94 tree *slot = DECL_NAMESPACE_BINDINGS (ns)
95 ->find_slot_with_hash (name, name ? IDENTIFIER_HASH_VALUE (name) : 0,
96 create_p ? INSERT : NO_INSERT);
97 return slot;
100 static tree
101 find_namespace_value (tree ns, tree name)
103 tree *b = find_namespace_slot (ns, name);
105 return b ? MAYBE_STAT_DECL (*b) : NULL_TREE;
108 /* Add DECL to the list of things declared in B. */
110 static void
111 add_decl_to_level (cp_binding_level *b, tree decl)
113 gcc_assert (b->kind != sk_class);
115 /* Make sure we don't create a circular list. xref_tag can end
116 up pushing the same artificial decl more than once. We
117 should have already detected that in update_binding. */
118 gcc_assert (b->names != decl);
120 /* We build up the list in reverse order, and reverse it later if
121 necessary. */
122 TREE_CHAIN (decl) = b->names;
123 b->names = decl;
125 /* If appropriate, add decl to separate list of statics. We
126 include extern variables because they might turn out to be
127 static later. It's OK for this list to contain a few false
128 positives. */
129 if (b->kind == sk_namespace
130 && ((VAR_P (decl)
131 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
132 || (TREE_CODE (decl) == FUNCTION_DECL
133 && (!TREE_PUBLIC (decl)
134 || decl_anon_ns_mem_p (decl)
135 || DECL_DECLARED_INLINE_P (decl)))))
136 vec_safe_push (static_decls, decl);
139 /* Find the binding for NAME in the local binding level B. */
141 static cxx_binding *
142 find_local_binding (cp_binding_level *b, tree name)
144 if (cxx_binding *binding = IDENTIFIER_BINDING (name))
145 for (;; b = b->level_chain)
147 if (binding->scope == b)
148 return binding;
150 /* Cleanup contours are transparent to the language. */
151 if (b->kind != sk_cleanup)
152 break;
154 return NULL;
157 struct name_lookup
159 public:
160 typedef std::pair<tree, tree> using_pair;
161 typedef vec<using_pair, va_heap, vl_embed> using_queue;
163 public:
164 tree name; /* The identifier being looked for. */
165 tree value; /* A (possibly ambiguous) set of things found. */
166 tree type; /* A type that has been found. */
167 int flags; /* Lookup flags. */
168 bool deduping; /* Full deduping is needed because using declarations
169 are in play. */
170 vec<tree, va_heap, vl_embed> *scopes;
171 name_lookup *previous; /* Previously active lookup. */
173 protected:
174 /* Marked scope stack for outermost name lookup. */
175 static vec<tree, va_heap, vl_embed> *shared_scopes;
176 /* Currently active lookup. */
177 static name_lookup *active;
179 public:
180 name_lookup (tree n, int f = 0)
181 : name (n), value (NULL_TREE), type (NULL_TREE), flags (f),
182 deduping (false), scopes (NULL), previous (NULL)
184 preserve_state ();
186 ~name_lookup ()
188 restore_state ();
191 private: /* Uncopyable, unmovable, unassignable. I am a rock. */
192 name_lookup (const name_lookup &);
193 name_lookup &operator= (const name_lookup &);
195 protected:
196 static bool seen_p (tree scope)
198 return LOOKUP_SEEN_P (scope);
200 static bool found_p (tree scope)
202 return LOOKUP_FOUND_P (scope);
205 void mark_seen (tree scope); /* Mark and add to scope vector. */
206 static void mark_found (tree scope)
208 gcc_checking_assert (seen_p (scope));
209 LOOKUP_FOUND_P (scope) = true;
211 bool see_and_mark (tree scope)
213 bool ret = seen_p (scope);
214 if (!ret)
215 mark_seen (scope);
216 return ret;
218 bool find_and_mark (tree scope);
220 private:
221 void preserve_state ();
222 void restore_state ();
224 private:
225 static tree ambiguous (tree thing, tree current);
226 void add_overload (tree fns);
227 void add_value (tree new_val);
228 void add_type (tree new_type);
229 bool process_binding (tree val_bind, tree type_bind);
231 /* Look in only namespace. */
232 bool search_namespace_only (tree scope);
233 /* Look in namespace and its (recursive) inlines. Ignore using
234 directives. Return true if something found (inc dups). */
235 bool search_namespace (tree scope);
236 /* Look in the using directives of namespace + inlines using
237 qualified lookup rules. */
238 bool search_usings (tree scope);
240 private:
241 using_queue *queue_namespace (using_queue *queue, int depth, tree scope);
242 using_queue *do_queue_usings (using_queue *queue, int depth,
243 vec<tree, va_gc> *usings);
244 using_queue *queue_usings (using_queue *queue, int depth,
245 vec<tree, va_gc> *usings)
247 if (usings)
248 queue = do_queue_usings (queue, depth, usings);
249 return queue;
252 private:
253 void add_fns (tree);
255 void adl_expr (tree);
256 void adl_type (tree);
257 void adl_template_arg (tree);
258 void adl_class (tree);
259 void adl_bases (tree);
260 void adl_class_only (tree);
261 void adl_namespace (tree);
262 void adl_namespace_only (tree);
264 public:
265 /* Search namespace + inlines + maybe usings as qualified lookup. */
266 bool search_qualified (tree scope, bool usings = true);
268 /* Search namespace + inlines + usings as unqualified lookup. */
269 bool search_unqualified (tree scope, cp_binding_level *);
271 /* ADL lookup of ARGS. */
272 tree search_adl (tree fns, vec<tree, va_gc> *args);
275 /* Scope stack shared by all outermost lookups. This avoids us
276 allocating and freeing on every single lookup. */
277 vec<tree, va_heap, vl_embed> *name_lookup::shared_scopes;
279 /* Currently active lookup. */
280 name_lookup *name_lookup::active;
282 /* Name lookup is recursive, becase ADL can cause template
283 instatiation. This is of course a rare event, so we optimize for
284 it not happening. When we discover an active name-lookup, which
285 must be an ADL lookup, we need to unmark the marked scopes and also
286 unmark the lookup we might have been accumulating. */
288 void
289 name_lookup::preserve_state ()
291 previous = active;
292 if (previous)
294 unsigned length = vec_safe_length (previous->scopes);
295 vec_safe_reserve (previous->scopes, length * 2);
296 for (unsigned ix = length; ix--;)
298 tree decl = (*previous->scopes)[ix];
300 gcc_checking_assert (LOOKUP_SEEN_P (decl));
301 LOOKUP_SEEN_P (decl) = false;
303 /* Preserve the FOUND_P state on the interrupted lookup's
304 stack. */
305 if (LOOKUP_FOUND_P (decl))
307 LOOKUP_FOUND_P (decl) = false;
308 previous->scopes->quick_push (decl);
312 /* Unmark the outer partial lookup. */
313 if (previous->deduping)
314 lookup_mark (previous->value, false);
316 else
317 scopes = shared_scopes;
318 active = this;
321 /* Restore the marking state of a lookup we interrupted. */
323 void
324 name_lookup::restore_state ()
326 if (deduping)
327 lookup_mark (value, false);
329 /* Unmark and empty this lookup's scope stack. */
330 for (unsigned ix = vec_safe_length (scopes); ix--;)
332 tree decl = scopes->pop ();
333 gcc_checking_assert (LOOKUP_SEEN_P (decl));
334 LOOKUP_SEEN_P (decl) = false;
335 LOOKUP_FOUND_P (decl) = false;
338 active = previous;
339 if (previous)
341 free (scopes);
343 unsigned length = vec_safe_length (previous->scopes);
344 for (unsigned ix = 0; ix != length; ix++)
346 tree decl = (*previous->scopes)[ix];
347 if (LOOKUP_SEEN_P (decl))
349 /* The remainder of the scope stack must be recording
350 FOUND_P decls, which we want to pop off. */
353 tree decl = previous->scopes->pop ();
354 gcc_checking_assert (LOOKUP_SEEN_P (decl)
355 && !LOOKUP_FOUND_P (decl));
356 LOOKUP_FOUND_P (decl) = true;
358 while (++ix != length);
359 break;
362 gcc_checking_assert (!LOOKUP_FOUND_P (decl));
363 LOOKUP_SEEN_P (decl) = true;
366 /* Remark the outer partial lookup. */
367 if (previous->deduping)
368 lookup_mark (previous->value, true);
370 else
371 shared_scopes = scopes;
374 void
375 name_lookup::mark_seen (tree scope)
377 gcc_checking_assert (!seen_p (scope));
378 LOOKUP_SEEN_P (scope) = true;
379 vec_safe_push (scopes, scope);
382 bool
383 name_lookup::find_and_mark (tree scope)
385 bool result = LOOKUP_FOUND_P (scope);
386 if (!result)
388 LOOKUP_FOUND_P (scope) = true;
389 if (!LOOKUP_SEEN_P (scope))
390 vec_safe_push (scopes, scope);
393 return result;
396 /* THING and CURRENT are ambiguous, concatenate them. */
398 tree
399 name_lookup::ambiguous (tree thing, tree current)
401 if (TREE_CODE (current) != TREE_LIST)
403 current = build_tree_list (NULL_TREE, current);
404 TREE_TYPE (current) = error_mark_node;
406 current = tree_cons (NULL_TREE, thing, current);
407 TREE_TYPE (current) = error_mark_node;
409 return current;
412 /* FNS is a new overload set to add to the exising set. */
414 void
415 name_lookup::add_overload (tree fns)
417 if (!deduping && TREE_CODE (fns) == OVERLOAD)
419 tree probe = fns;
420 if (flags & LOOKUP_HIDDEN)
421 probe = ovl_skip_hidden (probe);
422 if (probe && TREE_CODE (probe) == OVERLOAD && OVL_USING_P (probe))
424 /* We're about to add something found by a using
425 declaration, so need to engage deduping mode. */
426 lookup_mark (value, true);
427 deduping = true;
431 value = lookup_maybe_add (fns, value, deduping);
434 /* Add a NEW_VAL, a found value binding into the current value binding. */
436 void
437 name_lookup::add_value (tree new_val)
439 if (OVL_P (new_val) && (!value || OVL_P (value)))
440 add_overload (new_val);
441 else if (!value)
442 value = new_val;
443 else if (value == new_val)
445 else if ((TREE_CODE (value) == TYPE_DECL
446 && TREE_CODE (new_val) == TYPE_DECL
447 && same_type_p (TREE_TYPE (value), TREE_TYPE (new_val))))
448 /* Typedefs to the same type. */;
449 else if (TREE_CODE (value) == NAMESPACE_DECL
450 && TREE_CODE (new_val) == NAMESPACE_DECL
451 && ORIGINAL_NAMESPACE (value) == ORIGINAL_NAMESPACE (new_val))
452 /* Namespace (possibly aliased) to the same namespace. Locate
453 the namespace*/
454 value = ORIGINAL_NAMESPACE (value);
455 else
457 if (deduping)
459 /* Disengage deduping mode. */
460 lookup_mark (value, false);
461 deduping = false;
463 value = ambiguous (new_val, value);
467 /* Add a NEW_TYPE, a found type binding into the current type binding. */
469 void
470 name_lookup::add_type (tree new_type)
472 if (!type)
473 type = new_type;
474 else if (TREE_CODE (type) == TREE_LIST
475 || !same_type_p (TREE_TYPE (type), TREE_TYPE (new_type)))
476 type = ambiguous (new_type, type);
479 /* Process a found binding containing NEW_VAL and NEW_TYPE. Returns
480 true if we actually found something noteworthy. */
482 bool
483 name_lookup::process_binding (tree new_val, tree new_type)
485 /* Did we really see a type? */
486 if (new_type
487 && (LOOKUP_NAMESPACES_ONLY (flags)
488 || (!(flags & LOOKUP_HIDDEN)
489 && DECL_LANG_SPECIFIC (new_type)
490 && DECL_ANTICIPATED (new_type))))
491 new_type = NULL_TREE;
493 if (new_val && !(flags & LOOKUP_HIDDEN))
494 new_val = ovl_skip_hidden (new_val);
496 /* Do we really see a value? */
497 if (new_val)
498 switch (TREE_CODE (new_val))
500 case TEMPLATE_DECL:
501 /* If we expect types or namespaces, and not templates,
502 or this is not a template class. */
503 if ((LOOKUP_QUALIFIERS_ONLY (flags)
504 && !DECL_TYPE_TEMPLATE_P (new_val)))
505 new_val = NULL_TREE;
506 break;
507 case TYPE_DECL:
508 if (LOOKUP_NAMESPACES_ONLY (flags)
509 || (new_type && (flags & LOOKUP_PREFER_TYPES)))
510 new_val = NULL_TREE;
511 break;
512 case NAMESPACE_DECL:
513 if (LOOKUP_TYPES_ONLY (flags))
514 new_val = NULL_TREE;
515 break;
516 default:
517 if (LOOKUP_QUALIFIERS_ONLY (flags))
518 new_val = NULL_TREE;
521 if (!new_val)
523 new_val = new_type;
524 new_type = NULL_TREE;
527 /* Merge into the lookup */
528 if (new_val)
529 add_value (new_val);
530 if (new_type)
531 add_type (new_type);
533 return new_val != NULL_TREE;
536 /* Look in exactly namespace SCOPE. */
538 bool
539 name_lookup::search_namespace_only (tree scope)
541 bool found = false;
543 if (tree *binding = find_namespace_slot (scope, name))
544 found |= process_binding (MAYBE_STAT_DECL (*binding),
545 MAYBE_STAT_TYPE (*binding));
547 return found;
550 /* Conditionally look in namespace SCOPE and inline children. */
552 bool
553 name_lookup::search_namespace (tree scope)
555 if (see_and_mark (scope))
556 /* We've visited this scope before. Return what we found then. */
557 return found_p (scope);
559 /* Look in exactly namespace. */
560 bool found = search_namespace_only (scope);
562 /* Don't look into inline children, if we're looking for an
563 anonymous name -- it must be in the current scope, if anywhere. */
564 if (name)
565 /* Recursively look in its inline children. */
566 if (vec<tree, va_gc> *inlinees = DECL_NAMESPACE_INLINEES (scope))
567 for (unsigned ix = inlinees->length (); ix--;)
568 found |= search_namespace ((*inlinees)[ix]);
570 if (found)
571 mark_found (scope);
573 return found;
576 /* Recursively follow using directives of SCOPE & its inline children.
577 Such following is essentially a flood-fill algorithm. */
579 bool
580 name_lookup::search_usings (tree scope)
582 /* We do not check seen_p here, as that was already set during the
583 namespace_only walk. */
584 if (found_p (scope))
585 return true;
587 bool found = false;
588 if (vec<tree, va_gc> *usings = DECL_NAMESPACE_USING (scope))
589 for (unsigned ix = usings->length (); ix--;)
590 found |= search_qualified ((*usings)[ix], true);
592 /* Look in its inline children. */
593 if (vec<tree, va_gc> *inlinees = DECL_NAMESPACE_INLINEES (scope))
594 for (unsigned ix = inlinees->length (); ix--;)
595 found |= search_usings ((*inlinees)[ix]);
597 if (found)
598 mark_found (scope);
600 return found;
603 /* Qualified namespace lookup in SCOPE.
604 1) Look in SCOPE (+inlines). If found, we're done.
605 2) Otherwise, if USINGS is true,
606 recurse for every using directive of SCOPE (+inlines).
608 Trickiness is (a) loops and (b) multiple paths to same namespace.
609 In both cases we want to not repeat any lookups, and know whether
610 to stop the caller's step #2. Do this via the FOUND_P marker. */
612 bool
613 name_lookup::search_qualified (tree scope, bool usings)
615 bool found = false;
617 if (seen_p (scope))
618 found = found_p (scope);
619 else
621 found = search_namespace (scope);
622 if (!found && usings)
623 found = search_usings (scope);
626 return found;
629 /* Add SCOPE to the unqualified search queue, recursively add its
630 inlines and those via using directives. */
632 name_lookup::using_queue *
633 name_lookup::queue_namespace (using_queue *queue, int depth, tree scope)
635 if (see_and_mark (scope))
636 return queue;
638 /* Record it. */
639 tree common = scope;
640 while (SCOPE_DEPTH (common) > depth)
641 common = CP_DECL_CONTEXT (common);
642 vec_safe_push (queue, using_pair (common, scope));
644 /* Queue its inline children. */
645 if (vec<tree, va_gc> *inlinees = DECL_NAMESPACE_INLINEES (scope))
646 for (unsigned ix = inlinees->length (); ix--;)
647 queue = queue_namespace (queue, depth, (*inlinees)[ix]);
649 /* Queue its using targets. */
650 queue = queue_usings (queue, depth, DECL_NAMESPACE_USING (scope));
652 return queue;
655 /* Add the namespaces in USINGS to the unqualified search queue. */
657 name_lookup::using_queue *
658 name_lookup::do_queue_usings (using_queue *queue, int depth,
659 vec<tree, va_gc> *usings)
661 for (unsigned ix = usings->length (); ix--;)
662 queue = queue_namespace (queue, depth, (*usings)[ix]);
664 return queue;
667 /* Unqualified namespace lookup in SCOPE.
668 1) add scope+inlins to worklist.
669 2) recursively add target of every using directive
670 3) for each worklist item where SCOPE is common ancestor, search it
671 4) if nothing find, scope=parent, goto 1. */
673 bool
674 name_lookup::search_unqualified (tree scope, cp_binding_level *level)
676 /* Make static to avoid continual reallocation. We're not
677 recursive. */
678 static using_queue *queue = NULL;
679 bool found = false;
680 int length = vec_safe_length (queue);
682 /* Queue local using-directives. */
683 for (; level->kind != sk_namespace; level = level->level_chain)
684 queue = queue_usings (queue, SCOPE_DEPTH (scope), level->using_directives);
686 for (; !found; scope = CP_DECL_CONTEXT (scope))
688 gcc_assert (!DECL_NAMESPACE_ALIAS (scope));
689 int depth = SCOPE_DEPTH (scope);
691 /* Queue namespaces reachable from SCOPE. */
692 queue = queue_namespace (queue, depth, scope);
694 /* Search every queued namespace where SCOPE is the common
695 ancestor. Adjust the others. */
696 unsigned ix = length;
699 using_pair &pair = (*queue)[ix];
700 while (pair.first == scope)
702 found |= search_namespace_only (pair.second);
703 pair = queue->pop ();
704 if (ix == queue->length ())
705 goto done;
707 /* The depth is the same as SCOPE, find the parent scope. */
708 if (SCOPE_DEPTH (pair.first) == depth)
709 pair.first = CP_DECL_CONTEXT (pair.first);
710 ix++;
712 while (ix < queue->length ());
713 done:;
714 if (scope == global_namespace)
715 break;
717 /* If looking for hidden names, we only look in the innermost
718 namespace scope. [namespace.memdef]/3 If a friend
719 declaration in a non-local class first declares a class,
720 function, class template or function template the friend is a
721 member of the innermost enclosing namespace. See also
722 [basic.lookup.unqual]/7 */
723 if (flags & LOOKUP_HIDDEN)
724 break;
727 vec_safe_truncate (queue, length);
729 return found;
732 /* FNS is a value binding. If it is a (set of overloaded) functions,
733 add them into the current value. */
735 void
736 name_lookup::add_fns (tree fns)
738 if (!fns)
739 return;
740 else if (TREE_CODE (fns) == OVERLOAD)
742 if (TREE_TYPE (fns) != unknown_type_node)
743 fns = OVL_FUNCTION (fns);
745 else if (!DECL_DECLARES_FUNCTION_P (fns))
746 return;
748 add_overload (fns);
751 /* Add functions of a namespace to the lookup structure. */
753 void
754 name_lookup::adl_namespace_only (tree scope)
756 mark_seen (scope);
758 /* Look down into inline namespaces. */
759 if (vec<tree, va_gc> *inlinees = DECL_NAMESPACE_INLINEES (scope))
760 for (unsigned ix = inlinees->length (); ix--;)
761 adl_namespace_only ((*inlinees)[ix]);
763 if (tree fns = find_namespace_value (scope, name))
764 add_fns (ovl_skip_hidden (fns));
767 /* Find the containing non-inlined namespace, add it and all its
768 inlinees. */
770 void
771 name_lookup::adl_namespace (tree scope)
773 if (seen_p (scope))
774 return;
776 /* Find the containing non-inline namespace. */
777 while (DECL_NAMESPACE_INLINE_P (scope))
778 scope = CP_DECL_CONTEXT (scope);
780 adl_namespace_only (scope);
783 /* Adds the class and its friends to the lookup structure. */
785 void
786 name_lookup::adl_class_only (tree type)
788 /* Backend-built structures, such as __builtin_va_list, aren't
789 affected by all this. */
790 if (!CLASS_TYPE_P (type))
791 return;
793 type = TYPE_MAIN_VARIANT (type);
795 if (see_and_mark (type))
796 return;
798 tree context = decl_namespace_context (type);
799 adl_namespace (context);
801 complete_type (type);
803 /* Add friends. */
804 for (tree list = DECL_FRIENDLIST (TYPE_MAIN_DECL (type)); list;
805 list = TREE_CHAIN (list))
806 if (name == FRIEND_NAME (list))
807 for (tree friends = FRIEND_DECLS (list); friends;
808 friends = TREE_CHAIN (friends))
810 tree fn = TREE_VALUE (friends);
812 /* Only interested in global functions with potentially hidden
813 (i.e. unqualified) declarations. */
814 if (CP_DECL_CONTEXT (fn) != context)
815 continue;
817 /* Only interested in anticipated friends. (Non-anticipated
818 ones will have been inserted during the namespace
819 adl.) */
820 if (!DECL_ANTICIPATED (fn))
821 continue;
823 /* Template specializations are never found by name lookup.
824 (Templates themselves can be found, but not template
825 specializations.) */
826 if (TREE_CODE (fn) == FUNCTION_DECL && DECL_USE_TEMPLATE (fn))
827 continue;
829 add_fns (fn);
833 /* Adds the class and its bases to the lookup structure.
834 Returns true on error. */
836 void
837 name_lookup::adl_bases (tree type)
839 adl_class_only (type);
841 /* Process baseclasses. */
842 if (tree binfo = TYPE_BINFO (type))
844 tree base_binfo;
845 int i;
847 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
848 adl_bases (BINFO_TYPE (base_binfo));
852 /* Adds everything associated with a class argument type to the lookup
853 structure. Returns true on error.
855 If T is a class type (including unions), its associated classes are: the
856 class itself; the class of which it is a member, if any; and its direct
857 and indirect base classes. Its associated namespaces are the namespaces
858 of which its associated classes are members. Furthermore, if T is a
859 class template specialization, its associated namespaces and classes
860 also include: the namespaces and classes associated with the types of
861 the template arguments provided for template type parameters (excluding
862 template template parameters); the namespaces of which any template
863 template arguments are members; and the classes of which any member
864 templates used as template template arguments are members. [ Note:
865 non-type template arguments do not contribute to the set of associated
866 namespaces. --end note] */
868 void
869 name_lookup::adl_class (tree type)
871 /* Backend build structures, such as __builtin_va_list, aren't
872 affected by all this. */
873 if (!CLASS_TYPE_P (type))
874 return;
876 type = TYPE_MAIN_VARIANT (type);
877 /* We don't set found here because we have to have set seen first,
878 which is done in the adl_bases walk. */
879 if (found_p (type))
880 return;
882 adl_bases (type);
883 mark_found (type);
885 if (TYPE_CLASS_SCOPE_P (type))
886 adl_class_only (TYPE_CONTEXT (type));
888 /* Process template arguments. */
889 if (CLASSTYPE_TEMPLATE_INFO (type)
890 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
892 tree list = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
893 for (int i = 0; i < TREE_VEC_LENGTH (list); ++i)
894 adl_template_arg (TREE_VEC_ELT (list, i));
898 void
899 name_lookup::adl_expr (tree expr)
901 if (!expr)
902 return;
904 gcc_assert (!TYPE_P (expr));
906 if (TREE_TYPE (expr) != unknown_type_node)
908 adl_type (TREE_TYPE (expr));
909 return;
912 if (TREE_CODE (expr) == ADDR_EXPR)
913 expr = TREE_OPERAND (expr, 0);
914 if (TREE_CODE (expr) == COMPONENT_REF
915 || TREE_CODE (expr) == OFFSET_REF)
916 expr = TREE_OPERAND (expr, 1);
917 expr = MAYBE_BASELINK_FUNCTIONS (expr);
919 if (OVL_P (expr))
920 for (lkp_iterator iter (expr); iter; ++iter)
921 adl_type (TREE_TYPE (*iter));
922 else if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
924 /* The working paper doesn't currently say how to handle
925 template-id arguments. The sensible thing would seem to be
926 to handle the list of template candidates like a normal
927 overload set, and handle the template arguments like we do
928 for class template specializations. */
930 /* First the templates. */
931 adl_expr (TREE_OPERAND (expr, 0));
933 /* Now the arguments. */
934 if (tree args = TREE_OPERAND (expr, 1))
935 for (int ix = TREE_VEC_LENGTH (args); ix--;)
936 adl_template_arg (TREE_VEC_ELT (args, ix));
940 void
941 name_lookup::adl_type (tree type)
943 if (!type)
944 return;
946 if (TYPE_PTRDATAMEM_P (type))
948 /* Pointer to member: associate class type and value type. */
949 adl_type (TYPE_PTRMEM_CLASS_TYPE (type));
950 adl_type (TYPE_PTRMEM_POINTED_TO_TYPE (type));
951 return;
954 switch (TREE_CODE (type))
956 case RECORD_TYPE:
957 if (TYPE_PTRMEMFUNC_P (type))
959 adl_type (TYPE_PTRMEMFUNC_FN_TYPE (type));
960 return;
962 /* FALLTHRU */
963 case UNION_TYPE:
964 adl_class (type);
965 return;
967 case METHOD_TYPE:
968 /* The basetype is referenced in the first arg type, so just
969 fall through. */
970 case FUNCTION_TYPE:
971 /* Associate the parameter types. */
972 for (tree args = TYPE_ARG_TYPES (type); args; args = TREE_CHAIN (args))
973 adl_type (TREE_VALUE (args));
974 /* FALLTHROUGH */
976 case POINTER_TYPE:
977 case REFERENCE_TYPE:
978 case ARRAY_TYPE:
979 adl_type (TREE_TYPE (type));
980 return;
982 case ENUMERAL_TYPE:
983 if (TYPE_CLASS_SCOPE_P (type))
984 adl_class_only (TYPE_CONTEXT (type));
985 adl_namespace (decl_namespace_context (type));
986 return;
988 case LANG_TYPE:
989 gcc_assert (type == unknown_type_node
990 || type == init_list_type_node);
991 return;
993 case TYPE_PACK_EXPANSION:
994 adl_type (PACK_EXPANSION_PATTERN (type));
995 return;
997 default:
998 break;
1002 /* Adds everything associated with a template argument to the lookup
1003 structure. */
1005 void
1006 name_lookup::adl_template_arg (tree arg)
1008 /* [basic.lookup.koenig]
1010 If T is a template-id, its associated namespaces and classes are
1011 ... the namespaces and classes associated with the types of the
1012 template arguments provided for template type parameters
1013 (excluding template template parameters); the namespaces in which
1014 any template template arguments are defined; and the classes in
1015 which any member templates used as template template arguments
1016 are defined. [Note: non-type template arguments do not
1017 contribute to the set of associated namespaces. ] */
1019 /* Consider first template template arguments. */
1020 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
1021 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
1023 else if (TREE_CODE (arg) == TEMPLATE_DECL)
1025 tree ctx = CP_DECL_CONTEXT (arg);
1027 /* It's not a member template. */
1028 if (TREE_CODE (ctx) == NAMESPACE_DECL)
1029 adl_namespace (ctx);
1030 /* Otherwise, it must be member template. */
1031 else
1032 adl_class_only (ctx);
1034 /* It's an argument pack; handle it recursively. */
1035 else if (ARGUMENT_PACK_P (arg))
1037 tree args = ARGUMENT_PACK_ARGS (arg);
1038 int i, len = TREE_VEC_LENGTH (args);
1039 for (i = 0; i < len; ++i)
1040 adl_template_arg (TREE_VEC_ELT (args, i));
1042 /* It's not a template template argument, but it is a type template
1043 argument. */
1044 else if (TYPE_P (arg))
1045 adl_type (arg);
1048 /* Perform ADL lookup. FNS is the existing lookup result and ARGS are
1049 the call arguments. */
1051 tree
1052 name_lookup::search_adl (tree fns, vec<tree, va_gc> *args)
1054 if (fns)
1056 deduping = true;
1057 lookup_mark (fns, true);
1059 value = fns;
1061 unsigned ix;
1062 tree arg;
1064 FOR_EACH_VEC_ELT_REVERSE (*args, ix, arg)
1065 /* OMP reduction operators put an ADL-significant type as the
1066 first arg. */
1067 if (TYPE_P (arg))
1068 adl_type (arg);
1069 else
1070 adl_expr (arg);
1072 fns = value;
1074 return fns;
1077 static bool qualified_namespace_lookup (tree, name_lookup *);
1078 static void consider_binding_level (tree name,
1079 best_match <tree, const char *> &bm,
1080 cp_binding_level *lvl,
1081 bool look_within_fields,
1082 enum lookup_name_fuzzy_kind kind);
1083 static void diagnose_name_conflict (tree, tree);
1085 /* ADL lookup of NAME. FNS is the result of regular lookup, and we
1086 don't add duplicates to it. ARGS is the vector of call
1087 arguments (which will not be empty). */
1089 tree
1090 lookup_arg_dependent (tree name, tree fns, vec<tree, va_gc> *args)
1092 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
1093 name_lookup lookup (name);
1094 fns = lookup.search_adl (fns, args);
1095 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
1096 return fns;
1099 /* FNS is an overload set of conversion functions. Return the
1100 overloads converting to TYPE. */
1102 static tree
1103 extract_conversion_operator (tree fns, tree type)
1105 tree convs = NULL_TREE;
1106 tree tpls = NULL_TREE;
1108 for (ovl_iterator iter (fns); iter; ++iter)
1110 if (same_type_p (DECL_CONV_FN_TYPE (*iter), type))
1111 convs = lookup_add (*iter, convs);
1113 if (TREE_CODE (*iter) == TEMPLATE_DECL)
1114 tpls = lookup_add (*iter, tpls);
1117 if (!convs)
1118 convs = tpls;
1120 return convs;
1123 /* Binary search of (ordered) MEMBER_VEC for NAME. */
1125 static tree
1126 member_vec_binary_search (vec<tree, va_gc> *member_vec, tree name)
1128 for (unsigned lo = 0, hi = member_vec->length (); lo < hi;)
1130 unsigned mid = (lo + hi) / 2;
1131 tree binding = (*member_vec)[mid];
1132 tree binding_name = OVL_NAME (binding);
1134 if (binding_name > name)
1135 hi = mid;
1136 else if (binding_name < name)
1137 lo = mid + 1;
1138 else
1139 return binding;
1142 return NULL_TREE;
1145 /* Linear search of (unordered) MEMBER_VEC for NAME. */
1147 static tree
1148 member_vec_linear_search (vec<tree, va_gc> *member_vec, tree name)
1150 for (int ix = member_vec->length (); ix--;)
1151 if (tree binding = (*member_vec)[ix])
1152 if (OVL_NAME (binding) == name)
1153 return binding;
1155 return NULL_TREE;
1158 /* Linear search of (partially ordered) fields of KLASS for NAME. */
1160 static tree
1161 fields_linear_search (tree klass, tree name, bool want_type)
1163 for (tree fields = TYPE_FIELDS (klass); fields; fields = DECL_CHAIN (fields))
1165 tree decl = fields;
1167 if (TREE_CODE (decl) == FIELD_DECL
1168 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
1170 if (tree temp = search_anon_aggr (TREE_TYPE (decl), name, want_type))
1171 return temp;
1174 if (DECL_NAME (decl) != name)
1175 continue;
1177 if (TREE_CODE (decl) == USING_DECL)
1179 decl = strip_using_decl (decl);
1180 if (is_overloaded_fn (decl))
1181 continue;
1184 if (DECL_DECLARES_FUNCTION_P (decl))
1185 /* Functions are found separately. */
1186 continue;
1188 if (!want_type || DECL_DECLARES_TYPE_P (decl))
1189 return decl;
1192 return NULL_TREE;
1195 /* Look for NAME member inside of anonymous aggregate ANON. Although
1196 such things should only contain FIELD_DECLs, we check that too
1197 late, and would give very confusing errors if we weren't
1198 permissive here. */
1200 tree
1201 search_anon_aggr (tree anon, tree name, bool want_type)
1203 gcc_assert (COMPLETE_TYPE_P (anon));
1204 tree ret = get_class_binding_direct (anon, name, want_type);
1205 return ret;
1208 /* Look for NAME as an immediate member of KLASS (including
1209 anon-members or unscoped enum member). TYPE_OR_FNS is zero for
1210 regular search. >0 to get a type binding (if there is one) and <0
1211 if you want (just) the member function binding.
1213 Use this if you do not want lazy member creation. */
1215 tree
1216 get_class_binding_direct (tree klass, tree name, int type_or_fns)
1218 gcc_checking_assert (RECORD_OR_UNION_TYPE_P (klass));
1220 /* Conversion operators can only be found by the marker conversion
1221 operator name. */
1222 bool conv_op = IDENTIFIER_CONV_OP_P (name);
1223 tree lookup = conv_op ? conv_op_identifier : name;
1224 tree val = NULL_TREE;
1225 vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (klass);
1227 if (COMPLETE_TYPE_P (klass) && member_vec)
1229 val = member_vec_binary_search (member_vec, lookup);
1230 if (!val)
1232 else if (type_or_fns > 0)
1234 if (STAT_HACK_P (val))
1235 val = STAT_TYPE (val);
1236 else if (!DECL_DECLARES_TYPE_P (val))
1237 val = NULL_TREE;
1239 else if (STAT_HACK_P (val))
1240 val = STAT_DECL (val);
1242 if (val && TREE_CODE (val) == OVERLOAD
1243 && TREE_CODE (OVL_FUNCTION (val)) == USING_DECL)
1245 /* An overload with a dependent USING_DECL. Does the caller
1246 want the USING_DECL or the functions? */
1247 if (type_or_fns < 0)
1248 val = OVL_CHAIN (val);
1249 else
1250 val = OVL_FUNCTION (val);
1253 else
1255 if (member_vec && type_or_fns <= 0)
1256 val = member_vec_linear_search (member_vec, lookup);
1258 if (type_or_fns < 0)
1259 /* Don't bother looking for field. We don't want it. */;
1260 else if (!val || (TREE_CODE (val) == OVERLOAD && OVL_USING_P (val)))
1261 /* Dependent using declarations are a 'field', make sure we
1262 return that even if we saw an overload already. */
1263 if (tree field_val = fields_linear_search (klass, lookup,
1264 type_or_fns > 0))
1265 if (!val || TREE_CODE (field_val) == USING_DECL)
1266 val = field_val;
1269 /* Extract the conversion operators asked for, unless the general
1270 conversion operator was requested. */
1271 if (val && conv_op)
1273 gcc_checking_assert (OVL_FUNCTION (val) == conv_op_marker);
1274 val = OVL_CHAIN (val);
1275 if (tree type = TREE_TYPE (name))
1276 val = extract_conversion_operator (val, type);
1279 return val;
1282 /* Look for NAME's binding in exactly KLASS. See
1283 get_class_binding_direct for argument description. Does lazy
1284 special function creation as necessary. */
1286 tree
1287 get_class_binding (tree klass, tree name, int type_or_fns)
1289 klass = complete_type (klass);
1291 if (COMPLETE_TYPE_P (klass))
1293 /* Lazily declare functions, if we're going to search these. */
1294 if (IDENTIFIER_CTOR_P (name))
1296 if (CLASSTYPE_LAZY_DEFAULT_CTOR (klass))
1297 lazily_declare_fn (sfk_constructor, klass);
1298 if (CLASSTYPE_LAZY_COPY_CTOR (klass))
1299 lazily_declare_fn (sfk_copy_constructor, klass);
1300 if (CLASSTYPE_LAZY_MOVE_CTOR (klass))
1301 lazily_declare_fn (sfk_move_constructor, klass);
1303 else if (IDENTIFIER_DTOR_P (name))
1305 if (CLASSTYPE_LAZY_DESTRUCTOR (klass))
1306 lazily_declare_fn (sfk_destructor, klass);
1308 else if (name == assign_op_identifier)
1310 if (CLASSTYPE_LAZY_COPY_ASSIGN (klass))
1311 lazily_declare_fn (sfk_copy_assignment, klass);
1312 if (CLASSTYPE_LAZY_MOVE_ASSIGN (klass))
1313 lazily_declare_fn (sfk_move_assignment, klass);
1317 return get_class_binding_direct (klass, name, type_or_fns);
1320 /* Find the slot containing overloads called 'NAME'. If there is no
1321 such slot and the class is complete, create an empty one, at the
1322 correct point in the sorted member vector. Otherwise return NULL.
1323 Deals with conv_op marker handling. */
1325 tree *
1326 find_member_slot (tree klass, tree name)
1328 bool complete_p = COMPLETE_TYPE_P (klass);
1330 vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (klass);
1331 if (!member_vec)
1333 vec_alloc (member_vec, 8);
1334 CLASSTYPE_MEMBER_VEC (klass) = member_vec;
1335 if (complete_p)
1337 /* If the class is complete but had no member_vec, we need
1338 to add the TYPE_FIELDS into it. We're also most likely
1339 to be adding ctors & dtors, so ask for 6 spare slots (the
1340 abstract cdtors and their clones). */
1341 set_class_bindings (klass, 6);
1342 member_vec = CLASSTYPE_MEMBER_VEC (klass);
1346 if (IDENTIFIER_CONV_OP_P (name))
1347 name = conv_op_identifier;
1349 unsigned ix, length = member_vec->length ();
1350 for (ix = 0; ix < length; ix++)
1352 tree *slot = &(*member_vec)[ix];
1353 tree fn_name = OVL_NAME (*slot);
1355 if (fn_name == name)
1357 /* If we found an existing slot, it must be a function set.
1358 Even with insertion after completion, because those only
1359 happen with artificial fns that have unspellable names.
1360 This means we do not have to deal with the stat hack
1361 either. */
1362 gcc_checking_assert (OVL_P (*slot));
1363 if (name == conv_op_identifier)
1365 gcc_checking_assert (OVL_FUNCTION (*slot) == conv_op_marker);
1366 /* Skip the conv-op marker. */
1367 slot = &OVL_CHAIN (*slot);
1369 return slot;
1372 if (complete_p && fn_name > name)
1373 break;
1376 /* No slot found, add one if the class is complete. */
1377 if (complete_p)
1379 /* Do exact allocation, as we don't expect to add many. */
1380 gcc_assert (name != conv_op_identifier);
1381 vec_safe_reserve_exact (member_vec, 1);
1382 CLASSTYPE_MEMBER_VEC (klass) = member_vec;
1383 member_vec->quick_insert (ix, NULL_TREE);
1384 return &(*member_vec)[ix];
1387 return NULL;
1390 /* KLASS is an incomplete class to which we're adding a method NAME.
1391 Add a slot and deal with conv_op marker handling. */
1393 tree *
1394 add_member_slot (tree klass, tree name)
1396 gcc_assert (!COMPLETE_TYPE_P (klass));
1398 vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (klass);
1399 vec_safe_push (member_vec, NULL_TREE);
1400 CLASSTYPE_MEMBER_VEC (klass) = member_vec;
1402 tree *slot = &member_vec->last ();
1403 if (IDENTIFIER_CONV_OP_P (name))
1405 /* Install the marker prefix. */
1406 *slot = ovl_make (conv_op_marker, NULL_TREE);
1407 slot = &OVL_CHAIN (*slot);
1410 return slot;
1413 /* Comparison function to compare two MEMBER_VEC entries by name.
1414 Because we can have duplicates during insertion of TYPE_FIELDS, we
1415 do extra checking so deduping doesn't have to deal with so many
1416 cases. */
1418 static int
1419 member_name_cmp (const void *a_p, const void *b_p)
1421 tree a = *(const tree *)a_p;
1422 tree b = *(const tree *)b_p;
1423 tree name_a = DECL_NAME (TREE_CODE (a) == OVERLOAD ? OVL_FUNCTION (a) : a);
1424 tree name_b = DECL_NAME (TREE_CODE (b) == OVERLOAD ? OVL_FUNCTION (b) : b);
1426 gcc_checking_assert (name_a && name_b);
1427 if (name_a != name_b)
1428 return name_a < name_b ? -1 : +1;
1430 if (name_a == conv_op_identifier)
1432 /* Strip the conv-op markers. */
1433 gcc_checking_assert (OVL_FUNCTION (a) == conv_op_marker
1434 && OVL_FUNCTION (b) == conv_op_marker);
1435 a = OVL_CHAIN (a);
1436 b = OVL_CHAIN (b);
1439 if (TREE_CODE (a) == OVERLOAD)
1440 a = OVL_FUNCTION (a);
1441 if (TREE_CODE (b) == OVERLOAD)
1442 b = OVL_FUNCTION (b);
1444 /* We're in STAT_HACK or USING_DECL territory (or possibly error-land). */
1445 if (TREE_CODE (a) != TREE_CODE (b))
1447 /* If one of them is a TYPE_DECL, it loses. */
1448 if (TREE_CODE (a) == TYPE_DECL)
1449 return +1;
1450 else if (TREE_CODE (b) == TYPE_DECL)
1451 return -1;
1453 /* If one of them is a USING_DECL, it loses. */
1454 if (TREE_CODE (a) == USING_DECL)
1455 return +1;
1456 else if (TREE_CODE (b) == USING_DECL)
1457 return -1;
1459 /* There are no other cases with different kinds of decls, as
1460 duplicate detection should have kicked in earlier. However,
1461 some erroneous cases get though. */
1462 gcc_assert (errorcount);
1465 /* Using source location would be the best thing here, but we can
1466 get identically-located decls in the following circumstances:
1468 1) duplicate artificial type-decls for the same type.
1470 2) pack expansions of using-decls.
1472 We should not be doing #1, but in either case it doesn't matter
1473 how we order these. Use UID as a proxy for source ordering, so
1474 that identically-located decls still have a well-defined stable
1475 ordering. */
1476 if (DECL_UID (a) != DECL_UID (b))
1477 return DECL_UID (a) < DECL_UID (b) ? -1 : +1;
1478 gcc_assert (a == b);
1479 return 0;
1482 static struct {
1483 gt_pointer_operator new_value;
1484 void *cookie;
1485 } resort_data;
1487 /* This routine compares two fields like member_name_cmp but using the
1488 pointer operator in resort_field_decl_data. We don't have to deal
1489 with duplicates here. */
1491 static int
1492 resort_member_name_cmp (const void *a_p, const void *b_p)
1494 tree a = *(const tree *)a_p;
1495 tree b = *(const tree *)b_p;
1496 tree name_a = OVL_NAME (a);
1497 tree name_b = OVL_NAME (b);
1499 resort_data.new_value (&name_a, resort_data.cookie);
1500 resort_data.new_value (&name_b, resort_data.cookie);
1502 gcc_checking_assert (name_a != name_b);
1504 return name_a < name_b ? -1 : +1;
1507 /* Resort CLASSTYPE_MEMBER_VEC because pointers have been reordered. */
1509 void
1510 resort_type_member_vec (void *obj, void */*orig_obj*/,
1511 gt_pointer_operator new_value, void* cookie)
1513 if (vec<tree, va_gc> *member_vec = (vec<tree, va_gc> *) obj)
1515 resort_data.new_value = new_value;
1516 resort_data.cookie = cookie;
1517 member_vec->qsort (resort_member_name_cmp);
1521 /* Recursively count the number of fields in KLASS, including anonymous
1522 union members. */
1524 static unsigned
1525 count_class_fields (tree klass)
1527 unsigned n_fields = 0;
1529 for (tree fields = TYPE_FIELDS (klass); fields; fields = DECL_CHAIN (fields))
1530 if (DECL_DECLARES_FUNCTION_P (fields))
1531 /* Functions are dealt with separately. */;
1532 else if (TREE_CODE (fields) == FIELD_DECL
1533 && ANON_AGGR_TYPE_P (TREE_TYPE (fields)))
1534 n_fields += count_class_fields (TREE_TYPE (fields));
1535 else if (DECL_NAME (fields))
1536 n_fields += 1;
1538 return n_fields;
1541 /* Append all the nonfunction members fields of KLASS to MEMBER_VEC.
1542 Recurse for anonymous members. MEMBER_VEC must have space. */
1544 static void
1545 member_vec_append_class_fields (vec<tree, va_gc> *member_vec, tree klass)
1547 for (tree fields = TYPE_FIELDS (klass); fields; fields = DECL_CHAIN (fields))
1548 if (DECL_DECLARES_FUNCTION_P (fields))
1549 /* Functions are handled separately. */;
1550 else if (TREE_CODE (fields) == FIELD_DECL
1551 && ANON_AGGR_TYPE_P (TREE_TYPE (fields)))
1552 member_vec_append_class_fields (member_vec, TREE_TYPE (fields));
1553 else if (DECL_NAME (fields))
1555 tree field = fields;
1556 /* Mark a conv-op USING_DECL with the conv-op-marker. */
1557 if (TREE_CODE (field) == USING_DECL
1558 && IDENTIFIER_CONV_OP_P (DECL_NAME (field)))
1559 field = ovl_make (conv_op_marker, field);
1560 member_vec->quick_push (field);
1564 /* Append all of the enum values of ENUMTYPE to MEMBER_VEC.
1565 MEMBER_VEC must have space. */
1567 static void
1568 member_vec_append_enum_values (vec<tree, va_gc> *member_vec, tree enumtype)
1570 for (tree values = TYPE_VALUES (enumtype);
1571 values; values = TREE_CHAIN (values))
1572 member_vec->quick_push (TREE_VALUE (values));
1575 /* MEMBER_VEC has just had new DECLs added to it, but is sorted.
1576 DeDup adjacent DECLS of the same name. We already dealt with
1577 conflict resolution when adding the fields or methods themselves.
1578 There are three cases (which could all be combined):
1579 1) a TYPE_DECL and non TYPE_DECL. Deploy STAT_HACK as appropriate.
1580 2) a USING_DECL and an overload. If the USING_DECL is dependent,
1581 it wins. Otherwise the OVERLOAD does.
1582 3) two USING_DECLS. ...
1584 member_name_cmp will have ordered duplicates as
1585 <fns><using><type> */
1587 static void
1588 member_vec_dedup (vec<tree, va_gc> *member_vec)
1590 unsigned len = member_vec->length ();
1591 unsigned store = 0;
1593 if (!len)
1594 return;
1596 tree name = OVL_NAME ((*member_vec)[0]);
1597 for (unsigned jx, ix = 0; ix < len; ix = jx)
1599 tree current = NULL_TREE;
1600 tree to_type = NULL_TREE;
1601 tree to_using = NULL_TREE;
1602 tree marker = NULL_TREE;
1604 for (jx = ix; jx < len; jx++)
1606 tree next = (*member_vec)[jx];
1607 if (jx != ix)
1609 tree next_name = OVL_NAME (next);
1610 if (next_name != name)
1612 name = next_name;
1613 break;
1617 if (IDENTIFIER_CONV_OP_P (name))
1619 marker = next;
1620 next = OVL_CHAIN (next);
1623 if (TREE_CODE (next) == USING_DECL)
1625 if (IDENTIFIER_CTOR_P (name))
1626 /* Dependent inherited ctor. */
1627 continue;
1629 next = strip_using_decl (next);
1630 if (TREE_CODE (next) == USING_DECL)
1632 to_using = next;
1633 continue;
1636 if (is_overloaded_fn (next))
1637 continue;
1640 if (DECL_DECLARES_TYPE_P (next))
1642 to_type = next;
1643 continue;
1646 if (!current)
1647 current = next;
1650 if (to_using)
1652 if (!current)
1653 current = to_using;
1654 else
1655 current = ovl_make (to_using, current);
1658 if (to_type)
1660 if (!current)
1661 current = to_type;
1662 else
1663 current = stat_hack (current, to_type);
1666 if (current)
1668 if (marker)
1670 OVL_CHAIN (marker) = current;
1671 current = marker;
1673 (*member_vec)[store++] = current;
1677 while (store++ < len)
1678 member_vec->pop ();
1681 /* Add the non-function members to CLASSTYPE_MEMBER_VEC. If there is
1682 no existing MEMBER_VEC and fewer than 8 fields, do nothing. We
1683 know there must be at least 1 field -- the self-reference
1684 TYPE_DECL, except for anon aggregates, which will have at least
1685 one field. */
1687 void
1688 set_class_bindings (tree klass, unsigned extra)
1690 unsigned n_fields = count_class_fields (klass);
1691 vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (klass);
1693 if (member_vec || n_fields >= 8)
1695 /* Append the new fields. */
1696 vec_safe_reserve_exact (member_vec, extra + n_fields);
1697 member_vec_append_class_fields (member_vec, klass);
1700 if (member_vec)
1702 CLASSTYPE_MEMBER_VEC (klass) = member_vec;
1703 member_vec->qsort (member_name_cmp);
1704 member_vec_dedup (member_vec);
1708 /* Insert lately defined enum ENUMTYPE into KLASS for the sorted case. */
1710 void
1711 insert_late_enum_def_bindings (tree klass, tree enumtype)
1713 int n_fields;
1714 vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (klass);
1716 /* The enum bindings will already be on the TYPE_FIELDS, so don't
1717 count them twice. */
1718 if (!member_vec)
1719 n_fields = count_class_fields (klass);
1720 else
1721 n_fields = list_length (TYPE_VALUES (enumtype));
1723 if (member_vec || n_fields >= 8)
1725 vec_safe_reserve_exact (member_vec, n_fields);
1726 if (CLASSTYPE_MEMBER_VEC (klass))
1727 member_vec_append_enum_values (member_vec, enumtype);
1728 else
1729 member_vec_append_class_fields (member_vec, klass);
1730 CLASSTYPE_MEMBER_VEC (klass) = member_vec;
1731 member_vec->qsort (member_name_cmp);
1732 member_vec_dedup (member_vec);
1736 /* Compute the chain index of a binding_entry given the HASH value of its
1737 name and the total COUNT of chains. COUNT is assumed to be a power
1738 of 2. */
1740 #define ENTRY_INDEX(HASH, COUNT) (((HASH) >> 3) & ((COUNT) - 1))
1742 /* A free list of "binding_entry"s awaiting for re-use. */
1744 static GTY((deletable)) binding_entry free_binding_entry = NULL;
1746 /* The binding oracle; see cp-tree.h. */
1748 cp_binding_oracle_function *cp_binding_oracle;
1750 /* If we have a binding oracle, ask it for all namespace-scoped
1751 definitions of NAME. */
1753 static inline void
1754 query_oracle (tree name)
1756 if (!cp_binding_oracle)
1757 return;
1759 /* LOOKED_UP holds the set of identifiers that we have already
1760 looked up with the oracle. */
1761 static hash_set<tree> looked_up;
1762 if (looked_up.add (name))
1763 return;
1765 cp_binding_oracle (CP_ORACLE_IDENTIFIER, name);
1768 /* Create a binding_entry object for (NAME, TYPE). */
1770 static inline binding_entry
1771 binding_entry_make (tree name, tree type)
1773 binding_entry entry;
1775 if (free_binding_entry)
1777 entry = free_binding_entry;
1778 free_binding_entry = entry->chain;
1780 else
1781 entry = ggc_alloc<binding_entry_s> ();
1783 entry->name = name;
1784 entry->type = type;
1785 entry->chain = NULL;
1787 return entry;
1790 /* Put ENTRY back on the free list. */
1791 #if 0
1792 static inline void
1793 binding_entry_free (binding_entry entry)
1795 entry->name = NULL;
1796 entry->type = NULL;
1797 entry->chain = free_binding_entry;
1798 free_binding_entry = entry;
1800 #endif
1802 /* The datatype used to implement the mapping from names to types at
1803 a given scope. */
1804 struct GTY(()) binding_table_s {
1805 /* Array of chains of "binding_entry"s */
1806 binding_entry * GTY((length ("%h.chain_count"))) chain;
1808 /* The number of chains in this table. This is the length of the
1809 member "chain" considered as an array. */
1810 size_t chain_count;
1812 /* Number of "binding_entry"s in this table. */
1813 size_t entry_count;
1816 /* Construct TABLE with an initial CHAIN_COUNT. */
1818 static inline void
1819 binding_table_construct (binding_table table, size_t chain_count)
1821 table->chain_count = chain_count;
1822 table->entry_count = 0;
1823 table->chain = ggc_cleared_vec_alloc<binding_entry> (table->chain_count);
1826 /* Make TABLE's entries ready for reuse. */
1827 #if 0
1828 static void
1829 binding_table_free (binding_table table)
1831 size_t i;
1832 size_t count;
1834 if (table == NULL)
1835 return;
1837 for (i = 0, count = table->chain_count; i < count; ++i)
1839 binding_entry temp = table->chain[i];
1840 while (temp != NULL)
1842 binding_entry entry = temp;
1843 temp = entry->chain;
1844 binding_entry_free (entry);
1846 table->chain[i] = NULL;
1848 table->entry_count = 0;
1850 #endif
1852 /* Allocate a table with CHAIN_COUNT, assumed to be a power of two. */
1854 static inline binding_table
1855 binding_table_new (size_t chain_count)
1857 binding_table table = ggc_alloc<binding_table_s> ();
1858 table->chain = NULL;
1859 binding_table_construct (table, chain_count);
1860 return table;
1863 /* Expand TABLE to twice its current chain_count. */
1865 static void
1866 binding_table_expand (binding_table table)
1868 const size_t old_chain_count = table->chain_count;
1869 const size_t old_entry_count = table->entry_count;
1870 const size_t new_chain_count = 2 * old_chain_count;
1871 binding_entry *old_chains = table->chain;
1872 size_t i;
1874 binding_table_construct (table, new_chain_count);
1875 for (i = 0; i < old_chain_count; ++i)
1877 binding_entry entry = old_chains[i];
1878 for (; entry != NULL; entry = old_chains[i])
1880 const unsigned int hash = IDENTIFIER_HASH_VALUE (entry->name);
1881 const size_t j = ENTRY_INDEX (hash, new_chain_count);
1883 old_chains[i] = entry->chain;
1884 entry->chain = table->chain[j];
1885 table->chain[j] = entry;
1888 table->entry_count = old_entry_count;
1891 /* Insert a binding for NAME to TYPE into TABLE. */
1893 static void
1894 binding_table_insert (binding_table table, tree name, tree type)
1896 const unsigned int hash = IDENTIFIER_HASH_VALUE (name);
1897 const size_t i = ENTRY_INDEX (hash, table->chain_count);
1898 binding_entry entry = binding_entry_make (name, type);
1900 entry->chain = table->chain[i];
1901 table->chain[i] = entry;
1902 ++table->entry_count;
1904 if (3 * table->chain_count < 5 * table->entry_count)
1905 binding_table_expand (table);
1908 /* Return the binding_entry, if any, that maps NAME. */
1910 binding_entry
1911 binding_table_find (binding_table table, tree name)
1913 const unsigned int hash = IDENTIFIER_HASH_VALUE (name);
1914 binding_entry entry = table->chain[ENTRY_INDEX (hash, table->chain_count)];
1916 while (entry != NULL && entry->name != name)
1917 entry = entry->chain;
1919 return entry;
1922 /* Apply PROC -- with DATA -- to all entries in TABLE. */
1924 void
1925 binding_table_foreach (binding_table table, bt_foreach_proc proc, void *data)
1927 size_t chain_count;
1928 size_t i;
1930 if (!table)
1931 return;
1933 chain_count = table->chain_count;
1934 for (i = 0; i < chain_count; ++i)
1936 binding_entry entry = table->chain[i];
1937 for (; entry != NULL; entry = entry->chain)
1938 proc (entry, data);
1942 #ifndef ENABLE_SCOPE_CHECKING
1943 # define ENABLE_SCOPE_CHECKING 0
1944 #else
1945 # define ENABLE_SCOPE_CHECKING 1
1946 #endif
1948 /* A free list of "cxx_binding"s, connected by their PREVIOUS. */
1950 static GTY((deletable)) cxx_binding *free_bindings;
1952 /* Initialize VALUE and TYPE field for BINDING, and set the PREVIOUS
1953 field to NULL. */
1955 static inline void
1956 cxx_binding_init (cxx_binding *binding, tree value, tree type)
1958 binding->value = value;
1959 binding->type = type;
1960 binding->previous = NULL;
1963 /* (GC)-allocate a binding object with VALUE and TYPE member initialized. */
1965 static cxx_binding *
1966 cxx_binding_make (tree value, tree type)
1968 cxx_binding *binding;
1969 if (free_bindings)
1971 binding = free_bindings;
1972 free_bindings = binding->previous;
1974 else
1975 binding = ggc_alloc<cxx_binding> ();
1977 cxx_binding_init (binding, value, type);
1979 return binding;
1982 /* Put BINDING back on the free list. */
1984 static inline void
1985 cxx_binding_free (cxx_binding *binding)
1987 binding->scope = NULL;
1988 binding->previous = free_bindings;
1989 free_bindings = binding;
1992 /* Create a new binding for NAME (with the indicated VALUE and TYPE
1993 bindings) in the class scope indicated by SCOPE. */
1995 static cxx_binding *
1996 new_class_binding (tree name, tree value, tree type, cp_binding_level *scope)
1998 cp_class_binding cb = {cxx_binding_make (value, type), name};
1999 cxx_binding *binding = cb.base;
2000 vec_safe_push (scope->class_shadowed, cb);
2001 binding->scope = scope;
2002 return binding;
2005 /* Make DECL the innermost binding for ID. The LEVEL is the binding
2006 level at which this declaration is being bound. */
2008 void
2009 push_binding (tree id, tree decl, cp_binding_level* level)
2011 cxx_binding *binding;
2013 if (level != class_binding_level)
2015 binding = cxx_binding_make (decl, NULL_TREE);
2016 binding->scope = level;
2018 else
2019 binding = new_class_binding (id, decl, /*type=*/NULL_TREE, level);
2021 /* Now, fill in the binding information. */
2022 binding->previous = IDENTIFIER_BINDING (id);
2023 INHERITED_VALUE_BINDING_P (binding) = 0;
2024 LOCAL_BINDING_P (binding) = (level != class_binding_level);
2026 /* And put it on the front of the list of bindings for ID. */
2027 IDENTIFIER_BINDING (id) = binding;
2030 /* Remove the binding for DECL which should be the innermost binding
2031 for ID. */
2033 void
2034 pop_local_binding (tree id, tree decl)
2036 cxx_binding *binding;
2038 if (id == NULL_TREE)
2039 /* It's easiest to write the loops that call this function without
2040 checking whether or not the entities involved have names. We
2041 get here for such an entity. */
2042 return;
2044 /* Get the innermost binding for ID. */
2045 binding = IDENTIFIER_BINDING (id);
2047 /* The name should be bound. */
2048 gcc_assert (binding != NULL);
2050 /* The DECL will be either the ordinary binding or the type
2051 binding for this identifier. Remove that binding. */
2052 if (binding->value == decl)
2053 binding->value = NULL_TREE;
2054 else
2056 gcc_assert (binding->type == decl);
2057 binding->type = NULL_TREE;
2060 if (!binding->value && !binding->type)
2062 /* We're completely done with the innermost binding for this
2063 identifier. Unhook it from the list of bindings. */
2064 IDENTIFIER_BINDING (id) = binding->previous;
2066 /* Add it to the free list. */
2067 cxx_binding_free (binding);
2071 /* Remove the bindings for the decls of the current level and leave
2072 the current scope. */
2074 void
2075 pop_bindings_and_leave_scope (void)
2077 for (tree t = get_local_decls (); t; t = DECL_CHAIN (t))
2079 tree decl = TREE_CODE (t) == TREE_LIST ? TREE_VALUE (t) : t;
2080 tree name = OVL_NAME (decl);
2082 pop_local_binding (name, decl);
2085 leave_scope ();
2088 /* Strip non dependent using declarations. If DECL is dependent,
2089 surreptitiously create a typename_type and return it. */
2091 tree
2092 strip_using_decl (tree decl)
2094 if (decl == NULL_TREE)
2095 return NULL_TREE;
2097 while (TREE_CODE (decl) == USING_DECL && !DECL_DEPENDENT_P (decl))
2098 decl = USING_DECL_DECLS (decl);
2100 if (TREE_CODE (decl) == USING_DECL && DECL_DEPENDENT_P (decl)
2101 && USING_DECL_TYPENAME_P (decl))
2103 /* We have found a type introduced by a using
2104 declaration at class scope that refers to a dependent
2105 type.
2107 using typename :: [opt] nested-name-specifier unqualified-id ;
2109 decl = make_typename_type (TREE_TYPE (decl),
2110 DECL_NAME (decl),
2111 typename_type, tf_error);
2112 if (decl != error_mark_node)
2113 decl = TYPE_NAME (decl);
2116 return decl;
2119 /* Return true if OVL is an overload for an anticipated builtin. */
2121 static bool
2122 anticipated_builtin_p (tree ovl)
2124 if (TREE_CODE (ovl) != OVERLOAD)
2125 return false;
2127 if (!OVL_HIDDEN_P (ovl))
2128 return false;
2130 tree fn = OVL_FUNCTION (ovl);
2131 gcc_checking_assert (DECL_ANTICIPATED (fn));
2133 if (DECL_HIDDEN_FRIEND_P (fn))
2134 return false;
2136 return true;
2139 /* BINDING records an existing declaration for a name in the current scope.
2140 But, DECL is another declaration for that same identifier in the
2141 same scope. This is the `struct stat' hack whereby a non-typedef
2142 class name or enum-name can be bound at the same level as some other
2143 kind of entity.
2144 3.3.7/1
2146 A class name (9.1) or enumeration name (7.2) can be hidden by the
2147 name of an object, function, or enumerator declared in the same scope.
2148 If a class or enumeration name and an object, function, or enumerator
2149 are declared in the same scope (in any order) with the same name, the
2150 class or enumeration name is hidden wherever the object, function, or
2151 enumerator name is visible.
2153 It's the responsibility of the caller to check that
2154 inserting this name is valid here. Returns nonzero if the new binding
2155 was successful. */
2157 static bool
2158 supplement_binding_1 (cxx_binding *binding, tree decl)
2160 tree bval = binding->value;
2161 bool ok = true;
2162 tree target_bval = strip_using_decl (bval);
2163 tree target_decl = strip_using_decl (decl);
2165 if (TREE_CODE (target_decl) == TYPE_DECL && DECL_ARTIFICIAL (target_decl)
2166 && target_decl != target_bval
2167 && (TREE_CODE (target_bval) != TYPE_DECL
2168 /* We allow pushing an enum multiple times in a class
2169 template in order to handle late matching of underlying
2170 type on an opaque-enum-declaration followed by an
2171 enum-specifier. */
2172 || (processing_template_decl
2173 && TREE_CODE (TREE_TYPE (target_decl)) == ENUMERAL_TYPE
2174 && TREE_CODE (TREE_TYPE (target_bval)) == ENUMERAL_TYPE
2175 && (dependent_type_p (ENUM_UNDERLYING_TYPE
2176 (TREE_TYPE (target_decl)))
2177 || dependent_type_p (ENUM_UNDERLYING_TYPE
2178 (TREE_TYPE (target_bval)))))))
2179 /* The new name is the type name. */
2180 binding->type = decl;
2181 else if (/* TARGET_BVAL is null when push_class_level_binding moves
2182 an inherited type-binding out of the way to make room
2183 for a new value binding. */
2184 !target_bval
2185 /* TARGET_BVAL is error_mark_node when TARGET_DECL's name
2186 has been used in a non-class scope prior declaration.
2187 In that case, we should have already issued a
2188 diagnostic; for graceful error recovery purpose, pretend
2189 this was the intended declaration for that name. */
2190 || target_bval == error_mark_node
2191 /* If TARGET_BVAL is anticipated but has not yet been
2192 declared, pretend it is not there at all. */
2193 || anticipated_builtin_p (target_bval))
2194 binding->value = decl;
2195 else if (TREE_CODE (target_bval) == TYPE_DECL
2196 && DECL_ARTIFICIAL (target_bval)
2197 && target_decl != target_bval
2198 && (TREE_CODE (target_decl) != TYPE_DECL
2199 || same_type_p (TREE_TYPE (target_decl),
2200 TREE_TYPE (target_bval))))
2202 /* The old binding was a type name. It was placed in
2203 VALUE field because it was thought, at the point it was
2204 declared, to be the only entity with such a name. Move the
2205 type name into the type slot; it is now hidden by the new
2206 binding. */
2207 binding->type = bval;
2208 binding->value = decl;
2209 binding->value_is_inherited = false;
2211 else if (TREE_CODE (target_bval) == TYPE_DECL
2212 && TREE_CODE (target_decl) == TYPE_DECL
2213 && DECL_NAME (target_decl) == DECL_NAME (target_bval)
2214 && binding->scope->kind != sk_class
2215 && (same_type_p (TREE_TYPE (target_decl), TREE_TYPE (target_bval))
2216 /* If either type involves template parameters, we must
2217 wait until instantiation. */
2218 || uses_template_parms (TREE_TYPE (target_decl))
2219 || uses_template_parms (TREE_TYPE (target_bval))))
2220 /* We have two typedef-names, both naming the same type to have
2221 the same name. In general, this is OK because of:
2223 [dcl.typedef]
2225 In a given scope, a typedef specifier can be used to redefine
2226 the name of any type declared in that scope to refer to the
2227 type to which it already refers.
2229 However, in class scopes, this rule does not apply due to the
2230 stricter language in [class.mem] prohibiting redeclarations of
2231 members. */
2232 ok = false;
2233 /* There can be two block-scope declarations of the same variable,
2234 so long as they are `extern' declarations. However, there cannot
2235 be two declarations of the same static data member:
2237 [class.mem]
2239 A member shall not be declared twice in the
2240 member-specification. */
2241 else if (VAR_P (target_decl)
2242 && VAR_P (target_bval)
2243 && DECL_EXTERNAL (target_decl) && DECL_EXTERNAL (target_bval)
2244 && !DECL_CLASS_SCOPE_P (target_decl))
2246 duplicate_decls (decl, binding->value, /*newdecl_is_friend=*/false);
2247 ok = false;
2249 else if (TREE_CODE (decl) == NAMESPACE_DECL
2250 && TREE_CODE (bval) == NAMESPACE_DECL
2251 && DECL_NAMESPACE_ALIAS (decl)
2252 && DECL_NAMESPACE_ALIAS (bval)
2253 && ORIGINAL_NAMESPACE (bval) == ORIGINAL_NAMESPACE (decl))
2254 /* [namespace.alias]
2256 In a declarative region, a namespace-alias-definition can be
2257 used to redefine a namespace-alias declared in that declarative
2258 region to refer only to the namespace to which it already
2259 refers. */
2260 ok = false;
2261 else
2263 if (!error_operand_p (bval))
2264 diagnose_name_conflict (decl, bval);
2265 ok = false;
2268 return ok;
2271 /* Diagnose a name conflict between DECL and BVAL. */
2273 static void
2274 diagnose_name_conflict (tree decl, tree bval)
2276 if (TREE_CODE (decl) == TREE_CODE (bval)
2277 && TREE_CODE (decl) != NAMESPACE_DECL
2278 && !DECL_DECLARES_FUNCTION_P (decl)
2279 && (TREE_CODE (decl) != TYPE_DECL
2280 || DECL_ARTIFICIAL (decl) == DECL_ARTIFICIAL (bval))
2281 && CP_DECL_CONTEXT (decl) == CP_DECL_CONTEXT (bval))
2282 error ("redeclaration of %q#D", decl);
2283 else
2284 error ("%q#D conflicts with a previous declaration", decl);
2286 inform (location_of (bval), "previous declaration %q#D", bval);
2289 /* Wrapper for supplement_binding_1. */
2291 static bool
2292 supplement_binding (cxx_binding *binding, tree decl)
2294 bool ret;
2295 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
2296 ret = supplement_binding_1 (binding, decl);
2297 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
2298 return ret;
2301 /* Replace BINDING's current value on its scope's name list with
2302 NEWVAL. */
2304 static void
2305 update_local_overload (cxx_binding *binding, tree newval)
2307 tree *d;
2309 for (d = &binding->scope->names; ; d = &TREE_CHAIN (*d))
2310 if (*d == binding->value)
2312 /* Stitch new list node in. */
2313 *d = tree_cons (NULL_TREE, NULL_TREE, TREE_CHAIN (*d));
2314 break;
2316 else if (TREE_CODE (*d) == TREE_LIST && TREE_VALUE (*d) == binding->value)
2317 break;
2319 TREE_VALUE (*d) = newval;
2322 /* Compares the parameter-type-lists of ONE and TWO and
2323 returns false if they are different. If the DECLs are template
2324 functions, the return types and the template parameter lists are
2325 compared too (DR 565). */
2327 static bool
2328 matching_fn_p (tree one, tree two)
2330 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (one)),
2331 TYPE_ARG_TYPES (TREE_TYPE (two))))
2332 return false;
2334 if (TREE_CODE (one) == TEMPLATE_DECL
2335 && TREE_CODE (two) == TEMPLATE_DECL)
2337 /* Compare template parms. */
2338 if (!comp_template_parms (DECL_TEMPLATE_PARMS (one),
2339 DECL_TEMPLATE_PARMS (two)))
2340 return false;
2342 /* And return type. */
2343 if (!same_type_p (TREE_TYPE (TREE_TYPE (one)),
2344 TREE_TYPE (TREE_TYPE (two))))
2345 return false;
2348 return true;
2351 /* Push DECL into nonclass LEVEL BINDING or SLOT. OLD is the current
2352 binding value (possibly with anticipated builtins stripped).
2353 Diagnose conflicts and return updated decl. */
2355 static tree
2356 update_binding (cp_binding_level *level, cxx_binding *binding, tree *slot,
2357 tree old, tree decl, bool is_friend)
2359 tree to_val = decl;
2360 tree old_type = slot ? MAYBE_STAT_TYPE (*slot) : binding->type;
2361 tree to_type = old_type;
2363 gcc_assert (level->kind == sk_namespace ? !binding
2364 : level->kind != sk_class && !slot);
2365 if (old == error_mark_node)
2366 old = NULL_TREE;
2368 if (TREE_CODE (decl) == TYPE_DECL && DECL_ARTIFICIAL (decl))
2370 tree other = to_type;
2372 if (old && TREE_CODE (old) == TYPE_DECL && DECL_ARTIFICIAL (old))
2373 other = old;
2375 /* Pushing an artificial typedef. See if this matches either
2376 the type slot or the old value slot. */
2377 if (!other)
2379 else if (same_type_p (TREE_TYPE (other), TREE_TYPE (decl)))
2380 /* Two artificial decls to same type. Do nothing. */
2381 return other;
2382 else
2383 goto conflict;
2385 if (old)
2387 /* Slide decl into the type slot, keep old unaltered */
2388 to_type = decl;
2389 to_val = old;
2390 goto done;
2394 if (old && TREE_CODE (old) == TYPE_DECL && DECL_ARTIFICIAL (old))
2396 /* Slide old into the type slot. */
2397 to_type = old;
2398 old = NULL_TREE;
2401 if (DECL_DECLARES_FUNCTION_P (decl))
2403 if (!old)
2405 else if (OVL_P (old))
2407 for (ovl_iterator iter (old); iter; ++iter)
2409 tree fn = *iter;
2411 if (iter.using_p () && matching_fn_p (fn, decl))
2413 /* If a function declaration in namespace scope or
2414 block scope has the same name and the same
2415 parameter-type- list (8.3.5) as a function
2416 introduced by a using-declaration, and the
2417 declarations do not declare the same function,
2418 the program is ill-formed. [namespace.udecl]/14 */
2419 if (tree match = duplicate_decls (decl, fn, is_friend))
2420 return match;
2421 else
2422 /* FIXME: To preserve existing error behavior, we
2423 still push the decl. This might change. */
2424 diagnose_name_conflict (decl, fn);
2428 else
2429 goto conflict;
2431 if (to_type != old_type
2432 && warn_shadow
2433 && MAYBE_CLASS_TYPE_P (TREE_TYPE (to_type))
2434 && !(DECL_IN_SYSTEM_HEADER (decl)
2435 && DECL_IN_SYSTEM_HEADER (to_type)))
2436 warning (OPT_Wshadow, "%q#D hides constructor for %q#D",
2437 decl, to_type);
2439 to_val = ovl_insert (decl, old);
2441 else if (!old)
2443 else if (TREE_CODE (old) != TREE_CODE (decl))
2444 /* Different kinds of decls conflict. */
2445 goto conflict;
2446 else if (TREE_CODE (old) == TYPE_DECL)
2448 if (same_type_p (TREE_TYPE (old), TREE_TYPE (decl)))
2449 /* Two type decls to the same type. Do nothing. */
2450 return old;
2451 else
2452 goto conflict;
2454 else if (TREE_CODE (old) == NAMESPACE_DECL)
2456 /* Two maybe-aliased namespaces. If they're to the same target
2457 namespace, that's ok. */
2458 if (ORIGINAL_NAMESPACE (old) != ORIGINAL_NAMESPACE (decl))
2459 goto conflict;
2461 /* The new one must be an alias at this point. */
2462 gcc_assert (DECL_NAMESPACE_ALIAS (decl));
2463 return old;
2465 else if (TREE_CODE (old) == VAR_DECL)
2467 /* There can be two block-scope declarations of the same
2468 variable, so long as they are `extern' declarations. */
2469 if (!DECL_EXTERNAL (old) || !DECL_EXTERNAL (decl))
2470 goto conflict;
2471 else if (tree match = duplicate_decls (decl, old, false))
2472 return match;
2473 else
2474 goto conflict;
2476 else
2478 conflict:
2479 diagnose_name_conflict (decl, old);
2480 to_val = NULL_TREE;
2483 done:
2484 if (to_val)
2486 if (level->kind == sk_namespace || to_type == decl || to_val == decl)
2487 add_decl_to_level (level, decl);
2488 else
2490 gcc_checking_assert (binding->value && OVL_P (binding->value));
2491 update_local_overload (binding, to_val);
2494 if (slot)
2496 if (STAT_HACK_P (*slot))
2498 STAT_TYPE (*slot) = to_type;
2499 STAT_DECL (*slot) = to_val;
2501 else if (to_type)
2502 *slot = stat_hack (to_val, to_type);
2503 else
2504 *slot = to_val;
2506 else
2508 binding->type = to_type;
2509 binding->value = to_val;
2513 return decl;
2516 /* Table of identifiers to extern C declarations (or LISTS thereof). */
2518 static GTY(()) hash_table<named_decl_hash> *extern_c_decls;
2520 /* DECL has C linkage. If we have an existing instance, make sure the
2521 new one is compatible. Make sure it has the same exception
2522 specification [7.5, 7.6]. Add DECL to the map. */
2524 static void
2525 check_extern_c_conflict (tree decl)
2527 /* Ignore artificial or system header decls. */
2528 if (DECL_ARTIFICIAL (decl) || DECL_IN_SYSTEM_HEADER (decl))
2529 return;
2531 /* This only applies to decls at namespace scope. */
2532 if (!DECL_NAMESPACE_SCOPE_P (decl))
2533 return;
2535 if (!extern_c_decls)
2536 extern_c_decls = hash_table<named_decl_hash>::create_ggc (127);
2538 tree *slot = extern_c_decls
2539 ->find_slot_with_hash (DECL_NAME (decl),
2540 IDENTIFIER_HASH_VALUE (DECL_NAME (decl)), INSERT);
2541 if (tree old = *slot)
2543 if (TREE_CODE (old) == OVERLOAD)
2544 old = OVL_FUNCTION (old);
2546 int mismatch = 0;
2547 if (DECL_CONTEXT (old) == DECL_CONTEXT (decl))
2548 ; /* If they're in the same context, we'll have already complained
2549 about a (possible) mismatch, when inserting the decl. */
2550 else if (!decls_match (decl, old))
2551 mismatch = 1;
2552 else if (TREE_CODE (decl) == FUNCTION_DECL
2553 && !comp_except_specs (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (old)),
2554 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl)),
2555 ce_normal))
2556 mismatch = -1;
2557 else if (DECL_ASSEMBLER_NAME_SET_P (old))
2558 SET_DECL_ASSEMBLER_NAME (decl, DECL_ASSEMBLER_NAME (old));
2560 if (mismatch)
2562 auto_diagnostic_group d;
2563 pedwarn (input_location, 0,
2564 "conflicting C language linkage declaration %q#D", decl);
2565 inform (DECL_SOURCE_LOCATION (old),
2566 "previous declaration %q#D", old);
2567 if (mismatch < 0)
2568 inform (input_location,
2569 "due to different exception specifications");
2571 else
2573 if (old == *slot)
2574 /* The hash table expects OVERLOADS, so construct one with
2575 OLD as both the function and the chain. This allocate
2576 an excess OVERLOAD node, but it's rare to have multiple
2577 extern "C" decls of the same name. And we save
2578 complicating the hash table logic (which is used
2579 elsewhere). */
2580 *slot = ovl_make (old, old);
2582 slot = &OVL_CHAIN (*slot);
2584 /* Chain it on for c_linkage_binding's use. */
2585 *slot = tree_cons (NULL_TREE, decl, *slot);
2588 else
2589 *slot = decl;
2592 /* Returns a list of C-linkage decls with the name NAME. Used in
2593 c-family/c-pragma.c to implement redefine_extname pragma. */
2595 tree
2596 c_linkage_bindings (tree name)
2598 if (extern_c_decls)
2599 if (tree *slot = extern_c_decls
2600 ->find_slot_with_hash (name, IDENTIFIER_HASH_VALUE (name), NO_INSERT))
2602 tree result = *slot;
2603 if (TREE_CODE (result) == OVERLOAD)
2604 result = OVL_CHAIN (result);
2605 return result;
2608 return NULL_TREE;
2611 /* Subroutine of check_local_shadow. */
2613 static void
2614 inform_shadowed (tree shadowed)
2616 inform (DECL_SOURCE_LOCATION (shadowed),
2617 "shadowed declaration is here");
2620 /* DECL is being declared at a local scope. Emit suitable shadow
2621 warnings. */
2623 static void
2624 check_local_shadow (tree decl)
2626 /* Don't complain about the parms we push and then pop
2627 while tentatively parsing a function declarator. */
2628 if (TREE_CODE (decl) == PARM_DECL && !DECL_CONTEXT (decl))
2629 return;
2631 /* External decls are something else. */
2632 if (DECL_EXTERNAL (decl))
2633 return;
2635 tree old = NULL_TREE;
2636 cp_binding_level *old_scope = NULL;
2637 if (cxx_binding *binding = outer_binding (DECL_NAME (decl), NULL, true))
2639 old = binding->value;
2640 old_scope = binding->scope;
2643 if (old
2644 && (TREE_CODE (old) == PARM_DECL
2645 || VAR_P (old)
2646 || (TREE_CODE (old) == TYPE_DECL
2647 && (!DECL_ARTIFICIAL (old)
2648 || TREE_CODE (decl) == TYPE_DECL)))
2649 && DECL_FUNCTION_SCOPE_P (old)
2650 && (!DECL_ARTIFICIAL (decl)
2651 || is_capture_proxy (decl)
2652 || DECL_IMPLICIT_TYPEDEF_P (decl)
2653 || (VAR_P (decl) && DECL_ANON_UNION_VAR_P (decl))))
2655 /* DECL shadows a local thing possibly of interest. */
2657 /* DR 2211: check that captures and parameters
2658 do not have the same name. */
2659 if (is_capture_proxy (decl))
2661 if (current_lambda_expr ()
2662 && DECL_CONTEXT (old) == lambda_function (current_lambda_expr ())
2663 && TREE_CODE (old) == PARM_DECL
2664 && DECL_NAME (decl) != this_identifier)
2666 error_at (DECL_SOURCE_LOCATION (old),
2667 "lambda parameter %qD "
2668 "previously declared as a capture", old);
2670 return;
2672 /* Don't complain if it's from an enclosing function. */
2673 else if (DECL_CONTEXT (old) == current_function_decl
2674 && TREE_CODE (decl) != PARM_DECL
2675 && TREE_CODE (old) == PARM_DECL)
2677 /* Go to where the parms should be and see if we find
2678 them there. */
2679 cp_binding_level *b = current_binding_level->level_chain;
2681 if (FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
2682 /* Skip the ctor/dtor cleanup level. */
2683 b = b->level_chain;
2685 /* ARM $8.3 */
2686 if (b->kind == sk_function_parms)
2688 error ("declaration of %q#D shadows a parameter", decl);
2689 return;
2693 /* The local structure or class can't use parameters of
2694 the containing function anyway. */
2695 if (DECL_CONTEXT (old) != current_function_decl)
2697 for (cp_binding_level *scope = current_binding_level;
2698 scope != old_scope; scope = scope->level_chain)
2699 if (scope->kind == sk_class
2700 && !LAMBDA_TYPE_P (scope->this_entity))
2701 return;
2703 /* Error if redeclaring a local declared in a
2704 init-statement or in the condition of an if or
2705 switch statement when the new declaration is in the
2706 outermost block of the controlled statement.
2707 Redeclaring a variable from a for or while condition is
2708 detected elsewhere. */
2709 else if (VAR_P (old)
2710 && old_scope == current_binding_level->level_chain
2711 && (old_scope->kind == sk_cond || old_scope->kind == sk_for))
2713 auto_diagnostic_group d;
2714 error ("redeclaration of %q#D", decl);
2715 inform (DECL_SOURCE_LOCATION (old),
2716 "%q#D previously declared here", old);
2717 return;
2719 /* C++11:
2720 3.3.3/3: The name declared in an exception-declaration (...)
2721 shall not be redeclared in the outermost block of the handler.
2722 3.3.3/2: A parameter name shall not be redeclared (...) in
2723 the outermost block of any handler associated with a
2724 function-try-block.
2725 3.4.1/15: The function parameter names shall not be redeclared
2726 in the exception-declaration nor in the outermost block of a
2727 handler for the function-try-block. */
2728 else if ((TREE_CODE (old) == VAR_DECL
2729 && old_scope == current_binding_level->level_chain
2730 && old_scope->kind == sk_catch)
2731 || (TREE_CODE (old) == PARM_DECL
2732 && (current_binding_level->kind == sk_catch
2733 || current_binding_level->level_chain->kind == sk_catch)
2734 && in_function_try_handler))
2736 auto_diagnostic_group d;
2737 if (permerror (input_location, "redeclaration of %q#D", decl))
2738 inform (DECL_SOURCE_LOCATION (old),
2739 "%q#D previously declared here", old);
2740 return;
2743 /* If '-Wshadow=compatible-local' is specified without other
2744 -Wshadow= flags, we will warn only when the type of the
2745 shadowing variable (DECL) can be converted to that of the
2746 shadowed parameter (OLD_LOCAL). The reason why we only check
2747 if DECL's type can be converted to OLD_LOCAL's type (but not the
2748 other way around) is because when users accidentally shadow a
2749 parameter, more than often they would use the variable
2750 thinking (mistakenly) it's still the parameter. It would be
2751 rare that users would use the variable in the place that
2752 expects the parameter but thinking it's a new decl. */
2754 enum opt_code warning_code;
2755 if (warn_shadow)
2756 warning_code = OPT_Wshadow;
2757 else if (warn_shadow_local)
2758 warning_code = OPT_Wshadow_local;
2759 else if (warn_shadow_compatible_local
2760 && (same_type_p (TREE_TYPE (old), TREE_TYPE (decl))
2761 || (!dependent_type_p (TREE_TYPE (decl))
2762 && !dependent_type_p (TREE_TYPE (old))
2763 /* If the new decl uses auto, we don't yet know
2764 its type (the old type cannot be using auto
2765 at this point, without also being
2766 dependent). This is an indication we're
2767 (now) doing the shadow checking too
2768 early. */
2769 && !type_uses_auto (TREE_TYPE (decl))
2770 && can_convert (TREE_TYPE (old), TREE_TYPE (decl),
2771 tf_none))))
2772 warning_code = OPT_Wshadow_compatible_local;
2773 else
2774 return;
2776 const char *msg;
2777 if (TREE_CODE (old) == PARM_DECL)
2778 msg = "declaration of %q#D shadows a parameter";
2779 else if (is_capture_proxy (old))
2780 msg = "declaration of %qD shadows a lambda capture";
2781 else
2782 msg = "declaration of %qD shadows a previous local";
2784 auto_diagnostic_group d;
2785 if (warning_at (input_location, warning_code, msg, decl))
2786 inform_shadowed (old);
2787 return;
2790 if (!warn_shadow)
2791 return;
2793 /* Don't warn for artificial things that are not implicit typedefs. */
2794 if (DECL_ARTIFICIAL (decl) && !DECL_IMPLICIT_TYPEDEF_P (decl))
2795 return;
2797 if (nonlambda_method_basetype ())
2798 if (tree member = lookup_member (current_nonlambda_class_type (),
2799 DECL_NAME (decl), /*protect=*/0,
2800 /*want_type=*/false, tf_warning_or_error))
2802 member = MAYBE_BASELINK_FUNCTIONS (member);
2804 /* Warn if a variable shadows a non-function, or the variable
2805 is a function or a pointer-to-function. */
2806 if (!OVL_P (member)
2807 || TREE_CODE (decl) == FUNCTION_DECL
2808 || TYPE_PTRFN_P (TREE_TYPE (decl))
2809 || TYPE_PTRMEMFUNC_P (TREE_TYPE (decl)))
2811 auto_diagnostic_group d;
2812 if (warning_at (input_location, OPT_Wshadow,
2813 "declaration of %qD shadows a member of %qT",
2814 decl, current_nonlambda_class_type ())
2815 && DECL_P (member))
2816 inform_shadowed (member);
2818 return;
2821 /* Now look for a namespace shadow. */
2822 old = find_namespace_value (current_namespace, DECL_NAME (decl));
2823 if (old
2824 && (VAR_P (old)
2825 || (TREE_CODE (old) == TYPE_DECL
2826 && (!DECL_ARTIFICIAL (old)
2827 || TREE_CODE (decl) == TYPE_DECL)))
2828 && !instantiating_current_function_p ())
2829 /* XXX shadow warnings in outer-more namespaces */
2831 auto_diagnostic_group d;
2832 if (warning_at (input_location, OPT_Wshadow,
2833 "declaration of %qD shadows a global declaration",
2834 decl))
2835 inform_shadowed (old);
2836 return;
2839 return;
2842 /* DECL is being pushed inside function CTX. Set its context, if
2843 needed. */
2845 static void
2846 set_decl_context_in_fn (tree ctx, tree decl)
2848 if (!DECL_CONTEXT (decl)
2849 /* A local declaration for a function doesn't constitute
2850 nesting. */
2851 && TREE_CODE (decl) != FUNCTION_DECL
2852 /* A local declaration for an `extern' variable is in the
2853 scope of the current namespace, not the current
2854 function. */
2855 && !(VAR_P (decl) && DECL_EXTERNAL (decl))
2856 /* When parsing the parameter list of a function declarator,
2857 don't set DECL_CONTEXT to an enclosing function. When we
2858 push the PARM_DECLs in order to process the function body,
2859 current_binding_level->this_entity will be set. */
2860 && !(TREE_CODE (decl) == PARM_DECL
2861 && current_binding_level->kind == sk_function_parms
2862 && current_binding_level->this_entity == NULL))
2863 DECL_CONTEXT (decl) = ctx;
2865 /* If this is the declaration for a namespace-scope function,
2866 but the declaration itself is in a local scope, mark the
2867 declaration. */
2868 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (decl))
2869 DECL_LOCAL_FUNCTION_P (decl) = 1;
2872 /* DECL is a local-scope decl with linkage. SHADOWED is true if the
2873 name is already bound at the current level.
2875 [basic.link] If there is a visible declaration of an entity with
2876 linkage having the same name and type, ignoring entities declared
2877 outside the innermost enclosing namespace scope, the block scope
2878 declaration declares that same entity and receives the linkage of
2879 the previous declaration.
2881 Also, make sure that this decl matches any existing external decl
2882 in the enclosing namespace. */
2884 static void
2885 set_local_extern_decl_linkage (tree decl, bool shadowed)
2887 tree ns_value = decl; /* Unique marker. */
2889 if (!shadowed)
2891 tree loc_value = innermost_non_namespace_value (DECL_NAME (decl));
2892 if (!loc_value)
2894 ns_value
2895 = find_namespace_value (current_namespace, DECL_NAME (decl));
2896 loc_value = ns_value;
2898 if (loc_value == error_mark_node
2899 /* An ambiguous lookup. */
2900 || (loc_value && TREE_CODE (loc_value) == TREE_LIST))
2901 loc_value = NULL_TREE;
2903 for (ovl_iterator iter (loc_value); iter; ++iter)
2904 if (!iter.hidden_p ()
2905 && (TREE_STATIC (*iter) || DECL_EXTERNAL (*iter))
2906 && decls_match (*iter, decl))
2908 /* The standard only says that the local extern inherits
2909 linkage from the previous decl; in particular, default
2910 args are not shared. Add the decl into a hash table to
2911 make sure only the previous decl in this case is seen
2912 by the middle end. */
2913 struct cxx_int_tree_map *h;
2915 /* We inherit the outer decl's linkage. But we're a
2916 different decl. */
2917 TREE_PUBLIC (decl) = TREE_PUBLIC (*iter);
2919 if (cp_function_chain->extern_decl_map == NULL)
2920 cp_function_chain->extern_decl_map
2921 = hash_table<cxx_int_tree_map_hasher>::create_ggc (20);
2923 h = ggc_alloc<cxx_int_tree_map> ();
2924 h->uid = DECL_UID (decl);
2925 h->to = *iter;
2926 cxx_int_tree_map **loc = cp_function_chain->extern_decl_map
2927 ->find_slot (h, INSERT);
2928 *loc = h;
2929 break;
2933 if (TREE_PUBLIC (decl))
2935 /* DECL is externally visible. Make sure it matches a matching
2936 decl in the namespace scope. We only really need to check
2937 this when inserting the decl, not when we find an existing
2938 match in the current scope. However, in practice we're
2939 going to be inserting a new decl in the majority of cases --
2940 who writes multiple extern decls for the same thing in the
2941 same local scope? Doing it here often avoids a duplicate
2942 namespace lookup. */
2944 /* Avoid repeating a lookup. */
2945 if (ns_value == decl)
2946 ns_value = find_namespace_value (current_namespace, DECL_NAME (decl));
2948 if (ns_value == error_mark_node
2949 || (ns_value && TREE_CODE (ns_value) == TREE_LIST))
2950 ns_value = NULL_TREE;
2952 for (ovl_iterator iter (ns_value); iter; ++iter)
2954 tree other = *iter;
2956 if (!(TREE_PUBLIC (other) || DECL_EXTERNAL (other)))
2957 ; /* Not externally visible. */
2958 else if (DECL_EXTERN_C_P (decl) && DECL_EXTERN_C_P (other))
2959 ; /* Both are extern "C", we'll check via that mechanism. */
2960 else if (TREE_CODE (other) != TREE_CODE (decl)
2961 || ((VAR_P (decl) || matching_fn_p (other, decl))
2962 && !comptypes (TREE_TYPE (decl), TREE_TYPE (other),
2963 COMPARE_REDECLARATION)))
2965 auto_diagnostic_group d;
2966 if (permerror (DECL_SOURCE_LOCATION (decl),
2967 "local external declaration %q#D", decl))
2968 inform (DECL_SOURCE_LOCATION (other),
2969 "does not match previous declaration %q#D", other);
2970 break;
2976 /* Record DECL as belonging to the current lexical scope. Check for
2977 errors (such as an incompatible declaration for the same name
2978 already seen in the same scope). IS_FRIEND is true if DECL is
2979 declared as a friend.
2981 Returns either DECL or an old decl for the same name. If an old
2982 decl is returned, it may have been smashed to agree with what DECL
2983 says. */
2985 static tree
2986 do_pushdecl (tree decl, bool is_friend)
2988 if (decl == error_mark_node)
2989 return error_mark_node;
2991 if (!DECL_TEMPLATE_PARM_P (decl) && current_function_decl)
2992 set_decl_context_in_fn (current_function_decl, decl);
2994 /* The binding level we will be pushing into. During local class
2995 pushing, we want to push to the containing scope. */
2996 cp_binding_level *level = current_binding_level;
2997 while (level->kind == sk_class)
2998 level = level->level_chain;
3000 /* An anonymous namespace has a NULL DECL_NAME, but we still want to
3001 insert it. Other NULL-named decls, not so much. */
3002 tree name = DECL_NAME (decl);
3003 if (name || TREE_CODE (decl) == NAMESPACE_DECL)
3005 cxx_binding *binding = NULL; /* Local scope binding. */
3006 tree ns = NULL_TREE; /* Searched namespace. */
3007 tree *slot = NULL; /* Binding slot in namespace. */
3008 tree old = NULL_TREE;
3010 if (level->kind == sk_namespace)
3012 /* We look in the decl's namespace for an existing
3013 declaration, even though we push into the current
3014 namespace. */
3015 ns = (DECL_NAMESPACE_SCOPE_P (decl)
3016 ? CP_DECL_CONTEXT (decl) : current_namespace);
3017 /* Create the binding, if this is current namespace, because
3018 that's where we'll be pushing anyway. */
3019 slot = find_namespace_slot (ns, name, ns == current_namespace);
3020 if (slot)
3021 old = MAYBE_STAT_DECL (*slot);
3023 else
3025 binding = find_local_binding (level, name);
3026 if (binding)
3027 old = binding->value;
3030 if (current_function_decl && VAR_OR_FUNCTION_DECL_P (decl)
3031 && DECL_EXTERNAL (decl))
3032 set_local_extern_decl_linkage (decl, old != NULL_TREE);
3034 if (old == error_mark_node)
3035 old = NULL_TREE;
3037 for (ovl_iterator iter (old); iter; ++iter)
3038 if (iter.using_p ())
3039 ; /* Ignore using decls here. */
3040 else if (tree match = duplicate_decls (decl, *iter, is_friend))
3042 if (match == error_mark_node)
3044 else if (TREE_CODE (match) == TYPE_DECL)
3045 /* The IDENTIFIER will have the type referring to the
3046 now-smashed TYPE_DECL, because ...? Reset it. */
3047 SET_IDENTIFIER_TYPE_VALUE (name, TREE_TYPE (match));
3048 else if (iter.hidden_p () && !DECL_HIDDEN_P (match))
3050 /* Unhiding a previously hidden decl. */
3051 tree head = iter.reveal_node (old);
3052 if (head != old)
3054 if (!ns)
3056 update_local_overload (binding, head);
3057 binding->value = head;
3059 else if (STAT_HACK_P (*slot))
3060 STAT_DECL (*slot) = head;
3061 else
3062 *slot = head;
3064 if (DECL_EXTERN_C_P (match))
3065 /* We need to check and register the decl now. */
3066 check_extern_c_conflict (match);
3068 return match;
3071 /* We are pushing a new decl. */
3073 /* Skip a hidden builtin we failed to match already. There can
3074 only be one. */
3075 if (old && anticipated_builtin_p (old))
3076 old = OVL_CHAIN (old);
3078 check_template_shadow (decl);
3080 if (DECL_DECLARES_FUNCTION_P (decl))
3082 check_default_args (decl);
3084 if (is_friend)
3086 if (level->kind != sk_namespace)
3088 /* In a local class, a friend function declaration must
3089 find a matching decl in the innermost non-class scope.
3090 [class.friend/11] */
3091 error ("friend declaration %qD in local class without "
3092 "prior local declaration", decl);
3093 /* Don't attempt to push it. */
3094 return error_mark_node;
3096 /* Hide it from ordinary lookup. */
3097 DECL_ANTICIPATED (decl) = DECL_HIDDEN_FRIEND_P (decl) = true;
3101 if (level->kind != sk_namespace)
3103 check_local_shadow (decl);
3105 if (TREE_CODE (decl) == NAMESPACE_DECL)
3106 /* A local namespace alias. */
3107 set_identifier_type_value (name, NULL_TREE);
3109 if (!binding)
3110 binding = create_local_binding (level, name);
3112 else if (!slot)
3114 ns = current_namespace;
3115 slot = find_namespace_slot (ns, name, true);
3116 /* Update OLD to reflect the namespace we're going to be
3117 pushing into. */
3118 old = MAYBE_STAT_DECL (*slot);
3121 old = update_binding (level, binding, slot, old, decl, is_friend);
3123 if (old != decl)
3124 /* An existing decl matched, use it. */
3125 decl = old;
3126 else if (TREE_CODE (decl) == TYPE_DECL)
3128 tree type = TREE_TYPE (decl);
3130 if (type != error_mark_node)
3132 if (TYPE_NAME (type) != decl)
3133 set_underlying_type (decl);
3135 if (!ns)
3136 set_identifier_type_value_with_scope (name, decl, level);
3137 else
3138 SET_IDENTIFIER_TYPE_VALUE (name, global_type_node);
3141 /* If this is a locally defined typedef in a function that
3142 is not a template instantation, record it to implement
3143 -Wunused-local-typedefs. */
3144 if (!instantiating_current_function_p ())
3145 record_locally_defined_typedef (decl);
3147 else if (VAR_P (decl))
3148 maybe_register_incomplete_var (decl);
3150 if ((VAR_P (decl) || TREE_CODE (decl) == FUNCTION_DECL)
3151 && DECL_EXTERN_C_P (decl))
3152 check_extern_c_conflict (decl);
3154 else
3155 add_decl_to_level (level, decl);
3157 return decl;
3160 /* Record a decl-node X as belonging to the current lexical scope.
3161 It's a friend if IS_FRIEND is true -- which affects exactly where
3162 we push it. */
3164 tree
3165 pushdecl (tree x, bool is_friend)
3167 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3168 tree ret = do_pushdecl (x, is_friend);
3169 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3170 return ret;
3173 /* Enter DECL into the symbol table, if that's appropriate. Returns
3174 DECL, or a modified version thereof. */
3176 tree
3177 maybe_push_decl (tree decl)
3179 tree type = TREE_TYPE (decl);
3181 /* Add this decl to the current binding level, but not if it comes
3182 from another scope, e.g. a static member variable. TEM may equal
3183 DECL or it may be a previous decl of the same name. */
3184 if (decl == error_mark_node
3185 || (TREE_CODE (decl) != PARM_DECL
3186 && DECL_CONTEXT (decl) != NULL_TREE
3187 /* Definitions of namespace members outside their namespace are
3188 possible. */
3189 && !DECL_NAMESPACE_SCOPE_P (decl))
3190 || (TREE_CODE (decl) == TEMPLATE_DECL && !namespace_bindings_p ())
3191 || type == unknown_type_node
3192 /* The declaration of a template specialization does not affect
3193 the functions available for overload resolution, so we do not
3194 call pushdecl. */
3195 || (TREE_CODE (decl) == FUNCTION_DECL
3196 && DECL_TEMPLATE_SPECIALIZATION (decl)))
3197 return decl;
3198 else
3199 return pushdecl (decl);
3202 /* Bind DECL to ID in the current_binding_level, assumed to be a local
3203 binding level. If IS_USING is true, DECL got here through a
3204 using-declaration. */
3206 static void
3207 push_local_binding (tree id, tree decl, bool is_using)
3209 /* Skip over any local classes. This makes sense if we call
3210 push_local_binding with a friend decl of a local class. */
3211 cp_binding_level *b = innermost_nonclass_level ();
3213 gcc_assert (b->kind != sk_namespace);
3214 if (find_local_binding (b, id))
3216 /* Supplement the existing binding. */
3217 if (!supplement_binding (IDENTIFIER_BINDING (id), decl))
3218 /* It didn't work. Something else must be bound at this
3219 level. Do not add DECL to the list of things to pop
3220 later. */
3221 return;
3223 else
3224 /* Create a new binding. */
3225 push_binding (id, decl, b);
3227 if (TREE_CODE (decl) == OVERLOAD || is_using)
3228 /* We must put the OVERLOAD or using into a TREE_LIST since we
3229 cannot use the decl's chain itself. */
3230 decl = build_tree_list (NULL_TREE, decl);
3232 /* And put DECL on the list of things declared by the current
3233 binding level. */
3234 add_decl_to_level (b, decl);
3238 /* true means unconditionally make a BLOCK for the next level pushed. */
3240 static bool keep_next_level_flag;
3242 static int binding_depth = 0;
3244 static void
3245 indent (int depth)
3247 int i;
3249 for (i = 0; i < depth * 2; i++)
3250 putc (' ', stderr);
3253 /* Return a string describing the kind of SCOPE we have. */
3254 static const char *
3255 cp_binding_level_descriptor (cp_binding_level *scope)
3257 /* The order of this table must match the "scope_kind"
3258 enumerators. */
3259 static const char* scope_kind_names[] = {
3260 "block-scope",
3261 "cleanup-scope",
3262 "try-scope",
3263 "catch-scope",
3264 "for-scope",
3265 "function-parameter-scope",
3266 "class-scope",
3267 "namespace-scope",
3268 "template-parameter-scope",
3269 "template-explicit-spec-scope"
3271 const scope_kind kind = scope->explicit_spec_p
3272 ? sk_template_spec : scope->kind;
3274 return scope_kind_names[kind];
3277 /* Output a debugging information about SCOPE when performing
3278 ACTION at LINE. */
3279 static void
3280 cp_binding_level_debug (cp_binding_level *scope, int line, const char *action)
3282 const char *desc = cp_binding_level_descriptor (scope);
3283 if (scope->this_entity)
3284 verbatim ("%s %<%s(%E)%> %p %d\n", action, desc,
3285 scope->this_entity, (void *) scope, line);
3286 else
3287 verbatim ("%s %s %p %d\n", action, desc, (void *) scope, line);
3290 /* A chain of binding_level structures awaiting reuse. */
3292 static GTY((deletable)) cp_binding_level *free_binding_level;
3294 /* Insert SCOPE as the innermost binding level. */
3296 void
3297 push_binding_level (cp_binding_level *scope)
3299 /* Add it to the front of currently active scopes stack. */
3300 scope->level_chain = current_binding_level;
3301 current_binding_level = scope;
3302 keep_next_level_flag = false;
3304 if (ENABLE_SCOPE_CHECKING)
3306 scope->binding_depth = binding_depth;
3307 indent (binding_depth);
3308 cp_binding_level_debug (scope, LOCATION_LINE (input_location),
3309 "push");
3310 binding_depth++;
3314 /* Create a new KIND scope and make it the top of the active scopes stack.
3315 ENTITY is the scope of the associated C++ entity (namespace, class,
3316 function, C++0x enumeration); it is NULL otherwise. */
3318 cp_binding_level *
3319 begin_scope (scope_kind kind, tree entity)
3321 cp_binding_level *scope;
3323 /* Reuse or create a struct for this binding level. */
3324 if (!ENABLE_SCOPE_CHECKING && free_binding_level)
3326 scope = free_binding_level;
3327 free_binding_level = scope->level_chain;
3328 memset (scope, 0, sizeof (cp_binding_level));
3330 else
3331 scope = ggc_cleared_alloc<cp_binding_level> ();
3333 scope->this_entity = entity;
3334 scope->more_cleanups_ok = true;
3335 switch (kind)
3337 case sk_cleanup:
3338 scope->keep = true;
3339 break;
3341 case sk_template_spec:
3342 scope->explicit_spec_p = true;
3343 kind = sk_template_parms;
3344 /* Fall through. */
3345 case sk_template_parms:
3346 case sk_block:
3347 case sk_try:
3348 case sk_catch:
3349 case sk_for:
3350 case sk_cond:
3351 case sk_class:
3352 case sk_scoped_enum:
3353 case sk_function_parms:
3354 case sk_transaction:
3355 case sk_omp:
3356 scope->keep = keep_next_level_flag;
3357 break;
3359 case sk_namespace:
3360 NAMESPACE_LEVEL (entity) = scope;
3361 break;
3363 default:
3364 /* Should not happen. */
3365 gcc_unreachable ();
3366 break;
3368 scope->kind = kind;
3370 push_binding_level (scope);
3372 return scope;
3375 /* We're about to leave current scope. Pop the top of the stack of
3376 currently active scopes. Return the enclosing scope, now active. */
3378 cp_binding_level *
3379 leave_scope (void)
3381 cp_binding_level *scope = current_binding_level;
3383 if (scope->kind == sk_namespace && class_binding_level)
3384 current_binding_level = class_binding_level;
3386 /* We cannot leave a scope, if there are none left. */
3387 if (NAMESPACE_LEVEL (global_namespace))
3388 gcc_assert (!global_scope_p (scope));
3390 if (ENABLE_SCOPE_CHECKING)
3392 indent (--binding_depth);
3393 cp_binding_level_debug (scope, LOCATION_LINE (input_location),
3394 "leave");
3397 /* Move one nesting level up. */
3398 current_binding_level = scope->level_chain;
3400 /* Namespace-scopes are left most probably temporarily, not
3401 completely; they can be reopened later, e.g. in namespace-extension
3402 or any name binding activity that requires us to resume a
3403 namespace. For classes, we cache some binding levels. For other
3404 scopes, we just make the structure available for reuse. */
3405 if (scope->kind != sk_namespace
3406 && scope->kind != sk_class)
3408 scope->level_chain = free_binding_level;
3409 gcc_assert (!ENABLE_SCOPE_CHECKING
3410 || scope->binding_depth == binding_depth);
3411 free_binding_level = scope;
3414 if (scope->kind == sk_class)
3416 /* Reset DEFINING_CLASS_P to allow for reuse of a
3417 class-defining scope in a non-defining context. */
3418 scope->defining_class_p = 0;
3420 /* Find the innermost enclosing class scope, and reset
3421 CLASS_BINDING_LEVEL appropriately. */
3422 class_binding_level = NULL;
3423 for (scope = current_binding_level; scope; scope = scope->level_chain)
3424 if (scope->kind == sk_class)
3426 class_binding_level = scope;
3427 break;
3431 return current_binding_level;
3434 static void
3435 resume_scope (cp_binding_level* b)
3437 /* Resuming binding levels is meant only for namespaces,
3438 and those cannot nest into classes. */
3439 gcc_assert (!class_binding_level);
3440 /* Also, resuming a non-directly nested namespace is a no-no. */
3441 gcc_assert (b->level_chain == current_binding_level);
3442 current_binding_level = b;
3443 if (ENABLE_SCOPE_CHECKING)
3445 b->binding_depth = binding_depth;
3446 indent (binding_depth);
3447 cp_binding_level_debug (b, LOCATION_LINE (input_location), "resume");
3448 binding_depth++;
3452 /* Return the innermost binding level that is not for a class scope. */
3454 static cp_binding_level *
3455 innermost_nonclass_level (void)
3457 cp_binding_level *b;
3459 b = current_binding_level;
3460 while (b->kind == sk_class)
3461 b = b->level_chain;
3463 return b;
3466 /* We're defining an object of type TYPE. If it needs a cleanup, but
3467 we're not allowed to add any more objects with cleanups to the current
3468 scope, create a new binding level. */
3470 void
3471 maybe_push_cleanup_level (tree type)
3473 if (type != error_mark_node
3474 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
3475 && current_binding_level->more_cleanups_ok == 0)
3477 begin_scope (sk_cleanup, NULL);
3478 current_binding_level->statement_list = push_stmt_list ();
3482 /* Return true if we are in the global binding level. */
3484 bool
3485 global_bindings_p (void)
3487 return global_scope_p (current_binding_level);
3490 /* True if we are currently in a toplevel binding level. This
3491 means either the global binding level or a namespace in a toplevel
3492 binding level. Since there are no non-toplevel namespace levels,
3493 this really means any namespace or template parameter level. We
3494 also include a class whose context is toplevel. */
3496 bool
3497 toplevel_bindings_p (void)
3499 cp_binding_level *b = innermost_nonclass_level ();
3501 return b->kind == sk_namespace || b->kind == sk_template_parms;
3504 /* True if this is a namespace scope, or if we are defining a class
3505 which is itself at namespace scope, or whose enclosing class is
3506 such a class, etc. */
3508 bool
3509 namespace_bindings_p (void)
3511 cp_binding_level *b = innermost_nonclass_level ();
3513 return b->kind == sk_namespace;
3516 /* True if the innermost non-class scope is a block scope. */
3518 bool
3519 local_bindings_p (void)
3521 cp_binding_level *b = innermost_nonclass_level ();
3522 return b->kind < sk_function_parms || b->kind == sk_omp;
3525 /* True if the current level needs to have a BLOCK made. */
3527 bool
3528 kept_level_p (void)
3530 return (current_binding_level->blocks != NULL_TREE
3531 || current_binding_level->keep
3532 || current_binding_level->kind == sk_cleanup
3533 || current_binding_level->names != NULL_TREE
3534 || current_binding_level->using_directives);
3537 /* Returns the kind of the innermost scope. */
3539 scope_kind
3540 innermost_scope_kind (void)
3542 return current_binding_level->kind;
3545 /* Returns true if this scope was created to store template parameters. */
3547 bool
3548 template_parm_scope_p (void)
3550 return innermost_scope_kind () == sk_template_parms;
3553 /* If KEEP is true, make a BLOCK node for the next binding level,
3554 unconditionally. Otherwise, use the normal logic to decide whether
3555 or not to create a BLOCK. */
3557 void
3558 keep_next_level (bool keep)
3560 keep_next_level_flag = keep;
3563 /* Return the list of declarations of the current local scope. */
3565 tree
3566 get_local_decls (void)
3568 gcc_assert (current_binding_level->kind != sk_namespace
3569 && current_binding_level->kind != sk_class);
3570 return current_binding_level->names;
3573 /* Return how many function prototypes we are currently nested inside. */
3576 function_parm_depth (void)
3578 int level = 0;
3579 cp_binding_level *b;
3581 for (b = current_binding_level;
3582 b->kind == sk_function_parms;
3583 b = b->level_chain)
3584 ++level;
3586 return level;
3589 /* For debugging. */
3590 static int no_print_functions = 0;
3591 static int no_print_builtins = 0;
3593 static void
3594 print_binding_level (cp_binding_level* lvl)
3596 tree t;
3597 int i = 0, len;
3598 fprintf (stderr, " blocks=%p", (void *) lvl->blocks);
3599 if (lvl->more_cleanups_ok)
3600 fprintf (stderr, " more-cleanups-ok");
3601 if (lvl->have_cleanups)
3602 fprintf (stderr, " have-cleanups");
3603 fprintf (stderr, "\n");
3604 if (lvl->names)
3606 fprintf (stderr, " names:\t");
3607 /* We can probably fit 3 names to a line? */
3608 for (t = lvl->names; t; t = TREE_CHAIN (t))
3610 if (no_print_functions && (TREE_CODE (t) == FUNCTION_DECL))
3611 continue;
3612 if (no_print_builtins
3613 && (TREE_CODE (t) == TYPE_DECL)
3614 && DECL_IS_BUILTIN (t))
3615 continue;
3617 /* Function decls tend to have longer names. */
3618 if (TREE_CODE (t) == FUNCTION_DECL)
3619 len = 3;
3620 else
3621 len = 2;
3622 i += len;
3623 if (i > 6)
3625 fprintf (stderr, "\n\t");
3626 i = len;
3628 print_node_brief (stderr, "", t, 0);
3629 if (t == error_mark_node)
3630 break;
3632 if (i)
3633 fprintf (stderr, "\n");
3635 if (vec_safe_length (lvl->class_shadowed))
3637 size_t i;
3638 cp_class_binding *b;
3639 fprintf (stderr, " class-shadowed:");
3640 FOR_EACH_VEC_ELT (*lvl->class_shadowed, i, b)
3641 fprintf (stderr, " %s ", IDENTIFIER_POINTER (b->identifier));
3642 fprintf (stderr, "\n");
3644 if (lvl->type_shadowed)
3646 fprintf (stderr, " type-shadowed:");
3647 for (t = lvl->type_shadowed; t; t = TREE_CHAIN (t))
3649 fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
3651 fprintf (stderr, "\n");
3655 DEBUG_FUNCTION void
3656 debug (cp_binding_level &ref)
3658 print_binding_level (&ref);
3661 DEBUG_FUNCTION void
3662 debug (cp_binding_level *ptr)
3664 if (ptr)
3665 debug (*ptr);
3666 else
3667 fprintf (stderr, "<nil>\n");
3671 static void
3672 print_other_binding_stack (cp_binding_level *stack)
3674 cp_binding_level *level;
3675 for (level = stack; !global_scope_p (level); level = level->level_chain)
3677 fprintf (stderr, "binding level %p\n", (void *) level);
3678 print_binding_level (level);
3682 void
3683 print_binding_stack (void)
3685 cp_binding_level *b;
3686 fprintf (stderr, "current_binding_level=%p\n"
3687 "class_binding_level=%p\n"
3688 "NAMESPACE_LEVEL (global_namespace)=%p\n",
3689 (void *) current_binding_level, (void *) class_binding_level,
3690 (void *) NAMESPACE_LEVEL (global_namespace));
3691 if (class_binding_level)
3693 for (b = class_binding_level; b; b = b->level_chain)
3694 if (b == current_binding_level)
3695 break;
3696 if (b)
3697 b = class_binding_level;
3698 else
3699 b = current_binding_level;
3701 else
3702 b = current_binding_level;
3703 print_other_binding_stack (b);
3704 fprintf (stderr, "global:\n");
3705 print_binding_level (NAMESPACE_LEVEL (global_namespace));
3708 /* Return the type associated with ID. */
3710 static tree
3711 identifier_type_value_1 (tree id)
3713 /* There is no type with that name, anywhere. */
3714 if (REAL_IDENTIFIER_TYPE_VALUE (id) == NULL_TREE)
3715 return NULL_TREE;
3716 /* This is not the type marker, but the real thing. */
3717 if (REAL_IDENTIFIER_TYPE_VALUE (id) != global_type_node)
3718 return REAL_IDENTIFIER_TYPE_VALUE (id);
3719 /* Have to search for it. It must be on the global level, now.
3720 Ask lookup_name not to return non-types. */
3721 id = lookup_name_real (id, 2, 1, /*block_p=*/true, 0, 0);
3722 if (id)
3723 return TREE_TYPE (id);
3724 return NULL_TREE;
3727 /* Wrapper for identifier_type_value_1. */
3729 tree
3730 identifier_type_value (tree id)
3732 tree ret;
3733 timevar_start (TV_NAME_LOOKUP);
3734 ret = identifier_type_value_1 (id);
3735 timevar_stop (TV_NAME_LOOKUP);
3736 return ret;
3739 /* Push a definition of struct, union or enum tag named ID. into
3740 binding_level B. DECL is a TYPE_DECL for the type. We assume that
3741 the tag ID is not already defined. */
3743 static void
3744 set_identifier_type_value_with_scope (tree id, tree decl, cp_binding_level *b)
3746 tree type;
3748 if (b->kind != sk_namespace)
3750 /* Shadow the marker, not the real thing, so that the marker
3751 gets restored later. */
3752 tree old_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
3753 b->type_shadowed
3754 = tree_cons (id, old_type_value, b->type_shadowed);
3755 type = decl ? TREE_TYPE (decl) : NULL_TREE;
3756 TREE_TYPE (b->type_shadowed) = type;
3758 else
3760 tree *slot = find_namespace_slot (current_namespace, id, true);
3761 gcc_assert (decl);
3762 update_binding (b, NULL, slot, MAYBE_STAT_DECL (*slot), decl, false);
3764 /* Store marker instead of real type. */
3765 type = global_type_node;
3767 SET_IDENTIFIER_TYPE_VALUE (id, type);
3770 /* As set_identifier_type_value_with_scope, but using
3771 current_binding_level. */
3773 void
3774 set_identifier_type_value (tree id, tree decl)
3776 set_identifier_type_value_with_scope (id, decl, current_binding_level);
3779 /* Return the name for the constructor (or destructor) for the
3780 specified class. */
3782 tree
3783 constructor_name (tree type)
3785 tree decl = TYPE_NAME (TYPE_MAIN_VARIANT (type));
3787 return decl ? DECL_NAME (decl) : NULL_TREE;
3790 /* Returns TRUE if NAME is the name for the constructor for TYPE,
3791 which must be a class type. */
3793 bool
3794 constructor_name_p (tree name, tree type)
3796 gcc_assert (MAYBE_CLASS_TYPE_P (type));
3798 /* These don't have names. */
3799 if (TREE_CODE (type) == DECLTYPE_TYPE
3800 || TREE_CODE (type) == TYPEOF_TYPE)
3801 return false;
3803 if (name && name == constructor_name (type))
3804 return true;
3806 return false;
3809 /* Counter used to create anonymous type names. */
3811 static GTY(()) int anon_cnt;
3813 /* Return an IDENTIFIER which can be used as a name for
3814 unnamed structs and unions. */
3816 tree
3817 make_anon_name (void)
3819 char buf[32];
3821 sprintf (buf, anon_aggrname_format (), anon_cnt++);
3822 return get_identifier (buf);
3825 /* This code is practically identical to that for creating
3826 anonymous names, but is just used for lambdas instead. This isn't really
3827 necessary, but it's convenient to avoid treating lambdas like other
3828 unnamed types. */
3830 static GTY(()) int lambda_cnt = 0;
3832 tree
3833 make_lambda_name (void)
3835 char buf[32];
3837 sprintf (buf, LAMBDANAME_FORMAT, lambda_cnt++);
3838 return get_identifier (buf);
3841 /* Insert another USING_DECL into the current binding level, returning
3842 this declaration. If this is a redeclaration, do nothing, and
3843 return NULL_TREE if this not in namespace scope (in namespace
3844 scope, a using decl might extend any previous bindings). */
3846 static tree
3847 push_using_decl_1 (tree scope, tree name)
3849 tree decl;
3851 gcc_assert (TREE_CODE (scope) == NAMESPACE_DECL);
3852 gcc_assert (identifier_p (name));
3853 for (decl = current_binding_level->usings; decl; decl = DECL_CHAIN (decl))
3854 if (USING_DECL_SCOPE (decl) == scope && DECL_NAME (decl) == name)
3855 break;
3856 if (decl)
3857 return namespace_bindings_p () ? decl : NULL_TREE;
3858 decl = build_lang_decl (USING_DECL, name, NULL_TREE);
3859 USING_DECL_SCOPE (decl) = scope;
3860 DECL_CHAIN (decl) = current_binding_level->usings;
3861 current_binding_level->usings = decl;
3862 return decl;
3865 /* Wrapper for push_using_decl_1. */
3867 static tree
3868 push_using_decl (tree scope, tree name)
3870 tree ret;
3871 timevar_start (TV_NAME_LOOKUP);
3872 ret = push_using_decl_1 (scope, name);
3873 timevar_stop (TV_NAME_LOOKUP);
3874 return ret;
3877 /* Same as pushdecl, but define X in binding-level LEVEL. We rely on the
3878 caller to set DECL_CONTEXT properly.
3880 Note that this must only be used when X will be the new innermost
3881 binding for its name, as we tack it onto the front of IDENTIFIER_BINDING
3882 without checking to see if the current IDENTIFIER_BINDING comes from a
3883 closer binding level than LEVEL. */
3885 static tree
3886 do_pushdecl_with_scope (tree x, cp_binding_level *level, bool is_friend)
3888 cp_binding_level *b;
3890 if (level->kind == sk_class)
3892 b = class_binding_level;
3893 class_binding_level = level;
3894 pushdecl_class_level (x);
3895 class_binding_level = b;
3897 else
3899 tree function_decl = current_function_decl;
3900 if (level->kind == sk_namespace)
3901 current_function_decl = NULL_TREE;
3902 b = current_binding_level;
3903 current_binding_level = level;
3904 x = pushdecl (x, is_friend);
3905 current_binding_level = b;
3906 current_function_decl = function_decl;
3908 return x;
3911 /* Inject X into the local scope just before the function parms. */
3913 tree
3914 pushdecl_outermost_localscope (tree x)
3916 cp_binding_level *b = NULL;
3917 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3919 /* Find the scope just inside the function parms. */
3920 for (cp_binding_level *n = current_binding_level;
3921 n->kind != sk_function_parms; n = b->level_chain)
3922 b = n;
3924 tree ret = b ? do_pushdecl_with_scope (x, b, false) : error_mark_node;
3925 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3927 return ret;
3930 /* Check a non-member using-declaration. Return the name and scope
3931 being used, and the USING_DECL, or NULL_TREE on failure. */
3933 static tree
3934 validate_nonmember_using_decl (tree decl, tree scope, tree name)
3936 /* [namespace.udecl]
3937 A using-declaration for a class member shall be a
3938 member-declaration. */
3939 if (TYPE_P (scope))
3941 error ("%qT is not a namespace or unscoped enum", scope);
3942 return NULL_TREE;
3944 else if (scope == error_mark_node)
3945 return NULL_TREE;
3947 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR)
3949 /* 7.3.3/5
3950 A using-declaration shall not name a template-id. */
3951 error ("a using-declaration cannot specify a template-id. "
3952 "Try %<using %D%>", name);
3953 return NULL_TREE;
3956 if (TREE_CODE (decl) == NAMESPACE_DECL)
3958 error ("namespace %qD not allowed in using-declaration", decl);
3959 return NULL_TREE;
3962 if (TREE_CODE (decl) == SCOPE_REF)
3964 /* It's a nested name with template parameter dependent scope.
3965 This can only be using-declaration for class member. */
3966 error ("%qT is not a namespace", TREE_OPERAND (decl, 0));
3967 return NULL_TREE;
3970 decl = OVL_FIRST (decl);
3972 /* Make a USING_DECL. */
3973 tree using_decl = push_using_decl (scope, name);
3975 if (using_decl == NULL_TREE
3976 && at_function_scope_p ()
3977 && VAR_P (decl))
3978 /* C++11 7.3.3/10. */
3979 error ("%qD is already declared in this scope", name);
3981 return using_decl;
3984 /* Process a local-scope or namespace-scope using declaration. SCOPE
3985 is the nominated scope to search for NAME. VALUE_P and TYPE_P
3986 point to the binding for NAME in the current scope and are
3987 updated. */
3989 static void
3990 do_nonmember_using_decl (tree scope, tree name, tree *value_p, tree *type_p)
3992 name_lookup lookup (name, 0);
3994 if (!qualified_namespace_lookup (scope, &lookup))
3996 error ("%qD not declared", name);
3997 return;
3999 else if (TREE_CODE (lookup.value) == TREE_LIST)
4001 error ("reference to %qD is ambiguous", name);
4002 print_candidates (lookup.value);
4003 lookup.value = NULL_TREE;
4006 if (lookup.type && TREE_CODE (lookup.type) == TREE_LIST)
4008 error ("reference to %qD is ambiguous", name);
4009 print_candidates (lookup.type);
4010 lookup.type = NULL_TREE;
4013 tree value = *value_p;
4014 tree type = *type_p;
4016 /* Shift the old and new bindings around so we're comparing class and
4017 enumeration names to each other. */
4018 if (value && DECL_IMPLICIT_TYPEDEF_P (value))
4020 type = value;
4021 value = NULL_TREE;
4024 if (lookup.value && DECL_IMPLICIT_TYPEDEF_P (lookup.value))
4026 lookup.type = lookup.value;
4027 lookup.value = NULL_TREE;
4030 if (lookup.value && lookup.value != value)
4032 /* Check for using functions. */
4033 if (OVL_P (lookup.value) && (!value || OVL_P (value)))
4035 for (lkp_iterator usings (lookup.value); usings; ++usings)
4037 tree new_fn = *usings;
4039 /* [namespace.udecl]
4041 If a function declaration in namespace scope or block
4042 scope has the same name and the same parameter types as a
4043 function introduced by a using declaration the program is
4044 ill-formed. */
4045 bool found = false;
4046 for (ovl_iterator old (value); !found && old; ++old)
4048 tree old_fn = *old;
4050 if (new_fn == old_fn)
4051 /* The function already exists in the current
4052 namespace. */
4053 found = true;
4054 else if (old.using_p ())
4055 continue; /* This is a using decl. */
4056 else if (old.hidden_p () && !DECL_HIDDEN_FRIEND_P (old_fn))
4057 continue; /* This is an anticipated builtin. */
4058 else if (!matching_fn_p (new_fn, old_fn))
4059 continue; /* Parameters do not match. */
4060 else if (decls_match (new_fn, old_fn))
4061 found = true;
4062 else
4064 diagnose_name_conflict (new_fn, old_fn);
4065 found = true;
4069 if (!found)
4070 /* Unlike the overload case we don't drop anticipated
4071 builtins here. They don't cause a problem, and
4072 we'd like to match them with a future
4073 declaration. */
4074 value = ovl_insert (new_fn, value, true);
4077 else if (value
4078 /* Ignore anticipated builtins. */
4079 && !anticipated_builtin_p (value)
4080 && !decls_match (lookup.value, value))
4081 diagnose_name_conflict (lookup.value, value);
4082 else
4083 value = lookup.value;
4086 if (lookup.type && lookup.type != type)
4088 if (type && !decls_match (lookup.type, type))
4089 diagnose_name_conflict (lookup.type, type);
4090 else
4091 type = lookup.type;
4094 /* If bind->value is empty, shift any class or enumeration name back. */
4095 if (!value)
4097 value = type;
4098 type = NULL_TREE;
4101 *value_p = value;
4102 *type_p = type;
4105 /* Returns true if ANCESTOR encloses DESCENDANT, including matching.
4106 Both are namespaces. */
4108 bool
4109 is_nested_namespace (tree ancestor, tree descendant, bool inline_only)
4111 int depth = SCOPE_DEPTH (ancestor);
4113 if (!depth && !inline_only)
4114 /* The global namespace encloses everything. */
4115 return true;
4117 while (SCOPE_DEPTH (descendant) > depth
4118 && (!inline_only || DECL_NAMESPACE_INLINE_P (descendant)))
4119 descendant = CP_DECL_CONTEXT (descendant);
4121 return ancestor == descendant;
4124 /* Returns true if ROOT (a namespace, class, or function) encloses
4125 CHILD. CHILD may be either a class type or a namespace. */
4127 bool
4128 is_ancestor (tree root, tree child)
4130 gcc_assert ((TREE_CODE (root) == NAMESPACE_DECL
4131 || TREE_CODE (root) == FUNCTION_DECL
4132 || CLASS_TYPE_P (root)));
4133 gcc_assert ((TREE_CODE (child) == NAMESPACE_DECL
4134 || CLASS_TYPE_P (child)));
4136 /* The global namespace encloses everything. */
4137 if (root == global_namespace)
4138 return true;
4140 /* Search until we reach namespace scope. */
4141 while (TREE_CODE (child) != NAMESPACE_DECL)
4143 /* If we've reached the ROOT, it encloses CHILD. */
4144 if (root == child)
4145 return true;
4146 /* Go out one level. */
4147 if (TYPE_P (child))
4148 child = TYPE_NAME (child);
4149 child = CP_DECL_CONTEXT (child);
4152 if (TREE_CODE (root) == NAMESPACE_DECL)
4153 return is_nested_namespace (root, child);
4155 return false;
4158 /* Enter the class or namespace scope indicated by T suitable for name
4159 lookup. T can be arbitrary scope, not necessary nested inside the
4160 current scope. Returns a non-null scope to pop iff pop_scope
4161 should be called later to exit this scope. */
4163 tree
4164 push_scope (tree t)
4166 if (TREE_CODE (t) == NAMESPACE_DECL)
4167 push_decl_namespace (t);
4168 else if (CLASS_TYPE_P (t))
4170 if (!at_class_scope_p ()
4171 || !same_type_p (current_class_type, t))
4172 push_nested_class (t);
4173 else
4174 /* T is the same as the current scope. There is therefore no
4175 need to re-enter the scope. Since we are not actually
4176 pushing a new scope, our caller should not call
4177 pop_scope. */
4178 t = NULL_TREE;
4181 return t;
4184 /* Leave scope pushed by push_scope. */
4186 void
4187 pop_scope (tree t)
4189 if (t == NULL_TREE)
4190 return;
4191 if (TREE_CODE (t) == NAMESPACE_DECL)
4192 pop_decl_namespace ();
4193 else if CLASS_TYPE_P (t)
4194 pop_nested_class ();
4197 /* Subroutine of push_inner_scope. */
4199 static void
4200 push_inner_scope_r (tree outer, tree inner)
4202 tree prev;
4204 if (outer == inner
4205 || (TREE_CODE (inner) != NAMESPACE_DECL && !CLASS_TYPE_P (inner)))
4206 return;
4208 prev = CP_DECL_CONTEXT (TREE_CODE (inner) == NAMESPACE_DECL ? inner : TYPE_NAME (inner));
4209 if (outer != prev)
4210 push_inner_scope_r (outer, prev);
4211 if (TREE_CODE (inner) == NAMESPACE_DECL)
4213 cp_binding_level *save_template_parm = 0;
4214 /* Temporary take out template parameter scopes. They are saved
4215 in reversed order in save_template_parm. */
4216 while (current_binding_level->kind == sk_template_parms)
4218 cp_binding_level *b = current_binding_level;
4219 current_binding_level = b->level_chain;
4220 b->level_chain = save_template_parm;
4221 save_template_parm = b;
4224 resume_scope (NAMESPACE_LEVEL (inner));
4225 current_namespace = inner;
4227 /* Restore template parameter scopes. */
4228 while (save_template_parm)
4230 cp_binding_level *b = save_template_parm;
4231 save_template_parm = b->level_chain;
4232 b->level_chain = current_binding_level;
4233 current_binding_level = b;
4236 else
4237 pushclass (inner);
4240 /* Enter the scope INNER from current scope. INNER must be a scope
4241 nested inside current scope. This works with both name lookup and
4242 pushing name into scope. In case a template parameter scope is present,
4243 namespace is pushed under the template parameter scope according to
4244 name lookup rule in 14.6.1/6.
4246 Return the former current scope suitable for pop_inner_scope. */
4248 tree
4249 push_inner_scope (tree inner)
4251 tree outer = current_scope ();
4252 if (!outer)
4253 outer = current_namespace;
4255 push_inner_scope_r (outer, inner);
4256 return outer;
4259 /* Exit the current scope INNER back to scope OUTER. */
4261 void
4262 pop_inner_scope (tree outer, tree inner)
4264 if (outer == inner
4265 || (TREE_CODE (inner) != NAMESPACE_DECL && !CLASS_TYPE_P (inner)))
4266 return;
4268 while (outer != inner)
4270 if (TREE_CODE (inner) == NAMESPACE_DECL)
4272 cp_binding_level *save_template_parm = 0;
4273 /* Temporary take out template parameter scopes. They are saved
4274 in reversed order in save_template_parm. */
4275 while (current_binding_level->kind == sk_template_parms)
4277 cp_binding_level *b = current_binding_level;
4278 current_binding_level = b->level_chain;
4279 b->level_chain = save_template_parm;
4280 save_template_parm = b;
4283 pop_namespace ();
4285 /* Restore template parameter scopes. */
4286 while (save_template_parm)
4288 cp_binding_level *b = save_template_parm;
4289 save_template_parm = b->level_chain;
4290 b->level_chain = current_binding_level;
4291 current_binding_level = b;
4294 else
4295 popclass ();
4297 inner = CP_DECL_CONTEXT (TREE_CODE (inner) == NAMESPACE_DECL ? inner : TYPE_NAME (inner));
4301 /* Do a pushlevel for class declarations. */
4303 void
4304 pushlevel_class (void)
4306 class_binding_level = begin_scope (sk_class, current_class_type);
4309 /* ...and a poplevel for class declarations. */
4311 void
4312 poplevel_class (void)
4314 cp_binding_level *level = class_binding_level;
4315 cp_class_binding *cb;
4316 size_t i;
4317 tree shadowed;
4319 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4320 gcc_assert (level != 0);
4322 /* If we're leaving a toplevel class, cache its binding level. */
4323 if (current_class_depth == 1)
4324 previous_class_level = level;
4325 for (shadowed = level->type_shadowed;
4326 shadowed;
4327 shadowed = TREE_CHAIN (shadowed))
4328 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (shadowed), TREE_VALUE (shadowed));
4330 /* Remove the bindings for all of the class-level declarations. */
4331 if (level->class_shadowed)
4333 FOR_EACH_VEC_ELT (*level->class_shadowed, i, cb)
4335 IDENTIFIER_BINDING (cb->identifier) = cb->base->previous;
4336 cxx_binding_free (cb->base);
4338 ggc_free (level->class_shadowed);
4339 level->class_shadowed = NULL;
4342 /* Now, pop out of the binding level which we created up in the
4343 `pushlevel_class' routine. */
4344 gcc_assert (current_binding_level == level);
4345 leave_scope ();
4346 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4349 /* Set INHERITED_VALUE_BINDING_P on BINDING to true or false, as
4350 appropriate. DECL is the value to which a name has just been
4351 bound. CLASS_TYPE is the class in which the lookup occurred. */
4353 static void
4354 set_inherited_value_binding_p (cxx_binding *binding, tree decl,
4355 tree class_type)
4357 if (binding->value == decl && TREE_CODE (decl) != TREE_LIST)
4359 tree context;
4361 if (TREE_CODE (decl) == OVERLOAD)
4362 context = ovl_scope (decl);
4363 else
4365 gcc_assert (DECL_P (decl));
4366 context = context_for_name_lookup (decl);
4369 if (is_properly_derived_from (class_type, context))
4370 INHERITED_VALUE_BINDING_P (binding) = 1;
4371 else
4372 INHERITED_VALUE_BINDING_P (binding) = 0;
4374 else if (binding->value == decl)
4375 /* We only encounter a TREE_LIST when there is an ambiguity in the
4376 base classes. Such an ambiguity can be overridden by a
4377 definition in this class. */
4378 INHERITED_VALUE_BINDING_P (binding) = 1;
4379 else
4380 INHERITED_VALUE_BINDING_P (binding) = 0;
4383 /* Make the declaration of X appear in CLASS scope. */
4385 bool
4386 pushdecl_class_level (tree x)
4388 bool is_valid = true;
4389 bool subtime;
4391 /* Do nothing if we're adding to an outer lambda closure type,
4392 outer_binding will add it later if it's needed. */
4393 if (current_class_type != class_binding_level->this_entity)
4394 return true;
4396 subtime = timevar_cond_start (TV_NAME_LOOKUP);
4397 /* Get the name of X. */
4398 tree name = OVL_NAME (x);
4400 if (name)
4402 is_valid = push_class_level_binding (name, x);
4403 if (TREE_CODE (x) == TYPE_DECL)
4404 set_identifier_type_value (name, x);
4406 else if (ANON_AGGR_TYPE_P (TREE_TYPE (x)))
4408 /* If X is an anonymous aggregate, all of its members are
4409 treated as if they were members of the class containing the
4410 aggregate, for naming purposes. */
4411 location_t save_location = input_location;
4412 tree anon = TREE_TYPE (x);
4413 if (vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (anon))
4414 for (unsigned ix = member_vec->length (); ix--;)
4416 tree binding = (*member_vec)[ix];
4417 if (STAT_HACK_P (binding))
4419 if (!pushdecl_class_level (STAT_TYPE (binding)))
4420 is_valid = false;
4421 binding = STAT_DECL (binding);
4423 if (!pushdecl_class_level (binding))
4424 is_valid = false;
4426 else
4427 for (tree f = TYPE_FIELDS (anon); f; f = DECL_CHAIN (f))
4428 if (TREE_CODE (f) == FIELD_DECL)
4430 input_location = DECL_SOURCE_LOCATION (f);
4431 if (!pushdecl_class_level (f))
4432 is_valid = false;
4434 input_location = save_location;
4436 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4437 return is_valid;
4440 /* Return the BINDING (if any) for NAME in SCOPE, which is a class
4441 scope. If the value returned is non-NULL, and the PREVIOUS field
4442 is not set, callers must set the PREVIOUS field explicitly. */
4444 static cxx_binding *
4445 get_class_binding (tree name, cp_binding_level *scope)
4447 tree class_type;
4448 tree type_binding;
4449 tree value_binding;
4450 cxx_binding *binding;
4452 class_type = scope->this_entity;
4454 /* Get the type binding. */
4455 type_binding = lookup_member (class_type, name,
4456 /*protect=*/2, /*want_type=*/true,
4457 tf_warning_or_error);
4458 /* Get the value binding. */
4459 value_binding = lookup_member (class_type, name,
4460 /*protect=*/2, /*want_type=*/false,
4461 tf_warning_or_error);
4463 if (value_binding
4464 && (TREE_CODE (value_binding) == TYPE_DECL
4465 || DECL_CLASS_TEMPLATE_P (value_binding)
4466 || (TREE_CODE (value_binding) == TREE_LIST
4467 && TREE_TYPE (value_binding) == error_mark_node
4468 && (TREE_CODE (TREE_VALUE (value_binding))
4469 == TYPE_DECL))))
4470 /* We found a type binding, even when looking for a non-type
4471 binding. This means that we already processed this binding
4472 above. */
4474 else if (value_binding)
4476 if (TREE_CODE (value_binding) == TREE_LIST
4477 && TREE_TYPE (value_binding) == error_mark_node)
4478 /* NAME is ambiguous. */
4480 else if (BASELINK_P (value_binding))
4481 /* NAME is some overloaded functions. */
4482 value_binding = BASELINK_FUNCTIONS (value_binding);
4485 /* If we found either a type binding or a value binding, create a
4486 new binding object. */
4487 if (type_binding || value_binding)
4489 binding = new_class_binding (name,
4490 value_binding,
4491 type_binding,
4492 scope);
4493 /* This is a class-scope binding, not a block-scope binding. */
4494 LOCAL_BINDING_P (binding) = 0;
4495 set_inherited_value_binding_p (binding, value_binding, class_type);
4497 else
4498 binding = NULL;
4500 return binding;
4503 /* Make the declaration(s) of X appear in CLASS scope under the name
4504 NAME. Returns true if the binding is valid. */
4506 static bool
4507 push_class_level_binding_1 (tree name, tree x)
4509 cxx_binding *binding;
4510 tree decl = x;
4511 bool ok;
4513 /* The class_binding_level will be NULL if x is a template
4514 parameter name in a member template. */
4515 if (!class_binding_level)
4516 return true;
4518 if (name == error_mark_node)
4519 return false;
4521 /* Can happen for an erroneous declaration (c++/60384). */
4522 if (!identifier_p (name))
4524 gcc_assert (errorcount || sorrycount);
4525 return false;
4528 /* Check for invalid member names. But don't worry about a default
4529 argument-scope lambda being pushed after the class is complete. */
4530 gcc_assert (TYPE_BEING_DEFINED (current_class_type)
4531 || LAMBDA_TYPE_P (TREE_TYPE (decl)));
4532 /* Check that we're pushing into the right binding level. */
4533 gcc_assert (current_class_type == class_binding_level->this_entity);
4535 /* We could have been passed a tree list if this is an ambiguous
4536 declaration. If so, pull the declaration out because
4537 check_template_shadow will not handle a TREE_LIST. */
4538 if (TREE_CODE (decl) == TREE_LIST
4539 && TREE_TYPE (decl) == error_mark_node)
4540 decl = TREE_VALUE (decl);
4542 if (!check_template_shadow (decl))
4543 return false;
4545 /* [class.mem]
4547 If T is the name of a class, then each of the following shall
4548 have a name different from T:
4550 -- every static data member of class T;
4552 -- every member of class T that is itself a type;
4554 -- every enumerator of every member of class T that is an
4555 enumerated type;
4557 -- every member of every anonymous union that is a member of
4558 class T.
4560 (Non-static data members were also forbidden to have the same
4561 name as T until TC1.) */
4562 if ((VAR_P (x)
4563 || TREE_CODE (x) == CONST_DECL
4564 || (TREE_CODE (x) == TYPE_DECL
4565 && !DECL_SELF_REFERENCE_P (x))
4566 /* A data member of an anonymous union. */
4567 || (TREE_CODE (x) == FIELD_DECL
4568 && DECL_CONTEXT (x) != current_class_type))
4569 && DECL_NAME (x) == DECL_NAME (TYPE_NAME (current_class_type)))
4571 tree scope = context_for_name_lookup (x);
4572 if (TYPE_P (scope) && same_type_p (scope, current_class_type))
4574 error ("%qD has the same name as the class in which it is "
4575 "declared",
4577 return false;
4581 /* Get the current binding for NAME in this class, if any. */
4582 binding = IDENTIFIER_BINDING (name);
4583 if (!binding || binding->scope != class_binding_level)
4585 binding = get_class_binding (name, class_binding_level);
4586 /* If a new binding was created, put it at the front of the
4587 IDENTIFIER_BINDING list. */
4588 if (binding)
4590 binding->previous = IDENTIFIER_BINDING (name);
4591 IDENTIFIER_BINDING (name) = binding;
4595 /* If there is already a binding, then we may need to update the
4596 current value. */
4597 if (binding && binding->value)
4599 tree bval = binding->value;
4600 tree old_decl = NULL_TREE;
4601 tree target_decl = strip_using_decl (decl);
4602 tree target_bval = strip_using_decl (bval);
4604 if (INHERITED_VALUE_BINDING_P (binding))
4606 /* If the old binding was from a base class, and was for a
4607 tag name, slide it over to make room for the new binding.
4608 The old binding is still visible if explicitly qualified
4609 with a class-key. */
4610 if (TREE_CODE (target_bval) == TYPE_DECL
4611 && DECL_ARTIFICIAL (target_bval)
4612 && !(TREE_CODE (target_decl) == TYPE_DECL
4613 && DECL_ARTIFICIAL (target_decl)))
4615 old_decl = binding->type;
4616 binding->type = bval;
4617 binding->value = NULL_TREE;
4618 INHERITED_VALUE_BINDING_P (binding) = 0;
4620 else
4622 old_decl = bval;
4623 /* Any inherited type declaration is hidden by the type
4624 declaration in the derived class. */
4625 if (TREE_CODE (target_decl) == TYPE_DECL
4626 && DECL_ARTIFICIAL (target_decl))
4627 binding->type = NULL_TREE;
4630 else if (TREE_CODE (target_decl) == OVERLOAD
4631 && OVL_P (target_bval))
4632 old_decl = bval;
4633 else if (TREE_CODE (decl) == USING_DECL
4634 && TREE_CODE (bval) == USING_DECL
4635 && same_type_p (USING_DECL_SCOPE (decl),
4636 USING_DECL_SCOPE (bval)))
4637 /* This is a using redeclaration that will be diagnosed later
4638 in supplement_binding */
4640 else if (TREE_CODE (decl) == USING_DECL
4641 && TREE_CODE (bval) == USING_DECL
4642 && DECL_DEPENDENT_P (decl)
4643 && DECL_DEPENDENT_P (bval))
4644 return true;
4645 else if (TREE_CODE (decl) == USING_DECL
4646 && OVL_P (target_bval))
4647 old_decl = bval;
4648 else if (TREE_CODE (bval) == USING_DECL
4649 && OVL_P (target_decl))
4650 return true;
4652 if (old_decl && binding->scope == class_binding_level)
4654 binding->value = x;
4655 /* It is always safe to clear INHERITED_VALUE_BINDING_P
4656 here. This function is only used to register bindings
4657 from with the class definition itself. */
4658 INHERITED_VALUE_BINDING_P (binding) = 0;
4659 return true;
4663 /* Note that we declared this value so that we can issue an error if
4664 this is an invalid redeclaration of a name already used for some
4665 other purpose. */
4666 note_name_declared_in_class (name, decl);
4668 /* If we didn't replace an existing binding, put the binding on the
4669 stack of bindings for the identifier, and update the shadowed
4670 list. */
4671 if (binding && binding->scope == class_binding_level)
4672 /* Supplement the existing binding. */
4673 ok = supplement_binding (binding, decl);
4674 else
4676 /* Create a new binding. */
4677 push_binding (name, decl, class_binding_level);
4678 ok = true;
4681 return ok;
4684 /* Wrapper for push_class_level_binding_1. */
4686 bool
4687 push_class_level_binding (tree name, tree x)
4689 bool ret;
4690 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4691 ret = push_class_level_binding_1 (name, x);
4692 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4693 return ret;
4696 /* Process "using SCOPE::NAME" in a class scope. Return the
4697 USING_DECL created. */
4699 tree
4700 do_class_using_decl (tree scope, tree name)
4702 if (name == error_mark_node)
4703 return NULL_TREE;
4705 if (!scope || !TYPE_P (scope))
4707 error ("using-declaration for non-member at class scope");
4708 return NULL_TREE;
4711 /* Make sure the name is not invalid */
4712 if (TREE_CODE (name) == BIT_NOT_EXPR)
4714 error ("%<%T::%D%> names destructor", scope, name);
4715 return NULL_TREE;
4718 /* Using T::T declares inheriting ctors, even if T is a typedef. */
4719 if (MAYBE_CLASS_TYPE_P (scope)
4720 && (name == TYPE_IDENTIFIER (scope)
4721 || constructor_name_p (name, scope)))
4723 maybe_warn_cpp0x (CPP0X_INHERITING_CTORS);
4724 name = ctor_identifier;
4725 CLASSTYPE_NON_AGGREGATE (current_class_type) = true;
4728 /* Cannot introduce a constructor name. */
4729 if (constructor_name_p (name, current_class_type))
4731 error ("%<%T::%D%> names constructor in %qT",
4732 scope, name, current_class_type);
4733 return NULL_TREE;
4736 /* From [namespace.udecl]:
4738 A using-declaration used as a member-declaration shall refer to a
4739 member of a base class of the class being defined.
4741 In general, we cannot check this constraint in a template because
4742 we do not know the entire set of base classes of the current
4743 class type. Morover, if SCOPE is dependent, it might match a
4744 non-dependent base. */
4746 tree decl = NULL_TREE;
4747 if (!dependent_scope_p (scope))
4749 base_kind b_kind;
4750 tree binfo = lookup_base (current_class_type, scope, ba_any, &b_kind,
4751 tf_warning_or_error);
4752 if (b_kind < bk_proper_base)
4754 /* If there are dependent bases, scope might resolve at
4755 instantiation time, even if it isn't exactly one of the
4756 dependent bases. */
4757 if (b_kind == bk_same_type || !any_dependent_bases_p ())
4759 error_not_base_type (scope, current_class_type);
4760 return NULL_TREE;
4763 else if (name == ctor_identifier && !binfo_direct_p (binfo))
4765 error ("cannot inherit constructors from indirect base %qT", scope);
4766 return NULL_TREE;
4768 else if (!IDENTIFIER_CONV_OP_P (name)
4769 || !dependent_type_p (TREE_TYPE (name)))
4771 decl = lookup_member (binfo, name, 0, false, tf_warning_or_error);
4772 if (!decl)
4774 error ("no members matching %<%T::%D%> in %q#T", scope, name,
4775 scope);
4776 return NULL_TREE;
4779 /* The binfo from which the functions came does not matter. */
4780 if (BASELINK_P (decl))
4781 decl = BASELINK_FUNCTIONS (decl);
4785 tree value = build_lang_decl (USING_DECL, name, NULL_TREE);
4786 USING_DECL_DECLS (value) = decl;
4787 USING_DECL_SCOPE (value) = scope;
4788 DECL_DEPENDENT_P (value) = !decl;
4790 return value;
4794 /* Return the binding for NAME in NS. If NS is NULL, look in
4795 global_namespace. */
4797 tree
4798 get_namespace_binding (tree ns, tree name)
4800 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4801 if (!ns)
4802 ns = global_namespace;
4803 gcc_checking_assert (!DECL_NAMESPACE_ALIAS (ns));
4804 tree ret = find_namespace_value (ns, name);
4805 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4806 return ret;
4809 /* Push internal DECL into the global namespace. Does not do the
4810 full overload fn handling and does not add it to the list of things
4811 in the namespace. */
4813 void
4814 set_global_binding (tree decl)
4816 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4818 tree *slot = find_namespace_slot (global_namespace, DECL_NAME (decl), true);
4820 if (*slot)
4821 /* The user's placed something in the implementor's namespace. */
4822 diagnose_name_conflict (decl, MAYBE_STAT_DECL (*slot));
4824 /* Force the binding, so compiler internals continue to work. */
4825 *slot = decl;
4827 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4830 /* Set the context of a declaration to scope. Complain if we are not
4831 outside scope. */
4833 void
4834 set_decl_namespace (tree decl, tree scope, bool friendp)
4836 /* Get rid of namespace aliases. */
4837 scope = ORIGINAL_NAMESPACE (scope);
4839 /* It is ok for friends to be qualified in parallel space. */
4840 if (!friendp && !is_nested_namespace (current_namespace, scope))
4841 error ("declaration of %qD not in a namespace surrounding %qD",
4842 decl, scope);
4843 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
4845 /* See whether this has been declared in the namespace or inline
4846 children. */
4847 tree old = NULL_TREE;
4849 name_lookup lookup (DECL_NAME (decl), LOOKUP_HIDDEN);
4850 if (!lookup.search_qualified (scope, /*usings=*/false))
4851 /* No old declaration at all. */
4852 goto not_found;
4853 old = lookup.value;
4856 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
4857 if (TREE_CODE (old) == TREE_LIST)
4859 ambiguous:
4860 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
4861 error ("reference to %qD is ambiguous", decl);
4862 print_candidates (old);
4863 return;
4866 if (!DECL_DECLARES_FUNCTION_P (decl))
4868 /* Don't compare non-function decls with decls_match here, since
4869 it can't check for the correct constness at this
4870 point. pushdecl will find those errors later. */
4872 /* We might have found it in an inline namespace child of SCOPE. */
4873 if (TREE_CODE (decl) == TREE_CODE (old))
4874 DECL_CONTEXT (decl) = DECL_CONTEXT (old);
4876 found:
4877 /* Writing "N::i" to declare something directly in "N" is invalid. */
4878 if (CP_DECL_CONTEXT (decl) == current_namespace
4879 && at_namespace_scope_p ())
4880 error ("explicit qualification in declaration of %qD", decl);
4881 return;
4884 /* Since decl is a function, old should contain a function decl. */
4885 if (!OVL_P (old))
4886 goto not_found;
4888 /* We handle these in check_explicit_instantiation_namespace. */
4889 if (processing_explicit_instantiation)
4890 return;
4891 if (processing_template_decl || processing_specialization)
4892 /* We have not yet called push_template_decl to turn a
4893 FUNCTION_DECL into a TEMPLATE_DECL, so the declarations won't
4894 match. But, we'll check later, when we construct the
4895 template. */
4896 return;
4897 /* Instantiations or specializations of templates may be declared as
4898 friends in any namespace. */
4899 if (friendp && DECL_USE_TEMPLATE (decl))
4900 return;
4902 tree found;
4903 found = NULL_TREE;
4905 for (lkp_iterator iter (old); iter; ++iter)
4907 if (iter.using_p ())
4908 continue;
4910 tree ofn = *iter;
4912 /* Adjust DECL_CONTEXT first so decls_match will return true
4913 if DECL will match a declaration in an inline namespace. */
4914 DECL_CONTEXT (decl) = DECL_CONTEXT (ofn);
4915 if (decls_match (decl, ofn))
4917 if (found)
4919 /* We found more than one matching declaration. */
4920 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
4921 goto ambiguous;
4923 found = ofn;
4927 if (found)
4929 if (DECL_HIDDEN_FRIEND_P (found))
4931 pedwarn (DECL_SOURCE_LOCATION (decl), 0,
4932 "%qD has not been declared within %qD", decl, scope);
4933 inform (DECL_SOURCE_LOCATION (found),
4934 "only here as a %<friend%>");
4936 DECL_CONTEXT (decl) = DECL_CONTEXT (found);
4937 goto found;
4940 not_found:
4941 /* It didn't work, go back to the explicit scope. */
4942 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
4943 error ("%qD should have been declared inside %qD", decl, scope);
4946 /* Return the namespace where the current declaration is declared. */
4948 tree
4949 current_decl_namespace (void)
4951 tree result;
4952 /* If we have been pushed into a different namespace, use it. */
4953 if (!vec_safe_is_empty (decl_namespace_list))
4954 return decl_namespace_list->last ();
4956 if (current_class_type)
4957 result = decl_namespace_context (current_class_type);
4958 else if (current_function_decl)
4959 result = decl_namespace_context (current_function_decl);
4960 else
4961 result = current_namespace;
4962 return result;
4965 /* Process any ATTRIBUTES on a namespace definition. Returns true if
4966 attribute visibility is seen. */
4968 bool
4969 handle_namespace_attrs (tree ns, tree attributes)
4971 tree d;
4972 bool saw_vis = false;
4974 if (attributes == error_mark_node)
4975 return false;
4977 for (d = attributes; d; d = TREE_CHAIN (d))
4979 tree name = get_attribute_name (d);
4980 tree args = TREE_VALUE (d);
4982 if (is_attribute_p ("visibility", name))
4984 /* attribute visibility is a property of the syntactic block
4985 rather than the namespace as a whole, so we don't touch the
4986 NAMESPACE_DECL at all. */
4987 tree x = args ? TREE_VALUE (args) : NULL_TREE;
4988 if (x == NULL_TREE || TREE_CODE (x) != STRING_CST || TREE_CHAIN (args))
4990 warning (OPT_Wattributes,
4991 "%qD attribute requires a single NTBS argument",
4992 name);
4993 continue;
4996 if (!TREE_PUBLIC (ns))
4997 warning (OPT_Wattributes,
4998 "%qD attribute is meaningless since members of the "
4999 "anonymous namespace get local symbols", name);
5001 push_visibility (TREE_STRING_POINTER (x), 1);
5002 saw_vis = true;
5004 else if (is_attribute_p ("abi_tag", name))
5006 if (!DECL_NAME (ns))
5008 warning (OPT_Wattributes, "ignoring %qD attribute on anonymous "
5009 "namespace", name);
5010 continue;
5012 if (!DECL_NAMESPACE_INLINE_P (ns))
5014 warning (OPT_Wattributes, "ignoring %qD attribute on non-inline "
5015 "namespace", name);
5016 continue;
5018 if (!args)
5020 tree dn = DECL_NAME (ns);
5021 args = build_string (IDENTIFIER_LENGTH (dn) + 1,
5022 IDENTIFIER_POINTER (dn));
5023 TREE_TYPE (args) = char_array_type_node;
5024 args = fix_string_type (args);
5025 args = build_tree_list (NULL_TREE, args);
5027 if (check_abi_tag_args (args, name))
5028 DECL_ATTRIBUTES (ns) = tree_cons (name, args,
5029 DECL_ATTRIBUTES (ns));
5031 else
5033 warning (OPT_Wattributes, "%qD attribute directive ignored",
5034 name);
5035 continue;
5039 return saw_vis;
5042 /* Temporarily set the namespace for the current declaration. */
5044 void
5045 push_decl_namespace (tree decl)
5047 if (TREE_CODE (decl) != NAMESPACE_DECL)
5048 decl = decl_namespace_context (decl);
5049 vec_safe_push (decl_namespace_list, ORIGINAL_NAMESPACE (decl));
5052 /* [namespace.memdef]/2 */
5054 void
5055 pop_decl_namespace (void)
5057 decl_namespace_list->pop ();
5060 /* Process a namespace-alias declaration. */
5062 void
5063 do_namespace_alias (tree alias, tree name_space)
5065 if (name_space == error_mark_node)
5066 return;
5068 gcc_assert (TREE_CODE (name_space) == NAMESPACE_DECL);
5070 name_space = ORIGINAL_NAMESPACE (name_space);
5072 /* Build the alias. */
5073 alias = build_lang_decl (NAMESPACE_DECL, alias, void_type_node);
5074 DECL_NAMESPACE_ALIAS (alias) = name_space;
5075 DECL_EXTERNAL (alias) = 1;
5076 DECL_CONTEXT (alias) = FROB_CONTEXT (current_scope ());
5077 pushdecl (alias);
5079 /* Emit debug info for namespace alias. */
5080 if (!building_stmt_list_p ())
5081 (*debug_hooks->early_global_decl) (alias);
5084 /* Like pushdecl, only it places X in the current namespace,
5085 if appropriate. */
5087 tree
5088 pushdecl_namespace_level (tree x, bool is_friend)
5090 cp_binding_level *b = current_binding_level;
5091 tree t;
5093 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
5094 t = do_pushdecl_with_scope
5095 (x, NAMESPACE_LEVEL (current_namespace), is_friend);
5097 /* Now, the type_shadowed stack may screw us. Munge it so it does
5098 what we want. */
5099 if (TREE_CODE (t) == TYPE_DECL)
5101 tree name = DECL_NAME (t);
5102 tree newval;
5103 tree *ptr = (tree *)0;
5104 for (; !global_scope_p (b); b = b->level_chain)
5106 tree shadowed = b->type_shadowed;
5107 for (; shadowed; shadowed = TREE_CHAIN (shadowed))
5108 if (TREE_PURPOSE (shadowed) == name)
5110 ptr = &TREE_VALUE (shadowed);
5111 /* Can't break out of the loop here because sometimes
5112 a binding level will have duplicate bindings for
5113 PT names. It's gross, but I haven't time to fix it. */
5116 newval = TREE_TYPE (t);
5117 if (ptr == (tree *)0)
5119 /* @@ This shouldn't be needed. My test case "zstring.cc" trips
5120 up here if this is changed to an assertion. --KR */
5121 SET_IDENTIFIER_TYPE_VALUE (name, t);
5123 else
5125 *ptr = newval;
5128 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
5129 return t;
5132 /* Process a using-declaration appearing in namespace scope. */
5134 void
5135 finish_namespace_using_decl (tree decl, tree scope, tree name)
5137 tree orig_decl = decl;
5139 gcc_checking_assert (current_binding_level->kind == sk_namespace
5140 && !processing_template_decl);
5141 decl = validate_nonmember_using_decl (decl, scope, name);
5142 if (decl == NULL_TREE)
5143 return;
5145 tree *slot = find_namespace_slot (current_namespace, name, true);
5146 tree val = slot ? MAYBE_STAT_DECL (*slot) : NULL_TREE;
5147 tree type = slot ? MAYBE_STAT_TYPE (*slot) : NULL_TREE;
5148 do_nonmember_using_decl (scope, name, &val, &type);
5149 if (STAT_HACK_P (*slot))
5151 STAT_DECL (*slot) = val;
5152 STAT_TYPE (*slot) = type;
5154 else if (type)
5155 *slot = stat_hack (val, type);
5156 else
5157 *slot = val;
5159 /* Emit debug info. */
5160 cp_emit_debug_info_for_using (orig_decl, current_namespace);
5163 /* Process a using-declaration at function scope. */
5165 void
5166 finish_local_using_decl (tree decl, tree scope, tree name)
5168 tree orig_decl = decl;
5170 gcc_checking_assert (current_binding_level->kind != sk_class
5171 && current_binding_level->kind != sk_namespace);
5172 decl = validate_nonmember_using_decl (decl, scope, name);
5173 if (decl == NULL_TREE)
5174 return;
5176 add_decl_expr (decl);
5178 cxx_binding *binding = find_local_binding (current_binding_level, name);
5179 tree value = binding ? binding->value : NULL_TREE;
5180 tree type = binding ? binding->type : NULL_TREE;
5182 do_nonmember_using_decl (scope, name, &value, &type);
5184 if (!value)
5186 else if (binding && value == binding->value)
5188 else if (binding && binding->value && TREE_CODE (value) == OVERLOAD)
5190 update_local_overload (IDENTIFIER_BINDING (name), value);
5191 IDENTIFIER_BINDING (name)->value = value;
5193 else
5194 /* Install the new binding. */
5195 push_local_binding (name, value, true);
5197 if (!type)
5199 else if (binding && type == binding->type)
5201 else
5203 push_local_binding (name, type, true);
5204 set_identifier_type_value (name, type);
5207 /* Emit debug info. */
5208 if (!processing_template_decl)
5209 cp_emit_debug_info_for_using (orig_decl, current_scope ());
5212 /* Return the declarations that are members of the namespace NS. */
5214 tree
5215 cp_namespace_decls (tree ns)
5217 return NAMESPACE_LEVEL (ns)->names;
5220 /* Combine prefer_type and namespaces_only into flags. */
5222 static int
5223 lookup_flags (int prefer_type, int namespaces_only)
5225 if (namespaces_only)
5226 return LOOKUP_PREFER_NAMESPACES;
5227 if (prefer_type > 1)
5228 return LOOKUP_PREFER_TYPES;
5229 if (prefer_type > 0)
5230 return LOOKUP_PREFER_BOTH;
5231 return 0;
5234 /* Given a lookup that returned VAL, use FLAGS to decide if we want to
5235 ignore it or not. Subroutine of lookup_name_real and
5236 lookup_type_scope. */
5238 static bool
5239 qualify_lookup (tree val, int flags)
5241 if (val == NULL_TREE)
5242 return false;
5243 if ((flags & LOOKUP_PREFER_NAMESPACES) && TREE_CODE (val) == NAMESPACE_DECL)
5244 return true;
5245 if (flags & LOOKUP_PREFER_TYPES)
5247 tree target_val = strip_using_decl (val);
5248 if (TREE_CODE (target_val) == TYPE_DECL
5249 || TREE_CODE (target_val) == TEMPLATE_DECL)
5250 return true;
5252 if (flags & (LOOKUP_PREFER_NAMESPACES | LOOKUP_PREFER_TYPES))
5253 return false;
5254 /* Look through lambda things that we shouldn't be able to see. */
5255 if (!(flags & LOOKUP_HIDDEN) && is_lambda_ignored_entity (val))
5256 return false;
5257 return true;
5260 /* Is there a "using namespace std;" directive within USINGS? */
5262 static bool
5263 using_directives_contain_std_p (vec<tree, va_gc> *usings)
5265 if (!usings)
5266 return false;
5268 for (unsigned ix = usings->length (); ix--;)
5269 if ((*usings)[ix] == std_node)
5270 return true;
5272 return false;
5275 /* Is there a "using namespace std;" directive within the current
5276 namespace (or its ancestors)?
5277 Compare with name_lookup::search_unqualified. */
5279 static bool
5280 has_using_namespace_std_directive_p ()
5282 /* Look at local using-directives. */
5283 for (cp_binding_level *level = current_binding_level;
5284 level->kind != sk_namespace;
5285 level = level->level_chain)
5286 if (using_directives_contain_std_p (level->using_directives))
5287 return true;
5289 /* Look at this namespace and its ancestors. */
5290 for (tree scope = current_namespace; scope; scope = CP_DECL_CONTEXT (scope))
5292 if (using_directives_contain_std_p (DECL_NAMESPACE_USING (scope)))
5293 return true;
5295 if (scope == global_namespace)
5296 break;
5299 return false;
5302 /* Suggest alternatives for NAME, an IDENTIFIER_NODE for which name
5303 lookup failed. Search through all available namespaces and print out
5304 possible candidates. If no exact matches are found, and
5305 SUGGEST_MISSPELLINGS is true, then also look for near-matches and
5306 suggest the best near-match, if there is one. */
5308 void
5309 suggest_alternatives_for (location_t location, tree name,
5310 bool suggest_misspellings)
5312 vec<tree> candidates = vNULL;
5313 vec<tree> worklist = vNULL;
5314 unsigned limit = PARAM_VALUE (CXX_MAX_NAMESPACES_FOR_DIAGNOSTIC_HELP);
5315 bool limited = false;
5317 /* Breadth-first search of namespaces. Up to limit namespaces
5318 searched (limit zero == unlimited). */
5319 worklist.safe_push (global_namespace);
5320 for (unsigned ix = 0; ix != worklist.length (); ix++)
5322 tree ns = worklist[ix];
5323 name_lookup lookup (name);
5325 if (lookup.search_qualified (ns, false))
5326 candidates.safe_push (lookup.value);
5328 if (!limited)
5330 /* Look for child namespaces. We have to do this
5331 indirectly because they are chained in reverse order,
5332 which is confusing to the user. */
5333 vec<tree> children = vNULL;
5335 for (tree decl = NAMESPACE_LEVEL (ns)->names;
5336 decl; decl = TREE_CHAIN (decl))
5337 if (TREE_CODE (decl) == NAMESPACE_DECL
5338 && !DECL_NAMESPACE_ALIAS (decl)
5339 && !DECL_NAMESPACE_INLINE_P (decl))
5340 children.safe_push (decl);
5342 while (!limited && !children.is_empty ())
5344 if (worklist.length () == limit)
5346 /* Unconditionally warn that the search was truncated. */
5347 inform (location,
5348 "maximum limit of %d namespaces searched for %qE",
5349 limit, name);
5350 limited = true;
5352 else
5353 worklist.safe_push (children.pop ());
5355 children.release ();
5358 worklist.release ();
5360 if (candidates.length ())
5362 inform_n (location, candidates.length (),
5363 "suggested alternative:",
5364 "suggested alternatives:");
5365 for (unsigned ix = 0; ix != candidates.length (); ix++)
5367 tree val = candidates[ix];
5369 inform (location_of (val), " %qE", val);
5371 candidates.release ();
5372 return;
5375 /* No candidates were found in the available namespaces. */
5377 /* If there's a "using namespace std;" active, and this
5378 is one of the most common "std::" names, then it's probably a
5379 missing #include. */
5380 if (has_using_namespace_std_directive_p ())
5381 if (maybe_suggest_missing_std_header (location, name))
5382 return;
5384 /* Otherwise, consider misspellings. */
5385 if (!suggest_misspellings)
5386 return;
5387 if (name_hint hint = lookup_name_fuzzy (name, FUZZY_LOOKUP_NAME,
5388 location))
5390 /* Show a spelling correction. */
5391 gcc_rich_location richloc (location);
5393 richloc.add_fixit_replace (hint.suggestion ());
5394 inform (&richloc, "suggested alternative: %qs", hint.suggestion ());
5398 /* A well-known name within the C++ standard library, returned by
5399 get_std_name_hint. */
5401 struct std_name_hint
5403 /* A name within "std::". */
5404 const char *name;
5406 /* The header name defining it within the C++ Standard Library
5407 (with '<' and '>'). */
5408 const char *header;
5410 /* The dialect of C++ in which this was added. */
5411 enum cxx_dialect min_dialect;
5414 /* Subroutine of maybe_suggest_missing_header for handling unrecognized names
5415 for some of the most common names within "std::".
5416 Given non-NULL NAME, return the std_name_hint for it, or NULL. */
5418 static const std_name_hint *
5419 get_std_name_hint (const char *name)
5421 static const std_name_hint hints[] = {
5422 /* <any>. */
5423 {"any", "<any>", cxx17},
5424 {"any_cast", "<any>", cxx17},
5425 {"make_any", "<any>", cxx17},
5426 /* <array>. */
5427 {"array", "<array>", cxx11},
5428 /* <atomic>. */
5429 {"atomic", "<atomic>", cxx11},
5430 {"atomic_flag", "<atomic>", cxx11},
5431 /* <bitset>. */
5432 {"bitset", "<bitset>", cxx11},
5433 /* <complex>. */
5434 {"complex", "<complex>", cxx98},
5435 {"complex_literals", "<complex>", cxx98},
5436 /* <condition_variable>. */
5437 {"condition_variable", "<condition_variable>", cxx11},
5438 {"condition_variable_any", "<condition_variable>", cxx11},
5439 /* <deque>. */
5440 {"deque", "<deque>", cxx98},
5441 /* <forward_list>. */
5442 {"forward_list", "<forward_list>", cxx11},
5443 /* <fstream>. */
5444 {"basic_filebuf", "<fstream>", cxx98},
5445 {"basic_ifstream", "<fstream>", cxx98},
5446 {"basic_ofstream", "<fstream>", cxx98},
5447 {"basic_fstream", "<fstream>", cxx98},
5448 {"fstream", "<fstream>", cxx98},
5449 {"ifstream", "<fstream>", cxx98},
5450 {"ofstream", "<fstream>", cxx98},
5451 /* <functional>. */
5452 {"bind", "<functional>", cxx11},
5453 {"function", "<functional>", cxx11},
5454 {"hash", "<functional>", cxx11},
5455 {"mem_fn", "<functional>", cxx11},
5456 /* <future>. */
5457 {"async", "<future>", cxx11},
5458 {"future", "<future>", cxx11},
5459 {"packaged_task", "<future>", cxx11},
5460 {"promise", "<future>", cxx11},
5461 /* <iostream>. */
5462 {"cin", "<iostream>", cxx98},
5463 {"cout", "<iostream>", cxx98},
5464 {"cerr", "<iostream>", cxx98},
5465 {"clog", "<iostream>", cxx98},
5466 {"wcin", "<iostream>", cxx98},
5467 {"wcout", "<iostream>", cxx98},
5468 {"wclog", "<iostream>", cxx98},
5469 /* <istream>. */
5470 {"istream", "<istream>", cxx98},
5471 /* <iterator>. */
5472 {"advance", "<iterator>", cxx98},
5473 {"back_inserter", "<iterator>", cxx98},
5474 {"begin", "<iterator>", cxx11},
5475 {"distance", "<iterator>", cxx98},
5476 {"end", "<iterator>", cxx11},
5477 {"front_inserter", "<iterator>", cxx98},
5478 {"inserter", "<iterator>", cxx98},
5479 {"istream_iterator", "<iterator>", cxx98},
5480 {"istreambuf_iterator", "<iterator>", cxx98},
5481 {"iterator_traits", "<iterator>", cxx98},
5482 {"move_iterator", "<iterator>", cxx11},
5483 {"next", "<iterator>", cxx11},
5484 {"ostream_iterator", "<iterator>", cxx98},
5485 {"ostreambuf_iterator", "<iterator>", cxx98},
5486 {"prev", "<iterator>", cxx11},
5487 {"reverse_iterator", "<iterator>", cxx98},
5488 /* <ostream>. */
5489 {"ostream", "<ostream>", cxx98},
5490 /* <list>. */
5491 {"list", "<list>", cxx98},
5492 /* <map>. */
5493 {"map", "<map>", cxx98},
5494 {"multimap", "<map>", cxx98},
5495 /* <memory>. */
5496 {"make_shared", "<memory>", cxx11},
5497 {"make_unique", "<memory>", cxx11},
5498 {"shared_ptr", "<memory>", cxx11},
5499 {"unique_ptr", "<memory>", cxx11},
5500 {"weak_ptr", "<memory>", cxx11},
5501 /* <mutex>. */
5502 {"mutex", "<mutex>", cxx11},
5503 {"timed_mutex", "<mutex>", cxx11},
5504 {"recursive_mutex", "<mutex>", cxx11},
5505 {"recursive_timed_mutex", "<mutex>", cxx11},
5506 {"once_flag", "<mutex>", cxx11},
5507 {"call_once,", "<mutex>", cxx11},
5508 {"lock", "<mutex>", cxx11},
5509 {"scoped_lock", "<mutex>", cxx17},
5510 {"try_lock", "<mutex>", cxx11},
5511 {"lock_guard", "<mutex>", cxx11},
5512 {"unique_lock", "<mutex>", cxx11},
5513 /* <optional>. */
5514 {"optional", "<optional>", cxx17},
5515 {"make_optional", "<optional>", cxx17},
5516 /* <ostream>. */
5517 {"ostream", "<ostream>", cxx98},
5518 {"wostream", "<ostream>", cxx98},
5519 {"ends", "<ostream>", cxx98},
5520 {"flush", "<ostream>", cxx98},
5521 {"endl", "<ostream>", cxx98},
5522 /* <queue>. */
5523 {"queue", "<queue>", cxx98},
5524 {"priority_queue", "<queue>", cxx98},
5525 /* <set>. */
5526 {"set", "<set>", cxx98},
5527 {"multiset", "<set>", cxx98},
5528 /* <shared_mutex>. */
5529 {"shared_lock", "<shared_mutex>", cxx14},
5530 {"shared_mutex", "<shared_mutex>", cxx17},
5531 {"shared_timed_mutex", "<shared_mutex>", cxx14},
5532 /* <sstream>. */
5533 {"basic_stringbuf", "<sstream>", cxx98},
5534 {"basic_istringstream", "<sstream>", cxx98},
5535 {"basic_ostringstream", "<sstream>", cxx98},
5536 {"basic_stringstream", "<sstream>", cxx98},
5537 {"istringstream", "<sstream>", cxx98},
5538 {"ostringstream", "<sstream>", cxx98},
5539 {"stringstream", "<sstream>", cxx98},
5540 /* <stack>. */
5541 {"stack", "<stack>", cxx98},
5542 /* <string>. */
5543 {"basic_string", "<string>", cxx98},
5544 {"string", "<string>", cxx98},
5545 {"wstring", "<string>", cxx98},
5546 {"u16string", "<string>", cxx11},
5547 {"u32string", "<string>", cxx11},
5548 /* <string_view>. */
5549 {"string_view", "<string_view>", cxx17},
5550 /* <thread>. */
5551 {"thread", "<thread>", cxx11},
5552 /* <tuple>. */
5553 {"make_tuple", "<tuple>", cxx11},
5554 {"tuple", "<tuple>", cxx11},
5555 {"tuple_element", "<tuple>", cxx11},
5556 {"tuple_size", "<tuple>", cxx11},
5557 /* <unordered_map>. */
5558 {"unordered_map", "<unordered_map>", cxx11},
5559 {"unordered_multimap", "<unordered_map>", cxx11},
5560 /* <unordered_set>. */
5561 {"unordered_set", "<unordered_set>", cxx11},
5562 {"unordered_multiset", "<unordered_set>", cxx11},
5563 /* <utility>. */
5564 {"declval", "<utility>", cxx11},
5565 {"forward", "<utility>", cxx11},
5566 {"make_pair", "<utility>", cxx98},
5567 {"move", "<utility>", cxx11},
5568 {"pair", "<utility>", cxx98},
5569 /* <variant>. */
5570 {"variant", "<variant>", cxx17},
5571 {"visit", "<variant>", cxx17},
5572 /* <vector>. */
5573 {"vector", "<vector>", cxx98},
5575 const size_t num_hints = sizeof (hints) / sizeof (hints[0]);
5576 for (size_t i = 0; i < num_hints; i++)
5578 if (strcmp (name, hints[i].name) == 0)
5579 return &hints[i];
5581 return NULL;
5584 /* Describe DIALECT. */
5586 static const char *
5587 get_cxx_dialect_name (enum cxx_dialect dialect)
5589 switch (dialect)
5591 default:
5592 gcc_unreachable ();
5593 case cxx98:
5594 return "C++98";
5595 case cxx11:
5596 return "C++11";
5597 case cxx14:
5598 return "C++14";
5599 case cxx17:
5600 return "C++17";
5601 case cxx2a:
5602 return "C++2a";
5606 /* Suggest pertinent header files for NAME at LOCATION, for common
5607 names within the "std" namespace.
5608 Return true iff a suggestion was offered. */
5610 static bool
5611 maybe_suggest_missing_std_header (location_t location, tree name)
5613 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
5615 const char *name_str = IDENTIFIER_POINTER (name);
5616 const std_name_hint *header_hint = get_std_name_hint (name_str);
5617 if (!header_hint)
5618 return false;
5620 gcc_rich_location richloc (location);
5621 if (cxx_dialect >= header_hint->min_dialect)
5623 const char *header = header_hint->header;
5624 maybe_add_include_fixit (&richloc, header, true);
5625 inform (&richloc,
5626 "%<std::%s%> is defined in header %qs;"
5627 " did you forget to %<#include %s%>?",
5628 name_str, header, header);
5630 else
5632 inform (&richloc,
5633 "%<std::%s%> is only available from %s onwards",
5634 name_str, get_cxx_dialect_name (header_hint->min_dialect));
5636 return true;
5639 /* If SCOPE is the "std" namespace, then suggest pertinent header
5640 files for NAME at LOCATION.
5641 Return true iff a suggestion was offered. */
5643 static bool
5644 maybe_suggest_missing_header (location_t location, tree name, tree scope)
5646 if (scope == NULL_TREE)
5647 return false;
5648 if (TREE_CODE (scope) != NAMESPACE_DECL)
5649 return false;
5650 /* We only offer suggestions for the "std" namespace. */
5651 if (scope != std_node)
5652 return false;
5653 return maybe_suggest_missing_std_header (location, name);
5656 /* Look for alternatives for NAME, an IDENTIFIER_NODE for which name
5657 lookup failed within the explicitly provided SCOPE. Suggest the
5658 the best meaningful candidates (if any) as a fix-it hint.
5659 Return true iff a suggestion was provided. */
5661 bool
5662 suggest_alternative_in_explicit_scope (location_t location, tree name,
5663 tree scope)
5665 /* Something went very wrong; don't suggest anything. */
5666 if (name == error_mark_node)
5667 return false;
5669 /* Resolve any namespace aliases. */
5670 scope = ORIGINAL_NAMESPACE (scope);
5672 if (maybe_suggest_missing_header (location, name, scope))
5673 return true;
5675 cp_binding_level *level = NAMESPACE_LEVEL (scope);
5677 best_match <tree, const char *> bm (name);
5678 consider_binding_level (name, bm, level, false, FUZZY_LOOKUP_NAME);
5680 /* See if we have a good suggesion for the user. */
5681 const char *fuzzy_name = bm.get_best_meaningful_candidate ();
5682 if (fuzzy_name)
5684 gcc_rich_location richloc (location);
5685 richloc.add_fixit_replace (fuzzy_name);
5686 inform (&richloc, "suggested alternative: %qs",
5687 fuzzy_name);
5688 return true;
5691 return false;
5694 /* Look up NAME (an IDENTIFIER_NODE) in SCOPE (either a NAMESPACE_DECL
5695 or a class TYPE).
5697 If PREFER_TYPE is > 0, we only return TYPE_DECLs or namespaces.
5698 If PREFER_TYPE is > 1, we only return TYPE_DECLs.
5700 Returns a DECL (or OVERLOAD, or BASELINK) representing the
5701 declaration found. If no suitable declaration can be found,
5702 ERROR_MARK_NODE is returned. If COMPLAIN is true and SCOPE is
5703 neither a class-type nor a namespace a diagnostic is issued. */
5705 tree
5706 lookup_qualified_name (tree scope, tree name, int prefer_type, bool complain,
5707 bool find_hidden)
5709 tree t = NULL_TREE;
5711 if (TREE_CODE (scope) == NAMESPACE_DECL)
5713 int flags = lookup_flags (prefer_type, /*namespaces_only*/false);
5714 if (find_hidden)
5715 flags |= LOOKUP_HIDDEN;
5716 name_lookup lookup (name, flags);
5718 if (qualified_namespace_lookup (scope, &lookup))
5719 t = lookup.value;
5721 else if (cxx_dialect != cxx98 && TREE_CODE (scope) == ENUMERAL_TYPE)
5722 t = lookup_enumerator (scope, name);
5723 else if (is_class_type (scope, complain))
5724 t = lookup_member (scope, name, 2, prefer_type, tf_warning_or_error);
5726 if (!t)
5727 return error_mark_node;
5728 return t;
5731 /* [namespace.qual]
5732 Accepts the NAME to lookup and its qualifying SCOPE.
5733 Returns the name/type pair found into the cxx_binding *RESULT,
5734 or false on error. */
5736 static bool
5737 qualified_namespace_lookup (tree scope, name_lookup *lookup)
5739 timevar_start (TV_NAME_LOOKUP);
5740 query_oracle (lookup->name);
5741 bool found = lookup->search_qualified (ORIGINAL_NAMESPACE (scope));
5742 timevar_stop (TV_NAME_LOOKUP);
5743 return found;
5746 /* Helper function for lookup_name_fuzzy.
5747 Traverse binding level LVL, looking for good name matches for NAME
5748 (and BM). */
5749 static void
5750 consider_binding_level (tree name, best_match <tree, const char *> &bm,
5751 cp_binding_level *lvl, bool look_within_fields,
5752 enum lookup_name_fuzzy_kind kind)
5754 if (look_within_fields)
5755 if (lvl->this_entity && TREE_CODE (lvl->this_entity) == RECORD_TYPE)
5757 tree type = lvl->this_entity;
5758 bool want_type_p = (kind == FUZZY_LOOKUP_TYPENAME);
5759 tree best_matching_field
5760 = lookup_member_fuzzy (type, name, want_type_p);
5761 if (best_matching_field)
5762 bm.consider (IDENTIFIER_POINTER (best_matching_field));
5765 /* Only suggest names reserved for the implementation if NAME begins
5766 with an underscore. */
5767 bool consider_implementation_names = (IDENTIFIER_POINTER (name)[0] == '_');
5769 for (tree t = lvl->names; t; t = TREE_CHAIN (t))
5771 tree d = t;
5773 /* OVERLOADs or decls from using declaration are wrapped into
5774 TREE_LIST. */
5775 if (TREE_CODE (d) == TREE_LIST)
5776 d = OVL_FIRST (TREE_VALUE (d));
5778 /* Don't use bindings from implicitly declared functions,
5779 as they were likely misspellings themselves. */
5780 if (TREE_TYPE (d) == error_mark_node)
5781 continue;
5783 /* Skip anticipated decls of builtin functions. */
5784 if (TREE_CODE (d) == FUNCTION_DECL
5785 && fndecl_built_in_p (d)
5786 && DECL_ANTICIPATED (d))
5787 continue;
5789 /* Skip compiler-generated variables (e.g. __for_begin/__for_end
5790 within range for). */
5791 if (TREE_CODE (d) == VAR_DECL
5792 && DECL_ARTIFICIAL (d))
5793 continue;
5795 tree suggestion = DECL_NAME (d);
5796 if (!suggestion)
5797 continue;
5799 /* Don't suggest names that are for anonymous aggregate types, as
5800 they are an implementation detail generated by the compiler. */
5801 if (anon_aggrname_p (suggestion))
5802 continue;
5804 const char *suggestion_str = IDENTIFIER_POINTER (suggestion);
5806 /* Ignore internal names with spaces in them. */
5807 if (strchr (suggestion_str, ' '))
5808 continue;
5810 /* Don't suggest names that are reserved for use by the
5811 implementation, unless NAME began with an underscore. */
5812 if (name_reserved_for_implementation_p (suggestion_str)
5813 && !consider_implementation_names)
5814 continue;
5816 bm.consider (suggestion_str);
5820 /* Subclass of deferred_diagnostic. Notify the user that the
5821 given macro was used before it was defined.
5822 This can be done in the C++ frontend since tokenization happens
5823 upfront. */
5825 class macro_use_before_def : public deferred_diagnostic
5827 public:
5828 /* Factory function. Return a new macro_use_before_def instance if
5829 appropriate, or return NULL. */
5830 static macro_use_before_def *
5831 maybe_make (location_t use_loc, cpp_hashnode *macro)
5833 source_location def_loc = cpp_macro_definition_location (macro);
5834 if (def_loc == UNKNOWN_LOCATION)
5835 return NULL;
5837 /* We only want to issue a note if the macro was used *before* it was
5838 defined.
5839 We don't want to issue a note for cases where a macro was incorrectly
5840 used, leaving it unexpanded (e.g. by using the wrong argument
5841 count). */
5842 if (!linemap_location_before_p (line_table, use_loc, def_loc))
5843 return NULL;
5845 return new macro_use_before_def (use_loc, macro);
5848 private:
5849 /* Ctor. LOC is the location of the usage. MACRO is the
5850 macro that was used. */
5851 macro_use_before_def (location_t loc, cpp_hashnode *macro)
5852 : deferred_diagnostic (loc), m_macro (macro)
5854 gcc_assert (macro);
5857 ~macro_use_before_def ()
5859 if (is_suppressed_p ())
5860 return;
5862 inform (get_location (), "the macro %qs had not yet been defined",
5863 (const char *)m_macro->ident.str);
5864 inform (cpp_macro_definition_location (m_macro),
5865 "it was later defined here");
5868 private:
5869 cpp_hashnode *m_macro;
5872 /* Determine if it can ever make sense to offer RID as a suggestion for
5873 a misspelling.
5875 Subroutine of lookup_name_fuzzy. */
5877 static bool
5878 suggest_rid_p (enum rid rid)
5880 switch (rid)
5882 /* Support suggesting function-like keywords. */
5883 case RID_STATIC_ASSERT:
5884 return true;
5886 default:
5887 /* Support suggesting the various decl-specifier words, to handle
5888 e.g. "singed" vs "signed" typos. */
5889 if (cp_keyword_starts_decl_specifier_p (rid))
5890 return true;
5892 /* Otherwise, don't offer it. This avoids suggesting e.g. "if"
5893 and "do" for short misspellings, which are likely to lead to
5894 nonsensical results. */
5895 return false;
5899 /* Search for near-matches for NAME within the current bindings, and within
5900 macro names, returning the best match as a const char *, or NULL if
5901 no reasonable match is found.
5903 Use LOC for any deferred diagnostics. */
5905 name_hint
5906 lookup_name_fuzzy (tree name, enum lookup_name_fuzzy_kind kind, location_t loc)
5908 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
5910 /* First, try some well-known names in the C++ standard library, in case
5911 the user forgot a #include. */
5912 const char *header_hint
5913 = get_cp_stdlib_header_for_name (IDENTIFIER_POINTER (name));
5914 if (header_hint)
5915 return name_hint (NULL,
5916 new suggest_missing_header (loc,
5917 IDENTIFIER_POINTER (name),
5918 header_hint));
5920 best_match <tree, const char *> bm (name);
5922 cp_binding_level *lvl;
5923 for (lvl = scope_chain->class_bindings; lvl; lvl = lvl->level_chain)
5924 consider_binding_level (name, bm, lvl, true, kind);
5926 for (lvl = current_binding_level; lvl; lvl = lvl->level_chain)
5927 consider_binding_level (name, bm, lvl, false, kind);
5929 /* Consider macros: if the user misspelled a macro name e.g. "SOME_MACRO"
5931 x = SOME_OTHER_MACRO (y);
5932 then "SOME_OTHER_MACRO" will survive to the frontend and show up
5933 as a misspelled identifier.
5935 Use the best distance so far so that a candidate is only set if
5936 a macro is better than anything so far. This allows early rejection
5937 (without calculating the edit distance) of macro names that must have
5938 distance >= bm.get_best_distance (), and means that we only get a
5939 non-NULL result for best_macro_match if it's better than any of
5940 the identifiers already checked. */
5941 best_macro_match bmm (name, bm.get_best_distance (), parse_in);
5942 cpp_hashnode *best_macro = bmm.get_best_meaningful_candidate ();
5943 /* If a macro is the closest so far to NAME, consider it. */
5944 if (best_macro)
5945 bm.consider ((const char *)best_macro->ident.str);
5946 else if (bmm.get_best_distance () == 0)
5948 /* If we have an exact match for a macro name, then either the
5949 macro was used with the wrong argument count, or the macro
5950 has been used before it was defined. */
5951 if (cpp_hashnode *macro = bmm.blithely_get_best_candidate ())
5952 if (cpp_user_macro_p (macro))
5953 return name_hint (NULL,
5954 macro_use_before_def::maybe_make (loc, macro));
5957 /* Try the "starts_decl_specifier_p" keywords to detect
5958 "singed" vs "signed" typos. */
5959 for (unsigned i = 0; i < num_c_common_reswords; i++)
5961 const c_common_resword *resword = &c_common_reswords[i];
5963 if (!suggest_rid_p (resword->rid))
5964 continue;
5966 tree resword_identifier = ridpointers [resword->rid];
5967 if (!resword_identifier)
5968 continue;
5969 gcc_assert (TREE_CODE (resword_identifier) == IDENTIFIER_NODE);
5971 /* Only consider reserved words that survived the
5972 filtering in init_reswords (e.g. for -std). */
5973 if (!IDENTIFIER_KEYWORD_P (resword_identifier))
5974 continue;
5976 bm.consider (IDENTIFIER_POINTER (resword_identifier));
5979 return name_hint (bm.get_best_meaningful_candidate (), NULL);
5982 /* Subroutine of outer_binding.
5984 Returns TRUE if BINDING is a binding to a template parameter of
5985 SCOPE. In that case SCOPE is the scope of a primary template
5986 parameter -- in the sense of G++, i.e, a template that has its own
5987 template header.
5989 Returns FALSE otherwise. */
5991 static bool
5992 binding_to_template_parms_of_scope_p (cxx_binding *binding,
5993 cp_binding_level *scope)
5995 tree binding_value, tmpl, tinfo;
5996 int level;
5998 if (!binding || !scope || !scope->this_entity)
5999 return false;
6001 binding_value = binding->value ? binding->value : binding->type;
6002 tinfo = get_template_info (scope->this_entity);
6004 /* BINDING_VALUE must be a template parm. */
6005 if (binding_value == NULL_TREE
6006 || (!DECL_P (binding_value)
6007 || !DECL_TEMPLATE_PARM_P (binding_value)))
6008 return false;
6010 /* The level of BINDING_VALUE. */
6011 level =
6012 template_type_parameter_p (binding_value)
6013 ? TEMPLATE_PARM_LEVEL (TEMPLATE_TYPE_PARM_INDEX
6014 (TREE_TYPE (binding_value)))
6015 : TEMPLATE_PARM_LEVEL (DECL_INITIAL (binding_value));
6017 /* The template of the current scope, iff said scope is a primary
6018 template. */
6019 tmpl = (tinfo
6020 && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
6021 ? TI_TEMPLATE (tinfo)
6022 : NULL_TREE);
6024 /* If the level of the parm BINDING_VALUE equals the depth of TMPL,
6025 then BINDING_VALUE is a parameter of TMPL. */
6026 return (tmpl && level == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)));
6029 /* Return the innermost non-namespace binding for NAME from a scope
6030 containing BINDING, or, if BINDING is NULL, the current scope.
6031 Please note that for a given template, the template parameters are
6032 considered to be in the scope containing the current scope.
6033 If CLASS_P is false, then class bindings are ignored. */
6035 cxx_binding *
6036 outer_binding (tree name,
6037 cxx_binding *binding,
6038 bool class_p)
6040 cxx_binding *outer;
6041 cp_binding_level *scope;
6042 cp_binding_level *outer_scope;
6044 if (binding)
6046 scope = binding->scope->level_chain;
6047 outer = binding->previous;
6049 else
6051 scope = current_binding_level;
6052 outer = IDENTIFIER_BINDING (name);
6054 outer_scope = outer ? outer->scope : NULL;
6056 /* Because we create class bindings lazily, we might be missing a
6057 class binding for NAME. If there are any class binding levels
6058 between the LAST_BINDING_LEVEL and the scope in which OUTER was
6059 declared, we must lookup NAME in those class scopes. */
6060 if (class_p)
6061 while (scope && scope != outer_scope && scope->kind != sk_namespace)
6063 if (scope->kind == sk_class)
6065 cxx_binding *class_binding;
6067 class_binding = get_class_binding (name, scope);
6068 if (class_binding)
6070 /* Thread this new class-scope binding onto the
6071 IDENTIFIER_BINDING list so that future lookups
6072 find it quickly. */
6073 class_binding->previous = outer;
6074 if (binding)
6075 binding->previous = class_binding;
6076 else
6077 IDENTIFIER_BINDING (name) = class_binding;
6078 return class_binding;
6081 /* If we are in a member template, the template parms of the member
6082 template are considered to be inside the scope of the containing
6083 class, but within G++ the class bindings are all pushed between the
6084 template parms and the function body. So if the outer binding is
6085 a template parm for the current scope, return it now rather than
6086 look for a class binding. */
6087 if (outer_scope && outer_scope->kind == sk_template_parms
6088 && binding_to_template_parms_of_scope_p (outer, scope))
6089 return outer;
6091 scope = scope->level_chain;
6094 return outer;
6097 /* Return the innermost block-scope or class-scope value binding for
6098 NAME, or NULL_TREE if there is no such binding. */
6100 tree
6101 innermost_non_namespace_value (tree name)
6103 cxx_binding *binding;
6104 binding = outer_binding (name, /*binding=*/NULL, /*class_p=*/true);
6105 return binding ? binding->value : NULL_TREE;
6108 /* Look up NAME in the current binding level and its superiors in the
6109 namespace of variables, functions and typedefs. Return a ..._DECL
6110 node of some kind representing its definition if there is only one
6111 such declaration, or return a TREE_LIST with all the overloaded
6112 definitions if there are many, or return 0 if it is undefined.
6113 Hidden name, either friend declaration or built-in function, are
6114 not ignored.
6116 If PREFER_TYPE is > 0, we prefer TYPE_DECLs or namespaces.
6117 If PREFER_TYPE is > 1, we reject non-type decls (e.g. namespaces).
6118 Otherwise we prefer non-TYPE_DECLs.
6120 If NONCLASS is nonzero, bindings in class scopes are ignored. If
6121 BLOCK_P is false, bindings in block scopes are ignored. */
6123 static tree
6124 lookup_name_real_1 (tree name, int prefer_type, int nonclass, bool block_p,
6125 int namespaces_only, int flags)
6127 cxx_binding *iter;
6128 tree val = NULL_TREE;
6130 query_oracle (name);
6132 /* Conversion operators are handled specially because ordinary
6133 unqualified name lookup will not find template conversion
6134 operators. */
6135 if (IDENTIFIER_CONV_OP_P (name))
6137 cp_binding_level *level;
6139 for (level = current_binding_level;
6140 level && level->kind != sk_namespace;
6141 level = level->level_chain)
6143 tree class_type;
6144 tree operators;
6146 /* A conversion operator can only be declared in a class
6147 scope. */
6148 if (level->kind != sk_class)
6149 continue;
6151 /* Lookup the conversion operator in the class. */
6152 class_type = level->this_entity;
6153 operators = lookup_fnfields (class_type, name, /*protect=*/0);
6154 if (operators)
6155 return operators;
6158 return NULL_TREE;
6161 flags |= lookup_flags (prefer_type, namespaces_only);
6163 /* First, look in non-namespace scopes. */
6165 if (current_class_type == NULL_TREE)
6166 nonclass = 1;
6168 if (block_p || !nonclass)
6169 for (iter = outer_binding (name, NULL, !nonclass);
6170 iter;
6171 iter = outer_binding (name, iter, !nonclass))
6173 tree binding;
6175 /* Skip entities we don't want. */
6176 if (LOCAL_BINDING_P (iter) ? !block_p : nonclass)
6177 continue;
6179 /* If this is the kind of thing we're looking for, we're done. */
6180 if (qualify_lookup (iter->value, flags))
6181 binding = iter->value;
6182 else if ((flags & LOOKUP_PREFER_TYPES)
6183 && qualify_lookup (iter->type, flags))
6184 binding = iter->type;
6185 else
6186 binding = NULL_TREE;
6188 if (binding)
6190 if (TREE_CODE (binding) == TYPE_DECL && DECL_HIDDEN_P (binding))
6192 /* A non namespace-scope binding can only be hidden in the
6193 presence of a local class, due to friend declarations.
6195 In particular, consider:
6197 struct C;
6198 void f() {
6199 struct A {
6200 friend struct B;
6201 friend struct C;
6202 void g() {
6203 B* b; // error: B is hidden
6204 C* c; // OK, finds ::C
6207 B *b; // error: B is hidden
6208 C *c; // OK, finds ::C
6209 struct B {};
6210 B *bb; // OK
6213 The standard says that "B" is a local class in "f"
6214 (but not nested within "A") -- but that name lookup
6215 for "B" does not find this declaration until it is
6216 declared directly with "f".
6218 In particular:
6220 [class.friend]
6222 If a friend declaration appears in a local class and
6223 the name specified is an unqualified name, a prior
6224 declaration is looked up without considering scopes
6225 that are outside the innermost enclosing non-class
6226 scope. For a friend function declaration, if there is
6227 no prior declaration, the program is ill-formed. For a
6228 friend class declaration, if there is no prior
6229 declaration, the class that is specified belongs to the
6230 innermost enclosing non-class scope, but if it is
6231 subsequently referenced, its name is not found by name
6232 lookup until a matching declaration is provided in the
6233 innermost enclosing nonclass scope.
6235 So just keep looking for a non-hidden binding.
6237 gcc_assert (TREE_CODE (binding) == TYPE_DECL);
6238 continue;
6240 val = binding;
6241 break;
6245 /* Now lookup in namespace scopes. */
6246 if (!val)
6248 name_lookup lookup (name, flags);
6249 if (lookup.search_unqualified
6250 (current_decl_namespace (), current_binding_level))
6251 val = lookup.value;
6254 /* If we have a single function from a using decl, pull it out. */
6255 if (val && TREE_CODE (val) == OVERLOAD && !really_overloaded_fn (val))
6256 val = OVL_FUNCTION (val);
6258 return val;
6261 /* Wrapper for lookup_name_real_1. */
6263 tree
6264 lookup_name_real (tree name, int prefer_type, int nonclass, bool block_p,
6265 int namespaces_only, int flags)
6267 tree ret;
6268 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6269 ret = lookup_name_real_1 (name, prefer_type, nonclass, block_p,
6270 namespaces_only, flags);
6271 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6272 return ret;
6275 tree
6276 lookup_name_nonclass (tree name)
6278 return lookup_name_real (name, 0, 1, /*block_p=*/true, 0, 0);
6281 tree
6282 lookup_name (tree name)
6284 return lookup_name_real (name, 0, 0, /*block_p=*/true, 0, 0);
6287 tree
6288 lookup_name_prefer_type (tree name, int prefer_type)
6290 return lookup_name_real (name, prefer_type, 0, /*block_p=*/true, 0, 0);
6293 /* Look up NAME for type used in elaborated name specifier in
6294 the scopes given by SCOPE. SCOPE can be either TS_CURRENT or
6295 TS_WITHIN_ENCLOSING_NON_CLASS. Although not implied by the
6296 name, more scopes are checked if cleanup or template parameter
6297 scope is encountered.
6299 Unlike lookup_name_real, we make sure that NAME is actually
6300 declared in the desired scope, not from inheritance, nor using
6301 directive. For using declaration, there is DR138 still waiting
6302 to be resolved. Hidden name coming from an earlier friend
6303 declaration is also returned.
6305 A TYPE_DECL best matching the NAME is returned. Catching error
6306 and issuing diagnostics are caller's responsibility. */
6308 static tree
6309 lookup_type_scope_1 (tree name, tag_scope scope)
6311 cxx_binding *iter = NULL;
6312 tree val = NULL_TREE;
6313 cp_binding_level *level = NULL;
6315 /* Look in non-namespace scope first. */
6316 if (current_binding_level->kind != sk_namespace)
6317 iter = outer_binding (name, NULL, /*class_p=*/ true);
6318 for (; iter; iter = outer_binding (name, iter, /*class_p=*/ true))
6320 /* Check if this is the kind of thing we're looking for.
6321 If SCOPE is TS_CURRENT, also make sure it doesn't come from
6322 base class. For ITER->VALUE, we can simply use
6323 INHERITED_VALUE_BINDING_P. For ITER->TYPE, we have to use
6324 our own check.
6326 We check ITER->TYPE before ITER->VALUE in order to handle
6327 typedef struct C {} C;
6328 correctly. */
6330 if (qualify_lookup (iter->type, LOOKUP_PREFER_TYPES)
6331 && (scope != ts_current
6332 || LOCAL_BINDING_P (iter)
6333 || DECL_CONTEXT (iter->type) == iter->scope->this_entity))
6334 val = iter->type;
6335 else if ((scope != ts_current
6336 || !INHERITED_VALUE_BINDING_P (iter))
6337 && qualify_lookup (iter->value, LOOKUP_PREFER_TYPES))
6338 val = iter->value;
6340 if (val)
6341 break;
6344 /* Look in namespace scope. */
6345 if (val)
6346 level = iter->scope;
6347 else
6349 tree ns = current_decl_namespace ();
6351 if (tree *slot = find_namespace_slot (ns, name))
6353 /* If this is the kind of thing we're looking for, we're done. */
6354 if (tree type = MAYBE_STAT_TYPE (*slot))
6355 if (qualify_lookup (type, LOOKUP_PREFER_TYPES))
6356 val = type;
6357 if (!val)
6359 if (tree decl = MAYBE_STAT_DECL (*slot))
6360 if (qualify_lookup (decl, LOOKUP_PREFER_TYPES))
6361 val = decl;
6363 level = NAMESPACE_LEVEL (ns);
6367 /* Type found, check if it is in the allowed scopes, ignoring cleanup
6368 and template parameter scopes. */
6369 if (val)
6371 cp_binding_level *b = current_binding_level;
6372 while (b)
6374 if (level == b)
6375 return val;
6377 if (b->kind == sk_cleanup || b->kind == sk_template_parms
6378 || b->kind == sk_function_parms)
6379 b = b->level_chain;
6380 else if (b->kind == sk_class
6381 && scope == ts_within_enclosing_non_class)
6382 b = b->level_chain;
6383 else
6384 break;
6388 return NULL_TREE;
6391 /* Wrapper for lookup_type_scope_1. */
6393 tree
6394 lookup_type_scope (tree name, tag_scope scope)
6396 tree ret;
6397 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6398 ret = lookup_type_scope_1 (name, scope);
6399 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6400 return ret;
6403 /* Returns true iff DECL is a block-scope extern declaration of a function
6404 or variable. */
6406 bool
6407 is_local_extern (tree decl)
6409 cxx_binding *binding;
6411 /* For functions, this is easy. */
6412 if (TREE_CODE (decl) == FUNCTION_DECL)
6413 return DECL_LOCAL_FUNCTION_P (decl);
6415 if (!VAR_P (decl))
6416 return false;
6417 if (!current_function_decl)
6418 return false;
6420 /* For variables, this is not easy. We need to look at the binding stack
6421 for the identifier to see whether the decl we have is a local. */
6422 for (binding = IDENTIFIER_BINDING (DECL_NAME (decl));
6423 binding && binding->scope->kind != sk_namespace;
6424 binding = binding->previous)
6425 if (binding->value == decl)
6426 return LOCAL_BINDING_P (binding);
6428 return false;
6431 /* The type TYPE is being declared. If it is a class template, or a
6432 specialization of a class template, do any processing required and
6433 perform error-checking. If IS_FRIEND is nonzero, this TYPE is
6434 being declared a friend. B is the binding level at which this TYPE
6435 should be bound.
6437 Returns the TYPE_DECL for TYPE, which may have been altered by this
6438 processing. */
6440 static tree
6441 maybe_process_template_type_declaration (tree type, int is_friend,
6442 cp_binding_level *b)
6444 tree decl = TYPE_NAME (type);
6446 if (processing_template_parmlist)
6447 /* You can't declare a new template type in a template parameter
6448 list. But, you can declare a non-template type:
6450 template <class A*> struct S;
6452 is a forward-declaration of `A'. */
6454 else if (b->kind == sk_namespace
6455 && current_binding_level->kind != sk_namespace)
6456 /* If this new type is being injected into a containing scope,
6457 then it's not a template type. */
6459 else
6461 gcc_assert (MAYBE_CLASS_TYPE_P (type)
6462 || TREE_CODE (type) == ENUMERAL_TYPE);
6464 if (processing_template_decl)
6466 /* This may change after the call to
6467 push_template_decl_real, but we want the original value. */
6468 tree name = DECL_NAME (decl);
6470 decl = push_template_decl_real (decl, is_friend);
6471 if (decl == error_mark_node)
6472 return error_mark_node;
6474 /* If the current binding level is the binding level for the
6475 template parameters (see the comment in
6476 begin_template_parm_list) and the enclosing level is a class
6477 scope, and we're not looking at a friend, push the
6478 declaration of the member class into the class scope. In the
6479 friend case, push_template_decl will already have put the
6480 friend into global scope, if appropriate. */
6481 if (TREE_CODE (type) != ENUMERAL_TYPE
6482 && !is_friend && b->kind == sk_template_parms
6483 && b->level_chain->kind == sk_class)
6485 finish_member_declaration (CLASSTYPE_TI_TEMPLATE (type));
6487 if (!COMPLETE_TYPE_P (current_class_type))
6489 maybe_add_class_template_decl_list (current_class_type,
6490 type, /*friend_p=*/0);
6491 /* Put this UTD in the table of UTDs for the class. */
6492 if (CLASSTYPE_NESTED_UTDS (current_class_type) == NULL)
6493 CLASSTYPE_NESTED_UTDS (current_class_type) =
6494 binding_table_new (SCOPE_DEFAULT_HT_SIZE);
6496 binding_table_insert
6497 (CLASSTYPE_NESTED_UTDS (current_class_type), name, type);
6503 return decl;
6506 /* Push a tag name NAME for struct/class/union/enum type TYPE. In case
6507 that the NAME is a class template, the tag is processed but not pushed.
6509 The pushed scope depend on the SCOPE parameter:
6510 - When SCOPE is TS_CURRENT, put it into the inner-most non-sk_cleanup
6511 scope.
6512 - When SCOPE is TS_GLOBAL, put it in the inner-most non-class and
6513 non-template-parameter scope. This case is needed for forward
6514 declarations.
6515 - When SCOPE is TS_WITHIN_ENCLOSING_NON_CLASS, this is similar to
6516 TS_GLOBAL case except that names within template-parameter scopes
6517 are not pushed at all.
6519 Returns TYPE upon success and ERROR_MARK_NODE otherwise. */
6521 static tree
6522 do_pushtag (tree name, tree type, tag_scope scope)
6524 tree decl;
6526 cp_binding_level *b = current_binding_level;
6527 while (true)
6529 if (/* Cleanup scopes are not scopes from the point of view of
6530 the language. */
6531 b->kind == sk_cleanup
6532 /* Neither are function parameter scopes. */
6533 || b->kind == sk_function_parms
6534 /* Neither are the scopes used to hold template parameters
6535 for an explicit specialization. For an ordinary template
6536 declaration, these scopes are not scopes from the point of
6537 view of the language. */
6538 || (b->kind == sk_template_parms
6539 && (b->explicit_spec_p || scope == ts_global)))
6540 b = b->level_chain;
6541 else if (b->kind == sk_class
6542 && scope != ts_current)
6544 b = b->level_chain;
6545 if (b->kind == sk_template_parms)
6546 b = b->level_chain;
6548 else
6549 break;
6552 gcc_assert (identifier_p (name));
6554 /* Do C++ gratuitous typedefing. */
6555 if (identifier_type_value_1 (name) != type)
6557 tree tdef;
6558 int in_class = 0;
6559 tree context = TYPE_CONTEXT (type);
6561 if (! context)
6563 cp_binding_level *cb = b;
6564 while (cb->kind != sk_namespace
6565 && cb->kind != sk_class
6566 && (cb->kind != sk_function_parms
6567 || !cb->this_entity))
6568 cb = cb->level_chain;
6569 tree cs = cb->this_entity;
6571 gcc_checking_assert (TREE_CODE (cs) == FUNCTION_DECL
6572 ? cs == current_function_decl
6573 : TYPE_P (cs) ? cs == current_class_type
6574 : cs == current_namespace);
6576 if (scope == ts_current
6577 || (cs && TREE_CODE (cs) == FUNCTION_DECL))
6578 context = cs;
6579 else if (cs && TYPE_P (cs))
6580 /* When declaring a friend class of a local class, we want
6581 to inject the newly named class into the scope
6582 containing the local class, not the namespace
6583 scope. */
6584 context = decl_function_context (get_type_decl (cs));
6586 if (!context)
6587 context = current_namespace;
6589 if (b->kind == sk_class
6590 || (b->kind == sk_template_parms
6591 && b->level_chain->kind == sk_class))
6592 in_class = 1;
6594 tdef = create_implicit_typedef (name, type);
6595 DECL_CONTEXT (tdef) = FROB_CONTEXT (context);
6596 if (scope == ts_within_enclosing_non_class)
6598 /* This is a friend. Make this TYPE_DECL node hidden from
6599 ordinary name lookup. Its corresponding TEMPLATE_DECL
6600 will be marked in push_template_decl_real. */
6601 retrofit_lang_decl (tdef);
6602 DECL_ANTICIPATED (tdef) = 1;
6603 DECL_FRIEND_P (tdef) = 1;
6606 decl = maybe_process_template_type_declaration
6607 (type, scope == ts_within_enclosing_non_class, b);
6608 if (decl == error_mark_node)
6609 return decl;
6611 if (b->kind == sk_class)
6613 if (!TYPE_BEING_DEFINED (current_class_type))
6614 /* Don't push anywhere if the class is complete; a lambda in an
6615 NSDMI is not a member of the class. */
6617 else if (!PROCESSING_REAL_TEMPLATE_DECL_P ())
6618 /* Put this TYPE_DECL on the TYPE_FIELDS list for the
6619 class. But if it's a member template class, we want
6620 the TEMPLATE_DECL, not the TYPE_DECL, so this is done
6621 later. */
6622 finish_member_declaration (decl);
6623 else
6624 pushdecl_class_level (decl);
6626 else if (b->kind != sk_template_parms)
6628 decl = do_pushdecl_with_scope (decl, b, /*is_friend=*/false);
6629 if (decl == error_mark_node)
6630 return decl;
6632 if (DECL_CONTEXT (decl) == std_node
6633 && init_list_identifier == DECL_NAME (TYPE_NAME (type))
6634 && !CLASSTYPE_TEMPLATE_INFO (type))
6636 error ("declaration of %<std::initializer_list%> does not match "
6637 "%<#include <initializer_list>%>, isn't a template");
6638 return error_mark_node;
6642 if (! in_class)
6643 set_identifier_type_value_with_scope (name, tdef, b);
6645 TYPE_CONTEXT (type) = DECL_CONTEXT (decl);
6647 /* If this is a local class, keep track of it. We need this
6648 information for name-mangling, and so that it is possible to
6649 find all function definitions in a translation unit in a
6650 convenient way. (It's otherwise tricky to find a member
6651 function definition it's only pointed to from within a local
6652 class.) */
6653 if (TYPE_FUNCTION_SCOPE_P (type))
6655 if (processing_template_decl)
6657 /* Push a DECL_EXPR so we call pushtag at the right time in
6658 template instantiation rather than in some nested context. */
6659 add_decl_expr (decl);
6661 /* Lambdas use LAMBDA_EXPR_DISCRIMINATOR instead. */
6662 else if (!LAMBDA_TYPE_P (type))
6663 vec_safe_push (local_classes, type);
6667 if (b->kind == sk_class
6668 && !COMPLETE_TYPE_P (current_class_type))
6670 maybe_add_class_template_decl_list (current_class_type,
6671 type, /*friend_p=*/0);
6673 if (CLASSTYPE_NESTED_UTDS (current_class_type) == NULL)
6674 CLASSTYPE_NESTED_UTDS (current_class_type)
6675 = binding_table_new (SCOPE_DEFAULT_HT_SIZE);
6677 binding_table_insert
6678 (CLASSTYPE_NESTED_UTDS (current_class_type), name, type);
6681 decl = TYPE_NAME (type);
6682 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
6684 /* Set type visibility now if this is a forward declaration. */
6685 TREE_PUBLIC (decl) = 1;
6686 determine_visibility (decl);
6688 return type;
6691 /* Wrapper for do_pushtag. */
6693 tree
6694 pushtag (tree name, tree type, tag_scope scope)
6696 tree ret;
6697 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6698 ret = do_pushtag (name, type, scope);
6699 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6700 return ret;
6704 /* Subroutines for reverting temporarily to top-level for instantiation
6705 of templates and such. We actually need to clear out the class- and
6706 local-value slots of all identifiers, so that only the global values
6707 are at all visible. Simply setting current_binding_level to the global
6708 scope isn't enough, because more binding levels may be pushed. */
6709 struct saved_scope *scope_chain;
6711 /* Return true if ID has not already been marked. */
6713 static inline bool
6714 store_binding_p (tree id)
6716 if (!id || !IDENTIFIER_BINDING (id))
6717 return false;
6719 if (IDENTIFIER_MARKED (id))
6720 return false;
6722 return true;
6725 /* Add an appropriate binding to *OLD_BINDINGS which needs to already
6726 have enough space reserved. */
6728 static void
6729 store_binding (tree id, vec<cxx_saved_binding, va_gc> **old_bindings)
6731 cxx_saved_binding saved;
6733 gcc_checking_assert (store_binding_p (id));
6735 IDENTIFIER_MARKED (id) = 1;
6737 saved.identifier = id;
6738 saved.binding = IDENTIFIER_BINDING (id);
6739 saved.real_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
6740 (*old_bindings)->quick_push (saved);
6741 IDENTIFIER_BINDING (id) = NULL;
6744 static void
6745 store_bindings (tree names, vec<cxx_saved_binding, va_gc> **old_bindings)
6747 static vec<tree> bindings_need_stored;
6748 tree t, id;
6749 size_t i;
6751 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6752 for (t = names; t; t = TREE_CHAIN (t))
6754 if (TREE_CODE (t) == TREE_LIST)
6755 id = TREE_PURPOSE (t);
6756 else
6757 id = DECL_NAME (t);
6759 if (store_binding_p (id))
6760 bindings_need_stored.safe_push (id);
6762 if (!bindings_need_stored.is_empty ())
6764 vec_safe_reserve_exact (*old_bindings, bindings_need_stored.length ());
6765 for (i = 0; bindings_need_stored.iterate (i, &id); ++i)
6767 /* We can apparently have duplicates in NAMES. */
6768 if (store_binding_p (id))
6769 store_binding (id, old_bindings);
6771 bindings_need_stored.truncate (0);
6773 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6776 /* Like store_bindings, but NAMES is a vector of cp_class_binding
6777 objects, rather than a TREE_LIST. */
6779 static void
6780 store_class_bindings (vec<cp_class_binding, va_gc> *names,
6781 vec<cxx_saved_binding, va_gc> **old_bindings)
6783 static vec<tree> bindings_need_stored;
6784 size_t i;
6785 cp_class_binding *cb;
6787 for (i = 0; vec_safe_iterate (names, i, &cb); ++i)
6788 if (store_binding_p (cb->identifier))
6789 bindings_need_stored.safe_push (cb->identifier);
6790 if (!bindings_need_stored.is_empty ())
6792 tree id;
6793 vec_safe_reserve_exact (*old_bindings, bindings_need_stored.length ());
6794 for (i = 0; bindings_need_stored.iterate (i, &id); ++i)
6795 store_binding (id, old_bindings);
6796 bindings_need_stored.truncate (0);
6800 /* A chain of saved_scope structures awaiting reuse. */
6802 static GTY((deletable)) struct saved_scope *free_saved_scope;
6804 static void
6805 do_push_to_top_level (void)
6807 struct saved_scope *s;
6808 cp_binding_level *b;
6809 cxx_saved_binding *sb;
6810 size_t i;
6811 bool need_pop;
6813 /* Reuse or create a new structure for this saved scope. */
6814 if (free_saved_scope != NULL)
6816 s = free_saved_scope;
6817 free_saved_scope = s->prev;
6819 vec<cxx_saved_binding, va_gc> *old_bindings = s->old_bindings;
6820 memset (s, 0, sizeof (*s));
6821 /* Also reuse the structure's old_bindings vector. */
6822 vec_safe_truncate (old_bindings, 0);
6823 s->old_bindings = old_bindings;
6825 else
6826 s = ggc_cleared_alloc<saved_scope> ();
6828 b = scope_chain ? current_binding_level : 0;
6830 /* If we're in the middle of some function, save our state. */
6831 if (cfun)
6833 need_pop = true;
6834 push_function_context ();
6836 else
6837 need_pop = false;
6839 if (scope_chain && previous_class_level)
6840 store_class_bindings (previous_class_level->class_shadowed,
6841 &s->old_bindings);
6843 /* Have to include the global scope, because class-scope decls
6844 aren't listed anywhere useful. */
6845 for (; b; b = b->level_chain)
6847 tree t;
6849 /* Template IDs are inserted into the global level. If they were
6850 inserted into namespace level, finish_file wouldn't find them
6851 when doing pending instantiations. Therefore, don't stop at
6852 namespace level, but continue until :: . */
6853 if (global_scope_p (b))
6854 break;
6856 store_bindings (b->names, &s->old_bindings);
6857 /* We also need to check class_shadowed to save class-level type
6858 bindings, since pushclass doesn't fill in b->names. */
6859 if (b->kind == sk_class)
6860 store_class_bindings (b->class_shadowed, &s->old_bindings);
6862 /* Unwind type-value slots back to top level. */
6863 for (t = b->type_shadowed; t; t = TREE_CHAIN (t))
6864 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (t), TREE_VALUE (t));
6867 FOR_EACH_VEC_SAFE_ELT (s->old_bindings, i, sb)
6868 IDENTIFIER_MARKED (sb->identifier) = 0;
6870 s->prev = scope_chain;
6871 s->bindings = b;
6872 s->need_pop_function_context = need_pop;
6873 s->function_decl = current_function_decl;
6874 s->unevaluated_operand = cp_unevaluated_operand;
6875 s->inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
6876 s->x_stmt_tree.stmts_are_full_exprs_p = true;
6878 scope_chain = s;
6879 current_function_decl = NULL_TREE;
6880 current_lang_base = NULL;
6881 current_lang_name = lang_name_cplusplus;
6882 current_namespace = global_namespace;
6883 push_class_stack ();
6884 cp_unevaluated_operand = 0;
6885 c_inhibit_evaluation_warnings = 0;
6888 static void
6889 do_pop_from_top_level (void)
6891 struct saved_scope *s = scope_chain;
6892 cxx_saved_binding *saved;
6893 size_t i;
6895 /* Clear out class-level bindings cache. */
6896 if (previous_class_level)
6897 invalidate_class_lookup_cache ();
6898 pop_class_stack ();
6900 release_tree_vector (current_lang_base);
6902 scope_chain = s->prev;
6903 FOR_EACH_VEC_SAFE_ELT (s->old_bindings, i, saved)
6905 tree id = saved->identifier;
6907 IDENTIFIER_BINDING (id) = saved->binding;
6908 SET_IDENTIFIER_TYPE_VALUE (id, saved->real_type_value);
6911 /* If we were in the middle of compiling a function, restore our
6912 state. */
6913 if (s->need_pop_function_context)
6914 pop_function_context ();
6915 current_function_decl = s->function_decl;
6916 cp_unevaluated_operand = s->unevaluated_operand;
6917 c_inhibit_evaluation_warnings = s->inhibit_evaluation_warnings;
6919 /* Make this saved_scope structure available for reuse by
6920 push_to_top_level. */
6921 s->prev = free_saved_scope;
6922 free_saved_scope = s;
6925 /* Push into the scope of the namespace NS, even if it is deeply
6926 nested within another namespace. */
6928 static void
6929 do_push_nested_namespace (tree ns)
6931 if (ns == global_namespace)
6932 do_push_to_top_level ();
6933 else
6935 do_push_nested_namespace (CP_DECL_CONTEXT (ns));
6936 gcc_checking_assert
6937 (find_namespace_value (current_namespace, DECL_NAME (ns)) == ns);
6938 resume_scope (NAMESPACE_LEVEL (ns));
6939 current_namespace = ns;
6943 /* Pop back from the scope of the namespace NS, which was previously
6944 entered with push_nested_namespace. */
6946 static void
6947 do_pop_nested_namespace (tree ns)
6949 while (ns != global_namespace)
6951 ns = CP_DECL_CONTEXT (ns);
6952 current_namespace = ns;
6953 leave_scope ();
6956 do_pop_from_top_level ();
6959 /* Add TARGET to USINGS, if it does not already exist there.
6960 We used to build the complete graph of usings at this point, from
6961 the POV of the source namespaces. Now we build that as we perform
6962 the unqualified search. */
6964 static void
6965 add_using_namespace (vec<tree, va_gc> *&usings, tree target)
6967 if (usings)
6968 for (unsigned ix = usings->length (); ix--;)
6969 if ((*usings)[ix] == target)
6970 return;
6972 vec_safe_push (usings, target);
6975 /* Tell the debug system of a using directive. */
6977 static void
6978 emit_debug_info_using_namespace (tree from, tree target, bool implicit)
6980 /* Emit debugging info. */
6981 tree context = from != global_namespace ? from : NULL_TREE;
6982 debug_hooks->imported_module_or_decl (target, NULL_TREE, context, false,
6983 implicit);
6986 /* Process a namespace-scope using directive. */
6988 void
6989 finish_namespace_using_directive (tree target, tree attribs)
6991 gcc_checking_assert (namespace_bindings_p ());
6992 if (target == error_mark_node)
6993 return;
6995 add_using_namespace (DECL_NAMESPACE_USING (current_namespace),
6996 ORIGINAL_NAMESPACE (target));
6997 emit_debug_info_using_namespace (current_namespace,
6998 ORIGINAL_NAMESPACE (target), false);
7000 if (attribs == error_mark_node)
7001 return;
7003 for (tree a = attribs; a; a = TREE_CHAIN (a))
7005 tree name = get_attribute_name (a);
7006 if (is_attribute_p ("strong", name))
7008 warning (0, "strong using directive no longer supported");
7009 if (CP_DECL_CONTEXT (target) == current_namespace)
7010 inform (DECL_SOURCE_LOCATION (target),
7011 "you may use an inline namespace instead");
7013 else
7014 warning (OPT_Wattributes, "%qD attribute directive ignored", name);
7018 /* Process a function-scope using-directive. */
7020 void
7021 finish_local_using_directive (tree target, tree attribs)
7023 gcc_checking_assert (local_bindings_p ());
7024 if (target == error_mark_node)
7025 return;
7027 if (attribs)
7028 warning (OPT_Wattributes, "attributes ignored on local using directive");
7030 add_stmt (build_stmt (input_location, USING_STMT, target));
7032 add_using_namespace (current_binding_level->using_directives,
7033 ORIGINAL_NAMESPACE (target));
7036 /* Pushes X into the global namespace. */
7038 tree
7039 pushdecl_top_level (tree x, bool is_friend)
7041 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
7042 do_push_to_top_level ();
7043 x = pushdecl_namespace_level (x, is_friend);
7044 do_pop_from_top_level ();
7045 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
7046 return x;
7049 /* Pushes X into the global namespace and calls cp_finish_decl to
7050 register the variable, initializing it with INIT. */
7052 tree
7053 pushdecl_top_level_and_finish (tree x, tree init)
7055 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
7056 do_push_to_top_level ();
7057 x = pushdecl_namespace_level (x, false);
7058 cp_finish_decl (x, init, false, NULL_TREE, 0);
7059 do_pop_from_top_level ();
7060 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
7061 return x;
7064 /* Enter the namespaces from current_namerspace to NS. */
7066 static int
7067 push_inline_namespaces (tree ns)
7069 int count = 0;
7070 if (ns != current_namespace)
7072 gcc_assert (ns != global_namespace);
7073 count += push_inline_namespaces (CP_DECL_CONTEXT (ns));
7074 resume_scope (NAMESPACE_LEVEL (ns));
7075 current_namespace = ns;
7076 count++;
7078 return count;
7081 /* Push into the scope of the NAME namespace. If NAME is NULL_TREE,
7082 then we enter an anonymous namespace. If MAKE_INLINE is true, then
7083 we create an inline namespace (it is up to the caller to check upon
7084 redefinition). Return the number of namespaces entered. */
7087 push_namespace (tree name, bool make_inline)
7089 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
7090 int count = 0;
7092 /* We should not get here if the global_namespace is not yet constructed
7093 nor if NAME designates the global namespace: The global scope is
7094 constructed elsewhere. */
7095 gcc_checking_assert (global_namespace != NULL && name != global_identifier);
7097 tree ns = NULL_TREE;
7099 name_lookup lookup (name, 0);
7100 if (!lookup.search_qualified (current_namespace, /*usings=*/false))
7102 else if (TREE_CODE (lookup.value) != NAMESPACE_DECL)
7104 else if (tree dna = DECL_NAMESPACE_ALIAS (lookup.value))
7106 /* A namespace alias is not allowed here, but if the alias
7107 is for a namespace also inside the current scope,
7108 accept it with a diagnostic. That's better than dying
7109 horribly. */
7110 if (is_nested_namespace (current_namespace, CP_DECL_CONTEXT (dna)))
7112 error ("namespace alias %qD not allowed here, "
7113 "assuming %qD", lookup.value, dna);
7114 ns = dna;
7117 else
7118 ns = lookup.value;
7121 bool new_ns = false;
7122 if (ns)
7123 /* DR2061. NS might be a member of an inline namespace. We
7124 need to push into those namespaces. */
7125 count += push_inline_namespaces (CP_DECL_CONTEXT (ns));
7126 else
7128 ns = build_lang_decl (NAMESPACE_DECL, name, void_type_node);
7129 SCOPE_DEPTH (ns) = SCOPE_DEPTH (current_namespace) + 1;
7130 if (!SCOPE_DEPTH (ns))
7131 /* We only allow depth 255. */
7132 sorry ("cannot nest more than %d namespaces",
7133 SCOPE_DEPTH (current_namespace));
7134 DECL_CONTEXT (ns) = FROB_CONTEXT (current_namespace);
7135 new_ns = true;
7137 if (pushdecl (ns) == error_mark_node)
7138 ns = NULL_TREE;
7139 else
7141 if (!name)
7143 SET_DECL_ASSEMBLER_NAME (ns, anon_identifier);
7145 if (!make_inline)
7146 add_using_namespace (DECL_NAMESPACE_USING (current_namespace),
7147 ns);
7149 else if (TREE_PUBLIC (current_namespace))
7150 TREE_PUBLIC (ns) = 1;
7152 if (make_inline)
7154 DECL_NAMESPACE_INLINE_P (ns) = true;
7155 vec_safe_push (DECL_NAMESPACE_INLINEES (current_namespace), ns);
7158 if (!name || make_inline)
7159 emit_debug_info_using_namespace (current_namespace, ns, true);
7163 if (ns)
7165 if (make_inline && !DECL_NAMESPACE_INLINE_P (ns))
7167 error ("inline namespace must be specified at initial definition");
7168 inform (DECL_SOURCE_LOCATION (ns), "%qD defined here", ns);
7170 if (new_ns)
7171 begin_scope (sk_namespace, ns);
7172 else
7173 resume_scope (NAMESPACE_LEVEL (ns));
7174 current_namespace = ns;
7175 count++;
7178 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
7179 return count;
7182 /* Pop from the scope of the current namespace. */
7184 void
7185 pop_namespace (void)
7187 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
7189 gcc_assert (current_namespace != global_namespace);
7190 current_namespace = CP_DECL_CONTEXT (current_namespace);
7191 /* The binding level is not popped, as it might be re-opened later. */
7192 leave_scope ();
7194 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
7197 /* External entry points for do_{push_to/pop_from}_top_level. */
7199 void
7200 push_to_top_level (void)
7202 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
7203 do_push_to_top_level ();
7204 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
7207 void
7208 pop_from_top_level (void)
7210 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
7211 do_pop_from_top_level ();
7212 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
7215 /* External entry points for do_{push,pop}_nested_namespace. */
7217 void
7218 push_nested_namespace (tree ns)
7220 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
7221 do_push_nested_namespace (ns);
7222 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
7225 void
7226 pop_nested_namespace (tree ns)
7228 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
7229 gcc_assert (current_namespace == ns);
7230 do_pop_nested_namespace (ns);
7231 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
7234 /* Pop off extraneous binding levels left over due to syntax errors.
7235 We don't pop past namespaces, as they might be valid. */
7237 void
7238 pop_everything (void)
7240 if (ENABLE_SCOPE_CHECKING)
7241 verbatim ("XXX entering pop_everything ()\n");
7242 while (!namespace_bindings_p ())
7244 if (current_binding_level->kind == sk_class)
7245 pop_nested_class ();
7246 else
7247 poplevel (0, 0, 0);
7249 if (ENABLE_SCOPE_CHECKING)
7250 verbatim ("XXX leaving pop_everything ()\n");
7253 /* Emit debugging information for using declarations and directives.
7254 If input tree is overloaded fn then emit debug info for all
7255 candidates. */
7257 void
7258 cp_emit_debug_info_for_using (tree t, tree context)
7260 /* Don't try to emit any debug information if we have errors. */
7261 if (seen_error ())
7262 return;
7264 /* Ignore this FUNCTION_DECL if it refers to a builtin declaration
7265 of a builtin function. */
7266 if (TREE_CODE (t) == FUNCTION_DECL
7267 && DECL_EXTERNAL (t)
7268 && fndecl_built_in_p (t))
7269 return;
7271 /* Do not supply context to imported_module_or_decl, if
7272 it is a global namespace. */
7273 if (context == global_namespace)
7274 context = NULL_TREE;
7276 t = MAYBE_BASELINK_FUNCTIONS (t);
7278 /* FIXME: Handle TEMPLATE_DECLs. */
7279 for (lkp_iterator iter (t); iter; ++iter)
7281 tree fn = *iter;
7282 if (TREE_CODE (fn) != TEMPLATE_DECL)
7284 if (building_stmt_list_p ())
7285 add_stmt (build_stmt (input_location, USING_STMT, fn));
7286 else
7287 debug_hooks->imported_module_or_decl (fn, NULL_TREE, context,
7288 false, false);
7293 #include "gt-cp-name-lookup.h"