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