* name-lookup.c (anonymous_namespace_name): Make static.
[official-gcc.git] / gcc / cp / name-lookup.c
blob7b1ff39ca9146de2d8981f6fc6b5e8f7a68331ab
1 /* Definitions for C++ name lookup routines.
2 Copyright (C) 2003, 2004 Free Software Foundation, Inc.
3 Contributed by Gabriel Dos Reis <gdr@integrable-solutions.net>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
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, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, 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"
35 static cxx_scope *innermost_nonclass_level (void);
36 static tree select_decl (cxx_binding *, int);
37 static cxx_binding *binding_for_name (cxx_scope *, tree);
38 static tree lookup_name_current_level (tree);
39 static tree push_overloaded_decl (tree, int);
40 static bool lookup_using_namespace (tree, cxx_binding *, tree,
41 tree, int);
42 static bool qualified_lookup_using_namespace (tree, tree, cxx_binding *, int);
43 static tree lookup_type_current_level (tree);
44 static tree push_using_directive (tree);
45 static void cp_emit_debug_info_for_using (tree, tree);
47 /* The :: namespace. */
49 tree global_namespace;
51 /* The name of the anonymous namespace, throughout this translation
52 unit. */
53 static GTY(()) tree anonymous_namespace_name;
56 /* Compute the chain index of a binding_entry given the HASH value of its
57 name and the total COUNT of chains. COUNT is assumed to be a power
58 of 2. */
60 #define ENTRY_INDEX(HASH, COUNT) (((HASH) >> 3) & ((COUNT) - 1))
62 /* A free list of "binding_entry"s awaiting for re-use. */
64 static GTY((deletable)) binding_entry free_binding_entry = NULL;
66 /* Create a binding_entry object for (NAME, TYPE). */
68 static inline binding_entry
69 binding_entry_make (tree name, tree type)
71 binding_entry entry;
73 if (free_binding_entry)
75 entry = free_binding_entry;
76 free_binding_entry = entry->chain;
78 else
79 entry = ggc_alloc (sizeof (struct binding_entry_s));
81 entry->name = name;
82 entry->type = type;
83 entry->chain = NULL;
85 return entry;
88 /* Put ENTRY back on the free list. */
90 static inline void
91 binding_entry_free (binding_entry entry)
93 entry->name = NULL;
94 entry->type = NULL;
95 entry->chain = free_binding_entry;
96 free_binding_entry = entry;
99 /* The datatype used to implement the mapping from names to types at
100 a given scope. */
101 struct binding_table_s GTY(())
103 /* Array of chains of "binding_entry"s */
104 binding_entry * GTY((length ("%h.chain_count"))) chain;
106 /* The number of chains in this table. This is the length of the
107 the member "chain" considered as an array. */
108 size_t chain_count;
110 /* Number of "binding_entry"s in this table. */
111 size_t entry_count;
114 /* Construct TABLE with an initial CHAIN_COUNT. */
116 static inline void
117 binding_table_construct (binding_table table, size_t chain_count)
119 table->chain_count = chain_count;
120 table->entry_count = 0;
121 table->chain = ggc_alloc_cleared
122 (table->chain_count * sizeof (binding_entry));
125 /* Make TABLE's entries ready for reuse. */
127 static void
128 binding_table_free (binding_table table)
130 size_t i;
131 size_t count;
133 if (table == NULL)
134 return;
136 for (i = 0, count = table->chain_count; i < count; ++i)
138 binding_entry temp = table->chain[i];
139 while (temp != NULL)
141 binding_entry entry = temp;
142 temp = entry->chain;
143 binding_entry_free (entry);
145 table->chain[i] = NULL;
147 table->entry_count = 0;
150 /* Allocate a table with CHAIN_COUNT, assumed to be a power of two. */
152 static inline binding_table
153 binding_table_new (size_t chain_count)
155 binding_table table = ggc_alloc (sizeof (struct binding_table_s));
156 table->chain = NULL;
157 binding_table_construct (table, chain_count);
158 return table;
161 /* Expand TABLE to twice its current chain_count. */
163 static void
164 binding_table_expand (binding_table table)
166 const size_t old_chain_count = table->chain_count;
167 const size_t old_entry_count = table->entry_count;
168 const size_t new_chain_count = 2 * old_chain_count;
169 binding_entry *old_chains = table->chain;
170 size_t i;
172 binding_table_construct (table, new_chain_count);
173 for (i = 0; i < old_chain_count; ++i)
175 binding_entry entry = old_chains[i];
176 for (; entry != NULL; entry = old_chains[i])
178 const unsigned int hash = IDENTIFIER_HASH_VALUE (entry->name);
179 const size_t j = ENTRY_INDEX (hash, new_chain_count);
181 old_chains[i] = entry->chain;
182 entry->chain = table->chain[j];
183 table->chain[j] = entry;
186 table->entry_count = old_entry_count;
189 /* Insert a binding for NAME to TYPE into TABLE. */
191 static void
192 binding_table_insert (binding_table table, tree name, tree type)
194 const unsigned int hash = IDENTIFIER_HASH_VALUE (name);
195 const size_t i = ENTRY_INDEX (hash, table->chain_count);
196 binding_entry entry = binding_entry_make (name, type);
198 entry->chain = table->chain[i];
199 table->chain[i] = entry;
200 ++table->entry_count;
202 if (3 * table->chain_count < 5 * table->entry_count)
203 binding_table_expand (table);
206 /* Return the binding_entry, if any, that maps NAME. */
208 binding_entry
209 binding_table_find (binding_table table, tree name)
211 const unsigned int hash = IDENTIFIER_HASH_VALUE (name);
212 binding_entry entry = table->chain[ENTRY_INDEX (hash, table->chain_count)];
214 while (entry != NULL && entry->name != name)
215 entry = entry->chain;
217 return entry;
220 /* Return the binding_entry, if any, that maps NAME to an anonymous type. */
222 static tree
223 binding_table_find_anon_type (binding_table table, tree name)
225 const unsigned int hash = IDENTIFIER_HASH_VALUE (name);
226 binding_entry entry = table->chain[ENTRY_INDEX (hash, table->chain_count)];
228 while (entry != NULL && TYPE_IDENTIFIER (entry->type) != name)
229 entry = entry->chain;
231 return entry ? entry->type : NULL;
234 /* Return the binding_entry, if any, that has TYPE as target. If NAME
235 is non-null, then set the domain and rehash that entry. */
237 static binding_entry
238 binding_table_reverse_maybe_remap (binding_table table, tree type, tree name)
240 const size_t chain_count = table->chain_count;
241 binding_entry entry = NULL;
242 binding_entry *p = NULL;
243 size_t i;
245 for (i = 0; i < chain_count && entry == NULL; ++i)
247 p = &table->chain[i];
248 while (*p != NULL && entry == NULL)
249 if ((*p)->type == type)
250 entry = *p;
251 else
252 p = &(*p)->chain;
255 if (entry != NULL && name != NULL && entry->name != name)
257 /* Remove the bucket from the previous chain. */
258 *p = (*p)->chain;
260 /* Remap the name type to type. */
261 i = ENTRY_INDEX (IDENTIFIER_HASH_VALUE (name), chain_count);
262 entry->chain = table->chain[i];
263 entry->name = name;
264 table->chain[i] = entry;
267 return entry;
270 /* Remove from TABLE all entries that map to anonymous enums or
271 class-types. */
273 void
274 binding_table_remove_anonymous_types (binding_table table)
276 const size_t chain_count = table->chain_count;
277 size_t i;
279 for (i = 0; i < chain_count; ++i)
281 binding_entry *p = &table->chain[i];
283 while (*p != NULL)
284 if (ANON_AGGRNAME_P ((*p)->name))
286 binding_entry e = *p;
287 *p = (*p)->chain;
288 --table->entry_count;
289 binding_entry_free (e);
291 else
292 p = &(*p)->chain;
296 /* Apply PROC -- with DATA -- to all entries in TABLE. */
298 void
299 binding_table_foreach (binding_table table, bt_foreach_proc proc, void *data)
301 const size_t chain_count = table->chain_count;
302 size_t i;
304 for (i = 0; i < chain_count; ++i)
306 binding_entry entry = table->chain[i];
307 for (; entry != NULL; entry = entry->chain)
308 proc (entry, data);
312 #ifndef ENABLE_SCOPE_CHECKING
313 # define ENABLE_SCOPE_CHECKING 0
314 #else
315 # define ENABLE_SCOPE_CHECKING 1
316 #endif
318 /* A free list of "cxx_binding"s, connected by their PREVIOUS. */
320 static GTY((deletable)) cxx_binding *free_bindings;
322 /* Zero out a cxx_binding pointed to by B. */
323 #define cxx_binding_clear(B) memset ((B), 0, sizeof (cxx_binding))
325 /* (GC)-allocate a binding object with VALUE and TYPE member initialized. */
327 static cxx_binding *
328 cxx_binding_make (tree value, tree type)
330 cxx_binding *binding;
331 if (free_bindings)
333 binding = free_bindings;
334 free_bindings = binding->previous;
336 else
337 binding = ggc_alloc (sizeof (cxx_binding));
339 binding->value = value;
340 binding->type = type;
341 binding->previous = NULL;
343 return binding;
346 /* Put BINDING back on the free list. */
348 static inline void
349 cxx_binding_free (cxx_binding *binding)
351 binding->scope = NULL;
352 binding->previous = free_bindings;
353 free_bindings = binding;
356 /* Make DECL the innermost binding for ID. The LEVEL is the binding
357 level at which this declaration is being bound. */
359 static void
360 push_binding (tree id, tree decl, cxx_scope* level)
362 cxx_binding *binding = cxx_binding_make (decl, NULL);
364 /* Now, fill in the binding information. */
365 binding->previous = IDENTIFIER_BINDING (id);
366 binding->scope = level;
367 INHERITED_VALUE_BINDING_P (binding) = 0;
368 LOCAL_BINDING_P (binding) = (level != class_binding_level);
370 /* And put it on the front of the list of bindings for ID. */
371 IDENTIFIER_BINDING (id) = binding;
374 /* Remove the binding for DECL which should be the innermost binding
375 for ID. */
377 void
378 pop_binding (tree id, tree decl)
380 cxx_binding *binding;
382 if (id == NULL_TREE)
383 /* It's easiest to write the loops that call this function without
384 checking whether or not the entities involved have names. We
385 get here for such an entity. */
386 return;
388 /* Get the innermost binding for ID. */
389 binding = IDENTIFIER_BINDING (id);
391 /* The name should be bound. */
392 my_friendly_assert (binding != NULL, 0);
394 /* The DECL will be either the ordinary binding or the type
395 binding for this identifier. Remove that binding. */
396 if (binding->value == decl)
397 binding->value = NULL_TREE;
398 else if (binding->type == decl)
399 binding->type = NULL_TREE;
400 else
401 abort ();
403 if (!binding->value && !binding->type)
405 /* We're completely done with the innermost binding for this
406 identifier. Unhook it from the list of bindings. */
407 IDENTIFIER_BINDING (id) = binding->previous;
409 /* Add it to the free list. */
410 cxx_binding_free (binding);
414 /* BINDING records an existing declaration for a namein the current scope.
415 But, DECL is another declaration for that same identifier in the
416 same scope. This is the `struct stat' hack whereby a non-typedef
417 class name or enum-name can be bound at the same level as some other
418 kind of entity.
419 3.3.7/1
421 A class name (9.1) or enumeration name (7.2) can be hidden by the
422 name of an object, function, or enumerator declared in the same scope.
423 If a class or enumeration name and an object, function, or enumerator
424 are declared in the same scope (in any order) with the same name, the
425 class or enumeration name is hidden wherever the object, function, or
426 enumerator name is visible.
428 It's the responsibility of the caller to check that
429 inserting this name is valid here. Returns nonzero if the new binding
430 was successful. */
432 static bool
433 supplement_binding (cxx_binding *binding, tree decl)
435 tree bval = binding->value;
436 bool ok = true;
438 timevar_push (TV_NAME_LOOKUP);
439 if (TREE_CODE (decl) == TYPE_DECL && DECL_ARTIFICIAL (decl))
440 /* The new name is the type name. */
441 binding->type = decl;
442 else if (/* BVAL is null when push_class_level_binding moves an
443 inherited type-binding out of the way to make room for a
444 new value binding. */
445 !bval
446 /* BVAL is error_mark_node when DECL's name has been used
447 in a non-class scope prior declaration. In that case,
448 we should have already issued a diagnostic; for graceful
449 error recovery purpose, pretend this was the intended
450 declaration for that name. */
451 || bval == error_mark_node
452 /* If BVAL is a built-in that has not yet been declared,
453 pretend it is not there at all. */
454 || (TREE_CODE (bval) == FUNCTION_DECL
455 && DECL_ANTICIPATED (bval)))
456 binding->value = decl;
457 else if (TREE_CODE (bval) == TYPE_DECL && DECL_ARTIFICIAL (bval))
459 /* The old binding was a type name. It was placed in
460 VALUE field because it was thought, at the point it was
461 declared, to be the only entity with such a name. Move the
462 type name into the type slot; it is now hidden by the new
463 binding. */
464 binding->type = bval;
465 binding->value = decl;
466 binding->value_is_inherited = false;
468 else if (TREE_CODE (bval) == TYPE_DECL
469 && TREE_CODE (decl) == TYPE_DECL
470 && DECL_NAME (decl) == DECL_NAME (bval)
471 && (same_type_p (TREE_TYPE (decl), TREE_TYPE (bval))
472 /* If either type involves template parameters, we must
473 wait until instantiation. */
474 || uses_template_parms (TREE_TYPE (decl))
475 || uses_template_parms (TREE_TYPE (bval))))
476 /* We have two typedef-names, both naming the same type to have
477 the same name. This is OK because of:
479 [dcl.typedef]
481 In a given scope, a typedef specifier can be used to redefine
482 the name of any type declared in that scope to refer to the
483 type to which it already refers. */
484 ok = false;
485 /* There can be two block-scope declarations of the same variable,
486 so long as they are `extern' declarations. However, there cannot
487 be two declarations of the same static data member:
489 [class.mem]
491 A member shall not be declared twice in the
492 member-specification. */
493 else if (TREE_CODE (decl) == VAR_DECL && TREE_CODE (bval) == VAR_DECL
494 && DECL_EXTERNAL (decl) && DECL_EXTERNAL (bval)
495 && !DECL_CLASS_SCOPE_P (decl))
497 duplicate_decls (decl, binding->value);
498 ok = false;
500 else
502 error ("declaration of `%#D'", decl);
503 cp_error_at ("conflicts with previous declaration `%#D'",
504 binding->value);
505 ok = false;
508 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, ok);
511 /* Add DECL to the list of things declared in B. */
513 static void
514 add_decl_to_level (tree decl, cxx_scope *b)
516 if (TREE_CODE (decl) == NAMESPACE_DECL
517 && !DECL_NAMESPACE_ALIAS (decl))
519 TREE_CHAIN (decl) = b->namespaces;
520 b->namespaces = decl;
522 else if (TREE_CODE (decl) == VAR_DECL && DECL_VIRTUAL_P (decl))
524 TREE_CHAIN (decl) = b->vtables;
525 b->vtables = decl;
527 else
529 /* We build up the list in reverse order, and reverse it later if
530 necessary. */
531 TREE_CHAIN (decl) = b->names;
532 b->names = decl;
533 b->names_size++;
535 /* If appropriate, add decl to separate list of statics. We
536 include extern variables because they might turn out to be
537 static later. It's OK for this list to contain a few false
538 positives. */
539 if (b->kind == sk_namespace)
540 if ((TREE_CODE (decl) == VAR_DECL
541 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
542 || (TREE_CODE (decl) == FUNCTION_DECL
543 && (!TREE_PUBLIC (decl) || DECL_DECLARED_INLINE_P (decl))))
544 VARRAY_PUSH_TREE (b->static_decls, decl);
548 /* Record a decl-node X as belonging to the current lexical scope.
549 Check for errors (such as an incompatible declaration for the same
550 name already seen in the same scope).
552 Returns either X or an old decl for the same name.
553 If an old decl is returned, it may have been smashed
554 to agree with what X says. */
556 tree
557 pushdecl (tree x)
559 tree t;
560 tree name;
561 int need_new_binding;
563 timevar_push (TV_NAME_LOOKUP);
565 need_new_binding = 1;
567 if (DECL_TEMPLATE_PARM_P (x))
568 /* Template parameters have no context; they are not X::T even
569 when declared within a class or namespace. */
571 else
573 if (current_function_decl && x != current_function_decl
574 /* A local declaration for a function doesn't constitute
575 nesting. */
576 && TREE_CODE (x) != FUNCTION_DECL
577 /* A local declaration for an `extern' variable is in the
578 scope of the current namespace, not the current
579 function. */
580 && !(TREE_CODE (x) == VAR_DECL && DECL_EXTERNAL (x))
581 && !DECL_CONTEXT (x))
582 DECL_CONTEXT (x) = current_function_decl;
584 /* If this is the declaration for a namespace-scope function,
585 but the declaration itself is in a local scope, mark the
586 declaration. */
587 if (TREE_CODE (x) == FUNCTION_DECL
588 && DECL_NAMESPACE_SCOPE_P (x)
589 && current_function_decl
590 && x != current_function_decl)
591 DECL_LOCAL_FUNCTION_P (x) = 1;
594 name = DECL_NAME (x);
595 if (name)
597 int different_binding_level = 0;
599 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
600 name = TREE_OPERAND (name, 0);
602 /* In case this decl was explicitly namespace-qualified, look it
603 up in its namespace context. */
604 if (DECL_NAMESPACE_SCOPE_P (x) && namespace_bindings_p ())
605 t = namespace_binding (name, DECL_CONTEXT (x));
606 else
607 t = lookup_name_current_level (name);
609 /* [basic.link] If there is a visible declaration of an entity
610 with linkage having the same name and type, ignoring entities
611 declared outside the innermost enclosing namespace scope, the
612 block scope declaration declares that same entity and
613 receives the linkage of the previous declaration. */
614 if (! t && current_function_decl && x != current_function_decl
615 && (TREE_CODE (x) == FUNCTION_DECL || TREE_CODE (x) == VAR_DECL)
616 && DECL_EXTERNAL (x))
618 /* Look in block scope. */
619 t = IDENTIFIER_VALUE (name);
620 /* Or in the innermost namespace. */
621 if (! t)
622 t = namespace_binding (name, DECL_CONTEXT (x));
623 /* Does it have linkage? Note that if this isn't a DECL, it's an
624 OVERLOAD, which is OK. */
625 if (t && DECL_P (t) && ! (TREE_STATIC (t) || DECL_EXTERNAL (t)))
626 t = NULL_TREE;
627 if (t)
628 different_binding_level = 1;
631 /* If we are declaring a function, and the result of name-lookup
632 was an OVERLOAD, look for an overloaded instance that is
633 actually the same as the function we are declaring. (If
634 there is one, we have to merge our declaration with the
635 previous declaration.) */
636 if (t && TREE_CODE (t) == OVERLOAD)
638 tree match;
640 if (TREE_CODE (x) == FUNCTION_DECL)
641 for (match = t; match; match = OVL_NEXT (match))
643 if (decls_match (OVL_CURRENT (match), x))
644 break;
646 else
647 /* Just choose one. */
648 match = t;
650 if (match)
651 t = OVL_CURRENT (match);
652 else
653 t = NULL_TREE;
656 if (t && t != error_mark_node)
658 if (different_binding_level)
660 if (decls_match (x, t))
661 /* The standard only says that the local extern
662 inherits linkage from the previous decl; in
663 particular, default args are not shared. It would
664 be nice to propagate inlining info, though. FIXME. */
665 TREE_PUBLIC (x) = TREE_PUBLIC (t);
667 else if (TREE_CODE (t) == PARM_DECL)
669 if (DECL_CONTEXT (t) == NULL_TREE)
670 /* This is probably caused by too many errors, but calling
671 abort will say that if errors have occurred. */
672 abort ();
674 /* Check for duplicate params. */
675 if (duplicate_decls (x, t))
676 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
678 else if ((DECL_EXTERN_C_FUNCTION_P (x)
679 || DECL_FUNCTION_TEMPLATE_P (x))
680 && is_overloaded_fn (t))
681 /* Don't do anything just yet. */;
682 else if (t == wchar_decl_node)
684 if (pedantic && ! DECL_IN_SYSTEM_HEADER (x))
685 pedwarn ("redeclaration of `wchar_t' as `%T'",
686 TREE_TYPE (x));
688 /* Throw away the redeclaration. */
689 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
691 else
693 tree olddecl = duplicate_decls (x, t);
695 /* If the redeclaration failed, we can stop at this
696 point. */
697 if (olddecl == error_mark_node)
698 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
700 if (olddecl)
702 if (TREE_CODE (t) == TYPE_DECL)
703 SET_IDENTIFIER_TYPE_VALUE (name, TREE_TYPE (t));
704 else if (TREE_CODE (t) == FUNCTION_DECL)
705 check_default_args (t);
707 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
709 else if (DECL_MAIN_P (x) && TREE_CODE (t) == FUNCTION_DECL)
711 /* A redeclaration of main, but not a duplicate of the
712 previous one.
714 [basic.start.main]
716 This function shall not be overloaded. */
717 cp_error_at ("invalid redeclaration of `%D'", t);
718 error ("as `%D'", x);
719 /* We don't try to push this declaration since that
720 causes a crash. */
721 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
726 check_template_shadow (x);
728 /* If this is a function conjured up by the backend, massage it
729 so it looks friendly. */
730 if (DECL_NON_THUNK_FUNCTION_P (x) && ! DECL_LANG_SPECIFIC (x))
732 retrofit_lang_decl (x);
733 SET_DECL_LANGUAGE (x, lang_c);
736 if (DECL_NON_THUNK_FUNCTION_P (x) && ! DECL_FUNCTION_MEMBER_P (x))
738 t = push_overloaded_decl (x, PUSH_LOCAL);
739 if (t != x)
740 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
741 if (!namespace_bindings_p ())
742 /* We do not need to create a binding for this name;
743 push_overloaded_decl will have already done so if
744 necessary. */
745 need_new_binding = 0;
747 else if (DECL_FUNCTION_TEMPLATE_P (x) && DECL_NAMESPACE_SCOPE_P (x))
749 t = push_overloaded_decl (x, PUSH_GLOBAL);
750 if (t == x)
751 add_decl_to_level (x, NAMESPACE_LEVEL (CP_DECL_CONTEXT (t)));
752 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
755 /* If declaring a type as a typedef, copy the type (unless we're
756 at line 0), and install this TYPE_DECL as the new type's typedef
757 name. See the extensive comment in ../c-decl.c (pushdecl). */
758 if (TREE_CODE (x) == TYPE_DECL)
760 tree type = TREE_TYPE (x);
761 if (DECL_SOURCE_LINE (x) == 0)
763 if (TYPE_NAME (type) == 0)
764 TYPE_NAME (type) = x;
766 else if (type != error_mark_node && TYPE_NAME (type) != x
767 /* We don't want to copy the type when all we're
768 doing is making a TYPE_DECL for the purposes of
769 inlining. */
770 && (!TYPE_NAME (type)
771 || TYPE_NAME (type) != DECL_ABSTRACT_ORIGIN (x)))
773 DECL_ORIGINAL_TYPE (x) = type;
774 type = build_type_copy (type);
775 TYPE_STUB_DECL (type) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
776 TYPE_NAME (type) = x;
777 TREE_TYPE (x) = type;
780 if (type != error_mark_node
781 && TYPE_NAME (type)
782 && TYPE_IDENTIFIER (type))
783 set_identifier_type_value (DECL_NAME (x), x);
786 /* Multiple external decls of the same identifier ought to match.
788 We get warnings about inline functions where they are defined.
789 We get warnings about other functions from push_overloaded_decl.
791 Avoid duplicate warnings where they are used. */
792 if (TREE_PUBLIC (x) && TREE_CODE (x) != FUNCTION_DECL)
794 tree decl;
796 decl = IDENTIFIER_NAMESPACE_VALUE (name);
797 if (decl && TREE_CODE (decl) == OVERLOAD)
798 decl = OVL_FUNCTION (decl);
800 if (decl && decl != error_mark_node
801 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl))
802 /* If different sort of thing, we already gave an error. */
803 && TREE_CODE (decl) == TREE_CODE (x)
804 && !same_type_p (TREE_TYPE (x), TREE_TYPE (decl)))
806 pedwarn ("type mismatch with previous external decl of `%#D'", x);
807 cp_pedwarn_at ("previous external decl of `%#D'", decl);
811 /* This name is new in its binding level.
812 Install the new declaration and return it. */
813 if (namespace_bindings_p ())
815 /* Install a global value. */
817 /* If the first global decl has external linkage,
818 warn if we later see static one. */
819 if (IDENTIFIER_GLOBAL_VALUE (name) == NULL_TREE && TREE_PUBLIC (x))
820 TREE_PUBLIC (name) = 1;
822 /* Bind the name for the entity. */
823 if (!(TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x)
824 && t != NULL_TREE)
825 && (TREE_CODE (x) == TYPE_DECL
826 || TREE_CODE (x) == VAR_DECL
827 || TREE_CODE (x) == ALIAS_DECL
828 || TREE_CODE (x) == NAMESPACE_DECL
829 || TREE_CODE (x) == CONST_DECL
830 || TREE_CODE (x) == TEMPLATE_DECL))
831 SET_IDENTIFIER_NAMESPACE_VALUE (name, x);
833 /* If new decl is `static' and an `extern' was seen previously,
834 warn about it. */
835 if (x != NULL_TREE && t != NULL_TREE && decls_match (x, t))
836 warn_extern_redeclared_static (x, t);
838 else
840 /* Here to install a non-global value. */
841 tree oldlocal = IDENTIFIER_VALUE (name);
842 tree oldglobal = IDENTIFIER_NAMESPACE_VALUE (name);
844 if (need_new_binding)
846 push_local_binding (name, x, 0);
847 /* Because push_local_binding will hook X on to the
848 current_binding_level's name list, we don't want to
849 do that again below. */
850 need_new_binding = 0;
853 /* If this is a TYPE_DECL, push it into the type value slot. */
854 if (TREE_CODE (x) == TYPE_DECL)
855 set_identifier_type_value (name, x);
857 /* Clear out any TYPE_DECL shadowed by a namespace so that
858 we won't think this is a type. The C struct hack doesn't
859 go through namespaces. */
860 if (TREE_CODE (x) == NAMESPACE_DECL)
861 set_identifier_type_value (name, NULL_TREE);
863 if (oldlocal)
865 tree d = oldlocal;
867 while (oldlocal
868 && TREE_CODE (oldlocal) == VAR_DECL
869 && DECL_DEAD_FOR_LOCAL (oldlocal))
870 oldlocal = DECL_SHADOWED_FOR_VAR (oldlocal);
872 if (oldlocal == NULL_TREE)
873 oldlocal = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (d));
876 /* If this is an extern function declaration, see if we
877 have a global definition or declaration for the function. */
878 if (oldlocal == NULL_TREE
879 && DECL_EXTERNAL (x)
880 && oldglobal != NULL_TREE
881 && TREE_CODE (x) == FUNCTION_DECL
882 && TREE_CODE (oldglobal) == FUNCTION_DECL)
884 /* We have one. Their types must agree. */
885 if (decls_match (x, oldglobal))
886 /* OK */;
887 else
889 warning ("extern declaration of `%#D' doesn't match", x);
890 cp_warning_at ("global declaration `%#D'", oldglobal);
893 /* If we have a local external declaration,
894 and no file-scope declaration has yet been seen,
895 then if we later have a file-scope decl it must not be static. */
896 if (oldlocal == NULL_TREE
897 && oldglobal == NULL_TREE
898 && DECL_EXTERNAL (x)
899 && TREE_PUBLIC (x))
900 TREE_PUBLIC (name) = 1;
902 /* Warn if shadowing an argument at the top level of the body. */
903 if (oldlocal != NULL_TREE && !DECL_EXTERNAL (x)
904 /* Inline decls shadow nothing. */
905 && !DECL_FROM_INLINE (x)
906 && TREE_CODE (oldlocal) == PARM_DECL
907 /* Don't check the `this' parameter. */
908 && !DECL_ARTIFICIAL (oldlocal))
910 bool err = false;
912 /* Don't complain if it's from an enclosing function. */
913 if (DECL_CONTEXT (oldlocal) == current_function_decl
914 && TREE_CODE (x) != PARM_DECL)
916 /* Go to where the parms should be and see if we find
917 them there. */
918 struct cp_binding_level *b = current_binding_level->level_chain;
920 /* Skip the ctor/dtor cleanup level. */
921 b = b->level_chain;
923 /* ARM $8.3 */
924 if (b->kind == sk_function_parms)
926 error ("declaration of '%#D' shadows a parameter", x);
927 err = true;
931 if (warn_shadow && !err)
933 warning ("declaration of '%#D' shadows a parameter", x);
934 warning ("%Jshadowed declaration is here", oldlocal);
938 /* Maybe warn if shadowing something else. */
939 else if (warn_shadow && !DECL_EXTERNAL (x)
940 /* No shadow warnings for internally generated vars. */
941 && ! DECL_ARTIFICIAL (x)
942 /* No shadow warnings for vars made for inlining. */
943 && ! DECL_FROM_INLINE (x))
945 if (IDENTIFIER_CLASS_VALUE (name) != NULL_TREE
946 && current_class_ptr
947 && !TREE_STATIC (name))
949 /* Location of previous decl is not useful in this case. */
950 warning ("declaration of '%D' shadows a member of 'this'",
953 else if (oldlocal != NULL_TREE
954 && TREE_CODE (oldlocal) == VAR_DECL)
956 warning ("declaration of '%D' shadows a previous local", x);
957 warning ("%Jshadowed declaration is here", oldlocal);
959 else if (oldglobal != NULL_TREE
960 && TREE_CODE (oldglobal) == VAR_DECL)
961 /* XXX shadow warnings in outer-more namespaces */
963 warning ("declaration of '%D' shadows a global declaration",
965 warning ("%Jshadowed declaration is here", oldglobal);
970 if (TREE_CODE (x) == FUNCTION_DECL)
971 check_default_args (x);
973 if (TREE_CODE (x) == VAR_DECL)
974 maybe_register_incomplete_var (x);
977 if (need_new_binding)
978 add_decl_to_level (x,
979 DECL_NAMESPACE_SCOPE_P (x)
980 ? NAMESPACE_LEVEL (CP_DECL_CONTEXT (x))
981 : current_binding_level);
983 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
986 /* Enter DECL into the symbol table, if that's appropriate. Returns
987 DECL, or a modified version thereof. */
989 tree
990 maybe_push_decl (tree decl)
992 tree type = TREE_TYPE (decl);
994 /* Add this decl to the current binding level, but not if it comes
995 from another scope, e.g. a static member variable. TEM may equal
996 DECL or it may be a previous decl of the same name. */
997 if (decl == error_mark_node
998 || (TREE_CODE (decl) != PARM_DECL
999 && DECL_CONTEXT (decl) != NULL_TREE
1000 /* Definitions of namespace members outside their namespace are
1001 possible. */
1002 && TREE_CODE (DECL_CONTEXT (decl)) != NAMESPACE_DECL)
1003 || (TREE_CODE (decl) == TEMPLATE_DECL && !namespace_bindings_p ())
1004 || TREE_CODE (type) == UNKNOWN_TYPE
1005 /* The declaration of a template specialization does not affect
1006 the functions available for overload resolution, so we do not
1007 call pushdecl. */
1008 || (TREE_CODE (decl) == FUNCTION_DECL
1009 && DECL_TEMPLATE_SPECIALIZATION (decl)))
1010 return decl;
1011 else
1012 return pushdecl (decl);
1015 /* Bind DECL to ID in the current_binding_level, assumed to be a local
1016 binding level. If PUSH_USING is set in FLAGS, we know that DECL
1017 doesn't really belong to this binding level, that it got here
1018 through a using-declaration. */
1020 void
1021 push_local_binding (tree id, tree decl, int flags)
1023 struct cp_binding_level *b;
1025 /* Skip over any local classes. This makes sense if we call
1026 push_local_binding with a friend decl of a local class. */
1027 b = innermost_nonclass_level ();
1029 if (lookup_name_current_level (id))
1031 /* Supplement the existing binding. */
1032 if (!supplement_binding (IDENTIFIER_BINDING (id), decl))
1033 /* It didn't work. Something else must be bound at this
1034 level. Do not add DECL to the list of things to pop
1035 later. */
1036 return;
1038 else
1039 /* Create a new binding. */
1040 push_binding (id, decl, b);
1042 if (TREE_CODE (decl) == OVERLOAD || (flags & PUSH_USING))
1043 /* We must put the OVERLOAD into a TREE_LIST since the
1044 TREE_CHAIN of an OVERLOAD is already used. Similarly for
1045 decls that got here through a using-declaration. */
1046 decl = build_tree_list (NULL_TREE, decl);
1048 /* And put DECL on the list of things declared by the current
1049 binding level. */
1050 add_decl_to_level (decl, b);
1053 /* The old ARM scoping rules injected variables declared in the
1054 initialization statement of a for-statement into the surrounding
1055 scope. We support this usage, in order to be backward-compatible.
1056 DECL is a just-declared VAR_DECL; if necessary inject its
1057 declaration into the surrounding scope. */
1059 void
1060 maybe_inject_for_scope_var (tree decl)
1062 timevar_push (TV_NAME_LOOKUP);
1063 if (!DECL_NAME (decl))
1065 timevar_pop (TV_NAME_LOOKUP);
1066 return;
1069 /* Declarations of __FUNCTION__ and its ilk appear magically when
1070 the variable is first used. If that happens to be inside a
1071 for-loop, we don't want to do anything special. */
1072 if (DECL_PRETTY_FUNCTION_P (decl))
1074 timevar_pop (TV_NAME_LOOKUP);
1075 return;
1078 if (current_binding_level->kind == sk_for)
1080 struct cp_binding_level *outer
1081 = current_binding_level->level_chain;
1083 /* Check to see if the same name is already bound at the outer
1084 level, either because it was directly declared, or because a
1085 dead for-decl got preserved. In either case, the code would
1086 not have been valid under the ARM scope rules, so clear
1087 is_for_scope for the current_binding_level.
1089 Otherwise, we need to preserve the temp slot for decl to last
1090 into the outer binding level. */
1092 cxx_binding *outer_binding
1093 = IDENTIFIER_BINDING (DECL_NAME (decl))->previous;
1095 if (outer_binding && outer_binding->scope == outer
1096 && (TREE_CODE (outer_binding->value) == VAR_DECL)
1097 && DECL_DEAD_FOR_LOCAL (outer_binding->value))
1099 outer_binding->value = DECL_SHADOWED_FOR_VAR (outer_binding->value);
1100 current_binding_level->kind = sk_block;
1103 timevar_pop (TV_NAME_LOOKUP);
1106 /* Check to see whether or not DECL is a variable that would have been
1107 in scope under the ARM, but is not in scope under the ANSI/ISO
1108 standard. If so, issue an error message. If name lookup would
1109 work in both cases, but return a different result, this function
1110 returns the result of ANSI/ISO lookup. Otherwise, it returns
1111 DECL. */
1113 tree
1114 check_for_out_of_scope_variable (tree decl)
1116 tree shadowed;
1118 /* We only care about out of scope variables. */
1119 if (!(TREE_CODE (decl) == VAR_DECL && DECL_DEAD_FOR_LOCAL (decl)))
1120 return decl;
1122 shadowed = DECL_SHADOWED_FOR_VAR (decl);
1123 while (shadowed != NULL_TREE && TREE_CODE (shadowed) == VAR_DECL
1124 && DECL_DEAD_FOR_LOCAL (shadowed))
1125 shadowed = DECL_SHADOWED_FOR_VAR (shadowed);
1126 if (!shadowed)
1127 shadowed = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (decl));
1128 if (shadowed)
1130 if (!DECL_ERROR_REPORTED (decl))
1132 warning ("name lookup of `%D' changed",
1133 DECL_NAME (decl));
1134 cp_warning_at (" matches this `%D' under ISO standard rules",
1135 shadowed);
1136 cp_warning_at (" matches this `%D' under old rules", decl);
1137 DECL_ERROR_REPORTED (decl) = 1;
1139 return shadowed;
1142 /* If we have already complained about this declaration, there's no
1143 need to do it again. */
1144 if (DECL_ERROR_REPORTED (decl))
1145 return decl;
1147 DECL_ERROR_REPORTED (decl) = 1;
1148 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
1150 error ("name lookup of `%D' changed for new ISO `for' scoping",
1151 DECL_NAME (decl));
1152 cp_error_at (" cannot use obsolete binding at `%D' because it has a destructor", decl);
1153 return error_mark_node;
1155 else
1157 pedwarn ("name lookup of `%D' changed for new ISO `for' scoping",
1158 DECL_NAME (decl));
1159 cp_pedwarn_at (" using obsolete binding at `%D'", decl);
1162 return decl;
1165 /* true means unconditionally make a BLOCK for the next level pushed. */
1167 static bool keep_next_level_flag;
1169 static int binding_depth = 0;
1170 static int is_class_level = 0;
1172 static void
1173 indent (int depth)
1175 int i;
1177 for (i = 0; i < depth * 2; i++)
1178 putc (' ', stderr);
1181 /* Return a string describing the kind of SCOPE we have. */
1182 static const char *
1183 cxx_scope_descriptor (cxx_scope *scope)
1185 /* The order of this table must match the "scope_kind"
1186 enumerators. */
1187 static const char* scope_kind_names[] = {
1188 "block-scope",
1189 "cleanup-scope",
1190 "try-scope",
1191 "catch-scope",
1192 "for-scope",
1193 "function-parameter-scope",
1194 "class-scope",
1195 "namespace-scope",
1196 "template-parameter-scope",
1197 "template-explicit-spec-scope"
1199 const scope_kind kind = scope->explicit_spec_p
1200 ? sk_template_spec : scope->kind;
1202 return scope_kind_names[kind];
1205 /* Output a debugging information about SCOPE when performing
1206 ACTION at LINE. */
1207 static void
1208 cxx_scope_debug (cxx_scope *scope, int line, const char *action)
1210 const char *desc = cxx_scope_descriptor (scope);
1211 if (scope->this_entity)
1212 verbatim ("%s %s(%E) %p %d\n", action, desc,
1213 scope->this_entity, (void *) scope, line);
1214 else
1215 verbatim ("%s %s %p %d\n", action, desc, (void *) scope, line);
1218 /* Return the estimated initial size of the hashtable of a NAMESPACE
1219 scope. */
1221 static inline size_t
1222 namespace_scope_ht_size (tree ns)
1224 tree name = DECL_NAME (ns);
1226 return name == std_identifier
1227 ? NAMESPACE_STD_HT_SIZE
1228 : (name == global_scope_name
1229 ? GLOBAL_SCOPE_HT_SIZE
1230 : NAMESPACE_ORDINARY_HT_SIZE);
1233 /* A chain of binding_level structures awaiting reuse. */
1235 static GTY((deletable)) struct cp_binding_level *free_binding_level;
1237 /* Create a new KIND scope and make it the top of the active scopes stack.
1238 ENTITY is the scope of the associated C++ entity (namespace, class,
1239 function); it is NULL otherwise. */
1241 cxx_scope *
1242 begin_scope (scope_kind kind, tree entity)
1244 cxx_scope *scope;
1246 /* Reuse or create a struct for this binding level. */
1247 if (!ENABLE_SCOPE_CHECKING && free_binding_level)
1249 scope = free_binding_level;
1250 free_binding_level = scope->level_chain;
1252 else
1253 scope = ggc_alloc (sizeof (cxx_scope));
1254 memset (scope, 0, sizeof (cxx_scope));
1256 scope->this_entity = entity;
1257 scope->more_cleanups_ok = true;
1258 switch (kind)
1260 case sk_cleanup:
1261 scope->keep = true;
1262 break;
1264 case sk_template_spec:
1265 scope->explicit_spec_p = true;
1266 kind = sk_template_parms;
1267 /* Fall through. */
1268 case sk_template_parms:
1269 case sk_block:
1270 case sk_try:
1271 case sk_catch:
1272 case sk_for:
1273 case sk_class:
1274 case sk_function_parms:
1275 scope->keep = keep_next_level_flag;
1276 break;
1278 case sk_namespace:
1279 scope->type_decls = binding_table_new (namespace_scope_ht_size (entity));
1280 NAMESPACE_LEVEL (entity) = scope;
1281 VARRAY_TREE_INIT (scope->static_decls,
1282 DECL_NAME (entity) == std_identifier
1283 || DECL_NAME (entity) == global_scope_name
1284 ? 200 : 10,
1285 "Static declarations");
1286 break;
1288 default:
1289 /* Should not happen. */
1290 my_friendly_assert (false, 20030922);
1291 break;
1293 scope->kind = kind;
1295 /* Add it to the front of currently active scopes stack. */
1296 scope->level_chain = current_binding_level;
1297 current_binding_level = scope;
1298 keep_next_level_flag = false;
1300 if (ENABLE_SCOPE_CHECKING)
1302 scope->binding_depth = binding_depth;
1303 indent (binding_depth);
1304 cxx_scope_debug (scope, input_location.line, "push");
1305 is_class_level = 0;
1306 binding_depth++;
1309 return scope;
1312 /* We're about to leave current scope. Pop the top of the stack of
1313 currently active scopes. Return the enclosing scope, now active. */
1315 cxx_scope *
1316 leave_scope (void)
1318 cxx_scope *scope = current_binding_level;
1320 if (scope->kind == sk_namespace && class_binding_level)
1321 current_binding_level = class_binding_level;
1323 /* We cannot leave a scope, if there are none left. */
1324 if (NAMESPACE_LEVEL (global_namespace))
1325 my_friendly_assert (!global_scope_p (scope), 20030527);
1327 if (ENABLE_SCOPE_CHECKING)
1329 indent (--binding_depth);
1330 cxx_scope_debug (scope, input_location.line, "leave");
1331 if (is_class_level != (scope == class_binding_level))
1333 indent (binding_depth);
1334 verbatim ("XXX is_class_level != (current_scope == class_scope)\n");
1336 is_class_level = 0;
1339 /* Move one nesting level up. */
1340 current_binding_level = scope->level_chain;
1342 /* Namespace-scopes are left most probably temporarily, not completely;
1343 they can be reopen later, e.g. in namespace-extension or any name
1344 binding activity that requires us to resume a namespace. For other
1345 scopes, we just make the structure available for reuse. */
1346 if (scope->kind != sk_namespace)
1348 scope->level_chain = free_binding_level;
1349 if (scope->kind == sk_class)
1350 scope->type_decls = NULL;
1351 else
1352 binding_table_free (scope->type_decls);
1353 my_friendly_assert (!ENABLE_SCOPE_CHECKING
1354 || scope->binding_depth == binding_depth,
1355 20030529);
1356 free_binding_level = scope;
1359 /* Find the innermost enclosing class scope, and reset
1360 CLASS_BINDING_LEVEL appropriately. */
1361 for (scope = current_binding_level;
1362 scope && scope->kind != sk_class;
1363 scope = scope->level_chain)
1365 class_binding_level = scope && scope->kind == sk_class ? scope : NULL;
1367 return current_binding_level;
1370 static void
1371 resume_scope (struct cp_binding_level* b)
1373 /* Resuming binding levels is meant only for namespaces,
1374 and those cannot nest into classes. */
1375 my_friendly_assert(!class_binding_level, 386);
1376 /* Also, resuming a non-directly nested namespace is a no-no. */
1377 my_friendly_assert(b->level_chain == current_binding_level, 386);
1378 current_binding_level = b;
1379 if (ENABLE_SCOPE_CHECKING)
1381 b->binding_depth = binding_depth;
1382 indent (binding_depth);
1383 cxx_scope_debug (b, input_location.line, "resume");
1384 is_class_level = 0;
1385 binding_depth++;
1389 /* Return the innermost binding level that is not for a class scope. */
1391 static cxx_scope *
1392 innermost_nonclass_level (void)
1394 cxx_scope *b;
1396 b = current_binding_level;
1397 while (b->kind == sk_class)
1398 b = b->level_chain;
1400 return b;
1403 /* We're defining an object of type TYPE. If it needs a cleanup, but
1404 we're not allowed to add any more objects with cleanups to the current
1405 scope, create a new binding level. */
1407 void
1408 maybe_push_cleanup_level (tree type)
1410 if (type != error_mark_node
1411 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
1412 && current_binding_level->more_cleanups_ok == 0)
1414 begin_scope (sk_cleanup, NULL);
1415 clear_last_expr ();
1416 add_scope_stmt (/*begin_p=*/1, /*partial_p=*/1);
1420 /* Nonzero if we are currently in the global binding level. */
1423 global_bindings_p (void)
1425 return global_scope_p (current_binding_level);
1428 /* True if we are currently in a toplevel binding level. This
1429 means either the global binding level or a namespace in a toplevel
1430 binding level. Since there are no non-toplevel namespace levels,
1431 this really means any namespace or template parameter level. We
1432 also include a class whose context is toplevel. */
1434 bool
1435 toplevel_bindings_p (void)
1437 struct cp_binding_level *b = innermost_nonclass_level ();
1439 return b->kind == sk_namespace || b->kind == sk_template_parms;
1442 /* True if this is a namespace scope, or if we are defining a class
1443 which is itself at namespace scope, or whose enclosing class is
1444 such a class, etc. */
1446 bool
1447 namespace_bindings_p (void)
1449 struct cp_binding_level *b = innermost_nonclass_level ();
1451 return b->kind == sk_namespace;
1454 /* True if the current level needs to have a BLOCK made. */
1456 bool
1457 kept_level_p (void)
1459 return (current_binding_level->blocks != NULL_TREE
1460 || current_binding_level->keep
1461 || current_binding_level->kind == sk_cleanup
1462 || current_binding_level->names != NULL_TREE
1463 || current_binding_level->type_decls != NULL);
1466 /* Returns the kind of the innermost scope. */
1468 scope_kind
1469 innermost_scope_kind (void)
1471 return current_binding_level->kind;
1474 /* Returns true if this scope was created to store template parameters. */
1476 bool
1477 template_parm_scope_p (void)
1479 return innermost_scope_kind () == sk_template_parms;
1482 /* If KEEP is true, make a BLOCK node for the next binding level,
1483 unconditionally. Otherwise, use the normal logic to decide whether
1484 or not to create a BLOCK. */
1486 void
1487 keep_next_level (bool keep)
1489 keep_next_level_flag = keep;
1492 /* Return the list of declarations of the current level.
1493 Note that this list is in reverse order unless/until
1494 you nreverse it; and when you do nreverse it, you must
1495 store the result back using `storedecls' or you will lose. */
1497 tree
1498 getdecls (void)
1500 return current_binding_level->names;
1503 /* Set the current binding TABLE for type declarations.. This is a
1504 temporary workaround of the fact that the data structure classtypes
1505 does not currently carry its allocated cxx_scope structure. */
1506 void
1507 cxx_remember_type_decls (binding_table table)
1509 current_binding_level->type_decls = table;
1512 /* For debugging. */
1513 static int no_print_functions = 0;
1514 static int no_print_builtins = 0;
1516 /* Called from print_binding_level through binding_table_foreach to
1517 print the content of binding ENTRY. DATA is a pointer to line offset
1518 marker. */
1519 static void
1520 bt_print_entry (binding_entry entry, void *data)
1522 int *p = (int *) data;
1523 int len;
1525 if (entry->name == NULL)
1526 len = 3;
1527 else if (entry->name == TYPE_IDENTIFIER (entry->type))
1528 len = 2;
1529 else
1530 len = 4;
1531 len = 4;
1533 *p += len;
1535 if (*p > 5)
1537 fprintf (stderr, "\n\t");
1538 *p = len;
1540 if (entry->name == NULL)
1542 print_node_brief (stderr, "<unnamed-typedef", entry->type, 0);
1543 fprintf (stderr, ">");
1545 else if (entry->name == TYPE_IDENTIFIER (entry->type))
1546 print_node_brief (stderr, "", entry->type, 0);
1547 else
1549 print_node_brief (stderr, "<typedef", entry->name, 0);
1550 print_node_brief (stderr, "", entry->type, 0);
1551 fprintf (stderr, ">");
1555 void
1556 print_binding_level (struct cp_binding_level* lvl)
1558 tree t;
1559 int i = 0, len;
1560 fprintf (stderr, " blocks=" HOST_PTR_PRINTF, (void *) lvl->blocks);
1561 if (lvl->more_cleanups_ok)
1562 fprintf (stderr, " more-cleanups-ok");
1563 if (lvl->have_cleanups)
1564 fprintf (stderr, " have-cleanups");
1565 fprintf (stderr, "\n");
1566 if (lvl->names)
1568 fprintf (stderr, " names:\t");
1569 /* We can probably fit 3 names to a line? */
1570 for (t = lvl->names; t; t = TREE_CHAIN (t))
1572 if (no_print_functions && (TREE_CODE (t) == FUNCTION_DECL))
1573 continue;
1574 if (no_print_builtins
1575 && (TREE_CODE (t) == TYPE_DECL)
1576 && (!strcmp (DECL_SOURCE_FILE (t),"<built-in>")))
1577 continue;
1579 /* Function decls tend to have longer names. */
1580 if (TREE_CODE (t) == FUNCTION_DECL)
1581 len = 3;
1582 else
1583 len = 2;
1584 i += len;
1585 if (i > 6)
1587 fprintf (stderr, "\n\t");
1588 i = len;
1590 print_node_brief (stderr, "", t, 0);
1591 if (t == error_mark_node)
1592 break;
1594 if (i)
1595 fprintf (stderr, "\n");
1597 if (lvl->type_decls)
1599 fprintf (stderr, " tags:\t");
1600 i = 0;
1601 binding_table_foreach (lvl->type_decls, bt_print_entry, &i);
1602 if (i)
1603 fprintf (stderr, "\n");
1605 if (lvl->class_shadowed)
1607 fprintf (stderr, " class-shadowed:");
1608 for (t = lvl->class_shadowed; t; t = TREE_CHAIN (t))
1610 fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
1612 fprintf (stderr, "\n");
1614 if (lvl->type_shadowed)
1616 fprintf (stderr, " type-shadowed:");
1617 for (t = lvl->type_shadowed; t; t = TREE_CHAIN (t))
1619 fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
1621 fprintf (stderr, "\n");
1625 void
1626 print_other_binding_stack (struct cp_binding_level *stack)
1628 struct cp_binding_level *level;
1629 for (level = stack; !global_scope_p (level); level = level->level_chain)
1631 fprintf (stderr, "binding level " HOST_PTR_PRINTF "\n", (void *) level);
1632 print_binding_level (level);
1636 void
1637 print_binding_stack (void)
1639 struct cp_binding_level *b;
1640 fprintf (stderr, "current_binding_level=" HOST_PTR_PRINTF
1641 "\nclass_binding_level=" HOST_PTR_PRINTF
1642 "\nNAMESPACE_LEVEL (global_namespace)=" HOST_PTR_PRINTF "\n",
1643 (void *) current_binding_level, (void *) class_binding_level,
1644 (void *) NAMESPACE_LEVEL (global_namespace));
1645 if (class_binding_level)
1647 for (b = class_binding_level; b; b = b->level_chain)
1648 if (b == current_binding_level)
1649 break;
1650 if (b)
1651 b = class_binding_level;
1652 else
1653 b = current_binding_level;
1655 else
1656 b = current_binding_level;
1657 print_other_binding_stack (b);
1658 fprintf (stderr, "global:\n");
1659 print_binding_level (NAMESPACE_LEVEL (global_namespace));
1662 /* Return the type associated with id. */
1664 tree
1665 identifier_type_value (tree id)
1667 timevar_push (TV_NAME_LOOKUP);
1668 /* There is no type with that name, anywhere. */
1669 if (REAL_IDENTIFIER_TYPE_VALUE (id) == NULL_TREE)
1670 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
1671 /* This is not the type marker, but the real thing. */
1672 if (REAL_IDENTIFIER_TYPE_VALUE (id) != global_type_node)
1673 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, REAL_IDENTIFIER_TYPE_VALUE (id));
1674 /* Have to search for it. It must be on the global level, now.
1675 Ask lookup_name not to return non-types. */
1676 id = lookup_name_real (id, 2, 1, 0, LOOKUP_COMPLAIN);
1677 if (id)
1678 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, TREE_TYPE (id));
1679 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
1682 /* Return the IDENTIFIER_GLOBAL_VALUE of T, for use in common code, since
1683 the definition of IDENTIFIER_GLOBAL_VALUE is different for C and C++. */
1685 tree
1686 identifier_global_value (tree t)
1688 return IDENTIFIER_GLOBAL_VALUE (t);
1691 /* Push a definition of struct, union or enum tag named ID. into
1692 binding_level B. DECL is a TYPE_DECL for the type. We assume that
1693 the tag ID is not already defined. */
1695 static void
1696 set_identifier_type_value_with_scope (tree id, tree decl, cxx_scope *b)
1698 tree type;
1700 if (b->kind != sk_namespace)
1702 /* Shadow the marker, not the real thing, so that the marker
1703 gets restored later. */
1704 tree old_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
1705 b->type_shadowed
1706 = tree_cons (id, old_type_value, b->type_shadowed);
1707 type = decl ? TREE_TYPE (decl) : NULL_TREE;
1709 else
1711 cxx_binding *binding =
1712 binding_for_name (NAMESPACE_LEVEL (current_namespace), id);
1713 if (decl)
1715 if (binding->value)
1716 supplement_binding (binding, decl);
1717 else
1718 binding->value = decl;
1720 else
1721 abort ();
1722 /* Store marker instead of real type. */
1723 type = global_type_node;
1725 SET_IDENTIFIER_TYPE_VALUE (id, type);
1728 /* As set_identifier_type_value_with_scope, but using
1729 current_binding_level. */
1731 void
1732 set_identifier_type_value (tree id, tree decl)
1734 set_identifier_type_value_with_scope (id, decl, current_binding_level);
1737 /* Return the name for the constructor (or destructor) for the
1738 specified class TYPE. When given a template, this routine doesn't
1739 lose the specialization. */
1741 tree
1742 constructor_name_full (tree type)
1744 type = TYPE_MAIN_VARIANT (type);
1745 if (CLASS_TYPE_P (type) && TYPE_WAS_ANONYMOUS (type)
1746 && TYPE_HAS_CONSTRUCTOR (type))
1747 return DECL_NAME (OVL_CURRENT (CLASSTYPE_CONSTRUCTORS (type)));
1748 else
1749 return TYPE_IDENTIFIER (type);
1752 /* Return the name for the constructor (or destructor) for the
1753 specified class. When given a template, return the plain
1754 unspecialized name. */
1756 tree
1757 constructor_name (tree type)
1759 tree name;
1760 name = constructor_name_full (type);
1761 if (IDENTIFIER_TEMPLATE (name))
1762 name = IDENTIFIER_TEMPLATE (name);
1763 return name;
1766 /* Returns TRUE if NAME is the name for the constructor for TYPE. */
1768 bool
1769 constructor_name_p (tree name, tree type)
1771 tree ctor_name;
1773 if (!name)
1774 return false;
1776 if (TREE_CODE (name) != IDENTIFIER_NODE)
1777 return false;
1779 ctor_name = constructor_name_full (type);
1780 if (name == ctor_name)
1781 return true;
1782 if (IDENTIFIER_TEMPLATE (ctor_name)
1783 && name == IDENTIFIER_TEMPLATE (ctor_name))
1784 return true;
1785 return false;
1788 /* Counter used to create anonymous type names. */
1790 static GTY(()) int anon_cnt;
1792 /* Return an IDENTIFIER which can be used as a name for
1793 anonymous structs and unions. */
1795 tree
1796 make_anon_name (void)
1798 char buf[32];
1800 sprintf (buf, ANON_AGGRNAME_FORMAT, anon_cnt++);
1801 return get_identifier (buf);
1804 /* Clear the TREE_PURPOSE slot of UTDs which have anonymous typenames.
1805 This keeps dbxout from getting confused. */
1807 void
1808 clear_anon_tags (void)
1810 struct cp_binding_level *b;
1811 static int last_cnt = 0;
1813 /* Fast out if no new anon names were declared. */
1814 if (last_cnt == anon_cnt)
1815 return;
1817 b = current_binding_level;
1818 while (b->kind == sk_cleanup)
1819 b = b->level_chain;
1820 if (b->type_decls != NULL)
1821 binding_table_remove_anonymous_types (b->type_decls);
1822 last_cnt = anon_cnt;
1825 /* Return (from the stack of) the BINDING, if any, established at SCOPE. */
1827 static inline cxx_binding *
1828 find_binding (cxx_scope *scope, cxx_binding *binding)
1830 timevar_push (TV_NAME_LOOKUP);
1832 for (; binding != NULL; binding = binding->previous)
1833 if (binding->scope == scope)
1834 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, binding);
1836 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, (cxx_binding *)0);
1839 /* Return the binding for NAME in SCOPE, if any. Otherwise, return NULL. */
1841 static inline cxx_binding *
1842 cxx_scope_find_binding_for_name (cxx_scope *scope, tree name)
1844 cxx_binding *b = IDENTIFIER_NAMESPACE_BINDINGS (name);
1845 if (b)
1847 /* Fold-in case where NAME is used only once. */
1848 if (scope == b->scope && b->previous == NULL)
1849 return b;
1850 return find_binding (scope, b);
1852 return NULL;
1855 /* Always returns a binding for name in scope. If no binding is
1856 found, make a new one. */
1858 static cxx_binding *
1859 binding_for_name (cxx_scope *scope, tree name)
1861 cxx_binding *result;
1863 result = cxx_scope_find_binding_for_name (scope, name);
1864 if (result)
1865 return result;
1866 /* Not found, make a new one. */
1867 result = cxx_binding_make (NULL, NULL);
1868 result->previous = IDENTIFIER_NAMESPACE_BINDINGS (name);
1869 result->scope = scope;
1870 result->is_local = false;
1871 result->value_is_inherited = false;
1872 IDENTIFIER_NAMESPACE_BINDINGS (name) = result;
1873 return result;
1876 /* Insert another USING_DECL into the current binding level, returning
1877 this declaration. If this is a redeclaration, do nothing, and
1878 return NULL_TREE if this not in namespace scope (in namespace
1879 scope, a using decl might extend any previous bindings). */
1881 tree
1882 push_using_decl (tree scope, tree name)
1884 tree decl;
1886 timevar_push (TV_NAME_LOOKUP);
1887 my_friendly_assert (TREE_CODE (scope) == NAMESPACE_DECL, 383);
1888 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 384);
1889 for (decl = current_binding_level->usings; decl; decl = TREE_CHAIN (decl))
1890 if (DECL_INITIAL (decl) == scope && DECL_NAME (decl) == name)
1891 break;
1892 if (decl)
1893 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP,
1894 namespace_bindings_p () ? decl : NULL_TREE);
1895 decl = build_lang_decl (USING_DECL, name, void_type_node);
1896 DECL_INITIAL (decl) = scope;
1897 TREE_CHAIN (decl) = current_binding_level->usings;
1898 current_binding_level->usings = decl;
1899 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
1902 /* Same as pushdecl, but define X in binding-level LEVEL. We rely on the
1903 caller to set DECL_CONTEXT properly. */
1905 tree
1906 pushdecl_with_scope (tree x, cxx_scope *level)
1908 struct cp_binding_level *b;
1909 tree function_decl = current_function_decl;
1911 timevar_push (TV_NAME_LOOKUP);
1912 current_function_decl = NULL_TREE;
1913 if (level->kind == sk_class)
1915 b = class_binding_level;
1916 class_binding_level = level;
1917 pushdecl_class_level (x);
1918 class_binding_level = b;
1920 else
1922 b = current_binding_level;
1923 current_binding_level = level;
1924 x = pushdecl (x);
1925 current_binding_level = b;
1927 current_function_decl = function_decl;
1928 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
1931 /* DECL is a FUNCTION_DECL for a non-member function, which may have
1932 other definitions already in place. We get around this by making
1933 the value of the identifier point to a list of all the things that
1934 want to be referenced by that name. It is then up to the users of
1935 that name to decide what to do with that list.
1937 DECL may also be a TEMPLATE_DECL, with a FUNCTION_DECL in its
1938 DECL_TEMPLATE_RESULT. It is dealt with the same way.
1940 FLAGS is a bitwise-or of the following values:
1941 PUSH_LOCAL: Bind DECL in the current scope, rather than at
1942 namespace scope.
1943 PUSH_USING: DECL is being pushed as the result of a using
1944 declaration.
1946 The value returned may be a previous declaration if we guessed wrong
1947 about what language DECL should belong to (C or C++). Otherwise,
1948 it's always DECL (and never something that's not a _DECL). */
1950 static tree
1951 push_overloaded_decl (tree decl, int flags)
1953 tree name = DECL_NAME (decl);
1954 tree old;
1955 tree new_binding;
1956 int doing_global = (namespace_bindings_p () || !(flags & PUSH_LOCAL));
1958 timevar_push (TV_NAME_LOOKUP);
1959 if (doing_global)
1960 old = namespace_binding (name, DECL_CONTEXT (decl));
1961 else
1962 old = lookup_name_current_level (name);
1964 if (old)
1966 if (TREE_CODE (old) == TYPE_DECL && DECL_ARTIFICIAL (old))
1968 tree t = TREE_TYPE (old);
1969 if (IS_AGGR_TYPE (t) && warn_shadow
1970 && (! DECL_IN_SYSTEM_HEADER (decl)
1971 || ! DECL_IN_SYSTEM_HEADER (old)))
1972 warning ("`%#D' hides constructor for `%#T'", decl, t);
1973 old = NULL_TREE;
1975 else if (is_overloaded_fn (old))
1977 tree tmp;
1979 for (tmp = old; tmp; tmp = OVL_NEXT (tmp))
1981 tree fn = OVL_CURRENT (tmp);
1983 if (TREE_CODE (tmp) == OVERLOAD && OVL_USED (tmp)
1984 && !(flags & PUSH_USING)
1985 && compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
1986 TYPE_ARG_TYPES (TREE_TYPE (decl))))
1987 error ("`%#D' conflicts with previous using declaration `%#D'",
1988 decl, fn);
1990 if (duplicate_decls (decl, fn) == fn)
1991 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, fn);
1994 else if (old == error_mark_node)
1995 /* Ignore the undefined symbol marker. */
1996 old = NULL_TREE;
1997 else
1999 cp_error_at ("previous non-function declaration `%#D'", old);
2000 error ("conflicts with function declaration `%#D'", decl);
2001 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
2005 if (old || TREE_CODE (decl) == TEMPLATE_DECL
2006 /* If it's a using declaration, we always need to build an OVERLOAD,
2007 because it's the only way to remember that the declaration comes
2008 from 'using', and have the lookup behave correctly. */
2009 || (flags & PUSH_USING))
2011 if (old && TREE_CODE (old) != OVERLOAD)
2012 new_binding = ovl_cons (decl, ovl_cons (old, NULL_TREE));
2013 else
2014 new_binding = ovl_cons (decl, old);
2015 if (flags & PUSH_USING)
2016 OVL_USED (new_binding) = 1;
2018 else
2019 /* NAME is not ambiguous. */
2020 new_binding = decl;
2022 if (doing_global)
2023 set_namespace_binding (name, current_namespace, new_binding);
2024 else
2026 /* We only create an OVERLOAD if there was a previous binding at
2027 this level, or if decl is a template. In the former case, we
2028 need to remove the old binding and replace it with the new
2029 binding. We must also run through the NAMES on the binding
2030 level where the name was bound to update the chain. */
2032 if (TREE_CODE (new_binding) == OVERLOAD && old)
2034 tree *d;
2036 for (d = &IDENTIFIER_BINDING (name)->scope->names;
2038 d = &TREE_CHAIN (*d))
2039 if (*d == old
2040 || (TREE_CODE (*d) == TREE_LIST
2041 && TREE_VALUE (*d) == old))
2043 if (TREE_CODE (*d) == TREE_LIST)
2044 /* Just replace the old binding with the new. */
2045 TREE_VALUE (*d) = new_binding;
2046 else
2047 /* Build a TREE_LIST to wrap the OVERLOAD. */
2048 *d = tree_cons (NULL_TREE, new_binding,
2049 TREE_CHAIN (*d));
2051 /* And update the cxx_binding node. */
2052 IDENTIFIER_BINDING (name)->value = new_binding;
2053 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
2056 /* We should always find a previous binding in this case. */
2057 abort ();
2060 /* Install the new binding. */
2061 push_local_binding (name, new_binding, flags);
2064 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
2067 /* Check a non-member using-declaration. Return the name and scope
2068 being used, and the USING_DECL, or NULL_TREE on failure. */
2070 static tree
2071 validate_nonmember_using_decl (tree decl, tree scope, tree name)
2073 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR)
2075 /* 7.3.3/5
2076 A using-declaration shall not name a template-id. */
2077 error ("a using-declaration cannot specify a template-id. Try `using %D'", name);
2078 return NULL_TREE;
2081 if (TREE_CODE (decl) == NAMESPACE_DECL)
2083 error ("namespace `%D' not allowed in using-declaration", decl);
2084 return NULL_TREE;
2087 if (TREE_CODE (decl) == SCOPE_REF)
2089 /* It's a nested name with template parameter dependent scope.
2090 This can only be using-declaration for class member. */
2091 error ("`%T' is not a namespace", TREE_OPERAND (decl, 0));
2092 return NULL_TREE;
2095 if (is_overloaded_fn (decl))
2096 decl = get_first_fn (decl);
2098 my_friendly_assert (DECL_P (decl), 20020908);
2100 /* [namespace.udecl]
2101 A using-declaration for a class member shall be a
2102 member-declaration. */
2103 if (TYPE_P (scope))
2105 error ("`%T' is not a namespace", scope);
2106 return NULL_TREE;
2109 /* Make a USING_DECL. */
2110 return push_using_decl (scope, name);
2113 /* Process local and global using-declarations. */
2115 static void
2116 do_nonmember_using_decl (tree scope, tree name, tree oldval, tree oldtype,
2117 tree *newval, tree *newtype)
2119 cxx_binding decls;
2121 *newval = *newtype = NULL_TREE;
2122 cxx_binding_clear (&decls);
2123 if (!qualified_lookup_using_namespace (name, scope, &decls, 0))
2124 /* Lookup error */
2125 return;
2127 if (!decls.value && !decls.type)
2129 error ("`%D' not declared", name);
2130 return;
2133 /* Check for using functions. */
2134 if (decls.value && is_overloaded_fn (decls.value))
2136 tree tmp, tmp1;
2138 if (oldval && !is_overloaded_fn (oldval))
2140 if (!DECL_IMPLICIT_TYPEDEF_P (oldval))
2141 error ("`%D' is already declared in this scope", name);
2142 oldval = NULL_TREE;
2145 *newval = oldval;
2146 for (tmp = decls.value; tmp; tmp = OVL_NEXT (tmp))
2148 tree new_fn = OVL_CURRENT (tmp);
2150 /* [namespace.udecl]
2152 If a function declaration in namespace scope or block
2153 scope has the same name and the same parameter types as a
2154 function introduced by a using declaration the program is
2155 ill-formed. */
2156 for (tmp1 = oldval; tmp1; tmp1 = OVL_NEXT (tmp1))
2158 tree old_fn = OVL_CURRENT (tmp1);
2160 if (new_fn == old_fn)
2161 /* The function already exists in the current namespace. */
2162 break;
2163 else if (OVL_USED (tmp1))
2164 continue; /* this is a using decl */
2165 else if (compparms (TYPE_ARG_TYPES (TREE_TYPE (new_fn)),
2166 TYPE_ARG_TYPES (TREE_TYPE (old_fn))))
2168 /* There was already a non-using declaration in
2169 this scope with the same parameter types. If both
2170 are the same extern "C" functions, that's ok. */
2171 if (decls_match (new_fn, old_fn))
2173 /* If the OLD_FN was a builtin, there is now a
2174 real declaration. */
2175 if (DECL_ANTICIPATED (old_fn))
2176 DECL_ANTICIPATED (old_fn) = 0;
2177 break;
2179 else if (!DECL_ANTICIPATED (old_fn))
2181 /* If the OLD_FN was really declared, the
2182 declarations don't match. */
2183 error ("`%D' is already declared in this scope", name);
2184 break;
2187 /* If the OLD_FN was not really there, just ignore
2188 it and keep going. */
2192 /* If we broke out of the loop, there's no reason to add
2193 this function to the using declarations for this
2194 scope. */
2195 if (tmp1)
2196 continue;
2198 /* If we are adding to an existing OVERLOAD, then we no
2199 longer know the type of the set of functions. */
2200 if (*newval && TREE_CODE (*newval) == OVERLOAD)
2201 TREE_TYPE (*newval) = unknown_type_node;
2202 /* Add this new function to the set. */
2203 *newval = build_overload (OVL_CURRENT (tmp), *newval);
2204 /* If there is only one function, then we use its type. (A
2205 using-declaration naming a single function can be used in
2206 contexts where overload resolution cannot be
2207 performed.) */
2208 if (TREE_CODE (*newval) != OVERLOAD)
2210 *newval = ovl_cons (*newval, NULL_TREE);
2211 TREE_TYPE (*newval) = TREE_TYPE (OVL_CURRENT (tmp));
2213 OVL_USED (*newval) = 1;
2216 else
2218 *newval = decls.value;
2219 if (oldval && !decls_match (*newval, oldval))
2220 error ("`%D' is already declared in this scope", name);
2223 *newtype = decls.type;
2224 if (oldtype && *newtype && !same_type_p (oldtype, *newtype))
2226 error ("using declaration `%D' introduced ambiguous type `%T'",
2227 name, oldtype);
2228 return;
2232 /* Process a using-declaration at function scope. */
2234 void
2235 do_local_using_decl (tree decl, tree scope, tree name)
2237 tree oldval, oldtype, newval, newtype;
2238 tree orig_decl = decl;
2240 decl = validate_nonmember_using_decl (decl, scope, name);
2241 if (decl == NULL_TREE)
2242 return;
2244 if (building_stmt_tree ()
2245 && at_function_scope_p ())
2246 add_decl_stmt (decl);
2248 oldval = lookup_name_current_level (name);
2249 oldtype = lookup_type_current_level (name);
2251 do_nonmember_using_decl (scope, name, oldval, oldtype, &newval, &newtype);
2253 if (newval)
2255 if (is_overloaded_fn (newval))
2257 tree fn, term;
2259 /* We only need to push declarations for those functions
2260 that were not already bound in the current level.
2261 The old value might be NULL_TREE, it might be a single
2262 function, or an OVERLOAD. */
2263 if (oldval && TREE_CODE (oldval) == OVERLOAD)
2264 term = OVL_FUNCTION (oldval);
2265 else
2266 term = oldval;
2267 for (fn = newval; fn && OVL_CURRENT (fn) != term;
2268 fn = OVL_NEXT (fn))
2269 push_overloaded_decl (OVL_CURRENT (fn),
2270 PUSH_LOCAL | PUSH_USING);
2272 else
2273 push_local_binding (name, newval, PUSH_USING);
2275 if (newtype)
2277 push_local_binding (name, newtype, PUSH_USING);
2278 set_identifier_type_value (name, newtype);
2281 /* Emit debug info. */
2282 if (!processing_template_decl)
2283 cp_emit_debug_info_for_using (orig_decl, current_scope());
2286 /* Return the type that should be used when TYPE's name is preceded
2287 by a tag such as 'struct' or 'union', or null if the name cannot
2288 be used in this way.
2290 For example, when processing the third line of:
2292 struct A;
2293 typedef struct A A;
2294 struct A;
2296 lookup of A will find the typedef. Given A's typedef, this function
2297 will return the type associated with "struct A". For the tag to be
2298 anything other than TYPE, TYPE must be a typedef whose original type
2299 has the same name and context as TYPE itself.
2301 It is not valid for a typedef of an anonymous type to be used with
2302 an explicit tag:
2304 typedef struct { ... } B;
2305 struct B;
2307 Return null for this case. */
2309 static tree
2310 follow_tag_typedef (tree type)
2312 tree original;
2314 original = original_type (type);
2315 if (! TYPE_NAME (original))
2316 return NULL_TREE;
2317 if (TYPE_IDENTIFIER (original) == TYPE_IDENTIFIER (type)
2318 && (CP_DECL_CONTEXT (TYPE_NAME (original))
2319 == CP_DECL_CONTEXT (TYPE_NAME (type)))
2320 && !(CLASS_TYPE_P (original) && TYPE_WAS_ANONYMOUS (original)))
2321 return original;
2322 else
2323 return NULL_TREE;
2326 /* Given NAME, an IDENTIFIER_NODE,
2327 return the structure (or union or enum) definition for that name.
2328 Searches binding levels from its SCOPE up to the global level.
2329 If THISLEVEL_ONLY is nonzero, searches only the specified context
2330 (but skips any sk_cleanup contexts to find one that is
2331 meaningful for tags).
2332 FORM says which kind of type the caller wants;
2333 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
2334 If the wrong kind of type is found, and it's not a template, an error is
2335 reported. */
2337 tree
2338 lookup_tag (enum tree_code form, tree name,
2339 cxx_scope *binding_level, int thislevel_only)
2341 struct cp_binding_level *level;
2342 /* Nonzero if, we should look past a template parameter level, even
2343 if THISLEVEL_ONLY. */
2344 int allow_template_parms_p = 1;
2345 bool type_is_anonymous = ANON_AGGRNAME_P (name);
2347 timevar_push (TV_NAME_LOOKUP);
2348 for (level = binding_level; level; level = level->level_chain)
2350 tree tail;
2351 if (type_is_anonymous && level->type_decls != NULL)
2353 tree type = binding_table_find_anon_type (level->type_decls, name);
2354 /* There is no need for error checking here, because
2355 anon names are unique throughout the compilation. */
2356 if (type != NULL)
2357 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, type);
2359 else if (level->kind == sk_namespace)
2360 /* Do namespace lookup. */
2361 for (tail = current_namespace; 1; tail = CP_DECL_CONTEXT (tail))
2363 cxx_binding *binding =
2364 cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (tail), name);
2365 tree old;
2367 /* If we just skipped past a template parameter level,
2368 even though THISLEVEL_ONLY, and we find a template
2369 class declaration, then we use the _TYPE node for the
2370 template. See the example below. */
2371 if (thislevel_only && !allow_template_parms_p
2372 && binding && binding->value
2373 && DECL_CLASS_TEMPLATE_P (binding->value))
2374 old = binding->value;
2375 else if (binding)
2376 old = select_decl (binding, LOOKUP_PREFER_TYPES);
2377 else
2378 old = NULL_TREE;
2380 if (old)
2382 /* We've found something at this binding level. If it is
2383 a typedef, extract the tag it refers to. Lookup fails
2384 if the typedef doesn't refer to a taggable type. */
2385 old = TREE_TYPE (old);
2386 old = follow_tag_typedef (old);
2387 if (!old)
2388 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
2389 if (TREE_CODE (old) != form
2390 && (form == ENUMERAL_TYPE
2391 || TREE_CODE (old) == ENUMERAL_TYPE))
2393 error ("`%#D' redeclared as %C", old, form);
2394 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
2396 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, old);
2398 if (thislevel_only || tail == global_namespace)
2399 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
2401 else if (level->type_decls != NULL)
2403 binding_entry entry = binding_table_find (level->type_decls, name);
2404 if (entry != NULL)
2406 enum tree_code code = TREE_CODE (entry->type);
2408 if (code != form
2409 && (form == ENUMERAL_TYPE || code == ENUMERAL_TYPE))
2411 /* Definition isn't the kind we were looking for. */
2412 error ("`%#D' redeclared as %C", entry->type, form);
2413 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
2415 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, entry->type);
2418 if (thislevel_only && level->kind != sk_cleanup)
2420 if (level->kind == sk_template_parms && allow_template_parms_p)
2422 /* We must deal with cases like this:
2424 template <class T> struct S;
2425 template <class T> struct S {};
2427 When looking up `S', for the second declaration, we
2428 would like to find the first declaration. But, we
2429 are in the pseudo-global level created for the
2430 template parameters, rather than the (surrounding)
2431 namespace level. Thus, we keep going one more level,
2432 even though THISLEVEL_ONLY is nonzero. */
2433 allow_template_parms_p = 0;
2434 continue;
2436 else
2437 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
2440 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
2443 /* Given a type, find the tag that was defined for it and return the tag name.
2444 Otherwise return 0. However, the value can never be 0
2445 in the cases in which this is used.
2447 C++: If NAME is nonzero, this is the new name to install. This is
2448 done when replacing anonymous tags with real tag names. */
2450 tree
2451 lookup_tag_reverse (tree type, tree name)
2453 struct cp_binding_level *level;
2455 timevar_push (TV_NAME_LOOKUP);
2456 for (level = current_binding_level; level; level = level->level_chain)
2458 binding_entry entry = level->type_decls == NULL
2459 ? NULL
2460 : binding_table_reverse_maybe_remap (level->type_decls, type, name);
2461 if (entry)
2462 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, entry->name);
2464 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
2467 /* Returns true if ROOT (a namespace, class, or function) encloses
2468 CHILD. CHILD may be either a class type or a namespace. */
2470 bool
2471 is_ancestor (tree root, tree child)
2473 my_friendly_assert ((TREE_CODE (root) == NAMESPACE_DECL
2474 || TREE_CODE (root) == FUNCTION_DECL
2475 || CLASS_TYPE_P (root)), 20030307);
2476 my_friendly_assert ((TREE_CODE (child) == NAMESPACE_DECL
2477 || CLASS_TYPE_P (child)),
2478 20030307);
2480 /* The global namespace encloses everything. */
2481 if (root == global_namespace)
2482 return true;
2484 while (true)
2486 /* If we've run out of scopes, stop. */
2487 if (!child)
2488 return false;
2489 /* If we've reached the ROOT, it encloses CHILD. */
2490 if (root == child)
2491 return true;
2492 /* Go out one level. */
2493 if (TYPE_P (child))
2494 child = TYPE_NAME (child);
2495 child = DECL_CONTEXT (child);
2499 /* Enter the class or namespace scope indicated by T. Returns TRUE iff
2500 pop_scope should be called later to exit this scope. */
2502 bool
2503 push_scope (tree t)
2505 bool pop = true;
2507 if (TREE_CODE (t) == NAMESPACE_DECL)
2508 push_decl_namespace (t);
2509 else if (CLASS_TYPE_P (t))
2511 if (!at_class_scope_p ()
2512 || !same_type_p (current_class_type, t))
2513 push_nested_class (t);
2514 else
2515 /* T is the same as the current scope. There is therefore no
2516 need to re-enter the scope. Since we are not actually
2517 pushing a new scope, our caller should not call
2518 pop_scope. */
2519 pop = false;
2522 return pop;
2525 /* Leave scope pushed by push_scope. */
2527 void
2528 pop_scope (tree t)
2530 if (TREE_CODE (t) == NAMESPACE_DECL)
2531 pop_decl_namespace ();
2532 else if CLASS_TYPE_P (t)
2533 pop_nested_class ();
2536 /* Do a pushlevel for class declarations. */
2538 void
2539 pushlevel_class (void)
2541 if (ENABLE_SCOPE_CHECKING)
2542 is_class_level = 1;
2544 class_binding_level = begin_scope (sk_class, current_class_type);
2547 /* ...and a poplevel for class declarations. */
2549 void
2550 poplevel_class (void)
2552 struct cp_binding_level *level = class_binding_level;
2553 tree shadowed;
2555 timevar_push (TV_NAME_LOOKUP);
2556 my_friendly_assert (level != 0, 354);
2558 /* If we're leaving a toplevel class, don't bother to do the setting
2559 of IDENTIFIER_CLASS_VALUE to NULL_TREE, since first of all this slot
2560 shouldn't even be used when current_class_type isn't set, and second,
2561 if we don't touch it here, we're able to use the cache effect if the
2562 next time we're entering a class scope, it is the same class. */
2563 if (current_class_depth != 1)
2565 struct cp_binding_level* b;
2567 /* Clear out our IDENTIFIER_CLASS_VALUEs. */
2568 for (shadowed = level->class_shadowed;
2569 shadowed;
2570 shadowed = TREE_CHAIN (shadowed))
2571 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (shadowed)) = NULL_TREE;
2573 /* Find the next enclosing class, and recreate
2574 IDENTIFIER_CLASS_VALUEs appropriate for that class. */
2575 b = level->level_chain;
2576 while (b && b->kind != sk_class)
2577 b = b->level_chain;
2579 if (b)
2580 for (shadowed = b->class_shadowed;
2581 shadowed;
2582 shadowed = TREE_CHAIN (shadowed))
2584 cxx_binding *binding;
2586 binding = IDENTIFIER_BINDING (TREE_PURPOSE (shadowed));
2587 while (binding && binding->scope != b)
2588 binding = binding->previous;
2590 if (binding)
2591 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (shadowed))
2592 = binding->value;
2595 else
2596 /* Remember to save what IDENTIFIER's were bound in this scope so we
2597 can recover from cache misses. */
2599 previous_class_type = current_class_type;
2600 previous_class_values = class_binding_level->class_shadowed;
2602 for (shadowed = level->type_shadowed;
2603 shadowed;
2604 shadowed = TREE_CHAIN (shadowed))
2605 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (shadowed), TREE_VALUE (shadowed));
2607 /* Remove the bindings for all of the class-level declarations. */
2608 for (shadowed = level->class_shadowed;
2609 shadowed;
2610 shadowed = TREE_CHAIN (shadowed))
2611 pop_binding (TREE_PURPOSE (shadowed), TREE_TYPE (shadowed));
2613 /* Now, pop out of the binding level which we created up in the
2614 `pushlevel_class' routine. */
2615 if (ENABLE_SCOPE_CHECKING)
2616 is_class_level = 1;
2618 leave_scope ();
2619 timevar_pop (TV_NAME_LOOKUP);
2622 /* Bind DECL to ID in the class_binding_level. Returns nonzero if the
2623 binding was successful. */
2626 push_class_binding (tree id, tree decl)
2628 int result = 1;
2629 cxx_binding *binding = IDENTIFIER_BINDING (id);
2630 tree context;
2632 timevar_push (TV_NAME_LOOKUP);
2633 /* Note that we declared this value so that we can issue an error if
2634 this is an invalid redeclaration of a name already used for some
2635 other purpose. */
2636 note_name_declared_in_class (id, decl);
2638 if (binding && binding->scope == class_binding_level)
2639 /* Supplement the existing binding. */
2640 result = supplement_binding (IDENTIFIER_BINDING (id), decl);
2641 else
2642 /* Create a new binding. */
2643 push_binding (id, decl, class_binding_level);
2645 /* Update the IDENTIFIER_CLASS_VALUE for this ID to be the
2646 class-level declaration. Note that we do not use DECL here
2647 because of the possibility of the `struct stat' hack; if DECL is
2648 a class-name or enum-name we might prefer a field-name, or some
2649 such. */
2650 IDENTIFIER_CLASS_VALUE (id) = IDENTIFIER_BINDING (id)->value;
2652 /* If this is a binding from a base class, mark it as such. */
2653 binding = IDENTIFIER_BINDING (id);
2654 if (binding->value == decl && TREE_CODE (decl) != TREE_LIST)
2656 if (TREE_CODE (decl) == OVERLOAD)
2657 context = CP_DECL_CONTEXT (OVL_CURRENT (decl));
2658 else
2660 my_friendly_assert (DECL_P (decl), 0);
2661 context = context_for_name_lookup (decl);
2664 if (is_properly_derived_from (current_class_type, context))
2665 INHERITED_VALUE_BINDING_P (binding) = 1;
2666 else
2667 INHERITED_VALUE_BINDING_P (binding) = 0;
2669 else if (binding->value == decl)
2670 /* We only encounter a TREE_LIST when push_class_decls detects an
2671 ambiguity. Such an ambiguity can be overridden by a definition
2672 in this class. */
2673 INHERITED_VALUE_BINDING_P (binding) = 1;
2675 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, result);
2678 /* We are entering the scope of a class. Clear IDENTIFIER_CLASS_VALUE
2679 for any names in enclosing classes. */
2681 void
2682 clear_identifier_class_values (void)
2684 tree t;
2686 if (!class_binding_level)
2687 return;
2689 for (t = class_binding_level->class_shadowed;
2691 t = TREE_CHAIN (t))
2692 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (t)) = NULL_TREE;
2695 /* Make the declaration of X appear in CLASS scope. */
2697 bool
2698 pushdecl_class_level (tree x)
2700 tree name;
2701 bool is_valid = true;
2703 timevar_push (TV_NAME_LOOKUP);
2704 /* Get the name of X. */
2705 if (TREE_CODE (x) == OVERLOAD)
2706 name = DECL_NAME (get_first_fn (x));
2707 else
2708 name = DECL_NAME (x);
2710 if (name)
2712 is_valid = push_class_level_binding (name, x);
2713 if (TREE_CODE (x) == TYPE_DECL)
2714 set_identifier_type_value (name, x);
2716 else if (ANON_AGGR_TYPE_P (TREE_TYPE (x)))
2718 /* If X is an anonymous aggregate, all of its members are
2719 treated as if they were members of the class containing the
2720 aggregate, for naming purposes. */
2721 tree f;
2723 for (f = TYPE_FIELDS (TREE_TYPE (x)); f; f = TREE_CHAIN (f))
2725 location_t save_location = input_location;
2726 input_location = DECL_SOURCE_LOCATION (f);
2727 if (!pushdecl_class_level (f))
2728 is_valid = false;
2729 input_location = save_location;
2732 timevar_pop (TV_NAME_LOOKUP);
2734 return is_valid;
2737 /* Make the declaration(s) of X appear in CLASS scope under the name
2738 NAME. Returns true if the binding is valid. */
2740 bool
2741 push_class_level_binding (tree name, tree x)
2743 cxx_binding *binding;
2745 timevar_push (TV_NAME_LOOKUP);
2746 /* The class_binding_level will be NULL if x is a template
2747 parameter name in a member template. */
2748 if (!class_binding_level)
2749 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, true);
2751 /* Make sure that this new member does not have the same name
2752 as a template parameter. */
2753 if (TYPE_BEING_DEFINED (current_class_type))
2754 check_template_shadow (x);
2756 /* [class.mem]
2758 If T is the name of a class, then each of the following shall
2759 have a name different from T:
2761 -- every static data member of class T;
2763 -- every member of class T that is itself a type;
2765 -- every enumerator of every member of class T that is an
2766 enumerated type;
2768 -- every member of every anonymous union that is a member of
2769 class T.
2771 (Non-static data members were also forbidden to have the same
2772 name as T until TC1.) */
2773 if ((TREE_CODE (x) == VAR_DECL
2774 || TREE_CODE (x) == CONST_DECL
2775 || (TREE_CODE (x) == TYPE_DECL
2776 && !DECL_SELF_REFERENCE_P (x))
2777 /* A data member of an anonymous union. */
2778 || (TREE_CODE (x) == FIELD_DECL
2779 && DECL_CONTEXT (x) != current_class_type))
2780 && DECL_NAME (x) == constructor_name (current_class_type))
2782 tree scope = context_for_name_lookup (x);
2783 if (TYPE_P (scope) && same_type_p (scope, current_class_type))
2785 error ("`%D' has the same name as the class in which it is declared",
2787 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, false);
2791 /* If this declaration shadows a declaration from an enclosing
2792 class, then we will need to restore IDENTIFIER_CLASS_VALUE when
2793 we leave this class. Record the shadowed declaration here. */
2794 binding = IDENTIFIER_BINDING (name);
2795 if (binding && binding->value)
2797 tree bval = binding->value;
2798 tree old_decl = NULL_TREE;
2800 if (INHERITED_VALUE_BINDING_P (binding))
2802 /* If the old binding was from a base class, and was for a
2803 tag name, slide it over to make room for the new binding.
2804 The old binding is still visible if explicitly qualified
2805 with a class-key. */
2806 if (TREE_CODE (bval) == TYPE_DECL && DECL_ARTIFICIAL (bval)
2807 && !(TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x)))
2809 old_decl = binding->type;
2810 binding->type = bval;
2811 binding->value = NULL_TREE;
2812 INHERITED_VALUE_BINDING_P (binding) = 0;
2814 else
2815 old_decl = bval;
2817 else if (TREE_CODE (x) == OVERLOAD && is_overloaded_fn (bval))
2818 old_decl = bval;
2819 else if (TREE_CODE (x) == USING_DECL && TREE_CODE (bval) == USING_DECL)
2820 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, true);
2821 else if (TREE_CODE (x) == USING_DECL && is_overloaded_fn (bval))
2822 old_decl = bval;
2823 else if (TREE_CODE (bval) == USING_DECL && is_overloaded_fn (x))
2824 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, true);
2826 if (old_decl)
2828 tree shadow;
2830 /* Find the previous binding of name on the class-shadowed
2831 list, and update it. */
2832 for (shadow = class_binding_level->class_shadowed;
2833 shadow;
2834 shadow = TREE_CHAIN (shadow))
2835 if (TREE_PURPOSE (shadow) == name
2836 && TREE_TYPE (shadow) == old_decl)
2838 binding->value = x;
2839 INHERITED_VALUE_BINDING_P (binding) = 0;
2840 TREE_TYPE (shadow) = x;
2841 IDENTIFIER_CLASS_VALUE (name) = x;
2842 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, true);
2847 /* If we didn't replace an existing binding, put the binding on the
2848 stack of bindings for the identifier, and update the shadowed list. */
2849 if (push_class_binding (name, x))
2851 class_binding_level->class_shadowed
2852 = tree_cons (name, NULL,
2853 class_binding_level->class_shadowed);
2854 /* Record the value we are binding NAME to so that we can know
2855 what to pop later. */
2856 TREE_TYPE (class_binding_level->class_shadowed) = x;
2857 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, true);
2860 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, false);
2863 tree
2864 do_class_using_decl (tree decl)
2866 tree name, value, scope, type;
2868 if (TREE_CODE (decl) != SCOPE_REF
2869 || !TREE_OPERAND (decl, 0)
2870 || !TYPE_P (TREE_OPERAND (decl, 0)))
2872 error ("using-declaration for non-member at class scope");
2873 return NULL_TREE;
2875 scope = TREE_OPERAND (decl, 0);
2876 name = TREE_OPERAND (decl, 1);
2877 if (TREE_CODE (name) == BIT_NOT_EXPR)
2879 error ("using-declaration cannot name destructor");
2880 return NULL_TREE;
2882 if (TREE_CODE (name) == TYPE_DECL)
2883 name = DECL_NAME (name);
2884 else if (TREE_CODE (name) == TEMPLATE_DECL)
2885 name = DECL_NAME (name);
2886 else if (BASELINK_P (name))
2888 tree fns = BASELINK_FUNCTIONS (name);
2889 name = DECL_NAME (get_first_fn (fns));
2892 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 980716);
2894 /* Dependent using decls have a NULL type, non-dependent ones have a
2895 void type. */
2896 type = dependent_type_p (scope) ? NULL_TREE : void_type_node;
2897 value = build_lang_decl (USING_DECL, name, type);
2898 DECL_INITIAL (value) = scope;
2900 if (scope && !processing_template_decl)
2902 tree r;
2904 r = lookup_qualified_name (scope, name, false, false);
2905 if (r && TREE_CODE (r) != ERROR_MARK)
2906 cp_emit_debug_info_for_using (r, scope);
2908 return value;
2911 void
2912 set_class_shadows (tree shadows)
2914 class_binding_level->class_shadowed = shadows;
2917 /* Return the binding value for name in scope. */
2919 tree
2920 namespace_binding (tree name, tree scope)
2922 cxx_binding *binding;
2924 if (scope == NULL)
2925 scope = global_namespace;
2926 scope = ORIGINAL_NAMESPACE (scope);
2927 binding = cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (scope), name);
2929 return binding ? binding->value : NULL_TREE;
2932 /* Set the binding value for name in scope. */
2934 void
2935 set_namespace_binding (tree name, tree scope, tree val)
2937 cxx_binding *b;
2939 timevar_push (TV_NAME_LOOKUP);
2940 if (scope == NULL_TREE)
2941 scope = global_namespace;
2942 b = binding_for_name (NAMESPACE_LEVEL (scope), name);
2943 if (!b->value || TREE_CODE (val) == OVERLOAD || val == error_mark_node)
2944 b->value = val;
2945 else
2946 supplement_binding (b, val);
2947 timevar_pop (TV_NAME_LOOKUP);
2950 /* Compute the namespace where a declaration is defined. */
2952 static tree
2953 decl_namespace (tree decl)
2955 timevar_push (TV_NAME_LOOKUP);
2956 if (TYPE_P (decl))
2957 decl = TYPE_STUB_DECL (decl);
2958 while (DECL_CONTEXT (decl))
2960 decl = DECL_CONTEXT (decl);
2961 if (TREE_CODE (decl) == NAMESPACE_DECL)
2962 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
2963 if (TYPE_P (decl))
2964 decl = TYPE_STUB_DECL (decl);
2965 my_friendly_assert (DECL_P (decl), 390);
2968 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, global_namespace);
2971 /* Set the context of a declaration to scope. Complain if we are not
2972 outside scope. */
2974 void
2975 set_decl_namespace (tree decl, tree scope, bool friendp)
2977 tree old;
2979 /* Get rid of namespace aliases. */
2980 scope = ORIGINAL_NAMESPACE (scope);
2982 /* It is ok for friends to be qualified in parallel space. */
2983 if (!friendp && !is_ancestor (current_namespace, scope))
2984 error ("declaration of `%D' not in a namespace surrounding `%D'",
2985 decl, scope);
2986 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
2987 if (scope != current_namespace)
2989 /* See whether this has been declared in the namespace. */
2990 old = namespace_binding (DECL_NAME (decl), scope);
2991 if (!old)
2992 /* No old declaration at all. */
2993 goto complain;
2994 /* A template can be explicitly specialized in any namespace. */
2995 if (processing_explicit_instantiation)
2996 return;
2997 if (!is_overloaded_fn (decl))
2998 /* Don't compare non-function decls with decls_match here,
2999 since it can't check for the correct constness at this
3000 point. pushdecl will find those errors later. */
3001 return;
3002 /* Since decl is a function, old should contain a function decl. */
3003 if (!is_overloaded_fn (old))
3004 goto complain;
3005 if (processing_template_decl || processing_specialization)
3006 /* We have not yet called push_template_decl to turn a
3007 FUNCTION_DECL into a TEMPLATE_DECL, so the declarations
3008 won't match. But, we'll check later, when we construct the
3009 template. */
3010 return;
3011 if (is_overloaded_fn (old))
3013 for (; old; old = OVL_NEXT (old))
3014 if (decls_match (decl, OVL_CURRENT (old)))
3015 return;
3017 else
3018 if (decls_match (decl, old))
3019 return;
3021 else
3022 return;
3023 complain:
3024 error ("`%D' should have been declared inside `%D'",
3025 decl, scope);
3028 /* Return the namespace where the current declaration is declared. */
3030 tree
3031 current_decl_namespace (void)
3033 tree result;
3034 /* If we have been pushed into a different namespace, use it. */
3035 if (decl_namespace_list)
3036 return TREE_PURPOSE (decl_namespace_list);
3038 if (current_class_type)
3039 result = decl_namespace (TYPE_STUB_DECL (current_class_type));
3040 else if (current_function_decl)
3041 result = decl_namespace (current_function_decl);
3042 else
3043 result = current_namespace;
3044 return result;
3047 /* Push into the scope of the NAME namespace. If NAME is NULL_TREE, then we
3048 select a name that is unique to this compilation unit. */
3050 void
3051 push_namespace (tree name)
3053 tree d = NULL_TREE;
3054 int need_new = 1;
3055 int implicit_use = 0;
3056 bool anon = !name;
3058 timevar_push (TV_NAME_LOOKUP);
3060 /* We should not get here if the global_namespace is not yet constructed
3061 nor if NAME designates the global namespace: The global scope is
3062 constructed elsewhere. */
3063 my_friendly_assert (global_namespace != NULL && name != global_scope_name,
3064 20030531);
3066 if (anon)
3068 /* The name of anonymous namespace is unique for the translation
3069 unit. */
3070 if (!anonymous_namespace_name)
3071 anonymous_namespace_name = get_file_function_name ('N');
3072 name = anonymous_namespace_name;
3073 d = IDENTIFIER_NAMESPACE_VALUE (name);
3074 if (d)
3075 /* Reopening anonymous namespace. */
3076 need_new = 0;
3077 implicit_use = 1;
3079 else
3081 /* Check whether this is an extended namespace definition. */
3082 d = IDENTIFIER_NAMESPACE_VALUE (name);
3083 if (d != NULL_TREE && TREE_CODE (d) == NAMESPACE_DECL)
3085 need_new = 0;
3086 if (DECL_NAMESPACE_ALIAS (d))
3088 error ("namespace alias `%D' not allowed here, assuming `%D'",
3089 d, DECL_NAMESPACE_ALIAS (d));
3090 d = DECL_NAMESPACE_ALIAS (d);
3095 if (need_new)
3097 /* Make a new namespace, binding the name to it. */
3098 d = build_lang_decl (NAMESPACE_DECL, name, void_type_node);
3099 DECL_CONTEXT (d) = FROB_CONTEXT (current_namespace);
3100 pushdecl (d);
3101 if (anon)
3103 /* Clear DECL_NAME for the benefit of debugging back ends. */
3104 SET_DECL_ASSEMBLER_NAME (d, name);
3105 DECL_NAME (d) = NULL_TREE;
3107 begin_scope (sk_namespace, d);
3109 else
3110 resume_scope (NAMESPACE_LEVEL (d));
3112 if (implicit_use)
3113 do_using_directive (d);
3114 /* Enter the name space. */
3115 current_namespace = d;
3117 timevar_pop (TV_NAME_LOOKUP);
3120 /* Pop from the scope of the current namespace. */
3122 void
3123 pop_namespace (void)
3125 my_friendly_assert (current_namespace != global_namespace, 20010801);
3126 current_namespace = CP_DECL_CONTEXT (current_namespace);
3127 /* The binding level is not popped, as it might be re-opened later. */
3128 leave_scope ();
3131 /* Push into the scope of the namespace NS, even if it is deeply
3132 nested within another namespace. */
3134 void
3135 push_nested_namespace (tree ns)
3137 if (ns == global_namespace)
3138 push_to_top_level ();
3139 else
3141 push_nested_namespace (CP_DECL_CONTEXT (ns));
3142 push_namespace (DECL_NAME (ns));
3146 /* Pop back from the scope of the namespace NS, which was previously
3147 entered with push_nested_namespace. */
3149 void
3150 pop_nested_namespace (tree ns)
3152 timevar_push (TV_NAME_LOOKUP);
3153 while (ns != global_namespace)
3155 pop_namespace ();
3156 ns = CP_DECL_CONTEXT (ns);
3159 pop_from_top_level ();
3160 timevar_pop (TV_NAME_LOOKUP);
3163 /* Temporarily set the namespace for the current declaration. */
3165 void
3166 push_decl_namespace (tree decl)
3168 if (TREE_CODE (decl) != NAMESPACE_DECL)
3169 decl = decl_namespace (decl);
3170 decl_namespace_list = tree_cons (ORIGINAL_NAMESPACE (decl),
3171 NULL_TREE, decl_namespace_list);
3174 /* [namespace.memdef]/2 */
3176 void
3177 pop_decl_namespace (void)
3179 decl_namespace_list = TREE_CHAIN (decl_namespace_list);
3182 /* Return the namespace that is the common ancestor
3183 of two given namespaces. */
3185 static tree
3186 namespace_ancestor (tree ns1, tree ns2)
3188 timevar_push (TV_NAME_LOOKUP);
3189 if (is_ancestor (ns1, ns2))
3190 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, ns1);
3191 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP,
3192 namespace_ancestor (CP_DECL_CONTEXT (ns1), ns2));
3195 /* Process a namespace-alias declaration. */
3197 void
3198 do_namespace_alias (tree alias, tree namespace)
3200 if (TREE_CODE (namespace) != NAMESPACE_DECL)
3202 /* The parser did not find it, so it's not there. */
3203 error ("unknown namespace `%D'", namespace);
3204 return;
3207 namespace = ORIGINAL_NAMESPACE (namespace);
3209 /* Build the alias. */
3210 alias = build_lang_decl (NAMESPACE_DECL, alias, void_type_node);
3211 DECL_NAMESPACE_ALIAS (alias) = namespace;
3212 DECL_EXTERNAL (alias) = 1;
3213 pushdecl (alias);
3215 /* Emit debug info for namespace alias. */
3216 (*debug_hooks->global_decl) (alias);
3219 /* Like pushdecl, only it places X in the current namespace,
3220 if appropriate. */
3222 tree
3223 pushdecl_namespace_level (tree x)
3225 struct cp_binding_level *b = current_binding_level;
3226 tree t;
3228 timevar_push (TV_NAME_LOOKUP);
3229 t = pushdecl_with_scope (x, NAMESPACE_LEVEL (current_namespace));
3231 /* Now, the type_shadowed stack may screw us. Munge it so it does
3232 what we want. */
3233 if (TREE_CODE (x) == TYPE_DECL)
3235 tree name = DECL_NAME (x);
3236 tree newval;
3237 tree *ptr = (tree *)0;
3238 for (; !global_scope_p (b); b = b->level_chain)
3240 tree shadowed = b->type_shadowed;
3241 for (; shadowed; shadowed = TREE_CHAIN (shadowed))
3242 if (TREE_PURPOSE (shadowed) == name)
3244 ptr = &TREE_VALUE (shadowed);
3245 /* Can't break out of the loop here because sometimes
3246 a binding level will have duplicate bindings for
3247 PT names. It's gross, but I haven't time to fix it. */
3250 newval = TREE_TYPE (x);
3251 if (ptr == (tree *)0)
3253 /* @@ This shouldn't be needed. My test case "zstring.cc" trips
3254 up here if this is changed to an assertion. --KR */
3255 SET_IDENTIFIER_TYPE_VALUE (name, x);
3257 else
3259 *ptr = newval;
3262 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
3265 /* Insert USED into the using list of USER. Set INDIRECT_flag if this
3266 directive is not directly from the source. Also find the common
3267 ancestor and let our users know about the new namespace */
3268 static void
3269 add_using_namespace (tree user, tree used, bool indirect)
3271 tree t;
3272 timevar_push (TV_NAME_LOOKUP);
3273 /* Using oneself is a no-op. */
3274 if (user == used)
3276 timevar_pop (TV_NAME_LOOKUP);
3277 return;
3279 my_friendly_assert (TREE_CODE (user) == NAMESPACE_DECL, 380);
3280 my_friendly_assert (TREE_CODE (used) == NAMESPACE_DECL, 380);
3281 /* Check if we already have this. */
3282 t = purpose_member (used, DECL_NAMESPACE_USING (user));
3283 if (t != NULL_TREE)
3285 if (!indirect)
3286 /* Promote to direct usage. */
3287 TREE_INDIRECT_USING (t) = 0;
3288 timevar_pop (TV_NAME_LOOKUP);
3289 return;
3292 /* Add used to the user's using list. */
3293 DECL_NAMESPACE_USING (user)
3294 = tree_cons (used, namespace_ancestor (user, used),
3295 DECL_NAMESPACE_USING (user));
3297 TREE_INDIRECT_USING (DECL_NAMESPACE_USING (user)) = indirect;
3299 /* Add user to the used's users list. */
3300 DECL_NAMESPACE_USERS (used)
3301 = tree_cons (user, 0, DECL_NAMESPACE_USERS (used));
3303 /* Recursively add all namespaces used. */
3304 for (t = DECL_NAMESPACE_USING (used); t; t = TREE_CHAIN (t))
3305 /* indirect usage */
3306 add_using_namespace (user, TREE_PURPOSE (t), 1);
3308 /* Tell everyone using us about the new used namespaces. */
3309 for (t = DECL_NAMESPACE_USERS (user); t; t = TREE_CHAIN (t))
3310 add_using_namespace (TREE_PURPOSE (t), used, 1);
3311 timevar_pop (TV_NAME_LOOKUP);
3314 /* Process a using-declaration not appearing in class or local scope. */
3316 void
3317 do_toplevel_using_decl (tree decl, tree scope, tree name)
3319 tree oldval, oldtype, newval, newtype;
3320 tree orig_decl = decl;
3321 cxx_binding *binding;
3323 decl = validate_nonmember_using_decl (decl, scope, name);
3324 if (decl == NULL_TREE)
3325 return;
3327 binding = binding_for_name (NAMESPACE_LEVEL (current_namespace), name);
3329 oldval = binding->value;
3330 oldtype = binding->type;
3332 do_nonmember_using_decl (scope, name, oldval, oldtype, &newval, &newtype);
3334 /* Emit debug info. */
3335 if (!processing_template_decl)
3336 cp_emit_debug_info_for_using (orig_decl, current_namespace);
3338 /* Copy declarations found. */
3339 if (newval)
3340 binding->value = newval;
3341 if (newtype)
3342 binding->type = newtype;
3343 return;
3346 /* Process a using-directive. */
3348 void
3349 do_using_directive (tree namespace)
3351 tree context = NULL_TREE;
3353 if (building_stmt_tree ())
3354 add_stmt (build_stmt (USING_STMT, namespace));
3356 /* using namespace A::B::C; */
3357 if (TREE_CODE (namespace) == SCOPE_REF)
3358 namespace = TREE_OPERAND (namespace, 1);
3359 if (TREE_CODE (namespace) == IDENTIFIER_NODE)
3361 /* Lookup in lexer did not find a namespace. */
3362 if (!processing_template_decl)
3363 error ("namespace `%T' undeclared", namespace);
3364 return;
3366 if (TREE_CODE (namespace) != NAMESPACE_DECL)
3368 if (!processing_template_decl)
3369 error ("`%T' is not a namespace", namespace);
3370 return;
3372 namespace = ORIGINAL_NAMESPACE (namespace);
3373 if (!toplevel_bindings_p ())
3375 push_using_directive (namespace);
3376 context = current_scope ();
3378 else
3380 /* direct usage */
3381 add_using_namespace (current_namespace, namespace, 0);
3382 if (current_namespace != global_namespace)
3383 context = current_namespace;
3386 /* Emit debugging info. */
3387 if (!processing_template_decl)
3388 (*debug_hooks->imported_module_or_decl) (namespace, context);
3391 /* Deal with a using-directive seen by the parser. Currently we only
3392 handle attributes here, since they cannot appear inside a template. */
3394 void
3395 parse_using_directive (tree namespace, tree attribs)
3397 tree a;
3399 do_using_directive (namespace);
3401 for (a = attribs; a; a = TREE_CHAIN (a))
3403 tree name = TREE_PURPOSE (a);
3404 if (is_attribute_p ("strong", name))
3406 if (!toplevel_bindings_p ())
3407 error ("strong using only meaningful at namespace scope");
3408 else
3409 DECL_NAMESPACE_ASSOCIATIONS (namespace)
3410 = tree_cons (current_namespace, 0,
3411 DECL_NAMESPACE_ASSOCIATIONS (namespace));
3413 else
3414 warning ("`%D' attribute directive ignored", name);
3418 /* Like pushdecl, only it places X in the global scope if appropriate.
3419 Calls cp_finish_decl to register the variable, initializing it with
3420 *INIT, if INIT is non-NULL. */
3422 static tree
3423 pushdecl_top_level_1 (tree x, tree *init)
3425 timevar_push (TV_NAME_LOOKUP);
3426 push_to_top_level ();
3427 x = pushdecl_namespace_level (x);
3428 if (init)
3429 cp_finish_decl (x, *init, NULL_TREE, 0);
3430 pop_from_top_level ();
3431 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
3434 /* Like pushdecl, only it places X in the global scope if appropriate. */
3436 tree
3437 pushdecl_top_level (tree x)
3439 return pushdecl_top_level_1 (x, NULL);
3442 /* Like pushdecl, only it places X in the global scope if
3443 appropriate. Calls cp_finish_decl to register the variable,
3444 initializing it with INIT. */
3446 tree
3447 pushdecl_top_level_and_finish (tree x, tree init)
3449 return pushdecl_top_level_1 (x, &init);
3452 /* Combines two sets of overloaded functions into an OVERLOAD chain, removing
3453 duplicates. The first list becomes the tail of the result.
3455 The algorithm is O(n^2). We could get this down to O(n log n) by
3456 doing a sort on the addresses of the functions, if that becomes
3457 necessary. */
3459 static tree
3460 merge_functions (tree s1, tree s2)
3462 for (; s2; s2 = OVL_NEXT (s2))
3464 tree fn2 = OVL_CURRENT (s2);
3465 tree fns1;
3467 for (fns1 = s1; fns1; fns1 = OVL_NEXT (fns1))
3469 tree fn1 = OVL_CURRENT (fns1);
3471 /* If the function from S2 is already in S1, there is no
3472 need to add it again. For `extern "C"' functions, we
3473 might have two FUNCTION_DECLs for the same function, in
3474 different namespaces; again, we only need one of them. */
3475 if (fn1 == fn2
3476 || (DECL_EXTERN_C_P (fn1) && DECL_EXTERN_C_P (fn2)
3477 && DECL_NAME (fn1) == DECL_NAME (fn2)))
3478 break;
3481 /* If we exhausted all of the functions in S1, FN2 is new. */
3482 if (!fns1)
3483 s1 = build_overload (fn2, s1);
3485 return s1;
3488 /* This should return an error not all definitions define functions.
3489 It is not an error if we find two functions with exactly the
3490 same signature, only if these are selected in overload resolution.
3491 old is the current set of bindings, new the freshly-found binding.
3492 XXX Do we want to give *all* candidates in case of ambiguity?
3493 XXX In what way should I treat extern declarations?
3494 XXX I don't want to repeat the entire duplicate_decls here */
3496 static cxx_binding *
3497 ambiguous_decl (tree name, cxx_binding *old, cxx_binding *new, int flags)
3499 tree val, type;
3500 my_friendly_assert (old != NULL, 393);
3501 /* Copy the value. */
3502 val = new->value;
3503 if (val)
3504 switch (TREE_CODE (val))
3506 case TEMPLATE_DECL:
3507 /* If we expect types or namespaces, and not templates,
3508 or this is not a template class. */
3509 if (LOOKUP_QUALIFIERS_ONLY (flags)
3510 && !DECL_CLASS_TEMPLATE_P (val))
3511 val = NULL_TREE;
3512 break;
3513 case TYPE_DECL:
3514 if (LOOKUP_NAMESPACES_ONLY (flags))
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) || DECL_ANTICIPATED (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 /* Some declarations are functions, some are not. */
3540 if (flags & LOOKUP_COMPLAIN)
3542 /* If we've already given this error for this lookup,
3543 old->value is error_mark_node, so let's not
3544 repeat ourselves. */
3545 if (old->value != error_mark_node)
3547 error ("use of `%D' is ambiguous", name);
3548 cp_error_at (" first declared as `%#D' here",
3549 old->value);
3551 cp_error_at (" also declared as `%#D' here", val);
3553 old->value = error_mark_node;
3556 /* ... and copy the type. */
3557 type = new->type;
3558 if (LOOKUP_NAMESPACES_ONLY (flags))
3559 type = NULL_TREE;
3560 if (!old->type)
3561 old->type = type;
3562 else if (type && old->type != type)
3564 if (flags & LOOKUP_COMPLAIN)
3566 error ("`%D' denotes an ambiguous type",name);
3567 error ("%J first type here", TYPE_MAIN_DECL (old->type));
3568 error ("%J other type here", TYPE_MAIN_DECL (type));
3571 return old;
3574 /* Return the declarations that are members of the namespace NS. */
3576 tree
3577 cp_namespace_decls (tree ns)
3579 return NAMESPACE_LEVEL (ns)->names;
3582 /* Combine prefer_type and namespaces_only into flags. */
3584 static int
3585 lookup_flags (int prefer_type, int namespaces_only)
3587 if (namespaces_only)
3588 return LOOKUP_PREFER_NAMESPACES;
3589 if (prefer_type > 1)
3590 return LOOKUP_PREFER_TYPES;
3591 if (prefer_type > 0)
3592 return LOOKUP_PREFER_BOTH;
3593 return 0;
3596 /* Given a lookup that returned VAL, use FLAGS to decide if we want to
3597 ignore it or not. Subroutine of lookup_name_real. */
3599 static tree
3600 qualify_lookup (tree val, int flags)
3602 if (val == NULL_TREE)
3603 return val;
3604 if ((flags & LOOKUP_PREFER_NAMESPACES) && TREE_CODE (val) == NAMESPACE_DECL)
3605 return val;
3606 if ((flags & LOOKUP_PREFER_TYPES)
3607 && (TREE_CODE (val) == TYPE_DECL || TREE_CODE (val) == TEMPLATE_DECL))
3608 return val;
3609 if (flags & (LOOKUP_PREFER_NAMESPACES | LOOKUP_PREFER_TYPES))
3610 return NULL_TREE;
3611 return val;
3614 /* Look up NAME in the NAMESPACE. */
3616 tree
3617 lookup_namespace_name (tree namespace, tree name)
3619 tree val;
3620 tree template_id = NULL_TREE;
3621 cxx_binding binding;
3623 timevar_push (TV_NAME_LOOKUP);
3624 my_friendly_assert (TREE_CODE (namespace) == NAMESPACE_DECL, 370);
3626 if (TREE_CODE (name) == NAMESPACE_DECL)
3627 /* This happens for A::B<int> when B is a namespace. */
3628 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, name);
3629 else if (TREE_CODE (name) == TEMPLATE_DECL)
3631 /* This happens for A::B where B is a template, and there are no
3632 template arguments. */
3633 error ("invalid use of `%D'", name);
3634 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
3637 namespace = ORIGINAL_NAMESPACE (namespace);
3639 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
3641 template_id = name;
3642 name = TREE_OPERAND (name, 0);
3643 if (TREE_CODE (name) == OVERLOAD)
3644 name = DECL_NAME (OVL_CURRENT (name));
3645 else if (DECL_P (name))
3646 name = DECL_NAME (name);
3649 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 373);
3651 cxx_binding_clear (&binding);
3652 if (!qualified_lookup_using_namespace (name, namespace, &binding, 0))
3653 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
3655 if (binding.value)
3657 val = binding.value;
3659 if (template_id)
3661 if (DECL_CLASS_TEMPLATE_P (val))
3662 val = lookup_template_class (val,
3663 TREE_OPERAND (template_id, 1),
3664 /*in_decl=*/NULL_TREE,
3665 /*context=*/NULL_TREE,
3666 /*entering_scope=*/0,
3667 tf_error | tf_warning);
3668 else if (DECL_FUNCTION_TEMPLATE_P (val)
3669 || TREE_CODE (val) == OVERLOAD)
3670 val = lookup_template_function (val,
3671 TREE_OPERAND (template_id, 1));
3672 else
3674 error ("`%D::%D' is not a template",
3675 namespace, name);
3676 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
3680 /* If we have a single function from a using decl, pull it out. */
3681 if (TREE_CODE (val) == OVERLOAD && ! really_overloaded_fn (val))
3682 val = OVL_FUNCTION (val);
3684 /* Ignore built-in functions that haven't been prototyped yet. */
3685 if (!val || !DECL_P(val)
3686 || !DECL_LANG_SPECIFIC(val)
3687 || !DECL_ANTICIPATED (val))
3688 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
3691 error ("`%D' undeclared in namespace `%D'", name, namespace);
3692 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
3695 /* Select the right _DECL from multiple choices. */
3697 static tree
3698 select_decl (cxx_binding *binding, int flags)
3700 tree val;
3701 val = binding->value;
3703 timevar_push (TV_NAME_LOOKUP);
3704 if (LOOKUP_NAMESPACES_ONLY (flags))
3706 /* We are not interested in types. */
3707 if (val && TREE_CODE (val) == NAMESPACE_DECL)
3708 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
3709 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
3712 /* If looking for a type, or if there is no non-type binding, select
3713 the value binding. */
3714 if (binding->type && (!val || (flags & LOOKUP_PREFER_TYPES)))
3715 val = binding->type;
3716 /* Don't return non-types if we really prefer types. */
3717 else if (val && LOOKUP_TYPES_ONLY (flags) && TREE_CODE (val) != TYPE_DECL
3718 && (TREE_CODE (val) != TEMPLATE_DECL
3719 || !DECL_CLASS_TEMPLATE_P (val)))
3720 val = NULL_TREE;
3722 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
3725 /* Unscoped lookup of a global: iterate over current namespaces,
3726 considering using-directives. */
3728 static tree
3729 unqualified_namespace_lookup (tree name, int flags)
3731 tree initial = current_decl_namespace ();
3732 tree scope = initial;
3733 tree siter;
3734 struct cp_binding_level *level;
3735 tree val = NULL_TREE;
3736 cxx_binding binding;
3738 timevar_push (TV_NAME_LOOKUP);
3739 cxx_binding_clear (&binding);
3741 for (; !val; scope = CP_DECL_CONTEXT (scope))
3743 cxx_binding *b =
3744 cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (scope), name);
3746 /* Ignore anticipated built-in functions. */
3747 if (b && b->value && DECL_P (b->value)
3748 && DECL_LANG_SPECIFIC (b->value) && DECL_ANTICIPATED (b->value))
3749 /* Keep binding cleared. */;
3750 else if (b)
3752 /* Initialize binding for this context. */
3753 binding.value = b->value;
3754 binding.type = b->type;
3757 /* Add all _DECLs seen through local using-directives. */
3758 for (level = current_binding_level;
3759 level->kind != sk_namespace;
3760 level = level->level_chain)
3761 if (!lookup_using_namespace (name, &binding, level->using_directives,
3762 scope, flags))
3763 /* Give up because of error. */
3764 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
3766 /* Add all _DECLs seen through global using-directives. */
3767 /* XXX local and global using lists should work equally. */
3768 siter = initial;
3769 while (1)
3771 if (!lookup_using_namespace (name, &binding,
3772 DECL_NAMESPACE_USING (siter),
3773 scope, flags))
3774 /* Give up because of error. */
3775 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
3776 if (siter == scope) break;
3777 siter = CP_DECL_CONTEXT (siter);
3780 val = select_decl (&binding, flags);
3781 if (scope == global_namespace)
3782 break;
3784 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
3787 /* Look up NAME (an IDENTIFIER_NODE) in SCOPE (either a NAMESPACE_DECL
3788 or a class TYPE). If IS_TYPE_P is TRUE, then ignore non-type
3789 bindings.
3791 Returns a DECL (or OVERLOAD, or BASELINK) representing the
3792 declaration found. If no suitable declaration can be found,
3793 ERROR_MARK_NODE is returned. Iif COMPLAIN is true and SCOPE is
3794 neither a class-type nor a namespace a diagnostic is issued. */
3796 tree
3797 lookup_qualified_name (tree scope, tree name, bool is_type_p, bool complain)
3799 int flags = 0;
3801 if (TREE_CODE (scope) == NAMESPACE_DECL)
3803 cxx_binding binding;
3805 cxx_binding_clear (&binding);
3806 flags |= LOOKUP_COMPLAIN;
3807 if (is_type_p)
3808 flags |= LOOKUP_PREFER_TYPES;
3809 if (qualified_lookup_using_namespace (name, scope, &binding, flags))
3810 return select_decl (&binding, flags);
3812 else if (is_aggr_type (scope, complain))
3814 tree t;
3815 t = lookup_member (scope, name, 0, is_type_p);
3816 if (t)
3817 return t;
3820 return error_mark_node;
3823 /* Subroutine of unqualified_namespace_lookup:
3824 Add the bindings of NAME in used namespaces to VAL.
3825 We are currently looking for names in namespace SCOPE, so we
3826 look through USINGS for using-directives of namespaces
3827 which have SCOPE as a common ancestor with the current scope.
3828 Returns false on errors. */
3830 static bool
3831 lookup_using_namespace (tree name, cxx_binding *val, tree usings, tree scope,
3832 int flags)
3834 tree iter;
3835 timevar_push (TV_NAME_LOOKUP);
3836 /* Iterate over all used namespaces in current, searching for using
3837 directives of scope. */
3838 for (iter = usings; iter; iter = TREE_CHAIN (iter))
3839 if (TREE_VALUE (iter) == scope)
3841 tree used = ORIGINAL_NAMESPACE (TREE_PURPOSE (iter));
3842 cxx_binding *val1 =
3843 cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (used), name);
3844 /* Resolve ambiguities. */
3845 if (val1)
3846 val = ambiguous_decl (name, val, val1, flags);
3848 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val->value != error_mark_node);
3851 /* [namespace.qual]
3852 Accepts the NAME to lookup and its qualifying SCOPE.
3853 Returns the name/type pair found into the cxx_binding *RESULT,
3854 or false on error. */
3856 static bool
3857 qualified_lookup_using_namespace (tree name, tree scope, cxx_binding *result,
3858 int flags)
3860 /* Maintain a list of namespaces visited... */
3861 tree seen = NULL_TREE;
3862 /* ... and a list of namespace yet to see. */
3863 tree todo = NULL_TREE;
3864 tree todo_maybe = NULL_TREE;
3865 tree usings;
3866 timevar_push (TV_NAME_LOOKUP);
3867 /* Look through namespace aliases. */
3868 scope = ORIGINAL_NAMESPACE (scope);
3869 while (scope && result->value != error_mark_node)
3871 cxx_binding *binding =
3872 cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (scope), name);
3873 seen = tree_cons (scope, NULL_TREE, seen);
3874 if (binding)
3875 result = ambiguous_decl (name, result, binding, flags);
3877 /* Consider strong using directives always, and non-strong ones
3878 if we haven't found a binding yet. ??? Shouldn't we consider
3879 non-strong ones if the initial RESULT is non-NULL, but the
3880 binding in the given namespace is? */
3881 for (usings = DECL_NAMESPACE_USING (scope); usings;
3882 usings = TREE_CHAIN (usings))
3883 /* If this was a real directive, and we have not seen it. */
3884 if (!TREE_INDIRECT_USING (usings))
3886 /* Try to avoid queuing the same namespace more than once,
3887 the exception being when a namespace was already
3888 enqueued for todo_maybe and then a strong using is
3889 found for it. We could try to remove it from
3890 todo_maybe, but it's probably not worth the effort. */
3891 if (is_associated_namespace (scope, TREE_PURPOSE (usings))
3892 && !purpose_member (TREE_PURPOSE (usings), seen)
3893 && !purpose_member (TREE_PURPOSE (usings), todo))
3894 todo = tree_cons (TREE_PURPOSE (usings), NULL_TREE, todo);
3895 else if ((!result->value && !result->type)
3896 && !purpose_member (TREE_PURPOSE (usings), seen)
3897 && !purpose_member (TREE_PURPOSE (usings), todo)
3898 && !purpose_member (TREE_PURPOSE (usings), todo_maybe))
3899 todo_maybe = tree_cons (TREE_PURPOSE (usings), NULL_TREE,
3900 todo_maybe);
3902 if (todo)
3904 scope = TREE_PURPOSE (todo);
3905 todo = TREE_CHAIN (todo);
3907 else if (todo_maybe
3908 && (!result->value && !result->type))
3910 scope = TREE_PURPOSE (todo_maybe);
3911 todo = TREE_CHAIN (todo_maybe);
3912 todo_maybe = NULL_TREE;
3914 else
3915 scope = NULL_TREE; /* If there never was a todo list. */
3917 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, result->value != error_mark_node);
3920 /* Look up NAME in the current binding level and its superiors in the
3921 namespace of variables, functions and typedefs. Return a ..._DECL
3922 node of some kind representing its definition if there is only one
3923 such declaration, or return a TREE_LIST with all the overloaded
3924 definitions if there are many, or return 0 if it is undefined.
3926 If PREFER_TYPE is > 0, we prefer TYPE_DECLs or namespaces.
3927 If PREFER_TYPE is > 1, we reject non-type decls (e.g. namespaces).
3928 Otherwise we prefer non-TYPE_DECLs.
3930 If NONCLASS is nonzero, we don't look for the NAME in class scope,
3931 using IDENTIFIER_CLASS_VALUE. */
3933 tree
3934 lookup_name_real (tree name, int prefer_type, int nonclass,
3935 int namespaces_only, int flags)
3937 cxx_binding *iter;
3938 tree val = NULL_TREE;
3940 timevar_push (TV_NAME_LOOKUP);
3941 /* Conversion operators are handled specially because ordinary
3942 unqualified name lookup will not find template conversion
3943 operators. */
3944 if (IDENTIFIER_TYPENAME_P (name))
3946 struct cp_binding_level *level;
3948 for (level = current_binding_level;
3949 level && level->kind != sk_namespace;
3950 level = level->level_chain)
3952 tree class_type;
3953 tree operators;
3955 /* A conversion operator can only be declared in a class
3956 scope. */
3957 if (level->kind != sk_class)
3958 continue;
3960 /* Lookup the conversion operator in the class. */
3961 class_type = level->this_entity;
3962 operators = lookup_fnfields (class_type, name, /*protect=*/0);
3963 if (operators)
3964 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, operators);
3967 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
3970 flags |= lookup_flags (prefer_type, namespaces_only);
3972 /* First, look in non-namespace scopes. */
3974 if (current_class_type == NULL_TREE)
3975 nonclass = 1;
3977 for (iter = IDENTIFIER_BINDING (name); iter; iter = iter->previous)
3979 tree binding;
3981 if (!LOCAL_BINDING_P (iter) && nonclass)
3982 /* We're not looking for class-scoped bindings, so keep going. */
3983 continue;
3985 /* If this is the kind of thing we're looking for, we're done. */
3986 if (qualify_lookup (iter->value, flags))
3987 binding = iter->value;
3988 else if ((flags & LOOKUP_PREFER_TYPES)
3989 && qualify_lookup (iter->type, flags))
3990 binding = iter->type;
3991 else
3992 binding = NULL_TREE;
3994 if (binding)
3996 val = binding;
3997 break;
4001 /* Now lookup in namespace scopes. */
4002 if (!val)
4004 tree t = unqualified_namespace_lookup (name, flags);
4005 if (t)
4006 val = t;
4009 if (val)
4011 /* If we have a single function from a using decl, pull it out. */
4012 if (TREE_CODE (val) == OVERLOAD && ! really_overloaded_fn (val))
4013 val = OVL_FUNCTION (val);
4016 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
4019 tree
4020 lookup_name_nonclass (tree name)
4022 return lookup_name_real (name, 0, 1, 0, LOOKUP_COMPLAIN);
4025 tree
4026 lookup_function_nonclass (tree name, tree args)
4028 return lookup_arg_dependent (name, lookup_name_nonclass (name), args);
4031 tree
4032 lookup_name (tree name, int prefer_type)
4034 return lookup_name_real (name, prefer_type, 0, 0, LOOKUP_COMPLAIN);
4037 /* Similar to `lookup_name' but look only in the innermost non-class
4038 binding level. */
4040 static tree
4041 lookup_name_current_level (tree name)
4043 struct cp_binding_level *b;
4044 tree t = NULL_TREE;
4046 timevar_push (TV_NAME_LOOKUP);
4047 b = innermost_nonclass_level ();
4049 if (b->kind == sk_namespace)
4051 t = IDENTIFIER_NAMESPACE_VALUE (name);
4053 /* extern "C" function() */
4054 if (t != NULL_TREE && TREE_CODE (t) == TREE_LIST)
4055 t = TREE_VALUE (t);
4057 else if (IDENTIFIER_BINDING (name)
4058 && LOCAL_BINDING_P (IDENTIFIER_BINDING (name)))
4060 while (1)
4062 if (IDENTIFIER_BINDING (name)->scope == b)
4063 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, IDENTIFIER_VALUE (name));
4065 if (b->kind == sk_cleanup)
4066 b = b->level_chain;
4067 else
4068 break;
4072 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
4075 /* Like lookup_name_current_level, but for types. */
4077 static tree
4078 lookup_type_current_level (tree name)
4080 tree t = NULL_TREE;
4082 timevar_push (TV_NAME_LOOKUP);
4083 my_friendly_assert (current_binding_level->kind != sk_namespace,
4084 980716);
4086 if (REAL_IDENTIFIER_TYPE_VALUE (name) != NULL_TREE
4087 && REAL_IDENTIFIER_TYPE_VALUE (name) != global_type_node)
4089 struct cp_binding_level *b = current_binding_level;
4090 while (1)
4092 if (purpose_member (name, b->type_shadowed))
4093 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP,
4094 REAL_IDENTIFIER_TYPE_VALUE (name));
4095 if (b->kind == sk_cleanup)
4096 b = b->level_chain;
4097 else
4098 break;
4102 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
4105 /* [basic.lookup.koenig] */
4106 /* A nonzero return value in the functions below indicates an error. */
4108 struct arg_lookup
4110 tree name;
4111 tree namespaces;
4112 tree classes;
4113 tree functions;
4116 static bool arg_assoc (struct arg_lookup*, tree);
4117 static bool arg_assoc_args (struct arg_lookup*, tree);
4118 static bool arg_assoc_type (struct arg_lookup*, tree);
4119 static bool add_function (struct arg_lookup *, tree);
4120 static bool arg_assoc_namespace (struct arg_lookup *, tree);
4121 static bool arg_assoc_class (struct arg_lookup *, tree);
4122 static bool arg_assoc_template_arg (struct arg_lookup*, tree);
4124 /* Add a function to the lookup structure.
4125 Returns true on error. */
4127 static bool
4128 add_function (struct arg_lookup *k, tree fn)
4130 /* We used to check here to see if the function was already in the list,
4131 but that's O(n^2), which is just too expensive for function lookup.
4132 Now we deal with the occasional duplicate in joust. In doing this, we
4133 assume that the number of duplicates will be small compared to the
4134 total number of functions being compared, which should usually be the
4135 case. */
4137 /* We must find only functions, or exactly one non-function. */
4138 if (!k->functions)
4139 k->functions = fn;
4140 else if (fn == k->functions)
4142 else if (is_overloaded_fn (k->functions) && is_overloaded_fn (fn))
4143 k->functions = build_overload (fn, k->functions);
4144 else
4146 tree f1 = OVL_CURRENT (k->functions);
4147 tree f2 = fn;
4148 if (is_overloaded_fn (f1))
4150 fn = f1; f1 = f2; f2 = fn;
4152 cp_error_at ("`%D' is not a function,", f1);
4153 cp_error_at (" conflict with `%D'", f2);
4154 error (" in call to `%D'", k->name);
4155 return true;
4158 return false;
4161 /* Returns true iff CURRENT has declared itself to be an associated
4162 namespace of SCOPE via a strong using-directive (or transitive chain
4163 thereof). Both are namespaces. */
4165 bool
4166 is_associated_namespace (tree current, tree scope)
4168 tree seen = NULL_TREE;
4169 tree todo = NULL_TREE;
4170 tree t;
4171 while (1)
4173 if (scope == current)
4174 return true;
4175 seen = tree_cons (scope, NULL_TREE, seen);
4176 for (t = DECL_NAMESPACE_ASSOCIATIONS (scope); t; t = TREE_CHAIN (t))
4177 if (!purpose_member (TREE_PURPOSE (t), seen))
4178 todo = tree_cons (TREE_PURPOSE (t), NULL_TREE, todo);
4179 if (todo)
4181 scope = TREE_PURPOSE (todo);
4182 todo = TREE_CHAIN (todo);
4184 else
4185 return false;
4189 /* Add functions of a namespace to the lookup structure.
4190 Returns true on error. */
4192 static bool
4193 arg_assoc_namespace (struct arg_lookup *k, tree scope)
4195 tree value;
4197 if (purpose_member (scope, k->namespaces))
4198 return 0;
4199 k->namespaces = tree_cons (scope, NULL_TREE, k->namespaces);
4201 /* Check out our super-users. */
4202 for (value = DECL_NAMESPACE_ASSOCIATIONS (scope); value;
4203 value = TREE_CHAIN (value))
4204 if (arg_assoc_namespace (k, TREE_PURPOSE (value)))
4205 return true;
4207 value = namespace_binding (k->name, scope);
4208 if (!value)
4209 return false;
4211 for (; value; value = OVL_NEXT (value))
4212 if (add_function (k, OVL_CURRENT (value)))
4213 return true;
4215 return false;
4218 /* Adds everything associated with a template argument to the lookup
4219 structure. Returns true on error. */
4221 static bool
4222 arg_assoc_template_arg (struct arg_lookup *k, tree arg)
4224 /* [basic.lookup.koenig]
4226 If T is a template-id, its associated namespaces and classes are
4227 ... the namespaces and classes associated with the types of the
4228 template arguments provided for template type parameters
4229 (excluding template template parameters); the namespaces in which
4230 any template template arguments are defined; and the classes in
4231 which any member templates used as template template arguments
4232 are defined. [Note: non-type template arguments do not
4233 contribute to the set of associated namespaces. ] */
4235 /* Consider first template template arguments. */
4236 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
4237 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
4238 return false;
4239 else if (TREE_CODE (arg) == TEMPLATE_DECL)
4241 tree ctx = CP_DECL_CONTEXT (arg);
4243 /* It's not a member template. */
4244 if (TREE_CODE (ctx) == NAMESPACE_DECL)
4245 return arg_assoc_namespace (k, ctx);
4246 /* Otherwise, it must be member template. */
4247 else
4248 return arg_assoc_class (k, ctx);
4250 /* It's not a template template argument, but it is a type template
4251 argument. */
4252 else if (TYPE_P (arg))
4253 return arg_assoc_type (k, arg);
4254 /* It's a non-type template argument. */
4255 else
4256 return false;
4259 /* Adds everything associated with class to the lookup structure.
4260 Returns true on error. */
4262 static bool
4263 arg_assoc_class (struct arg_lookup *k, tree type)
4265 tree list, friends, context;
4266 int i;
4268 /* Backend build structures, such as __builtin_va_list, aren't
4269 affected by all this. */
4270 if (!CLASS_TYPE_P (type))
4271 return false;
4273 if (purpose_member (type, k->classes))
4274 return false;
4275 k->classes = tree_cons (type, NULL_TREE, k->classes);
4277 context = decl_namespace (TYPE_MAIN_DECL (type));
4278 if (arg_assoc_namespace (k, context))
4279 return true;
4281 /* Process baseclasses. */
4282 for (i = 0; i < CLASSTYPE_N_BASECLASSES (type); i++)
4283 if (arg_assoc_class (k, TYPE_BINFO_BASETYPE (type, i)))
4284 return true;
4286 /* Process friends. */
4287 for (list = DECL_FRIENDLIST (TYPE_MAIN_DECL (type)); list;
4288 list = TREE_CHAIN (list))
4289 if (k->name == FRIEND_NAME (list))
4290 for (friends = FRIEND_DECLS (list); friends;
4291 friends = TREE_CHAIN (friends))
4292 /* Only interested in global functions with potentially hidden
4293 (i.e. unqualified) declarations. */
4294 if (CP_DECL_CONTEXT (TREE_VALUE (friends)) == context)
4295 if (add_function (k, TREE_VALUE (friends)))
4296 return true;
4298 /* Process template arguments. */
4299 if (CLASSTYPE_TEMPLATE_INFO (type)
4300 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
4302 list = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
4303 for (i = 0; i < TREE_VEC_LENGTH (list); ++i)
4304 arg_assoc_template_arg (k, TREE_VEC_ELT (list, i));
4307 return false;
4310 /* Adds everything associated with a given type.
4311 Returns 1 on error. */
4313 static bool
4314 arg_assoc_type (struct arg_lookup *k, tree type)
4316 /* As we do not get the type of non-type dependent expressions
4317 right, we can end up with such things without a type. */
4318 if (!type)
4319 return false;
4321 if (TYPE_PTRMEM_P (type))
4323 /* Pointer to member: associate class type and value type. */
4324 if (arg_assoc_type (k, TYPE_PTRMEM_CLASS_TYPE (type)))
4325 return true;
4326 return arg_assoc_type (k, TYPE_PTRMEM_POINTED_TO_TYPE (type));
4328 else switch (TREE_CODE (type))
4330 case ERROR_MARK:
4331 return false;
4332 case VOID_TYPE:
4333 case INTEGER_TYPE:
4334 case REAL_TYPE:
4335 case COMPLEX_TYPE:
4336 case VECTOR_TYPE:
4337 case CHAR_TYPE:
4338 case BOOLEAN_TYPE:
4339 return false;
4340 case RECORD_TYPE:
4341 if (TYPE_PTRMEMFUNC_P (type))
4342 return arg_assoc_type (k, TYPE_PTRMEMFUNC_FN_TYPE (type));
4343 return arg_assoc_class (k, type);
4344 case POINTER_TYPE:
4345 case REFERENCE_TYPE:
4346 case ARRAY_TYPE:
4347 return arg_assoc_type (k, TREE_TYPE (type));
4348 case UNION_TYPE:
4349 case ENUMERAL_TYPE:
4350 return arg_assoc_namespace (k, decl_namespace (TYPE_MAIN_DECL (type)));
4351 case METHOD_TYPE:
4352 /* The basetype is referenced in the first arg type, so just
4353 fall through. */
4354 case FUNCTION_TYPE:
4355 /* Associate the parameter types. */
4356 if (arg_assoc_args (k, TYPE_ARG_TYPES (type)))
4357 return true;
4358 /* Associate the return type. */
4359 return arg_assoc_type (k, TREE_TYPE (type));
4360 case TEMPLATE_TYPE_PARM:
4361 case BOUND_TEMPLATE_TEMPLATE_PARM:
4362 return false;
4363 case TYPENAME_TYPE:
4364 return false;
4365 case LANG_TYPE:
4366 if (type == unknown_type_node)
4367 return false;
4368 /* else fall through */
4369 default:
4370 abort ();
4372 return false;
4375 /* Adds everything associated with arguments. Returns true on error. */
4377 static bool
4378 arg_assoc_args (struct arg_lookup *k, tree args)
4380 for (; args; args = TREE_CHAIN (args))
4381 if (arg_assoc (k, TREE_VALUE (args)))
4382 return true;
4383 return false;
4386 /* Adds everything associated with a given tree_node. Returns 1 on error. */
4388 static bool
4389 arg_assoc (struct arg_lookup *k, tree n)
4391 if (n == error_mark_node)
4392 return false;
4394 if (TYPE_P (n))
4395 return arg_assoc_type (k, n);
4397 if (! type_unknown_p (n))
4398 return arg_assoc_type (k, TREE_TYPE (n));
4400 if (TREE_CODE (n) == ADDR_EXPR)
4401 n = TREE_OPERAND (n, 0);
4402 if (TREE_CODE (n) == COMPONENT_REF)
4403 n = TREE_OPERAND (n, 1);
4404 if (TREE_CODE (n) == OFFSET_REF)
4405 n = TREE_OPERAND (n, 1);
4406 while (TREE_CODE (n) == TREE_LIST)
4407 n = TREE_VALUE (n);
4408 if (TREE_CODE (n) == BASELINK)
4409 n = BASELINK_FUNCTIONS (n);
4411 if (TREE_CODE (n) == FUNCTION_DECL)
4412 return arg_assoc_type (k, TREE_TYPE (n));
4413 if (TREE_CODE (n) == TEMPLATE_ID_EXPR)
4415 /* [basic.lookup.koenig]
4417 If T is a template-id, its associated namespaces and classes
4418 are the namespace in which the template is defined; for
4419 member templates, the member template's class... */
4420 tree template = TREE_OPERAND (n, 0);
4421 tree args = TREE_OPERAND (n, 1);
4422 tree ctx;
4423 int ix;
4425 if (TREE_CODE (template) == COMPONENT_REF)
4426 template = TREE_OPERAND (template, 1);
4428 /* First, the template. There may actually be more than one if
4429 this is an overloaded function template. But, in that case,
4430 we only need the first; all the functions will be in the same
4431 namespace. */
4432 template = OVL_CURRENT (template);
4434 ctx = CP_DECL_CONTEXT (template);
4436 if (TREE_CODE (ctx) == NAMESPACE_DECL)
4438 if (arg_assoc_namespace (k, ctx) == 1)
4439 return true;
4441 /* It must be a member template. */
4442 else if (arg_assoc_class (k, ctx) == 1)
4443 return true;
4445 /* Now the arguments. */
4446 for (ix = TREE_VEC_LENGTH (args); ix--;)
4447 if (arg_assoc_template_arg (k, TREE_VEC_ELT (args, ix)) == 1)
4448 return true;
4450 else
4452 my_friendly_assert (TREE_CODE (n) == OVERLOAD, 980715);
4454 for (; n; n = OVL_CHAIN (n))
4455 if (arg_assoc_type (k, TREE_TYPE (OVL_FUNCTION (n))))
4456 return true;
4459 return false;
4462 /* Performs Koenig lookup depending on arguments, where fns
4463 are the functions found in normal lookup. */
4465 tree
4466 lookup_arg_dependent (tree name, tree fns, tree args)
4468 struct arg_lookup k;
4469 tree fn = NULL_TREE;
4471 timevar_push (TV_NAME_LOOKUP);
4472 k.name = name;
4473 k.functions = fns;
4474 k.classes = NULL_TREE;
4476 /* We've already looked at some namespaces during normal unqualified
4477 lookup -- but we don't know exactly which ones. If the functions
4478 we found were brought into the current namespace via a using
4479 declaration, we have not really checked the namespace from which
4480 they came. Therefore, we check all namespaces here -- unless the
4481 function we have is from the current namespace. Even then, we
4482 must check all namespaces if the function is a local
4483 declaration; any other declarations present at namespace scope
4484 should be visible during argument-dependent lookup. */
4485 if (fns)
4486 fn = OVL_CURRENT (fns);
4487 if (fn && TREE_CODE (fn) == FUNCTION_DECL
4488 && (CP_DECL_CONTEXT (fn) != current_decl_namespace ()
4489 || DECL_LOCAL_FUNCTION_P (fn)))
4490 k.namespaces = NULL_TREE;
4491 else
4492 /* Setting NAMESPACES is purely an optimization; it prevents
4493 adding functions which are already in FNS. Adding them would
4494 be safe -- "joust" will eliminate the duplicates -- but
4495 wasteful. */
4496 k.namespaces = build_tree_list (current_decl_namespace (), NULL_TREE);
4498 arg_assoc_args (&k, args);
4499 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, k.functions);
4502 /* Add namespace to using_directives. Return NULL_TREE if nothing was
4503 changed (i.e. there was already a directive), or the fresh
4504 TREE_LIST otherwise. */
4506 static tree
4507 push_using_directive (tree used)
4509 tree ud = current_binding_level->using_directives;
4510 tree iter, ancestor;
4512 timevar_push (TV_NAME_LOOKUP);
4513 /* Check if we already have this. */
4514 if (purpose_member (used, ud) != NULL_TREE)
4515 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
4517 ancestor = namespace_ancestor (current_decl_namespace (), used);
4518 ud = current_binding_level->using_directives;
4519 ud = tree_cons (used, ancestor, ud);
4520 current_binding_level->using_directives = ud;
4522 /* Recursively add all namespaces used. */
4523 for (iter = DECL_NAMESPACE_USING (used); iter; iter = TREE_CHAIN (iter))
4524 push_using_directive (TREE_PURPOSE (iter));
4526 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, ud);
4529 /* The type TYPE is being declared. If it is a class template, or a
4530 specialization of a class template, do any processing required and
4531 perform error-checking. If IS_FRIEND is nonzero, this TYPE is
4532 being declared a friend. B is the binding level at which this TYPE
4533 should be bound.
4535 Returns the TYPE_DECL for TYPE, which may have been altered by this
4536 processing. */
4538 static tree
4539 maybe_process_template_type_declaration (tree type, int globalize,
4540 cxx_scope *b)
4542 tree decl = TYPE_NAME (type);
4544 if (processing_template_parmlist)
4545 /* You can't declare a new template type in a template parameter
4546 list. But, you can declare a non-template type:
4548 template <class A*> struct S;
4550 is a forward-declaration of `A'. */
4552 else
4554 maybe_check_template_type (type);
4556 my_friendly_assert (IS_AGGR_TYPE (type)
4557 || TREE_CODE (type) == ENUMERAL_TYPE, 0);
4560 if (processing_template_decl)
4562 /* This may change after the call to
4563 push_template_decl_real, but we want the original value. */
4564 tree name = DECL_NAME (decl);
4566 decl = push_template_decl_real (decl, globalize);
4567 /* If the current binding level is the binding level for the
4568 template parameters (see the comment in
4569 begin_template_parm_list) and the enclosing level is a class
4570 scope, and we're not looking at a friend, push the
4571 declaration of the member class into the class scope. In the
4572 friend case, push_template_decl will already have put the
4573 friend into global scope, if appropriate. */
4574 if (TREE_CODE (type) != ENUMERAL_TYPE
4575 && !globalize && b->kind == sk_template_parms
4576 && b->level_chain->kind == sk_class)
4578 finish_member_declaration (CLASSTYPE_TI_TEMPLATE (type));
4579 /* Put this UDT in the table of UDTs for the class, since
4580 that won't happen below because B is not the class
4581 binding level, but is instead the pseudo-global level. */
4582 if (b->level_chain->type_decls == NULL)
4583 b->level_chain->type_decls =
4584 binding_table_new (SCOPE_DEFAULT_HT_SIZE);
4585 binding_table_insert (b->level_chain->type_decls, name, type);
4586 if (!COMPLETE_TYPE_P (current_class_type))
4588 maybe_add_class_template_decl_list (current_class_type,
4589 type, /*friend_p=*/0);
4590 CLASSTYPE_NESTED_UTDS (current_class_type) =
4591 b->level_chain->type_decls;
4597 return decl;
4600 /* Push a tag name NAME for struct/class/union/enum type TYPE.
4601 Normally put it into the inner-most non-sk_cleanup scope,
4602 but if GLOBALIZE is true, put it in the inner-most non-class scope.
4603 The latter is needed for implicit declarations. */
4605 void
4606 pushtag (tree name, tree type, int globalize)
4608 struct cp_binding_level *b;
4610 timevar_push (TV_NAME_LOOKUP);
4611 b = current_binding_level;
4612 while (b->kind == sk_cleanup
4613 || (b->kind == sk_class
4614 && (globalize
4615 /* We may be defining a new type in the initializer
4616 of a static member variable. We allow this when
4617 not pedantic, and it is particularly useful for
4618 type punning via an anonymous union. */
4619 || COMPLETE_TYPE_P (b->this_entity))))
4620 b = b->level_chain;
4622 if (b->type_decls == NULL)
4623 b->type_decls = binding_table_new (SCOPE_DEFAULT_HT_SIZE);
4624 binding_table_insert (b->type_decls, name, type);
4626 if (name)
4628 /* Do C++ gratuitous typedefing. */
4629 if (IDENTIFIER_TYPE_VALUE (name) != type)
4631 tree d = NULL_TREE;
4632 int in_class = 0;
4633 tree context = TYPE_CONTEXT (type);
4635 if (! context)
4637 tree cs = current_scope ();
4639 if (! globalize)
4640 context = cs;
4641 else if (cs != NULL_TREE && TYPE_P (cs))
4642 /* When declaring a friend class of a local class, we want
4643 to inject the newly named class into the scope
4644 containing the local class, not the namespace scope. */
4645 context = decl_function_context (get_type_decl (cs));
4647 if (!context)
4648 context = current_namespace;
4650 if (b->kind == sk_class
4651 || (b->kind == sk_template_parms
4652 && b->level_chain->kind == sk_class))
4653 in_class = 1;
4655 if (current_lang_name == lang_name_java)
4656 TYPE_FOR_JAVA (type) = 1;
4658 d = create_implicit_typedef (name, type);
4659 DECL_CONTEXT (d) = FROB_CONTEXT (context);
4660 if (! in_class)
4661 set_identifier_type_value_with_scope (name, d, b);
4663 d = maybe_process_template_type_declaration (type,
4664 globalize, b);
4666 if (b->kind == sk_class)
4668 if (!PROCESSING_REAL_TEMPLATE_DECL_P ())
4669 /* Put this TYPE_DECL on the TYPE_FIELDS list for the
4670 class. But if it's a member template class, we
4671 want the TEMPLATE_DECL, not the TYPE_DECL, so this
4672 is done later. */
4673 finish_member_declaration (d);
4674 else
4675 pushdecl_class_level (d);
4677 else
4678 d = pushdecl_with_scope (d, b);
4680 /* FIXME what if it gets a name from typedef? */
4681 if (ANON_AGGRNAME_P (name))
4682 DECL_IGNORED_P (d) = 1;
4684 TYPE_CONTEXT (type) = DECL_CONTEXT (d);
4686 /* If this is a local class, keep track of it. We need this
4687 information for name-mangling, and so that it is possible to find
4688 all function definitions in a translation unit in a convenient
4689 way. (It's otherwise tricky to find a member function definition
4690 it's only pointed to from within a local class.) */
4691 if (TYPE_CONTEXT (type)
4692 && TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL
4693 && !processing_template_decl)
4694 VARRAY_PUSH_TREE (local_classes, type);
4696 if (b->kind == sk_class
4697 && !COMPLETE_TYPE_P (current_class_type))
4699 maybe_add_class_template_decl_list (current_class_type,
4700 type, /*friend_p=*/0);
4701 CLASSTYPE_NESTED_UTDS (current_class_type) = b->type_decls;
4705 if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL)
4706 /* Use the canonical TYPE_DECL for this node. */
4707 TYPE_STUB_DECL (type) = TYPE_NAME (type);
4708 else
4710 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE
4711 will be the tagged type we just added to the current
4712 binding level. This fake NULL-named TYPE_DECL node helps
4713 dwarfout.c to know when it needs to output a
4714 representation of a tagged type, and it also gives us a
4715 convenient place to record the "scope start" address for
4716 the tagged type. */
4718 tree d = build_decl (TYPE_DECL, NULL_TREE, type);
4719 TYPE_STUB_DECL (type) = pushdecl_with_scope (d, b);
4721 timevar_pop (TV_NAME_LOOKUP);
4724 /* Allocate storage for saving a C++ binding. */
4725 #define cxx_saved_binding_make() \
4726 (ggc_alloc (sizeof (cxx_saved_binding)))
4728 struct cxx_saved_binding GTY(())
4730 /* Link that chains saved C++ bindings for a given name into a stack. */
4731 cxx_saved_binding *previous;
4732 /* The name of the current binding. */
4733 tree identifier;
4734 /* The binding we're saving. */
4735 cxx_binding *binding;
4736 tree class_value;
4737 tree real_type_value;
4740 /* Subroutines for reverting temporarily to top-level for instantiation
4741 of templates and such. We actually need to clear out the class- and
4742 local-value slots of all identifiers, so that only the global values
4743 are at all visible. Simply setting current_binding_level to the global
4744 scope isn't enough, because more binding levels may be pushed. */
4745 struct saved_scope *scope_chain;
4747 static cxx_saved_binding *
4748 store_bindings (tree names, cxx_saved_binding *old_bindings)
4750 tree t;
4751 cxx_saved_binding *search_bindings = old_bindings;
4753 timevar_push (TV_NAME_LOOKUP);
4754 for (t = names; t; t = TREE_CHAIN (t))
4756 tree id;
4757 cxx_saved_binding *saved;
4758 cxx_saved_binding *t1;
4760 if (TREE_CODE (t) == TREE_LIST)
4761 id = TREE_PURPOSE (t);
4762 else
4763 id = DECL_NAME (t);
4765 if (!id
4766 /* Note that we may have an IDENTIFIER_CLASS_VALUE even when
4767 we have no IDENTIFIER_BINDING if we have left the class
4768 scope, but cached the class-level declarations. */
4769 || !(IDENTIFIER_BINDING (id) || IDENTIFIER_CLASS_VALUE (id)))
4770 continue;
4772 for (t1 = search_bindings; t1; t1 = t1->previous)
4773 if (t1->identifier == id)
4774 goto skip_it;
4776 my_friendly_assert (TREE_CODE (id) == IDENTIFIER_NODE, 135);
4777 saved = cxx_saved_binding_make ();
4778 saved->previous = old_bindings;
4779 saved->identifier = id;
4780 saved->binding = IDENTIFIER_BINDING (id);
4781 saved->class_value = IDENTIFIER_CLASS_VALUE (id);;
4782 saved->real_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
4783 IDENTIFIER_BINDING (id) = NULL;
4784 IDENTIFIER_CLASS_VALUE (id) = NULL_TREE;
4785 old_bindings = saved;
4786 skip_it:
4789 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, old_bindings);
4792 void
4793 push_to_top_level (void)
4795 struct saved_scope *s;
4796 struct cp_binding_level *b;
4797 cxx_saved_binding *old_bindings;
4798 int need_pop;
4800 timevar_push (TV_NAME_LOOKUP);
4801 s = ggc_alloc_cleared (sizeof (struct saved_scope));
4803 b = scope_chain ? current_binding_level : 0;
4805 /* If we're in the middle of some function, save our state. */
4806 if (cfun)
4808 need_pop = 1;
4809 push_function_context_to (NULL_TREE);
4811 else
4812 need_pop = 0;
4814 old_bindings = NULL;
4815 if (scope_chain && previous_class_type)
4816 old_bindings = store_bindings (previous_class_values, old_bindings);
4818 /* Have to include the global scope, because class-scope decls
4819 aren't listed anywhere useful. */
4820 for (; b; b = b->level_chain)
4822 tree t;
4824 /* Template IDs are inserted into the global level. If they were
4825 inserted into namespace level, finish_file wouldn't find them
4826 when doing pending instantiations. Therefore, don't stop at
4827 namespace level, but continue until :: . */
4828 if (global_scope_p (b))
4829 break;
4831 old_bindings = store_bindings (b->names, old_bindings);
4832 /* We also need to check class_shadowed to save class-level type
4833 bindings, since pushclass doesn't fill in b->names. */
4834 if (b->kind == sk_class)
4835 old_bindings = store_bindings (b->class_shadowed, old_bindings);
4837 /* Unwind type-value slots back to top level. */
4838 for (t = b->type_shadowed; t; t = TREE_CHAIN (t))
4839 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (t), TREE_VALUE (t));
4841 s->prev = scope_chain;
4842 s->old_bindings = old_bindings;
4843 s->bindings = b;
4844 s->need_pop_function_context = need_pop;
4845 s->function_decl = current_function_decl;
4847 scope_chain = s;
4848 current_function_decl = NULL_TREE;
4849 VARRAY_TREE_INIT (current_lang_base, 10, "current_lang_base");
4850 current_lang_name = lang_name_cplusplus;
4851 current_namespace = global_namespace;
4852 timevar_pop (TV_NAME_LOOKUP);
4855 void
4856 pop_from_top_level (void)
4858 struct saved_scope *s = scope_chain;
4859 cxx_saved_binding *saved;
4861 timevar_push (TV_NAME_LOOKUP);
4862 /* Clear out class-level bindings cache. */
4863 if (previous_class_type)
4864 invalidate_class_lookup_cache ();
4866 current_lang_base = 0;
4868 scope_chain = s->prev;
4869 for (saved = s->old_bindings; saved; saved = saved->previous)
4871 tree id = saved->identifier;
4873 IDENTIFIER_BINDING (id) = saved->binding;
4874 IDENTIFIER_CLASS_VALUE (id) = saved->class_value;
4875 SET_IDENTIFIER_TYPE_VALUE (id, saved->real_type_value);
4878 /* If we were in the middle of compiling a function, restore our
4879 state. */
4880 if (s->need_pop_function_context)
4881 pop_function_context_from (NULL_TREE);
4882 current_function_decl = s->function_decl;
4883 timevar_pop (TV_NAME_LOOKUP);
4886 /* Pop off extraneous binding levels left over due to syntax errors.
4888 We don't pop past namespaces, as they might be valid. */
4890 void
4891 pop_everything (void)
4893 if (ENABLE_SCOPE_CHECKING)
4894 verbatim ("XXX entering pop_everything ()\n");
4895 while (!toplevel_bindings_p ())
4897 if (current_binding_level->kind == sk_class)
4898 pop_nested_class ();
4899 else
4900 poplevel (0, 0, 0);
4902 if (ENABLE_SCOPE_CHECKING)
4903 verbatim ("XXX leaving pop_everything ()\n");
4906 /* Emit debugging information for using declarations and directives.
4907 If input tree is overloaded fn then emit debug info for all
4908 candidates. */
4910 static void
4911 cp_emit_debug_info_for_using (tree t, tree context)
4913 /* Ignore this FUNCTION_DECL if it refers to a builtin declaration
4914 of a builtin function. */
4915 if (TREE_CODE (t) == FUNCTION_DECL
4916 && DECL_EXTERNAL (t)
4917 && DECL_BUILT_IN (t))
4918 return;
4920 /* Do not supply context to imported_module_or_decl, if
4921 it is a global namespace. */
4922 if (context == global_namespace)
4923 context = NULL_TREE;
4925 if (BASELINK_P (t))
4926 t = BASELINK_FUNCTIONS (t);
4928 /* FIXME: Handle TEMPLATE_DECLs. */
4929 for (t = OVL_CURRENT (t); t; t = OVL_NEXT (t))
4930 if (TREE_CODE (t) != TEMPLATE_DECL)
4931 (*debug_hooks->imported_module_or_decl) (t, context);
4934 #include "gt-cp-name-lookup.h"