/
[official-gcc.git] / gcc / cp / name-lookup.c
blob4cf13809031dc27931f27876735fb9fc5e286288
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 "params.h"
37 /* The bindings for a particular name in a particular scope. */
39 struct scope_binding {
40 tree value;
41 tree type;
43 #define EMPTY_SCOPE_BINDING { NULL_TREE, NULL_TREE }
45 static cxx_scope *innermost_nonclass_level (void);
46 static cxx_binding *binding_for_name (cxx_scope *, tree);
47 static tree push_overloaded_decl (tree, int, bool);
48 static bool lookup_using_namespace (tree, struct scope_binding *, tree,
49 tree, int);
50 static bool qualified_lookup_using_namespace (tree, tree,
51 struct scope_binding *, int);
52 static tree lookup_type_current_level (tree);
53 static tree push_using_directive (tree);
54 static cxx_binding* lookup_extern_c_fun_binding_in_all_ns (tree);
56 /* The :: namespace. */
58 tree global_namespace;
60 /* The name of the anonymous namespace, throughout this translation
61 unit. */
62 static GTY(()) tree anonymous_namespace_name;
64 /* Initialize anonymous_namespace_name if necessary, and return it. */
66 static tree
67 get_anonymous_namespace_name (void)
69 if (!anonymous_namespace_name)
71 /* The anonymous namespace has to have a unique name
72 if typeinfo objects are being compared by name. */
73 if (! flag_weak || ! SUPPORTS_ONE_ONLY)
74 anonymous_namespace_name = get_file_function_name ("N");
75 else
76 /* The demangler expects anonymous namespaces to be called
77 something starting with '_GLOBAL__N_'. */
78 anonymous_namespace_name = get_identifier ("_GLOBAL__N_1");
80 return anonymous_namespace_name;
83 /* Compute the chain index of a binding_entry given the HASH value of its
84 name and the total COUNT of chains. COUNT is assumed to be a power
85 of 2. */
87 #define ENTRY_INDEX(HASH, COUNT) (((HASH) >> 3) & ((COUNT) - 1))
89 /* A free list of "binding_entry"s awaiting for re-use. */
91 static GTY((deletable)) binding_entry free_binding_entry = NULL;
93 /* Create a binding_entry object for (NAME, TYPE). */
95 static inline binding_entry
96 binding_entry_make (tree name, tree type)
98 binding_entry entry;
100 if (free_binding_entry)
102 entry = free_binding_entry;
103 free_binding_entry = entry->chain;
105 else
106 entry = ggc_alloc_binding_entry_s ();
108 entry->name = name;
109 entry->type = type;
110 entry->chain = NULL;
112 return entry;
115 /* Put ENTRY back on the free list. */
116 #if 0
117 static inline void
118 binding_entry_free (binding_entry entry)
120 entry->name = NULL;
121 entry->type = NULL;
122 entry->chain = free_binding_entry;
123 free_binding_entry = entry;
125 #endif
127 /* The datatype used to implement the mapping from names to types at
128 a given scope. */
129 struct GTY(()) binding_table_s {
130 /* Array of chains of "binding_entry"s */
131 binding_entry * GTY((length ("%h.chain_count"))) chain;
133 /* The number of chains in this table. This is the length of the
134 member "chain" considered as an array. */
135 size_t chain_count;
137 /* Number of "binding_entry"s in this table. */
138 size_t entry_count;
141 /* Construct TABLE with an initial CHAIN_COUNT. */
143 static inline void
144 binding_table_construct (binding_table table, size_t chain_count)
146 table->chain_count = chain_count;
147 table->entry_count = 0;
148 table->chain = ggc_alloc_cleared_vec_binding_entry (table->chain_count);
151 /* Make TABLE's entries ready for reuse. */
152 #if 0
153 static void
154 binding_table_free (binding_table table)
156 size_t i;
157 size_t count;
159 if (table == NULL)
160 return;
162 for (i = 0, count = table->chain_count; i < count; ++i)
164 binding_entry temp = table->chain[i];
165 while (temp != NULL)
167 binding_entry entry = temp;
168 temp = entry->chain;
169 binding_entry_free (entry);
171 table->chain[i] = NULL;
173 table->entry_count = 0;
175 #endif
177 /* Allocate a table with CHAIN_COUNT, assumed to be a power of two. */
179 static inline binding_table
180 binding_table_new (size_t chain_count)
182 binding_table table = ggc_alloc_binding_table_s ();
183 table->chain = NULL;
184 binding_table_construct (table, chain_count);
185 return table;
188 /* Expand TABLE to twice its current chain_count. */
190 static void
191 binding_table_expand (binding_table table)
193 const size_t old_chain_count = table->chain_count;
194 const size_t old_entry_count = table->entry_count;
195 const size_t new_chain_count = 2 * old_chain_count;
196 binding_entry *old_chains = table->chain;
197 size_t i;
199 binding_table_construct (table, new_chain_count);
200 for (i = 0; i < old_chain_count; ++i)
202 binding_entry entry = old_chains[i];
203 for (; entry != NULL; entry = old_chains[i])
205 const unsigned int hash = IDENTIFIER_HASH_VALUE (entry->name);
206 const size_t j = ENTRY_INDEX (hash, new_chain_count);
208 old_chains[i] = entry->chain;
209 entry->chain = table->chain[j];
210 table->chain[j] = entry;
213 table->entry_count = old_entry_count;
216 /* Insert a binding for NAME to TYPE into TABLE. */
218 static void
219 binding_table_insert (binding_table table, tree name, tree type)
221 const unsigned int hash = IDENTIFIER_HASH_VALUE (name);
222 const size_t i = ENTRY_INDEX (hash, table->chain_count);
223 binding_entry entry = binding_entry_make (name, type);
225 entry->chain = table->chain[i];
226 table->chain[i] = entry;
227 ++table->entry_count;
229 if (3 * table->chain_count < 5 * table->entry_count)
230 binding_table_expand (table);
233 /* Return the binding_entry, if any, that maps NAME. */
235 binding_entry
236 binding_table_find (binding_table table, tree name)
238 const unsigned int hash = IDENTIFIER_HASH_VALUE (name);
239 binding_entry entry = table->chain[ENTRY_INDEX (hash, table->chain_count)];
241 while (entry != NULL && entry->name != name)
242 entry = entry->chain;
244 return entry;
247 /* Apply PROC -- with DATA -- to all entries in TABLE. */
249 void
250 binding_table_foreach (binding_table table, bt_foreach_proc proc, void *data)
252 const size_t chain_count = table->chain_count;
253 size_t i;
255 for (i = 0; i < chain_count; ++i)
257 binding_entry entry = table->chain[i];
258 for (; entry != NULL; entry = entry->chain)
259 proc (entry, data);
263 #ifndef ENABLE_SCOPE_CHECKING
264 # define ENABLE_SCOPE_CHECKING 0
265 #else
266 # define ENABLE_SCOPE_CHECKING 1
267 #endif
269 /* A free list of "cxx_binding"s, connected by their PREVIOUS. */
271 static GTY((deletable)) cxx_binding *free_bindings;
273 /* Initialize VALUE and TYPE field for BINDING, and set the PREVIOUS
274 field to NULL. */
276 static inline void
277 cxx_binding_init (cxx_binding *binding, tree value, tree type)
279 binding->value = value;
280 binding->type = type;
281 binding->previous = NULL;
284 /* (GC)-allocate a binding object with VALUE and TYPE member initialized. */
286 static cxx_binding *
287 cxx_binding_make (tree value, tree type)
289 cxx_binding *binding;
290 if (free_bindings)
292 binding = free_bindings;
293 free_bindings = binding->previous;
295 else
296 binding = ggc_alloc_cxx_binding ();
298 cxx_binding_init (binding, value, type);
300 return binding;
303 /* Put BINDING back on the free list. */
305 static inline void
306 cxx_binding_free (cxx_binding *binding)
308 binding->scope = NULL;
309 binding->previous = free_bindings;
310 free_bindings = binding;
313 /* Create a new binding for NAME (with the indicated VALUE and TYPE
314 bindings) in the class scope indicated by SCOPE. */
316 static cxx_binding *
317 new_class_binding (tree name, tree value, tree type, cxx_scope *scope)
319 cp_class_binding *cb;
320 cxx_binding *binding;
322 if (VEC_length (cp_class_binding, scope->class_shadowed))
324 cp_class_binding *old_base;
325 old_base = VEC_index (cp_class_binding, scope->class_shadowed, 0);
326 if (VEC_reserve (cp_class_binding, gc, scope->class_shadowed, 1))
328 /* Fixup the current bindings, as they might have moved. */
329 size_t i;
331 FOR_EACH_VEC_ELT (cp_class_binding, scope->class_shadowed, i, cb)
333 cxx_binding **b;
334 b = &IDENTIFIER_BINDING (cb->identifier);
335 while (*b != &old_base[i].base)
336 b = &((*b)->previous);
337 *b = &cb->base;
340 cb = VEC_quick_push (cp_class_binding, scope->class_shadowed, NULL);
342 else
343 cb = VEC_safe_push (cp_class_binding, gc, scope->class_shadowed, NULL);
345 cb->identifier = name;
346 binding = &cb->base;
347 binding->scope = scope;
348 cxx_binding_init (binding, value, type);
349 return binding;
352 /* Make DECL the innermost binding for ID. The LEVEL is the binding
353 level at which this declaration is being bound. */
355 static void
356 push_binding (tree id, tree decl, cxx_scope* level)
358 cxx_binding *binding;
360 if (level != class_binding_level)
362 binding = cxx_binding_make (decl, NULL_TREE);
363 binding->scope = level;
365 else
366 binding = new_class_binding (id, decl, /*type=*/NULL_TREE, level);
368 /* Now, fill in the binding information. */
369 binding->previous = IDENTIFIER_BINDING (id);
370 INHERITED_VALUE_BINDING_P (binding) = 0;
371 LOCAL_BINDING_P (binding) = (level != class_binding_level);
373 /* And put it on the front of the list of bindings for ID. */
374 IDENTIFIER_BINDING (id) = binding;
377 /* Remove the binding for DECL which should be the innermost binding
378 for ID. */
380 void
381 pop_binding (tree id, tree decl)
383 cxx_binding *binding;
385 if (id == NULL_TREE)
386 /* It's easiest to write the loops that call this function without
387 checking whether or not the entities involved have names. We
388 get here for such an entity. */
389 return;
391 /* Get the innermost binding for ID. */
392 binding = IDENTIFIER_BINDING (id);
394 /* The name should be bound. */
395 gcc_assert (binding != NULL);
397 /* The DECL will be either the ordinary binding or the type
398 binding for this identifier. Remove that binding. */
399 if (binding->value == decl)
400 binding->value = NULL_TREE;
401 else
403 gcc_assert (binding->type == decl);
404 binding->type = NULL_TREE;
407 if (!binding->value && !binding->type)
409 /* We're completely done with the innermost binding for this
410 identifier. Unhook it from the list of bindings. */
411 IDENTIFIER_BINDING (id) = binding->previous;
413 /* Add it to the free list. */
414 cxx_binding_free (binding);
418 /* BINDING records an existing declaration for a name in the current scope.
419 But, DECL is another declaration for that same identifier in the
420 same scope. This is the `struct stat' hack whereby a non-typedef
421 class name or enum-name can be bound at the same level as some other
422 kind of entity.
423 3.3.7/1
425 A class name (9.1) or enumeration name (7.2) can be hidden by the
426 name of an object, function, or enumerator declared in the same scope.
427 If a class or enumeration name and an object, function, or enumerator
428 are declared in the same scope (in any order) with the same name, the
429 class or enumeration name is hidden wherever the object, function, or
430 enumerator name is visible.
432 It's the responsibility of the caller to check that
433 inserting this name is valid here. Returns nonzero if the new binding
434 was successful. */
436 static bool
437 supplement_binding (cxx_binding *binding, tree decl)
439 tree bval = binding->value;
440 bool ok = true;
442 timevar_push (TV_NAME_LOOKUP);
443 if (TREE_CODE (decl) == TYPE_DECL && DECL_ARTIFICIAL (decl))
444 /* The new name is the type name. */
445 binding->type = decl;
446 else if (/* BVAL is null when push_class_level_binding moves an
447 inherited type-binding out of the way to make room for a
448 new value binding. */
449 !bval
450 /* BVAL is error_mark_node when DECL's name has been used
451 in a non-class scope prior declaration. In that case,
452 we should have already issued a diagnostic; for graceful
453 error recovery purpose, pretend this was the intended
454 declaration for that name. */
455 || bval == error_mark_node
456 /* If BVAL is anticipated but has not yet been declared,
457 pretend it is not there at all. */
458 || (TREE_CODE (bval) == FUNCTION_DECL
459 && DECL_ANTICIPATED (bval)
460 && !DECL_HIDDEN_FRIEND_P (bval)))
461 binding->value = decl;
462 else if (TREE_CODE (bval) == TYPE_DECL && DECL_ARTIFICIAL (bval))
464 /* The old binding was a type name. It was placed in
465 VALUE field because it was thought, at the point it was
466 declared, to be the only entity with such a name. Move the
467 type name into the type slot; it is now hidden by the new
468 binding. */
469 binding->type = bval;
470 binding->value = decl;
471 binding->value_is_inherited = false;
473 else if (TREE_CODE (bval) == TYPE_DECL
474 && TREE_CODE (decl) == TYPE_DECL
475 && DECL_NAME (decl) == DECL_NAME (bval)
476 && binding->scope->kind != sk_class
477 && (same_type_p (TREE_TYPE (decl), TREE_TYPE (bval))
478 /* If either type involves template parameters, we must
479 wait until instantiation. */
480 || uses_template_parms (TREE_TYPE (decl))
481 || uses_template_parms (TREE_TYPE (bval))))
482 /* We have two typedef-names, both naming the same type to have
483 the same name. In general, this is OK because of:
485 [dcl.typedef]
487 In a given scope, a typedef specifier can be used to redefine
488 the name of any type declared in that scope to refer to the
489 type to which it already refers.
491 However, in class scopes, this rule does not apply due to the
492 stricter language in [class.mem] prohibiting redeclarations of
493 members. */
494 ok = false;
495 /* There can be two block-scope declarations of the same variable,
496 so long as they are `extern' declarations. However, there cannot
497 be two declarations of the same static data member:
499 [class.mem]
501 A member shall not be declared twice in the
502 member-specification. */
503 else if (TREE_CODE (decl) == VAR_DECL && TREE_CODE (bval) == VAR_DECL
504 && DECL_EXTERNAL (decl) && DECL_EXTERNAL (bval)
505 && !DECL_CLASS_SCOPE_P (decl))
507 duplicate_decls (decl, binding->value, /*newdecl_is_friend=*/false);
508 ok = false;
510 else if (TREE_CODE (decl) == NAMESPACE_DECL
511 && TREE_CODE (bval) == NAMESPACE_DECL
512 && DECL_NAMESPACE_ALIAS (decl)
513 && DECL_NAMESPACE_ALIAS (bval)
514 && ORIGINAL_NAMESPACE (bval) == ORIGINAL_NAMESPACE (decl))
515 /* [namespace.alias]
517 In a declarative region, a namespace-alias-definition can be
518 used to redefine a namespace-alias declared in that declarative
519 region to refer only to the namespace to which it already
520 refers. */
521 ok = false;
522 else
524 error ("declaration of %q#D", decl);
525 error ("conflicts with previous declaration %q+#D", bval);
526 ok = false;
529 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, ok);
532 /* Add DECL to the list of things declared in B. */
534 static void
535 add_decl_to_level (tree decl, cxx_scope *b)
537 /* We used to record virtual tables as if they were ordinary
538 variables, but no longer do so. */
539 gcc_assert (!(TREE_CODE (decl) == VAR_DECL && DECL_VIRTUAL_P (decl)));
541 if (TREE_CODE (decl) == NAMESPACE_DECL
542 && !DECL_NAMESPACE_ALIAS (decl))
544 DECL_CHAIN (decl) = b->namespaces;
545 b->namespaces = decl;
547 else
549 /* We build up the list in reverse order, and reverse it later if
550 necessary. */
551 TREE_CHAIN (decl) = b->names;
552 b->names = decl;
553 b->names_size++;
555 /* If appropriate, add decl to separate list of statics. We
556 include extern variables because they might turn out to be
557 static later. It's OK for this list to contain a few false
558 positives. */
559 if (b->kind == sk_namespace)
560 if ((TREE_CODE (decl) == VAR_DECL
561 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
562 || (TREE_CODE (decl) == FUNCTION_DECL
563 && (!TREE_PUBLIC (decl) || DECL_DECLARED_INLINE_P (decl))))
564 VEC_safe_push (tree, gc, b->static_decls, decl);
568 /* Record a decl-node X as belonging to the current lexical scope.
569 Check for errors (such as an incompatible declaration for the same
570 name already seen in the same scope). IS_FRIEND is true if X is
571 declared as a friend.
573 Returns either X or an old decl for the same name.
574 If an old decl is returned, it may have been smashed
575 to agree with what X says. */
577 tree
578 pushdecl_maybe_friend (tree x, bool is_friend)
580 tree t;
581 tree name;
582 int need_new_binding;
584 timevar_push (TV_NAME_LOOKUP);
586 if (x == error_mark_node)
587 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
589 need_new_binding = 1;
591 if (DECL_TEMPLATE_PARM_P (x))
592 /* Template parameters have no context; they are not X::T even
593 when declared within a class or namespace. */
595 else
597 if (current_function_decl && x != current_function_decl
598 /* A local declaration for a function doesn't constitute
599 nesting. */
600 && TREE_CODE (x) != FUNCTION_DECL
601 /* A local declaration for an `extern' variable is in the
602 scope of the current namespace, not the current
603 function. */
604 && !(TREE_CODE (x) == VAR_DECL && DECL_EXTERNAL (x))
605 /* When parsing the parameter list of a function declarator,
606 don't set DECL_CONTEXT to an enclosing function. When we
607 push the PARM_DECLs in order to process the function body,
608 current_binding_level->this_entity will be set. */
609 && !(TREE_CODE (x) == PARM_DECL
610 && current_binding_level->kind == sk_function_parms
611 && current_binding_level->this_entity == NULL)
612 && !DECL_CONTEXT (x))
613 DECL_CONTEXT (x) = current_function_decl;
615 /* If this is the declaration for a namespace-scope function,
616 but the declaration itself is in a local scope, mark the
617 declaration. */
618 if (TREE_CODE (x) == FUNCTION_DECL
619 && DECL_NAMESPACE_SCOPE_P (x)
620 && current_function_decl
621 && x != current_function_decl)
622 DECL_LOCAL_FUNCTION_P (x) = 1;
625 name = DECL_NAME (x);
626 if (name)
628 int different_binding_level = 0;
630 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
631 name = TREE_OPERAND (name, 0);
633 /* In case this decl was explicitly namespace-qualified, look it
634 up in its namespace context. */
635 if (DECL_NAMESPACE_SCOPE_P (x) && namespace_bindings_p ())
636 t = namespace_binding (name, DECL_CONTEXT (x));
637 else
638 t = lookup_name_innermost_nonclass_level (name);
640 /* [basic.link] If there is a visible declaration of an entity
641 with linkage having the same name and type, ignoring entities
642 declared outside the innermost enclosing namespace scope, the
643 block scope declaration declares that same entity and
644 receives the linkage of the previous declaration. */
645 if (! t && current_function_decl && x != current_function_decl
646 && (TREE_CODE (x) == FUNCTION_DECL || TREE_CODE (x) == VAR_DECL)
647 && DECL_EXTERNAL (x))
649 /* Look in block scope. */
650 t = innermost_non_namespace_value (name);
651 /* Or in the innermost namespace. */
652 if (! t)
653 t = namespace_binding (name, DECL_CONTEXT (x));
654 /* Does it have linkage? Note that if this isn't a DECL, it's an
655 OVERLOAD, which is OK. */
656 if (t && DECL_P (t) && ! (TREE_STATIC (t) || DECL_EXTERNAL (t)))
657 t = NULL_TREE;
658 if (t)
659 different_binding_level = 1;
662 /* If we are declaring a function, and the result of name-lookup
663 was an OVERLOAD, look for an overloaded instance that is
664 actually the same as the function we are declaring. (If
665 there is one, we have to merge our declaration with the
666 previous declaration.) */
667 if (t && TREE_CODE (t) == OVERLOAD)
669 tree match;
671 if (TREE_CODE (x) == FUNCTION_DECL)
672 for (match = t; match; match = OVL_NEXT (match))
674 if (decls_match (OVL_CURRENT (match), x))
675 break;
677 else
678 /* Just choose one. */
679 match = t;
681 if (match)
682 t = OVL_CURRENT (match);
683 else
684 t = NULL_TREE;
687 if (t && t != error_mark_node)
689 if (different_binding_level)
691 if (decls_match (x, t))
692 /* The standard only says that the local extern
693 inherits linkage from the previous decl; in
694 particular, default args are not shared. Add
695 the decl into a hash table to make sure only
696 the previous decl in this case is seen by the
697 middle end. */
699 struct cxx_int_tree_map *h;
700 void **loc;
702 TREE_PUBLIC (x) = TREE_PUBLIC (t);
704 if (cp_function_chain->extern_decl_map == NULL)
705 cp_function_chain->extern_decl_map
706 = htab_create_ggc (20, cxx_int_tree_map_hash,
707 cxx_int_tree_map_eq, NULL);
709 h = ggc_alloc_cxx_int_tree_map ();
710 h->uid = DECL_UID (x);
711 h->to = t;
712 loc = htab_find_slot_with_hash
713 (cp_function_chain->extern_decl_map, h,
714 h->uid, INSERT);
715 *(struct cxx_int_tree_map **) loc = h;
718 else if (TREE_CODE (t) == PARM_DECL)
720 /* Check for duplicate params. */
721 tree d = duplicate_decls (x, t, is_friend);
722 if (d)
723 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, d);
725 else if ((DECL_EXTERN_C_FUNCTION_P (x)
726 || DECL_FUNCTION_TEMPLATE_P (x))
727 && is_overloaded_fn (t))
728 /* Don't do anything just yet. */;
729 else if (t == wchar_decl_node)
731 if (! DECL_IN_SYSTEM_HEADER (x))
732 pedwarn (input_location, OPT_pedantic, "redeclaration of %<wchar_t%> as %qT",
733 TREE_TYPE (x));
735 /* Throw away the redeclaration. */
736 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
738 else
740 tree olddecl = duplicate_decls (x, t, is_friend);
742 /* If the redeclaration failed, we can stop at this
743 point. */
744 if (olddecl == error_mark_node)
745 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
747 if (olddecl)
749 if (TREE_CODE (t) == TYPE_DECL)
750 SET_IDENTIFIER_TYPE_VALUE (name, TREE_TYPE (t));
752 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
754 else if (DECL_MAIN_P (x) && TREE_CODE (t) == FUNCTION_DECL)
756 /* A redeclaration of main, but not a duplicate of the
757 previous one.
759 [basic.start.main]
761 This function shall not be overloaded. */
762 error ("invalid redeclaration of %q+D", t);
763 error ("as %qD", x);
764 /* We don't try to push this declaration since that
765 causes a crash. */
766 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
771 /* If x has C linkage-specification, (extern "C"),
772 lookup its binding, in case it's already bound to an object.
773 The lookup is done in all namespaces.
774 If we find an existing binding, make sure it has the same
775 exception specification as x, otherwise, bail in error [7.5, 7.6]. */
776 if ((TREE_CODE (x) == FUNCTION_DECL)
777 && DECL_EXTERN_C_P (x)
778 /* We should ignore declarations happening in system headers. */
779 && !DECL_ARTIFICIAL (x)
780 && !DECL_IN_SYSTEM_HEADER (x))
782 cxx_binding *function_binding =
783 lookup_extern_c_fun_binding_in_all_ns (x);
784 tree previous = (function_binding
785 ? function_binding->value
786 : NULL_TREE);
787 if (previous
788 && !DECL_ARTIFICIAL (previous)
789 && !DECL_IN_SYSTEM_HEADER (previous)
790 && DECL_CONTEXT (previous) != DECL_CONTEXT (x))
792 tree previous = function_binding->value;
794 /* In case either x or previous is declared to throw an exception,
795 make sure both exception specifications are equal. */
796 if (decls_match (x, previous))
798 tree x_exception_spec = NULL_TREE;
799 tree previous_exception_spec = NULL_TREE;
801 x_exception_spec =
802 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (x));
803 previous_exception_spec =
804 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (previous));
805 if (!comp_except_specs (previous_exception_spec,
806 x_exception_spec,
807 ce_normal))
809 pedwarn (input_location, 0, "declaration of %q#D with C language linkage",
811 pedwarn (input_location, 0, "conflicts with previous declaration %q+#D",
812 previous);
813 pedwarn (input_location, 0, "due to different exception specifications");
814 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
817 else
819 pedwarn (input_location, 0,
820 "declaration of %q#D with C language linkage", x);
821 pedwarn (input_location, 0,
822 "conflicts with previous declaration %q+#D",
823 previous);
828 check_template_shadow (x);
830 /* If this is a function conjured up by the back end, massage it
831 so it looks friendly. */
832 if (DECL_NON_THUNK_FUNCTION_P (x) && ! DECL_LANG_SPECIFIC (x))
834 retrofit_lang_decl (x);
835 SET_DECL_LANGUAGE (x, lang_c);
838 t = x;
839 if (DECL_NON_THUNK_FUNCTION_P (x) && ! DECL_FUNCTION_MEMBER_P (x))
841 t = push_overloaded_decl (x, PUSH_LOCAL, is_friend);
842 if (!namespace_bindings_p ())
843 /* We do not need to create a binding for this name;
844 push_overloaded_decl will have already done so if
845 necessary. */
846 need_new_binding = 0;
848 else if (DECL_FUNCTION_TEMPLATE_P (x) && DECL_NAMESPACE_SCOPE_P (x))
850 t = push_overloaded_decl (x, PUSH_GLOBAL, is_friend);
851 if (t == x)
852 add_decl_to_level (x, NAMESPACE_LEVEL (CP_DECL_CONTEXT (t)));
855 if (TREE_CODE (t) == FUNCTION_DECL || DECL_FUNCTION_TEMPLATE_P (t))
856 check_default_args (t);
858 if (t != x || DECL_FUNCTION_TEMPLATE_P (t))
859 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
861 /* If declaring a type as a typedef, copy the type (unless we're
862 at line 0), and install this TYPE_DECL as the new type's typedef
863 name. See the extensive comment of set_underlying_type (). */
864 if (TREE_CODE (x) == TYPE_DECL)
866 tree type = TREE_TYPE (x);
868 if (DECL_IS_BUILTIN (x)
869 || (TREE_TYPE (x) != error_mark_node
870 && TYPE_NAME (type) != x
871 /* We don't want to copy the type when all we're
872 doing is making a TYPE_DECL for the purposes of
873 inlining. */
874 && (!TYPE_NAME (type)
875 || TYPE_NAME (type) != DECL_ABSTRACT_ORIGIN (x))))
876 set_underlying_type (x);
878 if (type != error_mark_node
879 && TYPE_NAME (type)
880 && TYPE_IDENTIFIER (type))
881 set_identifier_type_value (DECL_NAME (x), x);
884 /* Multiple external decls of the same identifier ought to match.
886 We get warnings about inline functions where they are defined.
887 We get warnings about other functions from push_overloaded_decl.
889 Avoid duplicate warnings where they are used. */
890 if (TREE_PUBLIC (x) && TREE_CODE (x) != FUNCTION_DECL)
892 tree decl;
894 decl = IDENTIFIER_NAMESPACE_VALUE (name);
895 if (decl && TREE_CODE (decl) == OVERLOAD)
896 decl = OVL_FUNCTION (decl);
898 if (decl && decl != error_mark_node
899 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl))
900 /* If different sort of thing, we already gave an error. */
901 && TREE_CODE (decl) == TREE_CODE (x)
902 && !same_type_p (TREE_TYPE (x), TREE_TYPE (decl)))
904 permerror (input_location, "type mismatch with previous external decl of %q#D", x);
905 permerror (input_location, "previous external decl of %q+#D", decl);
909 if (TREE_CODE (x) == FUNCTION_DECL
910 && is_friend
911 && !flag_friend_injection)
913 /* This is a new declaration of a friend function, so hide
914 it from ordinary function lookup. */
915 DECL_ANTICIPATED (x) = 1;
916 DECL_HIDDEN_FRIEND_P (x) = 1;
919 /* This name is new in its binding level.
920 Install the new declaration and return it. */
921 if (namespace_bindings_p ())
923 /* Install a global value. */
925 /* If the first global decl has external linkage,
926 warn if we later see static one. */
927 if (IDENTIFIER_GLOBAL_VALUE (name) == NULL_TREE && TREE_PUBLIC (x))
928 TREE_PUBLIC (name) = 1;
930 /* Bind the name for the entity. */
931 if (!(TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x)
932 && t != NULL_TREE)
933 && (TREE_CODE (x) == TYPE_DECL
934 || TREE_CODE (x) == VAR_DECL
935 || TREE_CODE (x) == NAMESPACE_DECL
936 || TREE_CODE (x) == CONST_DECL
937 || TREE_CODE (x) == TEMPLATE_DECL))
938 SET_IDENTIFIER_NAMESPACE_VALUE (name, x);
940 /* If new decl is `static' and an `extern' was seen previously,
941 warn about it. */
942 if (x != NULL_TREE && t != NULL_TREE && decls_match (x, t))
943 warn_extern_redeclared_static (x, t);
945 else
947 /* Here to install a non-global value. */
948 tree oldlocal = innermost_non_namespace_value (name);
949 tree oldglobal = IDENTIFIER_NAMESPACE_VALUE (name);
951 if (need_new_binding)
953 push_local_binding (name, x, 0);
954 /* Because push_local_binding will hook X on to the
955 current_binding_level's name list, we don't want to
956 do that again below. */
957 need_new_binding = 0;
960 /* If this is a TYPE_DECL, push it into the type value slot. */
961 if (TREE_CODE (x) == TYPE_DECL)
962 set_identifier_type_value (name, x);
964 /* Clear out any TYPE_DECL shadowed by a namespace so that
965 we won't think this is a type. The C struct hack doesn't
966 go through namespaces. */
967 if (TREE_CODE (x) == NAMESPACE_DECL)
968 set_identifier_type_value (name, NULL_TREE);
970 if (oldlocal)
972 tree d = oldlocal;
974 while (oldlocal
975 && TREE_CODE (oldlocal) == VAR_DECL
976 && DECL_DEAD_FOR_LOCAL (oldlocal))
977 oldlocal = DECL_SHADOWED_FOR_VAR (oldlocal);
979 if (oldlocal == NULL_TREE)
980 oldlocal = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (d));
983 /* If this is an extern function declaration, see if we
984 have a global definition or declaration for the function. */
985 if (oldlocal == NULL_TREE
986 && DECL_EXTERNAL (x)
987 && oldglobal != NULL_TREE
988 && TREE_CODE (x) == FUNCTION_DECL
989 && TREE_CODE (oldglobal) == FUNCTION_DECL)
991 /* We have one. Their types must agree. */
992 if (decls_match (x, oldglobal))
993 /* OK */;
994 else
996 warning (0, "extern declaration of %q#D doesn%'t match", x);
997 warning (0, "global declaration %q+#D", oldglobal);
1000 /* If we have a local external declaration,
1001 and no file-scope declaration has yet been seen,
1002 then if we later have a file-scope decl it must not be static. */
1003 if (oldlocal == NULL_TREE
1004 && oldglobal == NULL_TREE
1005 && DECL_EXTERNAL (x)
1006 && TREE_PUBLIC (x))
1007 TREE_PUBLIC (name) = 1;
1009 /* Don't complain about the parms we push and then pop
1010 while tentatively parsing a function declarator. */
1011 if (TREE_CODE (x) == PARM_DECL && DECL_CONTEXT (x) == NULL_TREE)
1012 /* Ignore. */;
1014 /* Warn if shadowing an argument at the top level of the body. */
1015 else if (oldlocal != NULL_TREE && !DECL_EXTERNAL (x)
1016 /* Inline decls shadow nothing. */
1017 && !DECL_FROM_INLINE (x)
1018 && (TREE_CODE (oldlocal) == PARM_DECL
1019 || TREE_CODE (oldlocal) == VAR_DECL
1020 /* If the old decl is a type decl, only warn if the
1021 old decl is an explicit typedef or if both the old
1022 and new decls are type decls. */
1023 || (TREE_CODE (oldlocal) == TYPE_DECL
1024 && (!DECL_ARTIFICIAL (oldlocal)
1025 || TREE_CODE (x) == TYPE_DECL)))
1026 /* Don't check the `this' parameter or internally generated
1027 vars unless it's an implicit typedef (see
1028 create_implicit_typedef in decl.c). */
1029 && (!DECL_ARTIFICIAL (oldlocal)
1030 || DECL_IMPLICIT_TYPEDEF_P (oldlocal))
1031 /* Don't check for internally generated vars unless
1032 it's an implicit typedef (see create_implicit_typedef
1033 in decl.c). */
1034 && (!DECL_ARTIFICIAL (x) || DECL_IMPLICIT_TYPEDEF_P (x)))
1036 bool nowarn = false;
1038 /* Don't complain if it's from an enclosing function. */
1039 if (DECL_CONTEXT (oldlocal) == current_function_decl
1040 && TREE_CODE (x) != PARM_DECL
1041 && TREE_CODE (oldlocal) == PARM_DECL)
1043 /* Go to where the parms should be and see if we find
1044 them there. */
1045 struct cp_binding_level *b = current_binding_level->level_chain;
1047 if (FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
1048 /* Skip the ctor/dtor cleanup level. */
1049 b = b->level_chain;
1051 /* ARM $8.3 */
1052 if (b->kind == sk_function_parms)
1054 error ("declaration of %q#D shadows a parameter", x);
1055 nowarn = true;
1059 /* The local structure or class can't use parameters of
1060 the containing function anyway. */
1061 if (DECL_CONTEXT (oldlocal) != current_function_decl)
1063 cxx_scope *scope = current_binding_level;
1064 tree context = DECL_CONTEXT (oldlocal);
1065 for (; scope; scope = scope->level_chain)
1067 if (scope->kind == sk_function_parms
1068 && scope->this_entity == context)
1069 break;
1070 if (scope->kind == sk_class
1071 && !LAMBDA_TYPE_P (scope->this_entity))
1073 nowarn = true;
1074 break;
1079 if (warn_shadow && !nowarn)
1081 if (TREE_CODE (oldlocal) == PARM_DECL)
1082 warning_at (input_location, OPT_Wshadow,
1083 "declaration of %q#D shadows a parameter", x);
1084 else
1085 warning_at (input_location, OPT_Wshadow,
1086 "declaration of %qD shadows a previous local",
1088 warning_at (DECL_SOURCE_LOCATION (oldlocal), OPT_Wshadow,
1089 "shadowed declaration is here");
1093 /* Maybe warn if shadowing something else. */
1094 else if (warn_shadow && !DECL_EXTERNAL (x)
1095 /* No shadow warnings for internally generated vars unless
1096 it's an implicit typedef (see create_implicit_typedef
1097 in decl.c). */
1098 && (! DECL_ARTIFICIAL (x) || DECL_IMPLICIT_TYPEDEF_P (x))
1099 /* No shadow warnings for vars made for inlining. */
1100 && ! DECL_FROM_INLINE (x))
1102 tree member;
1104 if (current_class_ptr)
1105 member = lookup_member (current_class_type,
1106 name,
1107 /*protect=*/0,
1108 /*want_type=*/false);
1109 else
1110 member = NULL_TREE;
1112 if (member && !TREE_STATIC (member))
1114 /* Location of previous decl is not useful in this case. */
1115 warning (OPT_Wshadow, "declaration of %qD shadows a member of 'this'",
1118 else if (oldglobal != NULL_TREE
1119 && (TREE_CODE (oldglobal) == VAR_DECL
1120 /* If the old decl is a type decl, only warn if the
1121 old decl is an explicit typedef or if both the
1122 old and new decls are type decls. */
1123 || (TREE_CODE (oldglobal) == TYPE_DECL
1124 && (!DECL_ARTIFICIAL (oldglobal)
1125 || TREE_CODE (x) == TYPE_DECL))))
1126 /* XXX shadow warnings in outer-more namespaces */
1128 warning_at (input_location, OPT_Wshadow,
1129 "declaration of %qD shadows a global declaration", x);
1130 warning_at (DECL_SOURCE_LOCATION (oldglobal), OPT_Wshadow,
1131 "shadowed declaration is here");
1136 if (TREE_CODE (x) == VAR_DECL)
1137 maybe_register_incomplete_var (x);
1140 if (need_new_binding)
1141 add_decl_to_level (x,
1142 DECL_NAMESPACE_SCOPE_P (x)
1143 ? NAMESPACE_LEVEL (CP_DECL_CONTEXT (x))
1144 : current_binding_level);
1146 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
1149 /* Record a decl-node X as belonging to the current lexical scope. */
1151 tree
1152 pushdecl (tree x)
1154 return pushdecl_maybe_friend (x, false);
1157 /* Enter DECL into the symbol table, if that's appropriate. Returns
1158 DECL, or a modified version thereof. */
1160 tree
1161 maybe_push_decl (tree decl)
1163 tree type = TREE_TYPE (decl);
1165 /* Add this decl to the current binding level, but not if it comes
1166 from another scope, e.g. a static member variable. TEM may equal
1167 DECL or it may be a previous decl of the same name. */
1168 if (decl == error_mark_node
1169 || (TREE_CODE (decl) != PARM_DECL
1170 && DECL_CONTEXT (decl) != NULL_TREE
1171 /* Definitions of namespace members outside their namespace are
1172 possible. */
1173 && !DECL_NAMESPACE_SCOPE_P (decl))
1174 || (TREE_CODE (decl) == TEMPLATE_DECL && !namespace_bindings_p ())
1175 || type == unknown_type_node
1176 /* The declaration of a template specialization does not affect
1177 the functions available for overload resolution, so we do not
1178 call pushdecl. */
1179 || (TREE_CODE (decl) == FUNCTION_DECL
1180 && DECL_TEMPLATE_SPECIALIZATION (decl)))
1181 return decl;
1182 else
1183 return pushdecl (decl);
1186 /* Bind DECL to ID in the current_binding_level, assumed to be a local
1187 binding level. If PUSH_USING is set in FLAGS, we know that DECL
1188 doesn't really belong to this binding level, that it got here
1189 through a using-declaration. */
1191 void
1192 push_local_binding (tree id, tree decl, int flags)
1194 struct cp_binding_level *b;
1196 /* Skip over any local classes. This makes sense if we call
1197 push_local_binding with a friend decl of a local class. */
1198 b = innermost_nonclass_level ();
1200 if (lookup_name_innermost_nonclass_level (id))
1202 /* Supplement the existing binding. */
1203 if (!supplement_binding (IDENTIFIER_BINDING (id), decl))
1204 /* It didn't work. Something else must be bound at this
1205 level. Do not add DECL to the list of things to pop
1206 later. */
1207 return;
1209 else
1210 /* Create a new binding. */
1211 push_binding (id, decl, b);
1213 if (TREE_CODE (decl) == OVERLOAD || (flags & PUSH_USING))
1214 /* We must put the OVERLOAD into a TREE_LIST since the
1215 TREE_CHAIN of an OVERLOAD is already used. Similarly for
1216 decls that got here through a using-declaration. */
1217 decl = build_tree_list (NULL_TREE, decl);
1219 /* And put DECL on the list of things declared by the current
1220 binding level. */
1221 add_decl_to_level (decl, b);
1224 /* Check to see whether or not DECL is a variable that would have been
1225 in scope under the ARM, but is not in scope under the ANSI/ISO
1226 standard. If so, issue an error message. If name lookup would
1227 work in both cases, but return a different result, this function
1228 returns the result of ANSI/ISO lookup. Otherwise, it returns
1229 DECL. */
1231 tree
1232 check_for_out_of_scope_variable (tree decl)
1234 tree shadowed;
1236 /* We only care about out of scope variables. */
1237 if (!(TREE_CODE (decl) == VAR_DECL && DECL_DEAD_FOR_LOCAL (decl)))
1238 return decl;
1240 shadowed = DECL_HAS_SHADOWED_FOR_VAR_P (decl)
1241 ? DECL_SHADOWED_FOR_VAR (decl) : NULL_TREE ;
1242 while (shadowed != NULL_TREE && TREE_CODE (shadowed) == VAR_DECL
1243 && DECL_DEAD_FOR_LOCAL (shadowed))
1244 shadowed = DECL_HAS_SHADOWED_FOR_VAR_P (shadowed)
1245 ? DECL_SHADOWED_FOR_VAR (shadowed) : NULL_TREE;
1246 if (!shadowed)
1247 shadowed = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (decl));
1248 if (shadowed)
1250 if (!DECL_ERROR_REPORTED (decl))
1252 warning (0, "name lookup of %qD changed", DECL_NAME (decl));
1253 warning (0, " matches this %q+D under ISO standard rules",
1254 shadowed);
1255 warning (0, " matches this %q+D under old rules", decl);
1256 DECL_ERROR_REPORTED (decl) = 1;
1258 return shadowed;
1261 /* If we have already complained about this declaration, there's no
1262 need to do it again. */
1263 if (DECL_ERROR_REPORTED (decl))
1264 return decl;
1266 DECL_ERROR_REPORTED (decl) = 1;
1268 if (TREE_TYPE (decl) == error_mark_node)
1269 return decl;
1271 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
1273 error ("name lookup of %qD changed for ISO %<for%> scoping",
1274 DECL_NAME (decl));
1275 error (" cannot use obsolete binding at %q+D because "
1276 "it has a destructor", decl);
1277 return error_mark_node;
1279 else
1281 permerror (input_location, "name lookup of %qD changed for ISO %<for%> scoping",
1282 DECL_NAME (decl));
1283 if (flag_permissive)
1284 permerror (input_location, " using obsolete binding at %q+D", decl);
1285 else
1287 static bool hint;
1288 if (!hint)
1290 inform (input_location, "(if you use %<-fpermissive%> G++ will accept your code)");
1291 hint = true;
1296 return decl;
1299 /* true means unconditionally make a BLOCK for the next level pushed. */
1301 static bool keep_next_level_flag;
1303 static int binding_depth = 0;
1305 static void
1306 indent (int depth)
1308 int i;
1310 for (i = 0; i < depth * 2; i++)
1311 putc (' ', stderr);
1314 /* Return a string describing the kind of SCOPE we have. */
1315 static const char *
1316 cxx_scope_descriptor (cxx_scope *scope)
1318 /* The order of this table must match the "scope_kind"
1319 enumerators. */
1320 static const char* scope_kind_names[] = {
1321 "block-scope",
1322 "cleanup-scope",
1323 "try-scope",
1324 "catch-scope",
1325 "for-scope",
1326 "function-parameter-scope",
1327 "class-scope",
1328 "namespace-scope",
1329 "template-parameter-scope",
1330 "template-explicit-spec-scope"
1332 const scope_kind kind = scope->explicit_spec_p
1333 ? sk_template_spec : scope->kind;
1335 return scope_kind_names[kind];
1338 /* Output a debugging information about SCOPE when performing
1339 ACTION at LINE. */
1340 static void
1341 cxx_scope_debug (cxx_scope *scope, int line, const char *action)
1343 const char *desc = cxx_scope_descriptor (scope);
1344 if (scope->this_entity)
1345 verbatim ("%s %s(%E) %p %d\n", action, desc,
1346 scope->this_entity, (void *) scope, line);
1347 else
1348 verbatim ("%s %s %p %d\n", action, desc, (void *) scope, line);
1351 /* Return the estimated initial size of the hashtable of a NAMESPACE
1352 scope. */
1354 static inline size_t
1355 namespace_scope_ht_size (tree ns)
1357 tree name = DECL_NAME (ns);
1359 return name == std_identifier
1360 ? NAMESPACE_STD_HT_SIZE
1361 : (name == global_scope_name
1362 ? GLOBAL_SCOPE_HT_SIZE
1363 : NAMESPACE_ORDINARY_HT_SIZE);
1366 /* A chain of binding_level structures awaiting reuse. */
1368 static GTY((deletable)) struct cp_binding_level *free_binding_level;
1370 /* Insert SCOPE as the innermost binding level. */
1372 void
1373 push_binding_level (struct cp_binding_level *scope)
1375 /* Add it to the front of currently active scopes stack. */
1376 scope->level_chain = current_binding_level;
1377 current_binding_level = scope;
1378 keep_next_level_flag = false;
1380 if (ENABLE_SCOPE_CHECKING)
1382 scope->binding_depth = binding_depth;
1383 indent (binding_depth);
1384 cxx_scope_debug (scope, input_line, "push");
1385 binding_depth++;
1389 /* Create a new KIND scope and make it the top of the active scopes stack.
1390 ENTITY is the scope of the associated C++ entity (namespace, class,
1391 function, C++0x enumeration); it is NULL otherwise. */
1393 cxx_scope *
1394 begin_scope (scope_kind kind, tree entity)
1396 cxx_scope *scope;
1398 /* Reuse or create a struct for this binding level. */
1399 if (!ENABLE_SCOPE_CHECKING && free_binding_level)
1401 scope = free_binding_level;
1402 memset (scope, 0, sizeof (cxx_scope));
1403 free_binding_level = scope->level_chain;
1405 else
1406 scope = ggc_alloc_cleared_cxx_scope ();
1408 scope->this_entity = entity;
1409 scope->more_cleanups_ok = true;
1410 switch (kind)
1412 case sk_cleanup:
1413 scope->keep = true;
1414 break;
1416 case sk_template_spec:
1417 scope->explicit_spec_p = true;
1418 kind = sk_template_parms;
1419 /* Fall through. */
1420 case sk_template_parms:
1421 case sk_block:
1422 case sk_try:
1423 case sk_catch:
1424 case sk_for:
1425 case sk_class:
1426 case sk_scoped_enum:
1427 case sk_function_parms:
1428 case sk_omp:
1429 scope->keep = keep_next_level_flag;
1430 break;
1432 case sk_namespace:
1433 NAMESPACE_LEVEL (entity) = scope;
1434 scope->static_decls =
1435 VEC_alloc (tree, gc,
1436 DECL_NAME (entity) == std_identifier
1437 || DECL_NAME (entity) == global_scope_name
1438 ? 200 : 10);
1439 break;
1441 default:
1442 /* Should not happen. */
1443 gcc_unreachable ();
1444 break;
1446 scope->kind = kind;
1448 push_binding_level (scope);
1450 return scope;
1453 /* We're about to leave current scope. Pop the top of the stack of
1454 currently active scopes. Return the enclosing scope, now active. */
1456 cxx_scope *
1457 leave_scope (void)
1459 cxx_scope *scope = current_binding_level;
1461 if (scope->kind == sk_namespace && class_binding_level)
1462 current_binding_level = class_binding_level;
1464 /* We cannot leave a scope, if there are none left. */
1465 if (NAMESPACE_LEVEL (global_namespace))
1466 gcc_assert (!global_scope_p (scope));
1468 if (ENABLE_SCOPE_CHECKING)
1470 indent (--binding_depth);
1471 cxx_scope_debug (scope, input_line, "leave");
1474 /* Move one nesting level up. */
1475 current_binding_level = scope->level_chain;
1477 /* Namespace-scopes are left most probably temporarily, not
1478 completely; they can be reopened later, e.g. in namespace-extension
1479 or any name binding activity that requires us to resume a
1480 namespace. For classes, we cache some binding levels. For other
1481 scopes, we just make the structure available for reuse. */
1482 if (scope->kind != sk_namespace
1483 && scope->kind != sk_class)
1485 scope->level_chain = free_binding_level;
1486 gcc_assert (!ENABLE_SCOPE_CHECKING
1487 || scope->binding_depth == binding_depth);
1488 free_binding_level = scope;
1491 /* Find the innermost enclosing class scope, and reset
1492 CLASS_BINDING_LEVEL appropriately. */
1493 if (scope->kind == sk_class)
1495 class_binding_level = NULL;
1496 for (scope = current_binding_level; scope; scope = scope->level_chain)
1497 if (scope->kind == sk_class)
1499 class_binding_level = scope;
1500 break;
1504 return current_binding_level;
1507 static void
1508 resume_scope (struct cp_binding_level* b)
1510 /* Resuming binding levels is meant only for namespaces,
1511 and those cannot nest into classes. */
1512 gcc_assert (!class_binding_level);
1513 /* Also, resuming a non-directly nested namespace is a no-no. */
1514 gcc_assert (b->level_chain == current_binding_level);
1515 current_binding_level = b;
1516 if (ENABLE_SCOPE_CHECKING)
1518 b->binding_depth = binding_depth;
1519 indent (binding_depth);
1520 cxx_scope_debug (b, input_line, "resume");
1521 binding_depth++;
1525 /* Return the innermost binding level that is not for a class scope. */
1527 static cxx_scope *
1528 innermost_nonclass_level (void)
1530 cxx_scope *b;
1532 b = current_binding_level;
1533 while (b->kind == sk_class)
1534 b = b->level_chain;
1536 return b;
1539 /* We're defining an object of type TYPE. If it needs a cleanup, but
1540 we're not allowed to add any more objects with cleanups to the current
1541 scope, create a new binding level. */
1543 void
1544 maybe_push_cleanup_level (tree type)
1546 if (type != error_mark_node
1547 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
1548 && current_binding_level->more_cleanups_ok == 0)
1550 begin_scope (sk_cleanup, NULL);
1551 current_binding_level->statement_list = push_stmt_list ();
1555 /* Nonzero if we are currently in the global binding level. */
1558 global_bindings_p (void)
1560 return global_scope_p (current_binding_level);
1563 /* True if we are currently in a toplevel binding level. This
1564 means either the global binding level or a namespace in a toplevel
1565 binding level. Since there are no non-toplevel namespace levels,
1566 this really means any namespace or template parameter level. We
1567 also include a class whose context is toplevel. */
1569 bool
1570 toplevel_bindings_p (void)
1572 struct cp_binding_level *b = innermost_nonclass_level ();
1574 return b->kind == sk_namespace || b->kind == sk_template_parms;
1577 /* True if this is a namespace scope, or if we are defining a class
1578 which is itself at namespace scope, or whose enclosing class is
1579 such a class, etc. */
1581 bool
1582 namespace_bindings_p (void)
1584 struct cp_binding_level *b = innermost_nonclass_level ();
1586 return b->kind == sk_namespace;
1589 /* True if the current level needs to have a BLOCK made. */
1591 bool
1592 kept_level_p (void)
1594 return (current_binding_level->blocks != NULL_TREE
1595 || current_binding_level->keep
1596 || current_binding_level->kind == sk_cleanup
1597 || current_binding_level->names != NULL_TREE
1598 || current_binding_level->using_directives);
1601 /* Returns the kind of the innermost scope. */
1603 scope_kind
1604 innermost_scope_kind (void)
1606 return current_binding_level->kind;
1609 /* Returns true if this scope was created to store template parameters. */
1611 bool
1612 template_parm_scope_p (void)
1614 return innermost_scope_kind () == sk_template_parms;
1617 /* If KEEP is true, make a BLOCK node for the next binding level,
1618 unconditionally. Otherwise, use the normal logic to decide whether
1619 or not to create a BLOCK. */
1621 void
1622 keep_next_level (bool keep)
1624 keep_next_level_flag = keep;
1627 /* Return the list of declarations of the current level.
1628 Note that this list is in reverse order unless/until
1629 you nreverse it; and when you do nreverse it, you must
1630 store the result back using `storedecls' or you will lose. */
1632 tree
1633 getdecls (void)
1635 return current_binding_level->names;
1638 /* For debugging. */
1639 static int no_print_functions = 0;
1640 static int no_print_builtins = 0;
1642 static void
1643 print_binding_level (struct cp_binding_level* lvl)
1645 tree t;
1646 int i = 0, len;
1647 fprintf (stderr, " blocks=%p", (void *) lvl->blocks);
1648 if (lvl->more_cleanups_ok)
1649 fprintf (stderr, " more-cleanups-ok");
1650 if (lvl->have_cleanups)
1651 fprintf (stderr, " have-cleanups");
1652 fprintf (stderr, "\n");
1653 if (lvl->names)
1655 fprintf (stderr, " names:\t");
1656 /* We can probably fit 3 names to a line? */
1657 for (t = lvl->names; t; t = TREE_CHAIN (t))
1659 if (no_print_functions && (TREE_CODE (t) == FUNCTION_DECL))
1660 continue;
1661 if (no_print_builtins
1662 && (TREE_CODE (t) == TYPE_DECL)
1663 && DECL_IS_BUILTIN (t))
1664 continue;
1666 /* Function decls tend to have longer names. */
1667 if (TREE_CODE (t) == FUNCTION_DECL)
1668 len = 3;
1669 else
1670 len = 2;
1671 i += len;
1672 if (i > 6)
1674 fprintf (stderr, "\n\t");
1675 i = len;
1677 print_node_brief (stderr, "", t, 0);
1678 if (t == error_mark_node)
1679 break;
1681 if (i)
1682 fprintf (stderr, "\n");
1684 if (VEC_length (cp_class_binding, lvl->class_shadowed))
1686 size_t i;
1687 cp_class_binding *b;
1688 fprintf (stderr, " class-shadowed:");
1689 FOR_EACH_VEC_ELT (cp_class_binding, lvl->class_shadowed, i, b)
1690 fprintf (stderr, " %s ", IDENTIFIER_POINTER (b->identifier));
1691 fprintf (stderr, "\n");
1693 if (lvl->type_shadowed)
1695 fprintf (stderr, " type-shadowed:");
1696 for (t = lvl->type_shadowed; t; t = TREE_CHAIN (t))
1698 fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
1700 fprintf (stderr, "\n");
1704 void
1705 print_other_binding_stack (struct cp_binding_level *stack)
1707 struct cp_binding_level *level;
1708 for (level = stack; !global_scope_p (level); level = level->level_chain)
1710 fprintf (stderr, "binding level %p\n", (void *) level);
1711 print_binding_level (level);
1715 void
1716 print_binding_stack (void)
1718 struct cp_binding_level *b;
1719 fprintf (stderr, "current_binding_level=%p\n"
1720 "class_binding_level=%p\n"
1721 "NAMESPACE_LEVEL (global_namespace)=%p\n",
1722 (void *) current_binding_level, (void *) class_binding_level,
1723 (void *) NAMESPACE_LEVEL (global_namespace));
1724 if (class_binding_level)
1726 for (b = class_binding_level; b; b = b->level_chain)
1727 if (b == current_binding_level)
1728 break;
1729 if (b)
1730 b = class_binding_level;
1731 else
1732 b = current_binding_level;
1734 else
1735 b = current_binding_level;
1736 print_other_binding_stack (b);
1737 fprintf (stderr, "global:\n");
1738 print_binding_level (NAMESPACE_LEVEL (global_namespace));
1741 /* Return the type associated with id. */
1743 tree
1744 identifier_type_value (tree id)
1746 timevar_push (TV_NAME_LOOKUP);
1747 /* There is no type with that name, anywhere. */
1748 if (REAL_IDENTIFIER_TYPE_VALUE (id) == NULL_TREE)
1749 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
1750 /* This is not the type marker, but the real thing. */
1751 if (REAL_IDENTIFIER_TYPE_VALUE (id) != global_type_node)
1752 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, REAL_IDENTIFIER_TYPE_VALUE (id));
1753 /* Have to search for it. It must be on the global level, now.
1754 Ask lookup_name not to return non-types. */
1755 id = lookup_name_real (id, 2, 1, /*block_p=*/true, 0, LOOKUP_COMPLAIN);
1756 if (id)
1757 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, TREE_TYPE (id));
1758 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
1761 /* Return the IDENTIFIER_GLOBAL_VALUE of T, for use in common code, since
1762 the definition of IDENTIFIER_GLOBAL_VALUE is different for C and C++. */
1764 tree
1765 identifier_global_value (tree t)
1767 return IDENTIFIER_GLOBAL_VALUE (t);
1770 /* Push a definition of struct, union or enum tag named ID. into
1771 binding_level B. DECL is a TYPE_DECL for the type. We assume that
1772 the tag ID is not already defined. */
1774 static void
1775 set_identifier_type_value_with_scope (tree id, tree decl, cxx_scope *b)
1777 tree type;
1779 if (b->kind != sk_namespace)
1781 /* Shadow the marker, not the real thing, so that the marker
1782 gets restored later. */
1783 tree old_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
1784 b->type_shadowed
1785 = tree_cons (id, old_type_value, b->type_shadowed);
1786 type = decl ? TREE_TYPE (decl) : NULL_TREE;
1787 TREE_TYPE (b->type_shadowed) = type;
1789 else
1791 cxx_binding *binding =
1792 binding_for_name (NAMESPACE_LEVEL (current_namespace), id);
1793 gcc_assert (decl);
1794 if (binding->value)
1795 supplement_binding (binding, decl);
1796 else
1797 binding->value = decl;
1799 /* Store marker instead of real type. */
1800 type = global_type_node;
1802 SET_IDENTIFIER_TYPE_VALUE (id, type);
1805 /* As set_identifier_type_value_with_scope, but using
1806 current_binding_level. */
1808 void
1809 set_identifier_type_value (tree id, tree decl)
1811 set_identifier_type_value_with_scope (id, decl, current_binding_level);
1814 /* Return the name for the constructor (or destructor) for the
1815 specified class TYPE. When given a template, this routine doesn't
1816 lose the specialization. */
1818 static inline tree
1819 constructor_name_full (tree type)
1821 return TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
1824 /* Return the name for the constructor (or destructor) for the
1825 specified class. When given a template, return the plain
1826 unspecialized name. */
1828 tree
1829 constructor_name (tree type)
1831 tree name;
1832 name = constructor_name_full (type);
1833 if (IDENTIFIER_TEMPLATE (name))
1834 name = IDENTIFIER_TEMPLATE (name);
1835 return name;
1838 /* Returns TRUE if NAME is the name for the constructor for TYPE,
1839 which must be a class type. */
1841 bool
1842 constructor_name_p (tree name, tree type)
1844 tree ctor_name;
1846 gcc_assert (MAYBE_CLASS_TYPE_P (type));
1848 if (!name)
1849 return false;
1851 if (TREE_CODE (name) != IDENTIFIER_NODE)
1852 return false;
1854 ctor_name = constructor_name_full (type);
1855 if (name == ctor_name)
1856 return true;
1857 if (IDENTIFIER_TEMPLATE (ctor_name)
1858 && name == IDENTIFIER_TEMPLATE (ctor_name))
1859 return true;
1860 return false;
1863 /* Counter used to create anonymous type names. */
1865 static GTY(()) int anon_cnt;
1867 /* Return an IDENTIFIER which can be used as a name for
1868 anonymous structs and unions. */
1870 tree
1871 make_anon_name (void)
1873 char buf[32];
1875 sprintf (buf, ANON_AGGRNAME_FORMAT, anon_cnt++);
1876 return get_identifier (buf);
1879 /* This code is practically identical to that for creating
1880 anonymous names, but is just used for lambdas instead. This is necessary
1881 because anonymous names are recognized and cannot be passed to template
1882 functions. */
1883 /* FIXME is this still necessary? */
1885 static GTY(()) int lambda_cnt = 0;
1887 tree
1888 make_lambda_name (void)
1890 char buf[32];
1892 sprintf (buf, LAMBDANAME_FORMAT, lambda_cnt++);
1893 return get_identifier (buf);
1896 /* Return (from the stack of) the BINDING, if any, established at SCOPE. */
1898 static inline cxx_binding *
1899 find_binding (cxx_scope *scope, cxx_binding *binding)
1901 timevar_push (TV_NAME_LOOKUP);
1903 for (; binding != NULL; binding = binding->previous)
1904 if (binding->scope == scope)
1905 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, binding);
1907 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, (cxx_binding *)0);
1910 /* Return the binding for NAME in SCOPE, if any. Otherwise, return NULL. */
1912 static inline cxx_binding *
1913 cxx_scope_find_binding_for_name (cxx_scope *scope, tree name)
1915 cxx_binding *b = IDENTIFIER_NAMESPACE_BINDINGS (name);
1916 if (b)
1918 /* Fold-in case where NAME is used only once. */
1919 if (scope == b->scope && b->previous == NULL)
1920 return b;
1921 return find_binding (scope, b);
1923 return NULL;
1926 /* Always returns a binding for name in scope. If no binding is
1927 found, make a new one. */
1929 static cxx_binding *
1930 binding_for_name (cxx_scope *scope, tree name)
1932 cxx_binding *result;
1934 result = cxx_scope_find_binding_for_name (scope, name);
1935 if (result)
1936 return result;
1937 /* Not found, make a new one. */
1938 result = cxx_binding_make (NULL, NULL);
1939 result->previous = IDENTIFIER_NAMESPACE_BINDINGS (name);
1940 result->scope = scope;
1941 result->is_local = false;
1942 result->value_is_inherited = false;
1943 IDENTIFIER_NAMESPACE_BINDINGS (name) = result;
1944 return result;
1947 /* Walk through the bindings associated to the name of FUNCTION,
1948 and return the first binding that declares a function with a
1949 "C" linkage specification, a.k.a 'extern "C"'.
1950 This function looks for the binding, regardless of which scope it
1951 has been defined in. It basically looks in all the known scopes.
1952 Note that this function does not lookup for bindings of builtin functions
1953 or for functions declared in system headers. */
1954 static cxx_binding*
1955 lookup_extern_c_fun_binding_in_all_ns (tree function)
1957 tree name;
1958 cxx_binding *iter;
1960 gcc_assert (function && TREE_CODE (function) == FUNCTION_DECL);
1962 name = DECL_NAME (function);
1963 gcc_assert (name && TREE_CODE (name) == IDENTIFIER_NODE);
1965 for (iter = IDENTIFIER_NAMESPACE_BINDINGS (name);
1966 iter;
1967 iter = iter->previous)
1969 if (iter->value
1970 && TREE_CODE (iter->value) == FUNCTION_DECL
1971 && DECL_EXTERN_C_P (iter->value)
1972 && !DECL_ARTIFICIAL (iter->value))
1974 return iter;
1977 return NULL;
1980 /* Insert another USING_DECL into the current binding level, returning
1981 this declaration. If this is a redeclaration, do nothing, and
1982 return NULL_TREE if this not in namespace scope (in namespace
1983 scope, a using decl might extend any previous bindings). */
1985 static tree
1986 push_using_decl (tree scope, tree name)
1988 tree decl;
1990 timevar_push (TV_NAME_LOOKUP);
1991 gcc_assert (TREE_CODE (scope) == NAMESPACE_DECL);
1992 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
1993 for (decl = current_binding_level->usings; decl; decl = DECL_CHAIN (decl))
1994 if (USING_DECL_SCOPE (decl) == scope && DECL_NAME (decl) == name)
1995 break;
1996 if (decl)
1997 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP,
1998 namespace_bindings_p () ? decl : NULL_TREE);
1999 decl = build_lang_decl (USING_DECL, name, NULL_TREE);
2000 USING_DECL_SCOPE (decl) = scope;
2001 DECL_CHAIN (decl) = current_binding_level->usings;
2002 current_binding_level->usings = decl;
2003 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
2006 /* Same as pushdecl, but define X in binding-level LEVEL. We rely on the
2007 caller to set DECL_CONTEXT properly. */
2009 tree
2010 pushdecl_with_scope (tree x, cxx_scope *level, bool is_friend)
2012 struct cp_binding_level *b;
2013 tree function_decl = current_function_decl;
2015 timevar_push (TV_NAME_LOOKUP);
2016 current_function_decl = NULL_TREE;
2017 if (level->kind == sk_class)
2019 b = class_binding_level;
2020 class_binding_level = level;
2021 pushdecl_class_level (x);
2022 class_binding_level = b;
2024 else
2026 b = current_binding_level;
2027 current_binding_level = level;
2028 x = pushdecl_maybe_friend (x, is_friend);
2029 current_binding_level = b;
2031 current_function_decl = function_decl;
2032 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
2035 /* DECL is a FUNCTION_DECL for a non-member function, which may have
2036 other definitions already in place. We get around this by making
2037 the value of the identifier point to a list of all the things that
2038 want to be referenced by that name. It is then up to the users of
2039 that name to decide what to do with that list.
2041 DECL may also be a TEMPLATE_DECL, with a FUNCTION_DECL in its
2042 DECL_TEMPLATE_RESULT. It is dealt with the same way.
2044 FLAGS is a bitwise-or of the following values:
2045 PUSH_LOCAL: Bind DECL in the current scope, rather than at
2046 namespace scope.
2047 PUSH_USING: DECL is being pushed as the result of a using
2048 declaration.
2050 IS_FRIEND is true if this is a friend declaration.
2052 The value returned may be a previous declaration if we guessed wrong
2053 about what language DECL should belong to (C or C++). Otherwise,
2054 it's always DECL (and never something that's not a _DECL). */
2056 static tree
2057 push_overloaded_decl (tree decl, int flags, bool is_friend)
2059 tree name = DECL_NAME (decl);
2060 tree old;
2061 tree new_binding;
2062 int doing_global = (namespace_bindings_p () || !(flags & PUSH_LOCAL));
2064 timevar_push (TV_NAME_LOOKUP);
2065 if (doing_global)
2066 old = namespace_binding (name, DECL_CONTEXT (decl));
2067 else
2068 old = lookup_name_innermost_nonclass_level (name);
2070 if (old)
2072 if (TREE_CODE (old) == TYPE_DECL && DECL_ARTIFICIAL (old))
2074 tree t = TREE_TYPE (old);
2075 if (MAYBE_CLASS_TYPE_P (t) && warn_shadow
2076 && (! DECL_IN_SYSTEM_HEADER (decl)
2077 || ! DECL_IN_SYSTEM_HEADER (old)))
2078 warning (OPT_Wshadow, "%q#D hides constructor for %q#T", decl, t);
2079 old = NULL_TREE;
2081 else if (is_overloaded_fn (old))
2083 tree tmp;
2085 for (tmp = old; tmp; tmp = OVL_NEXT (tmp))
2087 tree fn = OVL_CURRENT (tmp);
2088 tree dup;
2090 if (TREE_CODE (tmp) == OVERLOAD && OVL_USED (tmp)
2091 && !(flags & PUSH_USING)
2092 && compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
2093 TYPE_ARG_TYPES (TREE_TYPE (decl)))
2094 && ! decls_match (fn, decl))
2095 error ("%q#D conflicts with previous using declaration %q#D",
2096 decl, fn);
2098 dup = duplicate_decls (decl, fn, is_friend);
2099 /* If DECL was a redeclaration of FN -- even an invalid
2100 one -- pass that information along to our caller. */
2101 if (dup == fn || dup == error_mark_node)
2102 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, dup);
2105 /* We don't overload implicit built-ins. duplicate_decls()
2106 may fail to merge the decls if the new decl is e.g. a
2107 template function. */
2108 if (TREE_CODE (old) == FUNCTION_DECL
2109 && DECL_ANTICIPATED (old)
2110 && !DECL_HIDDEN_FRIEND_P (old))
2111 old = NULL;
2113 else if (old == error_mark_node)
2114 /* Ignore the undefined symbol marker. */
2115 old = NULL_TREE;
2116 else
2118 error ("previous non-function declaration %q+#D", old);
2119 error ("conflicts with function declaration %q#D", decl);
2120 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
2124 if (old || TREE_CODE (decl) == TEMPLATE_DECL
2125 /* If it's a using declaration, we always need to build an OVERLOAD,
2126 because it's the only way to remember that the declaration comes
2127 from 'using', and have the lookup behave correctly. */
2128 || (flags & PUSH_USING))
2130 if (old && TREE_CODE (old) != OVERLOAD)
2131 new_binding = ovl_cons (decl, ovl_cons (old, NULL_TREE));
2132 else
2133 new_binding = ovl_cons (decl, old);
2134 if (flags & PUSH_USING)
2135 OVL_USED (new_binding) = 1;
2137 else
2138 /* NAME is not ambiguous. */
2139 new_binding = decl;
2141 if (doing_global)
2142 set_namespace_binding (name, current_namespace, new_binding);
2143 else
2145 /* We only create an OVERLOAD if there was a previous binding at
2146 this level, or if decl is a template. In the former case, we
2147 need to remove the old binding and replace it with the new
2148 binding. We must also run through the NAMES on the binding
2149 level where the name was bound to update the chain. */
2151 if (TREE_CODE (new_binding) == OVERLOAD && old)
2153 tree *d;
2155 for (d = &IDENTIFIER_BINDING (name)->scope->names;
2157 d = &TREE_CHAIN (*d))
2158 if (*d == old
2159 || (TREE_CODE (*d) == TREE_LIST
2160 && TREE_VALUE (*d) == old))
2162 if (TREE_CODE (*d) == TREE_LIST)
2163 /* Just replace the old binding with the new. */
2164 TREE_VALUE (*d) = new_binding;
2165 else
2166 /* Build a TREE_LIST to wrap the OVERLOAD. */
2167 *d = tree_cons (NULL_TREE, new_binding,
2168 TREE_CHAIN (*d));
2170 /* And update the cxx_binding node. */
2171 IDENTIFIER_BINDING (name)->value = new_binding;
2172 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
2175 /* We should always find a previous binding in this case. */
2176 gcc_unreachable ();
2179 /* Install the new binding. */
2180 push_local_binding (name, new_binding, flags);
2183 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
2186 /* Check a non-member using-declaration. Return the name and scope
2187 being used, and the USING_DECL, or NULL_TREE on failure. */
2189 static tree
2190 validate_nonmember_using_decl (tree decl, tree scope, tree name)
2192 /* [namespace.udecl]
2193 A using-declaration for a class member shall be a
2194 member-declaration. */
2195 if (TYPE_P (scope))
2197 error ("%qT is not a namespace", scope);
2198 return NULL_TREE;
2200 else if (scope == error_mark_node)
2201 return NULL_TREE;
2203 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR)
2205 /* 7.3.3/5
2206 A using-declaration shall not name a template-id. */
2207 error ("a using-declaration cannot specify a template-id. "
2208 "Try %<using %D%>", name);
2209 return NULL_TREE;
2212 if (TREE_CODE (decl) == NAMESPACE_DECL)
2214 error ("namespace %qD not allowed in using-declaration", decl);
2215 return NULL_TREE;
2218 if (TREE_CODE (decl) == SCOPE_REF)
2220 /* It's a nested name with template parameter dependent scope.
2221 This can only be using-declaration for class member. */
2222 error ("%qT is not a namespace", TREE_OPERAND (decl, 0));
2223 return NULL_TREE;
2226 if (is_overloaded_fn (decl))
2227 decl = get_first_fn (decl);
2229 gcc_assert (DECL_P (decl));
2231 /* Make a USING_DECL. */
2232 return push_using_decl (scope, name);
2235 /* Process local and global using-declarations. */
2237 static void
2238 do_nonmember_using_decl (tree scope, tree name, tree oldval, tree oldtype,
2239 tree *newval, tree *newtype)
2241 struct scope_binding decls = EMPTY_SCOPE_BINDING;
2243 *newval = *newtype = NULL_TREE;
2244 if (!qualified_lookup_using_namespace (name, scope, &decls, 0))
2245 /* Lookup error */
2246 return;
2248 if (!decls.value && !decls.type)
2250 error ("%qD not declared", name);
2251 return;
2254 /* Shift the old and new bindings around so we're comparing class and
2255 enumeration names to each other. */
2256 if (oldval && DECL_IMPLICIT_TYPEDEF_P (oldval))
2258 oldtype = oldval;
2259 oldval = NULL_TREE;
2262 if (decls.value && DECL_IMPLICIT_TYPEDEF_P (decls.value))
2264 decls.type = decls.value;
2265 decls.value = NULL_TREE;
2268 /* It is impossible to overload a built-in function; any explicit
2269 declaration eliminates the built-in declaration. So, if OLDVAL
2270 is a built-in, then we can just pretend it isn't there. */
2271 if (oldval
2272 && TREE_CODE (oldval) == FUNCTION_DECL
2273 && DECL_ANTICIPATED (oldval)
2274 && !DECL_HIDDEN_FRIEND_P (oldval))
2275 oldval = NULL_TREE;
2277 if (decls.value)
2279 /* Check for using functions. */
2280 if (is_overloaded_fn (decls.value))
2282 tree tmp, tmp1;
2284 if (oldval && !is_overloaded_fn (oldval))
2286 error ("%qD is already declared in this scope", name);
2287 oldval = NULL_TREE;
2290 *newval = oldval;
2291 for (tmp = decls.value; tmp; tmp = OVL_NEXT (tmp))
2293 tree new_fn = OVL_CURRENT (tmp);
2295 /* [namespace.udecl]
2297 If a function declaration in namespace scope or block
2298 scope has the same name and the same parameter types as a
2299 function introduced by a using declaration the program is
2300 ill-formed. */
2301 for (tmp1 = oldval; tmp1; tmp1 = OVL_NEXT (tmp1))
2303 tree old_fn = OVL_CURRENT (tmp1);
2305 if (new_fn == old_fn)
2306 /* The function already exists in the current namespace. */
2307 break;
2308 else if (OVL_USED (tmp1))
2309 continue; /* this is a using decl */
2310 else if (compparms (TYPE_ARG_TYPES (TREE_TYPE (new_fn)),
2311 TYPE_ARG_TYPES (TREE_TYPE (old_fn))))
2313 gcc_assert (!DECL_ANTICIPATED (old_fn)
2314 || DECL_HIDDEN_FRIEND_P (old_fn));
2316 /* There was already a non-using declaration in
2317 this scope with the same parameter types. If both
2318 are the same extern "C" functions, that's ok. */
2319 if (decls_match (new_fn, old_fn))
2320 break;
2321 else
2323 error ("%qD is already declared in this scope", name);
2324 break;
2329 /* If we broke out of the loop, there's no reason to add
2330 this function to the using declarations for this
2331 scope. */
2332 if (tmp1)
2333 continue;
2335 /* If we are adding to an existing OVERLOAD, then we no
2336 longer know the type of the set of functions. */
2337 if (*newval && TREE_CODE (*newval) == OVERLOAD)
2338 TREE_TYPE (*newval) = unknown_type_node;
2339 /* Add this new function to the set. */
2340 *newval = build_overload (OVL_CURRENT (tmp), *newval);
2341 /* If there is only one function, then we use its type. (A
2342 using-declaration naming a single function can be used in
2343 contexts where overload resolution cannot be
2344 performed.) */
2345 if (TREE_CODE (*newval) != OVERLOAD)
2347 *newval = ovl_cons (*newval, NULL_TREE);
2348 TREE_TYPE (*newval) = TREE_TYPE (OVL_CURRENT (tmp));
2350 OVL_USED (*newval) = 1;
2353 else
2355 *newval = decls.value;
2356 if (oldval && !decls_match (*newval, oldval))
2357 error ("%qD is already declared in this scope", name);
2360 else
2361 *newval = oldval;
2363 if (decls.type && TREE_CODE (decls.type) == TREE_LIST)
2365 error ("reference to %qD is ambiguous", name);
2366 print_candidates (decls.type);
2368 else
2370 *newtype = decls.type;
2371 if (oldtype && *newtype && !decls_match (oldtype, *newtype))
2372 error ("%qD is already declared in this scope", name);
2375 /* If *newval is empty, shift any class or enumeration name down. */
2376 if (!*newval)
2378 *newval = *newtype;
2379 *newtype = NULL_TREE;
2383 /* Process a using-declaration at function scope. */
2385 void
2386 do_local_using_decl (tree decl, tree scope, tree name)
2388 tree oldval, oldtype, newval, newtype;
2389 tree orig_decl = decl;
2391 decl = validate_nonmember_using_decl (decl, scope, name);
2392 if (decl == NULL_TREE)
2393 return;
2395 if (building_stmt_tree ()
2396 && at_function_scope_p ())
2397 add_decl_expr (decl);
2399 oldval = lookup_name_innermost_nonclass_level (name);
2400 oldtype = lookup_type_current_level (name);
2402 do_nonmember_using_decl (scope, name, oldval, oldtype, &newval, &newtype);
2404 if (newval)
2406 if (is_overloaded_fn (newval))
2408 tree fn, term;
2410 /* We only need to push declarations for those functions
2411 that were not already bound in the current level.
2412 The old value might be NULL_TREE, it might be a single
2413 function, or an OVERLOAD. */
2414 if (oldval && TREE_CODE (oldval) == OVERLOAD)
2415 term = OVL_FUNCTION (oldval);
2416 else
2417 term = oldval;
2418 for (fn = newval; fn && OVL_CURRENT (fn) != term;
2419 fn = OVL_NEXT (fn))
2420 push_overloaded_decl (OVL_CURRENT (fn),
2421 PUSH_LOCAL | PUSH_USING,
2422 false);
2424 else
2425 push_local_binding (name, newval, PUSH_USING);
2427 if (newtype)
2429 push_local_binding (name, newtype, PUSH_USING);
2430 set_identifier_type_value (name, newtype);
2433 /* Emit debug info. */
2434 if (!processing_template_decl)
2435 cp_emit_debug_info_for_using (orig_decl, current_scope());
2438 /* Returns true if ROOT (a namespace, class, or function) encloses
2439 CHILD. CHILD may be either a class type or a namespace. */
2441 bool
2442 is_ancestor (tree root, tree child)
2444 gcc_assert ((TREE_CODE (root) == NAMESPACE_DECL
2445 || TREE_CODE (root) == FUNCTION_DECL
2446 || CLASS_TYPE_P (root)));
2447 gcc_assert ((TREE_CODE (child) == NAMESPACE_DECL
2448 || CLASS_TYPE_P (child)));
2450 /* The global namespace encloses everything. */
2451 if (root == global_namespace)
2452 return true;
2454 while (true)
2456 /* If we've run out of scopes, stop. */
2457 if (!child)
2458 return false;
2459 /* If we've reached the ROOT, it encloses CHILD. */
2460 if (root == child)
2461 return true;
2462 /* Go out one level. */
2463 if (TYPE_P (child))
2464 child = TYPE_NAME (child);
2465 child = DECL_CONTEXT (child);
2469 /* Enter the class or namespace scope indicated by T suitable for name
2470 lookup. T can be arbitrary scope, not necessary nested inside the
2471 current scope. Returns a non-null scope to pop iff pop_scope
2472 should be called later to exit this scope. */
2474 tree
2475 push_scope (tree t)
2477 if (TREE_CODE (t) == NAMESPACE_DECL)
2478 push_decl_namespace (t);
2479 else if (CLASS_TYPE_P (t))
2481 if (!at_class_scope_p ()
2482 || !same_type_p (current_class_type, t))
2483 push_nested_class (t);
2484 else
2485 /* T is the same as the current scope. There is therefore no
2486 need to re-enter the scope. Since we are not actually
2487 pushing a new scope, our caller should not call
2488 pop_scope. */
2489 t = NULL_TREE;
2492 return t;
2495 /* Leave scope pushed by push_scope. */
2497 void
2498 pop_scope (tree t)
2500 if (t == NULL_TREE)
2501 return;
2502 if (TREE_CODE (t) == NAMESPACE_DECL)
2503 pop_decl_namespace ();
2504 else if CLASS_TYPE_P (t)
2505 pop_nested_class ();
2508 /* Subroutine of push_inner_scope. */
2510 static void
2511 push_inner_scope_r (tree outer, tree inner)
2513 tree prev;
2515 if (outer == inner
2516 || (TREE_CODE (inner) != NAMESPACE_DECL && !CLASS_TYPE_P (inner)))
2517 return;
2519 prev = CP_DECL_CONTEXT (TREE_CODE (inner) == NAMESPACE_DECL ? inner : TYPE_NAME (inner));
2520 if (outer != prev)
2521 push_inner_scope_r (outer, prev);
2522 if (TREE_CODE (inner) == NAMESPACE_DECL)
2524 struct cp_binding_level *save_template_parm = 0;
2525 /* Temporary take out template parameter scopes. They are saved
2526 in reversed order in save_template_parm. */
2527 while (current_binding_level->kind == sk_template_parms)
2529 struct cp_binding_level *b = current_binding_level;
2530 current_binding_level = b->level_chain;
2531 b->level_chain = save_template_parm;
2532 save_template_parm = b;
2535 resume_scope (NAMESPACE_LEVEL (inner));
2536 current_namespace = inner;
2538 /* Restore template parameter scopes. */
2539 while (save_template_parm)
2541 struct cp_binding_level *b = save_template_parm;
2542 save_template_parm = b->level_chain;
2543 b->level_chain = current_binding_level;
2544 current_binding_level = b;
2547 else
2548 pushclass (inner);
2551 /* Enter the scope INNER from current scope. INNER must be a scope
2552 nested inside current scope. This works with both name lookup and
2553 pushing name into scope. In case a template parameter scope is present,
2554 namespace is pushed under the template parameter scope according to
2555 name lookup rule in 14.6.1/6.
2557 Return the former current scope suitable for pop_inner_scope. */
2559 tree
2560 push_inner_scope (tree inner)
2562 tree outer = current_scope ();
2563 if (!outer)
2564 outer = current_namespace;
2566 push_inner_scope_r (outer, inner);
2567 return outer;
2570 /* Exit the current scope INNER back to scope OUTER. */
2572 void
2573 pop_inner_scope (tree outer, tree inner)
2575 if (outer == inner
2576 || (TREE_CODE (inner) != NAMESPACE_DECL && !CLASS_TYPE_P (inner)))
2577 return;
2579 while (outer != inner)
2581 if (TREE_CODE (inner) == NAMESPACE_DECL)
2583 struct cp_binding_level *save_template_parm = 0;
2584 /* Temporary take out template parameter scopes. They are saved
2585 in reversed order in save_template_parm. */
2586 while (current_binding_level->kind == sk_template_parms)
2588 struct cp_binding_level *b = current_binding_level;
2589 current_binding_level = b->level_chain;
2590 b->level_chain = save_template_parm;
2591 save_template_parm = b;
2594 pop_namespace ();
2596 /* Restore template parameter scopes. */
2597 while (save_template_parm)
2599 struct cp_binding_level *b = save_template_parm;
2600 save_template_parm = b->level_chain;
2601 b->level_chain = current_binding_level;
2602 current_binding_level = b;
2605 else
2606 popclass ();
2608 inner = CP_DECL_CONTEXT (TREE_CODE (inner) == NAMESPACE_DECL ? inner : TYPE_NAME (inner));
2612 /* Do a pushlevel for class declarations. */
2614 void
2615 pushlevel_class (void)
2617 class_binding_level = begin_scope (sk_class, current_class_type);
2620 /* ...and a poplevel for class declarations. */
2622 void
2623 poplevel_class (void)
2625 struct cp_binding_level *level = class_binding_level;
2626 cp_class_binding *cb;
2627 size_t i;
2628 tree shadowed;
2630 timevar_push (TV_NAME_LOOKUP);
2631 gcc_assert (level != 0);
2633 /* If we're leaving a toplevel class, cache its binding level. */
2634 if (current_class_depth == 1)
2635 previous_class_level = level;
2636 for (shadowed = level->type_shadowed;
2637 shadowed;
2638 shadowed = TREE_CHAIN (shadowed))
2639 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (shadowed), TREE_VALUE (shadowed));
2641 /* Remove the bindings for all of the class-level declarations. */
2642 if (level->class_shadowed)
2644 FOR_EACH_VEC_ELT (cp_class_binding, level->class_shadowed, i, cb)
2645 IDENTIFIER_BINDING (cb->identifier) = cb->base.previous;
2646 ggc_free (level->class_shadowed);
2647 level->class_shadowed = NULL;
2650 /* Now, pop out of the binding level which we created up in the
2651 `pushlevel_class' routine. */
2652 gcc_assert (current_binding_level == level);
2653 leave_scope ();
2654 timevar_pop (TV_NAME_LOOKUP);
2657 /* Set INHERITED_VALUE_BINDING_P on BINDING to true or false, as
2658 appropriate. DECL is the value to which a name has just been
2659 bound. CLASS_TYPE is the class in which the lookup occurred. */
2661 static void
2662 set_inherited_value_binding_p (cxx_binding *binding, tree decl,
2663 tree class_type)
2665 if (binding->value == decl && TREE_CODE (decl) != TREE_LIST)
2667 tree context;
2669 if (TREE_CODE (decl) == OVERLOAD)
2670 context = CP_DECL_CONTEXT (OVL_CURRENT (decl));
2671 else
2673 gcc_assert (DECL_P (decl));
2674 context = context_for_name_lookup (decl);
2677 if (is_properly_derived_from (class_type, context))
2678 INHERITED_VALUE_BINDING_P (binding) = 1;
2679 else
2680 INHERITED_VALUE_BINDING_P (binding) = 0;
2682 else if (binding->value == decl)
2683 /* We only encounter a TREE_LIST when there is an ambiguity in the
2684 base classes. Such an ambiguity can be overridden by a
2685 definition in this class. */
2686 INHERITED_VALUE_BINDING_P (binding) = 1;
2687 else
2688 INHERITED_VALUE_BINDING_P (binding) = 0;
2691 /* Make the declaration of X appear in CLASS scope. */
2693 bool
2694 pushdecl_class_level (tree x)
2696 tree name;
2697 bool is_valid = true;
2699 /* Do nothing if we're adding to an outer lambda closure type,
2700 outer_binding will add it later if it's needed. */
2701 if (current_class_type != class_binding_level->this_entity)
2702 return true;
2704 timevar_push (TV_NAME_LOOKUP);
2705 /* Get the name of X. */
2706 if (TREE_CODE (x) == OVERLOAD)
2707 name = DECL_NAME (get_first_fn (x));
2708 else
2709 name = DECL_NAME (x);
2711 if (name)
2713 is_valid = push_class_level_binding (name, x);
2714 if (TREE_CODE (x) == TYPE_DECL)
2715 set_identifier_type_value (name, x);
2717 else if (ANON_AGGR_TYPE_P (TREE_TYPE (x)))
2719 /* If X is an anonymous aggregate, all of its members are
2720 treated as if they were members of the class containing the
2721 aggregate, for naming purposes. */
2722 tree f;
2724 for (f = TYPE_FIELDS (TREE_TYPE (x)); f; f = DECL_CHAIN (f))
2726 location_t save_location = input_location;
2727 input_location = DECL_SOURCE_LOCATION (f);
2728 if (!pushdecl_class_level (f))
2729 is_valid = false;
2730 input_location = save_location;
2733 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, is_valid);
2736 /* Return the BINDING (if any) for NAME in SCOPE, which is a class
2737 scope. If the value returned is non-NULL, and the PREVIOUS field
2738 is not set, callers must set the PREVIOUS field explicitly. */
2740 static cxx_binding *
2741 get_class_binding (tree name, cxx_scope *scope)
2743 tree class_type;
2744 tree type_binding;
2745 tree value_binding;
2746 cxx_binding *binding;
2748 class_type = scope->this_entity;
2750 /* Get the type binding. */
2751 type_binding = lookup_member (class_type, name,
2752 /*protect=*/2, /*want_type=*/true);
2753 /* Get the value binding. */
2754 value_binding = lookup_member (class_type, name,
2755 /*protect=*/2, /*want_type=*/false);
2757 if (value_binding
2758 && (TREE_CODE (value_binding) == TYPE_DECL
2759 || DECL_CLASS_TEMPLATE_P (value_binding)
2760 || (TREE_CODE (value_binding) == TREE_LIST
2761 && TREE_TYPE (value_binding) == error_mark_node
2762 && (TREE_CODE (TREE_VALUE (value_binding))
2763 == TYPE_DECL))))
2764 /* We found a type binding, even when looking for a non-type
2765 binding. This means that we already processed this binding
2766 above. */
2768 else if (value_binding)
2770 if (TREE_CODE (value_binding) == TREE_LIST
2771 && TREE_TYPE (value_binding) == error_mark_node)
2772 /* NAME is ambiguous. */
2774 else if (BASELINK_P (value_binding))
2775 /* NAME is some overloaded functions. */
2776 value_binding = BASELINK_FUNCTIONS (value_binding);
2779 /* If we found either a type binding or a value binding, create a
2780 new binding object. */
2781 if (type_binding || value_binding)
2783 binding = new_class_binding (name,
2784 value_binding,
2785 type_binding,
2786 scope);
2787 /* This is a class-scope binding, not a block-scope binding. */
2788 LOCAL_BINDING_P (binding) = 0;
2789 set_inherited_value_binding_p (binding, value_binding, class_type);
2791 else
2792 binding = NULL;
2794 return binding;
2797 /* Make the declaration(s) of X appear in CLASS scope under the name
2798 NAME. Returns true if the binding is valid. */
2800 bool
2801 push_class_level_binding (tree name, tree x)
2803 cxx_binding *binding;
2804 tree decl = x;
2805 bool ok;
2807 timevar_push (TV_NAME_LOOKUP);
2808 /* The class_binding_level will be NULL if x is a template
2809 parameter name in a member template. */
2810 if (!class_binding_level)
2811 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, true);
2813 if (name == error_mark_node)
2814 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, false);
2816 /* Check for invalid member names. */
2817 gcc_assert (TYPE_BEING_DEFINED (current_class_type));
2818 /* Check that we're pushing into the right binding level. */
2819 gcc_assert (current_class_type == class_binding_level->this_entity);
2821 /* We could have been passed a tree list if this is an ambiguous
2822 declaration. If so, pull the declaration out because
2823 check_template_shadow will not handle a TREE_LIST. */
2824 if (TREE_CODE (decl) == TREE_LIST
2825 && TREE_TYPE (decl) == error_mark_node)
2826 decl = TREE_VALUE (decl);
2828 if (!check_template_shadow (decl))
2829 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, false);
2831 /* [class.mem]
2833 If T is the name of a class, then each of the following shall
2834 have a name different from T:
2836 -- every static data member of class T;
2838 -- every member of class T that is itself a type;
2840 -- every enumerator of every member of class T that is an
2841 enumerated type;
2843 -- every member of every anonymous union that is a member of
2844 class T.
2846 (Non-static data members were also forbidden to have the same
2847 name as T until TC1.) */
2848 if ((TREE_CODE (x) == VAR_DECL
2849 || TREE_CODE (x) == CONST_DECL
2850 || (TREE_CODE (x) == TYPE_DECL
2851 && !DECL_SELF_REFERENCE_P (x))
2852 /* A data member of an anonymous union. */
2853 || (TREE_CODE (x) == FIELD_DECL
2854 && DECL_CONTEXT (x) != current_class_type))
2855 && DECL_NAME (x) == constructor_name (current_class_type))
2857 tree scope = context_for_name_lookup (x);
2858 if (TYPE_P (scope) && same_type_p (scope, current_class_type))
2860 error ("%qD has the same name as the class in which it is "
2861 "declared",
2863 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, false);
2867 /* Get the current binding for NAME in this class, if any. */
2868 binding = IDENTIFIER_BINDING (name);
2869 if (!binding || binding->scope != class_binding_level)
2871 binding = get_class_binding (name, class_binding_level);
2872 /* If a new binding was created, put it at the front of the
2873 IDENTIFIER_BINDING list. */
2874 if (binding)
2876 binding->previous = IDENTIFIER_BINDING (name);
2877 IDENTIFIER_BINDING (name) = binding;
2881 /* If there is already a binding, then we may need to update the
2882 current value. */
2883 if (binding && binding->value)
2885 tree bval = binding->value;
2886 tree old_decl = NULL_TREE;
2888 if (INHERITED_VALUE_BINDING_P (binding))
2890 /* If the old binding was from a base class, and was for a
2891 tag name, slide it over to make room for the new binding.
2892 The old binding is still visible if explicitly qualified
2893 with a class-key. */
2894 if (TREE_CODE (bval) == TYPE_DECL && DECL_ARTIFICIAL (bval)
2895 && !(TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x)))
2897 old_decl = binding->type;
2898 binding->type = bval;
2899 binding->value = NULL_TREE;
2900 INHERITED_VALUE_BINDING_P (binding) = 0;
2902 else
2904 old_decl = bval;
2905 /* Any inherited type declaration is hidden by the type
2906 declaration in the derived class. */
2907 if (TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x))
2908 binding->type = NULL_TREE;
2911 else if (TREE_CODE (x) == OVERLOAD && is_overloaded_fn (bval))
2912 old_decl = bval;
2913 else if (TREE_CODE (x) == USING_DECL && TREE_CODE (bval) == USING_DECL)
2914 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, true);
2915 else if (TREE_CODE (x) == USING_DECL && is_overloaded_fn (bval))
2916 old_decl = bval;
2917 else if (TREE_CODE (bval) == USING_DECL && is_overloaded_fn (x))
2918 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, true);
2920 if (old_decl && binding->scope == class_binding_level)
2922 binding->value = x;
2923 /* It is always safe to clear INHERITED_VALUE_BINDING_P
2924 here. This function is only used to register bindings
2925 from with the class definition itself. */
2926 INHERITED_VALUE_BINDING_P (binding) = 0;
2927 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, true);
2931 /* Note that we declared this value so that we can issue an error if
2932 this is an invalid redeclaration of a name already used for some
2933 other purpose. */
2934 note_name_declared_in_class (name, decl);
2936 /* If we didn't replace an existing binding, put the binding on the
2937 stack of bindings for the identifier, and update the shadowed
2938 list. */
2939 if (binding && binding->scope == class_binding_level)
2940 /* Supplement the existing binding. */
2941 ok = supplement_binding (binding, decl);
2942 else
2944 /* Create a new binding. */
2945 push_binding (name, decl, class_binding_level);
2946 ok = true;
2949 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, ok);
2952 /* Process "using SCOPE::NAME" in a class scope. Return the
2953 USING_DECL created. */
2955 tree
2956 do_class_using_decl (tree scope, tree name)
2958 /* The USING_DECL returned by this function. */
2959 tree value;
2960 /* The declaration (or declarations) name by this using
2961 declaration. NULL if we are in a template and cannot figure out
2962 what has been named. */
2963 tree decl;
2964 /* True if SCOPE is a dependent type. */
2965 bool scope_dependent_p;
2966 /* True if SCOPE::NAME is dependent. */
2967 bool name_dependent_p;
2968 /* True if any of the bases of CURRENT_CLASS_TYPE are dependent. */
2969 bool bases_dependent_p;
2970 tree binfo;
2971 tree base_binfo;
2972 int i;
2974 if (name == error_mark_node)
2975 return NULL_TREE;
2977 if (!scope || !TYPE_P (scope))
2979 error ("using-declaration for non-member at class scope");
2980 return NULL_TREE;
2983 /* Make sure the name is not invalid */
2984 if (TREE_CODE (name) == BIT_NOT_EXPR)
2986 error ("%<%T::%D%> names destructor", scope, name);
2987 return NULL_TREE;
2989 if (MAYBE_CLASS_TYPE_P (scope) && constructor_name_p (name, scope))
2991 error ("%<%T::%D%> names constructor", scope, name);
2992 return NULL_TREE;
2994 if (constructor_name_p (name, current_class_type))
2996 error ("%<%T::%D%> names constructor in %qT",
2997 scope, name, current_class_type);
2998 return NULL_TREE;
3001 scope_dependent_p = dependent_type_p (scope);
3002 name_dependent_p = (scope_dependent_p
3003 || (IDENTIFIER_TYPENAME_P (name)
3004 && dependent_type_p (TREE_TYPE (name))));
3006 bases_dependent_p = false;
3007 if (processing_template_decl)
3008 for (binfo = TYPE_BINFO (current_class_type), i = 0;
3009 BINFO_BASE_ITERATE (binfo, i, base_binfo);
3010 i++)
3011 if (dependent_type_p (TREE_TYPE (base_binfo)))
3013 bases_dependent_p = true;
3014 break;
3017 decl = NULL_TREE;
3019 /* From [namespace.udecl]:
3021 A using-declaration used as a member-declaration shall refer to a
3022 member of a base class of the class being defined.
3024 In general, we cannot check this constraint in a template because
3025 we do not know the entire set of base classes of the current
3026 class type. However, if all of the base classes are
3027 non-dependent, then we can avoid delaying the check until
3028 instantiation. */
3029 if (!scope_dependent_p)
3031 base_kind b_kind;
3032 binfo = lookup_base (current_class_type, scope, ba_any, &b_kind);
3033 if (b_kind < bk_proper_base)
3035 if (!bases_dependent_p)
3037 error_not_base_type (scope, current_class_type);
3038 return NULL_TREE;
3041 else if (!name_dependent_p)
3043 decl = lookup_member (binfo, name, 0, false);
3044 if (!decl)
3046 error ("no members matching %<%T::%D%> in %q#T", scope, name,
3047 scope);
3048 return NULL_TREE;
3050 /* The binfo from which the functions came does not matter. */
3051 if (BASELINK_P (decl))
3052 decl = BASELINK_FUNCTIONS (decl);
3056 value = build_lang_decl (USING_DECL, name, NULL_TREE);
3057 USING_DECL_DECLS (value) = decl;
3058 USING_DECL_SCOPE (value) = scope;
3059 DECL_DEPENDENT_P (value) = !decl;
3061 return value;
3065 /* Return the binding value for name in scope. */
3067 tree
3068 namespace_binding (tree name, tree scope)
3070 cxx_binding *binding;
3072 if (SCOPE_FILE_SCOPE_P (scope))
3073 scope = global_namespace;
3074 else
3075 /* Unnecessary for the global namespace because it can't be an alias. */
3076 scope = ORIGINAL_NAMESPACE (scope);
3078 binding = cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (scope), name);
3080 return binding ? binding->value : NULL_TREE;
3083 /* Set the binding value for name in scope. */
3085 void
3086 set_namespace_binding (tree name, tree scope, tree val)
3088 cxx_binding *b;
3090 timevar_push (TV_NAME_LOOKUP);
3091 if (scope == NULL_TREE)
3092 scope = global_namespace;
3093 b = binding_for_name (NAMESPACE_LEVEL (scope), name);
3094 if (!b->value || TREE_CODE (val) == OVERLOAD || val == error_mark_node)
3095 b->value = val;
3096 else
3097 supplement_binding (b, val);
3098 timevar_pop (TV_NAME_LOOKUP);
3101 /* Set the context of a declaration to scope. Complain if we are not
3102 outside scope. */
3104 void
3105 set_decl_namespace (tree decl, tree scope, bool friendp)
3107 tree old;
3109 /* Get rid of namespace aliases. */
3110 scope = ORIGINAL_NAMESPACE (scope);
3112 /* It is ok for friends to be qualified in parallel space. */
3113 if (!friendp && !is_ancestor (current_namespace, scope))
3114 error ("declaration of %qD not in a namespace surrounding %qD",
3115 decl, scope);
3116 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
3118 /* Writing "int N::i" to declare a variable within "N" is invalid. */
3119 if (scope == current_namespace)
3121 if (at_namespace_scope_p ())
3122 error ("explicit qualification in declaration of %qD",
3123 decl);
3124 return;
3127 /* See whether this has been declared in the namespace. */
3128 old = lookup_qualified_name (scope, DECL_NAME (decl), false, true);
3129 if (old == error_mark_node)
3130 /* No old declaration at all. */
3131 goto complain;
3132 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
3133 if (TREE_CODE (old) == TREE_LIST)
3135 error ("reference to %qD is ambiguous", decl);
3136 print_candidates (old);
3137 return;
3139 if (!is_overloaded_fn (decl))
3141 /* We might have found OLD in an inline namespace inside SCOPE. */
3142 if (TREE_CODE (decl) == TREE_CODE (old))
3143 DECL_CONTEXT (decl) = DECL_CONTEXT (old);
3144 /* Don't compare non-function decls with decls_match here, since
3145 it can't check for the correct constness at this
3146 point. pushdecl will find those errors later. */
3147 return;
3149 /* Since decl is a function, old should contain a function decl. */
3150 if (!is_overloaded_fn (old))
3151 goto complain;
3152 /* A template can be explicitly specialized in any namespace. */
3153 if (processing_explicit_instantiation)
3154 return;
3155 if (processing_template_decl || processing_specialization)
3156 /* We have not yet called push_template_decl to turn a
3157 FUNCTION_DECL into a TEMPLATE_DECL, so the declarations won't
3158 match. But, we'll check later, when we construct the
3159 template. */
3160 return;
3161 /* Instantiations or specializations of templates may be declared as
3162 friends in any namespace. */
3163 if (friendp && DECL_USE_TEMPLATE (decl))
3164 return;
3165 if (is_overloaded_fn (old))
3167 tree found = NULL_TREE;
3168 tree elt = old;
3169 for (; elt; elt = OVL_NEXT (elt))
3171 tree ofn = OVL_CURRENT (elt);
3172 /* Adjust DECL_CONTEXT first so decls_match will return true
3173 if DECL will match a declaration in an inline namespace. */
3174 DECL_CONTEXT (decl) = DECL_CONTEXT (ofn);
3175 if (decls_match (decl, ofn))
3177 if (found && !decls_match (found, ofn))
3179 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
3180 error ("reference to %qD is ambiguous", decl);
3181 print_candidates (old);
3182 return;
3184 found = ofn;
3187 if (found)
3189 if (!is_associated_namespace (scope, CP_DECL_CONTEXT (found)))
3190 goto complain;
3191 DECL_CONTEXT (decl) = DECL_CONTEXT (found);
3192 return;
3195 else
3197 DECL_CONTEXT (decl) = DECL_CONTEXT (old);
3198 if (decls_match (decl, old))
3199 return;
3202 /* It didn't work, go back to the explicit scope. */
3203 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
3204 complain:
3205 error ("%qD should have been declared inside %qD", decl, scope);
3208 /* Return the namespace where the current declaration is declared. */
3210 tree
3211 current_decl_namespace (void)
3213 tree result;
3214 /* If we have been pushed into a different namespace, use it. */
3215 if (!VEC_empty (tree, decl_namespace_list))
3216 return VEC_last (tree, decl_namespace_list);
3218 if (current_class_type)
3219 result = decl_namespace_context (current_class_type);
3220 else if (current_function_decl)
3221 result = decl_namespace_context (current_function_decl);
3222 else
3223 result = current_namespace;
3224 return result;
3227 /* Process any ATTRIBUTES on a namespace definition. Currently only
3228 attribute visibility is meaningful, which is a property of the syntactic
3229 block rather than the namespace as a whole, so we don't touch the
3230 NAMESPACE_DECL at all. Returns true if attribute visibility is seen. */
3232 bool
3233 handle_namespace_attrs (tree ns, tree attributes)
3235 tree d;
3236 bool saw_vis = false;
3238 for (d = attributes; d; d = TREE_CHAIN (d))
3240 tree name = TREE_PURPOSE (d);
3241 tree args = TREE_VALUE (d);
3243 if (is_attribute_p ("visibility", name))
3245 tree x = args ? TREE_VALUE (args) : NULL_TREE;
3246 if (x == NULL_TREE || TREE_CODE (x) != STRING_CST || TREE_CHAIN (args))
3248 warning (OPT_Wattributes,
3249 "%qD attribute requires a single NTBS argument",
3250 name);
3251 continue;
3254 if (!TREE_PUBLIC (ns))
3255 warning (OPT_Wattributes,
3256 "%qD attribute is meaningless since members of the "
3257 "anonymous namespace get local symbols", name);
3259 push_visibility (TREE_STRING_POINTER (x), 1);
3260 saw_vis = true;
3262 else
3264 warning (OPT_Wattributes, "%qD attribute directive ignored",
3265 name);
3266 continue;
3270 return saw_vis;
3273 /* Push into the scope of the NAME namespace. If NAME is NULL_TREE, then we
3274 select a name that is unique to this compilation unit. */
3276 void
3277 push_namespace (tree name)
3279 tree d = NULL_TREE;
3280 int need_new = 1;
3281 int implicit_use = 0;
3282 bool anon = !name;
3284 timevar_push (TV_NAME_LOOKUP);
3286 /* We should not get here if the global_namespace is not yet constructed
3287 nor if NAME designates the global namespace: The global scope is
3288 constructed elsewhere. */
3289 gcc_assert (global_namespace != NULL && name != global_scope_name);
3291 if (anon)
3293 name = get_anonymous_namespace_name();
3294 d = IDENTIFIER_NAMESPACE_VALUE (name);
3295 if (d)
3296 /* Reopening anonymous namespace. */
3297 need_new = 0;
3298 implicit_use = 1;
3300 else
3302 /* Check whether this is an extended namespace definition. */
3303 d = IDENTIFIER_NAMESPACE_VALUE (name);
3304 if (d != NULL_TREE && TREE_CODE (d) == NAMESPACE_DECL)
3306 need_new = 0;
3307 if (DECL_NAMESPACE_ALIAS (d))
3309 error ("namespace alias %qD not allowed here, assuming %qD",
3310 d, DECL_NAMESPACE_ALIAS (d));
3311 d = DECL_NAMESPACE_ALIAS (d);
3316 if (need_new)
3318 /* Make a new namespace, binding the name to it. */
3319 d = build_lang_decl (NAMESPACE_DECL, name, void_type_node);
3320 DECL_CONTEXT (d) = FROB_CONTEXT (current_namespace);
3321 /* The name of this namespace is not visible to other translation
3322 units if it is an anonymous namespace or member thereof. */
3323 if (anon || decl_anon_ns_mem_p (current_namespace))
3324 TREE_PUBLIC (d) = 0;
3325 else
3326 TREE_PUBLIC (d) = 1;
3327 pushdecl (d);
3328 if (anon)
3330 /* Clear DECL_NAME for the benefit of debugging back ends. */
3331 SET_DECL_ASSEMBLER_NAME (d, name);
3332 DECL_NAME (d) = NULL_TREE;
3334 begin_scope (sk_namespace, d);
3336 else
3337 resume_scope (NAMESPACE_LEVEL (d));
3339 if (implicit_use)
3340 do_using_directive (d);
3341 /* Enter the name space. */
3342 current_namespace = d;
3344 timevar_pop (TV_NAME_LOOKUP);
3347 /* Pop from the scope of the current namespace. */
3349 void
3350 pop_namespace (void)
3352 gcc_assert (current_namespace != global_namespace);
3353 current_namespace = CP_DECL_CONTEXT (current_namespace);
3354 /* The binding level is not popped, as it might be re-opened later. */
3355 leave_scope ();
3358 /* Push into the scope of the namespace NS, even if it is deeply
3359 nested within another namespace. */
3361 void
3362 push_nested_namespace (tree ns)
3364 if (ns == global_namespace)
3365 push_to_top_level ();
3366 else
3368 push_nested_namespace (CP_DECL_CONTEXT (ns));
3369 push_namespace (DECL_NAME (ns));
3373 /* Pop back from the scope of the namespace NS, which was previously
3374 entered with push_nested_namespace. */
3376 void
3377 pop_nested_namespace (tree ns)
3379 timevar_push (TV_NAME_LOOKUP);
3380 gcc_assert (current_namespace == ns);
3381 while (ns != global_namespace)
3383 pop_namespace ();
3384 ns = CP_DECL_CONTEXT (ns);
3387 pop_from_top_level ();
3388 timevar_pop (TV_NAME_LOOKUP);
3391 /* Temporarily set the namespace for the current declaration. */
3393 void
3394 push_decl_namespace (tree decl)
3396 if (TREE_CODE (decl) != NAMESPACE_DECL)
3397 decl = decl_namespace_context (decl);
3398 VEC_safe_push (tree, gc, decl_namespace_list, ORIGINAL_NAMESPACE (decl));
3401 /* [namespace.memdef]/2 */
3403 void
3404 pop_decl_namespace (void)
3406 VEC_pop (tree, decl_namespace_list);
3409 /* Return the namespace that is the common ancestor
3410 of two given namespaces. */
3412 static tree
3413 namespace_ancestor (tree ns1, tree ns2)
3415 timevar_push (TV_NAME_LOOKUP);
3416 if (is_ancestor (ns1, ns2))
3417 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, ns1);
3418 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP,
3419 namespace_ancestor (CP_DECL_CONTEXT (ns1), ns2));
3422 /* Process a namespace-alias declaration. */
3424 void
3425 do_namespace_alias (tree alias, tree name_space)
3427 if (name_space == error_mark_node)
3428 return;
3430 gcc_assert (TREE_CODE (name_space) == NAMESPACE_DECL);
3432 name_space = ORIGINAL_NAMESPACE (name_space);
3434 /* Build the alias. */
3435 alias = build_lang_decl (NAMESPACE_DECL, alias, void_type_node);
3436 DECL_NAMESPACE_ALIAS (alias) = name_space;
3437 DECL_EXTERNAL (alias) = 1;
3438 DECL_CONTEXT (alias) = FROB_CONTEXT (current_scope ());
3439 pushdecl (alias);
3441 /* Emit debug info for namespace alias. */
3442 if (!building_stmt_tree ())
3443 (*debug_hooks->global_decl) (alias);
3446 /* Like pushdecl, only it places X in the current namespace,
3447 if appropriate. */
3449 tree
3450 pushdecl_namespace_level (tree x, bool is_friend)
3452 struct cp_binding_level *b = current_binding_level;
3453 tree t;
3455 timevar_push (TV_NAME_LOOKUP);
3456 t = pushdecl_with_scope (x, NAMESPACE_LEVEL (current_namespace), is_friend);
3458 /* Now, the type_shadowed stack may screw us. Munge it so it does
3459 what we want. */
3460 if (TREE_CODE (t) == TYPE_DECL)
3462 tree name = DECL_NAME (t);
3463 tree newval;
3464 tree *ptr = (tree *)0;
3465 for (; !global_scope_p (b); b = b->level_chain)
3467 tree shadowed = b->type_shadowed;
3468 for (; shadowed; shadowed = TREE_CHAIN (shadowed))
3469 if (TREE_PURPOSE (shadowed) == name)
3471 ptr = &TREE_VALUE (shadowed);
3472 /* Can't break out of the loop here because sometimes
3473 a binding level will have duplicate bindings for
3474 PT names. It's gross, but I haven't time to fix it. */
3477 newval = TREE_TYPE (t);
3478 if (ptr == (tree *)0)
3480 /* @@ This shouldn't be needed. My test case "zstring.cc" trips
3481 up here if this is changed to an assertion. --KR */
3482 SET_IDENTIFIER_TYPE_VALUE (name, t);
3484 else
3486 *ptr = newval;
3489 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
3492 /* Insert USED into the using list of USER. Set INDIRECT_flag if this
3493 directive is not directly from the source. Also find the common
3494 ancestor and let our users know about the new namespace */
3495 static void
3496 add_using_namespace (tree user, tree used, bool indirect)
3498 tree t;
3499 timevar_push (TV_NAME_LOOKUP);
3500 /* Using oneself is a no-op. */
3501 if (user == used)
3503 timevar_pop (TV_NAME_LOOKUP);
3504 return;
3506 gcc_assert (TREE_CODE (user) == NAMESPACE_DECL);
3507 gcc_assert (TREE_CODE (used) == NAMESPACE_DECL);
3508 /* Check if we already have this. */
3509 t = purpose_member (used, DECL_NAMESPACE_USING (user));
3510 if (t != NULL_TREE)
3512 if (!indirect)
3513 /* Promote to direct usage. */
3514 TREE_INDIRECT_USING (t) = 0;
3515 timevar_pop (TV_NAME_LOOKUP);
3516 return;
3519 /* Add used to the user's using list. */
3520 DECL_NAMESPACE_USING (user)
3521 = tree_cons (used, namespace_ancestor (user, used),
3522 DECL_NAMESPACE_USING (user));
3524 TREE_INDIRECT_USING (DECL_NAMESPACE_USING (user)) = indirect;
3526 /* Add user to the used's users list. */
3527 DECL_NAMESPACE_USERS (used)
3528 = tree_cons (user, 0, DECL_NAMESPACE_USERS (used));
3530 /* Recursively add all namespaces used. */
3531 for (t = DECL_NAMESPACE_USING (used); t; t = TREE_CHAIN (t))
3532 /* indirect usage */
3533 add_using_namespace (user, TREE_PURPOSE (t), 1);
3535 /* Tell everyone using us about the new used namespaces. */
3536 for (t = DECL_NAMESPACE_USERS (user); t; t = TREE_CHAIN (t))
3537 add_using_namespace (TREE_PURPOSE (t), used, 1);
3538 timevar_pop (TV_NAME_LOOKUP);
3541 /* Process a using-declaration not appearing in class or local scope. */
3543 void
3544 do_toplevel_using_decl (tree decl, tree scope, tree name)
3546 tree oldval, oldtype, newval, newtype;
3547 tree orig_decl = decl;
3548 cxx_binding *binding;
3550 decl = validate_nonmember_using_decl (decl, scope, name);
3551 if (decl == NULL_TREE)
3552 return;
3554 binding = binding_for_name (NAMESPACE_LEVEL (current_namespace), name);
3556 oldval = binding->value;
3557 oldtype = binding->type;
3559 do_nonmember_using_decl (scope, name, oldval, oldtype, &newval, &newtype);
3561 /* Emit debug info. */
3562 if (!processing_template_decl)
3563 cp_emit_debug_info_for_using (orig_decl, current_namespace);
3565 /* Copy declarations found. */
3566 if (newval)
3567 binding->value = newval;
3568 if (newtype)
3569 binding->type = newtype;
3572 /* Process a using-directive. */
3574 void
3575 do_using_directive (tree name_space)
3577 tree context = NULL_TREE;
3579 if (name_space == error_mark_node)
3580 return;
3582 gcc_assert (TREE_CODE (name_space) == NAMESPACE_DECL);
3584 if (building_stmt_tree ())
3585 add_stmt (build_stmt (input_location, USING_STMT, name_space));
3586 name_space = ORIGINAL_NAMESPACE (name_space);
3588 if (!toplevel_bindings_p ())
3590 push_using_directive (name_space);
3592 else
3594 /* direct usage */
3595 add_using_namespace (current_namespace, name_space, 0);
3596 if (current_namespace != global_namespace)
3597 context = current_namespace;
3599 /* Emit debugging info. */
3600 if (!processing_template_decl)
3601 (*debug_hooks->imported_module_or_decl) (name_space, NULL_TREE,
3602 context, false);
3606 /* Deal with a using-directive seen by the parser. Currently we only
3607 handle attributes here, since they cannot appear inside a template. */
3609 void
3610 parse_using_directive (tree name_space, tree attribs)
3612 tree a;
3614 do_using_directive (name_space);
3616 for (a = attribs; a; a = TREE_CHAIN (a))
3618 tree name = TREE_PURPOSE (a);
3619 if (is_attribute_p ("strong", name))
3621 if (!toplevel_bindings_p ())
3622 error ("strong using only meaningful at namespace scope");
3623 else if (name_space != error_mark_node)
3625 if (!is_ancestor (current_namespace, name_space))
3626 error ("current namespace %qD does not enclose strongly used namespace %qD",
3627 current_namespace, name_space);
3628 DECL_NAMESPACE_ASSOCIATIONS (name_space)
3629 = tree_cons (current_namespace, 0,
3630 DECL_NAMESPACE_ASSOCIATIONS (name_space));
3633 else
3634 warning (OPT_Wattributes, "%qD attribute directive ignored", name);
3638 /* Like pushdecl, only it places X in the global scope if appropriate.
3639 Calls cp_finish_decl to register the variable, initializing it with
3640 *INIT, if INIT is non-NULL. */
3642 static tree
3643 pushdecl_top_level_1 (tree x, tree *init, bool is_friend)
3645 timevar_push (TV_NAME_LOOKUP);
3646 push_to_top_level ();
3647 x = pushdecl_namespace_level (x, is_friend);
3648 if (init)
3649 cp_finish_decl (x, *init, false, NULL_TREE, 0);
3650 pop_from_top_level ();
3651 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
3654 /* Like pushdecl, only it places X in the global scope if appropriate. */
3656 tree
3657 pushdecl_top_level (tree x)
3659 return pushdecl_top_level_1 (x, NULL, false);
3662 /* Like pushdecl_top_level, but adding the IS_FRIEND parameter. */
3664 tree
3665 pushdecl_top_level_maybe_friend (tree x, bool is_friend)
3667 return pushdecl_top_level_1 (x, NULL, is_friend);
3670 /* Like pushdecl, only it places X in the global scope if
3671 appropriate. Calls cp_finish_decl to register the variable,
3672 initializing it with INIT. */
3674 tree
3675 pushdecl_top_level_and_finish (tree x, tree init)
3677 return pushdecl_top_level_1 (x, &init, false);
3680 /* Combines two sets of overloaded functions into an OVERLOAD chain, removing
3681 duplicates. The first list becomes the tail of the result.
3683 The algorithm is O(n^2). We could get this down to O(n log n) by
3684 doing a sort on the addresses of the functions, if that becomes
3685 necessary. */
3687 static tree
3688 merge_functions (tree s1, tree s2)
3690 for (; s2; s2 = OVL_NEXT (s2))
3692 tree fn2 = OVL_CURRENT (s2);
3693 tree fns1;
3695 for (fns1 = s1; fns1; fns1 = OVL_NEXT (fns1))
3697 tree fn1 = OVL_CURRENT (fns1);
3699 /* If the function from S2 is already in S1, there is no
3700 need to add it again. For `extern "C"' functions, we
3701 might have two FUNCTION_DECLs for the same function, in
3702 different namespaces, but let's leave them in in case
3703 they have different default arguments. */
3704 if (fn1 == fn2)
3705 break;
3708 /* If we exhausted all of the functions in S1, FN2 is new. */
3709 if (!fns1)
3710 s1 = build_overload (fn2, s1);
3712 return s1;
3715 /* Returns TRUE iff OLD and NEW are the same entity.
3717 3 [basic]/3: An entity is a value, object, reference, function,
3718 enumerator, type, class member, template, template specialization,
3719 namespace, parameter pack, or this.
3721 7.3.4 [namespace.udir]/4: If name lookup finds a declaration for a name
3722 in two different namespaces, and the declarations do not declare the
3723 same entity and do not declare functions, the use of the name is
3724 ill-formed. */
3726 static bool
3727 same_entity_p (tree one, tree two)
3729 if (one == two)
3730 return true;
3731 if (!one || !two)
3732 return false;
3733 if (TREE_CODE (one) == TYPE_DECL
3734 && TREE_CODE (two) == TYPE_DECL
3735 && same_type_p (TREE_TYPE (one), TREE_TYPE (two)))
3736 return true;
3737 return false;
3740 /* This should return an error not all definitions define functions.
3741 It is not an error if we find two functions with exactly the
3742 same signature, only if these are selected in overload resolution.
3743 old is the current set of bindings, new_binding the freshly-found binding.
3744 XXX Do we want to give *all* candidates in case of ambiguity?
3745 XXX In what way should I treat extern declarations?
3746 XXX I don't want to repeat the entire duplicate_decls here */
3748 static void
3749 ambiguous_decl (struct scope_binding *old, cxx_binding *new_binding, int flags)
3751 tree val, type;
3752 gcc_assert (old != NULL);
3754 /* Copy the type. */
3755 type = new_binding->type;
3756 if (LOOKUP_NAMESPACES_ONLY (flags)
3757 || (type && hidden_name_p (type) && !(flags & LOOKUP_HIDDEN)))
3758 type = NULL_TREE;
3760 /* Copy the value. */
3761 val = new_binding->value;
3762 if (val)
3764 if (hidden_name_p (val) && !(flags & LOOKUP_HIDDEN))
3765 val = NULL_TREE;
3766 else
3767 switch (TREE_CODE (val))
3769 case TEMPLATE_DECL:
3770 /* If we expect types or namespaces, and not templates,
3771 or this is not a template class. */
3772 if ((LOOKUP_QUALIFIERS_ONLY (flags)
3773 && !DECL_CLASS_TEMPLATE_P (val)))
3774 val = NULL_TREE;
3775 break;
3776 case TYPE_DECL:
3777 if (LOOKUP_NAMESPACES_ONLY (flags)
3778 || (type && (flags & LOOKUP_PREFER_TYPES)))
3779 val = NULL_TREE;
3780 break;
3781 case NAMESPACE_DECL:
3782 if (LOOKUP_TYPES_ONLY (flags))
3783 val = NULL_TREE;
3784 break;
3785 case FUNCTION_DECL:
3786 /* Ignore built-in functions that are still anticipated. */
3787 if (LOOKUP_QUALIFIERS_ONLY (flags))
3788 val = NULL_TREE;
3789 break;
3790 default:
3791 if (LOOKUP_QUALIFIERS_ONLY (flags))
3792 val = NULL_TREE;
3796 /* If val is hidden, shift down any class or enumeration name. */
3797 if (!val)
3799 val = type;
3800 type = NULL_TREE;
3803 if (!old->value)
3804 old->value = val;
3805 else if (val && !same_entity_p (val, old->value))
3807 if (is_overloaded_fn (old->value) && is_overloaded_fn (val))
3808 old->value = merge_functions (old->value, val);
3809 else
3811 old->value = tree_cons (NULL_TREE, old->value,
3812 build_tree_list (NULL_TREE, val));
3813 TREE_TYPE (old->value) = error_mark_node;
3817 if (!old->type)
3818 old->type = type;
3819 else if (type && old->type != type)
3821 old->type = tree_cons (NULL_TREE, old->type,
3822 build_tree_list (NULL_TREE, type));
3823 TREE_TYPE (old->type) = error_mark_node;
3827 /* Return the declarations that are members of the namespace NS. */
3829 tree
3830 cp_namespace_decls (tree ns)
3832 return NAMESPACE_LEVEL (ns)->names;
3835 /* Combine prefer_type and namespaces_only into flags. */
3837 static int
3838 lookup_flags (int prefer_type, int namespaces_only)
3840 if (namespaces_only)
3841 return LOOKUP_PREFER_NAMESPACES;
3842 if (prefer_type > 1)
3843 return LOOKUP_PREFER_TYPES;
3844 if (prefer_type > 0)
3845 return LOOKUP_PREFER_BOTH;
3846 return 0;
3849 /* Given a lookup that returned VAL, use FLAGS to decide if we want to
3850 ignore it or not. Subroutine of lookup_name_real and
3851 lookup_type_scope. */
3853 static bool
3854 qualify_lookup (tree val, int flags)
3856 if (val == NULL_TREE)
3857 return false;
3858 if ((flags & LOOKUP_PREFER_NAMESPACES) && TREE_CODE (val) == NAMESPACE_DECL)
3859 return true;
3860 if ((flags & LOOKUP_PREFER_TYPES)
3861 && (TREE_CODE (val) == TYPE_DECL || TREE_CODE (val) == TEMPLATE_DECL))
3862 return true;
3863 if (flags & (LOOKUP_PREFER_NAMESPACES | LOOKUP_PREFER_TYPES))
3864 return false;
3865 /* In unevaluated context, look past normal capture fields. */
3866 if (cp_unevaluated_operand && TREE_CODE (val) == FIELD_DECL
3867 && DECL_NORMAL_CAPTURE_P (val))
3868 return false;
3869 /* None of the lookups that use qualify_lookup want the op() from the
3870 lambda; they want the one from the enclosing class. */
3871 if (TREE_CODE (val) == FUNCTION_DECL && LAMBDA_FUNCTION_P (val))
3872 return false;
3873 return true;
3876 /* Given a lookup that returned VAL, decide if we want to ignore it or
3877 not based on DECL_ANTICIPATED. */
3879 bool
3880 hidden_name_p (tree val)
3882 if (DECL_P (val)
3883 && DECL_LANG_SPECIFIC (val)
3884 && DECL_ANTICIPATED (val))
3885 return true;
3886 return false;
3889 /* Remove any hidden friend functions from a possibly overloaded set
3890 of functions. */
3892 tree
3893 remove_hidden_names (tree fns)
3895 if (!fns)
3896 return fns;
3898 if (TREE_CODE (fns) == FUNCTION_DECL && hidden_name_p (fns))
3899 fns = NULL_TREE;
3900 else if (TREE_CODE (fns) == OVERLOAD)
3902 tree o;
3904 for (o = fns; o; o = OVL_NEXT (o))
3905 if (hidden_name_p (OVL_CURRENT (o)))
3906 break;
3907 if (o)
3909 tree n = NULL_TREE;
3911 for (o = fns; o; o = OVL_NEXT (o))
3912 if (!hidden_name_p (OVL_CURRENT (o)))
3913 n = build_overload (OVL_CURRENT (o), n);
3914 fns = n;
3918 return fns;
3921 /* Suggest alternatives for NAME, an IDENTIFIER_NODE for which name
3922 lookup failed. Search through all available namespaces and print out
3923 possible candidates. */
3925 void
3926 suggest_alternatives_for (tree name)
3928 VEC(tree,heap) *candidates = NULL;
3929 VEC(tree,heap) *namespaces_to_search = NULL;
3930 int max_to_search = PARAM_VALUE (CXX_MAX_NAMESPACES_FOR_DIAGNOSTIC_HELP);
3931 int n_searched = 0;
3932 tree t;
3933 unsigned ix;
3934 location_t name_location;
3936 VEC_safe_push (tree, heap, namespaces_to_search, global_namespace);
3938 while (!VEC_empty (tree, namespaces_to_search)
3939 && n_searched < max_to_search)
3941 tree scope = VEC_pop (tree, namespaces_to_search);
3942 struct scope_binding binding = EMPTY_SCOPE_BINDING;
3943 struct cp_binding_level *level = NAMESPACE_LEVEL (scope);
3945 /* Look in this namespace. */
3946 qualified_lookup_using_namespace (name, scope, &binding, 0);
3948 n_searched++;
3950 if (binding.value)
3951 VEC_safe_push (tree, heap, candidates, binding.value);
3953 /* Add child namespaces. */
3954 for (t = level->namespaces; t; t = DECL_CHAIN (t))
3955 VEC_safe_push (tree, heap, namespaces_to_search, t);
3958 name_location = location_of (name);
3960 /* If we stopped before we could examine all namespaces, inform the
3961 user. Do this even if we don't have any candidates, since there
3962 might be more candidates further down that we weren't able to
3963 find. */
3964 if (n_searched >= max_to_search
3965 && !VEC_empty (tree, namespaces_to_search))
3966 inform (name_location,
3967 "maximum limit of %d namespaces searched for %qE",
3968 max_to_search, name);
3970 VEC_free (tree, heap, namespaces_to_search);
3972 /* Nothing useful to report. */
3973 if (VEC_empty (tree, candidates))
3974 return;
3976 inform_n (name_location, VEC_length (tree, candidates),
3977 "suggested alternative:",
3978 "suggested alternatives:");
3980 FOR_EACH_VEC_ELT (tree, candidates, ix, t)
3981 inform (location_of (t), " %qE", t);
3983 VEC_free (tree, heap, candidates);
3986 /* Unscoped lookup of a global: iterate over current namespaces,
3987 considering using-directives. */
3989 static tree
3990 unqualified_namespace_lookup (tree name, int flags)
3992 tree initial = current_decl_namespace ();
3993 tree scope = initial;
3994 tree siter;
3995 struct cp_binding_level *level;
3996 tree val = NULL_TREE;
3998 timevar_push (TV_NAME_LOOKUP);
4000 for (; !val; scope = CP_DECL_CONTEXT (scope))
4002 struct scope_binding binding = EMPTY_SCOPE_BINDING;
4003 cxx_binding *b =
4004 cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (scope), name);
4006 if (b)
4007 ambiguous_decl (&binding, b, flags);
4009 /* Add all _DECLs seen through local using-directives. */
4010 for (level = current_binding_level;
4011 level->kind != sk_namespace;
4012 level = level->level_chain)
4013 if (!lookup_using_namespace (name, &binding, level->using_directives,
4014 scope, flags))
4015 /* Give up because of error. */
4016 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
4018 /* Add all _DECLs seen through global using-directives. */
4019 /* XXX local and global using lists should work equally. */
4020 siter = initial;
4021 while (1)
4023 if (!lookup_using_namespace (name, &binding,
4024 DECL_NAMESPACE_USING (siter),
4025 scope, flags))
4026 /* Give up because of error. */
4027 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
4028 if (siter == scope) break;
4029 siter = CP_DECL_CONTEXT (siter);
4032 val = binding.value;
4033 if (scope == global_namespace)
4034 break;
4036 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
4039 /* Look up NAME (an IDENTIFIER_NODE) in SCOPE (either a NAMESPACE_DECL
4040 or a class TYPE). If IS_TYPE_P is TRUE, then ignore non-type
4041 bindings.
4043 Returns a DECL (or OVERLOAD, or BASELINK) representing the
4044 declaration found. If no suitable declaration can be found,
4045 ERROR_MARK_NODE is returned. If COMPLAIN is true and SCOPE is
4046 neither a class-type nor a namespace a diagnostic is issued. */
4048 tree
4049 lookup_qualified_name (tree scope, tree name, bool is_type_p, bool complain)
4051 int flags = 0;
4052 tree t = NULL_TREE;
4054 if (TREE_CODE (scope) == NAMESPACE_DECL)
4056 struct scope_binding binding = EMPTY_SCOPE_BINDING;
4058 flags |= LOOKUP_COMPLAIN;
4059 if (is_type_p)
4060 flags |= LOOKUP_PREFER_TYPES;
4061 if (qualified_lookup_using_namespace (name, scope, &binding, flags))
4062 t = binding.value;
4064 else if (cxx_dialect != cxx98 && TREE_CODE (scope) == ENUMERAL_TYPE)
4065 t = lookup_enumerator (scope, name);
4066 else if (is_class_type (scope, complain))
4067 t = lookup_member (scope, name, 2, is_type_p);
4069 if (!t)
4070 return error_mark_node;
4071 return t;
4074 /* Subroutine of unqualified_namespace_lookup:
4075 Add the bindings of NAME in used namespaces to VAL.
4076 We are currently looking for names in namespace SCOPE, so we
4077 look through USINGS for using-directives of namespaces
4078 which have SCOPE as a common ancestor with the current scope.
4079 Returns false on errors. */
4081 static bool
4082 lookup_using_namespace (tree name, struct scope_binding *val,
4083 tree usings, tree scope, int flags)
4085 tree iter;
4086 timevar_push (TV_NAME_LOOKUP);
4087 /* Iterate over all used namespaces in current, searching for using
4088 directives of scope. */
4089 for (iter = usings; iter; iter = TREE_CHAIN (iter))
4090 if (TREE_VALUE (iter) == scope)
4092 tree used = ORIGINAL_NAMESPACE (TREE_PURPOSE (iter));
4093 cxx_binding *val1 =
4094 cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (used), name);
4095 /* Resolve ambiguities. */
4096 if (val1)
4097 ambiguous_decl (val, val1, flags);
4099 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val->value != error_mark_node);
4102 /* Returns true iff VEC contains TARGET. */
4104 static bool
4105 tree_vec_contains (VEC(tree,gc)* vec, tree target)
4107 unsigned int i;
4108 tree elt;
4109 FOR_EACH_VEC_ELT (tree,vec,i,elt)
4110 if (elt == target)
4111 return true;
4112 return false;
4115 /* [namespace.qual]
4116 Accepts the NAME to lookup and its qualifying SCOPE.
4117 Returns the name/type pair found into the cxx_binding *RESULT,
4118 or false on error. */
4120 static bool
4121 qualified_lookup_using_namespace (tree name, tree scope,
4122 struct scope_binding *result, int flags)
4124 /* Maintain a list of namespaces visited... */
4125 VEC(tree,gc) *seen = NULL;
4126 VEC(tree,gc) *seen_inline = NULL;
4127 /* ... and a list of namespace yet to see. */
4128 VEC(tree,gc) *todo = NULL;
4129 VEC(tree,gc) *todo_maybe = NULL;
4130 VEC(tree,gc) *todo_inline = NULL;
4131 tree usings;
4132 timevar_push (TV_NAME_LOOKUP);
4133 /* Look through namespace aliases. */
4134 scope = ORIGINAL_NAMESPACE (scope);
4136 /* Algorithm: Starting with SCOPE, walk through the the set of used
4137 namespaces. For each used namespace, look through its inline
4138 namespace set for any bindings and usings. If no bindings are found,
4139 add any usings seen to the set of used namespaces. */
4140 VEC_safe_push (tree, gc, todo, scope);
4142 while (VEC_length (tree, todo))
4144 bool found_here;
4145 scope = VEC_pop (tree, todo);
4146 if (tree_vec_contains (seen, scope))
4147 continue;
4148 VEC_safe_push (tree, gc, seen, scope);
4149 VEC_safe_push (tree, gc, todo_inline, scope);
4151 found_here = false;
4152 while (VEC_length (tree, todo_inline))
4154 cxx_binding *binding;
4156 scope = VEC_pop (tree, todo_inline);
4157 if (tree_vec_contains (seen_inline, scope))
4158 continue;
4159 VEC_safe_push (tree, gc, seen_inline, scope);
4161 binding =
4162 cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (scope), name);
4163 if (binding)
4165 found_here = true;
4166 ambiguous_decl (result, binding, flags);
4169 for (usings = DECL_NAMESPACE_USING (scope); usings;
4170 usings = TREE_CHAIN (usings))
4171 if (!TREE_INDIRECT_USING (usings))
4173 if (is_associated_namespace (scope, TREE_PURPOSE (usings)))
4174 VEC_safe_push (tree, gc, todo_inline, TREE_PURPOSE (usings));
4175 else
4176 VEC_safe_push (tree, gc, todo_maybe, TREE_PURPOSE (usings));
4180 if (found_here)
4181 VEC_truncate (tree, todo_maybe, 0);
4182 else
4183 while (VEC_length (tree, todo_maybe))
4184 VEC_safe_push (tree, gc, todo, VEC_pop (tree, todo_maybe));
4186 VEC_free (tree,gc,todo);
4187 VEC_free (tree,gc,todo_maybe);
4188 VEC_free (tree,gc,todo_inline);
4189 VEC_free (tree,gc,seen);
4190 VEC_free (tree,gc,seen_inline);
4191 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, result->value != error_mark_node);
4194 /* Subroutine of outer_binding.
4195 Returns TRUE if BINDING is a binding to a template parameter of SCOPE,
4196 FALSE otherwise. */
4198 static bool
4199 binding_to_template_parms_of_scope_p (cxx_binding *binding,
4200 cxx_scope *scope)
4202 tree binding_value;
4204 if (!binding || !scope)
4205 return false;
4207 binding_value = binding->value ? binding->value : binding->type;
4209 return (scope
4210 && scope->this_entity
4211 && get_template_info (scope->this_entity)
4212 && parameter_of_template_p (binding_value,
4213 TI_TEMPLATE (get_template_info \
4214 (scope->this_entity))));
4217 /* Return the innermost non-namespace binding for NAME from a scope
4218 containing BINDING, or, if BINDING is NULL, the current scope.
4219 Please note that for a given template, the template parameters are
4220 considered to be in the scope containing the current scope.
4221 If CLASS_P is false, then class bindings are ignored. */
4223 cxx_binding *
4224 outer_binding (tree name,
4225 cxx_binding *binding,
4226 bool class_p)
4228 cxx_binding *outer;
4229 cxx_scope *scope;
4230 cxx_scope *outer_scope;
4232 if (binding)
4234 scope = binding->scope->level_chain;
4235 outer = binding->previous;
4237 else
4239 scope = current_binding_level;
4240 outer = IDENTIFIER_BINDING (name);
4242 outer_scope = outer ? outer->scope : NULL;
4244 /* Because we create class bindings lazily, we might be missing a
4245 class binding for NAME. If there are any class binding levels
4246 between the LAST_BINDING_LEVEL and the scope in which OUTER was
4247 declared, we must lookup NAME in those class scopes. */
4248 if (class_p)
4249 while (scope && scope != outer_scope && scope->kind != sk_namespace)
4251 if (scope->kind == sk_class)
4253 cxx_binding *class_binding;
4255 class_binding = get_class_binding (name, scope);
4256 if (class_binding)
4258 /* Thread this new class-scope binding onto the
4259 IDENTIFIER_BINDING list so that future lookups
4260 find it quickly. */
4261 class_binding->previous = outer;
4262 if (binding)
4263 binding->previous = class_binding;
4264 else
4265 IDENTIFIER_BINDING (name) = class_binding;
4266 return class_binding;
4269 /* If we are in a member template, the template parms of the member
4270 template are considered to be inside the scope of the containing
4271 class, but within G++ the class bindings are all pushed between the
4272 template parms and the function body. So if the outer binding is
4273 a template parm for the current scope, return it now rather than
4274 look for a class binding. */
4275 if (outer_scope && outer_scope->kind == sk_template_parms
4276 && binding_to_template_parms_of_scope_p (outer, scope))
4277 return outer;
4279 scope = scope->level_chain;
4282 return outer;
4285 /* Return the innermost block-scope or class-scope value binding for
4286 NAME, or NULL_TREE if there is no such binding. */
4288 tree
4289 innermost_non_namespace_value (tree name)
4291 cxx_binding *binding;
4292 binding = outer_binding (name, /*binding=*/NULL, /*class_p=*/true);
4293 return binding ? binding->value : NULL_TREE;
4296 /* Look up NAME in the current binding level and its superiors in the
4297 namespace of variables, functions and typedefs. Return a ..._DECL
4298 node of some kind representing its definition if there is only one
4299 such declaration, or return a TREE_LIST with all the overloaded
4300 definitions if there are many, or return 0 if it is undefined.
4301 Hidden name, either friend declaration or built-in function, are
4302 not ignored.
4304 If PREFER_TYPE is > 0, we prefer TYPE_DECLs or namespaces.
4305 If PREFER_TYPE is > 1, we reject non-type decls (e.g. namespaces).
4306 Otherwise we prefer non-TYPE_DECLs.
4308 If NONCLASS is nonzero, bindings in class scopes are ignored. If
4309 BLOCK_P is false, bindings in block scopes are ignored. */
4311 tree
4312 lookup_name_real (tree name, int prefer_type, int nonclass, bool block_p,
4313 int namespaces_only, int flags)
4315 cxx_binding *iter;
4316 tree val = NULL_TREE;
4318 timevar_push (TV_NAME_LOOKUP);
4319 /* Conversion operators are handled specially because ordinary
4320 unqualified name lookup will not find template conversion
4321 operators. */
4322 if (IDENTIFIER_TYPENAME_P (name))
4324 struct cp_binding_level *level;
4326 for (level = current_binding_level;
4327 level && level->kind != sk_namespace;
4328 level = level->level_chain)
4330 tree class_type;
4331 tree operators;
4333 /* A conversion operator can only be declared in a class
4334 scope. */
4335 if (level->kind != sk_class)
4336 continue;
4338 /* Lookup the conversion operator in the class. */
4339 class_type = level->this_entity;
4340 operators = lookup_fnfields (class_type, name, /*protect=*/0);
4341 if (operators)
4342 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, operators);
4345 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
4348 flags |= lookup_flags (prefer_type, namespaces_only);
4350 /* First, look in non-namespace scopes. */
4352 if (current_class_type == NULL_TREE)
4353 nonclass = 1;
4355 if (block_p || !nonclass)
4356 for (iter = outer_binding (name, NULL, !nonclass);
4357 iter;
4358 iter = outer_binding (name, iter, !nonclass))
4360 tree binding;
4362 /* Skip entities we don't want. */
4363 if (LOCAL_BINDING_P (iter) ? !block_p : nonclass)
4364 continue;
4366 /* If this is the kind of thing we're looking for, we're done. */
4367 if (qualify_lookup (iter->value, flags))
4368 binding = iter->value;
4369 else if ((flags & LOOKUP_PREFER_TYPES)
4370 && qualify_lookup (iter->type, flags))
4371 binding = iter->type;
4372 else
4373 binding = NULL_TREE;
4375 if (binding)
4377 if (hidden_name_p (binding))
4379 /* A non namespace-scope binding can only be hidden in the
4380 presence of a local class, due to friend declarations.
4382 In particular, consider:
4384 struct C;
4385 void f() {
4386 struct A {
4387 friend struct B;
4388 friend struct C;
4389 void g() {
4390 B* b; // error: B is hidden
4391 C* c; // OK, finds ::C
4394 B *b; // error: B is hidden
4395 C *c; // OK, finds ::C
4396 struct B {};
4397 B *bb; // OK
4400 The standard says that "B" is a local class in "f"
4401 (but not nested within "A") -- but that name lookup
4402 for "B" does not find this declaration until it is
4403 declared directly with "f".
4405 In particular:
4407 [class.friend]
4409 If a friend declaration appears in a local class and
4410 the name specified is an unqualified name, a prior
4411 declaration is looked up without considering scopes
4412 that are outside the innermost enclosing non-class
4413 scope. For a friend function declaration, if there is
4414 no prior declaration, the program is ill-formed. For a
4415 friend class declaration, if there is no prior
4416 declaration, the class that is specified belongs to the
4417 innermost enclosing non-class scope, but if it is
4418 subsequently referenced, its name is not found by name
4419 lookup until a matching declaration is provided in the
4420 innermost enclosing nonclass scope.
4422 So just keep looking for a non-hidden binding.
4424 gcc_assert (TREE_CODE (binding) == TYPE_DECL);
4425 continue;
4427 val = binding;
4428 break;
4432 /* Now lookup in namespace scopes. */
4433 if (!val)
4434 val = unqualified_namespace_lookup (name, flags);
4436 /* If we have a single function from a using decl, pull it out. */
4437 if (val && TREE_CODE (val) == OVERLOAD && !really_overloaded_fn (val))
4438 val = OVL_FUNCTION (val);
4440 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
4443 tree
4444 lookup_name_nonclass (tree name)
4446 return lookup_name_real (name, 0, 1, /*block_p=*/true, 0, LOOKUP_COMPLAIN);
4449 tree
4450 lookup_function_nonclass (tree name, VEC(tree,gc) *args, bool block_p)
4452 return
4453 lookup_arg_dependent (name,
4454 lookup_name_real (name, 0, 1, block_p, 0,
4455 LOOKUP_COMPLAIN),
4456 args, false);
4459 tree
4460 lookup_name (tree name)
4462 return lookup_name_real (name, 0, 0, /*block_p=*/true, 0, LOOKUP_COMPLAIN);
4465 tree
4466 lookup_name_prefer_type (tree name, int prefer_type)
4468 return lookup_name_real (name, prefer_type, 0, /*block_p=*/true,
4469 0, LOOKUP_COMPLAIN);
4472 /* Look up NAME for type used in elaborated name specifier in
4473 the scopes given by SCOPE. SCOPE can be either TS_CURRENT or
4474 TS_WITHIN_ENCLOSING_NON_CLASS. Although not implied by the
4475 name, more scopes are checked if cleanup or template parameter
4476 scope is encountered.
4478 Unlike lookup_name_real, we make sure that NAME is actually
4479 declared in the desired scope, not from inheritance, nor using
4480 directive. For using declaration, there is DR138 still waiting
4481 to be resolved. Hidden name coming from an earlier friend
4482 declaration is also returned.
4484 A TYPE_DECL best matching the NAME is returned. Catching error
4485 and issuing diagnostics are caller's responsibility. */
4487 tree
4488 lookup_type_scope (tree name, tag_scope scope)
4490 cxx_binding *iter = NULL;
4491 tree val = NULL_TREE;
4493 timevar_push (TV_NAME_LOOKUP);
4495 /* Look in non-namespace scope first. */
4496 if (current_binding_level->kind != sk_namespace)
4497 iter = outer_binding (name, NULL, /*class_p=*/ true);
4498 for (; iter; iter = outer_binding (name, iter, /*class_p=*/ true))
4500 /* Check if this is the kind of thing we're looking for.
4501 If SCOPE is TS_CURRENT, also make sure it doesn't come from
4502 base class. For ITER->VALUE, we can simply use
4503 INHERITED_VALUE_BINDING_P. For ITER->TYPE, we have to use
4504 our own check.
4506 We check ITER->TYPE before ITER->VALUE in order to handle
4507 typedef struct C {} C;
4508 correctly. */
4510 if (qualify_lookup (iter->type, LOOKUP_PREFER_TYPES)
4511 && (scope != ts_current
4512 || LOCAL_BINDING_P (iter)
4513 || DECL_CONTEXT (iter->type) == iter->scope->this_entity))
4514 val = iter->type;
4515 else if ((scope != ts_current
4516 || !INHERITED_VALUE_BINDING_P (iter))
4517 && qualify_lookup (iter->value, LOOKUP_PREFER_TYPES))
4518 val = iter->value;
4520 if (val)
4521 break;
4524 /* Look in namespace scope. */
4525 if (!val)
4527 iter = cxx_scope_find_binding_for_name
4528 (NAMESPACE_LEVEL (current_decl_namespace ()), name);
4530 if (iter)
4532 /* If this is the kind of thing we're looking for, we're done. */
4533 if (qualify_lookup (iter->type, LOOKUP_PREFER_TYPES))
4534 val = iter->type;
4535 else if (qualify_lookup (iter->value, LOOKUP_PREFER_TYPES))
4536 val = iter->value;
4541 /* Type found, check if it is in the allowed scopes, ignoring cleanup
4542 and template parameter scopes. */
4543 if (val)
4545 struct cp_binding_level *b = current_binding_level;
4546 while (b)
4548 if (iter->scope == b)
4549 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
4551 if (b->kind == sk_cleanup || b->kind == sk_template_parms
4552 || b->kind == sk_function_parms)
4553 b = b->level_chain;
4554 else if (b->kind == sk_class
4555 && scope == ts_within_enclosing_non_class)
4556 b = b->level_chain;
4557 else
4558 break;
4562 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
4565 /* Similar to `lookup_name' but look only in the innermost non-class
4566 binding level. */
4568 tree
4569 lookup_name_innermost_nonclass_level (tree name)
4571 struct cp_binding_level *b;
4572 tree t = NULL_TREE;
4574 timevar_push (TV_NAME_LOOKUP);
4575 b = innermost_nonclass_level ();
4577 if (b->kind == sk_namespace)
4579 t = IDENTIFIER_NAMESPACE_VALUE (name);
4581 /* extern "C" function() */
4582 if (t != NULL_TREE && TREE_CODE (t) == TREE_LIST)
4583 t = TREE_VALUE (t);
4585 else if (IDENTIFIER_BINDING (name)
4586 && LOCAL_BINDING_P (IDENTIFIER_BINDING (name)))
4588 cxx_binding *binding;
4589 binding = IDENTIFIER_BINDING (name);
4590 while (1)
4592 if (binding->scope == b
4593 && !(TREE_CODE (binding->value) == VAR_DECL
4594 && DECL_DEAD_FOR_LOCAL (binding->value)))
4595 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, binding->value);
4597 if (b->kind == sk_cleanup)
4598 b = b->level_chain;
4599 else
4600 break;
4604 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
4607 /* Returns true iff DECL is a block-scope extern declaration of a function
4608 or variable. */
4610 bool
4611 is_local_extern (tree decl)
4613 cxx_binding *binding;
4615 /* For functions, this is easy. */
4616 if (TREE_CODE (decl) == FUNCTION_DECL)
4617 return DECL_LOCAL_FUNCTION_P (decl);
4619 if (TREE_CODE (decl) != VAR_DECL)
4620 return false;
4621 if (!current_function_decl)
4622 return false;
4624 /* For variables, this is not easy. We need to look at the binding stack
4625 for the identifier to see whether the decl we have is a local. */
4626 for (binding = IDENTIFIER_BINDING (DECL_NAME (decl));
4627 binding && binding->scope->kind != sk_namespace;
4628 binding = binding->previous)
4629 if (binding->value == decl)
4630 return LOCAL_BINDING_P (binding);
4632 return false;
4635 /* Like lookup_name_innermost_nonclass_level, but for types. */
4637 static tree
4638 lookup_type_current_level (tree name)
4640 tree t = NULL_TREE;
4642 timevar_push (TV_NAME_LOOKUP);
4643 gcc_assert (current_binding_level->kind != sk_namespace);
4645 if (REAL_IDENTIFIER_TYPE_VALUE (name) != NULL_TREE
4646 && REAL_IDENTIFIER_TYPE_VALUE (name) != global_type_node)
4648 struct cp_binding_level *b = current_binding_level;
4649 while (1)
4651 if (purpose_member (name, b->type_shadowed))
4652 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP,
4653 REAL_IDENTIFIER_TYPE_VALUE (name));
4654 if (b->kind == sk_cleanup)
4655 b = b->level_chain;
4656 else
4657 break;
4661 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
4664 /* [basic.lookup.koenig] */
4665 /* A nonzero return value in the functions below indicates an error. */
4667 struct arg_lookup
4669 tree name;
4670 VEC(tree,gc) *args;
4671 VEC(tree,gc) *namespaces;
4672 VEC(tree,gc) *classes;
4673 tree functions;
4676 static bool arg_assoc (struct arg_lookup*, tree);
4677 static bool arg_assoc_args (struct arg_lookup*, tree);
4678 static bool arg_assoc_args_vec (struct arg_lookup*, VEC(tree,gc) *);
4679 static bool arg_assoc_type (struct arg_lookup*, tree);
4680 static bool add_function (struct arg_lookup *, tree);
4681 static bool arg_assoc_namespace (struct arg_lookup *, tree);
4682 static bool arg_assoc_class_only (struct arg_lookup *, tree);
4683 static bool arg_assoc_bases (struct arg_lookup *, tree);
4684 static bool arg_assoc_class (struct arg_lookup *, tree);
4685 static bool arg_assoc_template_arg (struct arg_lookup*, tree);
4687 /* Add a function to the lookup structure.
4688 Returns true on error. */
4690 static bool
4691 add_function (struct arg_lookup *k, tree fn)
4693 /* We used to check here to see if the function was already in the list,
4694 but that's O(n^2), which is just too expensive for function lookup.
4695 Now we deal with the occasional duplicate in joust. In doing this, we
4696 assume that the number of duplicates will be small compared to the
4697 total number of functions being compared, which should usually be the
4698 case. */
4700 if (!is_overloaded_fn (fn))
4701 /* All names except those of (possibly overloaded) functions and
4702 function templates are ignored. */;
4703 else if (!k->functions)
4704 k->functions = fn;
4705 else if (fn == k->functions)
4707 else
4708 k->functions = build_overload (fn, k->functions);
4710 return false;
4713 /* Returns true iff CURRENT has declared itself to be an associated
4714 namespace of SCOPE via a strong using-directive (or transitive chain
4715 thereof). Both are namespaces. */
4717 bool
4718 is_associated_namespace (tree current, tree scope)
4720 VEC(tree,gc) *seen = make_tree_vector ();
4721 VEC(tree,gc) *todo = make_tree_vector ();
4722 tree t;
4723 bool ret;
4725 while (1)
4727 if (scope == current)
4729 ret = true;
4730 break;
4732 VEC_safe_push (tree, gc, seen, scope);
4733 for (t = DECL_NAMESPACE_ASSOCIATIONS (scope); t; t = TREE_CHAIN (t))
4734 if (!vec_member (TREE_PURPOSE (t), seen))
4735 VEC_safe_push (tree, gc, todo, TREE_PURPOSE (t));
4736 if (!VEC_empty (tree, todo))
4738 scope = VEC_last (tree, todo);
4739 VEC_pop (tree, todo);
4741 else
4743 ret = false;
4744 break;
4748 release_tree_vector (seen);
4749 release_tree_vector (todo);
4751 return ret;
4754 /* Add functions of a namespace to the lookup structure.
4755 Returns true on error. */
4757 static bool
4758 arg_assoc_namespace (struct arg_lookup *k, tree scope)
4760 tree value;
4762 if (vec_member (scope, k->namespaces))
4763 return false;
4764 VEC_safe_push (tree, gc, k->namespaces, scope);
4766 /* Check out our super-users. */
4767 for (value = DECL_NAMESPACE_ASSOCIATIONS (scope); value;
4768 value = TREE_CHAIN (value))
4769 if (arg_assoc_namespace (k, TREE_PURPOSE (value)))
4770 return true;
4772 /* Also look down into inline namespaces. */
4773 for (value = DECL_NAMESPACE_USING (scope); value;
4774 value = TREE_CHAIN (value))
4775 if (is_associated_namespace (scope, TREE_PURPOSE (value)))
4776 if (arg_assoc_namespace (k, TREE_PURPOSE (value)))
4777 return true;
4779 value = namespace_binding (k->name, scope);
4780 if (!value)
4781 return false;
4783 for (; value; value = OVL_NEXT (value))
4785 /* We don't want to find arbitrary hidden functions via argument
4786 dependent lookup. We only want to find friends of associated
4787 classes, which we'll do via arg_assoc_class. */
4788 if (hidden_name_p (OVL_CURRENT (value)))
4789 continue;
4791 if (add_function (k, OVL_CURRENT (value)))
4792 return true;
4795 return false;
4798 /* Adds everything associated with a template argument to the lookup
4799 structure. Returns true on error. */
4801 static bool
4802 arg_assoc_template_arg (struct arg_lookup *k, tree arg)
4804 /* [basic.lookup.koenig]
4806 If T is a template-id, its associated namespaces and classes are
4807 ... the namespaces and classes associated with the types of the
4808 template arguments provided for template type parameters
4809 (excluding template template parameters); the namespaces in which
4810 any template template arguments are defined; and the classes in
4811 which any member templates used as template template arguments
4812 are defined. [Note: non-type template arguments do not
4813 contribute to the set of associated namespaces. ] */
4815 /* Consider first template template arguments. */
4816 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
4817 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
4818 return false;
4819 else if (TREE_CODE (arg) == TEMPLATE_DECL)
4821 tree ctx = CP_DECL_CONTEXT (arg);
4823 /* It's not a member template. */
4824 if (TREE_CODE (ctx) == NAMESPACE_DECL)
4825 return arg_assoc_namespace (k, ctx);
4826 /* Otherwise, it must be member template. */
4827 else
4828 return arg_assoc_class_only (k, ctx);
4830 /* It's an argument pack; handle it recursively. */
4831 else if (ARGUMENT_PACK_P (arg))
4833 tree args = ARGUMENT_PACK_ARGS (arg);
4834 int i, len = TREE_VEC_LENGTH (args);
4835 for (i = 0; i < len; ++i)
4836 if (arg_assoc_template_arg (k, TREE_VEC_ELT (args, i)))
4837 return true;
4839 return false;
4841 /* It's not a template template argument, but it is a type template
4842 argument. */
4843 else if (TYPE_P (arg))
4844 return arg_assoc_type (k, arg);
4845 /* It's a non-type template argument. */
4846 else
4847 return false;
4850 /* Adds the class and its friends to the lookup structure.
4851 Returns true on error. */
4853 static bool
4854 arg_assoc_class_only (struct arg_lookup *k, tree type)
4856 tree list, friends, context;
4858 /* Backend-built structures, such as __builtin_va_list, aren't
4859 affected by all this. */
4860 if (!CLASS_TYPE_P (type))
4861 return false;
4863 context = decl_namespace_context (type);
4864 if (arg_assoc_namespace (k, context))
4865 return true;
4867 complete_type (type);
4869 /* Process friends. */
4870 for (list = DECL_FRIENDLIST (TYPE_MAIN_DECL (type)); list;
4871 list = TREE_CHAIN (list))
4872 if (k->name == FRIEND_NAME (list))
4873 for (friends = FRIEND_DECLS (list); friends;
4874 friends = TREE_CHAIN (friends))
4876 tree fn = TREE_VALUE (friends);
4878 /* Only interested in global functions with potentially hidden
4879 (i.e. unqualified) declarations. */
4880 if (CP_DECL_CONTEXT (fn) != context)
4881 continue;
4882 /* Template specializations are never found by name lookup.
4883 (Templates themselves can be found, but not template
4884 specializations.) */
4885 if (TREE_CODE (fn) == FUNCTION_DECL && DECL_USE_TEMPLATE (fn))
4886 continue;
4887 if (add_function (k, fn))
4888 return true;
4891 return false;
4894 /* Adds the class and its bases to the lookup structure.
4895 Returns true on error. */
4897 static bool
4898 arg_assoc_bases (struct arg_lookup *k, tree type)
4900 if (arg_assoc_class_only (k, type))
4901 return true;
4903 if (TYPE_BINFO (type))
4905 /* Process baseclasses. */
4906 tree binfo, base_binfo;
4907 int i;
4909 for (binfo = TYPE_BINFO (type), i = 0;
4910 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
4911 if (arg_assoc_bases (k, BINFO_TYPE (base_binfo)))
4912 return true;
4915 return false;
4918 /* Adds everything associated with a class argument type to the lookup
4919 structure. Returns true on error.
4921 If T is a class type (including unions), its associated classes are: the
4922 class itself; the class of which it is a member, if any; and its direct
4923 and indirect base classes. Its associated namespaces are the namespaces
4924 of which its associated classes are members. Furthermore, if T is a
4925 class template specialization, its associated namespaces and classes
4926 also include: the namespaces and classes associated with the types of
4927 the template arguments provided for template type parameters (excluding
4928 template template parameters); the namespaces of which any template
4929 template arguments are members; and the classes of which any member
4930 templates used as template template arguments are members. [ Note:
4931 non-type template arguments do not contribute to the set of associated
4932 namespaces. --end note] */
4934 static bool
4935 arg_assoc_class (struct arg_lookup *k, tree type)
4937 tree list;
4938 int i;
4940 /* Backend build structures, such as __builtin_va_list, aren't
4941 affected by all this. */
4942 if (!CLASS_TYPE_P (type))
4943 return false;
4945 if (vec_member (type, k->classes))
4946 return false;
4947 VEC_safe_push (tree, gc, k->classes, type);
4949 if (TYPE_CLASS_SCOPE_P (type)
4950 && arg_assoc_class_only (k, TYPE_CONTEXT (type)))
4951 return true;
4953 if (arg_assoc_bases (k, type))
4954 return true;
4956 /* Process template arguments. */
4957 if (CLASSTYPE_TEMPLATE_INFO (type)
4958 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
4960 list = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
4961 for (i = 0; i < TREE_VEC_LENGTH (list); ++i)
4962 if (arg_assoc_template_arg (k, TREE_VEC_ELT (list, i)))
4963 return true;
4966 return false;
4969 /* Adds everything associated with a given type.
4970 Returns 1 on error. */
4972 static bool
4973 arg_assoc_type (struct arg_lookup *k, tree type)
4975 /* As we do not get the type of non-type dependent expressions
4976 right, we can end up with such things without a type. */
4977 if (!type)
4978 return false;
4980 if (TYPE_PTRMEM_P (type))
4982 /* Pointer to member: associate class type and value type. */
4983 if (arg_assoc_type (k, TYPE_PTRMEM_CLASS_TYPE (type)))
4984 return true;
4985 return arg_assoc_type (k, TYPE_PTRMEM_POINTED_TO_TYPE (type));
4987 else switch (TREE_CODE (type))
4989 case ERROR_MARK:
4990 return false;
4991 case VOID_TYPE:
4992 case INTEGER_TYPE:
4993 case REAL_TYPE:
4994 case COMPLEX_TYPE:
4995 case VECTOR_TYPE:
4996 case BOOLEAN_TYPE:
4997 case FIXED_POINT_TYPE:
4998 case DECLTYPE_TYPE:
4999 case NULLPTR_TYPE:
5000 return false;
5001 case RECORD_TYPE:
5002 if (TYPE_PTRMEMFUNC_P (type))
5003 return arg_assoc_type (k, TYPE_PTRMEMFUNC_FN_TYPE (type));
5004 case UNION_TYPE:
5005 return arg_assoc_class (k, type);
5006 case POINTER_TYPE:
5007 case REFERENCE_TYPE:
5008 case ARRAY_TYPE:
5009 return arg_assoc_type (k, TREE_TYPE (type));
5010 case ENUMERAL_TYPE:
5011 if (TYPE_CLASS_SCOPE_P (type)
5012 && arg_assoc_class_only (k, TYPE_CONTEXT (type)))
5013 return true;
5014 return arg_assoc_namespace (k, decl_namespace_context (type));
5015 case METHOD_TYPE:
5016 /* The basetype is referenced in the first arg type, so just
5017 fall through. */
5018 case FUNCTION_TYPE:
5019 /* Associate the parameter types. */
5020 if (arg_assoc_args (k, TYPE_ARG_TYPES (type)))
5021 return true;
5022 /* Associate the return type. */
5023 return arg_assoc_type (k, TREE_TYPE (type));
5024 case TEMPLATE_TYPE_PARM:
5025 case BOUND_TEMPLATE_TEMPLATE_PARM:
5026 return false;
5027 case TYPENAME_TYPE:
5028 return false;
5029 case LANG_TYPE:
5030 gcc_assert (type == unknown_type_node
5031 || type == init_list_type_node);
5032 return false;
5033 case TYPE_PACK_EXPANSION:
5034 return arg_assoc_type (k, PACK_EXPANSION_PATTERN (type));
5036 default:
5037 gcc_unreachable ();
5039 return false;
5042 /* Adds everything associated with arguments. Returns true on error. */
5044 static bool
5045 arg_assoc_args (struct arg_lookup *k, tree args)
5047 for (; args; args = TREE_CHAIN (args))
5048 if (arg_assoc (k, TREE_VALUE (args)))
5049 return true;
5050 return false;
5053 /* Adds everything associated with an argument vector. Returns true
5054 on error. */
5056 static bool
5057 arg_assoc_args_vec (struct arg_lookup *k, VEC(tree,gc) *args)
5059 unsigned int ix;
5060 tree arg;
5062 FOR_EACH_VEC_ELT (tree, args, ix, arg)
5063 if (arg_assoc (k, arg))
5064 return true;
5065 return false;
5068 /* Adds everything associated with a given tree_node. Returns 1 on error. */
5070 static bool
5071 arg_assoc (struct arg_lookup *k, tree n)
5073 if (n == error_mark_node)
5074 return false;
5076 if (TYPE_P (n))
5077 return arg_assoc_type (k, n);
5079 if (! type_unknown_p (n))
5080 return arg_assoc_type (k, TREE_TYPE (n));
5082 if (TREE_CODE (n) == ADDR_EXPR)
5083 n = TREE_OPERAND (n, 0);
5084 if (TREE_CODE (n) == COMPONENT_REF)
5085 n = TREE_OPERAND (n, 1);
5086 if (TREE_CODE (n) == OFFSET_REF)
5087 n = TREE_OPERAND (n, 1);
5088 while (TREE_CODE (n) == TREE_LIST)
5089 n = TREE_VALUE (n);
5090 if (TREE_CODE (n) == BASELINK)
5091 n = BASELINK_FUNCTIONS (n);
5093 if (TREE_CODE (n) == FUNCTION_DECL)
5094 return arg_assoc_type (k, TREE_TYPE (n));
5095 if (TREE_CODE (n) == TEMPLATE_ID_EXPR)
5097 /* The working paper doesn't currently say how to handle template-id
5098 arguments. The sensible thing would seem to be to handle the list
5099 of template candidates like a normal overload set, and handle the
5100 template arguments like we do for class template
5101 specializations. */
5102 tree templ = TREE_OPERAND (n, 0);
5103 tree args = TREE_OPERAND (n, 1);
5104 int ix;
5106 /* First the templates. */
5107 if (arg_assoc (k, templ))
5108 return true;
5110 /* Now the arguments. */
5111 if (args)
5112 for (ix = TREE_VEC_LENGTH (args); ix--;)
5113 if (arg_assoc_template_arg (k, TREE_VEC_ELT (args, ix)) == 1)
5114 return true;
5116 else if (TREE_CODE (n) == OVERLOAD)
5118 for (; n; n = OVL_CHAIN (n))
5119 if (arg_assoc_type (k, TREE_TYPE (OVL_FUNCTION (n))))
5120 return true;
5123 return false;
5126 /* Performs Koenig lookup depending on arguments, where fns
5127 are the functions found in normal lookup. */
5129 tree
5130 lookup_arg_dependent (tree name, tree fns, VEC(tree,gc) *args,
5131 bool include_std)
5133 struct arg_lookup k;
5135 timevar_push (TV_NAME_LOOKUP);
5137 /* Remove any hidden friend functions from the list of functions
5138 found so far. They will be added back by arg_assoc_class as
5139 appropriate. */
5140 fns = remove_hidden_names (fns);
5142 k.name = name;
5143 k.args = args;
5144 k.functions = fns;
5145 k.classes = make_tree_vector ();
5147 /* We previously performed an optimization here by setting
5148 NAMESPACES to the current namespace when it was safe. However, DR
5149 164 says that namespaces that were already searched in the first
5150 stage of template processing are searched again (potentially
5151 picking up later definitions) in the second stage. */
5152 k.namespaces = make_tree_vector ();
5154 if (include_std)
5155 arg_assoc_namespace (&k, std_node);
5156 arg_assoc_args_vec (&k, args);
5158 fns = k.functions;
5160 if (fns
5161 && TREE_CODE (fns) != VAR_DECL
5162 && !is_overloaded_fn (fns))
5164 error ("argument dependent lookup finds %q+D", fns);
5165 error (" in call to %qD", name);
5166 fns = error_mark_node;
5169 release_tree_vector (k.classes);
5170 release_tree_vector (k.namespaces);
5172 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, fns);
5175 /* Add namespace to using_directives. Return NULL_TREE if nothing was
5176 changed (i.e. there was already a directive), or the fresh
5177 TREE_LIST otherwise. */
5179 static tree
5180 push_using_directive (tree used)
5182 tree ud = current_binding_level->using_directives;
5183 tree iter, ancestor;
5185 timevar_push (TV_NAME_LOOKUP);
5186 /* Check if we already have this. */
5187 if (purpose_member (used, ud) != NULL_TREE)
5188 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
5190 ancestor = namespace_ancestor (current_decl_namespace (), used);
5191 ud = current_binding_level->using_directives;
5192 ud = tree_cons (used, ancestor, ud);
5193 current_binding_level->using_directives = ud;
5195 /* Recursively add all namespaces used. */
5196 for (iter = DECL_NAMESPACE_USING (used); iter; iter = TREE_CHAIN (iter))
5197 push_using_directive (TREE_PURPOSE (iter));
5199 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, ud);
5202 /* The type TYPE is being declared. If it is a class template, or a
5203 specialization of a class template, do any processing required and
5204 perform error-checking. If IS_FRIEND is nonzero, this TYPE is
5205 being declared a friend. B is the binding level at which this TYPE
5206 should be bound.
5208 Returns the TYPE_DECL for TYPE, which may have been altered by this
5209 processing. */
5211 static tree
5212 maybe_process_template_type_declaration (tree type, int is_friend,
5213 cxx_scope *b)
5215 tree decl = TYPE_NAME (type);
5217 if (processing_template_parmlist)
5218 /* You can't declare a new template type in a template parameter
5219 list. But, you can declare a non-template type:
5221 template <class A*> struct S;
5223 is a forward-declaration of `A'. */
5225 else if (b->kind == sk_namespace
5226 && current_binding_level->kind != sk_namespace)
5227 /* If this new type is being injected into a containing scope,
5228 then it's not a template type. */
5230 else
5232 gcc_assert (MAYBE_CLASS_TYPE_P (type)
5233 || TREE_CODE (type) == ENUMERAL_TYPE);
5235 if (processing_template_decl)
5237 /* This may change after the call to
5238 push_template_decl_real, but we want the original value. */
5239 tree name = DECL_NAME (decl);
5241 decl = push_template_decl_real (decl, is_friend);
5242 if (decl == error_mark_node)
5243 return error_mark_node;
5245 /* If the current binding level is the binding level for the
5246 template parameters (see the comment in
5247 begin_template_parm_list) and the enclosing level is a class
5248 scope, and we're not looking at a friend, push the
5249 declaration of the member class into the class scope. In the
5250 friend case, push_template_decl will already have put the
5251 friend into global scope, if appropriate. */
5252 if (TREE_CODE (type) != ENUMERAL_TYPE
5253 && !is_friend && b->kind == sk_template_parms
5254 && b->level_chain->kind == sk_class)
5256 finish_member_declaration (CLASSTYPE_TI_TEMPLATE (type));
5258 if (!COMPLETE_TYPE_P (current_class_type))
5260 maybe_add_class_template_decl_list (current_class_type,
5261 type, /*friend_p=*/0);
5262 /* Put this UTD in the table of UTDs for the class. */
5263 if (CLASSTYPE_NESTED_UTDS (current_class_type) == NULL)
5264 CLASSTYPE_NESTED_UTDS (current_class_type) =
5265 binding_table_new (SCOPE_DEFAULT_HT_SIZE);
5267 binding_table_insert
5268 (CLASSTYPE_NESTED_UTDS (current_class_type), name, type);
5274 return decl;
5277 /* Push a tag name NAME for struct/class/union/enum type TYPE. In case
5278 that the NAME is a class template, the tag is processed but not pushed.
5280 The pushed scope depend on the SCOPE parameter:
5281 - When SCOPE is TS_CURRENT, put it into the inner-most non-sk_cleanup
5282 scope.
5283 - When SCOPE is TS_GLOBAL, put it in the inner-most non-class and
5284 non-template-parameter scope. This case is needed for forward
5285 declarations.
5286 - When SCOPE is TS_WITHIN_ENCLOSING_NON_CLASS, this is similar to
5287 TS_GLOBAL case except that names within template-parameter scopes
5288 are not pushed at all.
5290 Returns TYPE upon success and ERROR_MARK_NODE otherwise. */
5292 tree
5293 pushtag (tree name, tree type, tag_scope scope)
5295 struct cp_binding_level *b;
5296 tree decl;
5298 timevar_push (TV_NAME_LOOKUP);
5299 b = current_binding_level;
5300 while (/* Cleanup scopes are not scopes from the point of view of
5301 the language. */
5302 b->kind == sk_cleanup
5303 /* Neither are function parameter scopes. */
5304 || b->kind == sk_function_parms
5305 /* Neither are the scopes used to hold template parameters
5306 for an explicit specialization. For an ordinary template
5307 declaration, these scopes are not scopes from the point of
5308 view of the language. */
5309 || (b->kind == sk_template_parms
5310 && (b->explicit_spec_p || scope == ts_global))
5311 || (b->kind == sk_class
5312 && (scope != ts_current
5313 /* We may be defining a new type in the initializer
5314 of a static member variable. We allow this when
5315 not pedantic, and it is particularly useful for
5316 type punning via an anonymous union. */
5317 || COMPLETE_TYPE_P (b->this_entity))))
5318 b = b->level_chain;
5320 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
5322 /* Do C++ gratuitous typedefing. */
5323 if (IDENTIFIER_TYPE_VALUE (name) != type)
5325 tree tdef;
5326 int in_class = 0;
5327 tree context = TYPE_CONTEXT (type);
5329 if (! context)
5331 tree cs = current_scope ();
5333 if (scope == ts_current
5334 || (cs && TREE_CODE (cs) == FUNCTION_DECL))
5335 context = cs;
5336 else if (cs != NULL_TREE && TYPE_P (cs))
5337 /* When declaring a friend class of a local class, we want
5338 to inject the newly named class into the scope
5339 containing the local class, not the namespace
5340 scope. */
5341 context = decl_function_context (get_type_decl (cs));
5343 if (!context)
5344 context = current_namespace;
5346 if (b->kind == sk_class
5347 || (b->kind == sk_template_parms
5348 && b->level_chain->kind == sk_class))
5349 in_class = 1;
5351 if (current_lang_name == lang_name_java)
5352 TYPE_FOR_JAVA (type) = 1;
5354 tdef = create_implicit_typedef (name, type);
5355 DECL_CONTEXT (tdef) = FROB_CONTEXT (context);
5356 if (scope == ts_within_enclosing_non_class)
5358 /* This is a friend. Make this TYPE_DECL node hidden from
5359 ordinary name lookup. Its corresponding TEMPLATE_DECL
5360 will be marked in push_template_decl_real. */
5361 retrofit_lang_decl (tdef);
5362 DECL_ANTICIPATED (tdef) = 1;
5363 DECL_FRIEND_P (tdef) = 1;
5366 decl = maybe_process_template_type_declaration
5367 (type, scope == ts_within_enclosing_non_class, b);
5368 if (decl == error_mark_node)
5369 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
5371 if (b->kind == sk_class)
5373 if (!TYPE_BEING_DEFINED (current_class_type))
5374 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
5376 if (!PROCESSING_REAL_TEMPLATE_DECL_P ())
5377 /* Put this TYPE_DECL on the TYPE_FIELDS list for the
5378 class. But if it's a member template class, we want
5379 the TEMPLATE_DECL, not the TYPE_DECL, so this is done
5380 later. */
5381 finish_member_declaration (decl);
5382 else
5383 pushdecl_class_level (decl);
5385 else if (b->kind != sk_template_parms)
5387 decl = pushdecl_with_scope (decl, b, /*is_friend=*/false);
5388 if (decl == error_mark_node)
5389 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
5392 if (! in_class)
5393 set_identifier_type_value_with_scope (name, tdef, b);
5395 TYPE_CONTEXT (type) = DECL_CONTEXT (decl);
5397 /* If this is a local class, keep track of it. We need this
5398 information for name-mangling, and so that it is possible to
5399 find all function definitions in a translation unit in a
5400 convenient way. (It's otherwise tricky to find a member
5401 function definition it's only pointed to from within a local
5402 class.) */
5403 if (TYPE_CONTEXT (type)
5404 && TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL)
5405 VEC_safe_push (tree, gc, local_classes, type);
5407 if (b->kind == sk_class
5408 && !COMPLETE_TYPE_P (current_class_type))
5410 maybe_add_class_template_decl_list (current_class_type,
5411 type, /*friend_p=*/0);
5413 if (CLASSTYPE_NESTED_UTDS (current_class_type) == NULL)
5414 CLASSTYPE_NESTED_UTDS (current_class_type)
5415 = binding_table_new (SCOPE_DEFAULT_HT_SIZE);
5417 binding_table_insert
5418 (CLASSTYPE_NESTED_UTDS (current_class_type), name, type);
5421 decl = TYPE_NAME (type);
5422 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
5424 /* Set type visibility now if this is a forward declaration. */
5425 TREE_PUBLIC (decl) = 1;
5426 determine_visibility (decl);
5428 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, type);
5431 /* Subroutines for reverting temporarily to top-level for instantiation
5432 of templates and such. We actually need to clear out the class- and
5433 local-value slots of all identifiers, so that only the global values
5434 are at all visible. Simply setting current_binding_level to the global
5435 scope isn't enough, because more binding levels may be pushed. */
5436 struct saved_scope *scope_chain;
5438 /* If ID has not already been marked, add an appropriate binding to
5439 *OLD_BINDINGS. */
5441 static void
5442 store_binding (tree id, VEC(cxx_saved_binding,gc) **old_bindings)
5444 cxx_saved_binding *saved;
5446 if (!id || !IDENTIFIER_BINDING (id))
5447 return;
5449 if (IDENTIFIER_MARKED (id))
5450 return;
5452 IDENTIFIER_MARKED (id) = 1;
5454 saved = VEC_safe_push (cxx_saved_binding, gc, *old_bindings, NULL);
5455 saved->identifier = id;
5456 saved->binding = IDENTIFIER_BINDING (id);
5457 saved->real_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
5458 IDENTIFIER_BINDING (id) = NULL;
5461 static void
5462 store_bindings (tree names, VEC(cxx_saved_binding,gc) **old_bindings)
5464 tree t;
5466 timevar_push (TV_NAME_LOOKUP);
5467 for (t = names; t; t = TREE_CHAIN (t))
5469 tree id;
5471 if (TREE_CODE (t) == TREE_LIST)
5472 id = TREE_PURPOSE (t);
5473 else
5474 id = DECL_NAME (t);
5476 store_binding (id, old_bindings);
5478 timevar_pop (TV_NAME_LOOKUP);
5481 /* Like store_bindings, but NAMES is a vector of cp_class_binding
5482 objects, rather than a TREE_LIST. */
5484 static void
5485 store_class_bindings (VEC(cp_class_binding,gc) *names,
5486 VEC(cxx_saved_binding,gc) **old_bindings)
5488 size_t i;
5489 cp_class_binding *cb;
5491 timevar_push (TV_NAME_LOOKUP);
5492 for (i = 0; VEC_iterate(cp_class_binding, names, i, cb); ++i)
5493 store_binding (cb->identifier, old_bindings);
5494 timevar_pop (TV_NAME_LOOKUP);
5497 void
5498 push_to_top_level (void)
5500 struct saved_scope *s;
5501 struct cp_binding_level *b;
5502 cxx_saved_binding *sb;
5503 size_t i;
5504 bool need_pop;
5506 timevar_push (TV_NAME_LOOKUP);
5507 s = ggc_alloc_cleared_saved_scope ();
5509 b = scope_chain ? current_binding_level : 0;
5511 /* If we're in the middle of some function, save our state. */
5512 if (cfun)
5514 need_pop = true;
5515 push_function_context ();
5517 else
5518 need_pop = false;
5520 if (scope_chain && previous_class_level)
5521 store_class_bindings (previous_class_level->class_shadowed,
5522 &s->old_bindings);
5524 /* Have to include the global scope, because class-scope decls
5525 aren't listed anywhere useful. */
5526 for (; b; b = b->level_chain)
5528 tree t;
5530 /* Template IDs are inserted into the global level. If they were
5531 inserted into namespace level, finish_file wouldn't find them
5532 when doing pending instantiations. Therefore, don't stop at
5533 namespace level, but continue until :: . */
5534 if (global_scope_p (b))
5535 break;
5537 store_bindings (b->names, &s->old_bindings);
5538 /* We also need to check class_shadowed to save class-level type
5539 bindings, since pushclass doesn't fill in b->names. */
5540 if (b->kind == sk_class)
5541 store_class_bindings (b->class_shadowed, &s->old_bindings);
5543 /* Unwind type-value slots back to top level. */
5544 for (t = b->type_shadowed; t; t = TREE_CHAIN (t))
5545 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (t), TREE_VALUE (t));
5548 FOR_EACH_VEC_ELT (cxx_saved_binding, s->old_bindings, i, sb)
5549 IDENTIFIER_MARKED (sb->identifier) = 0;
5551 s->prev = scope_chain;
5552 s->bindings = b;
5553 s->need_pop_function_context = need_pop;
5554 s->function_decl = current_function_decl;
5555 s->unevaluated_operand = cp_unevaluated_operand;
5556 s->inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
5558 scope_chain = s;
5559 current_function_decl = NULL_TREE;
5560 current_lang_base = VEC_alloc (tree, gc, 10);
5561 current_lang_name = lang_name_cplusplus;
5562 current_namespace = global_namespace;
5563 push_class_stack ();
5564 cp_unevaluated_operand = 0;
5565 c_inhibit_evaluation_warnings = 0;
5566 timevar_pop (TV_NAME_LOOKUP);
5569 void
5570 pop_from_top_level (void)
5572 struct saved_scope *s = scope_chain;
5573 cxx_saved_binding *saved;
5574 size_t i;
5576 timevar_push (TV_NAME_LOOKUP);
5577 /* Clear out class-level bindings cache. */
5578 if (previous_class_level)
5579 invalidate_class_lookup_cache ();
5580 pop_class_stack ();
5582 current_lang_base = 0;
5584 scope_chain = s->prev;
5585 FOR_EACH_VEC_ELT (cxx_saved_binding, s->old_bindings, i, saved)
5587 tree id = saved->identifier;
5589 IDENTIFIER_BINDING (id) = saved->binding;
5590 SET_IDENTIFIER_TYPE_VALUE (id, saved->real_type_value);
5593 /* If we were in the middle of compiling a function, restore our
5594 state. */
5595 if (s->need_pop_function_context)
5596 pop_function_context ();
5597 current_function_decl = s->function_decl;
5598 cp_unevaluated_operand = s->unevaluated_operand;
5599 c_inhibit_evaluation_warnings = s->inhibit_evaluation_warnings;
5600 timevar_pop (TV_NAME_LOOKUP);
5603 /* Pop off extraneous binding levels left over due to syntax errors.
5605 We don't pop past namespaces, as they might be valid. */
5607 void
5608 pop_everything (void)
5610 if (ENABLE_SCOPE_CHECKING)
5611 verbatim ("XXX entering pop_everything ()\n");
5612 while (!toplevel_bindings_p ())
5614 if (current_binding_level->kind == sk_class)
5615 pop_nested_class ();
5616 else
5617 poplevel (0, 0, 0);
5619 if (ENABLE_SCOPE_CHECKING)
5620 verbatim ("XXX leaving pop_everything ()\n");
5623 /* Emit debugging information for using declarations and directives.
5624 If input tree is overloaded fn then emit debug info for all
5625 candidates. */
5627 void
5628 cp_emit_debug_info_for_using (tree t, tree context)
5630 /* Don't try to emit any debug information if we have errors. */
5631 if (seen_error ())
5632 return;
5634 /* Ignore this FUNCTION_DECL if it refers to a builtin declaration
5635 of a builtin function. */
5636 if (TREE_CODE (t) == FUNCTION_DECL
5637 && DECL_EXTERNAL (t)
5638 && DECL_BUILT_IN (t))
5639 return;
5641 /* Do not supply context to imported_module_or_decl, if
5642 it is a global namespace. */
5643 if (context == global_namespace)
5644 context = NULL_TREE;
5646 if (BASELINK_P (t))
5647 t = BASELINK_FUNCTIONS (t);
5649 /* FIXME: Handle TEMPLATE_DECLs. */
5650 for (t = OVL_CURRENT (t); t; t = OVL_NEXT (t))
5651 if (TREE_CODE (t) != TEMPLATE_DECL)
5653 if (building_stmt_tree ())
5654 add_stmt (build_stmt (input_location, USING_STMT, t));
5655 else
5656 (*debug_hooks->imported_module_or_decl) (t, NULL_TREE, context, false);
5660 #include "gt-cp-name-lookup.h"