1 /* Definitions for C++ name lookup routines.
2 Copyright (C) 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
3 Contributed by Gabriel Dos Reis <gdr@integrable-solutions.net>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to
19 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA. */
24 #include "coretypes.h"
29 #include "name-lookup.h"
32 #include "diagnostic.h"
36 /* The bindings for a particular name in a particular scope. */
38 struct scope_binding {
42 #define EMPTY_SCOPE_BINDING { NULL_TREE, NULL_TREE }
44 static cxx_scope *innermost_nonclass_level (void);
45 static 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, bool);
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);
55 /* The :: namespace. */
57 tree global_namespace;
59 /* The name of the anonymous namespace, throughout this translation
61 static GTY(()) tree anonymous_namespace_name;
63 /* Initialise anonymous_namespace_name if necessary, and return it. */
66 get_anonymous_namespace_name(void)
68 if (!anonymous_namespace_name)
70 /* The anonymous namespace has to have a unique name
71 if typeinfo objects are being compared by name. */
72 if (! flag_weak || ! SUPPORTS_ONE_ONLY)
73 anonymous_namespace_name = get_file_function_name ("N");
75 /* The demangler expects anonymous namespaces to be called
76 something starting with '_GLOBAL__N_'. */
77 anonymous_namespace_name = get_identifier ("_GLOBAL__N_1");
79 return anonymous_namespace_name;
82 /* Compute the chain index of a binding_entry given the HASH value of its
83 name and the total COUNT of chains. COUNT is assumed to be a power
86 #define ENTRY_INDEX(HASH, COUNT) (((HASH) >> 3) & ((COUNT) - 1))
88 /* A free list of "binding_entry"s awaiting for re-use. */
90 static GTY((deletable)) binding_entry free_binding_entry = NULL;
92 /* Create a binding_entry object for (NAME, TYPE). */
94 static inline binding_entry
95 binding_entry_make (tree name, tree type)
99 if (free_binding_entry)
101 entry = free_binding_entry;
102 free_binding_entry = entry->chain;
105 entry = GGC_NEW (struct binding_entry_s);
114 /* Put ENTRY back on the free list. */
117 binding_entry_free (binding_entry entry)
121 entry->chain = free_binding_entry;
122 free_binding_entry = entry;
126 /* The datatype used to implement the mapping from names to types at
128 struct binding_table_s GTY(())
130 /* Array of chains of "binding_entry"s */
131 binding_entry * GTY((length ("%h.chain_count"))) chain;
133 /* The number of chains in this table. This is the length of the
134 the member "chain" considered as an array. */
137 /* Number of "binding_entry"s in this table. */
141 /* Construct TABLE with an initial CHAIN_COUNT. */
144 binding_table_construct (binding_table table, size_t chain_count)
146 table->chain_count = chain_count;
147 table->entry_count = 0;
148 table->chain = GGC_CNEWVEC (binding_entry, table->chain_count);
151 /* Make TABLE's entries ready for reuse. */
154 binding_table_free (binding_table table)
162 for (i = 0, count = table->chain_count; i < count; ++i)
164 binding_entry temp = table->chain[i];
167 binding_entry entry = temp;
169 binding_entry_free (entry);
171 table->chain[i] = NULL;
173 table->entry_count = 0;
177 /* Allocate a table with CHAIN_COUNT, assumed to be a power of two. */
179 static inline binding_table
180 binding_table_new (size_t chain_count)
182 binding_table table = GGC_NEW (struct binding_table_s);
184 binding_table_construct (table, chain_count);
188 /* Expand TABLE to twice its current chain_count. */
191 binding_table_expand (binding_table table)
193 const size_t old_chain_count = table->chain_count;
194 const size_t old_entry_count = table->entry_count;
195 const size_t new_chain_count = 2 * old_chain_count;
196 binding_entry *old_chains = table->chain;
199 binding_table_construct (table, new_chain_count);
200 for (i = 0; i < old_chain_count; ++i)
202 binding_entry entry = old_chains[i];
203 for (; entry != NULL; entry = old_chains[i])
205 const unsigned int hash = IDENTIFIER_HASH_VALUE (entry->name);
206 const size_t j = ENTRY_INDEX (hash, new_chain_count);
208 old_chains[i] = entry->chain;
209 entry->chain = table->chain[j];
210 table->chain[j] = entry;
213 table->entry_count = old_entry_count;
216 /* Insert a binding for NAME to TYPE into TABLE. */
219 binding_table_insert (binding_table table, tree name, tree type)
221 const unsigned int hash = IDENTIFIER_HASH_VALUE (name);
222 const size_t i = ENTRY_INDEX (hash, table->chain_count);
223 binding_entry entry = binding_entry_make (name, type);
225 entry->chain = table->chain[i];
226 table->chain[i] = entry;
227 ++table->entry_count;
229 if (3 * table->chain_count < 5 * table->entry_count)
230 binding_table_expand (table);
233 /* Return the binding_entry, if any, that maps NAME. */
236 binding_table_find (binding_table table, tree name)
238 const unsigned int hash = IDENTIFIER_HASH_VALUE (name);
239 binding_entry entry = table->chain[ENTRY_INDEX (hash, table->chain_count)];
241 while (entry != NULL && entry->name != name)
242 entry = entry->chain;
247 /* Apply PROC -- with DATA -- to all entries in TABLE. */
250 binding_table_foreach (binding_table table, bt_foreach_proc proc, void *data)
252 const size_t chain_count = table->chain_count;
255 for (i = 0; i < chain_count; ++i)
257 binding_entry entry = table->chain[i];
258 for (; entry != NULL; entry = entry->chain)
263 #ifndef ENABLE_SCOPE_CHECKING
264 # define ENABLE_SCOPE_CHECKING 0
266 # define ENABLE_SCOPE_CHECKING 1
269 /* A free list of "cxx_binding"s, connected by their PREVIOUS. */
271 static GTY((deletable)) cxx_binding *free_bindings;
273 /* Initialize VALUE and TYPE field for BINDING, and set the PREVIOUS
277 cxx_binding_init (cxx_binding *binding, tree value, tree type)
279 binding->value = value;
280 binding->type = type;
281 binding->previous = NULL;
284 /* (GC)-allocate a binding object with VALUE and TYPE member initialized. */
287 cxx_binding_make (tree value, tree type)
289 cxx_binding *binding;
292 binding = free_bindings;
293 free_bindings = binding->previous;
296 binding = GGC_NEW (cxx_binding);
298 cxx_binding_init (binding, value, type);
303 /* Put BINDING back on the free list. */
306 cxx_binding_free (cxx_binding *binding)
308 binding->scope = NULL;
309 binding->previous = free_bindings;
310 free_bindings = binding;
313 /* Create a new binding for NAME (with the indicated VALUE and TYPE
314 bindings) in the class scope indicated by SCOPE. */
317 new_class_binding (tree name, tree value, tree type, cxx_scope *scope)
319 cp_class_binding *cb;
320 cxx_binding *binding;
322 cb = VEC_safe_push (cp_class_binding, gc, scope->class_shadowed, NULL);
324 cb->identifier = name;
325 cb->base = binding = cxx_binding_make (value, type);
326 binding->scope = scope;
330 /* Make DECL the innermost binding for ID. The LEVEL is the binding
331 level at which this declaration is being bound. */
334 push_binding (tree id, tree decl, cxx_scope* level)
336 cxx_binding *binding;
338 if (level != class_binding_level)
340 binding = cxx_binding_make (decl, NULL_TREE);
341 binding->scope = level;
342 /* APPLE LOCAL blocks 6040305 (ch) */
343 binding->declared_in_block = cur_block != 0;
346 binding = new_class_binding (id, decl, /*type=*/NULL_TREE, level);
348 /* Now, fill in the binding information. */
349 binding->previous = IDENTIFIER_BINDING (id);
350 INHERITED_VALUE_BINDING_P (binding) = 0;
351 LOCAL_BINDING_P (binding) = (level != class_binding_level);
353 /* And put it on the front of the list of bindings for ID. */
354 IDENTIFIER_BINDING (id) = binding;
357 /* Remove the binding for DECL which should be the innermost binding
361 pop_binding (tree id, tree decl)
363 cxx_binding *binding;
366 /* It's easiest to write the loops that call this function without
367 checking whether or not the entities involved have names. We
368 get here for such an entity. */
371 /* Get the innermost binding for ID. */
372 binding = IDENTIFIER_BINDING (id);
374 /* The name should be bound. */
375 gcc_assert (binding != NULL);
377 /* The DECL will be either the ordinary binding or the type
378 binding for this identifier. Remove that binding. */
379 if (binding->value == decl)
380 binding->value = NULL_TREE;
383 gcc_assert (binding->type == decl);
384 binding->type = NULL_TREE;
387 if (!binding->value && !binding->type)
389 /* We're completely done with the innermost binding for this
390 identifier. Unhook it from the list of bindings. */
391 IDENTIFIER_BINDING (id) = binding->previous;
393 /* Add it to the free list. */
394 cxx_binding_free (binding);
398 /* BINDING records an existing declaration for a name in the current scope.
399 But, DECL is another declaration for that same identifier in the
400 same scope. This is the `struct stat' hack whereby a non-typedef
401 class name or enum-name can be bound at the same level as some other
405 A class name (9.1) or enumeration name (7.2) can be hidden by the
406 name of an object, function, or enumerator declared in the same scope.
407 If a class or enumeration name and an object, function, or enumerator
408 are declared in the same scope (in any order) with the same name, the
409 class or enumeration name is hidden wherever the object, function, or
410 enumerator name is visible.
412 It's the responsibility of the caller to check that
413 inserting this name is valid here. Returns nonzero if the new binding
417 supplement_binding (cxx_binding *binding, tree decl)
419 tree bval = binding->value;
422 timevar_push (TV_NAME_LOOKUP);
423 if (TREE_CODE (decl) == TYPE_DECL && DECL_ARTIFICIAL (decl))
424 /* The new name is the type name. */
425 binding->type = decl;
426 else if (/* BVAL is null when push_class_level_binding moves an
427 inherited type-binding out of the way to make room for a
428 new value binding. */
430 /* BVAL is error_mark_node when DECL's name has been used
431 in a non-class scope prior declaration. In that case,
432 we should have already issued a diagnostic; for graceful
433 error recovery purpose, pretend this was the intended
434 declaration for that name. */
435 || bval == error_mark_node
436 /* If BVAL is anticipated but has not yet been declared,
437 pretend it is not there at all. */
438 || (TREE_CODE (bval) == FUNCTION_DECL
439 && DECL_ANTICIPATED (bval)
440 && !DECL_HIDDEN_FRIEND_P (bval)))
441 binding->value = decl;
442 else if (TREE_CODE (bval) == TYPE_DECL && DECL_ARTIFICIAL (bval))
444 /* The old binding was a type name. It was placed in
445 VALUE field because it was thought, at the point it was
446 declared, to be the only entity with such a name. Move the
447 type name into the type slot; it is now hidden by the new
449 binding->type = bval;
450 binding->value = decl;
451 binding->value_is_inherited = false;
453 else if (TREE_CODE (bval) == TYPE_DECL
454 && TREE_CODE (decl) == TYPE_DECL
455 && DECL_NAME (decl) == DECL_NAME (bval)
456 && binding->scope->kind != sk_class
457 && (same_type_p (TREE_TYPE (decl), TREE_TYPE (bval))
458 /* If either type involves template parameters, we must
459 wait until instantiation. */
460 || uses_template_parms (TREE_TYPE (decl))
461 || uses_template_parms (TREE_TYPE (bval))))
462 /* We have two typedef-names, both naming the same type to have
463 the same name. In general, this is OK because of:
467 In a given scope, a typedef specifier can be used to redefine
468 the name of any type declared in that scope to refer to the
469 type to which it already refers.
471 However, in class scopes, this rule does not apply due to the
472 stricter language in [class.mem] prohibiting redeclarations of
475 /* There can be two block-scope declarations of the same variable,
476 so long as they are `extern' declarations. However, there cannot
477 be two declarations of the same static data member:
481 A member shall not be declared twice in the
482 member-specification. */
483 else if (TREE_CODE (decl) == VAR_DECL && TREE_CODE (bval) == VAR_DECL
484 && DECL_EXTERNAL (decl) && DECL_EXTERNAL (bval)
485 && !DECL_CLASS_SCOPE_P (decl))
487 duplicate_decls (decl, binding->value, /*newdecl_is_friend=*/false);
490 else if (TREE_CODE (decl) == NAMESPACE_DECL
491 && TREE_CODE (bval) == NAMESPACE_DECL
492 && DECL_NAMESPACE_ALIAS (decl)
493 && DECL_NAMESPACE_ALIAS (bval)
494 && ORIGINAL_NAMESPACE (bval) == ORIGINAL_NAMESPACE (decl))
497 In a declarative region, a namespace-alias-definition can be
498 used to redefine a namespace-alias declared in that declarative
499 region to refer only to the namespace to which it already
504 error ("declaration of %q#D", decl);
505 error ("conflicts with previous declaration %q+#D", bval);
509 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, ok);
512 /* Add DECL to the list of things declared in B. */
515 add_decl_to_level (tree decl, cxx_scope *b)
517 if (TREE_CODE (decl) == NAMESPACE_DECL
518 && !DECL_NAMESPACE_ALIAS (decl))
520 TREE_CHAIN (decl) = b->namespaces;
521 b->namespaces = decl;
523 else if (TREE_CODE (decl) == VAR_DECL && DECL_VIRTUAL_P (decl))
525 TREE_CHAIN (decl) = b->vtables;
530 /* We build up the list in reverse order, and reverse it later if
532 TREE_CHAIN (decl) = b->names;
536 /* If appropriate, add decl to separate list of statics. We
537 include extern variables because they might turn out to be
538 static later. It's OK for this list to contain a few false
540 if (b->kind == sk_namespace)
541 if ((TREE_CODE (decl) == VAR_DECL
542 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
543 || (TREE_CODE (decl) == FUNCTION_DECL
544 && (!TREE_PUBLIC (decl) || DECL_DECLARED_INLINE_P (decl))))
545 VEC_safe_push (tree, gc, b->static_decls, decl);
549 /* Record a decl-node X as belonging to the current lexical scope.
550 Check for errors (such as an incompatible declaration for the same
551 name already seen in the same scope). IS_FRIEND is true if X is
552 declared as a friend.
554 Returns either X or an old decl for the same name.
555 If an old decl is returned, it may have been smashed
556 to agree with what X says. */
559 pushdecl_maybe_friend (tree x, bool is_friend)
563 int need_new_binding;
565 timevar_push (TV_NAME_LOOKUP);
567 if (x == error_mark_node)
568 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
570 need_new_binding = 1;
572 if (DECL_TEMPLATE_PARM_P (x))
573 /* Template parameters have no context; they are not X::T even
574 when declared within a class or namespace. */
578 if (current_function_decl && x != current_function_decl
579 /* A local declaration for a function doesn't constitute
581 && TREE_CODE (x) != FUNCTION_DECL
582 /* A local declaration for an `extern' variable is in the
583 scope of the current namespace, not the current
585 && !(TREE_CODE (x) == VAR_DECL && DECL_EXTERNAL (x))
586 && !DECL_CONTEXT (x))
587 DECL_CONTEXT (x) = current_function_decl;
589 /* If this is the declaration for a namespace-scope function,
590 but the declaration itself is in a local scope, mark the
592 if (TREE_CODE (x) == FUNCTION_DECL
593 && DECL_NAMESPACE_SCOPE_P (x)
594 && current_function_decl
595 && x != current_function_decl)
596 DECL_LOCAL_FUNCTION_P (x) = 1;
599 name = DECL_NAME (x);
602 int different_binding_level = 0;
604 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
605 name = TREE_OPERAND (name, 0);
607 /* In case this decl was explicitly namespace-qualified, look it
608 up in its namespace context. */
609 if (DECL_NAMESPACE_SCOPE_P (x) && namespace_bindings_p ())
610 t = namespace_binding (name, DECL_CONTEXT (x));
612 t = lookup_name_innermost_nonclass_level (name);
614 /* [basic.link] If there is a visible declaration of an entity
615 with linkage having the same name and type, ignoring entities
616 declared outside the innermost enclosing namespace scope, the
617 block scope declaration declares that same entity and
618 receives the linkage of the previous declaration. */
619 if (! t && current_function_decl && x != current_function_decl
620 && (TREE_CODE (x) == FUNCTION_DECL || TREE_CODE (x) == VAR_DECL)
621 && DECL_EXTERNAL (x))
623 /* Look in block scope. */
624 t = innermost_non_namespace_value (name);
625 /* Or in the innermost namespace. */
627 t = namespace_binding (name, DECL_CONTEXT (x));
628 /* Does it have linkage? Note that if this isn't a DECL, it's an
629 OVERLOAD, which is OK. */
630 if (t && DECL_P (t) && ! (TREE_STATIC (t) || DECL_EXTERNAL (t)))
633 different_binding_level = 1;
636 /* If we are declaring a function, and the result of name-lookup
637 was an OVERLOAD, look for an overloaded instance that is
638 actually the same as the function we are declaring. (If
639 there is one, we have to merge our declaration with the
640 previous declaration.) */
641 if (t && TREE_CODE (t) == OVERLOAD)
645 if (TREE_CODE (x) == FUNCTION_DECL)
646 for (match = t; match; match = OVL_NEXT (match))
648 if (decls_match (OVL_CURRENT (match), x))
652 /* Just choose one. */
656 t = OVL_CURRENT (match);
661 if (t && t != error_mark_node)
663 if (different_binding_level)
665 if (decls_match (x, t))
666 /* The standard only says that the local extern
667 inherits linkage from the previous decl; in
668 particular, default args are not shared. Add
669 the decl into a hash table to make sure only
670 the previous decl in this case is seen by the
673 struct cxx_int_tree_map *h;
676 TREE_PUBLIC (x) = TREE_PUBLIC (t);
678 if (cp_function_chain->extern_decl_map == NULL)
679 cp_function_chain->extern_decl_map
680 = htab_create_ggc (20, cxx_int_tree_map_hash,
681 cxx_int_tree_map_eq, NULL);
683 h = GGC_NEW (struct cxx_int_tree_map);
684 h->uid = DECL_UID (x);
686 loc = htab_find_slot_with_hash
687 (cp_function_chain->extern_decl_map, h,
689 *(struct cxx_int_tree_map **) loc = h;
692 else if (TREE_CODE (t) == PARM_DECL)
694 gcc_assert (DECL_CONTEXT (t));
696 /* Check for duplicate params. */
697 if (duplicate_decls (x, t, is_friend))
698 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
700 else if ((DECL_EXTERN_C_FUNCTION_P (x)
701 || DECL_FUNCTION_TEMPLATE_P (x))
702 && is_overloaded_fn (t))
703 /* Don't do anything just yet. */;
704 else if (t == wchar_decl_node)
706 if (pedantic && ! DECL_IN_SYSTEM_HEADER (x))
707 pedwarn ("redeclaration of %<wchar_t%> as %qT",
710 /* Throw away the redeclaration. */
711 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
715 tree olddecl = duplicate_decls (x, t, is_friend);
717 /* If the redeclaration failed, we can stop at this
719 if (olddecl == error_mark_node)
720 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
724 if (TREE_CODE (t) == TYPE_DECL)
725 SET_IDENTIFIER_TYPE_VALUE (name, TREE_TYPE (t));
727 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
729 else if (DECL_MAIN_P (x) && TREE_CODE (t) == FUNCTION_DECL)
731 /* A redeclaration of main, but not a duplicate of the
736 This function shall not be overloaded. */
737 error ("invalid redeclaration of %q+D", t);
739 /* We don't try to push this declaration since that
741 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
746 if (TREE_CODE (x) == FUNCTION_DECL || DECL_FUNCTION_TEMPLATE_P (x))
747 check_default_args (x);
749 check_template_shadow (x);
751 /* If this is a function conjured up by the backend, massage it
752 so it looks friendly. */
753 if (DECL_NON_THUNK_FUNCTION_P (x) && ! DECL_LANG_SPECIFIC (x))
755 retrofit_lang_decl (x);
756 SET_DECL_LANGUAGE (x, lang_c);
759 if (DECL_NON_THUNK_FUNCTION_P (x) && ! DECL_FUNCTION_MEMBER_P (x))
761 t = push_overloaded_decl (x, PUSH_LOCAL, is_friend);
763 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
764 if (!namespace_bindings_p ())
765 /* We do not need to create a binding for this name;
766 push_overloaded_decl will have already done so if
768 need_new_binding = 0;
770 else if (DECL_FUNCTION_TEMPLATE_P (x) && DECL_NAMESPACE_SCOPE_P (x))
772 t = push_overloaded_decl (x, PUSH_GLOBAL, is_friend);
774 add_decl_to_level (x, NAMESPACE_LEVEL (CP_DECL_CONTEXT (t)));
775 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
778 /* If declaring a type as a typedef, copy the type (unless we're
779 at line 0), and install this TYPE_DECL as the new type's typedef
780 name. See the extensive comment in ../c-decl.c (pushdecl). */
781 if (TREE_CODE (x) == TYPE_DECL)
783 tree type = TREE_TYPE (x);
784 if (DECL_IS_BUILTIN (x))
786 if (TYPE_NAME (type) == 0)
787 TYPE_NAME (type) = x;
789 else if (type != error_mark_node && TYPE_NAME (type) != x
790 /* We don't want to copy the type when all we're
791 doing is making a TYPE_DECL for the purposes of
793 && (!TYPE_NAME (type)
794 || TYPE_NAME (type) != DECL_ABSTRACT_ORIGIN (x)))
796 DECL_ORIGINAL_TYPE (x) = type;
797 type = build_variant_type_copy (type);
798 TYPE_STUB_DECL (type) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
799 TYPE_NAME (type) = x;
800 TREE_TYPE (x) = type;
803 if (type != error_mark_node
805 && TYPE_IDENTIFIER (type))
806 set_identifier_type_value (DECL_NAME (x), x);
809 /* Multiple external decls of the same identifier ought to match.
811 We get warnings about inline functions where they are defined.
812 We get warnings about other functions from push_overloaded_decl.
814 Avoid duplicate warnings where they are used. */
815 if (TREE_PUBLIC (x) && TREE_CODE (x) != FUNCTION_DECL)
819 decl = IDENTIFIER_NAMESPACE_VALUE (name);
820 if (decl && TREE_CODE (decl) == OVERLOAD)
821 decl = OVL_FUNCTION (decl);
823 if (decl && decl != error_mark_node
824 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl))
825 /* If different sort of thing, we already gave an error. */
826 && TREE_CODE (decl) == TREE_CODE (x)
827 && !same_type_p (TREE_TYPE (x), TREE_TYPE (decl)))
829 pedwarn ("type mismatch with previous external decl of %q#D", x);
830 pedwarn ("previous external decl of %q+#D", decl);
834 if (TREE_CODE (x) == FUNCTION_DECL
836 && !flag_friend_injection)
838 /* This is a new declaration of a friend function, so hide
839 it from ordinary function lookup. */
840 DECL_ANTICIPATED (x) = 1;
841 DECL_HIDDEN_FRIEND_P (x) = 1;
844 /* This name is new in its binding level.
845 Install the new declaration and return it. */
846 if (namespace_bindings_p ())
848 /* Install a global value. */
850 /* If the first global decl has external linkage,
851 warn if we later see static one. */
852 if (IDENTIFIER_GLOBAL_VALUE (name) == NULL_TREE && TREE_PUBLIC (x))
853 TREE_PUBLIC (name) = 1;
855 /* Bind the name for the entity. */
856 if (!(TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x)
858 && (TREE_CODE (x) == TYPE_DECL
859 || TREE_CODE (x) == VAR_DECL
860 || TREE_CODE (x) == NAMESPACE_DECL
861 || TREE_CODE (x) == CONST_DECL
862 || TREE_CODE (x) == TEMPLATE_DECL))
863 SET_IDENTIFIER_NAMESPACE_VALUE (name, x);
865 /* If new decl is `static' and an `extern' was seen previously,
867 if (x != NULL_TREE && t != NULL_TREE && decls_match (x, t))
868 warn_extern_redeclared_static (x, t);
872 /* Here to install a non-global value. */
873 tree oldlocal = innermost_non_namespace_value (name);
874 tree oldglobal = IDENTIFIER_NAMESPACE_VALUE (name);
876 if (need_new_binding)
878 push_local_binding (name, x, 0);
879 /* Because push_local_binding will hook X on to the
880 current_binding_level's name list, we don't want to
881 do that again below. */
882 need_new_binding = 0;
885 /* If this is a TYPE_DECL, push it into the type value slot. */
886 if (TREE_CODE (x) == TYPE_DECL)
887 set_identifier_type_value (name, x);
889 /* Clear out any TYPE_DECL shadowed by a namespace so that
890 we won't think this is a type. The C struct hack doesn't
891 go through namespaces. */
892 if (TREE_CODE (x) == NAMESPACE_DECL)
893 set_identifier_type_value (name, NULL_TREE);
900 && TREE_CODE (oldlocal) == VAR_DECL
901 && DECL_DEAD_FOR_LOCAL (oldlocal))
902 oldlocal = DECL_SHADOWED_FOR_VAR (oldlocal);
904 if (oldlocal == NULL_TREE)
905 oldlocal = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (d));
908 /* If this is an extern function declaration, see if we
909 have a global definition or declaration for the function. */
910 if (oldlocal == NULL_TREE
912 && oldglobal != NULL_TREE
913 && TREE_CODE (x) == FUNCTION_DECL
914 && TREE_CODE (oldglobal) == FUNCTION_DECL)
916 /* We have one. Their types must agree. */
917 if (decls_match (x, oldglobal))
921 warning (0, "extern declaration of %q#D doesn't match", x);
922 warning (0, "global declaration %q+#D", oldglobal);
925 /* If we have a local external declaration,
926 and no file-scope declaration has yet been seen,
927 then if we later have a file-scope decl it must not be static. */
928 if (oldlocal == NULL_TREE
929 && oldglobal == NULL_TREE
932 TREE_PUBLIC (name) = 1;
934 /* Warn if shadowing an argument at the top level of the body. */
935 if (oldlocal != NULL_TREE && !DECL_EXTERNAL (x)
936 /* Inline decls shadow nothing. */
937 && !DECL_FROM_INLINE (x)
938 && TREE_CODE (oldlocal) == PARM_DECL
939 /* Don't check the `this' parameter. */
940 && !DECL_ARTIFICIAL (oldlocal))
944 /* Don't complain if it's from an enclosing function. */
945 if (DECL_CONTEXT (oldlocal) == current_function_decl
946 && TREE_CODE (x) != PARM_DECL)
948 /* Go to where the parms should be and see if we find
950 struct cp_binding_level *b = current_binding_level->level_chain;
952 if (FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
953 /* Skip the ctor/dtor cleanup level. */
957 if (b->kind == sk_function_parms)
959 error ("declaration of %q#D shadows a parameter", x);
964 if (warn_shadow && !err)
966 warning (OPT_Wshadow, "declaration of %q#D shadows a parameter", x);
967 warning (OPT_Wshadow, "%Jshadowed declaration is here", oldlocal);
971 /* Maybe warn if shadowing something else. */
972 else if (warn_shadow && !DECL_EXTERNAL (x)
973 /* No shadow warnings for internally generated vars. */
974 && ! DECL_ARTIFICIAL (x)
975 /* No shadow warnings for vars made for inlining. */
976 && ! DECL_FROM_INLINE (x))
980 if (current_class_ptr)
981 member = lookup_member (current_class_type,
984 /*want_type=*/false);
988 if (member && !TREE_STATIC (member))
990 /* Location of previous decl is not useful in this case. */
991 warning (OPT_Wshadow, "declaration of %qD shadows a member of 'this'",
994 else if (oldlocal != NULL_TREE
995 && TREE_CODE (oldlocal) == VAR_DECL)
997 warning (OPT_Wshadow, "declaration of %qD shadows a previous local", x);
998 warning (OPT_Wshadow, "%Jshadowed declaration is here", oldlocal);
1000 else if (oldglobal != NULL_TREE
1001 && TREE_CODE (oldglobal) == VAR_DECL)
1002 /* XXX shadow warnings in outer-more namespaces */
1004 warning (OPT_Wshadow, "declaration of %qD shadows a global declaration",
1006 warning (OPT_Wshadow, "%Jshadowed declaration is here", oldglobal);
1011 if (TREE_CODE (x) == VAR_DECL)
1012 maybe_register_incomplete_var (x);
1015 if (need_new_binding)
1016 add_decl_to_level (x,
1017 DECL_NAMESPACE_SCOPE_P (x)
1018 ? NAMESPACE_LEVEL (CP_DECL_CONTEXT (x))
1019 : current_binding_level);
1021 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
1024 /* Record a decl-node X as belonging to the current lexical scope. */
1029 return pushdecl_maybe_friend (x, false);
1032 /* Enter DECL into the symbol table, if that's appropriate. Returns
1033 DECL, or a modified version thereof. */
1036 maybe_push_decl (tree decl)
1038 tree type = TREE_TYPE (decl);
1040 /* Add this decl to the current binding level, but not if it comes
1041 from another scope, e.g. a static member variable. TEM may equal
1042 DECL or it may be a previous decl of the same name. */
1043 if (decl == error_mark_node
1044 || (TREE_CODE (decl) != PARM_DECL
1045 && DECL_CONTEXT (decl) != NULL_TREE
1046 /* Definitions of namespace members outside their namespace are
1048 && TREE_CODE (DECL_CONTEXT (decl)) != NAMESPACE_DECL)
1049 || (TREE_CODE (decl) == TEMPLATE_DECL && !namespace_bindings_p ())
1050 || TREE_CODE (type) == UNKNOWN_TYPE
1051 /* The declaration of a template specialization does not affect
1052 the functions available for overload resolution, so we do not
1054 || (TREE_CODE (decl) == FUNCTION_DECL
1055 && DECL_TEMPLATE_SPECIALIZATION (decl)))
1058 return pushdecl (decl);
1061 /* Bind DECL to ID in the current_binding_level, assumed to be a local
1062 binding level. If PUSH_USING is set in FLAGS, we know that DECL
1063 doesn't really belong to this binding level, that it got here
1064 through a using-declaration. */
1067 push_local_binding (tree id, tree decl, int flags)
1069 struct cp_binding_level *b;
1071 /* Skip over any local classes. This makes sense if we call
1072 push_local_binding with a friend decl of a local class. */
1073 b = innermost_nonclass_level ();
1075 if (lookup_name_innermost_nonclass_level (id))
1077 /* Supplement the existing binding. */
1078 if (!supplement_binding (IDENTIFIER_BINDING (id), decl))
1079 /* It didn't work. Something else must be bound at this
1080 level. Do not add DECL to the list of things to pop
1085 /* Create a new binding. */
1086 push_binding (id, decl, b);
1088 if (TREE_CODE (decl) == OVERLOAD || (flags & PUSH_USING))
1089 /* We must put the OVERLOAD into a TREE_LIST since the
1090 TREE_CHAIN of an OVERLOAD is already used. Similarly for
1091 decls that got here through a using-declaration. */
1092 decl = build_tree_list (NULL_TREE, decl);
1094 /* And put DECL on the list of things declared by the current
1096 add_decl_to_level (decl, b);
1099 /* Check to see whether or not DECL is a variable that would have been
1100 in scope under the ARM, but is not in scope under the ANSI/ISO
1101 standard. If so, issue an error message. If name lookup would
1102 work in both cases, but return a different result, this function
1103 returns the result of ANSI/ISO lookup. Otherwise, it returns
1107 check_for_out_of_scope_variable (tree decl)
1111 /* We only care about out of scope variables. */
1112 if (!(TREE_CODE (decl) == VAR_DECL && DECL_DEAD_FOR_LOCAL (decl)))
1115 shadowed = DECL_HAS_SHADOWED_FOR_VAR_P (decl)
1116 ? DECL_SHADOWED_FOR_VAR (decl) : NULL_TREE ;
1117 while (shadowed != NULL_TREE && TREE_CODE (shadowed) == VAR_DECL
1118 && DECL_DEAD_FOR_LOCAL (shadowed))
1119 shadowed = DECL_HAS_SHADOWED_FOR_VAR_P (shadowed)
1120 ? DECL_SHADOWED_FOR_VAR (shadowed) : NULL_TREE;
1122 shadowed = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (decl));
1125 if (!DECL_ERROR_REPORTED (decl))
1127 warning (0, "name lookup of %qD changed", DECL_NAME (decl));
1128 warning (0, " matches this %q+D under ISO standard rules",
1130 warning (0, " matches this %q+D under old rules", decl);
1131 DECL_ERROR_REPORTED (decl) = 1;
1136 /* If we have already complained about this declaration, there's no
1137 need to do it again. */
1138 if (DECL_ERROR_REPORTED (decl))
1141 DECL_ERROR_REPORTED (decl) = 1;
1143 if (TREE_TYPE (decl) == error_mark_node)
1146 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
1148 error ("name lookup of %qD changed for new ISO %<for%> scoping",
1150 error (" cannot use obsolete binding at %q+D because "
1151 "it has a destructor", decl);
1152 return error_mark_node;
1156 pedwarn ("name lookup of %qD changed for new ISO %<for%> scoping",
1158 pedwarn (" using obsolete binding at %q+D", decl);
1164 /* true means unconditionally make a BLOCK for the next level pushed. */
1166 static bool keep_next_level_flag;
1168 static int binding_depth = 0;
1169 static int is_class_level = 0;
1176 for (i = 0; i < depth * 2; i++)
1180 /* Return a string describing the kind of SCOPE we have. */
1182 cxx_scope_descriptor (cxx_scope *scope)
1184 /* The order of this table must match the "scope_kind"
1186 static const char* scope_kind_names[] = {
1192 "function-parameter-scope",
1195 "template-parameter-scope",
1196 "template-explicit-spec-scope"
1198 const scope_kind kind = scope->explicit_spec_p
1199 ? sk_template_spec : scope->kind;
1201 return scope_kind_names[kind];
1204 /* Output a debugging information about SCOPE when performing
1207 cxx_scope_debug (cxx_scope *scope, int line, const char *action)
1209 const char *desc = cxx_scope_descriptor (scope);
1210 if (scope->this_entity)
1211 verbatim ("%s %s(%E) %p %d\n", action, desc,
1212 scope->this_entity, (void *) scope, line);
1214 verbatim ("%s %s %p %d\n", action, desc, (void *) scope, line);
1217 /* Return the estimated initial size of the hashtable of a NAMESPACE
1220 static inline size_t
1221 namespace_scope_ht_size (tree ns)
1223 tree name = DECL_NAME (ns);
1225 return name == std_identifier
1226 ? NAMESPACE_STD_HT_SIZE
1227 : (name == global_scope_name
1228 ? GLOBAL_SCOPE_HT_SIZE
1229 : NAMESPACE_ORDINARY_HT_SIZE);
1232 /* A chain of binding_level structures awaiting reuse. */
1234 static GTY((deletable)) struct cp_binding_level *free_binding_level;
1236 /* Insert SCOPE as the innermost binding level. */
1239 push_binding_level (struct cp_binding_level *scope)
1241 /* Add it to the front of currently active scopes stack. */
1242 scope->level_chain = current_binding_level;
1243 current_binding_level = scope;
1244 keep_next_level_flag = false;
1246 if (ENABLE_SCOPE_CHECKING)
1248 scope->binding_depth = binding_depth;
1249 indent (binding_depth);
1250 cxx_scope_debug (scope, input_line, "push");
1256 /* Create a new KIND scope and make it the top of the active scopes stack.
1257 ENTITY is the scope of the associated C++ entity (namespace, class,
1258 function); it is NULL otherwise. */
1261 begin_scope (scope_kind kind, tree entity)
1265 /* Reuse or create a struct for this binding level. */
1266 if (!ENABLE_SCOPE_CHECKING && free_binding_level)
1268 scope = free_binding_level;
1269 free_binding_level = scope->level_chain;
1272 scope = GGC_NEW (cxx_scope);
1273 memset (scope, 0, sizeof (cxx_scope));
1275 scope->this_entity = entity;
1276 scope->more_cleanups_ok = true;
1283 case sk_template_spec:
1284 scope->explicit_spec_p = true;
1285 kind = sk_template_parms;
1287 case sk_template_parms:
1293 case sk_function_parms:
1295 scope->keep = keep_next_level_flag;
1299 NAMESPACE_LEVEL (entity) = scope;
1300 scope->static_decls =
1301 VEC_alloc (tree, gc,
1302 DECL_NAME (entity) == std_identifier
1303 || DECL_NAME (entity) == global_scope_name
1308 /* Should not happen. */
1314 push_binding_level (scope);
1319 /* We're about to leave current scope. Pop the top of the stack of
1320 currently active scopes. Return the enclosing scope, now active. */
1325 cxx_scope *scope = current_binding_level;
1327 if (scope->kind == sk_namespace && class_binding_level)
1328 current_binding_level = class_binding_level;
1330 /* We cannot leave a scope, if there are none left. */
1331 if (NAMESPACE_LEVEL (global_namespace))
1332 gcc_assert (!global_scope_p (scope));
1334 if (ENABLE_SCOPE_CHECKING)
1336 indent (--binding_depth);
1337 cxx_scope_debug (scope, input_line, "leave");
1338 if (is_class_level != (scope == class_binding_level))
1340 indent (binding_depth);
1341 verbatim ("XXX is_class_level != (current_scope == class_scope)\n");
1346 #ifdef HANDLE_PRAGMA_VISIBILITY
1347 if (scope->has_visibility)
1351 /* Move one nesting level up. */
1352 current_binding_level = scope->level_chain;
1354 /* Namespace-scopes are left most probably temporarily, not
1355 completely; they can be reopened later, e.g. in namespace-extension
1356 or any name binding activity that requires us to resume a
1357 namespace. For classes, we cache some binding levels. For other
1358 scopes, we just make the structure available for reuse. */
1359 if (scope->kind != sk_namespace
1360 && scope->kind != sk_class)
1362 scope->level_chain = free_binding_level;
1363 gcc_assert (!ENABLE_SCOPE_CHECKING
1364 || scope->binding_depth == binding_depth);
1365 free_binding_level = scope;
1368 /* Find the innermost enclosing class scope, and reset
1369 CLASS_BINDING_LEVEL appropriately. */
1370 if (scope->kind == sk_class)
1372 class_binding_level = NULL;
1373 for (scope = current_binding_level; scope; scope = scope->level_chain)
1374 if (scope->kind == sk_class)
1376 class_binding_level = scope;
1381 return current_binding_level;
1385 resume_scope (struct cp_binding_level* b)
1387 /* Resuming binding levels is meant only for namespaces,
1388 and those cannot nest into classes. */
1389 gcc_assert (!class_binding_level);
1390 /* Also, resuming a non-directly nested namespace is a no-no. */
1391 gcc_assert (b->level_chain == current_binding_level);
1392 current_binding_level = b;
1393 if (ENABLE_SCOPE_CHECKING)
1395 b->binding_depth = binding_depth;
1396 indent (binding_depth);
1397 cxx_scope_debug (b, input_line, "resume");
1403 /* Return the innermost binding level that is not for a class scope. */
1406 innermost_nonclass_level (void)
1410 b = current_binding_level;
1411 while (b->kind == sk_class)
1417 /* We're defining an object of type TYPE. If it needs a cleanup, but
1418 we're not allowed to add any more objects with cleanups to the current
1419 scope, create a new binding level. */
1422 maybe_push_cleanup_level (tree type)
1424 if (type != error_mark_node
1425 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
1426 && current_binding_level->more_cleanups_ok == 0)
1428 begin_scope (sk_cleanup, NULL);
1429 current_binding_level->statement_list = push_stmt_list ();
1433 /* Nonzero if we are currently in the global binding level. */
1436 global_bindings_p (void)
1438 return global_scope_p (current_binding_level);
1441 /* True if we are currently in a toplevel binding level. This
1442 means either the global binding level or a namespace in a toplevel
1443 binding level. Since there are no non-toplevel namespace levels,
1444 this really means any namespace or template parameter level. We
1445 also include a class whose context is toplevel. */
1448 toplevel_bindings_p (void)
1450 struct cp_binding_level *b = innermost_nonclass_level ();
1452 return b->kind == sk_namespace || b->kind == sk_template_parms;
1455 /* True if this is a namespace scope, or if we are defining a class
1456 which is itself at namespace scope, or whose enclosing class is
1457 such a class, etc. */
1460 namespace_bindings_p (void)
1462 struct cp_binding_level *b = innermost_nonclass_level ();
1464 return b->kind == sk_namespace;
1467 /* True if the current level needs to have a BLOCK made. */
1472 return (current_binding_level->blocks != NULL_TREE
1473 || current_binding_level->keep
1474 || current_binding_level->kind == sk_cleanup
1475 || current_binding_level->names != NULL_TREE);
1478 /* Returns the kind of the innermost scope. */
1481 innermost_scope_kind (void)
1483 return current_binding_level->kind;
1486 /* Returns true if this scope was created to store template parameters. */
1489 template_parm_scope_p (void)
1491 return innermost_scope_kind () == sk_template_parms;
1494 /* If KEEP is true, make a BLOCK node for the next binding level,
1495 unconditionally. Otherwise, use the normal logic to decide whether
1496 or not to create a BLOCK. */
1499 keep_next_level (bool keep)
1501 keep_next_level_flag = keep;
1504 /* Return the list of declarations of the current level.
1505 Note that this list is in reverse order unless/until
1506 you nreverse it; and when you do nreverse it, you must
1507 store the result back using `storedecls' or you will lose. */
1512 return current_binding_level->names;
1515 /* For debugging. */
1516 static int no_print_functions = 0;
1517 static int no_print_builtins = 0;
1520 print_binding_level (struct cp_binding_level* lvl)
1524 fprintf (stderr, " blocks=%p", (void *) lvl->blocks);
1525 if (lvl->more_cleanups_ok)
1526 fprintf (stderr, " more-cleanups-ok");
1527 if (lvl->have_cleanups)
1528 fprintf (stderr, " have-cleanups");
1529 fprintf (stderr, "\n");
1532 fprintf (stderr, " names:\t");
1533 /* We can probably fit 3 names to a line? */
1534 for (t = lvl->names; t; t = TREE_CHAIN (t))
1536 if (no_print_functions && (TREE_CODE (t) == FUNCTION_DECL))
1538 if (no_print_builtins
1539 && (TREE_CODE (t) == TYPE_DECL)
1540 && DECL_IS_BUILTIN (t))
1543 /* Function decls tend to have longer names. */
1544 if (TREE_CODE (t) == FUNCTION_DECL)
1551 fprintf (stderr, "\n\t");
1554 print_node_brief (stderr, "", t, 0);
1555 if (t == error_mark_node)
1559 fprintf (stderr, "\n");
1561 if (VEC_length (cp_class_binding, lvl->class_shadowed))
1564 cp_class_binding *b;
1565 fprintf (stderr, " class-shadowed:");
1567 VEC_iterate(cp_class_binding, lvl->class_shadowed, i, b);
1569 fprintf (stderr, " %s ", IDENTIFIER_POINTER (b->identifier));
1570 fprintf (stderr, "\n");
1572 if (lvl->type_shadowed)
1574 fprintf (stderr, " type-shadowed:");
1575 for (t = lvl->type_shadowed; t; t = TREE_CHAIN (t))
1577 fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
1579 fprintf (stderr, "\n");
1584 print_other_binding_stack (struct cp_binding_level *stack)
1586 struct cp_binding_level *level;
1587 for (level = stack; !global_scope_p (level); level = level->level_chain)
1589 fprintf (stderr, "binding level %p\n", (void *) level);
1590 print_binding_level (level);
1595 print_binding_stack (void)
1597 struct cp_binding_level *b;
1598 fprintf (stderr, "current_binding_level=%p\n"
1599 "class_binding_level=%p\n"
1600 "NAMESPACE_LEVEL (global_namespace)=%p\n",
1601 (void *) current_binding_level, (void *) class_binding_level,
1602 (void *) NAMESPACE_LEVEL (global_namespace));
1603 if (class_binding_level)
1605 for (b = class_binding_level; b; b = b->level_chain)
1606 if (b == current_binding_level)
1609 b = class_binding_level;
1611 b = current_binding_level;
1614 b = current_binding_level;
1615 print_other_binding_stack (b);
1616 fprintf (stderr, "global:\n");
1617 print_binding_level (NAMESPACE_LEVEL (global_namespace));
1620 /* Return the type associated with id. */
1623 identifier_type_value (tree id)
1625 timevar_push (TV_NAME_LOOKUP);
1626 /* There is no type with that name, anywhere. */
1627 if (REAL_IDENTIFIER_TYPE_VALUE (id) == NULL_TREE)
1628 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
1629 /* This is not the type marker, but the real thing. */
1630 if (REAL_IDENTIFIER_TYPE_VALUE (id) != global_type_node)
1631 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, REAL_IDENTIFIER_TYPE_VALUE (id));
1632 /* Have to search for it. It must be on the global level, now.
1633 Ask lookup_name not to return non-types. */
1634 id = lookup_name_real (id, 2, 1, /*block_p=*/true, 0, LOOKUP_COMPLAIN);
1636 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, TREE_TYPE (id));
1637 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
1640 /* Return the IDENTIFIER_GLOBAL_VALUE of T, for use in common code, since
1641 the definition of IDENTIFIER_GLOBAL_VALUE is different for C and C++. */
1644 identifier_global_value (tree t)
1646 return IDENTIFIER_GLOBAL_VALUE (t);
1649 /* Push a definition of struct, union or enum tag named ID. into
1650 binding_level B. DECL is a TYPE_DECL for the type. We assume that
1651 the tag ID is not already defined. */
1654 set_identifier_type_value_with_scope (tree id, tree decl, cxx_scope *b)
1658 if (b->kind != sk_namespace)
1660 /* Shadow the marker, not the real thing, so that the marker
1661 gets restored later. */
1662 tree old_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
1664 = tree_cons (id, old_type_value, b->type_shadowed);
1665 type = decl ? TREE_TYPE (decl) : NULL_TREE;
1666 TREE_TYPE (b->type_shadowed) = type;
1670 cxx_binding *binding =
1671 binding_for_name (NAMESPACE_LEVEL (current_namespace), id);
1674 supplement_binding (binding, decl);
1676 binding->value = decl;
1678 /* Store marker instead of real type. */
1679 type = global_type_node;
1681 SET_IDENTIFIER_TYPE_VALUE (id, type);
1684 /* As set_identifier_type_value_with_scope, but using
1685 current_binding_level. */
1688 set_identifier_type_value (tree id, tree decl)
1690 set_identifier_type_value_with_scope (id, decl, current_binding_level);
1693 /* Return the name for the constructor (or destructor) for the
1694 specified class TYPE. When given a template, this routine doesn't
1695 lose the specialization. */
1698 constructor_name_full (tree type)
1700 return TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
1703 /* Return the name for the constructor (or destructor) for the
1704 specified class. When given a template, return the plain
1705 unspecialized name. */
1708 constructor_name (tree type)
1711 name = constructor_name_full (type);
1712 if (IDENTIFIER_TEMPLATE (name))
1713 name = IDENTIFIER_TEMPLATE (name);
1717 /* Returns TRUE if NAME is the name for the constructor for TYPE. */
1720 constructor_name_p (tree name, tree type)
1727 if (TREE_CODE (name) != IDENTIFIER_NODE)
1730 ctor_name = constructor_name_full (type);
1731 if (name == ctor_name)
1733 if (IDENTIFIER_TEMPLATE (ctor_name)
1734 && name == IDENTIFIER_TEMPLATE (ctor_name))
1739 /* Counter used to create anonymous type names. */
1741 static GTY(()) int anon_cnt;
1743 /* Return an IDENTIFIER which can be used as a name for
1744 anonymous structs and unions. */
1747 make_anon_name (void)
1751 sprintf (buf, ANON_AGGRNAME_FORMAT, anon_cnt++);
1752 return get_identifier (buf);
1755 /* Return (from the stack of) the BINDING, if any, established at SCOPE. */
1757 static inline cxx_binding *
1758 find_binding (cxx_scope *scope, cxx_binding *binding)
1760 timevar_push (TV_NAME_LOOKUP);
1762 for (; binding != NULL; binding = binding->previous)
1763 if (binding->scope == scope)
1764 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, binding);
1766 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, (cxx_binding *)0);
1769 /* Return the binding for NAME in SCOPE, if any. Otherwise, return NULL. */
1771 static inline cxx_binding *
1772 cxx_scope_find_binding_for_name (cxx_scope *scope, tree name)
1774 cxx_binding *b = IDENTIFIER_NAMESPACE_BINDINGS (name);
1777 /* Fold-in case where NAME is used only once. */
1778 if (scope == b->scope && b->previous == NULL)
1780 return find_binding (scope, b);
1785 /* Always returns a binding for name in scope. If no binding is
1786 found, make a new one. */
1788 static cxx_binding *
1789 binding_for_name (cxx_scope *scope, tree name)
1791 cxx_binding *result;
1793 result = cxx_scope_find_binding_for_name (scope, name);
1796 /* Not found, make a new one. */
1797 result = cxx_binding_make (NULL, NULL);
1798 result->previous = IDENTIFIER_NAMESPACE_BINDINGS (name);
1799 result->scope = scope;
1800 result->is_local = false;
1801 result->value_is_inherited = false;
1802 /* APPLE LOCAL blocks 6040305 (ch) */
1803 result->declared_in_block = 0;
1804 IDENTIFIER_NAMESPACE_BINDINGS (name) = result;
1808 /* Insert another USING_DECL into the current binding level, returning
1809 this declaration. If this is a redeclaration, do nothing, and
1810 return NULL_TREE if this not in namespace scope (in namespace
1811 scope, a using decl might extend any previous bindings). */
1814 push_using_decl (tree scope, tree name)
1818 timevar_push (TV_NAME_LOOKUP);
1819 gcc_assert (TREE_CODE (scope) == NAMESPACE_DECL);
1820 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
1821 for (decl = current_binding_level->usings; decl; decl = TREE_CHAIN (decl))
1822 if (USING_DECL_SCOPE (decl) == scope && DECL_NAME (decl) == name)
1825 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP,
1826 namespace_bindings_p () ? decl : NULL_TREE);
1827 decl = build_lang_decl (USING_DECL, name, NULL_TREE);
1828 USING_DECL_SCOPE (decl) = scope;
1829 TREE_CHAIN (decl) = current_binding_level->usings;
1830 current_binding_level->usings = decl;
1831 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
1834 /* Same as pushdecl, but define X in binding-level LEVEL. We rely on the
1835 caller to set DECL_CONTEXT properly. */
1838 pushdecl_with_scope (tree x, cxx_scope *level, bool is_friend)
1840 struct cp_binding_level *b;
1841 tree function_decl = current_function_decl;
1843 timevar_push (TV_NAME_LOOKUP);
1844 current_function_decl = NULL_TREE;
1845 if (level->kind == sk_class)
1847 b = class_binding_level;
1848 class_binding_level = level;
1849 pushdecl_class_level (x);
1850 class_binding_level = b;
1854 b = current_binding_level;
1855 current_binding_level = level;
1856 x = pushdecl_maybe_friend (x, is_friend);
1857 current_binding_level = b;
1859 current_function_decl = function_decl;
1860 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
1863 /* DECL is a FUNCTION_DECL for a non-member function, which may have
1864 other definitions already in place. We get around this by making
1865 the value of the identifier point to a list of all the things that
1866 want to be referenced by that name. It is then up to the users of
1867 that name to decide what to do with that list.
1869 DECL may also be a TEMPLATE_DECL, with a FUNCTION_DECL in its
1870 DECL_TEMPLATE_RESULT. It is dealt with the same way.
1872 FLAGS is a bitwise-or of the following values:
1873 PUSH_LOCAL: Bind DECL in the current scope, rather than at
1875 PUSH_USING: DECL is being pushed as the result of a using
1878 IS_FRIEND is true if this is a friend declaration.
1880 The value returned may be a previous declaration if we guessed wrong
1881 about what language DECL should belong to (C or C++). Otherwise,
1882 it's always DECL (and never something that's not a _DECL). */
1885 push_overloaded_decl (tree decl, int flags, bool is_friend)
1887 tree name = DECL_NAME (decl);
1890 int doing_global = (namespace_bindings_p () || !(flags & PUSH_LOCAL));
1892 timevar_push (TV_NAME_LOOKUP);
1894 old = namespace_binding (name, DECL_CONTEXT (decl));
1896 old = lookup_name_innermost_nonclass_level (name);
1900 if (TREE_CODE (old) == TYPE_DECL && DECL_ARTIFICIAL (old))
1902 tree t = TREE_TYPE (old);
1903 if (IS_AGGR_TYPE (t) && warn_shadow
1904 && (! DECL_IN_SYSTEM_HEADER (decl)
1905 || ! DECL_IN_SYSTEM_HEADER (old)))
1906 warning (0, "%q#D hides constructor for %q#T", decl, t);
1909 else if (is_overloaded_fn (old))
1913 for (tmp = old; tmp; tmp = OVL_NEXT (tmp))
1915 tree fn = OVL_CURRENT (tmp);
1918 if (TREE_CODE (tmp) == OVERLOAD && OVL_USED (tmp)
1919 && !(flags & PUSH_USING)
1920 && compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
1921 TYPE_ARG_TYPES (TREE_TYPE (decl)))
1922 && ! decls_match (fn, decl))
1923 error ("%q#D conflicts with previous using declaration %q#D",
1926 dup = duplicate_decls (decl, fn, is_friend);
1927 /* If DECL was a redeclaration of FN -- even an invalid
1928 one -- pass that information along to our caller. */
1929 if (dup == fn || dup == error_mark_node)
1930 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, dup);
1933 /* We don't overload implicit built-ins. duplicate_decls()
1934 may fail to merge the decls if the new decl is e.g. a
1935 template function. */
1936 if (TREE_CODE (old) == FUNCTION_DECL
1937 && DECL_ANTICIPATED (old)
1938 && !DECL_HIDDEN_FRIEND_P (old))
1941 else if (old == error_mark_node)
1942 /* Ignore the undefined symbol marker. */
1946 error ("previous non-function declaration %q+#D", old);
1947 error ("conflicts with function declaration %q#D", decl);
1948 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
1952 if (old || TREE_CODE (decl) == TEMPLATE_DECL
1953 /* If it's a using declaration, we always need to build an OVERLOAD,
1954 because it's the only way to remember that the declaration comes
1955 from 'using', and have the lookup behave correctly. */
1956 || (flags & PUSH_USING))
1958 if (old && TREE_CODE (old) != OVERLOAD)
1959 new_binding = ovl_cons (decl, ovl_cons (old, NULL_TREE));
1961 new_binding = ovl_cons (decl, old);
1962 if (flags & PUSH_USING)
1963 OVL_USED (new_binding) = 1;
1966 /* NAME is not ambiguous. */
1970 set_namespace_binding (name, current_namespace, new_binding);
1973 /* We only create an OVERLOAD if there was a previous binding at
1974 this level, or if decl is a template. In the former case, we
1975 need to remove the old binding and replace it with the new
1976 binding. We must also run through the NAMES on the binding
1977 level where the name was bound to update the chain. */
1979 if (TREE_CODE (new_binding) == OVERLOAD && old)
1983 for (d = &IDENTIFIER_BINDING (name)->scope->names;
1985 d = &TREE_CHAIN (*d))
1987 || (TREE_CODE (*d) == TREE_LIST
1988 && TREE_VALUE (*d) == old))
1990 if (TREE_CODE (*d) == TREE_LIST)
1991 /* Just replace the old binding with the new. */
1992 TREE_VALUE (*d) = new_binding;
1994 /* Build a TREE_LIST to wrap the OVERLOAD. */
1995 *d = tree_cons (NULL_TREE, new_binding,
1998 /* And update the cxx_binding node. */
1999 IDENTIFIER_BINDING (name)->value = new_binding;
2000 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
2003 /* We should always find a previous binding in this case. */
2007 /* Install the new binding. */
2008 push_local_binding (name, new_binding, flags);
2011 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
2014 /* Check a non-member using-declaration. Return the name and scope
2015 being used, and the USING_DECL, or NULL_TREE on failure. */
2018 validate_nonmember_using_decl (tree decl, tree scope, tree name)
2020 /* [namespace.udecl]
2021 A using-declaration for a class member shall be a
2022 member-declaration. */
2025 error ("%qT is not a namespace", scope);
2028 else if (scope == error_mark_node)
2031 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR)
2034 A using-declaration shall not name a template-id. */
2035 error ("a using-declaration cannot specify a template-id. "
2036 "Try %<using %D%>", name);
2040 if (TREE_CODE (decl) == NAMESPACE_DECL)
2042 error ("namespace %qD not allowed in using-declaration", decl);
2046 if (TREE_CODE (decl) == SCOPE_REF)
2048 /* It's a nested name with template parameter dependent scope.
2049 This can only be using-declaration for class member. */
2050 error ("%qT is not a namespace", TREE_OPERAND (decl, 0));
2054 if (is_overloaded_fn (decl))
2055 decl = get_first_fn (decl);
2057 gcc_assert (DECL_P (decl));
2059 /* Make a USING_DECL. */
2060 return push_using_decl (scope, name);
2063 /* Process local and global using-declarations. */
2066 do_nonmember_using_decl (tree scope, tree name, tree oldval, tree oldtype,
2067 tree *newval, tree *newtype)
2069 struct scope_binding decls = EMPTY_SCOPE_BINDING;
2071 *newval = *newtype = NULL_TREE;
2072 if (!qualified_lookup_using_namespace (name, scope, &decls, 0))
2076 if (!decls.value && !decls.type)
2078 error ("%qD not declared", name);
2082 /* LLVM LOCAL begin mainline */
2083 /* Shift the old and new bindings around so we're comparing class and
2084 enumeration names to each other. */
2085 if (oldval && DECL_IMPLICIT_TYPEDEF_P (oldval))
2091 if (decls.value && DECL_IMPLICIT_TYPEDEF_P (decls.value))
2093 decls.type = decls.value;
2094 decls.value = NULL_TREE;
2096 /* LLVM LOCAL end mainline */
2098 /* It is impossible to overload a built-in function; any explicit
2099 declaration eliminates the built-in declaration. So, if OLDVAL
2100 is a built-in, then we can just pretend it isn't there. */
2102 && TREE_CODE (oldval) == FUNCTION_DECL
2103 && DECL_ANTICIPATED (oldval)
2104 && !DECL_HIDDEN_FRIEND_P (oldval))
2107 /* LLVM LOCAL begin mainline */
2110 /* Check for using functions. */
2111 if (is_overloaded_fn (decls.value))
2115 if (oldval && !is_overloaded_fn (oldval))
2117 error ("%qD is already declared in this scope", name);
2122 for (tmp = decls.value; tmp; tmp = OVL_NEXT (tmp))
2124 tree new_fn = OVL_CURRENT (tmp);
2126 /* [namespace.udecl]
2128 If a function declaration in namespace scope or block
2129 scope has the same name and the same parameter types as a
2130 function introduced by a using declaration the program is
2132 for (tmp1 = oldval; tmp1; tmp1 = OVL_NEXT (tmp1))
2134 tree old_fn = OVL_CURRENT (tmp1);
2136 if (new_fn == old_fn)
2137 /* The function already exists in the current namespace. */
2139 else if (OVL_USED (tmp1))
2140 continue; /* this is a using decl */
2141 else if (compparms (TYPE_ARG_TYPES (TREE_TYPE (new_fn)),
2142 TYPE_ARG_TYPES (TREE_TYPE (old_fn))))
2144 gcc_assert (!DECL_ANTICIPATED (old_fn)
2145 || DECL_HIDDEN_FRIEND_P (old_fn));
2147 /* There was already a non-using declaration in
2148 this scope with the same parameter types. If both
2149 are the same extern "C" functions, that's ok. */
2150 if (decls_match (new_fn, old_fn))
2154 error ("%qD is already declared in this scope", name);
2160 /* If we broke out of the loop, there's no reason to add
2161 this function to the using declarations for this
2166 /* If we are adding to an existing OVERLOAD, then we no
2167 longer know the type of the set of functions. */
2168 if (*newval && TREE_CODE (*newval) == OVERLOAD)
2169 TREE_TYPE (*newval) = unknown_type_node;
2170 /* Add this new function to the set. */
2171 *newval = build_overload (OVL_CURRENT (tmp), *newval);
2172 /* If there is only one function, then we use its type. (A
2173 using-declaration naming a single function can be used in
2174 contexts where overload resolution cannot be
2176 if (TREE_CODE (*newval) != OVERLOAD)
2178 *newval = ovl_cons (*newval, NULL_TREE);
2179 TREE_TYPE (*newval) = TREE_TYPE (OVL_CURRENT (tmp));
2181 OVL_USED (*newval) = 1;
2186 *newval = decls.value;
2187 if (oldval && !decls_match (*newval, oldval))
2188 error ("%qD is already declared in this scope", name);
2194 if (decls.type && TREE_CODE (decls.type) == TREE_LIST)
2196 error ("reference to %qD is ambiguous", name);
2197 print_candidates (decls.type);
2201 *newtype = decls.type;
2202 if (oldtype && *newtype && !decls_match (oldtype, *newtype))
2203 error ("%qD is already declared in this scope", name);
2206 /* If *newval is empty, shift any class or enumeration name down. */
2210 *newtype = NULL_TREE;
2212 /* LLVM LOCAL end mainline */
2215 /* Process a using-declaration at function scope. */
2218 do_local_using_decl (tree decl, tree scope, tree name)
2220 tree oldval, oldtype, newval, newtype;
2221 tree orig_decl = decl;
2223 decl = validate_nonmember_using_decl (decl, scope, name);
2224 if (decl == NULL_TREE)
2227 if (building_stmt_tree ()
2228 && at_function_scope_p ())
2229 add_decl_expr (decl);
2231 oldval = lookup_name_innermost_nonclass_level (name);
2232 oldtype = lookup_type_current_level (name);
2234 do_nonmember_using_decl (scope, name, oldval, oldtype, &newval, &newtype);
2238 if (is_overloaded_fn (newval))
2242 /* We only need to push declarations for those functions
2243 that were not already bound in the current level.
2244 The old value might be NULL_TREE, it might be a single
2245 function, or an OVERLOAD. */
2246 if (oldval && TREE_CODE (oldval) == OVERLOAD)
2247 term = OVL_FUNCTION (oldval);
2250 for (fn = newval; fn && OVL_CURRENT (fn) != term;
2252 push_overloaded_decl (OVL_CURRENT (fn),
2253 PUSH_LOCAL | PUSH_USING,
2257 push_local_binding (name, newval, PUSH_USING);
2261 push_local_binding (name, newtype, PUSH_USING);
2262 set_identifier_type_value (name, newtype);
2265 /* Emit debug info. */
2266 if (!processing_template_decl)
2267 cp_emit_debug_info_for_using (orig_decl, current_scope());
2270 /* Returns true if ROOT (a namespace, class, or function) encloses
2271 CHILD. CHILD may be either a class type or a namespace. */
2274 is_ancestor (tree root, tree child)
2276 gcc_assert ((TREE_CODE (root) == NAMESPACE_DECL
2277 || TREE_CODE (root) == FUNCTION_DECL
2278 || CLASS_TYPE_P (root)));
2279 gcc_assert ((TREE_CODE (child) == NAMESPACE_DECL
2280 || CLASS_TYPE_P (child)));
2282 /* The global namespace encloses everything. */
2283 if (root == global_namespace)
2288 /* If we've run out of scopes, stop. */
2291 /* If we've reached the ROOT, it encloses CHILD. */
2294 /* Go out one level. */
2296 child = TYPE_NAME (child);
2297 child = DECL_CONTEXT (child);
2301 /* Enter the class or namespace scope indicated by T suitable for name
2302 lookup. T can be arbitrary scope, not necessary nested inside the
2303 current scope. Returns a non-null scope to pop iff pop_scope
2304 should be called later to exit this scope. */
2309 if (TREE_CODE (t) == NAMESPACE_DECL)
2310 push_decl_namespace (t);
2311 else if (CLASS_TYPE_P (t))
2313 if (!at_class_scope_p ()
2314 || !same_type_p (current_class_type, t))
2315 push_nested_class (t);
2317 /* T is the same as the current scope. There is therefore no
2318 need to re-enter the scope. Since we are not actually
2319 pushing a new scope, our caller should not call
2327 /* Leave scope pushed by push_scope. */
2332 if (TREE_CODE (t) == NAMESPACE_DECL)
2333 pop_decl_namespace ();
2334 else if CLASS_TYPE_P (t)
2335 pop_nested_class ();
2338 /* Subroutine of push_inner_scope. */
2341 push_inner_scope_r (tree outer, tree inner)
2346 || (TREE_CODE (inner) != NAMESPACE_DECL && !CLASS_TYPE_P (inner)))
2349 prev = CP_DECL_CONTEXT (TREE_CODE (inner) == NAMESPACE_DECL ? inner : TYPE_NAME (inner));
2351 push_inner_scope_r (outer, prev);
2352 if (TREE_CODE (inner) == NAMESPACE_DECL)
2354 struct cp_binding_level *save_template_parm = 0;
2355 /* Temporary take out template parameter scopes. They are saved
2356 in reversed order in save_template_parm. */
2357 while (current_binding_level->kind == sk_template_parms)
2359 struct cp_binding_level *b = current_binding_level;
2360 current_binding_level = b->level_chain;
2361 b->level_chain = save_template_parm;
2362 save_template_parm = b;
2365 resume_scope (NAMESPACE_LEVEL (inner));
2366 current_namespace = inner;
2368 /* Restore template parameter scopes. */
2369 while (save_template_parm)
2371 struct cp_binding_level *b = save_template_parm;
2372 save_template_parm = b->level_chain;
2373 b->level_chain = current_binding_level;
2374 current_binding_level = b;
2381 /* Enter the scope INNER from current scope. INNER must be a scope
2382 nested inside current scope. This works with both name lookup and
2383 pushing name into scope. In case a template parameter scope is present,
2384 namespace is pushed under the template parameter scope according to
2385 name lookup rule in 14.6.1/6.
2387 Return the former current scope suitable for pop_inner_scope. */
2390 push_inner_scope (tree inner)
2392 tree outer = current_scope ();
2394 outer = current_namespace;
2396 push_inner_scope_r (outer, inner);
2400 /* Exit the current scope INNER back to scope OUTER. */
2403 pop_inner_scope (tree outer, tree inner)
2406 || (TREE_CODE (inner) != NAMESPACE_DECL && !CLASS_TYPE_P (inner)))
2409 while (outer != inner)
2411 if (TREE_CODE (inner) == NAMESPACE_DECL)
2413 struct cp_binding_level *save_template_parm = 0;
2414 /* Temporary take out template parameter scopes. They are saved
2415 in reversed order in save_template_parm. */
2416 while (current_binding_level->kind == sk_template_parms)
2418 struct cp_binding_level *b = current_binding_level;
2419 current_binding_level = b->level_chain;
2420 b->level_chain = save_template_parm;
2421 save_template_parm = b;
2426 /* Restore template parameter scopes. */
2427 while (save_template_parm)
2429 struct cp_binding_level *b = save_template_parm;
2430 save_template_parm = b->level_chain;
2431 b->level_chain = current_binding_level;
2432 current_binding_level = b;
2438 inner = CP_DECL_CONTEXT (TREE_CODE (inner) == NAMESPACE_DECL ? inner : TYPE_NAME (inner));
2442 /* Do a pushlevel for class declarations. */
2445 pushlevel_class (void)
2447 if (ENABLE_SCOPE_CHECKING)
2450 class_binding_level = begin_scope (sk_class, current_class_type);
2453 /* ...and a poplevel for class declarations. */
2456 poplevel_class (void)
2458 struct cp_binding_level *level = class_binding_level;
2459 cp_class_binding *cb;
2463 timevar_push (TV_NAME_LOOKUP);
2464 gcc_assert (level != 0);
2466 /* If we're leaving a toplevel class, cache its binding level. */
2467 if (current_class_depth == 1)
2468 previous_class_level = level;
2469 for (shadowed = level->type_shadowed;
2471 shadowed = TREE_CHAIN (shadowed))
2472 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (shadowed), TREE_VALUE (shadowed));
2474 /* Remove the bindings for all of the class-level declarations. */
2475 if (level->class_shadowed)
2478 VEC_iterate (cp_class_binding, level->class_shadowed, i, cb);
2481 IDENTIFIER_BINDING (cb->identifier) = cb->base->previous;
2482 cxx_binding_free (cb->base);
2484 ggc_free (level->class_shadowed);
2485 level->class_shadowed = NULL;
2488 /* Now, pop out of the binding level which we created up in the
2489 `pushlevel_class' routine. */
2490 if (ENABLE_SCOPE_CHECKING)
2494 timevar_pop (TV_NAME_LOOKUP);
2497 /* Set INHERITED_VALUE_BINDING_P on BINDING to true or false, as
2498 appropriate. DECL is the value to which a name has just been
2499 bound. CLASS_TYPE is the class in which the lookup occurred. */
2502 set_inherited_value_binding_p (cxx_binding *binding, tree decl,
2505 if (binding->value == decl && TREE_CODE (decl) != TREE_LIST)
2509 if (TREE_CODE (decl) == OVERLOAD)
2510 context = CP_DECL_CONTEXT (OVL_CURRENT (decl));
2513 gcc_assert (DECL_P (decl));
2514 context = context_for_name_lookup (decl);
2517 if (is_properly_derived_from (class_type, context))
2518 INHERITED_VALUE_BINDING_P (binding) = 1;
2520 INHERITED_VALUE_BINDING_P (binding) = 0;
2522 else if (binding->value == decl)
2523 /* We only encounter a TREE_LIST when there is an ambiguity in the
2524 base classes. Such an ambiguity can be overridden by a
2525 definition in this class. */
2526 INHERITED_VALUE_BINDING_P (binding) = 1;
2528 INHERITED_VALUE_BINDING_P (binding) = 0;
2531 /* Make the declaration of X appear in CLASS scope. */
2534 pushdecl_class_level (tree x)
2537 bool is_valid = true;
2539 timevar_push (TV_NAME_LOOKUP);
2540 /* Get the name of X. */
2541 if (TREE_CODE (x) == OVERLOAD)
2542 name = DECL_NAME (get_first_fn (x));
2544 name = DECL_NAME (x);
2548 is_valid = push_class_level_binding (name, x);
2549 if (TREE_CODE (x) == TYPE_DECL)
2550 set_identifier_type_value (name, x);
2552 else if (ANON_AGGR_TYPE_P (TREE_TYPE (x)))
2554 /* If X is an anonymous aggregate, all of its members are
2555 treated as if they were members of the class containing the
2556 aggregate, for naming purposes. */
2559 for (f = TYPE_FIELDS (TREE_TYPE (x)); f; f = TREE_CHAIN (f))
2561 location_t save_location = input_location;
2562 input_location = DECL_SOURCE_LOCATION (f);
2563 if (!pushdecl_class_level (f))
2565 input_location = save_location;
2568 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, is_valid);
2571 /* Return the BINDING (if any) for NAME in SCOPE, which is a class
2572 scope. If the value returned is non-NULL, and the PREVIOUS field
2573 is not set, callers must set the PREVIOUS field explicitly. */
2575 static cxx_binding *
2576 get_class_binding (tree name, cxx_scope *scope)
2581 cxx_binding *binding;
2583 class_type = scope->this_entity;
2585 /* Get the type binding. */
2586 type_binding = lookup_member (class_type, name,
2587 /*protect=*/2, /*want_type=*/true);
2588 /* Get the value binding. */
2589 value_binding = lookup_member (class_type, name,
2590 /*protect=*/2, /*want_type=*/false);
2593 && (TREE_CODE (value_binding) == TYPE_DECL
2594 || DECL_CLASS_TEMPLATE_P (value_binding)
2595 || (TREE_CODE (value_binding) == TREE_LIST
2596 && TREE_TYPE (value_binding) == error_mark_node
2597 && (TREE_CODE (TREE_VALUE (value_binding))
2599 /* We found a type binding, even when looking for a non-type
2600 binding. This means that we already processed this binding
2603 else if (value_binding)
2605 if (TREE_CODE (value_binding) == TREE_LIST
2606 && TREE_TYPE (value_binding) == error_mark_node)
2607 /* NAME is ambiguous. */
2609 else if (BASELINK_P (value_binding))
2610 /* NAME is some overloaded functions. */
2611 value_binding = BASELINK_FUNCTIONS (value_binding);
2614 /* If we found either a type binding or a value binding, create a
2615 new binding object. */
2616 if (type_binding || value_binding)
2618 binding = new_class_binding (name,
2622 /* This is a class-scope binding, not a block-scope binding. */
2623 LOCAL_BINDING_P (binding) = 0;
2624 set_inherited_value_binding_p (binding, value_binding, class_type);
2632 /* Make the declaration(s) of X appear in CLASS scope under the name
2633 NAME. Returns true if the binding is valid. */
2636 push_class_level_binding (tree name, tree x)
2638 cxx_binding *binding;
2642 timevar_push (TV_NAME_LOOKUP);
2643 /* The class_binding_level will be NULL if x is a template
2644 parameter name in a member template. */
2645 if (!class_binding_level)
2646 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, true);
2648 if (name == error_mark_node)
2649 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, false);
2651 /* Check for invalid member names. */
2652 gcc_assert (TYPE_BEING_DEFINED (current_class_type));
2653 /* We could have been passed a tree list if this is an ambiguous
2654 declaration. If so, pull the declaration out because
2655 check_template_shadow will not handle a TREE_LIST. */
2656 if (TREE_CODE (decl) == TREE_LIST
2657 && TREE_TYPE (decl) == error_mark_node)
2658 decl = TREE_VALUE (decl);
2660 check_template_shadow (decl);
2664 If T is the name of a class, then each of the following shall
2665 have a name different from T:
2667 -- every static data member of class T;
2669 -- every member of class T that is itself a type;
2671 -- every enumerator of every member of class T that is an
2674 -- every member of every anonymous union that is a member of
2677 (Non-static data members were also forbidden to have the same
2678 name as T until TC1.) */
2679 if ((TREE_CODE (x) == VAR_DECL
2680 || TREE_CODE (x) == CONST_DECL
2681 || (TREE_CODE (x) == TYPE_DECL
2682 && !DECL_SELF_REFERENCE_P (x))
2683 /* A data member of an anonymous union. */
2684 || (TREE_CODE (x) == FIELD_DECL
2685 && DECL_CONTEXT (x) != current_class_type))
2686 && DECL_NAME (x) == constructor_name (current_class_type))
2688 tree scope = context_for_name_lookup (x);
2689 if (TYPE_P (scope) && same_type_p (scope, current_class_type))
2691 error ("%qD has the same name as the class in which it is "
2694 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, false);
2698 /* Get the current binding for NAME in this class, if any. */
2699 binding = IDENTIFIER_BINDING (name);
2700 if (!binding || binding->scope != class_binding_level)
2702 binding = get_class_binding (name, class_binding_level);
2703 /* If a new binding was created, put it at the front of the
2704 IDENTIFIER_BINDING list. */
2707 binding->previous = IDENTIFIER_BINDING (name);
2708 IDENTIFIER_BINDING (name) = binding;
2712 /* If there is already a binding, then we may need to update the
2714 if (binding && binding->value)
2716 tree bval = binding->value;
2717 tree old_decl = NULL_TREE;
2719 if (INHERITED_VALUE_BINDING_P (binding))
2721 /* If the old binding was from a base class, and was for a
2722 tag name, slide it over to make room for the new binding.
2723 The old binding is still visible if explicitly qualified
2724 with a class-key. */
2725 if (TREE_CODE (bval) == TYPE_DECL && DECL_ARTIFICIAL (bval)
2726 && !(TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x)))
2728 old_decl = binding->type;
2729 binding->type = bval;
2730 binding->value = NULL_TREE;
2731 INHERITED_VALUE_BINDING_P (binding) = 0;
2736 /* Any inherited type declaration is hidden by the type
2737 declaration in the derived class. */
2738 if (TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x))
2739 binding->type = NULL_TREE;
2742 else if (TREE_CODE (x) == OVERLOAD && is_overloaded_fn (bval))
2744 else if (TREE_CODE (x) == USING_DECL && TREE_CODE (bval) == USING_DECL)
2745 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, true);
2746 else if (TREE_CODE (x) == USING_DECL && is_overloaded_fn (bval))
2748 else if (TREE_CODE (bval) == USING_DECL && is_overloaded_fn (x))
2749 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, true);
2751 if (old_decl && binding->scope == class_binding_level)
2754 /* It is always safe to clear INHERITED_VALUE_BINDING_P
2755 here. This function is only used to register bindings
2756 from with the class definition itself. */
2757 INHERITED_VALUE_BINDING_P (binding) = 0;
2758 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, true);
2762 /* Note that we declared this value so that we can issue an error if
2763 this is an invalid redeclaration of a name already used for some
2765 note_name_declared_in_class (name, decl);
2767 /* If we didn't replace an existing binding, put the binding on the
2768 stack of bindings for the identifier, and update the shadowed
2770 if (binding && binding->scope == class_binding_level)
2771 /* Supplement the existing binding. */
2772 ok = supplement_binding (binding, decl);
2775 /* Create a new binding. */
2776 push_binding (name, decl, class_binding_level);
2780 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, ok);
2783 /* Process "using SCOPE::NAME" in a class scope. Return the
2784 USING_DECL created. */
2787 do_class_using_decl (tree scope, tree name)
2789 /* The USING_DECL returned by this function. */
2791 /* The declaration (or declarations) name by this using
2792 declaration. NULL if we are in a template and cannot figure out
2793 what has been named. */
2795 /* True if SCOPE is a dependent type. */
2796 bool scope_dependent_p;
2797 /* True if SCOPE::NAME is dependent. */
2798 bool name_dependent_p;
2799 /* True if any of the bases of CURRENT_CLASS_TYPE are dependent. */
2800 bool bases_dependent_p;
2805 if (name == error_mark_node)
2808 if (!scope || !TYPE_P (scope))
2810 error ("using-declaration for non-member at class scope");
2814 /* Make sure the name is not invalid */
2815 if (TREE_CODE (name) == BIT_NOT_EXPR)
2817 error ("%<%T::%D%> names destructor", scope, name);
2820 if (constructor_name_p (name, scope))
2822 error ("%<%T::%D%> names constructor", scope, name);
2825 if (constructor_name_p (name, current_class_type))
2827 error ("%<%T::%D%> names constructor in %qT",
2828 scope, name, current_class_type);
2832 scope_dependent_p = dependent_type_p (scope);
2833 name_dependent_p = (scope_dependent_p
2834 || (IDENTIFIER_TYPENAME_P (name)
2835 && dependent_type_p (TREE_TYPE (name))));
2837 bases_dependent_p = false;
2838 if (processing_template_decl)
2839 for (binfo = TYPE_BINFO (current_class_type), i = 0;
2840 BINFO_BASE_ITERATE (binfo, i, base_binfo);
2842 if (dependent_type_p (TREE_TYPE (base_binfo)))
2844 bases_dependent_p = true;
2850 /* From [namespace.udecl]:
2852 A using-declaration used as a member-declaration shall refer to a
2853 member of a base class of the class being defined.
2855 In general, we cannot check this constraint in a template because
2856 we do not know the entire set of base classes of the current
2857 class type. However, if all of the base classes are
2858 non-dependent, then we can avoid delaying the check until
2860 if (!scope_dependent_p)
2863 binfo = lookup_base (current_class_type, scope, ba_any, &b_kind);
2864 if (b_kind < bk_proper_base)
2866 if (!bases_dependent_p)
2868 error_not_base_type (scope, current_class_type);
2872 else if (!name_dependent_p)
2874 decl = lookup_member (binfo, name, 0, false);
2877 error ("no members matching %<%T::%D%> in %q#T", scope, name,
2881 /* The binfo from which the functions came does not matter. */
2882 if (BASELINK_P (decl))
2883 decl = BASELINK_FUNCTIONS (decl);
2887 value = build_lang_decl (USING_DECL, name, NULL_TREE);
2888 USING_DECL_DECLS (value) = decl;
2889 USING_DECL_SCOPE (value) = scope;
2890 DECL_DEPENDENT_P (value) = !decl;
2896 /* Return the binding value for name in scope. */
2899 namespace_binding (tree name, tree scope)
2901 cxx_binding *binding;
2904 scope = global_namespace;
2906 /* Unnecessary for the global namespace because it can't be an alias. */
2907 scope = ORIGINAL_NAMESPACE (scope);
2909 binding = cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (scope), name);
2911 return binding ? binding->value : NULL_TREE;
2914 /* Set the binding value for name in scope. */
2917 set_namespace_binding (tree name, tree scope, tree val)
2921 timevar_push (TV_NAME_LOOKUP);
2922 if (scope == NULL_TREE)
2923 scope = global_namespace;
2924 b = binding_for_name (NAMESPACE_LEVEL (scope), name);
2925 if (!b->value || TREE_CODE (val) == OVERLOAD || val == error_mark_node)
2928 supplement_binding (b, val);
2929 timevar_pop (TV_NAME_LOOKUP);
2932 /* Set the context of a declaration to scope. Complain if we are not
2936 set_decl_namespace (tree decl, tree scope, bool friendp)
2940 /* Get rid of namespace aliases. */
2941 scope = ORIGINAL_NAMESPACE (scope);
2943 /* It is ok for friends to be qualified in parallel space. */
2944 if (!friendp && !is_ancestor (current_namespace, scope))
2945 error ("declaration of %qD not in a namespace surrounding %qD",
2947 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
2949 /* Writing "int N::i" to declare a variable within "N" is invalid. */
2950 if (scope == current_namespace)
2952 if (at_namespace_scope_p ())
2953 error ("explicit qualification in declaration of %qD",
2958 /* See whether this has been declared in the namespace. */
2959 old = lookup_qualified_name (scope, DECL_NAME (decl), false, true);
2960 if (old == error_mark_node)
2961 /* No old declaration at all. */
2963 if (!is_overloaded_fn (decl))
2964 /* Don't compare non-function decls with decls_match here, since
2965 it can't check for the correct constness at this
2966 point. pushdecl will find those errors later. */
2968 /* Since decl is a function, old should contain a function decl. */
2969 if (!is_overloaded_fn (old))
2971 fn = OVL_CURRENT (old);
2972 if (!is_associated_namespace (scope, CP_DECL_CONTEXT (fn)))
2974 /* A template can be explicitly specialized in any namespace. */
2975 if (processing_explicit_instantiation)
2977 if (processing_template_decl || processing_specialization)
2978 /* We have not yet called push_template_decl to turn a
2979 FUNCTION_DECL into a TEMPLATE_DECL, so the declarations won't
2980 match. But, we'll check later, when we construct the
2983 /* Instantiations or specializations of templates may be declared as
2984 friends in any namespace. */
2985 if (friendp && DECL_USE_TEMPLATE (decl))
2987 if (is_overloaded_fn (old))
2989 for (; old; old = OVL_NEXT (old))
2990 if (decls_match (decl, OVL_CURRENT (old)))
2993 else if (decls_match (decl, old))
2996 error ("%qD should have been declared inside %qD", decl, scope);
2999 /* Return the namespace where the current declaration is declared. */
3002 current_decl_namespace (void)
3005 /* If we have been pushed into a different namespace, use it. */
3006 if (decl_namespace_list)
3007 return TREE_PURPOSE (decl_namespace_list);
3009 if (current_class_type)
3010 result = decl_namespace_context (current_class_type);
3011 else if (current_function_decl)
3012 result = decl_namespace_context (current_function_decl);
3014 result = current_namespace;
3018 /* Push into the scope of the NAME namespace. If NAME is NULL_TREE, then we
3019 select a name that is unique to this compilation unit. */
3022 push_namespace (tree name)
3024 push_namespace_with_attribs (name, NULL_TREE);
3027 /* Same, but specify attributes to apply to the namespace. The attributes
3028 only apply to the current namespace-body, not to any later extensions. */
3031 push_namespace_with_attribs (tree name, tree attributes)
3035 int implicit_use = 0;
3038 timevar_push (TV_NAME_LOOKUP);
3040 /* We should not get here if the global_namespace is not yet constructed
3041 nor if NAME designates the global namespace: The global scope is
3042 constructed elsewhere. */
3043 gcc_assert (global_namespace != NULL && name != global_scope_name);
3047 name = get_anonymous_namespace_name();
3048 d = IDENTIFIER_NAMESPACE_VALUE (name);
3050 /* Reopening anonymous namespace. */
3056 /* Check whether this is an extended namespace definition. */
3057 d = IDENTIFIER_NAMESPACE_VALUE (name);
3058 if (d != NULL_TREE && TREE_CODE (d) == NAMESPACE_DECL)
3061 if (DECL_NAMESPACE_ALIAS (d))
3063 error ("namespace alias %qD not allowed here, assuming %qD",
3064 d, DECL_NAMESPACE_ALIAS (d));
3065 d = DECL_NAMESPACE_ALIAS (d);
3072 /* Make a new namespace, binding the name to it. */
3073 d = build_lang_decl (NAMESPACE_DECL, name, void_type_node);
3074 DECL_CONTEXT (d) = FROB_CONTEXT (current_namespace);
3075 /* The name of this namespace is not visible to other translation
3076 units if it is an anonymous namespace or member thereof. */
3077 if (anon || decl_anon_ns_mem_p (current_namespace))
3078 TREE_PUBLIC (d) = 0;
3080 TREE_PUBLIC (d) = 1;
3084 /* Clear DECL_NAME for the benefit of debugging back ends. */
3085 SET_DECL_ASSEMBLER_NAME (d, name);
3086 DECL_NAME (d) = NULL_TREE;
3088 begin_scope (sk_namespace, d);
3091 resume_scope (NAMESPACE_LEVEL (d));
3094 do_using_directive (d);
3095 /* Enter the name space. */
3096 current_namespace = d;
3098 #ifdef HANDLE_PRAGMA_VISIBILITY
3099 /* Clear has_visibility in case a previous namespace-definition had a
3100 visibility attribute and this one doesn't. */
3101 current_binding_level->has_visibility = 0;
3102 for (d = attributes; d; d = TREE_CHAIN (d))
3104 tree name = TREE_PURPOSE (d);
3105 tree args = TREE_VALUE (d);
3108 if (! is_attribute_p ("visibility", name))
3110 warning (OPT_Wattributes, "%qs attribute directive ignored",
3111 IDENTIFIER_POINTER (name));
3115 x = args ? TREE_VALUE (args) : NULL_TREE;
3116 if (x == NULL_TREE || TREE_CODE (x) != STRING_CST || TREE_CHAIN (args))
3118 warning (OPT_Wattributes, "%qs attribute requires a single NTBS argument",
3119 IDENTIFIER_POINTER (name));
3123 current_binding_level->has_visibility = 1;
3124 push_visibility (TREE_STRING_POINTER (x));
3130 timevar_pop (TV_NAME_LOOKUP);
3133 /* Pop from the scope of the current namespace. */
3136 pop_namespace (void)
3138 gcc_assert (current_namespace != global_namespace);
3139 current_namespace = CP_DECL_CONTEXT (current_namespace);
3140 /* The binding level is not popped, as it might be re-opened later. */
3144 /* Push into the scope of the namespace NS, even if it is deeply
3145 nested within another namespace. */
3148 push_nested_namespace (tree ns)
3150 if (ns == global_namespace)
3151 push_to_top_level ();
3154 push_nested_namespace (CP_DECL_CONTEXT (ns));
3155 push_namespace (DECL_NAME (ns));
3159 /* Pop back from the scope of the namespace NS, which was previously
3160 entered with push_nested_namespace. */
3163 pop_nested_namespace (tree ns)
3165 timevar_push (TV_NAME_LOOKUP);
3166 while (ns != global_namespace)
3169 ns = CP_DECL_CONTEXT (ns);
3172 pop_from_top_level ();
3173 timevar_pop (TV_NAME_LOOKUP);
3176 /* Temporarily set the namespace for the current declaration. */
3179 push_decl_namespace (tree decl)
3181 if (TREE_CODE (decl) != NAMESPACE_DECL)
3182 decl = decl_namespace_context (decl);
3183 decl_namespace_list = tree_cons (ORIGINAL_NAMESPACE (decl),
3184 NULL_TREE, decl_namespace_list);
3187 /* [namespace.memdef]/2 */
3190 pop_decl_namespace (void)
3192 decl_namespace_list = TREE_CHAIN (decl_namespace_list);
3195 /* Return the namespace that is the common ancestor
3196 of two given namespaces. */
3199 namespace_ancestor (tree ns1, tree ns2)
3201 timevar_push (TV_NAME_LOOKUP);
3202 if (is_ancestor (ns1, ns2))
3203 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, ns1);
3204 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP,
3205 namespace_ancestor (CP_DECL_CONTEXT (ns1), ns2));
3208 /* Process a namespace-alias declaration. */
3211 do_namespace_alias (tree alias, tree namespace)
3213 if (namespace == error_mark_node)
3216 gcc_assert (TREE_CODE (namespace) == NAMESPACE_DECL);
3218 namespace = ORIGINAL_NAMESPACE (namespace);
3220 /* Build the alias. */
3221 alias = build_lang_decl (NAMESPACE_DECL, alias, void_type_node);
3222 DECL_NAMESPACE_ALIAS (alias) = namespace;
3223 DECL_EXTERNAL (alias) = 1;
3224 DECL_CONTEXT (alias) = FROB_CONTEXT (current_scope ());
3227 /* Emit debug info for namespace alias. */
3228 (*debug_hooks->global_decl) (alias);
3231 /* Like pushdecl, only it places X in the current namespace,
3235 pushdecl_namespace_level (tree x, bool is_friend)
3237 struct cp_binding_level *b = current_binding_level;
3240 timevar_push (TV_NAME_LOOKUP);
3241 t = pushdecl_with_scope (x, NAMESPACE_LEVEL (current_namespace), is_friend);
3243 /* Now, the type_shadowed stack may screw us. Munge it so it does
3245 if (TREE_CODE (t) == TYPE_DECL)
3247 tree name = DECL_NAME (t);
3249 tree *ptr = (tree *)0;
3250 for (; !global_scope_p (b); b = b->level_chain)
3252 tree shadowed = b->type_shadowed;
3253 for (; shadowed; shadowed = TREE_CHAIN (shadowed))
3254 if (TREE_PURPOSE (shadowed) == name)
3256 ptr = &TREE_VALUE (shadowed);
3257 /* Can't break out of the loop here because sometimes
3258 a binding level will have duplicate bindings for
3259 PT names. It's gross, but I haven't time to fix it. */
3262 newval = TREE_TYPE (t);
3263 if (ptr == (tree *)0)
3265 /* @@ This shouldn't be needed. My test case "zstring.cc" trips
3266 up here if this is changed to an assertion. --KR */
3267 SET_IDENTIFIER_TYPE_VALUE (name, t);
3274 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
3277 /* Insert USED into the using list of USER. Set INDIRECT_flag if this
3278 directive is not directly from the source. Also find the common
3279 ancestor and let our users know about the new namespace */
3281 add_using_namespace (tree user, tree used, bool indirect)
3284 timevar_push (TV_NAME_LOOKUP);
3285 /* Using oneself is a no-op. */
3288 timevar_pop (TV_NAME_LOOKUP);
3291 gcc_assert (TREE_CODE (user) == NAMESPACE_DECL);
3292 gcc_assert (TREE_CODE (used) == NAMESPACE_DECL);
3293 /* Check if we already have this. */
3294 t = purpose_member (used, DECL_NAMESPACE_USING (user));
3298 /* Promote to direct usage. */
3299 TREE_INDIRECT_USING (t) = 0;
3300 timevar_pop (TV_NAME_LOOKUP);
3304 /* Add used to the user's using list. */
3305 DECL_NAMESPACE_USING (user)
3306 = tree_cons (used, namespace_ancestor (user, used),
3307 DECL_NAMESPACE_USING (user));
3309 TREE_INDIRECT_USING (DECL_NAMESPACE_USING (user)) = indirect;
3311 /* Add user to the used's users list. */
3312 DECL_NAMESPACE_USERS (used)
3313 = tree_cons (user, 0, DECL_NAMESPACE_USERS (used));
3315 /* Recursively add all namespaces used. */
3316 for (t = DECL_NAMESPACE_USING (used); t; t = TREE_CHAIN (t))
3317 /* indirect usage */
3318 add_using_namespace (user, TREE_PURPOSE (t), 1);
3320 /* Tell everyone using us about the new used namespaces. */
3321 for (t = DECL_NAMESPACE_USERS (user); t; t = TREE_CHAIN (t))
3322 add_using_namespace (TREE_PURPOSE (t), used, 1);
3323 timevar_pop (TV_NAME_LOOKUP);
3326 /* Process a using-declaration not appearing in class or local scope. */
3329 do_toplevel_using_decl (tree decl, tree scope, tree name)
3331 tree oldval, oldtype, newval, newtype;
3332 tree orig_decl = decl;
3333 cxx_binding *binding;
3335 decl = validate_nonmember_using_decl (decl, scope, name);
3336 if (decl == NULL_TREE)
3339 binding = binding_for_name (NAMESPACE_LEVEL (current_namespace), name);
3341 oldval = binding->value;
3342 oldtype = binding->type;
3344 do_nonmember_using_decl (scope, name, oldval, oldtype, &newval, &newtype);
3346 /* Emit debug info. */
3347 if (!processing_template_decl)
3348 cp_emit_debug_info_for_using (orig_decl, current_namespace);
3350 /* Copy declarations found. */
3352 binding->value = newval;
3354 binding->type = newtype;
3357 /* Process a using-directive. */
3360 do_using_directive (tree namespace)
3362 tree context = NULL_TREE;
3364 if (namespace == error_mark_node)
3367 gcc_assert (TREE_CODE (namespace) == NAMESPACE_DECL);
3369 if (building_stmt_tree ())
3370 add_stmt (build_stmt (USING_STMT, namespace));
3371 namespace = ORIGINAL_NAMESPACE (namespace);
3373 if (!toplevel_bindings_p ())
3375 push_using_directive (namespace);
3376 context = current_scope ();
3381 add_using_namespace (current_namespace, namespace, 0);
3382 if (current_namespace != global_namespace)
3383 context = current_namespace;
3386 /* Emit debugging info. */
3387 if (!processing_template_decl)
3388 (*debug_hooks->imported_module_or_decl) (namespace, context);
3391 /* Deal with a using-directive seen by the parser. Currently we only
3392 handle attributes here, since they cannot appear inside a template. */
3395 parse_using_directive (tree namespace, tree attribs)
3399 do_using_directive (namespace);
3401 for (a = attribs; a; a = TREE_CHAIN (a))
3403 tree name = TREE_PURPOSE (a);
3404 if (is_attribute_p ("strong", name))
3406 if (!toplevel_bindings_p ())
3407 error ("strong using only meaningful at namespace scope");
3408 else if (namespace != error_mark_node)
3410 if (!is_ancestor (current_namespace, namespace))
3411 error ("current namespace %qD does not enclose strongly used namespace %qD",
3412 current_namespace, namespace);
3413 DECL_NAMESPACE_ASSOCIATIONS (namespace)
3414 = tree_cons (current_namespace, 0,
3415 DECL_NAMESPACE_ASSOCIATIONS (namespace));
3419 warning (OPT_Wattributes, "%qD attribute directive ignored", name);
3423 /* Like pushdecl, only it places X in the global scope if appropriate.
3424 Calls cp_finish_decl to register the variable, initializing it with
3425 *INIT, if INIT is non-NULL. */
3428 pushdecl_top_level_1 (tree x, tree *init, bool is_friend)
3430 timevar_push (TV_NAME_LOOKUP);
3431 push_to_top_level ();
3432 x = pushdecl_namespace_level (x, is_friend);
3434 finish_decl (x, *init, NULL_TREE);
3435 pop_from_top_level ();
3436 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
3439 /* Like pushdecl, only it places X in the global scope if appropriate. */
3442 pushdecl_top_level (tree x)
3444 return pushdecl_top_level_1 (x, NULL, false);
3447 /* Like pushdecl_top_level, but adding the IS_FRIEND parameter. */
3450 pushdecl_top_level_maybe_friend (tree x, bool is_friend)
3452 return pushdecl_top_level_1 (x, NULL, is_friend);
3455 /* Like pushdecl, only it places X in the global scope if
3456 appropriate. Calls cp_finish_decl to register the variable,
3457 initializing it with INIT. */
3460 pushdecl_top_level_and_finish (tree x, tree init)
3462 return pushdecl_top_level_1 (x, &init, false);
3465 /* Combines two sets of overloaded functions into an OVERLOAD chain, removing
3466 duplicates. The first list becomes the tail of the result.
3468 The algorithm is O(n^2). We could get this down to O(n log n) by
3469 doing a sort on the addresses of the functions, if that becomes
3473 merge_functions (tree s1, tree s2)
3475 for (; s2; s2 = OVL_NEXT (s2))
3477 tree fn2 = OVL_CURRENT (s2);
3480 for (fns1 = s1; fns1; fns1 = OVL_NEXT (fns1))
3482 tree fn1 = OVL_CURRENT (fns1);
3484 /* If the function from S2 is already in S1, there is no
3485 need to add it again. For `extern "C"' functions, we
3486 might have two FUNCTION_DECLs for the same function, in
3487 different namespaces; again, we only need one of them. */
3489 || (DECL_EXTERN_C_P (fn1) && DECL_EXTERN_C_P (fn2)
3490 && DECL_NAME (fn1) == DECL_NAME (fn2)))
3494 /* If we exhausted all of the functions in S1, FN2 is new. */
3496 s1 = build_overload (fn2, s1);
3501 /* This should return an error not all definitions define functions.
3502 It is not an error if we find two functions with exactly the
3503 same signature, only if these are selected in overload resolution.
3504 old is the current set of bindings, new the freshly-found binding.
3505 XXX Do we want to give *all* candidates in case of ambiguity?
3506 XXX In what way should I treat extern declarations?
3507 XXX I don't want to repeat the entire duplicate_decls here */
3509 /* LLVM LOCAL begin mainline */
3511 ambiguous_decl (struct scope_binding *old, cxx_binding *new, int flags)
3514 gcc_assert (old != NULL);
3516 /* Copy the type. */
3518 if (LOOKUP_NAMESPACES_ONLY (flags)
3519 || (type && hidden_name_p (type) && !(flags & LOOKUP_HIDDEN)))
3522 /* Copy the value. */
3526 if (hidden_name_p (val) && !(flags & LOOKUP_HIDDEN))
3529 switch (TREE_CODE (val))
3532 /* If we expect types or namespaces, and not templates,
3533 or this is not a template class. */
3534 if ((LOOKUP_QUALIFIERS_ONLY (flags)
3535 && !DECL_CLASS_TEMPLATE_P (val)))
3539 if (LOOKUP_NAMESPACES_ONLY (flags)
3540 || (type && (flags & LOOKUP_PREFER_TYPES)))
3543 case NAMESPACE_DECL:
3544 if (LOOKUP_TYPES_ONLY (flags))
3548 /* Ignore built-in functions that are still anticipated. */
3549 if (LOOKUP_QUALIFIERS_ONLY (flags))
3553 if (LOOKUP_QUALIFIERS_ONLY (flags))
3558 /* If val is hidden, shift down any class or enumeration name. */
3565 /* LLVM LOCAL end mainline */
3568 else if (val && val != old->value)
3570 if (is_overloaded_fn (old->value) && is_overloaded_fn (val))
3571 old->value = merge_functions (old->value, val);
3574 old->value = tree_cons (NULL_TREE, old->value,
3575 build_tree_list (NULL_TREE, val));
3576 TREE_TYPE (old->value) = error_mark_node;
3580 /* LLVM LOCAL begin mainline */
3583 else if (type && old->type != type)
3585 old->type = tree_cons (NULL_TREE, old->type,
3586 build_tree_list (NULL_TREE, type));
3587 TREE_TYPE (old->type) = error_mark_node;
3589 /* LLVM LOCAL end mainline */
3592 /* Return the declarations that are members of the namespace NS. */
3595 cp_namespace_decls (tree ns)
3597 return NAMESPACE_LEVEL (ns)->names;
3600 /* Combine prefer_type and namespaces_only into flags. */
3603 lookup_flags (int prefer_type, int namespaces_only)
3605 if (namespaces_only)
3606 return LOOKUP_PREFER_NAMESPACES;
3607 if (prefer_type > 1)
3608 return LOOKUP_PREFER_TYPES;
3609 if (prefer_type > 0)
3610 return LOOKUP_PREFER_BOTH;
3614 /* Given a lookup that returned VAL, use FLAGS to decide if we want to
3615 ignore it or not. Subroutine of lookup_name_real and
3616 lookup_type_scope. */
3619 qualify_lookup (tree val, int flags)
3621 if (val == NULL_TREE)
3623 if ((flags & LOOKUP_PREFER_NAMESPACES) && TREE_CODE (val) == NAMESPACE_DECL)
3625 if ((flags & LOOKUP_PREFER_TYPES)
3626 && (TREE_CODE (val) == TYPE_DECL || TREE_CODE (val) == TEMPLATE_DECL))
3628 if (flags & (LOOKUP_PREFER_NAMESPACES | LOOKUP_PREFER_TYPES))
3633 /* Given a lookup that returned VAL, decide if we want to ignore it or
3634 not based on DECL_ANTICIPATED. */
3637 hidden_name_p (tree val)
3640 && DECL_LANG_SPECIFIC (val)
3641 && DECL_ANTICIPATED (val))
3646 /* Remove any hidden friend functions from a possibly overloaded set
3650 remove_hidden_names (tree fns)
3655 if (TREE_CODE (fns) == FUNCTION_DECL && hidden_name_p (fns))
3657 else if (TREE_CODE (fns) == OVERLOAD)
3661 for (o = fns; o; o = OVL_NEXT (o))
3662 if (hidden_name_p (OVL_CURRENT (o)))
3668 for (o = fns; o; o = OVL_NEXT (o))
3669 if (!hidden_name_p (OVL_CURRENT (o)))
3670 n = build_overload (OVL_CURRENT (o), n);
3678 /* Unscoped lookup of a global: iterate over current namespaces,
3679 considering using-directives. */
3682 unqualified_namespace_lookup (tree name, int flags)
3684 tree initial = current_decl_namespace ();
3685 tree scope = initial;
3687 struct cp_binding_level *level;
3688 tree val = NULL_TREE;
3690 timevar_push (TV_NAME_LOOKUP);
3692 for (; !val; scope = CP_DECL_CONTEXT (scope))
3694 struct scope_binding binding = EMPTY_SCOPE_BINDING;
3696 cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (scope), name);
3699 /* LLVM LOCAL mainline */
3700 ambiguous_decl (&binding, b, flags);
3702 /* Add all _DECLs seen through local using-directives. */
3703 for (level = current_binding_level;
3704 level->kind != sk_namespace;
3705 level = level->level_chain)
3706 if (!lookup_using_namespace (name, &binding, level->using_directives,
3708 /* Give up because of error. */
3709 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
3711 /* Add all _DECLs seen through global using-directives. */
3712 /* XXX local and global using lists should work equally. */
3716 if (!lookup_using_namespace (name, &binding,
3717 DECL_NAMESPACE_USING (siter),
3719 /* Give up because of error. */
3720 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
3721 if (siter == scope) break;
3722 siter = CP_DECL_CONTEXT (siter);
3725 val = binding.value;
3726 if (scope == global_namespace)
3729 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
3732 /* Look up NAME (an IDENTIFIER_NODE) in SCOPE (either a NAMESPACE_DECL
3733 or a class TYPE). If IS_TYPE_P is TRUE, then ignore non-type
3736 Returns a DECL (or OVERLOAD, or BASELINK) representing the
3737 declaration found. If no suitable declaration can be found,
3738 ERROR_MARK_NODE is returned. If COMPLAIN is true and SCOPE is
3739 neither a class-type nor a namespace a diagnostic is issued. */
3742 lookup_qualified_name (tree scope, tree name, bool is_type_p, bool complain)
3747 if (TREE_CODE (scope) == NAMESPACE_DECL)
3749 struct scope_binding binding = EMPTY_SCOPE_BINDING;
3751 flags |= LOOKUP_COMPLAIN;
3753 flags |= LOOKUP_PREFER_TYPES;
3754 if (qualified_lookup_using_namespace (name, scope, &binding, flags))
3757 else if (is_aggr_type (scope, complain))
3758 t = lookup_member (scope, name, 2, is_type_p);
3761 return error_mark_node;
3765 /* Subroutine of unqualified_namespace_lookup:
3766 Add the bindings of NAME in used namespaces to VAL.
3767 We are currently looking for names in namespace SCOPE, so we
3768 look through USINGS for using-directives of namespaces
3769 which have SCOPE as a common ancestor with the current scope.
3770 Returns false on errors. */
3773 lookup_using_namespace (tree name, struct scope_binding *val,
3774 tree usings, tree scope, int flags)
3777 timevar_push (TV_NAME_LOOKUP);
3778 /* Iterate over all used namespaces in current, searching for using
3779 directives of scope. */
3780 for (iter = usings; iter; iter = TREE_CHAIN (iter))
3781 if (TREE_VALUE (iter) == scope)
3783 tree used = ORIGINAL_NAMESPACE (TREE_PURPOSE (iter));
3785 cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (used), name);
3786 /* Resolve ambiguities. */
3788 /* LLVM LOCAL mainline */
3789 ambiguous_decl (val, val1, flags);
3791 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val->value != error_mark_node);
3795 Accepts the NAME to lookup and its qualifying SCOPE.
3796 Returns the name/type pair found into the cxx_binding *RESULT,
3797 or false on error. */
3800 qualified_lookup_using_namespace (tree name, tree scope,
3801 struct scope_binding *result, int flags)
3803 /* Maintain a list of namespaces visited... */
3804 tree seen = NULL_TREE;
3805 /* ... and a list of namespace yet to see. */
3806 tree todo = NULL_TREE;
3807 tree todo_maybe = NULL_TREE;
3809 timevar_push (TV_NAME_LOOKUP);
3810 /* Look through namespace aliases. */
3811 scope = ORIGINAL_NAMESPACE (scope);
3812 while (scope && result->value != error_mark_node)
3814 cxx_binding *binding =
3815 cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (scope), name);
3816 seen = tree_cons (scope, NULL_TREE, seen);
3818 /* LLVM LOCAL mainline */
3819 ambiguous_decl (result, binding, flags);
3821 /* Consider strong using directives always, and non-strong ones
3822 if we haven't found a binding yet. ??? Shouldn't we consider
3823 non-strong ones if the initial RESULT is non-NULL, but the
3824 binding in the given namespace is? */
3825 for (usings = DECL_NAMESPACE_USING (scope); usings;
3826 usings = TREE_CHAIN (usings))
3827 /* If this was a real directive, and we have not seen it. */
3828 if (!TREE_INDIRECT_USING (usings))
3830 /* Try to avoid queuing the same namespace more than once,
3831 the exception being when a namespace was already
3832 enqueued for todo_maybe and then a strong using is
3833 found for it. We could try to remove it from
3834 todo_maybe, but it's probably not worth the effort. */
3835 if (is_associated_namespace (scope, TREE_PURPOSE (usings))
3836 && !purpose_member (TREE_PURPOSE (usings), seen)
3837 && !purpose_member (TREE_PURPOSE (usings), todo))
3838 todo = tree_cons (TREE_PURPOSE (usings), NULL_TREE, todo);
3839 else if ((!result->value && !result->type)
3840 && !purpose_member (TREE_PURPOSE (usings), seen)
3841 && !purpose_member (TREE_PURPOSE (usings), todo)
3842 && !purpose_member (TREE_PURPOSE (usings), todo_maybe))
3843 todo_maybe = tree_cons (TREE_PURPOSE (usings), NULL_TREE,
3848 scope = TREE_PURPOSE (todo);
3849 todo = TREE_CHAIN (todo);
3852 && (!result->value && !result->type))
3854 scope = TREE_PURPOSE (todo_maybe);
3855 todo = TREE_CHAIN (todo_maybe);
3856 todo_maybe = NULL_TREE;
3859 scope = NULL_TREE; /* If there never was a todo list. */
3861 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, result->value != error_mark_node);
3864 /* Return the innermost non-namespace binding for NAME from a scope
3865 containing BINDING, or, if BINDING is NULL, the current scope. If
3866 CLASS_P is false, then class bindings are ignored. */
3869 outer_binding (tree name,
3870 cxx_binding *binding,
3875 cxx_scope *outer_scope;
3879 scope = binding->scope->level_chain;
3880 outer = binding->previous;
3884 scope = current_binding_level;
3885 outer = IDENTIFIER_BINDING (name);
3887 outer_scope = outer ? outer->scope : NULL;
3889 /* Because we create class bindings lazily, we might be missing a
3890 class binding for NAME. If there are any class binding levels
3891 between the LAST_BINDING_LEVEL and the scope in which OUTER was
3892 declared, we must lookup NAME in those class scopes. */
3894 while (scope && scope != outer_scope && scope->kind != sk_namespace)
3896 if (scope->kind == sk_class)
3898 cxx_binding *class_binding;
3900 class_binding = get_class_binding (name, scope);
3903 /* Thread this new class-scope binding onto the
3904 IDENTIFIER_BINDING list so that future lookups
3906 class_binding->previous = outer;
3908 binding->previous = class_binding;
3910 IDENTIFIER_BINDING (name) = class_binding;
3911 return class_binding;
3914 scope = scope->level_chain;
3920 /* Return the innermost block-scope or class-scope value binding for
3921 NAME, or NULL_TREE if there is no such binding. */
3924 innermost_non_namespace_value (tree name)
3926 cxx_binding *binding;
3927 binding = outer_binding (name, /*binding=*/NULL, /*class_p=*/true);
3928 return binding ? binding->value : NULL_TREE;
3931 /* Look up NAME in the current binding level and its superiors in the
3932 namespace of variables, functions and typedefs. Return a ..._DECL
3933 node of some kind representing its definition if there is only one
3934 such declaration, or return a TREE_LIST with all the overloaded
3935 definitions if there are many, or return 0 if it is undefined.
3936 Hidden name, either friend declaration or built-in function, are
3939 If PREFER_TYPE is > 0, we prefer TYPE_DECLs or namespaces.
3940 If PREFER_TYPE is > 1, we reject non-type decls (e.g. namespaces).
3941 Otherwise we prefer non-TYPE_DECLs.
3943 If NONCLASS is nonzero, bindings in class scopes are ignored. If
3944 BLOCK_P is false, bindings in block scopes are ignored. */
3947 lookup_name_real (tree name, int prefer_type, int nonclass, bool block_p,
3948 int namespaces_only, int flags)
3951 tree val = NULL_TREE;
3953 timevar_push (TV_NAME_LOOKUP);
3954 /* Conversion operators are handled specially because ordinary
3955 unqualified name lookup will not find template conversion
3957 if (IDENTIFIER_TYPENAME_P (name))
3959 struct cp_binding_level *level;
3961 for (level = current_binding_level;
3962 level && level->kind != sk_namespace;
3963 level = level->level_chain)
3968 /* A conversion operator can only be declared in a class
3970 if (level->kind != sk_class)
3973 /* Lookup the conversion operator in the class. */
3974 class_type = level->this_entity;
3975 operators = lookup_fnfields (class_type, name, /*protect=*/0);
3977 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, operators);
3980 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
3983 flags |= lookup_flags (prefer_type, namespaces_only);
3985 /* First, look in non-namespace scopes. */
3987 if (current_class_type == NULL_TREE)
3990 if (block_p || !nonclass)
3991 for (iter = outer_binding (name, NULL, !nonclass);
3993 iter = outer_binding (name, iter, !nonclass))
3997 /* Skip entities we don't want. */
3998 if (LOCAL_BINDING_P (iter) ? !block_p : nonclass)
4001 /* If this is the kind of thing we're looking for, we're done. */
4002 if (qualify_lookup (iter->value, flags))
4003 binding = iter->value;
4004 else if ((flags & LOOKUP_PREFER_TYPES)
4005 && qualify_lookup (iter->type, flags))
4006 binding = iter->type;
4008 binding = NULL_TREE;
4012 if (hidden_name_p (binding))
4014 /* A non namespace-scope binding can only be hidden if
4015 we are in a local class, due to friend declarations.
4016 In particular, consider:
4021 void g() { B* b; } // error: B is hidden
4026 The standard says that "B" is a local class in "f"
4027 (but not nested within "A") -- but that name lookup
4028 for "B" does not find this declaration until it is
4029 declared directly with "f".
4035 If a friend declaration appears in a local class and
4036 the name specified is an unqualified name, a prior
4037 declaration is looked up without considering scopes
4038 that are outside the innermost enclosing non-class
4039 scope. For a friend class declaration, if there is no
4040 prior declaration, the class that is specified
4041 belongs to the innermost enclosing non-class scope,
4042 but if it is subsequently referenced, its name is not
4043 found by name lookup until a matching declaration is
4044 provided in the innermost enclosing nonclass scope.
4046 gcc_assert (current_class_type &&
4047 LOCAL_CLASS_P (current_class_type));
4049 /* This binding comes from a friend declaration in the local
4050 class. The standard (11.4.8) states that the lookup can
4051 only succeed if there is a non-hidden declaration in the
4052 current scope, which is not the case here. */
4053 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
4060 /* Now lookup in namespace scopes. */
4062 val = unqualified_namespace_lookup (name, flags);
4064 /* If we have a single function from a using decl, pull it out. */
4065 if (val && TREE_CODE (val) == OVERLOAD && !really_overloaded_fn (val))
4066 val = OVL_FUNCTION (val);
4068 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
4072 lookup_name_nonclass (tree name)
4074 return lookup_name_real (name, 0, 1, /*block_p=*/true, 0, LOOKUP_COMPLAIN);
4078 lookup_function_nonclass (tree name, tree args, bool block_p)
4081 lookup_arg_dependent (name,
4082 lookup_name_real (name, 0, 1, block_p, 0,
4088 lookup_name (tree name)
4090 return lookup_name_real (name, 0, 0, /*block_p=*/true, 0, LOOKUP_COMPLAIN);
4094 lookup_name_prefer_type (tree name, int prefer_type)
4096 return lookup_name_real (name, prefer_type, 0, /*block_p=*/true,
4097 0, LOOKUP_COMPLAIN);
4100 /* Look up NAME for type used in elaborated name specifier in
4101 the scopes given by SCOPE. SCOPE can be either TS_CURRENT or
4102 TS_WITHIN_ENCLOSING_NON_CLASS. Although not implied by the
4103 name, more scopes are checked if cleanup or template parameter
4104 scope is encountered.
4106 Unlike lookup_name_real, we make sure that NAME is actually
4107 declared in the desired scope, not from inheritance, nor using
4108 directive. For using declaration, there is DR138 still waiting
4109 to be resolved. Hidden name coming from an earlier friend
4110 declaration is also returned.
4112 A TYPE_DECL best matching the NAME is returned. Catching error
4113 and issuing diagnostics are caller's responsibility. */
4116 lookup_type_scope (tree name, tag_scope scope)
4118 cxx_binding *iter = NULL;
4119 tree val = NULL_TREE;
4121 timevar_push (TV_NAME_LOOKUP);
4123 /* Look in non-namespace scope first. */
4124 if (current_binding_level->kind != sk_namespace)
4125 iter = outer_binding (name, NULL, /*class_p=*/ true);
4126 for (; iter; iter = outer_binding (name, iter, /*class_p=*/ true))
4128 /* Check if this is the kind of thing we're looking for.
4129 If SCOPE is TS_CURRENT, also make sure it doesn't come from
4130 base class. For ITER->VALUE, we can simply use
4131 INHERITED_VALUE_BINDING_P. For ITER->TYPE, we have to use
4134 We check ITER->TYPE before ITER->VALUE in order to handle
4135 typedef struct C {} C;
4138 if (qualify_lookup (iter->type, LOOKUP_PREFER_TYPES)
4139 && (scope != ts_current
4140 || LOCAL_BINDING_P (iter)
4141 || DECL_CONTEXT (iter->type) == iter->scope->this_entity))
4143 else if ((scope != ts_current
4144 || !INHERITED_VALUE_BINDING_P (iter))
4145 && qualify_lookup (iter->value, LOOKUP_PREFER_TYPES))
4152 /* Look in namespace scope. */
4155 iter = cxx_scope_find_binding_for_name
4156 (NAMESPACE_LEVEL (current_decl_namespace ()), name);
4160 /* If this is the kind of thing we're looking for, we're done. */
4161 if (qualify_lookup (iter->type, LOOKUP_PREFER_TYPES))
4163 else if (qualify_lookup (iter->value, LOOKUP_PREFER_TYPES))
4169 /* Type found, check if it is in the allowed scopes, ignoring cleanup
4170 and template parameter scopes. */
4173 struct cp_binding_level *b = current_binding_level;
4176 if (iter->scope == b)
4177 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
4179 if (b->kind == sk_cleanup || b->kind == sk_template_parms)
4181 else if (b->kind == sk_class
4182 && scope == ts_within_enclosing_non_class)
4189 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
4192 /* Similar to `lookup_name' but look only in the innermost non-class
4196 lookup_name_innermost_nonclass_level (tree name)
4198 struct cp_binding_level *b;
4201 timevar_push (TV_NAME_LOOKUP);
4202 b = innermost_nonclass_level ();
4204 if (b->kind == sk_namespace)
4206 t = IDENTIFIER_NAMESPACE_VALUE (name);
4208 /* extern "C" function() */
4209 if (t != NULL_TREE && TREE_CODE (t) == TREE_LIST)
4212 else if (IDENTIFIER_BINDING (name)
4213 && LOCAL_BINDING_P (IDENTIFIER_BINDING (name)))
4215 cxx_binding *binding;
4216 binding = IDENTIFIER_BINDING (name);
4219 if (binding->scope == b
4220 && !(TREE_CODE (binding->value) == VAR_DECL
4221 && DECL_DEAD_FOR_LOCAL (binding->value)))
4222 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, binding->value);
4224 if (b->kind == sk_cleanup)
4231 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
4234 /* Like lookup_name_innermost_nonclass_level, but for types. */
4237 lookup_type_current_level (tree name)
4241 timevar_push (TV_NAME_LOOKUP);
4242 gcc_assert (current_binding_level->kind != sk_namespace);
4244 if (REAL_IDENTIFIER_TYPE_VALUE (name) != NULL_TREE
4245 && REAL_IDENTIFIER_TYPE_VALUE (name) != global_type_node)
4247 struct cp_binding_level *b = current_binding_level;
4250 if (purpose_member (name, b->type_shadowed))
4251 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP,
4252 REAL_IDENTIFIER_TYPE_VALUE (name));
4253 if (b->kind == sk_cleanup)
4260 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
4263 /* [basic.lookup.koenig] */
4264 /* A nonzero return value in the functions below indicates an error. */
4275 static bool arg_assoc (struct arg_lookup*, tree);
4276 static bool arg_assoc_args (struct arg_lookup*, tree);
4277 static bool arg_assoc_type (struct arg_lookup*, tree);
4278 static bool add_function (struct arg_lookup *, tree);
4279 static bool arg_assoc_namespace (struct arg_lookup *, tree);
4280 static bool arg_assoc_class (struct arg_lookup *, tree);
4281 static bool arg_assoc_template_arg (struct arg_lookup*, tree);
4283 /* Add a function to the lookup structure.
4284 Returns true on error. */
4287 add_function (struct arg_lookup *k, tree fn)
4289 /* We used to check here to see if the function was already in the list,
4290 but that's O(n^2), which is just too expensive for function lookup.
4291 Now we deal with the occasional duplicate in joust. In doing this, we
4292 assume that the number of duplicates will be small compared to the
4293 total number of functions being compared, which should usually be the
4296 /* We must find only functions, or exactly one non-function. */
4299 else if (fn == k->functions)
4301 else if (is_overloaded_fn (k->functions) && is_overloaded_fn (fn))
4302 k->functions = build_overload (fn, k->functions);
4305 tree f1 = OVL_CURRENT (k->functions);
4307 if (is_overloaded_fn (f1))
4309 fn = f1; f1 = f2; f2 = fn;
4311 error ("%q+D is not a function,", f1);
4312 error (" conflict with %q+D", f2);
4313 error (" in call to %qD", k->name);
4320 /* Returns true iff CURRENT has declared itself to be an associated
4321 namespace of SCOPE via a strong using-directive (or transitive chain
4322 thereof). Both are namespaces. */
4325 is_associated_namespace (tree current, tree scope)
4327 tree seen = NULL_TREE;
4328 tree todo = NULL_TREE;
4332 if (scope == current)
4334 seen = tree_cons (scope, NULL_TREE, seen);
4335 for (t = DECL_NAMESPACE_ASSOCIATIONS (scope); t; t = TREE_CHAIN (t))
4336 if (!purpose_member (TREE_PURPOSE (t), seen))
4337 todo = tree_cons (TREE_PURPOSE (t), NULL_TREE, todo);
4340 scope = TREE_PURPOSE (todo);
4341 todo = TREE_CHAIN (todo);
4348 /* Return whether FN is a friend of an associated class of ARG. */
4351 friend_of_associated_class_p (tree arg, tree fn)
4357 else if (type_unknown_p (arg))
4360 type = TREE_TYPE (arg);
4362 /* If TYPE is a class, the class itself and all base classes are
4363 associated classes. */
4364 if (CLASS_TYPE_P (type))
4366 if (is_friend (type, fn))
4369 if (TYPE_BINFO (type))
4371 tree binfo, base_binfo;
4374 for (binfo = TYPE_BINFO (type), i = 0;
4375 BINFO_BASE_ITERATE (binfo, i, base_binfo);
4377 if (is_friend (BINFO_TYPE (base_binfo), fn))
4382 /* If TYPE is a class member, the class of which it is a member is
4383 an associated class. */
4384 if ((CLASS_TYPE_P (type)
4385 || TREE_CODE (type) == UNION_TYPE
4386 || TREE_CODE (type) == ENUMERAL_TYPE)
4387 && TYPE_CONTEXT (type)
4388 && CLASS_TYPE_P (TYPE_CONTEXT (type))
4389 && is_friend (TYPE_CONTEXT (type), fn))
4395 /* Add functions of a namespace to the lookup structure.
4396 Returns true on error. */
4399 arg_assoc_namespace (struct arg_lookup *k, tree scope)
4403 if (purpose_member (scope, k->namespaces))
4405 k->namespaces = tree_cons (scope, NULL_TREE, k->namespaces);
4407 /* Check out our super-users. */
4408 for (value = DECL_NAMESPACE_ASSOCIATIONS (scope); value;
4409 value = TREE_CHAIN (value))
4410 if (arg_assoc_namespace (k, TREE_PURPOSE (value)))
4413 value = namespace_binding (k->name, scope);
4417 for (; value; value = OVL_NEXT (value))
4419 /* We don't want to find arbitrary hidden functions via argument
4420 dependent lookup. We only want to find friends of associated
4422 if (hidden_name_p (OVL_CURRENT (value)))
4426 for (args = k->args; args; args = TREE_CHAIN (args))
4427 if (friend_of_associated_class_p (TREE_VALUE (args),
4428 OVL_CURRENT (value)))
4434 if (add_function (k, OVL_CURRENT (value)))
4441 /* Adds everything associated with a template argument to the lookup
4442 structure. Returns true on error. */
4445 arg_assoc_template_arg (struct arg_lookup *k, tree arg)
4447 /* [basic.lookup.koenig]
4449 If T is a template-id, its associated namespaces and classes are
4450 ... the namespaces and classes associated with the types of the
4451 template arguments provided for template type parameters
4452 (excluding template template parameters); the namespaces in which
4453 any template template arguments are defined; and the classes in
4454 which any member templates used as template template arguments
4455 are defined. [Note: non-type template arguments do not
4456 contribute to the set of associated namespaces. ] */
4458 /* Consider first template template arguments. */
4459 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
4460 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
4462 else if (TREE_CODE (arg) == TEMPLATE_DECL)
4464 tree ctx = CP_DECL_CONTEXT (arg);
4466 /* It's not a member template. */
4467 if (TREE_CODE (ctx) == NAMESPACE_DECL)
4468 return arg_assoc_namespace (k, ctx);
4469 /* Otherwise, it must be member template. */
4471 return arg_assoc_class (k, ctx);
4473 /* It's not a template template argument, but it is a type template
4475 else if (TYPE_P (arg))
4476 return arg_assoc_type (k, arg);
4477 /* It's a non-type template argument. */
4482 /* Adds everything associated with class to the lookup structure.
4483 Returns true on error. */
4486 arg_assoc_class (struct arg_lookup *k, tree type)
4488 tree list, friends, context;
4491 /* Backend build structures, such as __builtin_va_list, aren't
4492 affected by all this. */
4493 if (!CLASS_TYPE_P (type))
4496 if (purpose_member (type, k->classes))
4498 k->classes = tree_cons (type, NULL_TREE, k->classes);
4500 context = decl_namespace_context (type);
4501 if (arg_assoc_namespace (k, context))
4504 if (TYPE_BINFO (type))
4506 /* Process baseclasses. */
4507 tree binfo, base_binfo;
4509 for (binfo = TYPE_BINFO (type), i = 0;
4510 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
4511 if (arg_assoc_class (k, BINFO_TYPE (base_binfo)))
4515 /* Process friends. */
4516 for (list = DECL_FRIENDLIST (TYPE_MAIN_DECL (type)); list;
4517 list = TREE_CHAIN (list))
4518 if (k->name == FRIEND_NAME (list))
4519 for (friends = FRIEND_DECLS (list); friends;
4520 friends = TREE_CHAIN (friends))
4522 tree fn = TREE_VALUE (friends);
4524 /* Only interested in global functions with potentially hidden
4525 (i.e. unqualified) declarations. */
4526 if (CP_DECL_CONTEXT (fn) != context)
4528 /* Template specializations are never found by name lookup.
4529 (Templates themselves can be found, but not template
4530 specializations.) */
4531 if (TREE_CODE (fn) == FUNCTION_DECL && DECL_USE_TEMPLATE (fn))
4533 if (add_function (k, fn))
4537 /* Process template arguments. */
4538 if (CLASSTYPE_TEMPLATE_INFO (type)
4539 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
4541 list = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
4542 for (i = 0; i < TREE_VEC_LENGTH (list); ++i)
4543 arg_assoc_template_arg (k, TREE_VEC_ELT (list, i));
4549 /* Adds everything associated with a given type.
4550 Returns 1 on error. */
4553 arg_assoc_type (struct arg_lookup *k, tree type)
4555 /* As we do not get the type of non-type dependent expressions
4556 right, we can end up with such things without a type. */
4560 if (TYPE_PTRMEM_P (type))
4562 /* Pointer to member: associate class type and value type. */
4563 if (arg_assoc_type (k, TYPE_PTRMEM_CLASS_TYPE (type)))
4565 return arg_assoc_type (k, TYPE_PTRMEM_POINTED_TO_TYPE (type));
4567 else switch (TREE_CODE (type))
4579 if (TYPE_PTRMEMFUNC_P (type))
4580 return arg_assoc_type (k, TYPE_PTRMEMFUNC_FN_TYPE (type));
4581 return arg_assoc_class (k, type);
4583 /* APPLE LOCAL blocks 6040305 */
4584 case BLOCK_POINTER_TYPE:
4585 case REFERENCE_TYPE:
4587 return arg_assoc_type (k, TREE_TYPE (type));
4590 return arg_assoc_namespace (k, decl_namespace_context (type));
4592 /* The basetype is referenced in the first arg type, so just
4595 /* Associate the parameter types. */
4596 if (arg_assoc_args (k, TYPE_ARG_TYPES (type)))
4598 /* Associate the return type. */
4599 return arg_assoc_type (k, TREE_TYPE (type));
4600 case TEMPLATE_TYPE_PARM:
4601 case BOUND_TEMPLATE_TEMPLATE_PARM:
4606 gcc_assert (type == unknown_type_node);
4614 /* Adds everything associated with arguments. Returns true on error. */
4617 arg_assoc_args (struct arg_lookup *k, tree args)
4619 for (; args; args = TREE_CHAIN (args))
4620 if (arg_assoc (k, TREE_VALUE (args)))
4625 /* Adds everything associated with a given tree_node. Returns 1 on error. */
4628 arg_assoc (struct arg_lookup *k, tree n)
4630 if (n == error_mark_node)
4634 return arg_assoc_type (k, n);
4636 if (! type_unknown_p (n))
4637 return arg_assoc_type (k, TREE_TYPE (n));
4639 if (TREE_CODE (n) == ADDR_EXPR)
4640 n = TREE_OPERAND (n, 0);
4641 if (TREE_CODE (n) == COMPONENT_REF)
4642 n = TREE_OPERAND (n, 1);
4643 if (TREE_CODE (n) == OFFSET_REF)
4644 n = TREE_OPERAND (n, 1);
4645 while (TREE_CODE (n) == TREE_LIST)
4647 if (TREE_CODE (n) == BASELINK)
4648 n = BASELINK_FUNCTIONS (n);
4650 if (TREE_CODE (n) == FUNCTION_DECL)
4651 return arg_assoc_type (k, TREE_TYPE (n));
4652 if (TREE_CODE (n) == TEMPLATE_ID_EXPR)
4654 /* [basic.lookup.koenig]
4656 If T is a template-id, its associated namespaces and classes
4657 are the namespace in which the template is defined; for
4658 member templates, the member template's class... */
4659 tree template = TREE_OPERAND (n, 0);
4660 tree args = TREE_OPERAND (n, 1);
4664 if (TREE_CODE (template) == COMPONENT_REF)
4665 template = TREE_OPERAND (template, 1);
4667 /* First, the template. There may actually be more than one if
4668 this is an overloaded function template. But, in that case,
4669 we only need the first; all the functions will be in the same
4671 template = OVL_CURRENT (template);
4673 ctx = CP_DECL_CONTEXT (template);
4675 if (TREE_CODE (ctx) == NAMESPACE_DECL)
4677 if (arg_assoc_namespace (k, ctx) == 1)
4680 /* It must be a member template. */
4681 else if (arg_assoc_class (k, ctx) == 1)
4684 /* Now the arguments. */
4686 for (ix = TREE_VEC_LENGTH (args); ix--;)
4687 if (arg_assoc_template_arg (k, TREE_VEC_ELT (args, ix)) == 1)
4690 else if (TREE_CODE (n) == OVERLOAD)
4692 for (; n; n = OVL_CHAIN (n))
4693 if (arg_assoc_type (k, TREE_TYPE (OVL_FUNCTION (n))))
4700 /* Performs Koenig lookup depending on arguments, where fns
4701 are the functions found in normal lookup. */
4704 lookup_arg_dependent (tree name, tree fns, tree args)
4706 struct arg_lookup k;
4708 timevar_push (TV_NAME_LOOKUP);
4710 /* Remove any hidden friend functions from the list of functions
4711 found so far. They will be added back by arg_assoc_class as
4713 fns = remove_hidden_names (fns);
4718 k.classes = NULL_TREE;
4720 /* We previously performed an optimization here by setting
4721 NAMESPACES to the current namespace when it was safe. However, DR
4722 164 says that namespaces that were already searched in the first
4723 stage of template processing are searched again (potentially
4724 picking up later definitions) in the second stage. */
4725 k.namespaces = NULL_TREE;
4727 arg_assoc_args (&k, args);
4732 && TREE_CODE (fns) != VAR_DECL
4733 && !is_overloaded_fn (fns))
4735 error ("argument dependent lookup finds %q+D", fns);
4736 error (" in call to %qD", name);
4737 fns = error_mark_node;
4740 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, fns);
4743 /* Add namespace to using_directives. Return NULL_TREE if nothing was
4744 changed (i.e. there was already a directive), or the fresh
4745 TREE_LIST otherwise. */
4748 push_using_directive (tree used)
4750 tree ud = current_binding_level->using_directives;
4751 tree iter, ancestor;
4753 timevar_push (TV_NAME_LOOKUP);
4754 /* Check if we already have this. */
4755 if (purpose_member (used, ud) != NULL_TREE)
4756 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
4758 ancestor = namespace_ancestor (current_decl_namespace (), used);
4759 ud = current_binding_level->using_directives;
4760 ud = tree_cons (used, ancestor, ud);
4761 current_binding_level->using_directives = ud;
4763 /* Recursively add all namespaces used. */
4764 for (iter = DECL_NAMESPACE_USING (used); iter; iter = TREE_CHAIN (iter))
4765 push_using_directive (TREE_PURPOSE (iter));
4767 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, ud);
4770 /* The type TYPE is being declared. If it is a class template, or a
4771 specialization of a class template, do any processing required and
4772 perform error-checking. If IS_FRIEND is nonzero, this TYPE is
4773 being declared a friend. B is the binding level at which this TYPE
4776 Returns the TYPE_DECL for TYPE, which may have been altered by this
4780 maybe_process_template_type_declaration (tree type, int is_friend,
4783 tree decl = TYPE_NAME (type);
4785 if (processing_template_parmlist)
4786 /* You can't declare a new template type in a template parameter
4787 list. But, you can declare a non-template type:
4789 template <class A*> struct S;
4791 is a forward-declaration of `A'. */
4793 else if (b->kind == sk_namespace
4794 && current_binding_level->kind != sk_namespace)
4795 /* If this new type is being injected into a containing scope,
4796 then it's not a template type. */
4800 gcc_assert (IS_AGGR_TYPE (type) || TREE_CODE (type) == ENUMERAL_TYPE);
4802 if (processing_template_decl)
4804 /* This may change after the call to
4805 push_template_decl_real, but we want the original value. */
4806 tree name = DECL_NAME (decl);
4808 decl = push_template_decl_real (decl, is_friend);
4809 /* If the current binding level is the binding level for the
4810 template parameters (see the comment in
4811 begin_template_parm_list) and the enclosing level is a class
4812 scope, and we're not looking at a friend, push the
4813 declaration of the member class into the class scope. In the
4814 friend case, push_template_decl will already have put the
4815 friend into global scope, if appropriate. */
4816 if (TREE_CODE (type) != ENUMERAL_TYPE
4817 && !is_friend && b->kind == sk_template_parms
4818 && b->level_chain->kind == sk_class)
4820 finish_member_declaration (CLASSTYPE_TI_TEMPLATE (type));
4822 if (!COMPLETE_TYPE_P (current_class_type))
4824 maybe_add_class_template_decl_list (current_class_type,
4825 type, /*friend_p=*/0);
4826 /* Put this UTD in the table of UTDs for the class. */
4827 if (CLASSTYPE_NESTED_UTDS (current_class_type) == NULL)
4828 CLASSTYPE_NESTED_UTDS (current_class_type) =
4829 binding_table_new (SCOPE_DEFAULT_HT_SIZE);
4831 binding_table_insert
4832 (CLASSTYPE_NESTED_UTDS (current_class_type), name, type);
4841 /* Push a tag name NAME for struct/class/union/enum type TYPE. In case
4842 that the NAME is a class template, the tag is processed but not pushed.
4844 The pushed scope depend on the SCOPE parameter:
4845 - When SCOPE is TS_CURRENT, put it into the inner-most non-sk_cleanup
4847 - When SCOPE is TS_GLOBAL, put it in the inner-most non-class and
4848 non-template-parameter scope. This case is needed for forward
4850 - When SCOPE is TS_WITHIN_ENCLOSING_NON_CLASS, this is similar to
4851 TS_GLOBAL case except that names within template-parameter scopes
4852 are not pushed at all.
4854 Returns TYPE upon success and ERROR_MARK_NODE otherwise. */
4857 pushtag (tree name, tree type, tag_scope scope)
4859 struct cp_binding_level *b;
4862 timevar_push (TV_NAME_LOOKUP);
4863 b = current_binding_level;
4864 while (/* Cleanup scopes are not scopes from the point of view of
4866 b->kind == sk_cleanup
4867 /* Neither are the scopes used to hold template parameters
4868 for an explicit specialization. For an ordinary template
4869 declaration, these scopes are not scopes from the point of
4870 view of the language. */
4871 || (b->kind == sk_template_parms
4872 && (b->explicit_spec_p || scope == ts_global))
4873 || (b->kind == sk_class
4874 && (scope != ts_current
4875 /* We may be defining a new type in the initializer
4876 of a static member variable. We allow this when
4877 not pedantic, and it is particularly useful for
4878 type punning via an anonymous union. */
4879 || COMPLETE_TYPE_P (b->this_entity))))
4882 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
4884 /* Do C++ gratuitous typedefing. */
4885 if (IDENTIFIER_TYPE_VALUE (name) != type)
4889 tree context = TYPE_CONTEXT (type);
4893 tree cs = current_scope ();
4895 if (scope == ts_current)
4897 else if (cs != NULL_TREE && TYPE_P (cs))
4898 /* When declaring a friend class of a local class, we want
4899 to inject the newly named class into the scope
4900 containing the local class, not the namespace
4902 context = decl_function_context (get_type_decl (cs));
4905 context = current_namespace;
4907 if (b->kind == sk_class
4908 || (b->kind == sk_template_parms
4909 && b->level_chain->kind == sk_class))
4912 if (current_lang_name == lang_name_java)
4913 TYPE_FOR_JAVA (type) = 1;
4915 tdef = create_implicit_typedef (name, type);
4916 DECL_CONTEXT (tdef) = FROB_CONTEXT (context);
4917 if (scope == ts_within_enclosing_non_class)
4919 /* This is a friend. Make this TYPE_DECL node hidden from
4920 ordinary name lookup. Its corresponding TEMPLATE_DECL
4921 will be marked in push_template_decl_real. */
4922 retrofit_lang_decl (tdef);
4923 DECL_ANTICIPATED (tdef) = 1;
4924 DECL_FRIEND_P (tdef) = 1;
4927 decl = maybe_process_template_type_declaration
4928 (type, scope == ts_within_enclosing_non_class, b);
4929 if (decl == error_mark_node)
4930 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
4933 set_identifier_type_value_with_scope (name, tdef, b);
4935 if (b->kind == sk_class)
4937 if (!PROCESSING_REAL_TEMPLATE_DECL_P ())
4938 /* Put this TYPE_DECL on the TYPE_FIELDS list for the
4939 class. But if it's a member template class, we want
4940 the TEMPLATE_DECL, not the TYPE_DECL, so this is done
4942 finish_member_declaration (decl);
4944 pushdecl_class_level (decl);
4946 else if (b->kind != sk_template_parms)
4948 decl = pushdecl_with_scope (decl, b, /*is_friend=*/false);
4949 if (decl == error_mark_node)
4950 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
4953 TYPE_CONTEXT (type) = DECL_CONTEXT (decl);
4955 /* If this is a local class, keep track of it. We need this
4956 information for name-mangling, and so that it is possible to
4957 find all function definitions in a translation unit in a
4958 convenient way. (It's otherwise tricky to find a member
4959 function definition it's only pointed to from within a local
4961 if (TYPE_CONTEXT (type)
4962 && TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL)
4963 VEC_safe_push (tree, gc, local_classes, type);
4965 if (b->kind == sk_class
4966 && !COMPLETE_TYPE_P (current_class_type))
4968 maybe_add_class_template_decl_list (current_class_type,
4969 type, /*friend_p=*/0);
4971 if (CLASSTYPE_NESTED_UTDS (current_class_type) == NULL)
4972 CLASSTYPE_NESTED_UTDS (current_class_type)
4973 = binding_table_new (SCOPE_DEFAULT_HT_SIZE);
4975 binding_table_insert
4976 (CLASSTYPE_NESTED_UTDS (current_class_type), name, type);
4979 decl = TYPE_NAME (type);
4980 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
4981 TYPE_STUB_DECL (type) = decl;
4983 /* Set type visibility now if this is a forward declaration. */
4984 TREE_PUBLIC (decl) = 1;
4985 determine_visibility (decl);
4987 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, type);
4990 /* Subroutines for reverting temporarily to top-level for instantiation
4991 of templates and such. We actually need to clear out the class- and
4992 local-value slots of all identifiers, so that only the global values
4993 are at all visible. Simply setting current_binding_level to the global
4994 scope isn't enough, because more binding levels may be pushed. */
4995 struct saved_scope *scope_chain;
4997 /* If ID has not already been marked, add an appropriate binding to
5001 store_binding (tree id, VEC(cxx_saved_binding,gc) **old_bindings)
5003 cxx_saved_binding *saved;
5005 if (!id || !IDENTIFIER_BINDING (id))
5008 if (IDENTIFIER_MARKED (id))
5011 IDENTIFIER_MARKED (id) = 1;
5013 saved = VEC_safe_push (cxx_saved_binding, gc, *old_bindings, NULL);
5014 saved->identifier = id;
5015 saved->binding = IDENTIFIER_BINDING (id);
5016 saved->real_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
5017 IDENTIFIER_BINDING (id) = NULL;
5021 store_bindings (tree names, VEC(cxx_saved_binding,gc) **old_bindings)
5025 timevar_push (TV_NAME_LOOKUP);
5026 for (t = names; t; t = TREE_CHAIN (t))
5030 if (TREE_CODE (t) == TREE_LIST)
5031 id = TREE_PURPOSE (t);
5035 store_binding (id, old_bindings);
5037 timevar_pop (TV_NAME_LOOKUP);
5040 /* Like store_bindings, but NAMES is a vector of cp_class_binding
5041 objects, rather than a TREE_LIST. */
5044 store_class_bindings (VEC(cp_class_binding,gc) *names,
5045 VEC(cxx_saved_binding,gc) **old_bindings)
5048 cp_class_binding *cb;
5050 timevar_push (TV_NAME_LOOKUP);
5051 for (i = 0; VEC_iterate(cp_class_binding, names, i, cb); ++i)
5052 store_binding (cb->identifier, old_bindings);
5053 timevar_pop (TV_NAME_LOOKUP);
5057 push_to_top_level (void)
5059 struct saved_scope *s;
5060 struct cp_binding_level *b;
5061 cxx_saved_binding *sb;
5065 timevar_push (TV_NAME_LOOKUP);
5066 s = GGC_CNEW (struct saved_scope);
5068 b = scope_chain ? current_binding_level : 0;
5070 /* If we're in the middle of some function, save our state. */
5074 push_function_context_to (NULL_TREE);
5079 if (scope_chain && previous_class_level)
5080 store_class_bindings (previous_class_level->class_shadowed,
5083 /* Have to include the global scope, because class-scope decls
5084 aren't listed anywhere useful. */
5085 for (; b; b = b->level_chain)
5089 /* Template IDs are inserted into the global level. If they were
5090 inserted into namespace level, finish_file wouldn't find them
5091 when doing pending instantiations. Therefore, don't stop at
5092 namespace level, but continue until :: . */
5093 if (global_scope_p (b))
5096 store_bindings (b->names, &s->old_bindings);
5097 /* We also need to check class_shadowed to save class-level type
5098 bindings, since pushclass doesn't fill in b->names. */
5099 if (b->kind == sk_class)
5100 store_class_bindings (b->class_shadowed, &s->old_bindings);
5102 /* Unwind type-value slots back to top level. */
5103 for (t = b->type_shadowed; t; t = TREE_CHAIN (t))
5104 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (t), TREE_VALUE (t));
5107 for (i = 0; VEC_iterate (cxx_saved_binding, s->old_bindings, i, sb); ++i)
5108 IDENTIFIER_MARKED (sb->identifier) = 0;
5110 s->prev = scope_chain;
5112 s->need_pop_function_context = need_pop;
5113 s->function_decl = current_function_decl;
5114 s->skip_evaluation = skip_evaluation;
5117 current_function_decl = NULL_TREE;
5118 current_lang_base = VEC_alloc (tree, gc, 10);
5119 current_lang_name = lang_name_cplusplus;
5120 current_namespace = global_namespace;
5121 push_class_stack ();
5122 skip_evaluation = 0;
5123 timevar_pop (TV_NAME_LOOKUP);
5127 pop_from_top_level (void)
5129 struct saved_scope *s = scope_chain;
5130 cxx_saved_binding *saved;
5133 timevar_push (TV_NAME_LOOKUP);
5134 /* Clear out class-level bindings cache. */
5135 if (previous_class_level)
5136 invalidate_class_lookup_cache ();
5139 current_lang_base = 0;
5141 scope_chain = s->prev;
5142 for (i = 0; VEC_iterate (cxx_saved_binding, s->old_bindings, i, saved); ++i)
5144 tree id = saved->identifier;
5146 IDENTIFIER_BINDING (id) = saved->binding;
5147 SET_IDENTIFIER_TYPE_VALUE (id, saved->real_type_value);
5150 /* If we were in the middle of compiling a function, restore our
5152 if (s->need_pop_function_context)
5153 pop_function_context_from (NULL_TREE);
5154 current_function_decl = s->function_decl;
5155 skip_evaluation = s->skip_evaluation;
5156 timevar_pop (TV_NAME_LOOKUP);
5159 /* Pop off extraneous binding levels left over due to syntax errors.
5161 We don't pop past namespaces, as they might be valid. */
5164 pop_everything (void)
5166 if (ENABLE_SCOPE_CHECKING)
5167 verbatim ("XXX entering pop_everything ()\n");
5168 while (!toplevel_bindings_p ())
5170 if (current_binding_level->kind == sk_class)
5171 pop_nested_class ();
5175 if (ENABLE_SCOPE_CHECKING)
5176 verbatim ("XXX leaving pop_everything ()\n");
5179 /* Emit debugging information for using declarations and directives.
5180 If input tree is overloaded fn then emit debug info for all
5184 cp_emit_debug_info_for_using (tree t, tree context)
5186 /* Don't try to emit any debug information if we have errors. */
5187 if (sorrycount || errorcount)
5190 /* Ignore this FUNCTION_DECL if it refers to a builtin declaration
5191 of a builtin function. */
5192 if (TREE_CODE (t) == FUNCTION_DECL
5193 && DECL_EXTERNAL (t)
5194 && DECL_BUILT_IN (t))
5197 /* Do not supply context to imported_module_or_decl, if
5198 it is a global namespace. */
5199 if (context == global_namespace)
5200 context = NULL_TREE;
5203 t = BASELINK_FUNCTIONS (t);
5205 /* FIXME: Handle TEMPLATE_DECLs. */
5206 for (t = OVL_CURRENT (t); t; t = OVL_NEXT (t))
5207 if (TREE_CODE (t) != TEMPLATE_DECL)
5208 (*debug_hooks->imported_module_or_decl) (t, context);
5211 #include "gt-cp-name-lookup.h"