First stab at getting namespaces working with PPH. This change will
[official-gcc.git] / gcc / cp / name-lookup.c
blob2e0aa584b30f267acc5e5c1e0e9b4d72a2466de4
1 /* Definitions for C++ name lookup routines.
2 Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
3 Free Software Foundation, Inc.
4 Contributed by Gabriel Dos Reis <gdr@integrable-solutions.net>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
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 "diagnostic-core.h"
32 #include "intl.h"
33 #include "debug.h"
34 #include "c-family/c-pragma.h"
35 #include "tree-pretty-print.h"
36 #include "pph.h"
37 #include "params.h"
38 #include "pph-streamer.h"
40 /* The bindings for a particular name in a particular scope. */
42 struct scope_binding {
43 tree value;
44 tree type;
46 #define EMPTY_SCOPE_BINDING { NULL_TREE, NULL_TREE }
48 static cxx_scope *innermost_nonclass_level (void);
49 static cxx_binding *binding_for_name (cxx_scope *, tree);
50 static tree push_overloaded_decl (tree, int, bool);
51 static bool lookup_using_namespace (tree, struct scope_binding *, tree,
52 tree, int);
53 static bool qualified_lookup_using_namespace (tree, tree,
54 struct scope_binding *, int);
55 static tree lookup_type_current_level (tree);
56 static tree push_using_directive (tree);
57 static cxx_binding* lookup_extern_c_fun_binding_in_all_ns (tree);
59 /* The :: namespace. */
61 tree global_namespace;
63 /* The name of the anonymous namespace, throughout this translation
64 unit. */
65 static GTY(()) tree anonymous_namespace_name;
67 /* Initialize anonymous_namespace_name if necessary, and return it. */
69 static tree
70 get_anonymous_namespace_name (void)
72 if (!anonymous_namespace_name)
74 /* The anonymous namespace has to have a unique name
75 if typeinfo objects are being compared by name. */
76 if (! flag_weak || ! SUPPORTS_ONE_ONLY)
77 anonymous_namespace_name = get_file_function_name ("N");
78 else
79 /* The demangler expects anonymous namespaces to be called
80 something starting with '_GLOBAL__N_'. */
81 anonymous_namespace_name = get_identifier ("_GLOBAL__N_1");
83 return anonymous_namespace_name;
86 /* Compute the chain index of a binding_entry given the HASH value of its
87 name and the total COUNT of chains. COUNT is assumed to be a power
88 of 2. */
90 #define ENTRY_INDEX(HASH, COUNT) (((HASH) >> 3) & ((COUNT) - 1))
92 /* A free list of "binding_entry"s awaiting for re-use. */
94 static GTY((deletable)) binding_entry free_binding_entry = NULL;
96 /* Create a binding_entry object for (NAME, TYPE). */
98 static inline binding_entry
99 binding_entry_make (tree name, tree type)
101 binding_entry entry;
103 if (free_binding_entry)
105 entry = free_binding_entry;
106 free_binding_entry = entry->chain;
108 else
109 entry = ggc_alloc_binding_entry_s ();
111 entry->name = name;
112 entry->type = type;
113 entry->chain = NULL;
115 return entry;
118 /* Put ENTRY back on the free list. */
119 #if 0
120 static inline void
121 binding_entry_free (binding_entry entry)
123 entry->name = NULL;
124 entry->type = NULL;
125 entry->chain = free_binding_entry;
126 free_binding_entry = entry;
128 #endif
130 /* The datatype used to implement the mapping from names to types at
131 a given scope. */
132 struct GTY(()) binding_table_s {
133 /* Array of chains of "binding_entry"s */
134 binding_entry * GTY((length ("%h.chain_count"))) chain;
136 /* The number of chains in this table. This is the length of the
137 member "chain" considered as an array. */
138 size_t chain_count;
140 /* Number of "binding_entry"s in this table. */
141 size_t entry_count;
144 /* Construct TABLE with an initial CHAIN_COUNT. */
146 static inline void
147 binding_table_construct (binding_table table, size_t chain_count)
149 table->chain_count = chain_count;
150 table->entry_count = 0;
151 table->chain = ggc_alloc_cleared_vec_binding_entry (table->chain_count);
154 /* Make TABLE's entries ready for reuse. */
155 #if 0
156 static void
157 binding_table_free (binding_table table)
159 size_t i;
160 size_t count;
162 if (table == NULL)
163 return;
165 for (i = 0, count = table->chain_count; i < count; ++i)
167 binding_entry temp = table->chain[i];
168 while (temp != NULL)
170 binding_entry entry = temp;
171 temp = entry->chain;
172 binding_entry_free (entry);
174 table->chain[i] = NULL;
176 table->entry_count = 0;
178 #endif
180 /* Allocate a table with CHAIN_COUNT, assumed to be a power of two. */
182 static inline binding_table
183 binding_table_new (size_t chain_count)
185 binding_table table = ggc_alloc_binding_table_s ();
186 table->chain = NULL;
187 binding_table_construct (table, chain_count);
188 return table;
191 /* Expand TABLE to twice its current chain_count. */
193 static void
194 binding_table_expand (binding_table table)
196 const size_t old_chain_count = table->chain_count;
197 const size_t old_entry_count = table->entry_count;
198 const size_t new_chain_count = 2 * old_chain_count;
199 binding_entry *old_chains = table->chain;
200 size_t i;
202 binding_table_construct (table, new_chain_count);
203 for (i = 0; i < old_chain_count; ++i)
205 binding_entry entry = old_chains[i];
206 for (; entry != NULL; entry = old_chains[i])
208 const unsigned int hash = IDENTIFIER_HASH_VALUE (entry->name);
209 const size_t j = ENTRY_INDEX (hash, new_chain_count);
211 old_chains[i] = entry->chain;
212 entry->chain = table->chain[j];
213 table->chain[j] = entry;
216 table->entry_count = old_entry_count;
219 /* Insert a binding for NAME to TYPE into TABLE. */
221 static void
222 binding_table_insert (binding_table table, tree name, tree type)
224 const unsigned int hash = IDENTIFIER_HASH_VALUE (name);
225 const size_t i = ENTRY_INDEX (hash, table->chain_count);
226 binding_entry entry = binding_entry_make (name, type);
228 entry->chain = table->chain[i];
229 table->chain[i] = entry;
230 ++table->entry_count;
232 if (3 * table->chain_count < 5 * table->entry_count)
233 binding_table_expand (table);
236 /* Return the binding_entry, if any, that maps NAME. */
238 binding_entry
239 binding_table_find (binding_table table, tree name)
241 const unsigned int hash = IDENTIFIER_HASH_VALUE (name);
242 binding_entry entry = table->chain[ENTRY_INDEX (hash, table->chain_count)];
244 while (entry != NULL && entry->name != name)
245 entry = entry->chain;
247 return entry;
250 /* Apply PROC -- with DATA -- to all entries in TABLE. */
252 void
253 binding_table_foreach (binding_table table, bt_foreach_proc proc, void *data)
255 const size_t chain_count = table->chain_count;
256 size_t i;
258 for (i = 0; i < chain_count; ++i)
260 binding_entry entry = table->chain[i];
261 for (; entry != NULL; entry = entry->chain)
262 proc (entry, data);
266 #ifndef ENABLE_SCOPE_CHECKING
267 # define ENABLE_SCOPE_CHECKING 0
268 #else
269 # define ENABLE_SCOPE_CHECKING 1
270 #endif
272 /* A free list of "cxx_binding"s, connected by their PREVIOUS. */
274 static GTY((deletable)) cxx_binding *free_bindings;
276 /* Initialize VALUE and TYPE field for BINDING, and set the PREVIOUS
277 field to NULL. */
279 static inline void
280 cxx_binding_init (cxx_binding *binding, tree value, tree type)
282 binding->value = value;
283 binding->type = type;
284 binding->previous = NULL;
287 /* (GC)-allocate a binding object with VALUE and TYPE member initialized. */
289 cxx_binding *
290 cxx_binding_make (tree value, tree type)
292 cxx_binding *binding;
293 if (free_bindings)
295 binding = free_bindings;
296 free_bindings = binding->previous;
298 else
299 binding = ggc_alloc_cxx_binding ();
301 cxx_binding_init (binding, value, type);
303 return binding;
306 /* Put BINDING back on the free list. */
308 static inline void
309 cxx_binding_free (cxx_binding *binding)
311 binding->scope = NULL;
312 binding->previous = free_bindings;
313 free_bindings = binding;
316 /* Create a new binding for NAME (with the indicated VALUE and TYPE
317 bindings) in the class scope indicated by SCOPE. */
319 static cxx_binding *
320 new_class_binding (tree name, tree value, tree type, cxx_scope *scope)
322 cp_class_binding *cb;
323 cxx_binding *binding;
325 if (VEC_length (cp_class_binding, scope->class_shadowed))
327 cp_class_binding *old_base;
328 old_base = VEC_index (cp_class_binding, scope->class_shadowed, 0);
329 if (VEC_reserve (cp_class_binding, gc, scope->class_shadowed, 1))
331 /* Fixup the current bindings, as they might have moved. */
332 size_t i;
334 FOR_EACH_VEC_ELT (cp_class_binding, scope->class_shadowed, i, cb)
336 cxx_binding **b;
337 b = &IDENTIFIER_BINDING (cb->identifier);
338 while (*b != &old_base[i].base)
339 b = &((*b)->previous);
340 *b = &cb->base;
343 cb = VEC_quick_push (cp_class_binding, scope->class_shadowed, NULL);
345 else
346 cb = VEC_safe_push (cp_class_binding, gc, scope->class_shadowed, NULL);
348 cb->identifier = name;
349 binding = &cb->base;
350 binding->scope = scope;
351 cxx_binding_init (binding, value, type);
352 return binding;
355 /* Make DECL the innermost binding for ID. The LEVEL is the binding
356 level at which this declaration is being bound. */
358 static void
359 push_binding (tree id, tree decl, cxx_scope* level)
361 cxx_binding *binding;
363 if (level != class_binding_level)
365 binding = cxx_binding_make (decl, NULL_TREE);
366 binding->scope = level;
368 else
369 binding = new_class_binding (id, decl, /*type=*/NULL_TREE, level);
371 /* Now, fill in the binding information. */
372 binding->previous = IDENTIFIER_BINDING (id);
373 INHERITED_VALUE_BINDING_P (binding) = 0;
374 LOCAL_BINDING_P (binding) = (level != class_binding_level);
376 /* And put it on the front of the list of bindings for ID. */
377 IDENTIFIER_BINDING (id) = binding;
380 /* Remove the binding for DECL which should be the innermost binding
381 for ID. */
383 void
384 pop_binding (tree id, tree decl)
386 cxx_binding *binding;
388 if (id == NULL_TREE)
389 /* It's easiest to write the loops that call this function without
390 checking whether or not the entities involved have names. We
391 get here for such an entity. */
392 return;
394 /* Get the innermost binding for ID. */
395 binding = IDENTIFIER_BINDING (id);
397 /* The name should be bound. */
398 gcc_assert (binding != NULL);
400 /* The DECL will be either the ordinary binding or the type
401 binding for this identifier. Remove that binding. */
402 if (binding->value == decl)
403 binding->value = NULL_TREE;
404 else
406 gcc_assert (binding->type == decl);
407 binding->type = NULL_TREE;
410 if (!binding->value && !binding->type)
412 /* We're completely done with the innermost binding for this
413 identifier. Unhook it from the list of bindings. */
414 IDENTIFIER_BINDING (id) = binding->previous;
416 /* Add it to the free list. */
417 cxx_binding_free (binding);
421 /* BINDING records an existing declaration for a name in the current scope.
422 But, DECL is another declaration for that same identifier in the
423 same scope. This is the `struct stat' hack whereby a non-typedef
424 class name or enum-name can be bound at the same level as some other
425 kind of entity.
426 3.3.7/1
428 A class name (9.1) or enumeration name (7.2) can be hidden by the
429 name of an object, function, or enumerator declared in the same scope.
430 If a class or enumeration name and an object, function, or enumerator
431 are declared in the same scope (in any order) with the same name, the
432 class or enumeration name is hidden wherever the object, function, or
433 enumerator name is visible.
435 It's the responsibility of the caller to check that
436 inserting this name is valid here. Returns nonzero if the new binding
437 was successful. */
439 static bool
440 supplement_binding_1 (cxx_binding *binding, tree decl)
442 tree bval = binding->value;
443 bool ok = true;
445 if (TREE_CODE (decl) == TYPE_DECL && DECL_ARTIFICIAL (decl))
446 /* The new name is the type name. */
447 binding->type = decl;
448 else if (/* BVAL is null when push_class_level_binding moves an
449 inherited type-binding out of the way to make room for a
450 new value binding. */
451 !bval
452 /* BVAL is error_mark_node when DECL's name has been used
453 in a non-class scope prior declaration. In that case,
454 we should have already issued a diagnostic; for graceful
455 error recovery purpose, pretend this was the intended
456 declaration for that name. */
457 || bval == error_mark_node
458 /* If BVAL is anticipated but has not yet been declared,
459 pretend it is not there at all. */
460 || (TREE_CODE (bval) == FUNCTION_DECL
461 && DECL_ANTICIPATED (bval)
462 && !DECL_HIDDEN_FRIEND_P (bval)))
463 binding->value = decl;
464 else if (TREE_CODE (bval) == TYPE_DECL && DECL_ARTIFICIAL (bval))
466 /* The old binding was a type name. It was placed in
467 VALUE field because it was thought, at the point it was
468 declared, to be the only entity with such a name. Move the
469 type name into the type slot; it is now hidden by the new
470 binding. */
471 binding->type = bval;
472 binding->value = decl;
473 binding->value_is_inherited = false;
475 else if (TREE_CODE (bval) == TYPE_DECL
476 && TREE_CODE (decl) == TYPE_DECL
477 && DECL_NAME (decl) == DECL_NAME (bval)
478 && binding->scope->kind != sk_class
479 && (same_type_p (TREE_TYPE (decl), TREE_TYPE (bval))
480 /* If either type involves template parameters, we must
481 wait until instantiation. */
482 || uses_template_parms (TREE_TYPE (decl))
483 || uses_template_parms (TREE_TYPE (bval))))
484 /* We have two typedef-names, both naming the same type to have
485 the same name. In general, this is OK because of:
487 [dcl.typedef]
489 In a given scope, a typedef specifier can be used to redefine
490 the name of any type declared in that scope to refer to the
491 type to which it already refers.
493 However, in class scopes, this rule does not apply due to the
494 stricter language in [class.mem] prohibiting redeclarations of
495 members. */
496 ok = false;
497 /* There can be two block-scope declarations of the same variable,
498 so long as they are `extern' declarations. However, there cannot
499 be two declarations of the same static data member:
501 [class.mem]
503 A member shall not be declared twice in the
504 member-specification. */
505 else if (TREE_CODE (decl) == VAR_DECL && TREE_CODE (bval) == VAR_DECL
506 && DECL_EXTERNAL (decl) && DECL_EXTERNAL (bval)
507 && !DECL_CLASS_SCOPE_P (decl))
509 duplicate_decls (decl, binding->value, /*newdecl_is_friend=*/false);
510 ok = false;
512 else if (TREE_CODE (decl) == NAMESPACE_DECL
513 && TREE_CODE (bval) == NAMESPACE_DECL
514 && DECL_NAMESPACE_ALIAS (decl)
515 && DECL_NAMESPACE_ALIAS (bval)
516 && ORIGINAL_NAMESPACE (bval) == ORIGINAL_NAMESPACE (decl))
517 /* [namespace.alias]
519 In a declarative region, a namespace-alias-definition can be
520 used to redefine a namespace-alias declared in that declarative
521 region to refer only to the namespace to which it already
522 refers. */
523 ok = false;
524 else
526 error ("declaration of %q#D", decl);
527 error ("conflicts with previous declaration %q+#D", bval);
528 ok = false;
531 return ok;
534 /* Wrapper for supplement_binding_1. */
536 static bool
537 supplement_binding (cxx_binding *binding, tree decl)
539 bool ret;
540 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
541 ret = supplement_binding_1 (binding, decl);
542 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
543 return ret;
546 /* Add DECL to the list of things declared in B. */
548 static void
549 add_decl_to_level (tree decl, cxx_scope *b)
551 /* We used to record virtual tables as if they were ordinary
552 variables, but no longer do so. */
553 gcc_assert (!(TREE_CODE (decl) == VAR_DECL && DECL_VIRTUAL_P (decl)));
555 if (TREE_CODE (decl) == NAMESPACE_DECL
556 && !DECL_NAMESPACE_ALIAS (decl))
558 DECL_CHAIN (decl) = b->namespaces;
559 b->namespaces = decl;
561 else
563 /* We build up the list in reverse order, and reverse it later if
564 necessary. */
565 TREE_CHAIN (decl) = b->names;
566 b->names = decl;
567 b->names_size++;
569 /* If appropriate, add decl to separate list of statics. We
570 include extern variables because they might turn out to be
571 static later. It's OK for this list to contain a few false
572 positives. */
573 if (b->kind == sk_namespace)
574 if ((TREE_CODE (decl) == VAR_DECL
575 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
576 || (TREE_CODE (decl) == FUNCTION_DECL
577 && (!TREE_PUBLIC (decl) || DECL_DECLARED_INLINE_P (decl))))
579 VEC_safe_push (tree, gc, b->static_decls, decl);
580 if (flag_pph_decls_debug >= 3)
582 fprintf (stderr, "Adding %p to static_decls:\n", (void*)decl);
583 print_generic_expr (stderr, decl, 0);
584 fprintf (stderr, "\n");
590 /* Record a decl-node X as belonging to the current lexical scope.
591 Check for errors (such as an incompatible declaration for the same
592 name already seen in the same scope). IS_FRIEND is true if X is
593 declared as a friend.
595 Returns either X or an old decl for the same name.
596 If an old decl is returned, it may have been smashed
597 to agree with what X says. */
599 static tree
600 pushdecl_maybe_friend_1 (tree x, bool is_friend)
602 tree t;
603 tree name;
604 int need_new_binding;
606 if (x == error_mark_node)
607 return error_mark_node;
609 need_new_binding = 1;
611 if (DECL_TEMPLATE_PARM_P (x))
612 /* Template parameters have no context; they are not X::T even
613 when declared within a class or namespace. */
615 else
617 if (current_function_decl && x != current_function_decl
618 /* A local declaration for a function doesn't constitute
619 nesting. */
620 && TREE_CODE (x) != FUNCTION_DECL
621 /* A local declaration for an `extern' variable is in the
622 scope of the current namespace, not the current
623 function. */
624 && !(TREE_CODE (x) == VAR_DECL && DECL_EXTERNAL (x))
625 /* When parsing the parameter list of a function declarator,
626 don't set DECL_CONTEXT to an enclosing function. When we
627 push the PARM_DECLs in order to process the function body,
628 current_binding_level->this_entity will be set. */
629 && !(TREE_CODE (x) == PARM_DECL
630 && current_binding_level->kind == sk_function_parms
631 && current_binding_level->this_entity == NULL)
632 && !DECL_CONTEXT (x))
633 DECL_CONTEXT (x) = current_function_decl;
635 /* If this is the declaration for a namespace-scope function,
636 but the declaration itself is in a local scope, mark the
637 declaration. */
638 if (TREE_CODE (x) == FUNCTION_DECL
639 && DECL_NAMESPACE_SCOPE_P (x)
640 && current_function_decl
641 && x != current_function_decl)
642 DECL_LOCAL_FUNCTION_P (x) = 1;
645 name = DECL_NAME (x);
646 if (name)
648 int different_binding_level = 0;
650 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
651 name = TREE_OPERAND (name, 0);
653 /* In case this decl was explicitly namespace-qualified, look it
654 up in its namespace context. */
655 if (DECL_NAMESPACE_SCOPE_P (x) && namespace_bindings_p ())
656 t = namespace_binding (name, DECL_CONTEXT (x));
657 else
658 t = lookup_name_innermost_nonclass_level (name);
660 /* [basic.link] If there is a visible declaration of an entity
661 with linkage having the same name and type, ignoring entities
662 declared outside the innermost enclosing namespace scope, the
663 block scope declaration declares that same entity and
664 receives the linkage of the previous declaration. */
665 if (! t && current_function_decl && x != current_function_decl
666 && (TREE_CODE (x) == FUNCTION_DECL || TREE_CODE (x) == VAR_DECL)
667 && DECL_EXTERNAL (x))
669 /* Look in block scope. */
670 t = innermost_non_namespace_value (name);
671 /* Or in the innermost namespace. */
672 if (! t)
673 t = namespace_binding (name, DECL_CONTEXT (x));
674 /* Does it have linkage? Note that if this isn't a DECL, it's an
675 OVERLOAD, which is OK. */
676 if (t && DECL_P (t) && ! (TREE_STATIC (t) || DECL_EXTERNAL (t)))
677 t = NULL_TREE;
678 if (t)
679 different_binding_level = 1;
682 /* If we are declaring a function, and the result of name-lookup
683 was an OVERLOAD, look for an overloaded instance that is
684 actually the same as the function we are declaring. (If
685 there is one, we have to merge our declaration with the
686 previous declaration.) */
687 if (t && TREE_CODE (t) == OVERLOAD)
689 tree match;
691 if (TREE_CODE (x) == FUNCTION_DECL)
692 for (match = t; match; match = OVL_NEXT (match))
694 if (decls_match (OVL_CURRENT (match), x))
695 break;
697 else
698 /* Just choose one. */
699 match = t;
701 if (match)
702 t = OVL_CURRENT (match);
703 else
704 t = NULL_TREE;
707 if (t && t != error_mark_node)
709 if (different_binding_level)
711 if (decls_match (x, t))
712 /* The standard only says that the local extern
713 inherits linkage from the previous decl; in
714 particular, default args are not shared. Add
715 the decl into a hash table to make sure only
716 the previous decl in this case is seen by the
717 middle end. */
719 struct cxx_int_tree_map *h;
720 void **loc;
722 TREE_PUBLIC (x) = TREE_PUBLIC (t);
724 if (cp_function_chain->extern_decl_map == NULL)
725 cp_function_chain->extern_decl_map
726 = htab_create_ggc (20, cxx_int_tree_map_hash,
727 cxx_int_tree_map_eq, NULL);
729 h = ggc_alloc_cxx_int_tree_map ();
730 h->uid = DECL_UID (x);
731 h->to = t;
732 loc = htab_find_slot_with_hash
733 (cp_function_chain->extern_decl_map, h,
734 h->uid, INSERT);
735 *(struct cxx_int_tree_map **) loc = h;
738 else if (TREE_CODE (t) == PARM_DECL)
740 /* Check for duplicate params. */
741 tree d = duplicate_decls (x, t, is_friend);
742 if (d)
743 return d;
745 else if ((DECL_EXTERN_C_FUNCTION_P (x)
746 || DECL_FUNCTION_TEMPLATE_P (x))
747 && is_overloaded_fn (t))
748 /* Don't do anything just yet. */;
749 else if (t == wchar_decl_node)
751 if (! DECL_IN_SYSTEM_HEADER (x))
752 pedwarn (input_location, OPT_pedantic, "redeclaration of %<wchar_t%> as %qT",
753 TREE_TYPE (x));
755 /* Throw away the redeclaration. */
756 return t;
758 else
760 tree olddecl = duplicate_decls (x, t, is_friend);
762 /* If the redeclaration failed, we can stop at this
763 point. */
764 if (olddecl == error_mark_node)
765 return error_mark_node;
767 if (olddecl)
769 if (TREE_CODE (t) == TYPE_DECL)
770 SET_IDENTIFIER_TYPE_VALUE (name, TREE_TYPE (t));
772 return t;
774 else if (DECL_MAIN_P (x) && TREE_CODE (t) == FUNCTION_DECL)
776 /* A redeclaration of main, but not a duplicate of the
777 previous one.
779 [basic.start.main]
781 This function shall not be overloaded. */
782 error ("invalid redeclaration of %q+D", t);
783 error ("as %qD", x);
784 /* We don't try to push this declaration since that
785 causes a crash. */
786 return x;
791 /* If x has C linkage-specification, (extern "C"),
792 lookup its binding, in case it's already bound to an object.
793 The lookup is done in all namespaces.
794 If we find an existing binding, make sure it has the same
795 exception specification as x, otherwise, bail in error [7.5, 7.6]. */
796 if ((TREE_CODE (x) == FUNCTION_DECL)
797 && DECL_EXTERN_C_P (x)
798 /* We should ignore declarations happening in system headers. */
799 && !DECL_ARTIFICIAL (x)
800 && !DECL_IN_SYSTEM_HEADER (x))
802 cxx_binding *function_binding =
803 lookup_extern_c_fun_binding_in_all_ns (x);
804 tree previous = (function_binding
805 ? function_binding->value
806 : NULL_TREE);
807 if (previous
808 && !DECL_ARTIFICIAL (previous)
809 && !DECL_IN_SYSTEM_HEADER (previous)
810 && DECL_CONTEXT (previous) != DECL_CONTEXT (x))
812 tree previous = function_binding->value;
814 /* In case either x or previous is declared to throw an exception,
815 make sure both exception specifications are equal. */
816 if (decls_match (x, previous))
818 tree x_exception_spec = NULL_TREE;
819 tree previous_exception_spec = NULL_TREE;
821 x_exception_spec =
822 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (x));
823 previous_exception_spec =
824 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (previous));
825 if (!comp_except_specs (previous_exception_spec,
826 x_exception_spec,
827 ce_normal))
829 pedwarn (input_location, 0,
830 "declaration of %q#D with C language linkage",
832 pedwarn (input_location, 0,
833 "conflicts with previous declaration %q+#D",
834 previous);
835 pedwarn (input_location, 0,
836 "due to different exception specifications");
837 return error_mark_node;
840 else
842 pedwarn (input_location, 0,
843 "declaration of %q#D with C language linkage", x);
844 pedwarn (input_location, 0,
845 "conflicts with previous declaration %q+#D",
846 previous);
851 check_template_shadow (x);
853 /* If this is a function conjured up by the back end, massage it
854 so it looks friendly. */
855 if (DECL_NON_THUNK_FUNCTION_P (x) && ! DECL_LANG_SPECIFIC (x))
857 retrofit_lang_decl (x);
858 SET_DECL_LANGUAGE (x, lang_c);
861 t = x;
862 if (DECL_NON_THUNK_FUNCTION_P (x) && ! DECL_FUNCTION_MEMBER_P (x))
864 t = push_overloaded_decl (x, PUSH_LOCAL, is_friend);
865 if (!namespace_bindings_p ())
866 /* We do not need to create a binding for this name;
867 push_overloaded_decl will have already done so if
868 necessary. */
869 need_new_binding = 0;
871 else if (DECL_FUNCTION_TEMPLATE_P (x) && DECL_NAMESPACE_SCOPE_P (x))
873 t = push_overloaded_decl (x, PUSH_GLOBAL, is_friend);
874 if (t == x)
875 add_decl_to_level (x, NAMESPACE_LEVEL (CP_DECL_CONTEXT (t)));
878 if (TREE_CODE (t) == FUNCTION_DECL || DECL_FUNCTION_TEMPLATE_P (t))
879 check_default_args (t);
881 if (t != x || DECL_FUNCTION_TEMPLATE_P (t))
882 return t;
884 /* If declaring a type as a typedef, copy the type (unless we're
885 at line 0), and install this TYPE_DECL as the new type's typedef
886 name. See the extensive comment of set_underlying_type (). */
887 if (TREE_CODE (x) == TYPE_DECL)
889 tree type = TREE_TYPE (x);
891 if (DECL_IS_BUILTIN (x)
892 || (TREE_TYPE (x) != error_mark_node
893 && TYPE_NAME (type) != x
894 /* We don't want to copy the type when all we're
895 doing is making a TYPE_DECL for the purposes of
896 inlining. */
897 && (!TYPE_NAME (type)
898 || TYPE_NAME (type) != DECL_ABSTRACT_ORIGIN (x))))
899 set_underlying_type (x);
901 if (type != error_mark_node
902 && TYPE_NAME (type)
903 && TYPE_IDENTIFIER (type))
904 set_identifier_type_value (DECL_NAME (x), x);
907 /* Multiple external decls of the same identifier ought to match.
909 We get warnings about inline functions where they are defined.
910 We get warnings about other functions from push_overloaded_decl.
912 Avoid duplicate warnings where they are used. */
913 if (TREE_PUBLIC (x) && TREE_CODE (x) != FUNCTION_DECL)
915 tree decl;
917 decl = IDENTIFIER_NAMESPACE_VALUE (name);
918 if (decl && TREE_CODE (decl) == OVERLOAD)
919 decl = OVL_FUNCTION (decl);
921 if (decl && decl != error_mark_node
922 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl))
923 /* If different sort of thing, we already gave an error. */
924 && TREE_CODE (decl) == TREE_CODE (x)
925 && !same_type_p (TREE_TYPE (x), TREE_TYPE (decl)))
927 permerror (input_location, "type mismatch with previous external decl of %q#D", x);
928 permerror (input_location, "previous external decl of %q+#D", decl);
932 if (TREE_CODE (x) == FUNCTION_DECL
933 && is_friend
934 && !flag_friend_injection)
936 /* This is a new declaration of a friend function, so hide
937 it from ordinary function lookup. */
938 DECL_ANTICIPATED (x) = 1;
939 DECL_HIDDEN_FRIEND_P (x) = 1;
942 /* This name is new in its binding level.
943 Install the new declaration and return it. */
944 if (namespace_bindings_p ())
946 /* Install a global value. */
948 /* If the first global decl has external linkage,
949 warn if we later see static one. */
950 if (IDENTIFIER_GLOBAL_VALUE (name) == NULL_TREE && TREE_PUBLIC (x))
951 TREE_PUBLIC (name) = 1;
953 /* Bind the name for the entity. */
954 if (!(TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x)
955 && t != NULL_TREE)
956 && (TREE_CODE (x) == TYPE_DECL
957 || TREE_CODE (x) == VAR_DECL
958 || TREE_CODE (x) == NAMESPACE_DECL
959 || TREE_CODE (x) == CONST_DECL
960 || TREE_CODE (x) == TEMPLATE_DECL))
961 SET_IDENTIFIER_NAMESPACE_VALUE (name, x);
963 /* If new decl is `static' and an `extern' was seen previously,
964 warn about it. */
965 if (x != NULL_TREE && t != NULL_TREE && decls_match (x, t))
966 warn_extern_redeclared_static (x, t);
968 else
970 /* Here to install a non-global value. */
971 tree oldlocal = innermost_non_namespace_value (name);
972 tree oldglobal = IDENTIFIER_NAMESPACE_VALUE (name);
974 if (need_new_binding)
976 push_local_binding (name, x, 0);
977 /* Because push_local_binding will hook X on to the
978 current_binding_level's name list, we don't want to
979 do that again below. */
980 need_new_binding = 0;
983 /* If this is a TYPE_DECL, push it into the type value slot. */
984 if (TREE_CODE (x) == TYPE_DECL)
985 set_identifier_type_value (name, x);
987 /* Clear out any TYPE_DECL shadowed by a namespace so that
988 we won't think this is a type. The C struct hack doesn't
989 go through namespaces. */
990 if (TREE_CODE (x) == NAMESPACE_DECL)
991 set_identifier_type_value (name, NULL_TREE);
993 if (oldlocal)
995 tree d = oldlocal;
997 while (oldlocal
998 && TREE_CODE (oldlocal) == VAR_DECL
999 && DECL_DEAD_FOR_LOCAL (oldlocal))
1000 oldlocal = DECL_SHADOWED_FOR_VAR (oldlocal);
1002 if (oldlocal == NULL_TREE)
1003 oldlocal = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (d));
1006 /* If this is an extern function declaration, see if we
1007 have a global definition or declaration for the function. */
1008 if (oldlocal == NULL_TREE
1009 && DECL_EXTERNAL (x)
1010 && oldglobal != NULL_TREE
1011 && TREE_CODE (x) == FUNCTION_DECL
1012 && TREE_CODE (oldglobal) == FUNCTION_DECL)
1014 /* We have one. Their types must agree. */
1015 if (decls_match (x, oldglobal))
1016 /* OK */;
1017 else
1019 warning (0, "extern declaration of %q#D doesn%'t match", x);
1020 warning (0, "global declaration %q+#D", oldglobal);
1023 /* If we have a local external declaration,
1024 and no file-scope declaration has yet been seen,
1025 then if we later have a file-scope decl it must not be static. */
1026 if (oldlocal == NULL_TREE
1027 && oldglobal == NULL_TREE
1028 && DECL_EXTERNAL (x)
1029 && TREE_PUBLIC (x))
1030 TREE_PUBLIC (name) = 1;
1032 /* Don't complain about the parms we push and then pop
1033 while tentatively parsing a function declarator. */
1034 if (TREE_CODE (x) == PARM_DECL && DECL_CONTEXT (x) == NULL_TREE)
1035 /* Ignore. */;
1037 /* Warn if shadowing an argument at the top level of the body. */
1038 else if (oldlocal != NULL_TREE && !DECL_EXTERNAL (x)
1039 /* Inline decls shadow nothing. */
1040 && !DECL_FROM_INLINE (x)
1041 && (TREE_CODE (oldlocal) == PARM_DECL
1042 || TREE_CODE (oldlocal) == VAR_DECL
1043 /* If the old decl is a type decl, only warn if the
1044 old decl is an explicit typedef or if both the old
1045 and new decls are type decls. */
1046 || (TREE_CODE (oldlocal) == TYPE_DECL
1047 && (!DECL_ARTIFICIAL (oldlocal)
1048 || TREE_CODE (x) == TYPE_DECL)))
1049 /* Don't check the `this' parameter or internally generated
1050 vars unless it's an implicit typedef (see
1051 create_implicit_typedef in decl.c). */
1052 && (!DECL_ARTIFICIAL (oldlocal)
1053 || DECL_IMPLICIT_TYPEDEF_P (oldlocal))
1054 /* Don't check for internally generated vars unless
1055 it's an implicit typedef (see create_implicit_typedef
1056 in decl.c). */
1057 && (!DECL_ARTIFICIAL (x) || DECL_IMPLICIT_TYPEDEF_P (x)))
1059 bool nowarn = false;
1061 /* Don't complain if it's from an enclosing function. */
1062 if (DECL_CONTEXT (oldlocal) == current_function_decl
1063 && TREE_CODE (x) != PARM_DECL
1064 && TREE_CODE (oldlocal) == PARM_DECL)
1066 /* Go to where the parms should be and see if we find
1067 them there. */
1068 struct cp_binding_level *b = current_binding_level->level_chain;
1070 if (FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
1071 /* Skip the ctor/dtor cleanup level. */
1072 b = b->level_chain;
1074 /* ARM $8.3 */
1075 if (b->kind == sk_function_parms)
1077 error ("declaration of %q#D shadows a parameter", x);
1078 nowarn = true;
1082 /* The local structure or class can't use parameters of
1083 the containing function anyway. */
1084 if (DECL_CONTEXT (oldlocal) != current_function_decl)
1086 cxx_scope *scope = current_binding_level;
1087 tree context = DECL_CONTEXT (oldlocal);
1088 for (; scope; scope = scope->level_chain)
1090 if (scope->kind == sk_function_parms
1091 && scope->this_entity == context)
1092 break;
1093 if (scope->kind == sk_class
1094 && !LAMBDA_TYPE_P (scope->this_entity))
1096 nowarn = true;
1097 break;
1102 if (warn_shadow && !nowarn)
1104 if (TREE_CODE (oldlocal) == PARM_DECL)
1105 warning_at (input_location, OPT_Wshadow,
1106 "declaration of %q#D shadows a parameter", x);
1107 else
1108 warning_at (input_location, OPT_Wshadow,
1109 "declaration of %qD shadows a previous local",
1111 warning_at (DECL_SOURCE_LOCATION (oldlocal), OPT_Wshadow,
1112 "shadowed declaration is here");
1116 /* Maybe warn if shadowing something else. */
1117 else if (warn_shadow && !DECL_EXTERNAL (x)
1118 /* No shadow warnings for internally generated vars unless
1119 it's an implicit typedef (see create_implicit_typedef
1120 in decl.c). */
1121 && (! DECL_ARTIFICIAL (x) || DECL_IMPLICIT_TYPEDEF_P (x))
1122 /* No shadow warnings for vars made for inlining. */
1123 && ! DECL_FROM_INLINE (x))
1125 tree member;
1127 if (current_class_ptr)
1128 member = lookup_member (current_class_type,
1129 name,
1130 /*protect=*/0,
1131 /*want_type=*/false);
1132 else
1133 member = NULL_TREE;
1135 if (member && !TREE_STATIC (member))
1137 /* Location of previous decl is not useful in this case. */
1138 warning (OPT_Wshadow, "declaration of %qD shadows a member of 'this'",
1141 else if (oldglobal != NULL_TREE
1142 && (TREE_CODE (oldglobal) == VAR_DECL
1143 /* If the old decl is a type decl, only warn if the
1144 old decl is an explicit typedef or if both the
1145 old and new decls are type decls. */
1146 || (TREE_CODE (oldglobal) == TYPE_DECL
1147 && (!DECL_ARTIFICIAL (oldglobal)
1148 || TREE_CODE (x) == TYPE_DECL))))
1149 /* XXX shadow warnings in outer-more namespaces */
1151 warning_at (input_location, OPT_Wshadow,
1152 "declaration of %qD shadows a global declaration", x);
1153 warning_at (DECL_SOURCE_LOCATION (oldglobal), OPT_Wshadow,
1154 "shadowed declaration is here");
1159 if (TREE_CODE (x) == VAR_DECL)
1160 maybe_register_incomplete_var (x);
1163 if (need_new_binding)
1164 add_decl_to_level (x,
1165 DECL_NAMESPACE_SCOPE_P (x)
1166 ? NAMESPACE_LEVEL (CP_DECL_CONTEXT (x))
1167 : current_binding_level);
1169 return x;
1172 /* Wrapper for pushdecl_maybe_friend_1. */
1174 tree
1175 pushdecl_maybe_friend (tree x, bool is_friend)
1177 tree ret;
1178 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
1179 ret = pushdecl_maybe_friend_1 (x, is_friend);
1180 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
1181 return ret;
1184 /* Record a decl-node X as belonging to the current lexical scope. */
1186 tree
1187 pushdecl (tree x)
1189 return pushdecl_maybe_friend (x, false);
1192 /* Enter DECL into the symbol table, if that's appropriate. Returns
1193 DECL, or a modified version thereof. */
1195 tree
1196 maybe_push_decl (tree decl)
1198 tree type = TREE_TYPE (decl);
1200 /* Add this decl to the current binding level, but not if it comes
1201 from another scope, e.g. a static member variable. TEM may equal
1202 DECL or it may be a previous decl of the same name. */
1203 if (decl == error_mark_node
1204 || (TREE_CODE (decl) != PARM_DECL
1205 && DECL_CONTEXT (decl) != NULL_TREE
1206 /* Definitions of namespace members outside their namespace are
1207 possible. */
1208 && !DECL_NAMESPACE_SCOPE_P (decl))
1209 || (TREE_CODE (decl) == TEMPLATE_DECL && !namespace_bindings_p ())
1210 || type == unknown_type_node
1211 /* The declaration of a template specialization does not affect
1212 the functions available for overload resolution, so we do not
1213 call pushdecl. */
1214 || (TREE_CODE (decl) == FUNCTION_DECL
1215 && DECL_TEMPLATE_SPECIALIZATION (decl)))
1216 return decl;
1217 else
1218 return pushdecl (decl);
1221 /* Bind DECL to ID in the current_binding_level, assumed to be a local
1222 binding level. If PUSH_USING is set in FLAGS, we know that DECL
1223 doesn't really belong to this binding level, that it got here
1224 through a using-declaration. */
1226 void
1227 push_local_binding (tree id, tree decl, int flags)
1229 struct cp_binding_level *b;
1231 /* Skip over any local classes. This makes sense if we call
1232 push_local_binding with a friend decl of a local class. */
1233 b = innermost_nonclass_level ();
1235 if (lookup_name_innermost_nonclass_level (id))
1237 /* Supplement the existing binding. */
1238 if (!supplement_binding (IDENTIFIER_BINDING (id), decl))
1239 /* It didn't work. Something else must be bound at this
1240 level. Do not add DECL to the list of things to pop
1241 later. */
1242 return;
1244 else
1245 /* Create a new binding. */
1246 push_binding (id, decl, b);
1248 if (TREE_CODE (decl) == OVERLOAD || (flags & PUSH_USING))
1249 /* We must put the OVERLOAD into a TREE_LIST since the
1250 TREE_CHAIN of an OVERLOAD is already used. Similarly for
1251 decls that got here through a using-declaration. */
1252 decl = build_tree_list (NULL_TREE, decl);
1254 /* And put DECL on the list of things declared by the current
1255 binding level. */
1256 add_decl_to_level (decl, b);
1259 /* Check to see whether or not DECL is a variable that would have been
1260 in scope under the ARM, but is not in scope under the ANSI/ISO
1261 standard. If so, issue an error message. If name lookup would
1262 work in both cases, but return a different result, this function
1263 returns the result of ANSI/ISO lookup. Otherwise, it returns
1264 DECL. */
1266 tree
1267 check_for_out_of_scope_variable (tree decl)
1269 tree shadowed;
1271 /* We only care about out of scope variables. */
1272 if (!(TREE_CODE (decl) == VAR_DECL && DECL_DEAD_FOR_LOCAL (decl)))
1273 return decl;
1275 shadowed = DECL_HAS_SHADOWED_FOR_VAR_P (decl)
1276 ? DECL_SHADOWED_FOR_VAR (decl) : NULL_TREE ;
1277 while (shadowed != NULL_TREE && TREE_CODE (shadowed) == VAR_DECL
1278 && DECL_DEAD_FOR_LOCAL (shadowed))
1279 shadowed = DECL_HAS_SHADOWED_FOR_VAR_P (shadowed)
1280 ? DECL_SHADOWED_FOR_VAR (shadowed) : NULL_TREE;
1281 if (!shadowed)
1282 shadowed = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (decl));
1283 if (shadowed)
1285 if (!DECL_ERROR_REPORTED (decl))
1287 warning (0, "name lookup of %qD changed", DECL_NAME (decl));
1288 warning (0, " matches this %q+D under ISO standard rules",
1289 shadowed);
1290 warning (0, " matches this %q+D under old rules", decl);
1291 DECL_ERROR_REPORTED (decl) = 1;
1293 return shadowed;
1296 /* If we have already complained about this declaration, there's no
1297 need to do it again. */
1298 if (DECL_ERROR_REPORTED (decl))
1299 return decl;
1301 DECL_ERROR_REPORTED (decl) = 1;
1303 if (TREE_TYPE (decl) == error_mark_node)
1304 return decl;
1306 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
1308 error ("name lookup of %qD changed for ISO %<for%> scoping",
1309 DECL_NAME (decl));
1310 error (" cannot use obsolete binding at %q+D because "
1311 "it has a destructor", decl);
1312 return error_mark_node;
1314 else
1316 permerror (input_location, "name lookup of %qD changed for ISO %<for%> scoping",
1317 DECL_NAME (decl));
1318 if (flag_permissive)
1319 permerror (input_location, " using obsolete binding at %q+D", decl);
1320 else
1322 static bool hint;
1323 if (!hint)
1325 inform (input_location, "(if you use %<-fpermissive%> G++ will accept your code)");
1326 hint = true;
1331 return decl;
1334 /* true means unconditionally make a BLOCK for the next level pushed. */
1336 static bool keep_next_level_flag;
1338 static int binding_depth = 0;
1340 static void
1341 indent (int depth)
1343 int i;
1345 for (i = 0; i < depth * 2; i++)
1346 putc (' ', stderr);
1349 /* Return a string describing the kind of SCOPE we have. */
1350 static const char *
1351 cxx_scope_descriptor (cxx_scope *scope)
1353 /* The order of this table must match the "scope_kind"
1354 enumerators. */
1355 static const char* scope_kind_names[] = {
1356 "block-scope",
1357 "cleanup-scope",
1358 "try-scope",
1359 "catch-scope",
1360 "for-scope",
1361 "function-parameter-scope",
1362 "class-scope",
1363 "namespace-scope",
1364 "template-parameter-scope",
1365 "template-explicit-spec-scope"
1367 const scope_kind kind = scope->explicit_spec_p
1368 ? sk_template_spec : scope->kind;
1370 return scope_kind_names[kind];
1373 /* Output a debugging information about SCOPE when performing
1374 ACTION at LINE. */
1375 static void
1376 cxx_scope_debug (cxx_scope *scope, int line, const char *action)
1378 const char *desc = cxx_scope_descriptor (scope);
1379 if (scope->this_entity)
1380 verbatim ("%s %s(%E) %p %d\n", action, desc,
1381 scope->this_entity, (void *) scope, line);
1382 else
1383 verbatim ("%s %s %p %d\n", action, desc, (void *) scope, line);
1386 /* Return the estimated initial size of the hashtable of a NAMESPACE
1387 scope. */
1389 static inline size_t
1390 namespace_scope_ht_size (tree ns)
1392 tree name = DECL_NAME (ns);
1394 return name == std_identifier
1395 ? NAMESPACE_STD_HT_SIZE
1396 : (name == global_scope_name
1397 ? GLOBAL_SCOPE_HT_SIZE
1398 : NAMESPACE_ORDINARY_HT_SIZE);
1401 /* A chain of binding_level structures awaiting reuse. */
1403 static GTY((deletable)) struct cp_binding_level *free_binding_level;
1405 /* Insert SCOPE as the innermost binding level. */
1407 void
1408 push_binding_level (struct cp_binding_level *scope)
1410 /* Add it to the front of currently active scopes stack. */
1411 scope->level_chain = current_binding_level;
1412 current_binding_level = scope;
1413 keep_next_level_flag = false;
1415 if (ENABLE_SCOPE_CHECKING)
1417 scope->binding_depth = binding_depth;
1418 indent (binding_depth);
1419 cxx_scope_debug (scope, input_line, "push");
1420 binding_depth++;
1424 /* Create a new KIND scope and make it the top of the active scopes stack.
1425 ENTITY is the scope of the associated C++ entity (namespace, class,
1426 function, C++0x enumeration); it is NULL otherwise. */
1428 cxx_scope *
1429 begin_scope (scope_kind kind, tree entity)
1431 cxx_scope *scope;
1433 /* Reuse or create a struct for this binding level. */
1434 if (!ENABLE_SCOPE_CHECKING && free_binding_level)
1436 scope = free_binding_level;
1437 memset (scope, 0, sizeof (cxx_scope));
1438 free_binding_level = scope->level_chain;
1440 else
1441 scope = ggc_alloc_cleared_cxx_scope ();
1443 scope->this_entity = entity;
1444 scope->more_cleanups_ok = true;
1445 switch (kind)
1447 case sk_cleanup:
1448 scope->keep = true;
1449 break;
1451 case sk_template_spec:
1452 scope->explicit_spec_p = true;
1453 kind = sk_template_parms;
1454 /* Fall through. */
1455 case sk_template_parms:
1456 case sk_block:
1457 case sk_try:
1458 case sk_catch:
1459 case sk_for:
1460 case sk_class:
1461 case sk_scoped_enum:
1462 case sk_function_parms:
1463 case sk_omp:
1464 scope->keep = keep_next_level_flag;
1465 break;
1467 case sk_namespace:
1468 NAMESPACE_LEVEL (entity) = scope;
1469 scope->static_decls =
1470 VEC_alloc (tree, gc,
1471 DECL_NAME (entity) == std_identifier
1472 || DECL_NAME (entity) == global_scope_name
1473 ? 200 : 10);
1474 break;
1476 default:
1477 /* Should not happen. */
1478 gcc_unreachable ();
1479 break;
1481 scope->kind = kind;
1483 push_binding_level (scope);
1485 return scope;
1488 /* We're about to leave current scope. Pop the top of the stack of
1489 currently active scopes. Return the enclosing scope, now active. */
1491 cxx_scope *
1492 leave_scope (void)
1494 cxx_scope *scope = current_binding_level;
1496 if (scope->kind == sk_namespace && class_binding_level)
1497 current_binding_level = class_binding_level;
1499 /* We cannot leave a scope, if there are none left. */
1500 if (NAMESPACE_LEVEL (global_namespace))
1501 gcc_assert (!global_scope_p (scope));
1503 if (ENABLE_SCOPE_CHECKING)
1505 indent (--binding_depth);
1506 cxx_scope_debug (scope, input_line, "leave");
1509 /* Move one nesting level up. */
1510 current_binding_level = scope->level_chain;
1512 /* Namespace-scopes are left most probably temporarily, not
1513 completely; they can be reopened later, e.g. in namespace-extension
1514 or any name binding activity that requires us to resume a
1515 namespace. For classes, we cache some binding levels. For other
1516 scopes, we just make the structure available for reuse. */
1517 if (scope->kind != sk_namespace
1518 && scope->kind != sk_class)
1520 scope->level_chain = free_binding_level;
1521 gcc_assert (!ENABLE_SCOPE_CHECKING
1522 || scope->binding_depth == binding_depth);
1523 free_binding_level = scope;
1526 /* Find the innermost enclosing class scope, and reset
1527 CLASS_BINDING_LEVEL appropriately. */
1528 if (scope->kind == sk_class)
1530 class_binding_level = NULL;
1531 for (scope = current_binding_level; scope; scope = scope->level_chain)
1532 if (scope->kind == sk_class)
1534 class_binding_level = scope;
1535 break;
1539 return current_binding_level;
1542 static void
1543 resume_scope (struct cp_binding_level* b)
1545 /* Resuming binding levels is meant only for namespaces,
1546 and those cannot nest into classes. */
1547 gcc_assert (!class_binding_level);
1548 /* Also, resuming a non-directly nested namespace is a no-no. */
1549 gcc_assert (b->level_chain == current_binding_level);
1550 current_binding_level = b;
1551 if (ENABLE_SCOPE_CHECKING)
1553 b->binding_depth = binding_depth;
1554 indent (binding_depth);
1555 cxx_scope_debug (b, input_line, "resume");
1556 binding_depth++;
1560 /* Return the innermost binding level that is not for a class scope. */
1562 static cxx_scope *
1563 innermost_nonclass_level (void)
1565 cxx_scope *b;
1567 b = current_binding_level;
1568 while (b->kind == sk_class)
1569 b = b->level_chain;
1571 return b;
1574 /* We're defining an object of type TYPE. If it needs a cleanup, but
1575 we're not allowed to add any more objects with cleanups to the current
1576 scope, create a new binding level. */
1578 void
1579 maybe_push_cleanup_level (tree type)
1581 if (type != error_mark_node
1582 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
1583 && current_binding_level->more_cleanups_ok == 0)
1585 begin_scope (sk_cleanup, NULL);
1586 current_binding_level->statement_list = push_stmt_list ();
1590 /* Nonzero if we are currently in the global binding level. */
1593 global_bindings_p (void)
1595 return global_scope_p (current_binding_level);
1598 /* True if we are currently in a toplevel binding level. This
1599 means either the global binding level or a namespace in a toplevel
1600 binding level. Since there are no non-toplevel namespace levels,
1601 this really means any namespace or template parameter level. We
1602 also include a class whose context is toplevel. */
1604 bool
1605 toplevel_bindings_p (void)
1607 struct cp_binding_level *b = innermost_nonclass_level ();
1609 return b->kind == sk_namespace || b->kind == sk_template_parms;
1612 /* True if this is a namespace scope, or if we are defining a class
1613 which is itself at namespace scope, or whose enclosing class is
1614 such a class, etc. */
1616 bool
1617 namespace_bindings_p (void)
1619 struct cp_binding_level *b = innermost_nonclass_level ();
1621 return b->kind == sk_namespace;
1624 /* True if the current level needs to have a BLOCK made. */
1626 bool
1627 kept_level_p (void)
1629 return (current_binding_level->blocks != NULL_TREE
1630 || current_binding_level->keep
1631 || current_binding_level->kind == sk_cleanup
1632 || current_binding_level->names != NULL_TREE
1633 || current_binding_level->using_directives);
1636 /* Returns the kind of the innermost scope. */
1638 scope_kind
1639 innermost_scope_kind (void)
1641 return current_binding_level->kind;
1644 /* Returns true if this scope was created to store template parameters. */
1646 bool
1647 template_parm_scope_p (void)
1649 return innermost_scope_kind () == sk_template_parms;
1652 /* If KEEP is true, make a BLOCK node for the next binding level,
1653 unconditionally. Otherwise, use the normal logic to decide whether
1654 or not to create a BLOCK. */
1656 void
1657 keep_next_level (bool keep)
1659 keep_next_level_flag = keep;
1662 /* Return the list of declarations of the current level.
1663 Note that this list is in reverse order unless/until
1664 you nreverse it; and when you do nreverse it, you must
1665 store the result back using `storedecls' or you will lose. */
1667 tree
1668 getdecls (void)
1670 return current_binding_level->names;
1673 /* Return how many function prototypes we are currently nested inside. */
1676 function_parm_depth (void)
1678 int level = 0;
1679 struct cp_binding_level *b;
1681 for (b = current_binding_level;
1682 b->kind == sk_function_parms;
1683 b = b->level_chain)
1684 ++level;
1686 return level;
1689 /* For debugging. */
1690 static int no_print_functions = 0;
1691 static int no_print_builtins = 0;
1693 static void
1694 print_binding_level (struct cp_binding_level* lvl)
1696 tree t;
1697 int i = 0, len;
1698 fprintf (stderr, " blocks=%p", (void *) lvl->blocks);
1699 if (lvl->more_cleanups_ok)
1700 fprintf (stderr, " more-cleanups-ok");
1701 if (lvl->have_cleanups)
1702 fprintf (stderr, " have-cleanups");
1703 fprintf (stderr, "\n");
1704 if (lvl->names)
1706 fprintf (stderr, " names:\t");
1707 /* We can probably fit 3 names to a line? */
1708 for (t = lvl->names; t; t = TREE_CHAIN (t))
1710 if (no_print_functions && (TREE_CODE (t) == FUNCTION_DECL))
1711 continue;
1712 if (no_print_builtins
1713 && (TREE_CODE (t) == TYPE_DECL)
1714 && DECL_IS_BUILTIN (t))
1715 continue;
1717 /* Function decls tend to have longer names. */
1718 if (TREE_CODE (t) == FUNCTION_DECL)
1719 len = 3;
1720 else
1721 len = 2;
1722 i += len;
1723 if (i > 6)
1725 fprintf (stderr, "\n\t");
1726 i = len;
1728 print_node_brief (stderr, "", t, 0);
1729 if (t == error_mark_node)
1730 break;
1732 if (i)
1733 fprintf (stderr, "\n");
1735 if (VEC_length (cp_class_binding, lvl->class_shadowed))
1737 size_t i;
1738 cp_class_binding *b;
1739 fprintf (stderr, " class-shadowed:");
1740 FOR_EACH_VEC_ELT (cp_class_binding, lvl->class_shadowed, i, b)
1741 fprintf (stderr, " %s ", IDENTIFIER_POINTER (b->identifier));
1742 fprintf (stderr, "\n");
1744 if (lvl->type_shadowed)
1746 fprintf (stderr, " type-shadowed:");
1747 for (t = lvl->type_shadowed; t; t = TREE_CHAIN (t))
1749 fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
1751 fprintf (stderr, "\n");
1756 /* Print the given namespace decl NS to stderr. */
1758 void
1759 print_namespace (tree ns)
1761 struct cp_binding_level *level = NAMESPACE_LEVEL (ns);
1763 fprintf (stderr, "Namespace name: ");
1764 print_generic_expr (stderr, ns, 0);
1765 fprintf (stderr, "\nBinding contour\n");
1766 print_binding_level (level);
1770 void
1771 print_other_binding_stack (struct cp_binding_level *stack)
1773 struct cp_binding_level *level;
1774 for (level = stack; !global_scope_p (level); level = level->level_chain)
1776 fprintf (stderr, "binding level %p\n", (void *) level);
1777 print_binding_level (level);
1781 void
1782 print_binding_stack (void)
1784 struct cp_binding_level *b;
1785 fprintf (stderr, "current_binding_level=%p\n"
1786 "class_binding_level=%p\n"
1787 "NAMESPACE_LEVEL (global_namespace)=%p\n",
1788 (void *) current_binding_level, (void *) class_binding_level,
1789 (void *) NAMESPACE_LEVEL (global_namespace));
1790 if (class_binding_level)
1792 for (b = class_binding_level; b; b = b->level_chain)
1793 if (b == current_binding_level)
1794 break;
1795 if (b)
1796 b = class_binding_level;
1797 else
1798 b = current_binding_level;
1800 else
1801 b = current_binding_level;
1802 print_other_binding_stack (b);
1803 fprintf (stderr, "global:\n");
1804 print_binding_level (NAMESPACE_LEVEL (global_namespace));
1807 /* Return the type associated with id. */
1809 static tree
1810 identifier_type_value_1 (tree id)
1812 timevar_push (TV_NAME_LOOKUP);
1813 /* There is no type with that name, anywhere. */
1814 if (REAL_IDENTIFIER_TYPE_VALUE (id) == NULL_TREE)
1815 return NULL_TREE;
1816 /* This is not the type marker, but the real thing. */
1817 if (REAL_IDENTIFIER_TYPE_VALUE (id) != global_type_node)
1818 return REAL_IDENTIFIER_TYPE_VALUE (id);
1819 /* Have to search for it. It must be on the global level, now.
1820 Ask lookup_name not to return non-types. */
1821 id = lookup_name_real (id, 2, 1, /*block_p=*/true, 0, LOOKUP_COMPLAIN);
1822 if (id)
1823 return TREE_TYPE (id);
1824 return NULL_TREE;
1827 /* Wrapper for identifier_type_value_1. */
1829 tree
1830 identifier_type_value (tree id)
1832 tree ret;
1833 timevar_start (TV_NAME_LOOKUP);
1834 ret = identifier_type_value_1 (id);
1835 pph_catch_name_lookup (ret);
1836 timevar_stop (TV_NAME_LOOKUP);
1837 return ret;
1841 /* Return the IDENTIFIER_GLOBAL_VALUE of T, for use in common code, since
1842 the definition of IDENTIFIER_GLOBAL_VALUE is different for C and C++. */
1844 tree
1845 identifier_global_value (tree t)
1847 return IDENTIFIER_GLOBAL_VALUE (t);
1850 /* Push a definition of struct, union or enum tag named ID. into
1851 binding_level B. DECL is a TYPE_DECL for the type. We assume that
1852 the tag ID is not already defined. */
1854 static void
1855 set_identifier_type_value_with_scope (tree id, tree decl, cxx_scope *b)
1857 tree type;
1859 if (b->kind != sk_namespace)
1861 /* Shadow the marker, not the real thing, so that the marker
1862 gets restored later. */
1863 tree old_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
1864 b->type_shadowed
1865 = tree_cons (id, old_type_value, b->type_shadowed);
1866 type = decl ? TREE_TYPE (decl) : NULL_TREE;
1867 TREE_TYPE (b->type_shadowed) = type;
1869 else
1871 cxx_binding *binding =
1872 binding_for_name (NAMESPACE_LEVEL (current_namespace), id);
1873 gcc_assert (decl);
1874 if (binding->value)
1875 supplement_binding (binding, decl);
1876 else
1877 binding->value = decl;
1879 /* Store marker instead of real type. */
1880 type = global_type_node;
1882 SET_IDENTIFIER_TYPE_VALUE (id, type);
1885 /* As set_identifier_type_value_with_scope, but using
1886 current_binding_level. */
1888 void
1889 set_identifier_type_value (tree id, tree decl)
1891 set_identifier_type_value_with_scope (id, decl, current_binding_level);
1894 /* Return the name for the constructor (or destructor) for the
1895 specified class TYPE. When given a template, this routine doesn't
1896 lose the specialization. */
1898 static inline tree
1899 constructor_name_full (tree type)
1901 return TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
1904 /* Return the name for the constructor (or destructor) for the
1905 specified class. When given a template, return the plain
1906 unspecialized name. */
1908 tree
1909 constructor_name (tree type)
1911 tree name;
1912 name = constructor_name_full (type);
1913 if (IDENTIFIER_TEMPLATE (name))
1914 name = IDENTIFIER_TEMPLATE (name);
1915 return name;
1918 /* Returns TRUE if NAME is the name for the constructor for TYPE,
1919 which must be a class type. */
1921 bool
1922 constructor_name_p (tree name, tree type)
1924 tree ctor_name;
1926 gcc_assert (MAYBE_CLASS_TYPE_P (type));
1928 if (!name)
1929 return false;
1931 if (TREE_CODE (name) != IDENTIFIER_NODE)
1932 return false;
1934 ctor_name = constructor_name_full (type);
1935 if (name == ctor_name)
1936 return true;
1937 if (IDENTIFIER_TEMPLATE (ctor_name)
1938 && name == IDENTIFIER_TEMPLATE (ctor_name))
1939 return true;
1940 return false;
1943 /* Counter used to create anonymous type names. */
1945 static GTY(()) int anon_cnt;
1947 /* Return an IDENTIFIER which can be used as a name for
1948 anonymous structs and unions. */
1950 tree
1951 make_anon_name (void)
1953 char buf[32];
1955 sprintf (buf, ANON_AGGRNAME_FORMAT, anon_cnt++);
1956 return get_identifier (buf);
1959 /* This code is practically identical to that for creating
1960 anonymous names, but is just used for lambdas instead. This is necessary
1961 because anonymous names are recognized and cannot be passed to template
1962 functions. */
1963 /* FIXME is this still necessary? */
1965 static GTY(()) int lambda_cnt = 0;
1967 tree
1968 make_lambda_name (void)
1970 char buf[32];
1972 sprintf (buf, LAMBDANAME_FORMAT, lambda_cnt++);
1973 return get_identifier (buf);
1976 /* Return (from the stack of) the BINDING, if any, established at SCOPE. */
1978 static inline cxx_binding *
1979 find_binding (cxx_scope *scope, cxx_binding *binding)
1981 for (; binding != NULL; binding = binding->previous)
1982 if (binding->scope == scope)
1983 return binding;
1985 return (cxx_binding *)0;
1988 /* Return the binding for NAME in SCOPE, if any. Otherwise, return NULL. */
1990 static inline cxx_binding *
1991 cxx_scope_find_binding_for_name (cxx_scope *scope, tree name)
1993 cxx_binding *b = IDENTIFIER_NAMESPACE_BINDINGS (name);
1994 if (b)
1996 /* Fold-in case where NAME is used only once. */
1997 if (scope == b->scope && b->previous == NULL)
1998 return b;
1999 return find_binding (scope, b);
2001 return NULL;
2004 /* Always returns a binding for name in scope. If no binding is
2005 found, make a new one. */
2007 static cxx_binding *
2008 binding_for_name (cxx_scope *scope, tree name)
2010 cxx_binding *result;
2012 result = cxx_scope_find_binding_for_name (scope, name);
2013 if (result)
2014 return result;
2015 /* Not found, make a new one. */
2016 result = cxx_binding_make (NULL, NULL);
2017 result->previous = IDENTIFIER_NAMESPACE_BINDINGS (name);
2018 result->scope = scope;
2019 result->is_local = false;
2020 result->value_is_inherited = false;
2021 IDENTIFIER_NAMESPACE_BINDINGS (name) = result;
2022 return result;
2025 /* Walk through the bindings associated to the name of FUNCTION,
2026 and return the first binding that declares a function with a
2027 "C" linkage specification, a.k.a 'extern "C"'.
2028 This function looks for the binding, regardless of which scope it
2029 has been defined in. It basically looks in all the known scopes.
2030 Note that this function does not lookup for bindings of builtin functions
2031 or for functions declared in system headers. */
2032 static cxx_binding*
2033 lookup_extern_c_fun_binding_in_all_ns (tree function)
2035 tree name;
2036 cxx_binding *iter;
2038 gcc_assert (function && TREE_CODE (function) == FUNCTION_DECL);
2040 name = DECL_NAME (function);
2041 gcc_assert (name && TREE_CODE (name) == IDENTIFIER_NODE);
2043 for (iter = IDENTIFIER_NAMESPACE_BINDINGS (name);
2044 iter;
2045 iter = iter->previous)
2047 if (iter->value
2048 && TREE_CODE (iter->value) == FUNCTION_DECL
2049 && DECL_EXTERN_C_P (iter->value)
2050 && !DECL_ARTIFICIAL (iter->value))
2052 return iter;
2055 return NULL;
2058 /* Insert another USING_DECL into the current binding level, returning
2059 this declaration. If this is a redeclaration, do nothing, and
2060 return NULL_TREE if this not in namespace scope (in namespace
2061 scope, a using decl might extend any previous bindings). */
2063 static tree
2064 push_using_decl_1 (tree scope, tree name)
2066 tree decl;
2068 gcc_assert (TREE_CODE (scope) == NAMESPACE_DECL);
2069 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
2070 for (decl = current_binding_level->usings; decl; decl = DECL_CHAIN (decl))
2071 if (USING_DECL_SCOPE (decl) == scope && DECL_NAME (decl) == name)
2072 break;
2073 if (decl)
2074 return namespace_bindings_p () ? decl : NULL_TREE;
2075 decl = build_lang_decl (USING_DECL, name, NULL_TREE);
2076 USING_DECL_SCOPE (decl) = scope;
2077 DECL_CHAIN (decl) = current_binding_level->usings;
2078 current_binding_level->usings = decl;
2079 return decl;
2082 /* Wrapper for push_using_decl_1. */
2084 static tree
2085 push_using_decl (tree scope, tree name)
2087 tree ret;
2088 timevar_start (TV_NAME_LOOKUP);
2089 ret = push_using_decl_1 (scope, name);
2090 timevar_stop (TV_NAME_LOOKUP);
2091 return ret;
2094 /* Same as pushdecl, but define X in binding-level LEVEL. We rely on the
2095 caller to set DECL_CONTEXT properly. */
2097 static tree
2098 pushdecl_with_scope_1 (tree x, cxx_scope *level, bool is_friend)
2100 struct cp_binding_level *b;
2101 tree function_decl = current_function_decl;
2103 current_function_decl = NULL_TREE;
2104 if (level->kind == sk_class)
2106 b = class_binding_level;
2107 class_binding_level = level;
2108 pushdecl_class_level (x);
2109 class_binding_level = b;
2111 else
2113 b = current_binding_level;
2114 current_binding_level = level;
2115 x = pushdecl_maybe_friend (x, is_friend);
2116 current_binding_level = b;
2118 current_function_decl = function_decl;
2119 return x;
2122 /* Wrapper for pushdecl_with_scope_1. */
2124 tree
2125 pushdecl_with_scope (tree x, cxx_scope *level, bool is_friend)
2127 tree ret;
2128 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
2129 ret = pushdecl_with_scope_1 (x, level, is_friend);
2130 pph_catch_name_lookup (ret);
2131 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
2132 return ret;
2136 /* DECL is a FUNCTION_DECL for a non-member function, which may have
2137 other definitions already in place. We get around this by making
2138 the value of the identifier point to a list of all the things that
2139 want to be referenced by that name. It is then up to the users of
2140 that name to decide what to do with that list.
2142 DECL may also be a TEMPLATE_DECL, with a FUNCTION_DECL in its
2143 DECL_TEMPLATE_RESULT. It is dealt with the same way.
2145 FLAGS is a bitwise-or of the following values:
2146 PUSH_LOCAL: Bind DECL in the current scope, rather than at
2147 namespace scope.
2148 PUSH_USING: DECL is being pushed as the result of a using
2149 declaration.
2151 IS_FRIEND is true if this is a friend declaration.
2153 The value returned may be a previous declaration if we guessed wrong
2154 about what language DECL should belong to (C or C++). Otherwise,
2155 it's always DECL (and never something that's not a _DECL). */
2157 static tree
2158 push_overloaded_decl_1 (tree decl, int flags, bool is_friend)
2160 tree name = DECL_NAME (decl);
2161 tree old;
2162 tree new_binding;
2163 int doing_global = (namespace_bindings_p () || !(flags & PUSH_LOCAL));
2165 if (doing_global)
2166 old = namespace_binding (name, DECL_CONTEXT (decl));
2167 else
2168 old = lookup_name_innermost_nonclass_level (name);
2170 if (old)
2172 if (TREE_CODE (old) == TYPE_DECL && DECL_ARTIFICIAL (old))
2174 tree t = TREE_TYPE (old);
2175 if (MAYBE_CLASS_TYPE_P (t) && warn_shadow
2176 && (! DECL_IN_SYSTEM_HEADER (decl)
2177 || ! DECL_IN_SYSTEM_HEADER (old)))
2178 warning (OPT_Wshadow, "%q#D hides constructor for %q#T", decl, t);
2179 old = NULL_TREE;
2181 else if (is_overloaded_fn (old))
2183 tree tmp;
2185 for (tmp = old; tmp; tmp = OVL_NEXT (tmp))
2187 tree fn = OVL_CURRENT (tmp);
2188 tree dup;
2190 if (TREE_CODE (tmp) == OVERLOAD && OVL_USED (tmp)
2191 && !(flags & PUSH_USING)
2192 && compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
2193 TYPE_ARG_TYPES (TREE_TYPE (decl)))
2194 && ! decls_match (fn, decl))
2195 error ("%q#D conflicts with previous using declaration %q#D",
2196 decl, fn);
2198 dup = duplicate_decls (decl, fn, is_friend);
2199 /* If DECL was a redeclaration of FN -- even an invalid
2200 one -- pass that information along to our caller. */
2201 if (dup == fn || dup == error_mark_node)
2202 return dup;
2205 /* We don't overload implicit built-ins. duplicate_decls()
2206 may fail to merge the decls if the new decl is e.g. a
2207 template function. */
2208 if (TREE_CODE (old) == FUNCTION_DECL
2209 && DECL_ANTICIPATED (old)
2210 && !DECL_HIDDEN_FRIEND_P (old))
2211 old = NULL;
2213 else if (old == error_mark_node)
2214 /* Ignore the undefined symbol marker. */
2215 old = NULL_TREE;
2216 else
2218 error ("previous non-function declaration %q+#D", old);
2219 error ("conflicts with function declaration %q#D", decl);
2220 return decl;
2224 if (old || TREE_CODE (decl) == TEMPLATE_DECL
2225 /* If it's a using declaration, we always need to build an OVERLOAD,
2226 because it's the only way to remember that the declaration comes
2227 from 'using', and have the lookup behave correctly. */
2228 || (flags & PUSH_USING))
2230 if (old && TREE_CODE (old) != OVERLOAD)
2231 new_binding = ovl_cons (decl, ovl_cons (old, NULL_TREE));
2232 else
2233 new_binding = ovl_cons (decl, old);
2234 if (flags & PUSH_USING)
2235 OVL_USED (new_binding) = 1;
2237 else
2238 /* NAME is not ambiguous. */
2239 new_binding = decl;
2241 if (doing_global)
2242 set_namespace_binding (name, current_namespace, new_binding);
2243 else
2245 /* We only create an OVERLOAD if there was a previous binding at
2246 this level, or if decl is a template. In the former case, we
2247 need to remove the old binding and replace it with the new
2248 binding. We must also run through the NAMES on the binding
2249 level where the name was bound to update the chain. */
2251 if (TREE_CODE (new_binding) == OVERLOAD && old)
2253 tree *d;
2255 for (d = &IDENTIFIER_BINDING (name)->scope->names;
2257 d = &TREE_CHAIN (*d))
2258 if (*d == old
2259 || (TREE_CODE (*d) == TREE_LIST
2260 && TREE_VALUE (*d) == old))
2262 if (TREE_CODE (*d) == TREE_LIST)
2263 /* Just replace the old binding with the new. */
2264 TREE_VALUE (*d) = new_binding;
2265 else
2266 /* Build a TREE_LIST to wrap the OVERLOAD. */
2267 *d = tree_cons (NULL_TREE, new_binding,
2268 TREE_CHAIN (*d));
2270 /* And update the cxx_binding node. */
2271 IDENTIFIER_BINDING (name)->value = new_binding;
2272 return decl;
2275 /* We should always find a previous binding in this case. */
2276 gcc_unreachable ();
2279 /* Install the new binding. */
2280 push_local_binding (name, new_binding, flags);
2283 return decl;
2286 /* Wrapper for push_overloaded_decl_1. */
2288 static tree
2289 push_overloaded_decl (tree decl, int flags, bool is_friend)
2291 tree ret;
2292 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
2293 ret = push_overloaded_decl_1 (decl, flags, is_friend);
2294 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
2295 return ret;
2298 /* Check a non-member using-declaration. Return the name and scope
2299 being used, and the USING_DECL, or NULL_TREE on failure. */
2301 static tree
2302 validate_nonmember_using_decl (tree decl, tree scope, tree name)
2304 /* [namespace.udecl]
2305 A using-declaration for a class member shall be a
2306 member-declaration. */
2307 if (TYPE_P (scope))
2309 error ("%qT is not a namespace", scope);
2310 return NULL_TREE;
2312 else if (scope == error_mark_node)
2313 return NULL_TREE;
2315 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR)
2317 /* 7.3.3/5
2318 A using-declaration shall not name a template-id. */
2319 error ("a using-declaration cannot specify a template-id. "
2320 "Try %<using %D%>", name);
2321 return NULL_TREE;
2324 if (TREE_CODE (decl) == NAMESPACE_DECL)
2326 error ("namespace %qD not allowed in using-declaration", decl);
2327 return NULL_TREE;
2330 if (TREE_CODE (decl) == SCOPE_REF)
2332 /* It's a nested name with template parameter dependent scope.
2333 This can only be using-declaration for class member. */
2334 error ("%qT is not a namespace", TREE_OPERAND (decl, 0));
2335 return NULL_TREE;
2338 if (is_overloaded_fn (decl))
2339 decl = get_first_fn (decl);
2341 gcc_assert (DECL_P (decl));
2343 /* Make a USING_DECL. */
2344 return push_using_decl (scope, name);
2347 /* Process local and global using-declarations. */
2349 static void
2350 do_nonmember_using_decl (tree scope, tree name, tree oldval, tree oldtype,
2351 tree *newval, tree *newtype)
2353 struct scope_binding decls = EMPTY_SCOPE_BINDING;
2355 *newval = *newtype = NULL_TREE;
2356 if (!qualified_lookup_using_namespace (name, scope, &decls, 0))
2357 /* Lookup error */
2358 return;
2360 if (!decls.value && !decls.type)
2362 error ("%qD not declared", name);
2363 return;
2366 /* Shift the old and new bindings around so we're comparing class and
2367 enumeration names to each other. */
2368 if (oldval && DECL_IMPLICIT_TYPEDEF_P (oldval))
2370 oldtype = oldval;
2371 oldval = NULL_TREE;
2374 if (decls.value && DECL_IMPLICIT_TYPEDEF_P (decls.value))
2376 decls.type = decls.value;
2377 decls.value = NULL_TREE;
2380 /* It is impossible to overload a built-in function; any explicit
2381 declaration eliminates the built-in declaration. So, if OLDVAL
2382 is a built-in, then we can just pretend it isn't there. */
2383 if (oldval
2384 && TREE_CODE (oldval) == FUNCTION_DECL
2385 && DECL_ANTICIPATED (oldval)
2386 && !DECL_HIDDEN_FRIEND_P (oldval))
2387 oldval = NULL_TREE;
2389 if (decls.value)
2391 /* Check for using functions. */
2392 if (is_overloaded_fn (decls.value))
2394 tree tmp, tmp1;
2396 if (oldval && !is_overloaded_fn (oldval))
2398 error ("%qD is already declared in this scope", name);
2399 oldval = NULL_TREE;
2402 *newval = oldval;
2403 for (tmp = decls.value; tmp; tmp = OVL_NEXT (tmp))
2405 tree new_fn = OVL_CURRENT (tmp);
2407 /* [namespace.udecl]
2409 If a function declaration in namespace scope or block
2410 scope has the same name and the same parameter types as a
2411 function introduced by a using declaration the program is
2412 ill-formed. */
2413 for (tmp1 = oldval; tmp1; tmp1 = OVL_NEXT (tmp1))
2415 tree old_fn = OVL_CURRENT (tmp1);
2417 if (new_fn == old_fn)
2418 /* The function already exists in the current namespace. */
2419 break;
2420 else if (OVL_USED (tmp1))
2421 continue; /* this is a using decl */
2422 else if (compparms (TYPE_ARG_TYPES (TREE_TYPE (new_fn)),
2423 TYPE_ARG_TYPES (TREE_TYPE (old_fn))))
2425 gcc_assert (!DECL_ANTICIPATED (old_fn)
2426 || DECL_HIDDEN_FRIEND_P (old_fn));
2428 /* There was already a non-using declaration in
2429 this scope with the same parameter types. If both
2430 are the same extern "C" functions, that's ok. */
2431 if (decls_match (new_fn, old_fn))
2432 break;
2433 else
2435 error ("%qD is already declared in this scope", name);
2436 break;
2441 /* If we broke out of the loop, there's no reason to add
2442 this function to the using declarations for this
2443 scope. */
2444 if (tmp1)
2445 continue;
2447 /* If we are adding to an existing OVERLOAD, then we no
2448 longer know the type of the set of functions. */
2449 if (*newval && TREE_CODE (*newval) == OVERLOAD)
2450 TREE_TYPE (*newval) = unknown_type_node;
2451 /* Add this new function to the set. */
2452 *newval = build_overload (OVL_CURRENT (tmp), *newval);
2453 /* If there is only one function, then we use its type. (A
2454 using-declaration naming a single function can be used in
2455 contexts where overload resolution cannot be
2456 performed.) */
2457 if (TREE_CODE (*newval) != OVERLOAD)
2459 *newval = ovl_cons (*newval, NULL_TREE);
2460 TREE_TYPE (*newval) = TREE_TYPE (OVL_CURRENT (tmp));
2462 OVL_USED (*newval) = 1;
2465 else
2467 *newval = decls.value;
2468 if (oldval && !decls_match (*newval, oldval))
2469 error ("%qD is already declared in this scope", name);
2472 else
2473 *newval = oldval;
2475 if (decls.type && TREE_CODE (decls.type) == TREE_LIST)
2477 error ("reference to %qD is ambiguous", name);
2478 print_candidates (decls.type);
2480 else
2482 *newtype = decls.type;
2483 if (oldtype && *newtype && !decls_match (oldtype, *newtype))
2484 error ("%qD is already declared in this scope", name);
2487 /* If *newval is empty, shift any class or enumeration name down. */
2488 if (!*newval)
2490 *newval = *newtype;
2491 *newtype = NULL_TREE;
2495 /* Process a using-declaration at function scope. */
2497 void
2498 do_local_using_decl (tree decl, tree scope, tree name)
2500 tree oldval, oldtype, newval, newtype;
2501 tree orig_decl = decl;
2503 decl = validate_nonmember_using_decl (decl, scope, name);
2504 if (decl == NULL_TREE)
2505 return;
2507 if (building_stmt_tree ()
2508 && at_function_scope_p ())
2509 add_decl_expr (decl);
2511 oldval = lookup_name_innermost_nonclass_level (name);
2512 oldtype = lookup_type_current_level (name);
2514 do_nonmember_using_decl (scope, name, oldval, oldtype, &newval, &newtype);
2516 if (newval)
2518 if (is_overloaded_fn (newval))
2520 tree fn, term;
2522 /* We only need to push declarations for those functions
2523 that were not already bound in the current level.
2524 The old value might be NULL_TREE, it might be a single
2525 function, or an OVERLOAD. */
2526 if (oldval && TREE_CODE (oldval) == OVERLOAD)
2527 term = OVL_FUNCTION (oldval);
2528 else
2529 term = oldval;
2530 for (fn = newval; fn && OVL_CURRENT (fn) != term;
2531 fn = OVL_NEXT (fn))
2532 push_overloaded_decl (OVL_CURRENT (fn),
2533 PUSH_LOCAL | PUSH_USING,
2534 false);
2536 else
2537 push_local_binding (name, newval, PUSH_USING);
2539 if (newtype)
2541 push_local_binding (name, newtype, PUSH_USING);
2542 set_identifier_type_value (name, newtype);
2545 /* Emit debug info. */
2546 if (!processing_template_decl)
2547 cp_emit_debug_info_for_using (orig_decl, current_scope());
2550 /* Returns true if ROOT (a namespace, class, or function) encloses
2551 CHILD. CHILD may be either a class type or a namespace. */
2553 bool
2554 is_ancestor (tree root, tree child)
2556 gcc_assert ((TREE_CODE (root) == NAMESPACE_DECL
2557 || TREE_CODE (root) == FUNCTION_DECL
2558 || CLASS_TYPE_P (root)));
2559 gcc_assert ((TREE_CODE (child) == NAMESPACE_DECL
2560 || CLASS_TYPE_P (child)));
2562 /* The global namespace encloses everything. */
2563 if (root == global_namespace)
2564 return true;
2566 while (true)
2568 /* If we've run out of scopes, stop. */
2569 if (!child)
2570 return false;
2571 /* If we've reached the ROOT, it encloses CHILD. */
2572 if (root == child)
2573 return true;
2574 /* Go out one level. */
2575 if (TYPE_P (child))
2576 child = TYPE_NAME (child);
2577 child = DECL_CONTEXT (child);
2581 /* Enter the class or namespace scope indicated by T suitable for name
2582 lookup. T can be arbitrary scope, not necessary nested inside the
2583 current scope. Returns a non-null scope to pop iff pop_scope
2584 should be called later to exit this scope. */
2586 tree
2587 push_scope (tree t)
2589 if (TREE_CODE (t) == NAMESPACE_DECL)
2590 push_decl_namespace (t);
2591 else if (CLASS_TYPE_P (t))
2593 if (!at_class_scope_p ()
2594 || !same_type_p (current_class_type, t))
2595 push_nested_class (t);
2596 else
2597 /* T is the same as the current scope. There is therefore no
2598 need to re-enter the scope. Since we are not actually
2599 pushing a new scope, our caller should not call
2600 pop_scope. */
2601 t = NULL_TREE;
2604 return t;
2607 /* Leave scope pushed by push_scope. */
2609 void
2610 pop_scope (tree t)
2612 if (t == NULL_TREE)
2613 return;
2614 if (TREE_CODE (t) == NAMESPACE_DECL)
2615 pop_decl_namespace ();
2616 else if CLASS_TYPE_P (t)
2617 pop_nested_class ();
2620 /* Subroutine of push_inner_scope. */
2622 static void
2623 push_inner_scope_r (tree outer, tree inner)
2625 tree prev;
2627 if (outer == inner
2628 || (TREE_CODE (inner) != NAMESPACE_DECL && !CLASS_TYPE_P (inner)))
2629 return;
2631 prev = CP_DECL_CONTEXT (TREE_CODE (inner) == NAMESPACE_DECL ? inner : TYPE_NAME (inner));
2632 if (outer != prev)
2633 push_inner_scope_r (outer, prev);
2634 if (TREE_CODE (inner) == NAMESPACE_DECL)
2636 struct cp_binding_level *save_template_parm = 0;
2637 /* Temporary take out template parameter scopes. They are saved
2638 in reversed order in save_template_parm. */
2639 while (current_binding_level->kind == sk_template_parms)
2641 struct cp_binding_level *b = current_binding_level;
2642 current_binding_level = b->level_chain;
2643 b->level_chain = save_template_parm;
2644 save_template_parm = b;
2647 resume_scope (NAMESPACE_LEVEL (inner));
2648 current_namespace = inner;
2650 /* Restore template parameter scopes. */
2651 while (save_template_parm)
2653 struct cp_binding_level *b = save_template_parm;
2654 save_template_parm = b->level_chain;
2655 b->level_chain = current_binding_level;
2656 current_binding_level = b;
2659 else
2660 pushclass (inner);
2663 /* Enter the scope INNER from current scope. INNER must be a scope
2664 nested inside current scope. This works with both name lookup and
2665 pushing name into scope. In case a template parameter scope is present,
2666 namespace is pushed under the template parameter scope according to
2667 name lookup rule in 14.6.1/6.
2669 Return the former current scope suitable for pop_inner_scope. */
2671 tree
2672 push_inner_scope (tree inner)
2674 tree outer = current_scope ();
2675 if (!outer)
2676 outer = current_namespace;
2678 push_inner_scope_r (outer, inner);
2679 return outer;
2682 /* Exit the current scope INNER back to scope OUTER. */
2684 void
2685 pop_inner_scope (tree outer, tree inner)
2687 if (outer == inner
2688 || (TREE_CODE (inner) != NAMESPACE_DECL && !CLASS_TYPE_P (inner)))
2689 return;
2691 while (outer != inner)
2693 if (TREE_CODE (inner) == NAMESPACE_DECL)
2695 struct cp_binding_level *save_template_parm = 0;
2696 /* Temporary take out template parameter scopes. They are saved
2697 in reversed order in save_template_parm. */
2698 while (current_binding_level->kind == sk_template_parms)
2700 struct cp_binding_level *b = current_binding_level;
2701 current_binding_level = b->level_chain;
2702 b->level_chain = save_template_parm;
2703 save_template_parm = b;
2706 pop_namespace ();
2708 /* Restore template parameter scopes. */
2709 while (save_template_parm)
2711 struct cp_binding_level *b = save_template_parm;
2712 save_template_parm = b->level_chain;
2713 b->level_chain = current_binding_level;
2714 current_binding_level = b;
2717 else
2718 popclass ();
2720 inner = CP_DECL_CONTEXT (TREE_CODE (inner) == NAMESPACE_DECL ? inner : TYPE_NAME (inner));
2724 /* Do a pushlevel for class declarations. */
2726 void
2727 pushlevel_class (void)
2729 class_binding_level = begin_scope (sk_class, current_class_type);
2732 /* ...and a poplevel for class declarations. */
2734 void
2735 poplevel_class (void)
2737 struct cp_binding_level *level = class_binding_level;
2738 cp_class_binding *cb;
2739 size_t i;
2740 tree shadowed;
2742 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
2743 gcc_assert (level != 0);
2745 /* If we're leaving a toplevel class, cache its binding level. */
2746 if (current_class_depth == 1)
2747 previous_class_level = level;
2748 for (shadowed = level->type_shadowed;
2749 shadowed;
2750 shadowed = TREE_CHAIN (shadowed))
2751 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (shadowed), TREE_VALUE (shadowed));
2753 /* Remove the bindings for all of the class-level declarations. */
2754 if (level->class_shadowed)
2756 FOR_EACH_VEC_ELT (cp_class_binding, level->class_shadowed, i, cb)
2757 IDENTIFIER_BINDING (cb->identifier) = cb->base.previous;
2758 ggc_free (level->class_shadowed);
2759 level->class_shadowed = NULL;
2762 /* Now, pop out of the binding level which we created up in the
2763 `pushlevel_class' routine. */
2764 gcc_assert (current_binding_level == level);
2765 leave_scope ();
2766 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
2769 /* Set INHERITED_VALUE_BINDING_P on BINDING to true or false, as
2770 appropriate. DECL is the value to which a name has just been
2771 bound. CLASS_TYPE is the class in which the lookup occurred. */
2773 static void
2774 set_inherited_value_binding_p (cxx_binding *binding, tree decl,
2775 tree class_type)
2777 if (binding->value == decl && TREE_CODE (decl) != TREE_LIST)
2779 tree context;
2781 if (TREE_CODE (decl) == OVERLOAD)
2782 context = CP_DECL_CONTEXT (OVL_CURRENT (decl));
2783 else
2785 gcc_assert (DECL_P (decl));
2786 context = context_for_name_lookup (decl);
2789 if (is_properly_derived_from (class_type, context))
2790 INHERITED_VALUE_BINDING_P (binding) = 1;
2791 else
2792 INHERITED_VALUE_BINDING_P (binding) = 0;
2794 else if (binding->value == decl)
2795 /* We only encounter a TREE_LIST when there is an ambiguity in the
2796 base classes. Such an ambiguity can be overridden by a
2797 definition in this class. */
2798 INHERITED_VALUE_BINDING_P (binding) = 1;
2799 else
2800 INHERITED_VALUE_BINDING_P (binding) = 0;
2803 /* Make the declaration of X appear in CLASS scope. */
2805 bool
2806 pushdecl_class_level (tree x)
2808 tree name;
2809 bool is_valid = true;
2810 bool subtime;
2812 /* Do nothing if we're adding to an outer lambda closure type,
2813 outer_binding will add it later if it's needed. */
2814 if (current_class_type != class_binding_level->this_entity)
2815 return true;
2817 subtime = timevar_cond_start (TV_NAME_LOOKUP);
2818 /* Get the name of X. */
2819 if (TREE_CODE (x) == OVERLOAD)
2820 name = DECL_NAME (get_first_fn (x));
2821 else
2822 name = DECL_NAME (x);
2824 if (name)
2826 is_valid = push_class_level_binding (name, x);
2827 if (TREE_CODE (x) == TYPE_DECL)
2828 set_identifier_type_value (name, x);
2830 else if (ANON_AGGR_TYPE_P (TREE_TYPE (x)))
2832 /* If X is an anonymous aggregate, all of its members are
2833 treated as if they were members of the class containing the
2834 aggregate, for naming purposes. */
2835 tree f;
2837 for (f = TYPE_FIELDS (TREE_TYPE (x)); f; f = DECL_CHAIN (f))
2839 location_t save_location = input_location;
2840 input_location = DECL_SOURCE_LOCATION (f);
2841 if (!pushdecl_class_level (f))
2842 is_valid = false;
2843 input_location = save_location;
2846 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
2847 return is_valid;
2850 /* Return the BINDING (if any) for NAME in SCOPE, which is a class
2851 scope. If the value returned is non-NULL, and the PREVIOUS field
2852 is not set, callers must set the PREVIOUS field explicitly. */
2854 static cxx_binding *
2855 get_class_binding (tree name, cxx_scope *scope)
2857 tree class_type;
2858 tree type_binding;
2859 tree value_binding;
2860 cxx_binding *binding;
2862 class_type = scope->this_entity;
2864 /* Get the type binding. */
2865 type_binding = lookup_member (class_type, name,
2866 /*protect=*/2, /*want_type=*/true);
2867 /* Get the value binding. */
2868 value_binding = lookup_member (class_type, name,
2869 /*protect=*/2, /*want_type=*/false);
2871 if (value_binding
2872 && (TREE_CODE (value_binding) == TYPE_DECL
2873 || DECL_CLASS_TEMPLATE_P (value_binding)
2874 || (TREE_CODE (value_binding) == TREE_LIST
2875 && TREE_TYPE (value_binding) == error_mark_node
2876 && (TREE_CODE (TREE_VALUE (value_binding))
2877 == TYPE_DECL))))
2878 /* We found a type binding, even when looking for a non-type
2879 binding. This means that we already processed this binding
2880 above. */
2882 else if (value_binding)
2884 if (TREE_CODE (value_binding) == TREE_LIST
2885 && TREE_TYPE (value_binding) == error_mark_node)
2886 /* NAME is ambiguous. */
2888 else if (BASELINK_P (value_binding))
2889 /* NAME is some overloaded functions. */
2890 value_binding = BASELINK_FUNCTIONS (value_binding);
2893 /* If we found either a type binding or a value binding, create a
2894 new binding object. */
2895 if (type_binding || value_binding)
2897 binding = new_class_binding (name,
2898 value_binding,
2899 type_binding,
2900 scope);
2901 /* This is a class-scope binding, not a block-scope binding. */
2902 LOCAL_BINDING_P (binding) = 0;
2903 set_inherited_value_binding_p (binding, value_binding, class_type);
2905 else
2906 binding = NULL;
2908 return binding;
2911 /* Make the declaration(s) of X appear in CLASS scope under the name
2912 NAME. Returns true if the binding is valid. */
2914 static bool
2915 push_class_level_binding_1 (tree name, tree x)
2917 cxx_binding *binding;
2918 tree decl = x;
2919 bool ok;
2921 /* The class_binding_level will be NULL if x is a template
2922 parameter name in a member template. */
2923 if (!class_binding_level)
2924 return true;
2926 if (name == error_mark_node)
2927 return false;
2929 /* Check for invalid member names. */
2930 gcc_assert (TYPE_BEING_DEFINED (current_class_type));
2931 /* Check that we're pushing into the right binding level. */
2932 gcc_assert (current_class_type == class_binding_level->this_entity);
2934 /* We could have been passed a tree list if this is an ambiguous
2935 declaration. If so, pull the declaration out because
2936 check_template_shadow will not handle a TREE_LIST. */
2937 if (TREE_CODE (decl) == TREE_LIST
2938 && TREE_TYPE (decl) == error_mark_node)
2939 decl = TREE_VALUE (decl);
2941 if (!check_template_shadow (decl))
2942 return false;
2944 /* [class.mem]
2946 If T is the name of a class, then each of the following shall
2947 have a name different from T:
2949 -- every static data member of class T;
2951 -- every member of class T that is itself a type;
2953 -- every enumerator of every member of class T that is an
2954 enumerated type;
2956 -- every member of every anonymous union that is a member of
2957 class T.
2959 (Non-static data members were also forbidden to have the same
2960 name as T until TC1.) */
2961 if ((TREE_CODE (x) == VAR_DECL
2962 || TREE_CODE (x) == CONST_DECL
2963 || (TREE_CODE (x) == TYPE_DECL
2964 && !DECL_SELF_REFERENCE_P (x))
2965 /* A data member of an anonymous union. */
2966 || (TREE_CODE (x) == FIELD_DECL
2967 && DECL_CONTEXT (x) != current_class_type))
2968 && DECL_NAME (x) == constructor_name (current_class_type))
2970 tree scope = context_for_name_lookup (x);
2971 if (TYPE_P (scope) && same_type_p (scope, current_class_type))
2973 error ("%qD has the same name as the class in which it is "
2974 "declared",
2976 return false;
2980 /* Get the current binding for NAME in this class, if any. */
2981 binding = IDENTIFIER_BINDING (name);
2982 if (!binding || binding->scope != class_binding_level)
2984 binding = get_class_binding (name, class_binding_level);
2985 /* If a new binding was created, put it at the front of the
2986 IDENTIFIER_BINDING list. */
2987 if (binding)
2989 binding->previous = IDENTIFIER_BINDING (name);
2990 IDENTIFIER_BINDING (name) = binding;
2994 /* If there is already a binding, then we may need to update the
2995 current value. */
2996 if (binding && binding->value)
2998 tree bval = binding->value;
2999 tree old_decl = NULL_TREE;
3001 if (INHERITED_VALUE_BINDING_P (binding))
3003 /* If the old binding was from a base class, and was for a
3004 tag name, slide it over to make room for the new binding.
3005 The old binding is still visible if explicitly qualified
3006 with a class-key. */
3007 if (TREE_CODE (bval) == TYPE_DECL && DECL_ARTIFICIAL (bval)
3008 && !(TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x)))
3010 old_decl = binding->type;
3011 binding->type = bval;
3012 binding->value = NULL_TREE;
3013 INHERITED_VALUE_BINDING_P (binding) = 0;
3015 else
3017 old_decl = bval;
3018 /* Any inherited type declaration is hidden by the type
3019 declaration in the derived class. */
3020 if (TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x))
3021 binding->type = NULL_TREE;
3024 else if (TREE_CODE (x) == OVERLOAD && is_overloaded_fn (bval))
3025 old_decl = bval;
3026 else if (TREE_CODE (x) == USING_DECL && TREE_CODE (bval) == USING_DECL)
3027 return true;
3028 else if (TREE_CODE (x) == USING_DECL && is_overloaded_fn (bval))
3029 old_decl = bval;
3030 else if (TREE_CODE (bval) == USING_DECL && is_overloaded_fn (x))
3031 return true;
3033 if (old_decl && binding->scope == class_binding_level)
3035 binding->value = x;
3036 /* It is always safe to clear INHERITED_VALUE_BINDING_P
3037 here. This function is only used to register bindings
3038 from with the class definition itself. */
3039 INHERITED_VALUE_BINDING_P (binding) = 0;
3040 return true;
3044 /* Note that we declared this value so that we can issue an error if
3045 this is an invalid redeclaration of a name already used for some
3046 other purpose. */
3047 note_name_declared_in_class (name, decl);
3049 /* If we didn't replace an existing binding, put the binding on the
3050 stack of bindings for the identifier, and update the shadowed
3051 list. */
3052 if (binding && binding->scope == class_binding_level)
3053 /* Supplement the existing binding. */
3054 ok = supplement_binding (binding, decl);
3055 else
3057 /* Create a new binding. */
3058 push_binding (name, decl, class_binding_level);
3059 ok = true;
3062 return ok;
3065 /* Wrapper for push_class_level_binding_1. */
3067 bool
3068 push_class_level_binding (tree name, tree x)
3070 bool ret;
3071 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3072 ret = push_class_level_binding_1 (name, x);
3073 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3074 return ret;
3077 /* Process "using SCOPE::NAME" in a class scope. Return the
3078 USING_DECL created. */
3080 tree
3081 do_class_using_decl (tree scope, tree name)
3083 /* The USING_DECL returned by this function. */
3084 tree value;
3085 /* The declaration (or declarations) name by this using
3086 declaration. NULL if we are in a template and cannot figure out
3087 what has been named. */
3088 tree decl;
3089 /* True if SCOPE is a dependent type. */
3090 bool scope_dependent_p;
3091 /* True if SCOPE::NAME is dependent. */
3092 bool name_dependent_p;
3093 /* True if any of the bases of CURRENT_CLASS_TYPE are dependent. */
3094 bool bases_dependent_p;
3095 tree binfo;
3096 tree base_binfo;
3097 int i;
3099 if (name == error_mark_node)
3100 return NULL_TREE;
3102 if (!scope || !TYPE_P (scope))
3104 error ("using-declaration for non-member at class scope");
3105 return NULL_TREE;
3108 /* Make sure the name is not invalid */
3109 if (TREE_CODE (name) == BIT_NOT_EXPR)
3111 error ("%<%T::%D%> names destructor", scope, name);
3112 return NULL_TREE;
3114 if (MAYBE_CLASS_TYPE_P (scope) && constructor_name_p (name, scope))
3116 error ("%<%T::%D%> names constructor", scope, name);
3117 return NULL_TREE;
3119 if (constructor_name_p (name, current_class_type))
3121 error ("%<%T::%D%> names constructor in %qT",
3122 scope, name, current_class_type);
3123 return NULL_TREE;
3126 scope_dependent_p = dependent_type_p (scope);
3127 name_dependent_p = (scope_dependent_p
3128 || (IDENTIFIER_TYPENAME_P (name)
3129 && dependent_type_p (TREE_TYPE (name))));
3131 bases_dependent_p = false;
3132 if (processing_template_decl)
3133 for (binfo = TYPE_BINFO (current_class_type), i = 0;
3134 BINFO_BASE_ITERATE (binfo, i, base_binfo);
3135 i++)
3136 if (dependent_type_p (TREE_TYPE (base_binfo)))
3138 bases_dependent_p = true;
3139 break;
3142 decl = NULL_TREE;
3144 /* From [namespace.udecl]:
3146 A using-declaration used as a member-declaration shall refer to a
3147 member of a base class of the class being defined.
3149 In general, we cannot check this constraint in a template because
3150 we do not know the entire set of base classes of the current
3151 class type. However, if all of the base classes are
3152 non-dependent, then we can avoid delaying the check until
3153 instantiation. */
3154 if (!scope_dependent_p)
3156 base_kind b_kind;
3157 binfo = lookup_base (current_class_type, scope, ba_any, &b_kind);
3158 if (b_kind < bk_proper_base)
3160 if (!bases_dependent_p)
3162 error_not_base_type (scope, current_class_type);
3163 return NULL_TREE;
3166 else if (!name_dependent_p)
3168 decl = lookup_member (binfo, name, 0, false);
3169 if (!decl)
3171 error ("no members matching %<%T::%D%> in %q#T", scope, name,
3172 scope);
3173 return NULL_TREE;
3175 /* The binfo from which the functions came does not matter. */
3176 if (BASELINK_P (decl))
3177 decl = BASELINK_FUNCTIONS (decl);
3181 value = build_lang_decl (USING_DECL, name, NULL_TREE);
3182 USING_DECL_DECLS (value) = decl;
3183 USING_DECL_SCOPE (value) = scope;
3184 DECL_DEPENDENT_P (value) = !decl;
3186 return value;
3190 /* Return the binding value for name in scope. */
3193 static tree
3194 namespace_binding_1 (tree name, tree scope)
3196 cxx_binding *binding;
3198 if (SCOPE_FILE_SCOPE_P (scope))
3199 scope = global_namespace;
3200 else
3201 /* Unnecessary for the global namespace because it can't be an alias. */
3202 scope = ORIGINAL_NAMESPACE (scope);
3204 binding = cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (scope), name);
3206 return binding ? binding->value : NULL_TREE;
3209 tree
3210 namespace_binding (tree name, tree scope)
3212 tree ret;
3213 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3214 ret = namespace_binding_1 (name, scope);
3215 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3216 return ret;
3219 /* Set the binding value for name in scope. */
3221 static void
3222 set_namespace_binding_1 (tree name, tree scope, tree val)
3224 cxx_binding *b;
3226 if (scope == NULL_TREE)
3227 scope = global_namespace;
3228 b = binding_for_name (NAMESPACE_LEVEL (scope), name);
3229 if (!b->value || TREE_CODE (val) == OVERLOAD || val == error_mark_node)
3230 b->value = val;
3231 else
3232 supplement_binding (b, val);
3235 /* Wrapper for set_namespace_binding_1. */
3237 void
3238 set_namespace_binding (tree name, tree scope, tree val)
3240 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3241 set_namespace_binding_1 (name, scope, val);
3242 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3245 /* Set the context of a declaration to scope. Complain if we are not
3246 outside scope. */
3248 void
3249 set_decl_namespace (tree decl, tree scope, bool friendp)
3251 tree old;
3253 /* Get rid of namespace aliases. */
3254 scope = ORIGINAL_NAMESPACE (scope);
3256 /* It is ok for friends to be qualified in parallel space. */
3257 if (!friendp && !is_ancestor (current_namespace, scope))
3258 error ("declaration of %qD not in a namespace surrounding %qD",
3259 decl, scope);
3260 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
3262 /* Writing "int N::i" to declare a variable within "N" is invalid. */
3263 if (scope == current_namespace)
3265 if (at_namespace_scope_p ())
3266 error ("explicit qualification in declaration of %qD",
3267 decl);
3268 return;
3271 /* See whether this has been declared in the namespace. */
3272 old = lookup_qualified_name (scope, DECL_NAME (decl), false, true);
3273 if (old == error_mark_node)
3274 /* No old declaration at all. */
3275 goto complain;
3276 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
3277 if (TREE_CODE (old) == TREE_LIST)
3279 error ("reference to %qD is ambiguous", decl);
3280 print_candidates (old);
3281 return;
3283 if (!is_overloaded_fn (decl))
3285 /* We might have found OLD in an inline namespace inside SCOPE. */
3286 if (TREE_CODE (decl) == TREE_CODE (old))
3287 DECL_CONTEXT (decl) = DECL_CONTEXT (old);
3288 /* Don't compare non-function decls with decls_match here, since
3289 it can't check for the correct constness at this
3290 point. pushdecl will find those errors later. */
3291 return;
3293 /* Since decl is a function, old should contain a function decl. */
3294 if (!is_overloaded_fn (old))
3295 goto complain;
3296 /* A template can be explicitly specialized in any namespace. */
3297 if (processing_explicit_instantiation)
3298 return;
3299 if (processing_template_decl || processing_specialization)
3300 /* We have not yet called push_template_decl to turn a
3301 FUNCTION_DECL into a TEMPLATE_DECL, so the declarations won't
3302 match. But, we'll check later, when we construct the
3303 template. */
3304 return;
3305 /* Instantiations or specializations of templates may be declared as
3306 friends in any namespace. */
3307 if (friendp && DECL_USE_TEMPLATE (decl))
3308 return;
3309 if (is_overloaded_fn (old))
3311 tree found = NULL_TREE;
3312 tree elt = old;
3313 for (; elt; elt = OVL_NEXT (elt))
3315 tree ofn = OVL_CURRENT (elt);
3316 /* Adjust DECL_CONTEXT first so decls_match will return true
3317 if DECL will match a declaration in an inline namespace. */
3318 DECL_CONTEXT (decl) = DECL_CONTEXT (ofn);
3319 if (decls_match (decl, ofn))
3321 if (found && !decls_match (found, ofn))
3323 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
3324 error ("reference to %qD is ambiguous", decl);
3325 print_candidates (old);
3326 return;
3328 found = ofn;
3331 if (found)
3333 if (!is_associated_namespace (scope, CP_DECL_CONTEXT (found)))
3334 goto complain;
3335 DECL_CONTEXT (decl) = DECL_CONTEXT (found);
3336 return;
3339 else
3341 DECL_CONTEXT (decl) = DECL_CONTEXT (old);
3342 if (decls_match (decl, old))
3343 return;
3346 /* It didn't work, go back to the explicit scope. */
3347 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
3348 complain:
3349 error ("%qD should have been declared inside %qD", decl, scope);
3352 /* Return the namespace where the current declaration is declared. */
3354 tree
3355 current_decl_namespace (void)
3357 tree result;
3358 /* If we have been pushed into a different namespace, use it. */
3359 if (!VEC_empty (tree, decl_namespace_list))
3360 return VEC_last (tree, decl_namespace_list);
3362 if (current_class_type)
3363 result = decl_namespace_context (current_class_type);
3364 else if (current_function_decl)
3365 result = decl_namespace_context (current_function_decl);
3366 else
3367 result = current_namespace;
3368 return result;
3371 /* Process any ATTRIBUTES on a namespace definition. Currently only
3372 attribute visibility is meaningful, which is a property of the syntactic
3373 block rather than the namespace as a whole, so we don't touch the
3374 NAMESPACE_DECL at all. Returns true if attribute visibility is seen. */
3376 bool
3377 handle_namespace_attrs (tree ns, tree attributes)
3379 tree d;
3380 bool saw_vis = false;
3382 for (d = attributes; d; d = TREE_CHAIN (d))
3384 tree name = TREE_PURPOSE (d);
3385 tree args = TREE_VALUE (d);
3387 if (is_attribute_p ("visibility", name))
3389 tree x = args ? TREE_VALUE (args) : NULL_TREE;
3390 if (x == NULL_TREE || TREE_CODE (x) != STRING_CST || TREE_CHAIN (args))
3392 warning (OPT_Wattributes,
3393 "%qD attribute requires a single NTBS argument",
3394 name);
3395 continue;
3398 if (!TREE_PUBLIC (ns))
3399 warning (OPT_Wattributes,
3400 "%qD attribute is meaningless since members of the "
3401 "anonymous namespace get local symbols", name);
3403 push_visibility (TREE_STRING_POINTER (x), 1);
3404 saw_vis = true;
3406 else
3408 warning (OPT_Wattributes, "%qD attribute directive ignored",
3409 name);
3410 continue;
3414 return saw_vis;
3417 /* Push into the scope of the NAME namespace. If NAME is NULL_TREE, then we
3418 select a name that is unique to this compilation unit. */
3420 void
3421 push_namespace (tree name)
3423 tree d = NULL_TREE;
3424 int need_new = 1;
3425 int implicit_use = 0;
3426 bool anon = !name;
3428 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3430 /* We should not get here if the global_namespace is not yet constructed
3431 nor if NAME designates the global namespace: The global scope is
3432 constructed elsewhere. */
3433 gcc_assert (global_namespace != NULL && name != global_scope_name);
3435 if (anon)
3437 name = get_anonymous_namespace_name();
3438 d = IDENTIFIER_NAMESPACE_VALUE (name);
3439 if (d)
3440 /* Reopening anonymous namespace. */
3441 need_new = 0;
3442 implicit_use = 1;
3444 else
3446 /* Check whether this is an extended namespace definition. */
3447 d = IDENTIFIER_NAMESPACE_VALUE (name);
3448 if (d != NULL_TREE && TREE_CODE (d) == NAMESPACE_DECL)
3450 need_new = 0;
3451 if (DECL_NAMESPACE_ALIAS (d))
3453 error ("namespace alias %qD not allowed here, assuming %qD",
3454 d, DECL_NAMESPACE_ALIAS (d));
3455 d = DECL_NAMESPACE_ALIAS (d);
3460 if (need_new)
3462 /* Make a new namespace, binding the name to it. */
3463 d = build_lang_decl (NAMESPACE_DECL, name, void_type_node);
3464 DECL_CONTEXT (d) = FROB_CONTEXT (current_namespace);
3465 /* The name of this namespace is not visible to other translation
3466 units if it is an anonymous namespace or member thereof. */
3467 if (anon || decl_anon_ns_mem_p (current_namespace))
3468 TREE_PUBLIC (d) = 0;
3469 else
3470 TREE_PUBLIC (d) = 1;
3471 pushdecl (d);
3472 if (anon)
3474 /* Clear DECL_NAME for the benefit of debugging back ends. */
3475 SET_DECL_ASSEMBLER_NAME (d, name);
3476 DECL_NAME (d) = NULL_TREE;
3478 begin_scope (sk_namespace, d);
3480 else
3481 resume_scope (NAMESPACE_LEVEL (d));
3483 if (implicit_use)
3484 do_using_directive (d);
3485 /* Enter the name space. */
3486 current_namespace = d;
3488 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3491 /* Pop from the scope of the current namespace. */
3493 void
3494 pop_namespace (void)
3496 gcc_assert (current_namespace != global_namespace);
3497 current_namespace = CP_DECL_CONTEXT (current_namespace);
3498 /* The binding level is not popped, as it might be re-opened later. */
3499 leave_scope ();
3502 /* Push into the scope of the namespace NS, even if it is deeply
3503 nested within another namespace. */
3505 void
3506 push_nested_namespace (tree ns)
3508 if (ns == global_namespace)
3509 push_to_top_level ();
3510 else
3512 push_nested_namespace (CP_DECL_CONTEXT (ns));
3513 push_namespace (DECL_NAME (ns));
3517 /* Pop back from the scope of the namespace NS, which was previously
3518 entered with push_nested_namespace. */
3520 void
3521 pop_nested_namespace (tree ns)
3523 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3524 gcc_assert (current_namespace == ns);
3525 while (ns != global_namespace)
3527 pop_namespace ();
3528 ns = CP_DECL_CONTEXT (ns);
3531 pop_from_top_level ();
3532 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3535 /* Temporarily set the namespace for the current declaration. */
3537 void
3538 push_decl_namespace (tree decl)
3540 if (TREE_CODE (decl) != NAMESPACE_DECL)
3541 decl = decl_namespace_context (decl);
3542 VEC_safe_push (tree, gc, decl_namespace_list, ORIGINAL_NAMESPACE (decl));
3545 /* [namespace.memdef]/2 */
3547 void
3548 pop_decl_namespace (void)
3550 VEC_pop (tree, decl_namespace_list);
3553 /* Return the namespace that is the common ancestor
3554 of two given namespaces. */
3556 static tree
3557 namespace_ancestor_1 (tree ns1, tree ns2)
3559 tree nsr;
3560 if (is_ancestor (ns1, ns2))
3561 nsr = ns1;
3562 else
3563 nsr = namespace_ancestor_1 (CP_DECL_CONTEXT (ns1), ns2);
3564 return nsr;
3567 /* Wrapper for namespace_ancestor_1. */
3569 static tree
3570 namespace_ancestor (tree ns1, tree ns2)
3572 tree nsr;
3573 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3574 nsr = namespace_ancestor_1 (ns1, ns2);
3575 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3576 return nsr;
3579 /* Process a namespace-alias declaration. */
3581 void
3582 do_namespace_alias (tree alias, tree name_space)
3584 if (name_space == error_mark_node)
3585 return;
3587 gcc_assert (TREE_CODE (name_space) == NAMESPACE_DECL);
3589 name_space = ORIGINAL_NAMESPACE (name_space);
3591 /* Build the alias. */
3592 alias = build_lang_decl (NAMESPACE_DECL, alias, void_type_node);
3593 DECL_NAMESPACE_ALIAS (alias) = name_space;
3594 DECL_EXTERNAL (alias) = 1;
3595 DECL_CONTEXT (alias) = FROB_CONTEXT (current_scope ());
3596 pushdecl (alias);
3598 /* Emit debug info for namespace alias. */
3599 if (!building_stmt_tree ())
3600 (*debug_hooks->global_decl) (alias);
3603 /* Like pushdecl, only it places X in the current namespace,
3604 if appropriate. */
3606 tree
3607 pushdecl_namespace_level (tree x, bool is_friend)
3609 struct cp_binding_level *b = current_binding_level;
3610 tree t;
3612 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3613 t = pushdecl_with_scope (x, NAMESPACE_LEVEL (current_namespace), is_friend);
3615 /* Now, the type_shadowed stack may screw us. Munge it so it does
3616 what we want. */
3617 if (TREE_CODE (t) == TYPE_DECL)
3619 tree name = DECL_NAME (t);
3620 tree newval;
3621 tree *ptr = (tree *)0;
3622 for (; !global_scope_p (b); b = b->level_chain)
3624 tree shadowed = b->type_shadowed;
3625 for (; shadowed; shadowed = TREE_CHAIN (shadowed))
3626 if (TREE_PURPOSE (shadowed) == name)
3628 ptr = &TREE_VALUE (shadowed);
3629 /* Can't break out of the loop here because sometimes
3630 a binding level will have duplicate bindings for
3631 PT names. It's gross, but I haven't time to fix it. */
3634 newval = TREE_TYPE (t);
3635 if (ptr == (tree *)0)
3637 /* @@ This shouldn't be needed. My test case "zstring.cc" trips
3638 up here if this is changed to an assertion. --KR */
3639 SET_IDENTIFIER_TYPE_VALUE (name, t);
3641 else
3643 *ptr = newval;
3646 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3647 return t;
3650 /* Insert USED into the using list of USER. Set INDIRECT_flag if this
3651 directive is not directly from the source. Also find the common
3652 ancestor and let our users know about the new namespace */
3654 static void
3655 add_using_namespace_1 (tree user, tree used, bool indirect)
3657 tree t;
3658 /* Using oneself is a no-op. */
3659 if (user == used)
3660 return;
3661 gcc_assert (TREE_CODE (user) == NAMESPACE_DECL);
3662 gcc_assert (TREE_CODE (used) == NAMESPACE_DECL);
3663 /* Check if we already have this. */
3664 t = purpose_member (used, DECL_NAMESPACE_USING (user));
3665 if (t != NULL_TREE)
3667 if (!indirect)
3668 /* Promote to direct usage. */
3669 TREE_INDIRECT_USING (t) = 0;
3670 return;
3673 /* Add used to the user's using list. */
3674 DECL_NAMESPACE_USING (user)
3675 = tree_cons (used, namespace_ancestor (user, used),
3676 DECL_NAMESPACE_USING (user));
3678 TREE_INDIRECT_USING (DECL_NAMESPACE_USING (user)) = indirect;
3680 /* Add user to the used's users list. */
3681 DECL_NAMESPACE_USERS (used)
3682 = tree_cons (user, 0, DECL_NAMESPACE_USERS (used));
3684 /* Recursively add all namespaces used. */
3685 for (t = DECL_NAMESPACE_USING (used); t; t = TREE_CHAIN (t))
3686 /* indirect usage */
3687 add_using_namespace_1 (user, TREE_PURPOSE (t), 1);
3689 /* Tell everyone using us about the new used namespaces. */
3690 for (t = DECL_NAMESPACE_USERS (user); t; t = TREE_CHAIN (t))
3691 add_using_namespace_1 (TREE_PURPOSE (t), used, 1);
3694 /* Wrapper for add_using_namespace_1. */
3696 static void
3697 add_using_namespace (tree user, tree used, bool indirect)
3699 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3700 add_using_namespace_1 (user, used, indirect);
3701 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3704 /* Process a using-declaration not appearing in class or local scope. */
3706 void
3707 do_toplevel_using_decl (tree decl, tree scope, tree name)
3709 tree oldval, oldtype, newval, newtype;
3710 tree orig_decl = decl;
3711 cxx_binding *binding;
3713 decl = validate_nonmember_using_decl (decl, scope, name);
3714 if (decl == NULL_TREE)
3715 return;
3717 binding = binding_for_name (NAMESPACE_LEVEL (current_namespace), name);
3719 oldval = binding->value;
3720 oldtype = binding->type;
3722 do_nonmember_using_decl (scope, name, oldval, oldtype, &newval, &newtype);
3724 /* Emit debug info. */
3725 if (!processing_template_decl)
3726 cp_emit_debug_info_for_using (orig_decl, current_namespace);
3728 /* Copy declarations found. */
3729 if (newval)
3730 binding->value = newval;
3731 if (newtype)
3732 binding->type = newtype;
3735 /* Process a using-directive. */
3737 void
3738 do_using_directive (tree name_space)
3740 tree context = NULL_TREE;
3742 if (name_space == error_mark_node)
3743 return;
3745 gcc_assert (TREE_CODE (name_space) == NAMESPACE_DECL);
3747 if (building_stmt_tree ())
3748 add_stmt (build_stmt (input_location, USING_STMT, name_space));
3749 name_space = ORIGINAL_NAMESPACE (name_space);
3751 if (!toplevel_bindings_p ())
3753 push_using_directive (name_space);
3755 else
3757 /* direct usage */
3758 add_using_namespace (current_namespace, name_space, 0);
3759 if (current_namespace != global_namespace)
3760 context = current_namespace;
3762 /* Emit debugging info. */
3763 if (!processing_template_decl)
3764 (*debug_hooks->imported_module_or_decl) (name_space, NULL_TREE,
3765 context, false);
3769 /* Deal with a using-directive seen by the parser. Currently we only
3770 handle attributes here, since they cannot appear inside a template. */
3772 void
3773 parse_using_directive (tree name_space, tree attribs)
3775 tree a;
3777 do_using_directive (name_space);
3779 for (a = attribs; a; a = TREE_CHAIN (a))
3781 tree name = TREE_PURPOSE (a);
3782 if (is_attribute_p ("strong", name))
3784 if (!toplevel_bindings_p ())
3785 error ("strong using only meaningful at namespace scope");
3786 else if (name_space != error_mark_node)
3788 if (!is_ancestor (current_namespace, name_space))
3789 error ("current namespace %qD does not enclose strongly used namespace %qD",
3790 current_namespace, name_space);
3791 DECL_NAMESPACE_ASSOCIATIONS (name_space)
3792 = tree_cons (current_namespace, 0,
3793 DECL_NAMESPACE_ASSOCIATIONS (name_space));
3796 else
3797 warning (OPT_Wattributes, "%qD attribute directive ignored", name);
3801 /* Like pushdecl, only it places X in the global scope if appropriate.
3802 Calls cp_finish_decl to register the variable, initializing it with
3803 *INIT, if INIT is non-NULL. */
3805 static tree
3806 pushdecl_top_level_1 (tree x, tree *init, bool is_friend)
3808 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3809 push_to_top_level ();
3810 x = pushdecl_namespace_level (x, is_friend);
3811 if (init)
3812 cp_finish_decl (x, *init, false, NULL_TREE, 0);
3813 pop_from_top_level ();
3814 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3815 return x;
3818 /* Like pushdecl, only it places X in the global scope if appropriate. */
3820 tree
3821 pushdecl_top_level (tree x)
3823 return pushdecl_top_level_1 (x, NULL, false);
3826 /* Like pushdecl_top_level, but adding the IS_FRIEND parameter. */
3828 tree
3829 pushdecl_top_level_maybe_friend (tree x, bool is_friend)
3831 return pushdecl_top_level_1 (x, NULL, is_friend);
3834 /* Like pushdecl, only it places X in the global scope if
3835 appropriate. Calls cp_finish_decl to register the variable,
3836 initializing it with INIT. */
3838 tree
3839 pushdecl_top_level_and_finish (tree x, tree init)
3841 return pushdecl_top_level_1 (x, &init, false);
3844 /* Combines two sets of overloaded functions into an OVERLOAD chain, removing
3845 duplicates. The first list becomes the tail of the result.
3847 The algorithm is O(n^2). We could get this down to O(n log n) by
3848 doing a sort on the addresses of the functions, if that becomes
3849 necessary. */
3851 static tree
3852 merge_functions (tree s1, tree s2)
3854 for (; s2; s2 = OVL_NEXT (s2))
3856 tree fn2 = OVL_CURRENT (s2);
3857 tree fns1;
3859 for (fns1 = s1; fns1; fns1 = OVL_NEXT (fns1))
3861 tree fn1 = OVL_CURRENT (fns1);
3863 /* If the function from S2 is already in S1, there is no
3864 need to add it again. For `extern "C"' functions, we
3865 might have two FUNCTION_DECLs for the same function, in
3866 different namespaces, but let's leave them in in case
3867 they have different default arguments. */
3868 if (fn1 == fn2)
3869 break;
3872 /* If we exhausted all of the functions in S1, FN2 is new. */
3873 if (!fns1)
3874 s1 = build_overload (fn2, s1);
3876 return s1;
3879 /* Returns TRUE iff OLD and NEW are the same entity.
3881 3 [basic]/3: An entity is a value, object, reference, function,
3882 enumerator, type, class member, template, template specialization,
3883 namespace, parameter pack, or this.
3885 7.3.4 [namespace.udir]/4: If name lookup finds a declaration for a name
3886 in two different namespaces, and the declarations do not declare the
3887 same entity and do not declare functions, the use of the name is
3888 ill-formed. */
3890 static bool
3891 same_entity_p (tree one, tree two)
3893 if (one == two)
3894 return true;
3895 if (!one || !two)
3896 return false;
3897 if (TREE_CODE (one) == TYPE_DECL
3898 && TREE_CODE (two) == TYPE_DECL
3899 && same_type_p (TREE_TYPE (one), TREE_TYPE (two)))
3900 return true;
3901 return false;
3904 /* This should return an error not all definitions define functions.
3905 It is not an error if we find two functions with exactly the
3906 same signature, only if these are selected in overload resolution.
3907 old is the current set of bindings, new_binding the freshly-found binding.
3908 XXX Do we want to give *all* candidates in case of ambiguity?
3909 XXX In what way should I treat extern declarations?
3910 XXX I don't want to repeat the entire duplicate_decls here */
3912 static void
3913 ambiguous_decl (struct scope_binding *old, cxx_binding *new_binding, int flags)
3915 tree val, type;
3916 gcc_assert (old != NULL);
3918 /* Copy the type. */
3919 type = new_binding->type;
3920 if (LOOKUP_NAMESPACES_ONLY (flags)
3921 || (type && hidden_name_p (type) && !(flags & LOOKUP_HIDDEN)))
3922 type = NULL_TREE;
3924 /* Copy the value. */
3925 val = new_binding->value;
3926 if (val)
3928 if (hidden_name_p (val) && !(flags & LOOKUP_HIDDEN))
3929 val = NULL_TREE;
3930 else
3931 switch (TREE_CODE (val))
3933 case TEMPLATE_DECL:
3934 /* If we expect types or namespaces, and not templates,
3935 or this is not a template class. */
3936 if ((LOOKUP_QUALIFIERS_ONLY (flags)
3937 && !DECL_CLASS_TEMPLATE_P (val)))
3938 val = NULL_TREE;
3939 break;
3940 case TYPE_DECL:
3941 if (LOOKUP_NAMESPACES_ONLY (flags)
3942 || (type && (flags & LOOKUP_PREFER_TYPES)))
3943 val = NULL_TREE;
3944 break;
3945 case NAMESPACE_DECL:
3946 if (LOOKUP_TYPES_ONLY (flags))
3947 val = NULL_TREE;
3948 break;
3949 case FUNCTION_DECL:
3950 /* Ignore built-in functions that are still anticipated. */
3951 if (LOOKUP_QUALIFIERS_ONLY (flags))
3952 val = NULL_TREE;
3953 break;
3954 default:
3955 if (LOOKUP_QUALIFIERS_ONLY (flags))
3956 val = NULL_TREE;
3960 /* If val is hidden, shift down any class or enumeration name. */
3961 if (!val)
3963 val = type;
3964 type = NULL_TREE;
3967 if (!old->value)
3968 old->value = val;
3969 else if (val && !same_entity_p (val, old->value))
3971 if (is_overloaded_fn (old->value) && is_overloaded_fn (val))
3972 old->value = merge_functions (old->value, val);
3973 else
3975 old->value = tree_cons (NULL_TREE, old->value,
3976 build_tree_list (NULL_TREE, val));
3977 TREE_TYPE (old->value) = error_mark_node;
3981 if (!old->type)
3982 old->type = type;
3983 else if (type && old->type != type)
3985 old->type = tree_cons (NULL_TREE, old->type,
3986 build_tree_list (NULL_TREE, type));
3987 TREE_TYPE (old->type) = error_mark_node;
3991 /* Return the declarations that are members of the namespace NS. */
3993 tree
3994 cp_namespace_decls (tree ns)
3996 return NAMESPACE_LEVEL (ns)->names;
3999 /* Combine prefer_type and namespaces_only into flags. */
4001 static int
4002 lookup_flags (int prefer_type, int namespaces_only)
4004 if (namespaces_only)
4005 return LOOKUP_PREFER_NAMESPACES;
4006 if (prefer_type > 1)
4007 return LOOKUP_PREFER_TYPES;
4008 if (prefer_type > 0)
4009 return LOOKUP_PREFER_BOTH;
4010 return 0;
4013 /* Given a lookup that returned VAL, use FLAGS to decide if we want to
4014 ignore it or not. Subroutine of lookup_name_real and
4015 lookup_type_scope. */
4017 static bool
4018 qualify_lookup (tree val, int flags)
4020 if (val == NULL_TREE)
4021 return false;
4022 if ((flags & LOOKUP_PREFER_NAMESPACES) && TREE_CODE (val) == NAMESPACE_DECL)
4023 return true;
4024 if ((flags & LOOKUP_PREFER_TYPES)
4025 && (TREE_CODE (val) == TYPE_DECL || TREE_CODE (val) == TEMPLATE_DECL))
4026 return true;
4027 if (flags & (LOOKUP_PREFER_NAMESPACES | LOOKUP_PREFER_TYPES))
4028 return false;
4029 /* In unevaluated context, look past normal capture fields. */
4030 if (cp_unevaluated_operand && TREE_CODE (val) == FIELD_DECL
4031 && DECL_NORMAL_CAPTURE_P (val))
4032 return false;
4033 /* None of the lookups that use qualify_lookup want the op() from the
4034 lambda; they want the one from the enclosing class. */
4035 if (TREE_CODE (val) == FUNCTION_DECL && LAMBDA_FUNCTION_P (val))
4036 return false;
4037 return true;
4040 /* Given a lookup that returned VAL, decide if we want to ignore it or
4041 not based on DECL_ANTICIPATED. */
4043 bool
4044 hidden_name_p (tree val)
4046 if (DECL_P (val)
4047 && DECL_LANG_SPECIFIC (val)
4048 && DECL_ANTICIPATED (val))
4049 return true;
4050 return false;
4053 /* Remove any hidden friend functions from a possibly overloaded set
4054 of functions. */
4056 tree
4057 remove_hidden_names (tree fns)
4059 if (!fns)
4060 return fns;
4062 if (TREE_CODE (fns) == FUNCTION_DECL && hidden_name_p (fns))
4063 fns = NULL_TREE;
4064 else if (TREE_CODE (fns) == OVERLOAD)
4066 tree o;
4068 for (o = fns; o; o = OVL_NEXT (o))
4069 if (hidden_name_p (OVL_CURRENT (o)))
4070 break;
4071 if (o)
4073 tree n = NULL_TREE;
4075 for (o = fns; o; o = OVL_NEXT (o))
4076 if (!hidden_name_p (OVL_CURRENT (o)))
4077 n = build_overload (OVL_CURRENT (o), n);
4078 fns = n;
4082 return fns;
4085 /* Suggest alternatives for NAME, an IDENTIFIER_NODE for which name
4086 lookup failed. Search through all available namespaces and print out
4087 possible candidates. */
4089 void
4090 suggest_alternatives_for (location_t location, tree name)
4092 VEC(tree,heap) *candidates = NULL;
4093 VEC(tree,heap) *namespaces_to_search = NULL;
4094 int max_to_search = PARAM_VALUE (CXX_MAX_NAMESPACES_FOR_DIAGNOSTIC_HELP);
4095 int n_searched = 0;
4096 tree t;
4097 unsigned ix;
4099 VEC_safe_push (tree, heap, namespaces_to_search, global_namespace);
4101 while (!VEC_empty (tree, namespaces_to_search)
4102 && n_searched < max_to_search)
4104 tree scope = VEC_pop (tree, namespaces_to_search);
4105 struct scope_binding binding = EMPTY_SCOPE_BINDING;
4106 struct cp_binding_level *level = NAMESPACE_LEVEL (scope);
4108 /* Look in this namespace. */
4109 qualified_lookup_using_namespace (name, scope, &binding, 0);
4111 n_searched++;
4113 if (binding.value)
4114 VEC_safe_push (tree, heap, candidates, binding.value);
4116 /* Add child namespaces. */
4117 for (t = level->namespaces; t; t = DECL_CHAIN (t))
4118 VEC_safe_push (tree, heap, namespaces_to_search, t);
4121 /* If we stopped before we could examine all namespaces, inform the
4122 user. Do this even if we don't have any candidates, since there
4123 might be more candidates further down that we weren't able to
4124 find. */
4125 if (n_searched >= max_to_search
4126 && !VEC_empty (tree, namespaces_to_search))
4127 inform (location,
4128 "maximum limit of %d namespaces searched for %qE",
4129 max_to_search, name);
4131 VEC_free (tree, heap, namespaces_to_search);
4133 /* Nothing useful to report. */
4134 if (VEC_empty (tree, candidates))
4135 return;
4137 inform_n (location, VEC_length (tree, candidates),
4138 "suggested alternative:",
4139 "suggested alternatives:");
4141 FOR_EACH_VEC_ELT (tree, candidates, ix, t)
4142 inform (location_of (t), " %qE", t);
4144 VEC_free (tree, heap, candidates);
4147 /* Unscoped lookup of a global: iterate over current namespaces,
4148 considering using-directives. */
4150 static tree
4151 unqualified_namespace_lookup_1 (tree name, int flags)
4153 tree initial = current_decl_namespace ();
4154 tree scope = initial;
4155 tree siter;
4156 struct cp_binding_level *level;
4157 tree val = NULL_TREE;
4159 for (; !val; scope = CP_DECL_CONTEXT (scope))
4161 struct scope_binding binding = EMPTY_SCOPE_BINDING;
4162 cxx_binding *b =
4163 cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (scope), name);
4165 if (b)
4166 ambiguous_decl (&binding, b, flags);
4168 /* Add all _DECLs seen through local using-directives. */
4169 for (level = current_binding_level;
4170 level->kind != sk_namespace;
4171 level = level->level_chain)
4172 if (!lookup_using_namespace (name, &binding, level->using_directives,
4173 scope, flags))
4174 /* Give up because of error. */
4175 return error_mark_node;
4177 /* Add all _DECLs seen through global using-directives. */
4178 /* XXX local and global using lists should work equally. */
4179 siter = initial;
4180 while (1)
4182 if (!lookup_using_namespace (name, &binding,
4183 DECL_NAMESPACE_USING (siter),
4184 scope, flags))
4185 /* Give up because of error. */
4186 return error_mark_node;
4187 if (siter == scope) break;
4188 siter = CP_DECL_CONTEXT (siter);
4191 val = binding.value;
4192 if (scope == global_namespace)
4193 break;
4195 return val;
4198 /* Wrapper for unqualified_namespace_lookup_1. */
4200 static tree
4201 unqualified_namespace_lookup (tree name, int flags)
4203 tree ret;
4204 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4205 ret = unqualified_namespace_lookup_1 (name, flags);
4206 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4207 return ret;
4210 /* Look up NAME (an IDENTIFIER_NODE) in SCOPE (either a NAMESPACE_DECL
4211 or a class TYPE). If IS_TYPE_P is TRUE, then ignore non-type
4212 bindings.
4214 Returns a DECL (or OVERLOAD, or BASELINK) representing the
4215 declaration found. If no suitable declaration can be found,
4216 ERROR_MARK_NODE is returned. If COMPLAIN is true and SCOPE is
4217 neither a class-type nor a namespace a diagnostic is issued. */
4219 tree
4220 lookup_qualified_name (tree scope, tree name, bool is_type_p, bool complain)
4222 int flags = 0;
4223 tree t = NULL_TREE;
4225 if (TREE_CODE (scope) == NAMESPACE_DECL)
4227 struct scope_binding binding = EMPTY_SCOPE_BINDING;
4229 flags |= LOOKUP_COMPLAIN;
4230 if (is_type_p)
4231 flags |= LOOKUP_PREFER_TYPES;
4232 if (qualified_lookup_using_namespace (name, scope, &binding, flags))
4233 t = binding.value;
4235 else if (cxx_dialect != cxx98 && TREE_CODE (scope) == ENUMERAL_TYPE)
4236 t = lookup_enumerator (scope, name);
4237 else if (is_class_type (scope, complain))
4238 t = lookup_member (scope, name, 2, is_type_p);
4240 if (!t)
4241 return error_mark_node;
4242 return t;
4245 /* Subroutine of unqualified_namespace_lookup:
4246 Add the bindings of NAME in used namespaces to VAL.
4247 We are currently looking for names in namespace SCOPE, so we
4248 look through USINGS for using-directives of namespaces
4249 which have SCOPE as a common ancestor with the current scope.
4250 Returns false on errors. */
4252 static bool
4253 lookup_using_namespace (tree name, struct scope_binding *val,
4254 tree usings, tree scope, int flags)
4256 tree iter;
4257 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4258 /* Iterate over all used namespaces in current, searching for using
4259 directives of scope. */
4260 for (iter = usings; iter; iter = TREE_CHAIN (iter))
4261 if (TREE_VALUE (iter) == scope)
4263 tree used = ORIGINAL_NAMESPACE (TREE_PURPOSE (iter));
4264 cxx_binding *val1 =
4265 cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (used), name);
4266 /* Resolve ambiguities. */
4267 if (val1)
4268 ambiguous_decl (val, val1, flags);
4270 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4271 return val->value != error_mark_node;
4274 /* Returns true iff VEC contains TARGET. */
4276 static bool
4277 tree_vec_contains (VEC(tree,gc)* vec, tree target)
4279 unsigned int i;
4280 tree elt;
4281 FOR_EACH_VEC_ELT (tree,vec,i,elt)
4282 if (elt == target)
4283 return true;
4284 return false;
4287 /* [namespace.qual]
4288 Accepts the NAME to lookup and its qualifying SCOPE.
4289 Returns the name/type pair found into the cxx_binding *RESULT,
4290 or false on error. */
4292 static bool
4293 qualified_lookup_using_namespace (tree name, tree scope,
4294 struct scope_binding *result, int flags)
4296 /* Maintain a list of namespaces visited... */
4297 VEC(tree,gc) *seen = NULL;
4298 VEC(tree,gc) *seen_inline = NULL;
4299 /* ... and a list of namespace yet to see. */
4300 VEC(tree,gc) *todo = NULL;
4301 VEC(tree,gc) *todo_maybe = NULL;
4302 VEC(tree,gc) *todo_inline = NULL;
4303 tree usings;
4304 timevar_start (TV_NAME_LOOKUP);
4305 /* Look through namespace aliases. */
4306 scope = ORIGINAL_NAMESPACE (scope);
4308 /* Algorithm: Starting with SCOPE, walk through the the set of used
4309 namespaces. For each used namespace, look through its inline
4310 namespace set for any bindings and usings. If no bindings are found,
4311 add any usings seen to the set of used namespaces. */
4312 VEC_safe_push (tree, gc, todo, scope);
4314 while (VEC_length (tree, todo))
4316 bool found_here;
4317 scope = VEC_pop (tree, todo);
4318 if (tree_vec_contains (seen, scope))
4319 continue;
4320 VEC_safe_push (tree, gc, seen, scope);
4321 VEC_safe_push (tree, gc, todo_inline, scope);
4323 found_here = false;
4324 while (VEC_length (tree, todo_inline))
4326 cxx_binding *binding;
4328 scope = VEC_pop (tree, todo_inline);
4329 if (tree_vec_contains (seen_inline, scope))
4330 continue;
4331 VEC_safe_push (tree, gc, seen_inline, scope);
4333 binding =
4334 cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (scope), name);
4335 if (binding)
4337 found_here = true;
4338 ambiguous_decl (result, binding, flags);
4341 for (usings = DECL_NAMESPACE_USING (scope); usings;
4342 usings = TREE_CHAIN (usings))
4343 if (!TREE_INDIRECT_USING (usings))
4345 if (is_associated_namespace (scope, TREE_PURPOSE (usings)))
4346 VEC_safe_push (tree, gc, todo_inline, TREE_PURPOSE (usings));
4347 else
4348 VEC_safe_push (tree, gc, todo_maybe, TREE_PURPOSE (usings));
4352 if (found_here)
4353 VEC_truncate (tree, todo_maybe, 0);
4354 else
4355 while (VEC_length (tree, todo_maybe))
4356 VEC_safe_push (tree, gc, todo, VEC_pop (tree, todo_maybe));
4358 VEC_free (tree,gc,todo);
4359 VEC_free (tree,gc,todo_maybe);
4360 VEC_free (tree,gc,todo_inline);
4361 VEC_free (tree,gc,seen);
4362 VEC_free (tree,gc,seen_inline);
4363 timevar_stop (TV_NAME_LOOKUP);
4364 return result->value != error_mark_node;
4367 /* Subroutine of outer_binding.
4369 Returns TRUE if BINDING is a binding to a template parameter of
4370 SCOPE. In that case SCOPE is the scope of a primary template
4371 parameter -- in the sense of G++, i.e, a template that has its own
4372 template header.
4374 Returns FALSE otherwise. */
4376 static bool
4377 binding_to_template_parms_of_scope_p (cxx_binding *binding,
4378 cxx_scope *scope)
4380 tree binding_value;
4382 if (!binding || !scope)
4383 return false;
4385 binding_value = binding->value ? binding->value : binding->type;
4387 return (scope
4388 && scope->this_entity
4389 && get_template_info (scope->this_entity)
4390 && PRIMARY_TEMPLATE_P (TI_TEMPLATE
4391 (get_template_info (scope->this_entity)))
4392 && parameter_of_template_p (binding_value,
4393 TI_TEMPLATE (get_template_info \
4394 (scope->this_entity))));
4397 /* Return the innermost non-namespace binding for NAME from a scope
4398 containing BINDING, or, if BINDING is NULL, the current scope.
4399 Please note that for a given template, the template parameters are
4400 considered to be in the scope containing the current scope.
4401 If CLASS_P is false, then class bindings are ignored. */
4403 cxx_binding *
4404 outer_binding (tree name,
4405 cxx_binding *binding,
4406 bool class_p)
4408 cxx_binding *outer;
4409 cxx_scope *scope;
4410 cxx_scope *outer_scope;
4412 if (binding)
4414 scope = binding->scope->level_chain;
4415 outer = binding->previous;
4417 else
4419 scope = current_binding_level;
4420 outer = IDENTIFIER_BINDING (name);
4422 outer_scope = outer ? outer->scope : NULL;
4424 /* Because we create class bindings lazily, we might be missing a
4425 class binding for NAME. If there are any class binding levels
4426 between the LAST_BINDING_LEVEL and the scope in which OUTER was
4427 declared, we must lookup NAME in those class scopes. */
4428 if (class_p)
4429 while (scope && scope != outer_scope && scope->kind != sk_namespace)
4431 if (scope->kind == sk_class)
4433 cxx_binding *class_binding;
4435 class_binding = get_class_binding (name, scope);
4436 if (class_binding)
4438 /* Thread this new class-scope binding onto the
4439 IDENTIFIER_BINDING list so that future lookups
4440 find it quickly. */
4441 class_binding->previous = outer;
4442 if (binding)
4443 binding->previous = class_binding;
4444 else
4445 IDENTIFIER_BINDING (name) = class_binding;
4446 return class_binding;
4449 /* If we are in a member template, the template parms of the member
4450 template are considered to be inside the scope of the containing
4451 class, but within G++ the class bindings are all pushed between the
4452 template parms and the function body. So if the outer binding is
4453 a template parm for the current scope, return it now rather than
4454 look for a class binding. */
4455 if (outer_scope && outer_scope->kind == sk_template_parms
4456 && binding_to_template_parms_of_scope_p (outer, scope))
4457 return outer;
4459 scope = scope->level_chain;
4462 return outer;
4465 /* Return the innermost block-scope or class-scope value binding for
4466 NAME, or NULL_TREE if there is no such binding. */
4468 tree
4469 innermost_non_namespace_value (tree name)
4471 cxx_binding *binding;
4472 binding = outer_binding (name, /*binding=*/NULL, /*class_p=*/true);
4473 return binding ? binding->value : NULL_TREE;
4476 /* Look up NAME in the current binding level and its superiors in the
4477 namespace of variables, functions and typedefs. Return a ..._DECL
4478 node of some kind representing its definition if there is only one
4479 such declaration, or return a TREE_LIST with all the overloaded
4480 definitions if there are many, or return 0 if it is undefined.
4481 Hidden name, either friend declaration or built-in function, are
4482 not ignored.
4484 If PREFER_TYPE is > 0, we prefer TYPE_DECLs or namespaces.
4485 If PREFER_TYPE is > 1, we reject non-type decls (e.g. namespaces).
4486 Otherwise we prefer non-TYPE_DECLs.
4488 If NONCLASS is nonzero, bindings in class scopes are ignored. If
4489 BLOCK_P is false, bindings in block scopes are ignored. */
4491 static tree
4492 lookup_name_real_1 (tree name, int prefer_type, int nonclass, bool block_p,
4493 int namespaces_only, int flags)
4495 cxx_binding *iter;
4496 tree val = NULL_TREE;
4498 /* Conversion operators are handled specially because ordinary
4499 unqualified name lookup will not find template conversion
4500 operators. */
4501 if (IDENTIFIER_TYPENAME_P (name))
4503 struct cp_binding_level *level;
4505 for (level = current_binding_level;
4506 level && level->kind != sk_namespace;
4507 level = level->level_chain)
4509 tree class_type;
4510 tree operators;
4512 /* A conversion operator can only be declared in a class
4513 scope. */
4514 if (level->kind != sk_class)
4515 continue;
4517 /* Lookup the conversion operator in the class. */
4518 class_type = level->this_entity;
4519 operators = lookup_fnfields (class_type, name, /*protect=*/0);
4520 if (operators)
4521 return operators;
4524 return NULL_TREE;
4527 flags |= lookup_flags (prefer_type, namespaces_only);
4529 /* First, look in non-namespace scopes. */
4531 if (current_class_type == NULL_TREE)
4532 nonclass = 1;
4534 if (block_p || !nonclass)
4535 for (iter = outer_binding (name, NULL, !nonclass);
4536 iter;
4537 iter = outer_binding (name, iter, !nonclass))
4539 tree binding;
4541 /* Skip entities we don't want. */
4542 if (LOCAL_BINDING_P (iter) ? !block_p : nonclass)
4543 continue;
4545 /* If this is the kind of thing we're looking for, we're done. */
4546 if (qualify_lookup (iter->value, flags))
4547 binding = iter->value;
4548 else if ((flags & LOOKUP_PREFER_TYPES)
4549 && qualify_lookup (iter->type, flags))
4550 binding = iter->type;
4551 else
4552 binding = NULL_TREE;
4554 if (binding)
4556 if (hidden_name_p (binding))
4558 /* A non namespace-scope binding can only be hidden in the
4559 presence of a local class, due to friend declarations.
4561 In particular, consider:
4563 struct C;
4564 void f() {
4565 struct A {
4566 friend struct B;
4567 friend struct C;
4568 void g() {
4569 B* b; // error: B is hidden
4570 C* c; // OK, finds ::C
4573 B *b; // error: B is hidden
4574 C *c; // OK, finds ::C
4575 struct B {};
4576 B *bb; // OK
4579 The standard says that "B" is a local class in "f"
4580 (but not nested within "A") -- but that name lookup
4581 for "B" does not find this declaration until it is
4582 declared directly with "f".
4584 In particular:
4586 [class.friend]
4588 If a friend declaration appears in a local class and
4589 the name specified is an unqualified name, a prior
4590 declaration is looked up without considering scopes
4591 that are outside the innermost enclosing non-class
4592 scope. For a friend function declaration, if there is
4593 no prior declaration, the program is ill-formed. For a
4594 friend class declaration, if there is no prior
4595 declaration, the class that is specified belongs to the
4596 innermost enclosing non-class scope, but if it is
4597 subsequently referenced, its name is not found by name
4598 lookup until a matching declaration is provided in the
4599 innermost enclosing nonclass scope.
4601 So just keep looking for a non-hidden binding.
4603 gcc_assert (TREE_CODE (binding) == TYPE_DECL);
4604 continue;
4606 val = binding;
4607 break;
4611 /* Now lookup in namespace scopes. */
4612 if (!val)
4613 val = unqualified_namespace_lookup (name, flags);
4615 /* If we have a single function from a using decl, pull it out. */
4616 if (val && TREE_CODE (val) == OVERLOAD && !really_overloaded_fn (val))
4617 val = OVL_FUNCTION (val);
4619 return val;
4622 /* Wrapper for lookup_name_real_1. */
4624 tree
4625 lookup_name_real (tree name, int prefer_type, int nonclass, bool block_p,
4626 int namespaces_only, int flags)
4628 tree ret;
4629 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4630 ret = lookup_name_real_1 (name, prefer_type, nonclass, block_p,
4631 namespaces_only, flags);
4632 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4633 return ret;
4636 tree
4637 lookup_name_nonclass (tree name)
4639 return lookup_name_real (name, 0, 1, /*block_p=*/true, 0, LOOKUP_COMPLAIN);
4642 tree
4643 lookup_function_nonclass (tree name, VEC(tree,gc) *args, bool block_p)
4645 return
4646 lookup_arg_dependent (name,
4647 lookup_name_real (name, 0, 1, block_p, 0,
4648 LOOKUP_COMPLAIN),
4649 args, false);
4652 tree
4653 lookup_name (tree name)
4655 return lookup_name_real (name, 0, 0, /*block_p=*/true, 0, LOOKUP_COMPLAIN);
4658 tree
4659 lookup_name_prefer_type (tree name, int prefer_type)
4661 return lookup_name_real (name, prefer_type, 0, /*block_p=*/true,
4662 0, LOOKUP_COMPLAIN);
4665 /* Look up NAME for type used in elaborated name specifier in
4666 the scopes given by SCOPE. SCOPE can be either TS_CURRENT or
4667 TS_WITHIN_ENCLOSING_NON_CLASS. Although not implied by the
4668 name, more scopes are checked if cleanup or template parameter
4669 scope is encountered.
4671 Unlike lookup_name_real, we make sure that NAME is actually
4672 declared in the desired scope, not from inheritance, nor using
4673 directive. For using declaration, there is DR138 still waiting
4674 to be resolved. Hidden name coming from an earlier friend
4675 declaration is also returned.
4677 A TYPE_DECL best matching the NAME is returned. Catching error
4678 and issuing diagnostics are caller's responsibility. */
4680 static tree
4681 lookup_type_scope_1 (tree name, tag_scope scope)
4683 cxx_binding *iter = NULL;
4684 tree val = NULL_TREE;
4686 /* Look in non-namespace scope first. */
4687 if (current_binding_level->kind != sk_namespace)
4688 iter = outer_binding (name, NULL, /*class_p=*/ true);
4689 for (; iter; iter = outer_binding (name, iter, /*class_p=*/ true))
4691 /* Check if this is the kind of thing we're looking for.
4692 If SCOPE is TS_CURRENT, also make sure it doesn't come from
4693 base class. For ITER->VALUE, we can simply use
4694 INHERITED_VALUE_BINDING_P. For ITER->TYPE, we have to use
4695 our own check.
4697 We check ITER->TYPE before ITER->VALUE in order to handle
4698 typedef struct C {} C;
4699 correctly. */
4701 if (qualify_lookup (iter->type, LOOKUP_PREFER_TYPES)
4702 && (scope != ts_current
4703 || LOCAL_BINDING_P (iter)
4704 || DECL_CONTEXT (iter->type) == iter->scope->this_entity))
4705 val = iter->type;
4706 else if ((scope != ts_current
4707 || !INHERITED_VALUE_BINDING_P (iter))
4708 && qualify_lookup (iter->value, LOOKUP_PREFER_TYPES))
4709 val = iter->value;
4711 if (val)
4712 break;
4715 /* Look in namespace scope. */
4716 if (!val)
4718 iter = cxx_scope_find_binding_for_name
4719 (NAMESPACE_LEVEL (current_decl_namespace ()), name);
4721 if (iter)
4723 /* If this is the kind of thing we're looking for, we're done. */
4724 if (qualify_lookup (iter->type, LOOKUP_PREFER_TYPES))
4725 val = iter->type;
4726 else if (qualify_lookup (iter->value, LOOKUP_PREFER_TYPES))
4727 val = iter->value;
4732 /* Type found, check if it is in the allowed scopes, ignoring cleanup
4733 and template parameter scopes. */
4734 if (val)
4736 struct cp_binding_level *b = current_binding_level;
4737 while (b)
4739 if (iter->scope == b)
4740 return val;
4742 if (b->kind == sk_cleanup || b->kind == sk_template_parms
4743 || b->kind == sk_function_parms)
4744 b = b->level_chain;
4745 else if (b->kind == sk_class
4746 && scope == ts_within_enclosing_non_class)
4747 b = b->level_chain;
4748 else
4749 break;
4753 return NULL_TREE;
4756 /* Wrapper for lookup_type_scope_1. */
4758 tree
4759 lookup_type_scope (tree name, tag_scope scope)
4761 tree ret;
4762 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4763 ret = lookup_type_scope_1 (name, scope);
4764 pph_catch_name_lookup (ret);
4765 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4766 return ret;
4770 /* Similar to `lookup_name' but look only in the innermost non-class
4771 binding level. */
4773 static tree
4774 lookup_name_innermost_nonclass_level_1 (tree name)
4776 struct cp_binding_level *b;
4777 tree t = NULL_TREE;
4779 timevar_push (TV_NAME_LOOKUP);
4780 b = innermost_nonclass_level ();
4782 if (b->kind == sk_namespace)
4784 t = IDENTIFIER_NAMESPACE_VALUE (name);
4786 /* extern "C" function() */
4787 if (t != NULL_TREE && TREE_CODE (t) == TREE_LIST)
4788 t = TREE_VALUE (t);
4790 else if (IDENTIFIER_BINDING (name)
4791 && LOCAL_BINDING_P (IDENTIFIER_BINDING (name)))
4793 cxx_binding *binding;
4794 binding = IDENTIFIER_BINDING (name);
4795 while (1)
4797 if (binding->scope == b
4798 && !(TREE_CODE (binding->value) == VAR_DECL
4799 && DECL_DEAD_FOR_LOCAL (binding->value)))
4800 return binding->value;
4802 if (b->kind == sk_cleanup)
4803 b = b->level_chain;
4804 else
4805 break;
4809 return t;
4812 /* Wrapper for lookup_name_innermost_nonclass_level_1. */
4814 tree
4815 lookup_name_innermost_nonclass_level (tree name)
4817 tree ret;
4818 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4819 ret = lookup_name_innermost_nonclass_level_1 (name);
4820 pph_catch_name_lookup (ret);
4821 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4822 return ret;
4826 /* Returns true iff DECL is a block-scope extern declaration of a function
4827 or variable. */
4829 bool
4830 is_local_extern (tree decl)
4832 cxx_binding *binding;
4834 /* For functions, this is easy. */
4835 if (TREE_CODE (decl) == FUNCTION_DECL)
4836 return DECL_LOCAL_FUNCTION_P (decl);
4838 if (TREE_CODE (decl) != VAR_DECL)
4839 return false;
4840 if (!current_function_decl)
4841 return false;
4843 /* For variables, this is not easy. We need to look at the binding stack
4844 for the identifier to see whether the decl we have is a local. */
4845 for (binding = IDENTIFIER_BINDING (DECL_NAME (decl));
4846 binding && binding->scope->kind != sk_namespace;
4847 binding = binding->previous)
4848 if (binding->value == decl)
4849 return LOCAL_BINDING_P (binding);
4851 return false;
4854 /* Like lookup_name_innermost_nonclass_level, but for types. */
4856 static tree
4857 lookup_type_current_level (tree name)
4859 tree t = NULL_TREE;
4861 timevar_start (TV_NAME_LOOKUP);
4862 gcc_assert (current_binding_level->kind != sk_namespace);
4864 if (REAL_IDENTIFIER_TYPE_VALUE (name) != NULL_TREE
4865 && REAL_IDENTIFIER_TYPE_VALUE (name) != global_type_node)
4867 struct cp_binding_level *b = current_binding_level;
4868 while (1)
4870 if (purpose_member (name, b->type_shadowed))
4872 t = REAL_IDENTIFIER_TYPE_VALUE (name);
4873 break;
4875 if (b->kind == sk_cleanup)
4876 b = b->level_chain;
4877 else
4878 break;
4882 timevar_stop (TV_NAME_LOOKUP);
4883 return t;
4886 /* [basic.lookup.koenig] */
4887 /* A nonzero return value in the functions below indicates an error. */
4889 struct arg_lookup
4891 tree name;
4892 VEC(tree,gc) *args;
4893 VEC(tree,gc) *namespaces;
4894 VEC(tree,gc) *classes;
4895 tree functions;
4898 static bool arg_assoc (struct arg_lookup*, tree);
4899 static bool arg_assoc_args (struct arg_lookup*, tree);
4900 static bool arg_assoc_args_vec (struct arg_lookup*, VEC(tree,gc) *);
4901 static bool arg_assoc_type (struct arg_lookup*, tree);
4902 static bool add_function (struct arg_lookup *, tree);
4903 static bool arg_assoc_namespace (struct arg_lookup *, tree);
4904 static bool arg_assoc_class_only (struct arg_lookup *, tree);
4905 static bool arg_assoc_bases (struct arg_lookup *, tree);
4906 static bool arg_assoc_class (struct arg_lookup *, tree);
4907 static bool arg_assoc_template_arg (struct arg_lookup*, tree);
4909 /* Add a function to the lookup structure.
4910 Returns true on error. */
4912 static bool
4913 add_function (struct arg_lookup *k, tree fn)
4915 /* We used to check here to see if the function was already in the list,
4916 but that's O(n^2), which is just too expensive for function lookup.
4917 Now we deal with the occasional duplicate in joust. In doing this, we
4918 assume that the number of duplicates will be small compared to the
4919 total number of functions being compared, which should usually be the
4920 case. */
4922 if (!is_overloaded_fn (fn))
4923 /* All names except those of (possibly overloaded) functions and
4924 function templates are ignored. */;
4925 else if (!k->functions)
4926 k->functions = fn;
4927 else if (fn == k->functions)
4929 else
4930 k->functions = build_overload (fn, k->functions);
4932 return false;
4935 /* Returns true iff CURRENT has declared itself to be an associated
4936 namespace of SCOPE via a strong using-directive (or transitive chain
4937 thereof). Both are namespaces. */
4939 bool
4940 is_associated_namespace (tree current, tree scope)
4942 VEC(tree,gc) *seen = make_tree_vector ();
4943 VEC(tree,gc) *todo = make_tree_vector ();
4944 tree t;
4945 bool ret;
4947 while (1)
4949 if (scope == current)
4951 ret = true;
4952 break;
4954 VEC_safe_push (tree, gc, seen, scope);
4955 for (t = DECL_NAMESPACE_ASSOCIATIONS (scope); t; t = TREE_CHAIN (t))
4956 if (!vec_member (TREE_PURPOSE (t), seen))
4957 VEC_safe_push (tree, gc, todo, TREE_PURPOSE (t));
4958 if (!VEC_empty (tree, todo))
4960 scope = VEC_last (tree, todo);
4961 VEC_pop (tree, todo);
4963 else
4965 ret = false;
4966 break;
4970 release_tree_vector (seen);
4971 release_tree_vector (todo);
4973 return ret;
4976 /* Add functions of a namespace to the lookup structure.
4977 Returns true on error. */
4979 static bool
4980 arg_assoc_namespace (struct arg_lookup *k, tree scope)
4982 tree value;
4984 if (vec_member (scope, k->namespaces))
4985 return false;
4986 VEC_safe_push (tree, gc, k->namespaces, scope);
4988 /* Check out our super-users. */
4989 for (value = DECL_NAMESPACE_ASSOCIATIONS (scope); value;
4990 value = TREE_CHAIN (value))
4991 if (arg_assoc_namespace (k, TREE_PURPOSE (value)))
4992 return true;
4994 /* Also look down into inline namespaces. */
4995 for (value = DECL_NAMESPACE_USING (scope); value;
4996 value = TREE_CHAIN (value))
4997 if (is_associated_namespace (scope, TREE_PURPOSE (value)))
4998 if (arg_assoc_namespace (k, TREE_PURPOSE (value)))
4999 return true;
5001 value = namespace_binding (k->name, scope);
5002 if (!value)
5003 return false;
5005 for (; value; value = OVL_NEXT (value))
5007 /* We don't want to find arbitrary hidden functions via argument
5008 dependent lookup. We only want to find friends of associated
5009 classes, which we'll do via arg_assoc_class. */
5010 if (hidden_name_p (OVL_CURRENT (value)))
5011 continue;
5013 if (add_function (k, OVL_CURRENT (value)))
5014 return true;
5017 return false;
5020 /* Adds everything associated with a template argument to the lookup
5021 structure. Returns true on error. */
5023 static bool
5024 arg_assoc_template_arg (struct arg_lookup *k, tree arg)
5026 /* [basic.lookup.koenig]
5028 If T is a template-id, its associated namespaces and classes are
5029 ... the namespaces and classes associated with the types of the
5030 template arguments provided for template type parameters
5031 (excluding template template parameters); the namespaces in which
5032 any template template arguments are defined; and the classes in
5033 which any member templates used as template template arguments
5034 are defined. [Note: non-type template arguments do not
5035 contribute to the set of associated namespaces. ] */
5037 /* Consider first template template arguments. */
5038 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
5039 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
5040 return false;
5041 else if (TREE_CODE (arg) == TEMPLATE_DECL)
5043 tree ctx = CP_DECL_CONTEXT (arg);
5045 /* It's not a member template. */
5046 if (TREE_CODE (ctx) == NAMESPACE_DECL)
5047 return arg_assoc_namespace (k, ctx);
5048 /* Otherwise, it must be member template. */
5049 else
5050 return arg_assoc_class_only (k, ctx);
5052 /* It's an argument pack; handle it recursively. */
5053 else if (ARGUMENT_PACK_P (arg))
5055 tree args = ARGUMENT_PACK_ARGS (arg);
5056 int i, len = TREE_VEC_LENGTH (args);
5057 for (i = 0; i < len; ++i)
5058 if (arg_assoc_template_arg (k, TREE_VEC_ELT (args, i)))
5059 return true;
5061 return false;
5063 /* It's not a template template argument, but it is a type template
5064 argument. */
5065 else if (TYPE_P (arg))
5066 return arg_assoc_type (k, arg);
5067 /* It's a non-type template argument. */
5068 else
5069 return false;
5072 /* Adds the class and its friends to the lookup structure.
5073 Returns true on error. */
5075 static bool
5076 arg_assoc_class_only (struct arg_lookup *k, tree type)
5078 tree list, friends, context;
5080 /* Backend-built structures, such as __builtin_va_list, aren't
5081 affected by all this. */
5082 if (!CLASS_TYPE_P (type))
5083 return false;
5085 context = decl_namespace_context (type);
5086 if (arg_assoc_namespace (k, context))
5087 return true;
5089 complete_type (type);
5091 /* Process friends. */
5092 for (list = DECL_FRIENDLIST (TYPE_MAIN_DECL (type)); list;
5093 list = TREE_CHAIN (list))
5094 if (k->name == FRIEND_NAME (list))
5095 for (friends = FRIEND_DECLS (list); friends;
5096 friends = TREE_CHAIN (friends))
5098 tree fn = TREE_VALUE (friends);
5100 /* Only interested in global functions with potentially hidden
5101 (i.e. unqualified) declarations. */
5102 if (CP_DECL_CONTEXT (fn) != context)
5103 continue;
5104 /* Template specializations are never found by name lookup.
5105 (Templates themselves can be found, but not template
5106 specializations.) */
5107 if (TREE_CODE (fn) == FUNCTION_DECL && DECL_USE_TEMPLATE (fn))
5108 continue;
5109 if (add_function (k, fn))
5110 return true;
5113 return false;
5116 /* Adds the class and its bases to the lookup structure.
5117 Returns true on error. */
5119 static bool
5120 arg_assoc_bases (struct arg_lookup *k, tree type)
5122 if (arg_assoc_class_only (k, type))
5123 return true;
5125 if (TYPE_BINFO (type))
5127 /* Process baseclasses. */
5128 tree binfo, base_binfo;
5129 int i;
5131 for (binfo = TYPE_BINFO (type), i = 0;
5132 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
5133 if (arg_assoc_bases (k, BINFO_TYPE (base_binfo)))
5134 return true;
5137 return false;
5140 /* Adds everything associated with a class argument type to the lookup
5141 structure. Returns true on error.
5143 If T is a class type (including unions), its associated classes are: the
5144 class itself; the class of which it is a member, if any; and its direct
5145 and indirect base classes. Its associated namespaces are the namespaces
5146 of which its associated classes are members. Furthermore, if T is a
5147 class template specialization, its associated namespaces and classes
5148 also include: the namespaces and classes associated with the types of
5149 the template arguments provided for template type parameters (excluding
5150 template template parameters); the namespaces of which any template
5151 template arguments are members; and the classes of which any member
5152 templates used as template template arguments are members. [ Note:
5153 non-type template arguments do not contribute to the set of associated
5154 namespaces. --end note] */
5156 static bool
5157 arg_assoc_class (struct arg_lookup *k, tree type)
5159 tree list;
5160 int i;
5162 /* Backend build structures, such as __builtin_va_list, aren't
5163 affected by all this. */
5164 if (!CLASS_TYPE_P (type))
5165 return false;
5167 if (vec_member (type, k->classes))
5168 return false;
5169 VEC_safe_push (tree, gc, k->classes, type);
5171 if (TYPE_CLASS_SCOPE_P (type)
5172 && arg_assoc_class_only (k, TYPE_CONTEXT (type)))
5173 return true;
5175 if (arg_assoc_bases (k, type))
5176 return true;
5178 /* Process template arguments. */
5179 if (CLASSTYPE_TEMPLATE_INFO (type)
5180 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
5182 list = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
5183 for (i = 0; i < TREE_VEC_LENGTH (list); ++i)
5184 if (arg_assoc_template_arg (k, TREE_VEC_ELT (list, i)))
5185 return true;
5188 return false;
5191 /* Adds everything associated with a given type.
5192 Returns 1 on error. */
5194 static bool
5195 arg_assoc_type (struct arg_lookup *k, tree type)
5197 /* As we do not get the type of non-type dependent expressions
5198 right, we can end up with such things without a type. */
5199 if (!type)
5200 return false;
5202 if (TYPE_PTRMEM_P (type))
5204 /* Pointer to member: associate class type and value type. */
5205 if (arg_assoc_type (k, TYPE_PTRMEM_CLASS_TYPE (type)))
5206 return true;
5207 return arg_assoc_type (k, TYPE_PTRMEM_POINTED_TO_TYPE (type));
5209 else switch (TREE_CODE (type))
5211 case ERROR_MARK:
5212 return false;
5213 case VOID_TYPE:
5214 case INTEGER_TYPE:
5215 case REAL_TYPE:
5216 case COMPLEX_TYPE:
5217 case VECTOR_TYPE:
5218 case BOOLEAN_TYPE:
5219 case FIXED_POINT_TYPE:
5220 case DECLTYPE_TYPE:
5221 case NULLPTR_TYPE:
5222 return false;
5223 case RECORD_TYPE:
5224 if (TYPE_PTRMEMFUNC_P (type))
5225 return arg_assoc_type (k, TYPE_PTRMEMFUNC_FN_TYPE (type));
5226 case UNION_TYPE:
5227 return arg_assoc_class (k, type);
5228 case POINTER_TYPE:
5229 case REFERENCE_TYPE:
5230 case ARRAY_TYPE:
5231 return arg_assoc_type (k, TREE_TYPE (type));
5232 case ENUMERAL_TYPE:
5233 if (TYPE_CLASS_SCOPE_P (type)
5234 && arg_assoc_class_only (k, TYPE_CONTEXT (type)))
5235 return true;
5236 return arg_assoc_namespace (k, decl_namespace_context (type));
5237 case METHOD_TYPE:
5238 /* The basetype is referenced in the first arg type, so just
5239 fall through. */
5240 case FUNCTION_TYPE:
5241 /* Associate the parameter types. */
5242 if (arg_assoc_args (k, TYPE_ARG_TYPES (type)))
5243 return true;
5244 /* Associate the return type. */
5245 return arg_assoc_type (k, TREE_TYPE (type));
5246 case TEMPLATE_TYPE_PARM:
5247 case BOUND_TEMPLATE_TEMPLATE_PARM:
5248 return false;
5249 case TYPENAME_TYPE:
5250 return false;
5251 case LANG_TYPE:
5252 gcc_assert (type == unknown_type_node
5253 || type == init_list_type_node);
5254 return false;
5255 case TYPE_PACK_EXPANSION:
5256 return arg_assoc_type (k, PACK_EXPANSION_PATTERN (type));
5258 default:
5259 gcc_unreachable ();
5261 return false;
5264 /* Adds everything associated with arguments. Returns true on error. */
5266 static bool
5267 arg_assoc_args (struct arg_lookup *k, tree args)
5269 for (; args; args = TREE_CHAIN (args))
5270 if (arg_assoc (k, TREE_VALUE (args)))
5271 return true;
5272 return false;
5275 /* Adds everything associated with an argument vector. Returns true
5276 on error. */
5278 static bool
5279 arg_assoc_args_vec (struct arg_lookup *k, VEC(tree,gc) *args)
5281 unsigned int ix;
5282 tree arg;
5284 FOR_EACH_VEC_ELT (tree, args, ix, arg)
5285 if (arg_assoc (k, arg))
5286 return true;
5287 return false;
5290 /* Adds everything associated with a given tree_node. Returns 1 on error. */
5292 static bool
5293 arg_assoc (struct arg_lookup *k, tree n)
5295 if (n == error_mark_node)
5296 return false;
5298 if (TYPE_P (n))
5299 return arg_assoc_type (k, n);
5301 if (! type_unknown_p (n))
5302 return arg_assoc_type (k, TREE_TYPE (n));
5304 if (TREE_CODE (n) == ADDR_EXPR)
5305 n = TREE_OPERAND (n, 0);
5306 if (TREE_CODE (n) == COMPONENT_REF)
5307 n = TREE_OPERAND (n, 1);
5308 if (TREE_CODE (n) == OFFSET_REF)
5309 n = TREE_OPERAND (n, 1);
5310 while (TREE_CODE (n) == TREE_LIST)
5311 n = TREE_VALUE (n);
5312 if (TREE_CODE (n) == BASELINK)
5313 n = BASELINK_FUNCTIONS (n);
5315 if (TREE_CODE (n) == FUNCTION_DECL)
5316 return arg_assoc_type (k, TREE_TYPE (n));
5317 if (TREE_CODE (n) == TEMPLATE_ID_EXPR)
5319 /* The working paper doesn't currently say how to handle template-id
5320 arguments. The sensible thing would seem to be to handle the list
5321 of template candidates like a normal overload set, and handle the
5322 template arguments like we do for class template
5323 specializations. */
5324 tree templ = TREE_OPERAND (n, 0);
5325 tree args = TREE_OPERAND (n, 1);
5326 int ix;
5328 /* First the templates. */
5329 if (arg_assoc (k, templ))
5330 return true;
5332 /* Now the arguments. */
5333 if (args)
5334 for (ix = TREE_VEC_LENGTH (args); ix--;)
5335 if (arg_assoc_template_arg (k, TREE_VEC_ELT (args, ix)) == 1)
5336 return true;
5338 else if (TREE_CODE (n) == OVERLOAD)
5340 for (; n; n = OVL_CHAIN (n))
5341 if (arg_assoc_type (k, TREE_TYPE (OVL_FUNCTION (n))))
5342 return true;
5345 return false;
5348 /* Performs Koenig lookup depending on arguments, where fns
5349 are the functions found in normal lookup. */
5351 static tree
5352 lookup_arg_dependent_1 (tree name, tree fns, VEC(tree,gc) *args,
5353 bool include_std)
5355 struct arg_lookup k;
5357 /* Remove any hidden friend functions from the list of functions
5358 found so far. They will be added back by arg_assoc_class as
5359 appropriate. */
5360 fns = remove_hidden_names (fns);
5362 k.name = name;
5363 k.args = args;
5364 k.functions = fns;
5365 k.classes = make_tree_vector ();
5367 /* We previously performed an optimization here by setting
5368 NAMESPACES to the current namespace when it was safe. However, DR
5369 164 says that namespaces that were already searched in the first
5370 stage of template processing are searched again (potentially
5371 picking up later definitions) in the second stage. */
5372 k.namespaces = make_tree_vector ();
5374 if (include_std)
5375 arg_assoc_namespace (&k, std_node);
5376 arg_assoc_args_vec (&k, args);
5378 fns = k.functions;
5380 if (fns
5381 && TREE_CODE (fns) != VAR_DECL
5382 && !is_overloaded_fn (fns))
5384 error ("argument dependent lookup finds %q+D", fns);
5385 error (" in call to %qD", name);
5386 fns = error_mark_node;
5389 release_tree_vector (k.classes);
5390 release_tree_vector (k.namespaces);
5392 return fns;
5395 /* Wrapper for lookup_arg_dependent_1. */
5397 tree
5398 lookup_arg_dependent (tree name, tree fns, VEC(tree,gc) *args,
5399 bool include_std)
5401 tree ret;
5402 timevar_start (TV_NAME_LOOKUP);
5403 ret = lookup_arg_dependent_1 (name, fns, args, include_std);
5404 pph_catch_name_lookup (ret);
5405 timevar_stop (TV_NAME_LOOKUP);
5406 return ret;
5410 /* Add namespace to using_directives. Return NULL_TREE if nothing was
5411 changed (i.e. there was already a directive), or the fresh
5412 TREE_LIST otherwise. */
5414 static tree
5415 push_using_directive_1 (tree used)
5417 tree ud = current_binding_level->using_directives;
5418 tree iter, ancestor;
5420 /* Check if we already have this. */
5421 if (purpose_member (used, ud) != NULL_TREE)
5422 return NULL_TREE;
5424 ancestor = namespace_ancestor (current_decl_namespace (), used);
5425 ud = current_binding_level->using_directives;
5426 ud = tree_cons (used, ancestor, ud);
5427 current_binding_level->using_directives = ud;
5429 /* Recursively add all namespaces used. */
5430 for (iter = DECL_NAMESPACE_USING (used); iter; iter = TREE_CHAIN (iter))
5431 push_using_directive (TREE_PURPOSE (iter));
5433 return ud;
5436 /* Wrapper for push_using_directive_1. */
5438 static tree
5439 push_using_directive (tree used)
5441 tree ret;
5442 timevar_start (TV_NAME_LOOKUP);
5443 ret = push_using_directive_1 (used);
5444 timevar_stop (TV_NAME_LOOKUP);
5445 return ret;
5448 /* The type TYPE is being declared. If it is a class template, or a
5449 specialization of a class template, do any processing required and
5450 perform error-checking. If IS_FRIEND is nonzero, this TYPE is
5451 being declared a friend. B is the binding level at which this TYPE
5452 should be bound.
5454 Returns the TYPE_DECL for TYPE, which may have been altered by this
5455 processing. */
5457 static tree
5458 maybe_process_template_type_declaration (tree type, int is_friend,
5459 cxx_scope *b)
5461 tree decl = TYPE_NAME (type);
5463 if (processing_template_parmlist)
5464 /* You can't declare a new template type in a template parameter
5465 list. But, you can declare a non-template type:
5467 template <class A*> struct S;
5469 is a forward-declaration of `A'. */
5471 else if (b->kind == sk_namespace
5472 && current_binding_level->kind != sk_namespace)
5473 /* If this new type is being injected into a containing scope,
5474 then it's not a template type. */
5476 else
5478 gcc_assert (MAYBE_CLASS_TYPE_P (type)
5479 || TREE_CODE (type) == ENUMERAL_TYPE);
5481 if (processing_template_decl)
5483 /* This may change after the call to
5484 push_template_decl_real, but we want the original value. */
5485 tree name = DECL_NAME (decl);
5487 decl = push_template_decl_real (decl, is_friend);
5488 if (decl == error_mark_node)
5489 return error_mark_node;
5491 /* If the current binding level is the binding level for the
5492 template parameters (see the comment in
5493 begin_template_parm_list) and the enclosing level is a class
5494 scope, and we're not looking at a friend, push the
5495 declaration of the member class into the class scope. In the
5496 friend case, push_template_decl will already have put the
5497 friend into global scope, if appropriate. */
5498 if (TREE_CODE (type) != ENUMERAL_TYPE
5499 && !is_friend && b->kind == sk_template_parms
5500 && b->level_chain->kind == sk_class)
5502 finish_member_declaration (CLASSTYPE_TI_TEMPLATE (type));
5504 if (!COMPLETE_TYPE_P (current_class_type))
5506 maybe_add_class_template_decl_list (current_class_type,
5507 type, /*friend_p=*/0);
5508 /* Put this UTD in the table of UTDs for the class. */
5509 if (CLASSTYPE_NESTED_UTDS (current_class_type) == NULL)
5510 CLASSTYPE_NESTED_UTDS (current_class_type) =
5511 binding_table_new (SCOPE_DEFAULT_HT_SIZE);
5513 binding_table_insert
5514 (CLASSTYPE_NESTED_UTDS (current_class_type), name, type);
5520 return decl;
5523 /* Push a tag name NAME for struct/class/union/enum type TYPE. In case
5524 that the NAME is a class template, the tag is processed but not pushed.
5526 The pushed scope depend on the SCOPE parameter:
5527 - When SCOPE is TS_CURRENT, put it into the inner-most non-sk_cleanup
5528 scope.
5529 - When SCOPE is TS_GLOBAL, put it in the inner-most non-class and
5530 non-template-parameter scope. This case is needed for forward
5531 declarations.
5532 - When SCOPE is TS_WITHIN_ENCLOSING_NON_CLASS, this is similar to
5533 TS_GLOBAL case except that names within template-parameter scopes
5534 are not pushed at all.
5536 Returns TYPE upon success and ERROR_MARK_NODE otherwise. */
5538 static tree
5539 pushtag_1 (tree name, tree type, tag_scope scope)
5541 struct cp_binding_level *b;
5542 tree decl;
5544 b = current_binding_level;
5545 while (/* Cleanup scopes are not scopes from the point of view of
5546 the language. */
5547 b->kind == sk_cleanup
5548 /* Neither are function parameter scopes. */
5549 || b->kind == sk_function_parms
5550 /* Neither are the scopes used to hold template parameters
5551 for an explicit specialization. For an ordinary template
5552 declaration, these scopes are not scopes from the point of
5553 view of the language. */
5554 || (b->kind == sk_template_parms
5555 && (b->explicit_spec_p || scope == ts_global))
5556 || (b->kind == sk_class
5557 && (scope != ts_current
5558 /* We may be defining a new type in the initializer
5559 of a static member variable. We allow this when
5560 not pedantic, and it is particularly useful for
5561 type punning via an anonymous union. */
5562 || COMPLETE_TYPE_P (b->this_entity))))
5563 b = b->level_chain;
5565 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
5567 /* Do C++ gratuitous typedefing. */
5568 if (identifier_type_value_1 (name) != type)
5570 tree tdef;
5571 int in_class = 0;
5572 tree context = TYPE_CONTEXT (type);
5574 if (! context)
5576 tree cs = current_scope ();
5578 if (scope == ts_current
5579 || (cs && TREE_CODE (cs) == FUNCTION_DECL))
5580 context = cs;
5581 else if (cs != NULL_TREE && TYPE_P (cs))
5582 /* When declaring a friend class of a local class, we want
5583 to inject the newly named class into the scope
5584 containing the local class, not the namespace
5585 scope. */
5586 context = decl_function_context (get_type_decl (cs));
5588 if (!context)
5589 context = current_namespace;
5591 if (b->kind == sk_class
5592 || (b->kind == sk_template_parms
5593 && b->level_chain->kind == sk_class))
5594 in_class = 1;
5596 if (current_lang_name == lang_name_java)
5597 TYPE_FOR_JAVA (type) = 1;
5599 tdef = create_implicit_typedef (name, type);
5600 DECL_CONTEXT (tdef) = FROB_CONTEXT (context);
5601 if (scope == ts_within_enclosing_non_class)
5603 /* This is a friend. Make this TYPE_DECL node hidden from
5604 ordinary name lookup. Its corresponding TEMPLATE_DECL
5605 will be marked in push_template_decl_real. */
5606 retrofit_lang_decl (tdef);
5607 DECL_ANTICIPATED (tdef) = 1;
5608 DECL_FRIEND_P (tdef) = 1;
5611 decl = maybe_process_template_type_declaration
5612 (type, scope == ts_within_enclosing_non_class, b);
5613 if (decl == error_mark_node)
5614 return decl;
5616 if (b->kind == sk_class)
5618 if (!TYPE_BEING_DEFINED (current_class_type))
5619 return error_mark_node;
5621 if (!PROCESSING_REAL_TEMPLATE_DECL_P ())
5622 /* Put this TYPE_DECL on the TYPE_FIELDS list for the
5623 class. But if it's a member template class, we want
5624 the TEMPLATE_DECL, not the TYPE_DECL, so this is done
5625 later. */
5626 finish_member_declaration (decl);
5627 else
5628 pushdecl_class_level (decl);
5630 else if (b->kind != sk_template_parms)
5632 decl = pushdecl_with_scope (decl, b, /*is_friend=*/false);
5633 if (decl == error_mark_node)
5634 return decl;
5637 if (! in_class)
5638 set_identifier_type_value_with_scope (name, tdef, b);
5640 TYPE_CONTEXT (type) = DECL_CONTEXT (decl);
5642 /* If this is a local class, keep track of it. We need this
5643 information for name-mangling, and so that it is possible to
5644 find all function definitions in a translation unit in a
5645 convenient way. (It's otherwise tricky to find a member
5646 function definition it's only pointed to from within a local
5647 class.) */
5648 if (TYPE_CONTEXT (type)
5649 && TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL)
5650 VEC_safe_push (tree, gc, local_classes, type);
5652 if (b->kind == sk_class
5653 && !COMPLETE_TYPE_P (current_class_type))
5655 maybe_add_class_template_decl_list (current_class_type,
5656 type, /*friend_p=*/0);
5658 if (CLASSTYPE_NESTED_UTDS (current_class_type) == NULL)
5659 CLASSTYPE_NESTED_UTDS (current_class_type)
5660 = binding_table_new (SCOPE_DEFAULT_HT_SIZE);
5662 binding_table_insert
5663 (CLASSTYPE_NESTED_UTDS (current_class_type), name, type);
5666 decl = TYPE_NAME (type);
5667 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
5669 /* Set type visibility now if this is a forward declaration. */
5670 TREE_PUBLIC (decl) = 1;
5671 determine_visibility (decl);
5673 return type;
5676 /* Wrapper for pushtag_1. */
5678 tree
5679 pushtag (tree name, tree type, tag_scope scope)
5681 tree ret;
5682 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
5683 ret = pushtag_1 (name, type, scope);
5684 pph_catch_name_lookup (ret);
5685 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
5686 return ret;
5689 /* Subroutines for reverting temporarily to top-level for instantiation
5690 of templates and such. We actually need to clear out the class- and
5691 local-value slots of all identifiers, so that only the global values
5692 are at all visible. Simply setting current_binding_level to the global
5693 scope isn't enough, because more binding levels may be pushed. */
5694 struct saved_scope *scope_chain;
5696 /* If ID has not already been marked, add an appropriate binding to
5697 *OLD_BINDINGS. */
5699 static void
5700 store_binding (tree id, VEC(cxx_saved_binding,gc) **old_bindings)
5702 cxx_saved_binding *saved;
5704 if (!id || !IDENTIFIER_BINDING (id))
5705 return;
5707 if (IDENTIFIER_MARKED (id))
5708 return;
5710 IDENTIFIER_MARKED (id) = 1;
5712 saved = VEC_safe_push (cxx_saved_binding, gc, *old_bindings, NULL);
5713 saved->identifier = id;
5714 saved->binding = IDENTIFIER_BINDING (id);
5715 saved->real_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
5716 IDENTIFIER_BINDING (id) = NULL;
5719 static void
5720 store_bindings (tree names, VEC(cxx_saved_binding,gc) **old_bindings)
5722 tree t;
5724 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
5725 for (t = names; t; t = TREE_CHAIN (t))
5727 tree id;
5729 if (TREE_CODE (t) == TREE_LIST)
5730 id = TREE_PURPOSE (t);
5731 else
5732 id = DECL_NAME (t);
5734 store_binding (id, old_bindings);
5736 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
5739 /* Like store_bindings, but NAMES is a vector of cp_class_binding
5740 objects, rather than a TREE_LIST. */
5742 static void
5743 store_class_bindings (VEC(cp_class_binding,gc) *names,
5744 VEC(cxx_saved_binding,gc) **old_bindings)
5746 size_t i;
5747 cp_class_binding *cb;
5749 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
5750 for (i = 0; VEC_iterate(cp_class_binding, names, i, cb); ++i)
5751 store_binding (cb->identifier, old_bindings);
5752 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
5755 void
5756 push_to_top_level (void)
5758 struct saved_scope *s;
5759 struct cp_binding_level *b;
5760 cxx_saved_binding *sb;
5761 size_t i;
5762 bool need_pop;
5764 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
5765 s = ggc_alloc_cleared_saved_scope ();
5767 b = scope_chain ? current_binding_level : 0;
5769 /* If we're in the middle of some function, save our state. */
5770 if (cfun)
5772 need_pop = true;
5773 push_function_context ();
5775 else
5776 need_pop = false;
5778 if (scope_chain && previous_class_level)
5779 store_class_bindings (previous_class_level->class_shadowed,
5780 &s->old_bindings);
5782 /* Have to include the global scope, because class-scope decls
5783 aren't listed anywhere useful. */
5784 for (; b; b = b->level_chain)
5786 tree t;
5788 /* Template IDs are inserted into the global level. If they were
5789 inserted into namespace level, finish_file wouldn't find them
5790 when doing pending instantiations. Therefore, don't stop at
5791 namespace level, but continue until :: . */
5792 if (global_scope_p (b))
5793 break;
5795 store_bindings (b->names, &s->old_bindings);
5796 /* We also need to check class_shadowed to save class-level type
5797 bindings, since pushclass doesn't fill in b->names. */
5798 if (b->kind == sk_class)
5799 store_class_bindings (b->class_shadowed, &s->old_bindings);
5801 /* Unwind type-value slots back to top level. */
5802 for (t = b->type_shadowed; t; t = TREE_CHAIN (t))
5803 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (t), TREE_VALUE (t));
5806 FOR_EACH_VEC_ELT (cxx_saved_binding, s->old_bindings, i, sb)
5807 IDENTIFIER_MARKED (sb->identifier) = 0;
5809 s->prev = scope_chain;
5810 s->bindings = b;
5811 s->need_pop_function_context = need_pop;
5812 s->function_decl = current_function_decl;
5813 s->unevaluated_operand = cp_unevaluated_operand;
5814 s->inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
5816 scope_chain = s;
5817 current_function_decl = NULL_TREE;
5818 current_lang_base = VEC_alloc (tree, gc, 10);
5819 current_lang_name = lang_name_cplusplus;
5820 current_namespace = global_namespace;
5821 push_class_stack ();
5822 cp_unevaluated_operand = 0;
5823 c_inhibit_evaluation_warnings = 0;
5824 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
5827 static void
5828 pop_from_top_level_1 (void)
5830 struct saved_scope *s = scope_chain;
5831 cxx_saved_binding *saved;
5832 size_t i;
5834 /* Clear out class-level bindings cache. */
5835 if (previous_class_level)
5836 invalidate_class_lookup_cache ();
5837 pop_class_stack ();
5839 current_lang_base = 0;
5841 scope_chain = s->prev;
5842 FOR_EACH_VEC_ELT (cxx_saved_binding, s->old_bindings, i, saved)
5844 tree id = saved->identifier;
5846 IDENTIFIER_BINDING (id) = saved->binding;
5847 SET_IDENTIFIER_TYPE_VALUE (id, saved->real_type_value);
5850 /* If we were in the middle of compiling a function, restore our
5851 state. */
5852 if (s->need_pop_function_context)
5853 pop_function_context ();
5854 current_function_decl = s->function_decl;
5855 cp_unevaluated_operand = s->unevaluated_operand;
5856 c_inhibit_evaluation_warnings = s->inhibit_evaluation_warnings;
5859 /* Wrapper for pop_from_top_level_1. */
5861 void
5862 pop_from_top_level (void)
5864 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
5865 pop_from_top_level_1 ();
5866 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
5870 /* Pop off extraneous binding levels left over due to syntax errors.
5872 We don't pop past namespaces, as they might be valid. */
5874 void
5875 pop_everything (void)
5877 if (ENABLE_SCOPE_CHECKING)
5878 verbatim ("XXX entering pop_everything ()\n");
5879 while (!toplevel_bindings_p ())
5881 if (current_binding_level->kind == sk_class)
5882 pop_nested_class ();
5883 else
5884 poplevel (0, 0, 0);
5886 if (ENABLE_SCOPE_CHECKING)
5887 verbatim ("XXX leaving pop_everything ()\n");
5890 /* Emit debugging information for using declarations and directives.
5891 If input tree is overloaded fn then emit debug info for all
5892 candidates. */
5894 void
5895 cp_emit_debug_info_for_using (tree t, tree context)
5897 /* Don't try to emit any debug information if we have errors. */
5898 if (seen_error ())
5899 return;
5901 /* Ignore this FUNCTION_DECL if it refers to a builtin declaration
5902 of a builtin function. */
5903 if (TREE_CODE (t) == FUNCTION_DECL
5904 && DECL_EXTERNAL (t)
5905 && DECL_BUILT_IN (t))
5906 return;
5908 /* Do not supply context to imported_module_or_decl, if
5909 it is a global namespace. */
5910 if (context == global_namespace)
5911 context = NULL_TREE;
5913 if (BASELINK_P (t))
5914 t = BASELINK_FUNCTIONS (t);
5916 /* FIXME: Handle TEMPLATE_DECLs. */
5917 for (t = OVL_CURRENT (t); t; t = OVL_NEXT (t))
5918 if (TREE_CODE (t) != TEMPLATE_DECL)
5920 if (building_stmt_tree ())
5921 add_stmt (build_stmt (input_location, USING_STMT, t));
5922 else
5923 (*debug_hooks->imported_module_or_decl) (t, NULL_TREE, context, false);
5928 /* Write a binding_entry instance BT to STREAM. If REF_P is true, all
5929 tree nodes in the table are written as references. */
5931 void
5932 pph_stream_write_binding_table (pph_stream *stream, binding_table bt,
5933 bool ref_p)
5935 size_t i;
5937 pph_output_uint (stream, bt->chain_count);
5938 for (i = 0; i < bt->chain_count; i++)
5940 if (bt->chain[i])
5942 pph_output_uchar (stream, PPH_RECORD_START);
5943 pph_output_tree_or_ref (stream, bt->chain[i]->name, ref_p);
5944 pph_output_tree_or_ref (stream, bt->chain[i]->type, ref_p);
5946 else
5947 pph_output_uchar (stream, PPH_RECORD_END);
5949 pph_output_uint (stream, bt->entry_count);
5953 /* Read and return a binding_entry instance BT from STREAM. */
5955 binding_table
5956 pph_stream_read_binding_table (pph_stream *stream)
5958 size_t i, chain_count;
5959 binding_table bt;
5961 chain_count = pph_input_uint (stream);
5962 bt = binding_table_new (chain_count);
5963 for (i = 0; i < chain_count; i++)
5965 unsigned char record_marker = pph_input_uchar (stream);
5966 if (record_marker == PPH_RECORD_START)
5968 tree name = pph_input_tree (stream);
5969 tree type = pph_input_tree (stream);
5970 binding_table_insert (bt, name, type);
5973 bt->entry_count = pph_input_uint (stream);
5975 return bt;
5978 #include "gt-cp-name-lookup.h"