libgo: add misc/cgo files
[official-gcc.git] / gcc / cp / name-lookup.c
blob0df546a2e6a463c6ebd8c0982d8d234b799cdbdc
1 /* Definitions for C++ name lookup routines.
2 Copyright (C) 2003-2017 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 #include "system.h"
23 #include "coretypes.h"
24 #include "cp-tree.h"
25 #include "timevar.h"
26 #include "stringpool.h"
27 #include "print-tree.h"
28 #include "attribs.h"
29 #include "debug.h"
30 #include "c-family/c-pragma.h"
31 #include "params.h"
32 #include "gcc-rich-location.h"
33 #include "spellcheck-tree.h"
34 #include "parser.h"
36 static cxx_binding *cxx_binding_make (tree value, tree type);
37 static cp_binding_level *innermost_nonclass_level (void);
38 static void set_identifier_type_value_with_scope (tree id, tree decl,
39 cp_binding_level *b);
41 /* Create an overload suitable for recording an artificial TYPE_DECL
42 and another decl. We use this machanism to implement the struct
43 stat hack within a namespace. It'd be nice to use it everywhere. */
45 #define STAT_HACK_P(N) ((N) && TREE_CODE (N) == OVERLOAD && OVL_LOOKUP_P (N))
46 #define STAT_TYPE(N) TREE_TYPE (N)
47 #define STAT_DECL(N) OVL_FUNCTION (N)
48 #define MAYBE_STAT_DECL(N) (STAT_HACK_P (N) ? STAT_DECL (N) : N)
49 #define MAYBE_STAT_TYPE(N) (STAT_HACK_P (N) ? STAT_TYPE (N) : NULL_TREE)
51 /* Create a STAT_HACK node with DECL as the value binding and TYPE as
52 the type binding. */
54 static tree
55 stat_hack (tree decl = NULL_TREE, tree type = NULL_TREE)
57 tree result = make_node (OVERLOAD);
59 /* Mark this as a lookup, so we can tell this is a stat hack. */
60 OVL_LOOKUP_P (result) = true;
61 STAT_DECL (result) = decl;
62 STAT_TYPE (result) = type;
63 return result;
66 /* Create a local binding level for NAME. */
68 static cxx_binding *
69 create_local_binding (cp_binding_level *level, tree name)
71 cxx_binding *binding = cxx_binding_make (NULL, NULL);
73 INHERITED_VALUE_BINDING_P (binding) = false;
74 LOCAL_BINDING_P (binding) = true;
75 binding->scope = level;
76 binding->previous = IDENTIFIER_BINDING (name);
78 IDENTIFIER_BINDING (name) = binding;
80 return binding;
83 /* Find the binding for NAME in namespace NS. If CREATE_P is true,
84 make an empty binding if there wasn't one. */
86 static tree *
87 find_namespace_slot (tree ns, tree name, bool create_p = false)
89 tree *slot;
91 if (create_p)
93 bool existed;
94 slot = &DECL_NAMESPACE_BINDINGS (ns)->get_or_insert (name, &existed);
95 if (!existed)
96 *slot = NULL_TREE;
98 else
99 slot = DECL_NAMESPACE_BINDINGS (ns)->get (name);
100 return slot;
103 static tree
104 find_namespace_value (tree ns, tree name)
106 tree *b = find_namespace_slot (ns, name);
108 return b ? MAYBE_STAT_DECL (*b) : NULL_TREE;
111 /* Add DECL to the list of things declared in B. */
113 static void
114 add_decl_to_level (cp_binding_level *b, tree decl)
116 gcc_assert (b->kind != sk_class);
118 /* Make sure we don't create a circular list. xref_tag can end
119 up pushing the same artificial decl more than once. We
120 should have already detected that in update_binding. */
121 gcc_assert (b->names != decl);
123 /* We build up the list in reverse order, and reverse it later if
124 necessary. */
125 TREE_CHAIN (decl) = b->names;
126 b->names = decl;
128 /* If appropriate, add decl to separate list of statics. We
129 include extern variables because they might turn out to be
130 static later. It's OK for this list to contain a few false
131 positives. */
132 if (b->kind == sk_namespace
133 && ((VAR_P (decl)
134 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
135 || (TREE_CODE (decl) == FUNCTION_DECL
136 && (!TREE_PUBLIC (decl)
137 || decl_anon_ns_mem_p (decl)
138 || DECL_DECLARED_INLINE_P (decl)))))
139 vec_safe_push (static_decls, decl);
142 /* Find the binding for NAME in the local binding level B. */
144 static cxx_binding *
145 find_local_binding (cp_binding_level *b, tree name)
147 if (cxx_binding *binding = IDENTIFIER_BINDING (name))
148 for (;; b = b->level_chain)
150 if (binding->scope == b
151 && !(VAR_P (binding->value)
152 && DECL_DEAD_FOR_LOCAL (binding->value)))
153 return binding;
155 /* Cleanup contours are transparent to the language. */
156 if (b->kind != sk_cleanup)
157 break;
159 return NULL;
162 struct name_lookup
164 public:
165 typedef std::pair<tree, tree> using_pair;
166 typedef vec<using_pair, va_heap, vl_embed> using_queue;
168 public:
169 tree name; /* The identifier being looked for. */
170 tree value; /* A (possibly ambiguous) set of things found. */
171 tree type; /* A type that has been found. */
172 int flags; /* Lookup flags. */
173 bool deduping; /* Full deduping is needed because using declarations
174 are in play. */
175 vec<tree, va_heap, vl_embed> *scopes;
176 name_lookup *previous; /* Previously active lookup. */
178 protected:
179 /* Marked scope stack for outermost name lookup. */
180 static vec<tree, va_heap, vl_embed> *shared_scopes;
181 /* Currently active lookup. */
182 static name_lookup *active;
184 public:
185 name_lookup (tree n, int f = 0)
186 : name (n), value (NULL_TREE), type (NULL_TREE), flags (f),
187 deduping (false), scopes (NULL), previous (NULL)
189 preserve_state ();
191 ~name_lookup ()
193 restore_state ();
196 private: /* Uncopyable, unmovable, unassignable. I am a rock. */
197 name_lookup (const name_lookup &);
198 name_lookup &operator= (const name_lookup &);
200 protected:
201 static bool seen_p (tree scope)
203 return LOOKUP_SEEN_P (scope);
205 static bool found_p (tree scope)
207 return LOOKUP_FOUND_P (scope);
210 void mark_seen (tree scope); /* Mark and add to scope vector. */
211 static void mark_found (tree scope)
213 gcc_checking_assert (seen_p (scope));
214 LOOKUP_FOUND_P (scope) = true;
216 bool see_and_mark (tree scope)
218 bool ret = seen_p (scope);
219 if (!ret)
220 mark_seen (scope);
221 return ret;
223 bool find_and_mark (tree scope);
225 private:
226 void preserve_state ();
227 void restore_state ();
229 private:
230 static tree ambiguous (tree thing, tree current);
231 void add_overload (tree fns);
232 void add_value (tree new_val);
233 void add_type (tree new_type);
234 bool process_binding (tree val_bind, tree type_bind);
236 /* Look in only namespace. */
237 bool search_namespace_only (tree scope);
238 /* Look in namespace and its (recursive) inlines. Ignore using
239 directives. Return true if something found (inc dups). */
240 bool search_namespace (tree scope);
241 /* Look in the using directives of namespace + inlines using
242 qualified lookup rules. */
243 bool search_usings (tree scope);
245 private:
246 using_queue *queue_namespace (using_queue *queue, int depth, tree scope);
247 using_queue *do_queue_usings (using_queue *queue, int depth,
248 vec<tree, va_gc> *usings);
249 using_queue *queue_usings (using_queue *queue, int depth,
250 vec<tree, va_gc> *usings)
252 if (usings)
253 queue = do_queue_usings (queue, depth, usings);
254 return queue;
257 private:
258 void add_fns (tree);
260 void adl_expr (tree);
261 void adl_type (tree);
262 void adl_template_arg (tree);
263 void adl_class (tree);
264 void adl_bases (tree);
265 void adl_class_only (tree);
266 void adl_namespace (tree);
267 void adl_namespace_only (tree);
269 public:
270 /* Search namespace + inlines + maybe usings as qualified lookup. */
271 bool search_qualified (tree scope, bool usings = true);
273 /* Search namespace + inlines + usings as unqualified lookup. */
274 bool search_unqualified (tree scope, cp_binding_level *);
276 /* ADL lookup of ARGS. */
277 tree search_adl (tree fns, vec<tree, va_gc> *args);
280 /* Scope stack shared by all outermost lookups. This avoids us
281 allocating and freeing on every single lookup. */
282 vec<tree, va_heap, vl_embed> *name_lookup::shared_scopes;
284 /* Currently active lookup. */
285 name_lookup *name_lookup::active;
287 /* Name lookup is recursive, becase ADL can cause template
288 instatiation. This is of course a rare event, so we optimize for
289 it not happening. When we discover an active name-lookup, which
290 must be an ADL lookup, we need to unmark the marked scopes and also
291 unmark the lookup we might have been accumulating. */
293 void
294 name_lookup::preserve_state ()
296 previous = active;
297 if (previous)
299 unsigned length = vec_safe_length (previous->scopes);
300 vec_safe_reserve (previous->scopes, length * 2);
301 for (unsigned ix = length; ix--;)
303 tree decl = (*previous->scopes)[ix];
305 gcc_checking_assert (LOOKUP_SEEN_P (decl));
306 LOOKUP_SEEN_P (decl) = false;
308 /* Preserve the FOUND_P state on the interrupted lookup's
309 stack. */
310 if (LOOKUP_FOUND_P (decl))
312 LOOKUP_FOUND_P (decl) = false;
313 previous->scopes->quick_push (decl);
317 /* Unmark the outer partial lookup. */
318 if (previous->deduping)
319 lookup_mark (previous->value, false);
321 else
322 scopes = shared_scopes;
323 active = this;
326 /* Restore the marking state of a lookup we interrupted. */
328 void
329 name_lookup::restore_state ()
331 if (deduping)
332 lookup_mark (value, false);
334 /* Unmark and empty this lookup's scope stack. */
335 for (unsigned ix = vec_safe_length (scopes); ix--;)
337 tree decl = scopes->pop ();
338 gcc_checking_assert (LOOKUP_SEEN_P (decl));
339 LOOKUP_SEEN_P (decl) = false;
340 LOOKUP_FOUND_P (decl) = false;
343 active = previous;
344 if (previous)
346 free (scopes);
348 unsigned length = vec_safe_length (previous->scopes);
349 for (unsigned ix = 0; ix != length; ix++)
351 tree decl = (*previous->scopes)[ix];
352 if (LOOKUP_SEEN_P (decl))
354 /* The remainder of the scope stack must be recording
355 FOUND_P decls, which we want to pop off. */
358 tree decl = previous->scopes->pop ();
359 gcc_checking_assert (LOOKUP_SEEN_P (decl)
360 && !LOOKUP_FOUND_P (decl));
361 LOOKUP_FOUND_P (decl) = true;
363 while (++ix != length);
364 break;
367 gcc_checking_assert (!LOOKUP_FOUND_P (decl));
368 LOOKUP_SEEN_P (decl) = true;
371 /* Remark the outer partial lookup. */
372 if (previous->deduping)
373 lookup_mark (previous->value, true);
375 else
376 shared_scopes = scopes;
379 void
380 name_lookup::mark_seen (tree scope)
382 gcc_checking_assert (!seen_p (scope));
383 LOOKUP_SEEN_P (scope) = true;
384 vec_safe_push (scopes, scope);
387 bool
388 name_lookup::find_and_mark (tree scope)
390 bool result = LOOKUP_FOUND_P (scope);
391 if (!result)
393 LOOKUP_FOUND_P (scope) = true;
394 if (!LOOKUP_SEEN_P (scope))
395 vec_safe_push (scopes, scope);
398 return result;
401 /* THING and CURRENT are ambiguous, concatenate them. */
403 tree
404 name_lookup::ambiguous (tree thing, tree current)
406 if (TREE_CODE (current) != TREE_LIST)
408 current = build_tree_list (NULL_TREE, current);
409 TREE_TYPE (current) = error_mark_node;
411 current = tree_cons (NULL_TREE, thing, current);
412 TREE_TYPE (current) = error_mark_node;
414 return current;
417 /* FNS is a new overload set to add to the exising set. */
419 void
420 name_lookup::add_overload (tree fns)
422 if (!deduping && TREE_CODE (fns) == OVERLOAD)
424 tree probe = fns;
425 if (flags & LOOKUP_HIDDEN)
426 probe = ovl_skip_hidden (probe);
427 if (probe && TREE_CODE (probe) == OVERLOAD && OVL_USING_P (probe))
429 /* We're about to add something found by a using
430 declaration, so need to engage deduping mode. */
431 lookup_mark (value, true);
432 deduping = true;
436 value = lookup_maybe_add (fns, value, deduping);
439 /* Add a NEW_VAL, a found value binding into the current value binding. */
441 void
442 name_lookup::add_value (tree new_val)
444 if (OVL_P (new_val) && (!value || OVL_P (value)))
445 add_overload (new_val);
446 else if (!value)
447 value = new_val;
448 else if (value == new_val)
450 else if ((TREE_CODE (value) == TYPE_DECL
451 && TREE_CODE (new_val) == TYPE_DECL
452 && same_type_p (TREE_TYPE (value), TREE_TYPE (new_val))))
453 /* Typedefs to the same type. */;
454 else if (TREE_CODE (value) == NAMESPACE_DECL
455 && TREE_CODE (new_val) == NAMESPACE_DECL
456 && ORIGINAL_NAMESPACE (value) == ORIGINAL_NAMESPACE (new_val))
457 /* Namespace (possibly aliased) to the same namespace. Locate
458 the namespace*/
459 value = ORIGINAL_NAMESPACE (value);
460 else
462 if (deduping)
464 /* Disengage deduping mode. */
465 lookup_mark (value, false);
466 deduping = false;
468 value = ambiguous (new_val, value);
472 /* Add a NEW_TYPE, a found type binding into the current type binding. */
474 void
475 name_lookup::add_type (tree new_type)
477 if (!type)
478 type = new_type;
479 else if (TREE_CODE (type) == TREE_LIST
480 || !same_type_p (TREE_TYPE (type), TREE_TYPE (new_type)))
481 type = ambiguous (new_type, type);
484 /* Process a found binding containing NEW_VAL and NEW_TYPE. Returns
485 true if we actually found something noteworthy. */
487 bool
488 name_lookup::process_binding (tree new_val, tree new_type)
490 /* Did we really see a type? */
491 if (new_type
492 && (LOOKUP_NAMESPACES_ONLY (flags)
493 || (!(flags & LOOKUP_HIDDEN)
494 && DECL_LANG_SPECIFIC (new_type)
495 && DECL_ANTICIPATED (new_type))))
496 new_type = NULL_TREE;
498 if (new_val && !(flags & LOOKUP_HIDDEN))
499 new_val = ovl_skip_hidden (new_val);
501 /* Do we really see a value? */
502 if (new_val)
503 switch (TREE_CODE (new_val))
505 case TEMPLATE_DECL:
506 /* If we expect types or namespaces, and not templates,
507 or this is not a template class. */
508 if ((LOOKUP_QUALIFIERS_ONLY (flags)
509 && !DECL_TYPE_TEMPLATE_P (new_val)))
510 new_val = NULL_TREE;
511 break;
512 case TYPE_DECL:
513 if (LOOKUP_NAMESPACES_ONLY (flags)
514 || (new_type && (flags & LOOKUP_PREFER_TYPES)))
515 new_val = NULL_TREE;
516 break;
517 case NAMESPACE_DECL:
518 if (LOOKUP_TYPES_ONLY (flags))
519 new_val = NULL_TREE;
520 break;
521 default:
522 if (LOOKUP_QUALIFIERS_ONLY (flags))
523 new_val = NULL_TREE;
526 if (!new_val)
528 new_val = new_type;
529 new_type = NULL_TREE;
532 /* Merge into the lookup */
533 if (new_val)
534 add_value (new_val);
535 if (new_type)
536 add_type (new_type);
538 return new_val != NULL_TREE;
541 /* Look in exactly namespace SCOPE. */
543 bool
544 name_lookup::search_namespace_only (tree scope)
546 bool found = false;
548 if (tree *binding = find_namespace_slot (scope, name))
549 found |= process_binding (MAYBE_STAT_DECL (*binding),
550 MAYBE_STAT_TYPE (*binding));
552 return found;
555 /* Conditionally look in namespace SCOPE and inline children. */
557 bool
558 name_lookup::search_namespace (tree scope)
560 if (see_and_mark (scope))
561 /* We've visited this scope before. Return what we found then. */
562 return found_p (scope);
564 /* Look in exactly namespace. */
565 bool found = search_namespace_only (scope);
567 /* Recursively look in its inline children. */
568 if (vec<tree, va_gc> *inlinees = DECL_NAMESPACE_INLINEES (scope))
569 for (unsigned ix = inlinees->length (); ix--;)
570 found |= search_namespace ((*inlinees)[ix]);
572 if (found)
573 mark_found (scope);
575 return found;
578 /* Recursively follow using directives of SCOPE & its inline children.
579 Such following is essentially a flood-fill algorithm. */
581 bool
582 name_lookup::search_usings (tree scope)
584 /* We do not check seen_p here, as that was already set during the
585 namespace_only walk. */
586 if (found_p (scope))
587 return true;
589 bool found = false;
590 if (vec<tree, va_gc> *usings = DECL_NAMESPACE_USING (scope))
591 for (unsigned ix = usings->length (); ix--;)
592 found |= search_qualified ((*usings)[ix], true);
594 /* Look in its inline children. */
595 if (vec<tree, va_gc> *inlinees = DECL_NAMESPACE_INLINEES (scope))
596 for (unsigned ix = inlinees->length (); ix--;)
597 found |= search_usings ((*inlinees)[ix]);
599 if (found)
600 mark_found (scope);
602 return found;
605 /* Qualified namespace lookup in SCOPE.
606 1) Look in SCOPE (+inlines). If found, we're done.
607 2) Otherwise, if USINGS is true,
608 recurse for every using directive of SCOPE (+inlines).
610 Trickiness is (a) loops and (b) multiple paths to same namespace.
611 In both cases we want to not repeat any lookups, and know whether
612 to stop the caller's step #2. Do this via the FOUND_P marker. */
614 bool
615 name_lookup::search_qualified (tree scope, bool usings)
617 bool found = false;
619 if (seen_p (scope))
620 found = found_p (scope);
621 else
623 found = search_namespace (scope);
624 if (!found && usings)
625 found = search_usings (scope);
628 return found;
631 /* Add SCOPE to the unqualified search queue, recursively add its
632 inlines and those via using directives. */
634 name_lookup::using_queue *
635 name_lookup::queue_namespace (using_queue *queue, int depth, tree scope)
637 if (see_and_mark (scope))
638 return queue;
640 /* Record it. */
641 tree common = scope;
642 while (SCOPE_DEPTH (common) > depth)
643 common = CP_DECL_CONTEXT (common);
644 vec_safe_push (queue, using_pair (common, scope));
646 /* Queue its inline children. */
647 if (vec<tree, va_gc> *inlinees = DECL_NAMESPACE_INLINEES (scope))
648 for (unsigned ix = inlinees->length (); ix--;)
649 queue = queue_namespace (queue, depth, (*inlinees)[ix]);
651 /* Queue its using targets. */
652 queue = queue_usings (queue, depth, DECL_NAMESPACE_USING (scope));
654 return queue;
657 /* Add the namespaces in USINGS to the unqualified search queue. */
659 name_lookup::using_queue *
660 name_lookup::do_queue_usings (using_queue *queue, int depth,
661 vec<tree, va_gc> *usings)
663 for (unsigned ix = usings->length (); ix--;)
664 queue = queue_namespace (queue, depth, (*usings)[ix]);
666 return queue;
669 /* Unqualified namespace lookup in SCOPE.
670 1) add scope+inlins to worklist.
671 2) recursively add target of every using directive
672 3) for each worklist item where SCOPE is common ancestor, search it
673 4) if nothing find, scope=parent, goto 1. */
675 bool
676 name_lookup::search_unqualified (tree scope, cp_binding_level *level)
678 /* Make static to avoid continual reallocation. We're not
679 recursive. */
680 static using_queue *queue = NULL;
681 bool found = false;
682 int length = vec_safe_length (queue);
684 /* Queue local using-directives. */
685 for (; level->kind != sk_namespace; level = level->level_chain)
686 queue = queue_usings (queue, SCOPE_DEPTH (scope), level->using_directives);
688 for (; !found; scope = CP_DECL_CONTEXT (scope))
690 gcc_assert (!DECL_NAMESPACE_ALIAS (scope));
691 int depth = SCOPE_DEPTH (scope);
693 /* Queue namespaces reachable from SCOPE. */
694 queue = queue_namespace (queue, depth, scope);
696 /* Search every queued namespace where SCOPE is the common
697 ancestor. Adjust the others. */
698 unsigned ix = length;
701 using_pair &pair = (*queue)[ix];
702 while (pair.first == scope)
704 found |= search_namespace_only (pair.second);
705 pair = queue->pop ();
706 if (ix == queue->length ())
707 goto done;
709 /* The depth is the same as SCOPE, find the parent scope. */
710 if (SCOPE_DEPTH (pair.first) == depth)
711 pair.first = CP_DECL_CONTEXT (pair.first);
712 ix++;
714 while (ix < queue->length ());
715 done:;
716 if (scope == global_namespace)
717 break;
720 vec_safe_truncate (queue, length);
722 return found;
725 /* FNS is a value binding. If it is a (set of overloaded) functions,
726 add them into the current value. */
728 void
729 name_lookup::add_fns (tree fns)
731 if (!fns)
732 return;
733 else if (TREE_CODE (fns) == OVERLOAD)
735 if (TREE_TYPE (fns) != unknown_type_node)
736 fns = OVL_FUNCTION (fns);
738 else if (!DECL_DECLARES_FUNCTION_P (fns))
739 return;
741 add_overload (fns);
744 /* Add functions of a namespace to the lookup structure. */
746 void
747 name_lookup::adl_namespace_only (tree scope)
749 mark_seen (scope);
751 /* Look down into inline namespaces. */
752 if (vec<tree, va_gc> *inlinees = DECL_NAMESPACE_INLINEES (scope))
753 for (unsigned ix = inlinees->length (); ix--;)
754 adl_namespace_only ((*inlinees)[ix]);
756 if (tree fns = find_namespace_value (scope, name))
757 add_fns (ovl_skip_hidden (fns));
760 /* Find the containing non-inlined namespace, add it and all its
761 inlinees. */
763 void
764 name_lookup::adl_namespace (tree scope)
766 if (seen_p (scope))
767 return;
769 /* Find the containing non-inline namespace. */
770 while (DECL_NAMESPACE_INLINE_P (scope))
771 scope = CP_DECL_CONTEXT (scope);
773 adl_namespace_only (scope);
776 /* Adds the class and its friends to the lookup structure. */
778 void
779 name_lookup::adl_class_only (tree type)
781 /* Backend-built structures, such as __builtin_va_list, aren't
782 affected by all this. */
783 if (!CLASS_TYPE_P (type))
784 return;
786 type = TYPE_MAIN_VARIANT (type);
788 if (see_and_mark (type))
789 return;
791 tree context = decl_namespace_context (type);
792 adl_namespace (context);
794 complete_type (type);
796 /* Add friends. */
797 for (tree list = DECL_FRIENDLIST (TYPE_MAIN_DECL (type)); list;
798 list = TREE_CHAIN (list))
799 if (name == FRIEND_NAME (list))
800 for (tree friends = FRIEND_DECLS (list); friends;
801 friends = TREE_CHAIN (friends))
803 tree fn = TREE_VALUE (friends);
805 /* Only interested in global functions with potentially hidden
806 (i.e. unqualified) declarations. */
807 if (CP_DECL_CONTEXT (fn) != context)
808 continue;
810 /* Only interested in anticipated friends. (Non-anticipated
811 ones will have been inserted during the namespace
812 adl.) */
813 if (!DECL_ANTICIPATED (fn))
814 continue;
816 /* Template specializations are never found by name lookup.
817 (Templates themselves can be found, but not template
818 specializations.) */
819 if (TREE_CODE (fn) == FUNCTION_DECL && DECL_USE_TEMPLATE (fn))
820 continue;
822 add_fns (fn);
826 /* Adds the class and its bases to the lookup structure.
827 Returns true on error. */
829 void
830 name_lookup::adl_bases (tree type)
832 adl_class_only (type);
834 /* Process baseclasses. */
835 if (tree binfo = TYPE_BINFO (type))
837 tree base_binfo;
838 int i;
840 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
841 adl_bases (BINFO_TYPE (base_binfo));
845 /* Adds everything associated with a class argument type to the lookup
846 structure. Returns true on error.
848 If T is a class type (including unions), its associated classes are: the
849 class itself; the class of which it is a member, if any; and its direct
850 and indirect base classes. Its associated namespaces are the namespaces
851 of which its associated classes are members. Furthermore, if T is a
852 class template specialization, its associated namespaces and classes
853 also include: the namespaces and classes associated with the types of
854 the template arguments provided for template type parameters (excluding
855 template template parameters); the namespaces of which any template
856 template arguments are members; and the classes of which any member
857 templates used as template template arguments are members. [ Note:
858 non-type template arguments do not contribute to the set of associated
859 namespaces. --end note] */
861 void
862 name_lookup::adl_class (tree type)
864 /* Backend build structures, such as __builtin_va_list, aren't
865 affected by all this. */
866 if (!CLASS_TYPE_P (type))
867 return;
869 type = TYPE_MAIN_VARIANT (type);
870 /* We don't set found here because we have to have set seen first,
871 which is done in the adl_bases walk. */
872 if (found_p (type))
873 return;
875 adl_bases (type);
876 mark_found (type);
878 if (TYPE_CLASS_SCOPE_P (type))
879 adl_class_only (TYPE_CONTEXT (type));
881 /* Process template arguments. */
882 if (CLASSTYPE_TEMPLATE_INFO (type)
883 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
885 tree list = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
886 for (int i = 0; i < TREE_VEC_LENGTH (list); ++i)
887 adl_template_arg (TREE_VEC_ELT (list, i));
891 void
892 name_lookup::adl_expr (tree expr)
894 if (!expr)
895 return;
897 gcc_assert (!TYPE_P (expr));
899 if (TREE_TYPE (expr) != unknown_type_node)
901 adl_type (TREE_TYPE (expr));
902 return;
905 if (TREE_CODE (expr) == ADDR_EXPR)
906 expr = TREE_OPERAND (expr, 0);
907 if (TREE_CODE (expr) == COMPONENT_REF
908 || TREE_CODE (expr) == OFFSET_REF)
909 expr = TREE_OPERAND (expr, 1);
910 expr = MAYBE_BASELINK_FUNCTIONS (expr);
912 if (OVL_P (expr))
913 for (lkp_iterator iter (expr); iter; ++iter)
914 adl_type (TREE_TYPE (*iter));
915 else if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
917 /* The working paper doesn't currently say how to handle
918 template-id arguments. The sensible thing would seem to be
919 to handle the list of template candidates like a normal
920 overload set, and handle the template arguments like we do
921 for class template specializations. */
923 /* First the templates. */
924 adl_expr (TREE_OPERAND (expr, 0));
926 /* Now the arguments. */
927 if (tree args = TREE_OPERAND (expr, 1))
928 for (int ix = TREE_VEC_LENGTH (args); ix--;)
929 adl_template_arg (TREE_VEC_ELT (args, ix));
933 void
934 name_lookup::adl_type (tree type)
936 if (!type)
937 return;
939 if (TYPE_PTRDATAMEM_P (type))
941 /* Pointer to member: associate class type and value type. */
942 adl_type (TYPE_PTRMEM_CLASS_TYPE (type));
943 adl_type (TYPE_PTRMEM_POINTED_TO_TYPE (type));
944 return;
947 switch (TREE_CODE (type))
949 case RECORD_TYPE:
950 if (TYPE_PTRMEMFUNC_P (type))
952 adl_type (TYPE_PTRMEMFUNC_FN_TYPE (type));
953 return;
955 /* FALLTHRU */
956 case UNION_TYPE:
957 adl_class (type);
958 return;
960 case METHOD_TYPE:
961 /* The basetype is referenced in the first arg type, so just
962 fall through. */
963 case FUNCTION_TYPE:
964 /* Associate the parameter types. */
965 for (tree args = TYPE_ARG_TYPES (type); args; args = TREE_CHAIN (args))
966 adl_type (TREE_VALUE (args));
967 /* FALLTHROUGH */
969 case POINTER_TYPE:
970 case REFERENCE_TYPE:
971 case ARRAY_TYPE:
972 adl_type (TREE_TYPE (type));
973 return;
975 case ENUMERAL_TYPE:
976 if (TYPE_CLASS_SCOPE_P (type))
977 adl_class_only (TYPE_CONTEXT (type));
978 adl_namespace (decl_namespace_context (type));
979 return;
981 case LANG_TYPE:
982 gcc_assert (type == unknown_type_node
983 || type == init_list_type_node);
984 return;
986 case TYPE_PACK_EXPANSION:
987 adl_type (PACK_EXPANSION_PATTERN (type));
988 return;
990 default:
991 break;
995 /* Adds everything associated with a template argument to the lookup
996 structure. */
998 void
999 name_lookup::adl_template_arg (tree arg)
1001 /* [basic.lookup.koenig]
1003 If T is a template-id, its associated namespaces and classes are
1004 ... the namespaces and classes associated with the types of the
1005 template arguments provided for template type parameters
1006 (excluding template template parameters); the namespaces in which
1007 any template template arguments are defined; and the classes in
1008 which any member templates used as template template arguments
1009 are defined. [Note: non-type template arguments do not
1010 contribute to the set of associated namespaces. ] */
1012 /* Consider first template template arguments. */
1013 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
1014 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
1016 else if (TREE_CODE (arg) == TEMPLATE_DECL)
1018 tree ctx = CP_DECL_CONTEXT (arg);
1020 /* It's not a member template. */
1021 if (TREE_CODE (ctx) == NAMESPACE_DECL)
1022 adl_namespace (ctx);
1023 /* Otherwise, it must be member template. */
1024 else
1025 adl_class_only (ctx);
1027 /* It's an argument pack; handle it recursively. */
1028 else if (ARGUMENT_PACK_P (arg))
1030 tree args = ARGUMENT_PACK_ARGS (arg);
1031 int i, len = TREE_VEC_LENGTH (args);
1032 for (i = 0; i < len; ++i)
1033 adl_template_arg (TREE_VEC_ELT (args, i));
1035 /* It's not a template template argument, but it is a type template
1036 argument. */
1037 else if (TYPE_P (arg))
1038 adl_type (arg);
1041 /* Perform ADL lookup. FNS is the existing lookup result and ARGS are
1042 the call arguments. */
1044 tree
1045 name_lookup::search_adl (tree fns, vec<tree, va_gc> *args)
1047 if (fns)
1049 deduping = true;
1050 lookup_mark (fns, true);
1052 value = fns;
1054 unsigned ix;
1055 tree arg;
1057 FOR_EACH_VEC_ELT_REVERSE (*args, ix, arg)
1058 /* OMP reduction operators put an ADL-significant type as the
1059 first arg. */
1060 if (TYPE_P (arg))
1061 adl_type (arg);
1062 else
1063 adl_expr (arg);
1065 fns = value;
1067 return fns;
1070 static bool qualified_namespace_lookup (tree, name_lookup *);
1071 static void consider_binding_level (tree name,
1072 best_match <tree, const char *> &bm,
1073 cp_binding_level *lvl,
1074 bool look_within_fields,
1075 enum lookup_name_fuzzy_kind kind);
1076 static void diagnose_name_conflict (tree, tree);
1078 /* ADL lookup of NAME. FNS is the result of regular lookup, and we
1079 don't add duplicates to it. ARGS is the vector of call
1080 arguments (which will not be empty). */
1082 tree
1083 lookup_arg_dependent (tree name, tree fns, vec<tree, va_gc> *args)
1085 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
1086 name_lookup lookup (name);
1087 fns = lookup.search_adl (fns, args);
1088 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
1089 return fns;
1092 /* Compute the chain index of a binding_entry given the HASH value of its
1093 name and the total COUNT of chains. COUNT is assumed to be a power
1094 of 2. */
1096 #define ENTRY_INDEX(HASH, COUNT) (((HASH) >> 3) & ((COUNT) - 1))
1098 /* A free list of "binding_entry"s awaiting for re-use. */
1100 static GTY((deletable)) binding_entry free_binding_entry = NULL;
1102 /* The binding oracle; see cp-tree.h. */
1104 cp_binding_oracle_function *cp_binding_oracle;
1106 /* If we have a binding oracle, ask it for all namespace-scoped
1107 definitions of NAME. */
1109 static inline void
1110 query_oracle (tree name)
1112 if (!cp_binding_oracle)
1113 return;
1115 /* LOOKED_UP holds the set of identifiers that we have already
1116 looked up with the oracle. */
1117 static hash_set<tree> looked_up;
1118 if (looked_up.add (name))
1119 return;
1121 cp_binding_oracle (CP_ORACLE_IDENTIFIER, name);
1124 /* Create a binding_entry object for (NAME, TYPE). */
1126 static inline binding_entry
1127 binding_entry_make (tree name, tree type)
1129 binding_entry entry;
1131 if (free_binding_entry)
1133 entry = free_binding_entry;
1134 free_binding_entry = entry->chain;
1136 else
1137 entry = ggc_alloc<binding_entry_s> ();
1139 entry->name = name;
1140 entry->type = type;
1141 entry->chain = NULL;
1143 return entry;
1146 /* Put ENTRY back on the free list. */
1147 #if 0
1148 static inline void
1149 binding_entry_free (binding_entry entry)
1151 entry->name = NULL;
1152 entry->type = NULL;
1153 entry->chain = free_binding_entry;
1154 free_binding_entry = entry;
1156 #endif
1158 /* The datatype used to implement the mapping from names to types at
1159 a given scope. */
1160 struct GTY(()) binding_table_s {
1161 /* Array of chains of "binding_entry"s */
1162 binding_entry * GTY((length ("%h.chain_count"))) chain;
1164 /* The number of chains in this table. This is the length of the
1165 member "chain" considered as an array. */
1166 size_t chain_count;
1168 /* Number of "binding_entry"s in this table. */
1169 size_t entry_count;
1172 /* Construct TABLE with an initial CHAIN_COUNT. */
1174 static inline void
1175 binding_table_construct (binding_table table, size_t chain_count)
1177 table->chain_count = chain_count;
1178 table->entry_count = 0;
1179 table->chain = ggc_cleared_vec_alloc<binding_entry> (table->chain_count);
1182 /* Make TABLE's entries ready for reuse. */
1183 #if 0
1184 static void
1185 binding_table_free (binding_table table)
1187 size_t i;
1188 size_t count;
1190 if (table == NULL)
1191 return;
1193 for (i = 0, count = table->chain_count; i < count; ++i)
1195 binding_entry temp = table->chain[i];
1196 while (temp != NULL)
1198 binding_entry entry = temp;
1199 temp = entry->chain;
1200 binding_entry_free (entry);
1202 table->chain[i] = NULL;
1204 table->entry_count = 0;
1206 #endif
1208 /* Allocate a table with CHAIN_COUNT, assumed to be a power of two. */
1210 static inline binding_table
1211 binding_table_new (size_t chain_count)
1213 binding_table table = ggc_alloc<binding_table_s> ();
1214 table->chain = NULL;
1215 binding_table_construct (table, chain_count);
1216 return table;
1219 /* Expand TABLE to twice its current chain_count. */
1221 static void
1222 binding_table_expand (binding_table table)
1224 const size_t old_chain_count = table->chain_count;
1225 const size_t old_entry_count = table->entry_count;
1226 const size_t new_chain_count = 2 * old_chain_count;
1227 binding_entry *old_chains = table->chain;
1228 size_t i;
1230 binding_table_construct (table, new_chain_count);
1231 for (i = 0; i < old_chain_count; ++i)
1233 binding_entry entry = old_chains[i];
1234 for (; entry != NULL; entry = old_chains[i])
1236 const unsigned int hash = IDENTIFIER_HASH_VALUE (entry->name);
1237 const size_t j = ENTRY_INDEX (hash, new_chain_count);
1239 old_chains[i] = entry->chain;
1240 entry->chain = table->chain[j];
1241 table->chain[j] = entry;
1244 table->entry_count = old_entry_count;
1247 /* Insert a binding for NAME to TYPE into TABLE. */
1249 static void
1250 binding_table_insert (binding_table table, tree name, tree type)
1252 const unsigned int hash = IDENTIFIER_HASH_VALUE (name);
1253 const size_t i = ENTRY_INDEX (hash, table->chain_count);
1254 binding_entry entry = binding_entry_make (name, type);
1256 entry->chain = table->chain[i];
1257 table->chain[i] = entry;
1258 ++table->entry_count;
1260 if (3 * table->chain_count < 5 * table->entry_count)
1261 binding_table_expand (table);
1264 /* Return the binding_entry, if any, that maps NAME. */
1266 binding_entry
1267 binding_table_find (binding_table table, tree name)
1269 const unsigned int hash = IDENTIFIER_HASH_VALUE (name);
1270 binding_entry entry = table->chain[ENTRY_INDEX (hash, table->chain_count)];
1272 while (entry != NULL && entry->name != name)
1273 entry = entry->chain;
1275 return entry;
1278 /* Apply PROC -- with DATA -- to all entries in TABLE. */
1280 void
1281 binding_table_foreach (binding_table table, bt_foreach_proc proc, void *data)
1283 size_t chain_count;
1284 size_t i;
1286 if (!table)
1287 return;
1289 chain_count = table->chain_count;
1290 for (i = 0; i < chain_count; ++i)
1292 binding_entry entry = table->chain[i];
1293 for (; entry != NULL; entry = entry->chain)
1294 proc (entry, data);
1298 #ifndef ENABLE_SCOPE_CHECKING
1299 # define ENABLE_SCOPE_CHECKING 0
1300 #else
1301 # define ENABLE_SCOPE_CHECKING 1
1302 #endif
1304 /* A free list of "cxx_binding"s, connected by their PREVIOUS. */
1306 static GTY((deletable)) cxx_binding *free_bindings;
1308 /* Initialize VALUE and TYPE field for BINDING, and set the PREVIOUS
1309 field to NULL. */
1311 static inline void
1312 cxx_binding_init (cxx_binding *binding, tree value, tree type)
1314 binding->value = value;
1315 binding->type = type;
1316 binding->previous = NULL;
1319 /* (GC)-allocate a binding object with VALUE and TYPE member initialized. */
1321 static cxx_binding *
1322 cxx_binding_make (tree value, tree type)
1324 cxx_binding *binding;
1325 if (free_bindings)
1327 binding = free_bindings;
1328 free_bindings = binding->previous;
1330 else
1331 binding = ggc_alloc<cxx_binding> ();
1333 cxx_binding_init (binding, value, type);
1335 return binding;
1338 /* Put BINDING back on the free list. */
1340 static inline void
1341 cxx_binding_free (cxx_binding *binding)
1343 binding->scope = NULL;
1344 binding->previous = free_bindings;
1345 free_bindings = binding;
1348 /* Create a new binding for NAME (with the indicated VALUE and TYPE
1349 bindings) in the class scope indicated by SCOPE. */
1351 static cxx_binding *
1352 new_class_binding (tree name, tree value, tree type, cp_binding_level *scope)
1354 cp_class_binding cb = {cxx_binding_make (value, type), name};
1355 cxx_binding *binding = cb.base;
1356 vec_safe_push (scope->class_shadowed, cb);
1357 binding->scope = scope;
1358 return binding;
1361 /* Make DECL the innermost binding for ID. The LEVEL is the binding
1362 level at which this declaration is being bound. */
1364 void
1365 push_binding (tree id, tree decl, cp_binding_level* level)
1367 cxx_binding *binding;
1369 if (level != class_binding_level)
1371 binding = cxx_binding_make (decl, NULL_TREE);
1372 binding->scope = level;
1374 else
1375 binding = new_class_binding (id, decl, /*type=*/NULL_TREE, level);
1377 /* Now, fill in the binding information. */
1378 binding->previous = IDENTIFIER_BINDING (id);
1379 INHERITED_VALUE_BINDING_P (binding) = 0;
1380 LOCAL_BINDING_P (binding) = (level != class_binding_level);
1382 /* And put it on the front of the list of bindings for ID. */
1383 IDENTIFIER_BINDING (id) = binding;
1386 /* Remove the binding for DECL which should be the innermost binding
1387 for ID. */
1389 void
1390 pop_local_binding (tree id, tree decl)
1392 cxx_binding *binding;
1394 if (id == NULL_TREE)
1395 /* It's easiest to write the loops that call this function without
1396 checking whether or not the entities involved have names. We
1397 get here for such an entity. */
1398 return;
1400 /* Get the innermost binding for ID. */
1401 binding = IDENTIFIER_BINDING (id);
1403 /* The name should be bound. */
1404 gcc_assert (binding != NULL);
1406 /* The DECL will be either the ordinary binding or the type
1407 binding for this identifier. Remove that binding. */
1408 if (binding->value == decl)
1409 binding->value = NULL_TREE;
1410 else
1412 gcc_assert (binding->type == decl);
1413 binding->type = NULL_TREE;
1416 if (!binding->value && !binding->type)
1418 /* We're completely done with the innermost binding for this
1419 identifier. Unhook it from the list of bindings. */
1420 IDENTIFIER_BINDING (id) = binding->previous;
1422 /* Add it to the free list. */
1423 cxx_binding_free (binding);
1427 /* Remove the bindings for the decls of the current level and leave
1428 the current scope. */
1430 void
1431 pop_bindings_and_leave_scope (void)
1433 for (tree t = get_local_decls (); t; t = DECL_CHAIN (t))
1435 tree decl = TREE_CODE (t) == TREE_LIST ? TREE_VALUE (t) : t;
1436 tree name = OVL_NAME (decl);
1438 pop_local_binding (name, decl);
1441 leave_scope ();
1444 /* Strip non dependent using declarations. If DECL is dependent,
1445 surreptitiously create a typename_type and return it. */
1447 tree
1448 strip_using_decl (tree decl)
1450 if (decl == NULL_TREE)
1451 return NULL_TREE;
1453 while (TREE_CODE (decl) == USING_DECL && !DECL_DEPENDENT_P (decl))
1454 decl = USING_DECL_DECLS (decl);
1456 if (TREE_CODE (decl) == USING_DECL && DECL_DEPENDENT_P (decl)
1457 && USING_DECL_TYPENAME_P (decl))
1459 /* We have found a type introduced by a using
1460 declaration at class scope that refers to a dependent
1461 type.
1463 using typename :: [opt] nested-name-specifier unqualified-id ;
1465 decl = make_typename_type (TREE_TYPE (decl),
1466 DECL_NAME (decl),
1467 typename_type, tf_error);
1468 if (decl != error_mark_node)
1469 decl = TYPE_NAME (decl);
1472 return decl;
1475 /* Return true if OVL is an overload for an anticipated builtin. */
1477 static bool
1478 anticipated_builtin_p (tree ovl)
1480 if (TREE_CODE (ovl) != OVERLOAD)
1481 return false;
1483 if (!OVL_HIDDEN_P (ovl))
1484 return false;
1486 tree fn = OVL_FUNCTION (ovl);
1487 gcc_checking_assert (DECL_ANTICIPATED (fn));
1489 if (DECL_HIDDEN_FRIEND_P (fn))
1490 return false;
1492 return true;
1495 /* BINDING records an existing declaration for a name in the current scope.
1496 But, DECL is another declaration for that same identifier in the
1497 same scope. This is the `struct stat' hack whereby a non-typedef
1498 class name or enum-name can be bound at the same level as some other
1499 kind of entity.
1500 3.3.7/1
1502 A class name (9.1) or enumeration name (7.2) can be hidden by the
1503 name of an object, function, or enumerator declared in the same scope.
1504 If a class or enumeration name and an object, function, or enumerator
1505 are declared in the same scope (in any order) with the same name, the
1506 class or enumeration name is hidden wherever the object, function, or
1507 enumerator name is visible.
1509 It's the responsibility of the caller to check that
1510 inserting this name is valid here. Returns nonzero if the new binding
1511 was successful. */
1513 static bool
1514 supplement_binding_1 (cxx_binding *binding, tree decl)
1516 tree bval = binding->value;
1517 bool ok = true;
1518 tree target_bval = strip_using_decl (bval);
1519 tree target_decl = strip_using_decl (decl);
1521 if (TREE_CODE (target_decl) == TYPE_DECL && DECL_ARTIFICIAL (target_decl)
1522 && target_decl != target_bval
1523 && (TREE_CODE (target_bval) != TYPE_DECL
1524 /* We allow pushing an enum multiple times in a class
1525 template in order to handle late matching of underlying
1526 type on an opaque-enum-declaration followed by an
1527 enum-specifier. */
1528 || (processing_template_decl
1529 && TREE_CODE (TREE_TYPE (target_decl)) == ENUMERAL_TYPE
1530 && TREE_CODE (TREE_TYPE (target_bval)) == ENUMERAL_TYPE
1531 && (dependent_type_p (ENUM_UNDERLYING_TYPE
1532 (TREE_TYPE (target_decl)))
1533 || dependent_type_p (ENUM_UNDERLYING_TYPE
1534 (TREE_TYPE (target_bval)))))))
1535 /* The new name is the type name. */
1536 binding->type = decl;
1537 else if (/* TARGET_BVAL is null when push_class_level_binding moves
1538 an inherited type-binding out of the way to make room
1539 for a new value binding. */
1540 !target_bval
1541 /* TARGET_BVAL is error_mark_node when TARGET_DECL's name
1542 has been used in a non-class scope prior declaration.
1543 In that case, we should have already issued a
1544 diagnostic; for graceful error recovery purpose, pretend
1545 this was the intended declaration for that name. */
1546 || target_bval == error_mark_node
1547 /* If TARGET_BVAL is anticipated but has not yet been
1548 declared, pretend it is not there at all. */
1549 || anticipated_builtin_p (target_bval))
1550 binding->value = decl;
1551 else if (TREE_CODE (target_bval) == TYPE_DECL
1552 && DECL_ARTIFICIAL (target_bval)
1553 && target_decl != target_bval
1554 && (TREE_CODE (target_decl) != TYPE_DECL
1555 || same_type_p (TREE_TYPE (target_decl),
1556 TREE_TYPE (target_bval))))
1558 /* The old binding was a type name. It was placed in
1559 VALUE field because it was thought, at the point it was
1560 declared, to be the only entity with such a name. Move the
1561 type name into the type slot; it is now hidden by the new
1562 binding. */
1563 binding->type = bval;
1564 binding->value = decl;
1565 binding->value_is_inherited = false;
1567 else if (TREE_CODE (target_bval) == TYPE_DECL
1568 && TREE_CODE (target_decl) == TYPE_DECL
1569 && DECL_NAME (target_decl) == DECL_NAME (target_bval)
1570 && binding->scope->kind != sk_class
1571 && (same_type_p (TREE_TYPE (target_decl), TREE_TYPE (target_bval))
1572 /* If either type involves template parameters, we must
1573 wait until instantiation. */
1574 || uses_template_parms (TREE_TYPE (target_decl))
1575 || uses_template_parms (TREE_TYPE (target_bval))))
1576 /* We have two typedef-names, both naming the same type to have
1577 the same name. In general, this is OK because of:
1579 [dcl.typedef]
1581 In a given scope, a typedef specifier can be used to redefine
1582 the name of any type declared in that scope to refer to the
1583 type to which it already refers.
1585 However, in class scopes, this rule does not apply due to the
1586 stricter language in [class.mem] prohibiting redeclarations of
1587 members. */
1588 ok = false;
1589 /* There can be two block-scope declarations of the same variable,
1590 so long as they are `extern' declarations. However, there cannot
1591 be two declarations of the same static data member:
1593 [class.mem]
1595 A member shall not be declared twice in the
1596 member-specification. */
1597 else if (VAR_P (target_decl)
1598 && VAR_P (target_bval)
1599 && DECL_EXTERNAL (target_decl) && DECL_EXTERNAL (target_bval)
1600 && !DECL_CLASS_SCOPE_P (target_decl))
1602 duplicate_decls (decl, binding->value, /*newdecl_is_friend=*/false);
1603 ok = false;
1605 else if (TREE_CODE (decl) == NAMESPACE_DECL
1606 && TREE_CODE (bval) == NAMESPACE_DECL
1607 && DECL_NAMESPACE_ALIAS (decl)
1608 && DECL_NAMESPACE_ALIAS (bval)
1609 && ORIGINAL_NAMESPACE (bval) == ORIGINAL_NAMESPACE (decl))
1610 /* [namespace.alias]
1612 In a declarative region, a namespace-alias-definition can be
1613 used to redefine a namespace-alias declared in that declarative
1614 region to refer only to the namespace to which it already
1615 refers. */
1616 ok = false;
1617 else if (maybe_remove_implicit_alias (bval))
1619 /* There was a mangling compatibility alias using this mangled name,
1620 but now we have a real decl that wants to use it instead. */
1621 binding->value = decl;
1623 else
1625 if (!error_operand_p (bval))
1626 diagnose_name_conflict (decl, bval);
1627 ok = false;
1630 return ok;
1633 /* Diagnose a name conflict between DECL and BVAL. */
1635 static void
1636 diagnose_name_conflict (tree decl, tree bval)
1638 if (TREE_CODE (decl) == TREE_CODE (bval)
1639 && TREE_CODE (decl) != NAMESPACE_DECL
1640 && !DECL_DECLARES_FUNCTION_P (decl)
1641 && (TREE_CODE (decl) != TYPE_DECL
1642 || DECL_ARTIFICIAL (decl) == DECL_ARTIFICIAL (bval))
1643 && CP_DECL_CONTEXT (decl) == CP_DECL_CONTEXT (bval))
1644 error ("redeclaration of %q#D", decl);
1645 else
1646 error ("%q#D conflicts with a previous declaration", decl);
1648 inform (location_of (bval), "previous declaration %q#D", bval);
1651 /* Wrapper for supplement_binding_1. */
1653 static bool
1654 supplement_binding (cxx_binding *binding, tree decl)
1656 bool ret;
1657 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
1658 ret = supplement_binding_1 (binding, decl);
1659 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
1660 return ret;
1663 /* Replace BINDING's current value on its scope's name list with
1664 NEWVAL. */
1666 static void
1667 update_local_overload (cxx_binding *binding, tree newval)
1669 tree *d;
1671 for (d = &binding->scope->names; ; d = &TREE_CHAIN (*d))
1672 if (*d == binding->value)
1674 /* Stitch new list node in. */
1675 *d = tree_cons (NULL_TREE, NULL_TREE, TREE_CHAIN (*d));
1676 break;
1678 else if (TREE_CODE (*d) == TREE_LIST && TREE_VALUE (*d) == binding->value)
1679 break;
1681 TREE_VALUE (*d) = newval;
1684 /* Compares the parameter-type-lists of ONE and TWO and
1685 returns false if they are different. If the DECLs are template
1686 functions, the return types and the template parameter lists are
1687 compared too (DR 565). */
1689 static bool
1690 matching_fn_p (tree one, tree two)
1692 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (one)),
1693 TYPE_ARG_TYPES (TREE_TYPE (two))))
1694 return false;
1696 if (TREE_CODE (one) == TEMPLATE_DECL
1697 && TREE_CODE (two) == TEMPLATE_DECL)
1699 /* Compare template parms. */
1700 if (!comp_template_parms (DECL_TEMPLATE_PARMS (one),
1701 DECL_TEMPLATE_PARMS (two)))
1702 return false;
1704 /* And return type. */
1705 if (!same_type_p (TREE_TYPE (TREE_TYPE (one)),
1706 TREE_TYPE (TREE_TYPE (two))))
1707 return false;
1710 return true;
1713 /* Push DECL into nonclass LEVEL BINDING or SLOT. OLD is the current
1714 binding value (possibly with anticipated builtins stripped).
1715 Diagnose conflicts and return updated decl. */
1717 static tree
1718 update_binding (cp_binding_level *level, cxx_binding *binding, tree *slot,
1719 tree old, tree decl, bool is_friend)
1721 tree to_val = decl;
1722 tree old_type = slot ? MAYBE_STAT_TYPE (*slot) : binding->type;
1723 tree to_type = old_type;
1725 gcc_assert (level->kind == sk_namespace ? !binding
1726 : level->kind != sk_class && !slot);
1727 if (old == error_mark_node)
1728 old = NULL_TREE;
1730 if (TREE_CODE (decl) == TYPE_DECL && DECL_ARTIFICIAL (decl))
1732 tree other = to_type;
1734 if (old && TREE_CODE (old) == TYPE_DECL && DECL_ARTIFICIAL (old))
1735 other = old;
1737 /* Pushing an artificial typedef. See if this matches either
1738 the type slot or the old value slot. */
1739 if (!other)
1741 else if (same_type_p (TREE_TYPE (other), TREE_TYPE (decl)))
1742 /* Two artificial decls to same type. Do nothing. */
1743 return other;
1744 else
1745 goto conflict;
1747 if (old)
1749 /* Slide decl into the type slot, keep old unaltered */
1750 to_type = decl;
1751 to_val = old;
1752 goto done;
1756 if (old && TREE_CODE (old) == TYPE_DECL && DECL_ARTIFICIAL (old))
1758 /* Slide old into the type slot. */
1759 to_type = old;
1760 old = NULL_TREE;
1763 if (DECL_DECLARES_FUNCTION_P (decl))
1765 if (!old)
1767 else if (OVL_P (old))
1769 for (ovl_iterator iter (old); iter; ++iter)
1771 tree fn = *iter;
1773 if (iter.using_p () && matching_fn_p (fn, decl))
1775 /* If a function declaration in namespace scope or
1776 block scope has the same name and the same
1777 parameter-type- list (8.3.5) as a function
1778 introduced by a using-declaration, and the
1779 declarations do not declare the same function,
1780 the program is ill-formed. [namespace.udecl]/14 */
1781 if (tree match = duplicate_decls (decl, fn, is_friend))
1782 return match;
1783 else
1784 /* FIXME: To preserve existing error behavior, we
1785 still push the decl. This might change. */
1786 diagnose_name_conflict (decl, fn);
1790 else
1791 goto conflict;
1793 if (to_type != old_type
1794 && warn_shadow
1795 && MAYBE_CLASS_TYPE_P (TREE_TYPE (to_type))
1796 && !(DECL_IN_SYSTEM_HEADER (decl)
1797 && DECL_IN_SYSTEM_HEADER (to_type)))
1798 warning (OPT_Wshadow, "%q#D hides constructor for %q#D",
1799 decl, to_type);
1801 to_val = ovl_insert (decl, old);
1803 else if (!old)
1805 else if (TREE_CODE (old) != TREE_CODE (decl))
1806 /* Different kinds of decls conflict. */
1807 goto conflict;
1808 else if (TREE_CODE (old) == TYPE_DECL)
1810 if (same_type_p (TREE_TYPE (old), TREE_TYPE (decl)))
1811 /* Two type decls to the same type. Do nothing. */
1812 return old;
1813 else
1814 goto conflict;
1816 else if (TREE_CODE (old) == NAMESPACE_DECL)
1818 /* Two maybe-aliased namespaces. If they're to the same target
1819 namespace, that's ok. */
1820 if (ORIGINAL_NAMESPACE (old) != ORIGINAL_NAMESPACE (decl))
1821 goto conflict;
1823 /* The new one must be an alias at this point. */
1824 gcc_assert (DECL_NAMESPACE_ALIAS (decl));
1825 return old;
1827 else if (TREE_CODE (old) == VAR_DECL)
1829 /* There can be two block-scope declarations of the same
1830 variable, so long as they are `extern' declarations. */
1831 if (!DECL_EXTERNAL (old) || !DECL_EXTERNAL (decl))
1832 goto conflict;
1833 else if (tree match = duplicate_decls (decl, old, false))
1834 return match;
1835 else
1836 goto conflict;
1838 else
1840 conflict:
1841 diagnose_name_conflict (decl, old);
1842 to_val = NULL_TREE;
1845 done:
1846 if (to_val)
1848 if (level->kind != sk_namespace
1849 && !to_type && binding->value && OVL_P (to_val))
1850 update_local_overload (binding, to_val);
1851 else
1853 tree to_add = to_val;
1855 if (level->kind == sk_namespace)
1856 to_add = decl;
1857 else if (to_type == decl)
1858 to_add = decl;
1859 else if (TREE_CODE (to_add) == OVERLOAD)
1860 to_add = build_tree_list (NULL_TREE, to_add);
1862 add_decl_to_level (level, to_add);
1865 if (slot)
1867 if (STAT_HACK_P (*slot))
1869 STAT_TYPE (*slot) = to_type;
1870 STAT_DECL (*slot) = to_val;
1872 else if (to_type)
1873 *slot = stat_hack (to_val, to_type);
1874 else
1875 *slot = to_val;
1877 else
1879 binding->type = to_type;
1880 binding->value = to_val;
1884 return decl;
1887 /* Map of identifiers to extern C functions (or LISTS thereof). */
1889 static GTY(()) hash_map<lang_identifier *, tree> *extern_c_fns;
1891 /* DECL has C linkage. If we have an existing instance, make sure it
1892 has the same exception specification [7.5, 7.6]. If there's no
1893 instance, add DECL to the map. */
1895 static void
1896 check_extern_c_conflict (tree decl)
1898 /* Ignore artificial or system header decls. */
1899 if (DECL_ARTIFICIAL (decl) || DECL_IN_SYSTEM_HEADER (decl))
1900 return;
1902 if (!extern_c_fns)
1903 extern_c_fns = hash_map<lang_identifier *,tree>::create_ggc (127);
1905 bool existed;
1906 tree *slot = &extern_c_fns->get_or_insert (DECL_NAME (decl), &existed);
1907 if (!existed)
1908 *slot = decl;
1909 else
1911 tree old = *slot;
1912 if (TREE_CODE (old) == TREE_LIST)
1913 old = TREE_VALUE (old);
1915 int mismatch = 0;
1916 if (DECL_CONTEXT (old) == DECL_CONTEXT (decl))
1917 ; /* If they're in the same context, we'll have already complained
1918 about a (possible) mismatch, when inserting the decl. */
1919 else if (!decls_match (decl, old))
1920 mismatch = 1;
1921 else if (!comp_except_specs (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (old)),
1922 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl)),
1923 ce_normal))
1924 mismatch = -1;
1925 else if (DECL_ASSEMBLER_NAME_SET_P (old))
1926 SET_DECL_ASSEMBLER_NAME (decl, DECL_ASSEMBLER_NAME (old));
1928 if (mismatch)
1930 pedwarn (input_location, 0,
1931 "declaration of %q#D with C language linkage", decl);
1932 pedwarn (DECL_SOURCE_LOCATION (old), 0,
1933 "conflicts with previous declaration %q#D", old);
1934 if (mismatch < 0)
1935 pedwarn (input_location, 0,
1936 "due to different exception specifications");
1938 else
1939 /* Chain it on for c_linkage_binding's use. */
1940 *slot = tree_cons (NULL_TREE, decl, *slot);
1944 /* Returns a list of C-linkage decls with the name NAME. Used in
1945 c-family/c-pragma.c to implement redefine_extname pragma. */
1947 tree
1948 c_linkage_bindings (tree name)
1950 if (extern_c_fns)
1951 if (tree *slot = extern_c_fns->get (name))
1952 return *slot;
1953 return NULL_TREE;
1956 /* DECL is being declared at a local scope. Emit suitable shadow
1957 warnings. */
1959 static void
1960 check_local_shadow (tree decl)
1962 /* Don't complain about the parms we push and then pop
1963 while tentatively parsing a function declarator. */
1964 if (TREE_CODE (decl) == PARM_DECL && !DECL_CONTEXT (decl))
1965 return;
1967 /* Inline decls shadow nothing. */
1968 if (DECL_FROM_INLINE (decl))
1969 return;
1971 /* External decls are something else. */
1972 if (DECL_EXTERNAL (decl))
1973 return;
1975 tree old = NULL_TREE;
1976 cp_binding_level *old_scope = NULL;
1977 if (cxx_binding *binding = outer_binding (DECL_NAME (decl), NULL, true))
1979 old = binding->value;
1980 old_scope = binding->scope;
1982 while (old && VAR_P (old) && DECL_DEAD_FOR_LOCAL (old))
1983 old = DECL_SHADOWED_FOR_VAR (old);
1985 tree shadowed = NULL_TREE;
1986 if (old
1987 && (TREE_CODE (old) == PARM_DECL
1988 || VAR_P (old)
1989 || (TREE_CODE (old) == TYPE_DECL
1990 && (!DECL_ARTIFICIAL (old)
1991 || TREE_CODE (decl) == TYPE_DECL)))
1992 && (!DECL_ARTIFICIAL (decl)
1993 || DECL_IMPLICIT_TYPEDEF_P (decl)
1994 || (VAR_P (decl) && DECL_ANON_UNION_VAR_P (decl))))
1996 /* DECL shadows a local thing possibly of interest. */
1998 /* Don't complain if it's from an enclosing function. */
1999 if (DECL_CONTEXT (old) == current_function_decl
2000 && TREE_CODE (decl) != PARM_DECL
2001 && TREE_CODE (old) == PARM_DECL)
2003 /* Go to where the parms should be and see if we find
2004 them there. */
2005 cp_binding_level *b = current_binding_level->level_chain;
2007 if (FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
2008 /* Skip the ctor/dtor cleanup level. */
2009 b = b->level_chain;
2011 /* ARM $8.3 */
2012 if (b->kind == sk_function_parms)
2014 error ("declaration of %q#D shadows a parameter", decl);
2015 return;
2019 /* The local structure or class can't use parameters of
2020 the containing function anyway. */
2021 if (DECL_CONTEXT (old) != current_function_decl)
2023 for (cp_binding_level *scope = current_binding_level;
2024 scope != old_scope; scope = scope->level_chain)
2025 if (scope->kind == sk_class
2026 && !LAMBDA_TYPE_P (scope->this_entity))
2027 return;
2029 /* Error if redeclaring a local declared in a
2030 init-statement or in the condition of an if or
2031 switch statement when the new declaration is in the
2032 outermost block of the controlled statement.
2033 Redeclaring a variable from a for or while condition is
2034 detected elsewhere. */
2035 else if (VAR_P (old)
2036 && old_scope == current_binding_level->level_chain
2037 && (old_scope->kind == sk_cond || old_scope->kind == sk_for))
2039 error ("redeclaration of %q#D", decl);
2040 inform (DECL_SOURCE_LOCATION (old),
2041 "%q#D previously declared here", old);
2042 return;
2044 /* C++11:
2045 3.3.3/3: The name declared in an exception-declaration (...)
2046 shall not be redeclared in the outermost block of the handler.
2047 3.3.3/2: A parameter name shall not be redeclared (...) in
2048 the outermost block of any handler associated with a
2049 function-try-block.
2050 3.4.1/15: The function parameter names shall not be redeclared
2051 in the exception-declaration nor in the outermost block of a
2052 handler for the function-try-block. */
2053 else if ((TREE_CODE (old) == VAR_DECL
2054 && old_scope == current_binding_level->level_chain
2055 && old_scope->kind == sk_catch)
2056 || (TREE_CODE (old) == PARM_DECL
2057 && (current_binding_level->kind == sk_catch
2058 || current_binding_level->level_chain->kind == sk_catch)
2059 && in_function_try_handler))
2061 if (permerror (input_location, "redeclaration of %q#D", decl))
2062 inform (DECL_SOURCE_LOCATION (old),
2063 "%q#D previously declared here", old);
2064 return;
2067 /* If '-Wshadow=compatible-local' is specified without other
2068 -Wshadow= flags, we will warn only when the type of the
2069 shadowing variable (DECL) can be converted to that of the
2070 shadowed parameter (OLD_LOCAL). The reason why we only check
2071 if DECL's type can be converted to OLD_LOCAL's type (but not the
2072 other way around) is because when users accidentally shadow a
2073 parameter, more than often they would use the variable
2074 thinking (mistakenly) it's still the parameter. It would be
2075 rare that users would use the variable in the place that
2076 expects the parameter but thinking it's a new decl. */
2078 enum opt_code warning_code;
2079 if (warn_shadow)
2080 warning_code = OPT_Wshadow;
2081 else if (warn_shadow_local)
2082 warning_code = OPT_Wshadow_local;
2083 else if (warn_shadow_compatible_local
2084 && can_convert (TREE_TYPE (old), TREE_TYPE (decl), tf_none))
2085 warning_code = OPT_Wshadow_compatible_local;
2086 else
2087 return;
2089 const char *msg;
2090 if (TREE_CODE (old) == PARM_DECL)
2091 msg = "declaration of %q#D shadows a parameter";
2092 else if (is_capture_proxy (old))
2093 msg = "declaration of %qD shadows a lambda capture";
2094 else
2095 msg = "declaration of %qD shadows a previous local";
2097 if (warning_at (input_location, warning_code, msg, decl))
2099 shadowed = old;
2100 goto inform_shadowed;
2102 return;
2105 if (!warn_shadow)
2106 return;
2108 /* Don't warn for artificial things that are not implicit typedefs. */
2109 if (DECL_ARTIFICIAL (decl) && !DECL_IMPLICIT_TYPEDEF_P (decl))
2110 return;
2112 if (nonlambda_method_basetype ())
2113 if (tree member = lookup_member (current_nonlambda_class_type (),
2114 DECL_NAME (decl), /*protect=*/0,
2115 /*want_type=*/false, tf_warning_or_error))
2117 member = MAYBE_BASELINK_FUNCTIONS (member);
2119 /* Warn if a variable shadows a non-function, or the variable
2120 is a function or a pointer-to-function. */
2121 if (!OVL_P (member)
2122 || TREE_CODE (decl) == FUNCTION_DECL
2123 || TYPE_PTRFN_P (TREE_TYPE (decl))
2124 || TYPE_PTRMEMFUNC_P (TREE_TYPE (decl)))
2126 if (warning_at (input_location, OPT_Wshadow,
2127 "declaration of %qD shadows a member of %qT",
2128 decl, current_nonlambda_class_type ())
2129 && DECL_P (member))
2131 shadowed = member;
2132 goto inform_shadowed;
2135 return;
2138 /* Now look for a namespace shadow. */
2139 old = find_namespace_value (current_namespace, DECL_NAME (decl));
2140 if (old
2141 && (VAR_P (old)
2142 || (TREE_CODE (old) == TYPE_DECL
2143 && (!DECL_ARTIFICIAL (old)
2144 || TREE_CODE (decl) == TYPE_DECL)))
2145 && !instantiating_current_function_p ())
2146 /* XXX shadow warnings in outer-more namespaces */
2148 if (warning_at (input_location, OPT_Wshadow,
2149 "declaration of %qD shadows a global declaration",
2150 decl))
2152 shadowed = old;
2153 goto inform_shadowed;
2155 return;
2158 return;
2160 inform_shadowed:
2161 inform (DECL_SOURCE_LOCATION (shadowed), "shadowed declaration is here");
2164 /* DECL is being pushed inside function CTX. Set its context, if
2165 needed. */
2167 static void
2168 set_decl_context_in_fn (tree ctx, tree decl)
2170 if (!DECL_CONTEXT (decl)
2171 /* A local declaration for a function doesn't constitute
2172 nesting. */
2173 && TREE_CODE (decl) != FUNCTION_DECL
2174 /* A local declaration for an `extern' variable is in the
2175 scope of the current namespace, not the current
2176 function. */
2177 && !(VAR_P (decl) && DECL_EXTERNAL (decl))
2178 /* When parsing the parameter list of a function declarator,
2179 don't set DECL_CONTEXT to an enclosing function. When we
2180 push the PARM_DECLs in order to process the function body,
2181 current_binding_level->this_entity will be set. */
2182 && !(TREE_CODE (decl) == PARM_DECL
2183 && current_binding_level->kind == sk_function_parms
2184 && current_binding_level->this_entity == NULL))
2185 DECL_CONTEXT (decl) = ctx;
2187 /* If this is the declaration for a namespace-scope function,
2188 but the declaration itself is in a local scope, mark the
2189 declaration. */
2190 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (decl))
2191 DECL_LOCAL_FUNCTION_P (decl) = 1;
2194 /* DECL is a local-scope decl with linkage. SHADOWED is true if the
2195 name is already bound at the current level.
2197 [basic.link] If there is a visible declaration of an entity with
2198 linkage having the same name and type, ignoring entities declared
2199 outside the innermost enclosing namespace scope, the block scope
2200 declaration declares that same entity and receives the linkage of
2201 the previous declaration.
2203 Also, make sure that this decl matches any existing external decl
2204 in the enclosing namespace. */
2206 static void
2207 set_local_extern_decl_linkage (tree decl, bool shadowed)
2209 tree ns_value = decl; /* Unique marker. */
2211 if (!shadowed)
2213 tree loc_value = innermost_non_namespace_value (DECL_NAME (decl));
2214 if (!loc_value)
2216 ns_value
2217 = find_namespace_value (current_namespace, DECL_NAME (decl));
2218 loc_value = ns_value;
2220 if (loc_value == error_mark_node)
2221 loc_value = NULL_TREE;
2223 for (ovl_iterator iter (loc_value); iter; ++iter)
2224 if (!iter.hidden_p ()
2225 && (TREE_STATIC (*iter) || DECL_EXTERNAL (*iter))
2226 && decls_match (*iter, decl))
2228 /* The standard only says that the local extern inherits
2229 linkage from the previous decl; in particular, default
2230 args are not shared. Add the decl into a hash table to
2231 make sure only the previous decl in this case is seen
2232 by the middle end. */
2233 struct cxx_int_tree_map *h;
2235 /* We inherit the outer decl's linkage. But we're a
2236 different decl. */
2237 TREE_PUBLIC (decl) = TREE_PUBLIC (*iter);
2239 if (cp_function_chain->extern_decl_map == NULL)
2240 cp_function_chain->extern_decl_map
2241 = hash_table<cxx_int_tree_map_hasher>::create_ggc (20);
2243 h = ggc_alloc<cxx_int_tree_map> ();
2244 h->uid = DECL_UID (decl);
2245 h->to = *iter;
2246 cxx_int_tree_map **loc = cp_function_chain->extern_decl_map
2247 ->find_slot (h, INSERT);
2248 *loc = h;
2249 break;
2253 if (TREE_PUBLIC (decl))
2255 /* DECL is externally visible. Make sure it matches a matching
2256 decl in the namespace scope. We only really need to check
2257 this when inserting the decl, not when we find an existing
2258 match in the current scope. However, in practice we're
2259 going to be inserting a new decl in the majority of cases --
2260 who writes multiple extern decls for the same thing in the
2261 same local scope? Doing it here often avoids a duplicate
2262 namespace lookup. */
2264 /* Avoid repeating a lookup. */
2265 if (ns_value == decl)
2266 ns_value = find_namespace_value (current_namespace, DECL_NAME (decl));
2268 if (ns_value == error_mark_node)
2269 ns_value = NULL_TREE;
2271 for (ovl_iterator iter (ns_value); iter; ++iter)
2273 tree other = *iter;
2275 if (!(TREE_PUBLIC (other) || DECL_EXTERNAL (other)))
2276 ; /* Not externally visible. */
2277 else if (DECL_EXTERN_C_P (decl) && DECL_EXTERN_C_P (other))
2278 ; /* Both are extern "C", we'll check via that mechanism. */
2279 else if (TREE_CODE (other) != TREE_CODE (decl)
2280 || ((VAR_P (decl) || matching_fn_p (other, decl))
2281 && !comptypes (TREE_TYPE (decl), TREE_TYPE (other),
2282 COMPARE_REDECLARATION)))
2284 if (permerror (DECL_SOURCE_LOCATION (decl),
2285 "local external declaration %q#D", decl))
2286 inform (DECL_SOURCE_LOCATION (other),
2287 "does not match previous declaration %q#D", other);
2288 break;
2294 /* Record DECL as belonging to the current lexical scope. Check for
2295 errors (such as an incompatible declaration for the same name
2296 already seen in the same scope). IS_FRIEND is true if DECL is
2297 declared as a friend.
2299 Returns either DECL or an old decl for the same name. If an old
2300 decl is returned, it may have been smashed to agree with what DECL
2301 says. */
2303 static tree
2304 do_pushdecl (tree decl, bool is_friend)
2306 if (decl == error_mark_node)
2307 return error_mark_node;
2309 if (!DECL_TEMPLATE_PARM_P (decl) && current_function_decl)
2310 set_decl_context_in_fn (current_function_decl, decl);
2312 /* The binding level we will be pushing into. During local class
2313 pushing, we want to push to the containing scope. */
2314 cp_binding_level *level = current_binding_level;
2315 while (level->kind == sk_class)
2316 level = level->level_chain;
2318 if (tree name = DECL_NAME (decl))
2320 cxx_binding *binding = NULL; /* Local scope binding. */
2321 tree ns = NULL_TREE; /* Searched namespace. */
2322 tree *slot = NULL; /* Binding slot in namespace. */
2323 tree old = NULL_TREE;
2325 if (level->kind == sk_namespace)
2327 /* We look in the decl's namespace for an existing
2328 declaration, even though we push into the current
2329 namespace. */
2330 ns = (DECL_NAMESPACE_SCOPE_P (decl)
2331 ? CP_DECL_CONTEXT (decl) : current_namespace);
2332 /* Create the binding, if this is current namespace, because
2333 that's where we'll be pushing anyway. */
2334 slot = find_namespace_slot (ns, name, ns == current_namespace);
2335 if (slot)
2336 old = MAYBE_STAT_DECL (*slot);
2338 else
2340 binding = find_local_binding (level, name);
2341 if (binding)
2342 old = binding->value;
2345 if (current_function_decl && VAR_OR_FUNCTION_DECL_P (decl)
2346 && DECL_EXTERNAL (decl))
2347 set_local_extern_decl_linkage (decl, old != NULL_TREE);
2349 if (old == error_mark_node)
2350 old = NULL_TREE;
2352 for (ovl_iterator iter (old); iter; ++iter)
2353 if (iter.using_p ())
2354 ; /* Ignore using decls here. */
2355 else if (tree match = duplicate_decls (decl, *iter, is_friend))
2357 if (iter.hidden_p ()
2358 && match != error_mark_node
2359 && !DECL_HIDDEN_P (match))
2361 /* Unhiding a previously hidden decl. */
2362 tree head = iter.reveal_node (old);
2363 if (head != old)
2365 if (!ns)
2367 update_local_overload (binding, head);
2368 binding->value = head;
2370 else if (STAT_HACK_P (*slot))
2371 STAT_DECL (*slot) = head;
2372 else
2373 *slot = head;
2375 if (TREE_CODE (match) == FUNCTION_DECL
2376 && DECL_EXTERN_C_P (match))
2377 /* We need to check and register the fn now. */
2378 check_extern_c_conflict (match);
2380 return match;
2383 /* We are pushing a new decl. */
2385 /* Skip a hidden builtin we failed to match already. There can
2386 only be one. */
2387 if (old && anticipated_builtin_p (old))
2388 old = OVL_CHAIN (old);
2390 check_template_shadow (decl);
2392 if (DECL_DECLARES_FUNCTION_P (decl))
2394 check_default_args (decl);
2396 if (is_friend)
2398 if (level->kind != sk_namespace)
2399 /* In a local class, a friend function declaration must
2400 find a matching decl in the innermost non-class scope.
2401 [class.friend/11] */
2402 error ("friend declaration %qD in local class without "
2403 "prior local declaration", decl);
2404 else if (!flag_friend_injection)
2405 /* Hide it from ordinary lookup. */
2406 DECL_ANTICIPATED (decl) = DECL_HIDDEN_FRIEND_P (decl) = true;
2410 if (level->kind != sk_namespace)
2412 check_local_shadow (decl);
2414 if (TREE_CODE (decl) == NAMESPACE_DECL)
2415 /* A local namespace alias. */
2416 set_identifier_type_value (name, NULL_TREE);
2418 if (!binding)
2419 binding = create_local_binding (level, name);
2421 else if (!slot)
2423 ns = current_namespace;
2424 slot = find_namespace_slot (ns, name, true);
2427 old = update_binding (level, binding, slot, old, decl, is_friend);
2429 if (old != decl)
2430 /* An existing decl matched, use it. */
2431 decl = old;
2432 else if (TREE_CODE (decl) == TYPE_DECL)
2434 tree type = TREE_TYPE (decl);
2436 if (type != error_mark_node)
2438 if (TYPE_NAME (type) != decl)
2439 set_underlying_type (decl);
2441 if (!ns)
2442 set_identifier_type_value_with_scope (name, decl, level);
2443 else
2444 SET_IDENTIFIER_TYPE_VALUE (name, global_type_node);
2447 /* If this is a locally defined typedef in a function that
2448 is not a template instantation, record it to implement
2449 -Wunused-local-typedefs. */
2450 if (!instantiating_current_function_p ())
2451 record_locally_defined_typedef (decl);
2453 else if (VAR_P (decl))
2454 maybe_register_incomplete_var (decl);
2455 else if (TREE_CODE (decl) == FUNCTION_DECL && DECL_EXTERN_C_P (decl))
2456 check_extern_c_conflict (decl);
2458 else
2459 add_decl_to_level (level, decl);
2461 return decl;
2464 /* Record a decl-node X as belonging to the current lexical scope.
2465 It's a friend if IS_FRIEND is true -- which affects exactly where
2466 we push it. */
2468 tree
2469 pushdecl (tree x, bool is_friend)
2471 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
2472 tree ret = do_pushdecl (x, is_friend);
2473 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
2474 return ret;
2477 /* Enter DECL into the symbol table, if that's appropriate. Returns
2478 DECL, or a modified version thereof. */
2480 tree
2481 maybe_push_decl (tree decl)
2483 tree type = TREE_TYPE (decl);
2485 /* Add this decl to the current binding level, but not if it comes
2486 from another scope, e.g. a static member variable. TEM may equal
2487 DECL or it may be a previous decl of the same name. */
2488 if (decl == error_mark_node
2489 || (TREE_CODE (decl) != PARM_DECL
2490 && DECL_CONTEXT (decl) != NULL_TREE
2491 /* Definitions of namespace members outside their namespace are
2492 possible. */
2493 && !DECL_NAMESPACE_SCOPE_P (decl))
2494 || (TREE_CODE (decl) == TEMPLATE_DECL && !namespace_bindings_p ())
2495 || type == unknown_type_node
2496 /* The declaration of a template specialization does not affect
2497 the functions available for overload resolution, so we do not
2498 call pushdecl. */
2499 || (TREE_CODE (decl) == FUNCTION_DECL
2500 && DECL_TEMPLATE_SPECIALIZATION (decl)))
2501 return decl;
2502 else
2503 return pushdecl (decl);
2506 /* Bind DECL to ID in the current_binding_level, assumed to be a local
2507 binding level. If IS_USING is true, DECL got here through a
2508 using-declaration. */
2510 static void
2511 push_local_binding (tree id, tree decl, bool is_using)
2513 /* Skip over any local classes. This makes sense if we call
2514 push_local_binding with a friend decl of a local class. */
2515 cp_binding_level *b = innermost_nonclass_level ();
2517 gcc_assert (b->kind != sk_namespace);
2518 if (find_local_binding (b, id))
2520 /* Supplement the existing binding. */
2521 if (!supplement_binding (IDENTIFIER_BINDING (id), decl))
2522 /* It didn't work. Something else must be bound at this
2523 level. Do not add DECL to the list of things to pop
2524 later. */
2525 return;
2527 else
2528 /* Create a new binding. */
2529 push_binding (id, decl, b);
2531 if (TREE_CODE (decl) == OVERLOAD || is_using)
2532 /* We must put the OVERLOAD or using into a TREE_LIST since we
2533 cannot use the decl's chain itself. */
2534 decl = build_tree_list (NULL_TREE, decl);
2536 /* And put DECL on the list of things declared by the current
2537 binding level. */
2538 add_decl_to_level (b, decl);
2541 /* Check to see whether or not DECL is a variable that would have been
2542 in scope under the ARM, but is not in scope under the ANSI/ISO
2543 standard. If so, issue an error message. If name lookup would
2544 work in both cases, but return a different result, this function
2545 returns the result of ANSI/ISO lookup. Otherwise, it returns
2546 DECL. */
2548 tree
2549 check_for_out_of_scope_variable (tree decl)
2551 tree shadowed;
2553 /* We only care about out of scope variables. */
2554 if (!(VAR_P (decl) && DECL_DEAD_FOR_LOCAL (decl)))
2555 return decl;
2557 shadowed = DECL_HAS_SHADOWED_FOR_VAR_P (decl)
2558 ? DECL_SHADOWED_FOR_VAR (decl) : NULL_TREE ;
2559 while (shadowed != NULL_TREE && VAR_P (shadowed)
2560 && DECL_DEAD_FOR_LOCAL (shadowed))
2561 shadowed = DECL_HAS_SHADOWED_FOR_VAR_P (shadowed)
2562 ? DECL_SHADOWED_FOR_VAR (shadowed) : NULL_TREE;
2563 if (!shadowed)
2564 shadowed = find_namespace_value (current_namespace, DECL_NAME (decl));
2565 if (shadowed)
2567 if (!DECL_ERROR_REPORTED (decl))
2569 warning (0, "name lookup of %qD changed", DECL_NAME (decl));
2570 warning_at (DECL_SOURCE_LOCATION (shadowed), 0,
2571 " matches this %qD under ISO standard rules",
2572 shadowed);
2573 warning_at (DECL_SOURCE_LOCATION (decl), 0,
2574 " matches this %qD under old rules", decl);
2575 DECL_ERROR_REPORTED (decl) = 1;
2577 return shadowed;
2580 /* If we have already complained about this declaration, there's no
2581 need to do it again. */
2582 if (DECL_ERROR_REPORTED (decl))
2583 return decl;
2585 DECL_ERROR_REPORTED (decl) = 1;
2587 if (TREE_TYPE (decl) == error_mark_node)
2588 return decl;
2590 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
2592 error ("name lookup of %qD changed for ISO %<for%> scoping",
2593 DECL_NAME (decl));
2594 error (" cannot use obsolete binding at %q+D because "
2595 "it has a destructor", decl);
2596 return error_mark_node;
2598 else
2600 permerror (input_location, "name lookup of %qD changed for ISO %<for%> scoping",
2601 DECL_NAME (decl));
2602 if (flag_permissive)
2603 permerror (DECL_SOURCE_LOCATION (decl),
2604 " using obsolete binding at %qD", decl);
2605 else
2607 static bool hint;
2608 if (!hint)
2610 inform (input_location, "(if you use %<-fpermissive%> G++ will accept your code)");
2611 hint = true;
2616 return decl;
2619 /* true means unconditionally make a BLOCK for the next level pushed. */
2621 static bool keep_next_level_flag;
2623 static int binding_depth = 0;
2625 static void
2626 indent (int depth)
2628 int i;
2630 for (i = 0; i < depth * 2; i++)
2631 putc (' ', stderr);
2634 /* Return a string describing the kind of SCOPE we have. */
2635 static const char *
2636 cp_binding_level_descriptor (cp_binding_level *scope)
2638 /* The order of this table must match the "scope_kind"
2639 enumerators. */
2640 static const char* scope_kind_names[] = {
2641 "block-scope",
2642 "cleanup-scope",
2643 "try-scope",
2644 "catch-scope",
2645 "for-scope",
2646 "function-parameter-scope",
2647 "class-scope",
2648 "namespace-scope",
2649 "template-parameter-scope",
2650 "template-explicit-spec-scope"
2652 const scope_kind kind = scope->explicit_spec_p
2653 ? sk_template_spec : scope->kind;
2655 return scope_kind_names[kind];
2658 /* Output a debugging information about SCOPE when performing
2659 ACTION at LINE. */
2660 static void
2661 cp_binding_level_debug (cp_binding_level *scope, int line, const char *action)
2663 const char *desc = cp_binding_level_descriptor (scope);
2664 if (scope->this_entity)
2665 verbatim ("%s %<%s(%E)%> %p %d\n", action, desc,
2666 scope->this_entity, (void *) scope, line);
2667 else
2668 verbatim ("%s %s %p %d\n", action, desc, (void *) scope, line);
2671 /* Return the estimated initial size of the hashtable of a NAMESPACE
2672 scope. */
2674 static inline size_t
2675 namespace_scope_ht_size (tree ns)
2677 tree name = DECL_NAME (ns);
2679 return name == std_identifier
2680 ? NAMESPACE_STD_HT_SIZE
2681 : (name == global_identifier
2682 ? GLOBAL_SCOPE_HT_SIZE
2683 : NAMESPACE_ORDINARY_HT_SIZE);
2686 /* A chain of binding_level structures awaiting reuse. */
2688 static GTY((deletable)) cp_binding_level *free_binding_level;
2690 /* Insert SCOPE as the innermost binding level. */
2692 void
2693 push_binding_level (cp_binding_level *scope)
2695 /* Add it to the front of currently active scopes stack. */
2696 scope->level_chain = current_binding_level;
2697 current_binding_level = scope;
2698 keep_next_level_flag = false;
2700 if (ENABLE_SCOPE_CHECKING)
2702 scope->binding_depth = binding_depth;
2703 indent (binding_depth);
2704 cp_binding_level_debug (scope, LOCATION_LINE (input_location),
2705 "push");
2706 binding_depth++;
2710 /* Create a new KIND scope and make it the top of the active scopes stack.
2711 ENTITY is the scope of the associated C++ entity (namespace, class,
2712 function, C++0x enumeration); it is NULL otherwise. */
2714 cp_binding_level *
2715 begin_scope (scope_kind kind, tree entity)
2717 cp_binding_level *scope;
2719 /* Reuse or create a struct for this binding level. */
2720 if (!ENABLE_SCOPE_CHECKING && free_binding_level)
2722 scope = free_binding_level;
2723 free_binding_level = scope->level_chain;
2724 memset (scope, 0, sizeof (cp_binding_level));
2726 else
2727 scope = ggc_cleared_alloc<cp_binding_level> ();
2729 scope->this_entity = entity;
2730 scope->more_cleanups_ok = true;
2731 switch (kind)
2733 case sk_cleanup:
2734 scope->keep = true;
2735 break;
2737 case sk_template_spec:
2738 scope->explicit_spec_p = true;
2739 kind = sk_template_parms;
2740 /* Fall through. */
2741 case sk_template_parms:
2742 case sk_block:
2743 case sk_try:
2744 case sk_catch:
2745 case sk_for:
2746 case sk_cond:
2747 case sk_class:
2748 case sk_scoped_enum:
2749 case sk_function_parms:
2750 case sk_transaction:
2751 case sk_omp:
2752 scope->keep = keep_next_level_flag;
2753 break;
2755 case sk_namespace:
2756 NAMESPACE_LEVEL (entity) = scope;
2757 break;
2759 default:
2760 /* Should not happen. */
2761 gcc_unreachable ();
2762 break;
2764 scope->kind = kind;
2766 push_binding_level (scope);
2768 return scope;
2771 /* We're about to leave current scope. Pop the top of the stack of
2772 currently active scopes. Return the enclosing scope, now active. */
2774 cp_binding_level *
2775 leave_scope (void)
2777 cp_binding_level *scope = current_binding_level;
2779 if (scope->kind == sk_namespace && class_binding_level)
2780 current_binding_level = class_binding_level;
2782 /* We cannot leave a scope, if there are none left. */
2783 if (NAMESPACE_LEVEL (global_namespace))
2784 gcc_assert (!global_scope_p (scope));
2786 if (ENABLE_SCOPE_CHECKING)
2788 indent (--binding_depth);
2789 cp_binding_level_debug (scope, LOCATION_LINE (input_location),
2790 "leave");
2793 /* Move one nesting level up. */
2794 current_binding_level = scope->level_chain;
2796 /* Namespace-scopes are left most probably temporarily, not
2797 completely; they can be reopened later, e.g. in namespace-extension
2798 or any name binding activity that requires us to resume a
2799 namespace. For classes, we cache some binding levels. For other
2800 scopes, we just make the structure available for reuse. */
2801 if (scope->kind != sk_namespace
2802 && scope->kind != sk_class)
2804 scope->level_chain = free_binding_level;
2805 gcc_assert (!ENABLE_SCOPE_CHECKING
2806 || scope->binding_depth == binding_depth);
2807 free_binding_level = scope;
2810 if (scope->kind == sk_class)
2812 /* Reset DEFINING_CLASS_P to allow for reuse of a
2813 class-defining scope in a non-defining context. */
2814 scope->defining_class_p = 0;
2816 /* Find the innermost enclosing class scope, and reset
2817 CLASS_BINDING_LEVEL appropriately. */
2818 class_binding_level = NULL;
2819 for (scope = current_binding_level; scope; scope = scope->level_chain)
2820 if (scope->kind == sk_class)
2822 class_binding_level = scope;
2823 break;
2827 return current_binding_level;
2830 static void
2831 resume_scope (cp_binding_level* b)
2833 /* Resuming binding levels is meant only for namespaces,
2834 and those cannot nest into classes. */
2835 gcc_assert (!class_binding_level);
2836 /* Also, resuming a non-directly nested namespace is a no-no. */
2837 gcc_assert (b->level_chain == current_binding_level);
2838 current_binding_level = b;
2839 if (ENABLE_SCOPE_CHECKING)
2841 b->binding_depth = binding_depth;
2842 indent (binding_depth);
2843 cp_binding_level_debug (b, LOCATION_LINE (input_location), "resume");
2844 binding_depth++;
2848 /* Return the innermost binding level that is not for a class scope. */
2850 static cp_binding_level *
2851 innermost_nonclass_level (void)
2853 cp_binding_level *b;
2855 b = current_binding_level;
2856 while (b->kind == sk_class)
2857 b = b->level_chain;
2859 return b;
2862 /* We're defining an object of type TYPE. If it needs a cleanup, but
2863 we're not allowed to add any more objects with cleanups to the current
2864 scope, create a new binding level. */
2866 void
2867 maybe_push_cleanup_level (tree type)
2869 if (type != error_mark_node
2870 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
2871 && current_binding_level->more_cleanups_ok == 0)
2873 begin_scope (sk_cleanup, NULL);
2874 current_binding_level->statement_list = push_stmt_list ();
2878 /* Return true if we are in the global binding level. */
2880 bool
2881 global_bindings_p (void)
2883 return global_scope_p (current_binding_level);
2886 /* True if we are currently in a toplevel binding level. This
2887 means either the global binding level or a namespace in a toplevel
2888 binding level. Since there are no non-toplevel namespace levels,
2889 this really means any namespace or template parameter level. We
2890 also include a class whose context is toplevel. */
2892 bool
2893 toplevel_bindings_p (void)
2895 cp_binding_level *b = innermost_nonclass_level ();
2897 return b->kind == sk_namespace || b->kind == sk_template_parms;
2900 /* True if this is a namespace scope, or if we are defining a class
2901 which is itself at namespace scope, or whose enclosing class is
2902 such a class, etc. */
2904 bool
2905 namespace_bindings_p (void)
2907 cp_binding_level *b = innermost_nonclass_level ();
2909 return b->kind == sk_namespace;
2912 /* True if the innermost non-class scope is a block scope. */
2914 bool
2915 local_bindings_p (void)
2917 cp_binding_level *b = innermost_nonclass_level ();
2918 return b->kind < sk_function_parms || b->kind == sk_omp;
2921 /* True if the current level needs to have a BLOCK made. */
2923 bool
2924 kept_level_p (void)
2926 return (current_binding_level->blocks != NULL_TREE
2927 || current_binding_level->keep
2928 || current_binding_level->kind == sk_cleanup
2929 || current_binding_level->names != NULL_TREE
2930 || current_binding_level->using_directives);
2933 /* Returns the kind of the innermost scope. */
2935 scope_kind
2936 innermost_scope_kind (void)
2938 return current_binding_level->kind;
2941 /* Returns true if this scope was created to store template parameters. */
2943 bool
2944 template_parm_scope_p (void)
2946 return innermost_scope_kind () == sk_template_parms;
2949 /* If KEEP is true, make a BLOCK node for the next binding level,
2950 unconditionally. Otherwise, use the normal logic to decide whether
2951 or not to create a BLOCK. */
2953 void
2954 keep_next_level (bool keep)
2956 keep_next_level_flag = keep;
2959 /* Return the list of declarations of the current local scope. */
2961 tree
2962 get_local_decls (void)
2964 gcc_assert (current_binding_level->kind != sk_namespace
2965 && current_binding_level->kind != sk_class);
2966 return current_binding_level->names;
2969 /* Return how many function prototypes we are currently nested inside. */
2972 function_parm_depth (void)
2974 int level = 0;
2975 cp_binding_level *b;
2977 for (b = current_binding_level;
2978 b->kind == sk_function_parms;
2979 b = b->level_chain)
2980 ++level;
2982 return level;
2985 /* For debugging. */
2986 static int no_print_functions = 0;
2987 static int no_print_builtins = 0;
2989 static void
2990 print_binding_level (cp_binding_level* lvl)
2992 tree t;
2993 int i = 0, len;
2994 fprintf (stderr, " blocks=%p", (void *) lvl->blocks);
2995 if (lvl->more_cleanups_ok)
2996 fprintf (stderr, " more-cleanups-ok");
2997 if (lvl->have_cleanups)
2998 fprintf (stderr, " have-cleanups");
2999 fprintf (stderr, "\n");
3000 if (lvl->names)
3002 fprintf (stderr, " names:\t");
3003 /* We can probably fit 3 names to a line? */
3004 for (t = lvl->names; t; t = TREE_CHAIN (t))
3006 if (no_print_functions && (TREE_CODE (t) == FUNCTION_DECL))
3007 continue;
3008 if (no_print_builtins
3009 && (TREE_CODE (t) == TYPE_DECL)
3010 && DECL_IS_BUILTIN (t))
3011 continue;
3013 /* Function decls tend to have longer names. */
3014 if (TREE_CODE (t) == FUNCTION_DECL)
3015 len = 3;
3016 else
3017 len = 2;
3018 i += len;
3019 if (i > 6)
3021 fprintf (stderr, "\n\t");
3022 i = len;
3024 print_node_brief (stderr, "", t, 0);
3025 if (t == error_mark_node)
3026 break;
3028 if (i)
3029 fprintf (stderr, "\n");
3031 if (vec_safe_length (lvl->class_shadowed))
3033 size_t i;
3034 cp_class_binding *b;
3035 fprintf (stderr, " class-shadowed:");
3036 FOR_EACH_VEC_ELT (*lvl->class_shadowed, i, b)
3037 fprintf (stderr, " %s ", IDENTIFIER_POINTER (b->identifier));
3038 fprintf (stderr, "\n");
3040 if (lvl->type_shadowed)
3042 fprintf (stderr, " type-shadowed:");
3043 for (t = lvl->type_shadowed; t; t = TREE_CHAIN (t))
3045 fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
3047 fprintf (stderr, "\n");
3051 DEBUG_FUNCTION void
3052 debug (cp_binding_level &ref)
3054 print_binding_level (&ref);
3057 DEBUG_FUNCTION void
3058 debug (cp_binding_level *ptr)
3060 if (ptr)
3061 debug (*ptr);
3062 else
3063 fprintf (stderr, "<nil>\n");
3067 void
3068 print_other_binding_stack (cp_binding_level *stack)
3070 cp_binding_level *level;
3071 for (level = stack; !global_scope_p (level); level = level->level_chain)
3073 fprintf (stderr, "binding level %p\n", (void *) level);
3074 print_binding_level (level);
3078 void
3079 print_binding_stack (void)
3081 cp_binding_level *b;
3082 fprintf (stderr, "current_binding_level=%p\n"
3083 "class_binding_level=%p\n"
3084 "NAMESPACE_LEVEL (global_namespace)=%p\n",
3085 (void *) current_binding_level, (void *) class_binding_level,
3086 (void *) NAMESPACE_LEVEL (global_namespace));
3087 if (class_binding_level)
3089 for (b = class_binding_level; b; b = b->level_chain)
3090 if (b == current_binding_level)
3091 break;
3092 if (b)
3093 b = class_binding_level;
3094 else
3095 b = current_binding_level;
3097 else
3098 b = current_binding_level;
3099 print_other_binding_stack (b);
3100 fprintf (stderr, "global:\n");
3101 print_binding_level (NAMESPACE_LEVEL (global_namespace));
3104 /* Return the type associated with ID. */
3106 static tree
3107 identifier_type_value_1 (tree id)
3109 /* There is no type with that name, anywhere. */
3110 if (REAL_IDENTIFIER_TYPE_VALUE (id) == NULL_TREE)
3111 return NULL_TREE;
3112 /* This is not the type marker, but the real thing. */
3113 if (REAL_IDENTIFIER_TYPE_VALUE (id) != global_type_node)
3114 return REAL_IDENTIFIER_TYPE_VALUE (id);
3115 /* Have to search for it. It must be on the global level, now.
3116 Ask lookup_name not to return non-types. */
3117 id = lookup_name_real (id, 2, 1, /*block_p=*/true, 0, 0);
3118 if (id)
3119 return TREE_TYPE (id);
3120 return NULL_TREE;
3123 /* Wrapper for identifier_type_value_1. */
3125 tree
3126 identifier_type_value (tree id)
3128 tree ret;
3129 timevar_start (TV_NAME_LOOKUP);
3130 ret = identifier_type_value_1 (id);
3131 timevar_stop (TV_NAME_LOOKUP);
3132 return ret;
3136 /* Return the IDENTIFIER_GLOBAL_VALUE of T, for use in common code, since
3137 the definition of IDENTIFIER_GLOBAL_VALUE is different for C and C++. */
3139 tree
3140 identifier_global_value (tree t)
3142 return IDENTIFIER_GLOBAL_VALUE (t);
3145 /* Push a definition of struct, union or enum tag named ID. into
3146 binding_level B. DECL is a TYPE_DECL for the type. We assume that
3147 the tag ID is not already defined. */
3149 static void
3150 set_identifier_type_value_with_scope (tree id, tree decl, cp_binding_level *b)
3152 tree type;
3154 if (b->kind != sk_namespace)
3156 /* Shadow the marker, not the real thing, so that the marker
3157 gets restored later. */
3158 tree old_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
3159 b->type_shadowed
3160 = tree_cons (id, old_type_value, b->type_shadowed);
3161 type = decl ? TREE_TYPE (decl) : NULL_TREE;
3162 TREE_TYPE (b->type_shadowed) = type;
3164 else
3166 tree *slot = find_namespace_slot (current_namespace, id, true);
3167 gcc_assert (decl);
3168 update_binding (b, NULL, slot, MAYBE_STAT_DECL (*slot), decl, false);
3170 /* Store marker instead of real type. */
3171 type = global_type_node;
3173 SET_IDENTIFIER_TYPE_VALUE (id, type);
3176 /* As set_identifier_type_value_with_scope, but using
3177 current_binding_level. */
3179 void
3180 set_identifier_type_value (tree id, tree decl)
3182 set_identifier_type_value_with_scope (id, decl, current_binding_level);
3185 /* Return the name for the constructor (or destructor) for the
3186 specified class TYPE. When given a template, this routine doesn't
3187 lose the specialization. */
3189 static inline tree
3190 constructor_name_full (tree type)
3192 return TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
3195 /* Return the name for the constructor (or destructor) for the
3196 specified class. When given a template, return the plain
3197 unspecialized name. */
3199 tree
3200 constructor_name (tree type)
3202 tree name;
3203 name = constructor_name_full (type);
3204 if (IDENTIFIER_TEMPLATE (name))
3205 name = IDENTIFIER_TEMPLATE (name);
3206 return name;
3209 /* Returns TRUE if NAME is the name for the constructor for TYPE,
3210 which must be a class type. */
3212 bool
3213 constructor_name_p (tree name, tree type)
3215 tree ctor_name;
3217 gcc_assert (MAYBE_CLASS_TYPE_P (type));
3219 if (!name)
3220 return false;
3222 if (!identifier_p (name))
3223 return false;
3225 /* These don't have names. */
3226 if (TREE_CODE (type) == DECLTYPE_TYPE
3227 || TREE_CODE (type) == TYPEOF_TYPE)
3228 return false;
3230 ctor_name = constructor_name_full (type);
3231 if (name == ctor_name)
3232 return true;
3233 if (IDENTIFIER_TEMPLATE (ctor_name)
3234 && name == IDENTIFIER_TEMPLATE (ctor_name))
3235 return true;
3236 return false;
3239 /* Counter used to create anonymous type names. */
3241 static GTY(()) int anon_cnt;
3243 /* Return an IDENTIFIER which can be used as a name for
3244 unnamed structs and unions. */
3246 tree
3247 make_anon_name (void)
3249 char buf[32];
3251 sprintf (buf, anon_aggrname_format (), anon_cnt++);
3252 return get_identifier (buf);
3255 /* This code is practically identical to that for creating
3256 anonymous names, but is just used for lambdas instead. This isn't really
3257 necessary, but it's convenient to avoid treating lambdas like other
3258 unnamed types. */
3260 static GTY(()) int lambda_cnt = 0;
3262 tree
3263 make_lambda_name (void)
3265 char buf[32];
3267 sprintf (buf, LAMBDANAME_FORMAT, lambda_cnt++);
3268 return get_identifier (buf);
3271 /* Insert another USING_DECL into the current binding level, returning
3272 this declaration. If this is a redeclaration, do nothing, and
3273 return NULL_TREE if this not in namespace scope (in namespace
3274 scope, a using decl might extend any previous bindings). */
3276 static tree
3277 push_using_decl_1 (tree scope, tree name)
3279 tree decl;
3281 gcc_assert (TREE_CODE (scope) == NAMESPACE_DECL);
3282 gcc_assert (identifier_p (name));
3283 for (decl = current_binding_level->usings; decl; decl = DECL_CHAIN (decl))
3284 if (USING_DECL_SCOPE (decl) == scope && DECL_NAME (decl) == name)
3285 break;
3286 if (decl)
3287 return namespace_bindings_p () ? decl : NULL_TREE;
3288 decl = build_lang_decl (USING_DECL, name, NULL_TREE);
3289 USING_DECL_SCOPE (decl) = scope;
3290 DECL_CHAIN (decl) = current_binding_level->usings;
3291 current_binding_level->usings = decl;
3292 return decl;
3295 /* Wrapper for push_using_decl_1. */
3297 static tree
3298 push_using_decl (tree scope, tree name)
3300 tree ret;
3301 timevar_start (TV_NAME_LOOKUP);
3302 ret = push_using_decl_1 (scope, name);
3303 timevar_stop (TV_NAME_LOOKUP);
3304 return ret;
3307 /* Same as pushdecl, but define X in binding-level LEVEL. We rely on the
3308 caller to set DECL_CONTEXT properly.
3310 Note that this must only be used when X will be the new innermost
3311 binding for its name, as we tack it onto the front of IDENTIFIER_BINDING
3312 without checking to see if the current IDENTIFIER_BINDING comes from a
3313 closer binding level than LEVEL. */
3315 static tree
3316 do_pushdecl_with_scope (tree x, cp_binding_level *level, bool is_friend)
3318 cp_binding_level *b;
3319 tree function_decl = current_function_decl;
3321 current_function_decl = NULL_TREE;
3322 if (level->kind == sk_class)
3324 b = class_binding_level;
3325 class_binding_level = level;
3326 pushdecl_class_level (x);
3327 class_binding_level = b;
3329 else
3331 b = current_binding_level;
3332 current_binding_level = level;
3333 x = pushdecl (x, is_friend);
3334 current_binding_level = b;
3336 current_function_decl = function_decl;
3337 return x;
3340 /* Inject X into the local scope just before the function parms. */
3342 tree
3343 pushdecl_outermost_localscope (tree x)
3345 cp_binding_level *b = NULL;
3346 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3348 /* Find the scope just inside the function parms. */
3349 for (cp_binding_level *n = current_binding_level;
3350 n->kind != sk_function_parms; n = b->level_chain)
3351 b = n;
3353 tree ret = b ? do_pushdecl_with_scope (x, b, false) : error_mark_node;
3354 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3356 return ret;
3359 /* Check a non-member using-declaration. Return the name and scope
3360 being used, and the USING_DECL, or NULL_TREE on failure. */
3362 static tree
3363 validate_nonmember_using_decl (tree decl, tree scope, tree name)
3365 /* [namespace.udecl]
3366 A using-declaration for a class member shall be a
3367 member-declaration. */
3368 if (TYPE_P (scope))
3370 error ("%qT is not a namespace or unscoped enum", scope);
3371 return NULL_TREE;
3373 else if (scope == error_mark_node)
3374 return NULL_TREE;
3376 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR)
3378 /* 7.3.3/5
3379 A using-declaration shall not name a template-id. */
3380 error ("a using-declaration cannot specify a template-id. "
3381 "Try %<using %D%>", name);
3382 return NULL_TREE;
3385 if (TREE_CODE (decl) == NAMESPACE_DECL)
3387 error ("namespace %qD not allowed in using-declaration", decl);
3388 return NULL_TREE;
3391 if (TREE_CODE (decl) == SCOPE_REF)
3393 /* It's a nested name with template parameter dependent scope.
3394 This can only be using-declaration for class member. */
3395 error ("%qT is not a namespace", TREE_OPERAND (decl, 0));
3396 return NULL_TREE;
3399 decl = OVL_FIRST (decl);
3401 /* Make a USING_DECL. */
3402 tree using_decl = push_using_decl (scope, name);
3404 if (using_decl == NULL_TREE
3405 && at_function_scope_p ()
3406 && VAR_P (decl))
3407 /* C++11 7.3.3/10. */
3408 error ("%qD is already declared in this scope", name);
3410 return using_decl;
3413 /* Process a local-scope or namespace-scope using declaration. SCOPE
3414 is the nominated scope to search for NAME. VALUE_P and TYPE_P
3415 point to the binding for NAME in the current scope and are
3416 updated. */
3418 static void
3419 do_nonmember_using_decl (tree scope, tree name, tree *value_p, tree *type_p)
3421 name_lookup lookup (name, 0);
3423 if (!qualified_namespace_lookup (scope, &lookup))
3425 error ("%qD not declared", name);
3426 return;
3428 else if (TREE_CODE (lookup.value) == TREE_LIST)
3430 error ("reference to %qD is ambiguous", name);
3431 print_candidates (lookup.value);
3432 lookup.value = NULL_TREE;
3435 if (lookup.type && TREE_CODE (lookup.type) == TREE_LIST)
3437 error ("reference to %qD is ambiguous", name);
3438 print_candidates (lookup.type);
3439 lookup.type = NULL_TREE;
3442 tree value = *value_p;
3443 tree type = *type_p;
3445 /* Shift the old and new bindings around so we're comparing class and
3446 enumeration names to each other. */
3447 if (value && DECL_IMPLICIT_TYPEDEF_P (value))
3449 type = value;
3450 value = NULL_TREE;
3453 if (lookup.value && DECL_IMPLICIT_TYPEDEF_P (lookup.value))
3455 lookup.type = lookup.value;
3456 lookup.value = NULL_TREE;
3459 if (lookup.value && lookup.value != value)
3461 /* Check for using functions. */
3462 if (OVL_P (lookup.value) && (!value || OVL_P (value)))
3464 for (lkp_iterator usings (lookup.value); usings; ++usings)
3466 tree new_fn = *usings;
3468 /* [namespace.udecl]
3470 If a function declaration in namespace scope or block
3471 scope has the same name and the same parameter types as a
3472 function introduced by a using declaration the program is
3473 ill-formed. */
3474 bool found = false;
3475 for (ovl_iterator old (value); !found && old; ++old)
3477 tree old_fn = *old;
3479 if (new_fn == old_fn)
3480 /* The function already exists in the current
3481 namespace. */
3482 found = true;
3483 else if (old.using_p ())
3484 continue; /* This is a using decl. */
3485 else if (old.hidden_p () && !DECL_HIDDEN_FRIEND_P (old_fn))
3486 continue; /* This is an anticipated builtin. */
3487 else if (!matching_fn_p (new_fn, old_fn))
3488 continue; /* Parameters do not match. */
3489 else if (decls_match (new_fn, old_fn))
3490 found = true;
3491 else
3493 diagnose_name_conflict (new_fn, old_fn);
3494 found = true;
3498 if (!found)
3499 /* Unlike the overload case we don't drop anticipated
3500 builtins here. They don't cause a problem, and
3501 we'd like to match them with a future
3502 declaration. */
3503 value = ovl_insert (new_fn, value, true);
3506 else if (value
3507 /* Ignore anticipated builtins. */
3508 && !anticipated_builtin_p (value)
3509 && !decls_match (lookup.value, value))
3510 diagnose_name_conflict (lookup.value, value);
3511 else
3512 value = lookup.value;
3515 if (lookup.type && lookup.type != type)
3517 if (type && !decls_match (lookup.type, type))
3518 diagnose_name_conflict (lookup.type, type);
3519 else
3520 type = lookup.type;
3523 /* If bind->value is empty, shift any class or enumeration name back. */
3524 if (!value)
3526 value = type;
3527 type = NULL_TREE;
3530 *value_p = value;
3531 *type_p = type;
3534 /* Returns true if ANCESTOR encloses DESCENDANT, including matching.
3535 Both are namespaces. */
3537 bool
3538 is_nested_namespace (tree ancestor, tree descendant, bool inline_only)
3540 int depth = SCOPE_DEPTH (ancestor);
3542 if (!depth && !inline_only)
3543 /* The global namespace encloses everything. */
3544 return true;
3546 while (SCOPE_DEPTH (descendant) > depth
3547 && (!inline_only || DECL_NAMESPACE_INLINE_P (descendant)))
3548 descendant = CP_DECL_CONTEXT (descendant);
3550 return ancestor == descendant;
3553 /* Returns true if ROOT (a namespace, class, or function) encloses
3554 CHILD. CHILD may be either a class type or a namespace. */
3556 bool
3557 is_ancestor (tree root, tree child)
3559 gcc_assert ((TREE_CODE (root) == NAMESPACE_DECL
3560 || TREE_CODE (root) == FUNCTION_DECL
3561 || CLASS_TYPE_P (root)));
3562 gcc_assert ((TREE_CODE (child) == NAMESPACE_DECL
3563 || CLASS_TYPE_P (child)));
3565 /* The global namespace encloses everything. */
3566 if (root == global_namespace)
3567 return true;
3569 /* Search until we reach namespace scope. */
3570 while (TREE_CODE (child) != NAMESPACE_DECL)
3572 /* If we've reached the ROOT, it encloses CHILD. */
3573 if (root == child)
3574 return true;
3575 /* Go out one level. */
3576 if (TYPE_P (child))
3577 child = TYPE_NAME (child);
3578 child = CP_DECL_CONTEXT (child);
3581 if (TREE_CODE (root) == NAMESPACE_DECL)
3582 return is_nested_namespace (root, child);
3584 return false;
3587 /* Enter the class or namespace scope indicated by T suitable for name
3588 lookup. T can be arbitrary scope, not necessary nested inside the
3589 current scope. Returns a non-null scope to pop iff pop_scope
3590 should be called later to exit this scope. */
3592 tree
3593 push_scope (tree t)
3595 if (TREE_CODE (t) == NAMESPACE_DECL)
3596 push_decl_namespace (t);
3597 else if (CLASS_TYPE_P (t))
3599 if (!at_class_scope_p ()
3600 || !same_type_p (current_class_type, t))
3601 push_nested_class (t);
3602 else
3603 /* T is the same as the current scope. There is therefore no
3604 need to re-enter the scope. Since we are not actually
3605 pushing a new scope, our caller should not call
3606 pop_scope. */
3607 t = NULL_TREE;
3610 return t;
3613 /* Leave scope pushed by push_scope. */
3615 void
3616 pop_scope (tree t)
3618 if (t == NULL_TREE)
3619 return;
3620 if (TREE_CODE (t) == NAMESPACE_DECL)
3621 pop_decl_namespace ();
3622 else if CLASS_TYPE_P (t)
3623 pop_nested_class ();
3626 /* Subroutine of push_inner_scope. */
3628 static void
3629 push_inner_scope_r (tree outer, tree inner)
3631 tree prev;
3633 if (outer == inner
3634 || (TREE_CODE (inner) != NAMESPACE_DECL && !CLASS_TYPE_P (inner)))
3635 return;
3637 prev = CP_DECL_CONTEXT (TREE_CODE (inner) == NAMESPACE_DECL ? inner : TYPE_NAME (inner));
3638 if (outer != prev)
3639 push_inner_scope_r (outer, prev);
3640 if (TREE_CODE (inner) == NAMESPACE_DECL)
3642 cp_binding_level *save_template_parm = 0;
3643 /* Temporary take out template parameter scopes. They are saved
3644 in reversed order in save_template_parm. */
3645 while (current_binding_level->kind == sk_template_parms)
3647 cp_binding_level *b = current_binding_level;
3648 current_binding_level = b->level_chain;
3649 b->level_chain = save_template_parm;
3650 save_template_parm = b;
3653 resume_scope (NAMESPACE_LEVEL (inner));
3654 current_namespace = inner;
3656 /* Restore template parameter scopes. */
3657 while (save_template_parm)
3659 cp_binding_level *b = save_template_parm;
3660 save_template_parm = b->level_chain;
3661 b->level_chain = current_binding_level;
3662 current_binding_level = b;
3665 else
3666 pushclass (inner);
3669 /* Enter the scope INNER from current scope. INNER must be a scope
3670 nested inside current scope. This works with both name lookup and
3671 pushing name into scope. In case a template parameter scope is present,
3672 namespace is pushed under the template parameter scope according to
3673 name lookup rule in 14.6.1/6.
3675 Return the former current scope suitable for pop_inner_scope. */
3677 tree
3678 push_inner_scope (tree inner)
3680 tree outer = current_scope ();
3681 if (!outer)
3682 outer = current_namespace;
3684 push_inner_scope_r (outer, inner);
3685 return outer;
3688 /* Exit the current scope INNER back to scope OUTER. */
3690 void
3691 pop_inner_scope (tree outer, tree inner)
3693 if (outer == inner
3694 || (TREE_CODE (inner) != NAMESPACE_DECL && !CLASS_TYPE_P (inner)))
3695 return;
3697 while (outer != inner)
3699 if (TREE_CODE (inner) == NAMESPACE_DECL)
3701 cp_binding_level *save_template_parm = 0;
3702 /* Temporary take out template parameter scopes. They are saved
3703 in reversed order in save_template_parm. */
3704 while (current_binding_level->kind == sk_template_parms)
3706 cp_binding_level *b = current_binding_level;
3707 current_binding_level = b->level_chain;
3708 b->level_chain = save_template_parm;
3709 save_template_parm = b;
3712 pop_namespace ();
3714 /* Restore template parameter scopes. */
3715 while (save_template_parm)
3717 cp_binding_level *b = save_template_parm;
3718 save_template_parm = b->level_chain;
3719 b->level_chain = current_binding_level;
3720 current_binding_level = b;
3723 else
3724 popclass ();
3726 inner = CP_DECL_CONTEXT (TREE_CODE (inner) == NAMESPACE_DECL ? inner : TYPE_NAME (inner));
3730 /* Do a pushlevel for class declarations. */
3732 void
3733 pushlevel_class (void)
3735 class_binding_level = begin_scope (sk_class, current_class_type);
3738 /* ...and a poplevel for class declarations. */
3740 void
3741 poplevel_class (void)
3743 cp_binding_level *level = class_binding_level;
3744 cp_class_binding *cb;
3745 size_t i;
3746 tree shadowed;
3748 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3749 gcc_assert (level != 0);
3751 /* If we're leaving a toplevel class, cache its binding level. */
3752 if (current_class_depth == 1)
3753 previous_class_level = level;
3754 for (shadowed = level->type_shadowed;
3755 shadowed;
3756 shadowed = TREE_CHAIN (shadowed))
3757 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (shadowed), TREE_VALUE (shadowed));
3759 /* Remove the bindings for all of the class-level declarations. */
3760 if (level->class_shadowed)
3762 FOR_EACH_VEC_ELT (*level->class_shadowed, i, cb)
3764 IDENTIFIER_BINDING (cb->identifier) = cb->base->previous;
3765 cxx_binding_free (cb->base);
3767 ggc_free (level->class_shadowed);
3768 level->class_shadowed = NULL;
3771 /* Now, pop out of the binding level which we created up in the
3772 `pushlevel_class' routine. */
3773 gcc_assert (current_binding_level == level);
3774 leave_scope ();
3775 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3778 /* Set INHERITED_VALUE_BINDING_P on BINDING to true or false, as
3779 appropriate. DECL is the value to which a name has just been
3780 bound. CLASS_TYPE is the class in which the lookup occurred. */
3782 static void
3783 set_inherited_value_binding_p (cxx_binding *binding, tree decl,
3784 tree class_type)
3786 if (binding->value == decl && TREE_CODE (decl) != TREE_LIST)
3788 tree context;
3790 if (TREE_CODE (decl) == OVERLOAD)
3791 context = ovl_scope (decl);
3792 else
3794 gcc_assert (DECL_P (decl));
3795 context = context_for_name_lookup (decl);
3798 if (is_properly_derived_from (class_type, context))
3799 INHERITED_VALUE_BINDING_P (binding) = 1;
3800 else
3801 INHERITED_VALUE_BINDING_P (binding) = 0;
3803 else if (binding->value == decl)
3804 /* We only encounter a TREE_LIST when there is an ambiguity in the
3805 base classes. Such an ambiguity can be overridden by a
3806 definition in this class. */
3807 INHERITED_VALUE_BINDING_P (binding) = 1;
3808 else
3809 INHERITED_VALUE_BINDING_P (binding) = 0;
3812 /* Make the declaration of X appear in CLASS scope. */
3814 bool
3815 pushdecl_class_level (tree x)
3817 bool is_valid = true;
3818 bool subtime;
3820 /* Do nothing if we're adding to an outer lambda closure type,
3821 outer_binding will add it later if it's needed. */
3822 if (current_class_type != class_binding_level->this_entity)
3823 return true;
3825 subtime = timevar_cond_start (TV_NAME_LOOKUP);
3826 /* Get the name of X. */
3827 tree name = OVL_NAME (x);
3829 if (name)
3831 is_valid = push_class_level_binding (name, x);
3832 if (TREE_CODE (x) == TYPE_DECL)
3833 set_identifier_type_value (name, x);
3835 else if (ANON_AGGR_TYPE_P (TREE_TYPE (x)))
3837 /* If X is an anonymous aggregate, all of its members are
3838 treated as if they were members of the class containing the
3839 aggregate, for naming purposes. */
3840 tree f;
3842 for (f = TYPE_FIELDS (TREE_TYPE (x)); f; f = DECL_CHAIN (f))
3844 location_t save_location = input_location;
3845 input_location = DECL_SOURCE_LOCATION (f);
3846 if (!pushdecl_class_level (f))
3847 is_valid = false;
3848 input_location = save_location;
3851 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3852 return is_valid;
3855 /* Return the BINDING (if any) for NAME in SCOPE, which is a class
3856 scope. If the value returned is non-NULL, and the PREVIOUS field
3857 is not set, callers must set the PREVIOUS field explicitly. */
3859 static cxx_binding *
3860 get_class_binding (tree name, cp_binding_level *scope)
3862 tree class_type;
3863 tree type_binding;
3864 tree value_binding;
3865 cxx_binding *binding;
3867 class_type = scope->this_entity;
3869 /* Get the type binding. */
3870 type_binding = lookup_member (class_type, name,
3871 /*protect=*/2, /*want_type=*/true,
3872 tf_warning_or_error);
3873 /* Get the value binding. */
3874 value_binding = lookup_member (class_type, name,
3875 /*protect=*/2, /*want_type=*/false,
3876 tf_warning_or_error);
3878 if (value_binding
3879 && (TREE_CODE (value_binding) == TYPE_DECL
3880 || DECL_CLASS_TEMPLATE_P (value_binding)
3881 || (TREE_CODE (value_binding) == TREE_LIST
3882 && TREE_TYPE (value_binding) == error_mark_node
3883 && (TREE_CODE (TREE_VALUE (value_binding))
3884 == TYPE_DECL))))
3885 /* We found a type binding, even when looking for a non-type
3886 binding. This means that we already processed this binding
3887 above. */
3889 else if (value_binding)
3891 if (TREE_CODE (value_binding) == TREE_LIST
3892 && TREE_TYPE (value_binding) == error_mark_node)
3893 /* NAME is ambiguous. */
3895 else if (BASELINK_P (value_binding))
3896 /* NAME is some overloaded functions. */
3897 value_binding = BASELINK_FUNCTIONS (value_binding);
3900 /* If we found either a type binding or a value binding, create a
3901 new binding object. */
3902 if (type_binding || value_binding)
3904 binding = new_class_binding (name,
3905 value_binding,
3906 type_binding,
3907 scope);
3908 /* This is a class-scope binding, not a block-scope binding. */
3909 LOCAL_BINDING_P (binding) = 0;
3910 set_inherited_value_binding_p (binding, value_binding, class_type);
3912 else
3913 binding = NULL;
3915 return binding;
3918 /* Make the declaration(s) of X appear in CLASS scope under the name
3919 NAME. Returns true if the binding is valid. */
3921 static bool
3922 push_class_level_binding_1 (tree name, tree x)
3924 cxx_binding *binding;
3925 tree decl = x;
3926 bool ok;
3928 /* The class_binding_level will be NULL if x is a template
3929 parameter name in a member template. */
3930 if (!class_binding_level)
3931 return true;
3933 if (name == error_mark_node)
3934 return false;
3936 /* Can happen for an erroneous declaration (c++/60384). */
3937 if (!identifier_p (name))
3939 gcc_assert (errorcount || sorrycount);
3940 return false;
3943 /* Check for invalid member names. But don't worry about a default
3944 argument-scope lambda being pushed after the class is complete. */
3945 gcc_assert (TYPE_BEING_DEFINED (current_class_type)
3946 || LAMBDA_TYPE_P (TREE_TYPE (decl)));
3947 /* Check that we're pushing into the right binding level. */
3948 gcc_assert (current_class_type == class_binding_level->this_entity);
3950 /* We could have been passed a tree list if this is an ambiguous
3951 declaration. If so, pull the declaration out because
3952 check_template_shadow will not handle a TREE_LIST. */
3953 if (TREE_CODE (decl) == TREE_LIST
3954 && TREE_TYPE (decl) == error_mark_node)
3955 decl = TREE_VALUE (decl);
3957 if (!check_template_shadow (decl))
3958 return false;
3960 /* [class.mem]
3962 If T is the name of a class, then each of the following shall
3963 have a name different from T:
3965 -- every static data member of class T;
3967 -- every member of class T that is itself a type;
3969 -- every enumerator of every member of class T that is an
3970 enumerated type;
3972 -- every member of every anonymous union that is a member of
3973 class T.
3975 (Non-static data members were also forbidden to have the same
3976 name as T until TC1.) */
3977 if ((VAR_P (x)
3978 || TREE_CODE (x) == CONST_DECL
3979 || (TREE_CODE (x) == TYPE_DECL
3980 && !DECL_SELF_REFERENCE_P (x))
3981 /* A data member of an anonymous union. */
3982 || (TREE_CODE (x) == FIELD_DECL
3983 && DECL_CONTEXT (x) != current_class_type))
3984 && DECL_NAME (x) == constructor_name (current_class_type))
3986 tree scope = context_for_name_lookup (x);
3987 if (TYPE_P (scope) && same_type_p (scope, current_class_type))
3989 error ("%qD has the same name as the class in which it is "
3990 "declared",
3992 return false;
3996 /* Get the current binding for NAME in this class, if any. */
3997 binding = IDENTIFIER_BINDING (name);
3998 if (!binding || binding->scope != class_binding_level)
4000 binding = get_class_binding (name, class_binding_level);
4001 /* If a new binding was created, put it at the front of the
4002 IDENTIFIER_BINDING list. */
4003 if (binding)
4005 binding->previous = IDENTIFIER_BINDING (name);
4006 IDENTIFIER_BINDING (name) = binding;
4010 /* If there is already a binding, then we may need to update the
4011 current value. */
4012 if (binding && binding->value)
4014 tree bval = binding->value;
4015 tree old_decl = NULL_TREE;
4016 tree target_decl = strip_using_decl (decl);
4017 tree target_bval = strip_using_decl (bval);
4019 if (INHERITED_VALUE_BINDING_P (binding))
4021 /* If the old binding was from a base class, and was for a
4022 tag name, slide it over to make room for the new binding.
4023 The old binding is still visible if explicitly qualified
4024 with a class-key. */
4025 if (TREE_CODE (target_bval) == TYPE_DECL
4026 && DECL_ARTIFICIAL (target_bval)
4027 && !(TREE_CODE (target_decl) == TYPE_DECL
4028 && DECL_ARTIFICIAL (target_decl)))
4030 old_decl = binding->type;
4031 binding->type = bval;
4032 binding->value = NULL_TREE;
4033 INHERITED_VALUE_BINDING_P (binding) = 0;
4035 else
4037 old_decl = bval;
4038 /* Any inherited type declaration is hidden by the type
4039 declaration in the derived class. */
4040 if (TREE_CODE (target_decl) == TYPE_DECL
4041 && DECL_ARTIFICIAL (target_decl))
4042 binding->type = NULL_TREE;
4045 else if (TREE_CODE (target_decl) == OVERLOAD
4046 && OVL_P (target_bval))
4047 old_decl = bval;
4048 else if (TREE_CODE (decl) == USING_DECL
4049 && TREE_CODE (bval) == USING_DECL
4050 && same_type_p (USING_DECL_SCOPE (decl),
4051 USING_DECL_SCOPE (bval)))
4052 /* This is a using redeclaration that will be diagnosed later
4053 in supplement_binding */
4055 else if (TREE_CODE (decl) == USING_DECL
4056 && TREE_CODE (bval) == USING_DECL
4057 && DECL_DEPENDENT_P (decl)
4058 && DECL_DEPENDENT_P (bval))
4059 return true;
4060 else if (TREE_CODE (decl) == USING_DECL
4061 && OVL_P (target_bval))
4062 old_decl = bval;
4063 else if (TREE_CODE (bval) == USING_DECL
4064 && OVL_P (target_decl))
4065 return true;
4067 if (old_decl && binding->scope == class_binding_level)
4069 binding->value = x;
4070 /* It is always safe to clear INHERITED_VALUE_BINDING_P
4071 here. This function is only used to register bindings
4072 from with the class definition itself. */
4073 INHERITED_VALUE_BINDING_P (binding) = 0;
4074 return true;
4078 /* Note that we declared this value so that we can issue an error if
4079 this is an invalid redeclaration of a name already used for some
4080 other purpose. */
4081 note_name_declared_in_class (name, decl);
4083 /* If we didn't replace an existing binding, put the binding on the
4084 stack of bindings for the identifier, and update the shadowed
4085 list. */
4086 if (binding && binding->scope == class_binding_level)
4087 /* Supplement the existing binding. */
4088 ok = supplement_binding (binding, decl);
4089 else
4091 /* Create a new binding. */
4092 push_binding (name, decl, class_binding_level);
4093 ok = true;
4096 return ok;
4099 /* Wrapper for push_class_level_binding_1. */
4101 bool
4102 push_class_level_binding (tree name, tree x)
4104 bool ret;
4105 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4106 ret = push_class_level_binding_1 (name, x);
4107 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4108 return ret;
4111 /* Process "using SCOPE::NAME" in a class scope. Return the
4112 USING_DECL created. */
4114 tree
4115 do_class_using_decl (tree scope, tree name)
4117 /* The USING_DECL returned by this function. */
4118 tree value;
4119 /* The declaration (or declarations) name by this using
4120 declaration. NULL if we are in a template and cannot figure out
4121 what has been named. */
4122 tree decl;
4123 /* True if SCOPE is a dependent type. */
4124 bool scope_dependent_p;
4125 /* True if SCOPE::NAME is dependent. */
4126 bool name_dependent_p;
4127 /* True if any of the bases of CURRENT_CLASS_TYPE are dependent. */
4128 bool bases_dependent_p;
4129 tree binfo;
4131 if (name == error_mark_node)
4132 return NULL_TREE;
4134 if (!scope || !TYPE_P (scope))
4136 error ("using-declaration for non-member at class scope");
4137 return NULL_TREE;
4140 /* Make sure the name is not invalid */
4141 if (TREE_CODE (name) == BIT_NOT_EXPR)
4143 error ("%<%T::%D%> names destructor", scope, name);
4144 return NULL_TREE;
4146 /* Using T::T declares inheriting ctors, even if T is a typedef. */
4147 if (MAYBE_CLASS_TYPE_P (scope)
4148 && (name == TYPE_IDENTIFIER (scope)
4149 || constructor_name_p (name, scope)))
4151 maybe_warn_cpp0x (CPP0X_INHERITING_CTORS);
4152 name = ctor_identifier;
4153 CLASSTYPE_NON_AGGREGATE (current_class_type) = true;
4155 if (constructor_name_p (name, current_class_type))
4157 error ("%<%T::%D%> names constructor in %qT",
4158 scope, name, current_class_type);
4159 return NULL_TREE;
4162 scope_dependent_p = dependent_scope_p (scope);
4163 name_dependent_p = (scope_dependent_p
4164 || (IDENTIFIER_CONV_OP_P (name)
4165 && dependent_type_p (TREE_TYPE (name))));
4167 bases_dependent_p = any_dependent_bases_p ();
4169 decl = NULL_TREE;
4171 /* From [namespace.udecl]:
4173 A using-declaration used as a member-declaration shall refer to a
4174 member of a base class of the class being defined.
4176 In general, we cannot check this constraint in a template because
4177 we do not know the entire set of base classes of the current
4178 class type. Morover, if SCOPE is dependent, it might match a
4179 non-dependent base. */
4181 if (!scope_dependent_p)
4183 base_kind b_kind;
4184 binfo = lookup_base (current_class_type, scope, ba_any, &b_kind,
4185 tf_warning_or_error);
4186 if (b_kind < bk_proper_base)
4188 if (!bases_dependent_p || b_kind == bk_same_type)
4190 error_not_base_type (scope, current_class_type);
4191 return NULL_TREE;
4194 else if (name == ctor_identifier
4195 && BINFO_INHERITANCE_CHAIN (BINFO_INHERITANCE_CHAIN (binfo)))
4197 error ("cannot inherit constructors from indirect base %qT", scope);
4198 return NULL_TREE;
4200 else if (!name_dependent_p)
4202 decl = lookup_member (binfo, name, 0, false, tf_warning_or_error);
4203 if (!decl)
4205 error ("no members matching %<%T::%D%> in %q#T", scope, name,
4206 scope);
4207 return NULL_TREE;
4209 /* The binfo from which the functions came does not matter. */
4210 if (BASELINK_P (decl))
4211 decl = BASELINK_FUNCTIONS (decl);
4215 value = build_lang_decl (USING_DECL, name, NULL_TREE);
4216 USING_DECL_DECLS (value) = decl;
4217 USING_DECL_SCOPE (value) = scope;
4218 DECL_DEPENDENT_P (value) = !decl;
4220 return value;
4224 /* Return the binding for NAME in NS. If NS is NULL, look in
4225 global_namespace. */
4227 tree
4228 get_namespace_binding (tree ns, tree name)
4230 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4231 if (!ns)
4232 ns = global_namespace;
4233 gcc_checking_assert (!DECL_NAMESPACE_ALIAS (ns));
4234 tree ret = find_namespace_value (ns, name);
4235 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4236 return ret;
4239 /* Set value binding of NAME in the global namespace to VAL. Does not
4240 add it to the list of things in the namespace. */
4242 void
4243 set_global_binding (tree name, tree val)
4245 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4247 tree *slot = find_namespace_slot (global_namespace, name, true);
4248 tree old = MAYBE_STAT_DECL (*slot);
4250 if (!old)
4251 *slot = val;
4252 else if (old == val)
4254 else if (!STAT_HACK_P (*slot)
4255 && TREE_CODE (val) == TYPE_DECL && DECL_ARTIFICIAL (val))
4256 *slot = stat_hack (old, val);
4257 else if (!STAT_HACK_P (*slot)
4258 && TREE_CODE (old) == TYPE_DECL && DECL_ARTIFICIAL (old))
4259 *slot = stat_hack (val, old);
4260 else
4261 /* The user's placed something in the implementor's
4262 namespace. */
4263 diagnose_name_conflict (val, old);
4265 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4268 /* Set the context of a declaration to scope. Complain if we are not
4269 outside scope. */
4271 void
4272 set_decl_namespace (tree decl, tree scope, bool friendp)
4274 /* Get rid of namespace aliases. */
4275 scope = ORIGINAL_NAMESPACE (scope);
4277 /* It is ok for friends to be qualified in parallel space. */
4278 if (!friendp && !is_nested_namespace (current_namespace, scope))
4279 error ("declaration of %qD not in a namespace surrounding %qD",
4280 decl, scope);
4281 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
4283 /* See whether this has been declared in the namespace or inline
4284 children. */
4285 tree old = NULL_TREE;
4287 name_lookup lookup (DECL_NAME (decl), LOOKUP_HIDDEN);
4288 if (!lookup.search_qualified (scope, /*usings=*/false))
4289 /* No old declaration at all. */
4290 goto not_found;
4291 old = lookup.value;
4294 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
4295 if (TREE_CODE (old) == TREE_LIST)
4297 ambiguous:
4298 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
4299 error ("reference to %qD is ambiguous", decl);
4300 print_candidates (old);
4301 return;
4304 if (!DECL_DECLARES_FUNCTION_P (decl))
4306 /* Don't compare non-function decls with decls_match here, since
4307 it can't check for the correct constness at this
4308 point. pushdecl will find those errors later. */
4310 /* We might have found it in an inline namespace child of SCOPE. */
4311 if (TREE_CODE (decl) == TREE_CODE (old))
4312 DECL_CONTEXT (decl) = DECL_CONTEXT (old);
4314 found:
4315 /* Writing "N::i" to declare something directly in "N" is invalid. */
4316 if (CP_DECL_CONTEXT (decl) == current_namespace
4317 && at_namespace_scope_p ())
4318 error ("explicit qualification in declaration of %qD", decl);
4319 return;
4322 /* Since decl is a function, old should contain a function decl. */
4323 if (!OVL_P (old))
4324 goto not_found;
4326 /* We handle these in check_explicit_instantiation_namespace. */
4327 if (processing_explicit_instantiation)
4328 return;
4329 if (processing_template_decl || processing_specialization)
4330 /* We have not yet called push_template_decl to turn a
4331 FUNCTION_DECL into a TEMPLATE_DECL, so the declarations won't
4332 match. But, we'll check later, when we construct the
4333 template. */
4334 return;
4335 /* Instantiations or specializations of templates may be declared as
4336 friends in any namespace. */
4337 if (friendp && DECL_USE_TEMPLATE (decl))
4338 return;
4340 tree found;
4341 found = NULL_TREE;
4343 for (lkp_iterator iter (old); iter; ++iter)
4345 if (iter.using_p ())
4346 continue;
4348 tree ofn = *iter;
4350 /* Adjust DECL_CONTEXT first so decls_match will return true
4351 if DECL will match a declaration in an inline namespace. */
4352 DECL_CONTEXT (decl) = DECL_CONTEXT (ofn);
4353 if (decls_match (decl, ofn))
4355 if (found)
4357 /* We found more than one matching declaration. */
4358 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
4359 goto ambiguous;
4361 found = ofn;
4365 if (found)
4367 if (DECL_HIDDEN_FRIEND_P (found))
4369 pedwarn (DECL_SOURCE_LOCATION (decl), 0,
4370 "%qD has not been declared within %qD", decl, scope);
4371 inform (DECL_SOURCE_LOCATION (found),
4372 "only here as a %<friend%>");
4374 DECL_CONTEXT (decl) = DECL_CONTEXT (found);
4375 goto found;
4378 not_found:
4379 /* It didn't work, go back to the explicit scope. */
4380 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
4381 error ("%qD should have been declared inside %qD", decl, scope);
4384 /* Return the namespace where the current declaration is declared. */
4386 tree
4387 current_decl_namespace (void)
4389 tree result;
4390 /* If we have been pushed into a different namespace, use it. */
4391 if (!vec_safe_is_empty (decl_namespace_list))
4392 return decl_namespace_list->last ();
4394 if (current_class_type)
4395 result = decl_namespace_context (current_class_type);
4396 else if (current_function_decl)
4397 result = decl_namespace_context (current_function_decl);
4398 else
4399 result = current_namespace;
4400 return result;
4403 /* Process any ATTRIBUTES on a namespace definition. Returns true if
4404 attribute visibility is seen. */
4406 bool
4407 handle_namespace_attrs (tree ns, tree attributes)
4409 tree d;
4410 bool saw_vis = false;
4412 for (d = attributes; d; d = TREE_CHAIN (d))
4414 tree name = get_attribute_name (d);
4415 tree args = TREE_VALUE (d);
4417 if (is_attribute_p ("visibility", name))
4419 /* attribute visibility is a property of the syntactic block
4420 rather than the namespace as a whole, so we don't touch the
4421 NAMESPACE_DECL at all. */
4422 tree x = args ? TREE_VALUE (args) : NULL_TREE;
4423 if (x == NULL_TREE || TREE_CODE (x) != STRING_CST || TREE_CHAIN (args))
4425 warning (OPT_Wattributes,
4426 "%qD attribute requires a single NTBS argument",
4427 name);
4428 continue;
4431 if (!TREE_PUBLIC (ns))
4432 warning (OPT_Wattributes,
4433 "%qD attribute is meaningless since members of the "
4434 "anonymous namespace get local symbols", name);
4436 push_visibility (TREE_STRING_POINTER (x), 1);
4437 saw_vis = true;
4439 else if (is_attribute_p ("abi_tag", name))
4441 if (!DECL_NAME (ns))
4443 warning (OPT_Wattributes, "ignoring %qD attribute on anonymous "
4444 "namespace", name);
4445 continue;
4447 if (!DECL_NAMESPACE_INLINE_P (ns))
4449 warning (OPT_Wattributes, "ignoring %qD attribute on non-inline "
4450 "namespace", name);
4451 continue;
4453 if (!args)
4455 tree dn = DECL_NAME (ns);
4456 args = build_string (IDENTIFIER_LENGTH (dn) + 1,
4457 IDENTIFIER_POINTER (dn));
4458 TREE_TYPE (args) = char_array_type_node;
4459 args = fix_string_type (args);
4460 args = build_tree_list (NULL_TREE, args);
4462 if (check_abi_tag_args (args, name))
4463 DECL_ATTRIBUTES (ns) = tree_cons (name, args,
4464 DECL_ATTRIBUTES (ns));
4466 else
4468 warning (OPT_Wattributes, "%qD attribute directive ignored",
4469 name);
4470 continue;
4474 return saw_vis;
4477 /* Temporarily set the namespace for the current declaration. */
4479 void
4480 push_decl_namespace (tree decl)
4482 if (TREE_CODE (decl) != NAMESPACE_DECL)
4483 decl = decl_namespace_context (decl);
4484 vec_safe_push (decl_namespace_list, ORIGINAL_NAMESPACE (decl));
4487 /* [namespace.memdef]/2 */
4489 void
4490 pop_decl_namespace (void)
4492 decl_namespace_list->pop ();
4495 /* Process a namespace-alias declaration. */
4497 void
4498 do_namespace_alias (tree alias, tree name_space)
4500 if (name_space == error_mark_node)
4501 return;
4503 gcc_assert (TREE_CODE (name_space) == NAMESPACE_DECL);
4505 name_space = ORIGINAL_NAMESPACE (name_space);
4507 /* Build the alias. */
4508 alias = build_lang_decl (NAMESPACE_DECL, alias, void_type_node);
4509 DECL_NAMESPACE_ALIAS (alias) = name_space;
4510 DECL_EXTERNAL (alias) = 1;
4511 DECL_CONTEXT (alias) = FROB_CONTEXT (current_scope ());
4512 pushdecl (alias);
4514 /* Emit debug info for namespace alias. */
4515 if (!building_stmt_list_p ())
4516 (*debug_hooks->early_global_decl) (alias);
4519 /* Like pushdecl, only it places X in the current namespace,
4520 if appropriate. */
4522 tree
4523 pushdecl_namespace_level (tree x, bool is_friend)
4525 cp_binding_level *b = current_binding_level;
4526 tree t;
4528 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4529 t = do_pushdecl_with_scope
4530 (x, NAMESPACE_LEVEL (current_namespace), is_friend);
4532 /* Now, the type_shadowed stack may screw us. Munge it so it does
4533 what we want. */
4534 if (TREE_CODE (t) == TYPE_DECL)
4536 tree name = DECL_NAME (t);
4537 tree newval;
4538 tree *ptr = (tree *)0;
4539 for (; !global_scope_p (b); b = b->level_chain)
4541 tree shadowed = b->type_shadowed;
4542 for (; shadowed; shadowed = TREE_CHAIN (shadowed))
4543 if (TREE_PURPOSE (shadowed) == name)
4545 ptr = &TREE_VALUE (shadowed);
4546 /* Can't break out of the loop here because sometimes
4547 a binding level will have duplicate bindings for
4548 PT names. It's gross, but I haven't time to fix it. */
4551 newval = TREE_TYPE (t);
4552 if (ptr == (tree *)0)
4554 /* @@ This shouldn't be needed. My test case "zstring.cc" trips
4555 up here if this is changed to an assertion. --KR */
4556 SET_IDENTIFIER_TYPE_VALUE (name, t);
4558 else
4560 *ptr = newval;
4563 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4564 return t;
4567 /* Process a using-declaration appearing in namespace scope. */
4569 void
4570 finish_namespace_using_decl (tree decl, tree scope, tree name)
4572 tree orig_decl = decl;
4574 gcc_checking_assert (current_binding_level->kind == sk_namespace
4575 && !processing_template_decl);
4576 decl = validate_nonmember_using_decl (decl, scope, name);
4577 if (decl == NULL_TREE)
4578 return;
4580 tree *slot = find_namespace_slot (current_namespace, name, true);
4581 tree val = slot ? MAYBE_STAT_DECL (*slot) : NULL_TREE;
4582 tree type = slot ? MAYBE_STAT_TYPE (*slot) : NULL_TREE;
4583 do_nonmember_using_decl (scope, name, &val, &type);
4584 if (STAT_HACK_P (*slot))
4586 STAT_DECL (*slot) = val;
4587 STAT_TYPE (*slot) = type;
4589 else if (type)
4590 *slot = stat_hack (val, type);
4591 else
4592 *slot = val;
4594 /* Emit debug info. */
4595 cp_emit_debug_info_for_using (orig_decl, current_namespace);
4598 /* Process a using-declaration at function scope. */
4600 void
4601 finish_local_using_decl (tree decl, tree scope, tree name)
4603 tree orig_decl = decl;
4605 gcc_checking_assert (current_binding_level->kind != sk_class
4606 && current_binding_level->kind != sk_namespace);
4607 decl = validate_nonmember_using_decl (decl, scope, name);
4608 if (decl == NULL_TREE)
4609 return;
4611 add_decl_expr (decl);
4613 cxx_binding *binding = find_local_binding (current_binding_level, name);
4614 tree value = binding ? binding->value : NULL_TREE;
4615 tree type = binding ? binding->type : NULL_TREE;
4617 do_nonmember_using_decl (scope, name, &value, &type);
4619 if (!value)
4621 else if (binding && value == binding->value)
4623 else if (binding && binding->value && TREE_CODE (value) == OVERLOAD)
4625 update_local_overload (IDENTIFIER_BINDING (name), value);
4626 IDENTIFIER_BINDING (name)->value = value;
4628 else
4629 /* Install the new binding. */
4630 push_local_binding (name, value, true);
4632 if (!type)
4634 else if (binding && type == binding->type)
4636 else
4638 push_local_binding (name, type, true);
4639 set_identifier_type_value (name, type);
4642 /* Emit debug info. */
4643 if (!processing_template_decl)
4644 cp_emit_debug_info_for_using (orig_decl, current_scope ());
4647 /* Return the declarations that are members of the namespace NS. */
4649 tree
4650 cp_namespace_decls (tree ns)
4652 return NAMESPACE_LEVEL (ns)->names;
4655 /* Combine prefer_type and namespaces_only into flags. */
4657 static int
4658 lookup_flags (int prefer_type, int namespaces_only)
4660 if (namespaces_only)
4661 return LOOKUP_PREFER_NAMESPACES;
4662 if (prefer_type > 1)
4663 return LOOKUP_PREFER_TYPES;
4664 if (prefer_type > 0)
4665 return LOOKUP_PREFER_BOTH;
4666 return 0;
4669 /* Given a lookup that returned VAL, use FLAGS to decide if we want to
4670 ignore it or not. Subroutine of lookup_name_real and
4671 lookup_type_scope. */
4673 static bool
4674 qualify_lookup (tree val, int flags)
4676 if (val == NULL_TREE)
4677 return false;
4678 if ((flags & LOOKUP_PREFER_NAMESPACES) && TREE_CODE (val) == NAMESPACE_DECL)
4679 return true;
4680 if (flags & LOOKUP_PREFER_TYPES)
4682 tree target_val = strip_using_decl (val);
4683 if (TREE_CODE (target_val) == TYPE_DECL
4684 || TREE_CODE (target_val) == TEMPLATE_DECL)
4685 return true;
4687 if (flags & (LOOKUP_PREFER_NAMESPACES | LOOKUP_PREFER_TYPES))
4688 return false;
4689 /* Look through lambda things that we shouldn't be able to see. */
4690 if (is_lambda_ignored_entity (val))
4691 return false;
4692 return true;
4695 /* Suggest alternatives for NAME, an IDENTIFIER_NODE for which name
4696 lookup failed. Search through all available namespaces and print out
4697 possible candidates. If no exact matches are found, and
4698 SUGGEST_MISSPELLINGS is true, then also look for near-matches and
4699 suggest the best near-match, if there is one. */
4701 void
4702 suggest_alternatives_for (location_t location, tree name,
4703 bool suggest_misspellings)
4705 vec<tree> candidates = vNULL;
4706 vec<tree> worklist = vNULL;
4707 unsigned limit = PARAM_VALUE (CXX_MAX_NAMESPACES_FOR_DIAGNOSTIC_HELP);
4708 bool limited = false;
4710 /* Breadth-first search of namespaces. Up to limit namespaces
4711 searched (limit zero == unlimited). */
4712 worklist.safe_push (global_namespace);
4713 for (unsigned ix = 0; ix != worklist.length (); ix++)
4715 tree ns = worklist[ix];
4716 name_lookup lookup (name);
4718 if (lookup.search_qualified (ns, false))
4719 candidates.safe_push (lookup.value);
4721 if (!limited)
4723 /* Look for child namespaces. We have to do this
4724 indirectly because they are chained in reverse order,
4725 which is confusing to the user. */
4726 vec<tree> children = vNULL;
4728 for (tree decl = NAMESPACE_LEVEL (ns)->names;
4729 decl; decl = TREE_CHAIN (decl))
4730 if (TREE_CODE (decl) == NAMESPACE_DECL
4731 && !DECL_NAMESPACE_ALIAS (decl)
4732 && !DECL_NAMESPACE_INLINE_P (decl))
4733 children.safe_push (decl);
4735 while (!limited && !children.is_empty ())
4737 if (worklist.length () == limit)
4739 /* Unconditionally warn that the search was truncated. */
4740 inform (location,
4741 "maximum limit of %d namespaces searched for %qE",
4742 limit, name);
4743 limited = true;
4745 else
4746 worklist.safe_push (children.pop ());
4748 children.release ();
4751 worklist.release ();
4753 if (candidates.length ())
4755 inform_n (location, candidates.length (),
4756 "suggested alternative:",
4757 "suggested alternatives:");
4758 for (unsigned ix = 0; ix != candidates.length (); ix++)
4760 tree val = candidates[ix];
4762 inform (location_of (val), " %qE", val);
4764 candidates.release ();
4766 else if (!suggest_misspellings)
4768 else if (const char *fuzzy = lookup_name_fuzzy (name, FUZZY_LOOKUP_NAME))
4770 /* Show a spelling correction. */
4771 gcc_rich_location richloc (location);
4773 richloc.add_fixit_replace (fuzzy);
4774 inform_at_rich_loc (&richloc, "suggested alternative: %qs", fuzzy);
4778 /* Subroutine of maybe_suggest_missing_header for handling unrecognized names
4779 for some of the most common names within "std::".
4780 Given non-NULL NAME, a name for lookup within "std::", return the header
4781 name defining it within the C++ Standard Library (without '<' and '>'),
4782 or NULL. */
4784 static const char *
4785 get_std_name_hint (const char *name)
4787 struct std_name_hint
4789 const char *name;
4790 const char *header;
4792 static const std_name_hint hints[] = {
4793 /* <array>. */
4794 {"array", "array"}, // C++11
4795 /* <deque>. */
4796 {"deque", "deque"},
4797 /* <forward_list>. */
4798 {"forward_list", "forward_list"}, // C++11
4799 /* <fstream>. */
4800 {"basic_filebuf", "fstream"},
4801 {"basic_ifstream", "fstream"},
4802 {"basic_ofstream", "fstream"},
4803 {"basic_fstream", "fstream"},
4804 /* <iostream>. */
4805 {"cin", "iostream"},
4806 {"cout", "iostream"},
4807 {"cerr", "iostream"},
4808 {"clog", "iostream"},
4809 {"wcin", "iostream"},
4810 {"wcout", "iostream"},
4811 {"wclog", "iostream"},
4812 /* <list>. */
4813 {"list", "list"},
4814 /* <map>. */
4815 {"map", "map"},
4816 {"multimap", "map"},
4817 /* <queue>. */
4818 {"queue", "queue"},
4819 {"priority_queue", "queue"},
4820 /* <ostream>. */
4821 {"ostream", "ostream"},
4822 {"wostream", "ostream"},
4823 {"ends", "ostream"},
4824 {"flush", "ostream"},
4825 {"endl", "ostream"},
4826 /* <set>. */
4827 {"set", "set"},
4828 {"multiset", "set"},
4829 /* <sstream>. */
4830 {"basic_stringbuf", "sstream"},
4831 {"basic_istringstream", "sstream"},
4832 {"basic_ostringstream", "sstream"},
4833 {"basic_stringstream", "sstream"},
4834 /* <stack>. */
4835 {"stack", "stack"},
4836 /* <string>. */
4837 {"string", "string"},
4838 {"wstring", "string"},
4839 {"u16string", "string"},
4840 {"u32string", "string"},
4841 /* <unordered_map>. */
4842 {"unordered_map", "unordered_map"}, // C++11
4843 {"unordered_multimap", "unordered_map"}, // C++11
4844 /* <unordered_set>. */
4845 {"unordered_set", "unordered_set"}, // C++11
4846 {"unordered_multiset", "unordered_set"}, // C++11
4847 /* <vector>. */
4848 {"vector", "vector"},
4850 const size_t num_hints = sizeof (hints) / sizeof (hints[0]);
4851 for (size_t i = 0; i < num_hints; i++)
4853 if (0 == strcmp (name, hints[i].name))
4854 return hints[i].header;
4856 return NULL;
4859 /* Subroutine of suggest_alternative_in_explicit_scope, for use when we have no
4860 suggestions to offer.
4861 If SCOPE is the "std" namespace, then suggest pertinent header
4862 files for NAME. */
4864 static void
4865 maybe_suggest_missing_header (location_t location, tree name, tree scope)
4867 if (scope == NULL_TREE)
4868 return;
4869 if (TREE_CODE (scope) != NAMESPACE_DECL)
4870 return;
4871 /* We only offer suggestions for the "std" namespace. */
4872 if (scope != std_node)
4873 return;
4874 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
4876 const char *name_str = IDENTIFIER_POINTER (name);
4877 const char *header_hint = get_std_name_hint (name_str);
4878 if (header_hint)
4879 inform (location,
4880 "%<std::%s%> is defined in header %<<%s>%>;"
4881 " did you forget to %<#include <%s>%>?",
4882 name_str, header_hint, header_hint);
4885 /* Look for alternatives for NAME, an IDENTIFIER_NODE for which name
4886 lookup failed within the explicitly provided SCOPE. Suggest the
4887 the best meaningful candidates (if any) as a fix-it hint.
4888 Return true iff a suggestion was provided. */
4890 bool
4891 suggest_alternative_in_explicit_scope (location_t location, tree name,
4892 tree scope)
4894 /* Resolve any namespace aliases. */
4895 scope = ORIGINAL_NAMESPACE (scope);
4897 cp_binding_level *level = NAMESPACE_LEVEL (scope);
4899 best_match <tree, const char *> bm (name);
4900 consider_binding_level (name, bm, level, false, FUZZY_LOOKUP_NAME);
4902 /* See if we have a good suggesion for the user. */
4903 const char *fuzzy_name = bm.get_best_meaningful_candidate ();
4904 if (fuzzy_name)
4906 gcc_rich_location richloc (location);
4907 richloc.add_fixit_replace (fuzzy_name);
4908 inform_at_rich_loc (&richloc, "suggested alternative: %qs",
4909 fuzzy_name);
4910 return true;
4912 else
4913 maybe_suggest_missing_header (location, name, scope);
4915 return false;
4918 /* Look up NAME (an IDENTIFIER_NODE) in SCOPE (either a NAMESPACE_DECL
4919 or a class TYPE).
4921 If PREFER_TYPE is > 0, we only return TYPE_DECLs or namespaces.
4922 If PREFER_TYPE is > 1, we only return TYPE_DECLs.
4924 Returns a DECL (or OVERLOAD, or BASELINK) representing the
4925 declaration found. If no suitable declaration can be found,
4926 ERROR_MARK_NODE is returned. If COMPLAIN is true and SCOPE is
4927 neither a class-type nor a namespace a diagnostic is issued. */
4929 tree
4930 lookup_qualified_name (tree scope, tree name, int prefer_type, bool complain,
4931 bool find_hidden)
4933 tree t = NULL_TREE;
4935 if (TREE_CODE (scope) == NAMESPACE_DECL)
4937 int flags = lookup_flags (prefer_type, /*namespaces_only*/false);
4938 if (find_hidden)
4939 flags |= LOOKUP_HIDDEN;
4940 name_lookup lookup (name, flags);
4942 if (qualified_namespace_lookup (scope, &lookup))
4943 t = lookup.value;
4945 else if (cxx_dialect != cxx98 && TREE_CODE (scope) == ENUMERAL_TYPE)
4946 t = lookup_enumerator (scope, name);
4947 else if (is_class_type (scope, complain))
4948 t = lookup_member (scope, name, 2, prefer_type, tf_warning_or_error);
4950 if (!t)
4951 return error_mark_node;
4952 return t;
4955 /* [namespace.qual]
4956 Accepts the NAME to lookup and its qualifying SCOPE.
4957 Returns the name/type pair found into the cxx_binding *RESULT,
4958 or false on error. */
4960 static bool
4961 qualified_namespace_lookup (tree scope, name_lookup *lookup)
4963 timevar_start (TV_NAME_LOOKUP);
4964 query_oracle (lookup->name);
4965 bool found = lookup->search_qualified (ORIGINAL_NAMESPACE (scope));
4966 timevar_stop (TV_NAME_LOOKUP);
4967 return found;
4970 /* Helper function for lookup_name_fuzzy.
4971 Traverse binding level LVL, looking for good name matches for NAME
4972 (and BM). */
4973 static void
4974 consider_binding_level (tree name, best_match <tree, const char *> &bm,
4975 cp_binding_level *lvl, bool look_within_fields,
4976 enum lookup_name_fuzzy_kind kind)
4978 if (look_within_fields)
4979 if (lvl->this_entity && TREE_CODE (lvl->this_entity) == RECORD_TYPE)
4981 tree type = lvl->this_entity;
4982 bool want_type_p = (kind == FUZZY_LOOKUP_TYPENAME);
4983 tree best_matching_field
4984 = lookup_member_fuzzy (type, name, want_type_p);
4985 if (best_matching_field)
4986 bm.consider (IDENTIFIER_POINTER (best_matching_field));
4989 for (tree t = lvl->names; t; t = TREE_CHAIN (t))
4991 tree d = t;
4993 /* OVERLOADs or decls from using declaration are wrapped into
4994 TREE_LIST. */
4995 if (TREE_CODE (d) == TREE_LIST)
4996 d = OVL_FIRST (TREE_VALUE (d));
4998 /* Don't use bindings from implicitly declared functions,
4999 as they were likely misspellings themselves. */
5000 if (TREE_TYPE (d) == error_mark_node)
5001 continue;
5003 /* Skip anticipated decls of builtin functions. */
5004 if (TREE_CODE (d) == FUNCTION_DECL
5005 && DECL_BUILT_IN (d)
5006 && DECL_ANTICIPATED (d))
5007 continue;
5009 if (tree name = DECL_NAME (d))
5010 /* Ignore internal names with spaces in them. */
5011 if (!strchr (IDENTIFIER_POINTER (name), ' '))
5012 bm.consider (IDENTIFIER_POINTER (name));
5016 /* Search for near-matches for NAME within the current bindings, and within
5017 macro names, returning the best match as a const char *, or NULL if
5018 no reasonable match is found. */
5020 const char *
5021 lookup_name_fuzzy (tree name, enum lookup_name_fuzzy_kind kind)
5023 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
5025 best_match <tree, const char *> bm (name);
5027 cp_binding_level *lvl;
5028 for (lvl = scope_chain->class_bindings; lvl; lvl = lvl->level_chain)
5029 consider_binding_level (name, bm, lvl, true, kind);
5031 for (lvl = current_binding_level; lvl; lvl = lvl->level_chain)
5032 consider_binding_level (name, bm, lvl, false, kind);
5034 /* Consider macros: if the user misspelled a macro name e.g. "SOME_MACRO"
5036 x = SOME_OTHER_MACRO (y);
5037 then "SOME_OTHER_MACRO" will survive to the frontend and show up
5038 as a misspelled identifier.
5040 Use the best distance so far so that a candidate is only set if
5041 a macro is better than anything so far. This allows early rejection
5042 (without calculating the edit distance) of macro names that must have
5043 distance >= bm.get_best_distance (), and means that we only get a
5044 non-NULL result for best_macro_match if it's better than any of
5045 the identifiers already checked. */
5046 best_macro_match bmm (name, bm.get_best_distance (), parse_in);
5047 cpp_hashnode *best_macro = bmm.get_best_meaningful_candidate ();
5048 /* If a macro is the closest so far to NAME, consider it. */
5049 if (best_macro)
5050 bm.consider ((const char *)best_macro->ident.str);
5052 /* Try the "starts_decl_specifier_p" keywords to detect
5053 "singed" vs "signed" typos. */
5054 for (unsigned i = 0; i < num_c_common_reswords; i++)
5056 const c_common_resword *resword = &c_common_reswords[i];
5058 if (kind == FUZZY_LOOKUP_TYPENAME)
5059 if (!cp_keyword_starts_decl_specifier_p (resword->rid))
5060 continue;
5062 tree resword_identifier = ridpointers [resword->rid];
5063 if (!resword_identifier)
5064 continue;
5065 gcc_assert (TREE_CODE (resword_identifier) == IDENTIFIER_NODE);
5067 /* Only consider reserved words that survived the
5068 filtering in init_reswords (e.g. for -std). */
5069 if (!IDENTIFIER_KEYWORD_P (resword_identifier))
5070 continue;
5072 bm.consider (IDENTIFIER_POINTER (resword_identifier));
5075 return bm.get_best_meaningful_candidate ();
5078 /* Subroutine of outer_binding.
5080 Returns TRUE if BINDING is a binding to a template parameter of
5081 SCOPE. In that case SCOPE is the scope of a primary template
5082 parameter -- in the sense of G++, i.e, a template that has its own
5083 template header.
5085 Returns FALSE otherwise. */
5087 static bool
5088 binding_to_template_parms_of_scope_p (cxx_binding *binding,
5089 cp_binding_level *scope)
5091 tree binding_value, tmpl, tinfo;
5092 int level;
5094 if (!binding || !scope || !scope->this_entity)
5095 return false;
5097 binding_value = binding->value ? binding->value : binding->type;
5098 tinfo = get_template_info (scope->this_entity);
5100 /* BINDING_VALUE must be a template parm. */
5101 if (binding_value == NULL_TREE
5102 || (!DECL_P (binding_value)
5103 || !DECL_TEMPLATE_PARM_P (binding_value)))
5104 return false;
5106 /* The level of BINDING_VALUE. */
5107 level =
5108 template_type_parameter_p (binding_value)
5109 ? TEMPLATE_PARM_LEVEL (TEMPLATE_TYPE_PARM_INDEX
5110 (TREE_TYPE (binding_value)))
5111 : TEMPLATE_PARM_LEVEL (DECL_INITIAL (binding_value));
5113 /* The template of the current scope, iff said scope is a primary
5114 template. */
5115 tmpl = (tinfo
5116 && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
5117 ? TI_TEMPLATE (tinfo)
5118 : NULL_TREE);
5120 /* If the level of the parm BINDING_VALUE equals the depth of TMPL,
5121 then BINDING_VALUE is a parameter of TMPL. */
5122 return (tmpl && level == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)));
5125 /* Return the innermost non-namespace binding for NAME from a scope
5126 containing BINDING, or, if BINDING is NULL, the current scope.
5127 Please note that for a given template, the template parameters are
5128 considered to be in the scope containing the current scope.
5129 If CLASS_P is false, then class bindings are ignored. */
5131 cxx_binding *
5132 outer_binding (tree name,
5133 cxx_binding *binding,
5134 bool class_p)
5136 cxx_binding *outer;
5137 cp_binding_level *scope;
5138 cp_binding_level *outer_scope;
5140 if (binding)
5142 scope = binding->scope->level_chain;
5143 outer = binding->previous;
5145 else
5147 scope = current_binding_level;
5148 outer = IDENTIFIER_BINDING (name);
5150 outer_scope = outer ? outer->scope : NULL;
5152 /* Because we create class bindings lazily, we might be missing a
5153 class binding for NAME. If there are any class binding levels
5154 between the LAST_BINDING_LEVEL and the scope in which OUTER was
5155 declared, we must lookup NAME in those class scopes. */
5156 if (class_p)
5157 while (scope && scope != outer_scope && scope->kind != sk_namespace)
5159 if (scope->kind == sk_class)
5161 cxx_binding *class_binding;
5163 class_binding = get_class_binding (name, scope);
5164 if (class_binding)
5166 /* Thread this new class-scope binding onto the
5167 IDENTIFIER_BINDING list so that future lookups
5168 find it quickly. */
5169 class_binding->previous = outer;
5170 if (binding)
5171 binding->previous = class_binding;
5172 else
5173 IDENTIFIER_BINDING (name) = class_binding;
5174 return class_binding;
5177 /* If we are in a member template, the template parms of the member
5178 template are considered to be inside the scope of the containing
5179 class, but within G++ the class bindings are all pushed between the
5180 template parms and the function body. So if the outer binding is
5181 a template parm for the current scope, return it now rather than
5182 look for a class binding. */
5183 if (outer_scope && outer_scope->kind == sk_template_parms
5184 && binding_to_template_parms_of_scope_p (outer, scope))
5185 return outer;
5187 scope = scope->level_chain;
5190 return outer;
5193 /* Return the innermost block-scope or class-scope value binding for
5194 NAME, or NULL_TREE if there is no such binding. */
5196 tree
5197 innermost_non_namespace_value (tree name)
5199 cxx_binding *binding;
5200 binding = outer_binding (name, /*binding=*/NULL, /*class_p=*/true);
5201 return binding ? binding->value : NULL_TREE;
5204 /* Look up NAME in the current binding level and its superiors in the
5205 namespace of variables, functions and typedefs. Return a ..._DECL
5206 node of some kind representing its definition if there is only one
5207 such declaration, or return a TREE_LIST with all the overloaded
5208 definitions if there are many, or return 0 if it is undefined.
5209 Hidden name, either friend declaration or built-in function, are
5210 not ignored.
5212 If PREFER_TYPE is > 0, we prefer TYPE_DECLs or namespaces.
5213 If PREFER_TYPE is > 1, we reject non-type decls (e.g. namespaces).
5214 Otherwise we prefer non-TYPE_DECLs.
5216 If NONCLASS is nonzero, bindings in class scopes are ignored. If
5217 BLOCK_P is false, bindings in block scopes are ignored. */
5219 static tree
5220 lookup_name_real_1 (tree name, int prefer_type, int nonclass, bool block_p,
5221 int namespaces_only, int flags)
5223 cxx_binding *iter;
5224 tree val = NULL_TREE;
5226 query_oracle (name);
5228 /* Conversion operators are handled specially because ordinary
5229 unqualified name lookup will not find template conversion
5230 operators. */
5231 if (IDENTIFIER_CONV_OP_P (name))
5233 cp_binding_level *level;
5235 for (level = current_binding_level;
5236 level && level->kind != sk_namespace;
5237 level = level->level_chain)
5239 tree class_type;
5240 tree operators;
5242 /* A conversion operator can only be declared in a class
5243 scope. */
5244 if (level->kind != sk_class)
5245 continue;
5247 /* Lookup the conversion operator in the class. */
5248 class_type = level->this_entity;
5249 operators = lookup_fnfields (class_type, name, /*protect=*/0);
5250 if (operators)
5251 return operators;
5254 return NULL_TREE;
5257 flags |= lookup_flags (prefer_type, namespaces_only);
5259 /* First, look in non-namespace scopes. */
5261 if (current_class_type == NULL_TREE)
5262 nonclass = 1;
5264 if (block_p || !nonclass)
5265 for (iter = outer_binding (name, NULL, !nonclass);
5266 iter;
5267 iter = outer_binding (name, iter, !nonclass))
5269 tree binding;
5271 /* Skip entities we don't want. */
5272 if (LOCAL_BINDING_P (iter) ? !block_p : nonclass)
5273 continue;
5275 /* If this is the kind of thing we're looking for, we're done. */
5276 if (qualify_lookup (iter->value, flags))
5277 binding = iter->value;
5278 else if ((flags & LOOKUP_PREFER_TYPES)
5279 && qualify_lookup (iter->type, flags))
5280 binding = iter->type;
5281 else
5282 binding = NULL_TREE;
5284 if (binding)
5286 if (TREE_CODE (binding) == TYPE_DECL && DECL_HIDDEN_P (binding))
5288 /* A non namespace-scope binding can only be hidden in the
5289 presence of a local class, due to friend declarations.
5291 In particular, consider:
5293 struct C;
5294 void f() {
5295 struct A {
5296 friend struct B;
5297 friend struct C;
5298 void g() {
5299 B* b; // error: B is hidden
5300 C* c; // OK, finds ::C
5303 B *b; // error: B is hidden
5304 C *c; // OK, finds ::C
5305 struct B {};
5306 B *bb; // OK
5309 The standard says that "B" is a local class in "f"
5310 (but not nested within "A") -- but that name lookup
5311 for "B" does not find this declaration until it is
5312 declared directly with "f".
5314 In particular:
5316 [class.friend]
5318 If a friend declaration appears in a local class and
5319 the name specified is an unqualified name, a prior
5320 declaration is looked up without considering scopes
5321 that are outside the innermost enclosing non-class
5322 scope. For a friend function declaration, if there is
5323 no prior declaration, the program is ill-formed. For a
5324 friend class declaration, if there is no prior
5325 declaration, the class that is specified belongs to the
5326 innermost enclosing non-class scope, but if it is
5327 subsequently referenced, its name is not found by name
5328 lookup until a matching declaration is provided in the
5329 innermost enclosing nonclass scope.
5331 So just keep looking for a non-hidden binding.
5333 gcc_assert (TREE_CODE (binding) == TYPE_DECL);
5334 continue;
5336 val = binding;
5337 break;
5341 /* Now lookup in namespace scopes. */
5342 if (!val)
5344 name_lookup lookup (name, flags);
5345 if (lookup.search_unqualified
5346 (current_decl_namespace (), current_binding_level))
5347 val = lookup.value;
5350 /* If we have a single function from a using decl, pull it out. */
5351 if (val && TREE_CODE (val) == OVERLOAD && !really_overloaded_fn (val))
5352 val = OVL_FUNCTION (val);
5354 return val;
5357 /* Wrapper for lookup_name_real_1. */
5359 tree
5360 lookup_name_real (tree name, int prefer_type, int nonclass, bool block_p,
5361 int namespaces_only, int flags)
5363 tree ret;
5364 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
5365 ret = lookup_name_real_1 (name, prefer_type, nonclass, block_p,
5366 namespaces_only, flags);
5367 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
5368 return ret;
5371 tree
5372 lookup_name_nonclass (tree name)
5374 return lookup_name_real (name, 0, 1, /*block_p=*/true, 0, 0);
5377 tree
5378 lookup_name (tree name)
5380 return lookup_name_real (name, 0, 0, /*block_p=*/true, 0, 0);
5383 tree
5384 lookup_name_prefer_type (tree name, int prefer_type)
5386 return lookup_name_real (name, prefer_type, 0, /*block_p=*/true, 0, 0);
5389 /* Look up NAME for type used in elaborated name specifier in
5390 the scopes given by SCOPE. SCOPE can be either TS_CURRENT or
5391 TS_WITHIN_ENCLOSING_NON_CLASS. Although not implied by the
5392 name, more scopes are checked if cleanup or template parameter
5393 scope is encountered.
5395 Unlike lookup_name_real, we make sure that NAME is actually
5396 declared in the desired scope, not from inheritance, nor using
5397 directive. For using declaration, there is DR138 still waiting
5398 to be resolved. Hidden name coming from an earlier friend
5399 declaration is also returned.
5401 A TYPE_DECL best matching the NAME is returned. Catching error
5402 and issuing diagnostics are caller's responsibility. */
5404 static tree
5405 lookup_type_scope_1 (tree name, tag_scope scope)
5407 cxx_binding *iter = NULL;
5408 tree val = NULL_TREE;
5409 cp_binding_level *level = NULL;
5411 /* Look in non-namespace scope first. */
5412 if (current_binding_level->kind != sk_namespace)
5413 iter = outer_binding (name, NULL, /*class_p=*/ true);
5414 for (; iter; iter = outer_binding (name, iter, /*class_p=*/ true))
5416 /* Check if this is the kind of thing we're looking for.
5417 If SCOPE is TS_CURRENT, also make sure it doesn't come from
5418 base class. For ITER->VALUE, we can simply use
5419 INHERITED_VALUE_BINDING_P. For ITER->TYPE, we have to use
5420 our own check.
5422 We check ITER->TYPE before ITER->VALUE in order to handle
5423 typedef struct C {} C;
5424 correctly. */
5426 if (qualify_lookup (iter->type, LOOKUP_PREFER_TYPES)
5427 && (scope != ts_current
5428 || LOCAL_BINDING_P (iter)
5429 || DECL_CONTEXT (iter->type) == iter->scope->this_entity))
5430 val = iter->type;
5431 else if ((scope != ts_current
5432 || !INHERITED_VALUE_BINDING_P (iter))
5433 && qualify_lookup (iter->value, LOOKUP_PREFER_TYPES))
5434 val = iter->value;
5436 if (val)
5437 break;
5440 /* Look in namespace scope. */
5441 if (val)
5442 level = iter->scope;
5443 else
5445 tree ns = current_decl_namespace ();
5447 if (tree *slot = find_namespace_slot (ns, name))
5449 /* If this is the kind of thing we're looking for, we're done. */
5450 if (tree type = MAYBE_STAT_TYPE (*slot))
5451 if (qualify_lookup (type, LOOKUP_PREFER_TYPES))
5452 val = type;
5453 if (!val)
5455 if (tree decl = MAYBE_STAT_DECL (*slot))
5456 if (qualify_lookup (decl, LOOKUP_PREFER_TYPES))
5457 val = decl;
5459 level = NAMESPACE_LEVEL (ns);
5463 /* Type found, check if it is in the allowed scopes, ignoring cleanup
5464 and template parameter scopes. */
5465 if (val)
5467 cp_binding_level *b = current_binding_level;
5468 while (b)
5470 if (level == b)
5471 return val;
5473 if (b->kind == sk_cleanup || b->kind == sk_template_parms
5474 || b->kind == sk_function_parms)
5475 b = b->level_chain;
5476 else if (b->kind == sk_class
5477 && scope == ts_within_enclosing_non_class)
5478 b = b->level_chain;
5479 else
5480 break;
5484 return NULL_TREE;
5487 /* Wrapper for lookup_type_scope_1. */
5489 tree
5490 lookup_type_scope (tree name, tag_scope scope)
5492 tree ret;
5493 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
5494 ret = lookup_type_scope_1 (name, scope);
5495 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
5496 return ret;
5499 /* Returns true iff DECL is a block-scope extern declaration of a function
5500 or variable. */
5502 bool
5503 is_local_extern (tree decl)
5505 cxx_binding *binding;
5507 /* For functions, this is easy. */
5508 if (TREE_CODE (decl) == FUNCTION_DECL)
5509 return DECL_LOCAL_FUNCTION_P (decl);
5511 if (!VAR_P (decl))
5512 return false;
5513 if (!current_function_decl)
5514 return false;
5516 /* For variables, this is not easy. We need to look at the binding stack
5517 for the identifier to see whether the decl we have is a local. */
5518 for (binding = IDENTIFIER_BINDING (DECL_NAME (decl));
5519 binding && binding->scope->kind != sk_namespace;
5520 binding = binding->previous)
5521 if (binding->value == decl)
5522 return LOCAL_BINDING_P (binding);
5524 return false;
5527 /* The type TYPE is being declared. If it is a class template, or a
5528 specialization of a class template, do any processing required and
5529 perform error-checking. If IS_FRIEND is nonzero, this TYPE is
5530 being declared a friend. B is the binding level at which this TYPE
5531 should be bound.
5533 Returns the TYPE_DECL for TYPE, which may have been altered by this
5534 processing. */
5536 static tree
5537 maybe_process_template_type_declaration (tree type, int is_friend,
5538 cp_binding_level *b)
5540 tree decl = TYPE_NAME (type);
5542 if (processing_template_parmlist)
5543 /* You can't declare a new template type in a template parameter
5544 list. But, you can declare a non-template type:
5546 template <class A*> struct S;
5548 is a forward-declaration of `A'. */
5550 else if (b->kind == sk_namespace
5551 && current_binding_level->kind != sk_namespace)
5552 /* If this new type is being injected into a containing scope,
5553 then it's not a template type. */
5555 else
5557 gcc_assert (MAYBE_CLASS_TYPE_P (type)
5558 || TREE_CODE (type) == ENUMERAL_TYPE);
5560 if (processing_template_decl)
5562 /* This may change after the call to
5563 push_template_decl_real, but we want the original value. */
5564 tree name = DECL_NAME (decl);
5566 decl = push_template_decl_real (decl, is_friend);
5567 if (decl == error_mark_node)
5568 return error_mark_node;
5570 /* If the current binding level is the binding level for the
5571 template parameters (see the comment in
5572 begin_template_parm_list) and the enclosing level is a class
5573 scope, and we're not looking at a friend, push the
5574 declaration of the member class into the class scope. In the
5575 friend case, push_template_decl will already have put the
5576 friend into global scope, if appropriate. */
5577 if (TREE_CODE (type) != ENUMERAL_TYPE
5578 && !is_friend && b->kind == sk_template_parms
5579 && b->level_chain->kind == sk_class)
5581 finish_member_declaration (CLASSTYPE_TI_TEMPLATE (type));
5583 if (!COMPLETE_TYPE_P (current_class_type))
5585 maybe_add_class_template_decl_list (current_class_type,
5586 type, /*friend_p=*/0);
5587 /* Put this UTD in the table of UTDs for the class. */
5588 if (CLASSTYPE_NESTED_UTDS (current_class_type) == NULL)
5589 CLASSTYPE_NESTED_UTDS (current_class_type) =
5590 binding_table_new (SCOPE_DEFAULT_HT_SIZE);
5592 binding_table_insert
5593 (CLASSTYPE_NESTED_UTDS (current_class_type), name, type);
5599 return decl;
5602 /* Push a tag name NAME for struct/class/union/enum type TYPE. In case
5603 that the NAME is a class template, the tag is processed but not pushed.
5605 The pushed scope depend on the SCOPE parameter:
5606 - When SCOPE is TS_CURRENT, put it into the inner-most non-sk_cleanup
5607 scope.
5608 - When SCOPE is TS_GLOBAL, put it in the inner-most non-class and
5609 non-template-parameter scope. This case is needed for forward
5610 declarations.
5611 - When SCOPE is TS_WITHIN_ENCLOSING_NON_CLASS, this is similar to
5612 TS_GLOBAL case except that names within template-parameter scopes
5613 are not pushed at all.
5615 Returns TYPE upon success and ERROR_MARK_NODE otherwise. */
5617 static tree
5618 do_pushtag (tree name, tree type, tag_scope scope)
5620 tree decl;
5622 cp_binding_level *b = current_binding_level;
5623 while (/* Cleanup scopes are not scopes from the point of view of
5624 the language. */
5625 b->kind == sk_cleanup
5626 /* Neither are function parameter scopes. */
5627 || b->kind == sk_function_parms
5628 /* Neither are the scopes used to hold template parameters
5629 for an explicit specialization. For an ordinary template
5630 declaration, these scopes are not scopes from the point of
5631 view of the language. */
5632 || (b->kind == sk_template_parms
5633 && (b->explicit_spec_p || scope == ts_global))
5634 /* Pushing into a class is ok for lambdas or when we want current */
5635 || (b->kind == sk_class
5636 && scope != ts_lambda
5637 && (scope != ts_current
5638 /* We may be defining a new type in the initializer
5639 of a static member variable. We allow this when
5640 not pedantic, and it is particularly useful for
5641 type punning via an anonymous union. */
5642 || COMPLETE_TYPE_P (b->this_entity))))
5643 b = b->level_chain;
5645 gcc_assert (identifier_p (name));
5647 /* Do C++ gratuitous typedefing. */
5648 if (identifier_type_value_1 (name) != type)
5650 tree tdef;
5651 int in_class = 0;
5652 tree context = TYPE_CONTEXT (type);
5654 if (! context)
5656 tree cs = current_scope ();
5658 if (scope == ts_current
5659 || scope == ts_lambda
5660 || (cs && TREE_CODE (cs) == FUNCTION_DECL))
5661 context = cs;
5662 else if (cs && TYPE_P (cs))
5663 /* When declaring a friend class of a local class, we want
5664 to inject the newly named class into the scope
5665 containing the local class, not the namespace
5666 scope. */
5667 context = decl_function_context (get_type_decl (cs));
5669 if (!context)
5670 context = current_namespace;
5672 if (b->kind == sk_class
5673 || (b->kind == sk_template_parms
5674 && b->level_chain->kind == sk_class))
5675 in_class = 1;
5677 tdef = create_implicit_typedef (name, type);
5678 DECL_CONTEXT (tdef) = FROB_CONTEXT (context);
5679 if (scope == ts_within_enclosing_non_class)
5681 /* This is a friend. Make this TYPE_DECL node hidden from
5682 ordinary name lookup. Its corresponding TEMPLATE_DECL
5683 will be marked in push_template_decl_real. */
5684 retrofit_lang_decl (tdef);
5685 DECL_ANTICIPATED (tdef) = 1;
5686 DECL_FRIEND_P (tdef) = 1;
5689 decl = maybe_process_template_type_declaration
5690 (type, scope == ts_within_enclosing_non_class, b);
5691 if (decl == error_mark_node)
5692 return decl;
5694 if (b->kind == sk_class)
5696 if (!TYPE_BEING_DEFINED (current_class_type)
5697 && scope != ts_lambda)
5698 return error_mark_node;
5700 if (!PROCESSING_REAL_TEMPLATE_DECL_P ())
5701 /* Put this TYPE_DECL on the TYPE_FIELDS list for the
5702 class. But if it's a member template class, we want
5703 the TEMPLATE_DECL, not the TYPE_DECL, so this is done
5704 later. */
5705 finish_member_declaration (decl);
5706 else
5707 pushdecl_class_level (decl);
5709 else if (b->kind != sk_template_parms)
5711 decl = do_pushdecl_with_scope (decl, b, /*is_friend=*/false);
5712 if (decl == error_mark_node)
5713 return decl;
5715 if (DECL_CONTEXT (decl) == std_node
5716 && init_list_identifier == DECL_NAME (TYPE_NAME (type))
5717 && !CLASSTYPE_TEMPLATE_INFO (type))
5719 error ("declaration of std::initializer_list does not match "
5720 "#include <initializer_list>, isn't a template");
5721 return error_mark_node;
5725 if (! in_class)
5726 set_identifier_type_value_with_scope (name, tdef, b);
5728 TYPE_CONTEXT (type) = DECL_CONTEXT (decl);
5730 /* If this is a local class, keep track of it. We need this
5731 information for name-mangling, and so that it is possible to
5732 find all function definitions in a translation unit in a
5733 convenient way. (It's otherwise tricky to find a member
5734 function definition it's only pointed to from within a local
5735 class.) */
5736 if (TYPE_FUNCTION_SCOPE_P (type))
5738 if (processing_template_decl)
5740 /* Push a DECL_EXPR so we call pushtag at the right time in
5741 template instantiation rather than in some nested context. */
5742 add_decl_expr (decl);
5744 else
5745 vec_safe_push (local_classes, type);
5749 if (b->kind == sk_class
5750 && !COMPLETE_TYPE_P (current_class_type))
5752 maybe_add_class_template_decl_list (current_class_type,
5753 type, /*friend_p=*/0);
5755 if (CLASSTYPE_NESTED_UTDS (current_class_type) == NULL)
5756 CLASSTYPE_NESTED_UTDS (current_class_type)
5757 = binding_table_new (SCOPE_DEFAULT_HT_SIZE);
5759 binding_table_insert
5760 (CLASSTYPE_NESTED_UTDS (current_class_type), name, type);
5763 decl = TYPE_NAME (type);
5764 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
5766 /* Set type visibility now if this is a forward declaration. */
5767 TREE_PUBLIC (decl) = 1;
5768 determine_visibility (decl);
5770 return type;
5773 /* Wrapper for do_pushtag. */
5775 tree
5776 pushtag (tree name, tree type, tag_scope scope)
5778 tree ret;
5779 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
5780 ret = do_pushtag (name, type, scope);
5781 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
5782 return ret;
5786 /* Subroutines for reverting temporarily to top-level for instantiation
5787 of templates and such. We actually need to clear out the class- and
5788 local-value slots of all identifiers, so that only the global values
5789 are at all visible. Simply setting current_binding_level to the global
5790 scope isn't enough, because more binding levels may be pushed. */
5791 struct saved_scope *scope_chain;
5793 /* Return true if ID has not already been marked. */
5795 static inline bool
5796 store_binding_p (tree id)
5798 if (!id || !IDENTIFIER_BINDING (id))
5799 return false;
5801 if (IDENTIFIER_MARKED (id))
5802 return false;
5804 return true;
5807 /* Add an appropriate binding to *OLD_BINDINGS which needs to already
5808 have enough space reserved. */
5810 static void
5811 store_binding (tree id, vec<cxx_saved_binding, va_gc> **old_bindings)
5813 cxx_saved_binding saved;
5815 gcc_checking_assert (store_binding_p (id));
5817 IDENTIFIER_MARKED (id) = 1;
5819 saved.identifier = id;
5820 saved.binding = IDENTIFIER_BINDING (id);
5821 saved.real_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
5822 (*old_bindings)->quick_push (saved);
5823 IDENTIFIER_BINDING (id) = NULL;
5826 static void
5827 store_bindings (tree names, vec<cxx_saved_binding, va_gc> **old_bindings)
5829 static vec<tree> bindings_need_stored;
5830 tree t, id;
5831 size_t i;
5833 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
5834 for (t = names; t; t = TREE_CHAIN (t))
5836 if (TREE_CODE (t) == TREE_LIST)
5837 id = TREE_PURPOSE (t);
5838 else
5839 id = DECL_NAME (t);
5841 if (store_binding_p (id))
5842 bindings_need_stored.safe_push (id);
5844 if (!bindings_need_stored.is_empty ())
5846 vec_safe_reserve_exact (*old_bindings, bindings_need_stored.length ());
5847 for (i = 0; bindings_need_stored.iterate (i, &id); ++i)
5849 /* We can apparently have duplicates in NAMES. */
5850 if (store_binding_p (id))
5851 store_binding (id, old_bindings);
5853 bindings_need_stored.truncate (0);
5855 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
5858 /* Like store_bindings, but NAMES is a vector of cp_class_binding
5859 objects, rather than a TREE_LIST. */
5861 static void
5862 store_class_bindings (vec<cp_class_binding, va_gc> *names,
5863 vec<cxx_saved_binding, va_gc> **old_bindings)
5865 static vec<tree> bindings_need_stored;
5866 size_t i;
5867 cp_class_binding *cb;
5869 for (i = 0; vec_safe_iterate (names, i, &cb); ++i)
5870 if (store_binding_p (cb->identifier))
5871 bindings_need_stored.safe_push (cb->identifier);
5872 if (!bindings_need_stored.is_empty ())
5874 tree id;
5875 vec_safe_reserve_exact (*old_bindings, bindings_need_stored.length ());
5876 for (i = 0; bindings_need_stored.iterate (i, &id); ++i)
5877 store_binding (id, old_bindings);
5878 bindings_need_stored.truncate (0);
5882 /* A chain of saved_scope structures awaiting reuse. */
5884 static GTY((deletable)) struct saved_scope *free_saved_scope;
5886 static void
5887 do_push_to_top_level (void)
5889 struct saved_scope *s;
5890 cp_binding_level *b;
5891 cxx_saved_binding *sb;
5892 size_t i;
5893 bool need_pop;
5895 /* Reuse or create a new structure for this saved scope. */
5896 if (free_saved_scope != NULL)
5898 s = free_saved_scope;
5899 free_saved_scope = s->prev;
5901 vec<cxx_saved_binding, va_gc> *old_bindings = s->old_bindings;
5902 memset (s, 0, sizeof (*s));
5903 /* Also reuse the structure's old_bindings vector. */
5904 vec_safe_truncate (old_bindings, 0);
5905 s->old_bindings = old_bindings;
5907 else
5908 s = ggc_cleared_alloc<saved_scope> ();
5910 b = scope_chain ? current_binding_level : 0;
5912 /* If we're in the middle of some function, save our state. */
5913 if (cfun)
5915 need_pop = true;
5916 push_function_context ();
5918 else
5919 need_pop = false;
5921 if (scope_chain && previous_class_level)
5922 store_class_bindings (previous_class_level->class_shadowed,
5923 &s->old_bindings);
5925 /* Have to include the global scope, because class-scope decls
5926 aren't listed anywhere useful. */
5927 for (; b; b = b->level_chain)
5929 tree t;
5931 /* Template IDs are inserted into the global level. If they were
5932 inserted into namespace level, finish_file wouldn't find them
5933 when doing pending instantiations. Therefore, don't stop at
5934 namespace level, but continue until :: . */
5935 if (global_scope_p (b))
5936 break;
5938 store_bindings (b->names, &s->old_bindings);
5939 /* We also need to check class_shadowed to save class-level type
5940 bindings, since pushclass doesn't fill in b->names. */
5941 if (b->kind == sk_class)
5942 store_class_bindings (b->class_shadowed, &s->old_bindings);
5944 /* Unwind type-value slots back to top level. */
5945 for (t = b->type_shadowed; t; t = TREE_CHAIN (t))
5946 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (t), TREE_VALUE (t));
5949 FOR_EACH_VEC_SAFE_ELT (s->old_bindings, i, sb)
5950 IDENTIFIER_MARKED (sb->identifier) = 0;
5952 s->prev = scope_chain;
5953 s->bindings = b;
5954 s->need_pop_function_context = need_pop;
5955 s->function_decl = current_function_decl;
5956 s->unevaluated_operand = cp_unevaluated_operand;
5957 s->inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
5958 s->x_stmt_tree.stmts_are_full_exprs_p = true;
5960 scope_chain = s;
5961 current_function_decl = NULL_TREE;
5962 vec_alloc (current_lang_base, 10);
5963 current_lang_name = lang_name_cplusplus;
5964 current_namespace = global_namespace;
5965 push_class_stack ();
5966 cp_unevaluated_operand = 0;
5967 c_inhibit_evaluation_warnings = 0;
5970 static void
5971 do_pop_from_top_level (void)
5973 struct saved_scope *s = scope_chain;
5974 cxx_saved_binding *saved;
5975 size_t i;
5977 /* Clear out class-level bindings cache. */
5978 if (previous_class_level)
5979 invalidate_class_lookup_cache ();
5980 pop_class_stack ();
5982 current_lang_base = 0;
5984 scope_chain = s->prev;
5985 FOR_EACH_VEC_SAFE_ELT (s->old_bindings, i, saved)
5987 tree id = saved->identifier;
5989 IDENTIFIER_BINDING (id) = saved->binding;
5990 SET_IDENTIFIER_TYPE_VALUE (id, saved->real_type_value);
5993 /* If we were in the middle of compiling a function, restore our
5994 state. */
5995 if (s->need_pop_function_context)
5996 pop_function_context ();
5997 current_function_decl = s->function_decl;
5998 cp_unevaluated_operand = s->unevaluated_operand;
5999 c_inhibit_evaluation_warnings = s->inhibit_evaluation_warnings;
6001 /* Make this saved_scope structure available for reuse by
6002 push_to_top_level. */
6003 s->prev = free_saved_scope;
6004 free_saved_scope = s;
6007 /* Push into the scope of the namespace NS, even if it is deeply
6008 nested within another namespace. */
6010 static void
6011 do_push_nested_namespace (tree ns)
6013 if (ns == global_namespace)
6014 do_push_to_top_level ();
6015 else
6017 do_push_nested_namespace (CP_DECL_CONTEXT (ns));
6018 gcc_checking_assert
6019 (find_namespace_value (current_namespace,
6020 DECL_NAME (ns) ? DECL_NAME (ns)
6021 : anon_identifier) == ns);
6022 resume_scope (NAMESPACE_LEVEL (ns));
6023 current_namespace = ns;
6027 /* Pop back from the scope of the namespace NS, which was previously
6028 entered with push_nested_namespace. */
6030 static void
6031 do_pop_nested_namespace (tree ns)
6033 while (ns != global_namespace)
6035 ns = CP_DECL_CONTEXT (ns);
6036 current_namespace = ns;
6037 leave_scope ();
6040 do_pop_from_top_level ();
6043 /* Add TARGET to USINGS, if it does not already exist there.
6044 We used to build the complete graph of usings at this point, from
6045 the POV of the source namespaces. Now we build that as we perform
6046 the unqualified search. */
6048 static void
6049 add_using_namespace (vec<tree, va_gc> *&usings, tree target)
6051 if (usings)
6052 for (unsigned ix = usings->length (); ix--;)
6053 if ((*usings)[ix] == target)
6054 return;
6056 vec_safe_push (usings, target);
6059 /* Tell the debug system of a using directive. */
6061 static void
6062 emit_debug_info_using_namespace (tree from, tree target)
6064 /* Emit debugging info. */
6065 tree context = from != global_namespace ? from : NULL_TREE;
6066 debug_hooks->imported_module_or_decl (target, NULL_TREE, context, false);
6069 /* Process a namespace-scope using directive. */
6071 void
6072 finish_namespace_using_directive (tree target, tree attribs)
6074 gcc_checking_assert (namespace_bindings_p ());
6075 if (target == error_mark_node)
6076 return;
6078 add_using_namespace (DECL_NAMESPACE_USING (current_namespace),
6079 ORIGINAL_NAMESPACE (target));
6080 emit_debug_info_using_namespace (current_namespace,
6081 ORIGINAL_NAMESPACE (target));
6083 if (attribs == error_mark_node)
6084 return;
6086 for (tree a = attribs; a; a = TREE_CHAIN (a))
6088 tree name = get_attribute_name (a);
6089 if (is_attribute_p ("strong", name))
6091 warning (0, "strong using directive no longer supported");
6092 if (CP_DECL_CONTEXT (target) == current_namespace)
6093 inform (DECL_SOURCE_LOCATION (target),
6094 "you may use an inline namespace instead");
6096 else
6097 warning (OPT_Wattributes, "%qD attribute directive ignored", name);
6101 /* Process a function-scope using-directive. */
6103 void
6104 finish_local_using_directive (tree target, tree attribs)
6106 gcc_checking_assert (local_bindings_p ());
6107 if (target == error_mark_node)
6108 return;
6110 if (attribs)
6111 warning (OPT_Wattributes, "attributes ignored on local using directive");
6113 add_stmt (build_stmt (input_location, USING_STMT, target));
6115 add_using_namespace (current_binding_level->using_directives,
6116 ORIGINAL_NAMESPACE (target));
6119 /* Pushes X into the global namespace. */
6121 tree
6122 pushdecl_top_level (tree x, bool is_friend)
6124 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6125 do_push_to_top_level ();
6126 x = pushdecl_namespace_level (x, is_friend);
6127 do_pop_from_top_level ();
6128 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6129 return x;
6132 /* Pushes X into the global namespace and calls cp_finish_decl to
6133 register the variable, initializing it with INIT. */
6135 tree
6136 pushdecl_top_level_and_finish (tree x, tree init)
6138 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6139 do_push_to_top_level ();
6140 x = pushdecl_namespace_level (x, false);
6141 cp_finish_decl (x, init, false, NULL_TREE, 0);
6142 do_pop_from_top_level ();
6143 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6144 return x;
6147 /* Enter the namespaces from current_namerspace to NS. */
6149 static int
6150 push_inline_namespaces (tree ns)
6152 int count = 0;
6153 if (ns != current_namespace)
6155 gcc_assert (ns != global_namespace);
6156 count += push_inline_namespaces (CP_DECL_CONTEXT (ns));
6157 resume_scope (NAMESPACE_LEVEL (ns));
6158 current_namespace = ns;
6159 count++;
6161 return count;
6164 /* Push into the scope of the NAME namespace. If NAME is NULL_TREE,
6165 then we enter an anonymous namespace. If MAKE_INLINE is true, then
6166 we create an inline namespace (it is up to the caller to check upon
6167 redefinition). Return the number of namespaces entered. */
6170 push_namespace (tree name, bool make_inline)
6172 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6173 int count = 0;
6175 /* We should not get here if the global_namespace is not yet constructed
6176 nor if NAME designates the global namespace: The global scope is
6177 constructed elsewhere. */
6178 gcc_assert (global_namespace != NULL && name != global_identifier);
6180 if (!name)
6181 name = anon_identifier;
6183 tree ns = NULL_TREE;
6185 name_lookup lookup (name, 0);
6186 if (!lookup.search_qualified (current_namespace, /*usings=*/false))
6188 else if (TREE_CODE (lookup.value) != NAMESPACE_DECL)
6190 else if (tree dna = DECL_NAMESPACE_ALIAS (lookup.value))
6192 /* A namespace alias is not allowed here, but if the alias
6193 is for a namespace also inside the current scope,
6194 accept it with a diagnostic. That's better than dying
6195 horribly. */
6196 if (is_nested_namespace (current_namespace, CP_DECL_CONTEXT (dna)))
6198 error ("namespace alias %qD not allowed here, "
6199 "assuming %qD", lookup.value, dna);
6200 ns = dna;
6203 else
6204 ns = lookup.value;
6207 bool new_ns = false;
6208 if (ns)
6209 /* DR2061. NS might be a member of an inline namespace. We
6210 need to push into those namespaces. */
6211 count += push_inline_namespaces (CP_DECL_CONTEXT (ns));
6212 else
6214 ns = build_lang_decl (NAMESPACE_DECL, name, void_type_node);
6215 SCOPE_DEPTH (ns) = SCOPE_DEPTH (current_namespace) + 1;
6216 if (!SCOPE_DEPTH (ns))
6217 /* We only allow depth 255. */
6218 sorry ("cannot nest more than %d namespaces",
6219 SCOPE_DEPTH (current_namespace));
6220 DECL_CONTEXT (ns) = FROB_CONTEXT (current_namespace);
6221 new_ns = true;
6223 if (pushdecl (ns) == error_mark_node)
6224 ns = NULL_TREE;
6225 else
6227 if (name == anon_identifier)
6229 /* Clear DECL_NAME for the benefit of debugging back ends. */
6230 SET_DECL_ASSEMBLER_NAME (ns, name);
6231 DECL_NAME (ns) = NULL_TREE;
6233 if (!make_inline)
6234 add_using_namespace (DECL_NAMESPACE_USING (current_namespace),
6235 ns);
6237 else if (TREE_PUBLIC (current_namespace))
6238 TREE_PUBLIC (ns) = 1;
6240 if (name == anon_identifier || make_inline)
6241 emit_debug_info_using_namespace (current_namespace, ns);
6243 if (make_inline)
6245 DECL_NAMESPACE_INLINE_P (ns) = true;
6246 vec_safe_push (DECL_NAMESPACE_INLINEES (current_namespace), ns);
6251 if (ns)
6253 if (make_inline && !DECL_NAMESPACE_INLINE_P (ns))
6255 error ("inline namespace must be specified at initial definition");
6256 inform (DECL_SOURCE_LOCATION (ns), "%qD defined here", ns);
6258 if (new_ns)
6259 begin_scope (sk_namespace, ns);
6260 else
6261 resume_scope (NAMESPACE_LEVEL (ns));
6262 current_namespace = ns;
6263 count++;
6266 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6267 return count;
6270 /* Pop from the scope of the current namespace. */
6272 void
6273 pop_namespace (void)
6275 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6277 gcc_assert (current_namespace != global_namespace);
6278 current_namespace = CP_DECL_CONTEXT (current_namespace);
6279 /* The binding level is not popped, as it might be re-opened later. */
6280 leave_scope ();
6282 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6285 /* External entry points for do_{push_to/pop_from}_top_level. */
6287 void
6288 push_to_top_level (void)
6290 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6291 do_push_to_top_level ();
6292 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6295 void
6296 pop_from_top_level (void)
6298 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6299 do_pop_from_top_level ();
6300 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6303 /* External entry points for do_{push,pop}_nested_namespace. */
6305 void
6306 push_nested_namespace (tree ns)
6308 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6309 do_push_nested_namespace (ns);
6310 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6313 void
6314 pop_nested_namespace (tree ns)
6316 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6317 gcc_assert (current_namespace == ns);
6318 do_pop_nested_namespace (ns);
6319 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6322 /* Pop off extraneous binding levels left over due to syntax errors.
6323 We don't pop past namespaces, as they might be valid. */
6325 void
6326 pop_everything (void)
6328 if (ENABLE_SCOPE_CHECKING)
6329 verbatim ("XXX entering pop_everything ()\n");
6330 while (!namespace_bindings_p ())
6332 if (current_binding_level->kind == sk_class)
6333 pop_nested_class ();
6334 else
6335 poplevel (0, 0, 0);
6337 if (ENABLE_SCOPE_CHECKING)
6338 verbatim ("XXX leaving pop_everything ()\n");
6341 /* Emit debugging information for using declarations and directives.
6342 If input tree is overloaded fn then emit debug info for all
6343 candidates. */
6345 void
6346 cp_emit_debug_info_for_using (tree t, tree context)
6348 /* Don't try to emit any debug information if we have errors. */
6349 if (seen_error ())
6350 return;
6352 /* Ignore this FUNCTION_DECL if it refers to a builtin declaration
6353 of a builtin function. */
6354 if (TREE_CODE (t) == FUNCTION_DECL
6355 && DECL_EXTERNAL (t)
6356 && DECL_BUILT_IN (t))
6357 return;
6359 /* Do not supply context to imported_module_or_decl, if
6360 it is a global namespace. */
6361 if (context == global_namespace)
6362 context = NULL_TREE;
6364 t = MAYBE_BASELINK_FUNCTIONS (t);
6366 /* FIXME: Handle TEMPLATE_DECLs. */
6367 for (lkp_iterator iter (t); iter; ++iter)
6369 tree fn = *iter;
6370 if (TREE_CODE (fn) != TEMPLATE_DECL)
6372 if (building_stmt_list_p ())
6373 add_stmt (build_stmt (input_location, USING_STMT, fn));
6374 else
6375 debug_hooks->imported_module_or_decl (fn,
6376 NULL_TREE, context, false);
6381 #include "gt-cp-name-lookup.h"