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_innermost_nonclass_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_NEW (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_CNEWVEC (binding_entry, table->chain_count);
133 /* Make TABLE's entries ready for reuse. */
136 binding_table_free (binding_table table)
144 for (i = 0, count = table->chain_count; i < count; ++i)
146 binding_entry temp = table->chain[i];
149 binding_entry entry = temp;
151 binding_entry_free (entry);
153 table->chain[i] = NULL;
155 table->entry_count = 0;
158 /* Allocate a table with CHAIN_COUNT, assumed to be a power of two. */
160 static inline binding_table
161 binding_table_new (size_t chain_count)
163 binding_table table = GGC_NEW (struct binding_table_s);
165 binding_table_construct (table, chain_count);
169 /* Expand TABLE to twice its current chain_count. */
172 binding_table_expand (binding_table table)
174 const size_t old_chain_count = table->chain_count;
175 const size_t old_entry_count = table->entry_count;
176 const size_t new_chain_count = 2 * old_chain_count;
177 binding_entry *old_chains = table->chain;
180 binding_table_construct (table, new_chain_count);
181 for (i = 0; i < old_chain_count; ++i)
183 binding_entry entry = old_chains[i];
184 for (; entry != NULL; entry = old_chains[i])
186 const unsigned int hash = IDENTIFIER_HASH_VALUE (entry->name);
187 const size_t j = ENTRY_INDEX (hash, new_chain_count);
189 old_chains[i] = entry->chain;
190 entry->chain = table->chain[j];
191 table->chain[j] = entry;
194 table->entry_count = old_entry_count;
197 /* Insert a binding for NAME to TYPE into TABLE. */
200 binding_table_insert (binding_table table, tree name, tree type)
202 const unsigned int hash = IDENTIFIER_HASH_VALUE (name);
203 const size_t i = ENTRY_INDEX (hash, table->chain_count);
204 binding_entry entry = binding_entry_make (name, type);
206 entry->chain = table->chain[i];
207 table->chain[i] = entry;
208 ++table->entry_count;
210 if (3 * table->chain_count < 5 * table->entry_count)
211 binding_table_expand (table);
214 /* Return the binding_entry, if any, that maps NAME. */
217 binding_table_find (binding_table table, tree name)
219 const unsigned int hash = IDENTIFIER_HASH_VALUE (name);
220 binding_entry entry = table->chain[ENTRY_INDEX (hash, table->chain_count)];
222 while (entry != NULL && entry->name != name)
223 entry = entry->chain;
228 /* Return the binding_entry, if any, that maps NAME to an anonymous type. */
231 binding_table_find_anon_type (binding_table table, tree name)
233 const unsigned int hash = IDENTIFIER_HASH_VALUE (name);
234 binding_entry entry = table->chain[ENTRY_INDEX (hash, table->chain_count)];
236 while (entry != NULL && TYPE_IDENTIFIER (entry->type) != name)
237 entry = entry->chain;
239 return entry ? entry->type : NULL;
242 /* Return the binding_entry, if any, that has TYPE as target. If NAME
243 is non-null, then set the domain and rehash that entry. */
246 binding_table_reverse_maybe_remap (binding_table table, tree type, tree name)
248 const size_t chain_count = table->chain_count;
249 binding_entry entry = NULL;
250 binding_entry *p = NULL;
253 for (i = 0; i < chain_count && entry == NULL; ++i)
255 p = &table->chain[i];
256 while (*p != NULL && entry == NULL)
257 if ((*p)->type == type)
263 if (entry != NULL && name != NULL && entry->name != name)
265 /* Remove the bucket from the previous chain. */
268 /* Remap the name type to type. */
269 i = ENTRY_INDEX (IDENTIFIER_HASH_VALUE (name), chain_count);
270 entry->chain = table->chain[i];
272 table->chain[i] = entry;
278 /* Remove from TABLE all entries that map to anonymous enums or
282 binding_table_remove_anonymous_types (binding_table table)
284 const size_t chain_count = table->chain_count;
287 for (i = 0; i < chain_count; ++i)
289 binding_entry *p = &table->chain[i];
292 if (ANON_AGGRNAME_P ((*p)->name))
294 binding_entry e = *p;
296 --table->entry_count;
297 binding_entry_free (e);
304 /* Apply PROC -- with DATA -- to all entries in TABLE. */
307 binding_table_foreach (binding_table table, bt_foreach_proc proc, void *data)
309 const size_t chain_count = table->chain_count;
312 for (i = 0; i < chain_count; ++i)
314 binding_entry entry = table->chain[i];
315 for (; entry != NULL; entry = entry->chain)
320 #ifndef ENABLE_SCOPE_CHECKING
321 # define ENABLE_SCOPE_CHECKING 0
323 # define ENABLE_SCOPE_CHECKING 1
326 /* A free list of "cxx_binding"s, connected by their PREVIOUS. */
328 static GTY((deletable)) cxx_binding *free_bindings;
330 /* Initialize VALUE and TYPE field for BINDING, and set the PREVIOUS
334 cxx_binding_init (cxx_binding *binding, tree value, tree type)
336 binding->value = value;
337 binding->type = type;
338 binding->previous = NULL;
341 /* (GC)-allocate a binding object with VALUE and TYPE member initialized. */
344 cxx_binding_make (tree value, tree type)
346 cxx_binding *binding;
349 binding = free_bindings;
350 free_bindings = binding->previous;
353 binding = GGC_NEW (cxx_binding);
355 cxx_binding_init (binding, value, type);
360 /* Put BINDING back on the free list. */
363 cxx_binding_free (cxx_binding *binding)
365 binding->scope = NULL;
366 binding->previous = free_bindings;
367 free_bindings = binding;
370 /* Create a new binding for NAME (with the indicated VALUE and TYPE
371 bindings) in the class scope indicated by SCOPE. */
374 new_class_binding (tree name, tree value, tree type, cxx_scope *scope)
376 cp_class_binding *cb;
377 cxx_binding *binding;
379 if (VEC_length (cp_class_binding, scope->class_shadowed))
381 cp_class_binding *old_base;
382 old_base = VEC_index (cp_class_binding, scope->class_shadowed, 0);
383 if (VEC_reserve (cp_class_binding, scope->class_shadowed, -1))
385 /* Fixup the current bindings, as they might have moved. */
389 VEC_iterate (cp_class_binding, scope->class_shadowed, i, cb);
393 b = &IDENTIFIER_BINDING (cb->identifier);
394 while (*b != &old_base[i].base)
395 b = &((*b)->previous);
399 cb = VEC_quick_push (cp_class_binding, scope->class_shadowed, NULL);
402 cb = VEC_safe_push (cp_class_binding, scope->class_shadowed, NULL);
404 cb->identifier = name;
406 binding->scope = scope;
407 cxx_binding_init (binding, value, type);
411 /* Make DECL the innermost binding for ID. The LEVEL is the binding
412 level at which this declaration is being bound. */
415 push_binding (tree id, tree decl, cxx_scope* level)
417 cxx_binding *binding;
419 if (level != class_binding_level)
421 binding = cxx_binding_make (decl, NULL_TREE);
422 binding->scope = level;
425 binding = new_class_binding (id, decl, /*type=*/NULL_TREE, level);
427 /* Now, fill in the binding information. */
428 binding->previous = IDENTIFIER_BINDING (id);
429 INHERITED_VALUE_BINDING_P (binding) = 0;
430 LOCAL_BINDING_P (binding) = (level != class_binding_level);
432 /* And put it on the front of the list of bindings for ID. */
433 IDENTIFIER_BINDING (id) = binding;
436 /* Remove the binding for DECL which should be the innermost binding
440 pop_binding (tree id, tree decl)
442 cxx_binding *binding;
445 /* It's easiest to write the loops that call this function without
446 checking whether or not the entities involved have names. We
447 get here for such an entity. */
450 /* Get the innermost binding for ID. */
451 binding = IDENTIFIER_BINDING (id);
453 /* The name should be bound. */
454 gcc_assert (binding != NULL);
456 /* The DECL will be either the ordinary binding or the type
457 binding for this identifier. Remove that binding. */
458 if (binding->value == decl)
459 binding->value = NULL_TREE;
462 gcc_assert (binding->type == decl);
463 binding->type = NULL_TREE;
466 if (!binding->value && !binding->type)
468 /* We're completely done with the innermost binding for this
469 identifier. Unhook it from the list of bindings. */
470 IDENTIFIER_BINDING (id) = binding->previous;
472 /* Add it to the free list. */
473 cxx_binding_free (binding);
477 /* BINDING records an existing declaration for a namein the current scope.
478 But, DECL is another declaration for that same identifier in the
479 same scope. This is the `struct stat' hack whereby a non-typedef
480 class name or enum-name can be bound at the same level as some other
484 A class name (9.1) or enumeration name (7.2) can be hidden by the
485 name of an object, function, or enumerator declared in the same scope.
486 If a class or enumeration name and an object, function, or enumerator
487 are declared in the same scope (in any order) with the same name, the
488 class or enumeration name is hidden wherever the object, function, or
489 enumerator name is visible.
491 It's the responsibility of the caller to check that
492 inserting this name is valid here. Returns nonzero if the new binding
496 supplement_binding (cxx_binding *binding, tree decl)
498 tree bval = binding->value;
501 timevar_push (TV_NAME_LOOKUP);
502 if (TREE_CODE (decl) == TYPE_DECL && DECL_ARTIFICIAL (decl))
503 /* The new name is the type name. */
504 binding->type = decl;
505 else if (/* BVAL is null when push_class_level_binding moves an
506 inherited type-binding out of the way to make room for a
507 new value binding. */
509 /* BVAL is error_mark_node when DECL's name has been used
510 in a non-class scope prior declaration. In that case,
511 we should have already issued a diagnostic; for graceful
512 error recovery purpose, pretend this was the intended
513 declaration for that name. */
514 || bval == error_mark_node
515 /* If BVAL is a built-in that has not yet been declared,
516 pretend it is not there at all. */
517 || (TREE_CODE (bval) == FUNCTION_DECL
518 && DECL_ANTICIPATED (bval)))
519 binding->value = decl;
520 else if (TREE_CODE (bval) == TYPE_DECL && DECL_ARTIFICIAL (bval))
522 /* The old binding was a type name. It was placed in
523 VALUE field because it was thought, at the point it was
524 declared, to be the only entity with such a name. Move the
525 type name into the type slot; it is now hidden by the new
527 binding->type = bval;
528 binding->value = decl;
529 binding->value_is_inherited = false;
531 else if (TREE_CODE (bval) == TYPE_DECL
532 && TREE_CODE (decl) == TYPE_DECL
533 && DECL_NAME (decl) == DECL_NAME (bval)
534 && binding->scope->kind != sk_class
535 && (same_type_p (TREE_TYPE (decl), TREE_TYPE (bval))
536 /* If either type involves template parameters, we must
537 wait until instantiation. */
538 || uses_template_parms (TREE_TYPE (decl))
539 || uses_template_parms (TREE_TYPE (bval))))
540 /* We have two typedef-names, both naming the same type to have
541 the same name. In general, this is OK because of:
545 In a given scope, a typedef specifier can be used to redefine
546 the name of any type declared in that scope to refer to the
547 type to which it already refers.
549 However, in class scopes, this rule does not apply due to the
550 stricter language in [class.mem] prohibiting redeclarations of
553 /* There can be two block-scope declarations of the same variable,
554 so long as they are `extern' declarations. However, there cannot
555 be two declarations of the same static data member:
559 A member shall not be declared twice in the
560 member-specification. */
561 else if (TREE_CODE (decl) == VAR_DECL && TREE_CODE (bval) == VAR_DECL
562 && DECL_EXTERNAL (decl) && DECL_EXTERNAL (bval)
563 && !DECL_CLASS_SCOPE_P (decl))
565 duplicate_decls (decl, binding->value);
568 else if (TREE_CODE (decl) == NAMESPACE_DECL
569 && TREE_CODE (bval) == NAMESPACE_DECL
570 && DECL_NAMESPACE_ALIAS (decl)
571 && DECL_NAMESPACE_ALIAS (bval)
572 && ORIGINAL_NAMESPACE (bval) == ORIGINAL_NAMESPACE (decl))
575 In a declarative region, a namespace-alias-definition can be
576 used to redefine a namespace-alias declared in that declarative
577 region to refer only to the namespace to which it already
582 error ("declaration of %q#D", decl);
583 cp_error_at ("conflicts with previous declaration %q#D", bval);
587 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, ok);
590 /* Add DECL to the list of things declared in B. */
593 add_decl_to_level (tree decl, cxx_scope *b)
595 if (TREE_CODE (decl) == NAMESPACE_DECL
596 && !DECL_NAMESPACE_ALIAS (decl))
598 TREE_CHAIN (decl) = b->namespaces;
599 b->namespaces = decl;
601 else if (TREE_CODE (decl) == VAR_DECL && DECL_VIRTUAL_P (decl))
603 TREE_CHAIN (decl) = b->vtables;
608 /* We build up the list in reverse order, and reverse it later if
610 TREE_CHAIN (decl) = b->names;
614 /* If appropriate, add decl to separate list of statics. We
615 include extern variables because they might turn out to be
616 static later. It's OK for this list to contain a few false
618 if (b->kind == sk_namespace)
619 if ((TREE_CODE (decl) == VAR_DECL
620 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
621 || (TREE_CODE (decl) == FUNCTION_DECL
622 && (!TREE_PUBLIC (decl) || DECL_DECLARED_INLINE_P (decl))))
623 VARRAY_PUSH_TREE (b->static_decls, decl);
627 /* Record a decl-node X as belonging to the current lexical scope.
628 Check for errors (such as an incompatible declaration for the same
629 name already seen in the same scope).
631 Returns either X or an old decl for the same name.
632 If an old decl is returned, it may have been smashed
633 to agree with what X says. */
640 int need_new_binding;
642 timevar_push (TV_NAME_LOOKUP);
644 need_new_binding = 1;
646 if (DECL_TEMPLATE_PARM_P (x))
647 /* Template parameters have no context; they are not X::T even
648 when declared within a class or namespace. */
652 if (current_function_decl && x != current_function_decl
653 /* A local declaration for a function doesn't constitute
655 && TREE_CODE (x) != FUNCTION_DECL
656 /* A local declaration for an `extern' variable is in the
657 scope of the current namespace, not the current
659 && !(TREE_CODE (x) == VAR_DECL && DECL_EXTERNAL (x))
660 && !DECL_CONTEXT (x))
661 DECL_CONTEXT (x) = current_function_decl;
663 /* If this is the declaration for a namespace-scope function,
664 but the declaration itself is in a local scope, mark the
666 if (TREE_CODE (x) == FUNCTION_DECL
667 && DECL_NAMESPACE_SCOPE_P (x)
668 && current_function_decl
669 && x != current_function_decl)
670 DECL_LOCAL_FUNCTION_P (x) = 1;
673 name = DECL_NAME (x);
676 int different_binding_level = 0;
678 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
679 name = TREE_OPERAND (name, 0);
681 /* In case this decl was explicitly namespace-qualified, look it
682 up in its namespace context. */
683 if (DECL_NAMESPACE_SCOPE_P (x) && namespace_bindings_p ())
684 t = namespace_binding (name, DECL_CONTEXT (x));
686 t = lookup_name_innermost_nonclass_level (name);
688 /* [basic.link] If there is a visible declaration of an entity
689 with linkage having the same name and type, ignoring entities
690 declared outside the innermost enclosing namespace scope, the
691 block scope declaration declares that same entity and
692 receives the linkage of the previous declaration. */
693 if (! t && current_function_decl && x != current_function_decl
694 && (TREE_CODE (x) == FUNCTION_DECL || TREE_CODE (x) == VAR_DECL)
695 && DECL_EXTERNAL (x))
697 /* Look in block scope. */
698 t = innermost_non_namespace_value (name);
699 /* Or in the innermost namespace. */
701 t = namespace_binding (name, DECL_CONTEXT (x));
702 /* Does it have linkage? Note that if this isn't a DECL, it's an
703 OVERLOAD, which is OK. */
704 if (t && DECL_P (t) && ! (TREE_STATIC (t) || DECL_EXTERNAL (t)))
707 different_binding_level = 1;
710 /* If we are declaring a function, and the result of name-lookup
711 was an OVERLOAD, look for an overloaded instance that is
712 actually the same as the function we are declaring. (If
713 there is one, we have to merge our declaration with the
714 previous declaration.) */
715 if (t && TREE_CODE (t) == OVERLOAD)
719 if (TREE_CODE (x) == FUNCTION_DECL)
720 for (match = t; match; match = OVL_NEXT (match))
722 if (decls_match (OVL_CURRENT (match), x))
726 /* Just choose one. */
730 t = OVL_CURRENT (match);
735 if (t && t != error_mark_node)
737 if (different_binding_level)
739 if (decls_match (x, t))
740 /* The standard only says that the local extern
741 inherits linkage from the previous decl; in
742 particular, default args are not shared. We must
743 also tell cgraph to treat these decls as the same,
744 or we may neglect to emit an "unused" static - we
745 do this by making the DECL_UIDs equal, which should
746 be viewed as a kludge. FIXME. */
748 TREE_PUBLIC (x) = TREE_PUBLIC (t);
749 DECL_UID (x) = DECL_UID (t);
752 else if (TREE_CODE (t) == PARM_DECL)
754 gcc_assert (DECL_CONTEXT (t));
756 /* Check for duplicate params. */
757 if (duplicate_decls (x, t))
758 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
760 else if ((DECL_EXTERN_C_FUNCTION_P (x)
761 || DECL_FUNCTION_TEMPLATE_P (x))
762 && is_overloaded_fn (t))
763 /* Don't do anything just yet. */;
764 else if (t == wchar_decl_node)
766 if (pedantic && ! DECL_IN_SYSTEM_HEADER (x))
767 pedwarn ("redeclaration of %<wchar_t%> as %qT",
770 /* Throw away the redeclaration. */
771 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
775 tree olddecl = duplicate_decls (x, t);
777 /* If the redeclaration failed, we can stop at this
779 if (olddecl == error_mark_node)
780 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
784 if (TREE_CODE (t) == TYPE_DECL)
785 SET_IDENTIFIER_TYPE_VALUE (name, TREE_TYPE (t));
786 else if (TREE_CODE (t) == FUNCTION_DECL)
787 check_default_args (t);
789 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
791 else if (DECL_MAIN_P (x) && TREE_CODE (t) == FUNCTION_DECL)
793 /* A redeclaration of main, but not a duplicate of the
798 This function shall not be overloaded. */
799 cp_error_at ("invalid redeclaration of %qD", t);
801 /* We don't try to push this declaration since that
803 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
808 check_template_shadow (x);
810 /* If this is a function conjured up by the backend, massage it
811 so it looks friendly. */
812 if (DECL_NON_THUNK_FUNCTION_P (x) && ! DECL_LANG_SPECIFIC (x))
814 retrofit_lang_decl (x);
815 SET_DECL_LANGUAGE (x, lang_c);
818 if (DECL_NON_THUNK_FUNCTION_P (x) && ! DECL_FUNCTION_MEMBER_P (x))
820 t = push_overloaded_decl (x, PUSH_LOCAL);
822 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
823 if (!namespace_bindings_p ())
824 /* We do not need to create a binding for this name;
825 push_overloaded_decl will have already done so if
827 need_new_binding = 0;
829 else if (DECL_FUNCTION_TEMPLATE_P (x) && DECL_NAMESPACE_SCOPE_P (x))
831 t = push_overloaded_decl (x, PUSH_GLOBAL);
833 add_decl_to_level (x, NAMESPACE_LEVEL (CP_DECL_CONTEXT (t)));
834 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
837 /* If declaring a type as a typedef, copy the type (unless we're
838 at line 0), and install this TYPE_DECL as the new type's typedef
839 name. See the extensive comment in ../c-decl.c (pushdecl). */
840 if (TREE_CODE (x) == TYPE_DECL)
842 tree type = TREE_TYPE (x);
843 if (DECL_IS_BUILTIN (x))
845 if (TYPE_NAME (type) == 0)
846 TYPE_NAME (type) = x;
848 else if (type != error_mark_node && TYPE_NAME (type) != x
849 /* We don't want to copy the type when all we're
850 doing is making a TYPE_DECL for the purposes of
852 && (!TYPE_NAME (type)
853 || TYPE_NAME (type) != DECL_ABSTRACT_ORIGIN (x)))
855 DECL_ORIGINAL_TYPE (x) = type;
856 type = build_variant_type_copy (type);
857 TYPE_STUB_DECL (type) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
858 TYPE_NAME (type) = x;
859 TREE_TYPE (x) = type;
862 if (type != error_mark_node
864 && TYPE_IDENTIFIER (type))
865 set_identifier_type_value (DECL_NAME (x), x);
868 /* Multiple external decls of the same identifier ought to match.
870 We get warnings about inline functions where they are defined.
871 We get warnings about other functions from push_overloaded_decl.
873 Avoid duplicate warnings where they are used. */
874 if (TREE_PUBLIC (x) && TREE_CODE (x) != FUNCTION_DECL)
878 decl = IDENTIFIER_NAMESPACE_VALUE (name);
879 if (decl && TREE_CODE (decl) == OVERLOAD)
880 decl = OVL_FUNCTION (decl);
882 if (decl && decl != error_mark_node
883 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl))
884 /* If different sort of thing, we already gave an error. */
885 && TREE_CODE (decl) == TREE_CODE (x)
886 && !same_type_p (TREE_TYPE (x), TREE_TYPE (decl)))
888 pedwarn ("type mismatch with previous external decl of %q#D", x);
889 cp_pedwarn_at ("previous external decl of %q#D", decl);
893 /* This name is new in its binding level.
894 Install the new declaration and return it. */
895 if (namespace_bindings_p ())
897 /* Install a global value. */
899 /* If the first global decl has external linkage,
900 warn if we later see static one. */
901 if (IDENTIFIER_GLOBAL_VALUE (name) == NULL_TREE && TREE_PUBLIC (x))
902 TREE_PUBLIC (name) = 1;
904 /* Bind the name for the entity. */
905 if (!(TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x)
907 && (TREE_CODE (x) == TYPE_DECL
908 || TREE_CODE (x) == VAR_DECL
909 || TREE_CODE (x) == ALIAS_DECL
910 || TREE_CODE (x) == NAMESPACE_DECL
911 || TREE_CODE (x) == CONST_DECL
912 || TREE_CODE (x) == TEMPLATE_DECL))
913 SET_IDENTIFIER_NAMESPACE_VALUE (name, x);
915 /* If new decl is `static' and an `extern' was seen previously,
917 if (x != NULL_TREE && t != NULL_TREE && decls_match (x, t))
918 warn_extern_redeclared_static (x, t);
922 /* Here to install a non-global value. */
923 tree oldlocal = innermost_non_namespace_value (name);
924 tree oldglobal = IDENTIFIER_NAMESPACE_VALUE (name);
926 if (need_new_binding)
928 push_local_binding (name, x, 0);
929 /* Because push_local_binding will hook X on to the
930 current_binding_level's name list, we don't want to
931 do that again below. */
932 need_new_binding = 0;
935 /* If this is a TYPE_DECL, push it into the type value slot. */
936 if (TREE_CODE (x) == TYPE_DECL)
937 set_identifier_type_value (name, x);
939 /* Clear out any TYPE_DECL shadowed by a namespace so that
940 we won't think this is a type. The C struct hack doesn't
941 go through namespaces. */
942 if (TREE_CODE (x) == NAMESPACE_DECL)
943 set_identifier_type_value (name, NULL_TREE);
950 && TREE_CODE (oldlocal) == VAR_DECL
951 && DECL_DEAD_FOR_LOCAL (oldlocal))
952 oldlocal = DECL_SHADOWED_FOR_VAR (oldlocal);
954 if (oldlocal == NULL_TREE)
955 oldlocal = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (d));
958 /* If this is an extern function declaration, see if we
959 have a global definition or declaration for the function. */
960 if (oldlocal == NULL_TREE
962 && oldglobal != NULL_TREE
963 && TREE_CODE (x) == FUNCTION_DECL
964 && TREE_CODE (oldglobal) == FUNCTION_DECL)
966 /* We have one. Their types must agree. */
967 if (decls_match (x, oldglobal))
971 warning ("extern declaration of %q#D doesn't match", x);
972 cp_warning_at ("global declaration %q#D", oldglobal);
975 /* If we have a local external declaration,
976 and no file-scope declaration has yet been seen,
977 then if we later have a file-scope decl it must not be static. */
978 if (oldlocal == NULL_TREE
979 && oldglobal == NULL_TREE
982 TREE_PUBLIC (name) = 1;
984 /* Warn if shadowing an argument at the top level of the body. */
985 if (oldlocal != NULL_TREE && !DECL_EXTERNAL (x)
986 /* Inline decls shadow nothing. */
987 && !DECL_FROM_INLINE (x)
988 && TREE_CODE (oldlocal) == PARM_DECL
989 /* Don't check the `this' parameter. */
990 && !DECL_ARTIFICIAL (oldlocal))
994 /* Don't complain if it's from an enclosing function. */
995 if (DECL_CONTEXT (oldlocal) == current_function_decl
996 && TREE_CODE (x) != PARM_DECL)
998 /* Go to where the parms should be and see if we find
1000 struct cp_binding_level *b = current_binding_level->level_chain;
1002 /* Skip the ctor/dtor cleanup level. */
1006 if (b->kind == sk_function_parms)
1008 error ("declaration of %q#D shadows a parameter", x);
1013 if (warn_shadow && !err)
1015 warning ("declaration of %q#D shadows a parameter", x);
1016 warning ("%Jshadowed declaration is here", oldlocal);
1020 /* Maybe warn if shadowing something else. */
1021 else if (warn_shadow && !DECL_EXTERNAL (x)
1022 /* No shadow warnings for internally generated vars. */
1023 && ! DECL_ARTIFICIAL (x)
1024 /* No shadow warnings for vars made for inlining. */
1025 && ! DECL_FROM_INLINE (x))
1029 if (current_class_ptr)
1030 member = lookup_member (current_class_type,
1033 /*want_type=*/false);
1037 if (member && !TREE_STATIC (member))
1039 /* Location of previous decl is not useful in this case. */
1040 warning ("declaration of %qD shadows a member of 'this'",
1043 else if (oldlocal != NULL_TREE
1044 && TREE_CODE (oldlocal) == VAR_DECL)
1046 warning ("declaration of %qD shadows a previous local", x);
1047 warning ("%Jshadowed declaration is here", oldlocal);
1049 else if (oldglobal != NULL_TREE
1050 && TREE_CODE (oldglobal) == VAR_DECL)
1051 /* XXX shadow warnings in outer-more namespaces */
1053 warning ("declaration of %qD shadows a global declaration",
1055 warning ("%Jshadowed declaration is here", oldglobal);
1060 if (TREE_CODE (x) == FUNCTION_DECL)
1061 check_default_args (x);
1063 if (TREE_CODE (x) == VAR_DECL)
1064 maybe_register_incomplete_var (x);
1067 if (need_new_binding)
1068 add_decl_to_level (x,
1069 DECL_NAMESPACE_SCOPE_P (x)
1070 ? NAMESPACE_LEVEL (CP_DECL_CONTEXT (x))
1071 : current_binding_level);
1073 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
1076 /* Enter DECL into the symbol table, if that's appropriate. Returns
1077 DECL, or a modified version thereof. */
1080 maybe_push_decl (tree decl)
1082 tree type = TREE_TYPE (decl);
1084 /* Add this decl to the current binding level, but not if it comes
1085 from another scope, e.g. a static member variable. TEM may equal
1086 DECL or it may be a previous decl of the same name. */
1087 if (decl == error_mark_node
1088 || (TREE_CODE (decl) != PARM_DECL
1089 && DECL_CONTEXT (decl) != NULL_TREE
1090 /* Definitions of namespace members outside their namespace are
1092 && TREE_CODE (DECL_CONTEXT (decl)) != NAMESPACE_DECL)
1093 || (TREE_CODE (decl) == TEMPLATE_DECL && !namespace_bindings_p ())
1094 || TREE_CODE (type) == UNKNOWN_TYPE
1095 /* The declaration of a template specialization does not affect
1096 the functions available for overload resolution, so we do not
1098 || (TREE_CODE (decl) == FUNCTION_DECL
1099 && DECL_TEMPLATE_SPECIALIZATION (decl)))
1102 return pushdecl (decl);
1105 /* Bind DECL to ID in the current_binding_level, assumed to be a local
1106 binding level. If PUSH_USING is set in FLAGS, we know that DECL
1107 doesn't really belong to this binding level, that it got here
1108 through a using-declaration. */
1111 push_local_binding (tree id, tree decl, int flags)
1113 struct cp_binding_level *b;
1115 /* Skip over any local classes. This makes sense if we call
1116 push_local_binding with a friend decl of a local class. */
1117 b = innermost_nonclass_level ();
1119 if (lookup_name_innermost_nonclass_level (id))
1121 /* Supplement the existing binding. */
1122 if (!supplement_binding (IDENTIFIER_BINDING (id), decl))
1123 /* It didn't work. Something else must be bound at this
1124 level. Do not add DECL to the list of things to pop
1129 /* Create a new binding. */
1130 push_binding (id, decl, b);
1132 if (TREE_CODE (decl) == OVERLOAD || (flags & PUSH_USING))
1133 /* We must put the OVERLOAD into a TREE_LIST since the
1134 TREE_CHAIN of an OVERLOAD is already used. Similarly for
1135 decls that got here through a using-declaration. */
1136 decl = build_tree_list (NULL_TREE, decl);
1138 /* And put DECL on the list of things declared by the current
1140 add_decl_to_level (decl, b);
1143 /* Check to see whether or not DECL is a variable that would have been
1144 in scope under the ARM, but is not in scope under the ANSI/ISO
1145 standard. If so, issue an error message. If name lookup would
1146 work in both cases, but return a different result, this function
1147 returns the result of ANSI/ISO lookup. Otherwise, it returns
1151 check_for_out_of_scope_variable (tree decl)
1155 /* We only care about out of scope variables. */
1156 if (!(TREE_CODE (decl) == VAR_DECL && DECL_DEAD_FOR_LOCAL (decl)))
1159 shadowed = DECL_SHADOWED_FOR_VAR (decl);
1160 while (shadowed != NULL_TREE && TREE_CODE (shadowed) == VAR_DECL
1161 && DECL_DEAD_FOR_LOCAL (shadowed))
1162 shadowed = DECL_SHADOWED_FOR_VAR (shadowed);
1164 shadowed = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (decl));
1167 if (!DECL_ERROR_REPORTED (decl))
1169 warning ("name lookup of %qD changed", DECL_NAME (decl));
1170 cp_warning_at (" matches this %qD under ISO standard rules",
1172 cp_warning_at (" matches this %qD under old rules", decl);
1173 DECL_ERROR_REPORTED (decl) = 1;
1178 /* If we have already complained about this declaration, there's no
1179 need to do it again. */
1180 if (DECL_ERROR_REPORTED (decl))
1183 DECL_ERROR_REPORTED (decl) = 1;
1185 if (TREE_TYPE (decl) == error_mark_node)
1188 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
1190 error ("name lookup of %qD changed for new ISO %<for%> scoping",
1192 cp_error_at (" cannot use obsolete binding at %qD because "
1193 "it has a destructor", decl);
1194 return error_mark_node;
1198 pedwarn ("name lookup of %qD changed for new ISO %<for%> scoping",
1200 cp_pedwarn_at (" using obsolete binding at %qD", decl);
1206 /* true means unconditionally make a BLOCK for the next level pushed. */
1208 static bool keep_next_level_flag;
1210 static int binding_depth = 0;
1211 static int is_class_level = 0;
1218 for (i = 0; i < depth * 2; i++)
1222 /* Return a string describing the kind of SCOPE we have. */
1224 cxx_scope_descriptor (cxx_scope *scope)
1226 /* The order of this table must match the "scope_kind"
1228 static const char* scope_kind_names[] = {
1234 "function-parameter-scope",
1237 "template-parameter-scope",
1238 "template-explicit-spec-scope"
1240 const scope_kind kind = scope->explicit_spec_p
1241 ? sk_template_spec : scope->kind;
1243 return scope_kind_names[kind];
1246 /* Output a debugging information about SCOPE when performing
1249 cxx_scope_debug (cxx_scope *scope, int line, const char *action)
1251 const char *desc = cxx_scope_descriptor (scope);
1252 if (scope->this_entity)
1253 verbatim ("%s %s(%E) %p %d\n", action, desc,
1254 scope->this_entity, (void *) scope, line);
1256 verbatim ("%s %s %p %d\n", action, desc, (void *) scope, line);
1259 /* Return the estimated initial size of the hashtable of a NAMESPACE
1262 static inline size_t
1263 namespace_scope_ht_size (tree ns)
1265 tree name = DECL_NAME (ns);
1267 return name == std_identifier
1268 ? NAMESPACE_STD_HT_SIZE
1269 : (name == global_scope_name
1270 ? GLOBAL_SCOPE_HT_SIZE
1271 : NAMESPACE_ORDINARY_HT_SIZE);
1274 /* A chain of binding_level structures awaiting reuse. */
1276 static GTY((deletable)) struct cp_binding_level *free_binding_level;
1278 /* Insert SCOPE as the innermost binding level. */
1281 push_binding_level (struct cp_binding_level *scope)
1283 /* Add it to the front of currently active scopes stack. */
1284 scope->level_chain = current_binding_level;
1285 current_binding_level = scope;
1286 keep_next_level_flag = false;
1288 if (ENABLE_SCOPE_CHECKING)
1290 scope->binding_depth = binding_depth;
1291 indent (binding_depth);
1292 cxx_scope_debug (scope, input_line, "push");
1298 /* Create a new KIND scope and make it the top of the active scopes stack.
1299 ENTITY is the scope of the associated C++ entity (namespace, class,
1300 function); it is NULL otherwise. */
1303 begin_scope (scope_kind kind, tree entity)
1307 /* Reuse or create a struct for this binding level. */
1308 if (!ENABLE_SCOPE_CHECKING && free_binding_level)
1310 scope = free_binding_level;
1311 free_binding_level = scope->level_chain;
1314 scope = GGC_NEW (cxx_scope);
1315 memset (scope, 0, sizeof (cxx_scope));
1317 scope->this_entity = entity;
1318 scope->more_cleanups_ok = true;
1325 case sk_template_spec:
1326 scope->explicit_spec_p = true;
1327 kind = sk_template_parms;
1329 case sk_template_parms:
1335 case sk_function_parms:
1336 scope->keep = keep_next_level_flag;
1340 scope->type_decls = binding_table_new (namespace_scope_ht_size (entity));
1341 NAMESPACE_LEVEL (entity) = scope;
1342 VARRAY_TREE_INIT (scope->static_decls,
1343 DECL_NAME (entity) == std_identifier
1344 || DECL_NAME (entity) == global_scope_name
1346 "Static declarations");
1350 /* Should not happen. */
1356 push_binding_level (scope);
1361 /* We're about to leave current scope. Pop the top of the stack of
1362 currently active scopes. Return the enclosing scope, now active. */
1367 cxx_scope *scope = current_binding_level;
1369 if (scope->kind == sk_namespace && class_binding_level)
1370 current_binding_level = class_binding_level;
1372 /* We cannot leave a scope, if there are none left. */
1373 if (NAMESPACE_LEVEL (global_namespace))
1374 gcc_assert (!global_scope_p (scope));
1376 if (ENABLE_SCOPE_CHECKING)
1378 indent (--binding_depth);
1379 cxx_scope_debug (scope, input_line, "leave");
1380 if (is_class_level != (scope == class_binding_level))
1382 indent (binding_depth);
1383 verbatim ("XXX is_class_level != (current_scope == class_scope)\n");
1388 /* Move one nesting level up. */
1389 current_binding_level = scope->level_chain;
1391 /* Namespace-scopes are left most probably temporarily, not
1392 completely; they can be reopen later, e.g. in namespace-extension
1393 or any name binding activity that requires us to resume a
1394 namespace. For classes, we cache some binding levels. For other
1395 scopes, we just make the structure available for reuse. */
1396 if (scope->kind != sk_namespace
1397 && scope->kind != sk_class)
1399 scope->level_chain = free_binding_level;
1400 if (scope->kind == sk_class)
1401 scope->type_decls = NULL;
1403 binding_table_free (scope->type_decls);
1404 gcc_assert (!ENABLE_SCOPE_CHECKING
1405 || scope->binding_depth == binding_depth);
1406 free_binding_level = scope;
1409 /* Find the innermost enclosing class scope, and reset
1410 CLASS_BINDING_LEVEL appropriately. */
1411 for (scope = current_binding_level;
1412 scope && scope->kind != sk_class;
1413 scope = scope->level_chain)
1415 class_binding_level = scope && scope->kind == sk_class ? scope : NULL;
1417 return current_binding_level;
1421 resume_scope (struct cp_binding_level* b)
1423 /* Resuming binding levels is meant only for namespaces,
1424 and those cannot nest into classes. */
1425 gcc_assert (!class_binding_level);
1426 /* Also, resuming a non-directly nested namespace is a no-no. */
1427 gcc_assert (b->level_chain == current_binding_level);
1428 current_binding_level = b;
1429 if (ENABLE_SCOPE_CHECKING)
1431 b->binding_depth = binding_depth;
1432 indent (binding_depth);
1433 cxx_scope_debug (b, input_line, "resume");
1439 /* Return the innermost binding level that is not for a class scope. */
1442 innermost_nonclass_level (void)
1446 b = current_binding_level;
1447 while (b->kind == sk_class)
1453 /* We're defining an object of type TYPE. If it needs a cleanup, but
1454 we're not allowed to add any more objects with cleanups to the current
1455 scope, create a new binding level. */
1458 maybe_push_cleanup_level (tree type)
1460 if (type != error_mark_node
1461 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
1462 && current_binding_level->more_cleanups_ok == 0)
1464 begin_scope (sk_cleanup, NULL);
1465 current_binding_level->statement_list = push_stmt_list ();
1469 /* Nonzero if we are currently in the global binding level. */
1472 global_bindings_p (void)
1474 return global_scope_p (current_binding_level);
1477 /* True if we are currently in a toplevel binding level. This
1478 means either the global binding level or a namespace in a toplevel
1479 binding level. Since there are no non-toplevel namespace levels,
1480 this really means any namespace or template parameter level. We
1481 also include a class whose context is toplevel. */
1484 toplevel_bindings_p (void)
1486 struct cp_binding_level *b = innermost_nonclass_level ();
1488 return b->kind == sk_namespace || b->kind == sk_template_parms;
1491 /* True if this is a namespace scope, or if we are defining a class
1492 which is itself at namespace scope, or whose enclosing class is
1493 such a class, etc. */
1496 namespace_bindings_p (void)
1498 struct cp_binding_level *b = innermost_nonclass_level ();
1500 return b->kind == sk_namespace;
1503 /* True if the current level needs to have a BLOCK made. */
1508 return (current_binding_level->blocks != NULL_TREE
1509 || current_binding_level->keep
1510 || current_binding_level->kind == sk_cleanup
1511 || current_binding_level->names != NULL_TREE
1512 || current_binding_level->type_decls != NULL);
1515 /* Returns the kind of the innermost scope. */
1518 innermost_scope_kind (void)
1520 return current_binding_level->kind;
1523 /* Returns true if this scope was created to store template parameters. */
1526 template_parm_scope_p (void)
1528 return innermost_scope_kind () == sk_template_parms;
1531 /* If KEEP is true, make a BLOCK node for the next binding level,
1532 unconditionally. Otherwise, use the normal logic to decide whether
1533 or not to create a BLOCK. */
1536 keep_next_level (bool keep)
1538 keep_next_level_flag = keep;
1541 /* Return the list of declarations of the current level.
1542 Note that this list is in reverse order unless/until
1543 you nreverse it; and when you do nreverse it, you must
1544 store the result back using `storedecls' or you will lose. */
1549 return current_binding_level->names;
1552 /* Set the current binding TABLE for type declarations.. This is a
1553 temporary workaround of the fact that the data structure classtypes
1554 does not currently carry its allocated cxx_scope structure. */
1556 cxx_remember_type_decls (binding_table table)
1558 current_binding_level->type_decls = table;
1561 /* For debugging. */
1562 static int no_print_functions = 0;
1563 static int no_print_builtins = 0;
1565 /* Called from print_binding_level through binding_table_foreach to
1566 print the content of binding ENTRY. DATA is a pointer to line offset
1569 bt_print_entry (binding_entry entry, void *data)
1571 int *p = (int *) data;
1574 if (entry->name == NULL)
1576 else if (entry->name == TYPE_IDENTIFIER (entry->type))
1586 fprintf (stderr, "\n\t");
1589 if (entry->name == NULL)
1591 print_node_brief (stderr, "<unnamed-typedef", entry->type, 0);
1592 fprintf (stderr, ">");
1594 else if (entry->name == TYPE_IDENTIFIER (entry->type))
1595 print_node_brief (stderr, "", entry->type, 0);
1598 print_node_brief (stderr, "<typedef", entry->name, 0);
1599 print_node_brief (stderr, "", entry->type, 0);
1600 fprintf (stderr, ">");
1605 print_binding_level (struct cp_binding_level* lvl)
1609 fprintf (stderr, " blocks=" HOST_PTR_PRINTF, (void *) lvl->blocks);
1610 if (lvl->more_cleanups_ok)
1611 fprintf (stderr, " more-cleanups-ok");
1612 if (lvl->have_cleanups)
1613 fprintf (stderr, " have-cleanups");
1614 fprintf (stderr, "\n");
1617 fprintf (stderr, " names:\t");
1618 /* We can probably fit 3 names to a line? */
1619 for (t = lvl->names; t; t = TREE_CHAIN (t))
1621 if (no_print_functions && (TREE_CODE (t) == FUNCTION_DECL))
1623 if (no_print_builtins
1624 && (TREE_CODE (t) == TYPE_DECL)
1625 && DECL_IS_BUILTIN (t))
1628 /* Function decls tend to have longer names. */
1629 if (TREE_CODE (t) == FUNCTION_DECL)
1636 fprintf (stderr, "\n\t");
1639 print_node_brief (stderr, "", t, 0);
1640 if (t == error_mark_node)
1644 fprintf (stderr, "\n");
1646 if (lvl->type_decls)
1648 fprintf (stderr, " tags:\t");
1650 binding_table_foreach (lvl->type_decls, bt_print_entry, &i);
1652 fprintf (stderr, "\n");
1654 if (VEC_length (cp_class_binding, lvl->class_shadowed))
1657 cp_class_binding *b;
1658 fprintf (stderr, " class-shadowed:");
1660 VEC_iterate(cp_class_binding, lvl->class_shadowed, i, b);
1662 fprintf (stderr, " %s ", IDENTIFIER_POINTER (b->identifier));
1663 fprintf (stderr, "\n");
1665 if (lvl->type_shadowed)
1667 fprintf (stderr, " type-shadowed:");
1668 for (t = lvl->type_shadowed; t; t = TREE_CHAIN (t))
1670 fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
1672 fprintf (stderr, "\n");
1677 print_other_binding_stack (struct cp_binding_level *stack)
1679 struct cp_binding_level *level;
1680 for (level = stack; !global_scope_p (level); level = level->level_chain)
1682 fprintf (stderr, "binding level " HOST_PTR_PRINTF "\n", (void *) level);
1683 print_binding_level (level);
1688 print_binding_stack (void)
1690 struct cp_binding_level *b;
1691 fprintf (stderr, "current_binding_level=" HOST_PTR_PRINTF
1692 "\nclass_binding_level=" HOST_PTR_PRINTF
1693 "\nNAMESPACE_LEVEL (global_namespace)=" HOST_PTR_PRINTF "\n",
1694 (void *) current_binding_level, (void *) class_binding_level,
1695 (void *) NAMESPACE_LEVEL (global_namespace));
1696 if (class_binding_level)
1698 for (b = class_binding_level; b; b = b->level_chain)
1699 if (b == current_binding_level)
1702 b = class_binding_level;
1704 b = current_binding_level;
1707 b = current_binding_level;
1708 print_other_binding_stack (b);
1709 fprintf (stderr, "global:\n");
1710 print_binding_level (NAMESPACE_LEVEL (global_namespace));
1713 /* Return the type associated with id. */
1716 identifier_type_value (tree id)
1718 timevar_push (TV_NAME_LOOKUP);
1719 /* There is no type with that name, anywhere. */
1720 if (REAL_IDENTIFIER_TYPE_VALUE (id) == NULL_TREE)
1721 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
1722 /* This is not the type marker, but the real thing. */
1723 if (REAL_IDENTIFIER_TYPE_VALUE (id) != global_type_node)
1724 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, REAL_IDENTIFIER_TYPE_VALUE (id));
1725 /* Have to search for it. It must be on the global level, now.
1726 Ask lookup_name not to return non-types. */
1727 id = lookup_name_real (id, 2, 1, /*block_p=*/true, 0, LOOKUP_COMPLAIN);
1729 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, TREE_TYPE (id));
1730 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
1733 /* Return the IDENTIFIER_GLOBAL_VALUE of T, for use in common code, since
1734 the definition of IDENTIFIER_GLOBAL_VALUE is different for C and C++. */
1737 identifier_global_value (tree t)
1739 return IDENTIFIER_GLOBAL_VALUE (t);
1742 /* Push a definition of struct, union or enum tag named ID. into
1743 binding_level B. DECL is a TYPE_DECL for the type. We assume that
1744 the tag ID is not already defined. */
1747 set_identifier_type_value_with_scope (tree id, tree decl, cxx_scope *b)
1751 if (b->kind != sk_namespace)
1753 /* Shadow the marker, not the real thing, so that the marker
1754 gets restored later. */
1755 tree old_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
1757 = tree_cons (id, old_type_value, b->type_shadowed);
1758 type = decl ? TREE_TYPE (decl) : NULL_TREE;
1759 TREE_TYPE (b->type_shadowed) = type;
1763 cxx_binding *binding =
1764 binding_for_name (NAMESPACE_LEVEL (current_namespace), id);
1767 supplement_binding (binding, decl);
1769 binding->value = decl;
1771 /* Store marker instead of real type. */
1772 type = global_type_node;
1774 SET_IDENTIFIER_TYPE_VALUE (id, type);
1777 /* As set_identifier_type_value_with_scope, but using
1778 current_binding_level. */
1781 set_identifier_type_value (tree id, tree decl)
1783 set_identifier_type_value_with_scope (id, decl, current_binding_level);
1786 /* Return the name for the constructor (or destructor) for the
1787 specified class TYPE. When given a template, this routine doesn't
1788 lose the specialization. */
1791 constructor_name_full (tree type)
1793 return TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
1796 /* Return the name for the constructor (or destructor) for the
1797 specified class. When given a template, return the plain
1798 unspecialized name. */
1801 constructor_name (tree type)
1804 name = constructor_name_full (type);
1805 if (IDENTIFIER_TEMPLATE (name))
1806 name = IDENTIFIER_TEMPLATE (name);
1810 /* Returns TRUE if NAME is the name for the constructor for TYPE. */
1813 constructor_name_p (tree name, tree type)
1820 if (TREE_CODE (name) != IDENTIFIER_NODE)
1823 ctor_name = constructor_name_full (type);
1824 if (name == ctor_name)
1826 if (IDENTIFIER_TEMPLATE (ctor_name)
1827 && name == IDENTIFIER_TEMPLATE (ctor_name))
1832 /* Counter used to create anonymous type names. */
1834 static GTY(()) int anon_cnt;
1836 /* Return an IDENTIFIER which can be used as a name for
1837 anonymous structs and unions. */
1840 make_anon_name (void)
1844 sprintf (buf, ANON_AGGRNAME_FORMAT, anon_cnt++);
1845 return get_identifier (buf);
1848 /* Clear the TREE_PURPOSE slot of UTDs which have anonymous typenames.
1849 This keeps dbxout from getting confused. */
1852 clear_anon_tags (void)
1854 struct cp_binding_level *b;
1855 static int last_cnt = 0;
1857 /* Fast out if no new anon names were declared. */
1858 if (last_cnt == anon_cnt)
1861 b = current_binding_level;
1862 while (b->kind == sk_cleanup)
1864 if (b->type_decls != NULL)
1865 binding_table_remove_anonymous_types (b->type_decls);
1866 last_cnt = anon_cnt;
1869 /* Return (from the stack of) the BINDING, if any, established at SCOPE. */
1871 static inline cxx_binding *
1872 find_binding (cxx_scope *scope, cxx_binding *binding)
1874 timevar_push (TV_NAME_LOOKUP);
1876 for (; binding != NULL; binding = binding->previous)
1877 if (binding->scope == scope)
1878 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, binding);
1880 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, (cxx_binding *)0);
1883 /* Return the binding for NAME in SCOPE, if any. Otherwise, return NULL. */
1885 static inline cxx_binding *
1886 cxx_scope_find_binding_for_name (cxx_scope *scope, tree name)
1888 cxx_binding *b = IDENTIFIER_NAMESPACE_BINDINGS (name);
1891 /* Fold-in case where NAME is used only once. */
1892 if (scope == b->scope && b->previous == NULL)
1894 return find_binding (scope, b);
1899 /* Always returns a binding for name in scope. If no binding is
1900 found, make a new one. */
1902 static cxx_binding *
1903 binding_for_name (cxx_scope *scope, tree name)
1905 cxx_binding *result;
1907 result = cxx_scope_find_binding_for_name (scope, name);
1910 /* Not found, make a new one. */
1911 result = cxx_binding_make (NULL, NULL);
1912 result->previous = IDENTIFIER_NAMESPACE_BINDINGS (name);
1913 result->scope = scope;
1914 result->is_local = false;
1915 result->value_is_inherited = false;
1916 IDENTIFIER_NAMESPACE_BINDINGS (name) = result;
1920 /* Insert another USING_DECL into the current binding level, returning
1921 this declaration. If this is a redeclaration, do nothing, and
1922 return NULL_TREE if this not in namespace scope (in namespace
1923 scope, a using decl might extend any previous bindings). */
1926 push_using_decl (tree scope, tree name)
1930 timevar_push (TV_NAME_LOOKUP);
1931 gcc_assert (TREE_CODE (scope) == NAMESPACE_DECL);
1932 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
1933 for (decl = current_binding_level->usings; decl; decl = TREE_CHAIN (decl))
1934 if (DECL_INITIAL (decl) == scope && DECL_NAME (decl) == name)
1937 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP,
1938 namespace_bindings_p () ? decl : NULL_TREE);
1939 decl = build_lang_decl (USING_DECL, name, void_type_node);
1940 DECL_INITIAL (decl) = scope;
1941 TREE_CHAIN (decl) = current_binding_level->usings;
1942 current_binding_level->usings = decl;
1943 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
1946 /* Same as pushdecl, but define X in binding-level LEVEL. We rely on the
1947 caller to set DECL_CONTEXT properly. */
1950 pushdecl_with_scope (tree x, cxx_scope *level)
1952 struct cp_binding_level *b;
1953 tree function_decl = current_function_decl;
1955 timevar_push (TV_NAME_LOOKUP);
1956 current_function_decl = NULL_TREE;
1957 if (level->kind == sk_class)
1959 b = class_binding_level;
1960 class_binding_level = level;
1961 pushdecl_class_level (x);
1962 class_binding_level = b;
1966 b = current_binding_level;
1967 current_binding_level = level;
1969 current_binding_level = b;
1971 current_function_decl = function_decl;
1972 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
1975 /* DECL is a FUNCTION_DECL for a non-member function, which may have
1976 other definitions already in place. We get around this by making
1977 the value of the identifier point to a list of all the things that
1978 want to be referenced by that name. It is then up to the users of
1979 that name to decide what to do with that list.
1981 DECL may also be a TEMPLATE_DECL, with a FUNCTION_DECL in its
1982 DECL_TEMPLATE_RESULT. It is dealt with the same way.
1984 FLAGS is a bitwise-or of the following values:
1985 PUSH_LOCAL: Bind DECL in the current scope, rather than at
1987 PUSH_USING: DECL is being pushed as the result of a using
1990 The value returned may be a previous declaration if we guessed wrong
1991 about what language DECL should belong to (C or C++). Otherwise,
1992 it's always DECL (and never something that's not a _DECL). */
1995 push_overloaded_decl (tree decl, int flags)
1997 tree name = DECL_NAME (decl);
2000 int doing_global = (namespace_bindings_p () || !(flags & PUSH_LOCAL));
2002 timevar_push (TV_NAME_LOOKUP);
2004 old = namespace_binding (name, DECL_CONTEXT (decl));
2006 old = lookup_name_innermost_nonclass_level (name);
2010 if (TREE_CODE (old) == TYPE_DECL && DECL_ARTIFICIAL (old))
2012 tree t = TREE_TYPE (old);
2013 if (IS_AGGR_TYPE (t) && warn_shadow
2014 && (! DECL_IN_SYSTEM_HEADER (decl)
2015 || ! DECL_IN_SYSTEM_HEADER (old)))
2016 warning ("%q#D hides constructor for %q#T", decl, t);
2019 else if (is_overloaded_fn (old))
2023 for (tmp = old; tmp; tmp = OVL_NEXT (tmp))
2025 tree fn = OVL_CURRENT (tmp);
2027 if (TREE_CODE (tmp) == OVERLOAD && OVL_USED (tmp)
2028 && !(flags & PUSH_USING)
2029 && compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
2030 TYPE_ARG_TYPES (TREE_TYPE (decl))))
2031 error ("%q#D conflicts with previous using declaration %q#D",
2034 if (duplicate_decls (decl, fn) == fn)
2035 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, fn);
2038 else if (old == error_mark_node)
2039 /* Ignore the undefined symbol marker. */
2043 cp_error_at ("previous non-function declaration %q#D", old);
2044 error ("conflicts with function declaration %q#D", decl);
2045 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
2049 if (old || TREE_CODE (decl) == TEMPLATE_DECL
2050 /* If it's a using declaration, we always need to build an OVERLOAD,
2051 because it's the only way to remember that the declaration comes
2052 from 'using', and have the lookup behave correctly. */
2053 || (flags & PUSH_USING))
2055 if (old && TREE_CODE (old) != OVERLOAD)
2056 new_binding = ovl_cons (decl, ovl_cons (old, NULL_TREE));
2058 new_binding = ovl_cons (decl, old);
2059 if (flags & PUSH_USING)
2060 OVL_USED (new_binding) = 1;
2063 /* NAME is not ambiguous. */
2067 set_namespace_binding (name, current_namespace, new_binding);
2070 /* We only create an OVERLOAD if there was a previous binding at
2071 this level, or if decl is a template. In the former case, we
2072 need to remove the old binding and replace it with the new
2073 binding. We must also run through the NAMES on the binding
2074 level where the name was bound to update the chain. */
2076 if (TREE_CODE (new_binding) == OVERLOAD && old)
2080 for (d = &IDENTIFIER_BINDING (name)->scope->names;
2082 d = &TREE_CHAIN (*d))
2084 || (TREE_CODE (*d) == TREE_LIST
2085 && TREE_VALUE (*d) == old))
2087 if (TREE_CODE (*d) == TREE_LIST)
2088 /* Just replace the old binding with the new. */
2089 TREE_VALUE (*d) = new_binding;
2091 /* Build a TREE_LIST to wrap the OVERLOAD. */
2092 *d = tree_cons (NULL_TREE, new_binding,
2095 /* And update the cxx_binding node. */
2096 IDENTIFIER_BINDING (name)->value = new_binding;
2097 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
2100 /* We should always find a previous binding in this case. */
2104 /* Install the new binding. */
2105 push_local_binding (name, new_binding, flags);
2108 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
2111 /* Check a non-member using-declaration. Return the name and scope
2112 being used, and the USING_DECL, or NULL_TREE on failure. */
2115 validate_nonmember_using_decl (tree decl, tree scope, tree name)
2117 /* [namespace.udecl]
2118 A using-declaration for a class member shall be a
2119 member-declaration. */
2122 error ("%qT is not a namespace", scope);
2125 else if (scope == error_mark_node)
2128 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR)
2131 A using-declaration shall not name a template-id. */
2132 error ("a using-declaration cannot specify a template-id. "
2133 "Try %<using %D%>", name);
2137 if (TREE_CODE (decl) == NAMESPACE_DECL)
2139 error ("namespace %qD not allowed in using-declaration", decl);
2143 if (TREE_CODE (decl) == SCOPE_REF)
2145 /* It's a nested name with template parameter dependent scope.
2146 This can only be using-declaration for class member. */
2147 error ("%qT is not a namespace", TREE_OPERAND (decl, 0));
2151 if (is_overloaded_fn (decl))
2152 decl = get_first_fn (decl);
2154 gcc_assert (DECL_P (decl));
2156 /* Make a USING_DECL. */
2157 return push_using_decl (scope, name);
2160 /* Process local and global using-declarations. */
2163 do_nonmember_using_decl (tree scope, tree name, tree oldval, tree oldtype,
2164 tree *newval, tree *newtype)
2166 struct scope_binding decls = EMPTY_SCOPE_BINDING;
2168 *newval = *newtype = NULL_TREE;
2169 if (!qualified_lookup_using_namespace (name, scope, &decls, 0))
2173 if (!decls.value && !decls.type)
2175 error ("%qD not declared", name);
2179 /* Check for using functions. */
2180 if (decls.value && is_overloaded_fn (decls.value))
2184 if (oldval && !is_overloaded_fn (oldval))
2186 if (!DECL_IMPLICIT_TYPEDEF_P (oldval))
2187 error ("%qD is already declared in this scope", name);
2192 for (tmp = decls.value; tmp; tmp = OVL_NEXT (tmp))
2194 tree new_fn = OVL_CURRENT (tmp);
2196 /* [namespace.udecl]
2198 If a function declaration in namespace scope or block
2199 scope has the same name and the same parameter types as a
2200 function introduced by a using declaration the program is
2202 for (tmp1 = oldval; tmp1; tmp1 = OVL_NEXT (tmp1))
2204 tree old_fn = OVL_CURRENT (tmp1);
2206 if (new_fn == old_fn)
2207 /* The function already exists in the current namespace. */
2209 else if (OVL_USED (tmp1))
2210 continue; /* this is a using decl */
2211 else if (compparms (TYPE_ARG_TYPES (TREE_TYPE (new_fn)),
2212 TYPE_ARG_TYPES (TREE_TYPE (old_fn))))
2214 /* There was already a non-using declaration in
2215 this scope with the same parameter types. If both
2216 are the same extern "C" functions, that's ok. */
2217 if (decls_match (new_fn, old_fn))
2219 /* If the OLD_FN was a builtin, there is now a
2220 real declaration. */
2221 if (DECL_ANTICIPATED (old_fn))
2222 DECL_ANTICIPATED (old_fn) = 0;
2225 else if (!DECL_ANTICIPATED (old_fn))
2227 /* If the OLD_FN was really declared, the
2228 declarations don't match. */
2229 error ("%qD is already declared in this scope", name);
2233 /* If the OLD_FN was not really there, just ignore
2234 it and keep going. */
2238 /* If we broke out of the loop, there's no reason to add
2239 this function to the using declarations for this
2244 /* If we are adding to an existing OVERLOAD, then we no
2245 longer know the type of the set of functions. */
2246 if (*newval && TREE_CODE (*newval) == OVERLOAD)
2247 TREE_TYPE (*newval) = unknown_type_node;
2248 /* Add this new function to the set. */
2249 *newval = build_overload (OVL_CURRENT (tmp), *newval);
2250 /* If there is only one function, then we use its type. (A
2251 using-declaration naming a single function can be used in
2252 contexts where overload resolution cannot be
2254 if (TREE_CODE (*newval) != OVERLOAD)
2256 *newval = ovl_cons (*newval, NULL_TREE);
2257 TREE_TYPE (*newval) = TREE_TYPE (OVL_CURRENT (tmp));
2259 OVL_USED (*newval) = 1;
2264 *newval = decls.value;
2265 if (oldval && !decls_match (*newval, oldval))
2266 error ("%qD is already declared in this scope", name);
2269 *newtype = decls.type;
2270 if (oldtype && *newtype && !same_type_p (oldtype, *newtype))
2272 error ("using declaration %qD introduced ambiguous type %qT",
2278 /* Process a using-declaration at function scope. */
2281 do_local_using_decl (tree decl, tree scope, tree name)
2283 tree oldval, oldtype, newval, newtype;
2284 tree orig_decl = decl;
2286 decl = validate_nonmember_using_decl (decl, scope, name);
2287 if (decl == NULL_TREE)
2290 if (building_stmt_tree ()
2291 && at_function_scope_p ())
2292 add_decl_expr (decl);
2294 oldval = lookup_name_innermost_nonclass_level (name);
2295 oldtype = lookup_type_current_level (name);
2297 do_nonmember_using_decl (scope, name, oldval, oldtype, &newval, &newtype);
2301 if (is_overloaded_fn (newval))
2305 /* We only need to push declarations for those functions
2306 that were not already bound in the current level.
2307 The old value might be NULL_TREE, it might be a single
2308 function, or an OVERLOAD. */
2309 if (oldval && TREE_CODE (oldval) == OVERLOAD)
2310 term = OVL_FUNCTION (oldval);
2313 for (fn = newval; fn && OVL_CURRENT (fn) != term;
2315 push_overloaded_decl (OVL_CURRENT (fn),
2316 PUSH_LOCAL | PUSH_USING);
2319 push_local_binding (name, newval, PUSH_USING);
2323 push_local_binding (name, newtype, PUSH_USING);
2324 set_identifier_type_value (name, newtype);
2327 /* Emit debug info. */
2328 if (!processing_template_decl)
2329 cp_emit_debug_info_for_using (orig_decl, current_scope());
2332 /* Return the type that should be used when TYPE's name is preceded
2333 by a tag such as 'struct' or 'union', or null if the name cannot
2334 be used in this way.
2336 For example, when processing the third line of:
2342 lookup of A will find the typedef. Given A's typedef, this function
2343 will return the type associated with "struct A". For the tag to be
2344 anything other than TYPE, TYPE must be a typedef whose original type
2345 has the same name and context as TYPE itself.
2347 It is not valid for a typedef of an anonymous type to be used with
2350 typedef struct { ... } B;
2353 Return null for this case. */
2356 follow_tag_typedef (tree type)
2360 original = original_type (type);
2361 if (! TYPE_NAME (original))
2363 if (TYPE_IDENTIFIER (original) == TYPE_IDENTIFIER (type)
2364 && (CP_DECL_CONTEXT (TYPE_NAME (original))
2365 == CP_DECL_CONTEXT (TYPE_NAME (type)))
2366 && !(CLASS_TYPE_P (original) && TYPE_WAS_ANONYMOUS (original)))
2372 /* Given NAME, an IDENTIFIER_NODE,
2373 return the structure (or union or enum) definition for that name.
2374 Searches binding levels from its SCOPE up to the global level.
2375 If THISLEVEL_ONLY is nonzero, searches only the specified context
2376 (but skips any sk_cleanup contexts to find one that is
2377 meaningful for tags).
2378 FORM says which kind of type the caller wants;
2379 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
2380 If the wrong kind of type is found, and it's not a template, an error is
2384 lookup_tag (enum tree_code form, tree name,
2385 cxx_scope *binding_level, int thislevel_only)
2387 struct cp_binding_level *level;
2388 /* Nonzero if, we should look past a template parameter level, even
2389 if THISLEVEL_ONLY. */
2390 int allow_template_parms_p = 1;
2391 bool type_is_anonymous = ANON_AGGRNAME_P (name);
2393 timevar_push (TV_NAME_LOOKUP);
2394 for (level = binding_level; level; level = level->level_chain)
2397 if (type_is_anonymous && level->type_decls != NULL)
2399 tree type = binding_table_find_anon_type (level->type_decls, name);
2400 /* There is no need for error checking here, because
2401 anon names are unique throughout the compilation. */
2403 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, type);
2405 else if (level->kind == sk_namespace)
2406 /* Do namespace lookup. */
2407 for (tail = current_namespace; 1; tail = CP_DECL_CONTEXT (tail))
2409 cxx_binding *binding =
2410 cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (tail), name);
2412 if (binding && (binding->type
2414 && DECL_DECLARES_TYPE_P (binding->value))))
2418 /* If we just skipped past a template parameter level,
2419 even though THISLEVEL_ONLY, and we find a template
2420 class declaration, then we use the _TYPE node for the
2421 template. See the example below. */
2422 if (thislevel_only && !allow_template_parms_p
2424 && DECL_CLASS_TEMPLATE_P (binding->value))
2425 old = binding->value;
2427 old = binding->type ? binding->type : binding->value;
2429 /* We've found something at this binding level. If it is
2430 a typedef, extract the tag it refers to. Lookup fails
2431 if the typedef doesn't refer to a taggable type. */
2432 old = TREE_TYPE (old);
2433 old = follow_tag_typedef (old);
2435 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
2436 if (TREE_CODE (old) != form
2437 && (form == ENUMERAL_TYPE
2438 || TREE_CODE (old) == ENUMERAL_TYPE))
2440 error ("%q#D redeclared as %C", old, form);
2441 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
2443 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, old);
2445 if (thislevel_only || tail == global_namespace)
2446 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
2448 else if (level->type_decls != NULL)
2450 binding_entry entry = binding_table_find (level->type_decls, name);
2453 enum tree_code code = TREE_CODE (entry->type);
2456 && (form == ENUMERAL_TYPE || code == ENUMERAL_TYPE))
2458 /* Definition isn't the kind we were looking for. */
2459 error ("%q#D redeclared as %C", entry->type, form);
2460 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
2462 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, entry->type);
2465 if (thislevel_only && level->kind != sk_cleanup)
2467 if (level->kind == sk_template_parms && allow_template_parms_p)
2469 /* We must deal with cases like this:
2471 template <class T> struct S;
2472 template <class T> struct S {};
2474 When looking up `S', for the second declaration, we
2475 would like to find the first declaration. But, we
2476 are in the pseudo-global level created for the
2477 template parameters, rather than the (surrounding)
2478 namespace level. Thus, we keep going one more level,
2479 even though THISLEVEL_ONLY is nonzero. */
2480 allow_template_parms_p = 0;
2484 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
2487 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
2490 /* Given a type, find the tag that was defined for it and return the tag name.
2491 Otherwise return 0. However, the value can never be 0
2492 in the cases in which this is used.
2494 C++: If NAME is nonzero, this is the new name to install. This is
2495 done when replacing anonymous tags with real tag names. */
2498 lookup_tag_reverse (tree type, tree name)
2500 struct cp_binding_level *level;
2502 timevar_push (TV_NAME_LOOKUP);
2503 for (level = current_binding_level; level; level = level->level_chain)
2505 binding_entry entry = level->type_decls == NULL
2507 : binding_table_reverse_maybe_remap (level->type_decls, type, name);
2509 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, entry->name);
2511 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
2514 /* Returns true if ROOT (a namespace, class, or function) encloses
2515 CHILD. CHILD may be either a class type or a namespace. */
2518 is_ancestor (tree root, tree child)
2520 gcc_assert ((TREE_CODE (root) == NAMESPACE_DECL
2521 || TREE_CODE (root) == FUNCTION_DECL
2522 || CLASS_TYPE_P (root)));
2523 gcc_assert ((TREE_CODE (child) == NAMESPACE_DECL
2524 || CLASS_TYPE_P (child)));
2526 /* The global namespace encloses everything. */
2527 if (root == global_namespace)
2532 /* If we've run out of scopes, stop. */
2535 /* If we've reached the ROOT, it encloses CHILD. */
2538 /* Go out one level. */
2540 child = TYPE_NAME (child);
2541 child = DECL_CONTEXT (child);
2545 /* Enter the class or namespace scope indicated by T. Returns TRUE iff
2546 pop_scope should be called later to exit this scope. */
2553 if (TREE_CODE (t) == NAMESPACE_DECL)
2554 push_decl_namespace (t);
2555 else if (CLASS_TYPE_P (t))
2557 if (!at_class_scope_p ()
2558 || !same_type_p (current_class_type, t))
2559 push_nested_class (t);
2561 /* T is the same as the current scope. There is therefore no
2562 need to re-enter the scope. Since we are not actually
2563 pushing a new scope, our caller should not call
2571 /* Leave scope pushed by push_scope. */
2576 if (TREE_CODE (t) == NAMESPACE_DECL)
2577 pop_decl_namespace ();
2578 else if CLASS_TYPE_P (t)
2579 pop_nested_class ();
2582 /* Do a pushlevel for class declarations. */
2585 pushlevel_class (void)
2587 if (ENABLE_SCOPE_CHECKING)
2590 class_binding_level = begin_scope (sk_class, current_class_type);
2593 /* ...and a poplevel for class declarations. */
2596 poplevel_class (void)
2598 struct cp_binding_level *level = class_binding_level;
2599 cp_class_binding *cb;
2603 timevar_push (TV_NAME_LOOKUP);
2604 gcc_assert (level != 0);
2606 /* If we're leaving a toplevel class, cache its binding level. */
2607 if (current_class_depth == 1)
2608 previous_class_level = level;
2609 for (shadowed = level->type_shadowed;
2611 shadowed = TREE_CHAIN (shadowed))
2612 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (shadowed), TREE_VALUE (shadowed));
2614 /* Remove the bindings for all of the class-level declarations. */
2615 if (level->class_shadowed)
2618 VEC_iterate (cp_class_binding, level->class_shadowed, i, cb);
2620 IDENTIFIER_BINDING (cb->identifier) = cb->base.previous;
2621 ggc_free (level->class_shadowed);
2622 level->class_shadowed = NULL;
2625 /* Now, pop out of the binding level which we created up in the
2626 `pushlevel_class' routine. */
2627 if (ENABLE_SCOPE_CHECKING)
2631 timevar_pop (TV_NAME_LOOKUP);
2634 /* Set INHERITED_VALUE_BINDING_P on BINDING to true or false, as
2635 appropriate. DECL is the value to which a name has just been
2636 bound. CLASS_TYPE is the class in which the lookup occurred. */
2639 set_inherited_value_binding_p (cxx_binding *binding, tree decl,
2642 if (binding->value == decl && TREE_CODE (decl) != TREE_LIST)
2646 if (TREE_CODE (decl) == OVERLOAD)
2647 context = CP_DECL_CONTEXT (OVL_CURRENT (decl));
2650 gcc_assert (DECL_P (decl));
2651 context = context_for_name_lookup (decl);
2654 if (is_properly_derived_from (class_type, context))
2655 INHERITED_VALUE_BINDING_P (binding) = 1;
2657 INHERITED_VALUE_BINDING_P (binding) = 0;
2659 else if (binding->value == decl)
2660 /* We only encounter a TREE_LIST when there is an ambiguity in the
2661 base classes. Such an ambiguity can be overridden by a
2662 definition in this class. */
2663 INHERITED_VALUE_BINDING_P (binding) = 1;
2665 INHERITED_VALUE_BINDING_P (binding) = 0;
2668 /* Make the declaration of X appear in CLASS scope. */
2671 pushdecl_class_level (tree x)
2674 bool is_valid = true;
2676 timevar_push (TV_NAME_LOOKUP);
2677 /* Get the name of X. */
2678 if (TREE_CODE (x) == OVERLOAD)
2679 name = DECL_NAME (get_first_fn (x));
2681 name = DECL_NAME (x);
2685 is_valid = push_class_level_binding (name, x);
2686 if (TREE_CODE (x) == TYPE_DECL)
2687 set_identifier_type_value (name, x);
2689 else if (ANON_AGGR_TYPE_P (TREE_TYPE (x)))
2691 /* If X is an anonymous aggregate, all of its members are
2692 treated as if they were members of the class containing the
2693 aggregate, for naming purposes. */
2696 for (f = TYPE_FIELDS (TREE_TYPE (x)); f; f = TREE_CHAIN (f))
2698 location_t save_location = input_location;
2699 input_location = DECL_SOURCE_LOCATION (f);
2700 if (!pushdecl_class_level (f))
2702 input_location = save_location;
2705 timevar_pop (TV_NAME_LOOKUP);
2710 /* Return the BINDING (if any) for NAME in SCOPE, which is a class
2711 scope. If the value returned is non-NULL, and the PREVIOUS field
2712 is not set, callers must set the PREVIOUS field explicitly. */
2714 static cxx_binding *
2715 get_class_binding (tree name, cxx_scope *scope)
2720 cxx_binding *binding;
2722 class_type = scope->this_entity;
2724 /* Get the type binding. */
2725 type_binding = lookup_member (class_type, name,
2726 /*protect=*/2, /*want_type=*/true);
2727 /* Get the value binding. */
2728 value_binding = lookup_member (class_type, name,
2729 /*protect=*/2, /*want_type=*/false);
2732 && (TREE_CODE (value_binding) == TYPE_DECL
2733 || DECL_CLASS_TEMPLATE_P (value_binding)
2734 || (TREE_CODE (value_binding) == TREE_LIST
2735 && TREE_TYPE (value_binding) == error_mark_node
2736 && (TREE_CODE (TREE_VALUE (value_binding))
2738 /* We found a type binding, even when looking for a non-type
2739 binding. This means that we already processed this binding
2742 else if (value_binding)
2744 if (TREE_CODE (value_binding) == TREE_LIST
2745 && TREE_TYPE (value_binding) == error_mark_node)
2746 /* NAME is ambiguous. */
2748 else if (BASELINK_P (value_binding))
2749 /* NAME is some overloaded functions. */
2750 value_binding = BASELINK_FUNCTIONS (value_binding);
2753 /* If we found either a type binding or a value binding, create a
2754 new binding object. */
2755 if (type_binding || value_binding)
2757 binding = new_class_binding (name,
2761 /* This is a class-scope binding, not a block-scope binding. */
2762 LOCAL_BINDING_P (binding) = 0;
2763 set_inherited_value_binding_p (binding, value_binding, class_type);
2771 /* Make the declaration(s) of X appear in CLASS scope under the name
2772 NAME. Returns true if the binding is valid. */
2775 push_class_level_binding (tree name, tree x)
2777 cxx_binding *binding;
2781 timevar_push (TV_NAME_LOOKUP);
2782 /* The class_binding_level will be NULL if x is a template
2783 parameter name in a member template. */
2784 if (!class_binding_level)
2785 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, true);
2787 /* Check for invalid member names. */
2788 gcc_assert (TYPE_BEING_DEFINED (current_class_type));
2789 /* We could have been passed a tree list if this is an ambiguous
2790 declaration. If so, pull the declaration out because
2791 check_template_shadow will not handle a TREE_LIST. */
2792 if (TREE_CODE (decl) == TREE_LIST
2793 && TREE_TYPE (decl) == error_mark_node)
2794 decl = TREE_VALUE (decl);
2796 check_template_shadow (decl);
2800 If T is the name of a class, then each of the following shall
2801 have a name different from T:
2803 -- every static data member of class T;
2805 -- every member of class T that is itself a type;
2807 -- every enumerator of every member of class T that is an
2810 -- every member of every anonymous union that is a member of
2813 (Non-static data members were also forbidden to have the same
2814 name as T until TC1.) */
2815 if ((TREE_CODE (x) == VAR_DECL
2816 || TREE_CODE (x) == CONST_DECL
2817 || (TREE_CODE (x) == TYPE_DECL
2818 && !DECL_SELF_REFERENCE_P (x))
2819 /* A data member of an anonymous union. */
2820 || (TREE_CODE (x) == FIELD_DECL
2821 && DECL_CONTEXT (x) != current_class_type))
2822 && DECL_NAME (x) == constructor_name (current_class_type))
2824 tree scope = context_for_name_lookup (x);
2825 if (TYPE_P (scope) && same_type_p (scope, current_class_type))
2827 error ("%qD has the same name as the class in which it is "
2830 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, false);
2834 /* Get the current binding for NAME in this class, if any. */
2835 binding = IDENTIFIER_BINDING (name);
2836 if (!binding || binding->scope != class_binding_level)
2838 binding = get_class_binding (name, class_binding_level);
2839 /* If a new binding was created, put it at the front of the
2840 IDENTIFIER_BINDING list. */
2843 binding->previous = IDENTIFIER_BINDING (name);
2844 IDENTIFIER_BINDING (name) = binding;
2848 /* If there is already a binding, then we may need to update the
2850 if (binding && binding->value)
2852 tree bval = binding->value;
2853 tree old_decl = NULL_TREE;
2855 if (INHERITED_VALUE_BINDING_P (binding))
2857 /* If the old binding was from a base class, and was for a
2858 tag name, slide it over to make room for the new binding.
2859 The old binding is still visible if explicitly qualified
2860 with a class-key. */
2861 if (TREE_CODE (bval) == TYPE_DECL && DECL_ARTIFICIAL (bval)
2862 && !(TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x)))
2864 old_decl = binding->type;
2865 binding->type = bval;
2866 binding->value = NULL_TREE;
2867 INHERITED_VALUE_BINDING_P (binding) = 0;
2872 else if (TREE_CODE (x) == OVERLOAD && is_overloaded_fn (bval))
2874 else if (TREE_CODE (x) == USING_DECL && TREE_CODE (bval) == USING_DECL)
2875 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, true);
2876 else if (TREE_CODE (x) == USING_DECL && is_overloaded_fn (bval))
2878 else if (TREE_CODE (bval) == USING_DECL && is_overloaded_fn (x))
2879 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, true);
2881 if (old_decl && binding->scope == class_binding_level)
2884 /* It is always safe to clear INHERITED_VALUE_BINDING_P
2885 here. This function is only used to register bindings
2886 from with the class definition itself. */
2887 INHERITED_VALUE_BINDING_P (binding) = 0;
2888 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, true);
2892 /* Note that we declared this value so that we can issue an error if
2893 this is an invalid redeclaration of a name already used for some
2895 note_name_declared_in_class (name, decl);
2897 /* If we didn't replace an existing binding, put the binding on the
2898 stack of bindings for the identifier, and update the shadowed
2900 if (binding && binding->scope == class_binding_level)
2901 /* Supplement the existing binding. */
2902 ok = supplement_binding (binding, decl);
2905 /* Create a new binding. */
2906 push_binding (name, decl, class_binding_level);
2910 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, ok);
2914 do_class_using_decl (tree decl)
2916 tree name, value, scope, type;
2918 if (TREE_CODE (decl) != SCOPE_REF
2919 || !TREE_OPERAND (decl, 0)
2920 || !TYPE_P (TREE_OPERAND (decl, 0)))
2922 error ("using-declaration for non-member at class scope");
2925 scope = TREE_OPERAND (decl, 0);
2926 name = TREE_OPERAND (decl, 1);
2927 if (TREE_CODE (name) == BIT_NOT_EXPR)
2929 error ("using-declaration cannot name destructor");
2932 if (TREE_CODE (name) == TYPE_DECL)
2933 name = DECL_NAME (name);
2934 else if (TREE_CODE (name) == TEMPLATE_DECL)
2935 name = DECL_NAME (name);
2936 else if (BASELINK_P (name))
2938 tree fns = BASELINK_FUNCTIONS (name);
2939 name = DECL_NAME (get_first_fn (fns));
2942 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
2944 /* Dependent using decls have a NULL type, non-dependent ones have a
2946 type = dependent_type_p (scope) ? NULL_TREE : void_type_node;
2947 value = build_lang_decl (USING_DECL, name, type);
2948 DECL_INITIAL (value) = scope;
2950 if (scope && !processing_template_decl)
2954 r = lookup_qualified_name (scope, name, false, false);
2955 if (r && (DECL_P (r) || TREE_CODE (r) == OVERLOAD))
2956 cp_emit_debug_info_for_using (r, scope);
2962 /* Return the binding value for name in scope. */
2965 namespace_binding (tree name, tree scope)
2967 cxx_binding *binding;
2970 scope = global_namespace;
2971 scope = ORIGINAL_NAMESPACE (scope);
2972 binding = cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (scope), name);
2974 return binding ? binding->value : NULL_TREE;
2977 /* Set the binding value for name in scope. */
2980 set_namespace_binding (tree name, tree scope, tree val)
2984 timevar_push (TV_NAME_LOOKUP);
2985 if (scope == NULL_TREE)
2986 scope = global_namespace;
2987 b = binding_for_name (NAMESPACE_LEVEL (scope), name);
2988 if (!b->value || TREE_CODE (val) == OVERLOAD || val == error_mark_node)
2991 supplement_binding (b, val);
2992 timevar_pop (TV_NAME_LOOKUP);
2995 /* Set the context of a declaration to scope. Complain if we are not
2999 set_decl_namespace (tree decl, tree scope, bool friendp)
3003 /* Get rid of namespace aliases. */
3004 scope = ORIGINAL_NAMESPACE (scope);
3006 /* It is ok for friends to be qualified in parallel space. */
3007 if (!friendp && !is_ancestor (current_namespace, scope))
3008 error ("declaration of %qD not in a namespace surrounding %qD",
3010 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
3011 if (scope != current_namespace)
3013 /* See whether this has been declared in the namespace. */
3014 old = namespace_binding (DECL_NAME (decl), scope);
3016 /* No old declaration at all. */
3018 /* A template can be explicitly specialized in any namespace. */
3019 if (processing_explicit_instantiation)
3021 if (!is_overloaded_fn (decl))
3022 /* Don't compare non-function decls with decls_match here,
3023 since it can't check for the correct constness at this
3024 point. pushdecl will find those errors later. */
3026 /* Since decl is a function, old should contain a function decl. */
3027 if (!is_overloaded_fn (old))
3029 if (processing_template_decl || processing_specialization)
3030 /* We have not yet called push_template_decl to turn a
3031 FUNCTION_DECL into a TEMPLATE_DECL, so the declarations
3032 won't match. But, we'll check later, when we construct the
3035 if (is_overloaded_fn (old))
3037 for (; old; old = OVL_NEXT (old))
3038 if (decls_match (decl, OVL_CURRENT (old)))
3042 if (decls_match (decl, old))
3048 error ("%qD should have been declared inside %qD", decl, scope);
3051 /* Return the namespace where the current declaration is declared. */
3054 current_decl_namespace (void)
3057 /* If we have been pushed into a different namespace, use it. */
3058 if (decl_namespace_list)
3059 return TREE_PURPOSE (decl_namespace_list);
3061 if (current_class_type)
3062 result = decl_namespace_context (current_class_type);
3063 else if (current_function_decl)
3064 result = decl_namespace_context (current_function_decl);
3066 result = current_namespace;
3070 /* Push into the scope of the NAME namespace. If NAME is NULL_TREE, then we
3071 select a name that is unique to this compilation unit. */
3074 push_namespace (tree name)
3078 int implicit_use = 0;
3081 timevar_push (TV_NAME_LOOKUP);
3083 /* We should not get here if the global_namespace is not yet constructed
3084 nor if NAME designates the global namespace: The global scope is
3085 constructed elsewhere. */
3086 gcc_assert (global_namespace != NULL && name != global_scope_name);
3090 /* The name of anonymous namespace is unique for the translation
3092 if (!anonymous_namespace_name)
3093 anonymous_namespace_name = get_file_function_name ('N');
3094 name = anonymous_namespace_name;
3095 d = IDENTIFIER_NAMESPACE_VALUE (name);
3097 /* Reopening anonymous namespace. */
3103 /* Check whether this is an extended namespace definition. */
3104 d = IDENTIFIER_NAMESPACE_VALUE (name);
3105 if (d != NULL_TREE && TREE_CODE (d) == NAMESPACE_DECL)
3108 if (DECL_NAMESPACE_ALIAS (d))
3110 error ("namespace alias %qD not allowed here, assuming %qD",
3111 d, DECL_NAMESPACE_ALIAS (d));
3112 d = DECL_NAMESPACE_ALIAS (d);
3119 /* Make a new namespace, binding the name to it. */
3120 d = build_lang_decl (NAMESPACE_DECL, name, void_type_node);
3121 DECL_CONTEXT (d) = FROB_CONTEXT (current_namespace);
3125 /* Clear DECL_NAME for the benefit of debugging back ends. */
3126 SET_DECL_ASSEMBLER_NAME (d, name);
3127 DECL_NAME (d) = NULL_TREE;
3129 begin_scope (sk_namespace, d);
3132 resume_scope (NAMESPACE_LEVEL (d));
3135 do_using_directive (d);
3136 /* Enter the name space. */
3137 current_namespace = d;
3139 timevar_pop (TV_NAME_LOOKUP);
3142 /* Pop from the scope of the current namespace. */
3145 pop_namespace (void)
3147 gcc_assert (current_namespace != global_namespace);
3148 current_namespace = CP_DECL_CONTEXT (current_namespace);
3149 /* The binding level is not popped, as it might be re-opened later. */
3153 /* Push into the scope of the namespace NS, even if it is deeply
3154 nested within another namespace. */
3157 push_nested_namespace (tree ns)
3159 if (ns == global_namespace)
3160 push_to_top_level ();
3163 push_nested_namespace (CP_DECL_CONTEXT (ns));
3164 push_namespace (DECL_NAME (ns));
3168 /* Pop back from the scope of the namespace NS, which was previously
3169 entered with push_nested_namespace. */
3172 pop_nested_namespace (tree ns)
3174 timevar_push (TV_NAME_LOOKUP);
3175 while (ns != global_namespace)
3178 ns = CP_DECL_CONTEXT (ns);
3181 pop_from_top_level ();
3182 timevar_pop (TV_NAME_LOOKUP);
3185 /* Temporarily set the namespace for the current declaration. */
3188 push_decl_namespace (tree decl)
3190 if (TREE_CODE (decl) != NAMESPACE_DECL)
3191 decl = decl_namespace_context (decl);
3192 decl_namespace_list = tree_cons (ORIGINAL_NAMESPACE (decl),
3193 NULL_TREE, decl_namespace_list);
3196 /* [namespace.memdef]/2 */
3199 pop_decl_namespace (void)
3201 decl_namespace_list = TREE_CHAIN (decl_namespace_list);
3204 /* Return the namespace that is the common ancestor
3205 of two given namespaces. */
3208 namespace_ancestor (tree ns1, tree ns2)
3210 timevar_push (TV_NAME_LOOKUP);
3211 if (is_ancestor (ns1, ns2))
3212 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, ns1);
3213 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP,
3214 namespace_ancestor (CP_DECL_CONTEXT (ns1), ns2));
3217 /* Process a namespace-alias declaration. */
3220 do_namespace_alias (tree alias, tree namespace)
3222 if (TREE_CODE (namespace) != NAMESPACE_DECL)
3224 /* The parser did not find it, so it's not there. */
3225 error ("unknown namespace %qD", namespace);
3229 namespace = ORIGINAL_NAMESPACE (namespace);
3231 /* Build the alias. */
3232 alias = build_lang_decl (NAMESPACE_DECL, alias, void_type_node);
3233 DECL_NAMESPACE_ALIAS (alias) = namespace;
3234 DECL_EXTERNAL (alias) = 1;
3235 DECL_CONTEXT (alias) = FROB_CONTEXT (current_scope ());
3238 /* Emit debug info for namespace alias. */
3239 (*debug_hooks->global_decl) (alias);
3242 /* Like pushdecl, only it places X in the current namespace,
3246 pushdecl_namespace_level (tree x)
3248 struct cp_binding_level *b = current_binding_level;
3251 timevar_push (TV_NAME_LOOKUP);
3252 t = pushdecl_with_scope (x, NAMESPACE_LEVEL (current_namespace));
3254 /* Now, the type_shadowed stack may screw us. Munge it so it does
3256 if (TREE_CODE (x) == TYPE_DECL)
3258 tree name = DECL_NAME (x);
3260 tree *ptr = (tree *)0;
3261 for (; !global_scope_p (b); b = b->level_chain)
3263 tree shadowed = b->type_shadowed;
3264 for (; shadowed; shadowed = TREE_CHAIN (shadowed))
3265 if (TREE_PURPOSE (shadowed) == name)
3267 ptr = &TREE_VALUE (shadowed);
3268 /* Can't break out of the loop here because sometimes
3269 a binding level will have duplicate bindings for
3270 PT names. It's gross, but I haven't time to fix it. */
3273 newval = TREE_TYPE (x);
3274 if (ptr == (tree *)0)
3276 /* @@ This shouldn't be needed. My test case "zstring.cc" trips
3277 up here if this is changed to an assertion. --KR */
3278 SET_IDENTIFIER_TYPE_VALUE (name, x);
3285 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
3288 /* Insert USED into the using list of USER. Set INDIRECT_flag if this
3289 directive is not directly from the source. Also find the common
3290 ancestor and let our users know about the new namespace */
3292 add_using_namespace (tree user, tree used, bool indirect)
3295 timevar_push (TV_NAME_LOOKUP);
3296 /* Using oneself is a no-op. */
3299 timevar_pop (TV_NAME_LOOKUP);
3302 gcc_assert (TREE_CODE (user) == NAMESPACE_DECL);
3303 gcc_assert (TREE_CODE (used) == NAMESPACE_DECL);
3304 /* Check if we already have this. */
3305 t = purpose_member (used, DECL_NAMESPACE_USING (user));
3309 /* Promote to direct usage. */
3310 TREE_INDIRECT_USING (t) = 0;
3311 timevar_pop (TV_NAME_LOOKUP);
3315 /* Add used to the user's using list. */
3316 DECL_NAMESPACE_USING (user)
3317 = tree_cons (used, namespace_ancestor (user, used),
3318 DECL_NAMESPACE_USING (user));
3320 TREE_INDIRECT_USING (DECL_NAMESPACE_USING (user)) = indirect;
3322 /* Add user to the used's users list. */
3323 DECL_NAMESPACE_USERS (used)
3324 = tree_cons (user, 0, DECL_NAMESPACE_USERS (used));
3326 /* Recursively add all namespaces used. */
3327 for (t = DECL_NAMESPACE_USING (used); t; t = TREE_CHAIN (t))
3328 /* indirect usage */
3329 add_using_namespace (user, TREE_PURPOSE (t), 1);
3331 /* Tell everyone using us about the new used namespaces. */
3332 for (t = DECL_NAMESPACE_USERS (user); t; t = TREE_CHAIN (t))
3333 add_using_namespace (TREE_PURPOSE (t), used, 1);
3334 timevar_pop (TV_NAME_LOOKUP);
3337 /* Process a using-declaration not appearing in class or local scope. */
3340 do_toplevel_using_decl (tree decl, tree scope, tree name)
3342 tree oldval, oldtype, newval, newtype;
3343 tree orig_decl = decl;
3344 cxx_binding *binding;
3346 decl = validate_nonmember_using_decl (decl, scope, name);
3347 if (decl == NULL_TREE)
3350 binding = binding_for_name (NAMESPACE_LEVEL (current_namespace), name);
3352 oldval = binding->value;
3353 oldtype = binding->type;
3355 do_nonmember_using_decl (scope, name, oldval, oldtype, &newval, &newtype);
3357 /* Emit debug info. */
3358 if (!processing_template_decl)
3359 cp_emit_debug_info_for_using (orig_decl, current_namespace);
3361 /* Copy declarations found. */
3363 binding->value = newval;
3365 binding->type = newtype;
3369 /* Process a using-directive. */
3372 do_using_directive (tree namespace)
3374 tree context = NULL_TREE;
3376 if (building_stmt_tree ())
3377 add_stmt (build_stmt (USING_STMT, namespace));
3379 /* using namespace A::B::C; */
3380 if (TREE_CODE (namespace) == SCOPE_REF)
3381 namespace = TREE_OPERAND (namespace, 1);
3382 if (TREE_CODE (namespace) == IDENTIFIER_NODE)
3384 /* Lookup in lexer did not find a namespace. */
3385 if (!processing_template_decl)
3386 error ("namespace %qT undeclared", namespace);
3389 if (TREE_CODE (namespace) != NAMESPACE_DECL)
3391 if (!processing_template_decl)
3392 error ("%qT is not a namespace", namespace);
3395 namespace = ORIGINAL_NAMESPACE (namespace);
3396 if (!toplevel_bindings_p ())
3398 push_using_directive (namespace);
3399 context = current_scope ();
3404 add_using_namespace (current_namespace, namespace, 0);
3405 if (current_namespace != global_namespace)
3406 context = current_namespace;
3409 /* Emit debugging info. */
3410 if (!processing_template_decl)
3411 (*debug_hooks->imported_module_or_decl) (namespace, context);
3414 /* Deal with a using-directive seen by the parser. Currently we only
3415 handle attributes here, since they cannot appear inside a template. */
3418 parse_using_directive (tree namespace, tree attribs)
3422 do_using_directive (namespace);
3424 for (a = attribs; a; a = TREE_CHAIN (a))
3426 tree name = TREE_PURPOSE (a);
3427 if (is_attribute_p ("strong", name))
3429 if (!toplevel_bindings_p ())
3430 error ("strong using only meaningful at namespace scope");
3431 else if (namespace != error_mark_node)
3432 DECL_NAMESPACE_ASSOCIATIONS (namespace)
3433 = tree_cons (current_namespace, 0,
3434 DECL_NAMESPACE_ASSOCIATIONS (namespace));
3437 warning ("%qD attribute directive ignored", name);
3441 /* Like pushdecl, only it places X in the global scope if appropriate.
3442 Calls cp_finish_decl to register the variable, initializing it with
3443 *INIT, if INIT is non-NULL. */
3446 pushdecl_top_level_1 (tree x, tree *init)
3448 timevar_push (TV_NAME_LOOKUP);
3449 push_to_top_level ();
3450 x = pushdecl_namespace_level (x);
3452 cp_finish_decl (x, *init, NULL_TREE, 0);
3453 pop_from_top_level ();
3454 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
3457 /* Like pushdecl, only it places X in the global scope if appropriate. */
3460 pushdecl_top_level (tree x)
3462 return pushdecl_top_level_1 (x, NULL);
3465 /* Like pushdecl, only it places X in the global scope if
3466 appropriate. Calls cp_finish_decl to register the variable,
3467 initializing it with INIT. */
3470 pushdecl_top_level_and_finish (tree x, tree init)
3472 return pushdecl_top_level_1 (x, &init);
3475 /* Combines two sets of overloaded functions into an OVERLOAD chain, removing
3476 duplicates. The first list becomes the tail of the result.
3478 The algorithm is O(n^2). We could get this down to O(n log n) by
3479 doing a sort on the addresses of the functions, if that becomes
3483 merge_functions (tree s1, tree s2)
3485 for (; s2; s2 = OVL_NEXT (s2))
3487 tree fn2 = OVL_CURRENT (s2);
3490 for (fns1 = s1; fns1; fns1 = OVL_NEXT (fns1))
3492 tree fn1 = OVL_CURRENT (fns1);
3494 /* If the function from S2 is already in S1, there is no
3495 need to add it again. For `extern "C"' functions, we
3496 might have two FUNCTION_DECLs for the same function, in
3497 different namespaces; again, we only need one of them. */
3499 || (DECL_EXTERN_C_P (fn1) && DECL_EXTERN_C_P (fn2)
3500 && DECL_NAME (fn1) == DECL_NAME (fn2)))
3504 /* If we exhausted all of the functions in S1, FN2 is new. */
3506 s1 = build_overload (fn2, s1);
3511 /* This should return an error not all definitions define functions.
3512 It is not an error if we find two functions with exactly the
3513 same signature, only if these are selected in overload resolution.
3514 old is the current set of bindings, new the freshly-found binding.
3515 XXX Do we want to give *all* candidates in case of ambiguity?
3516 XXX In what way should I treat extern declarations?
3517 XXX I don't want to repeat the entire duplicate_decls here */
3520 ambiguous_decl (tree name, struct scope_binding *old, cxx_binding *new,
3524 gcc_assert (old != NULL);
3525 /* Copy the value. */
3528 switch (TREE_CODE (val))
3531 /* If we expect types or namespaces, and not templates,
3532 or this is not a template class. */
3533 if (LOOKUP_QUALIFIERS_ONLY (flags)
3534 && !DECL_CLASS_TEMPLATE_P (val))
3538 if (LOOKUP_NAMESPACES_ONLY (flags))
3541 case NAMESPACE_DECL:
3542 if (LOOKUP_TYPES_ONLY (flags))
3546 /* Ignore built-in functions that are still anticipated. */
3547 if (LOOKUP_QUALIFIERS_ONLY (flags) || DECL_ANTICIPATED (val))
3551 if (LOOKUP_QUALIFIERS_ONLY (flags))
3557 else if (val && val != old->value)
3559 if (is_overloaded_fn (old->value) && is_overloaded_fn (val))
3560 old->value = merge_functions (old->value, val);
3563 /* Some declarations are functions, some are not. */
3564 if (flags & LOOKUP_COMPLAIN)
3566 /* If we've already given this error for this lookup,
3567 old->value is error_mark_node, so let's not
3568 repeat ourselves. */
3569 if (old->value != error_mark_node)
3571 error ("use of %qD is ambiguous", name);
3572 cp_error_at (" first declared as %q#D here", old->value);
3574 cp_error_at (" also declared as %q#D here", val);
3576 old->value = error_mark_node;
3579 /* ... and copy the type. */
3581 if (LOOKUP_NAMESPACES_ONLY (flags))
3585 else if (type && old->type != type)
3587 if (flags & LOOKUP_COMPLAIN)
3589 error ("%qD denotes an ambiguous type",name);
3590 error ("%J first type here", TYPE_MAIN_DECL (old->type));
3591 error ("%J other type here", TYPE_MAIN_DECL (type));
3596 /* Return the declarations that are members of the namespace NS. */
3599 cp_namespace_decls (tree ns)
3601 return NAMESPACE_LEVEL (ns)->names;
3604 /* Combine prefer_type and namespaces_only into flags. */
3607 lookup_flags (int prefer_type, int namespaces_only)
3609 if (namespaces_only)
3610 return LOOKUP_PREFER_NAMESPACES;
3611 if (prefer_type > 1)
3612 return LOOKUP_PREFER_TYPES;
3613 if (prefer_type > 0)
3614 return LOOKUP_PREFER_BOTH;
3618 /* Given a lookup that returned VAL, use FLAGS to decide if we want to
3619 ignore it or not. Subroutine of lookup_name_real. */
3622 qualify_lookup (tree val, int flags)
3624 if (val == NULL_TREE)
3626 if ((flags & LOOKUP_PREFER_NAMESPACES) && TREE_CODE (val) == NAMESPACE_DECL)
3628 if ((flags & LOOKUP_PREFER_TYPES)
3629 && (TREE_CODE (val) == TYPE_DECL || TREE_CODE (val) == TEMPLATE_DECL))
3631 if (flags & (LOOKUP_PREFER_NAMESPACES | LOOKUP_PREFER_TYPES))
3636 /* Look up NAME in the NAMESPACE. */
3639 lookup_namespace_name (tree namespace, tree name)
3642 tree template_id = NULL_TREE;
3643 struct scope_binding binding = EMPTY_SCOPE_BINDING;
3645 timevar_push (TV_NAME_LOOKUP);
3646 gcc_assert (TREE_CODE (namespace) == NAMESPACE_DECL);
3648 if (TREE_CODE (name) == NAMESPACE_DECL)
3649 /* This happens for A::B<int> when B is a namespace. */
3650 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, name);
3651 else if (TREE_CODE (name) == TEMPLATE_DECL)
3653 /* This happens for A::B where B is a template, and there are no
3654 template arguments. */
3655 error ("invalid use of %qD", name);
3656 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
3659 namespace = ORIGINAL_NAMESPACE (namespace);
3661 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
3664 name = TREE_OPERAND (name, 0);
3665 if (TREE_CODE (name) == OVERLOAD)
3666 name = DECL_NAME (OVL_CURRENT (name));
3667 else if (DECL_P (name))
3668 name = DECL_NAME (name);
3671 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
3673 if (!qualified_lookup_using_namespace (name, namespace, &binding, 0))
3674 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
3678 val = binding.value;
3682 if (DECL_CLASS_TEMPLATE_P (val))
3683 val = lookup_template_class (val,
3684 TREE_OPERAND (template_id, 1),
3685 /*in_decl=*/NULL_TREE,
3686 /*context=*/NULL_TREE,
3687 /*entering_scope=*/0,
3688 tf_error | tf_warning);
3689 else if (DECL_FUNCTION_TEMPLATE_P (val)
3690 || TREE_CODE (val) == OVERLOAD)
3691 val = lookup_template_function (val,
3692 TREE_OPERAND (template_id, 1));
3695 error ("%<%D::%D%> is not a template", namespace, name);
3696 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
3700 /* If we have a single function from a using decl, pull it out. */
3701 if (TREE_CODE (val) == OVERLOAD && ! really_overloaded_fn (val))
3702 val = OVL_FUNCTION (val);
3704 /* Ignore built-in functions that haven't been prototyped yet. */
3705 if (!val || !DECL_P(val)
3706 || !DECL_LANG_SPECIFIC(val)
3707 || !DECL_ANTICIPATED (val))
3708 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
3711 error ("%qD undeclared in namespace %qD", name, namespace);
3712 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
3715 /* Select the right _DECL from multiple choices. */
3718 select_decl (const struct scope_binding *binding, int flags)
3721 val = binding->value;
3723 timevar_push (TV_NAME_LOOKUP);
3724 if (LOOKUP_NAMESPACES_ONLY (flags))
3726 /* We are not interested in types. */
3727 if (val && TREE_CODE (val) == NAMESPACE_DECL)
3728 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
3729 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
3732 /* If looking for a type, or if there is no non-type binding, select
3733 the value binding. */
3734 if (binding->type && (!val || (flags & LOOKUP_PREFER_TYPES)))
3735 val = binding->type;
3736 /* Don't return non-types if we really prefer types. */
3737 else if (val && LOOKUP_TYPES_ONLY (flags)
3738 && ! DECL_DECLARES_TYPE_P (val))
3741 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
3744 /* Unscoped lookup of a global: iterate over current namespaces,
3745 considering using-directives. */
3748 unqualified_namespace_lookup (tree name, int flags)
3750 tree initial = current_decl_namespace ();
3751 tree scope = initial;
3753 struct cp_binding_level *level;
3754 tree val = NULL_TREE;
3755 struct scope_binding binding = EMPTY_SCOPE_BINDING;
3757 timevar_push (TV_NAME_LOOKUP);
3759 for (; !val; scope = CP_DECL_CONTEXT (scope))
3762 cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (scope), name);
3766 if (b->value && DECL_P (b->value)
3767 && DECL_LANG_SPECIFIC (b->value)
3768 && DECL_ANTICIPATED (b->value))
3769 /* Ignore anticipated built-in functions. */
3772 binding.value = b->value;
3773 binding.type = b->type;
3776 /* Add all _DECLs seen through local using-directives. */
3777 for (level = current_binding_level;
3778 level->kind != sk_namespace;
3779 level = level->level_chain)
3780 if (!lookup_using_namespace (name, &binding, level->using_directives,
3782 /* Give up because of error. */
3783 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
3785 /* Add all _DECLs seen through global using-directives. */
3786 /* XXX local and global using lists should work equally. */
3790 if (!lookup_using_namespace (name, &binding,
3791 DECL_NAMESPACE_USING (siter),
3793 /* Give up because of error. */
3794 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
3795 if (siter == scope) break;
3796 siter = CP_DECL_CONTEXT (siter);
3799 val = select_decl (&binding, flags);
3800 if (scope == global_namespace)
3803 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
3806 /* Look up NAME (an IDENTIFIER_NODE) in SCOPE (either a NAMESPACE_DECL
3807 or a class TYPE). If IS_TYPE_P is TRUE, then ignore non-type
3810 Returns a DECL (or OVERLOAD, or BASELINK) representing the
3811 declaration found. If no suitable declaration can be found,
3812 ERROR_MARK_NODE is returned. If COMPLAIN is true and SCOPE is
3813 neither a class-type nor a namespace a diagnostic is issued. */
3816 lookup_qualified_name (tree scope, tree name, bool is_type_p, bool complain)
3820 if (TREE_CODE (scope) == NAMESPACE_DECL)
3822 struct scope_binding binding = EMPTY_SCOPE_BINDING;
3824 flags |= LOOKUP_COMPLAIN;
3826 flags |= LOOKUP_PREFER_TYPES;
3827 if (qualified_lookup_using_namespace (name, scope, &binding, flags))
3828 return select_decl (&binding, flags);
3830 else if (is_aggr_type (scope, complain))
3833 t = lookup_member (scope, name, 2, is_type_p);
3838 return error_mark_node;
3841 /* Subroutine of unqualified_namespace_lookup:
3842 Add the bindings of NAME in used namespaces to VAL.
3843 We are currently looking for names in namespace SCOPE, so we
3844 look through USINGS for using-directives of namespaces
3845 which have SCOPE as a common ancestor with the current scope.
3846 Returns false on errors. */
3849 lookup_using_namespace (tree name, struct scope_binding *val,
3850 tree usings, tree scope, int flags)
3853 timevar_push (TV_NAME_LOOKUP);
3854 /* Iterate over all used namespaces in current, searching for using
3855 directives of scope. */
3856 for (iter = usings; iter; iter = TREE_CHAIN (iter))
3857 if (TREE_VALUE (iter) == scope)
3859 tree used = ORIGINAL_NAMESPACE (TREE_PURPOSE (iter));
3861 cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (used), name);
3862 /* Resolve ambiguities. */
3864 ambiguous_decl (name, val, val1, flags);
3866 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val->value != error_mark_node);
3870 Accepts the NAME to lookup and its qualifying SCOPE.
3871 Returns the name/type pair found into the cxx_binding *RESULT,
3872 or false on error. */
3875 qualified_lookup_using_namespace (tree name, tree scope,
3876 struct scope_binding *result, int flags)
3878 /* Maintain a list of namespaces visited... */
3879 tree seen = NULL_TREE;
3880 /* ... and a list of namespace yet to see. */
3881 tree todo = NULL_TREE;
3882 tree todo_maybe = NULL_TREE;
3884 timevar_push (TV_NAME_LOOKUP);
3885 /* Look through namespace aliases. */
3886 scope = ORIGINAL_NAMESPACE (scope);
3887 while (scope && result->value != error_mark_node)
3889 cxx_binding *binding =
3890 cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (scope), name);
3891 seen = tree_cons (scope, NULL_TREE, seen);
3893 ambiguous_decl (name, result, binding, flags);
3895 /* Consider strong using directives always, and non-strong ones
3896 if we haven't found a binding yet. ??? Shouldn't we consider
3897 non-strong ones if the initial RESULT is non-NULL, but the
3898 binding in the given namespace is? */
3899 for (usings = DECL_NAMESPACE_USING (scope); usings;
3900 usings = TREE_CHAIN (usings))
3901 /* If this was a real directive, and we have not seen it. */
3902 if (!TREE_INDIRECT_USING (usings))
3904 /* Try to avoid queuing the same namespace more than once,
3905 the exception being when a namespace was already
3906 enqueued for todo_maybe and then a strong using is
3907 found for it. We could try to remove it from
3908 todo_maybe, but it's probably not worth the effort. */
3909 if (is_associated_namespace (scope, TREE_PURPOSE (usings))
3910 && !purpose_member (TREE_PURPOSE (usings), seen)
3911 && !purpose_member (TREE_PURPOSE (usings), todo))
3912 todo = tree_cons (TREE_PURPOSE (usings), NULL_TREE, todo);
3913 else if ((!result->value && !result->type)
3914 && !purpose_member (TREE_PURPOSE (usings), seen)
3915 && !purpose_member (TREE_PURPOSE (usings), todo)
3916 && !purpose_member (TREE_PURPOSE (usings), todo_maybe))
3917 todo_maybe = tree_cons (TREE_PURPOSE (usings), NULL_TREE,
3922 scope = TREE_PURPOSE (todo);
3923 todo = TREE_CHAIN (todo);
3926 && (!result->value && !result->type))
3928 scope = TREE_PURPOSE (todo_maybe);
3929 todo = TREE_CHAIN (todo_maybe);
3930 todo_maybe = NULL_TREE;
3933 scope = NULL_TREE; /* If there never was a todo list. */
3935 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, result->value != error_mark_node);
3938 /* Return the innermost non-namespace binding for NAME from a scope
3939 containing BINDING, or, if BINDING is NULL, the current scope. If
3940 CLASS_P is false, then class bindings are ignored. */
3943 outer_binding (tree name,
3944 cxx_binding *binding,
3949 cxx_scope *outer_scope;
3953 scope = binding->scope->level_chain;
3954 outer = binding->previous;
3958 scope = current_binding_level;
3959 outer = IDENTIFIER_BINDING (name);
3961 outer_scope = outer ? outer->scope : NULL;
3963 /* Because we create class bindings lazily, we might be missing a
3964 class binding for NAME. If there are any class binding levels
3965 between the LAST_BINDING_LEVEL and the scope in which OUTER was
3966 declared, we must lookup NAME in those class scopes. */
3968 while (scope && scope != outer_scope && scope->kind != sk_namespace)
3970 if (scope->kind == sk_class)
3972 cxx_binding *class_binding;
3974 class_binding = get_class_binding (name, scope);
3977 /* Thread this new class-scope binding onto the
3978 IDENTIFIER_BINDING list so that future lookups
3980 class_binding->previous = outer;
3982 binding->previous = class_binding;
3984 IDENTIFIER_BINDING (name) = class_binding;
3985 return class_binding;
3988 scope = scope->level_chain;
3994 /* Return the innermost block-scope or class-scope value binding for
3995 NAME, or NULL_TREE if there is no such binding. */
3998 innermost_non_namespace_value (tree name)
4000 cxx_binding *binding;
4001 binding = outer_binding (name, /*binding=*/NULL, /*class_p=*/true);
4002 return binding ? binding->value : NULL_TREE;
4005 /* Look up NAME in the current binding level and its superiors in the
4006 namespace of variables, functions and typedefs. Return a ..._DECL
4007 node of some kind representing its definition if there is only one
4008 such declaration, or return a TREE_LIST with all the overloaded
4009 definitions if there are many, or return 0 if it is undefined.
4011 If PREFER_TYPE is > 0, we prefer TYPE_DECLs or namespaces.
4012 If PREFER_TYPE is > 1, we reject non-type decls (e.g. namespaces).
4013 Otherwise we prefer non-TYPE_DECLs.
4015 If NONCLASS is nonzero, bindings in class scopes are ignored. If
4016 BLOCK_P is false, bindings in block scopes are ignored. */
4019 lookup_name_real (tree name, int prefer_type, int nonclass, bool block_p,
4020 int namespaces_only, int flags)
4023 tree val = NULL_TREE;
4025 timevar_push (TV_NAME_LOOKUP);
4026 /* Conversion operators are handled specially because ordinary
4027 unqualified name lookup will not find template conversion
4029 if (IDENTIFIER_TYPENAME_P (name))
4031 struct cp_binding_level *level;
4033 for (level = current_binding_level;
4034 level && level->kind != sk_namespace;
4035 level = level->level_chain)
4040 /* A conversion operator can only be declared in a class
4042 if (level->kind != sk_class)
4045 /* Lookup the conversion operator in the class. */
4046 class_type = level->this_entity;
4047 operators = lookup_fnfields (class_type, name, /*protect=*/0);
4049 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, operators);
4052 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
4055 flags |= lookup_flags (prefer_type, namespaces_only);
4057 /* First, look in non-namespace scopes. */
4059 if (current_class_type == NULL_TREE)
4062 if (block_p || !nonclass)
4063 for (iter = outer_binding (name, NULL, !nonclass);
4065 iter = outer_binding (name, iter, !nonclass))
4069 /* Skip entities we don't want. */
4070 if (LOCAL_BINDING_P (iter) ? !block_p : nonclass)
4073 /* If this is the kind of thing we're looking for, we're done. */
4074 if (qualify_lookup (iter->value, flags))
4075 binding = iter->value;
4076 else if ((flags & LOOKUP_PREFER_TYPES)
4077 && qualify_lookup (iter->type, flags))
4078 binding = iter->type;
4080 binding = NULL_TREE;
4089 /* Now lookup in namespace scopes. */
4091 val = unqualified_namespace_lookup (name, flags);
4095 /* If we have a single function from a using decl, pull it out. */
4096 if (TREE_CODE (val) == OVERLOAD && ! really_overloaded_fn (val))
4097 val = OVL_FUNCTION (val);
4100 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
4104 lookup_name_nonclass (tree name)
4106 return lookup_name_real (name, 0, 1, /*block_p=*/true, 0, LOOKUP_COMPLAIN);
4110 lookup_function_nonclass (tree name, tree args, bool block_p)
4113 lookup_arg_dependent (name,
4114 lookup_name_real (name, 0, 1, block_p, 0,
4120 lookup_name (tree name, int prefer_type)
4122 return lookup_name_real (name, prefer_type, 0, /*block_p=*/true,
4123 0, LOOKUP_COMPLAIN);
4126 /* Look up NAME for type used in elaborated name specifier in
4127 the scopes given by SCOPE. SCOPE can be either TS_CURRENT or
4128 TS_WITHIN_ENCLOSING_NON_CLASS (possibly more scope is checked if
4129 cleanup or template parameter scope is encountered).
4131 Unlike lookup_name_real, we make sure that NAME is actually
4132 declared in the desired scope, not from inheritance, using
4133 declaration, nor using directive. A TYPE_DECL best matching
4134 the NAME is returned. Catching error and issuing diagnostics
4135 are caller's responsibility. */
4138 lookup_type_scope (tree name, tag_scope scope)
4140 cxx_binding *iter = NULL;
4141 tree val = NULL_TREE;
4143 timevar_push (TV_NAME_LOOKUP);
4145 /* Look in non-namespace scope first. */
4146 if (current_binding_level->kind != sk_namespace)
4147 iter = outer_binding (name, NULL, /*class_p=*/ true);
4148 for (; iter; iter = outer_binding (name, iter, /*class_p=*/ true))
4150 /* Check if this is the kind of thing we're looking for.
4151 If SCOPE is TS_CURRENT, also make sure it doesn't come from
4152 base class. For ITER->VALUE, we can simply use
4153 INHERITED_VALUE_BINDING_P. For ITER->TYPE, we have to use
4156 We check ITER->TYPE before ITER->VALUE in order to handle
4157 typedef struct C {} C;
4160 if (qualify_lookup (iter->type, LOOKUP_PREFER_TYPES)
4161 && (scope != ts_current
4162 || LOCAL_BINDING_P (iter)
4163 || DECL_CONTEXT (iter->type) == iter->scope->this_entity))
4165 else if ((scope != ts_current
4166 || !INHERITED_VALUE_BINDING_P (iter))
4167 && qualify_lookup (iter->value, LOOKUP_PREFER_TYPES))
4174 /* Look in namespace scope. */
4177 iter = cxx_scope_find_binding_for_name
4178 (NAMESPACE_LEVEL (current_decl_namespace ()), name);
4182 /* If this is the kind of thing we're looking for, we're done.
4183 Ignore names found via using declaration. See DR138 for
4185 if (qualify_lookup (iter->type, LOOKUP_PREFER_TYPES)
4186 && (CP_DECL_CONTEXT (iter->type) == iter->scope->this_entity))
4188 else if (qualify_lookup (iter->value, LOOKUP_PREFER_TYPES)
4189 && (CP_DECL_CONTEXT (iter->value)
4190 == iter->scope->this_entity))
4196 /* Type found, check if it is in the allowed scopes, ignoring cleanup
4197 and template parameter scopes. */
4200 struct cp_binding_level *b = current_binding_level;
4203 if (iter->scope == b)
4204 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
4206 if (b->kind == sk_cleanup || b->kind == sk_template_parms)
4208 else if (b->kind == sk_class
4209 && scope == ts_within_enclosing_non_class)
4216 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
4219 /* Similar to `lookup_name' but look only in the innermost non-class
4223 lookup_name_innermost_nonclass_level (tree name)
4225 struct cp_binding_level *b;
4228 timevar_push (TV_NAME_LOOKUP);
4229 b = innermost_nonclass_level ();
4231 if (b->kind == sk_namespace)
4233 t = IDENTIFIER_NAMESPACE_VALUE (name);
4235 /* extern "C" function() */
4236 if (t != NULL_TREE && TREE_CODE (t) == TREE_LIST)
4239 else if (IDENTIFIER_BINDING (name)
4240 && LOCAL_BINDING_P (IDENTIFIER_BINDING (name)))
4242 cxx_binding *binding;
4243 binding = IDENTIFIER_BINDING (name);
4246 if (binding->scope == b
4247 && !(TREE_CODE (binding->value) == VAR_DECL
4248 && DECL_DEAD_FOR_LOCAL (binding->value)))
4249 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, binding->value);
4251 if (b->kind == sk_cleanup)
4258 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
4261 /* Like lookup_name_innermost_nonclass_level, but for types. */
4264 lookup_type_current_level (tree name)
4268 timevar_push (TV_NAME_LOOKUP);
4269 gcc_assert (current_binding_level->kind != sk_namespace);
4271 if (REAL_IDENTIFIER_TYPE_VALUE (name) != NULL_TREE
4272 && REAL_IDENTIFIER_TYPE_VALUE (name) != global_type_node)
4274 struct cp_binding_level *b = current_binding_level;
4277 if (purpose_member (name, b->type_shadowed))
4278 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP,
4279 REAL_IDENTIFIER_TYPE_VALUE (name));
4280 if (b->kind == sk_cleanup)
4287 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
4290 /* [basic.lookup.koenig] */
4291 /* A nonzero return value in the functions below indicates an error. */
4301 static bool arg_assoc (struct arg_lookup*, tree);
4302 static bool arg_assoc_args (struct arg_lookup*, tree);
4303 static bool arg_assoc_type (struct arg_lookup*, tree);
4304 static bool add_function (struct arg_lookup *, tree);
4305 static bool arg_assoc_namespace (struct arg_lookup *, tree);
4306 static bool arg_assoc_class (struct arg_lookup *, tree);
4307 static bool arg_assoc_template_arg (struct arg_lookup*, tree);
4309 /* Add a function to the lookup structure.
4310 Returns true on error. */
4313 add_function (struct arg_lookup *k, tree fn)
4315 /* We used to check here to see if the function was already in the list,
4316 but that's O(n^2), which is just too expensive for function lookup.
4317 Now we deal with the occasional duplicate in joust. In doing this, we
4318 assume that the number of duplicates will be small compared to the
4319 total number of functions being compared, which should usually be the
4322 /* We must find only functions, or exactly one non-function. */
4325 else if (fn == k->functions)
4327 else if (is_overloaded_fn (k->functions) && is_overloaded_fn (fn))
4328 k->functions = build_overload (fn, k->functions);
4331 tree f1 = OVL_CURRENT (k->functions);
4333 if (is_overloaded_fn (f1))
4335 fn = f1; f1 = f2; f2 = fn;
4337 cp_error_at ("%qD is not a function,", f1);
4338 cp_error_at (" conflict with %qD", f2);
4339 error (" in call to %qD", k->name);
4346 /* Returns true iff CURRENT has declared itself to be an associated
4347 namespace of SCOPE via a strong using-directive (or transitive chain
4348 thereof). Both are namespaces. */
4351 is_associated_namespace (tree current, tree scope)
4353 tree seen = NULL_TREE;
4354 tree todo = NULL_TREE;
4358 if (scope == current)
4360 seen = tree_cons (scope, NULL_TREE, seen);
4361 for (t = DECL_NAMESPACE_ASSOCIATIONS (scope); t; t = TREE_CHAIN (t))
4362 if (!purpose_member (TREE_PURPOSE (t), seen))
4363 todo = tree_cons (TREE_PURPOSE (t), NULL_TREE, todo);
4366 scope = TREE_PURPOSE (todo);
4367 todo = TREE_CHAIN (todo);
4374 /* Add functions of a namespace to the lookup structure.
4375 Returns true on error. */
4378 arg_assoc_namespace (struct arg_lookup *k, tree scope)
4382 if (purpose_member (scope, k->namespaces))
4384 k->namespaces = tree_cons (scope, NULL_TREE, k->namespaces);
4386 /* Check out our super-users. */
4387 for (value = DECL_NAMESPACE_ASSOCIATIONS (scope); value;
4388 value = TREE_CHAIN (value))
4389 if (arg_assoc_namespace (k, TREE_PURPOSE (value)))
4392 value = namespace_binding (k->name, scope);
4396 for (; value; value = OVL_NEXT (value))
4397 if (add_function (k, OVL_CURRENT (value)))
4403 /* Adds everything associated with a template argument to the lookup
4404 structure. Returns true on error. */
4407 arg_assoc_template_arg (struct arg_lookup *k, tree arg)
4409 /* [basic.lookup.koenig]
4411 If T is a template-id, its associated namespaces and classes are
4412 ... the namespaces and classes associated with the types of the
4413 template arguments provided for template type parameters
4414 (excluding template template parameters); the namespaces in which
4415 any template template arguments are defined; and the classes in
4416 which any member templates used as template template arguments
4417 are defined. [Note: non-type template arguments do not
4418 contribute to the set of associated namespaces. ] */
4420 /* Consider first template template arguments. */
4421 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
4422 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
4424 else if (TREE_CODE (arg) == TEMPLATE_DECL)
4426 tree ctx = CP_DECL_CONTEXT (arg);
4428 /* It's not a member template. */
4429 if (TREE_CODE (ctx) == NAMESPACE_DECL)
4430 return arg_assoc_namespace (k, ctx);
4431 /* Otherwise, it must be member template. */
4433 return arg_assoc_class (k, ctx);
4435 /* It's not a template template argument, but it is a type template
4437 else if (TYPE_P (arg))
4438 return arg_assoc_type (k, arg);
4439 /* It's a non-type template argument. */
4444 /* Adds everything associated with class to the lookup structure.
4445 Returns true on error. */
4448 arg_assoc_class (struct arg_lookup *k, tree type)
4450 tree list, friends, context;
4453 /* Backend build structures, such as __builtin_va_list, aren't
4454 affected by all this. */
4455 if (!CLASS_TYPE_P (type))
4458 if (purpose_member (type, k->classes))
4460 k->classes = tree_cons (type, NULL_TREE, k->classes);
4462 context = decl_namespace_context (type);
4463 if (arg_assoc_namespace (k, context))
4466 if (TYPE_BINFO (type))
4468 /* Process baseclasses. */
4469 tree binfo, base_binfo;
4471 for (binfo = TYPE_BINFO (type), i = 0;
4472 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
4473 if (arg_assoc_class (k, BINFO_TYPE (base_binfo)))
4477 /* Process friends. */
4478 for (list = DECL_FRIENDLIST (TYPE_MAIN_DECL (type)); list;
4479 list = TREE_CHAIN (list))
4480 if (k->name == FRIEND_NAME (list))
4481 for (friends = FRIEND_DECLS (list); friends;
4482 friends = TREE_CHAIN (friends))
4484 tree fn = TREE_VALUE (friends);
4486 /* Only interested in global functions with potentially hidden
4487 (i.e. unqualified) declarations. */
4488 if (CP_DECL_CONTEXT (fn) != context)
4490 /* Template specializations are never found by name lookup.
4491 (Templates themselves can be found, but not template
4492 specializations.) */
4493 if (TREE_CODE (fn) == FUNCTION_DECL && DECL_USE_TEMPLATE (fn))
4495 if (add_function (k, fn))
4499 /* Process template arguments. */
4500 if (CLASSTYPE_TEMPLATE_INFO (type)
4501 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
4503 list = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
4504 for (i = 0; i < TREE_VEC_LENGTH (list); ++i)
4505 arg_assoc_template_arg (k, TREE_VEC_ELT (list, i));
4511 /* Adds everything associated with a given type.
4512 Returns 1 on error. */
4515 arg_assoc_type (struct arg_lookup *k, tree type)
4517 /* As we do not get the type of non-type dependent expressions
4518 right, we can end up with such things without a type. */
4522 if (TYPE_PTRMEM_P (type))
4524 /* Pointer to member: associate class type and value type. */
4525 if (arg_assoc_type (k, TYPE_PTRMEM_CLASS_TYPE (type)))
4527 return arg_assoc_type (k, TYPE_PTRMEM_POINTED_TO_TYPE (type));
4529 else switch (TREE_CODE (type))
4542 if (TYPE_PTRMEMFUNC_P (type))
4543 return arg_assoc_type (k, TYPE_PTRMEMFUNC_FN_TYPE (type));
4544 return arg_assoc_class (k, type);
4546 case REFERENCE_TYPE:
4548 return arg_assoc_type (k, TREE_TYPE (type));
4551 return arg_assoc_namespace (k, decl_namespace_context (type));
4553 /* The basetype is referenced in the first arg type, so just
4556 /* Associate the parameter types. */
4557 if (arg_assoc_args (k, TYPE_ARG_TYPES (type)))
4559 /* Associate the return type. */
4560 return arg_assoc_type (k, TREE_TYPE (type));
4561 case TEMPLATE_TYPE_PARM:
4562 case BOUND_TEMPLATE_TEMPLATE_PARM:
4567 gcc_assert (type == unknown_type_node);
4575 /* Adds everything associated with arguments. Returns true on error. */
4578 arg_assoc_args (struct arg_lookup *k, tree args)
4580 for (; args; args = TREE_CHAIN (args))
4581 if (arg_assoc (k, TREE_VALUE (args)))
4586 /* Adds everything associated with a given tree_node. Returns 1 on error. */
4589 arg_assoc (struct arg_lookup *k, tree n)
4591 if (n == error_mark_node)
4595 return arg_assoc_type (k, n);
4597 if (! type_unknown_p (n))
4598 return arg_assoc_type (k, TREE_TYPE (n));
4600 if (TREE_CODE (n) == ADDR_EXPR)
4601 n = TREE_OPERAND (n, 0);
4602 if (TREE_CODE (n) == COMPONENT_REF)
4603 n = TREE_OPERAND (n, 1);
4604 if (TREE_CODE (n) == OFFSET_REF)
4605 n = TREE_OPERAND (n, 1);
4606 while (TREE_CODE (n) == TREE_LIST)
4608 if (TREE_CODE (n) == BASELINK)
4609 n = BASELINK_FUNCTIONS (n);
4611 if (TREE_CODE (n) == FUNCTION_DECL)
4612 return arg_assoc_type (k, TREE_TYPE (n));
4613 if (TREE_CODE (n) == TEMPLATE_ID_EXPR)
4615 /* [basic.lookup.koenig]
4617 If T is a template-id, its associated namespaces and classes
4618 are the namespace in which the template is defined; for
4619 member templates, the member template's class... */
4620 tree template = TREE_OPERAND (n, 0);
4621 tree args = TREE_OPERAND (n, 1);
4625 if (TREE_CODE (template) == COMPONENT_REF)
4626 template = TREE_OPERAND (template, 1);
4628 /* First, the template. There may actually be more than one if
4629 this is an overloaded function template. But, in that case,
4630 we only need the first; all the functions will be in the same
4632 template = OVL_CURRENT (template);
4634 ctx = CP_DECL_CONTEXT (template);
4636 if (TREE_CODE (ctx) == NAMESPACE_DECL)
4638 if (arg_assoc_namespace (k, ctx) == 1)
4641 /* It must be a member template. */
4642 else if (arg_assoc_class (k, ctx) == 1)
4645 /* Now the arguments. */
4646 for (ix = TREE_VEC_LENGTH (args); ix--;)
4647 if (arg_assoc_template_arg (k, TREE_VEC_ELT (args, ix)) == 1)
4650 else if (TREE_CODE (n) == OVERLOAD)
4652 for (; n; n = OVL_CHAIN (n))
4653 if (arg_assoc_type (k, TREE_TYPE (OVL_FUNCTION (n))))
4660 /* Performs Koenig lookup depending on arguments, where fns
4661 are the functions found in normal lookup. */
4664 lookup_arg_dependent (tree name, tree fns, tree args)
4666 struct arg_lookup k;
4667 tree fn = NULL_TREE;
4669 timevar_push (TV_NAME_LOOKUP);
4672 k.classes = NULL_TREE;
4674 /* We've already looked at some namespaces during normal unqualified
4675 lookup -- but we don't know exactly which ones. If the functions
4676 we found were brought into the current namespace via a using
4677 declaration, we have not really checked the namespace from which
4678 they came. Therefore, we check all namespaces here -- unless the
4679 function we have is from the current namespace. Even then, we
4680 must check all namespaces if the function is a local
4681 declaration; any other declarations present at namespace scope
4682 should be visible during argument-dependent lookup. */
4684 fn = OVL_CURRENT (fns);
4685 if (fn && TREE_CODE (fn) == FUNCTION_DECL
4686 && (CP_DECL_CONTEXT (fn) != current_decl_namespace ()
4687 || DECL_LOCAL_FUNCTION_P (fn)))
4688 k.namespaces = NULL_TREE;
4690 /* Setting NAMESPACES is purely an optimization; it prevents
4691 adding functions which are already in FNS. Adding them would
4692 be safe -- "joust" will eliminate the duplicates -- but
4694 k.namespaces = build_tree_list (current_decl_namespace (), NULL_TREE);
4696 arg_assoc_args (&k, args);
4697 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, k.functions);
4700 /* Add namespace to using_directives. Return NULL_TREE if nothing was
4701 changed (i.e. there was already a directive), or the fresh
4702 TREE_LIST otherwise. */
4705 push_using_directive (tree used)
4707 tree ud = current_binding_level->using_directives;
4708 tree iter, ancestor;
4710 timevar_push (TV_NAME_LOOKUP);
4711 /* Check if we already have this. */
4712 if (purpose_member (used, ud) != NULL_TREE)
4713 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
4715 ancestor = namespace_ancestor (current_decl_namespace (), used);
4716 ud = current_binding_level->using_directives;
4717 ud = tree_cons (used, ancestor, ud);
4718 current_binding_level->using_directives = ud;
4720 /* Recursively add all namespaces used. */
4721 for (iter = DECL_NAMESPACE_USING (used); iter; iter = TREE_CHAIN (iter))
4722 push_using_directive (TREE_PURPOSE (iter));
4724 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, ud);
4727 /* The type TYPE is being declared. If it is a class template, or a
4728 specialization of a class template, do any processing required and
4729 perform error-checking. If IS_FRIEND is nonzero, this TYPE is
4730 being declared a friend. B is the binding level at which this TYPE
4733 Returns the TYPE_DECL for TYPE, which may have been altered by this
4737 maybe_process_template_type_declaration (tree type, int globalize,
4740 tree decl = TYPE_NAME (type);
4742 if (processing_template_parmlist)
4743 /* You can't declare a new template type in a template parameter
4744 list. But, you can declare a non-template type:
4746 template <class A*> struct S;
4748 is a forward-declaration of `A'. */
4752 maybe_check_template_type (type);
4754 gcc_assert (IS_AGGR_TYPE (type) || TREE_CODE (type) == ENUMERAL_TYPE);
4756 if (processing_template_decl)
4758 /* This may change after the call to
4759 push_template_decl_real, but we want the original value. */
4760 tree name = DECL_NAME (decl);
4762 decl = push_template_decl_real (decl, globalize);
4763 /* If the current binding level is the binding level for the
4764 template parameters (see the comment in
4765 begin_template_parm_list) and the enclosing level is a class
4766 scope, and we're not looking at a friend, push the
4767 declaration of the member class into the class scope. In the
4768 friend case, push_template_decl will already have put the
4769 friend into global scope, if appropriate. */
4770 if (TREE_CODE (type) != ENUMERAL_TYPE
4771 && !globalize && b->kind == sk_template_parms
4772 && b->level_chain->kind == sk_class)
4774 finish_member_declaration (CLASSTYPE_TI_TEMPLATE (type));
4775 /* Put this UDT in the table of UDTs for the class, since
4776 that won't happen below because B is not the class
4777 binding level, but is instead the pseudo-global level. */
4778 if (b->level_chain->type_decls == NULL)
4779 b->level_chain->type_decls =
4780 binding_table_new (SCOPE_DEFAULT_HT_SIZE);
4781 binding_table_insert (b->level_chain->type_decls, name, type);
4782 if (!COMPLETE_TYPE_P (current_class_type))
4784 maybe_add_class_template_decl_list (current_class_type,
4785 type, /*friend_p=*/0);
4786 CLASSTYPE_NESTED_UTDS (current_class_type) =
4787 b->level_chain->type_decls;
4796 /* Push a tag name NAME for struct/class/union/enum type TYPE.
4797 Normally put it into the inner-most non-sk_cleanup scope,
4798 but if GLOBALIZE is true, put it in the inner-most non-class scope.
4799 The latter is needed for implicit declarations. */
4802 pushtag (tree name, tree type, int globalize)
4804 struct cp_binding_level *b;
4806 timevar_push (TV_NAME_LOOKUP);
4807 b = current_binding_level;
4808 while (/* Cleanup scopes are not scopes from the point of view of
4810 b->kind == sk_cleanup
4811 /* Neither are the scopes used to hold template parameters
4812 for an explicit specialization. For an ordinary template
4813 declaration, these scopes are not scopes from the point of
4814 view of the language -- but we need a place to stash
4815 things that will go in the containing namespace when the
4816 template is instantiated. */
4817 || (b->kind == sk_template_parms && b->explicit_spec_p)
4818 || (b->kind == sk_class
4820 /* We may be defining a new type in the initializer
4821 of a static member variable. We allow this when
4822 not pedantic, and it is particularly useful for
4823 type punning via an anonymous union. */
4824 || COMPLETE_TYPE_P (b->this_entity))))
4827 if (b->type_decls == NULL)
4828 b->type_decls = binding_table_new (SCOPE_DEFAULT_HT_SIZE);
4829 binding_table_insert (b->type_decls, name, type);
4833 /* Do C++ gratuitous typedefing. */
4834 if (IDENTIFIER_TYPE_VALUE (name) != type)
4838 tree context = TYPE_CONTEXT (type);
4842 tree cs = current_scope ();
4846 else if (cs != NULL_TREE && TYPE_P (cs))
4847 /* When declaring a friend class of a local class, we want
4848 to inject the newly named class into the scope
4849 containing the local class, not the namespace scope. */
4850 context = decl_function_context (get_type_decl (cs));
4853 context = current_namespace;
4855 if (b->kind == sk_class
4856 || (b->kind == sk_template_parms
4857 && b->level_chain->kind == sk_class))
4860 if (current_lang_name == lang_name_java)
4861 TYPE_FOR_JAVA (type) = 1;
4863 d = create_implicit_typedef (name, type);
4864 DECL_CONTEXT (d) = FROB_CONTEXT (context);
4866 set_identifier_type_value_with_scope (name, d, b);
4868 d = maybe_process_template_type_declaration (type,
4871 if (b->kind == sk_class)
4873 if (!PROCESSING_REAL_TEMPLATE_DECL_P ())
4874 /* Put this TYPE_DECL on the TYPE_FIELDS list for the
4875 class. But if it's a member template class, we
4876 want the TEMPLATE_DECL, not the TYPE_DECL, so this
4878 finish_member_declaration (d);
4880 pushdecl_class_level (d);
4883 d = pushdecl_with_scope (d, b);
4885 /* FIXME what if it gets a name from typedef? */
4886 if (ANON_AGGRNAME_P (name))
4887 DECL_IGNORED_P (d) = 1;
4889 TYPE_CONTEXT (type) = DECL_CONTEXT (d);
4891 /* If this is a local class, keep track of it. We need this
4892 information for name-mangling, and so that it is possible to find
4893 all function definitions in a translation unit in a convenient
4894 way. (It's otherwise tricky to find a member function definition
4895 it's only pointed to from within a local class.) */
4896 if (TYPE_CONTEXT (type)
4897 && TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL
4898 && !processing_template_decl)
4899 VARRAY_PUSH_TREE (local_classes, type);
4901 if (b->kind == sk_class
4902 && !COMPLETE_TYPE_P (current_class_type))
4904 maybe_add_class_template_decl_list (current_class_type,
4905 type, /*friend_p=*/0);
4906 CLASSTYPE_NESTED_UTDS (current_class_type) = b->type_decls;
4910 if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL)
4911 /* Use the canonical TYPE_DECL for this node. */
4912 TYPE_STUB_DECL (type) = TYPE_NAME (type);
4915 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE
4916 will be the tagged type we just added to the current
4917 binding level. This fake NULL-named TYPE_DECL node helps
4918 dwarfout.c to know when it needs to output a
4919 representation of a tagged type, and it also gives us a
4920 convenient place to record the "scope start" address for
4923 tree d = build_decl (TYPE_DECL, NULL_TREE, type);
4924 TYPE_STUB_DECL (type) = pushdecl_with_scope (d, b);
4926 timevar_pop (TV_NAME_LOOKUP);
4929 /* Subroutines for reverting temporarily to top-level for instantiation
4930 of templates and such. We actually need to clear out the class- and
4931 local-value slots of all identifiers, so that only the global values
4932 are at all visible. Simply setting current_binding_level to the global
4933 scope isn't enough, because more binding levels may be pushed. */
4934 struct saved_scope *scope_chain;
4936 /* If ID has not already been marked, add an appropriate binding to
4940 store_binding (tree id, VEC(cxx_saved_binding) **old_bindings)
4942 cxx_saved_binding *saved;
4944 if (!id || !IDENTIFIER_BINDING (id))
4947 if (IDENTIFIER_MARKED (id))
4950 IDENTIFIER_MARKED (id) = 1;
4952 saved = VEC_safe_push (cxx_saved_binding, *old_bindings, NULL);
4953 saved->identifier = id;
4954 saved->binding = IDENTIFIER_BINDING (id);
4955 saved->real_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
4956 IDENTIFIER_BINDING (id) = NULL;
4960 store_bindings (tree names, VEC(cxx_saved_binding) **old_bindings)
4964 timevar_push (TV_NAME_LOOKUP);
4965 for (t = names; t; t = TREE_CHAIN (t))
4969 if (TREE_CODE (t) == TREE_LIST)
4970 id = TREE_PURPOSE (t);
4974 store_binding (id, old_bindings);
4976 timevar_pop (TV_NAME_LOOKUP);
4979 /* Like store_bindings, but NAMES is a vector of cp_class_binding
4980 objects, rather than a TREE_LIST. */
4983 store_class_bindings (VEC(cp_class_binding) *names,
4984 VEC(cxx_saved_binding) **old_bindings)
4987 cp_class_binding *cb;
4989 timevar_push (TV_NAME_LOOKUP);
4990 for (i = 0; VEC_iterate(cp_class_binding, names, i, cb); ++i)
4991 store_binding (cb->identifier, old_bindings);
4992 timevar_pop (TV_NAME_LOOKUP);
4996 push_to_top_level (void)
4998 struct saved_scope *s;
4999 struct cp_binding_level *b;
5000 cxx_saved_binding *sb;
5004 timevar_push (TV_NAME_LOOKUP);
5005 s = GGC_CNEW (struct saved_scope);
5007 b = scope_chain ? current_binding_level : 0;
5009 /* If we're in the middle of some function, save our state. */
5013 push_function_context_to (NULL_TREE);
5018 if (scope_chain && previous_class_level)
5019 store_class_bindings (previous_class_level->class_shadowed,
5022 /* Have to include the global scope, because class-scope decls
5023 aren't listed anywhere useful. */
5024 for (; b; b = b->level_chain)
5028 /* Template IDs are inserted into the global level. If they were
5029 inserted into namespace level, finish_file wouldn't find them
5030 when doing pending instantiations. Therefore, don't stop at
5031 namespace level, but continue until :: . */
5032 if (global_scope_p (b))
5035 store_bindings (b->names, &s->old_bindings);
5036 /* We also need to check class_shadowed to save class-level type
5037 bindings, since pushclass doesn't fill in b->names. */
5038 if (b->kind == sk_class)
5039 store_class_bindings (b->class_shadowed, &s->old_bindings);
5041 /* Unwind type-value slots back to top level. */
5042 for (t = b->type_shadowed; t; t = TREE_CHAIN (t))
5043 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (t), TREE_VALUE (t));
5046 for (i = 0; VEC_iterate (cxx_saved_binding, s->old_bindings, i, sb); ++i)
5047 IDENTIFIER_MARKED (sb->identifier) = 0;
5049 s->prev = scope_chain;
5051 s->need_pop_function_context = need_pop;
5052 s->function_decl = current_function_decl;
5055 current_function_decl = NULL_TREE;
5056 VARRAY_TREE_INIT (current_lang_base, 10, "current_lang_base");
5057 current_lang_name = lang_name_cplusplus;
5058 current_namespace = global_namespace;
5059 timevar_pop (TV_NAME_LOOKUP);
5063 pop_from_top_level (void)
5065 struct saved_scope *s = scope_chain;
5066 cxx_saved_binding *saved;
5069 timevar_push (TV_NAME_LOOKUP);
5070 /* Clear out class-level bindings cache. */
5071 if (previous_class_level)
5072 invalidate_class_lookup_cache ();
5074 current_lang_base = 0;
5076 scope_chain = s->prev;
5077 for (i = 0; VEC_iterate (cxx_saved_binding, s->old_bindings, i, saved); ++i)
5079 tree id = saved->identifier;
5081 IDENTIFIER_BINDING (id) = saved->binding;
5082 SET_IDENTIFIER_TYPE_VALUE (id, saved->real_type_value);
5085 /* If we were in the middle of compiling a function, restore our
5087 if (s->need_pop_function_context)
5088 pop_function_context_from (NULL_TREE);
5089 current_function_decl = s->function_decl;
5090 timevar_pop (TV_NAME_LOOKUP);
5093 /* Pop off extraneous binding levels left over due to syntax errors.
5095 We don't pop past namespaces, as they might be valid. */
5098 pop_everything (void)
5100 if (ENABLE_SCOPE_CHECKING)
5101 verbatim ("XXX entering pop_everything ()\n");
5102 while (!toplevel_bindings_p ())
5104 if (current_binding_level->kind == sk_class)
5105 pop_nested_class ();
5109 if (ENABLE_SCOPE_CHECKING)
5110 verbatim ("XXX leaving pop_everything ()\n");
5113 /* Emit debugging information for using declarations and directives.
5114 If input tree is overloaded fn then emit debug info for all
5118 cp_emit_debug_info_for_using (tree t, tree context)
5120 /* Ignore this FUNCTION_DECL if it refers to a builtin declaration
5121 of a builtin function. */
5122 if (TREE_CODE (t) == FUNCTION_DECL
5123 && DECL_EXTERNAL (t)
5124 && DECL_BUILT_IN (t))
5127 /* Do not supply context to imported_module_or_decl, if
5128 it is a global namespace. */
5129 if (context == global_namespace)
5130 context = NULL_TREE;
5133 t = BASELINK_FUNCTIONS (t);
5135 /* FIXME: Handle TEMPLATE_DECLs. */
5136 for (t = OVL_CURRENT (t); t; t = OVL_NEXT (t))
5137 if (TREE_CODE (t) != TEMPLATE_DECL)
5138 (*debug_hooks->imported_module_or_decl) (t, context);
5141 #include "gt-cp-name-lookup.h"