1 /* Definitions for C++ name lookup routines.
2 Copyright (C) 2003, 2004 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, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
24 #include "coretypes.h"
29 #include "name-lookup.h"
32 #include "diagnostic.h"
35 /* The bindings for a particular name in a particular scope. */
37 struct scope_binding {
41 #define EMPTY_SCOPE_BINDING { NULL_TREE, NULL_TREE }
43 static cxx_scope *innermost_nonclass_level (void);
44 static tree select_decl (const struct scope_binding *, int);
45 static cxx_binding *binding_for_name (cxx_scope *, tree);
46 static tree lookup_name_current_level (tree);
47 static tree push_overloaded_decl (tree, int);
48 static bool lookup_using_namespace (tree, struct scope_binding *, tree,
50 static bool qualified_lookup_using_namespace (tree, tree,
51 struct scope_binding *, int);
52 static tree lookup_type_current_level (tree);
53 static tree push_using_directive (tree);
54 static void cp_emit_debug_info_for_using (tree, 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;
65 /* Compute the chain index of a binding_entry given the HASH value of its
66 name and the total COUNT of chains. COUNT is assumed to be a power
69 #define ENTRY_INDEX(HASH, COUNT) (((HASH) >> 3) & ((COUNT) - 1))
71 /* A free list of "binding_entry"s awaiting for re-use. */
73 static GTY((deletable)) binding_entry free_binding_entry = NULL;
75 /* Create a binding_entry object for (NAME, TYPE). */
77 static inline binding_entry
78 binding_entry_make (tree name, tree type)
82 if (free_binding_entry)
84 entry = free_binding_entry;
85 free_binding_entry = entry->chain;
88 entry = ggc_alloc (sizeof (struct binding_entry_s));
97 /* Put ENTRY back on the free list. */
100 binding_entry_free (binding_entry entry)
104 entry->chain = free_binding_entry;
105 free_binding_entry = entry;
108 /* The datatype used to implement the mapping from names to types at
110 struct binding_table_s GTY(())
112 /* Array of chains of "binding_entry"s */
113 binding_entry * GTY((length ("%h.chain_count"))) chain;
115 /* The number of chains in this table. This is the length of the
116 the member "chain" considered as an array. */
119 /* Number of "binding_entry"s in this table. */
123 /* Construct TABLE with an initial CHAIN_COUNT. */
126 binding_table_construct (binding_table table, size_t chain_count)
128 table->chain_count = chain_count;
129 table->entry_count = 0;
130 table->chain = ggc_alloc_cleared
131 (table->chain_count * sizeof (binding_entry));
134 /* Make TABLE's entries ready for reuse. */
137 binding_table_free (binding_table table)
145 for (i = 0, count = table->chain_count; i < count; ++i)
147 binding_entry temp = table->chain[i];
150 binding_entry entry = temp;
152 binding_entry_free (entry);
154 table->chain[i] = NULL;
156 table->entry_count = 0;
159 /* Allocate a table with CHAIN_COUNT, assumed to be a power of two. */
161 static inline binding_table
162 binding_table_new (size_t chain_count)
164 binding_table table = ggc_alloc (sizeof (struct binding_table_s));
166 binding_table_construct (table, chain_count);
170 /* Expand TABLE to twice its current chain_count. */
173 binding_table_expand (binding_table table)
175 const size_t old_chain_count = table->chain_count;
176 const size_t old_entry_count = table->entry_count;
177 const size_t new_chain_count = 2 * old_chain_count;
178 binding_entry *old_chains = table->chain;
181 binding_table_construct (table, new_chain_count);
182 for (i = 0; i < old_chain_count; ++i)
184 binding_entry entry = old_chains[i];
185 for (; entry != NULL; entry = old_chains[i])
187 const unsigned int hash = IDENTIFIER_HASH_VALUE (entry->name);
188 const size_t j = ENTRY_INDEX (hash, new_chain_count);
190 old_chains[i] = entry->chain;
191 entry->chain = table->chain[j];
192 table->chain[j] = entry;
195 table->entry_count = old_entry_count;
198 /* Insert a binding for NAME to TYPE into TABLE. */
201 binding_table_insert (binding_table table, tree name, tree type)
203 const unsigned int hash = IDENTIFIER_HASH_VALUE (name);
204 const size_t i = ENTRY_INDEX (hash, table->chain_count);
205 binding_entry entry = binding_entry_make (name, type);
207 entry->chain = table->chain[i];
208 table->chain[i] = entry;
209 ++table->entry_count;
211 if (3 * table->chain_count < 5 * table->entry_count)
212 binding_table_expand (table);
215 /* Return the binding_entry, if any, that maps NAME. */
218 binding_table_find (binding_table table, tree name)
220 const unsigned int hash = IDENTIFIER_HASH_VALUE (name);
221 binding_entry entry = table->chain[ENTRY_INDEX (hash, table->chain_count)];
223 while (entry != NULL && entry->name != name)
224 entry = entry->chain;
229 /* Return the binding_entry, if any, that maps NAME to an anonymous type. */
232 binding_table_find_anon_type (binding_table table, tree name)
234 const unsigned int hash = IDENTIFIER_HASH_VALUE (name);
235 binding_entry entry = table->chain[ENTRY_INDEX (hash, table->chain_count)];
237 while (entry != NULL && TYPE_IDENTIFIER (entry->type) != name)
238 entry = entry->chain;
240 return entry ? entry->type : NULL;
243 /* Return the binding_entry, if any, that has TYPE as target. If NAME
244 is non-null, then set the domain and rehash that entry. */
247 binding_table_reverse_maybe_remap (binding_table table, tree type, tree name)
249 const size_t chain_count = table->chain_count;
250 binding_entry entry = NULL;
251 binding_entry *p = NULL;
254 for (i = 0; i < chain_count && entry == NULL; ++i)
256 p = &table->chain[i];
257 while (*p != NULL && entry == NULL)
258 if ((*p)->type == type)
264 if (entry != NULL && name != NULL && entry->name != name)
266 /* Remove the bucket from the previous chain. */
269 /* Remap the name type to type. */
270 i = ENTRY_INDEX (IDENTIFIER_HASH_VALUE (name), chain_count);
271 entry->chain = table->chain[i];
273 table->chain[i] = entry;
279 /* Remove from TABLE all entries that map to anonymous enums or
283 binding_table_remove_anonymous_types (binding_table table)
285 const size_t chain_count = table->chain_count;
288 for (i = 0; i < chain_count; ++i)
290 binding_entry *p = &table->chain[i];
293 if (ANON_AGGRNAME_P ((*p)->name))
295 binding_entry e = *p;
297 --table->entry_count;
298 binding_entry_free (e);
305 /* Apply PROC -- with DATA -- to all entries in TABLE. */
308 binding_table_foreach (binding_table table, bt_foreach_proc proc, void *data)
310 const size_t chain_count = table->chain_count;
313 for (i = 0; i < chain_count; ++i)
315 binding_entry entry = table->chain[i];
316 for (; entry != NULL; entry = entry->chain)
321 #ifndef ENABLE_SCOPE_CHECKING
322 # define ENABLE_SCOPE_CHECKING 0
324 # define ENABLE_SCOPE_CHECKING 1
327 /* A free list of "cxx_binding"s, connected by their PREVIOUS. */
329 static GTY((deletable)) cxx_binding *free_bindings;
331 /* Initialize VALUE and TYPE field for BINDING, and set the PREVIOUS
335 cxx_binding_init (cxx_binding *binding, tree value, tree type)
337 binding->value = value;
338 binding->type = type;
339 binding->previous = NULL;
342 /* (GC)-allocate a binding object with VALUE and TYPE member initialized. */
345 cxx_binding_make (tree value, tree type)
347 cxx_binding *binding;
350 binding = free_bindings;
351 free_bindings = binding->previous;
354 binding = ggc_alloc (sizeof (cxx_binding));
356 cxx_binding_init (binding, value, type);
361 /* Put BINDING back on the free list. */
364 cxx_binding_free (cxx_binding *binding)
366 binding->scope = NULL;
367 binding->previous = free_bindings;
368 free_bindings = binding;
371 /* Make DECL the innermost binding for ID. The LEVEL is the binding
372 level at which this declaration is being bound. */
375 push_binding (tree id, tree decl, cxx_scope* level)
377 cxx_binding *binding;
379 if (level != class_binding_level)
380 binding = cxx_binding_make (decl, NULL_TREE);
383 cp_class_binding *cb;
385 if (VEC_reserve (cp_class_binding, level->class_shadowed, -1))
387 /* Fixup the current bindings, as they might have moved. */
391 (cb = VEC_iterate (cp_class_binding, level->class_shadowed, i));
393 IDENTIFIER_BINDING (cb->identifier) = &cb->base;
396 cb = VEC_quick_push (cp_class_binding, level->class_shadowed, NULL);
399 cxx_binding_init (binding, decl, NULL_TREE);
402 /* Now, fill in the binding information. */
403 binding->previous = IDENTIFIER_BINDING (id);
404 binding->scope = level;
405 INHERITED_VALUE_BINDING_P (binding) = 0;
406 LOCAL_BINDING_P (binding) = (level != class_binding_level);
408 /* And put it on the front of the list of bindings for ID. */
409 IDENTIFIER_BINDING (id) = binding;
412 /* Remove the binding for DECL which should be the innermost binding
416 pop_binding (tree id, tree decl)
418 cxx_binding *binding;
421 /* It's easiest to write the loops that call this function without
422 checking whether or not the entities involved have names. We
423 get here for such an entity. */
426 /* Get the innermost binding for ID. */
427 binding = IDENTIFIER_BINDING (id);
429 /* The name should be bound. */
430 my_friendly_assert (binding != NULL, 0);
432 /* The DECL will be either the ordinary binding or the type
433 binding for this identifier. Remove that binding. */
434 if (binding->value == decl)
435 binding->value = NULL_TREE;
436 else if (binding->type == decl)
437 binding->type = NULL_TREE;
441 if (!binding->value && !binding->type)
443 /* We're completely done with the innermost binding for this
444 identifier. Unhook it from the list of bindings. */
445 IDENTIFIER_BINDING (id) = binding->previous;
447 /* Add it to the free list. */
448 cxx_binding_free (binding);
452 /* BINDING records an existing declaration for a namein the current scope.
453 But, DECL is another declaration for that same identifier in the
454 same scope. This is the `struct stat' hack whereby a non-typedef
455 class name or enum-name can be bound at the same level as some other
459 A class name (9.1) or enumeration name (7.2) can be hidden by the
460 name of an object, function, or enumerator declared in the same scope.
461 If a class or enumeration name and an object, function, or enumerator
462 are declared in the same scope (in any order) with the same name, the
463 class or enumeration name is hidden wherever the object, function, or
464 enumerator name is visible.
466 It's the responsibility of the caller to check that
467 inserting this name is valid here. Returns nonzero if the new binding
471 supplement_binding (cxx_binding *binding, tree decl)
473 tree bval = binding->value;
476 timevar_push (TV_NAME_LOOKUP);
477 if (TREE_CODE (decl) == TYPE_DECL && DECL_ARTIFICIAL (decl))
478 /* The new name is the type name. */
479 binding->type = decl;
480 else if (/* BVAL is null when push_class_level_binding moves an
481 inherited type-binding out of the way to make room for a
482 new value binding. */
484 /* BVAL is error_mark_node when DECL's name has been used
485 in a non-class scope prior declaration. In that case,
486 we should have already issued a diagnostic; for graceful
487 error recovery purpose, pretend this was the intended
488 declaration for that name. */
489 || bval == error_mark_node
490 /* If BVAL is a built-in that has not yet been declared,
491 pretend it is not there at all. */
492 || (TREE_CODE (bval) == FUNCTION_DECL
493 && DECL_ANTICIPATED (bval)))
494 binding->value = decl;
495 else if (TREE_CODE (bval) == TYPE_DECL && DECL_ARTIFICIAL (bval))
497 /* The old binding was a type name. It was placed in
498 VALUE field because it was thought, at the point it was
499 declared, to be the only entity with such a name. Move the
500 type name into the type slot; it is now hidden by the new
502 binding->type = bval;
503 binding->value = decl;
504 binding->value_is_inherited = false;
506 else if (TREE_CODE (bval) == TYPE_DECL
507 && TREE_CODE (decl) == TYPE_DECL
508 && DECL_NAME (decl) == DECL_NAME (bval)
509 && (same_type_p (TREE_TYPE (decl), TREE_TYPE (bval))
510 /* If either type involves template parameters, we must
511 wait until instantiation. */
512 || uses_template_parms (TREE_TYPE (decl))
513 || uses_template_parms (TREE_TYPE (bval))))
514 /* We have two typedef-names, both naming the same type to have
515 the same name. This is OK because of:
519 In a given scope, a typedef specifier can be used to redefine
520 the name of any type declared in that scope to refer to the
521 type to which it already refers. */
523 /* There can be two block-scope declarations of the same variable,
524 so long as they are `extern' declarations. However, there cannot
525 be two declarations of the same static data member:
529 A member shall not be declared twice in the
530 member-specification. */
531 else if (TREE_CODE (decl) == VAR_DECL && TREE_CODE (bval) == VAR_DECL
532 && DECL_EXTERNAL (decl) && DECL_EXTERNAL (bval)
533 && !DECL_CLASS_SCOPE_P (decl))
535 duplicate_decls (decl, binding->value);
538 else if (TREE_CODE (decl) == NAMESPACE_DECL
539 && TREE_CODE (bval) == NAMESPACE_DECL
540 && DECL_NAMESPACE_ALIAS (decl)
541 && DECL_NAMESPACE_ALIAS (bval)
542 && ORIGINAL_NAMESPACE (bval) == ORIGINAL_NAMESPACE (decl))
545 In a declarative region, a namespace-alias-definition can be
546 used to redefine a namespace-alias declared in that declarative
547 region to refer only to the namespace to which it already
552 error ("declaration of `%#D'", decl);
553 cp_error_at ("conflicts with previous declaration `%#D'", bval);
557 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, ok);
560 /* Add DECL to the list of things declared in B. */
563 add_decl_to_level (tree decl, cxx_scope *b)
565 if (TREE_CODE (decl) == NAMESPACE_DECL
566 && !DECL_NAMESPACE_ALIAS (decl))
568 TREE_CHAIN (decl) = b->namespaces;
569 b->namespaces = decl;
571 else if (TREE_CODE (decl) == VAR_DECL && DECL_VIRTUAL_P (decl))
573 TREE_CHAIN (decl) = b->vtables;
578 /* We build up the list in reverse order, and reverse it later if
580 TREE_CHAIN (decl) = b->names;
584 /* If appropriate, add decl to separate list of statics. We
585 include extern variables because they might turn out to be
586 static later. It's OK for this list to contain a few false
588 if (b->kind == sk_namespace)
589 if ((TREE_CODE (decl) == VAR_DECL
590 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
591 || (TREE_CODE (decl) == FUNCTION_DECL
592 && (!TREE_PUBLIC (decl) || DECL_DECLARED_INLINE_P (decl))))
593 VARRAY_PUSH_TREE (b->static_decls, decl);
597 /* Record a decl-node X as belonging to the current lexical scope.
598 Check for errors (such as an incompatible declaration for the same
599 name already seen in the same scope).
601 Returns either X or an old decl for the same name.
602 If an old decl is returned, it may have been smashed
603 to agree with what X says. */
610 int need_new_binding;
612 timevar_push (TV_NAME_LOOKUP);
614 need_new_binding = 1;
616 if (DECL_TEMPLATE_PARM_P (x))
617 /* Template parameters have no context; they are not X::T even
618 when declared within a class or namespace. */
622 if (current_function_decl && x != current_function_decl
623 /* A local declaration for a function doesn't constitute
625 && TREE_CODE (x) != FUNCTION_DECL
626 /* A local declaration for an `extern' variable is in the
627 scope of the current namespace, not the current
629 && !(TREE_CODE (x) == VAR_DECL && DECL_EXTERNAL (x))
630 && !DECL_CONTEXT (x))
631 DECL_CONTEXT (x) = current_function_decl;
633 /* If this is the declaration for a namespace-scope function,
634 but the declaration itself is in a local scope, mark the
636 if (TREE_CODE (x) == FUNCTION_DECL
637 && DECL_NAMESPACE_SCOPE_P (x)
638 && current_function_decl
639 && x != current_function_decl)
640 DECL_LOCAL_FUNCTION_P (x) = 1;
643 name = DECL_NAME (x);
646 int different_binding_level = 0;
648 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
649 name = TREE_OPERAND (name, 0);
651 /* In case this decl was explicitly namespace-qualified, look it
652 up in its namespace context. */
653 if (DECL_NAMESPACE_SCOPE_P (x) && namespace_bindings_p ())
654 t = namespace_binding (name, DECL_CONTEXT (x));
656 t = lookup_name_current_level (name);
658 /* [basic.link] If there is a visible declaration of an entity
659 with linkage having the same name and type, ignoring entities
660 declared outside the innermost enclosing namespace scope, the
661 block scope declaration declares that same entity and
662 receives the linkage of the previous declaration. */
663 if (! t && current_function_decl && x != current_function_decl
664 && (TREE_CODE (x) == FUNCTION_DECL || TREE_CODE (x) == VAR_DECL)
665 && DECL_EXTERNAL (x))
667 /* Look in block scope. */
668 t = IDENTIFIER_VALUE (name);
669 /* Or in the innermost namespace. */
671 t = namespace_binding (name, DECL_CONTEXT (x));
672 /* Does it have linkage? Note that if this isn't a DECL, it's an
673 OVERLOAD, which is OK. */
674 if (t && DECL_P (t) && ! (TREE_STATIC (t) || DECL_EXTERNAL (t)))
677 different_binding_level = 1;
680 /* If we are declaring a function, and the result of name-lookup
681 was an OVERLOAD, look for an overloaded instance that is
682 actually the same as the function we are declaring. (If
683 there is one, we have to merge our declaration with the
684 previous declaration.) */
685 if (t && TREE_CODE (t) == OVERLOAD)
689 if (TREE_CODE (x) == FUNCTION_DECL)
690 for (match = t; match; match = OVL_NEXT (match))
692 if (decls_match (OVL_CURRENT (match), x))
696 /* Just choose one. */
700 t = OVL_CURRENT (match);
705 if (t && t != error_mark_node)
707 if (different_binding_level)
709 if (decls_match (x, t))
710 /* The standard only says that the local extern
711 inherits linkage from the previous decl; in
712 particular, default args are not shared. We must
713 also tell cgraph to treat these decls as the same,
714 or we may neglect to emit an "unused" static - we
715 do this by making the DECL_UIDs equal, which should
716 be viewed as a kludge. FIXME. */
718 TREE_PUBLIC (x) = TREE_PUBLIC (t);
719 DECL_UID (x) = DECL_UID (t);
722 else if (TREE_CODE (t) == PARM_DECL)
724 if (DECL_CONTEXT (t) == NULL_TREE)
725 /* This is probably caused by too many errors, but calling
726 abort will say that if errors have occurred. */
729 /* Check for duplicate params. */
730 if (duplicate_decls (x, t))
731 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
733 else if ((DECL_EXTERN_C_FUNCTION_P (x)
734 || DECL_FUNCTION_TEMPLATE_P (x))
735 && is_overloaded_fn (t))
736 /* Don't do anything just yet. */;
737 else if (t == wchar_decl_node)
739 if (pedantic && ! DECL_IN_SYSTEM_HEADER (x))
740 pedwarn ("redeclaration of `wchar_t' as `%T'",
743 /* Throw away the redeclaration. */
744 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
748 tree olddecl = duplicate_decls (x, t);
750 /* If the redeclaration failed, we can stop at this
752 if (olddecl == error_mark_node)
753 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
757 if (TREE_CODE (t) == TYPE_DECL)
758 SET_IDENTIFIER_TYPE_VALUE (name, TREE_TYPE (t));
759 else if (TREE_CODE (t) == FUNCTION_DECL)
760 check_default_args (t);
762 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
764 else if (DECL_MAIN_P (x) && TREE_CODE (t) == FUNCTION_DECL)
766 /* A redeclaration of main, but not a duplicate of the
771 This function shall not be overloaded. */
772 cp_error_at ("invalid redeclaration of `%D'", t);
773 error ("as `%D'", x);
774 /* We don't try to push this declaration since that
776 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
781 check_template_shadow (x);
783 /* If this is a function conjured up by the backend, massage it
784 so it looks friendly. */
785 if (DECL_NON_THUNK_FUNCTION_P (x) && ! DECL_LANG_SPECIFIC (x))
787 retrofit_lang_decl (x);
788 SET_DECL_LANGUAGE (x, lang_c);
791 if (DECL_NON_THUNK_FUNCTION_P (x) && ! DECL_FUNCTION_MEMBER_P (x))
793 t = push_overloaded_decl (x, PUSH_LOCAL);
795 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
796 if (!namespace_bindings_p ())
797 /* We do not need to create a binding for this name;
798 push_overloaded_decl will have already done so if
800 need_new_binding = 0;
802 else if (DECL_FUNCTION_TEMPLATE_P (x) && DECL_NAMESPACE_SCOPE_P (x))
804 t = push_overloaded_decl (x, PUSH_GLOBAL);
806 add_decl_to_level (x, NAMESPACE_LEVEL (CP_DECL_CONTEXT (t)));
807 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
810 /* If declaring a type as a typedef, copy the type (unless we're
811 at line 0), and install this TYPE_DECL as the new type's typedef
812 name. See the extensive comment in ../c-decl.c (pushdecl). */
813 if (TREE_CODE (x) == TYPE_DECL)
815 tree type = TREE_TYPE (x);
816 if (DECL_IS_BUILTIN (x))
818 if (TYPE_NAME (type) == 0)
819 TYPE_NAME (type) = x;
821 else if (type != error_mark_node && TYPE_NAME (type) != x
822 /* We don't want to copy the type when all we're
823 doing is making a TYPE_DECL for the purposes of
825 && (!TYPE_NAME (type)
826 || TYPE_NAME (type) != DECL_ABSTRACT_ORIGIN (x)))
828 DECL_ORIGINAL_TYPE (x) = type;
829 type = build_type_copy (type);
830 TYPE_STUB_DECL (type) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
831 TYPE_NAME (type) = x;
832 TREE_TYPE (x) = type;
835 if (type != error_mark_node
837 && TYPE_IDENTIFIER (type))
838 set_identifier_type_value (DECL_NAME (x), x);
841 /* Multiple external decls of the same identifier ought to match.
843 We get warnings about inline functions where they are defined.
844 We get warnings about other functions from push_overloaded_decl.
846 Avoid duplicate warnings where they are used. */
847 if (TREE_PUBLIC (x) && TREE_CODE (x) != FUNCTION_DECL)
851 decl = IDENTIFIER_NAMESPACE_VALUE (name);
852 if (decl && TREE_CODE (decl) == OVERLOAD)
853 decl = OVL_FUNCTION (decl);
855 if (decl && decl != error_mark_node
856 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl))
857 /* If different sort of thing, we already gave an error. */
858 && TREE_CODE (decl) == TREE_CODE (x)
859 && !same_type_p (TREE_TYPE (x), TREE_TYPE (decl)))
861 pedwarn ("type mismatch with previous external decl of `%#D'", x);
862 cp_pedwarn_at ("previous external decl of `%#D'", decl);
866 /* This name is new in its binding level.
867 Install the new declaration and return it. */
868 if (namespace_bindings_p ())
870 /* Install a global value. */
872 /* If the first global decl has external linkage,
873 warn if we later see static one. */
874 if (IDENTIFIER_GLOBAL_VALUE (name) == NULL_TREE && TREE_PUBLIC (x))
875 TREE_PUBLIC (name) = 1;
877 /* Bind the name for the entity. */
878 if (!(TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x)
880 && (TREE_CODE (x) == TYPE_DECL
881 || TREE_CODE (x) == VAR_DECL
882 || TREE_CODE (x) == ALIAS_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 = IDENTIFIER_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 ("extern declaration of `%#D' doesn't match", x);
945 cp_warning_at ("global declaration `%#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 /* Skip the ctor/dtor cleanup level. */
979 if (b->kind == sk_function_parms)
981 error ("declaration of '%#D' shadows a parameter", x);
986 if (warn_shadow && !err)
988 warning ("declaration of '%#D' shadows a parameter", x);
989 warning ("%Jshadowed declaration is here", oldlocal);
993 /* Maybe warn if shadowing something else. */
994 else if (warn_shadow && !DECL_EXTERNAL (x)
995 /* No shadow warnings for internally generated vars. */
996 && ! DECL_ARTIFICIAL (x)
997 /* No shadow warnings for vars made for inlining. */
998 && ! DECL_FROM_INLINE (x))
1000 if (IDENTIFIER_CLASS_VALUE (name) != NULL_TREE
1001 && current_class_ptr
1002 && !TREE_STATIC (name))
1004 /* Location of previous decl is not useful in this case. */
1005 warning ("declaration of '%D' shadows a member of 'this'",
1008 else if (oldlocal != NULL_TREE
1009 && TREE_CODE (oldlocal) == VAR_DECL)
1011 warning ("declaration of '%D' shadows a previous local", x);
1012 warning ("%Jshadowed declaration is here", oldlocal);
1014 else if (oldglobal != NULL_TREE
1015 && TREE_CODE (oldglobal) == VAR_DECL)
1016 /* XXX shadow warnings in outer-more namespaces */
1018 warning ("declaration of '%D' shadows a global declaration",
1020 warning ("%Jshadowed declaration is here", oldglobal);
1025 if (TREE_CODE (x) == FUNCTION_DECL)
1026 check_default_args (x);
1028 if (TREE_CODE (x) == VAR_DECL)
1029 maybe_register_incomplete_var (x);
1032 if (need_new_binding)
1033 add_decl_to_level (x,
1034 DECL_NAMESPACE_SCOPE_P (x)
1035 ? NAMESPACE_LEVEL (CP_DECL_CONTEXT (x))
1036 : current_binding_level);
1038 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
1041 /* Enter DECL into the symbol table, if that's appropriate. Returns
1042 DECL, or a modified version thereof. */
1045 maybe_push_decl (tree decl)
1047 tree type = TREE_TYPE (decl);
1049 /* Add this decl to the current binding level, but not if it comes
1050 from another scope, e.g. a static member variable. TEM may equal
1051 DECL or it may be a previous decl of the same name. */
1052 if (decl == error_mark_node
1053 || (TREE_CODE (decl) != PARM_DECL
1054 && DECL_CONTEXT (decl) != NULL_TREE
1055 /* Definitions of namespace members outside their namespace are
1057 && TREE_CODE (DECL_CONTEXT (decl)) != NAMESPACE_DECL)
1058 || (TREE_CODE (decl) == TEMPLATE_DECL && !namespace_bindings_p ())
1059 || TREE_CODE (type) == UNKNOWN_TYPE
1060 /* The declaration of a template specialization does not affect
1061 the functions available for overload resolution, so we do not
1063 || (TREE_CODE (decl) == FUNCTION_DECL
1064 && DECL_TEMPLATE_SPECIALIZATION (decl)))
1067 return pushdecl (decl);
1070 /* Bind DECL to ID in the current_binding_level, assumed to be a local
1071 binding level. If PUSH_USING is set in FLAGS, we know that DECL
1072 doesn't really belong to this binding level, that it got here
1073 through a using-declaration. */
1076 push_local_binding (tree id, tree decl, int flags)
1078 struct cp_binding_level *b;
1080 /* Skip over any local classes. This makes sense if we call
1081 push_local_binding with a friend decl of a local class. */
1082 b = innermost_nonclass_level ();
1084 if (lookup_name_current_level (id))
1086 /* Supplement the existing binding. */
1087 if (!supplement_binding (IDENTIFIER_BINDING (id), decl))
1088 /* It didn't work. Something else must be bound at this
1089 level. Do not add DECL to the list of things to pop
1094 /* Create a new binding. */
1095 push_binding (id, decl, b);
1097 if (TREE_CODE (decl) == OVERLOAD || (flags & PUSH_USING))
1098 /* We must put the OVERLOAD into a TREE_LIST since the
1099 TREE_CHAIN of an OVERLOAD is already used. Similarly for
1100 decls that got here through a using-declaration. */
1101 decl = build_tree_list (NULL_TREE, decl);
1103 /* And put DECL on the list of things declared by the current
1105 add_decl_to_level (decl, b);
1108 /* The old ARM scoping rules injected variables declared in the
1109 initialization statement of a for-statement into the surrounding
1110 scope. We support this usage, in order to be backward-compatible.
1111 DECL is a just-declared VAR_DECL; if necessary inject its
1112 declaration into the surrounding scope. */
1115 maybe_inject_for_scope_var (tree decl)
1117 timevar_push (TV_NAME_LOOKUP);
1118 if (!DECL_NAME (decl))
1120 timevar_pop (TV_NAME_LOOKUP);
1124 /* Declarations of __FUNCTION__ and its ilk appear magically when
1125 the variable is first used. If that happens to be inside a
1126 for-loop, we don't want to do anything special. */
1127 if (DECL_PRETTY_FUNCTION_P (decl))
1129 timevar_pop (TV_NAME_LOOKUP);
1133 if (current_binding_level->kind == sk_for)
1135 struct cp_binding_level *outer
1136 = current_binding_level->level_chain;
1138 /* Check to see if the same name is already bound at the outer
1139 level, either because it was directly declared, or because a
1140 dead for-decl got preserved. In either case, the code would
1141 not have been valid under the ARM scope rules, so clear
1142 is_for_scope for the current_binding_level.
1144 Otherwise, we need to preserve the temp slot for decl to last
1145 into the outer binding level. */
1147 cxx_binding *outer_binding
1148 = IDENTIFIER_BINDING (DECL_NAME (decl))->previous;
1150 if (outer_binding && outer_binding->scope == outer
1151 && (TREE_CODE (outer_binding->value) == VAR_DECL)
1152 && DECL_DEAD_FOR_LOCAL (outer_binding->value))
1154 outer_binding->value = DECL_SHADOWED_FOR_VAR (outer_binding->value);
1155 current_binding_level->kind = sk_block;
1158 timevar_pop (TV_NAME_LOOKUP);
1161 /* Check to see whether or not DECL is a variable that would have been
1162 in scope under the ARM, but is not in scope under the ANSI/ISO
1163 standard. If so, issue an error message. If name lookup would
1164 work in both cases, but return a different result, this function
1165 returns the result of ANSI/ISO lookup. Otherwise, it returns
1169 check_for_out_of_scope_variable (tree decl)
1173 /* We only care about out of scope variables. */
1174 if (!(TREE_CODE (decl) == VAR_DECL && DECL_DEAD_FOR_LOCAL (decl)))
1177 shadowed = DECL_SHADOWED_FOR_VAR (decl);
1178 while (shadowed != NULL_TREE && TREE_CODE (shadowed) == VAR_DECL
1179 && DECL_DEAD_FOR_LOCAL (shadowed))
1180 shadowed = DECL_SHADOWED_FOR_VAR (shadowed);
1182 shadowed = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (decl));
1185 if (!DECL_ERROR_REPORTED (decl))
1187 warning ("name lookup of `%D' changed",
1189 cp_warning_at (" matches this `%D' under ISO standard rules",
1191 cp_warning_at (" matches this `%D' under old rules", decl);
1192 DECL_ERROR_REPORTED (decl) = 1;
1197 /* If we have already complained about this declaration, there's no
1198 need to do it again. */
1199 if (DECL_ERROR_REPORTED (decl))
1202 DECL_ERROR_REPORTED (decl) = 1;
1204 if (TREE_TYPE (decl) == error_mark_node)
1207 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
1209 error ("name lookup of `%D' changed for new ISO `for' scoping",
1211 cp_error_at (" cannot use obsolete binding at `%D' because it has a destructor", decl);
1212 return error_mark_node;
1216 pedwarn ("name lookup of `%D' changed for new ISO `for' scoping",
1218 cp_pedwarn_at (" using obsolete binding at `%D'", decl);
1224 /* true means unconditionally make a BLOCK for the next level pushed. */
1226 static bool keep_next_level_flag;
1228 static int binding_depth = 0;
1229 static int is_class_level = 0;
1236 for (i = 0; i < depth * 2; i++)
1240 /* Return a string describing the kind of SCOPE we have. */
1242 cxx_scope_descriptor (cxx_scope *scope)
1244 /* The order of this table must match the "scope_kind"
1246 static const char* scope_kind_names[] = {
1252 "function-parameter-scope",
1255 "template-parameter-scope",
1256 "template-explicit-spec-scope"
1258 const scope_kind kind = scope->explicit_spec_p
1259 ? sk_template_spec : scope->kind;
1261 return scope_kind_names[kind];
1264 /* Output a debugging information about SCOPE when performing
1267 cxx_scope_debug (cxx_scope *scope, int line, const char *action)
1269 const char *desc = cxx_scope_descriptor (scope);
1270 if (scope->this_entity)
1271 verbatim ("%s %s(%E) %p %d\n", action, desc,
1272 scope->this_entity, (void *) scope, line);
1274 verbatim ("%s %s %p %d\n", action, desc, (void *) scope, line);
1277 /* Return the estimated initial size of the hashtable of a NAMESPACE
1280 static inline size_t
1281 namespace_scope_ht_size (tree ns)
1283 tree name = DECL_NAME (ns);
1285 return name == std_identifier
1286 ? NAMESPACE_STD_HT_SIZE
1287 : (name == global_scope_name
1288 ? GLOBAL_SCOPE_HT_SIZE
1289 : NAMESPACE_ORDINARY_HT_SIZE);
1292 /* A chain of binding_level structures awaiting reuse. */
1294 static GTY((deletable)) struct cp_binding_level *free_binding_level;
1296 /* Insert SCOPE as the innermost binding level. */
1299 push_binding_level (struct cp_binding_level *scope)
1301 /* Add it to the front of currently active scopes stack. */
1302 scope->level_chain = current_binding_level;
1303 current_binding_level = scope;
1304 keep_next_level_flag = false;
1306 if (ENABLE_SCOPE_CHECKING)
1308 scope->binding_depth = binding_depth;
1309 indent (binding_depth);
1310 cxx_scope_debug (scope, input_line, "push");
1316 /* Create a new KIND scope and make it the top of the active scopes stack.
1317 ENTITY is the scope of the associated C++ entity (namespace, class,
1318 function); it is NULL otherwise. */
1321 begin_scope (scope_kind kind, tree entity)
1325 /* Reuse or create a struct for this binding level. */
1326 if (!ENABLE_SCOPE_CHECKING && free_binding_level)
1328 scope = free_binding_level;
1329 free_binding_level = scope->level_chain;
1332 scope = ggc_alloc (sizeof (cxx_scope));
1333 memset (scope, 0, sizeof (cxx_scope));
1335 scope->this_entity = entity;
1336 scope->more_cleanups_ok = true;
1343 case sk_template_spec:
1344 scope->explicit_spec_p = true;
1345 kind = sk_template_parms;
1347 case sk_template_parms:
1353 case sk_function_parms:
1354 scope->keep = keep_next_level_flag;
1358 scope->type_decls = binding_table_new (namespace_scope_ht_size (entity));
1359 NAMESPACE_LEVEL (entity) = scope;
1360 VARRAY_TREE_INIT (scope->static_decls,
1361 DECL_NAME (entity) == std_identifier
1362 || DECL_NAME (entity) == global_scope_name
1364 "Static declarations");
1368 /* Should not happen. */
1369 my_friendly_assert (false, 20030922);
1374 push_binding_level (scope);
1379 /* We're about to leave current scope. Pop the top of the stack of
1380 currently active scopes. Return the enclosing scope, now active. */
1385 cxx_scope *scope = current_binding_level;
1387 if (scope->kind == sk_namespace && class_binding_level)
1388 current_binding_level = class_binding_level;
1390 /* We cannot leave a scope, if there are none left. */
1391 if (NAMESPACE_LEVEL (global_namespace))
1392 my_friendly_assert (!global_scope_p (scope), 20030527);
1394 if (ENABLE_SCOPE_CHECKING)
1396 indent (--binding_depth);
1397 cxx_scope_debug (scope, input_line, "leave");
1398 if (is_class_level != (scope == class_binding_level))
1400 indent (binding_depth);
1401 verbatim ("XXX is_class_level != (current_scope == class_scope)\n");
1406 /* Move one nesting level up. */
1407 current_binding_level = scope->level_chain;
1409 /* Namespace-scopes are left most probably temporarily, not
1410 completely; they can be reopen later, e.g. in namespace-extension
1411 or any name binding activity that requires us to resume a
1412 namespace. For classes, we cache some binding levels. For other
1413 scopes, we just make the structure available for reuse. */
1414 if (scope->kind != sk_namespace
1415 && scope->kind != sk_class)
1417 scope->level_chain = free_binding_level;
1418 if (scope->kind == sk_class)
1419 scope->type_decls = NULL;
1421 binding_table_free (scope->type_decls);
1422 my_friendly_assert (!ENABLE_SCOPE_CHECKING
1423 || scope->binding_depth == binding_depth,
1425 free_binding_level = scope;
1428 /* Find the innermost enclosing class scope, and reset
1429 CLASS_BINDING_LEVEL appropriately. */
1430 for (scope = current_binding_level;
1431 scope && scope->kind != sk_class;
1432 scope = scope->level_chain)
1434 class_binding_level = scope && scope->kind == sk_class ? scope : NULL;
1436 return current_binding_level;
1440 resume_scope (struct cp_binding_level* b)
1442 /* Resuming binding levels is meant only for namespaces,
1443 and those cannot nest into classes. */
1444 my_friendly_assert(!class_binding_level, 386);
1445 /* Also, resuming a non-directly nested namespace is a no-no. */
1446 my_friendly_assert(b->level_chain == current_binding_level, 386);
1447 current_binding_level = b;
1448 if (ENABLE_SCOPE_CHECKING)
1450 b->binding_depth = binding_depth;
1451 indent (binding_depth);
1452 cxx_scope_debug (b, input_line, "resume");
1458 /* Return the innermost binding level that is not for a class scope. */
1461 innermost_nonclass_level (void)
1465 b = current_binding_level;
1466 while (b->kind == sk_class)
1472 /* We're defining an object of type TYPE. If it needs a cleanup, but
1473 we're not allowed to add any more objects with cleanups to the current
1474 scope, create a new binding level. */
1477 maybe_push_cleanup_level (tree type)
1479 if (type != error_mark_node
1480 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
1481 && current_binding_level->more_cleanups_ok == 0)
1483 begin_scope (sk_cleanup, NULL);
1484 current_binding_level->statement_list = push_stmt_list ();
1488 /* Nonzero if we are currently in the global binding level. */
1491 global_bindings_p (void)
1493 return global_scope_p (current_binding_level);
1496 /* True if we are currently in a toplevel binding level. This
1497 means either the global binding level or a namespace in a toplevel
1498 binding level. Since there are no non-toplevel namespace levels,
1499 this really means any namespace or template parameter level. We
1500 also include a class whose context is toplevel. */
1503 toplevel_bindings_p (void)
1505 struct cp_binding_level *b = innermost_nonclass_level ();
1507 return b->kind == sk_namespace || b->kind == sk_template_parms;
1510 /* True if this is a namespace scope, or if we are defining a class
1511 which is itself at namespace scope, or whose enclosing class is
1512 such a class, etc. */
1515 namespace_bindings_p (void)
1517 struct cp_binding_level *b = innermost_nonclass_level ();
1519 return b->kind == sk_namespace;
1522 /* True if the current level needs to have a BLOCK made. */
1527 return (current_binding_level->blocks != NULL_TREE
1528 || current_binding_level->keep
1529 || current_binding_level->kind == sk_cleanup
1530 || current_binding_level->names != NULL_TREE
1531 || current_binding_level->type_decls != NULL);
1534 /* Returns the kind of the innermost scope. */
1537 innermost_scope_kind (void)
1539 return current_binding_level->kind;
1542 /* Returns true if this scope was created to store template parameters. */
1545 template_parm_scope_p (void)
1547 return innermost_scope_kind () == sk_template_parms;
1550 /* If KEEP is true, make a BLOCK node for the next binding level,
1551 unconditionally. Otherwise, use the normal logic to decide whether
1552 or not to create a BLOCK. */
1555 keep_next_level (bool keep)
1557 keep_next_level_flag = keep;
1560 /* Return the list of declarations of the current level.
1561 Note that this list is in reverse order unless/until
1562 you nreverse it; and when you do nreverse it, you must
1563 store the result back using `storedecls' or you will lose. */
1568 return current_binding_level->names;
1571 /* Set the current binding TABLE for type declarations.. This is a
1572 temporary workaround of the fact that the data structure classtypes
1573 does not currently carry its allocated cxx_scope structure. */
1575 cxx_remember_type_decls (binding_table table)
1577 current_binding_level->type_decls = table;
1580 /* For debugging. */
1581 static int no_print_functions = 0;
1582 static int no_print_builtins = 0;
1584 /* Called from print_binding_level through binding_table_foreach to
1585 print the content of binding ENTRY. DATA is a pointer to line offset
1588 bt_print_entry (binding_entry entry, void *data)
1590 int *p = (int *) data;
1593 if (entry->name == NULL)
1595 else if (entry->name == TYPE_IDENTIFIER (entry->type))
1605 fprintf (stderr, "\n\t");
1608 if (entry->name == NULL)
1610 print_node_brief (stderr, "<unnamed-typedef", entry->type, 0);
1611 fprintf (stderr, ">");
1613 else if (entry->name == TYPE_IDENTIFIER (entry->type))
1614 print_node_brief (stderr, "", entry->type, 0);
1617 print_node_brief (stderr, "<typedef", entry->name, 0);
1618 print_node_brief (stderr, "", entry->type, 0);
1619 fprintf (stderr, ">");
1624 print_binding_level (struct cp_binding_level* lvl)
1628 fprintf (stderr, " blocks=" HOST_PTR_PRINTF, (void *) lvl->blocks);
1629 if (lvl->more_cleanups_ok)
1630 fprintf (stderr, " more-cleanups-ok");
1631 if (lvl->have_cleanups)
1632 fprintf (stderr, " have-cleanups");
1633 fprintf (stderr, "\n");
1636 fprintf (stderr, " names:\t");
1637 /* We can probably fit 3 names to a line? */
1638 for (t = lvl->names; t; t = TREE_CHAIN (t))
1640 if (no_print_functions && (TREE_CODE (t) == FUNCTION_DECL))
1642 if (no_print_builtins
1643 && (TREE_CODE (t) == TYPE_DECL)
1644 && DECL_IS_BUILTIN (t))
1647 /* Function decls tend to have longer names. */
1648 if (TREE_CODE (t) == FUNCTION_DECL)
1655 fprintf (stderr, "\n\t");
1658 print_node_brief (stderr, "", t, 0);
1659 if (t == error_mark_node)
1663 fprintf (stderr, "\n");
1665 if (lvl->type_decls)
1667 fprintf (stderr, " tags:\t");
1669 binding_table_foreach (lvl->type_decls, bt_print_entry, &i);
1671 fprintf (stderr, "\n");
1673 if (VEC_length (cp_class_binding, lvl->class_shadowed))
1676 cp_class_binding *b;
1677 fprintf (stderr, " class-shadowed:");
1679 (b = VEC_iterate(cp_class_binding,
1680 lvl->class_shadowed,
1683 fprintf (stderr, " %s ", IDENTIFIER_POINTER (b->identifier));
1684 fprintf (stderr, "\n");
1686 if (lvl->type_shadowed)
1688 fprintf (stderr, " type-shadowed:");
1689 for (t = lvl->type_shadowed; t; t = TREE_CHAIN (t))
1691 fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
1693 fprintf (stderr, "\n");
1698 print_other_binding_stack (struct cp_binding_level *stack)
1700 struct cp_binding_level *level;
1701 for (level = stack; !global_scope_p (level); level = level->level_chain)
1703 fprintf (stderr, "binding level " HOST_PTR_PRINTF "\n", (void *) level);
1704 print_binding_level (level);
1709 print_binding_stack (void)
1711 struct cp_binding_level *b;
1712 fprintf (stderr, "current_binding_level=" HOST_PTR_PRINTF
1713 "\nclass_binding_level=" HOST_PTR_PRINTF
1714 "\nNAMESPACE_LEVEL (global_namespace)=" HOST_PTR_PRINTF "\n",
1715 (void *) current_binding_level, (void *) class_binding_level,
1716 (void *) NAMESPACE_LEVEL (global_namespace));
1717 if (class_binding_level)
1719 for (b = class_binding_level; b; b = b->level_chain)
1720 if (b == current_binding_level)
1723 b = class_binding_level;
1725 b = current_binding_level;
1728 b = current_binding_level;
1729 print_other_binding_stack (b);
1730 fprintf (stderr, "global:\n");
1731 print_binding_level (NAMESPACE_LEVEL (global_namespace));
1734 /* Return the type associated with id. */
1737 identifier_type_value (tree id)
1739 timevar_push (TV_NAME_LOOKUP);
1740 /* There is no type with that name, anywhere. */
1741 if (REAL_IDENTIFIER_TYPE_VALUE (id) == NULL_TREE)
1742 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
1743 /* This is not the type marker, but the real thing. */
1744 if (REAL_IDENTIFIER_TYPE_VALUE (id) != global_type_node)
1745 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, REAL_IDENTIFIER_TYPE_VALUE (id));
1746 /* Have to search for it. It must be on the global level, now.
1747 Ask lookup_name not to return non-types. */
1748 id = lookup_name_real (id, 2, 1, 0, LOOKUP_COMPLAIN);
1750 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, TREE_TYPE (id));
1751 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
1754 /* Return the IDENTIFIER_GLOBAL_VALUE of T, for use in common code, since
1755 the definition of IDENTIFIER_GLOBAL_VALUE is different for C and C++. */
1758 identifier_global_value (tree t)
1760 return IDENTIFIER_GLOBAL_VALUE (t);
1763 /* Push a definition of struct, union or enum tag named ID. into
1764 binding_level B. DECL is a TYPE_DECL for the type. We assume that
1765 the tag ID is not already defined. */
1768 set_identifier_type_value_with_scope (tree id, tree decl, cxx_scope *b)
1772 if (b->kind != sk_namespace)
1774 /* Shadow the marker, not the real thing, so that the marker
1775 gets restored later. */
1776 tree old_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
1778 = tree_cons (id, old_type_value, b->type_shadowed);
1779 type = decl ? TREE_TYPE (decl) : NULL_TREE;
1783 cxx_binding *binding =
1784 binding_for_name (NAMESPACE_LEVEL (current_namespace), id);
1788 supplement_binding (binding, decl);
1790 binding->value = decl;
1794 /* Store marker instead of real type. */
1795 type = global_type_node;
1797 SET_IDENTIFIER_TYPE_VALUE (id, type);
1800 /* As set_identifier_type_value_with_scope, but using
1801 current_binding_level. */
1804 set_identifier_type_value (tree id, tree decl)
1806 set_identifier_type_value_with_scope (id, decl, current_binding_level);
1809 /* Return the name for the constructor (or destructor) for the
1810 specified class TYPE. When given a template, this routine doesn't
1811 lose the specialization. */
1814 constructor_name_full (tree type)
1816 type = TYPE_MAIN_VARIANT (type);
1817 if (CLASS_TYPE_P (type) && TYPE_WAS_ANONYMOUS (type)
1818 && TYPE_HAS_CONSTRUCTOR (type))
1819 return DECL_NAME (OVL_CURRENT (CLASSTYPE_CONSTRUCTORS (type)));
1821 return TYPE_IDENTIFIER (type);
1824 /* Return the name for the constructor (or destructor) for the
1825 specified class. When given a template, return the plain
1826 unspecialized name. */
1829 constructor_name (tree type)
1832 name = constructor_name_full (type);
1833 if (IDENTIFIER_TEMPLATE (name))
1834 name = IDENTIFIER_TEMPLATE (name);
1838 /* Returns TRUE if NAME is the name for the constructor for TYPE. */
1841 constructor_name_p (tree name, tree type)
1848 if (TREE_CODE (name) != IDENTIFIER_NODE)
1851 ctor_name = constructor_name_full (type);
1852 if (name == ctor_name)
1854 if (IDENTIFIER_TEMPLATE (ctor_name)
1855 && name == IDENTIFIER_TEMPLATE (ctor_name))
1860 /* Counter used to create anonymous type names. */
1862 static GTY(()) int anon_cnt;
1864 /* Return an IDENTIFIER which can be used as a name for
1865 anonymous structs and unions. */
1868 make_anon_name (void)
1872 sprintf (buf, ANON_AGGRNAME_FORMAT, anon_cnt++);
1873 return get_identifier (buf);
1876 /* Clear the TREE_PURPOSE slot of UTDs which have anonymous typenames.
1877 This keeps dbxout from getting confused. */
1880 clear_anon_tags (void)
1882 struct cp_binding_level *b;
1883 static int last_cnt = 0;
1885 /* Fast out if no new anon names were declared. */
1886 if (last_cnt == anon_cnt)
1889 b = current_binding_level;
1890 while (b->kind == sk_cleanup)
1892 if (b->type_decls != NULL)
1893 binding_table_remove_anonymous_types (b->type_decls);
1894 last_cnt = anon_cnt;
1897 /* Return (from the stack of) the BINDING, if any, established at SCOPE. */
1899 static inline cxx_binding *
1900 find_binding (cxx_scope *scope, cxx_binding *binding)
1902 timevar_push (TV_NAME_LOOKUP);
1904 for (; binding != NULL; binding = binding->previous)
1905 if (binding->scope == scope)
1906 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, binding);
1908 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, (cxx_binding *)0);
1911 /* Return the binding for NAME in SCOPE, if any. Otherwise, return NULL. */
1913 static inline cxx_binding *
1914 cxx_scope_find_binding_for_name (cxx_scope *scope, tree name)
1916 cxx_binding *b = IDENTIFIER_NAMESPACE_BINDINGS (name);
1919 /* Fold-in case where NAME is used only once. */
1920 if (scope == b->scope && b->previous == NULL)
1922 return find_binding (scope, b);
1927 /* Always returns a binding for name in scope. If no binding is
1928 found, make a new one. */
1930 static cxx_binding *
1931 binding_for_name (cxx_scope *scope, tree name)
1933 cxx_binding *result;
1935 result = cxx_scope_find_binding_for_name (scope, name);
1938 /* Not found, make a new one. */
1939 result = cxx_binding_make (NULL, NULL);
1940 result->previous = IDENTIFIER_NAMESPACE_BINDINGS (name);
1941 result->scope = scope;
1942 result->is_local = false;
1943 result->value_is_inherited = false;
1944 IDENTIFIER_NAMESPACE_BINDINGS (name) = result;
1948 /* Insert another USING_DECL into the current binding level, returning
1949 this declaration. If this is a redeclaration, do nothing, and
1950 return NULL_TREE if this not in namespace scope (in namespace
1951 scope, a using decl might extend any previous bindings). */
1954 push_using_decl (tree scope, tree name)
1958 timevar_push (TV_NAME_LOOKUP);
1959 my_friendly_assert (TREE_CODE (scope) == NAMESPACE_DECL, 383);
1960 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 384);
1961 for (decl = current_binding_level->usings; decl; decl = TREE_CHAIN (decl))
1962 if (DECL_INITIAL (decl) == scope && DECL_NAME (decl) == name)
1965 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP,
1966 namespace_bindings_p () ? decl : NULL_TREE);
1967 decl = build_lang_decl (USING_DECL, name, void_type_node);
1968 DECL_INITIAL (decl) = scope;
1969 TREE_CHAIN (decl) = current_binding_level->usings;
1970 current_binding_level->usings = decl;
1971 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
1974 /* Same as pushdecl, but define X in binding-level LEVEL. We rely on the
1975 caller to set DECL_CONTEXT properly. */
1978 pushdecl_with_scope (tree x, cxx_scope *level)
1980 struct cp_binding_level *b;
1981 tree function_decl = current_function_decl;
1983 timevar_push (TV_NAME_LOOKUP);
1984 current_function_decl = NULL_TREE;
1985 if (level->kind == sk_class)
1987 b = class_binding_level;
1988 class_binding_level = level;
1989 pushdecl_class_level (x);
1990 class_binding_level = b;
1994 b = current_binding_level;
1995 current_binding_level = level;
1997 current_binding_level = b;
1999 current_function_decl = function_decl;
2000 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
2003 /* DECL is a FUNCTION_DECL for a non-member function, which may have
2004 other definitions already in place. We get around this by making
2005 the value of the identifier point to a list of all the things that
2006 want to be referenced by that name. It is then up to the users of
2007 that name to decide what to do with that list.
2009 DECL may also be a TEMPLATE_DECL, with a FUNCTION_DECL in its
2010 DECL_TEMPLATE_RESULT. It is dealt with the same way.
2012 FLAGS is a bitwise-or of the following values:
2013 PUSH_LOCAL: Bind DECL in the current scope, rather than at
2015 PUSH_USING: DECL is being pushed as the result of a using
2018 The value returned may be a previous declaration if we guessed wrong
2019 about what language DECL should belong to (C or C++). Otherwise,
2020 it's always DECL (and never something that's not a _DECL). */
2023 push_overloaded_decl (tree decl, int flags)
2025 tree name = DECL_NAME (decl);
2028 int doing_global = (namespace_bindings_p () || !(flags & PUSH_LOCAL));
2030 timevar_push (TV_NAME_LOOKUP);
2032 old = namespace_binding (name, DECL_CONTEXT (decl));
2034 old = lookup_name_current_level (name);
2038 if (TREE_CODE (old) == TYPE_DECL && DECL_ARTIFICIAL (old))
2040 tree t = TREE_TYPE (old);
2041 if (IS_AGGR_TYPE (t) && warn_shadow
2042 && (! DECL_IN_SYSTEM_HEADER (decl)
2043 || ! DECL_IN_SYSTEM_HEADER (old)))
2044 warning ("`%#D' hides constructor for `%#T'", decl, t);
2047 else if (is_overloaded_fn (old))
2051 for (tmp = old; tmp; tmp = OVL_NEXT (tmp))
2053 tree fn = OVL_CURRENT (tmp);
2055 if (TREE_CODE (tmp) == OVERLOAD && OVL_USED (tmp)
2056 && !(flags & PUSH_USING)
2057 && compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
2058 TYPE_ARG_TYPES (TREE_TYPE (decl))))
2059 error ("`%#D' conflicts with previous using declaration `%#D'",
2062 if (duplicate_decls (decl, fn) == fn)
2063 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, fn);
2066 else if (old == error_mark_node)
2067 /* Ignore the undefined symbol marker. */
2071 cp_error_at ("previous non-function declaration `%#D'", old);
2072 error ("conflicts with function declaration `%#D'", decl);
2073 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
2077 if (old || TREE_CODE (decl) == TEMPLATE_DECL
2078 /* If it's a using declaration, we always need to build an OVERLOAD,
2079 because it's the only way to remember that the declaration comes
2080 from 'using', and have the lookup behave correctly. */
2081 || (flags & PUSH_USING))
2083 if (old && TREE_CODE (old) != OVERLOAD)
2084 new_binding = ovl_cons (decl, ovl_cons (old, NULL_TREE));
2086 new_binding = ovl_cons (decl, old);
2087 if (flags & PUSH_USING)
2088 OVL_USED (new_binding) = 1;
2091 /* NAME is not ambiguous. */
2095 set_namespace_binding (name, current_namespace, new_binding);
2098 /* We only create an OVERLOAD if there was a previous binding at
2099 this level, or if decl is a template. In the former case, we
2100 need to remove the old binding and replace it with the new
2101 binding. We must also run through the NAMES on the binding
2102 level where the name was bound to update the chain. */
2104 if (TREE_CODE (new_binding) == OVERLOAD && old)
2108 for (d = &IDENTIFIER_BINDING (name)->scope->names;
2110 d = &TREE_CHAIN (*d))
2112 || (TREE_CODE (*d) == TREE_LIST
2113 && TREE_VALUE (*d) == old))
2115 if (TREE_CODE (*d) == TREE_LIST)
2116 /* Just replace the old binding with the new. */
2117 TREE_VALUE (*d) = new_binding;
2119 /* Build a TREE_LIST to wrap the OVERLOAD. */
2120 *d = tree_cons (NULL_TREE, new_binding,
2123 /* And update the cxx_binding node. */
2124 IDENTIFIER_BINDING (name)->value = new_binding;
2125 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
2128 /* We should always find a previous binding in this case. */
2132 /* Install the new binding. */
2133 push_local_binding (name, new_binding, flags);
2136 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
2139 /* Check a non-member using-declaration. Return the name and scope
2140 being used, and the USING_DECL, or NULL_TREE on failure. */
2143 validate_nonmember_using_decl (tree decl, tree scope, tree name)
2145 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR)
2148 A using-declaration shall not name a template-id. */
2149 error ("a using-declaration cannot specify a template-id. Try `using %D'", name);
2153 if (TREE_CODE (decl) == NAMESPACE_DECL)
2155 error ("namespace `%D' not allowed in using-declaration", decl);
2159 if (TREE_CODE (decl) == SCOPE_REF)
2161 /* It's a nested name with template parameter dependent scope.
2162 This can only be using-declaration for class member. */
2163 error ("`%T' is not a namespace", TREE_OPERAND (decl, 0));
2167 if (is_overloaded_fn (decl))
2168 decl = get_first_fn (decl);
2170 my_friendly_assert (DECL_P (decl), 20020908);
2172 /* [namespace.udecl]
2173 A using-declaration for a class member shall be a
2174 member-declaration. */
2177 error ("`%T' is not a namespace", scope);
2181 /* Make a USING_DECL. */
2182 return push_using_decl (scope, name);
2185 /* Process local and global using-declarations. */
2188 do_nonmember_using_decl (tree scope, tree name, tree oldval, tree oldtype,
2189 tree *newval, tree *newtype)
2191 struct scope_binding decls = EMPTY_SCOPE_BINDING;
2193 *newval = *newtype = NULL_TREE;
2194 if (!qualified_lookup_using_namespace (name, scope, &decls, 0))
2198 if (!decls.value && !decls.type)
2200 error ("`%D' not declared", name);
2204 /* Check for using functions. */
2205 if (decls.value && is_overloaded_fn (decls.value))
2209 if (oldval && !is_overloaded_fn (oldval))
2211 if (!DECL_IMPLICIT_TYPEDEF_P (oldval))
2212 error ("`%D' is already declared in this scope", name);
2217 for (tmp = decls.value; tmp; tmp = OVL_NEXT (tmp))
2219 tree new_fn = OVL_CURRENT (tmp);
2221 /* [namespace.udecl]
2223 If a function declaration in namespace scope or block
2224 scope has the same name and the same parameter types as a
2225 function introduced by a using declaration the program is
2227 for (tmp1 = oldval; tmp1; tmp1 = OVL_NEXT (tmp1))
2229 tree old_fn = OVL_CURRENT (tmp1);
2231 if (new_fn == old_fn)
2232 /* The function already exists in the current namespace. */
2234 else if (OVL_USED (tmp1))
2235 continue; /* this is a using decl */
2236 else if (compparms (TYPE_ARG_TYPES (TREE_TYPE (new_fn)),
2237 TYPE_ARG_TYPES (TREE_TYPE (old_fn))))
2239 /* There was already a non-using declaration in
2240 this scope with the same parameter types. If both
2241 are the same extern "C" functions, that's ok. */
2242 if (decls_match (new_fn, old_fn))
2244 /* If the OLD_FN was a builtin, there is now a
2245 real declaration. */
2246 if (DECL_ANTICIPATED (old_fn))
2247 DECL_ANTICIPATED (old_fn) = 0;
2250 else if (!DECL_ANTICIPATED (old_fn))
2252 /* If the OLD_FN was really declared, the
2253 declarations don't match. */
2254 error ("`%D' is already declared in this scope", name);
2258 /* If the OLD_FN was not really there, just ignore
2259 it and keep going. */
2263 /* If we broke out of the loop, there's no reason to add
2264 this function to the using declarations for this
2269 /* If we are adding to an existing OVERLOAD, then we no
2270 longer know the type of the set of functions. */
2271 if (*newval && TREE_CODE (*newval) == OVERLOAD)
2272 TREE_TYPE (*newval) = unknown_type_node;
2273 /* Add this new function to the set. */
2274 *newval = build_overload (OVL_CURRENT (tmp), *newval);
2275 /* If there is only one function, then we use its type. (A
2276 using-declaration naming a single function can be used in
2277 contexts where overload resolution cannot be
2279 if (TREE_CODE (*newval) != OVERLOAD)
2281 *newval = ovl_cons (*newval, NULL_TREE);
2282 TREE_TYPE (*newval) = TREE_TYPE (OVL_CURRENT (tmp));
2284 OVL_USED (*newval) = 1;
2289 *newval = decls.value;
2290 if (oldval && !decls_match (*newval, oldval))
2291 error ("`%D' is already declared in this scope", name);
2294 *newtype = decls.type;
2295 if (oldtype && *newtype && !same_type_p (oldtype, *newtype))
2297 error ("using declaration `%D' introduced ambiguous type `%T'",
2303 /* Process a using-declaration at function scope. */
2306 do_local_using_decl (tree decl, tree scope, tree name)
2308 tree oldval, oldtype, newval, newtype;
2309 tree orig_decl = decl;
2311 decl = validate_nonmember_using_decl (decl, scope, name);
2312 if (decl == NULL_TREE)
2315 if (building_stmt_tree ()
2316 && at_function_scope_p ())
2317 add_decl_expr (decl);
2319 oldval = lookup_name_current_level (name);
2320 oldtype = lookup_type_current_level (name);
2322 do_nonmember_using_decl (scope, name, oldval, oldtype, &newval, &newtype);
2326 if (is_overloaded_fn (newval))
2330 /* We only need to push declarations for those functions
2331 that were not already bound in the current level.
2332 The old value might be NULL_TREE, it might be a single
2333 function, or an OVERLOAD. */
2334 if (oldval && TREE_CODE (oldval) == OVERLOAD)
2335 term = OVL_FUNCTION (oldval);
2338 for (fn = newval; fn && OVL_CURRENT (fn) != term;
2340 push_overloaded_decl (OVL_CURRENT (fn),
2341 PUSH_LOCAL | PUSH_USING);
2344 push_local_binding (name, newval, PUSH_USING);
2348 push_local_binding (name, newtype, PUSH_USING);
2349 set_identifier_type_value (name, newtype);
2352 /* Emit debug info. */
2353 if (!processing_template_decl)
2354 cp_emit_debug_info_for_using (orig_decl, current_scope());
2357 /* Return the type that should be used when TYPE's name is preceded
2358 by a tag such as 'struct' or 'union', or null if the name cannot
2359 be used in this way.
2361 For example, when processing the third line of:
2367 lookup of A will find the typedef. Given A's typedef, this function
2368 will return the type associated with "struct A". For the tag to be
2369 anything other than TYPE, TYPE must be a typedef whose original type
2370 has the same name and context as TYPE itself.
2372 It is not valid for a typedef of an anonymous type to be used with
2375 typedef struct { ... } B;
2378 Return null for this case. */
2381 follow_tag_typedef (tree type)
2385 original = original_type (type);
2386 if (! TYPE_NAME (original))
2388 if (TYPE_IDENTIFIER (original) == TYPE_IDENTIFIER (type)
2389 && (CP_DECL_CONTEXT (TYPE_NAME (original))
2390 == CP_DECL_CONTEXT (TYPE_NAME (type)))
2391 && !(CLASS_TYPE_P (original) && TYPE_WAS_ANONYMOUS (original)))
2397 /* Given NAME, an IDENTIFIER_NODE,
2398 return the structure (or union or enum) definition for that name.
2399 Searches binding levels from its SCOPE up to the global level.
2400 If THISLEVEL_ONLY is nonzero, searches only the specified context
2401 (but skips any sk_cleanup contexts to find one that is
2402 meaningful for tags).
2403 FORM says which kind of type the caller wants;
2404 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
2405 If the wrong kind of type is found, and it's not a template, an error is
2409 lookup_tag (enum tree_code form, tree name,
2410 cxx_scope *binding_level, int thislevel_only)
2412 struct cp_binding_level *level;
2413 /* Nonzero if, we should look past a template parameter level, even
2414 if THISLEVEL_ONLY. */
2415 int allow_template_parms_p = 1;
2416 bool type_is_anonymous = ANON_AGGRNAME_P (name);
2418 timevar_push (TV_NAME_LOOKUP);
2419 for (level = binding_level; level; level = level->level_chain)
2422 if (type_is_anonymous && level->type_decls != NULL)
2424 tree type = binding_table_find_anon_type (level->type_decls, name);
2425 /* There is no need for error checking here, because
2426 anon names are unique throughout the compilation. */
2428 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, type);
2430 else if (level->kind == sk_namespace)
2431 /* Do namespace lookup. */
2432 for (tail = current_namespace; 1; tail = CP_DECL_CONTEXT (tail))
2434 cxx_binding *binding =
2435 cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (tail), name);
2437 if (binding && (binding->type
2439 && DECL_DECLARES_TYPE_P (binding->value))))
2443 /* If we just skipped past a template parameter level,
2444 even though THISLEVEL_ONLY, and we find a template
2445 class declaration, then we use the _TYPE node for the
2446 template. See the example below. */
2447 if (thislevel_only && !allow_template_parms_p
2449 && DECL_CLASS_TEMPLATE_P (binding->value))
2450 old = binding->value;
2452 old = binding->type ? binding->type : binding->value;
2454 /* We've found something at this binding level. If it is
2455 a typedef, extract the tag it refers to. Lookup fails
2456 if the typedef doesn't refer to a taggable type. */
2457 old = TREE_TYPE (old);
2458 old = follow_tag_typedef (old);
2460 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
2461 if (TREE_CODE (old) != form
2462 && (form == ENUMERAL_TYPE
2463 || TREE_CODE (old) == ENUMERAL_TYPE))
2465 error ("`%#D' redeclared as %C", old, form);
2466 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
2468 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, old);
2470 if (thislevel_only || tail == global_namespace)
2471 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
2473 else if (level->type_decls != NULL)
2475 binding_entry entry = binding_table_find (level->type_decls, name);
2478 enum tree_code code = TREE_CODE (entry->type);
2481 && (form == ENUMERAL_TYPE || code == ENUMERAL_TYPE))
2483 /* Definition isn't the kind we were looking for. */
2484 error ("`%#D' redeclared as %C", entry->type, form);
2485 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
2487 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, entry->type);
2490 if (thislevel_only && level->kind != sk_cleanup)
2492 if (level->kind == sk_template_parms && allow_template_parms_p)
2494 /* We must deal with cases like this:
2496 template <class T> struct S;
2497 template <class T> struct S {};
2499 When looking up `S', for the second declaration, we
2500 would like to find the first declaration. But, we
2501 are in the pseudo-global level created for the
2502 template parameters, rather than the (surrounding)
2503 namespace level. Thus, we keep going one more level,
2504 even though THISLEVEL_ONLY is nonzero. */
2505 allow_template_parms_p = 0;
2509 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
2512 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
2515 /* Given a type, find the tag that was defined for it and return the tag name.
2516 Otherwise return 0. However, the value can never be 0
2517 in the cases in which this is used.
2519 C++: If NAME is nonzero, this is the new name to install. This is
2520 done when replacing anonymous tags with real tag names. */
2523 lookup_tag_reverse (tree type, tree name)
2525 struct cp_binding_level *level;
2527 timevar_push (TV_NAME_LOOKUP);
2528 for (level = current_binding_level; level; level = level->level_chain)
2530 binding_entry entry = level->type_decls == NULL
2532 : binding_table_reverse_maybe_remap (level->type_decls, type, name);
2534 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, entry->name);
2536 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
2539 /* Returns true if ROOT (a namespace, class, or function) encloses
2540 CHILD. CHILD may be either a class type or a namespace. */
2543 is_ancestor (tree root, tree child)
2545 my_friendly_assert ((TREE_CODE (root) == NAMESPACE_DECL
2546 || TREE_CODE (root) == FUNCTION_DECL
2547 || CLASS_TYPE_P (root)), 20030307);
2548 my_friendly_assert ((TREE_CODE (child) == NAMESPACE_DECL
2549 || CLASS_TYPE_P (child)),
2552 /* The global namespace encloses everything. */
2553 if (root == global_namespace)
2558 /* If we've run out of scopes, stop. */
2561 /* If we've reached the ROOT, it encloses CHILD. */
2564 /* Go out one level. */
2566 child = TYPE_NAME (child);
2567 child = DECL_CONTEXT (child);
2571 /* Enter the class or namespace scope indicated by T. Returns TRUE iff
2572 pop_scope should be called later to exit this scope. */
2579 if (TREE_CODE (t) == NAMESPACE_DECL)
2580 push_decl_namespace (t);
2581 else if (CLASS_TYPE_P (t))
2583 if (!at_class_scope_p ()
2584 || !same_type_p (current_class_type, t))
2585 push_nested_class (t);
2587 /* T is the same as the current scope. There is therefore no
2588 need to re-enter the scope. Since we are not actually
2589 pushing a new scope, our caller should not call
2597 /* Leave scope pushed by push_scope. */
2602 if (TREE_CODE (t) == NAMESPACE_DECL)
2603 pop_decl_namespace ();
2604 else if CLASS_TYPE_P (t)
2605 pop_nested_class ();
2608 /* Do a pushlevel for class declarations. */
2611 pushlevel_class (void)
2613 if (ENABLE_SCOPE_CHECKING)
2616 class_binding_level = begin_scope (sk_class, current_class_type);
2619 /* ...and a poplevel for class declarations. */
2622 poplevel_class (void)
2624 struct cp_binding_level *level = class_binding_level;
2625 cp_class_binding *cb;
2629 timevar_push (TV_NAME_LOOKUP);
2630 my_friendly_assert (level != 0, 354);
2632 /* If we're leaving a toplevel class, don't bother to do the setting
2633 of IDENTIFIER_CLASS_VALUE to NULL_TREE, since first of all this slot
2634 shouldn't even be used when current_class_type isn't set, and second,
2635 if we don't touch it here, we're able to use the cache effect if the
2636 next time we're entering a class scope, it is the same class. */
2637 if (current_class_depth != 1)
2639 struct cp_binding_level* b;
2640 cp_class_binding* cb;
2643 /* Clear out our IDENTIFIER_CLASS_VALUEs. */
2644 clear_identifier_class_values ();
2646 /* Find the next enclosing class, and recreate
2647 IDENTIFIER_CLASS_VALUEs appropriate for that class. */
2648 b = level->level_chain;
2649 while (b && b->kind != sk_class)
2654 (cb = VEC_iterate (cp_class_binding,
2659 cxx_binding *binding;
2661 binding = IDENTIFIER_BINDING (cb->identifier);
2662 while (binding && binding->scope != b)
2663 binding = binding->previous;
2666 IDENTIFIER_CLASS_VALUE (cb->identifier) = binding->value;
2670 /* Remember to save what IDENTIFIER's were bound in this scope so we
2671 can recover from cache misses. */
2672 previous_class_level = level;
2673 for (shadowed = level->type_shadowed;
2675 shadowed = TREE_CHAIN (shadowed))
2676 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (shadowed), TREE_VALUE (shadowed));
2678 /* Remove the bindings for all of the class-level declarations. */
2680 (cb = VEC_iterate (cp_class_binding, level->class_shadowed, i));
2682 IDENTIFIER_BINDING (cb->identifier) = cb->base.previous;
2684 /* Now, pop out of the binding level which we created up in the
2685 `pushlevel_class' routine. */
2686 if (ENABLE_SCOPE_CHECKING)
2690 timevar_pop (TV_NAME_LOOKUP);
2693 /* Bind DECL to ID in the class_binding_level. Returns nonzero if the
2694 binding was successful. */
2697 push_class_binding (tree id, tree decl)
2700 cxx_binding *binding = IDENTIFIER_BINDING (id);
2703 timevar_push (TV_NAME_LOOKUP);
2704 /* Note that we declared this value so that we can issue an error if
2705 this is an invalid redeclaration of a name already used for some
2707 note_name_declared_in_class (id, decl);
2709 if (binding && binding->scope == class_binding_level)
2710 /* Supplement the existing binding. */
2711 result = supplement_binding (IDENTIFIER_BINDING (id), decl);
2713 /* Create a new binding. */
2714 push_binding (id, decl, class_binding_level);
2716 /* Update the IDENTIFIER_CLASS_VALUE for this ID to be the
2717 class-level declaration. Note that we do not use DECL here
2718 because of the possibility of the `struct stat' hack; if DECL is
2719 a class-name or enum-name we might prefer a field-name, or some
2721 IDENTIFIER_CLASS_VALUE (id) = IDENTIFIER_BINDING (id)->value;
2723 /* If this is a binding from a base class, mark it as such. */
2724 binding = IDENTIFIER_BINDING (id);
2725 if (binding->value == decl && TREE_CODE (decl) != TREE_LIST)
2727 if (TREE_CODE (decl) == OVERLOAD)
2728 context = CP_DECL_CONTEXT (OVL_CURRENT (decl));
2731 my_friendly_assert (DECL_P (decl), 0);
2732 context = context_for_name_lookup (decl);
2735 if (is_properly_derived_from (current_class_type, context))
2736 INHERITED_VALUE_BINDING_P (binding) = 1;
2738 INHERITED_VALUE_BINDING_P (binding) = 0;
2740 else if (binding->value == decl)
2741 /* We only encounter a TREE_LIST when push_class_decls detects an
2742 ambiguity. Such an ambiguity can be overridden by a definition
2744 INHERITED_VALUE_BINDING_P (binding) = 1;
2746 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, result);
2749 /* We are entering the scope of a class. Clear IDENTIFIER_CLASS_VALUE
2750 for any names in enclosing classes. */
2753 clear_identifier_class_values (void)
2756 cp_class_binding *cb;
2758 if (class_binding_level)
2760 (cb = VEC_iterate (cp_class_binding,
2761 class_binding_level->class_shadowed,
2764 IDENTIFIER_CLASS_VALUE (cb->identifier) = NULL_TREE;
2767 /* Make the declaration of X appear in CLASS scope. */
2770 pushdecl_class_level (tree x)
2773 bool is_valid = true;
2775 timevar_push (TV_NAME_LOOKUP);
2776 /* Get the name of X. */
2777 if (TREE_CODE (x) == OVERLOAD)
2778 name = DECL_NAME (get_first_fn (x));
2780 name = DECL_NAME (x);
2784 is_valid = push_class_level_binding (name, x);
2785 if (TREE_CODE (x) == TYPE_DECL)
2786 set_identifier_type_value (name, x);
2788 else if (ANON_AGGR_TYPE_P (TREE_TYPE (x)))
2790 /* If X is an anonymous aggregate, all of its members are
2791 treated as if they were members of the class containing the
2792 aggregate, for naming purposes. */
2795 for (f = TYPE_FIELDS (TREE_TYPE (x)); f; f = TREE_CHAIN (f))
2797 location_t save_location = input_location;
2798 input_location = DECL_SOURCE_LOCATION (f);
2799 if (!pushdecl_class_level (f))
2801 input_location = save_location;
2804 timevar_pop (TV_NAME_LOOKUP);
2809 /* Make the declaration(s) of X appear in CLASS scope under the name
2810 NAME. Returns true if the binding is valid. */
2813 push_class_level_binding (tree name, tree x)
2815 cxx_binding *binding;
2817 timevar_push (TV_NAME_LOOKUP);
2818 /* The class_binding_level will be NULL if x is a template
2819 parameter name in a member template. */
2820 if (!class_binding_level)
2821 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, true);
2823 /* Make sure that this new member does not have the same name
2824 as a template parameter. */
2825 if (TYPE_BEING_DEFINED (current_class_type))
2829 /* We could have been passed a tree list if this is an ambiguous
2830 declaration. If so, pull the declaration out because
2831 check_template_shadow will not handle a TREE_LIST. */
2832 if (TREE_CODE (decl) == TREE_LIST
2833 && TREE_TYPE (decl) == error_mark_node)
2834 decl = TREE_VALUE (decl);
2836 check_template_shadow (decl);
2841 If T is the name of a class, then each of the following shall
2842 have a name different from T:
2844 -- every static data member of class T;
2846 -- every member of class T that is itself a type;
2848 -- every enumerator of every member of class T that is an
2851 -- every member of every anonymous union that is a member of
2854 (Non-static data members were also forbidden to have the same
2855 name as T until TC1.) */
2856 if ((TREE_CODE (x) == VAR_DECL
2857 || TREE_CODE (x) == CONST_DECL
2858 || (TREE_CODE (x) == TYPE_DECL
2859 && !DECL_SELF_REFERENCE_P (x))
2860 /* A data member of an anonymous union. */
2861 || (TREE_CODE (x) == FIELD_DECL
2862 && DECL_CONTEXT (x) != current_class_type))
2863 && DECL_NAME (x) == constructor_name (current_class_type))
2865 tree scope = context_for_name_lookup (x);
2866 if (TYPE_P (scope) && same_type_p (scope, current_class_type))
2868 error ("`%D' has the same name as the class in which it is declared",
2870 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, false);
2874 /* If this declaration shadows a declaration from an enclosing
2875 class, then we will need to restore IDENTIFIER_CLASS_VALUE when
2876 we leave this class. Record the shadowed declaration here. */
2877 binding = IDENTIFIER_BINDING (name);
2878 if (binding && binding->value)
2880 tree bval = binding->value;
2881 tree old_decl = NULL_TREE;
2883 if (INHERITED_VALUE_BINDING_P (binding))
2885 /* If the old binding was from a base class, and was for a
2886 tag name, slide it over to make room for the new binding.
2887 The old binding is still visible if explicitly qualified
2888 with a class-key. */
2889 if (TREE_CODE (bval) == TYPE_DECL && DECL_ARTIFICIAL (bval)
2890 && !(TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x)))
2892 old_decl = binding->type;
2893 binding->type = bval;
2894 binding->value = NULL_TREE;
2895 INHERITED_VALUE_BINDING_P (binding) = 0;
2900 else if (TREE_CODE (x) == OVERLOAD && is_overloaded_fn (bval))
2902 else if (TREE_CODE (x) == USING_DECL && TREE_CODE (bval) == USING_DECL)
2903 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, true);
2904 else if (TREE_CODE (x) == USING_DECL && is_overloaded_fn (bval))
2906 else if (TREE_CODE (bval) == USING_DECL && is_overloaded_fn (x))
2907 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, true);
2911 cp_class_binding *cb;
2914 /* Find the previous binding of name on the class-shadowed
2915 list, and update it. */
2917 (cb = VEC_iterate (cp_class_binding,
2918 class_binding_level->class_shadowed,
2921 if (cb->identifier == name
2922 && (cb->base.value == old_decl
2923 || cb->base.type == old_decl))
2926 INHERITED_VALUE_BINDING_P (binding) = 0;
2927 IDENTIFIER_CLASS_VALUE (name) = x;
2928 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, true);
2933 /* If we didn't replace an existing binding, put the binding on the
2934 stack of bindings for the identifier, and update the shadowed list. */
2935 if (push_class_binding (name, x))
2936 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, true);
2938 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, false);
2942 do_class_using_decl (tree decl)
2944 tree name, value, scope, type;
2946 if (TREE_CODE (decl) != SCOPE_REF
2947 || !TREE_OPERAND (decl, 0)
2948 || !TYPE_P (TREE_OPERAND (decl, 0)))
2950 error ("using-declaration for non-member at class scope");
2953 scope = TREE_OPERAND (decl, 0);
2954 name = TREE_OPERAND (decl, 1);
2955 if (TREE_CODE (name) == BIT_NOT_EXPR)
2957 error ("using-declaration cannot name destructor");
2960 if (TREE_CODE (name) == TYPE_DECL)
2961 name = DECL_NAME (name);
2962 else if (TREE_CODE (name) == TEMPLATE_DECL)
2963 name = DECL_NAME (name);
2964 else if (BASELINK_P (name))
2966 tree fns = BASELINK_FUNCTIONS (name);
2967 name = DECL_NAME (get_first_fn (fns));
2970 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 980716);
2972 /* Dependent using decls have a NULL type, non-dependent ones have a
2974 type = dependent_type_p (scope) ? NULL_TREE : void_type_node;
2975 value = build_lang_decl (USING_DECL, name, type);
2976 DECL_INITIAL (value) = scope;
2978 if (scope && !processing_template_decl)
2982 r = lookup_qualified_name (scope, name, false, false);
2983 if (r && TREE_CODE (r) != ERROR_MARK)
2984 cp_emit_debug_info_for_using (r, scope);
2990 /* Return the binding value for name in scope. */
2993 namespace_binding (tree name, tree scope)
2995 cxx_binding *binding;
2998 scope = global_namespace;
2999 scope = ORIGINAL_NAMESPACE (scope);
3000 binding = cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (scope), name);
3002 return binding ? binding->value : NULL_TREE;
3005 /* Set the binding value for name in scope. */
3008 set_namespace_binding (tree name, tree scope, tree val)
3012 timevar_push (TV_NAME_LOOKUP);
3013 if (scope == NULL_TREE)
3014 scope = global_namespace;
3015 b = binding_for_name (NAMESPACE_LEVEL (scope), name);
3016 if (!b->value || TREE_CODE (val) == OVERLOAD || val == error_mark_node)
3019 supplement_binding (b, val);
3020 timevar_pop (TV_NAME_LOOKUP);
3023 /* Compute the namespace where a declaration is defined. */
3026 decl_namespace (tree decl)
3028 timevar_push (TV_NAME_LOOKUP);
3030 decl = TYPE_STUB_DECL (decl);
3031 while (DECL_CONTEXT (decl))
3033 decl = DECL_CONTEXT (decl);
3034 if (TREE_CODE (decl) == NAMESPACE_DECL)
3035 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
3037 decl = TYPE_STUB_DECL (decl);
3038 my_friendly_assert (DECL_P (decl), 390);
3041 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, global_namespace);
3044 /* Set the context of a declaration to scope. Complain if we are not
3048 set_decl_namespace (tree decl, tree scope, bool friendp)
3052 /* Get rid of namespace aliases. */
3053 scope = ORIGINAL_NAMESPACE (scope);
3055 /* It is ok for friends to be qualified in parallel space. */
3056 if (!friendp && !is_ancestor (current_namespace, scope))
3057 error ("declaration of `%D' not in a namespace surrounding `%D'",
3059 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
3060 if (scope != current_namespace)
3062 /* See whether this has been declared in the namespace. */
3063 old = namespace_binding (DECL_NAME (decl), scope);
3065 /* No old declaration at all. */
3067 /* A template can be explicitly specialized in any namespace. */
3068 if (processing_explicit_instantiation)
3070 if (!is_overloaded_fn (decl))
3071 /* Don't compare non-function decls with decls_match here,
3072 since it can't check for the correct constness at this
3073 point. pushdecl will find those errors later. */
3075 /* Since decl is a function, old should contain a function decl. */
3076 if (!is_overloaded_fn (old))
3078 if (processing_template_decl || processing_specialization)
3079 /* We have not yet called push_template_decl to turn a
3080 FUNCTION_DECL into a TEMPLATE_DECL, so the declarations
3081 won't match. But, we'll check later, when we construct the
3084 if (is_overloaded_fn (old))
3086 for (; old; old = OVL_NEXT (old))
3087 if (decls_match (decl, OVL_CURRENT (old)))
3091 if (decls_match (decl, old))
3097 error ("`%D' should have been declared inside `%D'",
3101 /* Return the namespace where the current declaration is declared. */
3104 current_decl_namespace (void)
3107 /* If we have been pushed into a different namespace, use it. */
3108 if (decl_namespace_list)
3109 return TREE_PURPOSE (decl_namespace_list);
3111 if (current_class_type)
3112 result = decl_namespace (TYPE_STUB_DECL (current_class_type));
3113 else if (current_function_decl)
3114 result = decl_namespace (current_function_decl);
3116 result = current_namespace;
3120 /* Push into the scope of the NAME namespace. If NAME is NULL_TREE, then we
3121 select a name that is unique to this compilation unit. */
3124 push_namespace (tree name)
3128 int implicit_use = 0;
3131 timevar_push (TV_NAME_LOOKUP);
3133 /* We should not get here if the global_namespace is not yet constructed
3134 nor if NAME designates the global namespace: The global scope is
3135 constructed elsewhere. */
3136 my_friendly_assert (global_namespace != NULL && name != global_scope_name,
3141 /* The name of anonymous namespace is unique for the translation
3143 if (!anonymous_namespace_name)
3144 anonymous_namespace_name = get_file_function_name ('N');
3145 name = anonymous_namespace_name;
3146 d = IDENTIFIER_NAMESPACE_VALUE (name);
3148 /* Reopening anonymous namespace. */
3154 /* Check whether this is an extended namespace definition. */
3155 d = IDENTIFIER_NAMESPACE_VALUE (name);
3156 if (d != NULL_TREE && TREE_CODE (d) == NAMESPACE_DECL)
3159 if (DECL_NAMESPACE_ALIAS (d))
3161 error ("namespace alias `%D' not allowed here, assuming `%D'",
3162 d, DECL_NAMESPACE_ALIAS (d));
3163 d = DECL_NAMESPACE_ALIAS (d);
3170 /* Make a new namespace, binding the name to it. */
3171 d = build_lang_decl (NAMESPACE_DECL, name, void_type_node);
3172 DECL_CONTEXT (d) = FROB_CONTEXT (current_namespace);
3176 /* Clear DECL_NAME for the benefit of debugging back ends. */
3177 SET_DECL_ASSEMBLER_NAME (d, name);
3178 DECL_NAME (d) = NULL_TREE;
3180 begin_scope (sk_namespace, d);
3183 resume_scope (NAMESPACE_LEVEL (d));
3186 do_using_directive (d);
3187 /* Enter the name space. */
3188 current_namespace = d;
3190 timevar_pop (TV_NAME_LOOKUP);
3193 /* Pop from the scope of the current namespace. */
3196 pop_namespace (void)
3198 my_friendly_assert (current_namespace != global_namespace, 20010801);
3199 current_namespace = CP_DECL_CONTEXT (current_namespace);
3200 /* The binding level is not popped, as it might be re-opened later. */
3204 /* Push into the scope of the namespace NS, even if it is deeply
3205 nested within another namespace. */
3208 push_nested_namespace (tree ns)
3210 if (ns == global_namespace)
3211 push_to_top_level ();
3214 push_nested_namespace (CP_DECL_CONTEXT (ns));
3215 push_namespace (DECL_NAME (ns));
3219 /* Pop back from the scope of the namespace NS, which was previously
3220 entered with push_nested_namespace. */
3223 pop_nested_namespace (tree ns)
3225 timevar_push (TV_NAME_LOOKUP);
3226 while (ns != global_namespace)
3229 ns = CP_DECL_CONTEXT (ns);
3232 pop_from_top_level ();
3233 timevar_pop (TV_NAME_LOOKUP);
3236 /* Temporarily set the namespace for the current declaration. */
3239 push_decl_namespace (tree decl)
3241 if (TREE_CODE (decl) != NAMESPACE_DECL)
3242 decl = decl_namespace (decl);
3243 decl_namespace_list = tree_cons (ORIGINAL_NAMESPACE (decl),
3244 NULL_TREE, decl_namespace_list);
3247 /* [namespace.memdef]/2 */
3250 pop_decl_namespace (void)
3252 decl_namespace_list = TREE_CHAIN (decl_namespace_list);
3255 /* Return the namespace that is the common ancestor
3256 of two given namespaces. */
3259 namespace_ancestor (tree ns1, tree ns2)
3261 timevar_push (TV_NAME_LOOKUP);
3262 if (is_ancestor (ns1, ns2))
3263 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, ns1);
3264 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP,
3265 namespace_ancestor (CP_DECL_CONTEXT (ns1), ns2));
3268 /* Process a namespace-alias declaration. */
3271 do_namespace_alias (tree alias, tree namespace)
3273 if (TREE_CODE (namespace) != NAMESPACE_DECL)
3275 /* The parser did not find it, so it's not there. */
3276 error ("unknown namespace `%D'", namespace);
3280 namespace = ORIGINAL_NAMESPACE (namespace);
3282 /* Build the alias. */
3283 alias = build_lang_decl (NAMESPACE_DECL, alias, void_type_node);
3284 DECL_NAMESPACE_ALIAS (alias) = namespace;
3285 DECL_EXTERNAL (alias) = 1;
3288 /* Emit debug info for namespace alias. */
3289 (*debug_hooks->global_decl) (alias);
3292 /* Like pushdecl, only it places X in the current namespace,
3296 pushdecl_namespace_level (tree x)
3298 struct cp_binding_level *b = current_binding_level;
3301 timevar_push (TV_NAME_LOOKUP);
3302 t = pushdecl_with_scope (x, NAMESPACE_LEVEL (current_namespace));
3304 /* Now, the type_shadowed stack may screw us. Munge it so it does
3306 if (TREE_CODE (x) == TYPE_DECL)
3308 tree name = DECL_NAME (x);
3310 tree *ptr = (tree *)0;
3311 for (; !global_scope_p (b); b = b->level_chain)
3313 tree shadowed = b->type_shadowed;
3314 for (; shadowed; shadowed = TREE_CHAIN (shadowed))
3315 if (TREE_PURPOSE (shadowed) == name)
3317 ptr = &TREE_VALUE (shadowed);
3318 /* Can't break out of the loop here because sometimes
3319 a binding level will have duplicate bindings for
3320 PT names. It's gross, but I haven't time to fix it. */
3323 newval = TREE_TYPE (x);
3324 if (ptr == (tree *)0)
3326 /* @@ This shouldn't be needed. My test case "zstring.cc" trips
3327 up here if this is changed to an assertion. --KR */
3328 SET_IDENTIFIER_TYPE_VALUE (name, x);
3335 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
3338 /* Insert USED into the using list of USER. Set INDIRECT_flag if this
3339 directive is not directly from the source. Also find the common
3340 ancestor and let our users know about the new namespace */
3342 add_using_namespace (tree user, tree used, bool indirect)
3345 timevar_push (TV_NAME_LOOKUP);
3346 /* Using oneself is a no-op. */
3349 timevar_pop (TV_NAME_LOOKUP);
3352 my_friendly_assert (TREE_CODE (user) == NAMESPACE_DECL, 380);
3353 my_friendly_assert (TREE_CODE (used) == NAMESPACE_DECL, 380);
3354 /* Check if we already have this. */
3355 t = purpose_member (used, DECL_NAMESPACE_USING (user));
3359 /* Promote to direct usage. */
3360 TREE_INDIRECT_USING (t) = 0;
3361 timevar_pop (TV_NAME_LOOKUP);
3365 /* Add used to the user's using list. */
3366 DECL_NAMESPACE_USING (user)
3367 = tree_cons (used, namespace_ancestor (user, used),
3368 DECL_NAMESPACE_USING (user));
3370 TREE_INDIRECT_USING (DECL_NAMESPACE_USING (user)) = indirect;
3372 /* Add user to the used's users list. */
3373 DECL_NAMESPACE_USERS (used)
3374 = tree_cons (user, 0, DECL_NAMESPACE_USERS (used));
3376 /* Recursively add all namespaces used. */
3377 for (t = DECL_NAMESPACE_USING (used); t; t = TREE_CHAIN (t))
3378 /* indirect usage */
3379 add_using_namespace (user, TREE_PURPOSE (t), 1);
3381 /* Tell everyone using us about the new used namespaces. */
3382 for (t = DECL_NAMESPACE_USERS (user); t; t = TREE_CHAIN (t))
3383 add_using_namespace (TREE_PURPOSE (t), used, 1);
3384 timevar_pop (TV_NAME_LOOKUP);
3387 /* Process a using-declaration not appearing in class or local scope. */
3390 do_toplevel_using_decl (tree decl, tree scope, tree name)
3392 tree oldval, oldtype, newval, newtype;
3393 tree orig_decl = decl;
3394 cxx_binding *binding;
3396 decl = validate_nonmember_using_decl (decl, scope, name);
3397 if (decl == NULL_TREE)
3400 binding = binding_for_name (NAMESPACE_LEVEL (current_namespace), name);
3402 oldval = binding->value;
3403 oldtype = binding->type;
3405 do_nonmember_using_decl (scope, name, oldval, oldtype, &newval, &newtype);
3407 /* Emit debug info. */
3408 if (!processing_template_decl)
3409 cp_emit_debug_info_for_using (orig_decl, current_namespace);
3411 /* Copy declarations found. */
3413 binding->value = newval;
3415 binding->type = newtype;
3419 /* Process a using-directive. */
3422 do_using_directive (tree namespace)
3424 tree context = NULL_TREE;
3426 if (building_stmt_tree ())
3427 add_stmt (build_stmt (USING_STMT, namespace));
3429 /* using namespace A::B::C; */
3430 if (TREE_CODE (namespace) == SCOPE_REF)
3431 namespace = TREE_OPERAND (namespace, 1);
3432 if (TREE_CODE (namespace) == IDENTIFIER_NODE)
3434 /* Lookup in lexer did not find a namespace. */
3435 if (!processing_template_decl)
3436 error ("namespace `%T' undeclared", namespace);
3439 if (TREE_CODE (namespace) != NAMESPACE_DECL)
3441 if (!processing_template_decl)
3442 error ("`%T' is not a namespace", namespace);
3445 namespace = ORIGINAL_NAMESPACE (namespace);
3446 if (!toplevel_bindings_p ())
3448 push_using_directive (namespace);
3449 context = current_scope ();
3454 add_using_namespace (current_namespace, namespace, 0);
3455 if (current_namespace != global_namespace)
3456 context = current_namespace;
3459 /* Emit debugging info. */
3460 if (!processing_template_decl)
3461 (*debug_hooks->imported_module_or_decl) (namespace, context);
3464 /* Deal with a using-directive seen by the parser. Currently we only
3465 handle attributes here, since they cannot appear inside a template. */
3468 parse_using_directive (tree namespace, tree attribs)
3472 do_using_directive (namespace);
3474 for (a = attribs; a; a = TREE_CHAIN (a))
3476 tree name = TREE_PURPOSE (a);
3477 if (is_attribute_p ("strong", name))
3479 if (!toplevel_bindings_p ())
3480 error ("strong using only meaningful at namespace scope");
3482 DECL_NAMESPACE_ASSOCIATIONS (namespace)
3483 = tree_cons (current_namespace, 0,
3484 DECL_NAMESPACE_ASSOCIATIONS (namespace));
3487 warning ("`%D' attribute directive ignored", name);
3491 /* Like pushdecl, only it places X in the global scope if appropriate.
3492 Calls cp_finish_decl to register the variable, initializing it with
3493 *INIT, if INIT is non-NULL. */
3496 pushdecl_top_level_1 (tree x, tree *init)
3498 timevar_push (TV_NAME_LOOKUP);
3499 push_to_top_level ();
3500 x = pushdecl_namespace_level (x);
3502 cp_finish_decl (x, *init, NULL_TREE, 0);
3503 pop_from_top_level ();
3504 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
3507 /* Like pushdecl, only it places X in the global scope if appropriate. */
3510 pushdecl_top_level (tree x)
3512 return pushdecl_top_level_1 (x, NULL);
3515 /* Like pushdecl, only it places X in the global scope if
3516 appropriate. Calls cp_finish_decl to register the variable,
3517 initializing it with INIT. */
3520 pushdecl_top_level_and_finish (tree x, tree init)
3522 return pushdecl_top_level_1 (x, &init);
3525 /* Combines two sets of overloaded functions into an OVERLOAD chain, removing
3526 duplicates. The first list becomes the tail of the result.
3528 The algorithm is O(n^2). We could get this down to O(n log n) by
3529 doing a sort on the addresses of the functions, if that becomes
3533 merge_functions (tree s1, tree s2)
3535 for (; s2; s2 = OVL_NEXT (s2))
3537 tree fn2 = OVL_CURRENT (s2);
3540 for (fns1 = s1; fns1; fns1 = OVL_NEXT (fns1))
3542 tree fn1 = OVL_CURRENT (fns1);
3544 /* If the function from S2 is already in S1, there is no
3545 need to add it again. For `extern "C"' functions, we
3546 might have two FUNCTION_DECLs for the same function, in
3547 different namespaces; again, we only need one of them. */
3549 || (DECL_EXTERN_C_P (fn1) && DECL_EXTERN_C_P (fn2)
3550 && DECL_NAME (fn1) == DECL_NAME (fn2)))
3554 /* If we exhausted all of the functions in S1, FN2 is new. */
3556 s1 = build_overload (fn2, s1);
3561 /* This should return an error not all definitions define functions.
3562 It is not an error if we find two functions with exactly the
3563 same signature, only if these are selected in overload resolution.
3564 old is the current set of bindings, new the freshly-found binding.
3565 XXX Do we want to give *all* candidates in case of ambiguity?
3566 XXX In what way should I treat extern declarations?
3567 XXX I don't want to repeat the entire duplicate_decls here */
3570 ambiguous_decl (tree name, struct scope_binding *old, cxx_binding *new,
3574 my_friendly_assert (old != NULL, 393);
3575 /* Copy the value. */
3578 switch (TREE_CODE (val))
3581 /* If we expect types or namespaces, and not templates,
3582 or this is not a template class. */
3583 if (LOOKUP_QUALIFIERS_ONLY (flags)
3584 && !DECL_CLASS_TEMPLATE_P (val))
3588 if (LOOKUP_NAMESPACES_ONLY (flags))
3591 case NAMESPACE_DECL:
3592 if (LOOKUP_TYPES_ONLY (flags))
3596 /* Ignore built-in functions that are still anticipated. */
3597 if (LOOKUP_QUALIFIERS_ONLY (flags) || DECL_ANTICIPATED (val))
3601 if (LOOKUP_QUALIFIERS_ONLY (flags))
3607 else if (val && val != old->value)
3609 if (is_overloaded_fn (old->value) && is_overloaded_fn (val))
3610 old->value = merge_functions (old->value, val);
3613 /* Some declarations are functions, some are not. */
3614 if (flags & LOOKUP_COMPLAIN)
3616 /* If we've already given this error for this lookup,
3617 old->value is error_mark_node, so let's not
3618 repeat ourselves. */
3619 if (old->value != error_mark_node)
3621 error ("use of `%D' is ambiguous", name);
3622 cp_error_at (" first declared as `%#D' here",
3625 cp_error_at (" also declared as `%#D' here", val);
3627 old->value = error_mark_node;
3630 /* ... and copy the type. */
3632 if (LOOKUP_NAMESPACES_ONLY (flags))
3636 else if (type && old->type != type)
3638 if (flags & LOOKUP_COMPLAIN)
3640 error ("`%D' denotes an ambiguous type",name);
3641 error ("%J first type here", TYPE_MAIN_DECL (old->type));
3642 error ("%J other type here", TYPE_MAIN_DECL (type));
3647 /* Return the declarations that are members of the namespace NS. */
3650 cp_namespace_decls (tree ns)
3652 return NAMESPACE_LEVEL (ns)->names;
3655 /* Combine prefer_type and namespaces_only into flags. */
3658 lookup_flags (int prefer_type, int namespaces_only)
3660 if (namespaces_only)
3661 return LOOKUP_PREFER_NAMESPACES;
3662 if (prefer_type > 1)
3663 return LOOKUP_PREFER_TYPES;
3664 if (prefer_type > 0)
3665 return LOOKUP_PREFER_BOTH;
3669 /* Given a lookup that returned VAL, use FLAGS to decide if we want to
3670 ignore it or not. Subroutine of lookup_name_real. */
3673 qualify_lookup (tree val, int flags)
3675 if (val == NULL_TREE)
3677 if ((flags & LOOKUP_PREFER_NAMESPACES) && TREE_CODE (val) == NAMESPACE_DECL)
3679 if ((flags & LOOKUP_PREFER_TYPES)
3680 && (TREE_CODE (val) == TYPE_DECL || TREE_CODE (val) == TEMPLATE_DECL))
3682 if (flags & (LOOKUP_PREFER_NAMESPACES | LOOKUP_PREFER_TYPES))
3687 /* Look up NAME in the NAMESPACE. */
3690 lookup_namespace_name (tree namespace, tree name)
3693 tree template_id = NULL_TREE;
3694 struct scope_binding binding = EMPTY_SCOPE_BINDING;
3696 timevar_push (TV_NAME_LOOKUP);
3697 my_friendly_assert (TREE_CODE (namespace) == NAMESPACE_DECL, 370);
3699 if (TREE_CODE (name) == NAMESPACE_DECL)
3700 /* This happens for A::B<int> when B is a namespace. */
3701 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, name);
3702 else if (TREE_CODE (name) == TEMPLATE_DECL)
3704 /* This happens for A::B where B is a template, and there are no
3705 template arguments. */
3706 error ("invalid use of `%D'", name);
3707 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
3710 namespace = ORIGINAL_NAMESPACE (namespace);
3712 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
3715 name = TREE_OPERAND (name, 0);
3716 if (TREE_CODE (name) == OVERLOAD)
3717 name = DECL_NAME (OVL_CURRENT (name));
3718 else if (DECL_P (name))
3719 name = DECL_NAME (name);
3722 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 373);
3724 if (!qualified_lookup_using_namespace (name, namespace, &binding, 0))
3725 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
3729 val = binding.value;
3733 if (DECL_CLASS_TEMPLATE_P (val))
3734 val = lookup_template_class (val,
3735 TREE_OPERAND (template_id, 1),
3736 /*in_decl=*/NULL_TREE,
3737 /*context=*/NULL_TREE,
3738 /*entering_scope=*/0,
3739 tf_error | tf_warning);
3740 else if (DECL_FUNCTION_TEMPLATE_P (val)
3741 || TREE_CODE (val) == OVERLOAD)
3742 val = lookup_template_function (val,
3743 TREE_OPERAND (template_id, 1));
3746 error ("`%D::%D' is not a template",
3748 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
3752 /* If we have a single function from a using decl, pull it out. */
3753 if (TREE_CODE (val) == OVERLOAD && ! really_overloaded_fn (val))
3754 val = OVL_FUNCTION (val);
3756 /* Ignore built-in functions that haven't been prototyped yet. */
3757 if (!val || !DECL_P(val)
3758 || !DECL_LANG_SPECIFIC(val)
3759 || !DECL_ANTICIPATED (val))
3760 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
3763 error ("`%D' undeclared in namespace `%D'", name, namespace);
3764 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
3767 /* Select the right _DECL from multiple choices. */
3770 select_decl (const struct scope_binding *binding, int flags)
3773 val = binding->value;
3775 timevar_push (TV_NAME_LOOKUP);
3776 if (LOOKUP_NAMESPACES_ONLY (flags))
3778 /* We are not interested in types. */
3779 if (val && TREE_CODE (val) == NAMESPACE_DECL)
3780 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
3781 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
3784 /* If looking for a type, or if there is no non-type binding, select
3785 the value binding. */
3786 if (binding->type && (!val || (flags & LOOKUP_PREFER_TYPES)))
3787 val = binding->type;
3788 /* Don't return non-types if we really prefer types. */
3789 else if (val && LOOKUP_TYPES_ONLY (flags)
3790 && ! DECL_DECLARES_TYPE_P (val))
3793 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
3796 /* Unscoped lookup of a global: iterate over current namespaces,
3797 considering using-directives. */
3800 unqualified_namespace_lookup (tree name, int flags)
3802 tree initial = current_decl_namespace ();
3803 tree scope = initial;
3805 struct cp_binding_level *level;
3806 tree val = NULL_TREE;
3807 struct scope_binding binding = EMPTY_SCOPE_BINDING;
3809 timevar_push (TV_NAME_LOOKUP);
3811 for (; !val; scope = CP_DECL_CONTEXT (scope))
3814 cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (scope), name);
3818 if (b->value && DECL_P (b->value)
3819 && DECL_LANG_SPECIFIC (b->value)
3820 && DECL_ANTICIPATED (b->value))
3821 /* Ignore anticipated built-in functions. */
3824 binding.value = b->value;
3825 binding.type = b->type;
3828 /* Add all _DECLs seen through local using-directives. */
3829 for (level = current_binding_level;
3830 level->kind != sk_namespace;
3831 level = level->level_chain)
3832 if (!lookup_using_namespace (name, &binding, level->using_directives,
3834 /* Give up because of error. */
3835 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
3837 /* Add all _DECLs seen through global using-directives. */
3838 /* XXX local and global using lists should work equally. */
3842 if (!lookup_using_namespace (name, &binding,
3843 DECL_NAMESPACE_USING (siter),
3845 /* Give up because of error. */
3846 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
3847 if (siter == scope) break;
3848 siter = CP_DECL_CONTEXT (siter);
3851 val = select_decl (&binding, flags);
3852 if (scope == global_namespace)
3855 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
3858 /* Look up NAME (an IDENTIFIER_NODE) in SCOPE (either a NAMESPACE_DECL
3859 or a class TYPE). If IS_TYPE_P is TRUE, then ignore non-type
3862 Returns a DECL (or OVERLOAD, or BASELINK) representing the
3863 declaration found. If no suitable declaration can be found,
3864 ERROR_MARK_NODE is returned. Iif COMPLAIN is true and SCOPE is
3865 neither a class-type nor a namespace a diagnostic is issued. */
3868 lookup_qualified_name (tree scope, tree name, bool is_type_p, bool complain)
3872 if (TREE_CODE (scope) == NAMESPACE_DECL)
3874 struct scope_binding binding = EMPTY_SCOPE_BINDING;
3876 flags |= LOOKUP_COMPLAIN;
3878 flags |= LOOKUP_PREFER_TYPES;
3879 if (qualified_lookup_using_namespace (name, scope, &binding, flags))
3880 return select_decl (&binding, flags);
3882 else if (is_aggr_type (scope, complain))
3885 t = lookup_member (scope, name, 0, is_type_p);
3890 return error_mark_node;
3893 /* Subroutine of unqualified_namespace_lookup:
3894 Add the bindings of NAME in used namespaces to VAL.
3895 We are currently looking for names in namespace SCOPE, so we
3896 look through USINGS for using-directives of namespaces
3897 which have SCOPE as a common ancestor with the current scope.
3898 Returns false on errors. */
3901 lookup_using_namespace (tree name, struct scope_binding *val,
3902 tree usings, tree scope, int flags)
3905 timevar_push (TV_NAME_LOOKUP);
3906 /* Iterate over all used namespaces in current, searching for using
3907 directives of scope. */
3908 for (iter = usings; iter; iter = TREE_CHAIN (iter))
3909 if (TREE_VALUE (iter) == scope)
3911 tree used = ORIGINAL_NAMESPACE (TREE_PURPOSE (iter));
3913 cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (used), name);
3914 /* Resolve ambiguities. */
3916 ambiguous_decl (name, val, val1, flags);
3918 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val->value != error_mark_node);
3922 Accepts the NAME to lookup and its qualifying SCOPE.
3923 Returns the name/type pair found into the cxx_binding *RESULT,
3924 or false on error. */
3927 qualified_lookup_using_namespace (tree name, tree scope,
3928 struct scope_binding *result, int flags)
3930 /* Maintain a list of namespaces visited... */
3931 tree seen = NULL_TREE;
3932 /* ... and a list of namespace yet to see. */
3933 tree todo = NULL_TREE;
3934 tree todo_maybe = NULL_TREE;
3936 timevar_push (TV_NAME_LOOKUP);
3937 /* Look through namespace aliases. */
3938 scope = ORIGINAL_NAMESPACE (scope);
3939 while (scope && result->value != error_mark_node)
3941 cxx_binding *binding =
3942 cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (scope), name);
3943 seen = tree_cons (scope, NULL_TREE, seen);
3945 ambiguous_decl (name, result, binding, flags);
3947 /* Consider strong using directives always, and non-strong ones
3948 if we haven't found a binding yet. ??? Shouldn't we consider
3949 non-strong ones if the initial RESULT is non-NULL, but the
3950 binding in the given namespace is? */
3951 for (usings = DECL_NAMESPACE_USING (scope); usings;
3952 usings = TREE_CHAIN (usings))
3953 /* If this was a real directive, and we have not seen it. */
3954 if (!TREE_INDIRECT_USING (usings))
3956 /* Try to avoid queuing the same namespace more than once,
3957 the exception being when a namespace was already
3958 enqueued for todo_maybe and then a strong using is
3959 found for it. We could try to remove it from
3960 todo_maybe, but it's probably not worth the effort. */
3961 if (is_associated_namespace (scope, TREE_PURPOSE (usings))
3962 && !purpose_member (TREE_PURPOSE (usings), seen)
3963 && !purpose_member (TREE_PURPOSE (usings), todo))
3964 todo = tree_cons (TREE_PURPOSE (usings), NULL_TREE, todo);
3965 else if ((!result->value && !result->type)
3966 && !purpose_member (TREE_PURPOSE (usings), seen)
3967 && !purpose_member (TREE_PURPOSE (usings), todo)
3968 && !purpose_member (TREE_PURPOSE (usings), todo_maybe))
3969 todo_maybe = tree_cons (TREE_PURPOSE (usings), NULL_TREE,
3974 scope = TREE_PURPOSE (todo);
3975 todo = TREE_CHAIN (todo);
3978 && (!result->value && !result->type))
3980 scope = TREE_PURPOSE (todo_maybe);
3981 todo = TREE_CHAIN (todo_maybe);
3982 todo_maybe = NULL_TREE;
3985 scope = NULL_TREE; /* If there never was a todo list. */
3987 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, result->value != error_mark_node);
3990 /* Look up NAME in the current binding level and its superiors in the
3991 namespace of variables, functions and typedefs. Return a ..._DECL
3992 node of some kind representing its definition if there is only one
3993 such declaration, or return a TREE_LIST with all the overloaded
3994 definitions if there are many, or return 0 if it is undefined.
3996 If PREFER_TYPE is > 0, we prefer TYPE_DECLs or namespaces.
3997 If PREFER_TYPE is > 1, we reject non-type decls (e.g. namespaces).
3998 Otherwise we prefer non-TYPE_DECLs.
4000 If NONCLASS is nonzero, we don't look for the NAME in class scope,
4001 using IDENTIFIER_CLASS_VALUE. */
4004 lookup_name_real (tree name, int prefer_type, int nonclass,
4005 int namespaces_only, int flags)
4008 tree val = NULL_TREE;
4010 timevar_push (TV_NAME_LOOKUP);
4011 /* Conversion operators are handled specially because ordinary
4012 unqualified name lookup will not find template conversion
4014 if (IDENTIFIER_TYPENAME_P (name))
4016 struct cp_binding_level *level;
4018 for (level = current_binding_level;
4019 level && level->kind != sk_namespace;
4020 level = level->level_chain)
4025 /* A conversion operator can only be declared in a class
4027 if (level->kind != sk_class)
4030 /* Lookup the conversion operator in the class. */
4031 class_type = level->this_entity;
4032 operators = lookup_fnfields (class_type, name, /*protect=*/0);
4034 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, operators);
4037 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
4040 flags |= lookup_flags (prefer_type, namespaces_only);
4042 /* First, look in non-namespace scopes. */
4044 if (current_class_type == NULL_TREE)
4047 for (iter = IDENTIFIER_BINDING (name); iter; iter = iter->previous)
4051 if (!LOCAL_BINDING_P (iter) && nonclass)
4052 /* We're not looking for class-scoped bindings, so keep going. */
4055 /* If this is the kind of thing we're looking for, we're done. */
4056 if (qualify_lookup (iter->value, flags))
4057 binding = iter->value;
4058 else if ((flags & LOOKUP_PREFER_TYPES)
4059 && qualify_lookup (iter->type, flags))
4060 binding = iter->type;
4062 binding = NULL_TREE;
4071 /* Now lookup in namespace scopes. */
4074 tree t = unqualified_namespace_lookup (name, flags);
4081 /* If we have a single function from a using decl, pull it out. */
4082 if (TREE_CODE (val) == OVERLOAD && ! really_overloaded_fn (val))
4083 val = OVL_FUNCTION (val);
4086 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
4090 lookup_name_nonclass (tree name)
4092 return lookup_name_real (name, 0, 1, 0, LOOKUP_COMPLAIN);
4096 lookup_function_nonclass (tree name, tree args)
4098 return lookup_arg_dependent (name, lookup_name_nonclass (name), args);
4102 lookup_name (tree name, int prefer_type)
4104 return lookup_name_real (name, prefer_type, 0, 0, LOOKUP_COMPLAIN);
4107 /* Similar to `lookup_name' but look only in the innermost non-class
4111 lookup_name_current_level (tree name)
4113 struct cp_binding_level *b;
4116 timevar_push (TV_NAME_LOOKUP);
4117 b = innermost_nonclass_level ();
4119 if (b->kind == sk_namespace)
4121 t = IDENTIFIER_NAMESPACE_VALUE (name);
4123 /* extern "C" function() */
4124 if (t != NULL_TREE && TREE_CODE (t) == TREE_LIST)
4127 else if (IDENTIFIER_BINDING (name)
4128 && LOCAL_BINDING_P (IDENTIFIER_BINDING (name)))
4132 if (IDENTIFIER_BINDING (name)->scope == b)
4133 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, IDENTIFIER_VALUE (name));
4135 if (b->kind == sk_cleanup)
4142 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
4145 /* Like lookup_name_current_level, but for types. */
4148 lookup_type_current_level (tree name)
4152 timevar_push (TV_NAME_LOOKUP);
4153 my_friendly_assert (current_binding_level->kind != sk_namespace,
4156 if (REAL_IDENTIFIER_TYPE_VALUE (name) != NULL_TREE
4157 && REAL_IDENTIFIER_TYPE_VALUE (name) != global_type_node)
4159 struct cp_binding_level *b = current_binding_level;
4162 if (purpose_member (name, b->type_shadowed))
4163 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP,
4164 REAL_IDENTIFIER_TYPE_VALUE (name));
4165 if (b->kind == sk_cleanup)
4172 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
4175 /* [basic.lookup.koenig] */
4176 /* A nonzero return value in the functions below indicates an error. */
4186 static bool arg_assoc (struct arg_lookup*, tree);
4187 static bool arg_assoc_args (struct arg_lookup*, tree);
4188 static bool arg_assoc_type (struct arg_lookup*, tree);
4189 static bool add_function (struct arg_lookup *, tree);
4190 static bool arg_assoc_namespace (struct arg_lookup *, tree);
4191 static bool arg_assoc_class (struct arg_lookup *, tree);
4192 static bool arg_assoc_template_arg (struct arg_lookup*, tree);
4194 /* Add a function to the lookup structure.
4195 Returns true on error. */
4198 add_function (struct arg_lookup *k, tree fn)
4200 /* We used to check here to see if the function was already in the list,
4201 but that's O(n^2), which is just too expensive for function lookup.
4202 Now we deal with the occasional duplicate in joust. In doing this, we
4203 assume that the number of duplicates will be small compared to the
4204 total number of functions being compared, which should usually be the
4207 /* We must find only functions, or exactly one non-function. */
4210 else if (fn == k->functions)
4212 else if (is_overloaded_fn (k->functions) && is_overloaded_fn (fn))
4213 k->functions = build_overload (fn, k->functions);
4216 tree f1 = OVL_CURRENT (k->functions);
4218 if (is_overloaded_fn (f1))
4220 fn = f1; f1 = f2; f2 = fn;
4222 cp_error_at ("`%D' is not a function,", f1);
4223 cp_error_at (" conflict with `%D'", f2);
4224 error (" in call to `%D'", k->name);
4231 /* Returns true iff CURRENT has declared itself to be an associated
4232 namespace of SCOPE via a strong using-directive (or transitive chain
4233 thereof). Both are namespaces. */
4236 is_associated_namespace (tree current, tree scope)
4238 tree seen = NULL_TREE;
4239 tree todo = NULL_TREE;
4243 if (scope == current)
4245 seen = tree_cons (scope, NULL_TREE, seen);
4246 for (t = DECL_NAMESPACE_ASSOCIATIONS (scope); t; t = TREE_CHAIN (t))
4247 if (!purpose_member (TREE_PURPOSE (t), seen))
4248 todo = tree_cons (TREE_PURPOSE (t), NULL_TREE, todo);
4251 scope = TREE_PURPOSE (todo);
4252 todo = TREE_CHAIN (todo);
4259 /* Add functions of a namespace to the lookup structure.
4260 Returns true on error. */
4263 arg_assoc_namespace (struct arg_lookup *k, tree scope)
4267 if (purpose_member (scope, k->namespaces))
4269 k->namespaces = tree_cons (scope, NULL_TREE, k->namespaces);
4271 /* Check out our super-users. */
4272 for (value = DECL_NAMESPACE_ASSOCIATIONS (scope); value;
4273 value = TREE_CHAIN (value))
4274 if (arg_assoc_namespace (k, TREE_PURPOSE (value)))
4277 value = namespace_binding (k->name, scope);
4281 for (; value; value = OVL_NEXT (value))
4282 if (add_function (k, OVL_CURRENT (value)))
4288 /* Adds everything associated with a template argument to the lookup
4289 structure. Returns true on error. */
4292 arg_assoc_template_arg (struct arg_lookup *k, tree arg)
4294 /* [basic.lookup.koenig]
4296 If T is a template-id, its associated namespaces and classes are
4297 ... the namespaces and classes associated with the types of the
4298 template arguments provided for template type parameters
4299 (excluding template template parameters); the namespaces in which
4300 any template template arguments are defined; and the classes in
4301 which any member templates used as template template arguments
4302 are defined. [Note: non-type template arguments do not
4303 contribute to the set of associated namespaces. ] */
4305 /* Consider first template template arguments. */
4306 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
4307 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
4309 else if (TREE_CODE (arg) == TEMPLATE_DECL)
4311 tree ctx = CP_DECL_CONTEXT (arg);
4313 /* It's not a member template. */
4314 if (TREE_CODE (ctx) == NAMESPACE_DECL)
4315 return arg_assoc_namespace (k, ctx);
4316 /* Otherwise, it must be member template. */
4318 return arg_assoc_class (k, ctx);
4320 /* It's not a template template argument, but it is a type template
4322 else if (TYPE_P (arg))
4323 return arg_assoc_type (k, arg);
4324 /* It's a non-type template argument. */
4329 /* Adds everything associated with class to the lookup structure.
4330 Returns true on error. */
4333 arg_assoc_class (struct arg_lookup *k, tree type)
4335 tree list, friends, context;
4338 /* Backend build structures, such as __builtin_va_list, aren't
4339 affected by all this. */
4340 if (!CLASS_TYPE_P (type))
4343 if (purpose_member (type, k->classes))
4345 k->classes = tree_cons (type, NULL_TREE, k->classes);
4347 context = decl_namespace (TYPE_MAIN_DECL (type));
4348 if (arg_assoc_namespace (k, context))
4351 /* Process baseclasses. */
4352 for (i = 0; i < BINFO_N_BASE_BINFOS (TYPE_BINFO (type)); i++)
4354 (k, BINFO_TYPE (BINFO_BASE_BINFO (TYPE_BINFO (type), i))))
4357 /* Process friends. */
4358 for (list = DECL_FRIENDLIST (TYPE_MAIN_DECL (type)); list;
4359 list = TREE_CHAIN (list))
4360 if (k->name == FRIEND_NAME (list))
4361 for (friends = FRIEND_DECLS (list); friends;
4362 friends = TREE_CHAIN (friends))
4364 tree fn = TREE_VALUE (friends);
4366 /* Only interested in global functions with potentially hidden
4367 (i.e. unqualified) declarations. */
4368 if (CP_DECL_CONTEXT (fn) != context)
4370 /* Template specializations are never found by name lookup.
4371 (Templates themselves can be found, but not template
4372 specializations.) */
4373 if (TREE_CODE (fn) == FUNCTION_DECL && DECL_USE_TEMPLATE (fn))
4375 if (add_function (k, fn))
4379 /* Process template arguments. */
4380 if (CLASSTYPE_TEMPLATE_INFO (type)
4381 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
4383 list = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
4384 for (i = 0; i < TREE_VEC_LENGTH (list); ++i)
4385 arg_assoc_template_arg (k, TREE_VEC_ELT (list, i));
4391 /* Adds everything associated with a given type.
4392 Returns 1 on error. */
4395 arg_assoc_type (struct arg_lookup *k, tree type)
4397 /* As we do not get the type of non-type dependent expressions
4398 right, we can end up with such things without a type. */
4402 if (TYPE_PTRMEM_P (type))
4404 /* Pointer to member: associate class type and value type. */
4405 if (arg_assoc_type (k, TYPE_PTRMEM_CLASS_TYPE (type)))
4407 return arg_assoc_type (k, TYPE_PTRMEM_POINTED_TO_TYPE (type));
4409 else switch (TREE_CODE (type))
4422 if (TYPE_PTRMEMFUNC_P (type))
4423 return arg_assoc_type (k, TYPE_PTRMEMFUNC_FN_TYPE (type));
4424 return arg_assoc_class (k, type);
4426 case REFERENCE_TYPE:
4428 return arg_assoc_type (k, TREE_TYPE (type));
4431 return arg_assoc_namespace (k, decl_namespace (TYPE_MAIN_DECL (type)));
4433 /* The basetype is referenced in the first arg type, so just
4436 /* Associate the parameter types. */
4437 if (arg_assoc_args (k, TYPE_ARG_TYPES (type)))
4439 /* Associate the return type. */
4440 return arg_assoc_type (k, TREE_TYPE (type));
4441 case TEMPLATE_TYPE_PARM:
4442 case BOUND_TEMPLATE_TEMPLATE_PARM:
4447 if (type == unknown_type_node)
4449 /* else fall through */
4456 /* Adds everything associated with arguments. Returns true on error. */
4459 arg_assoc_args (struct arg_lookup *k, tree args)
4461 for (; args; args = TREE_CHAIN (args))
4462 if (arg_assoc (k, TREE_VALUE (args)))
4467 /* Adds everything associated with a given tree_node. Returns 1 on error. */
4470 arg_assoc (struct arg_lookup *k, tree n)
4472 if (n == error_mark_node)
4476 return arg_assoc_type (k, n);
4478 if (! type_unknown_p (n))
4479 return arg_assoc_type (k, TREE_TYPE (n));
4481 if (TREE_CODE (n) == ADDR_EXPR)
4482 n = TREE_OPERAND (n, 0);
4483 if (TREE_CODE (n) == COMPONENT_REF)
4484 n = TREE_OPERAND (n, 1);
4485 if (TREE_CODE (n) == OFFSET_REF)
4486 n = TREE_OPERAND (n, 1);
4487 while (TREE_CODE (n) == TREE_LIST)
4489 if (TREE_CODE (n) == BASELINK)
4490 n = BASELINK_FUNCTIONS (n);
4492 if (TREE_CODE (n) == FUNCTION_DECL)
4493 return arg_assoc_type (k, TREE_TYPE (n));
4494 if (TREE_CODE (n) == TEMPLATE_ID_EXPR)
4496 /* [basic.lookup.koenig]
4498 If T is a template-id, its associated namespaces and classes
4499 are the namespace in which the template is defined; for
4500 member templates, the member template's class... */
4501 tree template = TREE_OPERAND (n, 0);
4502 tree args = TREE_OPERAND (n, 1);
4506 if (TREE_CODE (template) == COMPONENT_REF)
4507 template = TREE_OPERAND (template, 1);
4509 /* First, the template. There may actually be more than one if
4510 this is an overloaded function template. But, in that case,
4511 we only need the first; all the functions will be in the same
4513 template = OVL_CURRENT (template);
4515 ctx = CP_DECL_CONTEXT (template);
4517 if (TREE_CODE (ctx) == NAMESPACE_DECL)
4519 if (arg_assoc_namespace (k, ctx) == 1)
4522 /* It must be a member template. */
4523 else if (arg_assoc_class (k, ctx) == 1)
4526 /* Now the arguments. */
4527 for (ix = TREE_VEC_LENGTH (args); ix--;)
4528 if (arg_assoc_template_arg (k, TREE_VEC_ELT (args, ix)) == 1)
4531 else if (TREE_CODE (n) == OVERLOAD)
4533 for (; n; n = OVL_CHAIN (n))
4534 if (arg_assoc_type (k, TREE_TYPE (OVL_FUNCTION (n))))
4541 /* Performs Koenig lookup depending on arguments, where fns
4542 are the functions found in normal lookup. */
4545 lookup_arg_dependent (tree name, tree fns, tree args)
4547 struct arg_lookup k;
4548 tree fn = NULL_TREE;
4550 timevar_push (TV_NAME_LOOKUP);
4553 k.classes = NULL_TREE;
4555 /* We've already looked at some namespaces during normal unqualified
4556 lookup -- but we don't know exactly which ones. If the functions
4557 we found were brought into the current namespace via a using
4558 declaration, we have not really checked the namespace from which
4559 they came. Therefore, we check all namespaces here -- unless the
4560 function we have is from the current namespace. Even then, we
4561 must check all namespaces if the function is a local
4562 declaration; any other declarations present at namespace scope
4563 should be visible during argument-dependent lookup. */
4565 fn = OVL_CURRENT (fns);
4566 if (fn && TREE_CODE (fn) == FUNCTION_DECL
4567 && (CP_DECL_CONTEXT (fn) != current_decl_namespace ()
4568 || DECL_LOCAL_FUNCTION_P (fn)))
4569 k.namespaces = NULL_TREE;
4571 /* Setting NAMESPACES is purely an optimization; it prevents
4572 adding functions which are already in FNS. Adding them would
4573 be safe -- "joust" will eliminate the duplicates -- but
4575 k.namespaces = build_tree_list (current_decl_namespace (), NULL_TREE);
4577 arg_assoc_args (&k, args);
4578 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, k.functions);
4581 /* Add namespace to using_directives. Return NULL_TREE if nothing was
4582 changed (i.e. there was already a directive), or the fresh
4583 TREE_LIST otherwise. */
4586 push_using_directive (tree used)
4588 tree ud = current_binding_level->using_directives;
4589 tree iter, ancestor;
4591 timevar_push (TV_NAME_LOOKUP);
4592 /* Check if we already have this. */
4593 if (purpose_member (used, ud) != NULL_TREE)
4594 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
4596 ancestor = namespace_ancestor (current_decl_namespace (), used);
4597 ud = current_binding_level->using_directives;
4598 ud = tree_cons (used, ancestor, ud);
4599 current_binding_level->using_directives = ud;
4601 /* Recursively add all namespaces used. */
4602 for (iter = DECL_NAMESPACE_USING (used); iter; iter = TREE_CHAIN (iter))
4603 push_using_directive (TREE_PURPOSE (iter));
4605 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, ud);
4608 /* The type TYPE is being declared. If it is a class template, or a
4609 specialization of a class template, do any processing required and
4610 perform error-checking. If IS_FRIEND is nonzero, this TYPE is
4611 being declared a friend. B is the binding level at which this TYPE
4614 Returns the TYPE_DECL for TYPE, which may have been altered by this
4618 maybe_process_template_type_declaration (tree type, int globalize,
4621 tree decl = TYPE_NAME (type);
4623 if (processing_template_parmlist)
4624 /* You can't declare a new template type in a template parameter
4625 list. But, you can declare a non-template type:
4627 template <class A*> struct S;
4629 is a forward-declaration of `A'. */
4633 maybe_check_template_type (type);
4635 my_friendly_assert (IS_AGGR_TYPE (type)
4636 || TREE_CODE (type) == ENUMERAL_TYPE, 0);
4639 if (processing_template_decl)
4641 /* This may change after the call to
4642 push_template_decl_real, but we want the original value. */
4643 tree name = DECL_NAME (decl);
4645 decl = push_template_decl_real (decl, globalize);
4646 /* If the current binding level is the binding level for the
4647 template parameters (see the comment in
4648 begin_template_parm_list) and the enclosing level is a class
4649 scope, and we're not looking at a friend, push the
4650 declaration of the member class into the class scope. In the
4651 friend case, push_template_decl will already have put the
4652 friend into global scope, if appropriate. */
4653 if (TREE_CODE (type) != ENUMERAL_TYPE
4654 && !globalize && b->kind == sk_template_parms
4655 && b->level_chain->kind == sk_class)
4657 finish_member_declaration (CLASSTYPE_TI_TEMPLATE (type));
4658 /* Put this UDT in the table of UDTs for the class, since
4659 that won't happen below because B is not the class
4660 binding level, but is instead the pseudo-global level. */
4661 if (b->level_chain->type_decls == NULL)
4662 b->level_chain->type_decls =
4663 binding_table_new (SCOPE_DEFAULT_HT_SIZE);
4664 binding_table_insert (b->level_chain->type_decls, name, type);
4665 if (!COMPLETE_TYPE_P (current_class_type))
4667 maybe_add_class_template_decl_list (current_class_type,
4668 type, /*friend_p=*/0);
4669 CLASSTYPE_NESTED_UTDS (current_class_type) =
4670 b->level_chain->type_decls;
4679 /* Push a tag name NAME for struct/class/union/enum type TYPE.
4680 Normally put it into the inner-most non-sk_cleanup scope,
4681 but if GLOBALIZE is true, put it in the inner-most non-class scope.
4682 The latter is needed for implicit declarations. */
4685 pushtag (tree name, tree type, int globalize)
4687 struct cp_binding_level *b;
4689 timevar_push (TV_NAME_LOOKUP);
4690 b = current_binding_level;
4691 while (/* Cleanup scopes are not scopes from the point of view of
4693 b->kind == sk_cleanup
4694 /* Neither are the scopes used to hold template parameters
4695 for an explicit specialization. For an ordinary template
4696 declaration, these scopes are not scopes from the point of
4697 view of the language -- but we need a place to stash
4698 things that will go in the containing namespace when the
4699 template is instantiated. */
4700 || (b->kind == sk_template_parms && b->explicit_spec_p)
4701 || (b->kind == sk_class
4703 /* We may be defining a new type in the initializer
4704 of a static member variable. We allow this when
4705 not pedantic, and it is particularly useful for
4706 type punning via an anonymous union. */
4707 || COMPLETE_TYPE_P (b->this_entity))))
4710 if (b->type_decls == NULL)
4711 b->type_decls = binding_table_new (SCOPE_DEFAULT_HT_SIZE);
4712 binding_table_insert (b->type_decls, name, type);
4716 /* Do C++ gratuitous typedefing. */
4717 if (IDENTIFIER_TYPE_VALUE (name) != type)
4721 tree context = TYPE_CONTEXT (type);
4725 tree cs = current_scope ();
4729 else if (cs != NULL_TREE && TYPE_P (cs))
4730 /* When declaring a friend class of a local class, we want
4731 to inject the newly named class into the scope
4732 containing the local class, not the namespace scope. */
4733 context = decl_function_context (get_type_decl (cs));
4736 context = current_namespace;
4738 if (b->kind == sk_class
4739 || (b->kind == sk_template_parms
4740 && b->level_chain->kind == sk_class))
4743 if (current_lang_name == lang_name_java)
4744 TYPE_FOR_JAVA (type) = 1;
4746 d = create_implicit_typedef (name, type);
4747 DECL_CONTEXT (d) = FROB_CONTEXT (context);
4749 set_identifier_type_value_with_scope (name, d, b);
4751 d = maybe_process_template_type_declaration (type,
4754 if (b->kind == sk_class)
4756 if (!PROCESSING_REAL_TEMPLATE_DECL_P ())
4757 /* Put this TYPE_DECL on the TYPE_FIELDS list for the
4758 class. But if it's a member template class, we
4759 want the TEMPLATE_DECL, not the TYPE_DECL, so this
4761 finish_member_declaration (d);
4763 pushdecl_class_level (d);
4766 d = pushdecl_with_scope (d, b);
4768 /* FIXME what if it gets a name from typedef? */
4769 if (ANON_AGGRNAME_P (name))
4770 DECL_IGNORED_P (d) = 1;
4772 TYPE_CONTEXT (type) = DECL_CONTEXT (d);
4774 /* If this is a local class, keep track of it. We need this
4775 information for name-mangling, and so that it is possible to find
4776 all function definitions in a translation unit in a convenient
4777 way. (It's otherwise tricky to find a member function definition
4778 it's only pointed to from within a local class.) */
4779 if (TYPE_CONTEXT (type)
4780 && TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL
4781 && !processing_template_decl)
4782 VARRAY_PUSH_TREE (local_classes, type);
4784 if (b->kind == sk_class
4785 && !COMPLETE_TYPE_P (current_class_type))
4787 maybe_add_class_template_decl_list (current_class_type,
4788 type, /*friend_p=*/0);
4789 CLASSTYPE_NESTED_UTDS (current_class_type) = b->type_decls;
4793 if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL)
4794 /* Use the canonical TYPE_DECL for this node. */
4795 TYPE_STUB_DECL (type) = TYPE_NAME (type);
4798 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE
4799 will be the tagged type we just added to the current
4800 binding level. This fake NULL-named TYPE_DECL node helps
4801 dwarfout.c to know when it needs to output a
4802 representation of a tagged type, and it also gives us a
4803 convenient place to record the "scope start" address for
4806 tree d = build_decl (TYPE_DECL, NULL_TREE, type);
4807 TYPE_STUB_DECL (type) = pushdecl_with_scope (d, b);
4809 timevar_pop (TV_NAME_LOOKUP);
4812 /* Allocate storage for saving a C++ binding. */
4813 #define cxx_saved_binding_make() \
4814 (ggc_alloc (sizeof (cxx_saved_binding)))
4816 struct cxx_saved_binding GTY(())
4818 /* Link that chains saved C++ bindings for a given name into a stack. */
4819 cxx_saved_binding *previous;
4820 /* The name of the current binding. */
4822 /* The binding we're saving. */
4823 cxx_binding *binding;
4825 tree real_type_value;
4828 /* Subroutines for reverting temporarily to top-level for instantiation
4829 of templates and such. We actually need to clear out the class- and
4830 local-value slots of all identifiers, so that only the global values
4831 are at all visible. Simply setting current_binding_level to the global
4832 scope isn't enough, because more binding levels may be pushed. */
4833 struct saved_scope *scope_chain;
4835 /* If ID is not already in the SEARCH_BINDINGS, prepend its binding
4836 information to OLD_BINDINGS. Returns the new OLD_BINDINGS
4839 static cxx_saved_binding *
4840 store_binding (tree id,
4841 cxx_saved_binding *old_bindings,
4842 cxx_saved_binding *search_bindings)
4844 cxx_saved_binding *saved;
4845 cxx_saved_binding *t1;
4848 /* Note that we may have an IDENTIFIER_CLASS_VALUE even when
4849 we have no IDENTIFIER_BINDING if we have left the class
4850 scope, but cached the class-level declarations. */
4851 || !(IDENTIFIER_BINDING (id) || IDENTIFIER_CLASS_VALUE (id)))
4852 return old_bindings;
4854 for (t1 = search_bindings; t1; t1 = t1->previous)
4855 if (t1->identifier == id)
4856 return old_bindings;
4858 my_friendly_assert (TREE_CODE (id) == IDENTIFIER_NODE, 135);
4859 saved = cxx_saved_binding_make ();
4860 saved->previous = old_bindings;
4861 saved->identifier = id;
4862 saved->binding = IDENTIFIER_BINDING (id);
4863 saved->class_value = IDENTIFIER_CLASS_VALUE (id);;
4864 saved->real_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
4865 IDENTIFIER_BINDING (id) = NULL;
4866 IDENTIFIER_CLASS_VALUE (id) = NULL_TREE;
4870 static cxx_saved_binding *
4871 store_bindings (tree names, cxx_saved_binding *old_bindings)
4874 cxx_saved_binding *search_bindings = old_bindings;
4876 timevar_push (TV_NAME_LOOKUP);
4877 for (t = names; t; t = TREE_CHAIN (t))
4881 if (TREE_CODE (t) == TREE_LIST)
4882 id = TREE_PURPOSE (t);
4887 = store_binding (id, old_bindings, search_bindings);
4889 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, old_bindings);
4892 /* Like store_bindings, but NAMES is a vector of cp_class_binding
4893 objects, rather than a TREE_LIST. */
4895 static cxx_saved_binding *
4896 store_class_bindings (VEC(cp_class_binding) *names,
4897 cxx_saved_binding *old_bindings)
4900 cp_class_binding *cb;
4901 cxx_saved_binding *search_bindings = old_bindings;
4903 timevar_push (TV_NAME_LOOKUP);
4905 (cb = VEC_iterate(cp_class_binding, names, i));
4908 = store_binding (cb->identifier, old_bindings, search_bindings);
4909 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, old_bindings);
4913 push_to_top_level (void)
4915 struct saved_scope *s;
4916 struct cp_binding_level *b;
4917 cxx_saved_binding *old_bindings;
4920 timevar_push (TV_NAME_LOOKUP);
4921 s = ggc_alloc_cleared (sizeof (struct saved_scope));
4923 b = scope_chain ? current_binding_level : 0;
4925 /* If we're in the middle of some function, save our state. */
4929 push_function_context_to (NULL_TREE);
4934 old_bindings = NULL;
4935 if (scope_chain && previous_class_level)
4936 old_bindings = store_class_bindings (previous_class_level->class_shadowed,
4939 /* Have to include the global scope, because class-scope decls
4940 aren't listed anywhere useful. */
4941 for (; b; b = b->level_chain)
4945 /* Template IDs are inserted into the global level. If they were
4946 inserted into namespace level, finish_file wouldn't find them
4947 when doing pending instantiations. Therefore, don't stop at
4948 namespace level, but continue until :: . */
4949 if (global_scope_p (b))
4952 old_bindings = store_bindings (b->names, old_bindings);
4953 /* We also need to check class_shadowed to save class-level type
4954 bindings, since pushclass doesn't fill in b->names. */
4955 if (b->kind == sk_class)
4956 old_bindings = store_class_bindings (b->class_shadowed, old_bindings);
4958 /* Unwind type-value slots back to top level. */
4959 for (t = b->type_shadowed; t; t = TREE_CHAIN (t))
4960 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (t), TREE_VALUE (t));
4962 s->prev = scope_chain;
4963 s->old_bindings = old_bindings;
4965 s->need_pop_function_context = need_pop;
4966 s->function_decl = current_function_decl;
4969 current_function_decl = NULL_TREE;
4970 VARRAY_TREE_INIT (current_lang_base, 10, "current_lang_base");
4971 current_lang_name = lang_name_cplusplus;
4972 current_namespace = global_namespace;
4973 timevar_pop (TV_NAME_LOOKUP);
4977 pop_from_top_level (void)
4979 struct saved_scope *s = scope_chain;
4980 cxx_saved_binding *saved;
4982 timevar_push (TV_NAME_LOOKUP);
4983 /* Clear out class-level bindings cache. */
4984 if (previous_class_level)
4985 invalidate_class_lookup_cache ();
4987 current_lang_base = 0;
4989 scope_chain = s->prev;
4990 for (saved = s->old_bindings; saved; saved = saved->previous)
4992 tree id = saved->identifier;
4994 IDENTIFIER_BINDING (id) = saved->binding;
4995 IDENTIFIER_CLASS_VALUE (id) = saved->class_value;
4996 SET_IDENTIFIER_TYPE_VALUE (id, saved->real_type_value);
4999 /* If we were in the middle of compiling a function, restore our
5001 if (s->need_pop_function_context)
5002 pop_function_context_from (NULL_TREE);
5003 current_function_decl = s->function_decl;
5004 timevar_pop (TV_NAME_LOOKUP);
5007 /* Pop off extraneous binding levels left over due to syntax errors.
5009 We don't pop past namespaces, as they might be valid. */
5012 pop_everything (void)
5014 if (ENABLE_SCOPE_CHECKING)
5015 verbatim ("XXX entering pop_everything ()\n");
5016 while (!toplevel_bindings_p ())
5018 if (current_binding_level->kind == sk_class)
5019 pop_nested_class ();
5023 if (ENABLE_SCOPE_CHECKING)
5024 verbatim ("XXX leaving pop_everything ()\n");
5027 /* Emit debugging information for using declarations and directives.
5028 If input tree is overloaded fn then emit debug info for all
5032 cp_emit_debug_info_for_using (tree t, tree context)
5034 /* Ignore this FUNCTION_DECL if it refers to a builtin declaration
5035 of a builtin function. */
5036 if (TREE_CODE (t) == FUNCTION_DECL
5037 && DECL_EXTERNAL (t)
5038 && DECL_BUILT_IN (t))
5041 /* Do not supply context to imported_module_or_decl, if
5042 it is a global namespace. */
5043 if (context == global_namespace)
5044 context = NULL_TREE;
5047 t = BASELINK_FUNCTIONS (t);
5049 /* FIXME: Handle TEMPLATE_DECLs. */
5050 for (t = OVL_CURRENT (t); t; t = OVL_NEXT (t))
5051 if (TREE_CODE (t) != TEMPLATE_DECL)
5052 (*debug_hooks->imported_module_or_decl) (t, context);
5055 #include "gt-cp-name-lookup.h"