1 /* Definitions for C++ name lookup routines.
2 Copyright (C) 2003, 2004, 2005, 2006 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 2, or (at your option)
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 COPYING. If not, write to
19 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA. */
24 #include "coretypes.h"
29 #include "name-lookup.h"
32 #include "diagnostic.h"
36 /* The bindings for a particular name in a particular scope. */
38 struct scope_binding {
42 #define EMPTY_SCOPE_BINDING { NULL_TREE, NULL_TREE }
44 static cxx_scope *innermost_nonclass_level (void);
45 static tree select_decl (const struct scope_binding *, int);
46 static cxx_binding *binding_for_name (cxx_scope *, tree);
47 static tree lookup_name_innermost_nonclass_level (tree);
48 static tree push_overloaded_decl (tree, int, bool);
49 static bool lookup_using_namespace (tree, struct scope_binding *, tree,
51 static bool qualified_lookup_using_namespace (tree, tree,
52 struct scope_binding *, int);
53 static tree lookup_type_current_level (tree);
54 static tree push_using_directive (tree);
56 /* The :: namespace. */
58 tree global_namespace;
60 /* The name of the anonymous namespace, throughout this translation
62 static GTY(()) tree anonymous_namespace_name;
64 /* Initialize anonymous_namespace_name if necessary, and return it. */
67 get_anonymous_namespace_name(void)
69 if (!anonymous_namespace_name)
71 /* The anonymous namespace has to have a unique name
72 if typeinfo objects are being compared by name. */
73 if (! flag_weak || ! SUPPORTS_ONE_ONLY)
74 anonymous_namespace_name = get_file_function_name ("N");
76 /* The demangler expects anonymous namespaces to be called
77 something starting with '_GLOBAL__N_'. */
78 anonymous_namespace_name = get_identifier ("_GLOBAL__N_1");
80 return anonymous_namespace_name;
83 /* Compute the chain index of a binding_entry given the HASH value of its
84 name and the total COUNT of chains. COUNT is assumed to be a power
87 #define ENTRY_INDEX(HASH, COUNT) (((HASH) >> 3) & ((COUNT) - 1))
89 /* A free list of "binding_entry"s awaiting for re-use. */
91 static GTY((deletable)) binding_entry free_binding_entry = NULL;
93 /* Create a binding_entry object for (NAME, TYPE). */
95 static inline binding_entry
96 binding_entry_make (tree name, tree type)
100 if (free_binding_entry)
102 entry = free_binding_entry;
103 free_binding_entry = entry->chain;
106 entry = GGC_NEW (struct binding_entry_s);
115 /* Put ENTRY back on the free list. */
118 binding_entry_free (binding_entry entry)
122 entry->chain = free_binding_entry;
123 free_binding_entry = entry;
127 /* The datatype used to implement the mapping from names to types at
129 struct binding_table_s GTY(())
131 /* Array of chains of "binding_entry"s */
132 binding_entry * GTY((length ("%h.chain_count"))) chain;
134 /* The number of chains in this table. This is the length of the
135 the member "chain" considered as an array. */
138 /* Number of "binding_entry"s in this table. */
142 /* Construct TABLE with an initial CHAIN_COUNT. */
145 binding_table_construct (binding_table table, size_t chain_count)
147 table->chain_count = chain_count;
148 table->entry_count = 0;
149 table->chain = GGC_CNEWVEC (binding_entry, table->chain_count);
152 /* Make TABLE's entries ready for reuse. */
155 binding_table_free (binding_table table)
163 for (i = 0, count = table->chain_count; i < count; ++i)
165 binding_entry temp = table->chain[i];
168 binding_entry entry = temp;
170 binding_entry_free (entry);
172 table->chain[i] = NULL;
174 table->entry_count = 0;
178 /* Allocate a table with CHAIN_COUNT, assumed to be a power of two. */
180 static inline binding_table
181 binding_table_new (size_t chain_count)
183 binding_table table = GGC_NEW (struct binding_table_s);
185 binding_table_construct (table, chain_count);
189 /* Expand TABLE to twice its current chain_count. */
192 binding_table_expand (binding_table table)
194 const size_t old_chain_count = table->chain_count;
195 const size_t old_entry_count = table->entry_count;
196 const size_t new_chain_count = 2 * old_chain_count;
197 binding_entry *old_chains = table->chain;
200 binding_table_construct (table, new_chain_count);
201 for (i = 0; i < old_chain_count; ++i)
203 binding_entry entry = old_chains[i];
204 for (; entry != NULL; entry = old_chains[i])
206 const unsigned int hash = IDENTIFIER_HASH_VALUE (entry->name);
207 const size_t j = ENTRY_INDEX (hash, new_chain_count);
209 old_chains[i] = entry->chain;
210 entry->chain = table->chain[j];
211 table->chain[j] = entry;
214 table->entry_count = old_entry_count;
217 /* Insert a binding for NAME to TYPE into TABLE. */
220 binding_table_insert (binding_table table, tree name, tree type)
222 const unsigned int hash = IDENTIFIER_HASH_VALUE (name);
223 const size_t i = ENTRY_INDEX (hash, table->chain_count);
224 binding_entry entry = binding_entry_make (name, type);
226 entry->chain = table->chain[i];
227 table->chain[i] = entry;
228 ++table->entry_count;
230 if (3 * table->chain_count < 5 * table->entry_count)
231 binding_table_expand (table);
234 /* Return the binding_entry, if any, that maps NAME. */
237 binding_table_find (binding_table table, tree name)
239 const unsigned int hash = IDENTIFIER_HASH_VALUE (name);
240 binding_entry entry = table->chain[ENTRY_INDEX (hash, table->chain_count)];
242 while (entry != NULL && entry->name != name)
243 entry = entry->chain;
248 /* Apply PROC -- with DATA -- to all entries in TABLE. */
251 binding_table_foreach (binding_table table, bt_foreach_proc proc, void *data)
253 const size_t chain_count = table->chain_count;
256 for (i = 0; i < chain_count; ++i)
258 binding_entry entry = table->chain[i];
259 for (; entry != NULL; entry = entry->chain)
264 #ifndef ENABLE_SCOPE_CHECKING
265 # define ENABLE_SCOPE_CHECKING 0
267 # define ENABLE_SCOPE_CHECKING 1
270 /* A free list of "cxx_binding"s, connected by their PREVIOUS. */
272 static GTY((deletable)) cxx_binding *free_bindings;
274 /* Initialize VALUE and TYPE field for BINDING, and set the PREVIOUS
278 cxx_binding_init (cxx_binding *binding, tree value, tree type)
280 binding->value = value;
281 binding->type = type;
282 binding->previous = NULL;
285 /* (GC)-allocate a binding object with VALUE and TYPE member initialized. */
288 cxx_binding_make (tree value, tree type)
290 cxx_binding *binding;
293 binding = free_bindings;
294 free_bindings = binding->previous;
297 binding = GGC_NEW (cxx_binding);
299 cxx_binding_init (binding, value, type);
304 /* Put BINDING back on the free list. */
307 cxx_binding_free (cxx_binding *binding)
309 binding->scope = NULL;
310 binding->previous = free_bindings;
311 free_bindings = binding;
314 /* Create a new binding for NAME (with the indicated VALUE and TYPE
315 bindings) in the class scope indicated by SCOPE. */
318 new_class_binding (tree name, tree value, tree type, cxx_scope *scope)
320 cp_class_binding *cb;
321 cxx_binding *binding;
323 if (VEC_length (cp_class_binding, scope->class_shadowed))
325 cp_class_binding *old_base;
326 old_base = VEC_index (cp_class_binding, scope->class_shadowed, 0);
327 if (VEC_reserve (cp_class_binding, gc, scope->class_shadowed, 1))
329 /* Fixup the current bindings, as they might have moved. */
333 VEC_iterate (cp_class_binding, scope->class_shadowed, i, cb);
337 b = &IDENTIFIER_BINDING (cb->identifier);
338 while (*b != &old_base[i].base)
339 b = &((*b)->previous);
343 cb = VEC_quick_push (cp_class_binding, scope->class_shadowed, NULL);
346 cb = VEC_safe_push (cp_class_binding, gc, scope->class_shadowed, NULL);
348 cb->identifier = name;
350 binding->scope = scope;
351 cxx_binding_init (binding, value, type);
355 /* Make DECL the innermost binding for ID. The LEVEL is the binding
356 level at which this declaration is being bound. */
359 push_binding (tree id, tree decl, cxx_scope* level)
361 cxx_binding *binding;
363 if (level != class_binding_level)
365 binding = cxx_binding_make (decl, NULL_TREE);
366 binding->scope = level;
369 binding = new_class_binding (id, decl, /*type=*/NULL_TREE, level);
371 /* Now, fill in the binding information. */
372 binding->previous = IDENTIFIER_BINDING (id);
373 INHERITED_VALUE_BINDING_P (binding) = 0;
374 LOCAL_BINDING_P (binding) = (level != class_binding_level);
376 /* And put it on the front of the list of bindings for ID. */
377 IDENTIFIER_BINDING (id) = binding;
380 /* Remove the binding for DECL which should be the innermost binding
384 pop_binding (tree id, tree decl)
386 cxx_binding *binding;
389 /* It's easiest to write the loops that call this function without
390 checking whether or not the entities involved have names. We
391 get here for such an entity. */
394 /* Get the innermost binding for ID. */
395 binding = IDENTIFIER_BINDING (id);
397 /* The name should be bound. */
398 gcc_assert (binding != NULL);
400 /* The DECL will be either the ordinary binding or the type
401 binding for this identifier. Remove that binding. */
402 if (binding->value == decl)
403 binding->value = NULL_TREE;
406 gcc_assert (binding->type == decl);
407 binding->type = NULL_TREE;
410 if (!binding->value && !binding->type)
412 /* We're completely done with the innermost binding for this
413 identifier. Unhook it from the list of bindings. */
414 IDENTIFIER_BINDING (id) = binding->previous;
416 /* Add it to the free list. */
417 cxx_binding_free (binding);
421 /* BINDING records an existing declaration for a name in the current scope.
422 But, DECL is another declaration for that same identifier in the
423 same scope. This is the `struct stat' hack whereby a non-typedef
424 class name or enum-name can be bound at the same level as some other
428 A class name (9.1) or enumeration name (7.2) can be hidden by the
429 name of an object, function, or enumerator declared in the same scope.
430 If a class or enumeration name and an object, function, or enumerator
431 are declared in the same scope (in any order) with the same name, the
432 class or enumeration name is hidden wherever the object, function, or
433 enumerator name is visible.
435 It's the responsibility of the caller to check that
436 inserting this name is valid here. Returns nonzero if the new binding
440 supplement_binding (cxx_binding *binding, tree decl)
442 tree bval = binding->value;
445 timevar_push (TV_NAME_LOOKUP);
446 if (TREE_CODE (decl) == TYPE_DECL && DECL_ARTIFICIAL (decl))
447 /* The new name is the type name. */
448 binding->type = decl;
449 else if (/* BVAL is null when push_class_level_binding moves an
450 inherited type-binding out of the way to make room for a
451 new value binding. */
453 /* BVAL is error_mark_node when DECL's name has been used
454 in a non-class scope prior declaration. In that case,
455 we should have already issued a diagnostic; for graceful
456 error recovery purpose, pretend this was the intended
457 declaration for that name. */
458 || bval == error_mark_node
459 /* If BVAL is anticipated but has not yet been declared,
460 pretend it is not there at all. */
461 || (TREE_CODE (bval) == FUNCTION_DECL
462 && DECL_ANTICIPATED (bval)
463 && !DECL_HIDDEN_FRIEND_P (bval)))
464 binding->value = decl;
465 else if (TREE_CODE (bval) == TYPE_DECL && DECL_ARTIFICIAL (bval))
467 /* The old binding was a type name. It was placed in
468 VALUE field because it was thought, at the point it was
469 declared, to be the only entity with such a name. Move the
470 type name into the type slot; it is now hidden by the new
472 binding->type = bval;
473 binding->value = decl;
474 binding->value_is_inherited = false;
476 else if (TREE_CODE (bval) == TYPE_DECL
477 && TREE_CODE (decl) == TYPE_DECL
478 && DECL_NAME (decl) == DECL_NAME (bval)
479 && binding->scope->kind != sk_class
480 && (same_type_p (TREE_TYPE (decl), TREE_TYPE (bval))
481 /* If either type involves template parameters, we must
482 wait until instantiation. */
483 || uses_template_parms (TREE_TYPE (decl))
484 || uses_template_parms (TREE_TYPE (bval))))
485 /* We have two typedef-names, both naming the same type to have
486 the same name. In general, this is OK because of:
490 In a given scope, a typedef specifier can be used to redefine
491 the name of any type declared in that scope to refer to the
492 type to which it already refers.
494 However, in class scopes, this rule does not apply due to the
495 stricter language in [class.mem] prohibiting redeclarations of
498 /* There can be two block-scope declarations of the same variable,
499 so long as they are `extern' declarations. However, there cannot
500 be two declarations of the same static data member:
504 A member shall not be declared twice in the
505 member-specification. */
506 else if (TREE_CODE (decl) == VAR_DECL && TREE_CODE (bval) == VAR_DECL
507 && DECL_EXTERNAL (decl) && DECL_EXTERNAL (bval)
508 && !DECL_CLASS_SCOPE_P (decl))
510 duplicate_decls (decl, binding->value, /*newdecl_is_friend=*/false);
513 else if (TREE_CODE (decl) == NAMESPACE_DECL
514 && TREE_CODE (bval) == NAMESPACE_DECL
515 && DECL_NAMESPACE_ALIAS (decl)
516 && DECL_NAMESPACE_ALIAS (bval)
517 && ORIGINAL_NAMESPACE (bval) == ORIGINAL_NAMESPACE (decl))
520 In a declarative region, a namespace-alias-definition can be
521 used to redefine a namespace-alias declared in that declarative
522 region to refer only to the namespace to which it already
527 error ("declaration of %q#D", decl);
528 error ("conflicts with previous declaration %q+#D", bval);
532 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, ok);
535 /* Add DECL to the list of things declared in B. */
538 add_decl_to_level (tree decl, cxx_scope *b)
540 if (TREE_CODE (decl) == NAMESPACE_DECL
541 && !DECL_NAMESPACE_ALIAS (decl))
543 TREE_CHAIN (decl) = b->namespaces;
544 b->namespaces = decl;
546 else if (TREE_CODE (decl) == VAR_DECL && DECL_VIRTUAL_P (decl))
548 TREE_CHAIN (decl) = b->vtables;
553 /* We build up the list in reverse order, and reverse it later if
555 TREE_CHAIN (decl) = b->names;
559 /* If appropriate, add decl to separate list of statics. We
560 include extern variables because they might turn out to be
561 static later. It's OK for this list to contain a few false
563 if (b->kind == sk_namespace)
564 if ((TREE_CODE (decl) == VAR_DECL
565 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
566 || (TREE_CODE (decl) == FUNCTION_DECL
567 && (!TREE_PUBLIC (decl) || DECL_DECLARED_INLINE_P (decl))))
568 VEC_safe_push (tree, gc, b->static_decls, decl);
572 /* Record a decl-node X as belonging to the current lexical scope.
573 Check for errors (such as an incompatible declaration for the same
574 name already seen in the same scope). IS_FRIEND is true if X is
575 declared as a friend.
577 Returns either X or an old decl for the same name.
578 If an old decl is returned, it may have been smashed
579 to agree with what X says. */
582 pushdecl_maybe_friend (tree x, bool is_friend)
586 int need_new_binding;
588 timevar_push (TV_NAME_LOOKUP);
590 if (x == error_mark_node)
591 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
593 need_new_binding = 1;
595 if (DECL_TEMPLATE_PARM_P (x))
596 /* Template parameters have no context; they are not X::T even
597 when declared within a class or namespace. */
601 if (current_function_decl && x != current_function_decl
602 /* A local declaration for a function doesn't constitute
604 && TREE_CODE (x) != FUNCTION_DECL
605 /* A local declaration for an `extern' variable is in the
606 scope of the current namespace, not the current
608 && !(TREE_CODE (x) == VAR_DECL && DECL_EXTERNAL (x))
609 && !DECL_CONTEXT (x))
610 DECL_CONTEXT (x) = current_function_decl;
612 /* If this is the declaration for a namespace-scope function,
613 but the declaration itself is in a local scope, mark the
615 if (TREE_CODE (x) == FUNCTION_DECL
616 && DECL_NAMESPACE_SCOPE_P (x)
617 && current_function_decl
618 && x != current_function_decl)
619 DECL_LOCAL_FUNCTION_P (x) = 1;
622 name = DECL_NAME (x);
625 int different_binding_level = 0;
627 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
628 name = TREE_OPERAND (name, 0);
630 /* In case this decl was explicitly namespace-qualified, look it
631 up in its namespace context. */
632 if (DECL_NAMESPACE_SCOPE_P (x) && namespace_bindings_p ())
633 t = namespace_binding (name, DECL_CONTEXT (x));
635 t = lookup_name_innermost_nonclass_level (name);
637 /* [basic.link] If there is a visible declaration of an entity
638 with linkage having the same name and type, ignoring entities
639 declared outside the innermost enclosing namespace scope, the
640 block scope declaration declares that same entity and
641 receives the linkage of the previous declaration. */
642 if (! t && current_function_decl && x != current_function_decl
643 && (TREE_CODE (x) == FUNCTION_DECL || TREE_CODE (x) == VAR_DECL)
644 && DECL_EXTERNAL (x))
646 /* Look in block scope. */
647 t = innermost_non_namespace_value (name);
648 /* Or in the innermost namespace. */
650 t = namespace_binding (name, DECL_CONTEXT (x));
651 /* Does it have linkage? Note that if this isn't a DECL, it's an
652 OVERLOAD, which is OK. */
653 if (t && DECL_P (t) && ! (TREE_STATIC (t) || DECL_EXTERNAL (t)))
656 different_binding_level = 1;
659 /* If we are declaring a function, and the result of name-lookup
660 was an OVERLOAD, look for an overloaded instance that is
661 actually the same as the function we are declaring. (If
662 there is one, we have to merge our declaration with the
663 previous declaration.) */
664 if (t && TREE_CODE (t) == OVERLOAD)
668 if (TREE_CODE (x) == FUNCTION_DECL)
669 for (match = t; match; match = OVL_NEXT (match))
671 if (decls_match (OVL_CURRENT (match), x))
675 /* Just choose one. */
679 t = OVL_CURRENT (match);
684 if (t && t != error_mark_node)
686 if (different_binding_level)
688 if (decls_match (x, t))
689 /* The standard only says that the local extern
690 inherits linkage from the previous decl; in
691 particular, default args are not shared. Add
692 the decl into a hash table to make sure only
693 the previous decl in this case is seen by the
696 struct cxx_int_tree_map *h;
699 TREE_PUBLIC (x) = TREE_PUBLIC (t);
701 if (cp_function_chain->extern_decl_map == NULL)
702 cp_function_chain->extern_decl_map
703 = htab_create_ggc (20, cxx_int_tree_map_hash,
704 cxx_int_tree_map_eq, NULL);
706 h = GGC_NEW (struct cxx_int_tree_map);
707 h->uid = DECL_UID (x);
709 loc = htab_find_slot_with_hash
710 (cp_function_chain->extern_decl_map, h,
712 *(struct cxx_int_tree_map **) loc = h;
715 else if (TREE_CODE (t) == PARM_DECL)
717 gcc_assert (DECL_CONTEXT (t));
719 /* Check for duplicate params. */
720 if (duplicate_decls (x, t, is_friend))
721 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
723 else if ((DECL_EXTERN_C_FUNCTION_P (x)
724 || DECL_FUNCTION_TEMPLATE_P (x))
725 && is_overloaded_fn (t))
726 /* Don't do anything just yet. */;
727 else if (t == wchar_decl_node)
729 if (pedantic && ! DECL_IN_SYSTEM_HEADER (x))
730 pedwarn ("redeclaration of %<wchar_t%> as %qT",
733 /* Throw away the redeclaration. */
734 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
738 tree olddecl = duplicate_decls (x, t, is_friend);
740 /* If the redeclaration failed, we can stop at this
742 if (olddecl == error_mark_node)
743 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
747 if (TREE_CODE (t) == TYPE_DECL)
748 SET_IDENTIFIER_TYPE_VALUE (name, TREE_TYPE (t));
750 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
752 else if (DECL_MAIN_P (x) && TREE_CODE (t) == FUNCTION_DECL)
754 /* A redeclaration of main, but not a duplicate of the
759 This function shall not be overloaded. */
760 error ("invalid redeclaration of %q+D", t);
762 /* We don't try to push this declaration since that
764 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
769 if (TREE_CODE (x) == FUNCTION_DECL || DECL_FUNCTION_TEMPLATE_P (x))
770 check_default_args (x);
772 check_template_shadow (x);
774 /* If this is a function conjured up by the backend, massage it
775 so it looks friendly. */
776 if (DECL_NON_THUNK_FUNCTION_P (x) && ! DECL_LANG_SPECIFIC (x))
778 retrofit_lang_decl (x);
779 SET_DECL_LANGUAGE (x, lang_c);
782 if (DECL_NON_THUNK_FUNCTION_P (x) && ! DECL_FUNCTION_MEMBER_P (x))
784 t = push_overloaded_decl (x, PUSH_LOCAL, is_friend);
786 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
787 if (!namespace_bindings_p ())
788 /* We do not need to create a binding for this name;
789 push_overloaded_decl will have already done so if
791 need_new_binding = 0;
793 else if (DECL_FUNCTION_TEMPLATE_P (x) && DECL_NAMESPACE_SCOPE_P (x))
795 t = push_overloaded_decl (x, PUSH_GLOBAL, is_friend);
797 add_decl_to_level (x, NAMESPACE_LEVEL (CP_DECL_CONTEXT (t)));
798 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
801 /* If declaring a type as a typedef, copy the type (unless we're
802 at line 0), and install this TYPE_DECL as the new type's typedef
803 name. See the extensive comment in ../c-decl.c (pushdecl). */
804 if (TREE_CODE (x) == TYPE_DECL)
806 tree type = TREE_TYPE (x);
807 if (DECL_IS_BUILTIN (x))
809 if (TYPE_NAME (type) == 0)
810 TYPE_NAME (type) = x;
812 else if (type != error_mark_node && TYPE_NAME (type) != x
813 /* We don't want to copy the type when all we're
814 doing is making a TYPE_DECL for the purposes of
816 && (!TYPE_NAME (type)
817 || TYPE_NAME (type) != DECL_ABSTRACT_ORIGIN (x)))
819 DECL_ORIGINAL_TYPE (x) = type;
820 type = build_variant_type_copy (type);
821 TYPE_STUB_DECL (type) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
822 TYPE_NAME (type) = x;
823 TREE_TYPE (x) = type;
826 if (type != error_mark_node
828 && TYPE_IDENTIFIER (type))
829 set_identifier_type_value (DECL_NAME (x), x);
832 /* Multiple external decls of the same identifier ought to match.
834 We get warnings about inline functions where they are defined.
835 We get warnings about other functions from push_overloaded_decl.
837 Avoid duplicate warnings where they are used. */
838 if (TREE_PUBLIC (x) && TREE_CODE (x) != FUNCTION_DECL)
842 decl = IDENTIFIER_NAMESPACE_VALUE (name);
843 if (decl && TREE_CODE (decl) == OVERLOAD)
844 decl = OVL_FUNCTION (decl);
846 if (decl && decl != error_mark_node
847 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl))
848 /* If different sort of thing, we already gave an error. */
849 && TREE_CODE (decl) == TREE_CODE (x)
850 && !same_type_p (TREE_TYPE (x), TREE_TYPE (decl)))
852 pedwarn ("type mismatch with previous external decl of %q#D", x);
853 pedwarn ("previous external decl of %q+#D", decl);
857 if (TREE_CODE (x) == FUNCTION_DECL
859 && !flag_friend_injection)
861 /* This is a new declaration of a friend function, so hide
862 it from ordinary function lookup. */
863 DECL_ANTICIPATED (x) = 1;
864 DECL_HIDDEN_FRIEND_P (x) = 1;
867 /* This name is new in its binding level.
868 Install the new declaration and return it. */
869 if (namespace_bindings_p ())
871 /* Install a global value. */
873 /* If the first global decl has external linkage,
874 warn if we later see static one. */
875 if (IDENTIFIER_GLOBAL_VALUE (name) == NULL_TREE && TREE_PUBLIC (x))
876 TREE_PUBLIC (name) = 1;
878 /* Bind the name for the entity. */
879 if (!(TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x)
881 && (TREE_CODE (x) == TYPE_DECL
882 || TREE_CODE (x) == VAR_DECL
883 || TREE_CODE (x) == NAMESPACE_DECL
884 || TREE_CODE (x) == CONST_DECL
885 || TREE_CODE (x) == TEMPLATE_DECL))
886 SET_IDENTIFIER_NAMESPACE_VALUE (name, x);
888 /* If new decl is `static' and an `extern' was seen previously,
890 if (x != NULL_TREE && t != NULL_TREE && decls_match (x, t))
891 warn_extern_redeclared_static (x, t);
895 /* Here to install a non-global value. */
896 tree oldlocal = innermost_non_namespace_value (name);
897 tree oldglobal = IDENTIFIER_NAMESPACE_VALUE (name);
899 if (need_new_binding)
901 push_local_binding (name, x, 0);
902 /* Because push_local_binding will hook X on to the
903 current_binding_level's name list, we don't want to
904 do that again below. */
905 need_new_binding = 0;
908 /* If this is a TYPE_DECL, push it into the type value slot. */
909 if (TREE_CODE (x) == TYPE_DECL)
910 set_identifier_type_value (name, x);
912 /* Clear out any TYPE_DECL shadowed by a namespace so that
913 we won't think this is a type. The C struct hack doesn't
914 go through namespaces. */
915 if (TREE_CODE (x) == NAMESPACE_DECL)
916 set_identifier_type_value (name, NULL_TREE);
923 && TREE_CODE (oldlocal) == VAR_DECL
924 && DECL_DEAD_FOR_LOCAL (oldlocal))
925 oldlocal = DECL_SHADOWED_FOR_VAR (oldlocal);
927 if (oldlocal == NULL_TREE)
928 oldlocal = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (d));
931 /* If this is an extern function declaration, see if we
932 have a global definition or declaration for the function. */
933 if (oldlocal == NULL_TREE
935 && oldglobal != NULL_TREE
936 && TREE_CODE (x) == FUNCTION_DECL
937 && TREE_CODE (oldglobal) == FUNCTION_DECL)
939 /* We have one. Their types must agree. */
940 if (decls_match (x, oldglobal))
944 warning (0, "extern declaration of %q#D doesn't match", x);
945 warning (0, "global declaration %q+#D", oldglobal);
948 /* If we have a local external declaration,
949 and no file-scope declaration has yet been seen,
950 then if we later have a file-scope decl it must not be static. */
951 if (oldlocal == NULL_TREE
952 && oldglobal == NULL_TREE
955 TREE_PUBLIC (name) = 1;
957 /* Warn if shadowing an argument at the top level of the body. */
958 if (oldlocal != NULL_TREE && !DECL_EXTERNAL (x)
959 /* Inline decls shadow nothing. */
960 && !DECL_FROM_INLINE (x)
961 && TREE_CODE (oldlocal) == PARM_DECL
962 /* Don't check the `this' parameter. */
963 && !DECL_ARTIFICIAL (oldlocal))
967 /* Don't complain if it's from an enclosing function. */
968 if (DECL_CONTEXT (oldlocal) == current_function_decl
969 && TREE_CODE (x) != PARM_DECL)
971 /* Go to where the parms should be and see if we find
973 struct cp_binding_level *b = current_binding_level->level_chain;
975 if (FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
976 /* Skip the ctor/dtor cleanup level. */
980 if (b->kind == sk_function_parms)
982 error ("declaration of %q#D shadows a parameter", x);
987 if (warn_shadow && !err)
989 warning (OPT_Wshadow, "declaration of %q#D shadows a parameter", x);
990 warning (OPT_Wshadow, "%Jshadowed declaration is here", oldlocal);
994 /* Maybe warn if shadowing something else. */
995 else if (warn_shadow && !DECL_EXTERNAL (x)
996 /* No shadow warnings for internally generated vars. */
997 && ! DECL_ARTIFICIAL (x)
998 /* No shadow warnings for vars made for inlining. */
999 && ! DECL_FROM_INLINE (x))
1003 if (current_class_ptr)
1004 member = lookup_member (current_class_type,
1007 /*want_type=*/false);
1011 if (member && !TREE_STATIC (member))
1013 /* Location of previous decl is not useful in this case. */
1014 warning (OPT_Wshadow, "declaration of %qD shadows a member of 'this'",
1017 else if (oldlocal != NULL_TREE
1018 && TREE_CODE (oldlocal) == VAR_DECL)
1020 warning (OPT_Wshadow, "declaration of %qD shadows a previous local", x);
1021 warning (OPT_Wshadow, "%Jshadowed declaration is here", oldlocal);
1023 else if (oldglobal != NULL_TREE
1024 && TREE_CODE (oldglobal) == VAR_DECL)
1025 /* XXX shadow warnings in outer-more namespaces */
1027 warning (OPT_Wshadow, "declaration of %qD shadows a global declaration",
1029 warning (OPT_Wshadow, "%Jshadowed declaration is here", oldglobal);
1034 if (TREE_CODE (x) == VAR_DECL)
1035 maybe_register_incomplete_var (x);
1038 if (need_new_binding)
1039 add_decl_to_level (x,
1040 DECL_NAMESPACE_SCOPE_P (x)
1041 ? NAMESPACE_LEVEL (CP_DECL_CONTEXT (x))
1042 : current_binding_level);
1044 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
1047 /* Record a decl-node X as belonging to the current lexical scope. */
1052 return pushdecl_maybe_friend (x, false);
1055 /* Enter DECL into the symbol table, if that's appropriate. Returns
1056 DECL, or a modified version thereof. */
1059 maybe_push_decl (tree decl)
1061 tree type = TREE_TYPE (decl);
1063 /* Add this decl to the current binding level, but not if it comes
1064 from another scope, e.g. a static member variable. TEM may equal
1065 DECL or it may be a previous decl of the same name. */
1066 if (decl == error_mark_node
1067 || (TREE_CODE (decl) != PARM_DECL
1068 && DECL_CONTEXT (decl) != NULL_TREE
1069 /* Definitions of namespace members outside their namespace are
1071 && TREE_CODE (DECL_CONTEXT (decl)) != NAMESPACE_DECL)
1072 || (TREE_CODE (decl) == TEMPLATE_DECL && !namespace_bindings_p ())
1073 || TREE_CODE (type) == UNKNOWN_TYPE
1074 /* The declaration of a template specialization does not affect
1075 the functions available for overload resolution, so we do not
1077 || (TREE_CODE (decl) == FUNCTION_DECL
1078 && DECL_TEMPLATE_SPECIALIZATION (decl)))
1081 return pushdecl (decl);
1084 /* Bind DECL to ID in the current_binding_level, assumed to be a local
1085 binding level. If PUSH_USING is set in FLAGS, we know that DECL
1086 doesn't really belong to this binding level, that it got here
1087 through a using-declaration. */
1090 push_local_binding (tree id, tree decl, int flags)
1092 struct cp_binding_level *b;
1094 /* Skip over any local classes. This makes sense if we call
1095 push_local_binding with a friend decl of a local class. */
1096 b = innermost_nonclass_level ();
1098 if (lookup_name_innermost_nonclass_level (id))
1100 /* Supplement the existing binding. */
1101 if (!supplement_binding (IDENTIFIER_BINDING (id), decl))
1102 /* It didn't work. Something else must be bound at this
1103 level. Do not add DECL to the list of things to pop
1108 /* Create a new binding. */
1109 push_binding (id, decl, b);
1111 if (TREE_CODE (decl) == OVERLOAD || (flags & PUSH_USING))
1112 /* We must put the OVERLOAD into a TREE_LIST since the
1113 TREE_CHAIN of an OVERLOAD is already used. Similarly for
1114 decls that got here through a using-declaration. */
1115 decl = build_tree_list (NULL_TREE, decl);
1117 /* And put DECL on the list of things declared by the current
1119 add_decl_to_level (decl, b);
1122 /* Check to see whether or not DECL is a variable that would have been
1123 in scope under the ARM, but is not in scope under the ANSI/ISO
1124 standard. If so, issue an error message. If name lookup would
1125 work in both cases, but return a different result, this function
1126 returns the result of ANSI/ISO lookup. Otherwise, it returns
1130 check_for_out_of_scope_variable (tree decl)
1134 /* We only care about out of scope variables. */
1135 if (!(TREE_CODE (decl) == VAR_DECL && DECL_DEAD_FOR_LOCAL (decl)))
1138 shadowed = DECL_HAS_SHADOWED_FOR_VAR_P (decl)
1139 ? DECL_SHADOWED_FOR_VAR (decl) : NULL_TREE ;
1140 while (shadowed != NULL_TREE && TREE_CODE (shadowed) == VAR_DECL
1141 && DECL_DEAD_FOR_LOCAL (shadowed))
1142 shadowed = DECL_HAS_SHADOWED_FOR_VAR_P (shadowed)
1143 ? DECL_SHADOWED_FOR_VAR (shadowed) : NULL_TREE;
1145 shadowed = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (decl));
1148 if (!DECL_ERROR_REPORTED (decl))
1150 warning (0, "name lookup of %qD changed", DECL_NAME (decl));
1151 warning (0, " matches this %q+D under ISO standard rules",
1153 warning (0, " matches this %q+D under old rules", decl);
1154 DECL_ERROR_REPORTED (decl) = 1;
1159 /* If we have already complained about this declaration, there's no
1160 need to do it again. */
1161 if (DECL_ERROR_REPORTED (decl))
1164 DECL_ERROR_REPORTED (decl) = 1;
1166 if (TREE_TYPE (decl) == error_mark_node)
1169 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
1171 error ("name lookup of %qD changed for new ISO %<for%> scoping",
1173 error (" cannot use obsolete binding at %q+D because "
1174 "it has a destructor", decl);
1175 return error_mark_node;
1179 pedwarn ("name lookup of %qD changed for new ISO %<for%> scoping",
1181 pedwarn (" using obsolete binding at %q+D", decl);
1187 /* true means unconditionally make a BLOCK for the next level pushed. */
1189 static bool keep_next_level_flag;
1191 static int binding_depth = 0;
1192 static int is_class_level = 0;
1199 for (i = 0; i < depth * 2; i++)
1203 /* Return a string describing the kind of SCOPE we have. */
1205 cxx_scope_descriptor (cxx_scope *scope)
1207 /* The order of this table must match the "scope_kind"
1209 static const char* scope_kind_names[] = {
1215 "function-parameter-scope",
1218 "template-parameter-scope",
1219 "template-explicit-spec-scope"
1221 const scope_kind kind = scope->explicit_spec_p
1222 ? sk_template_spec : scope->kind;
1224 return scope_kind_names[kind];
1227 /* Output a debugging information about SCOPE when performing
1230 cxx_scope_debug (cxx_scope *scope, int line, const char *action)
1232 const char *desc = cxx_scope_descriptor (scope);
1233 if (scope->this_entity)
1234 verbatim ("%s %s(%E) %p %d\n", action, desc,
1235 scope->this_entity, (void *) scope, line);
1237 verbatim ("%s %s %p %d\n", action, desc, (void *) scope, line);
1240 /* Return the estimated initial size of the hashtable of a NAMESPACE
1243 static inline size_t
1244 namespace_scope_ht_size (tree ns)
1246 tree name = DECL_NAME (ns);
1248 return name == std_identifier
1249 ? NAMESPACE_STD_HT_SIZE
1250 : (name == global_scope_name
1251 ? GLOBAL_SCOPE_HT_SIZE
1252 : NAMESPACE_ORDINARY_HT_SIZE);
1255 /* A chain of binding_level structures awaiting reuse. */
1257 static GTY((deletable)) struct cp_binding_level *free_binding_level;
1259 /* Insert SCOPE as the innermost binding level. */
1262 push_binding_level (struct cp_binding_level *scope)
1264 /* Add it to the front of currently active scopes stack. */
1265 scope->level_chain = current_binding_level;
1266 current_binding_level = scope;
1267 keep_next_level_flag = false;
1269 if (ENABLE_SCOPE_CHECKING)
1271 scope->binding_depth = binding_depth;
1272 indent (binding_depth);
1273 cxx_scope_debug (scope, input_line, "push");
1279 /* Create a new KIND scope and make it the top of the active scopes stack.
1280 ENTITY is the scope of the associated C++ entity (namespace, class,
1281 function); it is NULL otherwise. */
1284 begin_scope (scope_kind kind, tree entity)
1288 /* Reuse or create a struct for this binding level. */
1289 if (!ENABLE_SCOPE_CHECKING && free_binding_level)
1291 scope = free_binding_level;
1292 memset (scope, 0, sizeof (cxx_scope));
1293 free_binding_level = scope->level_chain;
1296 scope = GGC_CNEW (cxx_scope);
1298 scope->this_entity = entity;
1299 scope->more_cleanups_ok = true;
1306 case sk_template_spec:
1307 scope->explicit_spec_p = true;
1308 kind = sk_template_parms;
1310 case sk_template_parms:
1316 case sk_function_parms:
1318 scope->keep = keep_next_level_flag;
1322 NAMESPACE_LEVEL (entity) = scope;
1323 scope->static_decls =
1324 VEC_alloc (tree, gc,
1325 DECL_NAME (entity) == std_identifier
1326 || DECL_NAME (entity) == global_scope_name
1331 /* Should not happen. */
1337 push_binding_level (scope);
1342 /* We're about to leave current scope. Pop the top of the stack of
1343 currently active scopes. Return the enclosing scope, now active. */
1348 cxx_scope *scope = current_binding_level;
1350 if (scope->kind == sk_namespace && class_binding_level)
1351 current_binding_level = class_binding_level;
1353 /* We cannot leave a scope, if there are none left. */
1354 if (NAMESPACE_LEVEL (global_namespace))
1355 gcc_assert (!global_scope_p (scope));
1357 if (ENABLE_SCOPE_CHECKING)
1359 indent (--binding_depth);
1360 cxx_scope_debug (scope, input_line, "leave");
1361 if (is_class_level != (scope == class_binding_level))
1363 indent (binding_depth);
1364 verbatim ("XXX is_class_level != (current_scope == class_scope)\n");
1369 #ifdef HANDLE_PRAGMA_VISIBILITY
1370 if (scope->has_visibility)
1374 /* Move one nesting level up. */
1375 current_binding_level = scope->level_chain;
1377 /* Namespace-scopes are left most probably temporarily, not
1378 completely; they can be reopened later, e.g. in namespace-extension
1379 or any name binding activity that requires us to resume a
1380 namespace. For classes, we cache some binding levels. For other
1381 scopes, we just make the structure available for reuse. */
1382 if (scope->kind != sk_namespace
1383 && scope->kind != sk_class)
1385 scope->level_chain = free_binding_level;
1386 gcc_assert (!ENABLE_SCOPE_CHECKING
1387 || scope->binding_depth == binding_depth);
1388 free_binding_level = scope;
1391 /* Find the innermost enclosing class scope, and reset
1392 CLASS_BINDING_LEVEL appropriately. */
1393 if (scope->kind == sk_class)
1395 class_binding_level = NULL;
1396 for (scope = current_binding_level; scope; scope = scope->level_chain)
1397 if (scope->kind == sk_class)
1399 class_binding_level = scope;
1404 return current_binding_level;
1408 resume_scope (struct cp_binding_level* b)
1410 /* Resuming binding levels is meant only for namespaces,
1411 and those cannot nest into classes. */
1412 gcc_assert (!class_binding_level);
1413 /* Also, resuming a non-directly nested namespace is a no-no. */
1414 gcc_assert (b->level_chain == current_binding_level);
1415 current_binding_level = b;
1416 if (ENABLE_SCOPE_CHECKING)
1418 b->binding_depth = binding_depth;
1419 indent (binding_depth);
1420 cxx_scope_debug (b, input_line, "resume");
1426 /* Return the innermost binding level that is not for a class scope. */
1429 innermost_nonclass_level (void)
1433 b = current_binding_level;
1434 while (b->kind == sk_class)
1440 /* We're defining an object of type TYPE. If it needs a cleanup, but
1441 we're not allowed to add any more objects with cleanups to the current
1442 scope, create a new binding level. */
1445 maybe_push_cleanup_level (tree type)
1447 if (type != error_mark_node
1448 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
1449 && current_binding_level->more_cleanups_ok == 0)
1451 begin_scope (sk_cleanup, NULL);
1452 current_binding_level->statement_list = push_stmt_list ();
1456 /* Nonzero if we are currently in the global binding level. */
1459 global_bindings_p (void)
1461 return global_scope_p (current_binding_level);
1464 /* True if we are currently in a toplevel binding level. This
1465 means either the global binding level or a namespace in a toplevel
1466 binding level. Since there are no non-toplevel namespace levels,
1467 this really means any namespace or template parameter level. We
1468 also include a class whose context is toplevel. */
1471 toplevel_bindings_p (void)
1473 struct cp_binding_level *b = innermost_nonclass_level ();
1475 return b->kind == sk_namespace || b->kind == sk_template_parms;
1478 /* True if this is a namespace scope, or if we are defining a class
1479 which is itself at namespace scope, or whose enclosing class is
1480 such a class, etc. */
1483 namespace_bindings_p (void)
1485 struct cp_binding_level *b = innermost_nonclass_level ();
1487 return b->kind == sk_namespace;
1490 /* True if the current level needs to have a BLOCK made. */
1495 return (current_binding_level->blocks != NULL_TREE
1496 || current_binding_level->keep
1497 || current_binding_level->kind == sk_cleanup
1498 || current_binding_level->names != NULL_TREE);
1501 /* Returns the kind of the innermost scope. */
1504 innermost_scope_kind (void)
1506 return current_binding_level->kind;
1509 /* Returns true if this scope was created to store template parameters. */
1512 template_parm_scope_p (void)
1514 return innermost_scope_kind () == sk_template_parms;
1517 /* If KEEP is true, make a BLOCK node for the next binding level,
1518 unconditionally. Otherwise, use the normal logic to decide whether
1519 or not to create a BLOCK. */
1522 keep_next_level (bool keep)
1524 keep_next_level_flag = keep;
1527 /* Return the list of declarations of the current level.
1528 Note that this list is in reverse order unless/until
1529 you nreverse it; and when you do nreverse it, you must
1530 store the result back using `storedecls' or you will lose. */
1535 return current_binding_level->names;
1538 /* For debugging. */
1539 static int no_print_functions = 0;
1540 static int no_print_builtins = 0;
1543 print_binding_level (struct cp_binding_level* lvl)
1547 fprintf (stderr, " blocks=%p", (void *) lvl->blocks);
1548 if (lvl->more_cleanups_ok)
1549 fprintf (stderr, " more-cleanups-ok");
1550 if (lvl->have_cleanups)
1551 fprintf (stderr, " have-cleanups");
1552 fprintf (stderr, "\n");
1555 fprintf (stderr, " names:\t");
1556 /* We can probably fit 3 names to a line? */
1557 for (t = lvl->names; t; t = TREE_CHAIN (t))
1559 if (no_print_functions && (TREE_CODE (t) == FUNCTION_DECL))
1561 if (no_print_builtins
1562 && (TREE_CODE (t) == TYPE_DECL)
1563 && DECL_IS_BUILTIN (t))
1566 /* Function decls tend to have longer names. */
1567 if (TREE_CODE (t) == FUNCTION_DECL)
1574 fprintf (stderr, "\n\t");
1577 print_node_brief (stderr, "", t, 0);
1578 if (t == error_mark_node)
1582 fprintf (stderr, "\n");
1584 if (VEC_length (cp_class_binding, lvl->class_shadowed))
1587 cp_class_binding *b;
1588 fprintf (stderr, " class-shadowed:");
1590 VEC_iterate(cp_class_binding, lvl->class_shadowed, i, b);
1592 fprintf (stderr, " %s ", IDENTIFIER_POINTER (b->identifier));
1593 fprintf (stderr, "\n");
1595 if (lvl->type_shadowed)
1597 fprintf (stderr, " type-shadowed:");
1598 for (t = lvl->type_shadowed; t; t = TREE_CHAIN (t))
1600 fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
1602 fprintf (stderr, "\n");
1607 print_other_binding_stack (struct cp_binding_level *stack)
1609 struct cp_binding_level *level;
1610 for (level = stack; !global_scope_p (level); level = level->level_chain)
1612 fprintf (stderr, "binding level %p\n", (void *) level);
1613 print_binding_level (level);
1618 print_binding_stack (void)
1620 struct cp_binding_level *b;
1621 fprintf (stderr, "current_binding_level=%p\n"
1622 "class_binding_level=%p\n"
1623 "NAMESPACE_LEVEL (global_namespace)=%p\n",
1624 (void *) current_binding_level, (void *) class_binding_level,
1625 (void *) NAMESPACE_LEVEL (global_namespace));
1626 if (class_binding_level)
1628 for (b = class_binding_level; b; b = b->level_chain)
1629 if (b == current_binding_level)
1632 b = class_binding_level;
1634 b = current_binding_level;
1637 b = current_binding_level;
1638 print_other_binding_stack (b);
1639 fprintf (stderr, "global:\n");
1640 print_binding_level (NAMESPACE_LEVEL (global_namespace));
1643 /* Return the type associated with id. */
1646 identifier_type_value (tree id)
1648 timevar_push (TV_NAME_LOOKUP);
1649 /* There is no type with that name, anywhere. */
1650 if (REAL_IDENTIFIER_TYPE_VALUE (id) == NULL_TREE)
1651 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
1652 /* This is not the type marker, but the real thing. */
1653 if (REAL_IDENTIFIER_TYPE_VALUE (id) != global_type_node)
1654 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, REAL_IDENTIFIER_TYPE_VALUE (id));
1655 /* Have to search for it. It must be on the global level, now.
1656 Ask lookup_name not to return non-types. */
1657 id = lookup_name_real (id, 2, 1, /*block_p=*/true, 0, LOOKUP_COMPLAIN);
1659 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, TREE_TYPE (id));
1660 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
1663 /* Return the IDENTIFIER_GLOBAL_VALUE of T, for use in common code, since
1664 the definition of IDENTIFIER_GLOBAL_VALUE is different for C and C++. */
1667 identifier_global_value (tree t)
1669 return IDENTIFIER_GLOBAL_VALUE (t);
1672 /* Push a definition of struct, union or enum tag named ID. into
1673 binding_level B. DECL is a TYPE_DECL for the type. We assume that
1674 the tag ID is not already defined. */
1677 set_identifier_type_value_with_scope (tree id, tree decl, cxx_scope *b)
1681 if (b->kind != sk_namespace)
1683 /* Shadow the marker, not the real thing, so that the marker
1684 gets restored later. */
1685 tree old_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
1687 = tree_cons (id, old_type_value, b->type_shadowed);
1688 type = decl ? TREE_TYPE (decl) : NULL_TREE;
1689 TREE_TYPE (b->type_shadowed) = type;
1693 cxx_binding *binding =
1694 binding_for_name (NAMESPACE_LEVEL (current_namespace), id);
1697 supplement_binding (binding, decl);
1699 binding->value = decl;
1701 /* Store marker instead of real type. */
1702 type = global_type_node;
1704 SET_IDENTIFIER_TYPE_VALUE (id, type);
1707 /* As set_identifier_type_value_with_scope, but using
1708 current_binding_level. */
1711 set_identifier_type_value (tree id, tree decl)
1713 set_identifier_type_value_with_scope (id, decl, current_binding_level);
1716 /* Return the name for the constructor (or destructor) for the
1717 specified class TYPE. When given a template, this routine doesn't
1718 lose the specialization. */
1721 constructor_name_full (tree type)
1723 return TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
1726 /* Return the name for the constructor (or destructor) for the
1727 specified class. When given a template, return the plain
1728 unspecialized name. */
1731 constructor_name (tree type)
1734 name = constructor_name_full (type);
1735 if (IDENTIFIER_TEMPLATE (name))
1736 name = IDENTIFIER_TEMPLATE (name);
1740 /* Returns TRUE if NAME is the name for the constructor for TYPE. */
1743 constructor_name_p (tree name, tree type)
1750 if (TREE_CODE (name) != IDENTIFIER_NODE)
1753 ctor_name = constructor_name_full (type);
1754 if (name == ctor_name)
1756 if (IDENTIFIER_TEMPLATE (ctor_name)
1757 && name == IDENTIFIER_TEMPLATE (ctor_name))
1762 /* Counter used to create anonymous type names. */
1764 static GTY(()) int anon_cnt;
1766 /* Return an IDENTIFIER which can be used as a name for
1767 anonymous structs and unions. */
1770 make_anon_name (void)
1774 sprintf (buf, ANON_AGGRNAME_FORMAT, anon_cnt++);
1775 return get_identifier (buf);
1778 /* Return (from the stack of) the BINDING, if any, established at SCOPE. */
1780 static inline cxx_binding *
1781 find_binding (cxx_scope *scope, cxx_binding *binding)
1783 timevar_push (TV_NAME_LOOKUP);
1785 for (; binding != NULL; binding = binding->previous)
1786 if (binding->scope == scope)
1787 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, binding);
1789 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, (cxx_binding *)0);
1792 /* Return the binding for NAME in SCOPE, if any. Otherwise, return NULL. */
1794 static inline cxx_binding *
1795 cxx_scope_find_binding_for_name (cxx_scope *scope, tree name)
1797 cxx_binding *b = IDENTIFIER_NAMESPACE_BINDINGS (name);
1800 /* Fold-in case where NAME is used only once. */
1801 if (scope == b->scope && b->previous == NULL)
1803 return find_binding (scope, b);
1808 /* Always returns a binding for name in scope. If no binding is
1809 found, make a new one. */
1811 static cxx_binding *
1812 binding_for_name (cxx_scope *scope, tree name)
1814 cxx_binding *result;
1816 result = cxx_scope_find_binding_for_name (scope, name);
1819 /* Not found, make a new one. */
1820 result = cxx_binding_make (NULL, NULL);
1821 result->previous = IDENTIFIER_NAMESPACE_BINDINGS (name);
1822 result->scope = scope;
1823 result->is_local = false;
1824 result->value_is_inherited = false;
1825 IDENTIFIER_NAMESPACE_BINDINGS (name) = result;
1829 /* Insert another USING_DECL into the current binding level, returning
1830 this declaration. If this is a redeclaration, do nothing, and
1831 return NULL_TREE if this not in namespace scope (in namespace
1832 scope, a using decl might extend any previous bindings). */
1835 push_using_decl (tree scope, tree name)
1839 timevar_push (TV_NAME_LOOKUP);
1840 gcc_assert (TREE_CODE (scope) == NAMESPACE_DECL);
1841 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
1842 for (decl = current_binding_level->usings; decl; decl = TREE_CHAIN (decl))
1843 if (USING_DECL_SCOPE (decl) == scope && DECL_NAME (decl) == name)
1846 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP,
1847 namespace_bindings_p () ? decl : NULL_TREE);
1848 decl = build_lang_decl (USING_DECL, name, NULL_TREE);
1849 USING_DECL_SCOPE (decl) = scope;
1850 TREE_CHAIN (decl) = current_binding_level->usings;
1851 current_binding_level->usings = decl;
1852 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
1855 /* Same as pushdecl, but define X in binding-level LEVEL. We rely on the
1856 caller to set DECL_CONTEXT properly. */
1859 pushdecl_with_scope (tree x, cxx_scope *level, bool is_friend)
1861 struct cp_binding_level *b;
1862 tree function_decl = current_function_decl;
1864 timevar_push (TV_NAME_LOOKUP);
1865 current_function_decl = NULL_TREE;
1866 if (level->kind == sk_class)
1868 b = class_binding_level;
1869 class_binding_level = level;
1870 pushdecl_class_level (x);
1871 class_binding_level = b;
1875 b = current_binding_level;
1876 current_binding_level = level;
1877 x = pushdecl_maybe_friend (x, is_friend);
1878 current_binding_level = b;
1880 current_function_decl = function_decl;
1881 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
1884 /* DECL is a FUNCTION_DECL for a non-member function, which may have
1885 other definitions already in place. We get around this by making
1886 the value of the identifier point to a list of all the things that
1887 want to be referenced by that name. It is then up to the users of
1888 that name to decide what to do with that list.
1890 DECL may also be a TEMPLATE_DECL, with a FUNCTION_DECL in its
1891 DECL_TEMPLATE_RESULT. It is dealt with the same way.
1893 FLAGS is a bitwise-or of the following values:
1894 PUSH_LOCAL: Bind DECL in the current scope, rather than at
1896 PUSH_USING: DECL is being pushed as the result of a using
1899 IS_FRIEND is true if this is a friend declaration.
1901 The value returned may be a previous declaration if we guessed wrong
1902 about what language DECL should belong to (C or C++). Otherwise,
1903 it's always DECL (and never something that's not a _DECL). */
1906 push_overloaded_decl (tree decl, int flags, bool is_friend)
1908 tree name = DECL_NAME (decl);
1911 int doing_global = (namespace_bindings_p () || !(flags & PUSH_LOCAL));
1913 timevar_push (TV_NAME_LOOKUP);
1915 old = namespace_binding (name, DECL_CONTEXT (decl));
1917 old = lookup_name_innermost_nonclass_level (name);
1921 if (TREE_CODE (old) == TYPE_DECL && DECL_ARTIFICIAL (old))
1923 tree t = TREE_TYPE (old);
1924 if (IS_AGGR_TYPE (t) && warn_shadow
1925 && (! DECL_IN_SYSTEM_HEADER (decl)
1926 || ! DECL_IN_SYSTEM_HEADER (old)))
1927 warning (0, "%q#D hides constructor for %q#T", decl, t);
1930 else if (is_overloaded_fn (old))
1934 for (tmp = old; tmp; tmp = OVL_NEXT (tmp))
1936 tree fn = OVL_CURRENT (tmp);
1939 if (TREE_CODE (tmp) == OVERLOAD && OVL_USED (tmp)
1940 && !(flags & PUSH_USING)
1941 && compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
1942 TYPE_ARG_TYPES (TREE_TYPE (decl)))
1943 && ! decls_match (fn, decl))
1944 error ("%q#D conflicts with previous using declaration %q#D",
1947 dup = duplicate_decls (decl, fn, is_friend);
1948 /* If DECL was a redeclaration of FN -- even an invalid
1949 one -- pass that information along to our caller. */
1950 if (dup == fn || dup == error_mark_node)
1951 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, dup);
1954 /* We don't overload implicit built-ins. duplicate_decls()
1955 may fail to merge the decls if the new decl is e.g. a
1956 template function. */
1957 if (TREE_CODE (old) == FUNCTION_DECL
1958 && DECL_ANTICIPATED (old)
1959 && !DECL_HIDDEN_FRIEND_P (old))
1962 else if (old == error_mark_node)
1963 /* Ignore the undefined symbol marker. */
1967 error ("previous non-function declaration %q+#D", old);
1968 error ("conflicts with function declaration %q#D", decl);
1969 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
1973 if (old || TREE_CODE (decl) == TEMPLATE_DECL
1974 /* If it's a using declaration, we always need to build an OVERLOAD,
1975 because it's the only way to remember that the declaration comes
1976 from 'using', and have the lookup behave correctly. */
1977 || (flags & PUSH_USING))
1979 if (old && TREE_CODE (old) != OVERLOAD)
1980 new_binding = ovl_cons (decl, ovl_cons (old, NULL_TREE));
1982 new_binding = ovl_cons (decl, old);
1983 if (flags & PUSH_USING)
1984 OVL_USED (new_binding) = 1;
1987 /* NAME is not ambiguous. */
1991 set_namespace_binding (name, current_namespace, new_binding);
1994 /* We only create an OVERLOAD if there was a previous binding at
1995 this level, or if decl is a template. In the former case, we
1996 need to remove the old binding and replace it with the new
1997 binding. We must also run through the NAMES on the binding
1998 level where the name was bound to update the chain. */
2000 if (TREE_CODE (new_binding) == OVERLOAD && old)
2004 for (d = &IDENTIFIER_BINDING (name)->scope->names;
2006 d = &TREE_CHAIN (*d))
2008 || (TREE_CODE (*d) == TREE_LIST
2009 && TREE_VALUE (*d) == old))
2011 if (TREE_CODE (*d) == TREE_LIST)
2012 /* Just replace the old binding with the new. */
2013 TREE_VALUE (*d) = new_binding;
2015 /* Build a TREE_LIST to wrap the OVERLOAD. */
2016 *d = tree_cons (NULL_TREE, new_binding,
2019 /* And update the cxx_binding node. */
2020 IDENTIFIER_BINDING (name)->value = new_binding;
2021 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
2024 /* We should always find a previous binding in this case. */
2028 /* Install the new binding. */
2029 push_local_binding (name, new_binding, flags);
2032 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
2035 /* Check a non-member using-declaration. Return the name and scope
2036 being used, and the USING_DECL, or NULL_TREE on failure. */
2039 validate_nonmember_using_decl (tree decl, tree scope, tree name)
2041 /* [namespace.udecl]
2042 A using-declaration for a class member shall be a
2043 member-declaration. */
2046 error ("%qT is not a namespace", scope);
2049 else if (scope == error_mark_node)
2052 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR)
2055 A using-declaration shall not name a template-id. */
2056 error ("a using-declaration cannot specify a template-id. "
2057 "Try %<using %D%>", name);
2061 if (TREE_CODE (decl) == NAMESPACE_DECL)
2063 error ("namespace %qD not allowed in using-declaration", decl);
2067 if (TREE_CODE (decl) == SCOPE_REF)
2069 /* It's a nested name with template parameter dependent scope.
2070 This can only be using-declaration for class member. */
2071 error ("%qT is not a namespace", TREE_OPERAND (decl, 0));
2075 if (is_overloaded_fn (decl))
2076 decl = get_first_fn (decl);
2078 gcc_assert (DECL_P (decl));
2080 /* Make a USING_DECL. */
2081 return push_using_decl (scope, name);
2084 /* Process local and global using-declarations. */
2087 do_nonmember_using_decl (tree scope, tree name, tree oldval, tree oldtype,
2088 tree *newval, tree *newtype)
2090 struct scope_binding decls = EMPTY_SCOPE_BINDING;
2092 *newval = *newtype = NULL_TREE;
2093 if (!qualified_lookup_using_namespace (name, scope, &decls, 0))
2097 if (!decls.value && !decls.type)
2099 error ("%qD not declared", name);
2103 /* It is impossible to overload a built-in function; any explicit
2104 declaration eliminates the built-in declaration. So, if OLDVAL
2105 is a built-in, then we can just pretend it isn't there. */
2107 && TREE_CODE (oldval) == FUNCTION_DECL
2108 && DECL_ANTICIPATED (oldval)
2109 && !DECL_HIDDEN_FRIEND_P (oldval))
2112 /* Check for using functions. */
2113 if (decls.value && is_overloaded_fn (decls.value))
2117 if (oldval && !is_overloaded_fn (oldval))
2119 if (!DECL_IMPLICIT_TYPEDEF_P (oldval))
2120 error ("%qD is already declared in this scope", name);
2125 for (tmp = decls.value; tmp; tmp = OVL_NEXT (tmp))
2127 tree new_fn = OVL_CURRENT (tmp);
2129 /* [namespace.udecl]
2131 If a function declaration in namespace scope or block
2132 scope has the same name and the same parameter types as a
2133 function introduced by a using declaration the program is
2135 for (tmp1 = oldval; tmp1; tmp1 = OVL_NEXT (tmp1))
2137 tree old_fn = OVL_CURRENT (tmp1);
2139 if (new_fn == old_fn)
2140 /* The function already exists in the current namespace. */
2142 else if (OVL_USED (tmp1))
2143 continue; /* this is a using decl */
2144 else if (compparms (TYPE_ARG_TYPES (TREE_TYPE (new_fn)),
2145 TYPE_ARG_TYPES (TREE_TYPE (old_fn))))
2147 gcc_assert (!DECL_ANTICIPATED (old_fn)
2148 || DECL_HIDDEN_FRIEND_P (old_fn));
2150 /* There was already a non-using declaration in
2151 this scope with the same parameter types. If both
2152 are the same extern "C" functions, that's ok. */
2153 if (decls_match (new_fn, old_fn))
2157 error ("%qD is already declared in this scope", name);
2163 /* If we broke out of the loop, there's no reason to add
2164 this function to the using declarations for this
2169 /* If we are adding to an existing OVERLOAD, then we no
2170 longer know the type of the set of functions. */
2171 if (*newval && TREE_CODE (*newval) == OVERLOAD)
2172 TREE_TYPE (*newval) = unknown_type_node;
2173 /* Add this new function to the set. */
2174 *newval = build_overload (OVL_CURRENT (tmp), *newval);
2175 /* If there is only one function, then we use its type. (A
2176 using-declaration naming a single function can be used in
2177 contexts where overload resolution cannot be
2179 if (TREE_CODE (*newval) != OVERLOAD)
2181 *newval = ovl_cons (*newval, NULL_TREE);
2182 TREE_TYPE (*newval) = TREE_TYPE (OVL_CURRENT (tmp));
2184 OVL_USED (*newval) = 1;
2189 *newval = decls.value;
2190 if (oldval && !decls_match (*newval, oldval))
2191 error ("%qD is already declared in this scope", name);
2194 *newtype = decls.type;
2195 if (oldtype && *newtype && !same_type_p (oldtype, *newtype))
2197 error ("using declaration %qD introduced ambiguous type %qT",
2203 /* Process a using-declaration at function scope. */
2206 do_local_using_decl (tree decl, tree scope, tree name)
2208 tree oldval, oldtype, newval, newtype;
2209 tree orig_decl = decl;
2211 decl = validate_nonmember_using_decl (decl, scope, name);
2212 if (decl == NULL_TREE)
2215 if (building_stmt_tree ()
2216 && at_function_scope_p ())
2217 add_decl_expr (decl);
2219 oldval = lookup_name_innermost_nonclass_level (name);
2220 oldtype = lookup_type_current_level (name);
2222 do_nonmember_using_decl (scope, name, oldval, oldtype, &newval, &newtype);
2226 if (is_overloaded_fn (newval))
2230 /* We only need to push declarations for those functions
2231 that were not already bound in the current level.
2232 The old value might be NULL_TREE, it might be a single
2233 function, or an OVERLOAD. */
2234 if (oldval && TREE_CODE (oldval) == OVERLOAD)
2235 term = OVL_FUNCTION (oldval);
2238 for (fn = newval; fn && OVL_CURRENT (fn) != term;
2240 push_overloaded_decl (OVL_CURRENT (fn),
2241 PUSH_LOCAL | PUSH_USING,
2245 push_local_binding (name, newval, PUSH_USING);
2249 push_local_binding (name, newtype, PUSH_USING);
2250 set_identifier_type_value (name, newtype);
2253 /* Emit debug info. */
2254 if (!processing_template_decl)
2255 cp_emit_debug_info_for_using (orig_decl, current_scope());
2258 /* Returns true if ROOT (a namespace, class, or function) encloses
2259 CHILD. CHILD may be either a class type or a namespace. */
2262 is_ancestor (tree root, tree child)
2264 gcc_assert ((TREE_CODE (root) == NAMESPACE_DECL
2265 || TREE_CODE (root) == FUNCTION_DECL
2266 || CLASS_TYPE_P (root)));
2267 gcc_assert ((TREE_CODE (child) == NAMESPACE_DECL
2268 || CLASS_TYPE_P (child)));
2270 /* The global namespace encloses everything. */
2271 if (root == global_namespace)
2276 /* If we've run out of scopes, stop. */
2279 /* If we've reached the ROOT, it encloses CHILD. */
2282 /* Go out one level. */
2284 child = TYPE_NAME (child);
2285 child = DECL_CONTEXT (child);
2289 /* Enter the class or namespace scope indicated by T suitable for name
2290 lookup. T can be arbitrary scope, not necessary nested inside the
2291 current scope. Returns a non-null scope to pop iff pop_scope
2292 should be called later to exit this scope. */
2297 if (TREE_CODE (t) == NAMESPACE_DECL)
2298 push_decl_namespace (t);
2299 else if (CLASS_TYPE_P (t))
2301 if (!at_class_scope_p ()
2302 || !same_type_p (current_class_type, t))
2303 push_nested_class (t);
2305 /* T is the same as the current scope. There is therefore no
2306 need to re-enter the scope. Since we are not actually
2307 pushing a new scope, our caller should not call
2315 /* Leave scope pushed by push_scope. */
2320 if (TREE_CODE (t) == NAMESPACE_DECL)
2321 pop_decl_namespace ();
2322 else if CLASS_TYPE_P (t)
2323 pop_nested_class ();
2326 /* Subroutine of push_inner_scope. */
2329 push_inner_scope_r (tree outer, tree inner)
2334 || (TREE_CODE (inner) != NAMESPACE_DECL && !CLASS_TYPE_P (inner)))
2337 prev = CP_DECL_CONTEXT (TREE_CODE (inner) == NAMESPACE_DECL ? inner : TYPE_NAME (inner));
2339 push_inner_scope_r (outer, prev);
2340 if (TREE_CODE (inner) == NAMESPACE_DECL)
2342 struct cp_binding_level *save_template_parm = 0;
2343 /* Temporary take out template parameter scopes. They are saved
2344 in reversed order in save_template_parm. */
2345 while (current_binding_level->kind == sk_template_parms)
2347 struct cp_binding_level *b = current_binding_level;
2348 current_binding_level = b->level_chain;
2349 b->level_chain = save_template_parm;
2350 save_template_parm = b;
2353 resume_scope (NAMESPACE_LEVEL (inner));
2354 current_namespace = inner;
2356 /* Restore template parameter scopes. */
2357 while (save_template_parm)
2359 struct cp_binding_level *b = save_template_parm;
2360 save_template_parm = b->level_chain;
2361 b->level_chain = current_binding_level;
2362 current_binding_level = b;
2369 /* Enter the scope INNER from current scope. INNER must be a scope
2370 nested inside current scope. This works with both name lookup and
2371 pushing name into scope. In case a template parameter scope is present,
2372 namespace is pushed under the template parameter scope according to
2373 name lookup rule in 14.6.1/6.
2375 Return the former current scope suitable for pop_inner_scope. */
2378 push_inner_scope (tree inner)
2380 tree outer = current_scope ();
2382 outer = current_namespace;
2384 push_inner_scope_r (outer, inner);
2388 /* Exit the current scope INNER back to scope OUTER. */
2391 pop_inner_scope (tree outer, tree inner)
2394 || (TREE_CODE (inner) != NAMESPACE_DECL && !CLASS_TYPE_P (inner)))
2397 while (outer != inner)
2399 if (TREE_CODE (inner) == NAMESPACE_DECL)
2401 struct cp_binding_level *save_template_parm = 0;
2402 /* Temporary take out template parameter scopes. They are saved
2403 in reversed order in save_template_parm. */
2404 while (current_binding_level->kind == sk_template_parms)
2406 struct cp_binding_level *b = current_binding_level;
2407 current_binding_level = b->level_chain;
2408 b->level_chain = save_template_parm;
2409 save_template_parm = b;
2414 /* Restore template parameter scopes. */
2415 while (save_template_parm)
2417 struct cp_binding_level *b = save_template_parm;
2418 save_template_parm = b->level_chain;
2419 b->level_chain = current_binding_level;
2420 current_binding_level = b;
2426 inner = CP_DECL_CONTEXT (TREE_CODE (inner) == NAMESPACE_DECL ? inner : TYPE_NAME (inner));
2430 /* Do a pushlevel for class declarations. */
2433 pushlevel_class (void)
2435 if (ENABLE_SCOPE_CHECKING)
2438 class_binding_level = begin_scope (sk_class, current_class_type);
2441 /* ...and a poplevel for class declarations. */
2444 poplevel_class (void)
2446 struct cp_binding_level *level = class_binding_level;
2447 cp_class_binding *cb;
2451 timevar_push (TV_NAME_LOOKUP);
2452 gcc_assert (level != 0);
2454 /* If we're leaving a toplevel class, cache its binding level. */
2455 if (current_class_depth == 1)
2456 previous_class_level = level;
2457 for (shadowed = level->type_shadowed;
2459 shadowed = TREE_CHAIN (shadowed))
2460 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (shadowed), TREE_VALUE (shadowed));
2462 /* Remove the bindings for all of the class-level declarations. */
2463 if (level->class_shadowed)
2466 VEC_iterate (cp_class_binding, level->class_shadowed, i, cb);
2468 IDENTIFIER_BINDING (cb->identifier) = cb->base.previous;
2469 ggc_free (level->class_shadowed);
2470 level->class_shadowed = NULL;
2473 /* Now, pop out of the binding level which we created up in the
2474 `pushlevel_class' routine. */
2475 if (ENABLE_SCOPE_CHECKING)
2479 timevar_pop (TV_NAME_LOOKUP);
2482 /* Set INHERITED_VALUE_BINDING_P on BINDING to true or false, as
2483 appropriate. DECL is the value to which a name has just been
2484 bound. CLASS_TYPE is the class in which the lookup occurred. */
2487 set_inherited_value_binding_p (cxx_binding *binding, tree decl,
2490 if (binding->value == decl && TREE_CODE (decl) != TREE_LIST)
2494 if (TREE_CODE (decl) == OVERLOAD)
2495 context = CP_DECL_CONTEXT (OVL_CURRENT (decl));
2498 gcc_assert (DECL_P (decl));
2499 context = context_for_name_lookup (decl);
2502 if (is_properly_derived_from (class_type, context))
2503 INHERITED_VALUE_BINDING_P (binding) = 1;
2505 INHERITED_VALUE_BINDING_P (binding) = 0;
2507 else if (binding->value == decl)
2508 /* We only encounter a TREE_LIST when there is an ambiguity in the
2509 base classes. Such an ambiguity can be overridden by a
2510 definition in this class. */
2511 INHERITED_VALUE_BINDING_P (binding) = 1;
2513 INHERITED_VALUE_BINDING_P (binding) = 0;
2516 /* Make the declaration of X appear in CLASS scope. */
2519 pushdecl_class_level (tree x)
2522 bool is_valid = true;
2524 timevar_push (TV_NAME_LOOKUP);
2525 /* Get the name of X. */
2526 if (TREE_CODE (x) == OVERLOAD)
2527 name = DECL_NAME (get_first_fn (x));
2529 name = DECL_NAME (x);
2533 is_valid = push_class_level_binding (name, x);
2534 if (TREE_CODE (x) == TYPE_DECL)
2535 set_identifier_type_value (name, x);
2537 else if (ANON_AGGR_TYPE_P (TREE_TYPE (x)))
2539 /* If X is an anonymous aggregate, all of its members are
2540 treated as if they were members of the class containing the
2541 aggregate, for naming purposes. */
2544 for (f = TYPE_FIELDS (TREE_TYPE (x)); f; f = TREE_CHAIN (f))
2546 location_t save_location = input_location;
2547 input_location = DECL_SOURCE_LOCATION (f);
2548 if (!pushdecl_class_level (f))
2550 input_location = save_location;
2553 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, is_valid);
2556 /* Return the BINDING (if any) for NAME in SCOPE, which is a class
2557 scope. If the value returned is non-NULL, and the PREVIOUS field
2558 is not set, callers must set the PREVIOUS field explicitly. */
2560 static cxx_binding *
2561 get_class_binding (tree name, cxx_scope *scope)
2566 cxx_binding *binding;
2568 class_type = scope->this_entity;
2570 /* Get the type binding. */
2571 type_binding = lookup_member (class_type, name,
2572 /*protect=*/2, /*want_type=*/true);
2573 /* Get the value binding. */
2574 value_binding = lookup_member (class_type, name,
2575 /*protect=*/2, /*want_type=*/false);
2578 && (TREE_CODE (value_binding) == TYPE_DECL
2579 || DECL_CLASS_TEMPLATE_P (value_binding)
2580 || (TREE_CODE (value_binding) == TREE_LIST
2581 && TREE_TYPE (value_binding) == error_mark_node
2582 && (TREE_CODE (TREE_VALUE (value_binding))
2584 /* We found a type binding, even when looking for a non-type
2585 binding. This means that we already processed this binding
2588 else if (value_binding)
2590 if (TREE_CODE (value_binding) == TREE_LIST
2591 && TREE_TYPE (value_binding) == error_mark_node)
2592 /* NAME is ambiguous. */
2594 else if (BASELINK_P (value_binding))
2595 /* NAME is some overloaded functions. */
2596 value_binding = BASELINK_FUNCTIONS (value_binding);
2599 /* If we found either a type binding or a value binding, create a
2600 new binding object. */
2601 if (type_binding || value_binding)
2603 binding = new_class_binding (name,
2607 /* This is a class-scope binding, not a block-scope binding. */
2608 LOCAL_BINDING_P (binding) = 0;
2609 set_inherited_value_binding_p (binding, value_binding, class_type);
2617 /* Make the declaration(s) of X appear in CLASS scope under the name
2618 NAME. Returns true if the binding is valid. */
2621 push_class_level_binding (tree name, tree x)
2623 cxx_binding *binding;
2627 timevar_push (TV_NAME_LOOKUP);
2628 /* The class_binding_level will be NULL if x is a template
2629 parameter name in a member template. */
2630 if (!class_binding_level)
2631 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, true);
2633 if (name == error_mark_node)
2634 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, false);
2636 /* Check for invalid member names. */
2637 gcc_assert (TYPE_BEING_DEFINED (current_class_type));
2638 /* We could have been passed a tree list if this is an ambiguous
2639 declaration. If so, pull the declaration out because
2640 check_template_shadow will not handle a TREE_LIST. */
2641 if (TREE_CODE (decl) == TREE_LIST
2642 && TREE_TYPE (decl) == error_mark_node)
2643 decl = TREE_VALUE (decl);
2645 check_template_shadow (decl);
2649 If T is the name of a class, then each of the following shall
2650 have a name different from T:
2652 -- every static data member of class T;
2654 -- every member of class T that is itself a type;
2656 -- every enumerator of every member of class T that is an
2659 -- every member of every anonymous union that is a member of
2662 (Non-static data members were also forbidden to have the same
2663 name as T until TC1.) */
2664 if ((TREE_CODE (x) == VAR_DECL
2665 || TREE_CODE (x) == CONST_DECL
2666 || (TREE_CODE (x) == TYPE_DECL
2667 && !DECL_SELF_REFERENCE_P (x))
2668 /* A data member of an anonymous union. */
2669 || (TREE_CODE (x) == FIELD_DECL
2670 && DECL_CONTEXT (x) != current_class_type))
2671 && DECL_NAME (x) == constructor_name (current_class_type))
2673 tree scope = context_for_name_lookup (x);
2674 if (TYPE_P (scope) && same_type_p (scope, current_class_type))
2676 error ("%qD has the same name as the class in which it is "
2679 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, false);
2683 /* Get the current binding for NAME in this class, if any. */
2684 binding = IDENTIFIER_BINDING (name);
2685 if (!binding || binding->scope != class_binding_level)
2687 binding = get_class_binding (name, class_binding_level);
2688 /* If a new binding was created, put it at the front of the
2689 IDENTIFIER_BINDING list. */
2692 binding->previous = IDENTIFIER_BINDING (name);
2693 IDENTIFIER_BINDING (name) = binding;
2697 /* If there is already a binding, then we may need to update the
2699 if (binding && binding->value)
2701 tree bval = binding->value;
2702 tree old_decl = NULL_TREE;
2704 if (INHERITED_VALUE_BINDING_P (binding))
2706 /* If the old binding was from a base class, and was for a
2707 tag name, slide it over to make room for the new binding.
2708 The old binding is still visible if explicitly qualified
2709 with a class-key. */
2710 if (TREE_CODE (bval) == TYPE_DECL && DECL_ARTIFICIAL (bval)
2711 && !(TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x)))
2713 old_decl = binding->type;
2714 binding->type = bval;
2715 binding->value = NULL_TREE;
2716 INHERITED_VALUE_BINDING_P (binding) = 0;
2721 /* Any inherited type declaration is hidden by the type
2722 declaration in the derived class. */
2723 if (TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x))
2724 binding->type = NULL_TREE;
2727 else if (TREE_CODE (x) == OVERLOAD && is_overloaded_fn (bval))
2729 else if (TREE_CODE (x) == USING_DECL && TREE_CODE (bval) == USING_DECL)
2730 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, true);
2731 else if (TREE_CODE (x) == USING_DECL && is_overloaded_fn (bval))
2733 else if (TREE_CODE (bval) == USING_DECL && is_overloaded_fn (x))
2734 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, true);
2736 if (old_decl && binding->scope == class_binding_level)
2739 /* It is always safe to clear INHERITED_VALUE_BINDING_P
2740 here. This function is only used to register bindings
2741 from with the class definition itself. */
2742 INHERITED_VALUE_BINDING_P (binding) = 0;
2743 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, true);
2747 /* Note that we declared this value so that we can issue an error if
2748 this is an invalid redeclaration of a name already used for some
2750 note_name_declared_in_class (name, decl);
2752 /* If we didn't replace an existing binding, put the binding on the
2753 stack of bindings for the identifier, and update the shadowed
2755 if (binding && binding->scope == class_binding_level)
2756 /* Supplement the existing binding. */
2757 ok = supplement_binding (binding, decl);
2760 /* Create a new binding. */
2761 push_binding (name, decl, class_binding_level);
2765 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, ok);
2768 /* Process "using SCOPE::NAME" in a class scope. Return the
2769 USING_DECL created. */
2772 do_class_using_decl (tree scope, tree name)
2774 /* The USING_DECL returned by this function. */
2776 /* The declaration (or declarations) name by this using
2777 declaration. NULL if we are in a template and cannot figure out
2778 what has been named. */
2780 /* True if SCOPE is a dependent type. */
2781 bool scope_dependent_p;
2782 /* True if SCOPE::NAME is dependent. */
2783 bool name_dependent_p;
2784 /* True if any of the bases of CURRENT_CLASS_TYPE are dependent. */
2785 bool bases_dependent_p;
2790 if (name == error_mark_node)
2793 if (!scope || !TYPE_P (scope))
2795 error ("using-declaration for non-member at class scope");
2799 /* Make sure the name is not invalid */
2800 if (TREE_CODE (name) == BIT_NOT_EXPR)
2802 error ("%<%T::%D%> names destructor", scope, name);
2805 if (constructor_name_p (name, scope))
2807 error ("%<%T::%D%> names constructor", scope, name);
2810 if (constructor_name_p (name, current_class_type))
2812 error ("%<%T::%D%> names constructor in %qT",
2813 scope, name, current_class_type);
2817 scope_dependent_p = dependent_type_p (scope);
2818 name_dependent_p = (scope_dependent_p
2819 || (IDENTIFIER_TYPENAME_P (name)
2820 && dependent_type_p (TREE_TYPE (name))));
2822 bases_dependent_p = false;
2823 if (processing_template_decl)
2824 for (binfo = TYPE_BINFO (current_class_type), i = 0;
2825 BINFO_BASE_ITERATE (binfo, i, base_binfo);
2827 if (dependent_type_p (TREE_TYPE (base_binfo)))
2829 bases_dependent_p = true;
2835 /* From [namespace.udecl]:
2837 A using-declaration used as a member-declaration shall refer to a
2838 member of a base class of the class being defined.
2840 In general, we cannot check this constraint in a template because
2841 we do not know the entire set of base classes of the current
2842 class type. However, if all of the base classes are
2843 non-dependent, then we can avoid delaying the check until
2845 if (!scope_dependent_p)
2848 binfo = lookup_base (current_class_type, scope, ba_any, &b_kind);
2849 if (b_kind < bk_proper_base)
2851 if (!bases_dependent_p)
2853 error_not_base_type (scope, current_class_type);
2857 else if (!name_dependent_p)
2859 decl = lookup_member (binfo, name, 0, false);
2862 error ("no members matching %<%T::%D%> in %q#T", scope, name,
2866 /* The binfo from which the functions came does not matter. */
2867 if (BASELINK_P (decl))
2868 decl = BASELINK_FUNCTIONS (decl);
2872 value = build_lang_decl (USING_DECL, name, NULL_TREE);
2873 USING_DECL_DECLS (value) = decl;
2874 USING_DECL_SCOPE (value) = scope;
2875 DECL_DEPENDENT_P (value) = !decl;
2881 /* Return the binding value for name in scope. */
2884 namespace_binding (tree name, tree scope)
2886 cxx_binding *binding;
2889 scope = global_namespace;
2891 /* Unnecessary for the global namespace because it can't be an alias. */
2892 scope = ORIGINAL_NAMESPACE (scope);
2894 binding = cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (scope), name);
2896 return binding ? binding->value : NULL_TREE;
2899 /* Set the binding value for name in scope. */
2902 set_namespace_binding (tree name, tree scope, tree val)
2906 timevar_push (TV_NAME_LOOKUP);
2907 if (scope == NULL_TREE)
2908 scope = global_namespace;
2909 b = binding_for_name (NAMESPACE_LEVEL (scope), name);
2910 if (!b->value || TREE_CODE (val) == OVERLOAD || val == error_mark_node)
2913 supplement_binding (b, val);
2914 timevar_pop (TV_NAME_LOOKUP);
2917 /* Set the context of a declaration to scope. Complain if we are not
2921 set_decl_namespace (tree decl, tree scope, bool friendp)
2925 /* Get rid of namespace aliases. */
2926 scope = ORIGINAL_NAMESPACE (scope);
2928 /* It is ok for friends to be qualified in parallel space. */
2929 if (!friendp && !is_ancestor (current_namespace, scope))
2930 error ("declaration of %qD not in a namespace surrounding %qD",
2932 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
2934 /* Writing "int N::i" to declare a variable within "N" is invalid. */
2935 if (scope == current_namespace)
2937 if (at_namespace_scope_p ())
2938 error ("explicit qualification in declaration of %qD",
2943 /* See whether this has been declared in the namespace. */
2944 old = lookup_qualified_name (scope, DECL_NAME (decl), false, true);
2946 /* No old declaration at all. */
2948 if (!is_overloaded_fn (decl))
2949 /* Don't compare non-function decls with decls_match here, since
2950 it can't check for the correct constness at this
2951 point. pushdecl will find those errors later. */
2953 /* Since decl is a function, old should contain a function decl. */
2954 if (!is_overloaded_fn (old))
2956 fn = OVL_CURRENT (old);
2957 if (!is_associated_namespace (scope, CP_DECL_CONTEXT (fn)))
2959 /* A template can be explicitly specialized in any namespace. */
2960 if (processing_explicit_instantiation)
2962 if (processing_template_decl || processing_specialization)
2963 /* We have not yet called push_template_decl to turn a
2964 FUNCTION_DECL into a TEMPLATE_DECL, so the declarations won't
2965 match. But, we'll check later, when we construct the
2968 /* Instantiations or specializations of templates may be declared as
2969 friends in any namespace. */
2970 if (friendp && DECL_USE_TEMPLATE (decl))
2972 if (is_overloaded_fn (old))
2974 for (; old; old = OVL_NEXT (old))
2975 if (decls_match (decl, OVL_CURRENT (old)))
2978 else if (decls_match (decl, old))
2981 error ("%qD should have been declared inside %qD", decl, scope);
2984 /* Return the namespace where the current declaration is declared. */
2987 current_decl_namespace (void)
2990 /* If we have been pushed into a different namespace, use it. */
2991 if (decl_namespace_list)
2992 return TREE_PURPOSE (decl_namespace_list);
2994 if (current_class_type)
2995 result = decl_namespace_context (current_class_type);
2996 else if (current_function_decl)
2997 result = decl_namespace_context (current_function_decl);
2999 result = current_namespace;
3003 /* Push into the scope of the NAME namespace. If NAME is NULL_TREE, then we
3004 select a name that is unique to this compilation unit. */
3007 push_namespace (tree name)
3009 push_namespace_with_attribs (name, NULL_TREE);
3012 /* Same, but specify attributes to apply to the namespace. The attributes
3013 only apply to the current namespace-body, not to any later extensions. */
3016 push_namespace_with_attribs (tree name, tree attributes)
3020 int implicit_use = 0;
3023 timevar_push (TV_NAME_LOOKUP);
3025 /* We should not get here if the global_namespace is not yet constructed
3026 nor if NAME designates the global namespace: The global scope is
3027 constructed elsewhere. */
3028 gcc_assert (global_namespace != NULL && name != global_scope_name);
3032 name = get_anonymous_namespace_name();
3033 d = IDENTIFIER_NAMESPACE_VALUE (name);
3035 /* Reopening anonymous namespace. */
3041 /* Check whether this is an extended namespace definition. */
3042 d = IDENTIFIER_NAMESPACE_VALUE (name);
3043 if (d != NULL_TREE && TREE_CODE (d) == NAMESPACE_DECL)
3046 if (DECL_NAMESPACE_ALIAS (d))
3048 error ("namespace alias %qD not allowed here, assuming %qD",
3049 d, DECL_NAMESPACE_ALIAS (d));
3050 d = DECL_NAMESPACE_ALIAS (d);
3057 /* Make a new namespace, binding the name to it. */
3058 d = build_lang_decl (NAMESPACE_DECL, name, void_type_node);
3059 DECL_CONTEXT (d) = FROB_CONTEXT (current_namespace);
3060 /* The name of this namespace is not visible to other translation
3061 units if it is an anonymous namespace or member thereof. */
3062 if (anon || decl_anon_ns_mem_p (current_namespace))
3063 TREE_PUBLIC (d) = 0;
3065 TREE_PUBLIC (d) = 1;
3069 /* Clear DECL_NAME for the benefit of debugging back ends. */
3070 SET_DECL_ASSEMBLER_NAME (d, name);
3071 DECL_NAME (d) = NULL_TREE;
3073 begin_scope (sk_namespace, d);
3076 resume_scope (NAMESPACE_LEVEL (d));
3079 do_using_directive (d);
3080 /* Enter the name space. */
3081 current_namespace = d;
3083 #ifdef HANDLE_PRAGMA_VISIBILITY
3084 /* Clear has_visibility in case a previous namespace-definition had a
3085 visibility attribute and this one doesn't. */
3086 current_binding_level->has_visibility = 0;
3087 for (d = attributes; d; d = TREE_CHAIN (d))
3089 tree name = TREE_PURPOSE (d);
3090 tree args = TREE_VALUE (d);
3093 if (! is_attribute_p ("visibility", name))
3095 warning (OPT_Wattributes, "%qs attribute directive ignored",
3096 IDENTIFIER_POINTER (name));
3100 x = args ? TREE_VALUE (args) : NULL_TREE;
3101 if (x == NULL_TREE || TREE_CODE (x) != STRING_CST || TREE_CHAIN (args))
3103 warning (OPT_Wattributes, "%qs attribute requires a single NTBS argument",
3104 IDENTIFIER_POINTER (name));
3108 current_binding_level->has_visibility = 1;
3109 push_visibility (TREE_STRING_POINTER (x));
3115 timevar_pop (TV_NAME_LOOKUP);
3118 /* Pop from the scope of the current namespace. */
3121 pop_namespace (void)
3123 gcc_assert (current_namespace != global_namespace);
3124 current_namespace = CP_DECL_CONTEXT (current_namespace);
3125 /* The binding level is not popped, as it might be re-opened later. */
3129 /* Push into the scope of the namespace NS, even if it is deeply
3130 nested within another namespace. */
3133 push_nested_namespace (tree ns)
3135 if (ns == global_namespace)
3136 push_to_top_level ();
3139 push_nested_namespace (CP_DECL_CONTEXT (ns));
3140 push_namespace (DECL_NAME (ns));
3144 /* Pop back from the scope of the namespace NS, which was previously
3145 entered with push_nested_namespace. */
3148 pop_nested_namespace (tree ns)
3150 timevar_push (TV_NAME_LOOKUP);
3151 while (ns != global_namespace)
3154 ns = CP_DECL_CONTEXT (ns);
3157 pop_from_top_level ();
3158 timevar_pop (TV_NAME_LOOKUP);
3161 /* Temporarily set the namespace for the current declaration. */
3164 push_decl_namespace (tree decl)
3166 if (TREE_CODE (decl) != NAMESPACE_DECL)
3167 decl = decl_namespace_context (decl);
3168 decl_namespace_list = tree_cons (ORIGINAL_NAMESPACE (decl),
3169 NULL_TREE, decl_namespace_list);
3172 /* [namespace.memdef]/2 */
3175 pop_decl_namespace (void)
3177 decl_namespace_list = TREE_CHAIN (decl_namespace_list);
3180 /* Return the namespace that is the common ancestor
3181 of two given namespaces. */
3184 namespace_ancestor (tree ns1, tree ns2)
3186 timevar_push (TV_NAME_LOOKUP);
3187 if (is_ancestor (ns1, ns2))
3188 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, ns1);
3189 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP,
3190 namespace_ancestor (CP_DECL_CONTEXT (ns1), ns2));
3193 /* Process a namespace-alias declaration. */
3196 do_namespace_alias (tree alias, tree namespace)
3198 if (namespace == error_mark_node)
3201 gcc_assert (TREE_CODE (namespace) == NAMESPACE_DECL);
3203 namespace = ORIGINAL_NAMESPACE (namespace);
3205 /* Build the alias. */
3206 alias = build_lang_decl (NAMESPACE_DECL, alias, void_type_node);
3207 DECL_NAMESPACE_ALIAS (alias) = namespace;
3208 DECL_EXTERNAL (alias) = 1;
3209 DECL_CONTEXT (alias) = FROB_CONTEXT (current_scope ());
3212 /* Emit debug info for namespace alias. */
3213 (*debug_hooks->global_decl) (alias);
3216 /* Like pushdecl, only it places X in the current namespace,
3220 pushdecl_namespace_level (tree x, bool is_friend)
3222 struct cp_binding_level *b = current_binding_level;
3225 timevar_push (TV_NAME_LOOKUP);
3226 t = pushdecl_with_scope (x, NAMESPACE_LEVEL (current_namespace), is_friend);
3228 /* Now, the type_shadowed stack may screw us. Munge it so it does
3230 if (TREE_CODE (t) == TYPE_DECL)
3232 tree name = DECL_NAME (t);
3234 tree *ptr = (tree *)0;
3235 for (; !global_scope_p (b); b = b->level_chain)
3237 tree shadowed = b->type_shadowed;
3238 for (; shadowed; shadowed = TREE_CHAIN (shadowed))
3239 if (TREE_PURPOSE (shadowed) == name)
3241 ptr = &TREE_VALUE (shadowed);
3242 /* Can't break out of the loop here because sometimes
3243 a binding level will have duplicate bindings for
3244 PT names. It's gross, but I haven't time to fix it. */
3247 newval = TREE_TYPE (t);
3248 if (ptr == (tree *)0)
3250 /* @@ This shouldn't be needed. My test case "zstring.cc" trips
3251 up here if this is changed to an assertion. --KR */
3252 SET_IDENTIFIER_TYPE_VALUE (name, t);
3259 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
3262 /* Insert USED into the using list of USER. Set INDIRECT_flag if this
3263 directive is not directly from the source. Also find the common
3264 ancestor and let our users know about the new namespace */
3266 add_using_namespace (tree user, tree used, bool indirect)
3269 timevar_push (TV_NAME_LOOKUP);
3270 /* Using oneself is a no-op. */
3273 timevar_pop (TV_NAME_LOOKUP);
3276 gcc_assert (TREE_CODE (user) == NAMESPACE_DECL);
3277 gcc_assert (TREE_CODE (used) == NAMESPACE_DECL);
3278 /* Check if we already have this. */
3279 t = purpose_member (used, DECL_NAMESPACE_USING (user));
3283 /* Promote to direct usage. */
3284 TREE_INDIRECT_USING (t) = 0;
3285 timevar_pop (TV_NAME_LOOKUP);
3289 /* Add used to the user's using list. */
3290 DECL_NAMESPACE_USING (user)
3291 = tree_cons (used, namespace_ancestor (user, used),
3292 DECL_NAMESPACE_USING (user));
3294 TREE_INDIRECT_USING (DECL_NAMESPACE_USING (user)) = indirect;
3296 /* Add user to the used's users list. */
3297 DECL_NAMESPACE_USERS (used)
3298 = tree_cons (user, 0, DECL_NAMESPACE_USERS (used));
3300 /* Recursively add all namespaces used. */
3301 for (t = DECL_NAMESPACE_USING (used); t; t = TREE_CHAIN (t))
3302 /* indirect usage */
3303 add_using_namespace (user, TREE_PURPOSE (t), 1);
3305 /* Tell everyone using us about the new used namespaces. */
3306 for (t = DECL_NAMESPACE_USERS (user); t; t = TREE_CHAIN (t))
3307 add_using_namespace (TREE_PURPOSE (t), used, 1);
3308 timevar_pop (TV_NAME_LOOKUP);
3311 /* Process a using-declaration not appearing in class or local scope. */
3314 do_toplevel_using_decl (tree decl, tree scope, tree name)
3316 tree oldval, oldtype, newval, newtype;
3317 tree orig_decl = decl;
3318 cxx_binding *binding;
3320 decl = validate_nonmember_using_decl (decl, scope, name);
3321 if (decl == NULL_TREE)
3324 binding = binding_for_name (NAMESPACE_LEVEL (current_namespace), name);
3326 oldval = binding->value;
3327 oldtype = binding->type;
3329 do_nonmember_using_decl (scope, name, oldval, oldtype, &newval, &newtype);
3331 /* Emit debug info. */
3332 if (!processing_template_decl)
3333 cp_emit_debug_info_for_using (orig_decl, current_namespace);
3335 /* Copy declarations found. */
3337 binding->value = newval;
3339 binding->type = newtype;
3342 /* Process a using-directive. */
3345 do_using_directive (tree namespace)
3347 tree context = NULL_TREE;
3349 if (namespace == error_mark_node)
3352 gcc_assert (TREE_CODE (namespace) == NAMESPACE_DECL);
3354 if (building_stmt_tree ())
3355 add_stmt (build_stmt (USING_STMT, namespace));
3356 namespace = ORIGINAL_NAMESPACE (namespace);
3358 if (!toplevel_bindings_p ())
3360 push_using_directive (namespace);
3361 context = current_scope ();
3366 add_using_namespace (current_namespace, namespace, 0);
3367 if (current_namespace != global_namespace)
3368 context = current_namespace;
3371 /* Emit debugging info. */
3372 if (!processing_template_decl)
3373 (*debug_hooks->imported_module_or_decl) (namespace, context);
3376 /* Deal with a using-directive seen by the parser. Currently we only
3377 handle attributes here, since they cannot appear inside a template. */
3380 parse_using_directive (tree namespace, tree attribs)
3384 do_using_directive (namespace);
3386 for (a = attribs; a; a = TREE_CHAIN (a))
3388 tree name = TREE_PURPOSE (a);
3389 if (is_attribute_p ("strong", name))
3391 if (!toplevel_bindings_p ())
3392 error ("strong using only meaningful at namespace scope");
3393 else if (namespace != error_mark_node)
3395 if (!is_ancestor (current_namespace, namespace))
3396 error ("current namespace %qD does not enclose strongly used namespace %qD",
3397 current_namespace, namespace);
3398 DECL_NAMESPACE_ASSOCIATIONS (namespace)
3399 = tree_cons (current_namespace, 0,
3400 DECL_NAMESPACE_ASSOCIATIONS (namespace));
3404 warning (OPT_Wattributes, "%qD attribute directive ignored", name);
3408 /* Like pushdecl, only it places X in the global scope if appropriate.
3409 Calls cp_finish_decl to register the variable, initializing it with
3410 *INIT, if INIT is non-NULL. */
3413 pushdecl_top_level_1 (tree x, tree *init, bool is_friend)
3415 timevar_push (TV_NAME_LOOKUP);
3416 push_to_top_level ();
3417 x = pushdecl_namespace_level (x, is_friend);
3419 finish_decl (x, *init, NULL_TREE);
3420 pop_from_top_level ();
3421 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
3424 /* Like pushdecl, only it places X in the global scope if appropriate. */
3427 pushdecl_top_level (tree x)
3429 return pushdecl_top_level_1 (x, NULL, false);
3432 /* Like pushdecl_top_level, but adding the IS_FRIEND parameter. */
3435 pushdecl_top_level_maybe_friend (tree x, bool is_friend)
3437 return pushdecl_top_level_1 (x, NULL, is_friend);
3440 /* Like pushdecl, only it places X in the global scope if
3441 appropriate. Calls cp_finish_decl to register the variable,
3442 initializing it with INIT. */
3445 pushdecl_top_level_and_finish (tree x, tree init)
3447 return pushdecl_top_level_1 (x, &init, false);
3450 /* Combines two sets of overloaded functions into an OVERLOAD chain, removing
3451 duplicates. The first list becomes the tail of the result.
3453 The algorithm is O(n^2). We could get this down to O(n log n) by
3454 doing a sort on the addresses of the functions, if that becomes
3458 merge_functions (tree s1, tree s2)
3460 for (; s2; s2 = OVL_NEXT (s2))
3462 tree fn2 = OVL_CURRENT (s2);
3465 for (fns1 = s1; fns1; fns1 = OVL_NEXT (fns1))
3467 tree fn1 = OVL_CURRENT (fns1);
3469 /* If the function from S2 is already in S1, there is no
3470 need to add it again. For `extern "C"' functions, we
3471 might have two FUNCTION_DECLs for the same function, in
3472 different namespaces; again, we only need one of them. */
3474 || (DECL_EXTERN_C_P (fn1) && DECL_EXTERN_C_P (fn2)
3475 && DECL_NAME (fn1) == DECL_NAME (fn2)))
3479 /* If we exhausted all of the functions in S1, FN2 is new. */
3481 s1 = build_overload (fn2, s1);
3486 /* This should return an error not all definitions define functions.
3487 It is not an error if we find two functions with exactly the
3488 same signature, only if these are selected in overload resolution.
3489 old is the current set of bindings, new the freshly-found binding.
3490 XXX Do we want to give *all* candidates in case of ambiguity?
3491 XXX In what way should I treat extern declarations?
3492 XXX I don't want to repeat the entire duplicate_decls here */
3495 ambiguous_decl (tree name, struct scope_binding *old, cxx_binding *new,
3499 gcc_assert (old != NULL);
3500 /* Copy the value. */
3503 switch (TREE_CODE (val))
3506 /* If we expect types or namespaces, and not templates,
3507 or this is not a template class. */
3508 if ((LOOKUP_QUALIFIERS_ONLY (flags)
3509 && !DECL_CLASS_TEMPLATE_P (val))
3510 || hidden_name_p (val))
3514 if (LOOKUP_NAMESPACES_ONLY (flags) || hidden_name_p (val))
3517 case NAMESPACE_DECL:
3518 if (LOOKUP_TYPES_ONLY (flags))
3522 /* Ignore built-in functions that are still anticipated. */
3523 if (LOOKUP_QUALIFIERS_ONLY (flags) || hidden_name_p (val))
3527 if (LOOKUP_QUALIFIERS_ONLY (flags))
3533 else if (val && val != old->value)
3535 if (is_overloaded_fn (old->value) && is_overloaded_fn (val))
3536 old->value = merge_functions (old->value, val);
3539 old->value = tree_cons (NULL_TREE, old->value,
3540 build_tree_list (NULL_TREE, new->value));
3541 TREE_TYPE (old->value) = error_mark_node;
3544 /* ... and copy the type. */
3546 if (LOOKUP_NAMESPACES_ONLY (flags))
3550 else if (type && old->type != type)
3552 if (flags & LOOKUP_COMPLAIN)
3554 error ("%qD denotes an ambiguous type",name);
3555 error ("%J first type here", TYPE_MAIN_DECL (old->type));
3556 error ("%J other type here", TYPE_MAIN_DECL (type));
3561 /* Return the declarations that are members of the namespace NS. */
3564 cp_namespace_decls (tree ns)
3566 return NAMESPACE_LEVEL (ns)->names;
3569 /* Combine prefer_type and namespaces_only into flags. */
3572 lookup_flags (int prefer_type, int namespaces_only)
3574 if (namespaces_only)
3575 return LOOKUP_PREFER_NAMESPACES;
3576 if (prefer_type > 1)
3577 return LOOKUP_PREFER_TYPES;
3578 if (prefer_type > 0)
3579 return LOOKUP_PREFER_BOTH;
3583 /* Given a lookup that returned VAL, use FLAGS to decide if we want to
3584 ignore it or not. Subroutine of lookup_name_real and
3585 lookup_type_scope. */
3588 qualify_lookup (tree val, int flags)
3590 if (val == NULL_TREE)
3592 if ((flags & LOOKUP_PREFER_NAMESPACES) && TREE_CODE (val) == NAMESPACE_DECL)
3594 if ((flags & LOOKUP_PREFER_TYPES)
3595 && (TREE_CODE (val) == TYPE_DECL || TREE_CODE (val) == TEMPLATE_DECL))
3597 if (flags & (LOOKUP_PREFER_NAMESPACES | LOOKUP_PREFER_TYPES))
3602 /* Given a lookup that returned VAL, decide if we want to ignore it or
3603 not based on DECL_ANTICIPATED. */
3606 hidden_name_p (tree val)
3609 && DECL_LANG_SPECIFIC (val)
3610 && DECL_ANTICIPATED (val))
3615 /* Remove any hidden friend functions from a possibly overloaded set
3619 remove_hidden_names (tree fns)
3624 if (TREE_CODE (fns) == FUNCTION_DECL && hidden_name_p (fns))
3626 else if (TREE_CODE (fns) == OVERLOAD)
3630 for (o = fns; o; o = OVL_NEXT (o))
3631 if (hidden_name_p (OVL_CURRENT (o)))
3637 for (o = fns; o; o = OVL_NEXT (o))
3638 if (!hidden_name_p (OVL_CURRENT (o)))
3639 n = build_overload (OVL_CURRENT (o), n);
3647 /* Select the right _DECL from multiple choices. */
3650 select_decl (const struct scope_binding *binding, int flags)
3653 val = binding->value;
3655 timevar_push (TV_NAME_LOOKUP);
3656 if (LOOKUP_NAMESPACES_ONLY (flags))
3658 /* We are not interested in types. */
3659 if (val && (TREE_CODE (val) == NAMESPACE_DECL
3660 || TREE_CODE (val) == TREE_LIST))
3661 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
3662 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
3665 /* If looking for a type, or if there is no non-type binding, select
3666 the value binding. */
3667 if (binding->type && (!val || (flags & LOOKUP_PREFER_TYPES)))
3668 val = binding->type;
3669 /* Don't return non-types if we really prefer types. */
3670 else if (val && LOOKUP_TYPES_ONLY (flags)
3671 && ! DECL_DECLARES_TYPE_P (val))
3674 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
3677 /* Unscoped lookup of a global: iterate over current namespaces,
3678 considering using-directives. */
3681 unqualified_namespace_lookup (tree name, int flags)
3683 tree initial = current_decl_namespace ();
3684 tree scope = initial;
3686 struct cp_binding_level *level;
3687 tree val = NULL_TREE;
3688 struct scope_binding binding = EMPTY_SCOPE_BINDING;
3690 timevar_push (TV_NAME_LOOKUP);
3692 for (; !val; scope = CP_DECL_CONTEXT (scope))
3695 cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (scope), name);
3700 && ((flags & LOOKUP_HIDDEN) || !hidden_name_p (b->value)))
3701 binding.value = b->value;
3702 binding.type = b->type;
3705 /* Add all _DECLs seen through local using-directives. */
3706 for (level = current_binding_level;
3707 level->kind != sk_namespace;
3708 level = level->level_chain)
3709 if (!lookup_using_namespace (name, &binding, level->using_directives,
3711 /* Give up because of error. */
3712 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
3714 /* Add all _DECLs seen through global using-directives. */
3715 /* XXX local and global using lists should work equally. */
3719 if (!lookup_using_namespace (name, &binding,
3720 DECL_NAMESPACE_USING (siter),
3722 /* Give up because of error. */
3723 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
3724 if (siter == scope) break;
3725 siter = CP_DECL_CONTEXT (siter);
3728 val = select_decl (&binding, flags);
3729 if (scope == global_namespace)
3732 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
3735 /* Look up NAME (an IDENTIFIER_NODE) in SCOPE (either a NAMESPACE_DECL
3736 or a class TYPE). If IS_TYPE_P is TRUE, then ignore non-type
3739 Returns a DECL (or OVERLOAD, or BASELINK) representing the
3740 declaration found. If no suitable declaration can be found,
3741 ERROR_MARK_NODE is returned. If COMPLAIN is true and SCOPE is
3742 neither a class-type nor a namespace a diagnostic is issued. */
3745 lookup_qualified_name (tree scope, tree name, bool is_type_p, bool complain)
3750 if (TREE_CODE (scope) == NAMESPACE_DECL)
3752 struct scope_binding binding = EMPTY_SCOPE_BINDING;
3754 flags |= LOOKUP_COMPLAIN;
3756 flags |= LOOKUP_PREFER_TYPES;
3757 if (qualified_lookup_using_namespace (name, scope, &binding, flags))
3758 t = select_decl (&binding, flags);
3760 else if (is_aggr_type (scope, complain))
3761 t = lookup_member (scope, name, 2, is_type_p);
3764 return error_mark_node;
3768 /* Subroutine of unqualified_namespace_lookup:
3769 Add the bindings of NAME in used namespaces to VAL.
3770 We are currently looking for names in namespace SCOPE, so we
3771 look through USINGS for using-directives of namespaces
3772 which have SCOPE as a common ancestor with the current scope.
3773 Returns false on errors. */
3776 lookup_using_namespace (tree name, struct scope_binding *val,
3777 tree usings, tree scope, int flags)
3780 timevar_push (TV_NAME_LOOKUP);
3781 /* Iterate over all used namespaces in current, searching for using
3782 directives of scope. */
3783 for (iter = usings; iter; iter = TREE_CHAIN (iter))
3784 if (TREE_VALUE (iter) == scope)
3786 tree used = ORIGINAL_NAMESPACE (TREE_PURPOSE (iter));
3788 cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (used), name);
3789 /* Resolve ambiguities. */
3791 ambiguous_decl (name, val, val1, flags);
3793 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val->value != error_mark_node);
3797 Accepts the NAME to lookup and its qualifying SCOPE.
3798 Returns the name/type pair found into the cxx_binding *RESULT,
3799 or false on error. */
3802 qualified_lookup_using_namespace (tree name, tree scope,
3803 struct scope_binding *result, int flags)
3805 /* Maintain a list of namespaces visited... */
3806 tree seen = NULL_TREE;
3807 /* ... and a list of namespace yet to see. */
3808 tree todo = NULL_TREE;
3809 tree todo_maybe = NULL_TREE;
3811 timevar_push (TV_NAME_LOOKUP);
3812 /* Look through namespace aliases. */
3813 scope = ORIGINAL_NAMESPACE (scope);
3814 while (scope && result->value != error_mark_node)
3816 cxx_binding *binding =
3817 cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (scope), name);
3818 seen = tree_cons (scope, NULL_TREE, seen);
3820 ambiguous_decl (name, result, binding, flags);
3822 /* Consider strong using directives always, and non-strong ones
3823 if we haven't found a binding yet. ??? Shouldn't we consider
3824 non-strong ones if the initial RESULT is non-NULL, but the
3825 binding in the given namespace is? */
3826 for (usings = DECL_NAMESPACE_USING (scope); usings;
3827 usings = TREE_CHAIN (usings))
3828 /* If this was a real directive, and we have not seen it. */
3829 if (!TREE_INDIRECT_USING (usings))
3831 /* Try to avoid queuing the same namespace more than once,
3832 the exception being when a namespace was already
3833 enqueued for todo_maybe and then a strong using is
3834 found for it. We could try to remove it from
3835 todo_maybe, but it's probably not worth the effort. */
3836 if (is_associated_namespace (scope, TREE_PURPOSE (usings))
3837 && !purpose_member (TREE_PURPOSE (usings), seen)
3838 && !purpose_member (TREE_PURPOSE (usings), todo))
3839 todo = tree_cons (TREE_PURPOSE (usings), NULL_TREE, todo);
3840 else if ((!result->value && !result->type)
3841 && !purpose_member (TREE_PURPOSE (usings), seen)
3842 && !purpose_member (TREE_PURPOSE (usings), todo)
3843 && !purpose_member (TREE_PURPOSE (usings), todo_maybe))
3844 todo_maybe = tree_cons (TREE_PURPOSE (usings), NULL_TREE,
3849 scope = TREE_PURPOSE (todo);
3850 todo = TREE_CHAIN (todo);
3853 && (!result->value && !result->type))
3855 scope = TREE_PURPOSE (todo_maybe);
3856 todo = TREE_CHAIN (todo_maybe);
3857 todo_maybe = NULL_TREE;
3860 scope = NULL_TREE; /* If there never was a todo list. */
3862 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, result->value != error_mark_node);
3865 /* Return the innermost non-namespace binding for NAME from a scope
3866 containing BINDING, or, if BINDING is NULL, the current scope. If
3867 CLASS_P is false, then class bindings are ignored. */
3870 outer_binding (tree name,
3871 cxx_binding *binding,
3876 cxx_scope *outer_scope;
3880 scope = binding->scope->level_chain;
3881 outer = binding->previous;
3885 scope = current_binding_level;
3886 outer = IDENTIFIER_BINDING (name);
3888 outer_scope = outer ? outer->scope : NULL;
3890 /* Because we create class bindings lazily, we might be missing a
3891 class binding for NAME. If there are any class binding levels
3892 between the LAST_BINDING_LEVEL and the scope in which OUTER was
3893 declared, we must lookup NAME in those class scopes. */
3895 while (scope && scope != outer_scope && scope->kind != sk_namespace)
3897 if (scope->kind == sk_class)
3899 cxx_binding *class_binding;
3901 class_binding = get_class_binding (name, scope);
3904 /* Thread this new class-scope binding onto the
3905 IDENTIFIER_BINDING list so that future lookups
3907 class_binding->previous = outer;
3909 binding->previous = class_binding;
3911 IDENTIFIER_BINDING (name) = class_binding;
3912 return class_binding;
3915 scope = scope->level_chain;
3921 /* Return the innermost block-scope or class-scope value binding for
3922 NAME, or NULL_TREE if there is no such binding. */
3925 innermost_non_namespace_value (tree name)
3927 cxx_binding *binding;
3928 binding = outer_binding (name, /*binding=*/NULL, /*class_p=*/true);
3929 return binding ? binding->value : NULL_TREE;
3932 /* Look up NAME in the current binding level and its superiors in the
3933 namespace of variables, functions and typedefs. Return a ..._DECL
3934 node of some kind representing its definition if there is only one
3935 such declaration, or return a TREE_LIST with all the overloaded
3936 definitions if there are many, or return 0 if it is undefined.
3937 Hidden name, either friend declaration or built-in function, are
3940 If PREFER_TYPE is > 0, we prefer TYPE_DECLs or namespaces.
3941 If PREFER_TYPE is > 1, we reject non-type decls (e.g. namespaces).
3942 Otherwise we prefer non-TYPE_DECLs.
3944 If NONCLASS is nonzero, bindings in class scopes are ignored. If
3945 BLOCK_P is false, bindings in block scopes are ignored. */
3948 lookup_name_real (tree name, int prefer_type, int nonclass, bool block_p,
3949 int namespaces_only, int flags)
3952 tree val = NULL_TREE;
3954 timevar_push (TV_NAME_LOOKUP);
3955 /* Conversion operators are handled specially because ordinary
3956 unqualified name lookup will not find template conversion
3958 if (IDENTIFIER_TYPENAME_P (name))
3960 struct cp_binding_level *level;
3962 for (level = current_binding_level;
3963 level && level->kind != sk_namespace;
3964 level = level->level_chain)
3969 /* A conversion operator can only be declared in a class
3971 if (level->kind != sk_class)
3974 /* Lookup the conversion operator in the class. */
3975 class_type = level->this_entity;
3976 operators = lookup_fnfields (class_type, name, /*protect=*/0);
3978 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, operators);
3981 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
3984 flags |= lookup_flags (prefer_type, namespaces_only);
3986 /* First, look in non-namespace scopes. */
3988 if (current_class_type == NULL_TREE)
3991 if (block_p || !nonclass)
3992 for (iter = outer_binding (name, NULL, !nonclass);
3994 iter = outer_binding (name, iter, !nonclass))
3998 /* Skip entities we don't want. */
3999 if (LOCAL_BINDING_P (iter) ? !block_p : nonclass)
4002 /* If this is the kind of thing we're looking for, we're done. */
4003 if (qualify_lookup (iter->value, flags))
4004 binding = iter->value;
4005 else if ((flags & LOOKUP_PREFER_TYPES)
4006 && qualify_lookup (iter->type, flags))
4007 binding = iter->type;
4009 binding = NULL_TREE;
4013 /* Only namespace-scope bindings can be hidden. */
4014 gcc_assert (!hidden_name_p (binding));
4020 /* Now lookup in namespace scopes. */
4022 val = unqualified_namespace_lookup (name, flags);
4024 /* If we have a single function from a using decl, pull it out. */
4025 if (val && TREE_CODE (val) == OVERLOAD && !really_overloaded_fn (val))
4026 val = OVL_FUNCTION (val);
4028 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
4032 lookup_name_nonclass (tree name)
4034 return lookup_name_real (name, 0, 1, /*block_p=*/true, 0, LOOKUP_COMPLAIN);
4038 lookup_function_nonclass (tree name, tree args, bool block_p)
4041 lookup_arg_dependent (name,
4042 lookup_name_real (name, 0, 1, block_p, 0,
4048 lookup_name (tree name)
4050 return lookup_name_real (name, 0, 0, /*block_p=*/true, 0, LOOKUP_COMPLAIN);
4054 lookup_name_prefer_type (tree name, int prefer_type)
4056 return lookup_name_real (name, prefer_type, 0, /*block_p=*/true,
4057 0, LOOKUP_COMPLAIN);
4060 /* Look up NAME for type used in elaborated name specifier in
4061 the scopes given by SCOPE. SCOPE can be either TS_CURRENT or
4062 TS_WITHIN_ENCLOSING_NON_CLASS. Although not implied by the
4063 name, more scopes are checked if cleanup or template parameter
4064 scope is encountered.
4066 Unlike lookup_name_real, we make sure that NAME is actually
4067 declared in the desired scope, not from inheritance, nor using
4068 directive. For using declaration, there is DR138 still waiting
4069 to be resolved. Hidden name coming from an earlier friend
4070 declaration is also returned.
4072 A TYPE_DECL best matching the NAME is returned. Catching error
4073 and issuing diagnostics are caller's responsibility. */
4076 lookup_type_scope (tree name, tag_scope scope)
4078 cxx_binding *iter = NULL;
4079 tree val = NULL_TREE;
4081 timevar_push (TV_NAME_LOOKUP);
4083 /* Look in non-namespace scope first. */
4084 if (current_binding_level->kind != sk_namespace)
4085 iter = outer_binding (name, NULL, /*class_p=*/ true);
4086 for (; iter; iter = outer_binding (name, iter, /*class_p=*/ true))
4088 /* Check if this is the kind of thing we're looking for.
4089 If SCOPE is TS_CURRENT, also make sure it doesn't come from
4090 base class. For ITER->VALUE, we can simply use
4091 INHERITED_VALUE_BINDING_P. For ITER->TYPE, we have to use
4094 We check ITER->TYPE before ITER->VALUE in order to handle
4095 typedef struct C {} C;
4098 if (qualify_lookup (iter->type, LOOKUP_PREFER_TYPES)
4099 && (scope != ts_current
4100 || LOCAL_BINDING_P (iter)
4101 || DECL_CONTEXT (iter->type) == iter->scope->this_entity))
4103 else if ((scope != ts_current
4104 || !INHERITED_VALUE_BINDING_P (iter))
4105 && qualify_lookup (iter->value, LOOKUP_PREFER_TYPES))
4112 /* Look in namespace scope. */
4115 iter = cxx_scope_find_binding_for_name
4116 (NAMESPACE_LEVEL (current_decl_namespace ()), name);
4120 /* If this is the kind of thing we're looking for, we're done. */
4121 if (qualify_lookup (iter->type, LOOKUP_PREFER_TYPES))
4123 else if (qualify_lookup (iter->value, LOOKUP_PREFER_TYPES))
4129 /* Type found, check if it is in the allowed scopes, ignoring cleanup
4130 and template parameter scopes. */
4133 struct cp_binding_level *b = current_binding_level;
4136 if (iter->scope == b)
4137 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
4139 if (b->kind == sk_cleanup || b->kind == sk_template_parms)
4141 else if (b->kind == sk_class
4142 && scope == ts_within_enclosing_non_class)
4149 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
4152 /* Similar to `lookup_name' but look only in the innermost non-class
4156 lookup_name_innermost_nonclass_level (tree name)
4158 struct cp_binding_level *b;
4161 timevar_push (TV_NAME_LOOKUP);
4162 b = innermost_nonclass_level ();
4164 if (b->kind == sk_namespace)
4166 t = IDENTIFIER_NAMESPACE_VALUE (name);
4168 /* extern "C" function() */
4169 if (t != NULL_TREE && TREE_CODE (t) == TREE_LIST)
4172 else if (IDENTIFIER_BINDING (name)
4173 && LOCAL_BINDING_P (IDENTIFIER_BINDING (name)))
4175 cxx_binding *binding;
4176 binding = IDENTIFIER_BINDING (name);
4179 if (binding->scope == b
4180 && !(TREE_CODE (binding->value) == VAR_DECL
4181 && DECL_DEAD_FOR_LOCAL (binding->value)))
4182 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, binding->value);
4184 if (b->kind == sk_cleanup)
4191 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
4194 /* Like lookup_name_innermost_nonclass_level, but for types. */
4197 lookup_type_current_level (tree name)
4201 timevar_push (TV_NAME_LOOKUP);
4202 gcc_assert (current_binding_level->kind != sk_namespace);
4204 if (REAL_IDENTIFIER_TYPE_VALUE (name) != NULL_TREE
4205 && REAL_IDENTIFIER_TYPE_VALUE (name) != global_type_node)
4207 struct cp_binding_level *b = current_binding_level;
4210 if (purpose_member (name, b->type_shadowed))
4211 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP,
4212 REAL_IDENTIFIER_TYPE_VALUE (name));
4213 if (b->kind == sk_cleanup)
4220 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
4223 /* [basic.lookup.koenig] */
4224 /* A nonzero return value in the functions below indicates an error. */
4235 static bool arg_assoc (struct arg_lookup*, tree);
4236 static bool arg_assoc_args (struct arg_lookup*, tree);
4237 static bool arg_assoc_type (struct arg_lookup*, tree);
4238 static bool add_function (struct arg_lookup *, tree);
4239 static bool arg_assoc_namespace (struct arg_lookup *, tree);
4240 static bool arg_assoc_class (struct arg_lookup *, tree);
4241 static bool arg_assoc_template_arg (struct arg_lookup*, tree);
4243 /* Add a function to the lookup structure.
4244 Returns true on error. */
4247 add_function (struct arg_lookup *k, tree fn)
4249 /* We used to check here to see if the function was already in the list,
4250 but that's O(n^2), which is just too expensive for function lookup.
4251 Now we deal with the occasional duplicate in joust. In doing this, we
4252 assume that the number of duplicates will be small compared to the
4253 total number of functions being compared, which should usually be the
4256 /* We must find only functions, or exactly one non-function. */
4259 else if (fn == k->functions)
4261 else if (is_overloaded_fn (k->functions) && is_overloaded_fn (fn))
4262 k->functions = build_overload (fn, k->functions);
4265 tree f1 = OVL_CURRENT (k->functions);
4267 if (is_overloaded_fn (f1))
4269 fn = f1; f1 = f2; f2 = fn;
4271 error ("%q+D is not a function,", f1);
4272 error (" conflict with %q+D", f2);
4273 error (" in call to %qD", k->name);
4280 /* Returns true iff CURRENT has declared itself to be an associated
4281 namespace of SCOPE via a strong using-directive (or transitive chain
4282 thereof). Both are namespaces. */
4285 is_associated_namespace (tree current, tree scope)
4287 tree seen = NULL_TREE;
4288 tree todo = NULL_TREE;
4292 if (scope == current)
4294 seen = tree_cons (scope, NULL_TREE, seen);
4295 for (t = DECL_NAMESPACE_ASSOCIATIONS (scope); t; t = TREE_CHAIN (t))
4296 if (!purpose_member (TREE_PURPOSE (t), seen))
4297 todo = tree_cons (TREE_PURPOSE (t), NULL_TREE, todo);
4300 scope = TREE_PURPOSE (todo);
4301 todo = TREE_CHAIN (todo);
4308 /* Return whether FN is a friend of an associated class of ARG. */
4311 friend_of_associated_class_p (tree arg, tree fn)
4317 else if (type_unknown_p (arg))
4320 type = TREE_TYPE (arg);
4322 /* If TYPE is a class, the class itself and all base classes are
4323 associated classes. */
4324 if (CLASS_TYPE_P (type))
4326 if (is_friend (type, fn))
4329 if (TYPE_BINFO (type))
4331 tree binfo, base_binfo;
4334 for (binfo = TYPE_BINFO (type), i = 0;
4335 BINFO_BASE_ITERATE (binfo, i, base_binfo);
4337 if (is_friend (BINFO_TYPE (base_binfo), fn))
4342 /* If TYPE is a class member, the class of which it is a member is
4343 an associated class. */
4344 if ((CLASS_TYPE_P (type)
4345 || TREE_CODE (type) == UNION_TYPE
4346 || TREE_CODE (type) == ENUMERAL_TYPE)
4347 && TYPE_CONTEXT (type)
4348 && CLASS_TYPE_P (TYPE_CONTEXT (type))
4349 && is_friend (TYPE_CONTEXT (type), fn))
4355 /* Add functions of a namespace to the lookup structure.
4356 Returns true on error. */
4359 arg_assoc_namespace (struct arg_lookup *k, tree scope)
4363 if (purpose_member (scope, k->namespaces))
4365 k->namespaces = tree_cons (scope, NULL_TREE, k->namespaces);
4367 /* Check out our super-users. */
4368 for (value = DECL_NAMESPACE_ASSOCIATIONS (scope); value;
4369 value = TREE_CHAIN (value))
4370 if (arg_assoc_namespace (k, TREE_PURPOSE (value)))
4373 value = namespace_binding (k->name, scope);
4377 for (; value; value = OVL_NEXT (value))
4379 /* We don't want to find arbitrary hidden functions via argument
4380 dependent lookup. We only want to find friends of associated
4382 if (hidden_name_p (OVL_CURRENT (value)))
4386 for (args = k->args; args; args = TREE_CHAIN (args))
4387 if (friend_of_associated_class_p (TREE_VALUE (args),
4388 OVL_CURRENT (value)))
4394 if (add_function (k, OVL_CURRENT (value)))
4401 /* Adds everything associated with a template argument to the lookup
4402 structure. Returns true on error. */
4405 arg_assoc_template_arg (struct arg_lookup *k, tree arg)
4407 /* [basic.lookup.koenig]
4409 If T is a template-id, its associated namespaces and classes are
4410 ... the namespaces and classes associated with the types of the
4411 template arguments provided for template type parameters
4412 (excluding template template parameters); the namespaces in which
4413 any template template arguments are defined; and the classes in
4414 which any member templates used as template template arguments
4415 are defined. [Note: non-type template arguments do not
4416 contribute to the set of associated namespaces. ] */
4418 /* Consider first template template arguments. */
4419 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
4420 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
4422 else if (TREE_CODE (arg) == TEMPLATE_DECL)
4424 tree ctx = CP_DECL_CONTEXT (arg);
4426 /* It's not a member template. */
4427 if (TREE_CODE (ctx) == NAMESPACE_DECL)
4428 return arg_assoc_namespace (k, ctx);
4429 /* Otherwise, it must be member template. */
4431 return arg_assoc_class (k, ctx);
4433 /* It's not a template template argument, but it is a type template
4435 else if (TYPE_P (arg))
4436 return arg_assoc_type (k, arg);
4437 /* It's a non-type template argument. */
4442 /* Adds everything associated with class to the lookup structure.
4443 Returns true on error. */
4446 arg_assoc_class (struct arg_lookup *k, tree type)
4448 tree list, friends, context;
4451 /* Backend build structures, such as __builtin_va_list, aren't
4452 affected by all this. */
4453 if (!CLASS_TYPE_P (type))
4456 if (purpose_member (type, k->classes))
4458 k->classes = tree_cons (type, NULL_TREE, k->classes);
4460 context = decl_namespace_context (type);
4461 if (arg_assoc_namespace (k, context))
4464 if (TYPE_BINFO (type))
4466 /* Process baseclasses. */
4467 tree binfo, base_binfo;
4469 for (binfo = TYPE_BINFO (type), i = 0;
4470 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
4471 if (arg_assoc_class (k, BINFO_TYPE (base_binfo)))
4475 /* Process friends. */
4476 for (list = DECL_FRIENDLIST (TYPE_MAIN_DECL (type)); list;
4477 list = TREE_CHAIN (list))
4478 if (k->name == FRIEND_NAME (list))
4479 for (friends = FRIEND_DECLS (list); friends;
4480 friends = TREE_CHAIN (friends))
4482 tree fn = TREE_VALUE (friends);
4484 /* Only interested in global functions with potentially hidden
4485 (i.e. unqualified) declarations. */
4486 if (CP_DECL_CONTEXT (fn) != context)
4488 /* Template specializations are never found by name lookup.
4489 (Templates themselves can be found, but not template
4490 specializations.) */
4491 if (TREE_CODE (fn) == FUNCTION_DECL && DECL_USE_TEMPLATE (fn))
4493 if (add_function (k, fn))
4497 /* Process template arguments. */
4498 if (CLASSTYPE_TEMPLATE_INFO (type)
4499 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
4501 list = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
4502 for (i = 0; i < TREE_VEC_LENGTH (list); ++i)
4503 arg_assoc_template_arg (k, TREE_VEC_ELT (list, i));
4509 /* Adds everything associated with a given type.
4510 Returns 1 on error. */
4513 arg_assoc_type (struct arg_lookup *k, tree type)
4515 /* As we do not get the type of non-type dependent expressions
4516 right, we can end up with such things without a type. */
4520 if (TYPE_PTRMEM_P (type))
4522 /* Pointer to member: associate class type and value type. */
4523 if (arg_assoc_type (k, TYPE_PTRMEM_CLASS_TYPE (type)))
4525 return arg_assoc_type (k, TYPE_PTRMEM_POINTED_TO_TYPE (type));
4527 else switch (TREE_CODE (type))
4539 if (TYPE_PTRMEMFUNC_P (type))
4540 return arg_assoc_type (k, TYPE_PTRMEMFUNC_FN_TYPE (type));
4541 return arg_assoc_class (k, type);
4543 case REFERENCE_TYPE:
4545 return arg_assoc_type (k, TREE_TYPE (type));
4548 return arg_assoc_namespace (k, decl_namespace_context (type));
4550 /* The basetype is referenced in the first arg type, so just
4553 /* Associate the parameter types. */
4554 if (arg_assoc_args (k, TYPE_ARG_TYPES (type)))
4556 /* Associate the return type. */
4557 return arg_assoc_type (k, TREE_TYPE (type));
4558 case TEMPLATE_TYPE_PARM:
4559 case BOUND_TEMPLATE_TEMPLATE_PARM:
4564 gcc_assert (type == unknown_type_node);
4572 /* Adds everything associated with arguments. Returns true on error. */
4575 arg_assoc_args (struct arg_lookup *k, tree args)
4577 for (; args; args = TREE_CHAIN (args))
4578 if (arg_assoc (k, TREE_VALUE (args)))
4583 /* Adds everything associated with a given tree_node. Returns 1 on error. */
4586 arg_assoc (struct arg_lookup *k, tree n)
4588 if (n == error_mark_node)
4592 return arg_assoc_type (k, n);
4594 if (! type_unknown_p (n))
4595 return arg_assoc_type (k, TREE_TYPE (n));
4597 if (TREE_CODE (n) == ADDR_EXPR)
4598 n = TREE_OPERAND (n, 0);
4599 if (TREE_CODE (n) == COMPONENT_REF)
4600 n = TREE_OPERAND (n, 1);
4601 if (TREE_CODE (n) == OFFSET_REF)
4602 n = TREE_OPERAND (n, 1);
4603 while (TREE_CODE (n) == TREE_LIST)
4605 if (TREE_CODE (n) == BASELINK)
4606 n = BASELINK_FUNCTIONS (n);
4608 if (TREE_CODE (n) == FUNCTION_DECL)
4609 return arg_assoc_type (k, TREE_TYPE (n));
4610 if (TREE_CODE (n) == TEMPLATE_ID_EXPR)
4612 /* [basic.lookup.koenig]
4614 If T is a template-id, its associated namespaces and classes
4615 are the namespace in which the template is defined; for
4616 member templates, the member template's class... */
4617 tree template = TREE_OPERAND (n, 0);
4618 tree args = TREE_OPERAND (n, 1);
4622 if (TREE_CODE (template) == COMPONENT_REF)
4623 template = TREE_OPERAND (template, 1);
4625 /* First, the template. There may actually be more than one if
4626 this is an overloaded function template. But, in that case,
4627 we only need the first; all the functions will be in the same
4629 template = OVL_CURRENT (template);
4631 ctx = CP_DECL_CONTEXT (template);
4633 if (TREE_CODE (ctx) == NAMESPACE_DECL)
4635 if (arg_assoc_namespace (k, ctx) == 1)
4638 /* It must be a member template. */
4639 else if (arg_assoc_class (k, ctx) == 1)
4642 /* Now the arguments. */
4644 for (ix = TREE_VEC_LENGTH (args); ix--;)
4645 if (arg_assoc_template_arg (k, TREE_VEC_ELT (args, ix)) == 1)
4648 else if (TREE_CODE (n) == OVERLOAD)
4650 for (; n; n = OVL_CHAIN (n))
4651 if (arg_assoc_type (k, TREE_TYPE (OVL_FUNCTION (n))))
4658 /* Performs Koenig lookup depending on arguments, where fns
4659 are the functions found in normal lookup. */
4662 lookup_arg_dependent (tree name, tree fns, tree args)
4664 struct arg_lookup k;
4666 timevar_push (TV_NAME_LOOKUP);
4668 /* Remove any hidden friend functions from the list of functions
4669 found so far. They will be added back by arg_assoc_class as
4671 fns = remove_hidden_names (fns);
4676 k.classes = NULL_TREE;
4678 /* We previously performed an optimization here by setting
4679 NAMESPACES to the current namespace when it was safe. However, DR
4680 164 says that namespaces that were already searched in the first
4681 stage of template processing are searched again (potentially
4682 picking up later definitions) in the second stage. */
4683 k.namespaces = NULL_TREE;
4685 arg_assoc_args (&k, args);
4690 && TREE_CODE (fns) != VAR_DECL
4691 && !is_overloaded_fn (fns))
4693 error ("argument dependent lookup finds %q+D", fns);
4694 error (" in call to %qD", name);
4695 fns = error_mark_node;
4698 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, fns);
4701 /* Add namespace to using_directives. Return NULL_TREE if nothing was
4702 changed (i.e. there was already a directive), or the fresh
4703 TREE_LIST otherwise. */
4706 push_using_directive (tree used)
4708 tree ud = current_binding_level->using_directives;
4709 tree iter, ancestor;
4711 timevar_push (TV_NAME_LOOKUP);
4712 /* Check if we already have this. */
4713 if (purpose_member (used, ud) != NULL_TREE)
4714 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
4716 ancestor = namespace_ancestor (current_decl_namespace (), used);
4717 ud = current_binding_level->using_directives;
4718 ud = tree_cons (used, ancestor, ud);
4719 current_binding_level->using_directives = ud;
4721 /* Recursively add all namespaces used. */
4722 for (iter = DECL_NAMESPACE_USING (used); iter; iter = TREE_CHAIN (iter))
4723 push_using_directive (TREE_PURPOSE (iter));
4725 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, ud);
4728 /* The type TYPE is being declared. If it is a class template, or a
4729 specialization of a class template, do any processing required and
4730 perform error-checking. If IS_FRIEND is nonzero, this TYPE is
4731 being declared a friend. B is the binding level at which this TYPE
4734 Returns the TYPE_DECL for TYPE, which may have been altered by this
4738 maybe_process_template_type_declaration (tree type, int is_friend,
4741 tree decl = TYPE_NAME (type);
4743 if (processing_template_parmlist)
4744 /* You can't declare a new template type in a template parameter
4745 list. But, you can declare a non-template type:
4747 template <class A*> struct S;
4749 is a forward-declaration of `A'. */
4751 else if (b->kind == sk_namespace
4752 && current_binding_level->kind != sk_namespace)
4753 /* If this new type is being injected into a containing scope,
4754 then it's not a template type. */
4758 gcc_assert (IS_AGGR_TYPE (type) || TREE_CODE (type) == ENUMERAL_TYPE);
4760 if (processing_template_decl)
4762 /* This may change after the call to
4763 push_template_decl_real, but we want the original value. */
4764 tree name = DECL_NAME (decl);
4766 decl = push_template_decl_real (decl, is_friend);
4767 /* If the current binding level is the binding level for the
4768 template parameters (see the comment in
4769 begin_template_parm_list) and the enclosing level is a class
4770 scope, and we're not looking at a friend, push the
4771 declaration of the member class into the class scope. In the
4772 friend case, push_template_decl will already have put the
4773 friend into global scope, if appropriate. */
4774 if (TREE_CODE (type) != ENUMERAL_TYPE
4775 && !is_friend && b->kind == sk_template_parms
4776 && b->level_chain->kind == sk_class)
4778 finish_member_declaration (CLASSTYPE_TI_TEMPLATE (type));
4780 if (!COMPLETE_TYPE_P (current_class_type))
4782 maybe_add_class_template_decl_list (current_class_type,
4783 type, /*friend_p=*/0);
4784 /* Put this UTD in the table of UTDs for the class. */
4785 if (CLASSTYPE_NESTED_UTDS (current_class_type) == NULL)
4786 CLASSTYPE_NESTED_UTDS (current_class_type) =
4787 binding_table_new (SCOPE_DEFAULT_HT_SIZE);
4789 binding_table_insert
4790 (CLASSTYPE_NESTED_UTDS (current_class_type), name, type);
4799 /* Push a tag name NAME for struct/class/union/enum type TYPE. In case
4800 that the NAME is a class template, the tag is processed but not pushed.
4802 The pushed scope depend on the SCOPE parameter:
4803 - When SCOPE is TS_CURRENT, put it into the inner-most non-sk_cleanup
4805 - When SCOPE is TS_GLOBAL, put it in the inner-most non-class and
4806 non-template-parameter scope. This case is needed for forward
4808 - When SCOPE is TS_WITHIN_ENCLOSING_NON_CLASS, this is similar to
4809 TS_GLOBAL case except that names within template-parameter scopes
4810 are not pushed at all.
4812 Returns TYPE upon success and ERROR_MARK_NODE otherwise. */
4815 pushtag (tree name, tree type, tag_scope scope)
4817 struct cp_binding_level *b;
4820 timevar_push (TV_NAME_LOOKUP);
4821 b = current_binding_level;
4822 while (/* Cleanup scopes are not scopes from the point of view of
4824 b->kind == sk_cleanup
4825 /* Neither are the scopes used to hold template parameters
4826 for an explicit specialization. For an ordinary template
4827 declaration, these scopes are not scopes from the point of
4828 view of the language. */
4829 || (b->kind == sk_template_parms
4830 && (b->explicit_spec_p || scope == ts_global))
4831 || (b->kind == sk_class
4832 && (scope != ts_current
4833 /* We may be defining a new type in the initializer
4834 of a static member variable. We allow this when
4835 not pedantic, and it is particularly useful for
4836 type punning via an anonymous union. */
4837 || COMPLETE_TYPE_P (b->this_entity))))
4840 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
4842 /* Do C++ gratuitous typedefing. */
4843 if (IDENTIFIER_TYPE_VALUE (name) != type)
4847 tree context = TYPE_CONTEXT (type);
4851 tree cs = current_scope ();
4853 if (scope == ts_current)
4855 else if (cs != NULL_TREE && TYPE_P (cs))
4856 /* When declaring a friend class of a local class, we want
4857 to inject the newly named class into the scope
4858 containing the local class, not the namespace
4860 context = decl_function_context (get_type_decl (cs));
4863 context = current_namespace;
4865 if (b->kind == sk_class
4866 || (b->kind == sk_template_parms
4867 && b->level_chain->kind == sk_class))
4870 if (current_lang_name == lang_name_java)
4871 TYPE_FOR_JAVA (type) = 1;
4873 tdef = create_implicit_typedef (name, type);
4874 DECL_CONTEXT (tdef) = FROB_CONTEXT (context);
4875 if (scope == ts_within_enclosing_non_class)
4877 /* This is a friend. Make this TYPE_DECL node hidden from
4878 ordinary name lookup. Its corresponding TEMPLATE_DECL
4879 will be marked in push_template_decl_real. */
4880 retrofit_lang_decl (tdef);
4881 DECL_ANTICIPATED (tdef) = 1;
4882 DECL_FRIEND_P (tdef) = 1;
4885 decl = maybe_process_template_type_declaration
4886 (type, scope == ts_within_enclosing_non_class, b);
4887 if (decl == error_mark_node)
4888 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
4891 set_identifier_type_value_with_scope (name, tdef, b);
4893 if (b->kind == sk_class)
4895 if (!PROCESSING_REAL_TEMPLATE_DECL_P ())
4896 /* Put this TYPE_DECL on the TYPE_FIELDS list for the
4897 class. But if it's a member template class, we want
4898 the TEMPLATE_DECL, not the TYPE_DECL, so this is done
4900 finish_member_declaration (decl);
4902 pushdecl_class_level (decl);
4904 else if (b->kind != sk_template_parms)
4906 decl = pushdecl_with_scope (decl, b, /*is_friend=*/false);
4907 if (decl == error_mark_node)
4908 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
4911 TYPE_CONTEXT (type) = DECL_CONTEXT (decl);
4913 /* If this is a local class, keep track of it. We need this
4914 information for name-mangling, and so that it is possible to
4915 find all function definitions in a translation unit in a
4916 convenient way. (It's otherwise tricky to find a member
4917 function definition it's only pointed to from within a local
4919 if (TYPE_CONTEXT (type)
4920 && TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL)
4921 VEC_safe_push (tree, gc, local_classes, type);
4923 if (b->kind == sk_class
4924 && !COMPLETE_TYPE_P (current_class_type))
4926 maybe_add_class_template_decl_list (current_class_type,
4927 type, /*friend_p=*/0);
4929 if (CLASSTYPE_NESTED_UTDS (current_class_type) == NULL)
4930 CLASSTYPE_NESTED_UTDS (current_class_type)
4931 = binding_table_new (SCOPE_DEFAULT_HT_SIZE);
4933 binding_table_insert
4934 (CLASSTYPE_NESTED_UTDS (current_class_type), name, type);
4937 decl = TYPE_NAME (type);
4938 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
4939 TYPE_STUB_DECL (type) = decl;
4941 /* Set type visibility now if this is a forward declaration. */
4942 TREE_PUBLIC (decl) = 1;
4943 determine_visibility (decl);
4945 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, type);
4948 /* Subroutines for reverting temporarily to top-level for instantiation
4949 of templates and such. We actually need to clear out the class- and
4950 local-value slots of all identifiers, so that only the global values
4951 are at all visible. Simply setting current_binding_level to the global
4952 scope isn't enough, because more binding levels may be pushed. */
4953 struct saved_scope *scope_chain;
4955 /* If ID has not already been marked, add an appropriate binding to
4959 store_binding (tree id, VEC(cxx_saved_binding,gc) **old_bindings)
4961 cxx_saved_binding *saved;
4963 if (!id || !IDENTIFIER_BINDING (id))
4966 if (IDENTIFIER_MARKED (id))
4969 IDENTIFIER_MARKED (id) = 1;
4971 saved = VEC_safe_push (cxx_saved_binding, gc, *old_bindings, NULL);
4972 saved->identifier = id;
4973 saved->binding = IDENTIFIER_BINDING (id);
4974 saved->real_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
4975 IDENTIFIER_BINDING (id) = NULL;
4979 store_bindings (tree names, VEC(cxx_saved_binding,gc) **old_bindings)
4983 timevar_push (TV_NAME_LOOKUP);
4984 for (t = names; t; t = TREE_CHAIN (t))
4988 if (TREE_CODE (t) == TREE_LIST)
4989 id = TREE_PURPOSE (t);
4993 store_binding (id, old_bindings);
4995 timevar_pop (TV_NAME_LOOKUP);
4998 /* Like store_bindings, but NAMES is a vector of cp_class_binding
4999 objects, rather than a TREE_LIST. */
5002 store_class_bindings (VEC(cp_class_binding,gc) *names,
5003 VEC(cxx_saved_binding,gc) **old_bindings)
5006 cp_class_binding *cb;
5008 timevar_push (TV_NAME_LOOKUP);
5009 for (i = 0; VEC_iterate(cp_class_binding, names, i, cb); ++i)
5010 store_binding (cb->identifier, old_bindings);
5011 timevar_pop (TV_NAME_LOOKUP);
5015 push_to_top_level (void)
5017 struct saved_scope *s;
5018 struct cp_binding_level *b;
5019 cxx_saved_binding *sb;
5023 timevar_push (TV_NAME_LOOKUP);
5024 s = GGC_CNEW (struct saved_scope);
5026 b = scope_chain ? current_binding_level : 0;
5028 /* If we're in the middle of some function, save our state. */
5032 push_function_context_to (NULL_TREE);
5037 if (scope_chain && previous_class_level)
5038 store_class_bindings (previous_class_level->class_shadowed,
5041 /* Have to include the global scope, because class-scope decls
5042 aren't listed anywhere useful. */
5043 for (; b; b = b->level_chain)
5047 /* Template IDs are inserted into the global level. If they were
5048 inserted into namespace level, finish_file wouldn't find them
5049 when doing pending instantiations. Therefore, don't stop at
5050 namespace level, but continue until :: . */
5051 if (global_scope_p (b))
5054 store_bindings (b->names, &s->old_bindings);
5055 /* We also need to check class_shadowed to save class-level type
5056 bindings, since pushclass doesn't fill in b->names. */
5057 if (b->kind == sk_class)
5058 store_class_bindings (b->class_shadowed, &s->old_bindings);
5060 /* Unwind type-value slots back to top level. */
5061 for (t = b->type_shadowed; t; t = TREE_CHAIN (t))
5062 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (t), TREE_VALUE (t));
5065 for (i = 0; VEC_iterate (cxx_saved_binding, s->old_bindings, i, sb); ++i)
5066 IDENTIFIER_MARKED (sb->identifier) = 0;
5068 s->prev = scope_chain;
5070 s->need_pop_function_context = need_pop;
5071 s->function_decl = current_function_decl;
5072 s->skip_evaluation = skip_evaluation;
5075 current_function_decl = NULL_TREE;
5076 current_lang_base = VEC_alloc (tree, gc, 10);
5077 current_lang_name = lang_name_cplusplus;
5078 current_namespace = global_namespace;
5079 push_class_stack ();
5080 skip_evaluation = 0;
5081 timevar_pop (TV_NAME_LOOKUP);
5085 pop_from_top_level (void)
5087 struct saved_scope *s = scope_chain;
5088 cxx_saved_binding *saved;
5091 timevar_push (TV_NAME_LOOKUP);
5092 /* Clear out class-level bindings cache. */
5093 if (previous_class_level)
5094 invalidate_class_lookup_cache ();
5097 current_lang_base = 0;
5099 scope_chain = s->prev;
5100 for (i = 0; VEC_iterate (cxx_saved_binding, s->old_bindings, i, saved); ++i)
5102 tree id = saved->identifier;
5104 IDENTIFIER_BINDING (id) = saved->binding;
5105 SET_IDENTIFIER_TYPE_VALUE (id, saved->real_type_value);
5108 /* If we were in the middle of compiling a function, restore our
5110 if (s->need_pop_function_context)
5111 pop_function_context_from (NULL_TREE);
5112 current_function_decl = s->function_decl;
5113 skip_evaluation = s->skip_evaluation;
5114 timevar_pop (TV_NAME_LOOKUP);
5117 /* Pop off extraneous binding levels left over due to syntax errors.
5119 We don't pop past namespaces, as they might be valid. */
5122 pop_everything (void)
5124 if (ENABLE_SCOPE_CHECKING)
5125 verbatim ("XXX entering pop_everything ()\n");
5126 while (!toplevel_bindings_p ())
5128 if (current_binding_level->kind == sk_class)
5129 pop_nested_class ();
5133 if (ENABLE_SCOPE_CHECKING)
5134 verbatim ("XXX leaving pop_everything ()\n");
5137 /* Emit debugging information for using declarations and directives.
5138 If input tree is overloaded fn then emit debug info for all
5142 cp_emit_debug_info_for_using (tree t, tree context)
5144 /* Don't try to emit any debug information if we have errors. */
5145 if (sorrycount || errorcount)
5148 /* Ignore this FUNCTION_DECL if it refers to a builtin declaration
5149 of a builtin function. */
5150 if (TREE_CODE (t) == FUNCTION_DECL
5151 && DECL_EXTERNAL (t)
5152 && DECL_BUILT_IN (t))
5155 /* Do not supply context to imported_module_or_decl, if
5156 it is a global namespace. */
5157 if (context == global_namespace)
5158 context = NULL_TREE;
5161 t = BASELINK_FUNCTIONS (t);
5163 /* FIXME: Handle TEMPLATE_DECLs. */
5164 for (t = OVL_CURRENT (t); t; t = OVL_NEXT (t))
5165 if (TREE_CODE (t) != TEMPLATE_DECL)
5166 (*debug_hooks->imported_module_or_decl) (t, context);
5169 #include "gt-cp-name-lookup.h"