* trans-stmt.c (gfc_trans_simple_do): New function.
[official-gcc.git] / gcc / cp / name-lookup.c
blobb7e29aa55d3c01194a1b4f2371a51fc602da2dbd
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 /* The bindings for a particular name in a particular scope. */
37 struct scope_binding {
38 tree value;
39 tree type;
41 #define EMPTY_SCOPE_BINDING { NULL_TREE, NULL_TREE }
43 static cxx_scope *innermost_nonclass_level (void);
44 static tree select_decl (const struct scope_binding *, int);
45 static cxx_binding *binding_for_name (cxx_scope *, tree);
46 static tree lookup_name_current_level (tree);
47 static tree push_overloaded_decl (tree, int);
48 static bool lookup_using_namespace (tree, struct scope_binding *, tree,
49 tree, int);
50 static bool qualified_lookup_using_namespace (tree, tree,
51 struct scope_binding *, int);
52 static tree lookup_type_current_level (tree);
53 static tree push_using_directive (tree);
54 static void cp_emit_debug_info_for_using (tree, tree);
56 /* The :: namespace. */
58 tree global_namespace;
60 /* The name of the anonymous namespace, throughout this translation
61 unit. */
62 static GTY(()) tree anonymous_namespace_name;
65 /* Compute the chain index of a binding_entry given the HASH value of its
66 name and the total COUNT of chains. COUNT is assumed to be a power
67 of 2. */
69 #define ENTRY_INDEX(HASH, COUNT) (((HASH) >> 3) & ((COUNT) - 1))
71 /* A free list of "binding_entry"s awaiting for re-use. */
73 static GTY((deletable)) binding_entry free_binding_entry = NULL;
75 /* Create a binding_entry object for (NAME, TYPE). */
77 static inline binding_entry
78 binding_entry_make (tree name, tree type)
80 binding_entry entry;
82 if (free_binding_entry)
84 entry = free_binding_entry;
85 free_binding_entry = entry->chain;
87 else
88 entry = GGC_NEW (struct binding_entry_s);
90 entry->name = name;
91 entry->type = type;
92 entry->chain = NULL;
94 return entry;
97 /* Put ENTRY back on the free list. */
99 static inline void
100 binding_entry_free (binding_entry entry)
102 entry->name = NULL;
103 entry->type = NULL;
104 entry->chain = free_binding_entry;
105 free_binding_entry = entry;
108 /* The datatype used to implement the mapping from names to types at
109 a given scope. */
110 struct binding_table_s GTY(())
112 /* Array of chains of "binding_entry"s */
113 binding_entry * GTY((length ("%h.chain_count"))) chain;
115 /* The number of chains in this table. This is the length of the
116 the member "chain" considered as an array. */
117 size_t chain_count;
119 /* Number of "binding_entry"s in this table. */
120 size_t entry_count;
123 /* Construct TABLE with an initial CHAIN_COUNT. */
125 static inline void
126 binding_table_construct (binding_table table, size_t chain_count)
128 table->chain_count = chain_count;
129 table->entry_count = 0;
130 table->chain = GGC_CNEWVEC (binding_entry, table->chain_count);
133 /* Make TABLE's entries ready for reuse. */
135 static void
136 binding_table_free (binding_table table)
138 size_t i;
139 size_t count;
141 if (table == NULL)
142 return;
144 for (i = 0, count = table->chain_count; i < count; ++i)
146 binding_entry temp = table->chain[i];
147 while (temp != NULL)
149 binding_entry entry = temp;
150 temp = entry->chain;
151 binding_entry_free (entry);
153 table->chain[i] = NULL;
155 table->entry_count = 0;
158 /* Allocate a table with CHAIN_COUNT, assumed to be a power of two. */
160 static inline binding_table
161 binding_table_new (size_t chain_count)
163 binding_table table = GGC_NEW (struct binding_table_s);
164 table->chain = NULL;
165 binding_table_construct (table, chain_count);
166 return table;
169 /* Expand TABLE to twice its current chain_count. */
171 static void
172 binding_table_expand (binding_table table)
174 const size_t old_chain_count = table->chain_count;
175 const size_t old_entry_count = table->entry_count;
176 const size_t new_chain_count = 2 * old_chain_count;
177 binding_entry *old_chains = table->chain;
178 size_t i;
180 binding_table_construct (table, new_chain_count);
181 for (i = 0; i < old_chain_count; ++i)
183 binding_entry entry = old_chains[i];
184 for (; entry != NULL; entry = old_chains[i])
186 const unsigned int hash = IDENTIFIER_HASH_VALUE (entry->name);
187 const size_t j = ENTRY_INDEX (hash, new_chain_count);
189 old_chains[i] = entry->chain;
190 entry->chain = table->chain[j];
191 table->chain[j] = entry;
194 table->entry_count = old_entry_count;
197 /* Insert a binding for NAME to TYPE into TABLE. */
199 static void
200 binding_table_insert (binding_table table, tree name, tree type)
202 const unsigned int hash = IDENTIFIER_HASH_VALUE (name);
203 const size_t i = ENTRY_INDEX (hash, table->chain_count);
204 binding_entry entry = binding_entry_make (name, type);
206 entry->chain = table->chain[i];
207 table->chain[i] = entry;
208 ++table->entry_count;
210 if (3 * table->chain_count < 5 * table->entry_count)
211 binding_table_expand (table);
214 /* Return the binding_entry, if any, that maps NAME. */
216 binding_entry
217 binding_table_find (binding_table table, tree name)
219 const unsigned int hash = IDENTIFIER_HASH_VALUE (name);
220 binding_entry entry = table->chain[ENTRY_INDEX (hash, table->chain_count)];
222 while (entry != NULL && entry->name != name)
223 entry = entry->chain;
225 return entry;
228 /* Return the binding_entry, if any, that maps NAME to an anonymous type. */
230 static tree
231 binding_table_find_anon_type (binding_table table, tree name)
233 const unsigned int hash = IDENTIFIER_HASH_VALUE (name);
234 binding_entry entry = table->chain[ENTRY_INDEX (hash, table->chain_count)];
236 while (entry != NULL && TYPE_IDENTIFIER (entry->type) != name)
237 entry = entry->chain;
239 return entry ? entry->type : NULL;
242 /* Return the binding_entry, if any, that has TYPE as target. If NAME
243 is non-null, then set the domain and rehash that entry. */
245 static binding_entry
246 binding_table_reverse_maybe_remap (binding_table table, tree type, tree name)
248 const size_t chain_count = table->chain_count;
249 binding_entry entry = NULL;
250 binding_entry *p = NULL;
251 size_t i;
253 for (i = 0; i < chain_count && entry == NULL; ++i)
255 p = &table->chain[i];
256 while (*p != NULL && entry == NULL)
257 if ((*p)->type == type)
258 entry = *p;
259 else
260 p = &(*p)->chain;
263 if (entry != NULL && name != NULL && entry->name != name)
265 /* Remove the bucket from the previous chain. */
266 *p = (*p)->chain;
268 /* Remap the name type to type. */
269 i = ENTRY_INDEX (IDENTIFIER_HASH_VALUE (name), chain_count);
270 entry->chain = table->chain[i];
271 entry->name = name;
272 table->chain[i] = entry;
275 return entry;
278 /* Remove from TABLE all entries that map to anonymous enums or
279 class-types. */
281 void
282 binding_table_remove_anonymous_types (binding_table table)
284 const size_t chain_count = table->chain_count;
285 size_t i;
287 for (i = 0; i < chain_count; ++i)
289 binding_entry *p = &table->chain[i];
291 while (*p != NULL)
292 if (ANON_AGGRNAME_P ((*p)->name))
294 binding_entry e = *p;
295 *p = (*p)->chain;
296 --table->entry_count;
297 binding_entry_free (e);
299 else
300 p = &(*p)->chain;
304 /* Apply PROC -- with DATA -- to all entries in TABLE. */
306 void
307 binding_table_foreach (binding_table table, bt_foreach_proc proc, void *data)
309 const size_t chain_count = table->chain_count;
310 size_t i;
312 for (i = 0; i < chain_count; ++i)
314 binding_entry entry = table->chain[i];
315 for (; entry != NULL; entry = entry->chain)
316 proc (entry, data);
320 #ifndef ENABLE_SCOPE_CHECKING
321 # define ENABLE_SCOPE_CHECKING 0
322 #else
323 # define ENABLE_SCOPE_CHECKING 1
324 #endif
326 /* A free list of "cxx_binding"s, connected by their PREVIOUS. */
328 static GTY((deletable)) cxx_binding *free_bindings;
330 /* Initialize VALUE and TYPE field for BINDING, and set the PREVIOUS
331 field to NULL. */
333 static inline void
334 cxx_binding_init (cxx_binding *binding, tree value, tree type)
336 binding->value = value;
337 binding->type = type;
338 binding->previous = NULL;
341 /* (GC)-allocate a binding object with VALUE and TYPE member initialized. */
343 static cxx_binding *
344 cxx_binding_make (tree value, tree type)
346 cxx_binding *binding;
347 if (free_bindings)
349 binding = free_bindings;
350 free_bindings = binding->previous;
352 else
353 binding = GGC_NEW (cxx_binding);
355 cxx_binding_init (binding, value, type);
357 return binding;
360 /* Put BINDING back on the free list. */
362 static inline void
363 cxx_binding_free (cxx_binding *binding)
365 binding->scope = NULL;
366 binding->previous = free_bindings;
367 free_bindings = binding;
370 /* Create a new binding for NAME (with the indicated VALUE and TYPE
371 bindings) in the class scope indicated by SCOPE. */
373 static cxx_binding *
374 new_class_binding (tree name, tree value, tree type, cxx_scope *scope)
376 cp_class_binding *cb;
377 cxx_binding *binding;
379 if (VEC_length (cp_class_binding, scope->class_shadowed))
381 cp_class_binding *old_base;
382 old_base = VEC_index (cp_class_binding, scope->class_shadowed, 0);
383 if (VEC_reserve (cp_class_binding, scope->class_shadowed, -1))
385 /* Fixup the current bindings, as they might have moved. */
386 size_t i;
388 for (i = 0;
389 VEC_iterate (cp_class_binding, scope->class_shadowed, i, cb);
390 i++)
392 cxx_binding **b;
393 b = &IDENTIFIER_BINDING (cb->identifier);
394 while (*b != &old_base[i].base)
395 b = &((*b)->previous);
396 *b = &cb->base;
399 cb = VEC_quick_push (cp_class_binding, scope->class_shadowed, NULL);
401 else
402 cb = VEC_safe_push (cp_class_binding, scope->class_shadowed, NULL);
404 cb->identifier = name;
405 binding = &cb->base;
406 binding->scope = scope;
407 cxx_binding_init (binding, value, type);
408 return binding;
411 /* Make DECL the innermost binding for ID. The LEVEL is the binding
412 level at which this declaration is being bound. */
414 static void
415 push_binding (tree id, tree decl, cxx_scope* level)
417 cxx_binding *binding;
419 if (level != class_binding_level)
421 binding = cxx_binding_make (decl, NULL_TREE);
422 binding->scope = level;
424 else
425 binding = new_class_binding (id, decl, /*type=*/NULL_TREE, level);
427 /* Now, fill in the binding information. */
428 binding->previous = IDENTIFIER_BINDING (id);
429 INHERITED_VALUE_BINDING_P (binding) = 0;
430 LOCAL_BINDING_P (binding) = (level != class_binding_level);
432 /* And put it on the front of the list of bindings for ID. */
433 IDENTIFIER_BINDING (id) = binding;
436 /* Remove the binding for DECL which should be the innermost binding
437 for ID. */
439 void
440 pop_binding (tree id, tree decl)
442 cxx_binding *binding;
444 if (id == NULL_TREE)
445 /* It's easiest to write the loops that call this function without
446 checking whether or not the entities involved have names. We
447 get here for such an entity. */
448 return;
450 /* Get the innermost binding for ID. */
451 binding = IDENTIFIER_BINDING (id);
453 /* The name should be bound. */
454 gcc_assert (binding != NULL);
456 /* The DECL will be either the ordinary binding or the type
457 binding for this identifier. Remove that binding. */
458 if (binding->value == decl)
459 binding->value = NULL_TREE;
460 else
462 gcc_assert (binding->type == decl);
463 binding->type = NULL_TREE;
466 if (!binding->value && !binding->type)
468 /* We're completely done with the innermost binding for this
469 identifier. Unhook it from the list of bindings. */
470 IDENTIFIER_BINDING (id) = binding->previous;
472 /* Add it to the free list. */
473 cxx_binding_free (binding);
477 /* BINDING records an existing declaration for a namein the current scope.
478 But, DECL is another declaration for that same identifier in the
479 same scope. This is the `struct stat' hack whereby a non-typedef
480 class name or enum-name can be bound at the same level as some other
481 kind of entity.
482 3.3.7/1
484 A class name (9.1) or enumeration name (7.2) can be hidden by the
485 name of an object, function, or enumerator declared in the same scope.
486 If a class or enumeration name and an object, function, or enumerator
487 are declared in the same scope (in any order) with the same name, the
488 class or enumeration name is hidden wherever the object, function, or
489 enumerator name is visible.
491 It's the responsibility of the caller to check that
492 inserting this name is valid here. Returns nonzero if the new binding
493 was successful. */
495 static bool
496 supplement_binding (cxx_binding *binding, tree decl)
498 tree bval = binding->value;
499 bool ok = true;
501 timevar_push (TV_NAME_LOOKUP);
502 if (TREE_CODE (decl) == TYPE_DECL && DECL_ARTIFICIAL (decl))
503 /* The new name is the type name. */
504 binding->type = decl;
505 else if (/* BVAL is null when push_class_level_binding moves an
506 inherited type-binding out of the way to make room for a
507 new value binding. */
508 !bval
509 /* BVAL is error_mark_node when DECL's name has been used
510 in a non-class scope prior declaration. In that case,
511 we should have already issued a diagnostic; for graceful
512 error recovery purpose, pretend this was the intended
513 declaration for that name. */
514 || bval == error_mark_node
515 /* If BVAL is a built-in that has not yet been declared,
516 pretend it is not there at all. */
517 || (TREE_CODE (bval) == FUNCTION_DECL
518 && DECL_ANTICIPATED (bval)))
519 binding->value = decl;
520 else if (TREE_CODE (bval) == TYPE_DECL && DECL_ARTIFICIAL (bval))
522 /* The old binding was a type name. It was placed in
523 VALUE field because it was thought, at the point it was
524 declared, to be the only entity with such a name. Move the
525 type name into the type slot; it is now hidden by the new
526 binding. */
527 binding->type = bval;
528 binding->value = decl;
529 binding->value_is_inherited = false;
531 else if (TREE_CODE (bval) == TYPE_DECL
532 && TREE_CODE (decl) == TYPE_DECL
533 && DECL_NAME (decl) == DECL_NAME (bval)
534 && (same_type_p (TREE_TYPE (decl), TREE_TYPE (bval))
535 /* If either type involves template parameters, we must
536 wait until instantiation. */
537 || uses_template_parms (TREE_TYPE (decl))
538 || uses_template_parms (TREE_TYPE (bval))))
539 /* We have two typedef-names, both naming the same type to have
540 the same name. This is OK because of:
542 [dcl.typedef]
544 In a given scope, a typedef specifier can be used to redefine
545 the name of any type declared in that scope to refer to the
546 type to which it already refers. */
547 ok = false;
548 /* There can be two block-scope declarations of the same variable,
549 so long as they are `extern' declarations. However, there cannot
550 be two declarations of the same static data member:
552 [class.mem]
554 A member shall not be declared twice in the
555 member-specification. */
556 else if (TREE_CODE (decl) == VAR_DECL && TREE_CODE (bval) == VAR_DECL
557 && DECL_EXTERNAL (decl) && DECL_EXTERNAL (bval)
558 && !DECL_CLASS_SCOPE_P (decl))
560 duplicate_decls (decl, binding->value);
561 ok = false;
563 else if (TREE_CODE (decl) == NAMESPACE_DECL
564 && TREE_CODE (bval) == NAMESPACE_DECL
565 && DECL_NAMESPACE_ALIAS (decl)
566 && DECL_NAMESPACE_ALIAS (bval)
567 && ORIGINAL_NAMESPACE (bval) == ORIGINAL_NAMESPACE (decl))
568 /* [namespace.alias]
570 In a declarative region, a namespace-alias-definition can be
571 used to redefine a namespace-alias declared in that declarative
572 region to refer only to the namespace to which it already
573 refers. */
574 ok = false;
575 else
577 error ("declaration of %q#D", decl);
578 cp_error_at ("conflicts with previous declaration %q#D", bval);
579 ok = false;
582 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, ok);
585 /* Add DECL to the list of things declared in B. */
587 static void
588 add_decl_to_level (tree decl, cxx_scope *b)
590 if (TREE_CODE (decl) == NAMESPACE_DECL
591 && !DECL_NAMESPACE_ALIAS (decl))
593 TREE_CHAIN (decl) = b->namespaces;
594 b->namespaces = decl;
596 else if (TREE_CODE (decl) == VAR_DECL && DECL_VIRTUAL_P (decl))
598 TREE_CHAIN (decl) = b->vtables;
599 b->vtables = decl;
601 else
603 /* We build up the list in reverse order, and reverse it later if
604 necessary. */
605 TREE_CHAIN (decl) = b->names;
606 b->names = decl;
607 b->names_size++;
609 /* If appropriate, add decl to separate list of statics. We
610 include extern variables because they might turn out to be
611 static later. It's OK for this list to contain a few false
612 positives. */
613 if (b->kind == sk_namespace)
614 if ((TREE_CODE (decl) == VAR_DECL
615 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
616 || (TREE_CODE (decl) == FUNCTION_DECL
617 && (!TREE_PUBLIC (decl) || DECL_DECLARED_INLINE_P (decl))))
618 VARRAY_PUSH_TREE (b->static_decls, decl);
622 /* Record a decl-node X as belonging to the current lexical scope.
623 Check for errors (such as an incompatible declaration for the same
624 name already seen in the same scope).
626 Returns either X or an old decl for the same name.
627 If an old decl is returned, it may have been smashed
628 to agree with what X says. */
630 tree
631 pushdecl (tree x)
633 tree t;
634 tree name;
635 int need_new_binding;
637 timevar_push (TV_NAME_LOOKUP);
639 need_new_binding = 1;
641 if (DECL_TEMPLATE_PARM_P (x))
642 /* Template parameters have no context; they are not X::T even
643 when declared within a class or namespace. */
645 else
647 if (current_function_decl && x != current_function_decl
648 /* A local declaration for a function doesn't constitute
649 nesting. */
650 && TREE_CODE (x) != FUNCTION_DECL
651 /* A local declaration for an `extern' variable is in the
652 scope of the current namespace, not the current
653 function. */
654 && !(TREE_CODE (x) == VAR_DECL && DECL_EXTERNAL (x))
655 && !DECL_CONTEXT (x))
656 DECL_CONTEXT (x) = current_function_decl;
658 /* If this is the declaration for a namespace-scope function,
659 but the declaration itself is in a local scope, mark the
660 declaration. */
661 if (TREE_CODE (x) == FUNCTION_DECL
662 && DECL_NAMESPACE_SCOPE_P (x)
663 && current_function_decl
664 && x != current_function_decl)
665 DECL_LOCAL_FUNCTION_P (x) = 1;
668 name = DECL_NAME (x);
669 if (name)
671 int different_binding_level = 0;
673 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
674 name = TREE_OPERAND (name, 0);
676 /* In case this decl was explicitly namespace-qualified, look it
677 up in its namespace context. */
678 if (DECL_NAMESPACE_SCOPE_P (x) && namespace_bindings_p ())
679 t = namespace_binding (name, DECL_CONTEXT (x));
680 else
681 t = lookup_name_current_level (name);
683 /* [basic.link] If there is a visible declaration of an entity
684 with linkage having the same name and type, ignoring entities
685 declared outside the innermost enclosing namespace scope, the
686 block scope declaration declares that same entity and
687 receives the linkage of the previous declaration. */
688 if (! t && current_function_decl && x != current_function_decl
689 && (TREE_CODE (x) == FUNCTION_DECL || TREE_CODE (x) == VAR_DECL)
690 && DECL_EXTERNAL (x))
692 /* Look in block scope. */
693 t = innermost_non_namespace_value (name);
694 /* Or in the innermost namespace. */
695 if (! t)
696 t = namespace_binding (name, DECL_CONTEXT (x));
697 /* Does it have linkage? Note that if this isn't a DECL, it's an
698 OVERLOAD, which is OK. */
699 if (t && DECL_P (t) && ! (TREE_STATIC (t) || DECL_EXTERNAL (t)))
700 t = NULL_TREE;
701 if (t)
702 different_binding_level = 1;
705 /* If we are declaring a function, and the result of name-lookup
706 was an OVERLOAD, look for an overloaded instance that is
707 actually the same as the function we are declaring. (If
708 there is one, we have to merge our declaration with the
709 previous declaration.) */
710 if (t && TREE_CODE (t) == OVERLOAD)
712 tree match;
714 if (TREE_CODE (x) == FUNCTION_DECL)
715 for (match = t; match; match = OVL_NEXT (match))
717 if (decls_match (OVL_CURRENT (match), x))
718 break;
720 else
721 /* Just choose one. */
722 match = t;
724 if (match)
725 t = OVL_CURRENT (match);
726 else
727 t = NULL_TREE;
730 if (t && t != error_mark_node)
732 if (different_binding_level)
734 if (decls_match (x, t))
735 /* The standard only says that the local extern
736 inherits linkage from the previous decl; in
737 particular, default args are not shared. We must
738 also tell cgraph to treat these decls as the same,
739 or we may neglect to emit an "unused" static - we
740 do this by making the DECL_UIDs equal, which should
741 be viewed as a kludge. FIXME. */
743 TREE_PUBLIC (x) = TREE_PUBLIC (t);
744 DECL_UID (x) = DECL_UID (t);
747 else if (TREE_CODE (t) == PARM_DECL)
749 gcc_assert (DECL_CONTEXT (t));
751 /* Check for duplicate params. */
752 if (duplicate_decls (x, t))
753 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
755 else if ((DECL_EXTERN_C_FUNCTION_P (x)
756 || DECL_FUNCTION_TEMPLATE_P (x))
757 && is_overloaded_fn (t))
758 /* Don't do anything just yet. */;
759 else if (t == wchar_decl_node)
761 if (pedantic && ! DECL_IN_SYSTEM_HEADER (x))
762 pedwarn ("redeclaration of %<wchar_t%> as %qT",
763 TREE_TYPE (x));
765 /* Throw away the redeclaration. */
766 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
768 else
770 tree olddecl = duplicate_decls (x, t);
772 /* If the redeclaration failed, we can stop at this
773 point. */
774 if (olddecl == error_mark_node)
775 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
777 if (olddecl)
779 if (TREE_CODE (t) == TYPE_DECL)
780 SET_IDENTIFIER_TYPE_VALUE (name, TREE_TYPE (t));
781 else if (TREE_CODE (t) == FUNCTION_DECL)
782 check_default_args (t);
784 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
786 else if (DECL_MAIN_P (x) && TREE_CODE (t) == FUNCTION_DECL)
788 /* A redeclaration of main, but not a duplicate of the
789 previous one.
791 [basic.start.main]
793 This function shall not be overloaded. */
794 cp_error_at ("invalid redeclaration of %qD", t);
795 error ("as %qD", x);
796 /* We don't try to push this declaration since that
797 causes a crash. */
798 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
803 check_template_shadow (x);
805 /* If this is a function conjured up by the backend, massage it
806 so it looks friendly. */
807 if (DECL_NON_THUNK_FUNCTION_P (x) && ! DECL_LANG_SPECIFIC (x))
809 retrofit_lang_decl (x);
810 SET_DECL_LANGUAGE (x, lang_c);
813 if (DECL_NON_THUNK_FUNCTION_P (x) && ! DECL_FUNCTION_MEMBER_P (x))
815 t = push_overloaded_decl (x, PUSH_LOCAL);
816 if (t != x)
817 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
818 if (!namespace_bindings_p ())
819 /* We do not need to create a binding for this name;
820 push_overloaded_decl will have already done so if
821 necessary. */
822 need_new_binding = 0;
824 else if (DECL_FUNCTION_TEMPLATE_P (x) && DECL_NAMESPACE_SCOPE_P (x))
826 t = push_overloaded_decl (x, PUSH_GLOBAL);
827 if (t == x)
828 add_decl_to_level (x, NAMESPACE_LEVEL (CP_DECL_CONTEXT (t)));
829 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
832 /* If declaring a type as a typedef, copy the type (unless we're
833 at line 0), and install this TYPE_DECL as the new type's typedef
834 name. See the extensive comment in ../c-decl.c (pushdecl). */
835 if (TREE_CODE (x) == TYPE_DECL)
837 tree type = TREE_TYPE (x);
838 if (DECL_IS_BUILTIN (x))
840 if (TYPE_NAME (type) == 0)
841 TYPE_NAME (type) = x;
843 else if (type != error_mark_node && TYPE_NAME (type) != x
844 /* We don't want to copy the type when all we're
845 doing is making a TYPE_DECL for the purposes of
846 inlining. */
847 && (!TYPE_NAME (type)
848 || TYPE_NAME (type) != DECL_ABSTRACT_ORIGIN (x)))
850 DECL_ORIGINAL_TYPE (x) = type;
851 type = build_variant_type_copy (type);
852 TYPE_STUB_DECL (type) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
853 TYPE_NAME (type) = x;
854 TREE_TYPE (x) = type;
857 if (type != error_mark_node
858 && TYPE_NAME (type)
859 && TYPE_IDENTIFIER (type))
860 set_identifier_type_value (DECL_NAME (x), x);
863 /* Multiple external decls of the same identifier ought to match.
865 We get warnings about inline functions where they are defined.
866 We get warnings about other functions from push_overloaded_decl.
868 Avoid duplicate warnings where they are used. */
869 if (TREE_PUBLIC (x) && TREE_CODE (x) != FUNCTION_DECL)
871 tree decl;
873 decl = IDENTIFIER_NAMESPACE_VALUE (name);
874 if (decl && TREE_CODE (decl) == OVERLOAD)
875 decl = OVL_FUNCTION (decl);
877 if (decl && decl != error_mark_node
878 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl))
879 /* If different sort of thing, we already gave an error. */
880 && TREE_CODE (decl) == TREE_CODE (x)
881 && !same_type_p (TREE_TYPE (x), TREE_TYPE (decl)))
883 pedwarn ("type mismatch with previous external decl of %q#D", x);
884 cp_pedwarn_at ("previous external decl of %q#D", decl);
888 /* This name is new in its binding level.
889 Install the new declaration and return it. */
890 if (namespace_bindings_p ())
892 /* Install a global value. */
894 /* If the first global decl has external linkage,
895 warn if we later see static one. */
896 if (IDENTIFIER_GLOBAL_VALUE (name) == NULL_TREE && TREE_PUBLIC (x))
897 TREE_PUBLIC (name) = 1;
899 /* Bind the name for the entity. */
900 if (!(TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x)
901 && t != NULL_TREE)
902 && (TREE_CODE (x) == TYPE_DECL
903 || TREE_CODE (x) == VAR_DECL
904 || TREE_CODE (x) == ALIAS_DECL
905 || TREE_CODE (x) == NAMESPACE_DECL
906 || TREE_CODE (x) == CONST_DECL
907 || TREE_CODE (x) == TEMPLATE_DECL))
908 SET_IDENTIFIER_NAMESPACE_VALUE (name, x);
910 /* If new decl is `static' and an `extern' was seen previously,
911 warn about it. */
912 if (x != NULL_TREE && t != NULL_TREE && decls_match (x, t))
913 warn_extern_redeclared_static (x, t);
915 else
917 /* Here to install a non-global value. */
918 tree oldlocal = innermost_non_namespace_value (name);
919 tree oldglobal = IDENTIFIER_NAMESPACE_VALUE (name);
921 if (need_new_binding)
923 push_local_binding (name, x, 0);
924 /* Because push_local_binding will hook X on to the
925 current_binding_level's name list, we don't want to
926 do that again below. */
927 need_new_binding = 0;
930 /* If this is a TYPE_DECL, push it into the type value slot. */
931 if (TREE_CODE (x) == TYPE_DECL)
932 set_identifier_type_value (name, x);
934 /* Clear out any TYPE_DECL shadowed by a namespace so that
935 we won't think this is a type. The C struct hack doesn't
936 go through namespaces. */
937 if (TREE_CODE (x) == NAMESPACE_DECL)
938 set_identifier_type_value (name, NULL_TREE);
940 if (oldlocal)
942 tree d = oldlocal;
944 while (oldlocal
945 && TREE_CODE (oldlocal) == VAR_DECL
946 && DECL_DEAD_FOR_LOCAL (oldlocal))
947 oldlocal = DECL_SHADOWED_FOR_VAR (oldlocal);
949 if (oldlocal == NULL_TREE)
950 oldlocal = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (d));
953 /* If this is an extern function declaration, see if we
954 have a global definition or declaration for the function. */
955 if (oldlocal == NULL_TREE
956 && DECL_EXTERNAL (x)
957 && oldglobal != NULL_TREE
958 && TREE_CODE (x) == FUNCTION_DECL
959 && TREE_CODE (oldglobal) == FUNCTION_DECL)
961 /* We have one. Their types must agree. */
962 if (decls_match (x, oldglobal))
963 /* OK */;
964 else
966 warning ("extern declaration of %q#D doesn't match", x);
967 cp_warning_at ("global declaration %q#D", oldglobal);
970 /* If we have a local external declaration,
971 and no file-scope declaration has yet been seen,
972 then if we later have a file-scope decl it must not be static. */
973 if (oldlocal == NULL_TREE
974 && oldglobal == NULL_TREE
975 && DECL_EXTERNAL (x)
976 && TREE_PUBLIC (x))
977 TREE_PUBLIC (name) = 1;
979 /* Warn if shadowing an argument at the top level of the body. */
980 if (oldlocal != NULL_TREE && !DECL_EXTERNAL (x)
981 /* Inline decls shadow nothing. */
982 && !DECL_FROM_INLINE (x)
983 && TREE_CODE (oldlocal) == PARM_DECL
984 /* Don't check the `this' parameter. */
985 && !DECL_ARTIFICIAL (oldlocal))
987 bool err = false;
989 /* Don't complain if it's from an enclosing function. */
990 if (DECL_CONTEXT (oldlocal) == current_function_decl
991 && TREE_CODE (x) != PARM_DECL)
993 /* Go to where the parms should be and see if we find
994 them there. */
995 struct cp_binding_level *b = current_binding_level->level_chain;
997 /* Skip the ctor/dtor cleanup level. */
998 b = b->level_chain;
1000 /* ARM $8.3 */
1001 if (b->kind == sk_function_parms)
1003 error ("declaration of %q#D shadows a parameter", x);
1004 err = true;
1008 if (warn_shadow && !err)
1010 warning ("declaration of %q#D shadows a parameter", x);
1011 warning ("%Jshadowed declaration is here", oldlocal);
1015 /* Maybe warn if shadowing something else. */
1016 else if (warn_shadow && !DECL_EXTERNAL (x)
1017 /* No shadow warnings for internally generated vars. */
1018 && ! DECL_ARTIFICIAL (x)
1019 /* No shadow warnings for vars made for inlining. */
1020 && ! DECL_FROM_INLINE (x))
1022 tree member;
1024 if (current_class_ptr)
1025 member = lookup_member (current_class_type,
1026 name,
1027 /*protect=*/0,
1028 /*want_type=*/false);
1029 else
1030 member = NULL_TREE;
1032 if (member && !TREE_STATIC (member))
1034 /* Location of previous decl is not useful in this case. */
1035 warning ("declaration of %qD shadows a member of 'this'",
1038 else if (oldlocal != NULL_TREE
1039 && TREE_CODE (oldlocal) == VAR_DECL)
1041 warning ("declaration of %qD shadows a previous local", x);
1042 warning ("%Jshadowed declaration is here", oldlocal);
1044 else if (oldglobal != NULL_TREE
1045 && TREE_CODE (oldglobal) == VAR_DECL)
1046 /* XXX shadow warnings in outer-more namespaces */
1048 warning ("declaration of %qD shadows a global declaration",
1050 warning ("%Jshadowed declaration is here", oldglobal);
1055 if (TREE_CODE (x) == FUNCTION_DECL)
1056 check_default_args (x);
1058 if (TREE_CODE (x) == VAR_DECL)
1059 maybe_register_incomplete_var (x);
1062 if (need_new_binding)
1063 add_decl_to_level (x,
1064 DECL_NAMESPACE_SCOPE_P (x)
1065 ? NAMESPACE_LEVEL (CP_DECL_CONTEXT (x))
1066 : current_binding_level);
1068 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
1071 /* Enter DECL into the symbol table, if that's appropriate. Returns
1072 DECL, or a modified version thereof. */
1074 tree
1075 maybe_push_decl (tree decl)
1077 tree type = TREE_TYPE (decl);
1079 /* Add this decl to the current binding level, but not if it comes
1080 from another scope, e.g. a static member variable. TEM may equal
1081 DECL or it may be a previous decl of the same name. */
1082 if (decl == error_mark_node
1083 || (TREE_CODE (decl) != PARM_DECL
1084 && DECL_CONTEXT (decl) != NULL_TREE
1085 /* Definitions of namespace members outside their namespace are
1086 possible. */
1087 && TREE_CODE (DECL_CONTEXT (decl)) != NAMESPACE_DECL)
1088 || (TREE_CODE (decl) == TEMPLATE_DECL && !namespace_bindings_p ())
1089 || TREE_CODE (type) == UNKNOWN_TYPE
1090 /* The declaration of a template specialization does not affect
1091 the functions available for overload resolution, so we do not
1092 call pushdecl. */
1093 || (TREE_CODE (decl) == FUNCTION_DECL
1094 && DECL_TEMPLATE_SPECIALIZATION (decl)))
1095 return decl;
1096 else
1097 return pushdecl (decl);
1100 /* Bind DECL to ID in the current_binding_level, assumed to be a local
1101 binding level. If PUSH_USING is set in FLAGS, we know that DECL
1102 doesn't really belong to this binding level, that it got here
1103 through a using-declaration. */
1105 void
1106 push_local_binding (tree id, tree decl, int flags)
1108 struct cp_binding_level *b;
1110 /* Skip over any local classes. This makes sense if we call
1111 push_local_binding with a friend decl of a local class. */
1112 b = innermost_nonclass_level ();
1114 if (lookup_name_current_level (id))
1116 /* Supplement the existing binding. */
1117 if (!supplement_binding (IDENTIFIER_BINDING (id), decl))
1118 /* It didn't work. Something else must be bound at this
1119 level. Do not add DECL to the list of things to pop
1120 later. */
1121 return;
1123 else
1124 /* Create a new binding. */
1125 push_binding (id, decl, b);
1127 if (TREE_CODE (decl) == OVERLOAD || (flags & PUSH_USING))
1128 /* We must put the OVERLOAD into a TREE_LIST since the
1129 TREE_CHAIN of an OVERLOAD is already used. Similarly for
1130 decls that got here through a using-declaration. */
1131 decl = build_tree_list (NULL_TREE, decl);
1133 /* And put DECL on the list of things declared by the current
1134 binding level. */
1135 add_decl_to_level (decl, b);
1138 /* Check to see whether or not DECL is a variable that would have been
1139 in scope under the ARM, but is not in scope under the ANSI/ISO
1140 standard. If so, issue an error message. If name lookup would
1141 work in both cases, but return a different result, this function
1142 returns the result of ANSI/ISO lookup. Otherwise, it returns
1143 DECL. */
1145 tree
1146 check_for_out_of_scope_variable (tree decl)
1148 tree shadowed;
1150 /* We only care about out of scope variables. */
1151 if (!(TREE_CODE (decl) == VAR_DECL && DECL_DEAD_FOR_LOCAL (decl)))
1152 return decl;
1154 shadowed = DECL_SHADOWED_FOR_VAR (decl);
1155 while (shadowed != NULL_TREE && TREE_CODE (shadowed) == VAR_DECL
1156 && DECL_DEAD_FOR_LOCAL (shadowed))
1157 shadowed = DECL_SHADOWED_FOR_VAR (shadowed);
1158 if (!shadowed)
1159 shadowed = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (decl));
1160 if (shadowed)
1162 if (!DECL_ERROR_REPORTED (decl))
1164 warning ("name lookup of %qD changed", DECL_NAME (decl));
1165 cp_warning_at (" matches this %qD under ISO standard rules",
1166 shadowed);
1167 cp_warning_at (" matches this %qD under old rules", decl);
1168 DECL_ERROR_REPORTED (decl) = 1;
1170 return shadowed;
1173 /* If we have already complained about this declaration, there's no
1174 need to do it again. */
1175 if (DECL_ERROR_REPORTED (decl))
1176 return decl;
1178 DECL_ERROR_REPORTED (decl) = 1;
1180 if (TREE_TYPE (decl) == error_mark_node)
1181 return decl;
1183 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
1185 error ("name lookup of %qD changed for new ISO %<for%> scoping",
1186 DECL_NAME (decl));
1187 cp_error_at (" cannot use obsolete binding at %qD because "
1188 "it has a destructor", decl);
1189 return error_mark_node;
1191 else
1193 pedwarn ("name lookup of %qD changed for new ISO %<for%> scoping",
1194 DECL_NAME (decl));
1195 cp_pedwarn_at (" using obsolete binding at %qD", decl);
1198 return decl;
1201 /* true means unconditionally make a BLOCK for the next level pushed. */
1203 static bool keep_next_level_flag;
1205 static int binding_depth = 0;
1206 static int is_class_level = 0;
1208 static void
1209 indent (int depth)
1211 int i;
1213 for (i = 0; i < depth * 2; i++)
1214 putc (' ', stderr);
1217 /* Return a string describing the kind of SCOPE we have. */
1218 static const char *
1219 cxx_scope_descriptor (cxx_scope *scope)
1221 /* The order of this table must match the "scope_kind"
1222 enumerators. */
1223 static const char* scope_kind_names[] = {
1224 "block-scope",
1225 "cleanup-scope",
1226 "try-scope",
1227 "catch-scope",
1228 "for-scope",
1229 "function-parameter-scope",
1230 "class-scope",
1231 "namespace-scope",
1232 "template-parameter-scope",
1233 "template-explicit-spec-scope"
1235 const scope_kind kind = scope->explicit_spec_p
1236 ? sk_template_spec : scope->kind;
1238 return scope_kind_names[kind];
1241 /* Output a debugging information about SCOPE when performing
1242 ACTION at LINE. */
1243 static void
1244 cxx_scope_debug (cxx_scope *scope, int line, const char *action)
1246 const char *desc = cxx_scope_descriptor (scope);
1247 if (scope->this_entity)
1248 verbatim ("%s %s(%E) %p %d\n", action, desc,
1249 scope->this_entity, (void *) scope, line);
1250 else
1251 verbatim ("%s %s %p %d\n", action, desc, (void *) scope, line);
1254 /* Return the estimated initial size of the hashtable of a NAMESPACE
1255 scope. */
1257 static inline size_t
1258 namespace_scope_ht_size (tree ns)
1260 tree name = DECL_NAME (ns);
1262 return name == std_identifier
1263 ? NAMESPACE_STD_HT_SIZE
1264 : (name == global_scope_name
1265 ? GLOBAL_SCOPE_HT_SIZE
1266 : NAMESPACE_ORDINARY_HT_SIZE);
1269 /* A chain of binding_level structures awaiting reuse. */
1271 static GTY((deletable)) struct cp_binding_level *free_binding_level;
1273 /* Insert SCOPE as the innermost binding level. */
1275 void
1276 push_binding_level (struct cp_binding_level *scope)
1278 /* Add it to the front of currently active scopes stack. */
1279 scope->level_chain = current_binding_level;
1280 current_binding_level = scope;
1281 keep_next_level_flag = false;
1283 if (ENABLE_SCOPE_CHECKING)
1285 scope->binding_depth = binding_depth;
1286 indent (binding_depth);
1287 cxx_scope_debug (scope, input_line, "push");
1288 is_class_level = 0;
1289 binding_depth++;
1293 /* Create a new KIND scope and make it the top of the active scopes stack.
1294 ENTITY is the scope of the associated C++ entity (namespace, class,
1295 function); it is NULL otherwise. */
1297 cxx_scope *
1298 begin_scope (scope_kind kind, tree entity)
1300 cxx_scope *scope;
1302 /* Reuse or create a struct for this binding level. */
1303 if (!ENABLE_SCOPE_CHECKING && free_binding_level)
1305 scope = free_binding_level;
1306 free_binding_level = scope->level_chain;
1308 else
1309 scope = GGC_NEW (cxx_scope);
1310 memset (scope, 0, sizeof (cxx_scope));
1312 scope->this_entity = entity;
1313 scope->more_cleanups_ok = true;
1314 switch (kind)
1316 case sk_cleanup:
1317 scope->keep = true;
1318 break;
1320 case sk_template_spec:
1321 scope->explicit_spec_p = true;
1322 kind = sk_template_parms;
1323 /* Fall through. */
1324 case sk_template_parms:
1325 case sk_block:
1326 case sk_try:
1327 case sk_catch:
1328 case sk_for:
1329 case sk_class:
1330 case sk_function_parms:
1331 scope->keep = keep_next_level_flag;
1332 break;
1334 case sk_namespace:
1335 scope->type_decls = binding_table_new (namespace_scope_ht_size (entity));
1336 NAMESPACE_LEVEL (entity) = scope;
1337 VARRAY_TREE_INIT (scope->static_decls,
1338 DECL_NAME (entity) == std_identifier
1339 || DECL_NAME (entity) == global_scope_name
1340 ? 200 : 10,
1341 "Static declarations");
1342 break;
1344 default:
1345 /* Should not happen. */
1346 gcc_unreachable ();
1347 break;
1349 scope->kind = kind;
1351 push_binding_level (scope);
1353 return scope;
1356 /* We're about to leave current scope. Pop the top of the stack of
1357 currently active scopes. Return the enclosing scope, now active. */
1359 cxx_scope *
1360 leave_scope (void)
1362 cxx_scope *scope = current_binding_level;
1364 if (scope->kind == sk_namespace && class_binding_level)
1365 current_binding_level = class_binding_level;
1367 /* We cannot leave a scope, if there are none left. */
1368 if (NAMESPACE_LEVEL (global_namespace))
1369 gcc_assert (!global_scope_p (scope));
1371 if (ENABLE_SCOPE_CHECKING)
1373 indent (--binding_depth);
1374 cxx_scope_debug (scope, input_line, "leave");
1375 if (is_class_level != (scope == class_binding_level))
1377 indent (binding_depth);
1378 verbatim ("XXX is_class_level != (current_scope == class_scope)\n");
1380 is_class_level = 0;
1383 /* Move one nesting level up. */
1384 current_binding_level = scope->level_chain;
1386 /* Namespace-scopes are left most probably temporarily, not
1387 completely; they can be reopen later, e.g. in namespace-extension
1388 or any name binding activity that requires us to resume a
1389 namespace. For classes, we cache some binding levels. For other
1390 scopes, we just make the structure available for reuse. */
1391 if (scope->kind != sk_namespace
1392 && scope->kind != sk_class)
1394 scope->level_chain = free_binding_level;
1395 if (scope->kind == sk_class)
1396 scope->type_decls = NULL;
1397 else
1398 binding_table_free (scope->type_decls);
1399 gcc_assert (!ENABLE_SCOPE_CHECKING
1400 || scope->binding_depth == binding_depth);
1401 free_binding_level = scope;
1404 /* Find the innermost enclosing class scope, and reset
1405 CLASS_BINDING_LEVEL appropriately. */
1406 for (scope = current_binding_level;
1407 scope && scope->kind != sk_class;
1408 scope = scope->level_chain)
1410 class_binding_level = scope && scope->kind == sk_class ? scope : NULL;
1412 return current_binding_level;
1415 static void
1416 resume_scope (struct cp_binding_level* b)
1418 /* Resuming binding levels is meant only for namespaces,
1419 and those cannot nest into classes. */
1420 gcc_assert (!class_binding_level);
1421 /* Also, resuming a non-directly nested namespace is a no-no. */
1422 gcc_assert (b->level_chain == current_binding_level);
1423 current_binding_level = b;
1424 if (ENABLE_SCOPE_CHECKING)
1426 b->binding_depth = binding_depth;
1427 indent (binding_depth);
1428 cxx_scope_debug (b, input_line, "resume");
1429 is_class_level = 0;
1430 binding_depth++;
1434 /* Return the innermost binding level that is not for a class scope. */
1436 static cxx_scope *
1437 innermost_nonclass_level (void)
1439 cxx_scope *b;
1441 b = current_binding_level;
1442 while (b->kind == sk_class)
1443 b = b->level_chain;
1445 return b;
1448 /* We're defining an object of type TYPE. If it needs a cleanup, but
1449 we're not allowed to add any more objects with cleanups to the current
1450 scope, create a new binding level. */
1452 void
1453 maybe_push_cleanup_level (tree type)
1455 if (type != error_mark_node
1456 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
1457 && current_binding_level->more_cleanups_ok == 0)
1459 begin_scope (sk_cleanup, NULL);
1460 current_binding_level->statement_list = push_stmt_list ();
1464 /* Nonzero if we are currently in the global binding level. */
1467 global_bindings_p (void)
1469 return global_scope_p (current_binding_level);
1472 /* True if we are currently in a toplevel binding level. This
1473 means either the global binding level or a namespace in a toplevel
1474 binding level. Since there are no non-toplevel namespace levels,
1475 this really means any namespace or template parameter level. We
1476 also include a class whose context is toplevel. */
1478 bool
1479 toplevel_bindings_p (void)
1481 struct cp_binding_level *b = innermost_nonclass_level ();
1483 return b->kind == sk_namespace || b->kind == sk_template_parms;
1486 /* True if this is a namespace scope, or if we are defining a class
1487 which is itself at namespace scope, or whose enclosing class is
1488 such a class, etc. */
1490 bool
1491 namespace_bindings_p (void)
1493 struct cp_binding_level *b = innermost_nonclass_level ();
1495 return b->kind == sk_namespace;
1498 /* True if the current level needs to have a BLOCK made. */
1500 bool
1501 kept_level_p (void)
1503 return (current_binding_level->blocks != NULL_TREE
1504 || current_binding_level->keep
1505 || current_binding_level->kind == sk_cleanup
1506 || current_binding_level->names != NULL_TREE
1507 || current_binding_level->type_decls != NULL);
1510 /* Returns the kind of the innermost scope. */
1512 scope_kind
1513 innermost_scope_kind (void)
1515 return current_binding_level->kind;
1518 /* Returns true if this scope was created to store template parameters. */
1520 bool
1521 template_parm_scope_p (void)
1523 return innermost_scope_kind () == sk_template_parms;
1526 /* If KEEP is true, make a BLOCK node for the next binding level,
1527 unconditionally. Otherwise, use the normal logic to decide whether
1528 or not to create a BLOCK. */
1530 void
1531 keep_next_level (bool keep)
1533 keep_next_level_flag = keep;
1536 /* Return the list of declarations of the current level.
1537 Note that this list is in reverse order unless/until
1538 you nreverse it; and when you do nreverse it, you must
1539 store the result back using `storedecls' or you will lose. */
1541 tree
1542 getdecls (void)
1544 return current_binding_level->names;
1547 /* Set the current binding TABLE for type declarations.. This is a
1548 temporary workaround of the fact that the data structure classtypes
1549 does not currently carry its allocated cxx_scope structure. */
1550 void
1551 cxx_remember_type_decls (binding_table table)
1553 current_binding_level->type_decls = table;
1556 /* For debugging. */
1557 static int no_print_functions = 0;
1558 static int no_print_builtins = 0;
1560 /* Called from print_binding_level through binding_table_foreach to
1561 print the content of binding ENTRY. DATA is a pointer to line offset
1562 marker. */
1563 static void
1564 bt_print_entry (binding_entry entry, void *data)
1566 int *p = (int *) data;
1567 int len;
1569 if (entry->name == NULL)
1570 len = 3;
1571 else if (entry->name == TYPE_IDENTIFIER (entry->type))
1572 len = 2;
1573 else
1574 len = 4;
1575 len = 4;
1577 *p += len;
1579 if (*p > 5)
1581 fprintf (stderr, "\n\t");
1582 *p = len;
1584 if (entry->name == NULL)
1586 print_node_brief (stderr, "<unnamed-typedef", entry->type, 0);
1587 fprintf (stderr, ">");
1589 else if (entry->name == TYPE_IDENTIFIER (entry->type))
1590 print_node_brief (stderr, "", entry->type, 0);
1591 else
1593 print_node_brief (stderr, "<typedef", entry->name, 0);
1594 print_node_brief (stderr, "", entry->type, 0);
1595 fprintf (stderr, ">");
1599 void
1600 print_binding_level (struct cp_binding_level* lvl)
1602 tree t;
1603 int i = 0, len;
1604 fprintf (stderr, " blocks=" HOST_PTR_PRINTF, (void *) lvl->blocks);
1605 if (lvl->more_cleanups_ok)
1606 fprintf (stderr, " more-cleanups-ok");
1607 if (lvl->have_cleanups)
1608 fprintf (stderr, " have-cleanups");
1609 fprintf (stderr, "\n");
1610 if (lvl->names)
1612 fprintf (stderr, " names:\t");
1613 /* We can probably fit 3 names to a line? */
1614 for (t = lvl->names; t; t = TREE_CHAIN (t))
1616 if (no_print_functions && (TREE_CODE (t) == FUNCTION_DECL))
1617 continue;
1618 if (no_print_builtins
1619 && (TREE_CODE (t) == TYPE_DECL)
1620 && DECL_IS_BUILTIN (t))
1621 continue;
1623 /* Function decls tend to have longer names. */
1624 if (TREE_CODE (t) == FUNCTION_DECL)
1625 len = 3;
1626 else
1627 len = 2;
1628 i += len;
1629 if (i > 6)
1631 fprintf (stderr, "\n\t");
1632 i = len;
1634 print_node_brief (stderr, "", t, 0);
1635 if (t == error_mark_node)
1636 break;
1638 if (i)
1639 fprintf (stderr, "\n");
1641 if (lvl->type_decls)
1643 fprintf (stderr, " tags:\t");
1644 i = 0;
1645 binding_table_foreach (lvl->type_decls, bt_print_entry, &i);
1646 if (i)
1647 fprintf (stderr, "\n");
1649 if (VEC_length (cp_class_binding, lvl->class_shadowed))
1651 size_t i;
1652 cp_class_binding *b;
1653 fprintf (stderr, " class-shadowed:");
1654 for (i = 0;
1655 VEC_iterate(cp_class_binding, lvl->class_shadowed, i, b);
1656 ++i)
1657 fprintf (stderr, " %s ", IDENTIFIER_POINTER (b->identifier));
1658 fprintf (stderr, "\n");
1660 if (lvl->type_shadowed)
1662 fprintf (stderr, " type-shadowed:");
1663 for (t = lvl->type_shadowed; t; t = TREE_CHAIN (t))
1665 fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
1667 fprintf (stderr, "\n");
1671 void
1672 print_other_binding_stack (struct cp_binding_level *stack)
1674 struct cp_binding_level *level;
1675 for (level = stack; !global_scope_p (level); level = level->level_chain)
1677 fprintf (stderr, "binding level " HOST_PTR_PRINTF "\n", (void *) level);
1678 print_binding_level (level);
1682 void
1683 print_binding_stack (void)
1685 struct cp_binding_level *b;
1686 fprintf (stderr, "current_binding_level=" HOST_PTR_PRINTF
1687 "\nclass_binding_level=" HOST_PTR_PRINTF
1688 "\nNAMESPACE_LEVEL (global_namespace)=" HOST_PTR_PRINTF "\n",
1689 (void *) current_binding_level, (void *) class_binding_level,
1690 (void *) NAMESPACE_LEVEL (global_namespace));
1691 if (class_binding_level)
1693 for (b = class_binding_level; b; b = b->level_chain)
1694 if (b == current_binding_level)
1695 break;
1696 if (b)
1697 b = class_binding_level;
1698 else
1699 b = current_binding_level;
1701 else
1702 b = current_binding_level;
1703 print_other_binding_stack (b);
1704 fprintf (stderr, "global:\n");
1705 print_binding_level (NAMESPACE_LEVEL (global_namespace));
1708 /* Return the type associated with id. */
1710 tree
1711 identifier_type_value (tree id)
1713 timevar_push (TV_NAME_LOOKUP);
1714 /* There is no type with that name, anywhere. */
1715 if (REAL_IDENTIFIER_TYPE_VALUE (id) == NULL_TREE)
1716 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
1717 /* This is not the type marker, but the real thing. */
1718 if (REAL_IDENTIFIER_TYPE_VALUE (id) != global_type_node)
1719 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, REAL_IDENTIFIER_TYPE_VALUE (id));
1720 /* Have to search for it. It must be on the global level, now.
1721 Ask lookup_name not to return non-types. */
1722 id = lookup_name_real (id, 2, 1, /*block_p=*/true, 0, LOOKUP_COMPLAIN);
1723 if (id)
1724 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, TREE_TYPE (id));
1725 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
1728 /* Return the IDENTIFIER_GLOBAL_VALUE of T, for use in common code, since
1729 the definition of IDENTIFIER_GLOBAL_VALUE is different for C and C++. */
1731 tree
1732 identifier_global_value (tree t)
1734 return IDENTIFIER_GLOBAL_VALUE (t);
1737 /* Push a definition of struct, union or enum tag named ID. into
1738 binding_level B. DECL is a TYPE_DECL for the type. We assume that
1739 the tag ID is not already defined. */
1741 static void
1742 set_identifier_type_value_with_scope (tree id, tree decl, cxx_scope *b)
1744 tree type;
1746 if (b->kind != sk_namespace)
1748 /* Shadow the marker, not the real thing, so that the marker
1749 gets restored later. */
1750 tree old_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
1751 b->type_shadowed
1752 = tree_cons (id, old_type_value, b->type_shadowed);
1753 type = decl ? TREE_TYPE (decl) : NULL_TREE;
1754 TREE_TYPE (b->type_shadowed) = type;
1756 else
1758 cxx_binding *binding =
1759 binding_for_name (NAMESPACE_LEVEL (current_namespace), id);
1760 gcc_assert (decl);
1761 if (binding->value)
1762 supplement_binding (binding, decl);
1763 else
1764 binding->value = decl;
1766 /* Store marker instead of real type. */
1767 type = global_type_node;
1769 SET_IDENTIFIER_TYPE_VALUE (id, type);
1772 /* As set_identifier_type_value_with_scope, but using
1773 current_binding_level. */
1775 void
1776 set_identifier_type_value (tree id, tree decl)
1778 set_identifier_type_value_with_scope (id, decl, current_binding_level);
1781 /* Return the name for the constructor (or destructor) for the
1782 specified class TYPE. When given a template, this routine doesn't
1783 lose the specialization. */
1785 tree
1786 constructor_name_full (tree type)
1788 return TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
1791 /* Return the name for the constructor (or destructor) for the
1792 specified class. When given a template, return the plain
1793 unspecialized name. */
1795 tree
1796 constructor_name (tree type)
1798 tree name;
1799 name = constructor_name_full (type);
1800 if (IDENTIFIER_TEMPLATE (name))
1801 name = IDENTIFIER_TEMPLATE (name);
1802 return name;
1805 /* Returns TRUE if NAME is the name for the constructor for TYPE. */
1807 bool
1808 constructor_name_p (tree name, tree type)
1810 tree ctor_name;
1812 if (!name)
1813 return false;
1815 if (TREE_CODE (name) != IDENTIFIER_NODE)
1816 return false;
1818 ctor_name = constructor_name_full (type);
1819 if (name == ctor_name)
1820 return true;
1821 if (IDENTIFIER_TEMPLATE (ctor_name)
1822 && name == IDENTIFIER_TEMPLATE (ctor_name))
1823 return true;
1824 return false;
1827 /* Counter used to create anonymous type names. */
1829 static GTY(()) int anon_cnt;
1831 /* Return an IDENTIFIER which can be used as a name for
1832 anonymous structs and unions. */
1834 tree
1835 make_anon_name (void)
1837 char buf[32];
1839 sprintf (buf, ANON_AGGRNAME_FORMAT, anon_cnt++);
1840 return get_identifier (buf);
1843 /* Clear the TREE_PURPOSE slot of UTDs which have anonymous typenames.
1844 This keeps dbxout from getting confused. */
1846 void
1847 clear_anon_tags (void)
1849 struct cp_binding_level *b;
1850 static int last_cnt = 0;
1852 /* Fast out if no new anon names were declared. */
1853 if (last_cnt == anon_cnt)
1854 return;
1856 b = current_binding_level;
1857 while (b->kind == sk_cleanup)
1858 b = b->level_chain;
1859 if (b->type_decls != NULL)
1860 binding_table_remove_anonymous_types (b->type_decls);
1861 last_cnt = anon_cnt;
1864 /* Return (from the stack of) the BINDING, if any, established at SCOPE. */
1866 static inline cxx_binding *
1867 find_binding (cxx_scope *scope, cxx_binding *binding)
1869 timevar_push (TV_NAME_LOOKUP);
1871 for (; binding != NULL; binding = binding->previous)
1872 if (binding->scope == scope)
1873 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, binding);
1875 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, (cxx_binding *)0);
1878 /* Return the binding for NAME in SCOPE, if any. Otherwise, return NULL. */
1880 static inline cxx_binding *
1881 cxx_scope_find_binding_for_name (cxx_scope *scope, tree name)
1883 cxx_binding *b = IDENTIFIER_NAMESPACE_BINDINGS (name);
1884 if (b)
1886 /* Fold-in case where NAME is used only once. */
1887 if (scope == b->scope && b->previous == NULL)
1888 return b;
1889 return find_binding (scope, b);
1891 return NULL;
1894 /* Always returns a binding for name in scope. If no binding is
1895 found, make a new one. */
1897 static cxx_binding *
1898 binding_for_name (cxx_scope *scope, tree name)
1900 cxx_binding *result;
1902 result = cxx_scope_find_binding_for_name (scope, name);
1903 if (result)
1904 return result;
1905 /* Not found, make a new one. */
1906 result = cxx_binding_make (NULL, NULL);
1907 result->previous = IDENTIFIER_NAMESPACE_BINDINGS (name);
1908 result->scope = scope;
1909 result->is_local = false;
1910 result->value_is_inherited = false;
1911 IDENTIFIER_NAMESPACE_BINDINGS (name) = result;
1912 return result;
1915 /* Insert another USING_DECL into the current binding level, returning
1916 this declaration. If this is a redeclaration, do nothing, and
1917 return NULL_TREE if this not in namespace scope (in namespace
1918 scope, a using decl might extend any previous bindings). */
1920 tree
1921 push_using_decl (tree scope, tree name)
1923 tree decl;
1925 timevar_push (TV_NAME_LOOKUP);
1926 gcc_assert (TREE_CODE (scope) == NAMESPACE_DECL);
1927 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
1928 for (decl = current_binding_level->usings; decl; decl = TREE_CHAIN (decl))
1929 if (DECL_INITIAL (decl) == scope && DECL_NAME (decl) == name)
1930 break;
1931 if (decl)
1932 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP,
1933 namespace_bindings_p () ? decl : NULL_TREE);
1934 decl = build_lang_decl (USING_DECL, name, void_type_node);
1935 DECL_INITIAL (decl) = scope;
1936 TREE_CHAIN (decl) = current_binding_level->usings;
1937 current_binding_level->usings = decl;
1938 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
1941 /* Same as pushdecl, but define X in binding-level LEVEL. We rely on the
1942 caller to set DECL_CONTEXT properly. */
1944 tree
1945 pushdecl_with_scope (tree x, cxx_scope *level)
1947 struct cp_binding_level *b;
1948 tree function_decl = current_function_decl;
1950 timevar_push (TV_NAME_LOOKUP);
1951 current_function_decl = NULL_TREE;
1952 if (level->kind == sk_class)
1954 b = class_binding_level;
1955 class_binding_level = level;
1956 pushdecl_class_level (x);
1957 class_binding_level = b;
1959 else
1961 b = current_binding_level;
1962 current_binding_level = level;
1963 x = pushdecl (x);
1964 current_binding_level = b;
1966 current_function_decl = function_decl;
1967 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
1970 /* DECL is a FUNCTION_DECL for a non-member function, which may have
1971 other definitions already in place. We get around this by making
1972 the value of the identifier point to a list of all the things that
1973 want to be referenced by that name. It is then up to the users of
1974 that name to decide what to do with that list.
1976 DECL may also be a TEMPLATE_DECL, with a FUNCTION_DECL in its
1977 DECL_TEMPLATE_RESULT. It is dealt with the same way.
1979 FLAGS is a bitwise-or of the following values:
1980 PUSH_LOCAL: Bind DECL in the current scope, rather than at
1981 namespace scope.
1982 PUSH_USING: DECL is being pushed as the result of a using
1983 declaration.
1985 The value returned may be a previous declaration if we guessed wrong
1986 about what language DECL should belong to (C or C++). Otherwise,
1987 it's always DECL (and never something that's not a _DECL). */
1989 static tree
1990 push_overloaded_decl (tree decl, int flags)
1992 tree name = DECL_NAME (decl);
1993 tree old;
1994 tree new_binding;
1995 int doing_global = (namespace_bindings_p () || !(flags & PUSH_LOCAL));
1997 timevar_push (TV_NAME_LOOKUP);
1998 if (doing_global)
1999 old = namespace_binding (name, DECL_CONTEXT (decl));
2000 else
2001 old = lookup_name_current_level (name);
2003 if (old)
2005 if (TREE_CODE (old) == TYPE_DECL && DECL_ARTIFICIAL (old))
2007 tree t = TREE_TYPE (old);
2008 if (IS_AGGR_TYPE (t) && warn_shadow
2009 && (! DECL_IN_SYSTEM_HEADER (decl)
2010 || ! DECL_IN_SYSTEM_HEADER (old)))
2011 warning ("`%#D' hides constructor for `%#T'", decl, t);
2012 old = NULL_TREE;
2014 else if (is_overloaded_fn (old))
2016 tree tmp;
2018 for (tmp = old; tmp; tmp = OVL_NEXT (tmp))
2020 tree fn = OVL_CURRENT (tmp);
2022 if (TREE_CODE (tmp) == OVERLOAD && OVL_USED (tmp)
2023 && !(flags & PUSH_USING)
2024 && compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
2025 TYPE_ARG_TYPES (TREE_TYPE (decl))))
2026 error ("%q#D conflicts with previous using declaration %q#D",
2027 decl, fn);
2029 if (duplicate_decls (decl, fn) == fn)
2030 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, fn);
2033 else if (old == error_mark_node)
2034 /* Ignore the undefined symbol marker. */
2035 old = NULL_TREE;
2036 else
2038 cp_error_at ("previous non-function declaration %q#D", old);
2039 error ("conflicts with function declaration %q#D", decl);
2040 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
2044 if (old || TREE_CODE (decl) == TEMPLATE_DECL
2045 /* If it's a using declaration, we always need to build an OVERLOAD,
2046 because it's the only way to remember that the declaration comes
2047 from 'using', and have the lookup behave correctly. */
2048 || (flags & PUSH_USING))
2050 if (old && TREE_CODE (old) != OVERLOAD)
2051 new_binding = ovl_cons (decl, ovl_cons (old, NULL_TREE));
2052 else
2053 new_binding = ovl_cons (decl, old);
2054 if (flags & PUSH_USING)
2055 OVL_USED (new_binding) = 1;
2057 else
2058 /* NAME is not ambiguous. */
2059 new_binding = decl;
2061 if (doing_global)
2062 set_namespace_binding (name, current_namespace, new_binding);
2063 else
2065 /* We only create an OVERLOAD if there was a previous binding at
2066 this level, or if decl is a template. In the former case, we
2067 need to remove the old binding and replace it with the new
2068 binding. We must also run through the NAMES on the binding
2069 level where the name was bound to update the chain. */
2071 if (TREE_CODE (new_binding) == OVERLOAD && old)
2073 tree *d;
2075 for (d = &IDENTIFIER_BINDING (name)->scope->names;
2077 d = &TREE_CHAIN (*d))
2078 if (*d == old
2079 || (TREE_CODE (*d) == TREE_LIST
2080 && TREE_VALUE (*d) == old))
2082 if (TREE_CODE (*d) == TREE_LIST)
2083 /* Just replace the old binding with the new. */
2084 TREE_VALUE (*d) = new_binding;
2085 else
2086 /* Build a TREE_LIST to wrap the OVERLOAD. */
2087 *d = tree_cons (NULL_TREE, new_binding,
2088 TREE_CHAIN (*d));
2090 /* And update the cxx_binding node. */
2091 IDENTIFIER_BINDING (name)->value = new_binding;
2092 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
2095 /* We should always find a previous binding in this case. */
2096 gcc_unreachable ();
2099 /* Install the new binding. */
2100 push_local_binding (name, new_binding, flags);
2103 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
2106 /* Check a non-member using-declaration. Return the name and scope
2107 being used, and the USING_DECL, or NULL_TREE on failure. */
2109 static tree
2110 validate_nonmember_using_decl (tree decl, tree scope, tree name)
2112 /* [namespace.udecl]
2113 A using-declaration for a class member shall be a
2114 member-declaration. */
2115 if (TYPE_P (scope))
2117 error ("%qT is not a namespace", scope);
2118 return NULL_TREE;
2120 else if (scope == error_mark_node)
2121 return NULL_TREE;
2123 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR)
2125 /* 7.3.3/5
2126 A using-declaration shall not name a template-id. */
2127 error ("a using-declaration cannot specify a template-id. "
2128 "Try %<using %D%>", name);
2129 return NULL_TREE;
2132 if (TREE_CODE (decl) == NAMESPACE_DECL)
2134 error ("namespace %qD not allowed in using-declaration", decl);
2135 return NULL_TREE;
2138 if (TREE_CODE (decl) == SCOPE_REF)
2140 /* It's a nested name with template parameter dependent scope.
2141 This can only be using-declaration for class member. */
2142 error ("%qT is not a namespace", TREE_OPERAND (decl, 0));
2143 return NULL_TREE;
2146 if (is_overloaded_fn (decl))
2147 decl = get_first_fn (decl);
2149 gcc_assert (DECL_P (decl));
2151 /* Make a USING_DECL. */
2152 return push_using_decl (scope, name);
2155 /* Process local and global using-declarations. */
2157 static void
2158 do_nonmember_using_decl (tree scope, tree name, tree oldval, tree oldtype,
2159 tree *newval, tree *newtype)
2161 struct scope_binding decls = EMPTY_SCOPE_BINDING;
2163 *newval = *newtype = NULL_TREE;
2164 if (!qualified_lookup_using_namespace (name, scope, &decls, 0))
2165 /* Lookup error */
2166 return;
2168 if (!decls.value && !decls.type)
2170 error ("%qD not declared", name);
2171 return;
2174 /* Check for using functions. */
2175 if (decls.value && is_overloaded_fn (decls.value))
2177 tree tmp, tmp1;
2179 if (oldval && !is_overloaded_fn (oldval))
2181 if (!DECL_IMPLICIT_TYPEDEF_P (oldval))
2182 error ("%qD is already declared in this scope", name);
2183 oldval = NULL_TREE;
2186 *newval = oldval;
2187 for (tmp = decls.value; tmp; tmp = OVL_NEXT (tmp))
2189 tree new_fn = OVL_CURRENT (tmp);
2191 /* [namespace.udecl]
2193 If a function declaration in namespace scope or block
2194 scope has the same name and the same parameter types as a
2195 function introduced by a using declaration the program is
2196 ill-formed. */
2197 for (tmp1 = oldval; tmp1; tmp1 = OVL_NEXT (tmp1))
2199 tree old_fn = OVL_CURRENT (tmp1);
2201 if (new_fn == old_fn)
2202 /* The function already exists in the current namespace. */
2203 break;
2204 else if (OVL_USED (tmp1))
2205 continue; /* this is a using decl */
2206 else if (compparms (TYPE_ARG_TYPES (TREE_TYPE (new_fn)),
2207 TYPE_ARG_TYPES (TREE_TYPE (old_fn))))
2209 /* There was already a non-using declaration in
2210 this scope with the same parameter types. If both
2211 are the same extern "C" functions, that's ok. */
2212 if (decls_match (new_fn, old_fn))
2214 /* If the OLD_FN was a builtin, there is now a
2215 real declaration. */
2216 if (DECL_ANTICIPATED (old_fn))
2217 DECL_ANTICIPATED (old_fn) = 0;
2218 break;
2220 else if (!DECL_ANTICIPATED (old_fn))
2222 /* If the OLD_FN was really declared, the
2223 declarations don't match. */
2224 error ("%qD is already declared in this scope", name);
2225 break;
2228 /* If the OLD_FN was not really there, just ignore
2229 it and keep going. */
2233 /* If we broke out of the loop, there's no reason to add
2234 this function to the using declarations for this
2235 scope. */
2236 if (tmp1)
2237 continue;
2239 /* If we are adding to an existing OVERLOAD, then we no
2240 longer know the type of the set of functions. */
2241 if (*newval && TREE_CODE (*newval) == OVERLOAD)
2242 TREE_TYPE (*newval) = unknown_type_node;
2243 /* Add this new function to the set. */
2244 *newval = build_overload (OVL_CURRENT (tmp), *newval);
2245 /* If there is only one function, then we use its type. (A
2246 using-declaration naming a single function can be used in
2247 contexts where overload resolution cannot be
2248 performed.) */
2249 if (TREE_CODE (*newval) != OVERLOAD)
2251 *newval = ovl_cons (*newval, NULL_TREE);
2252 TREE_TYPE (*newval) = TREE_TYPE (OVL_CURRENT (tmp));
2254 OVL_USED (*newval) = 1;
2257 else
2259 *newval = decls.value;
2260 if (oldval && !decls_match (*newval, oldval))
2261 error ("%qD is already declared in this scope", name);
2264 *newtype = decls.type;
2265 if (oldtype && *newtype && !same_type_p (oldtype, *newtype))
2267 error ("using declaration %qD introduced ambiguous type %qT",
2268 name, oldtype);
2269 return;
2273 /* Process a using-declaration at function scope. */
2275 void
2276 do_local_using_decl (tree decl, tree scope, tree name)
2278 tree oldval, oldtype, newval, newtype;
2279 tree orig_decl = decl;
2281 decl = validate_nonmember_using_decl (decl, scope, name);
2282 if (decl == NULL_TREE)
2283 return;
2285 if (building_stmt_tree ()
2286 && at_function_scope_p ())
2287 add_decl_expr (decl);
2289 oldval = lookup_name_current_level (name);
2290 oldtype = lookup_type_current_level (name);
2292 do_nonmember_using_decl (scope, name, oldval, oldtype, &newval, &newtype);
2294 if (newval)
2296 if (is_overloaded_fn (newval))
2298 tree fn, term;
2300 /* We only need to push declarations for those functions
2301 that were not already bound in the current level.
2302 The old value might be NULL_TREE, it might be a single
2303 function, or an OVERLOAD. */
2304 if (oldval && TREE_CODE (oldval) == OVERLOAD)
2305 term = OVL_FUNCTION (oldval);
2306 else
2307 term = oldval;
2308 for (fn = newval; fn && OVL_CURRENT (fn) != term;
2309 fn = OVL_NEXT (fn))
2310 push_overloaded_decl (OVL_CURRENT (fn),
2311 PUSH_LOCAL | PUSH_USING);
2313 else
2314 push_local_binding (name, newval, PUSH_USING);
2316 if (newtype)
2318 push_local_binding (name, newtype, PUSH_USING);
2319 set_identifier_type_value (name, newtype);
2322 /* Emit debug info. */
2323 if (!processing_template_decl)
2324 cp_emit_debug_info_for_using (orig_decl, current_scope());
2327 /* Return the type that should be used when TYPE's name is preceded
2328 by a tag such as 'struct' or 'union', or null if the name cannot
2329 be used in this way.
2331 For example, when processing the third line of:
2333 struct A;
2334 typedef struct A A;
2335 struct A;
2337 lookup of A will find the typedef. Given A's typedef, this function
2338 will return the type associated with "struct A". For the tag to be
2339 anything other than TYPE, TYPE must be a typedef whose original type
2340 has the same name and context as TYPE itself.
2342 It is not valid for a typedef of an anonymous type to be used with
2343 an explicit tag:
2345 typedef struct { ... } B;
2346 struct B;
2348 Return null for this case. */
2350 static tree
2351 follow_tag_typedef (tree type)
2353 tree original;
2355 original = original_type (type);
2356 if (! TYPE_NAME (original))
2357 return NULL_TREE;
2358 if (TYPE_IDENTIFIER (original) == TYPE_IDENTIFIER (type)
2359 && (CP_DECL_CONTEXT (TYPE_NAME (original))
2360 == CP_DECL_CONTEXT (TYPE_NAME (type)))
2361 && !(CLASS_TYPE_P (original) && TYPE_WAS_ANONYMOUS (original)))
2362 return original;
2363 else
2364 return NULL_TREE;
2367 /* Given NAME, an IDENTIFIER_NODE,
2368 return the structure (or union or enum) definition for that name.
2369 Searches binding levels from its SCOPE up to the global level.
2370 If THISLEVEL_ONLY is nonzero, searches only the specified context
2371 (but skips any sk_cleanup contexts to find one that is
2372 meaningful for tags).
2373 FORM says which kind of type the caller wants;
2374 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
2375 If the wrong kind of type is found, and it's not a template, an error is
2376 reported. */
2378 tree
2379 lookup_tag (enum tree_code form, tree name,
2380 cxx_scope *binding_level, int thislevel_only)
2382 struct cp_binding_level *level;
2383 /* Nonzero if, we should look past a template parameter level, even
2384 if THISLEVEL_ONLY. */
2385 int allow_template_parms_p = 1;
2386 bool type_is_anonymous = ANON_AGGRNAME_P (name);
2388 timevar_push (TV_NAME_LOOKUP);
2389 for (level = binding_level; level; level = level->level_chain)
2391 tree tail;
2392 if (type_is_anonymous && level->type_decls != NULL)
2394 tree type = binding_table_find_anon_type (level->type_decls, name);
2395 /* There is no need for error checking here, because
2396 anon names are unique throughout the compilation. */
2397 if (type != NULL)
2398 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, type);
2400 else if (level->kind == sk_namespace)
2401 /* Do namespace lookup. */
2402 for (tail = current_namespace; 1; tail = CP_DECL_CONTEXT (tail))
2404 cxx_binding *binding =
2405 cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (tail), name);
2407 if (binding && (binding->type
2408 || (binding->value
2409 && DECL_DECLARES_TYPE_P (binding->value))))
2411 tree old;
2413 /* If we just skipped past a template parameter level,
2414 even though THISLEVEL_ONLY, and we find a template
2415 class declaration, then we use the _TYPE node for the
2416 template. See the example below. */
2417 if (thislevel_only && !allow_template_parms_p
2418 && binding->value
2419 && DECL_CLASS_TEMPLATE_P (binding->value))
2420 old = binding->value;
2421 else
2422 old = binding->type ? binding->type : binding->value;
2424 /* We've found something at this binding level. If it is
2425 a typedef, extract the tag it refers to. Lookup fails
2426 if the typedef doesn't refer to a taggable type. */
2427 old = TREE_TYPE (old);
2428 old = follow_tag_typedef (old);
2429 if (!old)
2430 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
2431 if (TREE_CODE (old) != form
2432 && (form == ENUMERAL_TYPE
2433 || TREE_CODE (old) == ENUMERAL_TYPE))
2435 error ("%q#D redeclared as %C", old, form);
2436 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
2438 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, old);
2440 if (thislevel_only || tail == global_namespace)
2441 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
2443 else if (level->type_decls != NULL)
2445 binding_entry entry = binding_table_find (level->type_decls, name);
2446 if (entry != NULL)
2448 enum tree_code code = TREE_CODE (entry->type);
2450 if (code != form
2451 && (form == ENUMERAL_TYPE || code == ENUMERAL_TYPE))
2453 /* Definition isn't the kind we were looking for. */
2454 error ("%q#D redeclared as %C", entry->type, form);
2455 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
2457 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, entry->type);
2460 if (thislevel_only && level->kind != sk_cleanup)
2462 if (level->kind == sk_template_parms && allow_template_parms_p)
2464 /* We must deal with cases like this:
2466 template <class T> struct S;
2467 template <class T> struct S {};
2469 When looking up `S', for the second declaration, we
2470 would like to find the first declaration. But, we
2471 are in the pseudo-global level created for the
2472 template parameters, rather than the (surrounding)
2473 namespace level. Thus, we keep going one more level,
2474 even though THISLEVEL_ONLY is nonzero. */
2475 allow_template_parms_p = 0;
2476 continue;
2478 else
2479 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
2482 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
2485 /* Given a type, find the tag that was defined for it and return the tag name.
2486 Otherwise return 0. However, the value can never be 0
2487 in the cases in which this is used.
2489 C++: If NAME is nonzero, this is the new name to install. This is
2490 done when replacing anonymous tags with real tag names. */
2492 tree
2493 lookup_tag_reverse (tree type, tree name)
2495 struct cp_binding_level *level;
2497 timevar_push (TV_NAME_LOOKUP);
2498 for (level = current_binding_level; level; level = level->level_chain)
2500 binding_entry entry = level->type_decls == NULL
2501 ? NULL
2502 : binding_table_reverse_maybe_remap (level->type_decls, type, name);
2503 if (entry)
2504 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, entry->name);
2506 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
2509 /* Returns true if ROOT (a namespace, class, or function) encloses
2510 CHILD. CHILD may be either a class type or a namespace. */
2512 bool
2513 is_ancestor (tree root, tree child)
2515 gcc_assert ((TREE_CODE (root) == NAMESPACE_DECL
2516 || TREE_CODE (root) == FUNCTION_DECL
2517 || CLASS_TYPE_P (root)));
2518 gcc_assert ((TREE_CODE (child) == NAMESPACE_DECL
2519 || CLASS_TYPE_P (child)));
2521 /* The global namespace encloses everything. */
2522 if (root == global_namespace)
2523 return true;
2525 while (true)
2527 /* If we've run out of scopes, stop. */
2528 if (!child)
2529 return false;
2530 /* If we've reached the ROOT, it encloses CHILD. */
2531 if (root == child)
2532 return true;
2533 /* Go out one level. */
2534 if (TYPE_P (child))
2535 child = TYPE_NAME (child);
2536 child = DECL_CONTEXT (child);
2540 /* Enter the class or namespace scope indicated by T. Returns TRUE iff
2541 pop_scope should be called later to exit this scope. */
2543 bool
2544 push_scope (tree t)
2546 bool pop = true;
2548 if (TREE_CODE (t) == NAMESPACE_DECL)
2549 push_decl_namespace (t);
2550 else if (CLASS_TYPE_P (t))
2552 if (!at_class_scope_p ()
2553 || !same_type_p (current_class_type, t))
2554 push_nested_class (t);
2555 else
2556 /* T is the same as the current scope. There is therefore no
2557 need to re-enter the scope. Since we are not actually
2558 pushing a new scope, our caller should not call
2559 pop_scope. */
2560 pop = false;
2563 return pop;
2566 /* Leave scope pushed by push_scope. */
2568 void
2569 pop_scope (tree t)
2571 if (TREE_CODE (t) == NAMESPACE_DECL)
2572 pop_decl_namespace ();
2573 else if CLASS_TYPE_P (t)
2574 pop_nested_class ();
2577 /* Do a pushlevel for class declarations. */
2579 void
2580 pushlevel_class (void)
2582 if (ENABLE_SCOPE_CHECKING)
2583 is_class_level = 1;
2585 class_binding_level = begin_scope (sk_class, current_class_type);
2588 /* ...and a poplevel for class declarations. */
2590 void
2591 poplevel_class (void)
2593 struct cp_binding_level *level = class_binding_level;
2594 cp_class_binding *cb;
2595 size_t i;
2596 tree shadowed;
2598 timevar_push (TV_NAME_LOOKUP);
2599 gcc_assert (level != 0);
2601 /* If we're leaving a toplevel class, cache its binding level. */
2602 if (current_class_depth == 1)
2603 previous_class_level = level;
2604 for (shadowed = level->type_shadowed;
2605 shadowed;
2606 shadowed = TREE_CHAIN (shadowed))
2607 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (shadowed), TREE_VALUE (shadowed));
2609 /* Remove the bindings for all of the class-level declarations. */
2610 if (level->class_shadowed)
2612 for (i = 0;
2613 VEC_iterate (cp_class_binding, level->class_shadowed, i, cb);
2614 ++i)
2615 IDENTIFIER_BINDING (cb->identifier) = cb->base.previous;
2616 ggc_free (level->class_shadowed);
2617 level->class_shadowed = NULL;
2620 /* Now, pop out of the binding level which we created up in the
2621 `pushlevel_class' routine. */
2622 if (ENABLE_SCOPE_CHECKING)
2623 is_class_level = 1;
2625 leave_scope ();
2626 timevar_pop (TV_NAME_LOOKUP);
2629 /* Set INHERITED_VALUE_BINDING_P on BINDING to true or false, as
2630 appropriate. DECL is the value to which a name has just been
2631 bound. CLASS_TYPE is the class in which the lookup occurred. */
2633 static void
2634 set_inherited_value_binding_p (cxx_binding *binding, tree decl,
2635 tree class_type)
2637 if (binding->value == decl && TREE_CODE (decl) != TREE_LIST)
2639 tree context;
2641 if (TREE_CODE (decl) == OVERLOAD)
2642 context = CP_DECL_CONTEXT (OVL_CURRENT (decl));
2643 else
2645 gcc_assert (DECL_P (decl));
2646 context = context_for_name_lookup (decl);
2649 if (is_properly_derived_from (class_type, context))
2650 INHERITED_VALUE_BINDING_P (binding) = 1;
2651 else
2652 INHERITED_VALUE_BINDING_P (binding) = 0;
2654 else if (binding->value == decl)
2655 /* We only encounter a TREE_LIST when there is an ambiguity in the
2656 base classes. Such an ambiguity can be overridden by a
2657 definition in this class. */
2658 INHERITED_VALUE_BINDING_P (binding) = 1;
2659 else
2660 INHERITED_VALUE_BINDING_P (binding) = 0;
2663 /* Make the declaration of X appear in CLASS scope. */
2665 bool
2666 pushdecl_class_level (tree x)
2668 tree name;
2669 bool is_valid = true;
2671 timevar_push (TV_NAME_LOOKUP);
2672 /* Get the name of X. */
2673 if (TREE_CODE (x) == OVERLOAD)
2674 name = DECL_NAME (get_first_fn (x));
2675 else
2676 name = DECL_NAME (x);
2678 if (name)
2680 is_valid = push_class_level_binding (name, x);
2681 if (TREE_CODE (x) == TYPE_DECL)
2682 set_identifier_type_value (name, x);
2684 else if (ANON_AGGR_TYPE_P (TREE_TYPE (x)))
2686 /* If X is an anonymous aggregate, all of its members are
2687 treated as if they were members of the class containing the
2688 aggregate, for naming purposes. */
2689 tree f;
2691 for (f = TYPE_FIELDS (TREE_TYPE (x)); f; f = TREE_CHAIN (f))
2693 location_t save_location = input_location;
2694 input_location = DECL_SOURCE_LOCATION (f);
2695 if (!pushdecl_class_level (f))
2696 is_valid = false;
2697 input_location = save_location;
2700 timevar_pop (TV_NAME_LOOKUP);
2702 return is_valid;
2705 /* Return the BINDING (if any) for NAME in SCOPE, which is a class
2706 scope. If the value returned is non-NULL, and the PREVIOUS field
2707 is not set, callers must set the PREVIOUS field explicitly. */
2709 static cxx_binding *
2710 get_class_binding (tree name, cxx_scope *scope)
2712 tree class_type;
2713 tree type_binding;
2714 tree value_binding;
2715 cxx_binding *binding;
2717 class_type = scope->this_entity;
2719 /* Get the type binding. */
2720 type_binding = lookup_member (class_type, name,
2721 /*protect=*/2, /*want_type=*/true);
2722 /* Get the value binding. */
2723 value_binding = lookup_member (class_type, name,
2724 /*protect=*/2, /*want_type=*/false);
2726 if (value_binding
2727 && (TREE_CODE (value_binding) == TYPE_DECL
2728 || DECL_CLASS_TEMPLATE_P (value_binding)
2729 || (TREE_CODE (value_binding) == TREE_LIST
2730 && TREE_TYPE (value_binding) == error_mark_node
2731 && (TREE_CODE (TREE_VALUE (value_binding))
2732 == TYPE_DECL))))
2733 /* We found a type binding, even when looking for a non-type
2734 binding. This means that we already processed this binding
2735 above. */
2737 else if (value_binding)
2739 if (TREE_CODE (value_binding) == TREE_LIST
2740 && TREE_TYPE (value_binding) == error_mark_node)
2741 /* NAME is ambiguous. */
2743 else if (BASELINK_P (value_binding))
2744 /* NAME is some overloaded functions. */
2745 value_binding = BASELINK_FUNCTIONS (value_binding);
2748 /* If we found either a type binding or a value binding, create a
2749 new binding object. */
2750 if (type_binding || value_binding)
2752 binding = new_class_binding (name,
2753 value_binding,
2754 type_binding,
2755 scope);
2756 /* This is a class-scope binding, not a block-scope binding. */
2757 LOCAL_BINDING_P (binding) = 0;
2758 set_inherited_value_binding_p (binding, value_binding, class_type);
2760 else
2761 binding = NULL;
2763 return binding;
2766 /* Make the declaration(s) of X appear in CLASS scope under the name
2767 NAME. Returns true if the binding is valid. */
2769 bool
2770 push_class_level_binding (tree name, tree x)
2772 cxx_binding *binding;
2773 tree decl = x;
2774 bool ok;
2776 timevar_push (TV_NAME_LOOKUP);
2777 /* The class_binding_level will be NULL if x is a template
2778 parameter name in a member template. */
2779 if (!class_binding_level)
2780 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, true);
2782 /* Check for invalid member names. */
2783 gcc_assert (TYPE_BEING_DEFINED (current_class_type));
2784 /* We could have been passed a tree list if this is an ambiguous
2785 declaration. If so, pull the declaration out because
2786 check_template_shadow will not handle a TREE_LIST. */
2787 if (TREE_CODE (decl) == TREE_LIST
2788 && TREE_TYPE (decl) == error_mark_node)
2789 decl = TREE_VALUE (decl);
2791 check_template_shadow (decl);
2793 /* [class.mem]
2795 If T is the name of a class, then each of the following shall
2796 have a name different from T:
2798 -- every static data member of class T;
2800 -- every member of class T that is itself a type;
2802 -- every enumerator of every member of class T that is an
2803 enumerated type;
2805 -- every member of every anonymous union that is a member of
2806 class T.
2808 (Non-static data members were also forbidden to have the same
2809 name as T until TC1.) */
2810 if ((TREE_CODE (x) == VAR_DECL
2811 || TREE_CODE (x) == CONST_DECL
2812 || (TREE_CODE (x) == TYPE_DECL
2813 && !DECL_SELF_REFERENCE_P (x))
2814 /* A data member of an anonymous union. */
2815 || (TREE_CODE (x) == FIELD_DECL
2816 && DECL_CONTEXT (x) != current_class_type))
2817 && DECL_NAME (x) == constructor_name (current_class_type))
2819 tree scope = context_for_name_lookup (x);
2820 if (TYPE_P (scope) && same_type_p (scope, current_class_type))
2822 error ("%qD has the same name as the class in which it is "
2823 "declared",
2825 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, false);
2829 /* Get the current binding for NAME in this class, if any. */
2830 binding = IDENTIFIER_BINDING (name);
2831 if (!binding || binding->scope != class_binding_level)
2833 binding = get_class_binding (name, class_binding_level);
2834 /* If a new binding was created, put it at the front of the
2835 IDENTIFIER_BINDING list. */
2836 if (binding)
2838 binding->previous = IDENTIFIER_BINDING (name);
2839 IDENTIFIER_BINDING (name) = binding;
2843 /* If there is already a binding, then we may need to update the
2844 current value. */
2845 if (binding && binding->value)
2847 tree bval = binding->value;
2848 tree old_decl = NULL_TREE;
2850 if (INHERITED_VALUE_BINDING_P (binding))
2852 /* If the old binding was from a base class, and was for a
2853 tag name, slide it over to make room for the new binding.
2854 The old binding is still visible if explicitly qualified
2855 with a class-key. */
2856 if (TREE_CODE (bval) == TYPE_DECL && DECL_ARTIFICIAL (bval)
2857 && !(TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x)))
2859 old_decl = binding->type;
2860 binding->type = bval;
2861 binding->value = NULL_TREE;
2862 INHERITED_VALUE_BINDING_P (binding) = 0;
2864 else
2865 old_decl = bval;
2867 else if (TREE_CODE (x) == OVERLOAD && is_overloaded_fn (bval))
2868 old_decl = bval;
2869 else if (TREE_CODE (x) == USING_DECL && TREE_CODE (bval) == USING_DECL)
2870 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, true);
2871 else if (TREE_CODE (x) == USING_DECL && is_overloaded_fn (bval))
2872 old_decl = bval;
2873 else if (TREE_CODE (bval) == USING_DECL && is_overloaded_fn (x))
2874 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, true);
2876 if (old_decl && binding->scope == class_binding_level)
2878 binding->value = x;
2879 /* It is always safe to clear INHERITED_VALUE_BINDING_P
2880 here. This function is only used to register bindings
2881 from with the class definition itself. */
2882 INHERITED_VALUE_BINDING_P (binding) = 0;
2883 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, true);
2887 /* Note that we declared this value so that we can issue an error if
2888 this is an invalid redeclaration of a name already used for some
2889 other purpose. */
2890 note_name_declared_in_class (name, decl);
2892 /* If we didn't replace an existing binding, put the binding on the
2893 stack of bindings for the identifier, and update the shadowed
2894 list. */
2895 if (binding && binding->scope == class_binding_level)
2896 /* Supplement the existing binding. */
2897 ok = supplement_binding (binding, decl);
2898 else
2900 /* Create a new binding. */
2901 push_binding (name, decl, class_binding_level);
2902 ok = true;
2905 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, ok);
2908 tree
2909 do_class_using_decl (tree decl)
2911 tree name, value, scope, type;
2913 if (TREE_CODE (decl) != SCOPE_REF
2914 || !TREE_OPERAND (decl, 0)
2915 || !TYPE_P (TREE_OPERAND (decl, 0)))
2917 error ("using-declaration for non-member at class scope");
2918 return NULL_TREE;
2920 scope = TREE_OPERAND (decl, 0);
2921 name = TREE_OPERAND (decl, 1);
2922 if (TREE_CODE (name) == BIT_NOT_EXPR)
2924 error ("using-declaration cannot name destructor");
2925 return NULL_TREE;
2927 if (TREE_CODE (name) == TYPE_DECL)
2928 name = DECL_NAME (name);
2929 else if (TREE_CODE (name) == TEMPLATE_DECL)
2930 name = DECL_NAME (name);
2931 else if (BASELINK_P (name))
2933 tree fns = BASELINK_FUNCTIONS (name);
2934 name = DECL_NAME (get_first_fn (fns));
2937 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
2939 /* Dependent using decls have a NULL type, non-dependent ones have a
2940 void type. */
2941 type = dependent_type_p (scope) ? NULL_TREE : void_type_node;
2942 value = build_lang_decl (USING_DECL, name, type);
2943 DECL_INITIAL (value) = scope;
2945 if (scope && !processing_template_decl)
2947 tree r;
2949 r = lookup_qualified_name (scope, name, false, false);
2950 if (r && (DECL_P (r) || TREE_CODE (r) == OVERLOAD))
2951 cp_emit_debug_info_for_using (r, scope);
2953 return value;
2957 /* Return the binding value for name in scope. */
2959 tree
2960 namespace_binding (tree name, tree scope)
2962 cxx_binding *binding;
2964 if (scope == NULL)
2965 scope = global_namespace;
2966 scope = ORIGINAL_NAMESPACE (scope);
2967 binding = cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (scope), name);
2969 return binding ? binding->value : NULL_TREE;
2972 /* Set the binding value for name in scope. */
2974 void
2975 set_namespace_binding (tree name, tree scope, tree val)
2977 cxx_binding *b;
2979 timevar_push (TV_NAME_LOOKUP);
2980 if (scope == NULL_TREE)
2981 scope = global_namespace;
2982 b = binding_for_name (NAMESPACE_LEVEL (scope), name);
2983 if (!b->value || TREE_CODE (val) == OVERLOAD || val == error_mark_node)
2984 b->value = val;
2985 else
2986 supplement_binding (b, val);
2987 timevar_pop (TV_NAME_LOOKUP);
2990 /* Set the context of a declaration to scope. Complain if we are not
2991 outside scope. */
2993 void
2994 set_decl_namespace (tree decl, tree scope, bool friendp)
2996 tree old;
2998 /* Get rid of namespace aliases. */
2999 scope = ORIGINAL_NAMESPACE (scope);
3001 /* It is ok for friends to be qualified in parallel space. */
3002 if (!friendp && !is_ancestor (current_namespace, scope))
3003 error ("declaration of `%D' not in a namespace surrounding `%D'",
3004 decl, scope);
3005 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
3006 if (scope != current_namespace)
3008 /* See whether this has been declared in the namespace. */
3009 old = namespace_binding (DECL_NAME (decl), scope);
3010 if (!old)
3011 /* No old declaration at all. */
3012 goto complain;
3013 /* A template can be explicitly specialized in any namespace. */
3014 if (processing_explicit_instantiation)
3015 return;
3016 if (!is_overloaded_fn (decl))
3017 /* Don't compare non-function decls with decls_match here,
3018 since it can't check for the correct constness at this
3019 point. pushdecl will find those errors later. */
3020 return;
3021 /* Since decl is a function, old should contain a function decl. */
3022 if (!is_overloaded_fn (old))
3023 goto complain;
3024 if (processing_template_decl || processing_specialization)
3025 /* We have not yet called push_template_decl to turn a
3026 FUNCTION_DECL into a TEMPLATE_DECL, so the declarations
3027 won't match. But, we'll check later, when we construct the
3028 template. */
3029 return;
3030 if (is_overloaded_fn (old))
3032 for (; old; old = OVL_NEXT (old))
3033 if (decls_match (decl, OVL_CURRENT (old)))
3034 return;
3036 else
3037 if (decls_match (decl, old))
3038 return;
3040 else
3041 return;
3042 complain:
3043 error ("%qD should have been declared inside %qD", decl, scope);
3046 /* Return the namespace where the current declaration is declared. */
3048 tree
3049 current_decl_namespace (void)
3051 tree result;
3052 /* If we have been pushed into a different namespace, use it. */
3053 if (decl_namespace_list)
3054 return TREE_PURPOSE (decl_namespace_list);
3056 if (current_class_type)
3057 result = decl_namespace_context (current_class_type);
3058 else if (current_function_decl)
3059 result = decl_namespace_context (current_function_decl);
3060 else
3061 result = current_namespace;
3062 return result;
3065 /* Push into the scope of the NAME namespace. If NAME is NULL_TREE, then we
3066 select a name that is unique to this compilation unit. */
3068 void
3069 push_namespace (tree name)
3071 tree d = NULL_TREE;
3072 int need_new = 1;
3073 int implicit_use = 0;
3074 bool anon = !name;
3076 timevar_push (TV_NAME_LOOKUP);
3078 /* We should not get here if the global_namespace is not yet constructed
3079 nor if NAME designates the global namespace: The global scope is
3080 constructed elsewhere. */
3081 gcc_assert (global_namespace != NULL && name != global_scope_name);
3083 if (anon)
3085 /* The name of anonymous namespace is unique for the translation
3086 unit. */
3087 if (!anonymous_namespace_name)
3088 anonymous_namespace_name = get_file_function_name ('N');
3089 name = anonymous_namespace_name;
3090 d = IDENTIFIER_NAMESPACE_VALUE (name);
3091 if (d)
3092 /* Reopening anonymous namespace. */
3093 need_new = 0;
3094 implicit_use = 1;
3096 else
3098 /* Check whether this is an extended namespace definition. */
3099 d = IDENTIFIER_NAMESPACE_VALUE (name);
3100 if (d != NULL_TREE && TREE_CODE (d) == NAMESPACE_DECL)
3102 need_new = 0;
3103 if (DECL_NAMESPACE_ALIAS (d))
3105 error ("namespace alias %qD not allowed here, assuming %qD",
3106 d, DECL_NAMESPACE_ALIAS (d));
3107 d = DECL_NAMESPACE_ALIAS (d);
3112 if (need_new)
3114 /* Make a new namespace, binding the name to it. */
3115 d = build_lang_decl (NAMESPACE_DECL, name, void_type_node);
3116 DECL_CONTEXT (d) = FROB_CONTEXT (current_namespace);
3117 pushdecl (d);
3118 if (anon)
3120 /* Clear DECL_NAME for the benefit of debugging back ends. */
3121 SET_DECL_ASSEMBLER_NAME (d, name);
3122 DECL_NAME (d) = NULL_TREE;
3124 begin_scope (sk_namespace, d);
3126 else
3127 resume_scope (NAMESPACE_LEVEL (d));
3129 if (implicit_use)
3130 do_using_directive (d);
3131 /* Enter the name space. */
3132 current_namespace = d;
3134 timevar_pop (TV_NAME_LOOKUP);
3137 /* Pop from the scope of the current namespace. */
3139 void
3140 pop_namespace (void)
3142 gcc_assert (current_namespace != global_namespace);
3143 current_namespace = CP_DECL_CONTEXT (current_namespace);
3144 /* The binding level is not popped, as it might be re-opened later. */
3145 leave_scope ();
3148 /* Push into the scope of the namespace NS, even if it is deeply
3149 nested within another namespace. */
3151 void
3152 push_nested_namespace (tree ns)
3154 if (ns == global_namespace)
3155 push_to_top_level ();
3156 else
3158 push_nested_namespace (CP_DECL_CONTEXT (ns));
3159 push_namespace (DECL_NAME (ns));
3163 /* Pop back from the scope of the namespace NS, which was previously
3164 entered with push_nested_namespace. */
3166 void
3167 pop_nested_namespace (tree ns)
3169 timevar_push (TV_NAME_LOOKUP);
3170 while (ns != global_namespace)
3172 pop_namespace ();
3173 ns = CP_DECL_CONTEXT (ns);
3176 pop_from_top_level ();
3177 timevar_pop (TV_NAME_LOOKUP);
3180 /* Temporarily set the namespace for the current declaration. */
3182 void
3183 push_decl_namespace (tree decl)
3185 if (TREE_CODE (decl) != NAMESPACE_DECL)
3186 decl = decl_namespace_context (decl);
3187 decl_namespace_list = tree_cons (ORIGINAL_NAMESPACE (decl),
3188 NULL_TREE, decl_namespace_list);
3191 /* [namespace.memdef]/2 */
3193 void
3194 pop_decl_namespace (void)
3196 decl_namespace_list = TREE_CHAIN (decl_namespace_list);
3199 /* Return the namespace that is the common ancestor
3200 of two given namespaces. */
3202 static tree
3203 namespace_ancestor (tree ns1, tree ns2)
3205 timevar_push (TV_NAME_LOOKUP);
3206 if (is_ancestor (ns1, ns2))
3207 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, ns1);
3208 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP,
3209 namespace_ancestor (CP_DECL_CONTEXT (ns1), ns2));
3212 /* Process a namespace-alias declaration. */
3214 void
3215 do_namespace_alias (tree alias, tree namespace)
3217 if (TREE_CODE (namespace) != NAMESPACE_DECL)
3219 /* The parser did not find it, so it's not there. */
3220 error ("unknown namespace %qD", namespace);
3221 return;
3224 namespace = ORIGINAL_NAMESPACE (namespace);
3226 /* Build the alias. */
3227 alias = build_lang_decl (NAMESPACE_DECL, alias, void_type_node);
3228 DECL_NAMESPACE_ALIAS (alias) = namespace;
3229 DECL_EXTERNAL (alias) = 1;
3230 pushdecl (alias);
3232 /* Emit debug info for namespace alias. */
3233 (*debug_hooks->global_decl) (alias);
3236 /* Like pushdecl, only it places X in the current namespace,
3237 if appropriate. */
3239 tree
3240 pushdecl_namespace_level (tree x)
3242 struct cp_binding_level *b = current_binding_level;
3243 tree t;
3245 timevar_push (TV_NAME_LOOKUP);
3246 t = pushdecl_with_scope (x, NAMESPACE_LEVEL (current_namespace));
3248 /* Now, the type_shadowed stack may screw us. Munge it so it does
3249 what we want. */
3250 if (TREE_CODE (x) == TYPE_DECL)
3252 tree name = DECL_NAME (x);
3253 tree newval;
3254 tree *ptr = (tree *)0;
3255 for (; !global_scope_p (b); b = b->level_chain)
3257 tree shadowed = b->type_shadowed;
3258 for (; shadowed; shadowed = TREE_CHAIN (shadowed))
3259 if (TREE_PURPOSE (shadowed) == name)
3261 ptr = &TREE_VALUE (shadowed);
3262 /* Can't break out of the loop here because sometimes
3263 a binding level will have duplicate bindings for
3264 PT names. It's gross, but I haven't time to fix it. */
3267 newval = TREE_TYPE (x);
3268 if (ptr == (tree *)0)
3270 /* @@ This shouldn't be needed. My test case "zstring.cc" trips
3271 up here if this is changed to an assertion. --KR */
3272 SET_IDENTIFIER_TYPE_VALUE (name, x);
3274 else
3276 *ptr = newval;
3279 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
3282 /* Insert USED into the using list of USER. Set INDIRECT_flag if this
3283 directive is not directly from the source. Also find the common
3284 ancestor and let our users know about the new namespace */
3285 static void
3286 add_using_namespace (tree user, tree used, bool indirect)
3288 tree t;
3289 timevar_push (TV_NAME_LOOKUP);
3290 /* Using oneself is a no-op. */
3291 if (user == used)
3293 timevar_pop (TV_NAME_LOOKUP);
3294 return;
3296 gcc_assert (TREE_CODE (user) == NAMESPACE_DECL);
3297 gcc_assert (TREE_CODE (used) == NAMESPACE_DECL);
3298 /* Check if we already have this. */
3299 t = purpose_member (used, DECL_NAMESPACE_USING (user));
3300 if (t != NULL_TREE)
3302 if (!indirect)
3303 /* Promote to direct usage. */
3304 TREE_INDIRECT_USING (t) = 0;
3305 timevar_pop (TV_NAME_LOOKUP);
3306 return;
3309 /* Add used to the user's using list. */
3310 DECL_NAMESPACE_USING (user)
3311 = tree_cons (used, namespace_ancestor (user, used),
3312 DECL_NAMESPACE_USING (user));
3314 TREE_INDIRECT_USING (DECL_NAMESPACE_USING (user)) = indirect;
3316 /* Add user to the used's users list. */
3317 DECL_NAMESPACE_USERS (used)
3318 = tree_cons (user, 0, DECL_NAMESPACE_USERS (used));
3320 /* Recursively add all namespaces used. */
3321 for (t = DECL_NAMESPACE_USING (used); t; t = TREE_CHAIN (t))
3322 /* indirect usage */
3323 add_using_namespace (user, TREE_PURPOSE (t), 1);
3325 /* Tell everyone using us about the new used namespaces. */
3326 for (t = DECL_NAMESPACE_USERS (user); t; t = TREE_CHAIN (t))
3327 add_using_namespace (TREE_PURPOSE (t), used, 1);
3328 timevar_pop (TV_NAME_LOOKUP);
3331 /* Process a using-declaration not appearing in class or local scope. */
3333 void
3334 do_toplevel_using_decl (tree decl, tree scope, tree name)
3336 tree oldval, oldtype, newval, newtype;
3337 tree orig_decl = decl;
3338 cxx_binding *binding;
3340 decl = validate_nonmember_using_decl (decl, scope, name);
3341 if (decl == NULL_TREE)
3342 return;
3344 binding = binding_for_name (NAMESPACE_LEVEL (current_namespace), name);
3346 oldval = binding->value;
3347 oldtype = binding->type;
3349 do_nonmember_using_decl (scope, name, oldval, oldtype, &newval, &newtype);
3351 /* Emit debug info. */
3352 if (!processing_template_decl)
3353 cp_emit_debug_info_for_using (orig_decl, current_namespace);
3355 /* Copy declarations found. */
3356 if (newval)
3357 binding->value = newval;
3358 if (newtype)
3359 binding->type = newtype;
3360 return;
3363 /* Process a using-directive. */
3365 void
3366 do_using_directive (tree namespace)
3368 tree context = NULL_TREE;
3370 if (building_stmt_tree ())
3371 add_stmt (build_stmt (USING_STMT, namespace));
3373 /* using namespace A::B::C; */
3374 if (TREE_CODE (namespace) == SCOPE_REF)
3375 namespace = TREE_OPERAND (namespace, 1);
3376 if (TREE_CODE (namespace) == IDENTIFIER_NODE)
3378 /* Lookup in lexer did not find a namespace. */
3379 if (!processing_template_decl)
3380 error ("namespace %qT undeclared", namespace);
3381 return;
3383 if (TREE_CODE (namespace) != NAMESPACE_DECL)
3385 if (!processing_template_decl)
3386 error ("%qT is not a namespace", namespace);
3387 return;
3389 namespace = ORIGINAL_NAMESPACE (namespace);
3390 if (!toplevel_bindings_p ())
3392 push_using_directive (namespace);
3393 context = current_scope ();
3395 else
3397 /* direct usage */
3398 add_using_namespace (current_namespace, namespace, 0);
3399 if (current_namespace != global_namespace)
3400 context = current_namespace;
3403 /* Emit debugging info. */
3404 if (!processing_template_decl)
3405 (*debug_hooks->imported_module_or_decl) (namespace, context);
3408 /* Deal with a using-directive seen by the parser. Currently we only
3409 handle attributes here, since they cannot appear inside a template. */
3411 void
3412 parse_using_directive (tree namespace, tree attribs)
3414 tree a;
3416 do_using_directive (namespace);
3418 for (a = attribs; a; a = TREE_CHAIN (a))
3420 tree name = TREE_PURPOSE (a);
3421 if (is_attribute_p ("strong", name))
3423 if (!toplevel_bindings_p ())
3424 error ("strong using only meaningful at namespace scope");
3425 else
3426 DECL_NAMESPACE_ASSOCIATIONS (namespace)
3427 = tree_cons (current_namespace, 0,
3428 DECL_NAMESPACE_ASSOCIATIONS (namespace));
3430 else
3431 warning ("%qD attribute directive ignored", name);
3435 /* Like pushdecl, only it places X in the global scope if appropriate.
3436 Calls cp_finish_decl to register the variable, initializing it with
3437 *INIT, if INIT is non-NULL. */
3439 static tree
3440 pushdecl_top_level_1 (tree x, tree *init)
3442 timevar_push (TV_NAME_LOOKUP);
3443 push_to_top_level ();
3444 x = pushdecl_namespace_level (x);
3445 if (init)
3446 cp_finish_decl (x, *init, NULL_TREE, 0);
3447 pop_from_top_level ();
3448 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
3451 /* Like pushdecl, only it places X in the global scope if appropriate. */
3453 tree
3454 pushdecl_top_level (tree x)
3456 return pushdecl_top_level_1 (x, NULL);
3459 /* Like pushdecl, only it places X in the global scope if
3460 appropriate. Calls cp_finish_decl to register the variable,
3461 initializing it with INIT. */
3463 tree
3464 pushdecl_top_level_and_finish (tree x, tree init)
3466 return pushdecl_top_level_1 (x, &init);
3469 /* Combines two sets of overloaded functions into an OVERLOAD chain, removing
3470 duplicates. The first list becomes the tail of the result.
3472 The algorithm is O(n^2). We could get this down to O(n log n) by
3473 doing a sort on the addresses of the functions, if that becomes
3474 necessary. */
3476 static tree
3477 merge_functions (tree s1, tree s2)
3479 for (; s2; s2 = OVL_NEXT (s2))
3481 tree fn2 = OVL_CURRENT (s2);
3482 tree fns1;
3484 for (fns1 = s1; fns1; fns1 = OVL_NEXT (fns1))
3486 tree fn1 = OVL_CURRENT (fns1);
3488 /* If the function from S2 is already in S1, there is no
3489 need to add it again. For `extern "C"' functions, we
3490 might have two FUNCTION_DECLs for the same function, in
3491 different namespaces; again, we only need one of them. */
3492 if (fn1 == fn2
3493 || (DECL_EXTERN_C_P (fn1) && DECL_EXTERN_C_P (fn2)
3494 && DECL_NAME (fn1) == DECL_NAME (fn2)))
3495 break;
3498 /* If we exhausted all of the functions in S1, FN2 is new. */
3499 if (!fns1)
3500 s1 = build_overload (fn2, s1);
3502 return s1;
3505 /* This should return an error not all definitions define functions.
3506 It is not an error if we find two functions with exactly the
3507 same signature, only if these are selected in overload resolution.
3508 old is the current set of bindings, new the freshly-found binding.
3509 XXX Do we want to give *all* candidates in case of ambiguity?
3510 XXX In what way should I treat extern declarations?
3511 XXX I don't want to repeat the entire duplicate_decls here */
3513 static void
3514 ambiguous_decl (tree name, struct scope_binding *old, cxx_binding *new,
3515 int flags)
3517 tree val, type;
3518 gcc_assert (old != NULL);
3519 /* Copy the value. */
3520 val = new->value;
3521 if (val)
3522 switch (TREE_CODE (val))
3524 case TEMPLATE_DECL:
3525 /* If we expect types or namespaces, and not templates,
3526 or this is not a template class. */
3527 if (LOOKUP_QUALIFIERS_ONLY (flags)
3528 && !DECL_CLASS_TEMPLATE_P (val))
3529 val = NULL_TREE;
3530 break;
3531 case TYPE_DECL:
3532 if (LOOKUP_NAMESPACES_ONLY (flags))
3533 val = NULL_TREE;
3534 break;
3535 case NAMESPACE_DECL:
3536 if (LOOKUP_TYPES_ONLY (flags))
3537 val = NULL_TREE;
3538 break;
3539 case FUNCTION_DECL:
3540 /* Ignore built-in functions that are still anticipated. */
3541 if (LOOKUP_QUALIFIERS_ONLY (flags) || DECL_ANTICIPATED (val))
3542 val = NULL_TREE;
3543 break;
3544 default:
3545 if (LOOKUP_QUALIFIERS_ONLY (flags))
3546 val = NULL_TREE;
3549 if (!old->value)
3550 old->value = val;
3551 else if (val && val != old->value)
3553 if (is_overloaded_fn (old->value) && is_overloaded_fn (val))
3554 old->value = merge_functions (old->value, val);
3555 else
3557 /* Some declarations are functions, some are not. */
3558 if (flags & LOOKUP_COMPLAIN)
3560 /* If we've already given this error for this lookup,
3561 old->value is error_mark_node, so let's not
3562 repeat ourselves. */
3563 if (old->value != error_mark_node)
3565 error ("use of %qD is ambiguous", name);
3566 cp_error_at (" first declared as %q#D here", old->value);
3568 cp_error_at (" also declared as %q#D here", val);
3570 old->value = error_mark_node;
3573 /* ... and copy the type. */
3574 type = new->type;
3575 if (LOOKUP_NAMESPACES_ONLY (flags))
3576 type = NULL_TREE;
3577 if (!old->type)
3578 old->type = type;
3579 else if (type && old->type != type)
3581 if (flags & LOOKUP_COMPLAIN)
3583 error ("%qD denotes an ambiguous type",name);
3584 error ("%J first type here", TYPE_MAIN_DECL (old->type));
3585 error ("%J other type here", TYPE_MAIN_DECL (type));
3590 /* Return the declarations that are members of the namespace NS. */
3592 tree
3593 cp_namespace_decls (tree ns)
3595 return NAMESPACE_LEVEL (ns)->names;
3598 /* Combine prefer_type and namespaces_only into flags. */
3600 static int
3601 lookup_flags (int prefer_type, int namespaces_only)
3603 if (namespaces_only)
3604 return LOOKUP_PREFER_NAMESPACES;
3605 if (prefer_type > 1)
3606 return LOOKUP_PREFER_TYPES;
3607 if (prefer_type > 0)
3608 return LOOKUP_PREFER_BOTH;
3609 return 0;
3612 /* Given a lookup that returned VAL, use FLAGS to decide if we want to
3613 ignore it or not. Subroutine of lookup_name_real. */
3615 static tree
3616 qualify_lookup (tree val, int flags)
3618 if (val == NULL_TREE)
3619 return val;
3620 if ((flags & LOOKUP_PREFER_NAMESPACES) && TREE_CODE (val) == NAMESPACE_DECL)
3621 return val;
3622 if ((flags & LOOKUP_PREFER_TYPES)
3623 && (TREE_CODE (val) == TYPE_DECL || TREE_CODE (val) == TEMPLATE_DECL))
3624 return val;
3625 if (flags & (LOOKUP_PREFER_NAMESPACES | LOOKUP_PREFER_TYPES))
3626 return NULL_TREE;
3627 return val;
3630 /* Look up NAME in the NAMESPACE. */
3632 tree
3633 lookup_namespace_name (tree namespace, tree name)
3635 tree val;
3636 tree template_id = NULL_TREE;
3637 struct scope_binding binding = EMPTY_SCOPE_BINDING;
3639 timevar_push (TV_NAME_LOOKUP);
3640 gcc_assert (TREE_CODE (namespace) == NAMESPACE_DECL);
3642 if (TREE_CODE (name) == NAMESPACE_DECL)
3643 /* This happens for A::B<int> when B is a namespace. */
3644 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, name);
3645 else if (TREE_CODE (name) == TEMPLATE_DECL)
3647 /* This happens for A::B where B is a template, and there are no
3648 template arguments. */
3649 error ("invalid use of %qD", name);
3650 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
3653 namespace = ORIGINAL_NAMESPACE (namespace);
3655 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
3657 template_id = name;
3658 name = TREE_OPERAND (name, 0);
3659 if (TREE_CODE (name) == OVERLOAD)
3660 name = DECL_NAME (OVL_CURRENT (name));
3661 else if (DECL_P (name))
3662 name = DECL_NAME (name);
3665 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
3667 if (!qualified_lookup_using_namespace (name, namespace, &binding, 0))
3668 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
3670 if (binding.value)
3672 val = binding.value;
3674 if (template_id)
3676 if (DECL_CLASS_TEMPLATE_P (val))
3677 val = lookup_template_class (val,
3678 TREE_OPERAND (template_id, 1),
3679 /*in_decl=*/NULL_TREE,
3680 /*context=*/NULL_TREE,
3681 /*entering_scope=*/0,
3682 tf_error | tf_warning);
3683 else if (DECL_FUNCTION_TEMPLATE_P (val)
3684 || TREE_CODE (val) == OVERLOAD)
3685 val = lookup_template_function (val,
3686 TREE_OPERAND (template_id, 1));
3687 else
3689 error ("%<%D::%D%> is not a template", namespace, name);
3690 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
3694 /* If we have a single function from a using decl, pull it out. */
3695 if (TREE_CODE (val) == OVERLOAD && ! really_overloaded_fn (val))
3696 val = OVL_FUNCTION (val);
3698 /* Ignore built-in functions that haven't been prototyped yet. */
3699 if (!val || !DECL_P(val)
3700 || !DECL_LANG_SPECIFIC(val)
3701 || !DECL_ANTICIPATED (val))
3702 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
3705 error ("%qD undeclared in namespace %qD", name, namespace);
3706 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
3709 /* Select the right _DECL from multiple choices. */
3711 static tree
3712 select_decl (const struct scope_binding *binding, int flags)
3714 tree val;
3715 val = binding->value;
3717 timevar_push (TV_NAME_LOOKUP);
3718 if (LOOKUP_NAMESPACES_ONLY (flags))
3720 /* We are not interested in types. */
3721 if (val && TREE_CODE (val) == NAMESPACE_DECL)
3722 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
3723 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
3726 /* If looking for a type, or if there is no non-type binding, select
3727 the value binding. */
3728 if (binding->type && (!val || (flags & LOOKUP_PREFER_TYPES)))
3729 val = binding->type;
3730 /* Don't return non-types if we really prefer types. */
3731 else if (val && LOOKUP_TYPES_ONLY (flags)
3732 && ! DECL_DECLARES_TYPE_P (val))
3733 val = NULL_TREE;
3735 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
3738 /* Unscoped lookup of a global: iterate over current namespaces,
3739 considering using-directives. */
3741 static tree
3742 unqualified_namespace_lookup (tree name, int flags)
3744 tree initial = current_decl_namespace ();
3745 tree scope = initial;
3746 tree siter;
3747 struct cp_binding_level *level;
3748 tree val = NULL_TREE;
3749 struct scope_binding binding = EMPTY_SCOPE_BINDING;
3751 timevar_push (TV_NAME_LOOKUP);
3753 for (; !val; scope = CP_DECL_CONTEXT (scope))
3755 cxx_binding *b =
3756 cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (scope), name);
3758 if (b)
3760 if (b->value && DECL_P (b->value)
3761 && DECL_LANG_SPECIFIC (b->value)
3762 && DECL_ANTICIPATED (b->value))
3763 /* Ignore anticipated built-in functions. */
3765 else
3766 binding.value = b->value;
3767 binding.type = b->type;
3770 /* Add all _DECLs seen through local using-directives. */
3771 for (level = current_binding_level;
3772 level->kind != sk_namespace;
3773 level = level->level_chain)
3774 if (!lookup_using_namespace (name, &binding, level->using_directives,
3775 scope, flags))
3776 /* Give up because of error. */
3777 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
3779 /* Add all _DECLs seen through global using-directives. */
3780 /* XXX local and global using lists should work equally. */
3781 siter = initial;
3782 while (1)
3784 if (!lookup_using_namespace (name, &binding,
3785 DECL_NAMESPACE_USING (siter),
3786 scope, flags))
3787 /* Give up because of error. */
3788 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
3789 if (siter == scope) break;
3790 siter = CP_DECL_CONTEXT (siter);
3793 val = select_decl (&binding, flags);
3794 if (scope == global_namespace)
3795 break;
3797 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
3800 /* Look up NAME (an IDENTIFIER_NODE) in SCOPE (either a NAMESPACE_DECL
3801 or a class TYPE). If IS_TYPE_P is TRUE, then ignore non-type
3802 bindings.
3804 Returns a DECL (or OVERLOAD, or BASELINK) representing the
3805 declaration found. If no suitable declaration can be found,
3806 ERROR_MARK_NODE is returned. If COMPLAIN is true and SCOPE is
3807 neither a class-type nor a namespace a diagnostic is issued. */
3809 tree
3810 lookup_qualified_name (tree scope, tree name, bool is_type_p, bool complain)
3812 int flags = 0;
3814 if (TREE_CODE (scope) == NAMESPACE_DECL)
3816 struct scope_binding binding = EMPTY_SCOPE_BINDING;
3818 flags |= LOOKUP_COMPLAIN;
3819 if (is_type_p)
3820 flags |= LOOKUP_PREFER_TYPES;
3821 if (qualified_lookup_using_namespace (name, scope, &binding, flags))
3822 return select_decl (&binding, flags);
3824 else if (is_aggr_type (scope, complain))
3826 tree t;
3827 t = lookup_member (scope, name, 2, is_type_p);
3828 if (t)
3829 return t;
3832 return error_mark_node;
3835 /* Subroutine of unqualified_namespace_lookup:
3836 Add the bindings of NAME in used namespaces to VAL.
3837 We are currently looking for names in namespace SCOPE, so we
3838 look through USINGS for using-directives of namespaces
3839 which have SCOPE as a common ancestor with the current scope.
3840 Returns false on errors. */
3842 static bool
3843 lookup_using_namespace (tree name, struct scope_binding *val,
3844 tree usings, tree scope, int flags)
3846 tree iter;
3847 timevar_push (TV_NAME_LOOKUP);
3848 /* Iterate over all used namespaces in current, searching for using
3849 directives of scope. */
3850 for (iter = usings; iter; iter = TREE_CHAIN (iter))
3851 if (TREE_VALUE (iter) == scope)
3853 tree used = ORIGINAL_NAMESPACE (TREE_PURPOSE (iter));
3854 cxx_binding *val1 =
3855 cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (used), name);
3856 /* Resolve ambiguities. */
3857 if (val1)
3858 ambiguous_decl (name, val, val1, flags);
3860 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val->value != error_mark_node);
3863 /* [namespace.qual]
3864 Accepts the NAME to lookup and its qualifying SCOPE.
3865 Returns the name/type pair found into the cxx_binding *RESULT,
3866 or false on error. */
3868 static bool
3869 qualified_lookup_using_namespace (tree name, tree scope,
3870 struct scope_binding *result, int flags)
3872 /* Maintain a list of namespaces visited... */
3873 tree seen = NULL_TREE;
3874 /* ... and a list of namespace yet to see. */
3875 tree todo = NULL_TREE;
3876 tree todo_maybe = NULL_TREE;
3877 tree usings;
3878 timevar_push (TV_NAME_LOOKUP);
3879 /* Look through namespace aliases. */
3880 scope = ORIGINAL_NAMESPACE (scope);
3881 while (scope && result->value != error_mark_node)
3883 cxx_binding *binding =
3884 cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (scope), name);
3885 seen = tree_cons (scope, NULL_TREE, seen);
3886 if (binding)
3887 ambiguous_decl (name, result, binding, flags);
3889 /* Consider strong using directives always, and non-strong ones
3890 if we haven't found a binding yet. ??? Shouldn't we consider
3891 non-strong ones if the initial RESULT is non-NULL, but the
3892 binding in the given namespace is? */
3893 for (usings = DECL_NAMESPACE_USING (scope); usings;
3894 usings = TREE_CHAIN (usings))
3895 /* If this was a real directive, and we have not seen it. */
3896 if (!TREE_INDIRECT_USING (usings))
3898 /* Try to avoid queuing the same namespace more than once,
3899 the exception being when a namespace was already
3900 enqueued for todo_maybe and then a strong using is
3901 found for it. We could try to remove it from
3902 todo_maybe, but it's probably not worth the effort. */
3903 if (is_associated_namespace (scope, TREE_PURPOSE (usings))
3904 && !purpose_member (TREE_PURPOSE (usings), seen)
3905 && !purpose_member (TREE_PURPOSE (usings), todo))
3906 todo = tree_cons (TREE_PURPOSE (usings), NULL_TREE, todo);
3907 else if ((!result->value && !result->type)
3908 && !purpose_member (TREE_PURPOSE (usings), seen)
3909 && !purpose_member (TREE_PURPOSE (usings), todo)
3910 && !purpose_member (TREE_PURPOSE (usings), todo_maybe))
3911 todo_maybe = tree_cons (TREE_PURPOSE (usings), NULL_TREE,
3912 todo_maybe);
3914 if (todo)
3916 scope = TREE_PURPOSE (todo);
3917 todo = TREE_CHAIN (todo);
3919 else if (todo_maybe
3920 && (!result->value && !result->type))
3922 scope = TREE_PURPOSE (todo_maybe);
3923 todo = TREE_CHAIN (todo_maybe);
3924 todo_maybe = NULL_TREE;
3926 else
3927 scope = NULL_TREE; /* If there never was a todo list. */
3929 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, result->value != error_mark_node);
3932 /* Return the innermost non-namespace binding for NAME from a scope
3933 containing BINDING, or, if BINDING is NULL, the current scope. If
3934 CLASS_P is false, then class bindings are ignored. */
3936 cxx_binding *
3937 outer_binding (tree name,
3938 cxx_binding *binding,
3939 bool class_p)
3941 cxx_binding *outer;
3942 cxx_scope *scope;
3943 cxx_scope *outer_scope;
3945 if (binding)
3947 scope = binding->scope->level_chain;
3948 outer = binding->previous;
3950 else
3952 scope = current_binding_level;
3953 outer = IDENTIFIER_BINDING (name);
3955 outer_scope = outer ? outer->scope : NULL;
3957 /* Because we create class bindings lazily, we might be missing a
3958 class binding for NAME. If there are any class binding levels
3959 between the LAST_BINDING_LEVEL and the scope in which OUTER was
3960 declared, we must lookup NAME in those class scopes. */
3961 if (class_p)
3962 while (scope && scope != outer_scope && scope->kind != sk_namespace)
3964 if (scope->kind == sk_class)
3966 cxx_binding *class_binding;
3968 class_binding = get_class_binding (name, scope);
3969 if (class_binding)
3971 /* Thread this new class-scope binding onto the
3972 IDENTIFIER_BINDING list so that future lookups
3973 find it quickly. */
3974 class_binding->previous = outer;
3975 if (binding)
3976 binding->previous = class_binding;
3977 else
3978 IDENTIFIER_BINDING (name) = class_binding;
3979 return class_binding;
3982 scope = scope->level_chain;
3985 return outer;
3988 /* Return the innermost block-scope or class-scope value binding for
3989 NAME, or NULL_TREE if there is no such binding. */
3991 tree
3992 innermost_non_namespace_value (tree name)
3994 cxx_binding *binding;
3995 binding = outer_binding (name, /*binding=*/NULL, /*class_p=*/true);
3996 return binding ? binding->value : NULL_TREE;
3999 /* Look up NAME in the current binding level and its superiors in the
4000 namespace of variables, functions and typedefs. Return a ..._DECL
4001 node of some kind representing its definition if there is only one
4002 such declaration, or return a TREE_LIST with all the overloaded
4003 definitions if there are many, or return 0 if it is undefined.
4005 If PREFER_TYPE is > 0, we prefer TYPE_DECLs or namespaces.
4006 If PREFER_TYPE is > 1, we reject non-type decls (e.g. namespaces).
4007 Otherwise we prefer non-TYPE_DECLs.
4009 If NONCLASS is nonzero, bindings in class scopes are ignored. If
4010 BLOCK_P is false, bindings in block scopes are ignored. */
4012 tree
4013 lookup_name_real (tree name, int prefer_type, int nonclass, bool block_p,
4014 int namespaces_only, int flags)
4016 cxx_binding *iter;
4017 tree val = NULL_TREE;
4019 timevar_push (TV_NAME_LOOKUP);
4020 /* Conversion operators are handled specially because ordinary
4021 unqualified name lookup will not find template conversion
4022 operators. */
4023 if (IDENTIFIER_TYPENAME_P (name))
4025 struct cp_binding_level *level;
4027 for (level = current_binding_level;
4028 level && level->kind != sk_namespace;
4029 level = level->level_chain)
4031 tree class_type;
4032 tree operators;
4034 /* A conversion operator can only be declared in a class
4035 scope. */
4036 if (level->kind != sk_class)
4037 continue;
4039 /* Lookup the conversion operator in the class. */
4040 class_type = level->this_entity;
4041 operators = lookup_fnfields (class_type, name, /*protect=*/0);
4042 if (operators)
4043 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, operators);
4046 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
4049 flags |= lookup_flags (prefer_type, namespaces_only);
4051 /* First, look in non-namespace scopes. */
4053 if (current_class_type == NULL_TREE)
4054 nonclass = 1;
4056 if (block_p || !nonclass)
4057 for (iter = outer_binding (name, NULL, !nonclass);
4058 iter;
4059 iter = outer_binding (name, iter, !nonclass))
4061 tree binding;
4063 /* Skip entities we don't want. */
4064 if (LOCAL_BINDING_P (iter) ? !block_p : nonclass)
4065 continue;
4067 /* If this is the kind of thing we're looking for, we're done. */
4068 if (qualify_lookup (iter->value, flags))
4069 binding = iter->value;
4070 else if ((flags & LOOKUP_PREFER_TYPES)
4071 && qualify_lookup (iter->type, flags))
4072 binding = iter->type;
4073 else
4074 binding = NULL_TREE;
4076 if (binding)
4078 val = binding;
4079 break;
4083 /* Now lookup in namespace scopes. */
4084 if (!val)
4086 tree t = unqualified_namespace_lookup (name, flags);
4087 if (t)
4088 val = t;
4091 if (val)
4093 /* If we have a single function from a using decl, pull it out. */
4094 if (TREE_CODE (val) == OVERLOAD && ! really_overloaded_fn (val))
4095 val = OVL_FUNCTION (val);
4098 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
4101 tree
4102 lookup_name_nonclass (tree name)
4104 return lookup_name_real (name, 0, 1, /*block_p=*/true, 0, LOOKUP_COMPLAIN);
4107 tree
4108 lookup_function_nonclass (tree name, tree args, bool block_p)
4110 return
4111 lookup_arg_dependent (name,
4112 lookup_name_real (name, 0, 1, block_p, 0,
4113 LOOKUP_COMPLAIN),
4114 args);
4117 tree
4118 lookup_name (tree name, int prefer_type)
4120 return lookup_name_real (name, prefer_type, 0, /*block_p=*/true,
4121 0, LOOKUP_COMPLAIN);
4124 /* Similar to `lookup_name' but look only in the innermost non-class
4125 binding level. */
4127 static tree
4128 lookup_name_current_level (tree name)
4130 struct cp_binding_level *b;
4131 tree t = NULL_TREE;
4133 timevar_push (TV_NAME_LOOKUP);
4134 b = innermost_nonclass_level ();
4136 if (b->kind == sk_namespace)
4138 t = IDENTIFIER_NAMESPACE_VALUE (name);
4140 /* extern "C" function() */
4141 if (t != NULL_TREE && TREE_CODE (t) == TREE_LIST)
4142 t = TREE_VALUE (t);
4144 else if (IDENTIFIER_BINDING (name)
4145 && LOCAL_BINDING_P (IDENTIFIER_BINDING (name)))
4147 cxx_binding *binding;
4148 binding = IDENTIFIER_BINDING (name);
4149 while (1)
4151 if (binding->scope == b
4152 && !(TREE_CODE (binding->value) == VAR_DECL
4153 && DECL_DEAD_FOR_LOCAL (binding->value)))
4154 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, binding->value);
4156 if (b->kind == sk_cleanup)
4157 b = b->level_chain;
4158 else
4159 break;
4163 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
4166 /* Like lookup_name_current_level, but for types. */
4168 static tree
4169 lookup_type_current_level (tree name)
4171 tree t = NULL_TREE;
4173 timevar_push (TV_NAME_LOOKUP);
4174 gcc_assert (current_binding_level->kind != sk_namespace);
4176 if (REAL_IDENTIFIER_TYPE_VALUE (name) != NULL_TREE
4177 && REAL_IDENTIFIER_TYPE_VALUE (name) != global_type_node)
4179 struct cp_binding_level *b = current_binding_level;
4180 while (1)
4182 if (purpose_member (name, b->type_shadowed))
4183 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP,
4184 REAL_IDENTIFIER_TYPE_VALUE (name));
4185 if (b->kind == sk_cleanup)
4186 b = b->level_chain;
4187 else
4188 break;
4192 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
4195 /* [basic.lookup.koenig] */
4196 /* A nonzero return value in the functions below indicates an error. */
4198 struct arg_lookup
4200 tree name;
4201 tree namespaces;
4202 tree classes;
4203 tree functions;
4206 static bool arg_assoc (struct arg_lookup*, tree);
4207 static bool arg_assoc_args (struct arg_lookup*, tree);
4208 static bool arg_assoc_type (struct arg_lookup*, tree);
4209 static bool add_function (struct arg_lookup *, tree);
4210 static bool arg_assoc_namespace (struct arg_lookup *, tree);
4211 static bool arg_assoc_class (struct arg_lookup *, tree);
4212 static bool arg_assoc_template_arg (struct arg_lookup*, tree);
4214 /* Add a function to the lookup structure.
4215 Returns true on error. */
4217 static bool
4218 add_function (struct arg_lookup *k, tree fn)
4220 /* We used to check here to see if the function was already in the list,
4221 but that's O(n^2), which is just too expensive for function lookup.
4222 Now we deal with the occasional duplicate in joust. In doing this, we
4223 assume that the number of duplicates will be small compared to the
4224 total number of functions being compared, which should usually be the
4225 case. */
4227 /* We must find only functions, or exactly one non-function. */
4228 if (!k->functions)
4229 k->functions = fn;
4230 else if (fn == k->functions)
4232 else if (is_overloaded_fn (k->functions) && is_overloaded_fn (fn))
4233 k->functions = build_overload (fn, k->functions);
4234 else
4236 tree f1 = OVL_CURRENT (k->functions);
4237 tree f2 = fn;
4238 if (is_overloaded_fn (f1))
4240 fn = f1; f1 = f2; f2 = fn;
4242 cp_error_at ("%qD is not a function,", f1);
4243 cp_error_at (" conflict with %qD", f2);
4244 error (" in call to %qD", k->name);
4245 return true;
4248 return false;
4251 /* Returns true iff CURRENT has declared itself to be an associated
4252 namespace of SCOPE via a strong using-directive (or transitive chain
4253 thereof). Both are namespaces. */
4255 bool
4256 is_associated_namespace (tree current, tree scope)
4258 tree seen = NULL_TREE;
4259 tree todo = NULL_TREE;
4260 tree t;
4261 while (1)
4263 if (scope == current)
4264 return true;
4265 seen = tree_cons (scope, NULL_TREE, seen);
4266 for (t = DECL_NAMESPACE_ASSOCIATIONS (scope); t; t = TREE_CHAIN (t))
4267 if (!purpose_member (TREE_PURPOSE (t), seen))
4268 todo = tree_cons (TREE_PURPOSE (t), NULL_TREE, todo);
4269 if (todo)
4271 scope = TREE_PURPOSE (todo);
4272 todo = TREE_CHAIN (todo);
4274 else
4275 return false;
4279 /* Add functions of a namespace to the lookup structure.
4280 Returns true on error. */
4282 static bool
4283 arg_assoc_namespace (struct arg_lookup *k, tree scope)
4285 tree value;
4287 if (purpose_member (scope, k->namespaces))
4288 return 0;
4289 k->namespaces = tree_cons (scope, NULL_TREE, k->namespaces);
4291 /* Check out our super-users. */
4292 for (value = DECL_NAMESPACE_ASSOCIATIONS (scope); value;
4293 value = TREE_CHAIN (value))
4294 if (arg_assoc_namespace (k, TREE_PURPOSE (value)))
4295 return true;
4297 value = namespace_binding (k->name, scope);
4298 if (!value)
4299 return false;
4301 for (; value; value = OVL_NEXT (value))
4302 if (add_function (k, OVL_CURRENT (value)))
4303 return true;
4305 return false;
4308 /* Adds everything associated with a template argument to the lookup
4309 structure. Returns true on error. */
4311 static bool
4312 arg_assoc_template_arg (struct arg_lookup *k, tree arg)
4314 /* [basic.lookup.koenig]
4316 If T is a template-id, its associated namespaces and classes are
4317 ... the namespaces and classes associated with the types of the
4318 template arguments provided for template type parameters
4319 (excluding template template parameters); the namespaces in which
4320 any template template arguments are defined; and the classes in
4321 which any member templates used as template template arguments
4322 are defined. [Note: non-type template arguments do not
4323 contribute to the set of associated namespaces. ] */
4325 /* Consider first template template arguments. */
4326 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
4327 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
4328 return false;
4329 else if (TREE_CODE (arg) == TEMPLATE_DECL)
4331 tree ctx = CP_DECL_CONTEXT (arg);
4333 /* It's not a member template. */
4334 if (TREE_CODE (ctx) == NAMESPACE_DECL)
4335 return arg_assoc_namespace (k, ctx);
4336 /* Otherwise, it must be member template. */
4337 else
4338 return arg_assoc_class (k, ctx);
4340 /* It's not a template template argument, but it is a type template
4341 argument. */
4342 else if (TYPE_P (arg))
4343 return arg_assoc_type (k, arg);
4344 /* It's a non-type template argument. */
4345 else
4346 return false;
4349 /* Adds everything associated with class to the lookup structure.
4350 Returns true on error. */
4352 static bool
4353 arg_assoc_class (struct arg_lookup *k, tree type)
4355 tree list, friends, context;
4356 int i;
4358 /* Backend build structures, such as __builtin_va_list, aren't
4359 affected by all this. */
4360 if (!CLASS_TYPE_P (type))
4361 return false;
4363 if (purpose_member (type, k->classes))
4364 return false;
4365 k->classes = tree_cons (type, NULL_TREE, k->classes);
4367 context = decl_namespace_context (type);
4368 if (arg_assoc_namespace (k, context))
4369 return true;
4371 if (TYPE_BINFO (type))
4373 /* Process baseclasses. */
4374 tree binfo, base_binfo;
4376 for (binfo = TYPE_BINFO (type), i = 0;
4377 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
4378 if (arg_assoc_class (k, BINFO_TYPE (base_binfo)))
4379 return true;
4382 /* Process friends. */
4383 for (list = DECL_FRIENDLIST (TYPE_MAIN_DECL (type)); list;
4384 list = TREE_CHAIN (list))
4385 if (k->name == FRIEND_NAME (list))
4386 for (friends = FRIEND_DECLS (list); friends;
4387 friends = TREE_CHAIN (friends))
4389 tree fn = TREE_VALUE (friends);
4391 /* Only interested in global functions with potentially hidden
4392 (i.e. unqualified) declarations. */
4393 if (CP_DECL_CONTEXT (fn) != context)
4394 continue;
4395 /* Template specializations are never found by name lookup.
4396 (Templates themselves can be found, but not template
4397 specializations.) */
4398 if (TREE_CODE (fn) == FUNCTION_DECL && DECL_USE_TEMPLATE (fn))
4399 continue;
4400 if (add_function (k, fn))
4401 return true;
4404 /* Process template arguments. */
4405 if (CLASSTYPE_TEMPLATE_INFO (type)
4406 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
4408 list = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
4409 for (i = 0; i < TREE_VEC_LENGTH (list); ++i)
4410 arg_assoc_template_arg (k, TREE_VEC_ELT (list, i));
4413 return false;
4416 /* Adds everything associated with a given type.
4417 Returns 1 on error. */
4419 static bool
4420 arg_assoc_type (struct arg_lookup *k, tree type)
4422 /* As we do not get the type of non-type dependent expressions
4423 right, we can end up with such things without a type. */
4424 if (!type)
4425 return false;
4427 if (TYPE_PTRMEM_P (type))
4429 /* Pointer to member: associate class type and value type. */
4430 if (arg_assoc_type (k, TYPE_PTRMEM_CLASS_TYPE (type)))
4431 return true;
4432 return arg_assoc_type (k, TYPE_PTRMEM_POINTED_TO_TYPE (type));
4434 else switch (TREE_CODE (type))
4436 case ERROR_MARK:
4437 return false;
4438 case VOID_TYPE:
4439 case INTEGER_TYPE:
4440 case REAL_TYPE:
4441 case COMPLEX_TYPE:
4442 case VECTOR_TYPE:
4443 case CHAR_TYPE:
4444 case BOOLEAN_TYPE:
4445 return false;
4446 case RECORD_TYPE:
4447 if (TYPE_PTRMEMFUNC_P (type))
4448 return arg_assoc_type (k, TYPE_PTRMEMFUNC_FN_TYPE (type));
4449 return arg_assoc_class (k, type);
4450 case POINTER_TYPE:
4451 case REFERENCE_TYPE:
4452 case ARRAY_TYPE:
4453 return arg_assoc_type (k, TREE_TYPE (type));
4454 case UNION_TYPE:
4455 case ENUMERAL_TYPE:
4456 return arg_assoc_namespace (k, decl_namespace_context (type));
4457 case METHOD_TYPE:
4458 /* The basetype is referenced in the first arg type, so just
4459 fall through. */
4460 case FUNCTION_TYPE:
4461 /* Associate the parameter types. */
4462 if (arg_assoc_args (k, TYPE_ARG_TYPES (type)))
4463 return true;
4464 /* Associate the return type. */
4465 return arg_assoc_type (k, TREE_TYPE (type));
4466 case TEMPLATE_TYPE_PARM:
4467 case BOUND_TEMPLATE_TEMPLATE_PARM:
4468 return false;
4469 case TYPENAME_TYPE:
4470 return false;
4471 case LANG_TYPE:
4472 gcc_assert (type == unknown_type_node);
4473 return false;
4474 default:
4475 gcc_unreachable ();
4477 return false;
4480 /* Adds everything associated with arguments. Returns true on error. */
4482 static bool
4483 arg_assoc_args (struct arg_lookup *k, tree args)
4485 for (; args; args = TREE_CHAIN (args))
4486 if (arg_assoc (k, TREE_VALUE (args)))
4487 return true;
4488 return false;
4491 /* Adds everything associated with a given tree_node. Returns 1 on error. */
4493 static bool
4494 arg_assoc (struct arg_lookup *k, tree n)
4496 if (n == error_mark_node)
4497 return false;
4499 if (TYPE_P (n))
4500 return arg_assoc_type (k, n);
4502 if (! type_unknown_p (n))
4503 return arg_assoc_type (k, TREE_TYPE (n));
4505 if (TREE_CODE (n) == ADDR_EXPR)
4506 n = TREE_OPERAND (n, 0);
4507 if (TREE_CODE (n) == COMPONENT_REF)
4508 n = TREE_OPERAND (n, 1);
4509 if (TREE_CODE (n) == OFFSET_REF)
4510 n = TREE_OPERAND (n, 1);
4511 while (TREE_CODE (n) == TREE_LIST)
4512 n = TREE_VALUE (n);
4513 if (TREE_CODE (n) == BASELINK)
4514 n = BASELINK_FUNCTIONS (n);
4516 if (TREE_CODE (n) == FUNCTION_DECL)
4517 return arg_assoc_type (k, TREE_TYPE (n));
4518 if (TREE_CODE (n) == TEMPLATE_ID_EXPR)
4520 /* [basic.lookup.koenig]
4522 If T is a template-id, its associated namespaces and classes
4523 are the namespace in which the template is defined; for
4524 member templates, the member template's class... */
4525 tree template = TREE_OPERAND (n, 0);
4526 tree args = TREE_OPERAND (n, 1);
4527 tree ctx;
4528 int ix;
4530 if (TREE_CODE (template) == COMPONENT_REF)
4531 template = TREE_OPERAND (template, 1);
4533 /* First, the template. There may actually be more than one if
4534 this is an overloaded function template. But, in that case,
4535 we only need the first; all the functions will be in the same
4536 namespace. */
4537 template = OVL_CURRENT (template);
4539 ctx = CP_DECL_CONTEXT (template);
4541 if (TREE_CODE (ctx) == NAMESPACE_DECL)
4543 if (arg_assoc_namespace (k, ctx) == 1)
4544 return true;
4546 /* It must be a member template. */
4547 else if (arg_assoc_class (k, ctx) == 1)
4548 return true;
4550 /* Now the arguments. */
4551 for (ix = TREE_VEC_LENGTH (args); ix--;)
4552 if (arg_assoc_template_arg (k, TREE_VEC_ELT (args, ix)) == 1)
4553 return true;
4555 else if (TREE_CODE (n) == OVERLOAD)
4557 for (; n; n = OVL_CHAIN (n))
4558 if (arg_assoc_type (k, TREE_TYPE (OVL_FUNCTION (n))))
4559 return true;
4562 return false;
4565 /* Performs Koenig lookup depending on arguments, where fns
4566 are the functions found in normal lookup. */
4568 tree
4569 lookup_arg_dependent (tree name, tree fns, tree args)
4571 struct arg_lookup k;
4572 tree fn = NULL_TREE;
4574 timevar_push (TV_NAME_LOOKUP);
4575 k.name = name;
4576 k.functions = fns;
4577 k.classes = NULL_TREE;
4579 /* We've already looked at some namespaces during normal unqualified
4580 lookup -- but we don't know exactly which ones. If the functions
4581 we found were brought into the current namespace via a using
4582 declaration, we have not really checked the namespace from which
4583 they came. Therefore, we check all namespaces here -- unless the
4584 function we have is from the current namespace. Even then, we
4585 must check all namespaces if the function is a local
4586 declaration; any other declarations present at namespace scope
4587 should be visible during argument-dependent lookup. */
4588 if (fns)
4589 fn = OVL_CURRENT (fns);
4590 if (fn && TREE_CODE (fn) == FUNCTION_DECL
4591 && (CP_DECL_CONTEXT (fn) != current_decl_namespace ()
4592 || DECL_LOCAL_FUNCTION_P (fn)))
4593 k.namespaces = NULL_TREE;
4594 else
4595 /* Setting NAMESPACES is purely an optimization; it prevents
4596 adding functions which are already in FNS. Adding them would
4597 be safe -- "joust" will eliminate the duplicates -- but
4598 wasteful. */
4599 k.namespaces = build_tree_list (current_decl_namespace (), NULL_TREE);
4601 arg_assoc_args (&k, args);
4602 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, k.functions);
4605 /* Add namespace to using_directives. Return NULL_TREE if nothing was
4606 changed (i.e. there was already a directive), or the fresh
4607 TREE_LIST otherwise. */
4609 static tree
4610 push_using_directive (tree used)
4612 tree ud = current_binding_level->using_directives;
4613 tree iter, ancestor;
4615 timevar_push (TV_NAME_LOOKUP);
4616 /* Check if we already have this. */
4617 if (purpose_member (used, ud) != NULL_TREE)
4618 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
4620 ancestor = namespace_ancestor (current_decl_namespace (), used);
4621 ud = current_binding_level->using_directives;
4622 ud = tree_cons (used, ancestor, ud);
4623 current_binding_level->using_directives = ud;
4625 /* Recursively add all namespaces used. */
4626 for (iter = DECL_NAMESPACE_USING (used); iter; iter = TREE_CHAIN (iter))
4627 push_using_directive (TREE_PURPOSE (iter));
4629 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, ud);
4632 /* The type TYPE is being declared. If it is a class template, or a
4633 specialization of a class template, do any processing required and
4634 perform error-checking. If IS_FRIEND is nonzero, this TYPE is
4635 being declared a friend. B is the binding level at which this TYPE
4636 should be bound.
4638 Returns the TYPE_DECL for TYPE, which may have been altered by this
4639 processing. */
4641 static tree
4642 maybe_process_template_type_declaration (tree type, int globalize,
4643 cxx_scope *b)
4645 tree decl = TYPE_NAME (type);
4647 if (processing_template_parmlist)
4648 /* You can't declare a new template type in a template parameter
4649 list. But, you can declare a non-template type:
4651 template <class A*> struct S;
4653 is a forward-declaration of `A'. */
4655 else
4657 maybe_check_template_type (type);
4659 gcc_assert (IS_AGGR_TYPE (type) || TREE_CODE (type) == ENUMERAL_TYPE);
4661 if (processing_template_decl)
4663 /* This may change after the call to
4664 push_template_decl_real, but we want the original value. */
4665 tree name = DECL_NAME (decl);
4667 decl = push_template_decl_real (decl, globalize);
4668 /* If the current binding level is the binding level for the
4669 template parameters (see the comment in
4670 begin_template_parm_list) and the enclosing level is a class
4671 scope, and we're not looking at a friend, push the
4672 declaration of the member class into the class scope. In the
4673 friend case, push_template_decl will already have put the
4674 friend into global scope, if appropriate. */
4675 if (TREE_CODE (type) != ENUMERAL_TYPE
4676 && !globalize && b->kind == sk_template_parms
4677 && b->level_chain->kind == sk_class)
4679 finish_member_declaration (CLASSTYPE_TI_TEMPLATE (type));
4680 /* Put this UDT in the table of UDTs for the class, since
4681 that won't happen below because B is not the class
4682 binding level, but is instead the pseudo-global level. */
4683 if (b->level_chain->type_decls == NULL)
4684 b->level_chain->type_decls =
4685 binding_table_new (SCOPE_DEFAULT_HT_SIZE);
4686 binding_table_insert (b->level_chain->type_decls, name, type);
4687 if (!COMPLETE_TYPE_P (current_class_type))
4689 maybe_add_class_template_decl_list (current_class_type,
4690 type, /*friend_p=*/0);
4691 CLASSTYPE_NESTED_UTDS (current_class_type) =
4692 b->level_chain->type_decls;
4698 return decl;
4701 /* Push a tag name NAME for struct/class/union/enum type TYPE.
4702 Normally put it into the inner-most non-sk_cleanup scope,
4703 but if GLOBALIZE is true, put it in the inner-most non-class scope.
4704 The latter is needed for implicit declarations. */
4706 void
4707 pushtag (tree name, tree type, int globalize)
4709 struct cp_binding_level *b;
4711 timevar_push (TV_NAME_LOOKUP);
4712 b = current_binding_level;
4713 while (/* Cleanup scopes are not scopes from the point of view of
4714 the language. */
4715 b->kind == sk_cleanup
4716 /* Neither are the scopes used to hold template parameters
4717 for an explicit specialization. For an ordinary template
4718 declaration, these scopes are not scopes from the point of
4719 view of the language -- but we need a place to stash
4720 things that will go in the containing namespace when the
4721 template is instantiated. */
4722 || (b->kind == sk_template_parms && b->explicit_spec_p)
4723 || (b->kind == sk_class
4724 && (globalize
4725 /* We may be defining a new type in the initializer
4726 of a static member variable. We allow this when
4727 not pedantic, and it is particularly useful for
4728 type punning via an anonymous union. */
4729 || COMPLETE_TYPE_P (b->this_entity))))
4730 b = b->level_chain;
4732 if (b->type_decls == NULL)
4733 b->type_decls = binding_table_new (SCOPE_DEFAULT_HT_SIZE);
4734 binding_table_insert (b->type_decls, name, type);
4736 if (name)
4738 /* Do C++ gratuitous typedefing. */
4739 if (IDENTIFIER_TYPE_VALUE (name) != type)
4741 tree d = NULL_TREE;
4742 int in_class = 0;
4743 tree context = TYPE_CONTEXT (type);
4745 if (! context)
4747 tree cs = current_scope ();
4749 if (! globalize)
4750 context = cs;
4751 else if (cs != NULL_TREE && TYPE_P (cs))
4752 /* When declaring a friend class of a local class, we want
4753 to inject the newly named class into the scope
4754 containing the local class, not the namespace scope. */
4755 context = decl_function_context (get_type_decl (cs));
4757 if (!context)
4758 context = current_namespace;
4760 if (b->kind == sk_class
4761 || (b->kind == sk_template_parms
4762 && b->level_chain->kind == sk_class))
4763 in_class = 1;
4765 if (current_lang_name == lang_name_java)
4766 TYPE_FOR_JAVA (type) = 1;
4768 d = create_implicit_typedef (name, type);
4769 DECL_CONTEXT (d) = FROB_CONTEXT (context);
4770 if (! in_class)
4771 set_identifier_type_value_with_scope (name, d, b);
4773 d = maybe_process_template_type_declaration (type,
4774 globalize, b);
4776 if (b->kind == sk_class)
4778 if (!PROCESSING_REAL_TEMPLATE_DECL_P ())
4779 /* Put this TYPE_DECL on the TYPE_FIELDS list for the
4780 class. But if it's a member template class, we
4781 want the TEMPLATE_DECL, not the TYPE_DECL, so this
4782 is done later. */
4783 finish_member_declaration (d);
4784 else
4785 pushdecl_class_level (d);
4787 else
4788 d = pushdecl_with_scope (d, b);
4790 /* FIXME what if it gets a name from typedef? */
4791 if (ANON_AGGRNAME_P (name))
4792 DECL_IGNORED_P (d) = 1;
4794 TYPE_CONTEXT (type) = DECL_CONTEXT (d);
4796 /* If this is a local class, keep track of it. We need this
4797 information for name-mangling, and so that it is possible to find
4798 all function definitions in a translation unit in a convenient
4799 way. (It's otherwise tricky to find a member function definition
4800 it's only pointed to from within a local class.) */
4801 if (TYPE_CONTEXT (type)
4802 && TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL
4803 && !processing_template_decl)
4804 VARRAY_PUSH_TREE (local_classes, type);
4806 if (b->kind == sk_class
4807 && !COMPLETE_TYPE_P (current_class_type))
4809 maybe_add_class_template_decl_list (current_class_type,
4810 type, /*friend_p=*/0);
4811 CLASSTYPE_NESTED_UTDS (current_class_type) = b->type_decls;
4815 if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL)
4816 /* Use the canonical TYPE_DECL for this node. */
4817 TYPE_STUB_DECL (type) = TYPE_NAME (type);
4818 else
4820 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE
4821 will be the tagged type we just added to the current
4822 binding level. This fake NULL-named TYPE_DECL node helps
4823 dwarfout.c to know when it needs to output a
4824 representation of a tagged type, and it also gives us a
4825 convenient place to record the "scope start" address for
4826 the tagged type. */
4828 tree d = build_decl (TYPE_DECL, NULL_TREE, type);
4829 TYPE_STUB_DECL (type) = pushdecl_with_scope (d, b);
4831 timevar_pop (TV_NAME_LOOKUP);
4834 /* Subroutines for reverting temporarily to top-level for instantiation
4835 of templates and such. We actually need to clear out the class- and
4836 local-value slots of all identifiers, so that only the global values
4837 are at all visible. Simply setting current_binding_level to the global
4838 scope isn't enough, because more binding levels may be pushed. */
4839 struct saved_scope *scope_chain;
4841 /* If ID has not already been marked, add an appropriate binding to
4842 *OLD_BINDINGS. */
4844 static void
4845 store_binding (tree id, VEC(cxx_saved_binding) **old_bindings)
4847 cxx_saved_binding *saved;
4849 if (!id || !IDENTIFIER_BINDING (id))
4850 return;
4852 if (IDENTIFIER_MARKED (id))
4853 return;
4855 IDENTIFIER_MARKED (id) = 1;
4857 saved = VEC_safe_push (cxx_saved_binding, *old_bindings, NULL);
4858 saved->identifier = id;
4859 saved->binding = IDENTIFIER_BINDING (id);
4860 saved->real_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
4861 IDENTIFIER_BINDING (id) = NULL;
4864 static void
4865 store_bindings (tree names, VEC(cxx_saved_binding) **old_bindings)
4867 tree t;
4869 timevar_push (TV_NAME_LOOKUP);
4870 for (t = names; t; t = TREE_CHAIN (t))
4872 tree id;
4874 if (TREE_CODE (t) == TREE_LIST)
4875 id = TREE_PURPOSE (t);
4876 else
4877 id = DECL_NAME (t);
4879 store_binding (id, old_bindings);
4881 timevar_pop (TV_NAME_LOOKUP);
4884 /* Like store_bindings, but NAMES is a vector of cp_class_binding
4885 objects, rather than a TREE_LIST. */
4887 static void
4888 store_class_bindings (VEC(cp_class_binding) *names,
4889 VEC(cxx_saved_binding) **old_bindings)
4891 size_t i;
4892 cp_class_binding *cb;
4894 timevar_push (TV_NAME_LOOKUP);
4895 for (i = 0; VEC_iterate(cp_class_binding, names, i, cb); ++i)
4896 store_binding (cb->identifier, old_bindings);
4897 timevar_pop (TV_NAME_LOOKUP);
4900 void
4901 push_to_top_level (void)
4903 struct saved_scope *s;
4904 struct cp_binding_level *b;
4905 cxx_saved_binding *sb;
4906 size_t i;
4907 int need_pop;
4909 timevar_push (TV_NAME_LOOKUP);
4910 s = GGC_CNEW (struct saved_scope);
4912 b = scope_chain ? current_binding_level : 0;
4914 /* If we're in the middle of some function, save our state. */
4915 if (cfun)
4917 need_pop = 1;
4918 push_function_context_to (NULL_TREE);
4920 else
4921 need_pop = 0;
4923 if (scope_chain && previous_class_level)
4924 store_class_bindings (previous_class_level->class_shadowed,
4925 &s->old_bindings);
4927 /* Have to include the global scope, because class-scope decls
4928 aren't listed anywhere useful. */
4929 for (; b; b = b->level_chain)
4931 tree t;
4933 /* Template IDs are inserted into the global level. If they were
4934 inserted into namespace level, finish_file wouldn't find them
4935 when doing pending instantiations. Therefore, don't stop at
4936 namespace level, but continue until :: . */
4937 if (global_scope_p (b))
4938 break;
4940 store_bindings (b->names, &s->old_bindings);
4941 /* We also need to check class_shadowed to save class-level type
4942 bindings, since pushclass doesn't fill in b->names. */
4943 if (b->kind == sk_class)
4944 store_class_bindings (b->class_shadowed, &s->old_bindings);
4946 /* Unwind type-value slots back to top level. */
4947 for (t = b->type_shadowed; t; t = TREE_CHAIN (t))
4948 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (t), TREE_VALUE (t));
4951 for (i = 0; VEC_iterate (cxx_saved_binding, s->old_bindings, i, sb); ++i)
4952 IDENTIFIER_MARKED (sb->identifier) = 0;
4954 s->prev = scope_chain;
4955 s->bindings = b;
4956 s->need_pop_function_context = need_pop;
4957 s->function_decl = current_function_decl;
4959 scope_chain = s;
4960 current_function_decl = NULL_TREE;
4961 VARRAY_TREE_INIT (current_lang_base, 10, "current_lang_base");
4962 current_lang_name = lang_name_cplusplus;
4963 current_namespace = global_namespace;
4964 timevar_pop (TV_NAME_LOOKUP);
4967 void
4968 pop_from_top_level (void)
4970 struct saved_scope *s = scope_chain;
4971 cxx_saved_binding *saved;
4972 size_t i;
4974 timevar_push (TV_NAME_LOOKUP);
4975 /* Clear out class-level bindings cache. */
4976 if (previous_class_level)
4977 invalidate_class_lookup_cache ();
4979 current_lang_base = 0;
4981 scope_chain = s->prev;
4982 for (i = 0; VEC_iterate (cxx_saved_binding, s->old_bindings, i, saved); ++i)
4984 tree id = saved->identifier;
4986 IDENTIFIER_BINDING (id) = saved->binding;
4987 SET_IDENTIFIER_TYPE_VALUE (id, saved->real_type_value);
4990 /* If we were in the middle of compiling a function, restore our
4991 state. */
4992 if (s->need_pop_function_context)
4993 pop_function_context_from (NULL_TREE);
4994 current_function_decl = s->function_decl;
4995 timevar_pop (TV_NAME_LOOKUP);
4998 /* Pop off extraneous binding levels left over due to syntax errors.
5000 We don't pop past namespaces, as they might be valid. */
5002 void
5003 pop_everything (void)
5005 if (ENABLE_SCOPE_CHECKING)
5006 verbatim ("XXX entering pop_everything ()\n");
5007 while (!toplevel_bindings_p ())
5009 if (current_binding_level->kind == sk_class)
5010 pop_nested_class ();
5011 else
5012 poplevel (0, 0, 0);
5014 if (ENABLE_SCOPE_CHECKING)
5015 verbatim ("XXX leaving pop_everything ()\n");
5018 /* Emit debugging information for using declarations and directives.
5019 If input tree is overloaded fn then emit debug info for all
5020 candidates. */
5022 static void
5023 cp_emit_debug_info_for_using (tree t, tree context)
5025 /* Ignore this FUNCTION_DECL if it refers to a builtin declaration
5026 of a builtin function. */
5027 if (TREE_CODE (t) == FUNCTION_DECL
5028 && DECL_EXTERNAL (t)
5029 && DECL_BUILT_IN (t))
5030 return;
5032 /* Do not supply context to imported_module_or_decl, if
5033 it is a global namespace. */
5034 if (context == global_namespace)
5035 context = NULL_TREE;
5037 if (BASELINK_P (t))
5038 t = BASELINK_FUNCTIONS (t);
5040 /* FIXME: Handle TEMPLATE_DECLs. */
5041 for (t = OVL_CURRENT (t); t; t = OVL_NEXT (t))
5042 if (TREE_CODE (t) != TEMPLATE_DECL)
5043 (*debug_hooks->imported_module_or_decl) (t, context);
5046 #include "gt-cp-name-lookup.h"