PR c++/14401
[official-gcc.git] / gcc / cp / name-lookup.c
blob82e583c3351fa595fda780fb40a5d96f513ca7f2
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 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 || bval == error_mark_node)
443 /* VALUE is null when push_class_level_binding moves an inherited
444 type-binding out of the way to make room for a new value binding.
445 It is an error_mark_node when DECL's name has been used in a
446 non-class scope prior declaration. In that case, we should have
447 already issued a diagnostic; for graceful error recovery purpose,
448 pretend this was the intended declaration for that name. */
449 binding->value = decl;
450 else if (TREE_CODE (bval) == TYPE_DECL && DECL_ARTIFICIAL (bval))
452 /* The old binding was a type name. It was placed in
453 VALUE field because it was thought, at the point it was
454 declared, to be the only entity with such a name. Move the
455 type name into the type slot; it is now hidden by the new
456 binding. */
457 binding->type = bval;
458 binding->value = decl;
459 binding->value_is_inherited = false;
461 else if (TREE_CODE (bval) == TYPE_DECL
462 && TREE_CODE (decl) == TYPE_DECL
463 && DECL_NAME (decl) == DECL_NAME (bval)
464 && (same_type_p (TREE_TYPE (decl), TREE_TYPE (bval))
465 /* If either type involves template parameters, we must
466 wait until instantiation. */
467 || uses_template_parms (TREE_TYPE (decl))
468 || uses_template_parms (TREE_TYPE (bval))))
469 /* We have two typedef-names, both naming the same type to have
470 the same name. This is OK because of:
472 [dcl.typedef]
474 In a given scope, a typedef specifier can be used to redefine
475 the name of any type declared in that scope to refer to the
476 type to which it already refers. */
477 ok = false;
478 /* There can be two block-scope declarations of the same variable,
479 so long as they are `extern' declarations. However, there cannot
480 be two declarations of the same static data member:
482 [class.mem]
484 A member shall not be declared twice in the
485 member-specification. */
486 else if (TREE_CODE (decl) == VAR_DECL && TREE_CODE (bval) == VAR_DECL
487 && DECL_EXTERNAL (decl) && DECL_EXTERNAL (bval)
488 && !DECL_CLASS_SCOPE_P (decl))
490 duplicate_decls (decl, binding->value);
491 ok = false;
493 else
495 error ("declaration of `%#D'", decl);
496 cp_error_at ("conflicts with previous declaration `%#D'",
497 binding->value);
498 ok = false;
501 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, ok);
504 /* Add DECL to the list of things declared in B. */
506 static void
507 add_decl_to_level (tree decl, cxx_scope *b)
509 if (TREE_CODE (decl) == NAMESPACE_DECL
510 && !DECL_NAMESPACE_ALIAS (decl))
512 TREE_CHAIN (decl) = b->namespaces;
513 b->namespaces = decl;
515 else if (TREE_CODE (decl) == VAR_DECL && DECL_VIRTUAL_P (decl))
517 TREE_CHAIN (decl) = b->vtables;
518 b->vtables = decl;
520 else
522 /* We build up the list in reverse order, and reverse it later if
523 necessary. */
524 TREE_CHAIN (decl) = b->names;
525 b->names = decl;
526 b->names_size++;
528 /* If appropriate, add decl to separate list of statics. We
529 include extern variables because they might turn out to be
530 static later. It's OK for this list to contain a few false
531 positives. */
532 if (b->kind == sk_namespace)
533 if ((TREE_CODE (decl) == VAR_DECL
534 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
535 || (TREE_CODE (decl) == FUNCTION_DECL
536 && (!TREE_PUBLIC (decl) || DECL_DECLARED_INLINE_P (decl))))
537 VARRAY_PUSH_TREE (b->static_decls, decl);
541 /* Record a decl-node X as belonging to the current lexical scope.
542 Check for errors (such as an incompatible declaration for the same
543 name already seen in the same scope).
545 Returns either X or an old decl for the same name.
546 If an old decl is returned, it may have been smashed
547 to agree with what X says. */
549 tree
550 pushdecl (tree x)
552 tree t;
553 tree name;
554 int need_new_binding;
556 timevar_push (TV_NAME_LOOKUP);
558 need_new_binding = 1;
560 if (DECL_TEMPLATE_PARM_P (x))
561 /* Template parameters have no context; they are not X::T even
562 when declared within a class or namespace. */
564 else
566 if (current_function_decl && x != current_function_decl
567 /* A local declaration for a function doesn't constitute
568 nesting. */
569 && TREE_CODE (x) != FUNCTION_DECL
570 /* A local declaration for an `extern' variable is in the
571 scope of the current namespace, not the current
572 function. */
573 && !(TREE_CODE (x) == VAR_DECL && DECL_EXTERNAL (x))
574 && !DECL_CONTEXT (x))
575 DECL_CONTEXT (x) = current_function_decl;
577 /* If this is the declaration for a namespace-scope function,
578 but the declaration itself is in a local scope, mark the
579 declaration. */
580 if (TREE_CODE (x) == FUNCTION_DECL
581 && DECL_NAMESPACE_SCOPE_P (x)
582 && current_function_decl
583 && x != current_function_decl)
584 DECL_LOCAL_FUNCTION_P (x) = 1;
587 name = DECL_NAME (x);
588 if (name)
590 int different_binding_level = 0;
592 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
593 name = TREE_OPERAND (name, 0);
595 /* In case this decl was explicitly namespace-qualified, look it
596 up in its namespace context. */
597 if (DECL_NAMESPACE_SCOPE_P (x) && namespace_bindings_p ())
598 t = namespace_binding (name, DECL_CONTEXT (x));
599 else
600 t = lookup_name_current_level (name);
602 /* [basic.link] If there is a visible declaration of an entity
603 with linkage having the same name and type, ignoring entities
604 declared outside the innermost enclosing namespace scope, the
605 block scope declaration declares that same entity and
606 receives the linkage of the previous declaration. */
607 if (! t && current_function_decl && x != current_function_decl
608 && (TREE_CODE (x) == FUNCTION_DECL || TREE_CODE (x) == VAR_DECL)
609 && DECL_EXTERNAL (x))
611 /* Look in block scope. */
612 t = IDENTIFIER_VALUE (name);
613 /* Or in the innermost namespace. */
614 if (! t)
615 t = namespace_binding (name, DECL_CONTEXT (x));
616 /* Does it have linkage? Note that if this isn't a DECL, it's an
617 OVERLOAD, which is OK. */
618 if (t && DECL_P (t) && ! (TREE_STATIC (t) || DECL_EXTERNAL (t)))
619 t = NULL_TREE;
620 if (t)
621 different_binding_level = 1;
624 /* If we are declaring a function, and the result of name-lookup
625 was an OVERLOAD, look for an overloaded instance that is
626 actually the same as the function we are declaring. (If
627 there is one, we have to merge our declaration with the
628 previous declaration.) */
629 if (t && TREE_CODE (t) == OVERLOAD)
631 tree match;
633 if (TREE_CODE (x) == FUNCTION_DECL)
634 for (match = t; match; match = OVL_NEXT (match))
636 if (decls_match (OVL_CURRENT (match), x))
637 break;
639 else
640 /* Just choose one. */
641 match = t;
643 if (match)
644 t = OVL_CURRENT (match);
645 else
646 t = NULL_TREE;
649 if (t && t != error_mark_node)
651 if (different_binding_level)
653 if (decls_match (x, t))
654 /* The standard only says that the local extern
655 inherits linkage from the previous decl; in
656 particular, default args are not shared. It would
657 be nice to propagate inlining info, though. FIXME. */
658 TREE_PUBLIC (x) = TREE_PUBLIC (t);
660 else if (TREE_CODE (t) == PARM_DECL)
662 if (DECL_CONTEXT (t) == NULL_TREE)
663 /* This is probably caused by too many errors, but calling
664 abort will say that if errors have occurred. */
665 abort ();
667 /* Check for duplicate params. */
668 if (duplicate_decls (x, t))
669 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
671 else if ((DECL_EXTERN_C_FUNCTION_P (x)
672 || DECL_FUNCTION_TEMPLATE_P (x))
673 && is_overloaded_fn (t))
674 /* Don't do anything just yet. */;
675 else if (t == wchar_decl_node)
677 if (pedantic && ! DECL_IN_SYSTEM_HEADER (x))
678 pedwarn ("redeclaration of `wchar_t' as `%T'",
679 TREE_TYPE (x));
681 /* Throw away the redeclaration. */
682 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
684 else
686 tree olddecl = duplicate_decls (x, t);
688 /* If the redeclaration failed, we can stop at this
689 point. */
690 if (olddecl == error_mark_node)
691 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
693 if (olddecl)
695 if (TREE_CODE (t) == TYPE_DECL)
696 SET_IDENTIFIER_TYPE_VALUE (name, TREE_TYPE (t));
697 else if (TREE_CODE (t) == FUNCTION_DECL)
698 check_default_args (t);
700 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
702 else if (DECL_MAIN_P (x))
704 /* A redeclaration of main, but not a duplicate of the
705 previous one.
707 [basic.start.main]
709 This function shall not be overloaded. */
710 cp_error_at ("invalid redeclaration of `%D'", t);
711 error ("as `%D'", x);
712 /* We don't try to push this declaration since that
713 causes a crash. */
714 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
719 check_template_shadow (x);
721 /* If this is a function conjured up by the backend, massage it
722 so it looks friendly. */
723 if (DECL_NON_THUNK_FUNCTION_P (x) && ! DECL_LANG_SPECIFIC (x))
725 retrofit_lang_decl (x);
726 SET_DECL_LANGUAGE (x, lang_c);
729 if (DECL_NON_THUNK_FUNCTION_P (x) && ! DECL_FUNCTION_MEMBER_P (x))
731 t = push_overloaded_decl (x, PUSH_LOCAL);
732 if (t != x)
733 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
734 if (!namespace_bindings_p ())
735 /* We do not need to create a binding for this name;
736 push_overloaded_decl will have already done so if
737 necessary. */
738 need_new_binding = 0;
740 else if (DECL_FUNCTION_TEMPLATE_P (x) && DECL_NAMESPACE_SCOPE_P (x))
742 t = push_overloaded_decl (x, PUSH_GLOBAL);
743 if (t == x)
744 add_decl_to_level (x, NAMESPACE_LEVEL (CP_DECL_CONTEXT (t)));
745 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
748 /* If declaring a type as a typedef, copy the type (unless we're
749 at line 0), and install this TYPE_DECL as the new type's typedef
750 name. See the extensive comment in ../c-decl.c (pushdecl). */
751 if (TREE_CODE (x) == TYPE_DECL)
753 tree type = TREE_TYPE (x);
754 if (DECL_SOURCE_LINE (x) == 0)
756 if (TYPE_NAME (type) == 0)
757 TYPE_NAME (type) = x;
759 else if (type != error_mark_node && TYPE_NAME (type) != x
760 /* We don't want to copy the type when all we're
761 doing is making a TYPE_DECL for the purposes of
762 inlining. */
763 && (!TYPE_NAME (type)
764 || TYPE_NAME (type) != DECL_ABSTRACT_ORIGIN (x)))
766 DECL_ORIGINAL_TYPE (x) = type;
767 type = build_type_copy (type);
768 TYPE_STUB_DECL (type) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
769 TYPE_NAME (type) = x;
770 TREE_TYPE (x) = type;
773 if (type != error_mark_node
774 && TYPE_NAME (type)
775 && TYPE_IDENTIFIER (type))
776 set_identifier_type_value (DECL_NAME (x), x);
779 /* Multiple external decls of the same identifier ought to match.
781 We get warnings about inline functions where they are defined.
782 We get warnings about other functions from push_overloaded_decl.
784 Avoid duplicate warnings where they are used. */
785 if (TREE_PUBLIC (x) && TREE_CODE (x) != FUNCTION_DECL)
787 tree decl;
789 decl = IDENTIFIER_NAMESPACE_VALUE (name);
790 if (decl && TREE_CODE (decl) == OVERLOAD)
791 decl = OVL_FUNCTION (decl);
793 if (decl && decl != error_mark_node
794 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl))
795 /* If different sort of thing, we already gave an error. */
796 && TREE_CODE (decl) == TREE_CODE (x)
797 && !same_type_p (TREE_TYPE (x), TREE_TYPE (decl)))
799 pedwarn ("type mismatch with previous external decl of `%#D'", x);
800 cp_pedwarn_at ("previous external decl of `%#D'", decl);
804 /* This name is new in its binding level.
805 Install the new declaration and return it. */
806 if (namespace_bindings_p ())
808 /* Install a global value. */
810 /* If the first global decl has external linkage,
811 warn if we later see static one. */
812 if (IDENTIFIER_GLOBAL_VALUE (name) == NULL_TREE && TREE_PUBLIC (x))
813 TREE_PUBLIC (name) = 1;
815 /* Bind the name for the entity. */
816 if (!(TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x)
817 && t != NULL_TREE)
818 && (TREE_CODE (x) == TYPE_DECL
819 || TREE_CODE (x) == VAR_DECL
820 || TREE_CODE (x) == ALIAS_DECL
821 || TREE_CODE (x) == NAMESPACE_DECL
822 || TREE_CODE (x) == CONST_DECL
823 || TREE_CODE (x) == TEMPLATE_DECL))
824 SET_IDENTIFIER_NAMESPACE_VALUE (name, x);
826 /* If new decl is `static' and an `extern' was seen previously,
827 warn about it. */
828 if (x != NULL_TREE && t != NULL_TREE && decls_match (x, t))
829 warn_extern_redeclared_static (x, t);
831 else
833 /* Here to install a non-global value. */
834 tree oldlocal = IDENTIFIER_VALUE (name);
835 tree oldglobal = IDENTIFIER_NAMESPACE_VALUE (name);
837 if (need_new_binding)
839 push_local_binding (name, x, 0);
840 /* Because push_local_binding will hook X on to the
841 current_binding_level's name list, we don't want to
842 do that again below. */
843 need_new_binding = 0;
846 /* If this is a TYPE_DECL, push it into the type value slot. */
847 if (TREE_CODE (x) == TYPE_DECL)
848 set_identifier_type_value (name, x);
850 /* Clear out any TYPE_DECL shadowed by a namespace so that
851 we won't think this is a type. The C struct hack doesn't
852 go through namespaces. */
853 if (TREE_CODE (x) == NAMESPACE_DECL)
854 set_identifier_type_value (name, NULL_TREE);
856 if (oldlocal)
858 tree d = oldlocal;
860 while (oldlocal
861 && TREE_CODE (oldlocal) == VAR_DECL
862 && DECL_DEAD_FOR_LOCAL (oldlocal))
863 oldlocal = DECL_SHADOWED_FOR_VAR (oldlocal);
865 if (oldlocal == NULL_TREE)
866 oldlocal = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (d));
869 /* If this is an extern function declaration, see if we
870 have a global definition or declaration for the function. */
871 if (oldlocal == NULL_TREE
872 && DECL_EXTERNAL (x)
873 && oldglobal != NULL_TREE
874 && TREE_CODE (x) == FUNCTION_DECL
875 && TREE_CODE (oldglobal) == FUNCTION_DECL)
877 /* We have one. Their types must agree. */
878 if (decls_match (x, oldglobal))
879 /* OK */;
880 else
882 warning ("extern declaration of `%#D' doesn't match", x);
883 cp_warning_at ("global declaration `%#D'", oldglobal);
886 /* If we have a local external declaration,
887 and no file-scope declaration has yet been seen,
888 then if we later have a file-scope decl it must not be static. */
889 if (oldlocal == NULL_TREE
890 && oldglobal == NULL_TREE
891 && DECL_EXTERNAL (x)
892 && TREE_PUBLIC (x))
893 TREE_PUBLIC (name) = 1;
895 /* Warn if shadowing an argument at the top level of the body. */
896 if (oldlocal != NULL_TREE && !DECL_EXTERNAL (x)
897 /* Inline decls shadow nothing. */
898 && !DECL_FROM_INLINE (x)
899 && TREE_CODE (oldlocal) == PARM_DECL
900 /* Don't check the `this' parameter. */
901 && !DECL_ARTIFICIAL (oldlocal))
903 bool err = false;
905 /* Don't complain if it's from an enclosing function. */
906 if (DECL_CONTEXT (oldlocal) == current_function_decl
907 && TREE_CODE (x) != PARM_DECL)
909 /* Go to where the parms should be and see if we find
910 them there. */
911 struct cp_binding_level *b = current_binding_level->level_chain;
913 /* Skip the ctor/dtor cleanup level. */
914 b = b->level_chain;
916 /* ARM $8.3 */
917 if (b->kind == sk_function_parms)
919 error ("declaration of '%#D' shadows a parameter", x);
920 err = true;
924 if (warn_shadow && !err)
926 warning ("declaration of '%#D' shadows a parameter", x);
927 warning ("%Jshadowed declaration is here", oldlocal);
931 /* Maybe warn if shadowing something else. */
932 else if (warn_shadow && !DECL_EXTERNAL (x)
933 /* No shadow warnings for internally generated vars. */
934 && ! DECL_ARTIFICIAL (x)
935 /* No shadow warnings for vars made for inlining. */
936 && ! DECL_FROM_INLINE (x))
938 if (IDENTIFIER_CLASS_VALUE (name) != NULL_TREE
939 && current_class_ptr
940 && !TREE_STATIC (name))
942 /* Location of previous decl is not useful in this case. */
943 warning ("declaration of '%D' shadows a member of 'this'",
946 else if (oldlocal != NULL_TREE
947 && TREE_CODE (oldlocal) == VAR_DECL)
949 warning ("declaration of '%D' shadows a previous local", x);
950 warning ("%Jshadowed declaration is here", oldlocal);
952 else if (oldglobal != NULL_TREE
953 && TREE_CODE (oldglobal) == VAR_DECL)
954 /* XXX shadow warnings in outer-more namespaces */
956 warning ("declaration of '%D' shadows a global declaration",
958 warning ("%Jshadowed declaration is here", oldglobal);
963 if (TREE_CODE (x) == FUNCTION_DECL)
964 check_default_args (x);
966 if (TREE_CODE (x) == VAR_DECL)
967 maybe_register_incomplete_var (x);
970 if (need_new_binding)
971 add_decl_to_level (x,
972 DECL_NAMESPACE_SCOPE_P (x)
973 ? NAMESPACE_LEVEL (CP_DECL_CONTEXT (x))
974 : current_binding_level);
976 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
979 /* Enter DECL into the symbol table, if that's appropriate. Returns
980 DECL, or a modified version thereof. */
982 tree
983 maybe_push_decl (tree decl)
985 tree type = TREE_TYPE (decl);
987 /* Add this decl to the current binding level, but not if it comes
988 from another scope, e.g. a static member variable. TEM may equal
989 DECL or it may be a previous decl of the same name. */
990 if (decl == error_mark_node
991 || (TREE_CODE (decl) != PARM_DECL
992 && DECL_CONTEXT (decl) != NULL_TREE
993 /* Definitions of namespace members outside their namespace are
994 possible. */
995 && TREE_CODE (DECL_CONTEXT (decl)) != NAMESPACE_DECL)
996 || (TREE_CODE (decl) == TEMPLATE_DECL && !namespace_bindings_p ())
997 || TREE_CODE (type) == UNKNOWN_TYPE
998 /* The declaration of a template specialization does not affect
999 the functions available for overload resolution, so we do not
1000 call pushdecl. */
1001 || (TREE_CODE (decl) == FUNCTION_DECL
1002 && DECL_TEMPLATE_SPECIALIZATION (decl)))
1003 return decl;
1004 else
1005 return pushdecl (decl);
1008 /* Bind DECL to ID in the current_binding_level, assumed to be a local
1009 binding level. If PUSH_USING is set in FLAGS, we know that DECL
1010 doesn't really belong to this binding level, that it got here
1011 through a using-declaration. */
1013 void
1014 push_local_binding (tree id, tree decl, int flags)
1016 struct cp_binding_level *b;
1018 /* Skip over any local classes. This makes sense if we call
1019 push_local_binding with a friend decl of a local class. */
1020 b = innermost_nonclass_level ();
1022 if (lookup_name_current_level (id))
1024 /* Supplement the existing binding. */
1025 if (!supplement_binding (IDENTIFIER_BINDING (id), decl))
1026 /* It didn't work. Something else must be bound at this
1027 level. Do not add DECL to the list of things to pop
1028 later. */
1029 return;
1031 else
1032 /* Create a new binding. */
1033 push_binding (id, decl, b);
1035 if (TREE_CODE (decl) == OVERLOAD || (flags & PUSH_USING))
1036 /* We must put the OVERLOAD into a TREE_LIST since the
1037 TREE_CHAIN of an OVERLOAD is already used. Similarly for
1038 decls that got here through a using-declaration. */
1039 decl = build_tree_list (NULL_TREE, decl);
1041 /* And put DECL on the list of things declared by the current
1042 binding level. */
1043 add_decl_to_level (decl, b);
1046 /* The old ARM scoping rules injected variables declared in the
1047 initialization statement of a for-statement into the surrounding
1048 scope. We support this usage, in order to be backward-compatible.
1049 DECL is a just-declared VAR_DECL; if necessary inject its
1050 declaration into the surrounding scope. */
1052 void
1053 maybe_inject_for_scope_var (tree decl)
1055 timevar_push (TV_NAME_LOOKUP);
1056 if (!DECL_NAME (decl))
1058 timevar_pop (TV_NAME_LOOKUP);
1059 return;
1062 /* Declarations of __FUNCTION__ and its ilk appear magically when
1063 the variable is first used. If that happens to be inside a
1064 for-loop, we don't want to do anything special. */
1065 if (DECL_PRETTY_FUNCTION_P (decl))
1067 timevar_pop (TV_NAME_LOOKUP);
1068 return;
1071 if (current_binding_level->kind == sk_for)
1073 struct cp_binding_level *outer
1074 = current_binding_level->level_chain;
1076 /* Check to see if the same name is already bound at the outer
1077 level, either because it was directly declared, or because a
1078 dead for-decl got preserved. In either case, the code would
1079 not have been valid under the ARM scope rules, so clear
1080 is_for_scope for the current_binding_level.
1082 Otherwise, we need to preserve the temp slot for decl to last
1083 into the outer binding level. */
1085 cxx_binding *outer_binding
1086 = IDENTIFIER_BINDING (DECL_NAME (decl))->previous;
1088 if (outer_binding && outer_binding->scope == outer
1089 && (TREE_CODE (outer_binding->value) == VAR_DECL)
1090 && DECL_DEAD_FOR_LOCAL (outer_binding->value))
1092 outer_binding->value = DECL_SHADOWED_FOR_VAR (outer_binding->value);
1093 current_binding_level->kind = sk_block;
1096 timevar_pop (TV_NAME_LOOKUP);
1099 /* Check to see whether or not DECL is a variable that would have been
1100 in scope under the ARM, but is not in scope under the ANSI/ISO
1101 standard. If so, issue an error message. If name lookup would
1102 work in both cases, but return a different result, this function
1103 returns the result of ANSI/ISO lookup. Otherwise, it returns
1104 DECL. */
1106 tree
1107 check_for_out_of_scope_variable (tree decl)
1109 tree shadowed;
1111 /* We only care about out of scope variables. */
1112 if (!(TREE_CODE (decl) == VAR_DECL && DECL_DEAD_FOR_LOCAL (decl)))
1113 return decl;
1115 shadowed = DECL_SHADOWED_FOR_VAR (decl);
1116 while (shadowed != NULL_TREE && TREE_CODE (shadowed) == VAR_DECL
1117 && DECL_DEAD_FOR_LOCAL (shadowed))
1118 shadowed = DECL_SHADOWED_FOR_VAR (shadowed);
1119 if (!shadowed)
1120 shadowed = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (decl));
1121 if (shadowed)
1123 if (!DECL_ERROR_REPORTED (decl))
1125 warning ("name lookup of `%D' changed",
1126 DECL_NAME (decl));
1127 cp_warning_at (" matches this `%D' under ISO standard rules",
1128 shadowed);
1129 cp_warning_at (" matches this `%D' under old rules", decl);
1130 DECL_ERROR_REPORTED (decl) = 1;
1132 return shadowed;
1135 /* If we have already complained about this declaration, there's no
1136 need to do it again. */
1137 if (DECL_ERROR_REPORTED (decl))
1138 return decl;
1140 DECL_ERROR_REPORTED (decl) = 1;
1141 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
1143 error ("name lookup of `%D' changed for new ISO `for' scoping",
1144 DECL_NAME (decl));
1145 cp_error_at (" cannot use obsolete binding at `%D' because it has a destructor", decl);
1146 return error_mark_node;
1148 else
1150 pedwarn ("name lookup of `%D' changed for new ISO `for' scoping",
1151 DECL_NAME (decl));
1152 cp_pedwarn_at (" using obsolete binding at `%D'", decl);
1155 return decl;
1158 /* true means unconditionally make a BLOCK for the next level pushed. */
1160 static bool keep_next_level_flag;
1162 static int binding_depth = 0;
1163 static int is_class_level = 0;
1165 static void
1166 indent (int depth)
1168 int i;
1170 for (i = 0; i < depth * 2; i++)
1171 putc (' ', stderr);
1174 /* Return a string describing the kind of SCOPE we have. */
1175 static const char *
1176 cxx_scope_descriptor (cxx_scope *scope)
1178 /* The order of this table must match the "scope_kind"
1179 enumerators. */
1180 static const char* scope_kind_names[] = {
1181 "block-scope",
1182 "cleanup-scope",
1183 "try-scope",
1184 "catch-scope",
1185 "for-scope",
1186 "function-parameter-scope",
1187 "class-scope",
1188 "namespace-scope",
1189 "template-parameter-scope",
1190 "template-explicit-spec-scope"
1192 const scope_kind kind = scope->explicit_spec_p
1193 ? sk_template_spec : scope->kind;
1195 return scope_kind_names[kind];
1198 /* Output a debugging information about SCOPE when performing
1199 ACTION at LINE. */
1200 static void
1201 cxx_scope_debug (cxx_scope *scope, int line, const char *action)
1203 const char *desc = cxx_scope_descriptor (scope);
1204 if (scope->this_entity)
1205 verbatim ("%s %s(%E) %p %d\n", action, desc,
1206 scope->this_entity, (void *) scope, line);
1207 else
1208 verbatim ("%s %s %p %d\n", action, desc, (void *) scope, line);
1211 /* Return the estimated initial size of the hashtable of a NAMESPACE
1212 scope. */
1214 static inline size_t
1215 namespace_scope_ht_size (tree ns)
1217 tree name = DECL_NAME (ns);
1219 return name == std_identifier
1220 ? NAMESPACE_STD_HT_SIZE
1221 : (name == global_scope_name
1222 ? GLOBAL_SCOPE_HT_SIZE
1223 : NAMESPACE_ORDINARY_HT_SIZE);
1226 /* A chain of binding_level structures awaiting reuse. */
1228 static GTY((deletable (""))) struct cp_binding_level *free_binding_level;
1230 /* Create a new KIND scope and make it the top of the active scopes stack.
1231 ENTITY is the scope of the associated C++ entity (namespace, class,
1232 function); it is NULL otherwise. */
1234 cxx_scope *
1235 begin_scope (scope_kind kind, tree entity)
1237 cxx_scope *scope;
1239 /* Reuse or create a struct for this binding level. */
1240 if (!ENABLE_SCOPE_CHECKING && free_binding_level)
1242 scope = free_binding_level;
1243 free_binding_level = scope->level_chain;
1245 else
1246 scope = ggc_alloc (sizeof (cxx_scope));
1247 memset (scope, 0, sizeof (cxx_scope));
1249 scope->this_entity = entity;
1250 scope->more_cleanups_ok = true;
1251 switch (kind)
1253 case sk_cleanup:
1254 scope->keep = true;
1255 break;
1257 case sk_template_spec:
1258 scope->explicit_spec_p = true;
1259 kind = sk_template_parms;
1260 /* Fall through. */
1261 case sk_template_parms:
1262 case sk_block:
1263 case sk_try:
1264 case sk_catch:
1265 case sk_for:
1266 case sk_class:
1267 case sk_function_parms:
1268 scope->keep = keep_next_level_flag;
1269 break;
1271 case sk_namespace:
1272 scope->type_decls = binding_table_new (namespace_scope_ht_size (entity));
1273 NAMESPACE_LEVEL (entity) = scope;
1274 VARRAY_TREE_INIT (scope->static_decls,
1275 DECL_NAME (entity) == std_identifier
1276 || DECL_NAME (entity) == global_scope_name
1277 ? 200 : 10,
1278 "Static declarations");
1279 break;
1281 default:
1282 /* Should not happen. */
1283 my_friendly_assert (false, 20030922);
1284 break;
1286 scope->kind = kind;
1288 /* Add it to the front of currently active scopes stack. */
1289 scope->level_chain = current_binding_level;
1290 current_binding_level = scope;
1291 keep_next_level_flag = false;
1293 if (ENABLE_SCOPE_CHECKING)
1295 scope->binding_depth = binding_depth;
1296 indent (binding_depth);
1297 cxx_scope_debug (scope, input_location.line, "push");
1298 is_class_level = 0;
1299 binding_depth++;
1302 return scope;
1305 /* We're about to leave current scope. Pop the top of the stack of
1306 currently active scopes. Return the enclosing scope, now active. */
1308 cxx_scope *
1309 leave_scope (void)
1311 cxx_scope *scope = current_binding_level;
1313 if (scope->kind == sk_namespace && class_binding_level)
1314 current_binding_level = class_binding_level;
1316 /* We cannot leave a scope, if there are none left. */
1317 if (NAMESPACE_LEVEL (global_namespace))
1318 my_friendly_assert (!global_scope_p (scope), 20030527);
1320 if (ENABLE_SCOPE_CHECKING)
1322 indent (--binding_depth);
1323 cxx_scope_debug (scope, input_location.line, "leave");
1324 if (is_class_level != (scope == class_binding_level))
1326 indent (binding_depth);
1327 verbatim ("XXX is_class_level != (current_scope == class_scope)\n");
1329 is_class_level = 0;
1332 /* Move one nesting level up. */
1333 current_binding_level = scope->level_chain;
1335 /* Namespace-scopes are left most probably temporarily, not completely;
1336 they can be reopen later, e.g. in namespace-extension or any name
1337 binding activity that requires us to resume a namespace. For other
1338 scopes, we just make the structure available for reuse. */
1339 if (scope->kind != sk_namespace)
1341 scope->level_chain = free_binding_level;
1342 if (scope->kind == sk_class)
1343 scope->type_decls = NULL;
1344 else
1345 binding_table_free (scope->type_decls);
1346 my_friendly_assert (!ENABLE_SCOPE_CHECKING
1347 || scope->binding_depth == binding_depth,
1348 20030529);
1349 free_binding_level = scope;
1352 /* Find the innermost enclosing class scope, and reset
1353 CLASS_BINDING_LEVEL appropriately. */
1354 for (scope = current_binding_level;
1355 scope && scope->kind != sk_class;
1356 scope = scope->level_chain)
1358 class_binding_level = scope && scope->kind == sk_class ? scope : NULL;
1360 return current_binding_level;
1363 static void
1364 resume_scope (struct cp_binding_level* b)
1366 /* Resuming binding levels is meant only for namespaces,
1367 and those cannot nest into classes. */
1368 my_friendly_assert(!class_binding_level, 386);
1369 /* Also, resuming a non-directly nested namespace is a no-no. */
1370 my_friendly_assert(b->level_chain == current_binding_level, 386);
1371 current_binding_level = b;
1372 if (ENABLE_SCOPE_CHECKING)
1374 b->binding_depth = binding_depth;
1375 indent (binding_depth);
1376 cxx_scope_debug (b, input_location.line, "resume");
1377 is_class_level = 0;
1378 binding_depth++;
1382 /* Return the innermost binding level that is not for a class scope. */
1384 static cxx_scope *
1385 innermost_nonclass_level (void)
1387 cxx_scope *b;
1389 b = current_binding_level;
1390 while (b->kind == sk_class)
1391 b = b->level_chain;
1393 return b;
1396 /* We're defining an object of type TYPE. If it needs a cleanup, but
1397 we're not allowed to add any more objects with cleanups to the current
1398 scope, create a new binding level. */
1400 void
1401 maybe_push_cleanup_level (tree type)
1403 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
1404 && current_binding_level->more_cleanups_ok == 0)
1406 begin_scope (sk_cleanup, NULL);
1407 clear_last_expr ();
1408 add_scope_stmt (/*begin_p=*/1, /*partial_p=*/1);
1412 /* Nonzero if we are currently in the global binding level. */
1415 global_bindings_p (void)
1417 return global_scope_p (current_binding_level);
1420 /* True if we are currently in a toplevel binding level. This
1421 means either the global binding level or a namespace in a toplevel
1422 binding level. Since there are no non-toplevel namespace levels,
1423 this really means any namespace or template parameter level. We
1424 also include a class whose context is toplevel. */
1426 bool
1427 toplevel_bindings_p (void)
1429 struct cp_binding_level *b = innermost_nonclass_level ();
1431 return b->kind == sk_namespace || b->kind == sk_template_parms;
1434 /* True if this is a namespace scope, or if we are defining a class
1435 which is itself at namespace scope, or whose enclosing class is
1436 such a class, etc. */
1438 bool
1439 namespace_bindings_p (void)
1441 struct cp_binding_level *b = innermost_nonclass_level ();
1443 return b->kind == sk_namespace;
1446 /* True if the current level needs to have a BLOCK made. */
1448 bool
1449 kept_level_p (void)
1451 return (current_binding_level->blocks != NULL_TREE
1452 || current_binding_level->keep
1453 || current_binding_level->kind == sk_cleanup
1454 || current_binding_level->names != NULL_TREE
1455 || current_binding_level->type_decls != NULL);
1458 /* Returns the kind of the innermost scope. */
1460 scope_kind
1461 innermost_scope_kind (void)
1463 return current_binding_level->kind;
1466 /* Returns true if this scope was created to store template parameters. */
1468 bool
1469 template_parm_scope_p (void)
1471 return innermost_scope_kind () == sk_template_parms;
1474 /* If KEEP is true, make a BLOCK node for the next binding level,
1475 unconditionally. Otherwise, use the normal logic to decide whether
1476 or not to create a BLOCK. */
1478 void
1479 keep_next_level (bool keep)
1481 keep_next_level_flag = keep;
1484 /* Return the list of declarations of the current level.
1485 Note that this list is in reverse order unless/until
1486 you nreverse it; and when you do nreverse it, you must
1487 store the result back using `storedecls' or you will lose. */
1489 tree
1490 getdecls (void)
1492 return current_binding_level->names;
1495 /* Set the current binding TABLE for type declarations.. This is a
1496 temporary workaround of the fact that the data structure classtypes
1497 does not currently carry its allocated cxx_scope structure. */
1498 void
1499 cxx_remember_type_decls (binding_table table)
1501 current_binding_level->type_decls = table;
1504 /* For debugging. */
1505 static int no_print_functions = 0;
1506 static int no_print_builtins = 0;
1508 /* Called from print_binding_level through binding_table_foreach to
1509 print the content of binding ENTRY. DATA is a pointer to line offset
1510 marker. */
1511 static void
1512 bt_print_entry (binding_entry entry, void *data)
1514 int *p = (int *) data;
1515 int len;
1517 if (entry->name == NULL)
1518 len = 3;
1519 else if (entry->name == TYPE_IDENTIFIER (entry->type))
1520 len = 2;
1521 else
1522 len = 4;
1523 len = 4;
1525 *p += len;
1527 if (*p > 5)
1529 fprintf (stderr, "\n\t");
1530 *p = len;
1532 if (entry->name == NULL)
1534 print_node_brief (stderr, "<unnamed-typedef", entry->type, 0);
1535 fprintf (stderr, ">");
1537 else if (entry->name == TYPE_IDENTIFIER (entry->type))
1538 print_node_brief (stderr, "", entry->type, 0);
1539 else
1541 print_node_brief (stderr, "<typedef", entry->name, 0);
1542 print_node_brief (stderr, "", entry->type, 0);
1543 fprintf (stderr, ">");
1547 void
1548 print_binding_level (struct cp_binding_level* lvl)
1550 tree t;
1551 int i = 0, len;
1552 fprintf (stderr, " blocks=" HOST_PTR_PRINTF, (void *) lvl->blocks);
1553 if (lvl->more_cleanups_ok)
1554 fprintf (stderr, " more-cleanups-ok");
1555 if (lvl->have_cleanups)
1556 fprintf (stderr, " have-cleanups");
1557 fprintf (stderr, "\n");
1558 if (lvl->names)
1560 fprintf (stderr, " names:\t");
1561 /* We can probably fit 3 names to a line? */
1562 for (t = lvl->names; t; t = TREE_CHAIN (t))
1564 if (no_print_functions && (TREE_CODE (t) == FUNCTION_DECL))
1565 continue;
1566 if (no_print_builtins
1567 && (TREE_CODE (t) == TYPE_DECL)
1568 && (!strcmp (DECL_SOURCE_FILE (t),"<built-in>")))
1569 continue;
1571 /* Function decls tend to have longer names. */
1572 if (TREE_CODE (t) == FUNCTION_DECL)
1573 len = 3;
1574 else
1575 len = 2;
1576 i += len;
1577 if (i > 6)
1579 fprintf (stderr, "\n\t");
1580 i = len;
1582 print_node_brief (stderr, "", t, 0);
1583 if (t == error_mark_node)
1584 break;
1586 if (i)
1587 fprintf (stderr, "\n");
1589 if (lvl->type_decls)
1591 fprintf (stderr, " tags:\t");
1592 i = 0;
1593 binding_table_foreach (lvl->type_decls, bt_print_entry, &i);
1594 if (i)
1595 fprintf (stderr, "\n");
1597 if (lvl->class_shadowed)
1599 fprintf (stderr, " class-shadowed:");
1600 for (t = lvl->class_shadowed; t; t = TREE_CHAIN (t))
1602 fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
1604 fprintf (stderr, "\n");
1606 if (lvl->type_shadowed)
1608 fprintf (stderr, " type-shadowed:");
1609 for (t = lvl->type_shadowed; t; t = TREE_CHAIN (t))
1611 fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
1613 fprintf (stderr, "\n");
1617 void
1618 print_other_binding_stack (struct cp_binding_level *stack)
1620 struct cp_binding_level *level;
1621 for (level = stack; !global_scope_p (level); level = level->level_chain)
1623 fprintf (stderr, "binding level " HOST_PTR_PRINTF "\n", (void *) level);
1624 print_binding_level (level);
1628 void
1629 print_binding_stack (void)
1631 struct cp_binding_level *b;
1632 fprintf (stderr, "current_binding_level=" HOST_PTR_PRINTF
1633 "\nclass_binding_level=" HOST_PTR_PRINTF
1634 "\nNAMESPACE_LEVEL (global_namespace)=" HOST_PTR_PRINTF "\n",
1635 (void *) current_binding_level, (void *) class_binding_level,
1636 (void *) NAMESPACE_LEVEL (global_namespace));
1637 if (class_binding_level)
1639 for (b = class_binding_level; b; b = b->level_chain)
1640 if (b == current_binding_level)
1641 break;
1642 if (b)
1643 b = class_binding_level;
1644 else
1645 b = current_binding_level;
1647 else
1648 b = current_binding_level;
1649 print_other_binding_stack (b);
1650 fprintf (stderr, "global:\n");
1651 print_binding_level (NAMESPACE_LEVEL (global_namespace));
1654 /* Return the type associated with id. */
1656 tree
1657 identifier_type_value (tree id)
1659 timevar_push (TV_NAME_LOOKUP);
1660 /* There is no type with that name, anywhere. */
1661 if (REAL_IDENTIFIER_TYPE_VALUE (id) == NULL_TREE)
1662 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
1663 /* This is not the type marker, but the real thing. */
1664 if (REAL_IDENTIFIER_TYPE_VALUE (id) != global_type_node)
1665 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, REAL_IDENTIFIER_TYPE_VALUE (id));
1666 /* Have to search for it. It must be on the global level, now.
1667 Ask lookup_name not to return non-types. */
1668 id = lookup_name_real (id, 2, 1, 0, LOOKUP_COMPLAIN);
1669 if (id)
1670 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, TREE_TYPE (id));
1671 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
1674 /* Return the IDENTIFIER_GLOBAL_VALUE of T, for use in common code, since
1675 the definition of IDENTIFIER_GLOBAL_VALUE is different for C and C++. */
1677 tree
1678 identifier_global_value (tree t)
1680 return IDENTIFIER_GLOBAL_VALUE (t);
1683 /* Push a definition of struct, union or enum tag named ID. into
1684 binding_level B. DECL is a TYPE_DECL for the type. We assume that
1685 the tag ID is not already defined. */
1687 static void
1688 set_identifier_type_value_with_scope (tree id, tree decl, cxx_scope *b)
1690 tree type;
1692 if (b->kind != sk_namespace)
1694 /* Shadow the marker, not the real thing, so that the marker
1695 gets restored later. */
1696 tree old_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
1697 b->type_shadowed
1698 = tree_cons (id, old_type_value, b->type_shadowed);
1699 type = decl ? TREE_TYPE (decl) : NULL_TREE;
1701 else
1703 cxx_binding *binding =
1704 binding_for_name (NAMESPACE_LEVEL (current_namespace), id);
1705 if (decl)
1707 if (binding->value)
1708 supplement_binding (binding, decl);
1709 else
1710 binding->value = decl;
1712 else
1713 abort ();
1714 /* Store marker instead of real type. */
1715 type = global_type_node;
1717 SET_IDENTIFIER_TYPE_VALUE (id, type);
1720 /* As set_identifier_type_value_with_scope, but using
1721 current_binding_level. */
1723 void
1724 set_identifier_type_value (tree id, tree decl)
1726 set_identifier_type_value_with_scope (id, decl, current_binding_level);
1729 /* Return the name for the constructor (or destructor) for the
1730 specified class TYPE. When given a template, this routine doesn't
1731 lose the specialization. */
1733 tree
1734 constructor_name_full (tree type)
1736 type = TYPE_MAIN_VARIANT (type);
1737 if (CLASS_TYPE_P (type) && TYPE_WAS_ANONYMOUS (type)
1738 && TYPE_HAS_CONSTRUCTOR (type))
1739 return DECL_NAME (OVL_CURRENT (CLASSTYPE_CONSTRUCTORS (type)));
1740 else
1741 return TYPE_IDENTIFIER (type);
1744 /* Return the name for the constructor (or destructor) for the
1745 specified class. When given a template, return the plain
1746 unspecialized name. */
1748 tree
1749 constructor_name (tree type)
1751 tree name;
1752 name = constructor_name_full (type);
1753 if (IDENTIFIER_TEMPLATE (name))
1754 name = IDENTIFIER_TEMPLATE (name);
1755 return name;
1758 /* Returns TRUE if NAME is the name for the constructor for TYPE. */
1760 bool
1761 constructor_name_p (tree name, tree type)
1763 tree ctor_name;
1765 if (!name)
1766 return false;
1768 if (TREE_CODE (name) != IDENTIFIER_NODE)
1769 return false;
1771 ctor_name = constructor_name_full (type);
1772 if (name == ctor_name)
1773 return true;
1774 if (IDENTIFIER_TEMPLATE (ctor_name)
1775 && name == IDENTIFIER_TEMPLATE (ctor_name))
1776 return true;
1777 return false;
1780 /* Counter used to create anonymous type names. */
1782 static GTY(()) int anon_cnt;
1784 /* Return an IDENTIFIER which can be used as a name for
1785 anonymous structs and unions. */
1787 tree
1788 make_anon_name (void)
1790 char buf[32];
1792 sprintf (buf, ANON_AGGRNAME_FORMAT, anon_cnt++);
1793 return get_identifier (buf);
1796 /* Clear the TREE_PURPOSE slot of UTDs which have anonymous typenames.
1797 This keeps dbxout from getting confused. */
1799 void
1800 clear_anon_tags (void)
1802 struct cp_binding_level *b;
1803 static int last_cnt = 0;
1805 /* Fast out if no new anon names were declared. */
1806 if (last_cnt == anon_cnt)
1807 return;
1809 b = current_binding_level;
1810 while (b->kind == sk_cleanup)
1811 b = b->level_chain;
1812 if (b->type_decls != NULL)
1813 binding_table_remove_anonymous_types (b->type_decls);
1814 last_cnt = anon_cnt;
1817 /* Return (from the stack of) the BINDING, if any, established at SCOPE. */
1819 static inline cxx_binding *
1820 find_binding (cxx_scope *scope, cxx_binding *binding)
1822 timevar_push (TV_NAME_LOOKUP);
1824 for (; binding != NULL; binding = binding->previous)
1825 if (binding->scope == scope)
1826 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, binding);
1828 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, (cxx_binding *)0);
1831 /* Return the binding for NAME in SCOPE, if any. Otherwise, return NULL. */
1833 static inline cxx_binding *
1834 cxx_scope_find_binding_for_name (cxx_scope *scope, tree name)
1836 cxx_binding *b = IDENTIFIER_NAMESPACE_BINDINGS (name);
1837 if (b)
1839 /* Fold-in case where NAME is used only once. */
1840 if (scope == b->scope && b->previous == NULL)
1841 return b;
1842 return find_binding (scope, b);
1844 return NULL;
1847 /* Always returns a binding for name in scope. If no binding is
1848 found, make a new one. */
1850 static cxx_binding *
1851 binding_for_name (cxx_scope *scope, tree name)
1853 cxx_binding *result;
1855 result = cxx_scope_find_binding_for_name (scope, name);
1856 if (result)
1857 return result;
1858 /* Not found, make a new one. */
1859 result = cxx_binding_make (NULL, NULL);
1860 result->previous = IDENTIFIER_NAMESPACE_BINDINGS (name);
1861 result->scope = scope;
1862 result->is_local = false;
1863 result->value_is_inherited = false;
1864 IDENTIFIER_NAMESPACE_BINDINGS (name) = result;
1865 return result;
1868 /* Insert another USING_DECL into the current binding level, returning
1869 this declaration. If this is a redeclaration, do nothing, and
1870 return NULL_TREE if this not in namespace scope (in namespace
1871 scope, a using decl might extend any previous bindings). */
1873 tree
1874 push_using_decl (tree scope, tree name)
1876 tree decl;
1878 timevar_push (TV_NAME_LOOKUP);
1879 my_friendly_assert (TREE_CODE (scope) == NAMESPACE_DECL, 383);
1880 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 384);
1881 for (decl = current_binding_level->usings; decl; decl = TREE_CHAIN (decl))
1882 if (DECL_INITIAL (decl) == scope && DECL_NAME (decl) == name)
1883 break;
1884 if (decl)
1885 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP,
1886 namespace_bindings_p () ? decl : NULL_TREE);
1887 decl = build_lang_decl (USING_DECL, name, void_type_node);
1888 DECL_INITIAL (decl) = scope;
1889 TREE_CHAIN (decl) = current_binding_level->usings;
1890 current_binding_level->usings = decl;
1891 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
1894 /* Same as pushdecl, but define X in binding-level LEVEL. We rely on the
1895 caller to set DECL_CONTEXT properly. */
1897 tree
1898 pushdecl_with_scope (tree x, cxx_scope *level)
1900 struct cp_binding_level *b;
1901 tree function_decl = current_function_decl;
1903 timevar_push (TV_NAME_LOOKUP);
1904 current_function_decl = NULL_TREE;
1905 if (level->kind == sk_class)
1907 b = class_binding_level;
1908 class_binding_level = level;
1909 pushdecl_class_level (x);
1910 class_binding_level = b;
1912 else
1914 b = current_binding_level;
1915 current_binding_level = level;
1916 x = pushdecl (x);
1917 current_binding_level = b;
1919 current_function_decl = function_decl;
1920 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
1923 /* DECL is a FUNCTION_DECL for a non-member function, which may have
1924 other definitions already in place. We get around this by making
1925 the value of the identifier point to a list of all the things that
1926 want to be referenced by that name. It is then up to the users of
1927 that name to decide what to do with that list.
1929 DECL may also be a TEMPLATE_DECL, with a FUNCTION_DECL in its
1930 DECL_TEMPLATE_RESULT. It is dealt with the same way.
1932 FLAGS is a bitwise-or of the following values:
1933 PUSH_LOCAL: Bind DECL in the current scope, rather than at
1934 namespace scope.
1935 PUSH_USING: DECL is being pushed as the result of a using
1936 declaration.
1938 The value returned may be a previous declaration if we guessed wrong
1939 about what language DECL should belong to (C or C++). Otherwise,
1940 it's always DECL (and never something that's not a _DECL). */
1942 static tree
1943 push_overloaded_decl (tree decl, int flags)
1945 tree name = DECL_NAME (decl);
1946 tree old;
1947 tree new_binding;
1948 int doing_global = (namespace_bindings_p () || !(flags & PUSH_LOCAL));
1950 timevar_push (TV_NAME_LOOKUP);
1951 if (doing_global)
1952 old = namespace_binding (name, DECL_CONTEXT (decl));
1953 else
1954 old = lookup_name_current_level (name);
1956 if (old)
1958 if (TREE_CODE (old) == TYPE_DECL && DECL_ARTIFICIAL (old))
1960 tree t = TREE_TYPE (old);
1961 if (IS_AGGR_TYPE (t) && warn_shadow
1962 && (! DECL_IN_SYSTEM_HEADER (decl)
1963 || ! DECL_IN_SYSTEM_HEADER (old)))
1964 warning ("`%#D' hides constructor for `%#T'", decl, t);
1965 old = NULL_TREE;
1967 else if (is_overloaded_fn (old))
1969 tree tmp;
1971 for (tmp = old; tmp; tmp = OVL_NEXT (tmp))
1973 tree fn = OVL_CURRENT (tmp);
1975 if (TREE_CODE (tmp) == OVERLOAD && OVL_USED (tmp)
1976 && !(flags & PUSH_USING)
1977 && compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
1978 TYPE_ARG_TYPES (TREE_TYPE (decl))))
1979 error ("`%#D' conflicts with previous using declaration `%#D'",
1980 decl, fn);
1982 if (duplicate_decls (decl, fn) == fn)
1983 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, fn);
1986 else if (old == error_mark_node)
1987 /* Ignore the undefined symbol marker. */
1988 old = NULL_TREE;
1989 else
1991 cp_error_at ("previous non-function declaration `%#D'", old);
1992 error ("conflicts with function declaration `%#D'", decl);
1993 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
1997 if (old || TREE_CODE (decl) == TEMPLATE_DECL
1998 /* If it's a using declaration, we always need to build an OVERLOAD,
1999 because it's the only way to remember that the declaration comes
2000 from 'using', and have the lookup behave correctly. */
2001 || (flags & PUSH_USING))
2003 if (old && TREE_CODE (old) != OVERLOAD)
2004 new_binding = ovl_cons (decl, ovl_cons (old, NULL_TREE));
2005 else
2006 new_binding = ovl_cons (decl, old);
2007 if (flags & PUSH_USING)
2008 OVL_USED (new_binding) = 1;
2010 else
2011 /* NAME is not ambiguous. */
2012 new_binding = decl;
2014 if (doing_global)
2015 set_namespace_binding (name, current_namespace, new_binding);
2016 else
2018 /* We only create an OVERLOAD if there was a previous binding at
2019 this level, or if decl is a template. In the former case, we
2020 need to remove the old binding and replace it with the new
2021 binding. We must also run through the NAMES on the binding
2022 level where the name was bound to update the chain. */
2024 if (TREE_CODE (new_binding) == OVERLOAD && old)
2026 tree *d;
2028 for (d = &IDENTIFIER_BINDING (name)->scope->names;
2030 d = &TREE_CHAIN (*d))
2031 if (*d == old
2032 || (TREE_CODE (*d) == TREE_LIST
2033 && TREE_VALUE (*d) == old))
2035 if (TREE_CODE (*d) == TREE_LIST)
2036 /* Just replace the old binding with the new. */
2037 TREE_VALUE (*d) = new_binding;
2038 else
2039 /* Build a TREE_LIST to wrap the OVERLOAD. */
2040 *d = tree_cons (NULL_TREE, new_binding,
2041 TREE_CHAIN (*d));
2043 /* And update the cxx_binding node. */
2044 IDENTIFIER_BINDING (name)->value = new_binding;
2045 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
2048 /* We should always find a previous binding in this case. */
2049 abort ();
2052 /* Install the new binding. */
2053 push_local_binding (name, new_binding, flags);
2056 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
2059 /* Check a non-member using-declaration. Return the name and scope
2060 being used, and the USING_DECL, or NULL_TREE on failure. */
2062 static tree
2063 validate_nonmember_using_decl (tree decl, tree scope, tree name)
2065 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR)
2067 /* 7.3.3/5
2068 A using-declaration shall not name a template-id. */
2069 error ("a using-declaration cannot specify a template-id. Try `using %D'", name);
2070 return NULL_TREE;
2073 if (TREE_CODE (decl) == NAMESPACE_DECL)
2075 error ("namespace `%D' not allowed in using-declaration", decl);
2076 return NULL_TREE;
2079 if (TREE_CODE (decl) == SCOPE_REF)
2081 /* It's a nested name with template parameter dependent scope.
2082 This can only be using-declaration for class member. */
2083 error ("`%T' is not a namespace", TREE_OPERAND (decl, 0));
2084 return NULL_TREE;
2087 if (is_overloaded_fn (decl))
2088 decl = get_first_fn (decl);
2090 my_friendly_assert (DECL_P (decl), 20020908);
2092 /* [namespace.udecl]
2093 A using-declaration for a class member shall be a
2094 member-declaration. */
2095 if (TYPE_P (scope))
2097 error ("`%T' is not a namespace", scope);
2098 return NULL_TREE;
2101 /* Make a USING_DECL. */
2102 return push_using_decl (scope, name);
2105 /* Process local and global using-declarations. */
2107 static void
2108 do_nonmember_using_decl (tree scope, tree name, tree oldval, tree oldtype,
2109 tree *newval, tree *newtype)
2111 cxx_binding decls;
2113 *newval = *newtype = NULL_TREE;
2114 cxx_binding_clear (&decls);
2115 if (!qualified_lookup_using_namespace (name, scope, &decls, 0))
2116 /* Lookup error */
2117 return;
2119 if (!decls.value && !decls.type)
2121 error ("`%D' not declared", name);
2122 return;
2125 /* Check for using functions. */
2126 if (decls.value && is_overloaded_fn (decls.value))
2128 tree tmp, tmp1;
2130 if (oldval && !is_overloaded_fn (oldval))
2132 if (!DECL_IMPLICIT_TYPEDEF_P (oldval))
2133 error ("`%D' is already declared in this scope", name);
2134 oldval = NULL_TREE;
2137 *newval = oldval;
2138 for (tmp = decls.value; tmp; tmp = OVL_NEXT (tmp))
2140 tree new_fn = OVL_CURRENT (tmp);
2142 /* [namespace.udecl]
2144 If a function declaration in namespace scope or block
2145 scope has the same name and the same parameter types as a
2146 function introduced by a using declaration the program is
2147 ill-formed. */
2148 for (tmp1 = oldval; tmp1; tmp1 = OVL_NEXT (tmp1))
2150 tree old_fn = OVL_CURRENT (tmp1);
2152 if (new_fn == old_fn)
2153 /* The function already exists in the current namespace. */
2154 break;
2155 else if (OVL_USED (tmp1))
2156 continue; /* this is a using decl */
2157 else if (compparms (TYPE_ARG_TYPES (TREE_TYPE (new_fn)),
2158 TYPE_ARG_TYPES (TREE_TYPE (old_fn))))
2160 /* There was already a non-using declaration in
2161 this scope with the same parameter types. If both
2162 are the same extern "C" functions, that's ok. */
2163 if (decls_match (new_fn, old_fn))
2165 /* If the OLD_FN was a builtin, there is now a
2166 real declaration. */
2167 if (DECL_ANTICIPATED (old_fn))
2168 DECL_ANTICIPATED (old_fn) = 0;
2169 break;
2171 else if (!DECL_ANTICIPATED (old_fn))
2173 /* If the OLD_FN was really declared, the
2174 declarations don't match. */
2175 error ("`%D' is already declared in this scope", name);
2176 break;
2179 /* If the OLD_FN was not really there, just ignore
2180 it and keep going. */
2184 /* If we broke out of the loop, there's no reason to add
2185 this function to the using declarations for this
2186 scope. */
2187 if (tmp1)
2188 continue;
2190 *newval = build_overload (OVL_CURRENT (tmp), *newval);
2191 if (TREE_CODE (*newval) != OVERLOAD)
2192 *newval = ovl_cons (*newval, NULL_TREE);
2193 OVL_USED (*newval) = 1;
2196 else
2198 *newval = decls.value;
2199 if (oldval && !decls_match (*newval, oldval))
2200 error ("`%D' is already declared in this scope", name);
2203 *newtype = decls.type;
2204 if (oldtype && *newtype && !same_type_p (oldtype, *newtype))
2206 error ("using declaration `%D' introduced ambiguous type `%T'",
2207 name, oldtype);
2208 return;
2212 /* Process a using-declaration at function scope. */
2214 void
2215 do_local_using_decl (tree decl, tree scope, tree name)
2217 tree oldval, oldtype, newval, newtype;
2218 tree orig_decl = decl;
2220 decl = validate_nonmember_using_decl (decl, scope, name);
2221 if (decl == NULL_TREE)
2222 return;
2224 if (building_stmt_tree ()
2225 && at_function_scope_p ())
2226 add_decl_stmt (decl);
2228 oldval = lookup_name_current_level (name);
2229 oldtype = lookup_type_current_level (name);
2231 do_nonmember_using_decl (scope, name, oldval, oldtype, &newval, &newtype);
2233 if (newval)
2235 if (is_overloaded_fn (newval))
2237 tree fn, term;
2239 /* We only need to push declarations for those functions
2240 that were not already bound in the current level.
2241 The old value might be NULL_TREE, it might be a single
2242 function, or an OVERLOAD. */
2243 if (oldval && TREE_CODE (oldval) == OVERLOAD)
2244 term = OVL_FUNCTION (oldval);
2245 else
2246 term = oldval;
2247 for (fn = newval; fn && OVL_CURRENT (fn) != term;
2248 fn = OVL_NEXT (fn))
2249 push_overloaded_decl (OVL_CURRENT (fn),
2250 PUSH_LOCAL | PUSH_USING);
2252 else
2253 push_local_binding (name, newval, PUSH_USING);
2255 if (newtype)
2256 set_identifier_type_value (name, newtype);
2258 /* Emit debug info. */
2259 if (!processing_template_decl)
2260 cp_emit_debug_info_for_using (orig_decl, current_scope());
2263 /* Return the type that should be used when TYPE's name is preceded
2264 by a tag such as 'struct' or 'union', or null if the name cannot
2265 be used in this way.
2267 For example, when processing the third line of:
2269 struct A;
2270 typedef struct A A;
2271 struct A;
2273 lookup of A will find the typedef. Given A's typedef, this function
2274 will return the type associated with "struct A". For the tag to be
2275 anything other than TYPE, TYPE must be a typedef whose original type
2276 has the same name and context as TYPE itself.
2278 It is not valid for a typedef of an anonymous type to be used with
2279 an explicit tag:
2281 typedef struct { ... } B;
2282 struct B;
2284 Return null for this case. */
2286 static tree
2287 follow_tag_typedef (tree type)
2289 tree original;
2291 original = original_type (type);
2292 if (! TYPE_NAME (original))
2293 return NULL_TREE;
2294 if (TYPE_IDENTIFIER (original) == TYPE_IDENTIFIER (type)
2295 && (CP_DECL_CONTEXT (TYPE_NAME (original))
2296 == CP_DECL_CONTEXT (TYPE_NAME (type)))
2297 && !(CLASS_TYPE_P (original) && TYPE_WAS_ANONYMOUS (original)))
2298 return original;
2299 else
2300 return NULL_TREE;
2303 /* Given NAME, an IDENTIFIER_NODE,
2304 return the structure (or union or enum) definition for that name.
2305 Searches binding levels from its SCOPE up to the global level.
2306 If THISLEVEL_ONLY is nonzero, searches only the specified context
2307 (but skips any sk_cleanup contexts to find one that is
2308 meaningful for tags).
2309 FORM says which kind of type the caller wants;
2310 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
2311 If the wrong kind of type is found, and it's not a template, an error is
2312 reported. */
2314 tree
2315 lookup_tag (enum tree_code form, tree name,
2316 cxx_scope *binding_level, int thislevel_only)
2318 struct cp_binding_level *level;
2319 /* Nonzero if, we should look past a template parameter level, even
2320 if THISLEVEL_ONLY. */
2321 int allow_template_parms_p = 1;
2322 bool type_is_anonymous = ANON_AGGRNAME_P (name);
2324 timevar_push (TV_NAME_LOOKUP);
2325 for (level = binding_level; level; level = level->level_chain)
2327 tree tail;
2328 if (type_is_anonymous && level->type_decls != NULL)
2330 tree type = binding_table_find_anon_type (level->type_decls, name);
2331 /* There is no need for error checking here, because
2332 anon names are unique throughout the compilation. */
2333 if (type != NULL)
2334 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, type);
2336 else if (level->kind == sk_namespace)
2337 /* Do namespace lookup. */
2338 for (tail = current_namespace; 1; tail = CP_DECL_CONTEXT (tail))
2340 cxx_binding *binding =
2341 cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (tail), name);
2342 tree old;
2344 /* If we just skipped past a template parameter level,
2345 even though THISLEVEL_ONLY, and we find a template
2346 class declaration, then we use the _TYPE node for the
2347 template. See the example below. */
2348 if (thislevel_only && !allow_template_parms_p
2349 && binding && binding->value
2350 && DECL_CLASS_TEMPLATE_P (binding->value))
2351 old = binding->value;
2352 else if (binding)
2353 old = select_decl (binding, LOOKUP_PREFER_TYPES);
2354 else
2355 old = NULL_TREE;
2357 if (old)
2359 /* We've found something at this binding level. If it is
2360 a typedef, extract the tag it refers to. Lookup fails
2361 if the typedef doesn't refer to a taggable type. */
2362 old = TREE_TYPE (old);
2363 old = follow_tag_typedef (old);
2364 if (!old)
2365 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
2366 if (TREE_CODE (old) != form
2367 && (form == ENUMERAL_TYPE
2368 || TREE_CODE (old) == ENUMERAL_TYPE))
2370 error ("`%#D' redeclared as %C", old, form);
2371 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
2373 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, old);
2375 if (thislevel_only || tail == global_namespace)
2376 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
2378 else if (level->type_decls != NULL)
2380 binding_entry entry = binding_table_find (level->type_decls, name);
2381 if (entry != NULL)
2383 enum tree_code code = TREE_CODE (entry->type);
2385 if (code != form
2386 && (form == ENUMERAL_TYPE || code == ENUMERAL_TYPE))
2388 /* Definition isn't the kind we were looking for. */
2389 error ("`%#D' redeclared as %C", entry->type, form);
2390 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
2392 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, entry->type);
2395 if (thislevel_only && level->kind != sk_cleanup)
2397 if (level->kind == sk_template_parms && allow_template_parms_p)
2399 /* We must deal with cases like this:
2401 template <class T> struct S;
2402 template <class T> struct S {};
2404 When looking up `S', for the second declaration, we
2405 would like to find the first declaration. But, we
2406 are in the pseudo-global level created for the
2407 template parameters, rather than the (surrounding)
2408 namespace level. Thus, we keep going one more level,
2409 even though THISLEVEL_ONLY is nonzero. */
2410 allow_template_parms_p = 0;
2411 continue;
2413 else
2414 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
2417 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
2420 /* Given a type, find the tag that was defined for it and return the tag name.
2421 Otherwise return 0. However, the value can never be 0
2422 in the cases in which this is used.
2424 C++: If NAME is nonzero, this is the new name to install. This is
2425 done when replacing anonymous tags with real tag names. */
2427 tree
2428 lookup_tag_reverse (tree type, tree name)
2430 struct cp_binding_level *level;
2432 timevar_push (TV_NAME_LOOKUP);
2433 for (level = current_binding_level; level; level = level->level_chain)
2435 binding_entry entry = level->type_decls == NULL
2436 ? NULL
2437 : binding_table_reverse_maybe_remap (level->type_decls, type, name);
2438 if (entry)
2439 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, entry->name);
2441 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
2444 /* Returns true if ROOT (a namespace, class, or function) encloses
2445 CHILD. CHILD may be either a class type or a namespace. */
2447 bool
2448 is_ancestor (tree root, tree child)
2450 my_friendly_assert ((TREE_CODE (root) == NAMESPACE_DECL
2451 || TREE_CODE (root) == FUNCTION_DECL
2452 || CLASS_TYPE_P (root)), 20030307);
2453 my_friendly_assert ((TREE_CODE (child) == NAMESPACE_DECL
2454 || CLASS_TYPE_P (child)),
2455 20030307);
2457 /* The global namespace encloses everything. */
2458 if (root == global_namespace)
2459 return true;
2461 while (true)
2463 /* If we've run out of scopes, stop. */
2464 if (!child)
2465 return false;
2466 /* If we've reached the ROOT, it encloses CHILD. */
2467 if (root == child)
2468 return true;
2469 /* Go out one level. */
2470 if (TYPE_P (child))
2471 child = TYPE_NAME (child);
2472 child = DECL_CONTEXT (child);
2476 /* Enter the class or namespace scope indicated by T. Returns TRUE iff
2477 pop_scope should be called later to exit this scope. */
2479 bool
2480 push_scope (tree t)
2482 bool pop = true;
2484 if (TREE_CODE (t) == NAMESPACE_DECL)
2485 push_decl_namespace (t);
2486 else if (CLASS_TYPE_P (t))
2488 if (!at_class_scope_p ()
2489 || !same_type_p (current_class_type, t))
2490 push_nested_class (t);
2491 else
2492 /* T is the same as the current scope. There is therefore no
2493 need to re-enter the scope. Since we are not actually
2494 pushing a new scope, our caller should not call
2495 pop_scope. */
2496 pop = false;
2499 return pop;
2502 /* Leave scope pushed by push_scope. */
2504 void
2505 pop_scope (tree t)
2507 if (TREE_CODE (t) == NAMESPACE_DECL)
2508 pop_decl_namespace ();
2509 else if CLASS_TYPE_P (t)
2510 pop_nested_class ();
2513 /* Do a pushlevel for class declarations. */
2515 void
2516 pushlevel_class (void)
2518 if (ENABLE_SCOPE_CHECKING)
2519 is_class_level = 1;
2521 class_binding_level = begin_scope (sk_class, current_class_type);
2524 /* ...and a poplevel for class declarations. */
2526 void
2527 poplevel_class (void)
2529 struct cp_binding_level *level = class_binding_level;
2530 tree shadowed;
2532 timevar_push (TV_NAME_LOOKUP);
2533 my_friendly_assert (level != 0, 354);
2535 /* If we're leaving a toplevel class, don't bother to do the setting
2536 of IDENTIFIER_CLASS_VALUE to NULL_TREE, since first of all this slot
2537 shouldn't even be used when current_class_type isn't set, and second,
2538 if we don't touch it here, we're able to use the cache effect if the
2539 next time we're entering a class scope, it is the same class. */
2540 if (current_class_depth != 1)
2542 struct cp_binding_level* b;
2544 /* Clear out our IDENTIFIER_CLASS_VALUEs. */
2545 for (shadowed = level->class_shadowed;
2546 shadowed;
2547 shadowed = TREE_CHAIN (shadowed))
2548 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (shadowed)) = NULL_TREE;
2550 /* Find the next enclosing class, and recreate
2551 IDENTIFIER_CLASS_VALUEs appropriate for that class. */
2552 b = level->level_chain;
2553 while (b && b->kind != sk_class)
2554 b = b->level_chain;
2556 if (b)
2557 for (shadowed = b->class_shadowed;
2558 shadowed;
2559 shadowed = TREE_CHAIN (shadowed))
2561 cxx_binding *binding;
2563 binding = IDENTIFIER_BINDING (TREE_PURPOSE (shadowed));
2564 while (binding && binding->scope != b)
2565 binding = binding->previous;
2567 if (binding)
2568 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (shadowed))
2569 = binding->value;
2572 else
2573 /* Remember to save what IDENTIFIER's were bound in this scope so we
2574 can recover from cache misses. */
2576 previous_class_type = current_class_type;
2577 previous_class_values = class_binding_level->class_shadowed;
2579 for (shadowed = level->type_shadowed;
2580 shadowed;
2581 shadowed = TREE_CHAIN (shadowed))
2582 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (shadowed), TREE_VALUE (shadowed));
2584 /* Remove the bindings for all of the class-level declarations. */
2585 for (shadowed = level->class_shadowed;
2586 shadowed;
2587 shadowed = TREE_CHAIN (shadowed))
2588 pop_binding (TREE_PURPOSE (shadowed), TREE_TYPE (shadowed));
2590 /* Now, pop out of the binding level which we created up in the
2591 `pushlevel_class' routine. */
2592 if (ENABLE_SCOPE_CHECKING)
2593 is_class_level = 1;
2595 leave_scope ();
2596 timevar_pop (TV_NAME_LOOKUP);
2599 /* Bind DECL to ID in the class_binding_level. Returns nonzero if the
2600 binding was successful. */
2603 push_class_binding (tree id, tree decl)
2605 int result = 1;
2606 cxx_binding *binding = IDENTIFIER_BINDING (id);
2607 tree context;
2609 timevar_push (TV_NAME_LOOKUP);
2610 /* Note that we declared this value so that we can issue an error if
2611 this is an invalid redeclaration of a name already used for some
2612 other purpose. */
2613 note_name_declared_in_class (id, decl);
2615 if (binding && binding->scope == class_binding_level)
2616 /* Supplement the existing binding. */
2617 result = supplement_binding (IDENTIFIER_BINDING (id), decl);
2618 else
2619 /* Create a new binding. */
2620 push_binding (id, decl, class_binding_level);
2622 /* Update the IDENTIFIER_CLASS_VALUE for this ID to be the
2623 class-level declaration. Note that we do not use DECL here
2624 because of the possibility of the `struct stat' hack; if DECL is
2625 a class-name or enum-name we might prefer a field-name, or some
2626 such. */
2627 IDENTIFIER_CLASS_VALUE (id) = IDENTIFIER_BINDING (id)->value;
2629 /* If this is a binding from a base class, mark it as such. */
2630 binding = IDENTIFIER_BINDING (id);
2631 if (binding->value == decl && TREE_CODE (decl) != TREE_LIST)
2633 if (TREE_CODE (decl) == OVERLOAD)
2634 context = CP_DECL_CONTEXT (OVL_CURRENT (decl));
2635 else
2637 my_friendly_assert (DECL_P (decl), 0);
2638 context = context_for_name_lookup (decl);
2641 if (is_properly_derived_from (current_class_type, context))
2642 INHERITED_VALUE_BINDING_P (binding) = 1;
2643 else
2644 INHERITED_VALUE_BINDING_P (binding) = 0;
2646 else if (binding->value == decl)
2647 /* We only encounter a TREE_LIST when push_class_decls detects an
2648 ambiguity. Such an ambiguity can be overridden by a definition
2649 in this class. */
2650 INHERITED_VALUE_BINDING_P (binding) = 1;
2652 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, result);
2655 /* We are entering the scope of a class. Clear IDENTIFIER_CLASS_VALUE
2656 for any names in enclosing classes. */
2658 void
2659 clear_identifier_class_values (void)
2661 tree t;
2663 if (!class_binding_level)
2664 return;
2666 for (t = class_binding_level->class_shadowed;
2668 t = TREE_CHAIN (t))
2669 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (t)) = NULL_TREE;
2672 /* Make the declaration of X appear in CLASS scope. */
2674 bool
2675 pushdecl_class_level (tree x)
2677 tree name;
2678 bool is_valid = true;
2680 timevar_push (TV_NAME_LOOKUP);
2681 /* Get the name of X. */
2682 if (TREE_CODE (x) == OVERLOAD)
2683 name = DECL_NAME (get_first_fn (x));
2684 else
2685 name = DECL_NAME (x);
2687 if (name)
2689 is_valid = push_class_level_binding (name, x);
2690 if (TREE_CODE (x) == TYPE_DECL)
2691 set_identifier_type_value (name, x);
2693 else if (ANON_AGGR_TYPE_P (TREE_TYPE (x)))
2695 /* If X is an anonymous aggregate, all of its members are
2696 treated as if they were members of the class containing the
2697 aggregate, for naming purposes. */
2698 tree f;
2700 for (f = TYPE_FIELDS (TREE_TYPE (x)); f; f = TREE_CHAIN (f))
2702 location_t save_location = input_location;
2703 input_location = DECL_SOURCE_LOCATION (f);
2704 if (!pushdecl_class_level (f))
2705 is_valid = false;
2706 input_location = save_location;
2709 timevar_pop (TV_NAME_LOOKUP);
2711 return is_valid;
2714 /* Make the declaration(s) of X appear in CLASS scope under the name
2715 NAME. Returns true if the binding is valid. */
2717 bool
2718 push_class_level_binding (tree name, tree x)
2720 cxx_binding *binding;
2722 timevar_push (TV_NAME_LOOKUP);
2723 /* The class_binding_level will be NULL if x is a template
2724 parameter name in a member template. */
2725 if (!class_binding_level)
2726 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, true);
2728 /* Make sure that this new member does not have the same name
2729 as a template parameter. */
2730 if (TYPE_BEING_DEFINED (current_class_type))
2731 check_template_shadow (x);
2733 /* [class.mem]
2735 If T is the name of a class, then each of the following shall
2736 have a name different from T:
2738 -- every static data member of class T;
2740 -- every member of class T that is itself a type;
2742 -- every enumerator of every member of class T that is an
2743 enumerated type;
2745 -- every member of every anonymous union that is a member of
2746 class T.
2748 (Non-static data members were also forbidden to have the same
2749 name as T until TC1.) */
2750 if ((TREE_CODE (x) == VAR_DECL
2751 || TREE_CODE (x) == CONST_DECL
2752 || (TREE_CODE (x) == TYPE_DECL
2753 && !DECL_SELF_REFERENCE_P (x))
2754 /* A data member of an anonymous union. */
2755 || (TREE_CODE (x) == FIELD_DECL
2756 && DECL_CONTEXT (x) != current_class_type))
2757 && DECL_NAME (x) == constructor_name (current_class_type))
2759 tree scope = context_for_name_lookup (x);
2760 if (TYPE_P (scope) && same_type_p (scope, current_class_type))
2762 error ("`%D' has the same name as the class in which it is declared",
2764 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, false);
2768 /* If this declaration shadows a declaration from an enclosing
2769 class, then we will need to restore IDENTIFIER_CLASS_VALUE when
2770 we leave this class. Record the shadowed declaration here. */
2771 binding = IDENTIFIER_BINDING (name);
2772 if (binding && binding->value)
2774 tree bval = binding->value;
2775 tree old_decl = NULL_TREE;
2777 if (INHERITED_VALUE_BINDING_P (binding))
2779 /* If the old binding was from a base class, and was for a
2780 tag name, slide it over to make room for the new binding.
2781 The old binding is still visible if explicitly qualified
2782 with a class-key. */
2783 if (TREE_CODE (bval) == TYPE_DECL && DECL_ARTIFICIAL (bval)
2784 && !(TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x)))
2786 old_decl = binding->type;
2787 binding->type = bval;
2788 binding->value = NULL_TREE;
2789 INHERITED_VALUE_BINDING_P (binding) = 0;
2791 else
2792 old_decl = bval;
2794 else if (TREE_CODE (x) == OVERLOAD && is_overloaded_fn (bval))
2795 old_decl = bval;
2796 else if (TREE_CODE (x) == USING_DECL && TREE_CODE (bval) == USING_DECL)
2797 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, true);
2798 else if (TREE_CODE (x) == USING_DECL && is_overloaded_fn (bval))
2799 old_decl = bval;
2800 else if (TREE_CODE (bval) == USING_DECL && is_overloaded_fn (x))
2801 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, true);
2803 if (old_decl)
2805 tree shadow;
2807 /* Find the previous binding of name on the class-shadowed
2808 list, and update it. */
2809 for (shadow = class_binding_level->class_shadowed;
2810 shadow;
2811 shadow = TREE_CHAIN (shadow))
2812 if (TREE_PURPOSE (shadow) == name
2813 && TREE_TYPE (shadow) == old_decl)
2815 binding->value = x;
2816 INHERITED_VALUE_BINDING_P (binding) = 0;
2817 TREE_TYPE (shadow) = x;
2818 IDENTIFIER_CLASS_VALUE (name) = x;
2819 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, true);
2824 /* If we didn't replace an existing binding, put the binding on the
2825 stack of bindings for the identifier, and update the shadowed list. */
2826 if (push_class_binding (name, x))
2828 class_binding_level->class_shadowed
2829 = tree_cons (name, NULL,
2830 class_binding_level->class_shadowed);
2831 /* Record the value we are binding NAME to so that we can know
2832 what to pop later. */
2833 TREE_TYPE (class_binding_level->class_shadowed) = x;
2834 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, true);
2837 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, false);
2840 tree
2841 do_class_using_decl (tree decl)
2843 tree name, value, scope, type;
2845 if (TREE_CODE (decl) != SCOPE_REF
2846 || !TREE_OPERAND (decl, 0)
2847 || !TYPE_P (TREE_OPERAND (decl, 0)))
2849 error ("using-declaration for non-member at class scope");
2850 return NULL_TREE;
2852 scope = TREE_OPERAND (decl, 0);
2853 name = TREE_OPERAND (decl, 1);
2854 if (TREE_CODE (name) == BIT_NOT_EXPR)
2856 error ("using-declaration cannot name destructor");
2857 return NULL_TREE;
2859 if (TREE_CODE (name) == TYPE_DECL)
2860 name = DECL_NAME (name);
2861 else if (TREE_CODE (name) == TEMPLATE_DECL)
2862 name = DECL_NAME (name);
2863 else if (BASELINK_P (name))
2865 tree fns = BASELINK_FUNCTIONS (name);
2866 name = DECL_NAME (get_first_fn (fns));
2869 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 980716);
2871 /* Dependent using decls have a NULL type, non-dependent ones have a
2872 void type. */
2873 type = dependent_type_p (scope) ? NULL_TREE : void_type_node;
2874 value = build_lang_decl (USING_DECL, name, type);
2875 DECL_INITIAL (value) = scope;
2877 if (scope && !processing_template_decl)
2879 tree r;
2881 r = lookup_qualified_name (scope, name, false, false);
2882 if (r && TREE_CODE (r) != ERROR_MARK)
2883 cp_emit_debug_info_for_using (r, scope);
2885 return value;
2888 void
2889 set_class_shadows (tree shadows)
2891 class_binding_level->class_shadowed = shadows;
2894 /* Return the binding value for name in scope. */
2896 tree
2897 namespace_binding (tree name, tree scope)
2899 cxx_binding *binding;
2901 if (scope == NULL)
2902 scope = global_namespace;
2903 scope = ORIGINAL_NAMESPACE (scope);
2904 binding = cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (scope), name);
2906 return binding ? binding->value : NULL_TREE;
2909 /* Set the binding value for name in scope. */
2911 void
2912 set_namespace_binding (tree name, tree scope, tree val)
2914 cxx_binding *b;
2916 timevar_push (TV_NAME_LOOKUP);
2917 if (scope == NULL_TREE)
2918 scope = global_namespace;
2919 b = binding_for_name (NAMESPACE_LEVEL (scope), name);
2920 if (!b->value || TREE_CODE (val) == OVERLOAD || val == error_mark_node)
2921 b->value = val;
2922 else
2923 supplement_binding (b, val);
2924 timevar_pop (TV_NAME_LOOKUP);
2927 /* Compute the namespace where a declaration is defined. */
2929 static tree
2930 decl_namespace (tree decl)
2932 timevar_push (TV_NAME_LOOKUP);
2933 if (TYPE_P (decl))
2934 decl = TYPE_STUB_DECL (decl);
2935 while (DECL_CONTEXT (decl))
2937 decl = DECL_CONTEXT (decl);
2938 if (TREE_CODE (decl) == NAMESPACE_DECL)
2939 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
2940 if (TYPE_P (decl))
2941 decl = TYPE_STUB_DECL (decl);
2942 my_friendly_assert (DECL_P (decl), 390);
2945 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, global_namespace);
2948 /* Set the context of a declaration to scope. Complain if we are not
2949 outside scope. */
2951 void
2952 set_decl_namespace (tree decl, tree scope, bool friendp)
2954 tree old;
2956 /* Get rid of namespace aliases. */
2957 scope = ORIGINAL_NAMESPACE (scope);
2959 /* It is ok for friends to be qualified in parallel space. */
2960 if (!friendp && !is_ancestor (current_namespace, scope))
2961 error ("declaration of `%D' not in a namespace surrounding `%D'",
2962 decl, scope);
2963 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
2964 if (scope != current_namespace)
2966 /* See whether this has been declared in the namespace. */
2967 old = namespace_binding (DECL_NAME (decl), scope);
2968 if (!old)
2969 /* No old declaration at all. */
2970 goto complain;
2971 /* A template can be explicitly specialized in any namespace. */
2972 if (processing_explicit_instantiation)
2973 return;
2974 if (!is_overloaded_fn (decl))
2975 /* Don't compare non-function decls with decls_match here,
2976 since it can't check for the correct constness at this
2977 point. pushdecl will find those errors later. */
2978 return;
2979 /* Since decl is a function, old should contain a function decl. */
2980 if (!is_overloaded_fn (old))
2981 goto complain;
2982 if (processing_template_decl || processing_specialization)
2983 /* We have not yet called push_template_decl to turn a
2984 FUNCTION_DECL into a TEMPLATE_DECL, so the declarations
2985 won't match. But, we'll check later, when we construct the
2986 template. */
2987 return;
2988 if (is_overloaded_fn (old))
2990 for (; old; old = OVL_NEXT (old))
2991 if (decls_match (decl, OVL_CURRENT (old)))
2992 return;
2994 else
2995 if (decls_match (decl, old))
2996 return;
2998 else
2999 return;
3000 complain:
3001 error ("`%D' should have been declared inside `%D'",
3002 decl, scope);
3005 /* Return the namespace where the current declaration is declared. */
3007 tree
3008 current_decl_namespace (void)
3010 tree result;
3011 /* If we have been pushed into a different namespace, use it. */
3012 if (decl_namespace_list)
3013 return TREE_PURPOSE (decl_namespace_list);
3015 if (current_class_type)
3016 result = decl_namespace (TYPE_STUB_DECL (current_class_type));
3017 else if (current_function_decl)
3018 result = decl_namespace (current_function_decl);
3019 else
3020 result = current_namespace;
3021 return result;
3024 /* Push into the scope of the NAME namespace. If NAME is NULL_TREE, then we
3025 select a name that is unique to this compilation unit. */
3027 void
3028 push_namespace (tree name)
3030 tree d = NULL_TREE;
3031 int need_new = 1;
3032 int implicit_use = 0;
3033 bool anon = !name;
3035 timevar_push (TV_NAME_LOOKUP);
3037 /* We should not get here if the global_namespace is not yet constructed
3038 nor if NAME designates the global namespace: The global scope is
3039 constructed elsewhere. */
3040 my_friendly_assert (global_namespace != NULL && name != global_scope_name,
3041 20030531);
3043 if (anon)
3045 /* The name of anonymous namespace is unique for the translation
3046 unit. */
3047 if (!anonymous_namespace_name)
3048 anonymous_namespace_name = get_file_function_name ('N');
3049 name = anonymous_namespace_name;
3050 d = IDENTIFIER_NAMESPACE_VALUE (name);
3051 if (d)
3052 /* Reopening anonymous namespace. */
3053 need_new = 0;
3054 implicit_use = 1;
3056 else
3058 /* Check whether this is an extended namespace definition. */
3059 d = IDENTIFIER_NAMESPACE_VALUE (name);
3060 if (d != NULL_TREE && TREE_CODE (d) == NAMESPACE_DECL)
3062 need_new = 0;
3063 if (DECL_NAMESPACE_ALIAS (d))
3065 error ("namespace alias `%D' not allowed here, assuming `%D'",
3066 d, DECL_NAMESPACE_ALIAS (d));
3067 d = DECL_NAMESPACE_ALIAS (d);
3072 if (need_new)
3074 /* Make a new namespace, binding the name to it. */
3075 d = build_lang_decl (NAMESPACE_DECL, name, void_type_node);
3076 DECL_CONTEXT (d) = FROB_CONTEXT (current_namespace);
3077 pushdecl (d);
3078 if (anon)
3080 /* Clear DECL_NAME for the benefit of debugging back ends. */
3081 SET_DECL_ASSEMBLER_NAME (d, name);
3082 DECL_NAME (d) = NULL_TREE;
3084 begin_scope (sk_namespace, d);
3086 else
3087 resume_scope (NAMESPACE_LEVEL (d));
3089 if (implicit_use)
3090 do_using_directive (d);
3091 /* Enter the name space. */
3092 current_namespace = d;
3094 timevar_pop (TV_NAME_LOOKUP);
3097 /* Pop from the scope of the current namespace. */
3099 void
3100 pop_namespace (void)
3102 my_friendly_assert (current_namespace != global_namespace, 20010801);
3103 current_namespace = CP_DECL_CONTEXT (current_namespace);
3104 /* The binding level is not popped, as it might be re-opened later. */
3105 leave_scope ();
3108 /* Push into the scope of the namespace NS, even if it is deeply
3109 nested within another namespace. */
3111 void
3112 push_nested_namespace (tree ns)
3114 if (ns == global_namespace)
3115 push_to_top_level ();
3116 else
3118 push_nested_namespace (CP_DECL_CONTEXT (ns));
3119 push_namespace (DECL_NAME (ns));
3123 /* Pop back from the scope of the namespace NS, which was previously
3124 entered with push_nested_namespace. */
3126 void
3127 pop_nested_namespace (tree ns)
3129 timevar_push (TV_NAME_LOOKUP);
3130 while (ns != global_namespace)
3132 pop_namespace ();
3133 ns = CP_DECL_CONTEXT (ns);
3136 pop_from_top_level ();
3137 timevar_pop (TV_NAME_LOOKUP);
3140 /* Temporarily set the namespace for the current declaration. */
3142 void
3143 push_decl_namespace (tree decl)
3145 if (TREE_CODE (decl) != NAMESPACE_DECL)
3146 decl = decl_namespace (decl);
3147 decl_namespace_list = tree_cons (ORIGINAL_NAMESPACE (decl),
3148 NULL_TREE, decl_namespace_list);
3151 /* [namespace.memdef]/2 */
3153 void
3154 pop_decl_namespace (void)
3156 decl_namespace_list = TREE_CHAIN (decl_namespace_list);
3159 /* Return the namespace that is the common ancestor
3160 of two given namespaces. */
3162 static tree
3163 namespace_ancestor (tree ns1, tree ns2)
3165 timevar_push (TV_NAME_LOOKUP);
3166 if (is_ancestor (ns1, ns2))
3167 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, ns1);
3168 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP,
3169 namespace_ancestor (CP_DECL_CONTEXT (ns1), ns2));
3172 /* Process a namespace-alias declaration. */
3174 void
3175 do_namespace_alias (tree alias, tree namespace)
3177 if (TREE_CODE (namespace) != NAMESPACE_DECL)
3179 /* The parser did not find it, so it's not there. */
3180 error ("unknown namespace `%D'", namespace);
3181 return;
3184 namespace = ORIGINAL_NAMESPACE (namespace);
3186 /* Build the alias. */
3187 alias = build_lang_decl (NAMESPACE_DECL, alias, void_type_node);
3188 DECL_NAMESPACE_ALIAS (alias) = namespace;
3189 DECL_EXTERNAL (alias) = 1;
3190 pushdecl (alias);
3192 /* Emit debug info for namespace alias. */
3193 (*debug_hooks->global_decl) (alias);
3196 /* Like pushdecl, only it places X in the current namespace,
3197 if appropriate. */
3199 tree
3200 pushdecl_namespace_level (tree x)
3202 struct cp_binding_level *b = current_binding_level;
3203 tree t;
3205 timevar_push (TV_NAME_LOOKUP);
3206 t = pushdecl_with_scope (x, NAMESPACE_LEVEL (current_namespace));
3208 /* Now, the type_shadowed stack may screw us. Munge it so it does
3209 what we want. */
3210 if (TREE_CODE (x) == TYPE_DECL)
3212 tree name = DECL_NAME (x);
3213 tree newval;
3214 tree *ptr = (tree *)0;
3215 for (; !global_scope_p (b); b = b->level_chain)
3217 tree shadowed = b->type_shadowed;
3218 for (; shadowed; shadowed = TREE_CHAIN (shadowed))
3219 if (TREE_PURPOSE (shadowed) == name)
3221 ptr = &TREE_VALUE (shadowed);
3222 /* Can't break out of the loop here because sometimes
3223 a binding level will have duplicate bindings for
3224 PT names. It's gross, but I haven't time to fix it. */
3227 newval = TREE_TYPE (x);
3228 if (ptr == (tree *)0)
3230 /* @@ This shouldn't be needed. My test case "zstring.cc" trips
3231 up here if this is changed to an assertion. --KR */
3232 SET_IDENTIFIER_TYPE_VALUE (name, x);
3234 else
3236 *ptr = newval;
3239 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
3242 /* Insert USED into the using list of USER. Set INDIRECT_flag if this
3243 directive is not directly from the source. Also find the common
3244 ancestor and let our users know about the new namespace */
3245 static void
3246 add_using_namespace (tree user, tree used, bool indirect)
3248 tree t;
3249 timevar_push (TV_NAME_LOOKUP);
3250 /* Using oneself is a no-op. */
3251 if (user == used)
3253 timevar_pop (TV_NAME_LOOKUP);
3254 return;
3256 my_friendly_assert (TREE_CODE (user) == NAMESPACE_DECL, 380);
3257 my_friendly_assert (TREE_CODE (used) == NAMESPACE_DECL, 380);
3258 /* Check if we already have this. */
3259 t = purpose_member (used, DECL_NAMESPACE_USING (user));
3260 if (t != NULL_TREE)
3262 if (!indirect)
3263 /* Promote to direct usage. */
3264 TREE_INDIRECT_USING (t) = 0;
3265 timevar_pop (TV_NAME_LOOKUP);
3266 return;
3269 /* Add used to the user's using list. */
3270 DECL_NAMESPACE_USING (user)
3271 = tree_cons (used, namespace_ancestor (user, used),
3272 DECL_NAMESPACE_USING (user));
3274 TREE_INDIRECT_USING (DECL_NAMESPACE_USING (user)) = indirect;
3276 /* Add user to the used's users list. */
3277 DECL_NAMESPACE_USERS (used)
3278 = tree_cons (user, 0, DECL_NAMESPACE_USERS (used));
3280 /* Recursively add all namespaces used. */
3281 for (t = DECL_NAMESPACE_USING (used); t; t = TREE_CHAIN (t))
3282 /* indirect usage */
3283 add_using_namespace (user, TREE_PURPOSE (t), 1);
3285 /* Tell everyone using us about the new used namespaces. */
3286 for (t = DECL_NAMESPACE_USERS (user); t; t = TREE_CHAIN (t))
3287 add_using_namespace (TREE_PURPOSE (t), used, 1);
3288 timevar_pop (TV_NAME_LOOKUP);
3291 /* Process a using-declaration not appearing in class or local scope. */
3293 void
3294 do_toplevel_using_decl (tree decl, tree scope, tree name)
3296 tree oldval, oldtype, newval, newtype;
3297 tree orig_decl = decl;
3298 cxx_binding *binding;
3300 decl = validate_nonmember_using_decl (decl, scope, name);
3301 if (decl == NULL_TREE)
3302 return;
3304 binding = binding_for_name (NAMESPACE_LEVEL (current_namespace), name);
3306 oldval = binding->value;
3307 oldtype = binding->type;
3309 do_nonmember_using_decl (scope, name, oldval, oldtype, &newval, &newtype);
3311 /* Emit debug info. */
3312 if (!processing_template_decl)
3313 cp_emit_debug_info_for_using (orig_decl, current_namespace);
3315 /* Copy declarations found. */
3316 if (newval)
3317 binding->value = newval;
3318 if (newtype)
3319 binding->type = newtype;
3320 return;
3323 /* Process a using-directive. */
3325 void
3326 do_using_directive (tree namespace)
3328 tree context = NULL_TREE;
3330 if (building_stmt_tree ())
3331 add_stmt (build_stmt (USING_STMT, namespace));
3333 /* using namespace A::B::C; */
3334 if (TREE_CODE (namespace) == SCOPE_REF)
3335 namespace = TREE_OPERAND (namespace, 1);
3336 if (TREE_CODE (namespace) == IDENTIFIER_NODE)
3338 /* Lookup in lexer did not find a namespace. */
3339 if (!processing_template_decl)
3340 error ("namespace `%T' undeclared", namespace);
3341 return;
3343 if (TREE_CODE (namespace) != NAMESPACE_DECL)
3345 if (!processing_template_decl)
3346 error ("`%T' is not a namespace", namespace);
3347 return;
3349 namespace = ORIGINAL_NAMESPACE (namespace);
3350 if (!toplevel_bindings_p ())
3352 push_using_directive (namespace);
3353 context = current_scope ();
3355 else
3357 /* direct usage */
3358 add_using_namespace (current_namespace, namespace, 0);
3359 if (current_namespace != global_namespace)
3360 context = current_namespace;
3363 /* Emit debugging info. */
3364 if (!processing_template_decl)
3365 (*debug_hooks->imported_module_or_decl) (namespace, context);
3368 /* Deal with a using-directive seen by the parser. Currently we only
3369 handle attributes here, since they cannot appear inside a template. */
3371 void
3372 parse_using_directive (tree namespace, tree attribs)
3374 tree a;
3376 do_using_directive (namespace);
3378 for (a = attribs; a; a = TREE_CHAIN (a))
3380 tree name = TREE_PURPOSE (a);
3381 if (is_attribute_p ("strong", name))
3383 if (!toplevel_bindings_p ())
3384 error ("strong using only meaningful at namespace scope");
3385 else
3386 DECL_NAMESPACE_ASSOCIATIONS (namespace)
3387 = tree_cons (current_namespace, 0,
3388 DECL_NAMESPACE_ASSOCIATIONS (namespace));
3390 else
3391 warning ("`%D' attribute directive ignored", name);
3395 /* Like pushdecl, only it places X in the global scope if appropriate.
3396 Calls cp_finish_decl to register the variable, initializing it with
3397 *INIT, if INIT is non-NULL. */
3399 static tree
3400 pushdecl_top_level_1 (tree x, tree *init)
3402 timevar_push (TV_NAME_LOOKUP);
3403 push_to_top_level ();
3404 x = pushdecl_namespace_level (x);
3405 if (init)
3406 cp_finish_decl (x, *init, NULL_TREE, 0);
3407 pop_from_top_level ();
3408 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
3411 /* Like pushdecl, only it places X in the global scope if appropriate. */
3413 tree
3414 pushdecl_top_level (tree x)
3416 return pushdecl_top_level_1 (x, NULL);
3419 /* Like pushdecl, only it places X in the global scope if
3420 appropriate. Calls cp_finish_decl to register the variable,
3421 initializing it with INIT. */
3423 tree
3424 pushdecl_top_level_and_finish (tree x, tree init)
3426 return pushdecl_top_level_1 (x, &init);
3429 /* Combines two sets of overloaded functions into an OVERLOAD chain, removing
3430 duplicates. The first list becomes the tail of the result.
3432 The algorithm is O(n^2). We could get this down to O(n log n) by
3433 doing a sort on the addresses of the functions, if that becomes
3434 necessary. */
3436 static tree
3437 merge_functions (tree s1, tree s2)
3439 for (; s2; s2 = OVL_NEXT (s2))
3441 tree fn2 = OVL_CURRENT (s2);
3442 tree fns1;
3444 for (fns1 = s1; fns1; fns1 = OVL_NEXT (fns1))
3446 tree fn1 = OVL_CURRENT (fns1);
3448 /* If the function from S2 is already in S1, there is no
3449 need to add it again. For `extern "C"' functions, we
3450 might have two FUNCTION_DECLs for the same function, in
3451 different namespaces; again, we only need one of them. */
3452 if (fn1 == fn2
3453 || (DECL_EXTERN_C_P (fn1) && DECL_EXTERN_C_P (fn2)
3454 && DECL_NAME (fn1) == DECL_NAME (fn2)))
3455 break;
3458 /* If we exhausted all of the functions in S1, FN2 is new. */
3459 if (!fns1)
3460 s1 = build_overload (fn2, s1);
3462 return s1;
3465 /* This should return an error not all definitions define functions.
3466 It is not an error if we find two functions with exactly the
3467 same signature, only if these are selected in overload resolution.
3468 old is the current set of bindings, new the freshly-found binding.
3469 XXX Do we want to give *all* candidates in case of ambiguity?
3470 XXX In what way should I treat extern declarations?
3471 XXX I don't want to repeat the entire duplicate_decls here */
3473 static cxx_binding *
3474 ambiguous_decl (tree name, cxx_binding *old, cxx_binding *new, int flags)
3476 tree val, type;
3477 my_friendly_assert (old != NULL, 393);
3478 /* Copy the value. */
3479 val = new->value;
3480 if (val)
3481 switch (TREE_CODE (val))
3483 case TEMPLATE_DECL:
3484 /* If we expect types or namespaces, and not templates,
3485 or this is not a template class. */
3486 if (LOOKUP_QUALIFIERS_ONLY (flags)
3487 && !DECL_CLASS_TEMPLATE_P (val))
3488 val = NULL_TREE;
3489 break;
3490 case TYPE_DECL:
3491 if (LOOKUP_NAMESPACES_ONLY (flags))
3492 val = NULL_TREE;
3493 break;
3494 case NAMESPACE_DECL:
3495 if (LOOKUP_TYPES_ONLY (flags))
3496 val = NULL_TREE;
3497 break;
3498 case FUNCTION_DECL:
3499 /* Ignore built-in functions that are still anticipated. */
3500 if (LOOKUP_QUALIFIERS_ONLY (flags) || DECL_ANTICIPATED (val))
3501 val = NULL_TREE;
3502 break;
3503 default:
3504 if (LOOKUP_QUALIFIERS_ONLY (flags))
3505 val = NULL_TREE;
3508 if (!old->value)
3509 old->value = val;
3510 else if (val && val != old->value)
3512 if (is_overloaded_fn (old->value) && is_overloaded_fn (val))
3513 old->value = merge_functions (old->value, val);
3514 else
3516 /* Some declarations are functions, some are not. */
3517 if (flags & LOOKUP_COMPLAIN)
3519 /* If we've already given this error for this lookup,
3520 old->value is error_mark_node, so let's not
3521 repeat ourselves. */
3522 if (old->value != error_mark_node)
3524 error ("use of `%D' is ambiguous", name);
3525 cp_error_at (" first declared as `%#D' here",
3526 old->value);
3528 cp_error_at (" also declared as `%#D' here", val);
3530 old->value = error_mark_node;
3533 /* ... and copy the type. */
3534 type = new->type;
3535 if (LOOKUP_NAMESPACES_ONLY (flags))
3536 type = NULL_TREE;
3537 if (!old->type)
3538 old->type = type;
3539 else if (type && old->type != type)
3541 if (flags & LOOKUP_COMPLAIN)
3543 error ("`%D' denotes an ambiguous type",name);
3544 error ("%J first type here", TYPE_MAIN_DECL (old->type));
3545 error ("%J other type here", TYPE_MAIN_DECL (type));
3548 return old;
3551 /* Return the declarations that are members of the namespace NS. */
3553 tree
3554 cp_namespace_decls (tree ns)
3556 return NAMESPACE_LEVEL (ns)->names;
3559 /* Combine prefer_type and namespaces_only into flags. */
3561 static int
3562 lookup_flags (int prefer_type, int namespaces_only)
3564 if (namespaces_only)
3565 return LOOKUP_PREFER_NAMESPACES;
3566 if (prefer_type > 1)
3567 return LOOKUP_PREFER_TYPES;
3568 if (prefer_type > 0)
3569 return LOOKUP_PREFER_BOTH;
3570 return 0;
3573 /* Given a lookup that returned VAL, use FLAGS to decide if we want to
3574 ignore it or not. Subroutine of lookup_name_real. */
3576 static tree
3577 qualify_lookup (tree val, int flags)
3579 if (val == NULL_TREE)
3580 return val;
3581 if ((flags & LOOKUP_PREFER_NAMESPACES) && TREE_CODE (val) == NAMESPACE_DECL)
3582 return val;
3583 if ((flags & LOOKUP_PREFER_TYPES)
3584 && (TREE_CODE (val) == TYPE_DECL || TREE_CODE (val) == TEMPLATE_DECL))
3585 return val;
3586 if (flags & (LOOKUP_PREFER_NAMESPACES | LOOKUP_PREFER_TYPES))
3587 return NULL_TREE;
3588 return val;
3591 /* Look up NAME in the NAMESPACE. */
3593 tree
3594 lookup_namespace_name (tree namespace, tree name)
3596 tree val;
3597 tree template_id = NULL_TREE;
3598 cxx_binding binding;
3600 timevar_push (TV_NAME_LOOKUP);
3601 my_friendly_assert (TREE_CODE (namespace) == NAMESPACE_DECL, 370);
3603 if (TREE_CODE (name) == NAMESPACE_DECL)
3604 /* This happens for A::B<int> when B is a namespace. */
3605 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, name);
3606 else if (TREE_CODE (name) == TEMPLATE_DECL)
3608 /* This happens for A::B where B is a template, and there are no
3609 template arguments. */
3610 error ("invalid use of `%D'", name);
3611 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
3614 namespace = ORIGINAL_NAMESPACE (namespace);
3616 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
3618 template_id = name;
3619 name = TREE_OPERAND (name, 0);
3620 if (TREE_CODE (name) == OVERLOAD)
3621 name = DECL_NAME (OVL_CURRENT (name));
3622 else if (DECL_P (name))
3623 name = DECL_NAME (name);
3626 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 373);
3628 cxx_binding_clear (&binding);
3629 if (!qualified_lookup_using_namespace (name, namespace, &binding, 0))
3630 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
3632 if (binding.value)
3634 val = binding.value;
3636 if (template_id)
3638 if (DECL_CLASS_TEMPLATE_P (val))
3639 val = lookup_template_class (val,
3640 TREE_OPERAND (template_id, 1),
3641 /*in_decl=*/NULL_TREE,
3642 /*context=*/NULL_TREE,
3643 /*entering_scope=*/0,
3644 tf_error | tf_warning);
3645 else if (DECL_FUNCTION_TEMPLATE_P (val)
3646 || TREE_CODE (val) == OVERLOAD)
3647 val = lookup_template_function (val,
3648 TREE_OPERAND (template_id, 1));
3649 else
3651 error ("`%D::%D' is not a template",
3652 namespace, name);
3653 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
3657 /* If we have a single function from a using decl, pull it out. */
3658 if (TREE_CODE (val) == OVERLOAD && ! really_overloaded_fn (val))
3659 val = OVL_FUNCTION (val);
3661 /* Ignore built-in functions that haven't been prototyped yet. */
3662 if (!val || !DECL_P(val)
3663 || !DECL_LANG_SPECIFIC(val)
3664 || !DECL_ANTICIPATED (val))
3665 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
3668 error ("`%D' undeclared in namespace `%D'", name, namespace);
3669 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
3672 /* Select the right _DECL from multiple choices. */
3674 static tree
3675 select_decl (cxx_binding *binding, int flags)
3677 tree val;
3678 val = binding->value;
3680 timevar_push (TV_NAME_LOOKUP);
3681 if (LOOKUP_NAMESPACES_ONLY (flags))
3683 /* We are not interested in types. */
3684 if (val && TREE_CODE (val) == NAMESPACE_DECL)
3685 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
3686 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
3689 /* If looking for a type, or if there is no non-type binding, select
3690 the value binding. */
3691 if (binding->type && (!val || (flags & LOOKUP_PREFER_TYPES)))
3692 val = binding->type;
3693 /* Don't return non-types if we really prefer types. */
3694 else if (val && LOOKUP_TYPES_ONLY (flags) && TREE_CODE (val) != TYPE_DECL
3695 && (TREE_CODE (val) != TEMPLATE_DECL
3696 || !DECL_CLASS_TEMPLATE_P (val)))
3697 val = NULL_TREE;
3699 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
3702 /* Unscoped lookup of a global: iterate over current namespaces,
3703 considering using-directives. */
3705 static tree
3706 unqualified_namespace_lookup (tree name, int flags)
3708 tree initial = current_decl_namespace ();
3709 tree scope = initial;
3710 tree siter;
3711 struct cp_binding_level *level;
3712 tree val = NULL_TREE;
3713 cxx_binding binding;
3715 timevar_push (TV_NAME_LOOKUP);
3716 cxx_binding_clear (&binding);
3718 for (; !val; scope = CP_DECL_CONTEXT (scope))
3720 cxx_binding *b =
3721 cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (scope), name);
3723 /* Ignore anticipated built-in functions. */
3724 if (b && b->value && DECL_P (b->value)
3725 && DECL_LANG_SPECIFIC (b->value) && DECL_ANTICIPATED (b->value))
3726 /* Keep binding cleared. */;
3727 else if (b)
3729 /* Initialize binding for this context. */
3730 binding.value = b->value;
3731 binding.type = b->type;
3734 /* Add all _DECLs seen through local using-directives. */
3735 for (level = current_binding_level;
3736 level->kind != sk_namespace;
3737 level = level->level_chain)
3738 if (!lookup_using_namespace (name, &binding, level->using_directives,
3739 scope, flags))
3740 /* Give up because of error. */
3741 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
3743 /* Add all _DECLs seen through global using-directives. */
3744 /* XXX local and global using lists should work equally. */
3745 siter = initial;
3746 while (1)
3748 if (!lookup_using_namespace (name, &binding,
3749 DECL_NAMESPACE_USING (siter),
3750 scope, flags))
3751 /* Give up because of error. */
3752 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
3753 if (siter == scope) break;
3754 siter = CP_DECL_CONTEXT (siter);
3757 val = select_decl (&binding, flags);
3758 if (scope == global_namespace)
3759 break;
3761 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
3764 /* Look up NAME (an IDENTIFIER_NODE) in SCOPE (either a NAMESPACE_DECL
3765 or a class TYPE). If IS_TYPE_P is TRUE, then ignore non-type
3766 bindings.
3768 Returns a DECL (or OVERLOAD, or BASELINK) representing the
3769 declaration found. If no suitable declaration can be found,
3770 ERROR_MARK_NODE is returned. Iif COMPLAIN is true and SCOPE is
3771 neither a class-type nor a namespace a diagnostic is issued. */
3773 tree
3774 lookup_qualified_name (tree scope, tree name, bool is_type_p, bool complain)
3776 int flags = 0;
3778 if (TREE_CODE (scope) == NAMESPACE_DECL)
3780 cxx_binding binding;
3782 cxx_binding_clear (&binding);
3783 flags |= LOOKUP_COMPLAIN;
3784 if (is_type_p)
3785 flags |= LOOKUP_PREFER_TYPES;
3786 if (qualified_lookup_using_namespace (name, scope, &binding, flags))
3787 return select_decl (&binding, flags);
3789 else if (is_aggr_type (scope, complain))
3791 tree t;
3792 t = lookup_member (scope, name, 0, is_type_p);
3793 if (t)
3794 return t;
3797 return error_mark_node;
3800 /* Subroutine of unqualified_namespace_lookup:
3801 Add the bindings of NAME in used namespaces to VAL.
3802 We are currently looking for names in namespace SCOPE, so we
3803 look through USINGS for using-directives of namespaces
3804 which have SCOPE as a common ancestor with the current scope.
3805 Returns false on errors. */
3807 static bool
3808 lookup_using_namespace (tree name, cxx_binding *val, tree usings, tree scope,
3809 int flags)
3811 tree iter;
3812 timevar_push (TV_NAME_LOOKUP);
3813 /* Iterate over all used namespaces in current, searching for using
3814 directives of scope. */
3815 for (iter = usings; iter; iter = TREE_CHAIN (iter))
3816 if (TREE_VALUE (iter) == scope)
3818 tree used = ORIGINAL_NAMESPACE (TREE_PURPOSE (iter));
3819 cxx_binding *val1 =
3820 cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (used), name);
3821 /* Resolve ambiguities. */
3822 if (val1)
3823 val = ambiguous_decl (name, val, val1, flags);
3825 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val->value != error_mark_node);
3828 /* [namespace.qual]
3829 Accepts the NAME to lookup and its qualifying SCOPE.
3830 Returns the name/type pair found into the cxx_binding *RESULT,
3831 or false on error. */
3833 static bool
3834 qualified_lookup_using_namespace (tree name, tree scope, cxx_binding *result,
3835 int flags)
3837 /* Maintain a list of namespaces visited... */
3838 tree seen = NULL_TREE;
3839 /* ... and a list of namespace yet to see. */
3840 tree todo = NULL_TREE;
3841 tree todo_maybe = NULL_TREE;
3842 tree usings;
3843 timevar_push (TV_NAME_LOOKUP);
3844 /* Look through namespace aliases. */
3845 scope = ORIGINAL_NAMESPACE (scope);
3846 while (scope && result->value != error_mark_node)
3848 cxx_binding *binding =
3849 cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (scope), name);
3850 seen = tree_cons (scope, NULL_TREE, seen);
3851 if (binding)
3852 result = ambiguous_decl (name, result, binding, flags);
3854 /* Consider strong using directives always, and non-strong ones
3855 if we haven't found a binding yet. ??? Shouldn't we consider
3856 non-strong ones if the initial RESULT is non-NULL, but the
3857 binding in the given namespace is? */
3858 for (usings = DECL_NAMESPACE_USING (scope); usings;
3859 usings = TREE_CHAIN (usings))
3860 /* If this was a real directive, and we have not seen it. */
3861 if (!TREE_INDIRECT_USING (usings))
3863 /* Try to avoid queuing the same namespace more than once,
3864 the exception being when a namespace was already
3865 enqueued for todo_maybe and then a strong using is
3866 found for it. We could try to remove it from
3867 todo_maybe, but it's probably not worth the effort. */
3868 if (is_associated_namespace (scope, TREE_PURPOSE (usings))
3869 && !purpose_member (TREE_PURPOSE (usings), seen)
3870 && !purpose_member (TREE_PURPOSE (usings), todo))
3871 todo = tree_cons (TREE_PURPOSE (usings), NULL_TREE, todo);
3872 else if ((!result->value && !result->type)
3873 && !purpose_member (TREE_PURPOSE (usings), seen)
3874 && !purpose_member (TREE_PURPOSE (usings), todo)
3875 && !purpose_member (TREE_PURPOSE (usings), todo_maybe))
3876 todo_maybe = tree_cons (TREE_PURPOSE (usings), NULL_TREE,
3877 todo_maybe);
3879 if (todo)
3881 scope = TREE_PURPOSE (todo);
3882 todo = TREE_CHAIN (todo);
3884 else if (todo_maybe
3885 && (!result->value && !result->type))
3887 scope = TREE_PURPOSE (todo_maybe);
3888 todo = TREE_CHAIN (todo_maybe);
3889 todo_maybe = NULL_TREE;
3891 else
3892 scope = NULL_TREE; /* If there never was a todo list. */
3894 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, result->value != error_mark_node);
3897 /* Look up NAME in the current binding level and its superiors in the
3898 namespace of variables, functions and typedefs. Return a ..._DECL
3899 node of some kind representing its definition if there is only one
3900 such declaration, or return a TREE_LIST with all the overloaded
3901 definitions if there are many, or return 0 if it is undefined.
3903 If PREFER_TYPE is > 0, we prefer TYPE_DECLs or namespaces.
3904 If PREFER_TYPE is > 1, we reject non-type decls (e.g. namespaces).
3905 Otherwise we prefer non-TYPE_DECLs.
3907 If NONCLASS is nonzero, we don't look for the NAME in class scope,
3908 using IDENTIFIER_CLASS_VALUE. */
3910 tree
3911 lookup_name_real (tree name, int prefer_type, int nonclass,
3912 int namespaces_only, int flags)
3914 cxx_binding *iter;
3915 tree val = NULL_TREE;
3917 timevar_push (TV_NAME_LOOKUP);
3918 /* Conversion operators are handled specially because ordinary
3919 unqualified name lookup will not find template conversion
3920 operators. */
3921 if (IDENTIFIER_TYPENAME_P (name))
3923 struct cp_binding_level *level;
3925 for (level = current_binding_level;
3926 level && level->kind != sk_namespace;
3927 level = level->level_chain)
3929 tree class_type;
3930 tree operators;
3932 /* A conversion operator can only be declared in a class
3933 scope. */
3934 if (level->kind != sk_class)
3935 continue;
3937 /* Lookup the conversion operator in the class. */
3938 class_type = level->this_entity;
3939 operators = lookup_fnfields (class_type, name, /*protect=*/0);
3940 if (operators)
3941 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, operators);
3944 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
3947 flags |= lookup_flags (prefer_type, namespaces_only);
3949 /* First, look in non-namespace scopes. */
3951 if (current_class_type == NULL_TREE)
3952 nonclass = 1;
3954 for (iter = IDENTIFIER_BINDING (name); iter; iter = iter->previous)
3956 tree binding;
3958 if (!LOCAL_BINDING_P (iter) && nonclass)
3959 /* We're not looking for class-scoped bindings, so keep going. */
3960 continue;
3962 /* If this is the kind of thing we're looking for, we're done. */
3963 if (qualify_lookup (iter->value, flags))
3964 binding = iter->value;
3965 else if ((flags & LOOKUP_PREFER_TYPES)
3966 && qualify_lookup (iter->type, flags))
3967 binding = iter->type;
3968 else
3969 binding = NULL_TREE;
3971 if (binding)
3973 val = binding;
3974 break;
3978 /* Now lookup in namespace scopes. */
3979 if (!val)
3981 tree t = unqualified_namespace_lookup (name, flags);
3982 if (t)
3983 val = t;
3986 if (val)
3988 /* If we have a single function from a using decl, pull it out. */
3989 if (TREE_CODE (val) == OVERLOAD && ! really_overloaded_fn (val))
3990 val = OVL_FUNCTION (val);
3993 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
3996 tree
3997 lookup_name_nonclass (tree name)
3999 return lookup_name_real (name, 0, 1, 0, LOOKUP_COMPLAIN);
4002 tree
4003 lookup_function_nonclass (tree name, tree args)
4005 return lookup_arg_dependent (name, lookup_name_nonclass (name), args);
4008 tree
4009 lookup_name (tree name, int prefer_type)
4011 return lookup_name_real (name, prefer_type, 0, 0, LOOKUP_COMPLAIN);
4014 /* Similar to `lookup_name' but look only in the innermost non-class
4015 binding level. */
4017 static tree
4018 lookup_name_current_level (tree name)
4020 struct cp_binding_level *b;
4021 tree t = NULL_TREE;
4023 timevar_push (TV_NAME_LOOKUP);
4024 b = innermost_nonclass_level ();
4026 if (b->kind == sk_namespace)
4028 t = IDENTIFIER_NAMESPACE_VALUE (name);
4030 /* extern "C" function() */
4031 if (t != NULL_TREE && TREE_CODE (t) == TREE_LIST)
4032 t = TREE_VALUE (t);
4034 else if (IDENTIFIER_BINDING (name)
4035 && LOCAL_BINDING_P (IDENTIFIER_BINDING (name)))
4037 while (1)
4039 if (IDENTIFIER_BINDING (name)->scope == b)
4040 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, IDENTIFIER_VALUE (name));
4042 if (b->kind == sk_cleanup)
4043 b = b->level_chain;
4044 else
4045 break;
4049 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
4052 /* Like lookup_name_current_level, but for types. */
4054 static tree
4055 lookup_type_current_level (tree name)
4057 tree t = NULL_TREE;
4059 timevar_push (TV_NAME_LOOKUP);
4060 my_friendly_assert (current_binding_level->kind != sk_namespace,
4061 980716);
4063 if (REAL_IDENTIFIER_TYPE_VALUE (name) != NULL_TREE
4064 && REAL_IDENTIFIER_TYPE_VALUE (name) != global_type_node)
4066 struct cp_binding_level *b = current_binding_level;
4067 while (1)
4069 if (purpose_member (name, b->type_shadowed))
4070 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP,
4071 REAL_IDENTIFIER_TYPE_VALUE (name));
4072 if (b->kind == sk_cleanup)
4073 b = b->level_chain;
4074 else
4075 break;
4079 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
4082 /* [basic.lookup.koenig] */
4083 /* A nonzero return value in the functions below indicates an error. */
4085 struct arg_lookup
4087 tree name;
4088 tree namespaces;
4089 tree classes;
4090 tree functions;
4093 static bool arg_assoc (struct arg_lookup*, tree);
4094 static bool arg_assoc_args (struct arg_lookup*, tree);
4095 static bool arg_assoc_type (struct arg_lookup*, tree);
4096 static bool add_function (struct arg_lookup *, tree);
4097 static bool arg_assoc_namespace (struct arg_lookup *, tree);
4098 static bool arg_assoc_class (struct arg_lookup *, tree);
4099 static bool arg_assoc_template_arg (struct arg_lookup*, tree);
4101 /* Add a function to the lookup structure.
4102 Returns true on error. */
4104 static bool
4105 add_function (struct arg_lookup *k, tree fn)
4107 /* We used to check here to see if the function was already in the list,
4108 but that's O(n^2), which is just too expensive for function lookup.
4109 Now we deal with the occasional duplicate in joust. In doing this, we
4110 assume that the number of duplicates will be small compared to the
4111 total number of functions being compared, which should usually be the
4112 case. */
4114 /* We must find only functions, or exactly one non-function. */
4115 if (!k->functions)
4116 k->functions = fn;
4117 else if (fn == k->functions)
4119 else if (is_overloaded_fn (k->functions) && is_overloaded_fn (fn))
4120 k->functions = build_overload (fn, k->functions);
4121 else
4123 tree f1 = OVL_CURRENT (k->functions);
4124 tree f2 = fn;
4125 if (is_overloaded_fn (f1))
4127 fn = f1; f1 = f2; f2 = fn;
4129 cp_error_at ("`%D' is not a function,", f1);
4130 cp_error_at (" conflict with `%D'", f2);
4131 error (" in call to `%D'", k->name);
4132 return true;
4135 return false;
4138 /* Returns true iff CURRENT has declared itself to be an associated
4139 namespace of SCOPE via a strong using-directive (or transitive chain
4140 thereof). Both are namespaces. */
4142 bool
4143 is_associated_namespace (tree current, tree scope)
4145 tree seen = NULL_TREE;
4146 tree todo = NULL_TREE;
4147 tree t;
4148 while (1)
4150 if (scope == current)
4151 return true;
4152 seen = tree_cons (scope, NULL_TREE, seen);
4153 for (t = DECL_NAMESPACE_ASSOCIATIONS (scope); t; t = TREE_CHAIN (t))
4154 if (!purpose_member (TREE_PURPOSE (t), seen))
4155 todo = tree_cons (TREE_PURPOSE (t), NULL_TREE, todo);
4156 if (todo)
4158 scope = TREE_PURPOSE (todo);
4159 todo = TREE_CHAIN (todo);
4161 else
4162 return false;
4166 /* Add functions of a namespace to the lookup structure.
4167 Returns true on error. */
4169 static bool
4170 arg_assoc_namespace (struct arg_lookup *k, tree scope)
4172 tree value;
4174 if (purpose_member (scope, k->namespaces))
4175 return 0;
4176 k->namespaces = tree_cons (scope, NULL_TREE, k->namespaces);
4178 /* Check out our super-users. */
4179 for (value = DECL_NAMESPACE_ASSOCIATIONS (scope); value;
4180 value = TREE_CHAIN (value))
4181 if (arg_assoc_namespace (k, TREE_PURPOSE (value)))
4182 return true;
4184 value = namespace_binding (k->name, scope);
4185 if (!value)
4186 return false;
4188 for (; value; value = OVL_NEXT (value))
4189 if (add_function (k, OVL_CURRENT (value)))
4190 return true;
4192 return false;
4195 /* Adds everything associated with a template argument to the lookup
4196 structure. Returns true on error. */
4198 static bool
4199 arg_assoc_template_arg (struct arg_lookup *k, tree arg)
4201 /* [basic.lookup.koenig]
4203 If T is a template-id, its associated namespaces and classes are
4204 ... the namespaces and classes associated with the types of the
4205 template arguments provided for template type parameters
4206 (excluding template template parameters); the namespaces in which
4207 any template template arguments are defined; and the classes in
4208 which any member templates used as template template arguments
4209 are defined. [Note: non-type template arguments do not
4210 contribute to the set of associated namespaces. ] */
4212 /* Consider first template template arguments. */
4213 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
4214 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
4215 return false;
4216 else if (TREE_CODE (arg) == TEMPLATE_DECL)
4218 tree ctx = CP_DECL_CONTEXT (arg);
4220 /* It's not a member template. */
4221 if (TREE_CODE (ctx) == NAMESPACE_DECL)
4222 return arg_assoc_namespace (k, ctx);
4223 /* Otherwise, it must be member template. */
4224 else
4225 return arg_assoc_class (k, ctx);
4227 /* It's not a template template argument, but it is a type template
4228 argument. */
4229 else if (TYPE_P (arg))
4230 return arg_assoc_type (k, arg);
4231 /* It's a non-type template argument. */
4232 else
4233 return false;
4236 /* Adds everything associated with class to the lookup structure.
4237 Returns true on error. */
4239 static bool
4240 arg_assoc_class (struct arg_lookup *k, tree type)
4242 tree list, friends, context;
4243 int i;
4245 /* Backend build structures, such as __builtin_va_list, aren't
4246 affected by all this. */
4247 if (!CLASS_TYPE_P (type))
4248 return false;
4250 if (purpose_member (type, k->classes))
4251 return false;
4252 k->classes = tree_cons (type, NULL_TREE, k->classes);
4254 context = decl_namespace (TYPE_MAIN_DECL (type));
4255 if (arg_assoc_namespace (k, context))
4256 return true;
4258 /* Process baseclasses. */
4259 for (i = 0; i < CLASSTYPE_N_BASECLASSES (type); i++)
4260 if (arg_assoc_class (k, TYPE_BINFO_BASETYPE (type, i)))
4261 return true;
4263 /* Process friends. */
4264 for (list = DECL_FRIENDLIST (TYPE_MAIN_DECL (type)); list;
4265 list = TREE_CHAIN (list))
4266 if (k->name == FRIEND_NAME (list))
4267 for (friends = FRIEND_DECLS (list); friends;
4268 friends = TREE_CHAIN (friends))
4269 /* Only interested in global functions with potentially hidden
4270 (i.e. unqualified) declarations. */
4271 if (CP_DECL_CONTEXT (TREE_VALUE (friends)) == context)
4272 if (add_function (k, TREE_VALUE (friends)))
4273 return true;
4275 /* Process template arguments. */
4276 if (CLASSTYPE_TEMPLATE_INFO (type)
4277 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
4279 list = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
4280 for (i = 0; i < TREE_VEC_LENGTH (list); ++i)
4281 arg_assoc_template_arg (k, TREE_VEC_ELT (list, i));
4284 return false;
4287 /* Adds everything associated with a given type.
4288 Returns 1 on error. */
4290 static bool
4291 arg_assoc_type (struct arg_lookup *k, tree type)
4293 /* As we do not get the type of non-type dependent expressions
4294 right, we can end up with such things without a type. */
4295 if (!type)
4296 return false;
4298 if (TYPE_PTRMEM_P (type))
4300 /* Pointer to member: associate class type and value type. */
4301 if (arg_assoc_type (k, TYPE_PTRMEM_CLASS_TYPE (type)))
4302 return true;
4303 return arg_assoc_type (k, TYPE_PTRMEM_POINTED_TO_TYPE (type));
4305 else switch (TREE_CODE (type))
4307 case ERROR_MARK:
4308 return false;
4309 case VOID_TYPE:
4310 case INTEGER_TYPE:
4311 case REAL_TYPE:
4312 case COMPLEX_TYPE:
4313 case VECTOR_TYPE:
4314 case CHAR_TYPE:
4315 case BOOLEAN_TYPE:
4316 return false;
4317 case RECORD_TYPE:
4318 if (TYPE_PTRMEMFUNC_P (type))
4319 return arg_assoc_type (k, TYPE_PTRMEMFUNC_FN_TYPE (type));
4320 return arg_assoc_class (k, type);
4321 case POINTER_TYPE:
4322 case REFERENCE_TYPE:
4323 case ARRAY_TYPE:
4324 return arg_assoc_type (k, TREE_TYPE (type));
4325 case UNION_TYPE:
4326 case ENUMERAL_TYPE:
4327 return arg_assoc_namespace (k, decl_namespace (TYPE_MAIN_DECL (type)));
4328 case METHOD_TYPE:
4329 /* The basetype is referenced in the first arg type, so just
4330 fall through. */
4331 case FUNCTION_TYPE:
4332 /* Associate the parameter types. */
4333 if (arg_assoc_args (k, TYPE_ARG_TYPES (type)))
4334 return true;
4335 /* Associate the return type. */
4336 return arg_assoc_type (k, TREE_TYPE (type));
4337 case TEMPLATE_TYPE_PARM:
4338 case BOUND_TEMPLATE_TEMPLATE_PARM:
4339 return false;
4340 case TYPENAME_TYPE:
4341 return false;
4342 case LANG_TYPE:
4343 if (type == unknown_type_node)
4344 return false;
4345 /* else fall through */
4346 default:
4347 abort ();
4349 return false;
4352 /* Adds everything associated with arguments. Returns true on error. */
4354 static bool
4355 arg_assoc_args (struct arg_lookup *k, tree args)
4357 for (; args; args = TREE_CHAIN (args))
4358 if (arg_assoc (k, TREE_VALUE (args)))
4359 return true;
4360 return false;
4363 /* Adds everything associated with a given tree_node. Returns 1 on error. */
4365 static bool
4366 arg_assoc (struct arg_lookup *k, tree n)
4368 if (n == error_mark_node)
4369 return false;
4371 if (TYPE_P (n))
4372 return arg_assoc_type (k, n);
4374 if (! type_unknown_p (n))
4375 return arg_assoc_type (k, TREE_TYPE (n));
4377 if (TREE_CODE (n) == ADDR_EXPR)
4378 n = TREE_OPERAND (n, 0);
4379 if (TREE_CODE (n) == COMPONENT_REF)
4380 n = TREE_OPERAND (n, 1);
4381 if (TREE_CODE (n) == OFFSET_REF)
4382 n = TREE_OPERAND (n, 1);
4383 while (TREE_CODE (n) == TREE_LIST)
4384 n = TREE_VALUE (n);
4385 if (TREE_CODE (n) == BASELINK)
4386 n = BASELINK_FUNCTIONS (n);
4388 if (TREE_CODE (n) == FUNCTION_DECL)
4389 return arg_assoc_type (k, TREE_TYPE (n));
4390 if (TREE_CODE (n) == TEMPLATE_ID_EXPR)
4392 /* [basic.lookup.koenig]
4394 If T is a template-id, its associated namespaces and classes
4395 are the namespace in which the template is defined; for
4396 member templates, the member template's class... */
4397 tree template = TREE_OPERAND (n, 0);
4398 tree args = TREE_OPERAND (n, 1);
4399 tree ctx;
4400 int ix;
4402 if (TREE_CODE (template) == COMPONENT_REF)
4403 template = TREE_OPERAND (template, 1);
4405 /* First, the template. There may actually be more than one if
4406 this is an overloaded function template. But, in that case,
4407 we only need the first; all the functions will be in the same
4408 namespace. */
4409 template = OVL_CURRENT (template);
4411 ctx = CP_DECL_CONTEXT (template);
4413 if (TREE_CODE (ctx) == NAMESPACE_DECL)
4415 if (arg_assoc_namespace (k, ctx) == 1)
4416 return true;
4418 /* It must be a member template. */
4419 else if (arg_assoc_class (k, ctx) == 1)
4420 return true;
4422 /* Now the arguments. */
4423 for (ix = TREE_VEC_LENGTH (args); ix--;)
4424 if (arg_assoc_template_arg (k, TREE_VEC_ELT (args, ix)) == 1)
4425 return true;
4427 else
4429 my_friendly_assert (TREE_CODE (n) == OVERLOAD, 980715);
4431 for (; n; n = OVL_CHAIN (n))
4432 if (arg_assoc_type (k, TREE_TYPE (OVL_FUNCTION (n))))
4433 return true;
4436 return false;
4439 /* Performs Koenig lookup depending on arguments, where fns
4440 are the functions found in normal lookup. */
4442 tree
4443 lookup_arg_dependent (tree name, tree fns, tree args)
4445 struct arg_lookup k;
4446 tree fn = NULL_TREE;
4448 timevar_push (TV_NAME_LOOKUP);
4449 k.name = name;
4450 k.functions = fns;
4451 k.classes = NULL_TREE;
4453 /* We've already looked at some namespaces during normal unqualified
4454 lookup -- but we don't know exactly which ones. If the functions
4455 we found were brought into the current namespace via a using
4456 declaration, we have not really checked the namespace from which
4457 they came. Therefore, we check all namespaces here -- unless the
4458 function we have is from the current namespace. */
4459 if (fns)
4460 fn = OVL_CURRENT (fns);
4461 if (fn && TREE_CODE (fn) == FUNCTION_DECL
4462 && CP_DECL_CONTEXT (fn) != current_decl_namespace ())
4463 k.namespaces = NULL_TREE;
4464 else
4465 /* Setting NAMESPACES is purely an optimization; it prevents
4466 adding functions which are already in FNS. Adding them would
4467 be safe -- "joust" will eliminate the duplicates -- but
4468 wasteful. */
4469 k.namespaces = build_tree_list (current_decl_namespace (), NULL_TREE);
4471 arg_assoc_args (&k, args);
4472 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, k.functions);
4475 /* Add namespace to using_directives. Return NULL_TREE if nothing was
4476 changed (i.e. there was already a directive), or the fresh
4477 TREE_LIST otherwise. */
4479 static tree
4480 push_using_directive (tree used)
4482 tree ud = current_binding_level->using_directives;
4483 tree iter, ancestor;
4485 timevar_push (TV_NAME_LOOKUP);
4486 /* Check if we already have this. */
4487 if (purpose_member (used, ud) != NULL_TREE)
4488 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
4490 ancestor = namespace_ancestor (current_decl_namespace (), used);
4491 ud = current_binding_level->using_directives;
4492 ud = tree_cons (used, ancestor, ud);
4493 current_binding_level->using_directives = ud;
4495 /* Recursively add all namespaces used. */
4496 for (iter = DECL_NAMESPACE_USING (used); iter; iter = TREE_CHAIN (iter))
4497 push_using_directive (TREE_PURPOSE (iter));
4499 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, ud);
4502 /* The type TYPE is being declared. If it is a class template, or a
4503 specialization of a class template, do any processing required and
4504 perform error-checking. If IS_FRIEND is nonzero, this TYPE is
4505 being declared a friend. B is the binding level at which this TYPE
4506 should be bound.
4508 Returns the TYPE_DECL for TYPE, which may have been altered by this
4509 processing. */
4511 static tree
4512 maybe_process_template_type_declaration (tree type, int globalize,
4513 cxx_scope *b)
4515 tree decl = TYPE_NAME (type);
4517 if (processing_template_parmlist)
4518 /* You can't declare a new template type in a template parameter
4519 list. But, you can declare a non-template type:
4521 template <class A*> struct S;
4523 is a forward-declaration of `A'. */
4525 else
4527 maybe_check_template_type (type);
4529 my_friendly_assert (IS_AGGR_TYPE (type)
4530 || TREE_CODE (type) == ENUMERAL_TYPE, 0);
4533 if (processing_template_decl)
4535 /* This may change after the call to
4536 push_template_decl_real, but we want the original value. */
4537 tree name = DECL_NAME (decl);
4539 decl = push_template_decl_real (decl, globalize);
4540 /* If the current binding level is the binding level for the
4541 template parameters (see the comment in
4542 begin_template_parm_list) and the enclosing level is a class
4543 scope, and we're not looking at a friend, push the
4544 declaration of the member class into the class scope. In the
4545 friend case, push_template_decl will already have put the
4546 friend into global scope, if appropriate. */
4547 if (TREE_CODE (type) != ENUMERAL_TYPE
4548 && !globalize && b->kind == sk_template_parms
4549 && b->level_chain->kind == sk_class)
4551 finish_member_declaration (CLASSTYPE_TI_TEMPLATE (type));
4552 /* Put this UDT in the table of UDTs for the class, since
4553 that won't happen below because B is not the class
4554 binding level, but is instead the pseudo-global level. */
4555 if (b->level_chain->type_decls == NULL)
4556 b->level_chain->type_decls =
4557 binding_table_new (SCOPE_DEFAULT_HT_SIZE);
4558 binding_table_insert (b->level_chain->type_decls, name, type);
4559 if (!COMPLETE_TYPE_P (current_class_type))
4561 maybe_add_class_template_decl_list (current_class_type,
4562 type, /*friend_p=*/0);
4563 CLASSTYPE_NESTED_UTDS (current_class_type) =
4564 b->level_chain->type_decls;
4570 return decl;
4573 /* Push a tag name NAME for struct/class/union/enum type TYPE.
4574 Normally put it into the inner-most non-sk_cleanup scope,
4575 but if GLOBALIZE is true, put it in the inner-most non-class scope.
4576 The latter is needed for implicit declarations. */
4578 void
4579 pushtag (tree name, tree type, int globalize)
4581 struct cp_binding_level *b;
4583 timevar_push (TV_NAME_LOOKUP);
4584 b = current_binding_level;
4585 while (b->kind == sk_cleanup
4586 || (b->kind == sk_class
4587 && (globalize
4588 /* We may be defining a new type in the initializer
4589 of a static member variable. We allow this when
4590 not pedantic, and it is particularly useful for
4591 type punning via an anonymous union. */
4592 || COMPLETE_TYPE_P (b->this_entity))))
4593 b = b->level_chain;
4595 if (b->type_decls == NULL)
4596 b->type_decls = binding_table_new (SCOPE_DEFAULT_HT_SIZE);
4597 binding_table_insert (b->type_decls, name, type);
4599 if (name)
4601 /* Do C++ gratuitous typedefing. */
4602 if (IDENTIFIER_TYPE_VALUE (name) != type)
4604 tree d = NULL_TREE;
4605 int in_class = 0;
4606 tree context = TYPE_CONTEXT (type);
4608 if (! context)
4610 tree cs = current_scope ();
4612 if (! globalize)
4613 context = cs;
4614 else if (cs != NULL_TREE && TYPE_P (cs))
4615 /* When declaring a friend class of a local class, we want
4616 to inject the newly named class into the scope
4617 containing the local class, not the namespace scope. */
4618 context = decl_function_context (get_type_decl (cs));
4620 if (!context)
4621 context = current_namespace;
4623 if (b->kind == sk_class
4624 || (b->kind == sk_template_parms
4625 && b->level_chain->kind == sk_class))
4626 in_class = 1;
4628 if (current_lang_name == lang_name_java)
4629 TYPE_FOR_JAVA (type) = 1;
4631 d = create_implicit_typedef (name, type);
4632 DECL_CONTEXT (d) = FROB_CONTEXT (context);
4633 if (! in_class)
4634 set_identifier_type_value_with_scope (name, d, b);
4636 d = maybe_process_template_type_declaration (type,
4637 globalize, b);
4639 if (b->kind == sk_class)
4641 if (!PROCESSING_REAL_TEMPLATE_DECL_P ())
4642 /* Put this TYPE_DECL on the TYPE_FIELDS list for the
4643 class. But if it's a member template class, we
4644 want the TEMPLATE_DECL, not the TYPE_DECL, so this
4645 is done later. */
4646 finish_member_declaration (d);
4647 else
4648 pushdecl_class_level (d);
4650 else
4651 d = pushdecl_with_scope (d, b);
4653 /* FIXME what if it gets a name from typedef? */
4654 if (ANON_AGGRNAME_P (name))
4655 DECL_IGNORED_P (d) = 1;
4657 TYPE_CONTEXT (type) = DECL_CONTEXT (d);
4659 /* If this is a local class, keep track of it. We need this
4660 information for name-mangling, and so that it is possible to find
4661 all function definitions in a translation unit in a convenient
4662 way. (It's otherwise tricky to find a member function definition
4663 it's only pointed to from within a local class.) */
4664 if (TYPE_CONTEXT (type)
4665 && TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL
4666 && !processing_template_decl)
4667 VARRAY_PUSH_TREE (local_classes, type);
4669 if (b->kind == sk_class
4670 && !COMPLETE_TYPE_P (current_class_type))
4672 maybe_add_class_template_decl_list (current_class_type,
4673 type, /*friend_p=*/0);
4674 CLASSTYPE_NESTED_UTDS (current_class_type) = b->type_decls;
4678 if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL)
4679 /* Use the canonical TYPE_DECL for this node. */
4680 TYPE_STUB_DECL (type) = TYPE_NAME (type);
4681 else
4683 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE
4684 will be the tagged type we just added to the current
4685 binding level. This fake NULL-named TYPE_DECL node helps
4686 dwarfout.c to know when it needs to output a
4687 representation of a tagged type, and it also gives us a
4688 convenient place to record the "scope start" address for
4689 the tagged type. */
4691 tree d = build_decl (TYPE_DECL, NULL_TREE, type);
4692 TYPE_STUB_DECL (type) = pushdecl_with_scope (d, b);
4694 timevar_pop (TV_NAME_LOOKUP);
4697 /* Allocate storage for saving a C++ binding. */
4698 #define cxx_saved_binding_make() \
4699 (ggc_alloc (sizeof (cxx_saved_binding)))
4701 struct cxx_saved_binding GTY(())
4703 /* Link that chains saved C++ bindings for a given name into a stack. */
4704 cxx_saved_binding *previous;
4705 /* The name of the current binding. */
4706 tree identifier;
4707 /* The binding we're saving. */
4708 cxx_binding *binding;
4709 tree class_value;
4710 tree real_type_value;
4713 /* Subroutines for reverting temporarily to top-level for instantiation
4714 of templates and such. We actually need to clear out the class- and
4715 local-value slots of all identifiers, so that only the global values
4716 are at all visible. Simply setting current_binding_level to the global
4717 scope isn't enough, because more binding levels may be pushed. */
4718 struct saved_scope *scope_chain;
4720 static cxx_saved_binding *
4721 store_bindings (tree names, cxx_saved_binding *old_bindings)
4723 tree t;
4724 cxx_saved_binding *search_bindings = old_bindings;
4726 timevar_push (TV_NAME_LOOKUP);
4727 for (t = names; t; t = TREE_CHAIN (t))
4729 tree id;
4730 cxx_saved_binding *saved;
4731 cxx_saved_binding *t1;
4733 if (TREE_CODE (t) == TREE_LIST)
4734 id = TREE_PURPOSE (t);
4735 else
4736 id = DECL_NAME (t);
4738 if (!id
4739 /* Note that we may have an IDENTIFIER_CLASS_VALUE even when
4740 we have no IDENTIFIER_BINDING if we have left the class
4741 scope, but cached the class-level declarations. */
4742 || !(IDENTIFIER_BINDING (id) || IDENTIFIER_CLASS_VALUE (id)))
4743 continue;
4745 for (t1 = search_bindings; t1; t1 = t1->previous)
4746 if (t1->identifier == id)
4747 goto skip_it;
4749 my_friendly_assert (TREE_CODE (id) == IDENTIFIER_NODE, 135);
4750 saved = cxx_saved_binding_make ();
4751 saved->previous = old_bindings;
4752 saved->identifier = id;
4753 saved->binding = IDENTIFIER_BINDING (id);
4754 saved->class_value = IDENTIFIER_CLASS_VALUE (id);;
4755 saved->real_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
4756 IDENTIFIER_BINDING (id) = NULL;
4757 IDENTIFIER_CLASS_VALUE (id) = NULL_TREE;
4758 old_bindings = saved;
4759 skip_it:
4762 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, old_bindings);
4765 void
4766 push_to_top_level (void)
4768 struct saved_scope *s;
4769 struct cp_binding_level *b;
4770 cxx_saved_binding *old_bindings;
4771 int need_pop;
4773 timevar_push (TV_NAME_LOOKUP);
4774 s = ggc_alloc_cleared (sizeof (struct saved_scope));
4776 b = scope_chain ? current_binding_level : 0;
4778 /* If we're in the middle of some function, save our state. */
4779 if (cfun)
4781 need_pop = 1;
4782 push_function_context_to (NULL_TREE);
4784 else
4785 need_pop = 0;
4787 old_bindings = NULL;
4788 if (scope_chain && previous_class_type)
4789 old_bindings = store_bindings (previous_class_values, old_bindings);
4791 /* Have to include the global scope, because class-scope decls
4792 aren't listed anywhere useful. */
4793 for (; b; b = b->level_chain)
4795 tree t;
4797 /* Template IDs are inserted into the global level. If they were
4798 inserted into namespace level, finish_file wouldn't find them
4799 when doing pending instantiations. Therefore, don't stop at
4800 namespace level, but continue until :: . */
4801 if (global_scope_p (b))
4802 break;
4804 old_bindings = store_bindings (b->names, old_bindings);
4805 /* We also need to check class_shadowed to save class-level type
4806 bindings, since pushclass doesn't fill in b->names. */
4807 if (b->kind == sk_class)
4808 old_bindings = store_bindings (b->class_shadowed, old_bindings);
4810 /* Unwind type-value slots back to top level. */
4811 for (t = b->type_shadowed; t; t = TREE_CHAIN (t))
4812 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (t), TREE_VALUE (t));
4814 s->prev = scope_chain;
4815 s->old_bindings = old_bindings;
4816 s->bindings = b;
4817 s->need_pop_function_context = need_pop;
4818 s->function_decl = current_function_decl;
4819 s->last_parms = last_function_parms;
4821 scope_chain = s;
4822 current_function_decl = NULL_TREE;
4823 VARRAY_TREE_INIT (current_lang_base, 10, "current_lang_base");
4824 current_lang_name = lang_name_cplusplus;
4825 current_namespace = global_namespace;
4826 timevar_pop (TV_NAME_LOOKUP);
4829 void
4830 pop_from_top_level (void)
4832 struct saved_scope *s = scope_chain;
4833 cxx_saved_binding *saved;
4835 timevar_push (TV_NAME_LOOKUP);
4836 /* Clear out class-level bindings cache. */
4837 if (previous_class_type)
4838 invalidate_class_lookup_cache ();
4840 current_lang_base = 0;
4842 scope_chain = s->prev;
4843 for (saved = s->old_bindings; saved; saved = saved->previous)
4845 tree id = saved->identifier;
4847 IDENTIFIER_BINDING (id) = saved->binding;
4848 IDENTIFIER_CLASS_VALUE (id) = saved->class_value;
4849 SET_IDENTIFIER_TYPE_VALUE (id, saved->real_type_value);
4852 /* If we were in the middle of compiling a function, restore our
4853 state. */
4854 if (s->need_pop_function_context)
4855 pop_function_context_from (NULL_TREE);
4856 current_function_decl = s->function_decl;
4857 last_function_parms = s->last_parms;
4858 timevar_pop (TV_NAME_LOOKUP);
4861 /* Pop off extraneous binding levels left over due to syntax errors.
4863 We don't pop past namespaces, as they might be valid. */
4865 void
4866 pop_everything (void)
4868 if (ENABLE_SCOPE_CHECKING)
4869 verbatim ("XXX entering pop_everything ()\n");
4870 while (!toplevel_bindings_p ())
4872 if (current_binding_level->kind == sk_class)
4873 pop_nested_class ();
4874 else
4875 poplevel (0, 0, 0);
4877 if (ENABLE_SCOPE_CHECKING)
4878 verbatim ("XXX leaving pop_everything ()\n");
4881 /* Emit debugging information for using declarations and directives.
4882 If input tree is overloaded fn then emit debug info for all
4883 candidates. */
4885 static void
4886 cp_emit_debug_info_for_using (tree t, tree context)
4888 /* Ignore this FUNCTION_DECL if it refers to a builtin declaration
4889 of a builtin function. */
4890 if (TREE_CODE (t) == FUNCTION_DECL
4891 && DECL_EXTERNAL (t)
4892 && DECL_BUILT_IN (t))
4893 return;
4895 /* Do not supply context to imported_module_or_decl, if
4896 it is a global namespace. */
4897 if (context == global_namespace)
4898 context = NULL_TREE;
4900 if (BASELINK_P (t))
4901 t = BASELINK_FUNCTIONS (t);
4903 /* FIXME: Handle TEMPLATE_DECLs. */
4904 for (t = OVL_CURRENT (t); t; t = OVL_NEXT (t))
4905 if (TREE_CODE (t) != TEMPLATE_DECL)
4906 (*debug_hooks->imported_module_or_decl) (t, context);
4909 #include "gt-cp-name-lookup.h"