PR c++/82401
[official-gcc.git] / gcc / cp / name-lookup.c
blob9f65c4d7992738d005ceb36e48be75586ce40ff3
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 #define INCLUDE_UNIQUE_PTR
23 #include "system.h"
24 #include "coretypes.h"
25 #include "cp-tree.h"
26 #include "timevar.h"
27 #include "stringpool.h"
28 #include "print-tree.h"
29 #include "attribs.h"
30 #include "debug.h"
31 #include "c-family/c-pragma.h"
32 #include "params.h"
33 #include "gcc-rich-location.h"
34 #include "spellcheck-tree.h"
35 #include "parser.h"
36 #include "c-family/name-hint.h"
37 #include "c-family/known-headers.h"
39 static cxx_binding *cxx_binding_make (tree value, tree type);
40 static cp_binding_level *innermost_nonclass_level (void);
41 static void set_identifier_type_value_with_scope (tree id, tree decl,
42 cp_binding_level *b);
44 /* Create an overload suitable for recording an artificial TYPE_DECL
45 and another decl. We use this machanism to implement the struct
46 stat hack within a namespace. It'd be nice to use it everywhere. */
48 #define STAT_HACK_P(N) ((N) && TREE_CODE (N) == OVERLOAD && OVL_LOOKUP_P (N))
49 #define STAT_TYPE(N) TREE_TYPE (N)
50 #define STAT_DECL(N) OVL_FUNCTION (N)
51 #define MAYBE_STAT_DECL(N) (STAT_HACK_P (N) ? STAT_DECL (N) : N)
52 #define MAYBE_STAT_TYPE(N) (STAT_HACK_P (N) ? STAT_TYPE (N) : NULL_TREE)
54 /* Create a STAT_HACK node with DECL as the value binding and TYPE as
55 the type binding. */
57 static tree
58 stat_hack (tree decl = NULL_TREE, tree type = NULL_TREE)
60 tree result = make_node (OVERLOAD);
62 /* Mark this as a lookup, so we can tell this is a stat hack. */
63 OVL_LOOKUP_P (result) = true;
64 STAT_DECL (result) = decl;
65 STAT_TYPE (result) = type;
66 return result;
69 /* Create a local binding level for NAME. */
71 static cxx_binding *
72 create_local_binding (cp_binding_level *level, tree name)
74 cxx_binding *binding = cxx_binding_make (NULL, NULL);
76 INHERITED_VALUE_BINDING_P (binding) = false;
77 LOCAL_BINDING_P (binding) = true;
78 binding->scope = level;
79 binding->previous = IDENTIFIER_BINDING (name);
81 IDENTIFIER_BINDING (name) = binding;
83 return binding;
86 /* Find the binding for NAME in namespace NS. If CREATE_P is true,
87 make an empty binding if there wasn't one. */
89 static tree *
90 find_namespace_slot (tree ns, tree name, bool create_p = false)
92 tree *slot = DECL_NAMESPACE_BINDINGS (ns)
93 ->find_slot_with_hash (name, name ? IDENTIFIER_HASH_VALUE (name) : 0,
94 create_p ? INSERT : NO_INSERT);
95 return slot;
98 static tree
99 find_namespace_value (tree ns, tree name)
101 tree *b = find_namespace_slot (ns, name);
103 return b ? MAYBE_STAT_DECL (*b) : NULL_TREE;
106 /* Add DECL to the list of things declared in B. */
108 static void
109 add_decl_to_level (cp_binding_level *b, tree decl)
111 gcc_assert (b->kind != sk_class);
113 /* Make sure we don't create a circular list. xref_tag can end
114 up pushing the same artificial decl more than once. We
115 should have already detected that in update_binding. */
116 gcc_assert (b->names != decl);
118 /* We build up the list in reverse order, and reverse it later if
119 necessary. */
120 TREE_CHAIN (decl) = b->names;
121 b->names = decl;
123 /* If appropriate, add decl to separate list of statics. We
124 include extern variables because they might turn out to be
125 static later. It's OK for this list to contain a few false
126 positives. */
127 if (b->kind == sk_namespace
128 && ((VAR_P (decl)
129 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
130 || (TREE_CODE (decl) == FUNCTION_DECL
131 && (!TREE_PUBLIC (decl)
132 || decl_anon_ns_mem_p (decl)
133 || DECL_DECLARED_INLINE_P (decl)))))
134 vec_safe_push (static_decls, decl);
137 /* Find the binding for NAME in the local binding level B. */
139 static cxx_binding *
140 find_local_binding (cp_binding_level *b, tree name)
142 if (cxx_binding *binding = IDENTIFIER_BINDING (name))
143 for (;; b = b->level_chain)
145 if (binding->scope == b
146 && !(VAR_P (binding->value)
147 && DECL_DEAD_FOR_LOCAL (binding->value)))
148 return binding;
150 /* Cleanup contours are transparent to the language. */
151 if (b->kind != sk_cleanup)
152 break;
154 return NULL;
157 struct name_lookup
159 public:
160 typedef std::pair<tree, tree> using_pair;
161 typedef vec<using_pair, va_heap, vl_embed> using_queue;
163 public:
164 tree name; /* The identifier being looked for. */
165 tree value; /* A (possibly ambiguous) set of things found. */
166 tree type; /* A type that has been found. */
167 int flags; /* Lookup flags. */
168 bool deduping; /* Full deduping is needed because using declarations
169 are in play. */
170 vec<tree, va_heap, vl_embed> *scopes;
171 name_lookup *previous; /* Previously active lookup. */
173 protected:
174 /* Marked scope stack for outermost name lookup. */
175 static vec<tree, va_heap, vl_embed> *shared_scopes;
176 /* Currently active lookup. */
177 static name_lookup *active;
179 public:
180 name_lookup (tree n, int f = 0)
181 : name (n), value (NULL_TREE), type (NULL_TREE), flags (f),
182 deduping (false), scopes (NULL), previous (NULL)
184 preserve_state ();
186 ~name_lookup ()
188 restore_state ();
191 private: /* Uncopyable, unmovable, unassignable. I am a rock. */
192 name_lookup (const name_lookup &);
193 name_lookup &operator= (const name_lookup &);
195 protected:
196 static bool seen_p (tree scope)
198 return LOOKUP_SEEN_P (scope);
200 static bool found_p (tree scope)
202 return LOOKUP_FOUND_P (scope);
205 void mark_seen (tree scope); /* Mark and add to scope vector. */
206 static void mark_found (tree scope)
208 gcc_checking_assert (seen_p (scope));
209 LOOKUP_FOUND_P (scope) = true;
211 bool see_and_mark (tree scope)
213 bool ret = seen_p (scope);
214 if (!ret)
215 mark_seen (scope);
216 return ret;
218 bool find_and_mark (tree scope);
220 private:
221 void preserve_state ();
222 void restore_state ();
224 private:
225 static tree ambiguous (tree thing, tree current);
226 void add_overload (tree fns);
227 void add_value (tree new_val);
228 void add_type (tree new_type);
229 bool process_binding (tree val_bind, tree type_bind);
231 /* Look in only namespace. */
232 bool search_namespace_only (tree scope);
233 /* Look in namespace and its (recursive) inlines. Ignore using
234 directives. Return true if something found (inc dups). */
235 bool search_namespace (tree scope);
236 /* Look in the using directives of namespace + inlines using
237 qualified lookup rules. */
238 bool search_usings (tree scope);
240 private:
241 using_queue *queue_namespace (using_queue *queue, int depth, tree scope);
242 using_queue *do_queue_usings (using_queue *queue, int depth,
243 vec<tree, va_gc> *usings);
244 using_queue *queue_usings (using_queue *queue, int depth,
245 vec<tree, va_gc> *usings)
247 if (usings)
248 queue = do_queue_usings (queue, depth, usings);
249 return queue;
252 private:
253 void add_fns (tree);
255 void adl_expr (tree);
256 void adl_type (tree);
257 void adl_template_arg (tree);
258 void adl_class (tree);
259 void adl_bases (tree);
260 void adl_class_only (tree);
261 void adl_namespace (tree);
262 void adl_namespace_only (tree);
264 public:
265 /* Search namespace + inlines + maybe usings as qualified lookup. */
266 bool search_qualified (tree scope, bool usings = true);
268 /* Search namespace + inlines + usings as unqualified lookup. */
269 bool search_unqualified (tree scope, cp_binding_level *);
271 /* ADL lookup of ARGS. */
272 tree search_adl (tree fns, vec<tree, va_gc> *args);
275 /* Scope stack shared by all outermost lookups. This avoids us
276 allocating and freeing on every single lookup. */
277 vec<tree, va_heap, vl_embed> *name_lookup::shared_scopes;
279 /* Currently active lookup. */
280 name_lookup *name_lookup::active;
282 /* Name lookup is recursive, becase ADL can cause template
283 instatiation. This is of course a rare event, so we optimize for
284 it not happening. When we discover an active name-lookup, which
285 must be an ADL lookup, we need to unmark the marked scopes and also
286 unmark the lookup we might have been accumulating. */
288 void
289 name_lookup::preserve_state ()
291 previous = active;
292 if (previous)
294 unsigned length = vec_safe_length (previous->scopes);
295 vec_safe_reserve (previous->scopes, length * 2);
296 for (unsigned ix = length; ix--;)
298 tree decl = (*previous->scopes)[ix];
300 gcc_checking_assert (LOOKUP_SEEN_P (decl));
301 LOOKUP_SEEN_P (decl) = false;
303 /* Preserve the FOUND_P state on the interrupted lookup's
304 stack. */
305 if (LOOKUP_FOUND_P (decl))
307 LOOKUP_FOUND_P (decl) = false;
308 previous->scopes->quick_push (decl);
312 /* Unmark the outer partial lookup. */
313 if (previous->deduping)
314 lookup_mark (previous->value, false);
316 else
317 scopes = shared_scopes;
318 active = this;
321 /* Restore the marking state of a lookup we interrupted. */
323 void
324 name_lookup::restore_state ()
326 if (deduping)
327 lookup_mark (value, false);
329 /* Unmark and empty this lookup's scope stack. */
330 for (unsigned ix = vec_safe_length (scopes); ix--;)
332 tree decl = scopes->pop ();
333 gcc_checking_assert (LOOKUP_SEEN_P (decl));
334 LOOKUP_SEEN_P (decl) = false;
335 LOOKUP_FOUND_P (decl) = false;
338 active = previous;
339 if (previous)
341 free (scopes);
343 unsigned length = vec_safe_length (previous->scopes);
344 for (unsigned ix = 0; ix != length; ix++)
346 tree decl = (*previous->scopes)[ix];
347 if (LOOKUP_SEEN_P (decl))
349 /* The remainder of the scope stack must be recording
350 FOUND_P decls, which we want to pop off. */
353 tree decl = previous->scopes->pop ();
354 gcc_checking_assert (LOOKUP_SEEN_P (decl)
355 && !LOOKUP_FOUND_P (decl));
356 LOOKUP_FOUND_P (decl) = true;
358 while (++ix != length);
359 break;
362 gcc_checking_assert (!LOOKUP_FOUND_P (decl));
363 LOOKUP_SEEN_P (decl) = true;
366 /* Remark the outer partial lookup. */
367 if (previous->deduping)
368 lookup_mark (previous->value, true);
370 else
371 shared_scopes = scopes;
374 void
375 name_lookup::mark_seen (tree scope)
377 gcc_checking_assert (!seen_p (scope));
378 LOOKUP_SEEN_P (scope) = true;
379 vec_safe_push (scopes, scope);
382 bool
383 name_lookup::find_and_mark (tree scope)
385 bool result = LOOKUP_FOUND_P (scope);
386 if (!result)
388 LOOKUP_FOUND_P (scope) = true;
389 if (!LOOKUP_SEEN_P (scope))
390 vec_safe_push (scopes, scope);
393 return result;
396 /* THING and CURRENT are ambiguous, concatenate them. */
398 tree
399 name_lookup::ambiguous (tree thing, tree current)
401 if (TREE_CODE (current) != TREE_LIST)
403 current = build_tree_list (NULL_TREE, current);
404 TREE_TYPE (current) = error_mark_node;
406 current = tree_cons (NULL_TREE, thing, current);
407 TREE_TYPE (current) = error_mark_node;
409 return current;
412 /* FNS is a new overload set to add to the exising set. */
414 void
415 name_lookup::add_overload (tree fns)
417 if (!deduping && TREE_CODE (fns) == OVERLOAD)
419 tree probe = fns;
420 if (flags & LOOKUP_HIDDEN)
421 probe = ovl_skip_hidden (probe);
422 if (probe && TREE_CODE (probe) == OVERLOAD && OVL_USING_P (probe))
424 /* We're about to add something found by a using
425 declaration, so need to engage deduping mode. */
426 lookup_mark (value, true);
427 deduping = true;
431 value = lookup_maybe_add (fns, value, deduping);
434 /* Add a NEW_VAL, a found value binding into the current value binding. */
436 void
437 name_lookup::add_value (tree new_val)
439 if (OVL_P (new_val) && (!value || OVL_P (value)))
440 add_overload (new_val);
441 else if (!value)
442 value = new_val;
443 else if (value == new_val)
445 else if ((TREE_CODE (value) == TYPE_DECL
446 && TREE_CODE (new_val) == TYPE_DECL
447 && same_type_p (TREE_TYPE (value), TREE_TYPE (new_val))))
448 /* Typedefs to the same type. */;
449 else if (TREE_CODE (value) == NAMESPACE_DECL
450 && TREE_CODE (new_val) == NAMESPACE_DECL
451 && ORIGINAL_NAMESPACE (value) == ORIGINAL_NAMESPACE (new_val))
452 /* Namespace (possibly aliased) to the same namespace. Locate
453 the namespace*/
454 value = ORIGINAL_NAMESPACE (value);
455 else
457 if (deduping)
459 /* Disengage deduping mode. */
460 lookup_mark (value, false);
461 deduping = false;
463 value = ambiguous (new_val, value);
467 /* Add a NEW_TYPE, a found type binding into the current type binding. */
469 void
470 name_lookup::add_type (tree new_type)
472 if (!type)
473 type = new_type;
474 else if (TREE_CODE (type) == TREE_LIST
475 || !same_type_p (TREE_TYPE (type), TREE_TYPE (new_type)))
476 type = ambiguous (new_type, type);
479 /* Process a found binding containing NEW_VAL and NEW_TYPE. Returns
480 true if we actually found something noteworthy. */
482 bool
483 name_lookup::process_binding (tree new_val, tree new_type)
485 /* Did we really see a type? */
486 if (new_type
487 && (LOOKUP_NAMESPACES_ONLY (flags)
488 || (!(flags & LOOKUP_HIDDEN)
489 && DECL_LANG_SPECIFIC (new_type)
490 && DECL_ANTICIPATED (new_type))))
491 new_type = NULL_TREE;
493 if (new_val && !(flags & LOOKUP_HIDDEN))
494 new_val = ovl_skip_hidden (new_val);
496 /* Do we really see a value? */
497 if (new_val)
498 switch (TREE_CODE (new_val))
500 case TEMPLATE_DECL:
501 /* If we expect types or namespaces, and not templates,
502 or this is not a template class. */
503 if ((LOOKUP_QUALIFIERS_ONLY (flags)
504 && !DECL_TYPE_TEMPLATE_P (new_val)))
505 new_val = NULL_TREE;
506 break;
507 case TYPE_DECL:
508 if (LOOKUP_NAMESPACES_ONLY (flags)
509 || (new_type && (flags & LOOKUP_PREFER_TYPES)))
510 new_val = NULL_TREE;
511 break;
512 case NAMESPACE_DECL:
513 if (LOOKUP_TYPES_ONLY (flags))
514 new_val = NULL_TREE;
515 break;
516 default:
517 if (LOOKUP_QUALIFIERS_ONLY (flags))
518 new_val = NULL_TREE;
521 if (!new_val)
523 new_val = new_type;
524 new_type = NULL_TREE;
527 /* Merge into the lookup */
528 if (new_val)
529 add_value (new_val);
530 if (new_type)
531 add_type (new_type);
533 return new_val != NULL_TREE;
536 /* Look in exactly namespace SCOPE. */
538 bool
539 name_lookup::search_namespace_only (tree scope)
541 bool found = false;
543 if (tree *binding = find_namespace_slot (scope, name))
544 found |= process_binding (MAYBE_STAT_DECL (*binding),
545 MAYBE_STAT_TYPE (*binding));
547 return found;
550 /* Conditionally look in namespace SCOPE and inline children. */
552 bool
553 name_lookup::search_namespace (tree scope)
555 if (see_and_mark (scope))
556 /* We've visited this scope before. Return what we found then. */
557 return found_p (scope);
559 /* Look in exactly namespace. */
560 bool found = search_namespace_only (scope);
562 /* Recursively look in its inline children. */
563 if (vec<tree, va_gc> *inlinees = DECL_NAMESPACE_INLINEES (scope))
564 for (unsigned ix = inlinees->length (); ix--;)
565 found |= search_namespace ((*inlinees)[ix]);
567 if (found)
568 mark_found (scope);
570 return found;
573 /* Recursively follow using directives of SCOPE & its inline children.
574 Such following is essentially a flood-fill algorithm. */
576 bool
577 name_lookup::search_usings (tree scope)
579 /* We do not check seen_p here, as that was already set during the
580 namespace_only walk. */
581 if (found_p (scope))
582 return true;
584 bool found = false;
585 if (vec<tree, va_gc> *usings = DECL_NAMESPACE_USING (scope))
586 for (unsigned ix = usings->length (); ix--;)
587 found |= search_qualified ((*usings)[ix], true);
589 /* Look in its inline children. */
590 if (vec<tree, va_gc> *inlinees = DECL_NAMESPACE_INLINEES (scope))
591 for (unsigned ix = inlinees->length (); ix--;)
592 found |= search_usings ((*inlinees)[ix]);
594 if (found)
595 mark_found (scope);
597 return found;
600 /* Qualified namespace lookup in SCOPE.
601 1) Look in SCOPE (+inlines). If found, we're done.
602 2) Otherwise, if USINGS is true,
603 recurse for every using directive of SCOPE (+inlines).
605 Trickiness is (a) loops and (b) multiple paths to same namespace.
606 In both cases we want to not repeat any lookups, and know whether
607 to stop the caller's step #2. Do this via the FOUND_P marker. */
609 bool
610 name_lookup::search_qualified (tree scope, bool usings)
612 bool found = false;
614 if (seen_p (scope))
615 found = found_p (scope);
616 else
618 found = search_namespace (scope);
619 if (!found && usings)
620 found = search_usings (scope);
623 return found;
626 /* Add SCOPE to the unqualified search queue, recursively add its
627 inlines and those via using directives. */
629 name_lookup::using_queue *
630 name_lookup::queue_namespace (using_queue *queue, int depth, tree scope)
632 if (see_and_mark (scope))
633 return queue;
635 /* Record it. */
636 tree common = scope;
637 while (SCOPE_DEPTH (common) > depth)
638 common = CP_DECL_CONTEXT (common);
639 vec_safe_push (queue, using_pair (common, scope));
641 /* Queue its inline children. */
642 if (vec<tree, va_gc> *inlinees = DECL_NAMESPACE_INLINEES (scope))
643 for (unsigned ix = inlinees->length (); ix--;)
644 queue = queue_namespace (queue, depth, (*inlinees)[ix]);
646 /* Queue its using targets. */
647 queue = queue_usings (queue, depth, DECL_NAMESPACE_USING (scope));
649 return queue;
652 /* Add the namespaces in USINGS to the unqualified search queue. */
654 name_lookup::using_queue *
655 name_lookup::do_queue_usings (using_queue *queue, int depth,
656 vec<tree, va_gc> *usings)
658 for (unsigned ix = usings->length (); ix--;)
659 queue = queue_namespace (queue, depth, (*usings)[ix]);
661 return queue;
664 /* Unqualified namespace lookup in SCOPE.
665 1) add scope+inlins to worklist.
666 2) recursively add target of every using directive
667 3) for each worklist item where SCOPE is common ancestor, search it
668 4) if nothing find, scope=parent, goto 1. */
670 bool
671 name_lookup::search_unqualified (tree scope, cp_binding_level *level)
673 /* Make static to avoid continual reallocation. We're not
674 recursive. */
675 static using_queue *queue = NULL;
676 bool found = false;
677 int length = vec_safe_length (queue);
679 /* Queue local using-directives. */
680 for (; level->kind != sk_namespace; level = level->level_chain)
681 queue = queue_usings (queue, SCOPE_DEPTH (scope), level->using_directives);
683 for (; !found; scope = CP_DECL_CONTEXT (scope))
685 gcc_assert (!DECL_NAMESPACE_ALIAS (scope));
686 int depth = SCOPE_DEPTH (scope);
688 /* Queue namespaces reachable from SCOPE. */
689 queue = queue_namespace (queue, depth, scope);
691 /* Search every queued namespace where SCOPE is the common
692 ancestor. Adjust the others. */
693 unsigned ix = length;
696 using_pair &pair = (*queue)[ix];
697 while (pair.first == scope)
699 found |= search_namespace_only (pair.second);
700 pair = queue->pop ();
701 if (ix == queue->length ())
702 goto done;
704 /* The depth is the same as SCOPE, find the parent scope. */
705 if (SCOPE_DEPTH (pair.first) == depth)
706 pair.first = CP_DECL_CONTEXT (pair.first);
707 ix++;
709 while (ix < queue->length ());
710 done:;
711 if (scope == global_namespace)
712 break;
715 vec_safe_truncate (queue, length);
717 return found;
720 /* FNS is a value binding. If it is a (set of overloaded) functions,
721 add them into the current value. */
723 void
724 name_lookup::add_fns (tree fns)
726 if (!fns)
727 return;
728 else if (TREE_CODE (fns) == OVERLOAD)
730 if (TREE_TYPE (fns) != unknown_type_node)
731 fns = OVL_FUNCTION (fns);
733 else if (!DECL_DECLARES_FUNCTION_P (fns))
734 return;
736 add_overload (fns);
739 /* Add functions of a namespace to the lookup structure. */
741 void
742 name_lookup::adl_namespace_only (tree scope)
744 mark_seen (scope);
746 /* Look down into inline namespaces. */
747 if (vec<tree, va_gc> *inlinees = DECL_NAMESPACE_INLINEES (scope))
748 for (unsigned ix = inlinees->length (); ix--;)
749 adl_namespace_only ((*inlinees)[ix]);
751 if (tree fns = find_namespace_value (scope, name))
752 add_fns (ovl_skip_hidden (fns));
755 /* Find the containing non-inlined namespace, add it and all its
756 inlinees. */
758 void
759 name_lookup::adl_namespace (tree scope)
761 if (seen_p (scope))
762 return;
764 /* Find the containing non-inline namespace. */
765 while (DECL_NAMESPACE_INLINE_P (scope))
766 scope = CP_DECL_CONTEXT (scope);
768 adl_namespace_only (scope);
771 /* Adds the class and its friends to the lookup structure. */
773 void
774 name_lookup::adl_class_only (tree type)
776 /* Backend-built structures, such as __builtin_va_list, aren't
777 affected by all this. */
778 if (!CLASS_TYPE_P (type))
779 return;
781 type = TYPE_MAIN_VARIANT (type);
783 if (see_and_mark (type))
784 return;
786 tree context = decl_namespace_context (type);
787 adl_namespace (context);
789 complete_type (type);
791 /* Add friends. */
792 for (tree list = DECL_FRIENDLIST (TYPE_MAIN_DECL (type)); list;
793 list = TREE_CHAIN (list))
794 if (name == FRIEND_NAME (list))
795 for (tree friends = FRIEND_DECLS (list); friends;
796 friends = TREE_CHAIN (friends))
798 tree fn = TREE_VALUE (friends);
800 /* Only interested in global functions with potentially hidden
801 (i.e. unqualified) declarations. */
802 if (CP_DECL_CONTEXT (fn) != context)
803 continue;
805 /* Only interested in anticipated friends. (Non-anticipated
806 ones will have been inserted during the namespace
807 adl.) */
808 if (!DECL_ANTICIPATED (fn))
809 continue;
811 /* Template specializations are never found by name lookup.
812 (Templates themselves can be found, but not template
813 specializations.) */
814 if (TREE_CODE (fn) == FUNCTION_DECL && DECL_USE_TEMPLATE (fn))
815 continue;
817 add_fns (fn);
821 /* Adds the class and its bases to the lookup structure.
822 Returns true on error. */
824 void
825 name_lookup::adl_bases (tree type)
827 adl_class_only (type);
829 /* Process baseclasses. */
830 if (tree binfo = TYPE_BINFO (type))
832 tree base_binfo;
833 int i;
835 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
836 adl_bases (BINFO_TYPE (base_binfo));
840 /* Adds everything associated with a class argument type to the lookup
841 structure. Returns true on error.
843 If T is a class type (including unions), its associated classes are: the
844 class itself; the class of which it is a member, if any; and its direct
845 and indirect base classes. Its associated namespaces are the namespaces
846 of which its associated classes are members. Furthermore, if T is a
847 class template specialization, its associated namespaces and classes
848 also include: the namespaces and classes associated with the types of
849 the template arguments provided for template type parameters (excluding
850 template template parameters); the namespaces of which any template
851 template arguments are members; and the classes of which any member
852 templates used as template template arguments are members. [ Note:
853 non-type template arguments do not contribute to the set of associated
854 namespaces. --end note] */
856 void
857 name_lookup::adl_class (tree type)
859 /* Backend build structures, such as __builtin_va_list, aren't
860 affected by all this. */
861 if (!CLASS_TYPE_P (type))
862 return;
864 type = TYPE_MAIN_VARIANT (type);
865 /* We don't set found here because we have to have set seen first,
866 which is done in the adl_bases walk. */
867 if (found_p (type))
868 return;
870 adl_bases (type);
871 mark_found (type);
873 if (TYPE_CLASS_SCOPE_P (type))
874 adl_class_only (TYPE_CONTEXT (type));
876 /* Process template arguments. */
877 if (CLASSTYPE_TEMPLATE_INFO (type)
878 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
880 tree list = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
881 for (int i = 0; i < TREE_VEC_LENGTH (list); ++i)
882 adl_template_arg (TREE_VEC_ELT (list, i));
886 void
887 name_lookup::adl_expr (tree expr)
889 if (!expr)
890 return;
892 gcc_assert (!TYPE_P (expr));
894 if (TREE_TYPE (expr) != unknown_type_node)
896 adl_type (TREE_TYPE (expr));
897 return;
900 if (TREE_CODE (expr) == ADDR_EXPR)
901 expr = TREE_OPERAND (expr, 0);
902 if (TREE_CODE (expr) == COMPONENT_REF
903 || TREE_CODE (expr) == OFFSET_REF)
904 expr = TREE_OPERAND (expr, 1);
905 expr = MAYBE_BASELINK_FUNCTIONS (expr);
907 if (OVL_P (expr))
908 for (lkp_iterator iter (expr); iter; ++iter)
909 adl_type (TREE_TYPE (*iter));
910 else if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
912 /* The working paper doesn't currently say how to handle
913 template-id arguments. The sensible thing would seem to be
914 to handle the list of template candidates like a normal
915 overload set, and handle the template arguments like we do
916 for class template specializations. */
918 /* First the templates. */
919 adl_expr (TREE_OPERAND (expr, 0));
921 /* Now the arguments. */
922 if (tree args = TREE_OPERAND (expr, 1))
923 for (int ix = TREE_VEC_LENGTH (args); ix--;)
924 adl_template_arg (TREE_VEC_ELT (args, ix));
928 void
929 name_lookup::adl_type (tree type)
931 if (!type)
932 return;
934 if (TYPE_PTRDATAMEM_P (type))
936 /* Pointer to member: associate class type and value type. */
937 adl_type (TYPE_PTRMEM_CLASS_TYPE (type));
938 adl_type (TYPE_PTRMEM_POINTED_TO_TYPE (type));
939 return;
942 switch (TREE_CODE (type))
944 case RECORD_TYPE:
945 if (TYPE_PTRMEMFUNC_P (type))
947 adl_type (TYPE_PTRMEMFUNC_FN_TYPE (type));
948 return;
950 /* FALLTHRU */
951 case UNION_TYPE:
952 adl_class (type);
953 return;
955 case METHOD_TYPE:
956 /* The basetype is referenced in the first arg type, so just
957 fall through. */
958 case FUNCTION_TYPE:
959 /* Associate the parameter types. */
960 for (tree args = TYPE_ARG_TYPES (type); args; args = TREE_CHAIN (args))
961 adl_type (TREE_VALUE (args));
962 /* FALLTHROUGH */
964 case POINTER_TYPE:
965 case REFERENCE_TYPE:
966 case ARRAY_TYPE:
967 adl_type (TREE_TYPE (type));
968 return;
970 case ENUMERAL_TYPE:
971 if (TYPE_CLASS_SCOPE_P (type))
972 adl_class_only (TYPE_CONTEXT (type));
973 adl_namespace (decl_namespace_context (type));
974 return;
976 case LANG_TYPE:
977 gcc_assert (type == unknown_type_node
978 || type == init_list_type_node);
979 return;
981 case TYPE_PACK_EXPANSION:
982 adl_type (PACK_EXPANSION_PATTERN (type));
983 return;
985 default:
986 break;
990 /* Adds everything associated with a template argument to the lookup
991 structure. */
993 void
994 name_lookup::adl_template_arg (tree arg)
996 /* [basic.lookup.koenig]
998 If T is a template-id, its associated namespaces and classes are
999 ... the namespaces and classes associated with the types of the
1000 template arguments provided for template type parameters
1001 (excluding template template parameters); the namespaces in which
1002 any template template arguments are defined; and the classes in
1003 which any member templates used as template template arguments
1004 are defined. [Note: non-type template arguments do not
1005 contribute to the set of associated namespaces. ] */
1007 /* Consider first template template arguments. */
1008 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
1009 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
1011 else if (TREE_CODE (arg) == TEMPLATE_DECL)
1013 tree ctx = CP_DECL_CONTEXT (arg);
1015 /* It's not a member template. */
1016 if (TREE_CODE (ctx) == NAMESPACE_DECL)
1017 adl_namespace (ctx);
1018 /* Otherwise, it must be member template. */
1019 else
1020 adl_class_only (ctx);
1022 /* It's an argument pack; handle it recursively. */
1023 else if (ARGUMENT_PACK_P (arg))
1025 tree args = ARGUMENT_PACK_ARGS (arg);
1026 int i, len = TREE_VEC_LENGTH (args);
1027 for (i = 0; i < len; ++i)
1028 adl_template_arg (TREE_VEC_ELT (args, i));
1030 /* It's not a template template argument, but it is a type template
1031 argument. */
1032 else if (TYPE_P (arg))
1033 adl_type (arg);
1036 /* Perform ADL lookup. FNS is the existing lookup result and ARGS are
1037 the call arguments. */
1039 tree
1040 name_lookup::search_adl (tree fns, vec<tree, va_gc> *args)
1042 if (fns)
1044 deduping = true;
1045 lookup_mark (fns, true);
1047 value = fns;
1049 unsigned ix;
1050 tree arg;
1052 FOR_EACH_VEC_ELT_REVERSE (*args, ix, arg)
1053 /* OMP reduction operators put an ADL-significant type as the
1054 first arg. */
1055 if (TYPE_P (arg))
1056 adl_type (arg);
1057 else
1058 adl_expr (arg);
1060 fns = value;
1062 return fns;
1065 static bool qualified_namespace_lookup (tree, name_lookup *);
1066 static void consider_binding_level (tree name,
1067 best_match <tree, const char *> &bm,
1068 cp_binding_level *lvl,
1069 bool look_within_fields,
1070 enum lookup_name_fuzzy_kind kind);
1071 static void diagnose_name_conflict (tree, tree);
1073 /* ADL lookup of NAME. FNS is the result of regular lookup, and we
1074 don't add duplicates to it. ARGS is the vector of call
1075 arguments (which will not be empty). */
1077 tree
1078 lookup_arg_dependent (tree name, tree fns, vec<tree, va_gc> *args)
1080 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
1081 name_lookup lookup (name);
1082 fns = lookup.search_adl (fns, args);
1083 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
1084 return fns;
1087 /* FNS is an overload set of conversion functions. Return the
1088 overloads converting to TYPE. */
1090 static tree
1091 extract_conversion_operator (tree fns, tree type)
1093 tree convs = NULL_TREE;
1094 tree tpls = NULL_TREE;
1096 for (ovl_iterator iter (fns); iter; ++iter)
1098 if (same_type_p (DECL_CONV_FN_TYPE (*iter), type))
1099 convs = lookup_add (*iter, convs);
1101 if (TREE_CODE (*iter) == TEMPLATE_DECL)
1102 tpls = lookup_add (*iter, tpls);
1105 if (!convs)
1106 convs = tpls;
1108 return convs;
1111 /* Binary search of (ordered) MEMBER_VEC for NAME. */
1113 static tree
1114 member_vec_binary_search (vec<tree, va_gc> *member_vec, tree name)
1116 for (unsigned lo = 0, hi = member_vec->length (); lo < hi;)
1118 unsigned mid = (lo + hi) / 2;
1119 tree binding = (*member_vec)[mid];
1120 tree binding_name = OVL_NAME (binding);
1122 if (binding_name > name)
1123 hi = mid;
1124 else if (binding_name < name)
1125 lo = mid + 1;
1126 else
1127 return binding;
1130 return NULL_TREE;
1133 /* Linear search of (unordered) MEMBER_VEC for NAME. */
1135 static tree
1136 member_vec_linear_search (vec<tree, va_gc> *member_vec, tree name)
1138 for (int ix = member_vec->length (); ix--;)
1139 /* We can get a NULL binding during insertion of a new method
1140 name, because the identifier_binding machinery performs a
1141 lookup. If we find such a NULL slot, that's the thing we were
1142 looking for, so we might as well bail out immediately. */
1143 if (tree binding = (*member_vec)[ix])
1145 if (OVL_NAME (binding) == name)
1146 return binding;
1148 else
1149 break;
1151 return NULL_TREE;
1154 /* Linear search of (partially ordered) fields of KLASS for NAME. */
1156 static tree
1157 fields_linear_search (tree klass, tree name, bool want_type)
1159 for (tree fields = TYPE_FIELDS (klass); fields; fields = DECL_CHAIN (fields))
1161 tree decl = fields;
1163 if (!want_type
1164 && TREE_CODE (decl) == FIELD_DECL
1165 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
1167 if (tree temp = search_anon_aggr (TREE_TYPE (decl), name))
1168 return temp;
1171 if (DECL_NAME (decl) != name)
1172 continue;
1174 if (TREE_CODE (decl) == USING_DECL)
1176 decl = strip_using_decl (decl);
1177 if (is_overloaded_fn (decl))
1178 continue;
1181 if (DECL_DECLARES_FUNCTION_P (decl))
1182 /* Functions are found separately. */
1183 continue;
1185 if (!want_type || DECL_DECLARES_TYPE_P (decl))
1186 return decl;
1189 return NULL_TREE;
1192 /* Look for NAME field inside of anonymous aggregate ANON. */
1194 tree
1195 search_anon_aggr (tree anon, tree name)
1197 gcc_assert (COMPLETE_TYPE_P (anon));
1198 tree ret;
1200 if (vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (anon))
1201 ret = member_vec_linear_search (member_vec, name);
1202 else
1203 ret = fields_linear_search (anon, name, false);
1205 if (ret)
1207 /* Anon members can only contain fields. */
1208 gcc_assert (!STAT_HACK_P (ret) && !DECL_DECLARES_TYPE_P (ret));
1209 return ret;
1211 return NULL_TREE;
1214 /* Look for NAME as an immediate member of KLASS (including
1215 anon-members or unscoped enum member). TYPE_OR_FNS is zero for
1216 regular search. >0 to get a type binding (if there is one) and <0
1217 if you want (just) the member function binding.
1219 Use this if you do not want lazy member creation. */
1221 tree
1222 get_class_binding_direct (tree klass, tree name, int type_or_fns)
1224 gcc_checking_assert (RECORD_OR_UNION_TYPE_P (klass));
1226 /* Conversion operators can only be found by the marker conversion
1227 operator name. */
1228 bool conv_op = IDENTIFIER_CONV_OP_P (name);
1229 tree lookup = conv_op ? conv_op_identifier : name;
1230 tree val = NULL_TREE;
1231 vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (klass);
1233 if (COMPLETE_TYPE_P (klass) && member_vec)
1235 val = member_vec_binary_search (member_vec, lookup);
1236 if (!val)
1238 else if (type_or_fns > 0)
1240 if (STAT_HACK_P (val))
1241 val = STAT_TYPE (val);
1242 else if (!DECL_DECLARES_TYPE_P (val))
1243 val = NULL_TREE;
1245 else if (STAT_HACK_P (val))
1246 val = STAT_DECL (val);
1248 if (val && TREE_CODE (val) == OVERLOAD
1249 && TREE_CODE (OVL_FUNCTION (val)) == USING_DECL)
1251 /* An overload with a dependent USING_DECL. Does the caller
1252 want the USING_DECL or the functions? */
1253 if (type_or_fns < 0)
1254 val = OVL_CHAIN (val);
1255 else
1256 val = OVL_FUNCTION (val);
1259 else
1261 if (member_vec && type_or_fns <= 0)
1262 val = member_vec_linear_search (member_vec, lookup);
1264 if (type_or_fns < 0)
1265 /* Don't bother looking for field. We don't want it. */;
1266 else if (!val || (TREE_CODE (val) == OVERLOAD && OVL_USING_P (val)))
1267 /* Dependent using declarations are a 'field', make sure we
1268 return that even if we saw an overload already. */
1269 if (tree field_val = fields_linear_search (klass, lookup,
1270 type_or_fns > 0))
1271 if (!val || TREE_CODE (field_val) == USING_DECL)
1272 val = field_val;
1275 /* Extract the conversion operators asked for, unless the general
1276 conversion operator was requested. */
1277 if (val && conv_op)
1279 gcc_checking_assert (OVL_FUNCTION (val) == conv_op_marker);
1280 val = OVL_CHAIN (val);
1281 if (tree type = TREE_TYPE (name))
1282 val = extract_conversion_operator (val, type);
1285 return val;
1288 /* Look for NAME's binding in exactly KLASS. See
1289 get_class_binding_direct for argument description. Does lazy
1290 special function creation as necessary. */
1292 tree
1293 get_class_binding (tree klass, tree name, int type_or_fns)
1295 klass = complete_type (klass);
1297 if (COMPLETE_TYPE_P (klass))
1299 /* Lazily declare functions, if we're going to search these. */
1300 if (IDENTIFIER_CTOR_P (name))
1302 if (CLASSTYPE_LAZY_DEFAULT_CTOR (klass))
1303 lazily_declare_fn (sfk_constructor, klass);
1304 if (CLASSTYPE_LAZY_COPY_CTOR (klass))
1305 lazily_declare_fn (sfk_copy_constructor, klass);
1306 if (CLASSTYPE_LAZY_MOVE_CTOR (klass))
1307 lazily_declare_fn (sfk_move_constructor, klass);
1309 else if (IDENTIFIER_DTOR_P (name))
1311 if (CLASSTYPE_LAZY_DESTRUCTOR (klass))
1312 lazily_declare_fn (sfk_destructor, klass);
1314 else if (name == assign_op_identifier)
1316 if (CLASSTYPE_LAZY_COPY_ASSIGN (klass))
1317 lazily_declare_fn (sfk_copy_assignment, klass);
1318 if (CLASSTYPE_LAZY_MOVE_ASSIGN (klass))
1319 lazily_declare_fn (sfk_move_assignment, klass);
1323 return get_class_binding_direct (klass, name, type_or_fns);
1326 /* Find the slot containing overloads called 'NAME'. If there is no
1327 such slot, create an empty one. KLASS might be complete at this
1328 point, in which case we need to preserve ordering. Deals with
1329 conv_op marker handling. */
1331 tree *
1332 get_member_slot (tree klass, tree name)
1334 bool complete_p = COMPLETE_TYPE_P (klass);
1336 vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (klass);
1337 if (!member_vec)
1339 vec_alloc (member_vec, 8);
1340 CLASSTYPE_MEMBER_VEC (klass) = member_vec;
1341 if (complete_p)
1343 /* If the class is complete but had no member_vec, we need
1344 to add the TYPE_FIELDS into it. We're also most likely
1345 to be adding ctors & dtors, so ask for 6 spare slots (the
1346 abstract cdtors and their clones). */
1347 set_class_bindings (klass, 6);
1348 member_vec = CLASSTYPE_MEMBER_VEC (klass);
1352 if (IDENTIFIER_CONV_OP_P (name))
1353 name = conv_op_identifier;
1355 unsigned ix, length = member_vec->length ();
1356 for (ix = 0; ix < length; ix++)
1358 tree *slot = &(*member_vec)[ix];
1359 tree fn_name = OVL_NAME (*slot);
1361 if (fn_name == name)
1363 /* If we found an existing slot, it must be a function set.
1364 Even with insertion after completion, because those only
1365 happen with artificial fns that have unspellable names.
1366 This means we do not have to deal with the stat hack
1367 either. */
1368 gcc_checking_assert (OVL_P (*slot));
1369 if (name == conv_op_identifier)
1371 gcc_checking_assert (OVL_FUNCTION (*slot) == conv_op_marker);
1372 /* Skip the conv-op marker. */
1373 slot = &OVL_CHAIN (*slot);
1375 return slot;
1378 if (complete_p && fn_name > name)
1379 break;
1382 /* No slot found. Create one at IX. We know in this case that our
1383 caller will succeed in adding the function. */
1384 if (complete_p)
1386 /* Do exact allocation when complete, as we don't expect to add
1387 many. */
1388 vec_safe_reserve_exact (member_vec, 1);
1389 member_vec->quick_insert (ix, NULL_TREE);
1391 else
1393 gcc_checking_assert (ix == length);
1394 vec_safe_push (member_vec, NULL_TREE);
1396 CLASSTYPE_MEMBER_VEC (klass) = member_vec;
1398 tree *slot = &(*member_vec)[ix];
1399 if (name == conv_op_identifier)
1401 /* Install the marker prefix. */
1402 *slot = ovl_make (conv_op_marker, NULL_TREE);
1403 slot = &OVL_CHAIN (*slot);
1406 return slot;
1409 /* Comparison function to compare two MEMBER_VEC entries by name.
1410 Because we can have duplicates during insertion of TYPE_FIELDS, we
1411 do extra checking so deduping doesn't have to deal with so many
1412 cases. */
1414 static int
1415 member_name_cmp (const void *a_p, const void *b_p)
1417 tree a = *(const tree *)a_p;
1418 tree b = *(const tree *)b_p;
1419 tree name_a = DECL_NAME (TREE_CODE (a) == OVERLOAD ? OVL_FUNCTION (a) : a);
1420 tree name_b = DECL_NAME (TREE_CODE (b) == OVERLOAD ? OVL_FUNCTION (b) : b);
1422 gcc_checking_assert (name_a && name_b);
1423 if (name_a != name_b)
1424 return name_a < name_b ? -1 : +1;
1426 if (name_a == conv_op_identifier)
1428 /* Strip the conv-op markers. */
1429 gcc_checking_assert (OVL_FUNCTION (a) == conv_op_marker
1430 && OVL_FUNCTION (b) == conv_op_marker);
1431 a = OVL_CHAIN (a);
1432 b = OVL_CHAIN (b);
1435 if (TREE_CODE (a) == OVERLOAD)
1436 a = OVL_FUNCTION (a);
1437 if (TREE_CODE (b) == OVERLOAD)
1438 b = OVL_FUNCTION (b);
1440 /* We're in STAT_HACK or USING_DECL territory (or possibly error-land). */
1441 if (TREE_CODE (a) != TREE_CODE (b))
1443 /* If one of them is a TYPE_DECL, it loses. */
1444 if (TREE_CODE (a) == TYPE_DECL)
1445 return +1;
1446 else if (TREE_CODE (b) == TYPE_DECL)
1447 return -1;
1449 /* If one of them is a USING_DECL, it loses. */
1450 if (TREE_CODE (a) == USING_DECL)
1451 return +1;
1452 else if (TREE_CODE (b) == USING_DECL)
1453 return -1;
1455 /* There are no other cases with different kinds of decls, as
1456 duplicate detection should have kicked in earlier. However,
1457 some erroneous cases get though. */
1458 gcc_assert (errorcount);
1461 /* Using source location would be the best thing here, but we can
1462 get identically-located decls in the following circumstances:
1464 1) duplicate artificial type-decls for the same type.
1466 2) pack expansions of using-decls.
1468 We should not be doing #1, but in either case it doesn't matter
1469 how we order these. Use UID as a proxy for source ordering, so
1470 that identically-located decls still have a well-defined stable
1471 ordering. */
1472 if (DECL_UID (a) != DECL_UID (b))
1473 return DECL_UID (a) < DECL_UID (b) ? -1 : +1;
1474 gcc_assert (a == b);
1475 return 0;
1478 static struct {
1479 gt_pointer_operator new_value;
1480 void *cookie;
1481 } resort_data;
1483 /* This routine compares two fields like member_name_cmp but using the
1484 pointer operator in resort_field_decl_data. We don't have to deal
1485 with duplicates here. */
1487 static int
1488 resort_member_name_cmp (const void *a_p, const void *b_p)
1490 tree a = *(const tree *)a_p;
1491 tree b = *(const tree *)b_p;
1492 tree name_a = OVL_NAME (a);
1493 tree name_b = OVL_NAME (b);
1495 resort_data.new_value (&name_a, resort_data.cookie);
1496 resort_data.new_value (&name_b, resort_data.cookie);
1498 gcc_checking_assert (name_a != name_b);
1500 return name_a < name_b ? -1 : +1;
1503 /* Resort CLASSTYPE_MEMBER_VEC because pointers have been reordered. */
1505 void
1506 resort_type_member_vec (void *obj, void */*orig_obj*/,
1507 gt_pointer_operator new_value, void* cookie)
1509 if (vec<tree, va_gc> *member_vec = (vec<tree, va_gc> *) obj)
1511 resort_data.new_value = new_value;
1512 resort_data.cookie = cookie;
1513 qsort (member_vec->address (), member_vec->length (),
1514 sizeof (tree), resort_member_name_cmp);
1518 /* Recursively count the number of fields in KLASS, including anonymous
1519 union members. */
1521 static unsigned
1522 count_class_fields (tree klass)
1524 unsigned n_fields = 0;
1526 for (tree fields = TYPE_FIELDS (klass); fields; fields = DECL_CHAIN (fields))
1527 if (DECL_DECLARES_FUNCTION_P (fields))
1528 /* Functions are dealt with separately. */;
1529 else if (TREE_CODE (fields) == FIELD_DECL
1530 && ANON_AGGR_TYPE_P (TREE_TYPE (fields)))
1531 n_fields += count_class_fields (TREE_TYPE (fields));
1532 else if (DECL_NAME (fields))
1533 n_fields += 1;
1535 return n_fields;
1538 /* Append all the nonfunction members fields of KLASS to MEMBER_VEC.
1539 Recurse for anonymous members. MEMBER_VEC must have space. */
1541 static void
1542 member_vec_append_class_fields (vec<tree, va_gc> *member_vec, tree klass)
1544 for (tree fields = TYPE_FIELDS (klass); fields; fields = DECL_CHAIN (fields))
1545 if (DECL_DECLARES_FUNCTION_P (fields))
1546 /* Functions are handled separately. */;
1547 else if (TREE_CODE (fields) == FIELD_DECL
1548 && ANON_AGGR_TYPE_P (TREE_TYPE (fields)))
1549 member_vec_append_class_fields (member_vec, TREE_TYPE (fields));
1550 else if (DECL_NAME (fields))
1552 tree field = fields;
1553 /* Mark a conv-op USING_DECL with the conv-op-marker. */
1554 if (TREE_CODE (field) == USING_DECL
1555 && IDENTIFIER_CONV_OP_P (DECL_NAME (field)))
1556 field = ovl_make (conv_op_marker, field);
1557 member_vec->quick_push (field);
1561 /* Append all of the enum values of ENUMTYPE to MEMBER_VEC.
1562 MEMBER_VEC must have space. */
1564 static void
1565 member_vec_append_enum_values (vec<tree, va_gc> *member_vec, tree enumtype)
1567 for (tree values = TYPE_VALUES (enumtype);
1568 values; values = TREE_CHAIN (values))
1569 member_vec->quick_push (TREE_VALUE (values));
1572 /* MEMBER_VEC has just had new DECLs added to it, but is sorted.
1573 DeDup adjacent DECLS of the same name. We already dealt with
1574 conflict resolution when adding the fields or methods themselves.
1575 There are three cases (which could all be combined):
1576 1) a TYPE_DECL and non TYPE_DECL. Deploy STAT_HACK as appropriate.
1577 2) a USING_DECL and an overload. If the USING_DECL is dependent,
1578 it wins. Otherwise the OVERLOAD does.
1579 3) two USING_DECLS. ...
1581 member_name_cmp will have ordered duplicates as
1582 <fns><using><type> */
1584 static void
1585 member_vec_dedup (vec<tree, va_gc> *member_vec)
1587 unsigned len = member_vec->length ();
1588 unsigned store = 0;
1590 tree current = (*member_vec)[0], name = OVL_NAME (current);
1591 tree next = NULL_TREE, next_name = NULL_TREE;
1592 for (unsigned jx, ix = 0; ix < len;
1593 ix = jx, current = next, name = next_name)
1595 tree to_type = NULL_TREE;
1596 tree to_using = NULL_TREE;
1597 tree marker = NULL_TREE;
1598 if (IDENTIFIER_CONV_OP_P (name))
1600 marker = current;
1601 current = OVL_CHAIN (current);
1602 name = DECL_NAME (OVL_FUNCTION (marker));
1603 gcc_checking_assert (name == conv_op_identifier);
1606 if (TREE_CODE (current) == USING_DECL)
1608 current = strip_using_decl (current);
1609 if (is_overloaded_fn (current))
1610 current = NULL_TREE;
1611 else if (TREE_CODE (current) == USING_DECL)
1613 to_using = current;
1614 current = NULL_TREE;
1618 if (current && DECL_DECLARES_TYPE_P (current))
1620 to_type = current;
1621 current = NULL_TREE;
1624 for (jx = ix + 1; jx < len; jx++)
1626 next = (*member_vec)[jx];
1627 next_name = OVL_NAME (next);
1628 if (next_name != name)
1629 break;
1631 if (marker)
1633 gcc_checking_assert (OVL_FUNCTION (marker)
1634 == OVL_FUNCTION (next));
1635 next = OVL_CHAIN (next);
1638 if (TREE_CODE (next) == USING_DECL)
1640 next = strip_using_decl (next);
1641 if (is_overloaded_fn (next))
1642 next = NULL_TREE;
1643 else if (TREE_CODE (next) == USING_DECL)
1645 to_using = next;
1646 next = NULL_TREE;
1650 if (next && DECL_DECLARES_TYPE_P (next))
1651 to_type = next;
1654 if (to_using)
1656 if (!current)
1657 current = to_using;
1658 else
1659 current = ovl_make (to_using, current);
1662 if (to_type)
1664 if (!current)
1665 current = to_type;
1666 else
1667 current = stat_hack (current, to_type);
1670 gcc_assert (current);
1671 if (marker)
1673 OVL_CHAIN (marker) = current;
1674 current = marker;
1676 (*member_vec)[store++] = current;
1679 while (store++ < len)
1680 member_vec->pop ();
1683 /* Add the non-function members to CLASSTYPE_MEMBER_VEC. If there is
1684 no existing MEMBER_VEC and fewer than 8 fields, do nothing. We
1685 know there must be at least 1 field -- the self-reference
1686 TYPE_DECL, except for anon aggregates, which will have at least
1687 one field. */
1689 void
1690 set_class_bindings (tree klass, unsigned extra)
1692 unsigned n_fields = count_class_fields (klass);
1693 vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (klass);
1695 if (member_vec || n_fields >= 8)
1697 /* Append the new fields. */
1698 vec_safe_reserve_exact (member_vec, extra + n_fields);
1699 member_vec_append_class_fields (member_vec, klass);
1702 if (member_vec)
1704 CLASSTYPE_MEMBER_VEC (klass) = member_vec;
1705 qsort (member_vec->address (), member_vec->length (),
1706 sizeof (tree), member_name_cmp);
1707 member_vec_dedup (member_vec);
1711 /* Insert lately defined enum ENUMTYPE into KLASS for the sorted case. */
1713 void
1714 insert_late_enum_def_bindings (tree klass, tree enumtype)
1716 int n_fields;
1717 vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (klass);
1719 /* The enum bindings will already be on the TYPE_FIELDS, so don't
1720 count them twice. */
1721 if (!member_vec)
1722 n_fields = count_class_fields (klass);
1723 else
1724 n_fields = list_length (TYPE_VALUES (enumtype));
1726 if (member_vec || n_fields >= 8)
1728 vec_safe_reserve_exact (member_vec, n_fields);
1729 if (CLASSTYPE_MEMBER_VEC (klass))
1730 member_vec_append_enum_values (member_vec, enumtype);
1731 else
1732 member_vec_append_class_fields (member_vec, klass);
1733 CLASSTYPE_MEMBER_VEC (klass) = member_vec;
1734 qsort (member_vec->address (), member_vec->length (),
1735 sizeof (tree), member_name_cmp);
1736 member_vec_dedup (member_vec);
1740 /* Compute the chain index of a binding_entry given the HASH value of its
1741 name and the total COUNT of chains. COUNT is assumed to be a power
1742 of 2. */
1744 #define ENTRY_INDEX(HASH, COUNT) (((HASH) >> 3) & ((COUNT) - 1))
1746 /* A free list of "binding_entry"s awaiting for re-use. */
1748 static GTY((deletable)) binding_entry free_binding_entry = NULL;
1750 /* The binding oracle; see cp-tree.h. */
1752 cp_binding_oracle_function *cp_binding_oracle;
1754 /* If we have a binding oracle, ask it for all namespace-scoped
1755 definitions of NAME. */
1757 static inline void
1758 query_oracle (tree name)
1760 if (!cp_binding_oracle)
1761 return;
1763 /* LOOKED_UP holds the set of identifiers that we have already
1764 looked up with the oracle. */
1765 static hash_set<tree> looked_up;
1766 if (looked_up.add (name))
1767 return;
1769 cp_binding_oracle (CP_ORACLE_IDENTIFIER, name);
1772 /* Create a binding_entry object for (NAME, TYPE). */
1774 static inline binding_entry
1775 binding_entry_make (tree name, tree type)
1777 binding_entry entry;
1779 if (free_binding_entry)
1781 entry = free_binding_entry;
1782 free_binding_entry = entry->chain;
1784 else
1785 entry = ggc_alloc<binding_entry_s> ();
1787 entry->name = name;
1788 entry->type = type;
1789 entry->chain = NULL;
1791 return entry;
1794 /* Put ENTRY back on the free list. */
1795 #if 0
1796 static inline void
1797 binding_entry_free (binding_entry entry)
1799 entry->name = NULL;
1800 entry->type = NULL;
1801 entry->chain = free_binding_entry;
1802 free_binding_entry = entry;
1804 #endif
1806 /* The datatype used to implement the mapping from names to types at
1807 a given scope. */
1808 struct GTY(()) binding_table_s {
1809 /* Array of chains of "binding_entry"s */
1810 binding_entry * GTY((length ("%h.chain_count"))) chain;
1812 /* The number of chains in this table. This is the length of the
1813 member "chain" considered as an array. */
1814 size_t chain_count;
1816 /* Number of "binding_entry"s in this table. */
1817 size_t entry_count;
1820 /* Construct TABLE with an initial CHAIN_COUNT. */
1822 static inline void
1823 binding_table_construct (binding_table table, size_t chain_count)
1825 table->chain_count = chain_count;
1826 table->entry_count = 0;
1827 table->chain = ggc_cleared_vec_alloc<binding_entry> (table->chain_count);
1830 /* Make TABLE's entries ready for reuse. */
1831 #if 0
1832 static void
1833 binding_table_free (binding_table table)
1835 size_t i;
1836 size_t count;
1838 if (table == NULL)
1839 return;
1841 for (i = 0, count = table->chain_count; i < count; ++i)
1843 binding_entry temp = table->chain[i];
1844 while (temp != NULL)
1846 binding_entry entry = temp;
1847 temp = entry->chain;
1848 binding_entry_free (entry);
1850 table->chain[i] = NULL;
1852 table->entry_count = 0;
1854 #endif
1856 /* Allocate a table with CHAIN_COUNT, assumed to be a power of two. */
1858 static inline binding_table
1859 binding_table_new (size_t chain_count)
1861 binding_table table = ggc_alloc<binding_table_s> ();
1862 table->chain = NULL;
1863 binding_table_construct (table, chain_count);
1864 return table;
1867 /* Expand TABLE to twice its current chain_count. */
1869 static void
1870 binding_table_expand (binding_table table)
1872 const size_t old_chain_count = table->chain_count;
1873 const size_t old_entry_count = table->entry_count;
1874 const size_t new_chain_count = 2 * old_chain_count;
1875 binding_entry *old_chains = table->chain;
1876 size_t i;
1878 binding_table_construct (table, new_chain_count);
1879 for (i = 0; i < old_chain_count; ++i)
1881 binding_entry entry = old_chains[i];
1882 for (; entry != NULL; entry = old_chains[i])
1884 const unsigned int hash = IDENTIFIER_HASH_VALUE (entry->name);
1885 const size_t j = ENTRY_INDEX (hash, new_chain_count);
1887 old_chains[i] = entry->chain;
1888 entry->chain = table->chain[j];
1889 table->chain[j] = entry;
1892 table->entry_count = old_entry_count;
1895 /* Insert a binding for NAME to TYPE into TABLE. */
1897 static void
1898 binding_table_insert (binding_table table, tree name, tree type)
1900 const unsigned int hash = IDENTIFIER_HASH_VALUE (name);
1901 const size_t i = ENTRY_INDEX (hash, table->chain_count);
1902 binding_entry entry = binding_entry_make (name, type);
1904 entry->chain = table->chain[i];
1905 table->chain[i] = entry;
1906 ++table->entry_count;
1908 if (3 * table->chain_count < 5 * table->entry_count)
1909 binding_table_expand (table);
1912 /* Return the binding_entry, if any, that maps NAME. */
1914 binding_entry
1915 binding_table_find (binding_table table, tree name)
1917 const unsigned int hash = IDENTIFIER_HASH_VALUE (name);
1918 binding_entry entry = table->chain[ENTRY_INDEX (hash, table->chain_count)];
1920 while (entry != NULL && entry->name != name)
1921 entry = entry->chain;
1923 return entry;
1926 /* Apply PROC -- with DATA -- to all entries in TABLE. */
1928 void
1929 binding_table_foreach (binding_table table, bt_foreach_proc proc, void *data)
1931 size_t chain_count;
1932 size_t i;
1934 if (!table)
1935 return;
1937 chain_count = table->chain_count;
1938 for (i = 0; i < chain_count; ++i)
1940 binding_entry entry = table->chain[i];
1941 for (; entry != NULL; entry = entry->chain)
1942 proc (entry, data);
1946 #ifndef ENABLE_SCOPE_CHECKING
1947 # define ENABLE_SCOPE_CHECKING 0
1948 #else
1949 # define ENABLE_SCOPE_CHECKING 1
1950 #endif
1952 /* A free list of "cxx_binding"s, connected by their PREVIOUS. */
1954 static GTY((deletable)) cxx_binding *free_bindings;
1956 /* Initialize VALUE and TYPE field for BINDING, and set the PREVIOUS
1957 field to NULL. */
1959 static inline void
1960 cxx_binding_init (cxx_binding *binding, tree value, tree type)
1962 binding->value = value;
1963 binding->type = type;
1964 binding->previous = NULL;
1967 /* (GC)-allocate a binding object with VALUE and TYPE member initialized. */
1969 static cxx_binding *
1970 cxx_binding_make (tree value, tree type)
1972 cxx_binding *binding;
1973 if (free_bindings)
1975 binding = free_bindings;
1976 free_bindings = binding->previous;
1978 else
1979 binding = ggc_alloc<cxx_binding> ();
1981 cxx_binding_init (binding, value, type);
1983 return binding;
1986 /* Put BINDING back on the free list. */
1988 static inline void
1989 cxx_binding_free (cxx_binding *binding)
1991 binding->scope = NULL;
1992 binding->previous = free_bindings;
1993 free_bindings = binding;
1996 /* Create a new binding for NAME (with the indicated VALUE and TYPE
1997 bindings) in the class scope indicated by SCOPE. */
1999 static cxx_binding *
2000 new_class_binding (tree name, tree value, tree type, cp_binding_level *scope)
2002 cp_class_binding cb = {cxx_binding_make (value, type), name};
2003 cxx_binding *binding = cb.base;
2004 vec_safe_push (scope->class_shadowed, cb);
2005 binding->scope = scope;
2006 return binding;
2009 /* Make DECL the innermost binding for ID. The LEVEL is the binding
2010 level at which this declaration is being bound. */
2012 void
2013 push_binding (tree id, tree decl, cp_binding_level* level)
2015 cxx_binding *binding;
2017 if (level != class_binding_level)
2019 binding = cxx_binding_make (decl, NULL_TREE);
2020 binding->scope = level;
2022 else
2023 binding = new_class_binding (id, decl, /*type=*/NULL_TREE, level);
2025 /* Now, fill in the binding information. */
2026 binding->previous = IDENTIFIER_BINDING (id);
2027 INHERITED_VALUE_BINDING_P (binding) = 0;
2028 LOCAL_BINDING_P (binding) = (level != class_binding_level);
2030 /* And put it on the front of the list of bindings for ID. */
2031 IDENTIFIER_BINDING (id) = binding;
2034 /* Remove the binding for DECL which should be the innermost binding
2035 for ID. */
2037 void
2038 pop_local_binding (tree id, tree decl)
2040 cxx_binding *binding;
2042 if (id == NULL_TREE)
2043 /* It's easiest to write the loops that call this function without
2044 checking whether or not the entities involved have names. We
2045 get here for such an entity. */
2046 return;
2048 /* Get the innermost binding for ID. */
2049 binding = IDENTIFIER_BINDING (id);
2051 /* The name should be bound. */
2052 gcc_assert (binding != NULL);
2054 /* The DECL will be either the ordinary binding or the type
2055 binding for this identifier. Remove that binding. */
2056 if (binding->value == decl)
2057 binding->value = NULL_TREE;
2058 else
2060 gcc_assert (binding->type == decl);
2061 binding->type = NULL_TREE;
2064 if (!binding->value && !binding->type)
2066 /* We're completely done with the innermost binding for this
2067 identifier. Unhook it from the list of bindings. */
2068 IDENTIFIER_BINDING (id) = binding->previous;
2070 /* Add it to the free list. */
2071 cxx_binding_free (binding);
2075 /* Remove the bindings for the decls of the current level and leave
2076 the current scope. */
2078 void
2079 pop_bindings_and_leave_scope (void)
2081 for (tree t = get_local_decls (); t; t = DECL_CHAIN (t))
2083 tree decl = TREE_CODE (t) == TREE_LIST ? TREE_VALUE (t) : t;
2084 tree name = OVL_NAME (decl);
2086 pop_local_binding (name, decl);
2089 leave_scope ();
2092 /* Strip non dependent using declarations. If DECL is dependent,
2093 surreptitiously create a typename_type and return it. */
2095 tree
2096 strip_using_decl (tree decl)
2098 if (decl == NULL_TREE)
2099 return NULL_TREE;
2101 while (TREE_CODE (decl) == USING_DECL && !DECL_DEPENDENT_P (decl))
2102 decl = USING_DECL_DECLS (decl);
2104 if (TREE_CODE (decl) == USING_DECL && DECL_DEPENDENT_P (decl)
2105 && USING_DECL_TYPENAME_P (decl))
2107 /* We have found a type introduced by a using
2108 declaration at class scope that refers to a dependent
2109 type.
2111 using typename :: [opt] nested-name-specifier unqualified-id ;
2113 decl = make_typename_type (TREE_TYPE (decl),
2114 DECL_NAME (decl),
2115 typename_type, tf_error);
2116 if (decl != error_mark_node)
2117 decl = TYPE_NAME (decl);
2120 return decl;
2123 /* Return true if OVL is an overload for an anticipated builtin. */
2125 static bool
2126 anticipated_builtin_p (tree ovl)
2128 if (TREE_CODE (ovl) != OVERLOAD)
2129 return false;
2131 if (!OVL_HIDDEN_P (ovl))
2132 return false;
2134 tree fn = OVL_FUNCTION (ovl);
2135 gcc_checking_assert (DECL_ANTICIPATED (fn));
2137 if (DECL_HIDDEN_FRIEND_P (fn))
2138 return false;
2140 return true;
2143 /* BINDING records an existing declaration for a name in the current scope.
2144 But, DECL is another declaration for that same identifier in the
2145 same scope. This is the `struct stat' hack whereby a non-typedef
2146 class name or enum-name can be bound at the same level as some other
2147 kind of entity.
2148 3.3.7/1
2150 A class name (9.1) or enumeration name (7.2) can be hidden by the
2151 name of an object, function, or enumerator declared in the same scope.
2152 If a class or enumeration name and an object, function, or enumerator
2153 are declared in the same scope (in any order) with the same name, the
2154 class or enumeration name is hidden wherever the object, function, or
2155 enumerator name is visible.
2157 It's the responsibility of the caller to check that
2158 inserting this name is valid here. Returns nonzero if the new binding
2159 was successful. */
2161 static bool
2162 supplement_binding_1 (cxx_binding *binding, tree decl)
2164 tree bval = binding->value;
2165 bool ok = true;
2166 tree target_bval = strip_using_decl (bval);
2167 tree target_decl = strip_using_decl (decl);
2169 if (TREE_CODE (target_decl) == TYPE_DECL && DECL_ARTIFICIAL (target_decl)
2170 && target_decl != target_bval
2171 && (TREE_CODE (target_bval) != TYPE_DECL
2172 /* We allow pushing an enum multiple times in a class
2173 template in order to handle late matching of underlying
2174 type on an opaque-enum-declaration followed by an
2175 enum-specifier. */
2176 || (processing_template_decl
2177 && TREE_CODE (TREE_TYPE (target_decl)) == ENUMERAL_TYPE
2178 && TREE_CODE (TREE_TYPE (target_bval)) == ENUMERAL_TYPE
2179 && (dependent_type_p (ENUM_UNDERLYING_TYPE
2180 (TREE_TYPE (target_decl)))
2181 || dependent_type_p (ENUM_UNDERLYING_TYPE
2182 (TREE_TYPE (target_bval)))))))
2183 /* The new name is the type name. */
2184 binding->type = decl;
2185 else if (/* TARGET_BVAL is null when push_class_level_binding moves
2186 an inherited type-binding out of the way to make room
2187 for a new value binding. */
2188 !target_bval
2189 /* TARGET_BVAL is error_mark_node when TARGET_DECL's name
2190 has been used in a non-class scope prior declaration.
2191 In that case, we should have already issued a
2192 diagnostic; for graceful error recovery purpose, pretend
2193 this was the intended declaration for that name. */
2194 || target_bval == error_mark_node
2195 /* If TARGET_BVAL is anticipated but has not yet been
2196 declared, pretend it is not there at all. */
2197 || anticipated_builtin_p (target_bval))
2198 binding->value = decl;
2199 else if (TREE_CODE (target_bval) == TYPE_DECL
2200 && DECL_ARTIFICIAL (target_bval)
2201 && target_decl != target_bval
2202 && (TREE_CODE (target_decl) != TYPE_DECL
2203 || same_type_p (TREE_TYPE (target_decl),
2204 TREE_TYPE (target_bval))))
2206 /* The old binding was a type name. It was placed in
2207 VALUE field because it was thought, at the point it was
2208 declared, to be the only entity with such a name. Move the
2209 type name into the type slot; it is now hidden by the new
2210 binding. */
2211 binding->type = bval;
2212 binding->value = decl;
2213 binding->value_is_inherited = false;
2215 else if (TREE_CODE (target_bval) == TYPE_DECL
2216 && TREE_CODE (target_decl) == TYPE_DECL
2217 && DECL_NAME (target_decl) == DECL_NAME (target_bval)
2218 && binding->scope->kind != sk_class
2219 && (same_type_p (TREE_TYPE (target_decl), TREE_TYPE (target_bval))
2220 /* If either type involves template parameters, we must
2221 wait until instantiation. */
2222 || uses_template_parms (TREE_TYPE (target_decl))
2223 || uses_template_parms (TREE_TYPE (target_bval))))
2224 /* We have two typedef-names, both naming the same type to have
2225 the same name. In general, this is OK because of:
2227 [dcl.typedef]
2229 In a given scope, a typedef specifier can be used to redefine
2230 the name of any type declared in that scope to refer to the
2231 type to which it already refers.
2233 However, in class scopes, this rule does not apply due to the
2234 stricter language in [class.mem] prohibiting redeclarations of
2235 members. */
2236 ok = false;
2237 /* There can be two block-scope declarations of the same variable,
2238 so long as they are `extern' declarations. However, there cannot
2239 be two declarations of the same static data member:
2241 [class.mem]
2243 A member shall not be declared twice in the
2244 member-specification. */
2245 else if (VAR_P (target_decl)
2246 && VAR_P (target_bval)
2247 && DECL_EXTERNAL (target_decl) && DECL_EXTERNAL (target_bval)
2248 && !DECL_CLASS_SCOPE_P (target_decl))
2250 duplicate_decls (decl, binding->value, /*newdecl_is_friend=*/false);
2251 ok = false;
2253 else if (TREE_CODE (decl) == NAMESPACE_DECL
2254 && TREE_CODE (bval) == NAMESPACE_DECL
2255 && DECL_NAMESPACE_ALIAS (decl)
2256 && DECL_NAMESPACE_ALIAS (bval)
2257 && ORIGINAL_NAMESPACE (bval) == ORIGINAL_NAMESPACE (decl))
2258 /* [namespace.alias]
2260 In a declarative region, a namespace-alias-definition can be
2261 used to redefine a namespace-alias declared in that declarative
2262 region to refer only to the namespace to which it already
2263 refers. */
2264 ok = false;
2265 else
2267 if (!error_operand_p (bval))
2268 diagnose_name_conflict (decl, bval);
2269 ok = false;
2272 return ok;
2275 /* Diagnose a name conflict between DECL and BVAL. */
2277 static void
2278 diagnose_name_conflict (tree decl, tree bval)
2280 if (TREE_CODE (decl) == TREE_CODE (bval)
2281 && TREE_CODE (decl) != NAMESPACE_DECL
2282 && !DECL_DECLARES_FUNCTION_P (decl)
2283 && (TREE_CODE (decl) != TYPE_DECL
2284 || DECL_ARTIFICIAL (decl) == DECL_ARTIFICIAL (bval))
2285 && CP_DECL_CONTEXT (decl) == CP_DECL_CONTEXT (bval))
2286 error ("redeclaration of %q#D", decl);
2287 else
2288 error ("%q#D conflicts with a previous declaration", decl);
2290 inform (location_of (bval), "previous declaration %q#D", bval);
2293 /* Wrapper for supplement_binding_1. */
2295 static bool
2296 supplement_binding (cxx_binding *binding, tree decl)
2298 bool ret;
2299 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
2300 ret = supplement_binding_1 (binding, decl);
2301 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
2302 return ret;
2305 /* Replace BINDING's current value on its scope's name list with
2306 NEWVAL. */
2308 static void
2309 update_local_overload (cxx_binding *binding, tree newval)
2311 tree *d;
2313 for (d = &binding->scope->names; ; d = &TREE_CHAIN (*d))
2314 if (*d == binding->value)
2316 /* Stitch new list node in. */
2317 *d = tree_cons (NULL_TREE, NULL_TREE, TREE_CHAIN (*d));
2318 break;
2320 else if (TREE_CODE (*d) == TREE_LIST && TREE_VALUE (*d) == binding->value)
2321 break;
2323 TREE_VALUE (*d) = newval;
2326 /* Compares the parameter-type-lists of ONE and TWO and
2327 returns false if they are different. If the DECLs are template
2328 functions, the return types and the template parameter lists are
2329 compared too (DR 565). */
2331 static bool
2332 matching_fn_p (tree one, tree two)
2334 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (one)),
2335 TYPE_ARG_TYPES (TREE_TYPE (two))))
2336 return false;
2338 if (TREE_CODE (one) == TEMPLATE_DECL
2339 && TREE_CODE (two) == TEMPLATE_DECL)
2341 /* Compare template parms. */
2342 if (!comp_template_parms (DECL_TEMPLATE_PARMS (one),
2343 DECL_TEMPLATE_PARMS (two)))
2344 return false;
2346 /* And return type. */
2347 if (!same_type_p (TREE_TYPE (TREE_TYPE (one)),
2348 TREE_TYPE (TREE_TYPE (two))))
2349 return false;
2352 return true;
2355 /* Push DECL into nonclass LEVEL BINDING or SLOT. OLD is the current
2356 binding value (possibly with anticipated builtins stripped).
2357 Diagnose conflicts and return updated decl. */
2359 static tree
2360 update_binding (cp_binding_level *level, cxx_binding *binding, tree *slot,
2361 tree old, tree decl, bool is_friend)
2363 tree to_val = decl;
2364 tree old_type = slot ? MAYBE_STAT_TYPE (*slot) : binding->type;
2365 tree to_type = old_type;
2367 gcc_assert (level->kind == sk_namespace ? !binding
2368 : level->kind != sk_class && !slot);
2369 if (old == error_mark_node)
2370 old = NULL_TREE;
2372 if (TREE_CODE (decl) == TYPE_DECL && DECL_ARTIFICIAL (decl))
2374 tree other = to_type;
2376 if (old && TREE_CODE (old) == TYPE_DECL && DECL_ARTIFICIAL (old))
2377 other = old;
2379 /* Pushing an artificial typedef. See if this matches either
2380 the type slot or the old value slot. */
2381 if (!other)
2383 else if (same_type_p (TREE_TYPE (other), TREE_TYPE (decl)))
2384 /* Two artificial decls to same type. Do nothing. */
2385 return other;
2386 else
2387 goto conflict;
2389 if (old)
2391 /* Slide decl into the type slot, keep old unaltered */
2392 to_type = decl;
2393 to_val = old;
2394 goto done;
2398 if (old && TREE_CODE (old) == TYPE_DECL && DECL_ARTIFICIAL (old))
2400 /* Slide old into the type slot. */
2401 to_type = old;
2402 old = NULL_TREE;
2405 if (DECL_DECLARES_FUNCTION_P (decl))
2407 if (!old)
2409 else if (OVL_P (old))
2411 for (ovl_iterator iter (old); iter; ++iter)
2413 tree fn = *iter;
2415 if (iter.using_p () && matching_fn_p (fn, decl))
2417 /* If a function declaration in namespace scope or
2418 block scope has the same name and the same
2419 parameter-type- list (8.3.5) as a function
2420 introduced by a using-declaration, and the
2421 declarations do not declare the same function,
2422 the program is ill-formed. [namespace.udecl]/14 */
2423 if (tree match = duplicate_decls (decl, fn, is_friend))
2424 return match;
2425 else
2426 /* FIXME: To preserve existing error behavior, we
2427 still push the decl. This might change. */
2428 diagnose_name_conflict (decl, fn);
2432 else
2433 goto conflict;
2435 if (to_type != old_type
2436 && warn_shadow
2437 && MAYBE_CLASS_TYPE_P (TREE_TYPE (to_type))
2438 && !(DECL_IN_SYSTEM_HEADER (decl)
2439 && DECL_IN_SYSTEM_HEADER (to_type)))
2440 warning (OPT_Wshadow, "%q#D hides constructor for %q#D",
2441 decl, to_type);
2443 to_val = ovl_insert (decl, old);
2445 else if (!old)
2447 else if (TREE_CODE (old) != TREE_CODE (decl))
2448 /* Different kinds of decls conflict. */
2449 goto conflict;
2450 else if (TREE_CODE (old) == TYPE_DECL)
2452 if (same_type_p (TREE_TYPE (old), TREE_TYPE (decl)))
2453 /* Two type decls to the same type. Do nothing. */
2454 return old;
2455 else
2456 goto conflict;
2458 else if (TREE_CODE (old) == NAMESPACE_DECL)
2460 /* Two maybe-aliased namespaces. If they're to the same target
2461 namespace, that's ok. */
2462 if (ORIGINAL_NAMESPACE (old) != ORIGINAL_NAMESPACE (decl))
2463 goto conflict;
2465 /* The new one must be an alias at this point. */
2466 gcc_assert (DECL_NAMESPACE_ALIAS (decl));
2467 return old;
2469 else if (TREE_CODE (old) == VAR_DECL)
2471 /* There can be two block-scope declarations of the same
2472 variable, so long as they are `extern' declarations. */
2473 if (!DECL_EXTERNAL (old) || !DECL_EXTERNAL (decl))
2474 goto conflict;
2475 else if (tree match = duplicate_decls (decl, old, false))
2476 return match;
2477 else
2478 goto conflict;
2480 else
2482 conflict:
2483 diagnose_name_conflict (decl, old);
2484 to_val = NULL_TREE;
2487 done:
2488 if (to_val)
2490 if (level->kind != sk_namespace
2491 && !to_type && binding->value && OVL_P (to_val))
2492 update_local_overload (binding, to_val);
2493 else
2495 tree to_add = to_val;
2497 if (level->kind == sk_namespace)
2498 to_add = decl;
2499 else if (to_type == decl)
2500 to_add = decl;
2501 else if (TREE_CODE (to_add) == OVERLOAD)
2502 to_add = build_tree_list (NULL_TREE, to_add);
2504 add_decl_to_level (level, to_add);
2507 if (slot)
2509 if (STAT_HACK_P (*slot))
2511 STAT_TYPE (*slot) = to_type;
2512 STAT_DECL (*slot) = to_val;
2514 else if (to_type)
2515 *slot = stat_hack (to_val, to_type);
2516 else
2517 *slot = to_val;
2519 else
2521 binding->type = to_type;
2522 binding->value = to_val;
2526 return decl;
2529 /* Table of identifiers to extern C declarations (or LISTS thereof). */
2531 static GTY(()) hash_table<named_decl_hash> *extern_c_decls;
2533 /* DECL has C linkage. If we have an existing instance, make sure the
2534 new one is compatible. Make sure it has the same exception
2535 specification [7.5, 7.6]. Add DECL to the map. */
2537 static void
2538 check_extern_c_conflict (tree decl)
2540 /* Ignore artificial or system header decls. */
2541 if (DECL_ARTIFICIAL (decl) || DECL_IN_SYSTEM_HEADER (decl))
2542 return;
2544 if (!extern_c_decls)
2545 extern_c_decls = hash_table<named_decl_hash>::create_ggc (127);
2547 tree *slot = extern_c_decls
2548 ->find_slot_with_hash (DECL_NAME (decl),
2549 IDENTIFIER_HASH_VALUE (DECL_NAME (decl)), INSERT);
2550 if (tree old = *slot)
2552 if (TREE_CODE (old) == OVERLOAD)
2553 old = OVL_FUNCTION (old);
2555 int mismatch = 0;
2556 if (DECL_CONTEXT (old) == DECL_CONTEXT (decl))
2557 ; /* If they're in the same context, we'll have already complained
2558 about a (possible) mismatch, when inserting the decl. */
2559 else if (!decls_match (decl, old))
2560 mismatch = 1;
2561 else if (TREE_CODE (decl) == FUNCTION_DECL
2562 && !comp_except_specs (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (old)),
2563 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl)),
2564 ce_normal))
2565 mismatch = -1;
2566 else if (DECL_ASSEMBLER_NAME_SET_P (old))
2567 SET_DECL_ASSEMBLER_NAME (decl, DECL_ASSEMBLER_NAME (old));
2569 if (mismatch)
2571 pedwarn (input_location, 0,
2572 "conflicting C language linkage declaration %q#D", decl);
2573 inform (DECL_SOURCE_LOCATION (old),
2574 "previous declaration %q#D", old);
2575 if (mismatch < 0)
2576 inform (input_location,
2577 "due to different exception specifications");
2579 else
2581 if (old == *slot)
2582 /* The hash table expects OVERLOADS, so construct one with
2583 OLD as both the function and the chain. This allocate
2584 an excess OVERLOAD node, but it's rare to have multiple
2585 extern "C" decls of the same name. And we save
2586 complicating the hash table logic (which is used
2587 elsewhere). */
2588 *slot = ovl_make (old, old);
2590 slot = &OVL_CHAIN (*slot);
2592 /* Chain it on for c_linkage_binding's use. */
2593 *slot = tree_cons (NULL_TREE, decl, *slot);
2596 else
2597 *slot = decl;
2600 /* Returns a list of C-linkage decls with the name NAME. Used in
2601 c-family/c-pragma.c to implement redefine_extname pragma. */
2603 tree
2604 c_linkage_bindings (tree name)
2606 if (extern_c_decls)
2607 if (tree *slot = extern_c_decls
2608 ->find_slot_with_hash (name, IDENTIFIER_HASH_VALUE (name), NO_INSERT))
2610 tree result = *slot;
2611 if (TREE_CODE (result) == OVERLOAD)
2612 result = OVL_CHAIN (result);
2613 return result;
2616 return NULL_TREE;
2619 /* DECL is being declared at a local scope. Emit suitable shadow
2620 warnings. */
2622 static void
2623 check_local_shadow (tree decl)
2625 /* Don't complain about the parms we push and then pop
2626 while tentatively parsing a function declarator. */
2627 if (TREE_CODE (decl) == PARM_DECL && !DECL_CONTEXT (decl))
2628 return;
2630 /* Inline decls shadow nothing. */
2631 if (DECL_FROM_INLINE (decl))
2632 return;
2634 /* External decls are something else. */
2635 if (DECL_EXTERNAL (decl))
2636 return;
2638 tree old = NULL_TREE;
2639 cp_binding_level *old_scope = NULL;
2640 if (cxx_binding *binding = outer_binding (DECL_NAME (decl), NULL, true))
2642 old = binding->value;
2643 old_scope = binding->scope;
2645 while (old && VAR_P (old) && DECL_DEAD_FOR_LOCAL (old))
2646 old = DECL_SHADOWED_FOR_VAR (old);
2648 tree shadowed = NULL_TREE;
2649 if (old
2650 && (TREE_CODE (old) == PARM_DECL
2651 || VAR_P (old)
2652 || (TREE_CODE (old) == TYPE_DECL
2653 && (!DECL_ARTIFICIAL (old)
2654 || TREE_CODE (decl) == TYPE_DECL)))
2655 && (!DECL_ARTIFICIAL (decl)
2656 || DECL_IMPLICIT_TYPEDEF_P (decl)
2657 || (VAR_P (decl) && DECL_ANON_UNION_VAR_P (decl))))
2659 /* DECL shadows a local thing possibly of interest. */
2661 /* Don't complain if it's from an enclosing function. */
2662 if (DECL_CONTEXT (old) == current_function_decl
2663 && TREE_CODE (decl) != PARM_DECL
2664 && TREE_CODE (old) == PARM_DECL)
2666 /* Go to where the parms should be and see if we find
2667 them there. */
2668 cp_binding_level *b = current_binding_level->level_chain;
2670 if (FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
2671 /* Skip the ctor/dtor cleanup level. */
2672 b = b->level_chain;
2674 /* ARM $8.3 */
2675 if (b->kind == sk_function_parms)
2677 error ("declaration of %q#D shadows a parameter", decl);
2678 return;
2682 /* The local structure or class can't use parameters of
2683 the containing function anyway. */
2684 if (DECL_CONTEXT (old) != current_function_decl)
2686 for (cp_binding_level *scope = current_binding_level;
2687 scope != old_scope; scope = scope->level_chain)
2688 if (scope->kind == sk_class
2689 && !LAMBDA_TYPE_P (scope->this_entity))
2690 return;
2692 /* Error if redeclaring a local declared in a
2693 init-statement or in the condition of an if or
2694 switch statement when the new declaration is in the
2695 outermost block of the controlled statement.
2696 Redeclaring a variable from a for or while condition is
2697 detected elsewhere. */
2698 else if (VAR_P (old)
2699 && old_scope == current_binding_level->level_chain
2700 && (old_scope->kind == sk_cond || old_scope->kind == sk_for))
2702 error ("redeclaration of %q#D", decl);
2703 inform (DECL_SOURCE_LOCATION (old),
2704 "%q#D previously declared here", old);
2705 return;
2707 /* C++11:
2708 3.3.3/3: The name declared in an exception-declaration (...)
2709 shall not be redeclared in the outermost block of the handler.
2710 3.3.3/2: A parameter name shall not be redeclared (...) in
2711 the outermost block of any handler associated with a
2712 function-try-block.
2713 3.4.1/15: The function parameter names shall not be redeclared
2714 in the exception-declaration nor in the outermost block of a
2715 handler for the function-try-block. */
2716 else if ((TREE_CODE (old) == VAR_DECL
2717 && old_scope == current_binding_level->level_chain
2718 && old_scope->kind == sk_catch)
2719 || (TREE_CODE (old) == PARM_DECL
2720 && (current_binding_level->kind == sk_catch
2721 || current_binding_level->level_chain->kind == sk_catch)
2722 && in_function_try_handler))
2724 if (permerror (input_location, "redeclaration of %q#D", decl))
2725 inform (DECL_SOURCE_LOCATION (old),
2726 "%q#D previously declared here", old);
2727 return;
2730 /* If '-Wshadow=compatible-local' is specified without other
2731 -Wshadow= flags, we will warn only when the type of the
2732 shadowing variable (DECL) can be converted to that of the
2733 shadowed parameter (OLD_LOCAL). The reason why we only check
2734 if DECL's type can be converted to OLD_LOCAL's type (but not the
2735 other way around) is because when users accidentally shadow a
2736 parameter, more than often they would use the variable
2737 thinking (mistakenly) it's still the parameter. It would be
2738 rare that users would use the variable in the place that
2739 expects the parameter but thinking it's a new decl. */
2741 enum opt_code warning_code;
2742 if (warn_shadow)
2743 warning_code = OPT_Wshadow;
2744 else if (warn_shadow_local)
2745 warning_code = OPT_Wshadow_local;
2746 else if (warn_shadow_compatible_local
2747 && (same_type_p (TREE_TYPE (old), TREE_TYPE (decl))
2748 || (!dependent_type_p (TREE_TYPE (decl))
2749 && !dependent_type_p (TREE_TYPE (old))
2750 && can_convert (TREE_TYPE (old), TREE_TYPE (decl),
2751 tf_none))))
2752 warning_code = OPT_Wshadow_compatible_local;
2753 else
2754 return;
2756 const char *msg;
2757 if (TREE_CODE (old) == PARM_DECL)
2758 msg = "declaration of %q#D shadows a parameter";
2759 else if (is_capture_proxy (old))
2760 msg = "declaration of %qD shadows a lambda capture";
2761 else
2762 msg = "declaration of %qD shadows a previous local";
2764 if (warning_at (input_location, warning_code, msg, decl))
2766 shadowed = old;
2767 goto inform_shadowed;
2769 return;
2772 if (!warn_shadow)
2773 return;
2775 /* Don't warn for artificial things that are not implicit typedefs. */
2776 if (DECL_ARTIFICIAL (decl) && !DECL_IMPLICIT_TYPEDEF_P (decl))
2777 return;
2779 if (nonlambda_method_basetype ())
2780 if (tree member = lookup_member (current_nonlambda_class_type (),
2781 DECL_NAME (decl), /*protect=*/0,
2782 /*want_type=*/false, tf_warning_or_error))
2784 member = MAYBE_BASELINK_FUNCTIONS (member);
2786 /* Warn if a variable shadows a non-function, or the variable
2787 is a function or a pointer-to-function. */
2788 if (!OVL_P (member)
2789 || TREE_CODE (decl) == FUNCTION_DECL
2790 || TYPE_PTRFN_P (TREE_TYPE (decl))
2791 || TYPE_PTRMEMFUNC_P (TREE_TYPE (decl)))
2793 if (warning_at (input_location, OPT_Wshadow,
2794 "declaration of %qD shadows a member of %qT",
2795 decl, current_nonlambda_class_type ())
2796 && DECL_P (member))
2798 shadowed = member;
2799 goto inform_shadowed;
2802 return;
2805 /* Now look for a namespace shadow. */
2806 old = find_namespace_value (current_namespace, DECL_NAME (decl));
2807 if (old
2808 && (VAR_P (old)
2809 || (TREE_CODE (old) == TYPE_DECL
2810 && (!DECL_ARTIFICIAL (old)
2811 || TREE_CODE (decl) == TYPE_DECL)))
2812 && !instantiating_current_function_p ())
2813 /* XXX shadow warnings in outer-more namespaces */
2815 if (warning_at (input_location, OPT_Wshadow,
2816 "declaration of %qD shadows a global declaration",
2817 decl))
2819 shadowed = old;
2820 goto inform_shadowed;
2822 return;
2825 return;
2827 inform_shadowed:
2828 inform (DECL_SOURCE_LOCATION (shadowed), "shadowed declaration is here");
2831 /* DECL is being pushed inside function CTX. Set its context, if
2832 needed. */
2834 static void
2835 set_decl_context_in_fn (tree ctx, tree decl)
2837 if (!DECL_CONTEXT (decl)
2838 /* A local declaration for a function doesn't constitute
2839 nesting. */
2840 && TREE_CODE (decl) != FUNCTION_DECL
2841 /* A local declaration for an `extern' variable is in the
2842 scope of the current namespace, not the current
2843 function. */
2844 && !(VAR_P (decl) && DECL_EXTERNAL (decl))
2845 /* When parsing the parameter list of a function declarator,
2846 don't set DECL_CONTEXT to an enclosing function. When we
2847 push the PARM_DECLs in order to process the function body,
2848 current_binding_level->this_entity will be set. */
2849 && !(TREE_CODE (decl) == PARM_DECL
2850 && current_binding_level->kind == sk_function_parms
2851 && current_binding_level->this_entity == NULL))
2852 DECL_CONTEXT (decl) = ctx;
2854 /* If this is the declaration for a namespace-scope function,
2855 but the declaration itself is in a local scope, mark the
2856 declaration. */
2857 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (decl))
2858 DECL_LOCAL_FUNCTION_P (decl) = 1;
2861 /* DECL is a local-scope decl with linkage. SHADOWED is true if the
2862 name is already bound at the current level.
2864 [basic.link] If there is a visible declaration of an entity with
2865 linkage having the same name and type, ignoring entities declared
2866 outside the innermost enclosing namespace scope, the block scope
2867 declaration declares that same entity and receives the linkage of
2868 the previous declaration.
2870 Also, make sure that this decl matches any existing external decl
2871 in the enclosing namespace. */
2873 static void
2874 set_local_extern_decl_linkage (tree decl, bool shadowed)
2876 tree ns_value = decl; /* Unique marker. */
2878 if (!shadowed)
2880 tree loc_value = innermost_non_namespace_value (DECL_NAME (decl));
2881 if (!loc_value)
2883 ns_value
2884 = find_namespace_value (current_namespace, DECL_NAME (decl));
2885 loc_value = ns_value;
2887 if (loc_value == error_mark_node)
2888 loc_value = NULL_TREE;
2890 for (ovl_iterator iter (loc_value); iter; ++iter)
2891 if (!iter.hidden_p ()
2892 && (TREE_STATIC (*iter) || DECL_EXTERNAL (*iter))
2893 && decls_match (*iter, decl))
2895 /* The standard only says that the local extern inherits
2896 linkage from the previous decl; in particular, default
2897 args are not shared. Add the decl into a hash table to
2898 make sure only the previous decl in this case is seen
2899 by the middle end. */
2900 struct cxx_int_tree_map *h;
2902 /* We inherit the outer decl's linkage. But we're a
2903 different decl. */
2904 TREE_PUBLIC (decl) = TREE_PUBLIC (*iter);
2906 if (cp_function_chain->extern_decl_map == NULL)
2907 cp_function_chain->extern_decl_map
2908 = hash_table<cxx_int_tree_map_hasher>::create_ggc (20);
2910 h = ggc_alloc<cxx_int_tree_map> ();
2911 h->uid = DECL_UID (decl);
2912 h->to = *iter;
2913 cxx_int_tree_map **loc = cp_function_chain->extern_decl_map
2914 ->find_slot (h, INSERT);
2915 *loc = h;
2916 break;
2920 if (TREE_PUBLIC (decl))
2922 /* DECL is externally visible. Make sure it matches a matching
2923 decl in the namespace scope. We only really need to check
2924 this when inserting the decl, not when we find an existing
2925 match in the current scope. However, in practice we're
2926 going to be inserting a new decl in the majority of cases --
2927 who writes multiple extern decls for the same thing in the
2928 same local scope? Doing it here often avoids a duplicate
2929 namespace lookup. */
2931 /* Avoid repeating a lookup. */
2932 if (ns_value == decl)
2933 ns_value = find_namespace_value (current_namespace, DECL_NAME (decl));
2935 if (ns_value == error_mark_node)
2936 ns_value = NULL_TREE;
2938 for (ovl_iterator iter (ns_value); iter; ++iter)
2940 tree other = *iter;
2942 if (!(TREE_PUBLIC (other) || DECL_EXTERNAL (other)))
2943 ; /* Not externally visible. */
2944 else if (DECL_EXTERN_C_P (decl) && DECL_EXTERN_C_P (other))
2945 ; /* Both are extern "C", we'll check via that mechanism. */
2946 else if (TREE_CODE (other) != TREE_CODE (decl)
2947 || ((VAR_P (decl) || matching_fn_p (other, decl))
2948 && !comptypes (TREE_TYPE (decl), TREE_TYPE (other),
2949 COMPARE_REDECLARATION)))
2951 if (permerror (DECL_SOURCE_LOCATION (decl),
2952 "local external declaration %q#D", decl))
2953 inform (DECL_SOURCE_LOCATION (other),
2954 "does not match previous declaration %q#D", other);
2955 break;
2961 /* Record DECL as belonging to the current lexical scope. Check for
2962 errors (such as an incompatible declaration for the same name
2963 already seen in the same scope). IS_FRIEND is true if DECL is
2964 declared as a friend.
2966 Returns either DECL or an old decl for the same name. If an old
2967 decl is returned, it may have been smashed to agree with what DECL
2968 says. */
2970 static tree
2971 do_pushdecl (tree decl, bool is_friend)
2973 if (decl == error_mark_node)
2974 return error_mark_node;
2976 if (!DECL_TEMPLATE_PARM_P (decl) && current_function_decl)
2977 set_decl_context_in_fn (current_function_decl, decl);
2979 /* The binding level we will be pushing into. During local class
2980 pushing, we want to push to the containing scope. */
2981 cp_binding_level *level = current_binding_level;
2982 while (level->kind == sk_class)
2983 level = level->level_chain;
2985 /* An anonymous namespace has a NULL DECL_NAME, but we still want to
2986 insert it. Other NULL-named decls, not so much. */
2987 tree name = DECL_NAME (decl);
2988 if (name || TREE_CODE (decl) == NAMESPACE_DECL)
2990 cxx_binding *binding = NULL; /* Local scope binding. */
2991 tree ns = NULL_TREE; /* Searched namespace. */
2992 tree *slot = NULL; /* Binding slot in namespace. */
2993 tree old = NULL_TREE;
2995 if (level->kind == sk_namespace)
2997 /* We look in the decl's namespace for an existing
2998 declaration, even though we push into the current
2999 namespace. */
3000 ns = (DECL_NAMESPACE_SCOPE_P (decl)
3001 ? CP_DECL_CONTEXT (decl) : current_namespace);
3002 /* Create the binding, if this is current namespace, because
3003 that's where we'll be pushing anyway. */
3004 slot = find_namespace_slot (ns, name, ns == current_namespace);
3005 if (slot)
3006 old = MAYBE_STAT_DECL (*slot);
3008 else
3010 binding = find_local_binding (level, name);
3011 if (binding)
3012 old = binding->value;
3015 if (current_function_decl && VAR_OR_FUNCTION_DECL_P (decl)
3016 && DECL_EXTERNAL (decl))
3017 set_local_extern_decl_linkage (decl, old != NULL_TREE);
3019 if (old == error_mark_node)
3020 old = NULL_TREE;
3022 for (ovl_iterator iter (old); iter; ++iter)
3023 if (iter.using_p ())
3024 ; /* Ignore using decls here. */
3025 else if (tree match = duplicate_decls (decl, *iter, is_friend))
3027 if (match == error_mark_node)
3029 else if (TREE_CODE (match) == TYPE_DECL)
3030 /* The IDENTIFIER will have the type referring to the
3031 now-smashed TYPE_DECL, because ...? Reset it. */
3032 SET_IDENTIFIER_TYPE_VALUE (name, TREE_TYPE (match));
3033 else if (iter.hidden_p () && !DECL_HIDDEN_P (match))
3035 /* Unhiding a previously hidden decl. */
3036 tree head = iter.reveal_node (old);
3037 if (head != old)
3039 if (!ns)
3041 update_local_overload (binding, head);
3042 binding->value = head;
3044 else if (STAT_HACK_P (*slot))
3045 STAT_DECL (*slot) = head;
3046 else
3047 *slot = head;
3049 if (DECL_EXTERN_C_P (match))
3050 /* We need to check and register the decl now. */
3051 check_extern_c_conflict (match);
3053 return match;
3056 /* We are pushing a new decl. */
3058 /* Skip a hidden builtin we failed to match already. There can
3059 only be one. */
3060 if (old && anticipated_builtin_p (old))
3061 old = OVL_CHAIN (old);
3063 check_template_shadow (decl);
3065 if (DECL_DECLARES_FUNCTION_P (decl))
3067 check_default_args (decl);
3069 if (is_friend)
3071 if (level->kind != sk_namespace)
3072 /* In a local class, a friend function declaration must
3073 find a matching decl in the innermost non-class scope.
3074 [class.friend/11] */
3075 error ("friend declaration %qD in local class without "
3076 "prior local declaration", decl);
3077 else if (!flag_friend_injection)
3078 /* Hide it from ordinary lookup. */
3079 DECL_ANTICIPATED (decl) = DECL_HIDDEN_FRIEND_P (decl) = true;
3083 if (level->kind != sk_namespace)
3085 check_local_shadow (decl);
3087 if (TREE_CODE (decl) == NAMESPACE_DECL)
3088 /* A local namespace alias. */
3089 set_identifier_type_value (name, NULL_TREE);
3091 if (!binding)
3092 binding = create_local_binding (level, name);
3094 else if (!slot)
3096 ns = current_namespace;
3097 slot = find_namespace_slot (ns, name, true);
3098 /* Update OLD to reflect the namespace we're going to be
3099 pushing into. */
3100 old = MAYBE_STAT_DECL (*slot);
3103 old = update_binding (level, binding, slot, old, decl, is_friend);
3105 if (old != decl)
3106 /* An existing decl matched, use it. */
3107 decl = old;
3108 else if (TREE_CODE (decl) == TYPE_DECL)
3110 tree type = TREE_TYPE (decl);
3112 if (type != error_mark_node)
3114 if (TYPE_NAME (type) != decl)
3115 set_underlying_type (decl);
3117 if (!ns)
3118 set_identifier_type_value_with_scope (name, decl, level);
3119 else
3120 SET_IDENTIFIER_TYPE_VALUE (name, global_type_node);
3123 /* If this is a locally defined typedef in a function that
3124 is not a template instantation, record it to implement
3125 -Wunused-local-typedefs. */
3126 if (!instantiating_current_function_p ())
3127 record_locally_defined_typedef (decl);
3129 else if (VAR_P (decl))
3130 maybe_register_incomplete_var (decl);
3132 if ((VAR_P (decl) || TREE_CODE (decl) == FUNCTION_DECL)
3133 && DECL_EXTERN_C_P (decl))
3134 check_extern_c_conflict (decl);
3136 else
3137 add_decl_to_level (level, decl);
3139 return decl;
3142 /* Record a decl-node X as belonging to the current lexical scope.
3143 It's a friend if IS_FRIEND is true -- which affects exactly where
3144 we push it. */
3146 tree
3147 pushdecl (tree x, bool is_friend)
3149 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3150 tree ret = do_pushdecl (x, is_friend);
3151 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3152 return ret;
3155 /* Enter DECL into the symbol table, if that's appropriate. Returns
3156 DECL, or a modified version thereof. */
3158 tree
3159 maybe_push_decl (tree decl)
3161 tree type = TREE_TYPE (decl);
3163 /* Add this decl to the current binding level, but not if it comes
3164 from another scope, e.g. a static member variable. TEM may equal
3165 DECL or it may be a previous decl of the same name. */
3166 if (decl == error_mark_node
3167 || (TREE_CODE (decl) != PARM_DECL
3168 && DECL_CONTEXT (decl) != NULL_TREE
3169 /* Definitions of namespace members outside their namespace are
3170 possible. */
3171 && !DECL_NAMESPACE_SCOPE_P (decl))
3172 || (TREE_CODE (decl) == TEMPLATE_DECL && !namespace_bindings_p ())
3173 || type == unknown_type_node
3174 /* The declaration of a template specialization does not affect
3175 the functions available for overload resolution, so we do not
3176 call pushdecl. */
3177 || (TREE_CODE (decl) == FUNCTION_DECL
3178 && DECL_TEMPLATE_SPECIALIZATION (decl)))
3179 return decl;
3180 else
3181 return pushdecl (decl);
3184 /* Bind DECL to ID in the current_binding_level, assumed to be a local
3185 binding level. If IS_USING is true, DECL got here through a
3186 using-declaration. */
3188 static void
3189 push_local_binding (tree id, tree decl, bool is_using)
3191 /* Skip over any local classes. This makes sense if we call
3192 push_local_binding with a friend decl of a local class. */
3193 cp_binding_level *b = innermost_nonclass_level ();
3195 gcc_assert (b->kind != sk_namespace);
3196 if (find_local_binding (b, id))
3198 /* Supplement the existing binding. */
3199 if (!supplement_binding (IDENTIFIER_BINDING (id), decl))
3200 /* It didn't work. Something else must be bound at this
3201 level. Do not add DECL to the list of things to pop
3202 later. */
3203 return;
3205 else
3206 /* Create a new binding. */
3207 push_binding (id, decl, b);
3209 if (TREE_CODE (decl) == OVERLOAD || is_using)
3210 /* We must put the OVERLOAD or using into a TREE_LIST since we
3211 cannot use the decl's chain itself. */
3212 decl = build_tree_list (NULL_TREE, decl);
3214 /* And put DECL on the list of things declared by the current
3215 binding level. */
3216 add_decl_to_level (b, decl);
3219 /* Check to see whether or not DECL is a variable that would have been
3220 in scope under the ARM, but is not in scope under the ANSI/ISO
3221 standard. If so, issue an error message. If name lookup would
3222 work in both cases, but return a different result, this function
3223 returns the result of ANSI/ISO lookup. Otherwise, it returns
3224 DECL. */
3226 tree
3227 check_for_out_of_scope_variable (tree decl)
3229 tree shadowed;
3231 /* We only care about out of scope variables. */
3232 if (!(VAR_P (decl) && DECL_DEAD_FOR_LOCAL (decl)))
3233 return decl;
3235 shadowed = DECL_HAS_SHADOWED_FOR_VAR_P (decl)
3236 ? DECL_SHADOWED_FOR_VAR (decl) : NULL_TREE ;
3237 while (shadowed != NULL_TREE && VAR_P (shadowed)
3238 && DECL_DEAD_FOR_LOCAL (shadowed))
3239 shadowed = DECL_HAS_SHADOWED_FOR_VAR_P (shadowed)
3240 ? DECL_SHADOWED_FOR_VAR (shadowed) : NULL_TREE;
3241 if (!shadowed)
3242 shadowed = find_namespace_value (current_namespace, DECL_NAME (decl));
3243 if (shadowed)
3245 if (!DECL_ERROR_REPORTED (decl))
3247 warning (0, "name lookup of %qD changed", DECL_NAME (decl));
3248 warning_at (DECL_SOURCE_LOCATION (shadowed), 0,
3249 " matches this %qD under ISO standard rules",
3250 shadowed);
3251 warning_at (DECL_SOURCE_LOCATION (decl), 0,
3252 " matches this %qD under old rules", decl);
3253 DECL_ERROR_REPORTED (decl) = 1;
3255 return shadowed;
3258 /* If we have already complained about this declaration, there's no
3259 need to do it again. */
3260 if (DECL_ERROR_REPORTED (decl))
3261 return decl;
3263 DECL_ERROR_REPORTED (decl) = 1;
3265 if (TREE_TYPE (decl) == error_mark_node)
3266 return decl;
3268 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
3270 error ("name lookup of %qD changed for ISO %<for%> scoping",
3271 DECL_NAME (decl));
3272 error (" cannot use obsolete binding at %q+D because "
3273 "it has a destructor", decl);
3274 return error_mark_node;
3276 else
3278 permerror (input_location, "name lookup of %qD changed for ISO %<for%> scoping",
3279 DECL_NAME (decl));
3280 if (flag_permissive)
3281 permerror (DECL_SOURCE_LOCATION (decl),
3282 " using obsolete binding at %qD", decl);
3283 else
3285 static bool hint;
3286 if (!hint)
3288 inform (input_location, "(if you use %<-fpermissive%> G++ will accept your code)");
3289 hint = true;
3294 return decl;
3297 /* true means unconditionally make a BLOCK for the next level pushed. */
3299 static bool keep_next_level_flag;
3301 static int binding_depth = 0;
3303 static void
3304 indent (int depth)
3306 int i;
3308 for (i = 0; i < depth * 2; i++)
3309 putc (' ', stderr);
3312 /* Return a string describing the kind of SCOPE we have. */
3313 static const char *
3314 cp_binding_level_descriptor (cp_binding_level *scope)
3316 /* The order of this table must match the "scope_kind"
3317 enumerators. */
3318 static const char* scope_kind_names[] = {
3319 "block-scope",
3320 "cleanup-scope",
3321 "try-scope",
3322 "catch-scope",
3323 "for-scope",
3324 "function-parameter-scope",
3325 "class-scope",
3326 "namespace-scope",
3327 "template-parameter-scope",
3328 "template-explicit-spec-scope"
3330 const scope_kind kind = scope->explicit_spec_p
3331 ? sk_template_spec : scope->kind;
3333 return scope_kind_names[kind];
3336 /* Output a debugging information about SCOPE when performing
3337 ACTION at LINE. */
3338 static void
3339 cp_binding_level_debug (cp_binding_level *scope, int line, const char *action)
3341 const char *desc = cp_binding_level_descriptor (scope);
3342 if (scope->this_entity)
3343 verbatim ("%s %<%s(%E)%> %p %d\n", action, desc,
3344 scope->this_entity, (void *) scope, line);
3345 else
3346 verbatim ("%s %s %p %d\n", action, desc, (void *) scope, line);
3349 /* Return the estimated initial size of the hashtable of a NAMESPACE
3350 scope. */
3352 static inline size_t
3353 namespace_scope_ht_size (tree ns)
3355 tree name = DECL_NAME (ns);
3357 return name == std_identifier
3358 ? NAMESPACE_STD_HT_SIZE
3359 : (name == global_identifier
3360 ? GLOBAL_SCOPE_HT_SIZE
3361 : NAMESPACE_ORDINARY_HT_SIZE);
3364 /* A chain of binding_level structures awaiting reuse. */
3366 static GTY((deletable)) cp_binding_level *free_binding_level;
3368 /* Insert SCOPE as the innermost binding level. */
3370 void
3371 push_binding_level (cp_binding_level *scope)
3373 /* Add it to the front of currently active scopes stack. */
3374 scope->level_chain = current_binding_level;
3375 current_binding_level = scope;
3376 keep_next_level_flag = false;
3378 if (ENABLE_SCOPE_CHECKING)
3380 scope->binding_depth = binding_depth;
3381 indent (binding_depth);
3382 cp_binding_level_debug (scope, LOCATION_LINE (input_location),
3383 "push");
3384 binding_depth++;
3388 /* Create a new KIND scope and make it the top of the active scopes stack.
3389 ENTITY is the scope of the associated C++ entity (namespace, class,
3390 function, C++0x enumeration); it is NULL otherwise. */
3392 cp_binding_level *
3393 begin_scope (scope_kind kind, tree entity)
3395 cp_binding_level *scope;
3397 /* Reuse or create a struct for this binding level. */
3398 if (!ENABLE_SCOPE_CHECKING && free_binding_level)
3400 scope = free_binding_level;
3401 free_binding_level = scope->level_chain;
3402 memset (scope, 0, sizeof (cp_binding_level));
3404 else
3405 scope = ggc_cleared_alloc<cp_binding_level> ();
3407 scope->this_entity = entity;
3408 scope->more_cleanups_ok = true;
3409 switch (kind)
3411 case sk_cleanup:
3412 scope->keep = true;
3413 break;
3415 case sk_template_spec:
3416 scope->explicit_spec_p = true;
3417 kind = sk_template_parms;
3418 /* Fall through. */
3419 case sk_template_parms:
3420 case sk_block:
3421 case sk_try:
3422 case sk_catch:
3423 case sk_for:
3424 case sk_cond:
3425 case sk_class:
3426 case sk_scoped_enum:
3427 case sk_function_parms:
3428 case sk_transaction:
3429 case sk_omp:
3430 scope->keep = keep_next_level_flag;
3431 break;
3433 case sk_namespace:
3434 NAMESPACE_LEVEL (entity) = scope;
3435 break;
3437 default:
3438 /* Should not happen. */
3439 gcc_unreachable ();
3440 break;
3442 scope->kind = kind;
3444 push_binding_level (scope);
3446 return scope;
3449 /* We're about to leave current scope. Pop the top of the stack of
3450 currently active scopes. Return the enclosing scope, now active. */
3452 cp_binding_level *
3453 leave_scope (void)
3455 cp_binding_level *scope = current_binding_level;
3457 if (scope->kind == sk_namespace && class_binding_level)
3458 current_binding_level = class_binding_level;
3460 /* We cannot leave a scope, if there are none left. */
3461 if (NAMESPACE_LEVEL (global_namespace))
3462 gcc_assert (!global_scope_p (scope));
3464 if (ENABLE_SCOPE_CHECKING)
3466 indent (--binding_depth);
3467 cp_binding_level_debug (scope, LOCATION_LINE (input_location),
3468 "leave");
3471 /* Move one nesting level up. */
3472 current_binding_level = scope->level_chain;
3474 /* Namespace-scopes are left most probably temporarily, not
3475 completely; they can be reopened later, e.g. in namespace-extension
3476 or any name binding activity that requires us to resume a
3477 namespace. For classes, we cache some binding levels. For other
3478 scopes, we just make the structure available for reuse. */
3479 if (scope->kind != sk_namespace
3480 && scope->kind != sk_class)
3482 scope->level_chain = free_binding_level;
3483 gcc_assert (!ENABLE_SCOPE_CHECKING
3484 || scope->binding_depth == binding_depth);
3485 free_binding_level = scope;
3488 if (scope->kind == sk_class)
3490 /* Reset DEFINING_CLASS_P to allow for reuse of a
3491 class-defining scope in a non-defining context. */
3492 scope->defining_class_p = 0;
3494 /* Find the innermost enclosing class scope, and reset
3495 CLASS_BINDING_LEVEL appropriately. */
3496 class_binding_level = NULL;
3497 for (scope = current_binding_level; scope; scope = scope->level_chain)
3498 if (scope->kind == sk_class)
3500 class_binding_level = scope;
3501 break;
3505 return current_binding_level;
3508 static void
3509 resume_scope (cp_binding_level* b)
3511 /* Resuming binding levels is meant only for namespaces,
3512 and those cannot nest into classes. */
3513 gcc_assert (!class_binding_level);
3514 /* Also, resuming a non-directly nested namespace is a no-no. */
3515 gcc_assert (b->level_chain == current_binding_level);
3516 current_binding_level = b;
3517 if (ENABLE_SCOPE_CHECKING)
3519 b->binding_depth = binding_depth;
3520 indent (binding_depth);
3521 cp_binding_level_debug (b, LOCATION_LINE (input_location), "resume");
3522 binding_depth++;
3526 /* Return the innermost binding level that is not for a class scope. */
3528 static cp_binding_level *
3529 innermost_nonclass_level (void)
3531 cp_binding_level *b;
3533 b = current_binding_level;
3534 while (b->kind == sk_class)
3535 b = b->level_chain;
3537 return b;
3540 /* We're defining an object of type TYPE. If it needs a cleanup, but
3541 we're not allowed to add any more objects with cleanups to the current
3542 scope, create a new binding level. */
3544 void
3545 maybe_push_cleanup_level (tree type)
3547 if (type != error_mark_node
3548 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
3549 && current_binding_level->more_cleanups_ok == 0)
3551 begin_scope (sk_cleanup, NULL);
3552 current_binding_level->statement_list = push_stmt_list ();
3556 /* Return true if we are in the global binding level. */
3558 bool
3559 global_bindings_p (void)
3561 return global_scope_p (current_binding_level);
3564 /* True if we are currently in a toplevel binding level. This
3565 means either the global binding level or a namespace in a toplevel
3566 binding level. Since there are no non-toplevel namespace levels,
3567 this really means any namespace or template parameter level. We
3568 also include a class whose context is toplevel. */
3570 bool
3571 toplevel_bindings_p (void)
3573 cp_binding_level *b = innermost_nonclass_level ();
3575 return b->kind == sk_namespace || b->kind == sk_template_parms;
3578 /* True if this is a namespace scope, or if we are defining a class
3579 which is itself at namespace scope, or whose enclosing class is
3580 such a class, etc. */
3582 bool
3583 namespace_bindings_p (void)
3585 cp_binding_level *b = innermost_nonclass_level ();
3587 return b->kind == sk_namespace;
3590 /* True if the innermost non-class scope is a block scope. */
3592 bool
3593 local_bindings_p (void)
3595 cp_binding_level *b = innermost_nonclass_level ();
3596 return b->kind < sk_function_parms || b->kind == sk_omp;
3599 /* True if the current level needs to have a BLOCK made. */
3601 bool
3602 kept_level_p (void)
3604 return (current_binding_level->blocks != NULL_TREE
3605 || current_binding_level->keep
3606 || current_binding_level->kind == sk_cleanup
3607 || current_binding_level->names != NULL_TREE
3608 || current_binding_level->using_directives);
3611 /* Returns the kind of the innermost scope. */
3613 scope_kind
3614 innermost_scope_kind (void)
3616 return current_binding_level->kind;
3619 /* Returns true if this scope was created to store template parameters. */
3621 bool
3622 template_parm_scope_p (void)
3624 return innermost_scope_kind () == sk_template_parms;
3627 /* If KEEP is true, make a BLOCK node for the next binding level,
3628 unconditionally. Otherwise, use the normal logic to decide whether
3629 or not to create a BLOCK. */
3631 void
3632 keep_next_level (bool keep)
3634 keep_next_level_flag = keep;
3637 /* Return the list of declarations of the current local scope. */
3639 tree
3640 get_local_decls (void)
3642 gcc_assert (current_binding_level->kind != sk_namespace
3643 && current_binding_level->kind != sk_class);
3644 return current_binding_level->names;
3647 /* Return how many function prototypes we are currently nested inside. */
3650 function_parm_depth (void)
3652 int level = 0;
3653 cp_binding_level *b;
3655 for (b = current_binding_level;
3656 b->kind == sk_function_parms;
3657 b = b->level_chain)
3658 ++level;
3660 return level;
3663 /* For debugging. */
3664 static int no_print_functions = 0;
3665 static int no_print_builtins = 0;
3667 static void
3668 print_binding_level (cp_binding_level* lvl)
3670 tree t;
3671 int i = 0, len;
3672 fprintf (stderr, " blocks=%p", (void *) lvl->blocks);
3673 if (lvl->more_cleanups_ok)
3674 fprintf (stderr, " more-cleanups-ok");
3675 if (lvl->have_cleanups)
3676 fprintf (stderr, " have-cleanups");
3677 fprintf (stderr, "\n");
3678 if (lvl->names)
3680 fprintf (stderr, " names:\t");
3681 /* We can probably fit 3 names to a line? */
3682 for (t = lvl->names; t; t = TREE_CHAIN (t))
3684 if (no_print_functions && (TREE_CODE (t) == FUNCTION_DECL))
3685 continue;
3686 if (no_print_builtins
3687 && (TREE_CODE (t) == TYPE_DECL)
3688 && DECL_IS_BUILTIN (t))
3689 continue;
3691 /* Function decls tend to have longer names. */
3692 if (TREE_CODE (t) == FUNCTION_DECL)
3693 len = 3;
3694 else
3695 len = 2;
3696 i += len;
3697 if (i > 6)
3699 fprintf (stderr, "\n\t");
3700 i = len;
3702 print_node_brief (stderr, "", t, 0);
3703 if (t == error_mark_node)
3704 break;
3706 if (i)
3707 fprintf (stderr, "\n");
3709 if (vec_safe_length (lvl->class_shadowed))
3711 size_t i;
3712 cp_class_binding *b;
3713 fprintf (stderr, " class-shadowed:");
3714 FOR_EACH_VEC_ELT (*lvl->class_shadowed, i, b)
3715 fprintf (stderr, " %s ", IDENTIFIER_POINTER (b->identifier));
3716 fprintf (stderr, "\n");
3718 if (lvl->type_shadowed)
3720 fprintf (stderr, " type-shadowed:");
3721 for (t = lvl->type_shadowed; t; t = TREE_CHAIN (t))
3723 fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
3725 fprintf (stderr, "\n");
3729 DEBUG_FUNCTION void
3730 debug (cp_binding_level &ref)
3732 print_binding_level (&ref);
3735 DEBUG_FUNCTION void
3736 debug (cp_binding_level *ptr)
3738 if (ptr)
3739 debug (*ptr);
3740 else
3741 fprintf (stderr, "<nil>\n");
3745 void
3746 print_other_binding_stack (cp_binding_level *stack)
3748 cp_binding_level *level;
3749 for (level = stack; !global_scope_p (level); level = level->level_chain)
3751 fprintf (stderr, "binding level %p\n", (void *) level);
3752 print_binding_level (level);
3756 void
3757 print_binding_stack (void)
3759 cp_binding_level *b;
3760 fprintf (stderr, "current_binding_level=%p\n"
3761 "class_binding_level=%p\n"
3762 "NAMESPACE_LEVEL (global_namespace)=%p\n",
3763 (void *) current_binding_level, (void *) class_binding_level,
3764 (void *) NAMESPACE_LEVEL (global_namespace));
3765 if (class_binding_level)
3767 for (b = class_binding_level; b; b = b->level_chain)
3768 if (b == current_binding_level)
3769 break;
3770 if (b)
3771 b = class_binding_level;
3772 else
3773 b = current_binding_level;
3775 else
3776 b = current_binding_level;
3777 print_other_binding_stack (b);
3778 fprintf (stderr, "global:\n");
3779 print_binding_level (NAMESPACE_LEVEL (global_namespace));
3782 /* Return the type associated with ID. */
3784 static tree
3785 identifier_type_value_1 (tree id)
3787 /* There is no type with that name, anywhere. */
3788 if (REAL_IDENTIFIER_TYPE_VALUE (id) == NULL_TREE)
3789 return NULL_TREE;
3790 /* This is not the type marker, but the real thing. */
3791 if (REAL_IDENTIFIER_TYPE_VALUE (id) != global_type_node)
3792 return REAL_IDENTIFIER_TYPE_VALUE (id);
3793 /* Have to search for it. It must be on the global level, now.
3794 Ask lookup_name not to return non-types. */
3795 id = lookup_name_real (id, 2, 1, /*block_p=*/true, 0, 0);
3796 if (id)
3797 return TREE_TYPE (id);
3798 return NULL_TREE;
3801 /* Wrapper for identifier_type_value_1. */
3803 tree
3804 identifier_type_value (tree id)
3806 tree ret;
3807 timevar_start (TV_NAME_LOOKUP);
3808 ret = identifier_type_value_1 (id);
3809 timevar_stop (TV_NAME_LOOKUP);
3810 return ret;
3813 /* Push a definition of struct, union or enum tag named ID. into
3814 binding_level B. DECL is a TYPE_DECL for the type. We assume that
3815 the tag ID is not already defined. */
3817 static void
3818 set_identifier_type_value_with_scope (tree id, tree decl, cp_binding_level *b)
3820 tree type;
3822 if (b->kind != sk_namespace)
3824 /* Shadow the marker, not the real thing, so that the marker
3825 gets restored later. */
3826 tree old_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
3827 b->type_shadowed
3828 = tree_cons (id, old_type_value, b->type_shadowed);
3829 type = decl ? TREE_TYPE (decl) : NULL_TREE;
3830 TREE_TYPE (b->type_shadowed) = type;
3832 else
3834 tree *slot = find_namespace_slot (current_namespace, id, true);
3835 gcc_assert (decl);
3836 update_binding (b, NULL, slot, MAYBE_STAT_DECL (*slot), decl, false);
3838 /* Store marker instead of real type. */
3839 type = global_type_node;
3841 SET_IDENTIFIER_TYPE_VALUE (id, type);
3844 /* As set_identifier_type_value_with_scope, but using
3845 current_binding_level. */
3847 void
3848 set_identifier_type_value (tree id, tree decl)
3850 set_identifier_type_value_with_scope (id, decl, current_binding_level);
3853 /* Return the name for the constructor (or destructor) for the
3854 specified class. */
3856 tree
3857 constructor_name (tree type)
3859 tree decl = TYPE_NAME (TYPE_MAIN_VARIANT (type));
3861 return decl ? DECL_NAME (decl) : NULL_TREE;
3864 /* Returns TRUE if NAME is the name for the constructor for TYPE,
3865 which must be a class type. */
3867 bool
3868 constructor_name_p (tree name, tree type)
3870 gcc_assert (MAYBE_CLASS_TYPE_P (type));
3872 /* These don't have names. */
3873 if (TREE_CODE (type) == DECLTYPE_TYPE
3874 || TREE_CODE (type) == TYPEOF_TYPE)
3875 return false;
3877 if (name && name == constructor_name (type))
3878 return true;
3880 return false;
3883 /* Counter used to create anonymous type names. */
3885 static GTY(()) int anon_cnt;
3887 /* Return an IDENTIFIER which can be used as a name for
3888 unnamed structs and unions. */
3890 tree
3891 make_anon_name (void)
3893 char buf[32];
3895 sprintf (buf, anon_aggrname_format (), anon_cnt++);
3896 return get_identifier (buf);
3899 /* This code is practically identical to that for creating
3900 anonymous names, but is just used for lambdas instead. This isn't really
3901 necessary, but it's convenient to avoid treating lambdas like other
3902 unnamed types. */
3904 static GTY(()) int lambda_cnt = 0;
3906 tree
3907 make_lambda_name (void)
3909 char buf[32];
3911 sprintf (buf, LAMBDANAME_FORMAT, lambda_cnt++);
3912 return get_identifier (buf);
3915 /* Insert another USING_DECL into the current binding level, returning
3916 this declaration. If this is a redeclaration, do nothing, and
3917 return NULL_TREE if this not in namespace scope (in namespace
3918 scope, a using decl might extend any previous bindings). */
3920 static tree
3921 push_using_decl_1 (tree scope, tree name)
3923 tree decl;
3925 gcc_assert (TREE_CODE (scope) == NAMESPACE_DECL);
3926 gcc_assert (identifier_p (name));
3927 for (decl = current_binding_level->usings; decl; decl = DECL_CHAIN (decl))
3928 if (USING_DECL_SCOPE (decl) == scope && DECL_NAME (decl) == name)
3929 break;
3930 if (decl)
3931 return namespace_bindings_p () ? decl : NULL_TREE;
3932 decl = build_lang_decl (USING_DECL, name, NULL_TREE);
3933 USING_DECL_SCOPE (decl) = scope;
3934 DECL_CHAIN (decl) = current_binding_level->usings;
3935 current_binding_level->usings = decl;
3936 return decl;
3939 /* Wrapper for push_using_decl_1. */
3941 static tree
3942 push_using_decl (tree scope, tree name)
3944 tree ret;
3945 timevar_start (TV_NAME_LOOKUP);
3946 ret = push_using_decl_1 (scope, name);
3947 timevar_stop (TV_NAME_LOOKUP);
3948 return ret;
3951 /* Same as pushdecl, but define X in binding-level LEVEL. We rely on the
3952 caller to set DECL_CONTEXT properly.
3954 Note that this must only be used when X will be the new innermost
3955 binding for its name, as we tack it onto the front of IDENTIFIER_BINDING
3956 without checking to see if the current IDENTIFIER_BINDING comes from a
3957 closer binding level than LEVEL. */
3959 static tree
3960 do_pushdecl_with_scope (tree x, cp_binding_level *level, bool is_friend)
3962 cp_binding_level *b;
3963 tree function_decl = current_function_decl;
3965 current_function_decl = NULL_TREE;
3966 if (level->kind == sk_class)
3968 b = class_binding_level;
3969 class_binding_level = level;
3970 pushdecl_class_level (x);
3971 class_binding_level = b;
3973 else
3975 b = current_binding_level;
3976 current_binding_level = level;
3977 x = pushdecl (x, is_friend);
3978 current_binding_level = b;
3980 current_function_decl = function_decl;
3981 return x;
3984 /* Inject X into the local scope just before the function parms. */
3986 tree
3987 pushdecl_outermost_localscope (tree x)
3989 cp_binding_level *b = NULL;
3990 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3992 /* Find the scope just inside the function parms. */
3993 for (cp_binding_level *n = current_binding_level;
3994 n->kind != sk_function_parms; n = b->level_chain)
3995 b = n;
3997 tree ret = b ? do_pushdecl_with_scope (x, b, false) : error_mark_node;
3998 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4000 return ret;
4003 /* Check a non-member using-declaration. Return the name and scope
4004 being used, and the USING_DECL, or NULL_TREE on failure. */
4006 static tree
4007 validate_nonmember_using_decl (tree decl, tree scope, tree name)
4009 /* [namespace.udecl]
4010 A using-declaration for a class member shall be a
4011 member-declaration. */
4012 if (TYPE_P (scope))
4014 error ("%qT is not a namespace or unscoped enum", scope);
4015 return NULL_TREE;
4017 else if (scope == error_mark_node)
4018 return NULL_TREE;
4020 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR)
4022 /* 7.3.3/5
4023 A using-declaration shall not name a template-id. */
4024 error ("a using-declaration cannot specify a template-id. "
4025 "Try %<using %D%>", name);
4026 return NULL_TREE;
4029 if (TREE_CODE (decl) == NAMESPACE_DECL)
4031 error ("namespace %qD not allowed in using-declaration", decl);
4032 return NULL_TREE;
4035 if (TREE_CODE (decl) == SCOPE_REF)
4037 /* It's a nested name with template parameter dependent scope.
4038 This can only be using-declaration for class member. */
4039 error ("%qT is not a namespace", TREE_OPERAND (decl, 0));
4040 return NULL_TREE;
4043 decl = OVL_FIRST (decl);
4045 /* Make a USING_DECL. */
4046 tree using_decl = push_using_decl (scope, name);
4048 if (using_decl == NULL_TREE
4049 && at_function_scope_p ()
4050 && VAR_P (decl))
4051 /* C++11 7.3.3/10. */
4052 error ("%qD is already declared in this scope", name);
4054 return using_decl;
4057 /* Process a local-scope or namespace-scope using declaration. SCOPE
4058 is the nominated scope to search for NAME. VALUE_P and TYPE_P
4059 point to the binding for NAME in the current scope and are
4060 updated. */
4062 static void
4063 do_nonmember_using_decl (tree scope, tree name, tree *value_p, tree *type_p)
4065 name_lookup lookup (name, 0);
4067 if (!qualified_namespace_lookup (scope, &lookup))
4069 error ("%qD not declared", name);
4070 return;
4072 else if (TREE_CODE (lookup.value) == TREE_LIST)
4074 error ("reference to %qD is ambiguous", name);
4075 print_candidates (lookup.value);
4076 lookup.value = NULL_TREE;
4079 if (lookup.type && TREE_CODE (lookup.type) == TREE_LIST)
4081 error ("reference to %qD is ambiguous", name);
4082 print_candidates (lookup.type);
4083 lookup.type = NULL_TREE;
4086 tree value = *value_p;
4087 tree type = *type_p;
4089 /* Shift the old and new bindings around so we're comparing class and
4090 enumeration names to each other. */
4091 if (value && DECL_IMPLICIT_TYPEDEF_P (value))
4093 type = value;
4094 value = NULL_TREE;
4097 if (lookup.value && DECL_IMPLICIT_TYPEDEF_P (lookup.value))
4099 lookup.type = lookup.value;
4100 lookup.value = NULL_TREE;
4103 if (lookup.value && lookup.value != value)
4105 /* Check for using functions. */
4106 if (OVL_P (lookup.value) && (!value || OVL_P (value)))
4108 for (lkp_iterator usings (lookup.value); usings; ++usings)
4110 tree new_fn = *usings;
4112 /* [namespace.udecl]
4114 If a function declaration in namespace scope or block
4115 scope has the same name and the same parameter types as a
4116 function introduced by a using declaration the program is
4117 ill-formed. */
4118 bool found = false;
4119 for (ovl_iterator old (value); !found && old; ++old)
4121 tree old_fn = *old;
4123 if (new_fn == old_fn)
4124 /* The function already exists in the current
4125 namespace. */
4126 found = true;
4127 else if (old.using_p ())
4128 continue; /* This is a using decl. */
4129 else if (old.hidden_p () && !DECL_HIDDEN_FRIEND_P (old_fn))
4130 continue; /* This is an anticipated builtin. */
4131 else if (!matching_fn_p (new_fn, old_fn))
4132 continue; /* Parameters do not match. */
4133 else if (decls_match (new_fn, old_fn))
4134 found = true;
4135 else
4137 diagnose_name_conflict (new_fn, old_fn);
4138 found = true;
4142 if (!found)
4143 /* Unlike the overload case we don't drop anticipated
4144 builtins here. They don't cause a problem, and
4145 we'd like to match them with a future
4146 declaration. */
4147 value = ovl_insert (new_fn, value, true);
4150 else if (value
4151 /* Ignore anticipated builtins. */
4152 && !anticipated_builtin_p (value)
4153 && !decls_match (lookup.value, value))
4154 diagnose_name_conflict (lookup.value, value);
4155 else
4156 value = lookup.value;
4159 if (lookup.type && lookup.type != type)
4161 if (type && !decls_match (lookup.type, type))
4162 diagnose_name_conflict (lookup.type, type);
4163 else
4164 type = lookup.type;
4167 /* If bind->value is empty, shift any class or enumeration name back. */
4168 if (!value)
4170 value = type;
4171 type = NULL_TREE;
4174 *value_p = value;
4175 *type_p = type;
4178 /* Returns true if ANCESTOR encloses DESCENDANT, including matching.
4179 Both are namespaces. */
4181 bool
4182 is_nested_namespace (tree ancestor, tree descendant, bool inline_only)
4184 int depth = SCOPE_DEPTH (ancestor);
4186 if (!depth && !inline_only)
4187 /* The global namespace encloses everything. */
4188 return true;
4190 while (SCOPE_DEPTH (descendant) > depth
4191 && (!inline_only || DECL_NAMESPACE_INLINE_P (descendant)))
4192 descendant = CP_DECL_CONTEXT (descendant);
4194 return ancestor == descendant;
4197 /* Returns true if ROOT (a namespace, class, or function) encloses
4198 CHILD. CHILD may be either a class type or a namespace. */
4200 bool
4201 is_ancestor (tree root, tree child)
4203 gcc_assert ((TREE_CODE (root) == NAMESPACE_DECL
4204 || TREE_CODE (root) == FUNCTION_DECL
4205 || CLASS_TYPE_P (root)));
4206 gcc_assert ((TREE_CODE (child) == NAMESPACE_DECL
4207 || CLASS_TYPE_P (child)));
4209 /* The global namespace encloses everything. */
4210 if (root == global_namespace)
4211 return true;
4213 /* Search until we reach namespace scope. */
4214 while (TREE_CODE (child) != NAMESPACE_DECL)
4216 /* If we've reached the ROOT, it encloses CHILD. */
4217 if (root == child)
4218 return true;
4219 /* Go out one level. */
4220 if (TYPE_P (child))
4221 child = TYPE_NAME (child);
4222 child = CP_DECL_CONTEXT (child);
4225 if (TREE_CODE (root) == NAMESPACE_DECL)
4226 return is_nested_namespace (root, child);
4228 return false;
4231 /* Enter the class or namespace scope indicated by T suitable for name
4232 lookup. T can be arbitrary scope, not necessary nested inside the
4233 current scope. Returns a non-null scope to pop iff pop_scope
4234 should be called later to exit this scope. */
4236 tree
4237 push_scope (tree t)
4239 if (TREE_CODE (t) == NAMESPACE_DECL)
4240 push_decl_namespace (t);
4241 else if (CLASS_TYPE_P (t))
4243 if (!at_class_scope_p ()
4244 || !same_type_p (current_class_type, t))
4245 push_nested_class (t);
4246 else
4247 /* T is the same as the current scope. There is therefore no
4248 need to re-enter the scope. Since we are not actually
4249 pushing a new scope, our caller should not call
4250 pop_scope. */
4251 t = NULL_TREE;
4254 return t;
4257 /* Leave scope pushed by push_scope. */
4259 void
4260 pop_scope (tree t)
4262 if (t == NULL_TREE)
4263 return;
4264 if (TREE_CODE (t) == NAMESPACE_DECL)
4265 pop_decl_namespace ();
4266 else if CLASS_TYPE_P (t)
4267 pop_nested_class ();
4270 /* Subroutine of push_inner_scope. */
4272 static void
4273 push_inner_scope_r (tree outer, tree inner)
4275 tree prev;
4277 if (outer == inner
4278 || (TREE_CODE (inner) != NAMESPACE_DECL && !CLASS_TYPE_P (inner)))
4279 return;
4281 prev = CP_DECL_CONTEXT (TREE_CODE (inner) == NAMESPACE_DECL ? inner : TYPE_NAME (inner));
4282 if (outer != prev)
4283 push_inner_scope_r (outer, prev);
4284 if (TREE_CODE (inner) == NAMESPACE_DECL)
4286 cp_binding_level *save_template_parm = 0;
4287 /* Temporary take out template parameter scopes. They are saved
4288 in reversed order in save_template_parm. */
4289 while (current_binding_level->kind == sk_template_parms)
4291 cp_binding_level *b = current_binding_level;
4292 current_binding_level = b->level_chain;
4293 b->level_chain = save_template_parm;
4294 save_template_parm = b;
4297 resume_scope (NAMESPACE_LEVEL (inner));
4298 current_namespace = inner;
4300 /* Restore template parameter scopes. */
4301 while (save_template_parm)
4303 cp_binding_level *b = save_template_parm;
4304 save_template_parm = b->level_chain;
4305 b->level_chain = current_binding_level;
4306 current_binding_level = b;
4309 else
4310 pushclass (inner);
4313 /* Enter the scope INNER from current scope. INNER must be a scope
4314 nested inside current scope. This works with both name lookup and
4315 pushing name into scope. In case a template parameter scope is present,
4316 namespace is pushed under the template parameter scope according to
4317 name lookup rule in 14.6.1/6.
4319 Return the former current scope suitable for pop_inner_scope. */
4321 tree
4322 push_inner_scope (tree inner)
4324 tree outer = current_scope ();
4325 if (!outer)
4326 outer = current_namespace;
4328 push_inner_scope_r (outer, inner);
4329 return outer;
4332 /* Exit the current scope INNER back to scope OUTER. */
4334 void
4335 pop_inner_scope (tree outer, tree inner)
4337 if (outer == inner
4338 || (TREE_CODE (inner) != NAMESPACE_DECL && !CLASS_TYPE_P (inner)))
4339 return;
4341 while (outer != inner)
4343 if (TREE_CODE (inner) == NAMESPACE_DECL)
4345 cp_binding_level *save_template_parm = 0;
4346 /* Temporary take out template parameter scopes. They are saved
4347 in reversed order in save_template_parm. */
4348 while (current_binding_level->kind == sk_template_parms)
4350 cp_binding_level *b = current_binding_level;
4351 current_binding_level = b->level_chain;
4352 b->level_chain = save_template_parm;
4353 save_template_parm = b;
4356 pop_namespace ();
4358 /* Restore template parameter scopes. */
4359 while (save_template_parm)
4361 cp_binding_level *b = save_template_parm;
4362 save_template_parm = b->level_chain;
4363 b->level_chain = current_binding_level;
4364 current_binding_level = b;
4367 else
4368 popclass ();
4370 inner = CP_DECL_CONTEXT (TREE_CODE (inner) == NAMESPACE_DECL ? inner : TYPE_NAME (inner));
4374 /* Do a pushlevel for class declarations. */
4376 void
4377 pushlevel_class (void)
4379 class_binding_level = begin_scope (sk_class, current_class_type);
4382 /* ...and a poplevel for class declarations. */
4384 void
4385 poplevel_class (void)
4387 cp_binding_level *level = class_binding_level;
4388 cp_class_binding *cb;
4389 size_t i;
4390 tree shadowed;
4392 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4393 gcc_assert (level != 0);
4395 /* If we're leaving a toplevel class, cache its binding level. */
4396 if (current_class_depth == 1)
4397 previous_class_level = level;
4398 for (shadowed = level->type_shadowed;
4399 shadowed;
4400 shadowed = TREE_CHAIN (shadowed))
4401 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (shadowed), TREE_VALUE (shadowed));
4403 /* Remove the bindings for all of the class-level declarations. */
4404 if (level->class_shadowed)
4406 FOR_EACH_VEC_ELT (*level->class_shadowed, i, cb)
4408 IDENTIFIER_BINDING (cb->identifier) = cb->base->previous;
4409 cxx_binding_free (cb->base);
4411 ggc_free (level->class_shadowed);
4412 level->class_shadowed = NULL;
4415 /* Now, pop out of the binding level which we created up in the
4416 `pushlevel_class' routine. */
4417 gcc_assert (current_binding_level == level);
4418 leave_scope ();
4419 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4422 /* Set INHERITED_VALUE_BINDING_P on BINDING to true or false, as
4423 appropriate. DECL is the value to which a name has just been
4424 bound. CLASS_TYPE is the class in which the lookup occurred. */
4426 static void
4427 set_inherited_value_binding_p (cxx_binding *binding, tree decl,
4428 tree class_type)
4430 if (binding->value == decl && TREE_CODE (decl) != TREE_LIST)
4432 tree context;
4434 if (TREE_CODE (decl) == OVERLOAD)
4435 context = ovl_scope (decl);
4436 else
4438 gcc_assert (DECL_P (decl));
4439 context = context_for_name_lookup (decl);
4442 if (is_properly_derived_from (class_type, context))
4443 INHERITED_VALUE_BINDING_P (binding) = 1;
4444 else
4445 INHERITED_VALUE_BINDING_P (binding) = 0;
4447 else if (binding->value == decl)
4448 /* We only encounter a TREE_LIST when there is an ambiguity in the
4449 base classes. Such an ambiguity can be overridden by a
4450 definition in this class. */
4451 INHERITED_VALUE_BINDING_P (binding) = 1;
4452 else
4453 INHERITED_VALUE_BINDING_P (binding) = 0;
4456 /* Make the declaration of X appear in CLASS scope. */
4458 bool
4459 pushdecl_class_level (tree x)
4461 bool is_valid = true;
4462 bool subtime;
4464 /* Do nothing if we're adding to an outer lambda closure type,
4465 outer_binding will add it later if it's needed. */
4466 if (current_class_type != class_binding_level->this_entity)
4467 return true;
4469 subtime = timevar_cond_start (TV_NAME_LOOKUP);
4470 /* Get the name of X. */
4471 tree name = OVL_NAME (x);
4473 if (name)
4475 is_valid = push_class_level_binding (name, x);
4476 if (TREE_CODE (x) == TYPE_DECL)
4477 set_identifier_type_value (name, x);
4479 else if (ANON_AGGR_TYPE_P (TREE_TYPE (x)))
4481 /* If X is an anonymous aggregate, all of its members are
4482 treated as if they were members of the class containing the
4483 aggregate, for naming purposes. */
4484 tree f;
4486 for (f = TYPE_FIELDS (TREE_TYPE (x)); f; f = DECL_CHAIN (f))
4488 location_t save_location = input_location;
4489 input_location = DECL_SOURCE_LOCATION (f);
4490 if (!pushdecl_class_level (f))
4491 is_valid = false;
4492 input_location = save_location;
4495 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4496 return is_valid;
4499 /* Return the BINDING (if any) for NAME in SCOPE, which is a class
4500 scope. If the value returned is non-NULL, and the PREVIOUS field
4501 is not set, callers must set the PREVIOUS field explicitly. */
4503 static cxx_binding *
4504 get_class_binding (tree name, cp_binding_level *scope)
4506 tree class_type;
4507 tree type_binding;
4508 tree value_binding;
4509 cxx_binding *binding;
4511 class_type = scope->this_entity;
4513 /* Get the type binding. */
4514 type_binding = lookup_member (class_type, name,
4515 /*protect=*/2, /*want_type=*/true,
4516 tf_warning_or_error);
4517 /* Get the value binding. */
4518 value_binding = lookup_member (class_type, name,
4519 /*protect=*/2, /*want_type=*/false,
4520 tf_warning_or_error);
4522 if (value_binding
4523 && (TREE_CODE (value_binding) == TYPE_DECL
4524 || DECL_CLASS_TEMPLATE_P (value_binding)
4525 || (TREE_CODE (value_binding) == TREE_LIST
4526 && TREE_TYPE (value_binding) == error_mark_node
4527 && (TREE_CODE (TREE_VALUE (value_binding))
4528 == TYPE_DECL))))
4529 /* We found a type binding, even when looking for a non-type
4530 binding. This means that we already processed this binding
4531 above. */
4533 else if (value_binding)
4535 if (TREE_CODE (value_binding) == TREE_LIST
4536 && TREE_TYPE (value_binding) == error_mark_node)
4537 /* NAME is ambiguous. */
4539 else if (BASELINK_P (value_binding))
4540 /* NAME is some overloaded functions. */
4541 value_binding = BASELINK_FUNCTIONS (value_binding);
4544 /* If we found either a type binding or a value binding, create a
4545 new binding object. */
4546 if (type_binding || value_binding)
4548 binding = new_class_binding (name,
4549 value_binding,
4550 type_binding,
4551 scope);
4552 /* This is a class-scope binding, not a block-scope binding. */
4553 LOCAL_BINDING_P (binding) = 0;
4554 set_inherited_value_binding_p (binding, value_binding, class_type);
4556 else
4557 binding = NULL;
4559 return binding;
4562 /* Make the declaration(s) of X appear in CLASS scope under the name
4563 NAME. Returns true if the binding is valid. */
4565 static bool
4566 push_class_level_binding_1 (tree name, tree x)
4568 cxx_binding *binding;
4569 tree decl = x;
4570 bool ok;
4572 /* The class_binding_level will be NULL if x is a template
4573 parameter name in a member template. */
4574 if (!class_binding_level)
4575 return true;
4577 if (name == error_mark_node)
4578 return false;
4580 /* Can happen for an erroneous declaration (c++/60384). */
4581 if (!identifier_p (name))
4583 gcc_assert (errorcount || sorrycount);
4584 return false;
4587 /* Check for invalid member names. But don't worry about a default
4588 argument-scope lambda being pushed after the class is complete. */
4589 gcc_assert (TYPE_BEING_DEFINED (current_class_type)
4590 || LAMBDA_TYPE_P (TREE_TYPE (decl)));
4591 /* Check that we're pushing into the right binding level. */
4592 gcc_assert (current_class_type == class_binding_level->this_entity);
4594 /* We could have been passed a tree list if this is an ambiguous
4595 declaration. If so, pull the declaration out because
4596 check_template_shadow will not handle a TREE_LIST. */
4597 if (TREE_CODE (decl) == TREE_LIST
4598 && TREE_TYPE (decl) == error_mark_node)
4599 decl = TREE_VALUE (decl);
4601 if (!check_template_shadow (decl))
4602 return false;
4604 /* [class.mem]
4606 If T is the name of a class, then each of the following shall
4607 have a name different from T:
4609 -- every static data member of class T;
4611 -- every member of class T that is itself a type;
4613 -- every enumerator of every member of class T that is an
4614 enumerated type;
4616 -- every member of every anonymous union that is a member of
4617 class T.
4619 (Non-static data members were also forbidden to have the same
4620 name as T until TC1.) */
4621 if ((VAR_P (x)
4622 || TREE_CODE (x) == CONST_DECL
4623 || (TREE_CODE (x) == TYPE_DECL
4624 && !DECL_SELF_REFERENCE_P (x))
4625 /* A data member of an anonymous union. */
4626 || (TREE_CODE (x) == FIELD_DECL
4627 && DECL_CONTEXT (x) != current_class_type))
4628 && DECL_NAME (x) == DECL_NAME (TYPE_NAME (current_class_type)))
4630 tree scope = context_for_name_lookup (x);
4631 if (TYPE_P (scope) && same_type_p (scope, current_class_type))
4633 error ("%qD has the same name as the class in which it is "
4634 "declared",
4636 return false;
4640 /* Get the current binding for NAME in this class, if any. */
4641 binding = IDENTIFIER_BINDING (name);
4642 if (!binding || binding->scope != class_binding_level)
4644 binding = get_class_binding (name, class_binding_level);
4645 /* If a new binding was created, put it at the front of the
4646 IDENTIFIER_BINDING list. */
4647 if (binding)
4649 binding->previous = IDENTIFIER_BINDING (name);
4650 IDENTIFIER_BINDING (name) = binding;
4654 /* If there is already a binding, then we may need to update the
4655 current value. */
4656 if (binding && binding->value)
4658 tree bval = binding->value;
4659 tree old_decl = NULL_TREE;
4660 tree target_decl = strip_using_decl (decl);
4661 tree target_bval = strip_using_decl (bval);
4663 if (INHERITED_VALUE_BINDING_P (binding))
4665 /* If the old binding was from a base class, and was for a
4666 tag name, slide it over to make room for the new binding.
4667 The old binding is still visible if explicitly qualified
4668 with a class-key. */
4669 if (TREE_CODE (target_bval) == TYPE_DECL
4670 && DECL_ARTIFICIAL (target_bval)
4671 && !(TREE_CODE (target_decl) == TYPE_DECL
4672 && DECL_ARTIFICIAL (target_decl)))
4674 old_decl = binding->type;
4675 binding->type = bval;
4676 binding->value = NULL_TREE;
4677 INHERITED_VALUE_BINDING_P (binding) = 0;
4679 else
4681 old_decl = bval;
4682 /* Any inherited type declaration is hidden by the type
4683 declaration in the derived class. */
4684 if (TREE_CODE (target_decl) == TYPE_DECL
4685 && DECL_ARTIFICIAL (target_decl))
4686 binding->type = NULL_TREE;
4689 else if (TREE_CODE (target_decl) == OVERLOAD
4690 && OVL_P (target_bval))
4691 old_decl = bval;
4692 else if (TREE_CODE (decl) == USING_DECL
4693 && TREE_CODE (bval) == USING_DECL
4694 && same_type_p (USING_DECL_SCOPE (decl),
4695 USING_DECL_SCOPE (bval)))
4696 /* This is a using redeclaration that will be diagnosed later
4697 in supplement_binding */
4699 else if (TREE_CODE (decl) == USING_DECL
4700 && TREE_CODE (bval) == USING_DECL
4701 && DECL_DEPENDENT_P (decl)
4702 && DECL_DEPENDENT_P (bval))
4703 return true;
4704 else if (TREE_CODE (decl) == USING_DECL
4705 && OVL_P (target_bval))
4706 old_decl = bval;
4707 else if (TREE_CODE (bval) == USING_DECL
4708 && OVL_P (target_decl))
4709 return true;
4711 if (old_decl && binding->scope == class_binding_level)
4713 binding->value = x;
4714 /* It is always safe to clear INHERITED_VALUE_BINDING_P
4715 here. This function is only used to register bindings
4716 from with the class definition itself. */
4717 INHERITED_VALUE_BINDING_P (binding) = 0;
4718 return true;
4722 /* Note that we declared this value so that we can issue an error if
4723 this is an invalid redeclaration of a name already used for some
4724 other purpose. */
4725 note_name_declared_in_class (name, decl);
4727 /* If we didn't replace an existing binding, put the binding on the
4728 stack of bindings for the identifier, and update the shadowed
4729 list. */
4730 if (binding && binding->scope == class_binding_level)
4731 /* Supplement the existing binding. */
4732 ok = supplement_binding (binding, decl);
4733 else
4735 /* Create a new binding. */
4736 push_binding (name, decl, class_binding_level);
4737 ok = true;
4740 return ok;
4743 /* Wrapper for push_class_level_binding_1. */
4745 bool
4746 push_class_level_binding (tree name, tree x)
4748 bool ret;
4749 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4750 ret = push_class_level_binding_1 (name, x);
4751 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4752 return ret;
4755 /* Process "using SCOPE::NAME" in a class scope. Return the
4756 USING_DECL created. */
4758 tree
4759 do_class_using_decl (tree scope, tree name)
4761 if (name == error_mark_node)
4762 return NULL_TREE;
4764 if (!scope || !TYPE_P (scope))
4766 error ("using-declaration for non-member at class scope");
4767 return NULL_TREE;
4770 /* Make sure the name is not invalid */
4771 if (TREE_CODE (name) == BIT_NOT_EXPR)
4773 error ("%<%T::%D%> names destructor", scope, name);
4774 return NULL_TREE;
4777 /* Using T::T declares inheriting ctors, even if T is a typedef. */
4778 if (MAYBE_CLASS_TYPE_P (scope)
4779 && (name == TYPE_IDENTIFIER (scope)
4780 || constructor_name_p (name, scope)))
4782 maybe_warn_cpp0x (CPP0X_INHERITING_CTORS);
4783 name = ctor_identifier;
4784 CLASSTYPE_NON_AGGREGATE (current_class_type) = true;
4787 /* Cannot introduce a constructor name. */
4788 if (constructor_name_p (name, current_class_type))
4790 error ("%<%T::%D%> names constructor in %qT",
4791 scope, name, current_class_type);
4792 return NULL_TREE;
4795 /* From [namespace.udecl]:
4797 A using-declaration used as a member-declaration shall refer to a
4798 member of a base class of the class being defined.
4800 In general, we cannot check this constraint in a template because
4801 we do not know the entire set of base classes of the current
4802 class type. Morover, if SCOPE is dependent, it might match a
4803 non-dependent base. */
4805 tree decl = NULL_TREE;
4806 if (!dependent_scope_p (scope))
4808 base_kind b_kind;
4809 tree binfo = lookup_base (current_class_type, scope, ba_any, &b_kind,
4810 tf_warning_or_error);
4811 if (b_kind < bk_proper_base)
4813 /* If there are dependent bases, scope might resolve at
4814 instantiation time, even if it isn't exactly one of the
4815 dependent bases. */
4816 if (b_kind == bk_same_type || !any_dependent_bases_p ())
4818 error_not_base_type (scope, current_class_type);
4819 return NULL_TREE;
4822 else if (name == ctor_identifier && !binfo_direct_p (binfo))
4824 error ("cannot inherit constructors from indirect base %qT", scope);
4825 return NULL_TREE;
4827 else if (!IDENTIFIER_CONV_OP_P (name)
4828 || !dependent_type_p (TREE_TYPE (name)))
4830 decl = lookup_member (binfo, name, 0, false, tf_warning_or_error);
4831 if (!decl)
4833 error ("no members matching %<%T::%D%> in %q#T", scope, name,
4834 scope);
4835 return NULL_TREE;
4838 /* The binfo from which the functions came does not matter. */
4839 if (BASELINK_P (decl))
4840 decl = BASELINK_FUNCTIONS (decl);
4844 tree value = build_lang_decl (USING_DECL, name, NULL_TREE);
4845 USING_DECL_DECLS (value) = decl;
4846 USING_DECL_SCOPE (value) = scope;
4847 DECL_DEPENDENT_P (value) = !decl;
4849 return value;
4853 /* Return the binding for NAME in NS. If NS is NULL, look in
4854 global_namespace. */
4856 tree
4857 get_namespace_binding (tree ns, tree name)
4859 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4860 if (!ns)
4861 ns = global_namespace;
4862 gcc_checking_assert (!DECL_NAMESPACE_ALIAS (ns));
4863 tree ret = find_namespace_value (ns, name);
4864 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4865 return ret;
4868 /* Push internal DECL into the global namespace. Does not do the
4869 full overload fn handling and does not add it to the list of things
4870 in the namespace. */
4872 void
4873 set_global_binding (tree decl)
4875 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4877 tree *slot = find_namespace_slot (global_namespace, DECL_NAME (decl), true);
4879 if (*slot)
4880 /* The user's placed something in the implementor's namespace. */
4881 diagnose_name_conflict (decl, MAYBE_STAT_DECL (*slot));
4883 /* Force the binding, so compiler internals continue to work. */
4884 *slot = decl;
4886 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4889 /* Set the context of a declaration to scope. Complain if we are not
4890 outside scope. */
4892 void
4893 set_decl_namespace (tree decl, tree scope, bool friendp)
4895 /* Get rid of namespace aliases. */
4896 scope = ORIGINAL_NAMESPACE (scope);
4898 /* It is ok for friends to be qualified in parallel space. */
4899 if (!friendp && !is_nested_namespace (current_namespace, scope))
4900 error ("declaration of %qD not in a namespace surrounding %qD",
4901 decl, scope);
4902 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
4904 /* See whether this has been declared in the namespace or inline
4905 children. */
4906 tree old = NULL_TREE;
4908 name_lookup lookup (DECL_NAME (decl), LOOKUP_HIDDEN);
4909 if (!lookup.search_qualified (scope, /*usings=*/false))
4910 /* No old declaration at all. */
4911 goto not_found;
4912 old = lookup.value;
4915 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
4916 if (TREE_CODE (old) == TREE_LIST)
4918 ambiguous:
4919 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
4920 error ("reference to %qD is ambiguous", decl);
4921 print_candidates (old);
4922 return;
4925 if (!DECL_DECLARES_FUNCTION_P (decl))
4927 /* Don't compare non-function decls with decls_match here, since
4928 it can't check for the correct constness at this
4929 point. pushdecl will find those errors later. */
4931 /* We might have found it in an inline namespace child of SCOPE. */
4932 if (TREE_CODE (decl) == TREE_CODE (old))
4933 DECL_CONTEXT (decl) = DECL_CONTEXT (old);
4935 found:
4936 /* Writing "N::i" to declare something directly in "N" is invalid. */
4937 if (CP_DECL_CONTEXT (decl) == current_namespace
4938 && at_namespace_scope_p ())
4939 error ("explicit qualification in declaration of %qD", decl);
4940 return;
4943 /* Since decl is a function, old should contain a function decl. */
4944 if (!OVL_P (old))
4945 goto not_found;
4947 /* We handle these in check_explicit_instantiation_namespace. */
4948 if (processing_explicit_instantiation)
4949 return;
4950 if (processing_template_decl || processing_specialization)
4951 /* We have not yet called push_template_decl to turn a
4952 FUNCTION_DECL into a TEMPLATE_DECL, so the declarations won't
4953 match. But, we'll check later, when we construct the
4954 template. */
4955 return;
4956 /* Instantiations or specializations of templates may be declared as
4957 friends in any namespace. */
4958 if (friendp && DECL_USE_TEMPLATE (decl))
4959 return;
4961 tree found;
4962 found = NULL_TREE;
4964 for (lkp_iterator iter (old); iter; ++iter)
4966 if (iter.using_p ())
4967 continue;
4969 tree ofn = *iter;
4971 /* Adjust DECL_CONTEXT first so decls_match will return true
4972 if DECL will match a declaration in an inline namespace. */
4973 DECL_CONTEXT (decl) = DECL_CONTEXT (ofn);
4974 if (decls_match (decl, ofn))
4976 if (found)
4978 /* We found more than one matching declaration. */
4979 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
4980 goto ambiguous;
4982 found = ofn;
4986 if (found)
4988 if (DECL_HIDDEN_FRIEND_P (found))
4990 pedwarn (DECL_SOURCE_LOCATION (decl), 0,
4991 "%qD has not been declared within %qD", decl, scope);
4992 inform (DECL_SOURCE_LOCATION (found),
4993 "only here as a %<friend%>");
4995 DECL_CONTEXT (decl) = DECL_CONTEXT (found);
4996 goto found;
4999 not_found:
5000 /* It didn't work, go back to the explicit scope. */
5001 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
5002 error ("%qD should have been declared inside %qD", decl, scope);
5005 /* Return the namespace where the current declaration is declared. */
5007 tree
5008 current_decl_namespace (void)
5010 tree result;
5011 /* If we have been pushed into a different namespace, use it. */
5012 if (!vec_safe_is_empty (decl_namespace_list))
5013 return decl_namespace_list->last ();
5015 if (current_class_type)
5016 result = decl_namespace_context (current_class_type);
5017 else if (current_function_decl)
5018 result = decl_namespace_context (current_function_decl);
5019 else
5020 result = current_namespace;
5021 return result;
5024 /* Process any ATTRIBUTES on a namespace definition. Returns true if
5025 attribute visibility is seen. */
5027 bool
5028 handle_namespace_attrs (tree ns, tree attributes)
5030 tree d;
5031 bool saw_vis = false;
5033 for (d = attributes; d; d = TREE_CHAIN (d))
5035 tree name = get_attribute_name (d);
5036 tree args = TREE_VALUE (d);
5038 if (is_attribute_p ("visibility", name))
5040 /* attribute visibility is a property of the syntactic block
5041 rather than the namespace as a whole, so we don't touch the
5042 NAMESPACE_DECL at all. */
5043 tree x = args ? TREE_VALUE (args) : NULL_TREE;
5044 if (x == NULL_TREE || TREE_CODE (x) != STRING_CST || TREE_CHAIN (args))
5046 warning (OPT_Wattributes,
5047 "%qD attribute requires a single NTBS argument",
5048 name);
5049 continue;
5052 if (!TREE_PUBLIC (ns))
5053 warning (OPT_Wattributes,
5054 "%qD attribute is meaningless since members of the "
5055 "anonymous namespace get local symbols", name);
5057 push_visibility (TREE_STRING_POINTER (x), 1);
5058 saw_vis = true;
5060 else if (is_attribute_p ("abi_tag", name))
5062 if (!DECL_NAME (ns))
5064 warning (OPT_Wattributes, "ignoring %qD attribute on anonymous "
5065 "namespace", name);
5066 continue;
5068 if (!DECL_NAMESPACE_INLINE_P (ns))
5070 warning (OPT_Wattributes, "ignoring %qD attribute on non-inline "
5071 "namespace", name);
5072 continue;
5074 if (!args)
5076 tree dn = DECL_NAME (ns);
5077 args = build_string (IDENTIFIER_LENGTH (dn) + 1,
5078 IDENTIFIER_POINTER (dn));
5079 TREE_TYPE (args) = char_array_type_node;
5080 args = fix_string_type (args);
5081 args = build_tree_list (NULL_TREE, args);
5083 if (check_abi_tag_args (args, name))
5084 DECL_ATTRIBUTES (ns) = tree_cons (name, args,
5085 DECL_ATTRIBUTES (ns));
5087 else
5089 warning (OPT_Wattributes, "%qD attribute directive ignored",
5090 name);
5091 continue;
5095 return saw_vis;
5098 /* Temporarily set the namespace for the current declaration. */
5100 void
5101 push_decl_namespace (tree decl)
5103 if (TREE_CODE (decl) != NAMESPACE_DECL)
5104 decl = decl_namespace_context (decl);
5105 vec_safe_push (decl_namespace_list, ORIGINAL_NAMESPACE (decl));
5108 /* [namespace.memdef]/2 */
5110 void
5111 pop_decl_namespace (void)
5113 decl_namespace_list->pop ();
5116 /* Process a namespace-alias declaration. */
5118 void
5119 do_namespace_alias (tree alias, tree name_space)
5121 if (name_space == error_mark_node)
5122 return;
5124 gcc_assert (TREE_CODE (name_space) == NAMESPACE_DECL);
5126 name_space = ORIGINAL_NAMESPACE (name_space);
5128 /* Build the alias. */
5129 alias = build_lang_decl (NAMESPACE_DECL, alias, void_type_node);
5130 DECL_NAMESPACE_ALIAS (alias) = name_space;
5131 DECL_EXTERNAL (alias) = 1;
5132 DECL_CONTEXT (alias) = FROB_CONTEXT (current_scope ());
5133 pushdecl (alias);
5135 /* Emit debug info for namespace alias. */
5136 if (!building_stmt_list_p ())
5137 (*debug_hooks->early_global_decl) (alias);
5140 /* Like pushdecl, only it places X in the current namespace,
5141 if appropriate. */
5143 tree
5144 pushdecl_namespace_level (tree x, bool is_friend)
5146 cp_binding_level *b = current_binding_level;
5147 tree t;
5149 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
5150 t = do_pushdecl_with_scope
5151 (x, NAMESPACE_LEVEL (current_namespace), is_friend);
5153 /* Now, the type_shadowed stack may screw us. Munge it so it does
5154 what we want. */
5155 if (TREE_CODE (t) == TYPE_DECL)
5157 tree name = DECL_NAME (t);
5158 tree newval;
5159 tree *ptr = (tree *)0;
5160 for (; !global_scope_p (b); b = b->level_chain)
5162 tree shadowed = b->type_shadowed;
5163 for (; shadowed; shadowed = TREE_CHAIN (shadowed))
5164 if (TREE_PURPOSE (shadowed) == name)
5166 ptr = &TREE_VALUE (shadowed);
5167 /* Can't break out of the loop here because sometimes
5168 a binding level will have duplicate bindings for
5169 PT names. It's gross, but I haven't time to fix it. */
5172 newval = TREE_TYPE (t);
5173 if (ptr == (tree *)0)
5175 /* @@ This shouldn't be needed. My test case "zstring.cc" trips
5176 up here if this is changed to an assertion. --KR */
5177 SET_IDENTIFIER_TYPE_VALUE (name, t);
5179 else
5181 *ptr = newval;
5184 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
5185 return t;
5188 /* Process a using-declaration appearing in namespace scope. */
5190 void
5191 finish_namespace_using_decl (tree decl, tree scope, tree name)
5193 tree orig_decl = decl;
5195 gcc_checking_assert (current_binding_level->kind == sk_namespace
5196 && !processing_template_decl);
5197 decl = validate_nonmember_using_decl (decl, scope, name);
5198 if (decl == NULL_TREE)
5199 return;
5201 tree *slot = find_namespace_slot (current_namespace, name, true);
5202 tree val = slot ? MAYBE_STAT_DECL (*slot) : NULL_TREE;
5203 tree type = slot ? MAYBE_STAT_TYPE (*slot) : NULL_TREE;
5204 do_nonmember_using_decl (scope, name, &val, &type);
5205 if (STAT_HACK_P (*slot))
5207 STAT_DECL (*slot) = val;
5208 STAT_TYPE (*slot) = type;
5210 else if (type)
5211 *slot = stat_hack (val, type);
5212 else
5213 *slot = val;
5215 /* Emit debug info. */
5216 cp_emit_debug_info_for_using (orig_decl, current_namespace);
5219 /* Process a using-declaration at function scope. */
5221 void
5222 finish_local_using_decl (tree decl, tree scope, tree name)
5224 tree orig_decl = decl;
5226 gcc_checking_assert (current_binding_level->kind != sk_class
5227 && current_binding_level->kind != sk_namespace);
5228 decl = validate_nonmember_using_decl (decl, scope, name);
5229 if (decl == NULL_TREE)
5230 return;
5232 add_decl_expr (decl);
5234 cxx_binding *binding = find_local_binding (current_binding_level, name);
5235 tree value = binding ? binding->value : NULL_TREE;
5236 tree type = binding ? binding->type : NULL_TREE;
5238 do_nonmember_using_decl (scope, name, &value, &type);
5240 if (!value)
5242 else if (binding && value == binding->value)
5244 else if (binding && binding->value && TREE_CODE (value) == OVERLOAD)
5246 update_local_overload (IDENTIFIER_BINDING (name), value);
5247 IDENTIFIER_BINDING (name)->value = value;
5249 else
5250 /* Install the new binding. */
5251 push_local_binding (name, value, true);
5253 if (!type)
5255 else if (binding && type == binding->type)
5257 else
5259 push_local_binding (name, type, true);
5260 set_identifier_type_value (name, type);
5263 /* Emit debug info. */
5264 if (!processing_template_decl)
5265 cp_emit_debug_info_for_using (orig_decl, current_scope ());
5268 /* Return the declarations that are members of the namespace NS. */
5270 tree
5271 cp_namespace_decls (tree ns)
5273 return NAMESPACE_LEVEL (ns)->names;
5276 /* Combine prefer_type and namespaces_only into flags. */
5278 static int
5279 lookup_flags (int prefer_type, int namespaces_only)
5281 if (namespaces_only)
5282 return LOOKUP_PREFER_NAMESPACES;
5283 if (prefer_type > 1)
5284 return LOOKUP_PREFER_TYPES;
5285 if (prefer_type > 0)
5286 return LOOKUP_PREFER_BOTH;
5287 return 0;
5290 /* Given a lookup that returned VAL, use FLAGS to decide if we want to
5291 ignore it or not. Subroutine of lookup_name_real and
5292 lookup_type_scope. */
5294 static bool
5295 qualify_lookup (tree val, int flags)
5297 if (val == NULL_TREE)
5298 return false;
5299 if ((flags & LOOKUP_PREFER_NAMESPACES) && TREE_CODE (val) == NAMESPACE_DECL)
5300 return true;
5301 if (flags & LOOKUP_PREFER_TYPES)
5303 tree target_val = strip_using_decl (val);
5304 if (TREE_CODE (target_val) == TYPE_DECL
5305 || TREE_CODE (target_val) == TEMPLATE_DECL)
5306 return true;
5308 if (flags & (LOOKUP_PREFER_NAMESPACES | LOOKUP_PREFER_TYPES))
5309 return false;
5310 /* Look through lambda things that we shouldn't be able to see. */
5311 if (!(flags & LOOKUP_HIDDEN) && is_lambda_ignored_entity (val))
5312 return false;
5313 return true;
5316 /* Suggest alternatives for NAME, an IDENTIFIER_NODE for which name
5317 lookup failed. Search through all available namespaces and print out
5318 possible candidates. If no exact matches are found, and
5319 SUGGEST_MISSPELLINGS is true, then also look for near-matches and
5320 suggest the best near-match, if there is one. */
5322 void
5323 suggest_alternatives_for (location_t location, tree name,
5324 bool suggest_misspellings)
5326 vec<tree> candidates = vNULL;
5327 vec<tree> worklist = vNULL;
5328 unsigned limit = PARAM_VALUE (CXX_MAX_NAMESPACES_FOR_DIAGNOSTIC_HELP);
5329 bool limited = false;
5331 /* Breadth-first search of namespaces. Up to limit namespaces
5332 searched (limit zero == unlimited). */
5333 worklist.safe_push (global_namespace);
5334 for (unsigned ix = 0; ix != worklist.length (); ix++)
5336 tree ns = worklist[ix];
5337 name_lookup lookup (name);
5339 if (lookup.search_qualified (ns, false))
5340 candidates.safe_push (lookup.value);
5342 if (!limited)
5344 /* Look for child namespaces. We have to do this
5345 indirectly because they are chained in reverse order,
5346 which is confusing to the user. */
5347 vec<tree> children = vNULL;
5349 for (tree decl = NAMESPACE_LEVEL (ns)->names;
5350 decl; decl = TREE_CHAIN (decl))
5351 if (TREE_CODE (decl) == NAMESPACE_DECL
5352 && !DECL_NAMESPACE_ALIAS (decl)
5353 && !DECL_NAMESPACE_INLINE_P (decl))
5354 children.safe_push (decl);
5356 while (!limited && !children.is_empty ())
5358 if (worklist.length () == limit)
5360 /* Unconditionally warn that the search was truncated. */
5361 inform (location,
5362 "maximum limit of %d namespaces searched for %qE",
5363 limit, name);
5364 limited = true;
5366 else
5367 worklist.safe_push (children.pop ());
5369 children.release ();
5372 worklist.release ();
5374 if (candidates.length ())
5376 inform_n (location, candidates.length (),
5377 "suggested alternative:",
5378 "suggested alternatives:");
5379 for (unsigned ix = 0; ix != candidates.length (); ix++)
5381 tree val = candidates[ix];
5383 inform (location_of (val), " %qE", val);
5385 candidates.release ();
5387 else if (!suggest_misspellings)
5389 else if (name_hint hint = lookup_name_fuzzy (name, FUZZY_LOOKUP_NAME,
5390 location))
5392 /* Show a spelling correction. */
5393 gcc_rich_location richloc (location);
5395 richloc.add_fixit_replace (hint.suggestion ());
5396 inform (&richloc, "suggested alternative: %qs", hint.suggestion ());
5400 /* Subroutine of maybe_suggest_missing_header for handling unrecognized names
5401 for some of the most common names within "std::".
5402 Given non-NULL NAME, a name for lookup within "std::", return the header
5403 name defining it within the C++ Standard Library (with '<' and '>'),
5404 or NULL. */
5406 static const char *
5407 get_std_name_hint (const char *name)
5409 struct std_name_hint
5411 const char *name;
5412 const char *header;
5414 static const std_name_hint hints[] = {
5415 /* <array>. */
5416 {"array", "<array>"}, // C++11
5417 /* <deque>. */
5418 {"deque", "<deque>"},
5419 /* <forward_list>. */
5420 {"forward_list", "<forward_list>"}, // C++11
5421 /* <fstream>. */
5422 {"basic_filebuf", "<fstream>"},
5423 {"basic_ifstream", "<fstream>"},
5424 {"basic_ofstream", "<fstream>"},
5425 {"basic_fstream", "<fstream>"},
5426 /* <iostream>. */
5427 {"cin", "<iostream>"},
5428 {"cout", "<iostream>"},
5429 {"cerr", "<iostream>"},
5430 {"clog", "<iostream>"},
5431 {"wcin", "<iostream>"},
5432 {"wcout", "<iostream>"},
5433 {"wclog", "<iostream>"},
5434 /* <list>. */
5435 {"list", "<list>"},
5436 /* <map>. */
5437 {"map", "<map>"},
5438 {"multimap", "<map>"},
5439 /* <queue>. */
5440 {"queue", "<queue>"},
5441 {"priority_queue", "<queue>"},
5442 /* <ostream>. */
5443 {"ostream", "<ostream>"},
5444 {"wostream", "<ostream>"},
5445 {"ends", "<ostream>"},
5446 {"flush", "<ostream>"},
5447 {"endl", "<ostream>"},
5448 /* <set>. */
5449 {"set", "<set>"},
5450 {"multiset", "<set>"},
5451 /* <sstream>. */
5452 {"basic_stringbuf", "<sstream>"},
5453 {"basic_istringstream", "<sstream>"},
5454 {"basic_ostringstream", "<sstream>"},
5455 {"basic_stringstream", "<sstream>"},
5456 /* <stack>. */
5457 {"stack", "<stack>"},
5458 /* <string>. */
5459 {"string", "<string>"},
5460 {"wstring", "<string>"},
5461 {"u16string", "<string>"},
5462 {"u32string", "<string>"},
5463 /* <unordered_map>. */
5464 {"unordered_map", "<unordered_map>"}, // C++11
5465 {"unordered_multimap", "<unordered_map>"}, // C++11
5466 /* <unordered_set>. */
5467 {"unordered_set", "<unordered_set>"}, // C++11
5468 {"unordered_multiset", "<unordered_set>"}, // C++11
5469 /* <vector>. */
5470 {"vector", "<vector>"},
5472 const size_t num_hints = sizeof (hints) / sizeof (hints[0]);
5473 for (size_t i = 0; i < num_hints; i++)
5475 if (0 == strcmp (name, hints[i].name))
5476 return hints[i].header;
5478 return NULL;
5481 /* If SCOPE is the "std" namespace, then suggest pertinent header
5482 files for NAME at LOCATION.
5483 Return true iff a suggestion was offered. */
5485 static bool
5486 maybe_suggest_missing_header (location_t location, tree name, tree scope)
5488 if (scope == NULL_TREE)
5489 return false;
5490 if (TREE_CODE (scope) != NAMESPACE_DECL)
5491 return false;
5492 /* We only offer suggestions for the "std" namespace. */
5493 if (scope != std_node)
5494 return false;
5495 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
5497 const char *name_str = IDENTIFIER_POINTER (name);
5498 const char *header_hint = get_std_name_hint (name_str);
5499 if (!header_hint)
5500 return false;
5502 gcc_rich_location richloc (location);
5503 maybe_add_include_fixit (&richloc, header_hint);
5504 inform (&richloc,
5505 "%<std::%s%> is defined in header %qs;"
5506 " did you forget to %<#include %s%>?",
5507 name_str, header_hint, header_hint);
5508 return true;
5511 /* Look for alternatives for NAME, an IDENTIFIER_NODE for which name
5512 lookup failed within the explicitly provided SCOPE. Suggest the
5513 the best meaningful candidates (if any) as a fix-it hint.
5514 Return true iff a suggestion was provided. */
5516 bool
5517 suggest_alternative_in_explicit_scope (location_t location, tree name,
5518 tree scope)
5520 /* Resolve any namespace aliases. */
5521 scope = ORIGINAL_NAMESPACE (scope);
5523 if (maybe_suggest_missing_header (location, name, scope))
5524 return true;
5526 cp_binding_level *level = NAMESPACE_LEVEL (scope);
5528 best_match <tree, const char *> bm (name);
5529 consider_binding_level (name, bm, level, false, FUZZY_LOOKUP_NAME);
5531 /* See if we have a good suggesion for the user. */
5532 const char *fuzzy_name = bm.get_best_meaningful_candidate ();
5533 if (fuzzy_name)
5535 gcc_rich_location richloc (location);
5536 richloc.add_fixit_replace (fuzzy_name);
5537 inform (&richloc, "suggested alternative: %qs",
5538 fuzzy_name);
5539 return true;
5542 return false;
5545 /* Look up NAME (an IDENTIFIER_NODE) in SCOPE (either a NAMESPACE_DECL
5546 or a class TYPE).
5548 If PREFER_TYPE is > 0, we only return TYPE_DECLs or namespaces.
5549 If PREFER_TYPE is > 1, we only return TYPE_DECLs.
5551 Returns a DECL (or OVERLOAD, or BASELINK) representing the
5552 declaration found. If no suitable declaration can be found,
5553 ERROR_MARK_NODE is returned. If COMPLAIN is true and SCOPE is
5554 neither a class-type nor a namespace a diagnostic is issued. */
5556 tree
5557 lookup_qualified_name (tree scope, tree name, int prefer_type, bool complain,
5558 bool find_hidden)
5560 tree t = NULL_TREE;
5562 if (TREE_CODE (scope) == NAMESPACE_DECL)
5564 int flags = lookup_flags (prefer_type, /*namespaces_only*/false);
5565 if (find_hidden)
5566 flags |= LOOKUP_HIDDEN;
5567 name_lookup lookup (name, flags);
5569 if (qualified_namespace_lookup (scope, &lookup))
5570 t = lookup.value;
5572 else if (cxx_dialect != cxx98 && TREE_CODE (scope) == ENUMERAL_TYPE)
5573 t = lookup_enumerator (scope, name);
5574 else if (is_class_type (scope, complain))
5575 t = lookup_member (scope, name, 2, prefer_type, tf_warning_or_error);
5577 if (!t)
5578 return error_mark_node;
5579 return t;
5582 /* [namespace.qual]
5583 Accepts the NAME to lookup and its qualifying SCOPE.
5584 Returns the name/type pair found into the cxx_binding *RESULT,
5585 or false on error. */
5587 static bool
5588 qualified_namespace_lookup (tree scope, name_lookup *lookup)
5590 timevar_start (TV_NAME_LOOKUP);
5591 query_oracle (lookup->name);
5592 bool found = lookup->search_qualified (ORIGINAL_NAMESPACE (scope));
5593 timevar_stop (TV_NAME_LOOKUP);
5594 return found;
5597 /* Helper function for lookup_name_fuzzy.
5598 Traverse binding level LVL, looking for good name matches for NAME
5599 (and BM). */
5600 static void
5601 consider_binding_level (tree name, best_match <tree, const char *> &bm,
5602 cp_binding_level *lvl, bool look_within_fields,
5603 enum lookup_name_fuzzy_kind kind)
5605 if (look_within_fields)
5606 if (lvl->this_entity && TREE_CODE (lvl->this_entity) == RECORD_TYPE)
5608 tree type = lvl->this_entity;
5609 bool want_type_p = (kind == FUZZY_LOOKUP_TYPENAME);
5610 tree best_matching_field
5611 = lookup_member_fuzzy (type, name, want_type_p);
5612 if (best_matching_field)
5613 bm.consider (IDENTIFIER_POINTER (best_matching_field));
5616 for (tree t = lvl->names; t; t = TREE_CHAIN (t))
5618 tree d = t;
5620 /* OVERLOADs or decls from using declaration are wrapped into
5621 TREE_LIST. */
5622 if (TREE_CODE (d) == TREE_LIST)
5623 d = OVL_FIRST (TREE_VALUE (d));
5625 /* Don't use bindings from implicitly declared functions,
5626 as they were likely misspellings themselves. */
5627 if (TREE_TYPE (d) == error_mark_node)
5628 continue;
5630 /* Skip anticipated decls of builtin functions. */
5631 if (TREE_CODE (d) == FUNCTION_DECL
5632 && DECL_BUILT_IN (d)
5633 && DECL_ANTICIPATED (d))
5634 continue;
5636 if (tree name = DECL_NAME (d))
5637 /* Ignore internal names with spaces in them. */
5638 if (!strchr (IDENTIFIER_POINTER (name), ' '))
5639 bm.consider (IDENTIFIER_POINTER (name));
5643 /* Subclass of deferred_diagnostic. Notify the user that the
5644 given macro was used before it was defined.
5645 This can be done in the C++ frontend since tokenization happens
5646 upfront. */
5648 class macro_use_before_def : public deferred_diagnostic
5650 public:
5651 /* Ctor. LOC is the location of the usage. MACRO is the
5652 macro that was used. */
5653 macro_use_before_def (location_t loc, cpp_hashnode *macro)
5654 : deferred_diagnostic (loc), m_macro (macro)
5656 gcc_assert (macro);
5659 ~macro_use_before_def ()
5661 if (is_suppressed_p ())
5662 return;
5664 source_location def_loc = cpp_macro_definition_location (m_macro);
5665 if (def_loc != UNKNOWN_LOCATION)
5667 inform (get_location (), "the macro %qs had not yet been defined",
5668 (const char *)m_macro->ident.str);
5669 inform (def_loc, "it was later defined here");
5673 private:
5674 cpp_hashnode *m_macro;
5678 /* Search for near-matches for NAME within the current bindings, and within
5679 macro names, returning the best match as a const char *, or NULL if
5680 no reasonable match is found.
5682 Use LOC for any deferred diagnostics. */
5684 name_hint
5685 lookup_name_fuzzy (tree name, enum lookup_name_fuzzy_kind kind, location_t loc)
5687 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
5689 /* First, try some well-known names in the C++ standard library, in case
5690 the user forgot a #include. */
5691 const char *header_hint
5692 = get_cp_stdlib_header_for_name (IDENTIFIER_POINTER (name));
5693 if (header_hint)
5694 return name_hint (NULL,
5695 new suggest_missing_header (loc,
5696 IDENTIFIER_POINTER (name),
5697 header_hint));
5699 best_match <tree, const char *> bm (name);
5701 cp_binding_level *lvl;
5702 for (lvl = scope_chain->class_bindings; lvl; lvl = lvl->level_chain)
5703 consider_binding_level (name, bm, lvl, true, kind);
5705 for (lvl = current_binding_level; lvl; lvl = lvl->level_chain)
5706 consider_binding_level (name, bm, lvl, false, kind);
5708 /* Consider macros: if the user misspelled a macro name e.g. "SOME_MACRO"
5710 x = SOME_OTHER_MACRO (y);
5711 then "SOME_OTHER_MACRO" will survive to the frontend and show up
5712 as a misspelled identifier.
5714 Use the best distance so far so that a candidate is only set if
5715 a macro is better than anything so far. This allows early rejection
5716 (without calculating the edit distance) of macro names that must have
5717 distance >= bm.get_best_distance (), and means that we only get a
5718 non-NULL result for best_macro_match if it's better than any of
5719 the identifiers already checked. */
5720 best_macro_match bmm (name, bm.get_best_distance (), parse_in);
5721 cpp_hashnode *best_macro = bmm.get_best_meaningful_candidate ();
5722 /* If a macro is the closest so far to NAME, consider it. */
5723 if (best_macro)
5724 bm.consider ((const char *)best_macro->ident.str);
5725 else if (bmm.get_best_distance () == 0)
5727 /* If we have an exact match for a macro name, then the
5728 macro has been used before it was defined. */
5729 cpp_hashnode *macro = bmm.blithely_get_best_candidate ();
5730 if (macro)
5731 return name_hint (NULL,
5732 new macro_use_before_def (loc, macro));
5735 /* Try the "starts_decl_specifier_p" keywords to detect
5736 "singed" vs "signed" typos. */
5737 for (unsigned i = 0; i < num_c_common_reswords; i++)
5739 const c_common_resword *resword = &c_common_reswords[i];
5741 if (kind == FUZZY_LOOKUP_TYPENAME)
5742 if (!cp_keyword_starts_decl_specifier_p (resword->rid))
5743 continue;
5745 tree resword_identifier = ridpointers [resword->rid];
5746 if (!resword_identifier)
5747 continue;
5748 gcc_assert (TREE_CODE (resword_identifier) == IDENTIFIER_NODE);
5750 /* Only consider reserved words that survived the
5751 filtering in init_reswords (e.g. for -std). */
5752 if (!IDENTIFIER_KEYWORD_P (resword_identifier))
5753 continue;
5755 bm.consider (IDENTIFIER_POINTER (resword_identifier));
5758 return name_hint (bm.get_best_meaningful_candidate (), NULL);
5761 /* Subroutine of outer_binding.
5763 Returns TRUE if BINDING is a binding to a template parameter of
5764 SCOPE. In that case SCOPE is the scope of a primary template
5765 parameter -- in the sense of G++, i.e, a template that has its own
5766 template header.
5768 Returns FALSE otherwise. */
5770 static bool
5771 binding_to_template_parms_of_scope_p (cxx_binding *binding,
5772 cp_binding_level *scope)
5774 tree binding_value, tmpl, tinfo;
5775 int level;
5777 if (!binding || !scope || !scope->this_entity)
5778 return false;
5780 binding_value = binding->value ? binding->value : binding->type;
5781 tinfo = get_template_info (scope->this_entity);
5783 /* BINDING_VALUE must be a template parm. */
5784 if (binding_value == NULL_TREE
5785 || (!DECL_P (binding_value)
5786 || !DECL_TEMPLATE_PARM_P (binding_value)))
5787 return false;
5789 /* The level of BINDING_VALUE. */
5790 level =
5791 template_type_parameter_p (binding_value)
5792 ? TEMPLATE_PARM_LEVEL (TEMPLATE_TYPE_PARM_INDEX
5793 (TREE_TYPE (binding_value)))
5794 : TEMPLATE_PARM_LEVEL (DECL_INITIAL (binding_value));
5796 /* The template of the current scope, iff said scope is a primary
5797 template. */
5798 tmpl = (tinfo
5799 && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
5800 ? TI_TEMPLATE (tinfo)
5801 : NULL_TREE);
5803 /* If the level of the parm BINDING_VALUE equals the depth of TMPL,
5804 then BINDING_VALUE is a parameter of TMPL. */
5805 return (tmpl && level == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)));
5808 /* Return the innermost non-namespace binding for NAME from a scope
5809 containing BINDING, or, if BINDING is NULL, the current scope.
5810 Please note that for a given template, the template parameters are
5811 considered to be in the scope containing the current scope.
5812 If CLASS_P is false, then class bindings are ignored. */
5814 cxx_binding *
5815 outer_binding (tree name,
5816 cxx_binding *binding,
5817 bool class_p)
5819 cxx_binding *outer;
5820 cp_binding_level *scope;
5821 cp_binding_level *outer_scope;
5823 if (binding)
5825 scope = binding->scope->level_chain;
5826 outer = binding->previous;
5828 else
5830 scope = current_binding_level;
5831 outer = IDENTIFIER_BINDING (name);
5833 outer_scope = outer ? outer->scope : NULL;
5835 /* Because we create class bindings lazily, we might be missing a
5836 class binding for NAME. If there are any class binding levels
5837 between the LAST_BINDING_LEVEL and the scope in which OUTER was
5838 declared, we must lookup NAME in those class scopes. */
5839 if (class_p)
5840 while (scope && scope != outer_scope && scope->kind != sk_namespace)
5842 if (scope->kind == sk_class)
5844 cxx_binding *class_binding;
5846 class_binding = get_class_binding (name, scope);
5847 if (class_binding)
5849 /* Thread this new class-scope binding onto the
5850 IDENTIFIER_BINDING list so that future lookups
5851 find it quickly. */
5852 class_binding->previous = outer;
5853 if (binding)
5854 binding->previous = class_binding;
5855 else
5856 IDENTIFIER_BINDING (name) = class_binding;
5857 return class_binding;
5860 /* If we are in a member template, the template parms of the member
5861 template are considered to be inside the scope of the containing
5862 class, but within G++ the class bindings are all pushed between the
5863 template parms and the function body. So if the outer binding is
5864 a template parm for the current scope, return it now rather than
5865 look for a class binding. */
5866 if (outer_scope && outer_scope->kind == sk_template_parms
5867 && binding_to_template_parms_of_scope_p (outer, scope))
5868 return outer;
5870 scope = scope->level_chain;
5873 return outer;
5876 /* Return the innermost block-scope or class-scope value binding for
5877 NAME, or NULL_TREE if there is no such binding. */
5879 tree
5880 innermost_non_namespace_value (tree name)
5882 cxx_binding *binding;
5883 binding = outer_binding (name, /*binding=*/NULL, /*class_p=*/true);
5884 return binding ? binding->value : NULL_TREE;
5887 /* Look up NAME in the current binding level and its superiors in the
5888 namespace of variables, functions and typedefs. Return a ..._DECL
5889 node of some kind representing its definition if there is only one
5890 such declaration, or return a TREE_LIST with all the overloaded
5891 definitions if there are many, or return 0 if it is undefined.
5892 Hidden name, either friend declaration or built-in function, are
5893 not ignored.
5895 If PREFER_TYPE is > 0, we prefer TYPE_DECLs or namespaces.
5896 If PREFER_TYPE is > 1, we reject non-type decls (e.g. namespaces).
5897 Otherwise we prefer non-TYPE_DECLs.
5899 If NONCLASS is nonzero, bindings in class scopes are ignored. If
5900 BLOCK_P is false, bindings in block scopes are ignored. */
5902 static tree
5903 lookup_name_real_1 (tree name, int prefer_type, int nonclass, bool block_p,
5904 int namespaces_only, int flags)
5906 cxx_binding *iter;
5907 tree val = NULL_TREE;
5909 query_oracle (name);
5911 /* Conversion operators are handled specially because ordinary
5912 unqualified name lookup will not find template conversion
5913 operators. */
5914 if (IDENTIFIER_CONV_OP_P (name))
5916 cp_binding_level *level;
5918 for (level = current_binding_level;
5919 level && level->kind != sk_namespace;
5920 level = level->level_chain)
5922 tree class_type;
5923 tree operators;
5925 /* A conversion operator can only be declared in a class
5926 scope. */
5927 if (level->kind != sk_class)
5928 continue;
5930 /* Lookup the conversion operator in the class. */
5931 class_type = level->this_entity;
5932 operators = lookup_fnfields (class_type, name, /*protect=*/0);
5933 if (operators)
5934 return operators;
5937 return NULL_TREE;
5940 flags |= lookup_flags (prefer_type, namespaces_only);
5942 /* First, look in non-namespace scopes. */
5944 if (current_class_type == NULL_TREE)
5945 nonclass = 1;
5947 if (block_p || !nonclass)
5948 for (iter = outer_binding (name, NULL, !nonclass);
5949 iter;
5950 iter = outer_binding (name, iter, !nonclass))
5952 tree binding;
5954 /* Skip entities we don't want. */
5955 if (LOCAL_BINDING_P (iter) ? !block_p : nonclass)
5956 continue;
5958 /* If this is the kind of thing we're looking for, we're done. */
5959 if (qualify_lookup (iter->value, flags))
5960 binding = iter->value;
5961 else if ((flags & LOOKUP_PREFER_TYPES)
5962 && qualify_lookup (iter->type, flags))
5963 binding = iter->type;
5964 else
5965 binding = NULL_TREE;
5967 if (binding)
5969 if (TREE_CODE (binding) == TYPE_DECL && DECL_HIDDEN_P (binding))
5971 /* A non namespace-scope binding can only be hidden in the
5972 presence of a local class, due to friend declarations.
5974 In particular, consider:
5976 struct C;
5977 void f() {
5978 struct A {
5979 friend struct B;
5980 friend struct C;
5981 void g() {
5982 B* b; // error: B is hidden
5983 C* c; // OK, finds ::C
5986 B *b; // error: B is hidden
5987 C *c; // OK, finds ::C
5988 struct B {};
5989 B *bb; // OK
5992 The standard says that "B" is a local class in "f"
5993 (but not nested within "A") -- but that name lookup
5994 for "B" does not find this declaration until it is
5995 declared directly with "f".
5997 In particular:
5999 [class.friend]
6001 If a friend declaration appears in a local class and
6002 the name specified is an unqualified name, a prior
6003 declaration is looked up without considering scopes
6004 that are outside the innermost enclosing non-class
6005 scope. For a friend function declaration, if there is
6006 no prior declaration, the program is ill-formed. For a
6007 friend class declaration, if there is no prior
6008 declaration, the class that is specified belongs to the
6009 innermost enclosing non-class scope, but if it is
6010 subsequently referenced, its name is not found by name
6011 lookup until a matching declaration is provided in the
6012 innermost enclosing nonclass scope.
6014 So just keep looking for a non-hidden binding.
6016 gcc_assert (TREE_CODE (binding) == TYPE_DECL);
6017 continue;
6019 val = binding;
6020 break;
6024 /* Now lookup in namespace scopes. */
6025 if (!val)
6027 name_lookup lookup (name, flags);
6028 if (lookup.search_unqualified
6029 (current_decl_namespace (), current_binding_level))
6030 val = lookup.value;
6033 /* If we have a single function from a using decl, pull it out. */
6034 if (val && TREE_CODE (val) == OVERLOAD && !really_overloaded_fn (val))
6035 val = OVL_FUNCTION (val);
6037 return val;
6040 /* Wrapper for lookup_name_real_1. */
6042 tree
6043 lookup_name_real (tree name, int prefer_type, int nonclass, bool block_p,
6044 int namespaces_only, int flags)
6046 tree ret;
6047 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6048 ret = lookup_name_real_1 (name, prefer_type, nonclass, block_p,
6049 namespaces_only, flags);
6050 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6051 return ret;
6054 tree
6055 lookup_name_nonclass (tree name)
6057 return lookup_name_real (name, 0, 1, /*block_p=*/true, 0, 0);
6060 tree
6061 lookup_name (tree name)
6063 return lookup_name_real (name, 0, 0, /*block_p=*/true, 0, 0);
6066 tree
6067 lookup_name_prefer_type (tree name, int prefer_type)
6069 return lookup_name_real (name, prefer_type, 0, /*block_p=*/true, 0, 0);
6072 /* Look up NAME for type used in elaborated name specifier in
6073 the scopes given by SCOPE. SCOPE can be either TS_CURRENT or
6074 TS_WITHIN_ENCLOSING_NON_CLASS. Although not implied by the
6075 name, more scopes are checked if cleanup or template parameter
6076 scope is encountered.
6078 Unlike lookup_name_real, we make sure that NAME is actually
6079 declared in the desired scope, not from inheritance, nor using
6080 directive. For using declaration, there is DR138 still waiting
6081 to be resolved. Hidden name coming from an earlier friend
6082 declaration is also returned.
6084 A TYPE_DECL best matching the NAME is returned. Catching error
6085 and issuing diagnostics are caller's responsibility. */
6087 static tree
6088 lookup_type_scope_1 (tree name, tag_scope scope)
6090 cxx_binding *iter = NULL;
6091 tree val = NULL_TREE;
6092 cp_binding_level *level = NULL;
6094 /* Look in non-namespace scope first. */
6095 if (current_binding_level->kind != sk_namespace)
6096 iter = outer_binding (name, NULL, /*class_p=*/ true);
6097 for (; iter; iter = outer_binding (name, iter, /*class_p=*/ true))
6099 /* Check if this is the kind of thing we're looking for.
6100 If SCOPE is TS_CURRENT, also make sure it doesn't come from
6101 base class. For ITER->VALUE, we can simply use
6102 INHERITED_VALUE_BINDING_P. For ITER->TYPE, we have to use
6103 our own check.
6105 We check ITER->TYPE before ITER->VALUE in order to handle
6106 typedef struct C {} C;
6107 correctly. */
6109 if (qualify_lookup (iter->type, LOOKUP_PREFER_TYPES)
6110 && (scope != ts_current
6111 || LOCAL_BINDING_P (iter)
6112 || DECL_CONTEXT (iter->type) == iter->scope->this_entity))
6113 val = iter->type;
6114 else if ((scope != ts_current
6115 || !INHERITED_VALUE_BINDING_P (iter))
6116 && qualify_lookup (iter->value, LOOKUP_PREFER_TYPES))
6117 val = iter->value;
6119 if (val)
6120 break;
6123 /* Look in namespace scope. */
6124 if (val)
6125 level = iter->scope;
6126 else
6128 tree ns = current_decl_namespace ();
6130 if (tree *slot = find_namespace_slot (ns, name))
6132 /* If this is the kind of thing we're looking for, we're done. */
6133 if (tree type = MAYBE_STAT_TYPE (*slot))
6134 if (qualify_lookup (type, LOOKUP_PREFER_TYPES))
6135 val = type;
6136 if (!val)
6138 if (tree decl = MAYBE_STAT_DECL (*slot))
6139 if (qualify_lookup (decl, LOOKUP_PREFER_TYPES))
6140 val = decl;
6142 level = NAMESPACE_LEVEL (ns);
6146 /* Type found, check if it is in the allowed scopes, ignoring cleanup
6147 and template parameter scopes. */
6148 if (val)
6150 cp_binding_level *b = current_binding_level;
6151 while (b)
6153 if (level == b)
6154 return val;
6156 if (b->kind == sk_cleanup || b->kind == sk_template_parms
6157 || b->kind == sk_function_parms)
6158 b = b->level_chain;
6159 else if (b->kind == sk_class
6160 && scope == ts_within_enclosing_non_class)
6161 b = b->level_chain;
6162 else
6163 break;
6167 return NULL_TREE;
6170 /* Wrapper for lookup_type_scope_1. */
6172 tree
6173 lookup_type_scope (tree name, tag_scope scope)
6175 tree ret;
6176 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6177 ret = lookup_type_scope_1 (name, scope);
6178 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6179 return ret;
6182 /* Returns true iff DECL is a block-scope extern declaration of a function
6183 or variable. */
6185 bool
6186 is_local_extern (tree decl)
6188 cxx_binding *binding;
6190 /* For functions, this is easy. */
6191 if (TREE_CODE (decl) == FUNCTION_DECL)
6192 return DECL_LOCAL_FUNCTION_P (decl);
6194 if (!VAR_P (decl))
6195 return false;
6196 if (!current_function_decl)
6197 return false;
6199 /* For variables, this is not easy. We need to look at the binding stack
6200 for the identifier to see whether the decl we have is a local. */
6201 for (binding = IDENTIFIER_BINDING (DECL_NAME (decl));
6202 binding && binding->scope->kind != sk_namespace;
6203 binding = binding->previous)
6204 if (binding->value == decl)
6205 return LOCAL_BINDING_P (binding);
6207 return false;
6210 /* The type TYPE is being declared. If it is a class template, or a
6211 specialization of a class template, do any processing required and
6212 perform error-checking. If IS_FRIEND is nonzero, this TYPE is
6213 being declared a friend. B is the binding level at which this TYPE
6214 should be bound.
6216 Returns the TYPE_DECL for TYPE, which may have been altered by this
6217 processing. */
6219 static tree
6220 maybe_process_template_type_declaration (tree type, int is_friend,
6221 cp_binding_level *b)
6223 tree decl = TYPE_NAME (type);
6225 if (processing_template_parmlist)
6226 /* You can't declare a new template type in a template parameter
6227 list. But, you can declare a non-template type:
6229 template <class A*> struct S;
6231 is a forward-declaration of `A'. */
6233 else if (b->kind == sk_namespace
6234 && current_binding_level->kind != sk_namespace)
6235 /* If this new type is being injected into a containing scope,
6236 then it's not a template type. */
6238 else
6240 gcc_assert (MAYBE_CLASS_TYPE_P (type)
6241 || TREE_CODE (type) == ENUMERAL_TYPE);
6243 if (processing_template_decl)
6245 /* This may change after the call to
6246 push_template_decl_real, but we want the original value. */
6247 tree name = DECL_NAME (decl);
6249 decl = push_template_decl_real (decl, is_friend);
6250 if (decl == error_mark_node)
6251 return error_mark_node;
6253 /* If the current binding level is the binding level for the
6254 template parameters (see the comment in
6255 begin_template_parm_list) and the enclosing level is a class
6256 scope, and we're not looking at a friend, push the
6257 declaration of the member class into the class scope. In the
6258 friend case, push_template_decl will already have put the
6259 friend into global scope, if appropriate. */
6260 if (TREE_CODE (type) != ENUMERAL_TYPE
6261 && !is_friend && b->kind == sk_template_parms
6262 && b->level_chain->kind == sk_class)
6264 finish_member_declaration (CLASSTYPE_TI_TEMPLATE (type));
6266 if (!COMPLETE_TYPE_P (current_class_type))
6268 maybe_add_class_template_decl_list (current_class_type,
6269 type, /*friend_p=*/0);
6270 /* Put this UTD in the table of UTDs for the class. */
6271 if (CLASSTYPE_NESTED_UTDS (current_class_type) == NULL)
6272 CLASSTYPE_NESTED_UTDS (current_class_type) =
6273 binding_table_new (SCOPE_DEFAULT_HT_SIZE);
6275 binding_table_insert
6276 (CLASSTYPE_NESTED_UTDS (current_class_type), name, type);
6282 return decl;
6285 /* Push a tag name NAME for struct/class/union/enum type TYPE. In case
6286 that the NAME is a class template, the tag is processed but not pushed.
6288 The pushed scope depend on the SCOPE parameter:
6289 - When SCOPE is TS_CURRENT, put it into the inner-most non-sk_cleanup
6290 scope.
6291 - When SCOPE is TS_GLOBAL, put it in the inner-most non-class and
6292 non-template-parameter scope. This case is needed for forward
6293 declarations.
6294 - When SCOPE is TS_WITHIN_ENCLOSING_NON_CLASS, this is similar to
6295 TS_GLOBAL case except that names within template-parameter scopes
6296 are not pushed at all.
6298 Returns TYPE upon success and ERROR_MARK_NODE otherwise. */
6300 static tree
6301 do_pushtag (tree name, tree type, tag_scope scope)
6303 tree decl;
6305 cp_binding_level *b = current_binding_level;
6306 while (/* Cleanup scopes are not scopes from the point of view of
6307 the language. */
6308 b->kind == sk_cleanup
6309 /* Neither are function parameter scopes. */
6310 || b->kind == sk_function_parms
6311 /* Neither are the scopes used to hold template parameters
6312 for an explicit specialization. For an ordinary template
6313 declaration, these scopes are not scopes from the point of
6314 view of the language. */
6315 || (b->kind == sk_template_parms
6316 && (b->explicit_spec_p || scope == ts_global))
6317 || (b->kind == sk_class
6318 && (scope != ts_current
6319 /* We may be defining a new type in the initializer
6320 of a static member variable. We allow this when
6321 not pedantic, and it is particularly useful for
6322 type punning via an anonymous union. */
6323 || COMPLETE_TYPE_P (b->this_entity))))
6324 b = b->level_chain;
6326 gcc_assert (identifier_p (name));
6328 /* Do C++ gratuitous typedefing. */
6329 if (identifier_type_value_1 (name) != type)
6331 tree tdef;
6332 int in_class = 0;
6333 tree context = TYPE_CONTEXT (type);
6335 if (! context)
6337 tree cs = current_scope ();
6339 if (scope == ts_current
6340 || (cs && TREE_CODE (cs) == FUNCTION_DECL))
6341 context = cs;
6342 else if (cs && TYPE_P (cs))
6343 /* When declaring a friend class of a local class, we want
6344 to inject the newly named class into the scope
6345 containing the local class, not the namespace
6346 scope. */
6347 context = decl_function_context (get_type_decl (cs));
6349 if (!context)
6350 context = current_namespace;
6352 if (b->kind == sk_class
6353 || (b->kind == sk_template_parms
6354 && b->level_chain->kind == sk_class))
6355 in_class = 1;
6357 tdef = create_implicit_typedef (name, type);
6358 DECL_CONTEXT (tdef) = FROB_CONTEXT (context);
6359 if (scope == ts_within_enclosing_non_class)
6361 /* This is a friend. Make this TYPE_DECL node hidden from
6362 ordinary name lookup. Its corresponding TEMPLATE_DECL
6363 will be marked in push_template_decl_real. */
6364 retrofit_lang_decl (tdef);
6365 DECL_ANTICIPATED (tdef) = 1;
6366 DECL_FRIEND_P (tdef) = 1;
6369 decl = maybe_process_template_type_declaration
6370 (type, scope == ts_within_enclosing_non_class, b);
6371 if (decl == error_mark_node)
6372 return decl;
6374 if (b->kind == sk_class)
6376 if (!TYPE_BEING_DEFINED (current_class_type))
6377 return error_mark_node;
6379 if (!PROCESSING_REAL_TEMPLATE_DECL_P ())
6380 /* Put this TYPE_DECL on the TYPE_FIELDS list for the
6381 class. But if it's a member template class, we want
6382 the TEMPLATE_DECL, not the TYPE_DECL, so this is done
6383 later. */
6384 finish_member_declaration (decl);
6385 else
6386 pushdecl_class_level (decl);
6388 else if (b->kind != sk_template_parms)
6390 decl = do_pushdecl_with_scope (decl, b, /*is_friend=*/false);
6391 if (decl == error_mark_node)
6392 return decl;
6394 if (DECL_CONTEXT (decl) == std_node
6395 && init_list_identifier == DECL_NAME (TYPE_NAME (type))
6396 && !CLASSTYPE_TEMPLATE_INFO (type))
6398 error ("declaration of std::initializer_list does not match "
6399 "#include <initializer_list>, isn't a template");
6400 return error_mark_node;
6404 if (! in_class)
6405 set_identifier_type_value_with_scope (name, tdef, b);
6407 TYPE_CONTEXT (type) = DECL_CONTEXT (decl);
6409 /* If this is a local class, keep track of it. We need this
6410 information for name-mangling, and so that it is possible to
6411 find all function definitions in a translation unit in a
6412 convenient way. (It's otherwise tricky to find a member
6413 function definition it's only pointed to from within a local
6414 class.) */
6415 if (TYPE_FUNCTION_SCOPE_P (type))
6417 if (processing_template_decl)
6419 /* Push a DECL_EXPR so we call pushtag at the right time in
6420 template instantiation rather than in some nested context. */
6421 add_decl_expr (decl);
6423 else
6424 vec_safe_push (local_classes, type);
6428 if (b->kind == sk_class
6429 && !COMPLETE_TYPE_P (current_class_type))
6431 maybe_add_class_template_decl_list (current_class_type,
6432 type, /*friend_p=*/0);
6434 if (CLASSTYPE_NESTED_UTDS (current_class_type) == NULL)
6435 CLASSTYPE_NESTED_UTDS (current_class_type)
6436 = binding_table_new (SCOPE_DEFAULT_HT_SIZE);
6438 binding_table_insert
6439 (CLASSTYPE_NESTED_UTDS (current_class_type), name, type);
6442 decl = TYPE_NAME (type);
6443 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
6445 /* Set type visibility now if this is a forward declaration. */
6446 TREE_PUBLIC (decl) = 1;
6447 determine_visibility (decl);
6449 return type;
6452 /* Wrapper for do_pushtag. */
6454 tree
6455 pushtag (tree name, tree type, tag_scope scope)
6457 tree ret;
6458 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6459 ret = do_pushtag (name, type, scope);
6460 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6461 return ret;
6465 /* Subroutines for reverting temporarily to top-level for instantiation
6466 of templates and such. We actually need to clear out the class- and
6467 local-value slots of all identifiers, so that only the global values
6468 are at all visible. Simply setting current_binding_level to the global
6469 scope isn't enough, because more binding levels may be pushed. */
6470 struct saved_scope *scope_chain;
6472 /* Return true if ID has not already been marked. */
6474 static inline bool
6475 store_binding_p (tree id)
6477 if (!id || !IDENTIFIER_BINDING (id))
6478 return false;
6480 if (IDENTIFIER_MARKED (id))
6481 return false;
6483 return true;
6486 /* Add an appropriate binding to *OLD_BINDINGS which needs to already
6487 have enough space reserved. */
6489 static void
6490 store_binding (tree id, vec<cxx_saved_binding, va_gc> **old_bindings)
6492 cxx_saved_binding saved;
6494 gcc_checking_assert (store_binding_p (id));
6496 IDENTIFIER_MARKED (id) = 1;
6498 saved.identifier = id;
6499 saved.binding = IDENTIFIER_BINDING (id);
6500 saved.real_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
6501 (*old_bindings)->quick_push (saved);
6502 IDENTIFIER_BINDING (id) = NULL;
6505 static void
6506 store_bindings (tree names, vec<cxx_saved_binding, va_gc> **old_bindings)
6508 static vec<tree> bindings_need_stored;
6509 tree t, id;
6510 size_t i;
6512 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6513 for (t = names; t; t = TREE_CHAIN (t))
6515 if (TREE_CODE (t) == TREE_LIST)
6516 id = TREE_PURPOSE (t);
6517 else
6518 id = DECL_NAME (t);
6520 if (store_binding_p (id))
6521 bindings_need_stored.safe_push (id);
6523 if (!bindings_need_stored.is_empty ())
6525 vec_safe_reserve_exact (*old_bindings, bindings_need_stored.length ());
6526 for (i = 0; bindings_need_stored.iterate (i, &id); ++i)
6528 /* We can apparently have duplicates in NAMES. */
6529 if (store_binding_p (id))
6530 store_binding (id, old_bindings);
6532 bindings_need_stored.truncate (0);
6534 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6537 /* Like store_bindings, but NAMES is a vector of cp_class_binding
6538 objects, rather than a TREE_LIST. */
6540 static void
6541 store_class_bindings (vec<cp_class_binding, va_gc> *names,
6542 vec<cxx_saved_binding, va_gc> **old_bindings)
6544 static vec<tree> bindings_need_stored;
6545 size_t i;
6546 cp_class_binding *cb;
6548 for (i = 0; vec_safe_iterate (names, i, &cb); ++i)
6549 if (store_binding_p (cb->identifier))
6550 bindings_need_stored.safe_push (cb->identifier);
6551 if (!bindings_need_stored.is_empty ())
6553 tree id;
6554 vec_safe_reserve_exact (*old_bindings, bindings_need_stored.length ());
6555 for (i = 0; bindings_need_stored.iterate (i, &id); ++i)
6556 store_binding (id, old_bindings);
6557 bindings_need_stored.truncate (0);
6561 /* A chain of saved_scope structures awaiting reuse. */
6563 static GTY((deletable)) struct saved_scope *free_saved_scope;
6565 static void
6566 do_push_to_top_level (void)
6568 struct saved_scope *s;
6569 cp_binding_level *b;
6570 cxx_saved_binding *sb;
6571 size_t i;
6572 bool need_pop;
6574 /* Reuse or create a new structure for this saved scope. */
6575 if (free_saved_scope != NULL)
6577 s = free_saved_scope;
6578 free_saved_scope = s->prev;
6580 vec<cxx_saved_binding, va_gc> *old_bindings = s->old_bindings;
6581 memset (s, 0, sizeof (*s));
6582 /* Also reuse the structure's old_bindings vector. */
6583 vec_safe_truncate (old_bindings, 0);
6584 s->old_bindings = old_bindings;
6586 else
6587 s = ggc_cleared_alloc<saved_scope> ();
6589 b = scope_chain ? current_binding_level : 0;
6591 /* If we're in the middle of some function, save our state. */
6592 if (cfun)
6594 need_pop = true;
6595 push_function_context ();
6597 else
6598 need_pop = false;
6600 if (scope_chain && previous_class_level)
6601 store_class_bindings (previous_class_level->class_shadowed,
6602 &s->old_bindings);
6604 /* Have to include the global scope, because class-scope decls
6605 aren't listed anywhere useful. */
6606 for (; b; b = b->level_chain)
6608 tree t;
6610 /* Template IDs are inserted into the global level. If they were
6611 inserted into namespace level, finish_file wouldn't find them
6612 when doing pending instantiations. Therefore, don't stop at
6613 namespace level, but continue until :: . */
6614 if (global_scope_p (b))
6615 break;
6617 store_bindings (b->names, &s->old_bindings);
6618 /* We also need to check class_shadowed to save class-level type
6619 bindings, since pushclass doesn't fill in b->names. */
6620 if (b->kind == sk_class)
6621 store_class_bindings (b->class_shadowed, &s->old_bindings);
6623 /* Unwind type-value slots back to top level. */
6624 for (t = b->type_shadowed; t; t = TREE_CHAIN (t))
6625 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (t), TREE_VALUE (t));
6628 FOR_EACH_VEC_SAFE_ELT (s->old_bindings, i, sb)
6629 IDENTIFIER_MARKED (sb->identifier) = 0;
6631 s->prev = scope_chain;
6632 s->bindings = b;
6633 s->need_pop_function_context = need_pop;
6634 s->function_decl = current_function_decl;
6635 s->unevaluated_operand = cp_unevaluated_operand;
6636 s->inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
6637 s->x_stmt_tree.stmts_are_full_exprs_p = true;
6639 scope_chain = s;
6640 current_function_decl = NULL_TREE;
6641 vec_alloc (current_lang_base, 10);
6642 current_lang_name = lang_name_cplusplus;
6643 current_namespace = global_namespace;
6644 push_class_stack ();
6645 cp_unevaluated_operand = 0;
6646 c_inhibit_evaluation_warnings = 0;
6649 static void
6650 do_pop_from_top_level (void)
6652 struct saved_scope *s = scope_chain;
6653 cxx_saved_binding *saved;
6654 size_t i;
6656 /* Clear out class-level bindings cache. */
6657 if (previous_class_level)
6658 invalidate_class_lookup_cache ();
6659 pop_class_stack ();
6661 current_lang_base = 0;
6663 scope_chain = s->prev;
6664 FOR_EACH_VEC_SAFE_ELT (s->old_bindings, i, saved)
6666 tree id = saved->identifier;
6668 IDENTIFIER_BINDING (id) = saved->binding;
6669 SET_IDENTIFIER_TYPE_VALUE (id, saved->real_type_value);
6672 /* If we were in the middle of compiling a function, restore our
6673 state. */
6674 if (s->need_pop_function_context)
6675 pop_function_context ();
6676 current_function_decl = s->function_decl;
6677 cp_unevaluated_operand = s->unevaluated_operand;
6678 c_inhibit_evaluation_warnings = s->inhibit_evaluation_warnings;
6680 /* Make this saved_scope structure available for reuse by
6681 push_to_top_level. */
6682 s->prev = free_saved_scope;
6683 free_saved_scope = s;
6686 /* Push into the scope of the namespace NS, even if it is deeply
6687 nested within another namespace. */
6689 static void
6690 do_push_nested_namespace (tree ns)
6692 if (ns == global_namespace)
6693 do_push_to_top_level ();
6694 else
6696 do_push_nested_namespace (CP_DECL_CONTEXT (ns));
6697 gcc_checking_assert
6698 (find_namespace_value (current_namespace, DECL_NAME (ns)) == ns);
6699 resume_scope (NAMESPACE_LEVEL (ns));
6700 current_namespace = ns;
6704 /* Pop back from the scope of the namespace NS, which was previously
6705 entered with push_nested_namespace. */
6707 static void
6708 do_pop_nested_namespace (tree ns)
6710 while (ns != global_namespace)
6712 ns = CP_DECL_CONTEXT (ns);
6713 current_namespace = ns;
6714 leave_scope ();
6717 do_pop_from_top_level ();
6720 /* Add TARGET to USINGS, if it does not already exist there.
6721 We used to build the complete graph of usings at this point, from
6722 the POV of the source namespaces. Now we build that as we perform
6723 the unqualified search. */
6725 static void
6726 add_using_namespace (vec<tree, va_gc> *&usings, tree target)
6728 if (usings)
6729 for (unsigned ix = usings->length (); ix--;)
6730 if ((*usings)[ix] == target)
6731 return;
6733 vec_safe_push (usings, target);
6736 /* Tell the debug system of a using directive. */
6738 static void
6739 emit_debug_info_using_namespace (tree from, tree target, bool implicit)
6741 /* Emit debugging info. */
6742 tree context = from != global_namespace ? from : NULL_TREE;
6743 debug_hooks->imported_module_or_decl (target, NULL_TREE, context, false,
6744 implicit);
6747 /* Process a namespace-scope using directive. */
6749 void
6750 finish_namespace_using_directive (tree target, tree attribs)
6752 gcc_checking_assert (namespace_bindings_p ());
6753 if (target == error_mark_node)
6754 return;
6756 add_using_namespace (DECL_NAMESPACE_USING (current_namespace),
6757 ORIGINAL_NAMESPACE (target));
6758 emit_debug_info_using_namespace (current_namespace,
6759 ORIGINAL_NAMESPACE (target), false);
6761 if (attribs == error_mark_node)
6762 return;
6764 for (tree a = attribs; a; a = TREE_CHAIN (a))
6766 tree name = get_attribute_name (a);
6767 if (is_attribute_p ("strong", name))
6769 warning (0, "strong using directive no longer supported");
6770 if (CP_DECL_CONTEXT (target) == current_namespace)
6771 inform (DECL_SOURCE_LOCATION (target),
6772 "you may use an inline namespace instead");
6774 else
6775 warning (OPT_Wattributes, "%qD attribute directive ignored", name);
6779 /* Process a function-scope using-directive. */
6781 void
6782 finish_local_using_directive (tree target, tree attribs)
6784 gcc_checking_assert (local_bindings_p ());
6785 if (target == error_mark_node)
6786 return;
6788 if (attribs)
6789 warning (OPT_Wattributes, "attributes ignored on local using directive");
6791 add_stmt (build_stmt (input_location, USING_STMT, target));
6793 add_using_namespace (current_binding_level->using_directives,
6794 ORIGINAL_NAMESPACE (target));
6797 /* Pushes X into the global namespace. */
6799 tree
6800 pushdecl_top_level (tree x, bool is_friend)
6802 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6803 do_push_to_top_level ();
6804 x = pushdecl_namespace_level (x, is_friend);
6805 do_pop_from_top_level ();
6806 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6807 return x;
6810 /* Pushes X into the global namespace and calls cp_finish_decl to
6811 register the variable, initializing it with INIT. */
6813 tree
6814 pushdecl_top_level_and_finish (tree x, tree init)
6816 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6817 do_push_to_top_level ();
6818 x = pushdecl_namespace_level (x, false);
6819 cp_finish_decl (x, init, false, NULL_TREE, 0);
6820 do_pop_from_top_level ();
6821 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6822 return x;
6825 /* Enter the namespaces from current_namerspace to NS. */
6827 static int
6828 push_inline_namespaces (tree ns)
6830 int count = 0;
6831 if (ns != current_namespace)
6833 gcc_assert (ns != global_namespace);
6834 count += push_inline_namespaces (CP_DECL_CONTEXT (ns));
6835 resume_scope (NAMESPACE_LEVEL (ns));
6836 current_namespace = ns;
6837 count++;
6839 return count;
6842 /* Push into the scope of the NAME namespace. If NAME is NULL_TREE,
6843 then we enter an anonymous namespace. If MAKE_INLINE is true, then
6844 we create an inline namespace (it is up to the caller to check upon
6845 redefinition). Return the number of namespaces entered. */
6848 push_namespace (tree name, bool make_inline)
6850 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6851 int count = 0;
6853 /* We should not get here if the global_namespace is not yet constructed
6854 nor if NAME designates the global namespace: The global scope is
6855 constructed elsewhere. */
6856 gcc_checking_assert (global_namespace != NULL && name != global_identifier);
6858 tree ns = NULL_TREE;
6860 name_lookup lookup (name, 0);
6861 if (!lookup.search_qualified (current_namespace, /*usings=*/false))
6863 else if (TREE_CODE (lookup.value) != NAMESPACE_DECL)
6865 else if (tree dna = DECL_NAMESPACE_ALIAS (lookup.value))
6867 /* A namespace alias is not allowed here, but if the alias
6868 is for a namespace also inside the current scope,
6869 accept it with a diagnostic. That's better than dying
6870 horribly. */
6871 if (is_nested_namespace (current_namespace, CP_DECL_CONTEXT (dna)))
6873 error ("namespace alias %qD not allowed here, "
6874 "assuming %qD", lookup.value, dna);
6875 ns = dna;
6878 else
6879 ns = lookup.value;
6882 bool new_ns = false;
6883 if (ns)
6884 /* DR2061. NS might be a member of an inline namespace. We
6885 need to push into those namespaces. */
6886 count += push_inline_namespaces (CP_DECL_CONTEXT (ns));
6887 else
6889 ns = build_lang_decl (NAMESPACE_DECL, name, void_type_node);
6890 SCOPE_DEPTH (ns) = SCOPE_DEPTH (current_namespace) + 1;
6891 if (!SCOPE_DEPTH (ns))
6892 /* We only allow depth 255. */
6893 sorry ("cannot nest more than %d namespaces",
6894 SCOPE_DEPTH (current_namespace));
6895 DECL_CONTEXT (ns) = FROB_CONTEXT (current_namespace);
6896 new_ns = true;
6898 if (pushdecl (ns) == error_mark_node)
6899 ns = NULL_TREE;
6900 else
6902 if (!name)
6904 SET_DECL_ASSEMBLER_NAME (ns, anon_identifier);
6906 if (!make_inline)
6907 add_using_namespace (DECL_NAMESPACE_USING (current_namespace),
6908 ns);
6910 else if (TREE_PUBLIC (current_namespace))
6911 TREE_PUBLIC (ns) = 1;
6913 if (make_inline)
6915 DECL_NAMESPACE_INLINE_P (ns) = true;
6916 vec_safe_push (DECL_NAMESPACE_INLINEES (current_namespace), ns);
6919 if (!name || make_inline)
6920 emit_debug_info_using_namespace (current_namespace, ns, true);
6924 if (ns)
6926 if (make_inline && !DECL_NAMESPACE_INLINE_P (ns))
6928 error ("inline namespace must be specified at initial definition");
6929 inform (DECL_SOURCE_LOCATION (ns), "%qD defined here", ns);
6931 if (new_ns)
6932 begin_scope (sk_namespace, ns);
6933 else
6934 resume_scope (NAMESPACE_LEVEL (ns));
6935 current_namespace = ns;
6936 count++;
6939 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6940 return count;
6943 /* Pop from the scope of the current namespace. */
6945 void
6946 pop_namespace (void)
6948 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6950 gcc_assert (current_namespace != global_namespace);
6951 current_namespace = CP_DECL_CONTEXT (current_namespace);
6952 /* The binding level is not popped, as it might be re-opened later. */
6953 leave_scope ();
6955 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6958 /* External entry points for do_{push_to/pop_from}_top_level. */
6960 void
6961 push_to_top_level (void)
6963 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6964 do_push_to_top_level ();
6965 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6968 void
6969 pop_from_top_level (void)
6971 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6972 do_pop_from_top_level ();
6973 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6976 /* External entry points for do_{push,pop}_nested_namespace. */
6978 void
6979 push_nested_namespace (tree ns)
6981 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6982 do_push_nested_namespace (ns);
6983 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6986 void
6987 pop_nested_namespace (tree ns)
6989 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6990 gcc_assert (current_namespace == ns);
6991 do_pop_nested_namespace (ns);
6992 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6995 /* Pop off extraneous binding levels left over due to syntax errors.
6996 We don't pop past namespaces, as they might be valid. */
6998 void
6999 pop_everything (void)
7001 if (ENABLE_SCOPE_CHECKING)
7002 verbatim ("XXX entering pop_everything ()\n");
7003 while (!namespace_bindings_p ())
7005 if (current_binding_level->kind == sk_class)
7006 pop_nested_class ();
7007 else
7008 poplevel (0, 0, 0);
7010 if (ENABLE_SCOPE_CHECKING)
7011 verbatim ("XXX leaving pop_everything ()\n");
7014 /* Emit debugging information for using declarations and directives.
7015 If input tree is overloaded fn then emit debug info for all
7016 candidates. */
7018 void
7019 cp_emit_debug_info_for_using (tree t, tree context)
7021 /* Don't try to emit any debug information if we have errors. */
7022 if (seen_error ())
7023 return;
7025 /* Ignore this FUNCTION_DECL if it refers to a builtin declaration
7026 of a builtin function. */
7027 if (TREE_CODE (t) == FUNCTION_DECL
7028 && DECL_EXTERNAL (t)
7029 && DECL_BUILT_IN (t))
7030 return;
7032 /* Do not supply context to imported_module_or_decl, if
7033 it is a global namespace. */
7034 if (context == global_namespace)
7035 context = NULL_TREE;
7037 t = MAYBE_BASELINK_FUNCTIONS (t);
7039 /* FIXME: Handle TEMPLATE_DECLs. */
7040 for (lkp_iterator iter (t); iter; ++iter)
7042 tree fn = *iter;
7043 if (TREE_CODE (fn) != TEMPLATE_DECL)
7045 if (building_stmt_list_p ())
7046 add_stmt (build_stmt (input_location, USING_STMT, fn));
7047 else
7048 debug_hooks->imported_module_or_decl (fn, NULL_TREE, context,
7049 false, false);
7054 #include "gt-cp-name-lookup.h"