gcc/:
[official-gcc.git] / gcc / cp / name-lookup.c
blobd8b2e7c5e92d189798401b9709500094038d4be0
1 /* Definitions for C++ name lookup routines.
2 Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009
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 "toplev.h"
32 #include "diagnostic.h"
33 #include "debug.h"
34 #include "c-pragma.h"
36 /* The bindings for a particular name in a particular scope. */
38 struct scope_binding {
39 tree value;
40 tree type;
42 #define EMPTY_SCOPE_BINDING { NULL_TREE, NULL_TREE }
44 static cxx_scope *innermost_nonclass_level (void);
45 static cxx_binding *binding_for_name (cxx_scope *, tree);
46 static tree push_overloaded_decl (tree, int, bool);
47 static bool lookup_using_namespace (tree, struct scope_binding *, tree,
48 tree, int);
49 static bool qualified_lookup_using_namespace (tree, tree,
50 struct scope_binding *, int);
51 static tree lookup_type_current_level (tree);
52 static tree push_using_directive (tree);
53 static cxx_binding* lookup_extern_c_fun_binding_in_all_ns (tree);
55 /* The :: namespace. */
57 tree global_namespace;
59 /* The name of the anonymous namespace, throughout this translation
60 unit. */
61 static GTY(()) tree anonymous_namespace_name;
63 /* Initialize anonymous_namespace_name if necessary, and return it. */
65 static tree
66 get_anonymous_namespace_name(void)
68 if (!anonymous_namespace_name)
70 /* The anonymous namespace has to have a unique name
71 if typeinfo objects are being compared by name. */
72 if (! flag_weak || ! SUPPORTS_ONE_ONLY)
73 anonymous_namespace_name = get_file_function_name ("N");
74 else
75 /* The demangler expects anonymous namespaces to be called
76 something starting with '_GLOBAL__N_'. */
77 anonymous_namespace_name = get_identifier ("_GLOBAL__N_1");
79 return anonymous_namespace_name;
82 /* Compute the chain index of a binding_entry given the HASH value of its
83 name and the total COUNT of chains. COUNT is assumed to be a power
84 of 2. */
86 #define ENTRY_INDEX(HASH, COUNT) (((HASH) >> 3) & ((COUNT) - 1))
88 /* A free list of "binding_entry"s awaiting for re-use. */
90 static GTY((deletable)) binding_entry free_binding_entry = NULL;
92 /* Create a binding_entry object for (NAME, TYPE). */
94 static inline binding_entry
95 binding_entry_make (tree name, tree type)
97 binding_entry entry;
99 if (free_binding_entry)
101 entry = free_binding_entry;
102 free_binding_entry = entry->chain;
104 else
105 entry = GGC_NEW (struct binding_entry_s);
107 entry->name = name;
108 entry->type = type;
109 entry->chain = NULL;
111 return entry;
114 /* Put ENTRY back on the free list. */
115 #if 0
116 static inline void
117 binding_entry_free (binding_entry entry)
119 entry->name = NULL;
120 entry->type = NULL;
121 entry->chain = free_binding_entry;
122 free_binding_entry = entry;
124 #endif
126 /* The datatype used to implement the mapping from names to types at
127 a given scope. */
128 struct binding_table_s GTY(())
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_CNEWVEC (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_NEW (struct 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_NEW (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 (i = 0;
332 VEC_iterate (cp_class_binding, scope->class_shadowed, i, cb);
333 i++)
335 cxx_binding **b;
336 b = &IDENTIFIER_BINDING (cb->identifier);
337 while (*b != &old_base[i].base)
338 b = &((*b)->previous);
339 *b = &cb->base;
342 cb = VEC_quick_push (cp_class_binding, scope->class_shadowed, NULL);
344 else
345 cb = VEC_safe_push (cp_class_binding, gc, scope->class_shadowed, NULL);
347 cb->identifier = name;
348 binding = &cb->base;
349 binding->scope = scope;
350 cxx_binding_init (binding, value, type);
351 return binding;
354 /* Make DECL the innermost binding for ID. The LEVEL is the binding
355 level at which this declaration is being bound. */
357 static void
358 push_binding (tree id, tree decl, cxx_scope* level)
360 cxx_binding *binding;
362 if (level != class_binding_level)
364 binding = cxx_binding_make (decl, NULL_TREE);
365 binding->scope = level;
367 else
368 binding = new_class_binding (id, decl, /*type=*/NULL_TREE, level);
370 /* Now, fill in the binding information. */
371 binding->previous = IDENTIFIER_BINDING (id);
372 INHERITED_VALUE_BINDING_P (binding) = 0;
373 LOCAL_BINDING_P (binding) = (level != class_binding_level);
375 /* And put it on the front of the list of bindings for ID. */
376 IDENTIFIER_BINDING (id) = binding;
379 /* Remove the binding for DECL which should be the innermost binding
380 for ID. */
382 void
383 pop_binding (tree id, tree decl)
385 cxx_binding *binding;
387 if (id == NULL_TREE)
388 /* It's easiest to write the loops that call this function without
389 checking whether or not the entities involved have names. We
390 get here for such an entity. */
391 return;
393 /* Get the innermost binding for ID. */
394 binding = IDENTIFIER_BINDING (id);
396 /* The name should be bound. */
397 gcc_assert (binding != NULL);
399 /* The DECL will be either the ordinary binding or the type
400 binding for this identifier. Remove that binding. */
401 if (binding->value == decl)
402 binding->value = NULL_TREE;
403 else
405 gcc_assert (binding->type == decl);
406 binding->type = NULL_TREE;
409 if (!binding->value && !binding->type)
411 /* We're completely done with the innermost binding for this
412 identifier. Unhook it from the list of bindings. */
413 IDENTIFIER_BINDING (id) = binding->previous;
415 /* Add it to the free list. */
416 cxx_binding_free (binding);
420 /* BINDING records an existing declaration for a name in the current scope.
421 But, DECL is another declaration for that same identifier in the
422 same scope. This is the `struct stat' hack whereby a non-typedef
423 class name or enum-name can be bound at the same level as some other
424 kind of entity.
425 3.3.7/1
427 A class name (9.1) or enumeration name (7.2) can be hidden by the
428 name of an object, function, or enumerator declared in the same scope.
429 If a class or enumeration name and an object, function, or enumerator
430 are declared in the same scope (in any order) with the same name, the
431 class or enumeration name is hidden wherever the object, function, or
432 enumerator name is visible.
434 It's the responsibility of the caller to check that
435 inserting this name is valid here. Returns nonzero if the new binding
436 was successful. */
438 static bool
439 supplement_binding (cxx_binding *binding, tree decl)
441 tree bval = binding->value;
442 bool ok = true;
444 timevar_push (TV_NAME_LOOKUP);
445 if (TREE_CODE (decl) == TYPE_DECL && DECL_ARTIFICIAL (decl))
446 /* The new name is the type name. */
447 binding->type = decl;
448 else if (/* BVAL is null when push_class_level_binding moves an
449 inherited type-binding out of the way to make room for a
450 new value binding. */
451 !bval
452 /* BVAL is error_mark_node when DECL's name has been used
453 in a non-class scope prior declaration. In that case,
454 we should have already issued a diagnostic; for graceful
455 error recovery purpose, pretend this was the intended
456 declaration for that name. */
457 || bval == error_mark_node
458 /* If BVAL is anticipated but has not yet been declared,
459 pretend it is not there at all. */
460 || (TREE_CODE (bval) == FUNCTION_DECL
461 && DECL_ANTICIPATED (bval)
462 && !DECL_HIDDEN_FRIEND_P (bval)))
463 binding->value = decl;
464 else if (TREE_CODE (bval) == TYPE_DECL && DECL_ARTIFICIAL (bval))
466 /* The old binding was a type name. It was placed in
467 VALUE field because it was thought, at the point it was
468 declared, to be the only entity with such a name. Move the
469 type name into the type slot; it is now hidden by the new
470 binding. */
471 binding->type = bval;
472 binding->value = decl;
473 binding->value_is_inherited = false;
475 else if (TREE_CODE (bval) == TYPE_DECL
476 && TREE_CODE (decl) == TYPE_DECL
477 && DECL_NAME (decl) == DECL_NAME (bval)
478 && binding->scope->kind != sk_class
479 && (same_type_p (TREE_TYPE (decl), TREE_TYPE (bval))
480 /* If either type involves template parameters, we must
481 wait until instantiation. */
482 || uses_template_parms (TREE_TYPE (decl))
483 || uses_template_parms (TREE_TYPE (bval))))
484 /* We have two typedef-names, both naming the same type to have
485 the same name. In general, this is OK because of:
487 [dcl.typedef]
489 In a given scope, a typedef specifier can be used to redefine
490 the name of any type declared in that scope to refer to the
491 type to which it already refers.
493 However, in class scopes, this rule does not apply due to the
494 stricter language in [class.mem] prohibiting redeclarations of
495 members. */
496 ok = false;
497 /* There can be two block-scope declarations of the same variable,
498 so long as they are `extern' declarations. However, there cannot
499 be two declarations of the same static data member:
501 [class.mem]
503 A member shall not be declared twice in the
504 member-specification. */
505 else if (TREE_CODE (decl) == VAR_DECL && TREE_CODE (bval) == VAR_DECL
506 && DECL_EXTERNAL (decl) && DECL_EXTERNAL (bval)
507 && !DECL_CLASS_SCOPE_P (decl))
509 duplicate_decls (decl, binding->value, /*newdecl_is_friend=*/false);
510 ok = false;
512 else if (TREE_CODE (decl) == NAMESPACE_DECL
513 && TREE_CODE (bval) == NAMESPACE_DECL
514 && DECL_NAMESPACE_ALIAS (decl)
515 && DECL_NAMESPACE_ALIAS (bval)
516 && ORIGINAL_NAMESPACE (bval) == ORIGINAL_NAMESPACE (decl))
517 /* [namespace.alias]
519 In a declarative region, a namespace-alias-definition can be
520 used to redefine a namespace-alias declared in that declarative
521 region to refer only to the namespace to which it already
522 refers. */
523 ok = false;
524 else
526 error ("declaration of %q#D", decl);
527 error ("conflicts with previous declaration %q+#D", bval);
528 ok = false;
531 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, ok);
534 /* Add DECL to the list of things declared in B. */
536 static void
537 add_decl_to_level (tree decl, cxx_scope *b)
539 /* We used to record virtual tables as if they were ordinary
540 variables, but no longer do so. */
541 gcc_assert (!(TREE_CODE (decl) == VAR_DECL && DECL_VIRTUAL_P (decl)));
543 if (TREE_CODE (decl) == NAMESPACE_DECL
544 && !DECL_NAMESPACE_ALIAS (decl))
546 TREE_CHAIN (decl) = b->namespaces;
547 b->namespaces = decl;
549 else
551 /* We build up the list in reverse order, and reverse it later if
552 necessary. */
553 TREE_CHAIN (decl) = b->names;
554 b->names = decl;
555 b->names_size++;
557 /* If appropriate, add decl to separate list of statics. We
558 include extern variables because they might turn out to be
559 static later. It's OK for this list to contain a few false
560 positives. */
561 if (b->kind == sk_namespace)
562 if ((TREE_CODE (decl) == VAR_DECL
563 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
564 || (TREE_CODE (decl) == FUNCTION_DECL
565 && (!TREE_PUBLIC (decl) || DECL_DECLARED_INLINE_P (decl))))
566 VEC_safe_push (tree, gc, b->static_decls, decl);
570 /* Record a decl-node X as belonging to the current lexical scope.
571 Check for errors (such as an incompatible declaration for the same
572 name already seen in the same scope). IS_FRIEND is true if X is
573 declared as a friend.
575 Returns either X or an old decl for the same name.
576 If an old decl is returned, it may have been smashed
577 to agree with what X says. */
579 tree
580 pushdecl_maybe_friend (tree x, bool is_friend)
582 tree t;
583 tree name;
584 int need_new_binding;
586 timevar_push (TV_NAME_LOOKUP);
588 if (x == error_mark_node)
589 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
591 need_new_binding = 1;
593 if (DECL_TEMPLATE_PARM_P (x))
594 /* Template parameters have no context; they are not X::T even
595 when declared within a class or namespace. */
597 else
599 if (current_function_decl && x != current_function_decl
600 /* A local declaration for a function doesn't constitute
601 nesting. */
602 && TREE_CODE (x) != FUNCTION_DECL
603 /* A local declaration for an `extern' variable is in the
604 scope of the current namespace, not the current
605 function. */
606 && !(TREE_CODE (x) == VAR_DECL && DECL_EXTERNAL (x))
607 /* When parsing the parameter list of a function declarator,
608 don't set DECL_CONTEXT to an enclosing function. When we
609 push the PARM_DECLs in order to process the function body,
610 current_binding_level->this_entity will be set. */
611 && !(TREE_CODE (x) == PARM_DECL
612 && current_binding_level->kind == sk_function_parms
613 && current_binding_level->this_entity == NULL)
614 && !DECL_CONTEXT (x))
615 DECL_CONTEXT (x) = current_function_decl;
617 /* If this is the declaration for a namespace-scope function,
618 but the declaration itself is in a local scope, mark the
619 declaration. */
620 if (TREE_CODE (x) == FUNCTION_DECL
621 && DECL_NAMESPACE_SCOPE_P (x)
622 && current_function_decl
623 && x != current_function_decl)
624 DECL_LOCAL_FUNCTION_P (x) = 1;
627 name = DECL_NAME (x);
628 if (name)
630 int different_binding_level = 0;
632 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
633 name = TREE_OPERAND (name, 0);
635 /* In case this decl was explicitly namespace-qualified, look it
636 up in its namespace context. */
637 if (DECL_NAMESPACE_SCOPE_P (x) && namespace_bindings_p ())
638 t = namespace_binding (name, DECL_CONTEXT (x));
639 else
640 t = lookup_name_innermost_nonclass_level (name);
642 /* [basic.link] If there is a visible declaration of an entity
643 with linkage having the same name and type, ignoring entities
644 declared outside the innermost enclosing namespace scope, the
645 block scope declaration declares that same entity and
646 receives the linkage of the previous declaration. */
647 if (! t && current_function_decl && x != current_function_decl
648 && (TREE_CODE (x) == FUNCTION_DECL || TREE_CODE (x) == VAR_DECL)
649 && DECL_EXTERNAL (x))
651 /* Look in block scope. */
652 t = innermost_non_namespace_value (name);
653 /* Or in the innermost namespace. */
654 if (! t)
655 t = namespace_binding (name, DECL_CONTEXT (x));
656 /* Does it have linkage? Note that if this isn't a DECL, it's an
657 OVERLOAD, which is OK. */
658 if (t && DECL_P (t) && ! (TREE_STATIC (t) || DECL_EXTERNAL (t)))
659 t = NULL_TREE;
660 if (t)
661 different_binding_level = 1;
664 /* If we are declaring a function, and the result of name-lookup
665 was an OVERLOAD, look for an overloaded instance that is
666 actually the same as the function we are declaring. (If
667 there is one, we have to merge our declaration with the
668 previous declaration.) */
669 if (t && TREE_CODE (t) == OVERLOAD)
671 tree match;
673 if (TREE_CODE (x) == FUNCTION_DECL)
674 for (match = t; match; match = OVL_NEXT (match))
676 if (decls_match (OVL_CURRENT (match), x))
677 break;
679 else
680 /* Just choose one. */
681 match = t;
683 if (match)
684 t = OVL_CURRENT (match);
685 else
686 t = NULL_TREE;
689 if (t && t != error_mark_node)
691 if (different_binding_level)
693 if (decls_match (x, t))
694 /* The standard only says that the local extern
695 inherits linkage from the previous decl; in
696 particular, default args are not shared. Add
697 the decl into a hash table to make sure only
698 the previous decl in this case is seen by the
699 middle end. */
701 struct cxx_int_tree_map *h;
702 void **loc;
704 TREE_PUBLIC (x) = TREE_PUBLIC (t);
706 if (cp_function_chain->extern_decl_map == NULL)
707 cp_function_chain->extern_decl_map
708 = htab_create_ggc (20, cxx_int_tree_map_hash,
709 cxx_int_tree_map_eq, NULL);
711 h = GGC_NEW (struct cxx_int_tree_map);
712 h->uid = DECL_UID (x);
713 h->to = t;
714 loc = htab_find_slot_with_hash
715 (cp_function_chain->extern_decl_map, h,
716 h->uid, INSERT);
717 *(struct cxx_int_tree_map **) loc = h;
720 else if (TREE_CODE (t) == PARM_DECL)
722 /* Check for duplicate params. */
723 tree d = duplicate_decls (x, t, is_friend);
724 if (d)
725 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, d);
727 else if ((DECL_EXTERN_C_FUNCTION_P (x)
728 || DECL_FUNCTION_TEMPLATE_P (x))
729 && is_overloaded_fn (t))
730 /* Don't do anything just yet. */;
731 else if (t == wchar_decl_node)
733 if (! DECL_IN_SYSTEM_HEADER (x))
734 pedwarn (input_location, OPT_pedantic, "redeclaration of %<wchar_t%> as %qT",
735 TREE_TYPE (x));
737 /* Throw away the redeclaration. */
738 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
740 else
742 tree olddecl = duplicate_decls (x, t, is_friend);
744 /* If the redeclaration failed, we can stop at this
745 point. */
746 if (olddecl == error_mark_node)
747 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
749 if (olddecl)
751 if (TREE_CODE (t) == TYPE_DECL)
752 SET_IDENTIFIER_TYPE_VALUE (name, TREE_TYPE (t));
754 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
756 else if (DECL_MAIN_P (x) && TREE_CODE (t) == FUNCTION_DECL)
758 /* A redeclaration of main, but not a duplicate of the
759 previous one.
761 [basic.start.main]
763 This function shall not be overloaded. */
764 error ("invalid redeclaration of %q+D", t);
765 error ("as %qD", x);
766 /* We don't try to push this declaration since that
767 causes a crash. */
768 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
773 /* If x has C linkage-specification, (extern "C"),
774 lookup its binding, in case it's already bound to an object.
775 The lookup is done in all namespaces.
776 If we find an existing binding, make sure it has the same
777 exception specification as x, otherwise, bail in error [7.5, 7.6]. */
778 if ((TREE_CODE (x) == FUNCTION_DECL)
779 && DECL_EXTERN_C_P (x)
780 /* We should ignore declarations happening in system headers. */
781 && !DECL_ARTIFICIAL (x)
782 && !DECL_IN_SYSTEM_HEADER (x))
784 cxx_binding *function_binding =
785 lookup_extern_c_fun_binding_in_all_ns (x);
786 tree previous = (function_binding
787 ? function_binding->value
788 : NULL_TREE);
789 if (previous
790 && !DECL_ARTIFICIAL (previous)
791 && !DECL_IN_SYSTEM_HEADER (previous)
792 && DECL_CONTEXT (previous) != DECL_CONTEXT (x))
794 tree previous = function_binding->value;
796 /* In case either x or previous is declared to throw an exception,
797 make sure both exception specifications are equal. */
798 if (decls_match (x, previous))
800 tree x_exception_spec = NULL_TREE;
801 tree previous_exception_spec = NULL_TREE;
803 x_exception_spec =
804 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (x));
805 previous_exception_spec =
806 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (previous));
807 if (!comp_except_specs (previous_exception_spec,
808 x_exception_spec,
809 true))
811 pedwarn (input_location, 0, "declaration of %q#D with C language linkage",
813 pedwarn (input_location, 0, "conflicts with previous declaration %q+#D",
814 previous);
815 pedwarn (input_location, 0, "due to different exception specifications");
816 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
819 else
821 pedwarn (input_location, 0,
822 "declaration of %q#D with C language linkage", x);
823 pedwarn (input_location, 0,
824 "conflicts with previous declaration %q+#D",
825 previous);
830 check_template_shadow (x);
832 /* If this is a function conjured up by the back end, massage it
833 so it looks friendly. */
834 if (DECL_NON_THUNK_FUNCTION_P (x) && ! DECL_LANG_SPECIFIC (x))
836 retrofit_lang_decl (x);
837 SET_DECL_LANGUAGE (x, lang_c);
840 t = x;
841 if (DECL_NON_THUNK_FUNCTION_P (x) && ! DECL_FUNCTION_MEMBER_P (x))
843 t = push_overloaded_decl (x, PUSH_LOCAL, is_friend);
844 if (!namespace_bindings_p ())
845 /* We do not need to create a binding for this name;
846 push_overloaded_decl will have already done so if
847 necessary. */
848 need_new_binding = 0;
850 else if (DECL_FUNCTION_TEMPLATE_P (x) && DECL_NAMESPACE_SCOPE_P (x))
852 t = push_overloaded_decl (x, PUSH_GLOBAL, is_friend);
853 if (t == x)
854 add_decl_to_level (x, NAMESPACE_LEVEL (CP_DECL_CONTEXT (t)));
857 if (TREE_CODE (x) == FUNCTION_DECL || DECL_FUNCTION_TEMPLATE_P (x))
858 check_default_args (x);
860 if (t != x || DECL_FUNCTION_TEMPLATE_P (t))
861 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
863 /* If declaring a type as a typedef, copy the type (unless we're
864 at line 0), and install this TYPE_DECL as the new type's typedef
865 name. See the extensive comment of set_underlying_type (). */
866 if (TREE_CODE (x) == TYPE_DECL)
868 tree type = TREE_TYPE (x);
870 if (DECL_IS_BUILTIN (x)
871 || (TREE_TYPE (x) != error_mark_node
872 && TYPE_NAME (type) != x
873 /* We don't want to copy the type when all we're
874 doing is making a TYPE_DECL for the purposes of
875 inlining. */
876 && (!TYPE_NAME (type)
877 || TYPE_NAME (type) != DECL_ABSTRACT_ORIGIN (x))))
878 set_underlying_type (x);
880 if (type != error_mark_node
881 && TYPE_NAME (type)
882 && TYPE_IDENTIFIER (type))
883 set_identifier_type_value (DECL_NAME (x), x);
886 /* Multiple external decls of the same identifier ought to match.
888 We get warnings about inline functions where they are defined.
889 We get warnings about other functions from push_overloaded_decl.
891 Avoid duplicate warnings where they are used. */
892 if (TREE_PUBLIC (x) && TREE_CODE (x) != FUNCTION_DECL)
894 tree decl;
896 decl = IDENTIFIER_NAMESPACE_VALUE (name);
897 if (decl && TREE_CODE (decl) == OVERLOAD)
898 decl = OVL_FUNCTION (decl);
900 if (decl && decl != error_mark_node
901 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl))
902 /* If different sort of thing, we already gave an error. */
903 && TREE_CODE (decl) == TREE_CODE (x)
904 && !same_type_p (TREE_TYPE (x), TREE_TYPE (decl)))
906 permerror (input_location, "type mismatch with previous external decl of %q#D", x);
907 permerror (input_location, "previous external decl of %q+#D", decl);
911 if (TREE_CODE (x) == FUNCTION_DECL
912 && is_friend
913 && !flag_friend_injection)
915 /* This is a new declaration of a friend function, so hide
916 it from ordinary function lookup. */
917 DECL_ANTICIPATED (x) = 1;
918 DECL_HIDDEN_FRIEND_P (x) = 1;
921 /* This name is new in its binding level.
922 Install the new declaration and return it. */
923 if (namespace_bindings_p ())
925 /* Install a global value. */
927 /* If the first global decl has external linkage,
928 warn if we later see static one. */
929 if (IDENTIFIER_GLOBAL_VALUE (name) == NULL_TREE && TREE_PUBLIC (x))
930 TREE_PUBLIC (name) = 1;
932 /* Bind the name for the entity. */
933 if (!(TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x)
934 && t != NULL_TREE)
935 && (TREE_CODE (x) == TYPE_DECL
936 || TREE_CODE (x) == VAR_DECL
937 || TREE_CODE (x) == NAMESPACE_DECL
938 || TREE_CODE (x) == CONST_DECL
939 || TREE_CODE (x) == TEMPLATE_DECL))
940 SET_IDENTIFIER_NAMESPACE_VALUE (name, x);
942 /* If new decl is `static' and an `extern' was seen previously,
943 warn about it. */
944 if (x != NULL_TREE && t != NULL_TREE && decls_match (x, t))
945 warn_extern_redeclared_static (x, t);
947 else
949 /* Here to install a non-global value. */
950 tree oldlocal = innermost_non_namespace_value (name);
951 tree oldglobal = IDENTIFIER_NAMESPACE_VALUE (name);
953 if (need_new_binding)
955 push_local_binding (name, x, 0);
956 /* Because push_local_binding will hook X on to the
957 current_binding_level's name list, we don't want to
958 do that again below. */
959 need_new_binding = 0;
962 /* If this is a TYPE_DECL, push it into the type value slot. */
963 if (TREE_CODE (x) == TYPE_DECL)
964 set_identifier_type_value (name, x);
966 /* Clear out any TYPE_DECL shadowed by a namespace so that
967 we won't think this is a type. The C struct hack doesn't
968 go through namespaces. */
969 if (TREE_CODE (x) == NAMESPACE_DECL)
970 set_identifier_type_value (name, NULL_TREE);
972 if (oldlocal)
974 tree d = oldlocal;
976 while (oldlocal
977 && TREE_CODE (oldlocal) == VAR_DECL
978 && DECL_DEAD_FOR_LOCAL (oldlocal))
979 oldlocal = DECL_SHADOWED_FOR_VAR (oldlocal);
981 if (oldlocal == NULL_TREE)
982 oldlocal = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (d));
985 /* If this is an extern function declaration, see if we
986 have a global definition or declaration for the function. */
987 if (oldlocal == NULL_TREE
988 && DECL_EXTERNAL (x)
989 && oldglobal != NULL_TREE
990 && TREE_CODE (x) == FUNCTION_DECL
991 && TREE_CODE (oldglobal) == FUNCTION_DECL)
993 /* We have one. Their types must agree. */
994 if (decls_match (x, oldglobal))
995 /* OK */;
996 else
998 warning (0, "extern declaration of %q#D doesn't match", x);
999 warning (0, "global declaration %q+#D", oldglobal);
1002 /* If we have a local external declaration,
1003 and no file-scope declaration has yet been seen,
1004 then if we later have a file-scope decl it must not be static. */
1005 if (oldlocal == NULL_TREE
1006 && oldglobal == NULL_TREE
1007 && DECL_EXTERNAL (x)
1008 && TREE_PUBLIC (x))
1009 TREE_PUBLIC (name) = 1;
1011 /* Don't complain about the parms we push and then pop
1012 while tentatively parsing a function declarator. */
1013 if (TREE_CODE (x) == PARM_DECL && DECL_CONTEXT (x) == NULL_TREE)
1014 /* Ignore. */;
1016 /* Warn if shadowing an argument at the top level of the body. */
1017 else if (oldlocal != NULL_TREE && !DECL_EXTERNAL (x)
1018 /* Inline decls shadow nothing. */
1019 && !DECL_FROM_INLINE (x)
1020 && TREE_CODE (oldlocal) == PARM_DECL
1021 /* Don't check the `this' parameter. */
1022 && !DECL_ARTIFICIAL (oldlocal))
1024 bool err = false;
1026 /* Don't complain if it's from an enclosing function. */
1027 if (DECL_CONTEXT (oldlocal) == current_function_decl
1028 && TREE_CODE (x) != PARM_DECL)
1030 /* Go to where the parms should be and see if we find
1031 them there. */
1032 struct cp_binding_level *b = current_binding_level->level_chain;
1034 if (FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
1035 /* Skip the ctor/dtor cleanup level. */
1036 b = b->level_chain;
1038 /* ARM $8.3 */
1039 if (b->kind == sk_function_parms)
1041 error ("declaration of %q#D shadows a parameter", x);
1042 err = true;
1046 if (warn_shadow && !err)
1048 warning (OPT_Wshadow, "declaration of %q#D shadows a parameter", x);
1049 warning (OPT_Wshadow, "%Jshadowed declaration is here", oldlocal);
1053 /* Maybe warn if shadowing something else. */
1054 else if (warn_shadow && !DECL_EXTERNAL (x)
1055 /* No shadow warnings for internally generated vars. */
1056 && ! DECL_ARTIFICIAL (x)
1057 /* No shadow warnings for vars made for inlining. */
1058 && ! DECL_FROM_INLINE (x))
1060 tree member;
1062 if (current_class_ptr)
1063 member = lookup_member (current_class_type,
1064 name,
1065 /*protect=*/0,
1066 /*want_type=*/false);
1067 else
1068 member = NULL_TREE;
1070 if (member && !TREE_STATIC (member))
1072 /* Location of previous decl is not useful in this case. */
1073 warning (OPT_Wshadow, "declaration of %qD shadows a member of 'this'",
1076 else if (oldlocal != NULL_TREE
1077 && TREE_CODE (oldlocal) == VAR_DECL)
1079 warning (OPT_Wshadow, "declaration of %qD shadows a previous local", x);
1080 warning (OPT_Wshadow, "%Jshadowed declaration is here", oldlocal);
1082 else if (oldglobal != NULL_TREE
1083 && TREE_CODE (oldglobal) == VAR_DECL)
1084 /* XXX shadow warnings in outer-more namespaces */
1086 warning (OPT_Wshadow, "declaration of %qD shadows a global declaration",
1088 warning (OPT_Wshadow, "%Jshadowed declaration is here", oldglobal);
1093 if (TREE_CODE (x) == VAR_DECL)
1094 maybe_register_incomplete_var (x);
1097 if (need_new_binding)
1098 add_decl_to_level (x,
1099 DECL_NAMESPACE_SCOPE_P (x)
1100 ? NAMESPACE_LEVEL (CP_DECL_CONTEXT (x))
1101 : current_binding_level);
1103 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
1106 /* Record a decl-node X as belonging to the current lexical scope. */
1108 tree
1109 pushdecl (tree x)
1111 return pushdecl_maybe_friend (x, false);
1114 /* Enter DECL into the symbol table, if that's appropriate. Returns
1115 DECL, or a modified version thereof. */
1117 tree
1118 maybe_push_decl (tree decl)
1120 tree type = TREE_TYPE (decl);
1122 /* Add this decl to the current binding level, but not if it comes
1123 from another scope, e.g. a static member variable. TEM may equal
1124 DECL or it may be a previous decl of the same name. */
1125 if (decl == error_mark_node
1126 || (TREE_CODE (decl) != PARM_DECL
1127 && DECL_CONTEXT (decl) != NULL_TREE
1128 /* Definitions of namespace members outside their namespace are
1129 possible. */
1130 && TREE_CODE (DECL_CONTEXT (decl)) != NAMESPACE_DECL)
1131 || (TREE_CODE (decl) == TEMPLATE_DECL && !namespace_bindings_p ())
1132 || TREE_CODE (type) == UNKNOWN_TYPE
1133 /* The declaration of a template specialization does not affect
1134 the functions available for overload resolution, so we do not
1135 call pushdecl. */
1136 || (TREE_CODE (decl) == FUNCTION_DECL
1137 && DECL_TEMPLATE_SPECIALIZATION (decl)))
1138 return decl;
1139 else
1140 return pushdecl (decl);
1143 /* Bind DECL to ID in the current_binding_level, assumed to be a local
1144 binding level. If PUSH_USING is set in FLAGS, we know that DECL
1145 doesn't really belong to this binding level, that it got here
1146 through a using-declaration. */
1148 void
1149 push_local_binding (tree id, tree decl, int flags)
1151 struct cp_binding_level *b;
1153 /* Skip over any local classes. This makes sense if we call
1154 push_local_binding with a friend decl of a local class. */
1155 b = innermost_nonclass_level ();
1157 if (lookup_name_innermost_nonclass_level (id))
1159 /* Supplement the existing binding. */
1160 if (!supplement_binding (IDENTIFIER_BINDING (id), decl))
1161 /* It didn't work. Something else must be bound at this
1162 level. Do not add DECL to the list of things to pop
1163 later. */
1164 return;
1166 else
1167 /* Create a new binding. */
1168 push_binding (id, decl, b);
1170 if (TREE_CODE (decl) == OVERLOAD || (flags & PUSH_USING))
1171 /* We must put the OVERLOAD into a TREE_LIST since the
1172 TREE_CHAIN of an OVERLOAD is already used. Similarly for
1173 decls that got here through a using-declaration. */
1174 decl = build_tree_list (NULL_TREE, decl);
1176 /* And put DECL on the list of things declared by the current
1177 binding level. */
1178 add_decl_to_level (decl, b);
1181 /* Check to see whether or not DECL is a variable that would have been
1182 in scope under the ARM, but is not in scope under the ANSI/ISO
1183 standard. If so, issue an error message. If name lookup would
1184 work in both cases, but return a different result, this function
1185 returns the result of ANSI/ISO lookup. Otherwise, it returns
1186 DECL. */
1188 tree
1189 check_for_out_of_scope_variable (tree decl)
1191 tree shadowed;
1193 /* We only care about out of scope variables. */
1194 if (!(TREE_CODE (decl) == VAR_DECL && DECL_DEAD_FOR_LOCAL (decl)))
1195 return decl;
1197 shadowed = DECL_HAS_SHADOWED_FOR_VAR_P (decl)
1198 ? DECL_SHADOWED_FOR_VAR (decl) : NULL_TREE ;
1199 while (shadowed != NULL_TREE && TREE_CODE (shadowed) == VAR_DECL
1200 && DECL_DEAD_FOR_LOCAL (shadowed))
1201 shadowed = DECL_HAS_SHADOWED_FOR_VAR_P (shadowed)
1202 ? DECL_SHADOWED_FOR_VAR (shadowed) : NULL_TREE;
1203 if (!shadowed)
1204 shadowed = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (decl));
1205 if (shadowed)
1207 if (!DECL_ERROR_REPORTED (decl))
1209 warning (0, "name lookup of %qD changed", DECL_NAME (decl));
1210 warning (0, " matches this %q+D under ISO standard rules",
1211 shadowed);
1212 warning (0, " matches this %q+D under old rules", decl);
1213 DECL_ERROR_REPORTED (decl) = 1;
1215 return shadowed;
1218 /* If we have already complained about this declaration, there's no
1219 need to do it again. */
1220 if (DECL_ERROR_REPORTED (decl))
1221 return decl;
1223 DECL_ERROR_REPORTED (decl) = 1;
1225 if (TREE_TYPE (decl) == error_mark_node)
1226 return decl;
1228 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
1230 error ("name lookup of %qD changed for ISO %<for%> scoping",
1231 DECL_NAME (decl));
1232 error (" cannot use obsolete binding at %q+D because "
1233 "it has a destructor", decl);
1234 return error_mark_node;
1236 else
1238 permerror (input_location, "name lookup of %qD changed for ISO %<for%> scoping",
1239 DECL_NAME (decl));
1240 if (flag_permissive)
1241 permerror (input_location, " using obsolete binding at %q+D", decl);
1242 else
1244 static bool hint;
1245 if (!hint)
1247 inform (input_location, "(if you use %<-fpermissive%> G++ will accept your code)");
1248 hint = true;
1253 return decl;
1256 /* true means unconditionally make a BLOCK for the next level pushed. */
1258 static bool keep_next_level_flag;
1260 static int binding_depth = 0;
1261 static int is_class_level = 0;
1263 static void
1264 indent (int depth)
1266 int i;
1268 for (i = 0; i < depth * 2; i++)
1269 putc (' ', stderr);
1272 /* Return a string describing the kind of SCOPE we have. */
1273 static const char *
1274 cxx_scope_descriptor (cxx_scope *scope)
1276 /* The order of this table must match the "scope_kind"
1277 enumerators. */
1278 static const char* scope_kind_names[] = {
1279 "block-scope",
1280 "cleanup-scope",
1281 "try-scope",
1282 "catch-scope",
1283 "for-scope",
1284 "function-parameter-scope",
1285 "class-scope",
1286 "namespace-scope",
1287 "template-parameter-scope",
1288 "template-explicit-spec-scope"
1290 const scope_kind kind = scope->explicit_spec_p
1291 ? sk_template_spec : scope->kind;
1293 return scope_kind_names[kind];
1296 /* Output a debugging information about SCOPE when performing
1297 ACTION at LINE. */
1298 static void
1299 cxx_scope_debug (cxx_scope *scope, int line, const char *action)
1301 const char *desc = cxx_scope_descriptor (scope);
1302 if (scope->this_entity)
1303 verbatim ("%s %s(%E) %p %d\n", action, desc,
1304 scope->this_entity, (void *) scope, line);
1305 else
1306 verbatim ("%s %s %p %d\n", action, desc, (void *) scope, line);
1309 /* Return the estimated initial size of the hashtable of a NAMESPACE
1310 scope. */
1312 static inline size_t
1313 namespace_scope_ht_size (tree ns)
1315 tree name = DECL_NAME (ns);
1317 return name == std_identifier
1318 ? NAMESPACE_STD_HT_SIZE
1319 : (name == global_scope_name
1320 ? GLOBAL_SCOPE_HT_SIZE
1321 : NAMESPACE_ORDINARY_HT_SIZE);
1324 /* A chain of binding_level structures awaiting reuse. */
1326 static GTY((deletable)) struct cp_binding_level *free_binding_level;
1328 /* Insert SCOPE as the innermost binding level. */
1330 void
1331 push_binding_level (struct cp_binding_level *scope)
1333 /* Add it to the front of currently active scopes stack. */
1334 scope->level_chain = current_binding_level;
1335 current_binding_level = scope;
1336 keep_next_level_flag = false;
1338 if (ENABLE_SCOPE_CHECKING)
1340 scope->binding_depth = binding_depth;
1341 indent (binding_depth);
1342 cxx_scope_debug (scope, input_line, "push");
1343 is_class_level = 0;
1344 binding_depth++;
1348 /* Create a new KIND scope and make it the top of the active scopes stack.
1349 ENTITY is the scope of the associated C++ entity (namespace, class,
1350 function, C++0x enumeration); it is NULL otherwise. */
1352 cxx_scope *
1353 begin_scope (scope_kind kind, tree entity)
1355 cxx_scope *scope;
1357 /* Reuse or create a struct for this binding level. */
1358 if (!ENABLE_SCOPE_CHECKING && free_binding_level)
1360 scope = free_binding_level;
1361 memset (scope, 0, sizeof (cxx_scope));
1362 free_binding_level = scope->level_chain;
1364 else
1365 scope = GGC_CNEW (cxx_scope);
1367 scope->this_entity = entity;
1368 scope->more_cleanups_ok = true;
1369 switch (kind)
1371 case sk_cleanup:
1372 scope->keep = true;
1373 break;
1375 case sk_template_spec:
1376 scope->explicit_spec_p = true;
1377 kind = sk_template_parms;
1378 /* Fall through. */
1379 case sk_template_parms:
1380 case sk_block:
1381 case sk_try:
1382 case sk_catch:
1383 case sk_for:
1384 case sk_class:
1385 case sk_scoped_enum:
1386 case sk_function_parms:
1387 case sk_omp:
1388 scope->keep = keep_next_level_flag;
1389 break;
1391 case sk_namespace:
1392 NAMESPACE_LEVEL (entity) = scope;
1393 scope->static_decls =
1394 VEC_alloc (tree, gc,
1395 DECL_NAME (entity) == std_identifier
1396 || DECL_NAME (entity) == global_scope_name
1397 ? 200 : 10);
1398 break;
1400 default:
1401 /* Should not happen. */
1402 gcc_unreachable ();
1403 break;
1405 scope->kind = kind;
1407 push_binding_level (scope);
1409 return scope;
1412 /* We're about to leave current scope. Pop the top of the stack of
1413 currently active scopes. Return the enclosing scope, now active. */
1415 cxx_scope *
1416 leave_scope (void)
1418 cxx_scope *scope = current_binding_level;
1420 if (scope->kind == sk_namespace && class_binding_level)
1421 current_binding_level = class_binding_level;
1423 /* We cannot leave a scope, if there are none left. */
1424 if (NAMESPACE_LEVEL (global_namespace))
1425 gcc_assert (!global_scope_p (scope));
1427 if (ENABLE_SCOPE_CHECKING)
1429 indent (--binding_depth);
1430 cxx_scope_debug (scope, input_line, "leave");
1431 if (is_class_level != (scope == class_binding_level))
1433 indent (binding_depth);
1434 verbatim ("XXX is_class_level != (current_scope == class_scope)\n");
1436 is_class_level = 0;
1439 /* Move one nesting level up. */
1440 current_binding_level = scope->level_chain;
1442 /* Namespace-scopes are left most probably temporarily, not
1443 completely; they can be reopened later, e.g. in namespace-extension
1444 or any name binding activity that requires us to resume a
1445 namespace. For classes, we cache some binding levels. For other
1446 scopes, we just make the structure available for reuse. */
1447 if (scope->kind != sk_namespace
1448 && scope->kind != sk_class)
1450 scope->level_chain = free_binding_level;
1451 gcc_assert (!ENABLE_SCOPE_CHECKING
1452 || scope->binding_depth == binding_depth);
1453 free_binding_level = scope;
1456 /* Find the innermost enclosing class scope, and reset
1457 CLASS_BINDING_LEVEL appropriately. */
1458 if (scope->kind == sk_class)
1460 class_binding_level = NULL;
1461 for (scope = current_binding_level; scope; scope = scope->level_chain)
1462 if (scope->kind == sk_class)
1464 class_binding_level = scope;
1465 break;
1469 return current_binding_level;
1472 static void
1473 resume_scope (struct cp_binding_level* b)
1475 /* Resuming binding levels is meant only for namespaces,
1476 and those cannot nest into classes. */
1477 gcc_assert (!class_binding_level);
1478 /* Also, resuming a non-directly nested namespace is a no-no. */
1479 gcc_assert (b->level_chain == current_binding_level);
1480 current_binding_level = b;
1481 if (ENABLE_SCOPE_CHECKING)
1483 b->binding_depth = binding_depth;
1484 indent (binding_depth);
1485 cxx_scope_debug (b, input_line, "resume");
1486 is_class_level = 0;
1487 binding_depth++;
1491 /* Return the innermost binding level that is not for a class scope. */
1493 static cxx_scope *
1494 innermost_nonclass_level (void)
1496 cxx_scope *b;
1498 b = current_binding_level;
1499 while (b->kind == sk_class)
1500 b = b->level_chain;
1502 return b;
1505 /* We're defining an object of type TYPE. If it needs a cleanup, but
1506 we're not allowed to add any more objects with cleanups to the current
1507 scope, create a new binding level. */
1509 void
1510 maybe_push_cleanup_level (tree type)
1512 if (type != error_mark_node
1513 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
1514 && current_binding_level->more_cleanups_ok == 0)
1516 begin_scope (sk_cleanup, NULL);
1517 current_binding_level->statement_list = push_stmt_list ();
1521 /* Nonzero if we are currently in the global binding level. */
1524 global_bindings_p (void)
1526 return global_scope_p (current_binding_level);
1529 /* True if we are currently in a toplevel binding level. This
1530 means either the global binding level or a namespace in a toplevel
1531 binding level. Since there are no non-toplevel namespace levels,
1532 this really means any namespace or template parameter level. We
1533 also include a class whose context is toplevel. */
1535 bool
1536 toplevel_bindings_p (void)
1538 struct cp_binding_level *b = innermost_nonclass_level ();
1540 return b->kind == sk_namespace || b->kind == sk_template_parms;
1543 /* True if this is a namespace scope, or if we are defining a class
1544 which is itself at namespace scope, or whose enclosing class is
1545 such a class, etc. */
1547 bool
1548 namespace_bindings_p (void)
1550 struct cp_binding_level *b = innermost_nonclass_level ();
1552 return b->kind == sk_namespace;
1555 /* True if the current level needs to have a BLOCK made. */
1557 bool
1558 kept_level_p (void)
1560 return (current_binding_level->blocks != NULL_TREE
1561 || current_binding_level->keep
1562 || current_binding_level->kind == sk_cleanup
1563 || current_binding_level->names != NULL_TREE
1564 || current_binding_level->using_directives);
1567 /* Returns the kind of the innermost scope. */
1569 scope_kind
1570 innermost_scope_kind (void)
1572 return current_binding_level->kind;
1575 /* Returns true if this scope was created to store template parameters. */
1577 bool
1578 template_parm_scope_p (void)
1580 return innermost_scope_kind () == sk_template_parms;
1583 /* If KEEP is true, make a BLOCK node for the next binding level,
1584 unconditionally. Otherwise, use the normal logic to decide whether
1585 or not to create a BLOCK. */
1587 void
1588 keep_next_level (bool keep)
1590 keep_next_level_flag = keep;
1593 /* Return the list of declarations of the current level.
1594 Note that this list is in reverse order unless/until
1595 you nreverse it; and when you do nreverse it, you must
1596 store the result back using `storedecls' or you will lose. */
1598 tree
1599 getdecls (void)
1601 return current_binding_level->names;
1604 /* For debugging. */
1605 static int no_print_functions = 0;
1606 static int no_print_builtins = 0;
1608 static void
1609 print_binding_level (struct cp_binding_level* lvl)
1611 tree t;
1612 int i = 0, len;
1613 fprintf (stderr, " blocks=%p", (void *) lvl->blocks);
1614 if (lvl->more_cleanups_ok)
1615 fprintf (stderr, " more-cleanups-ok");
1616 if (lvl->have_cleanups)
1617 fprintf (stderr, " have-cleanups");
1618 fprintf (stderr, "\n");
1619 if (lvl->names)
1621 fprintf (stderr, " names:\t");
1622 /* We can probably fit 3 names to a line? */
1623 for (t = lvl->names; t; t = TREE_CHAIN (t))
1625 if (no_print_functions && (TREE_CODE (t) == FUNCTION_DECL))
1626 continue;
1627 if (no_print_builtins
1628 && (TREE_CODE (t) == TYPE_DECL)
1629 && DECL_IS_BUILTIN (t))
1630 continue;
1632 /* Function decls tend to have longer names. */
1633 if (TREE_CODE (t) == FUNCTION_DECL)
1634 len = 3;
1635 else
1636 len = 2;
1637 i += len;
1638 if (i > 6)
1640 fprintf (stderr, "\n\t");
1641 i = len;
1643 print_node_brief (stderr, "", t, 0);
1644 if (t == error_mark_node)
1645 break;
1647 if (i)
1648 fprintf (stderr, "\n");
1650 if (VEC_length (cp_class_binding, lvl->class_shadowed))
1652 size_t i;
1653 cp_class_binding *b;
1654 fprintf (stderr, " class-shadowed:");
1655 for (i = 0;
1656 VEC_iterate(cp_class_binding, lvl->class_shadowed, i, b);
1657 ++i)
1658 fprintf (stderr, " %s ", IDENTIFIER_POINTER (b->identifier));
1659 fprintf (stderr, "\n");
1661 if (lvl->type_shadowed)
1663 fprintf (stderr, " type-shadowed:");
1664 for (t = lvl->type_shadowed; t; t = TREE_CHAIN (t))
1666 fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
1668 fprintf (stderr, "\n");
1672 void
1673 print_other_binding_stack (struct cp_binding_level *stack)
1675 struct cp_binding_level *level;
1676 for (level = stack; !global_scope_p (level); level = level->level_chain)
1678 fprintf (stderr, "binding level %p\n", (void *) level);
1679 print_binding_level (level);
1683 void
1684 print_binding_stack (void)
1686 struct cp_binding_level *b;
1687 fprintf (stderr, "current_binding_level=%p\n"
1688 "class_binding_level=%p\n"
1689 "NAMESPACE_LEVEL (global_namespace)=%p\n",
1690 (void *) current_binding_level, (void *) class_binding_level,
1691 (void *) NAMESPACE_LEVEL (global_namespace));
1692 if (class_binding_level)
1694 for (b = class_binding_level; b; b = b->level_chain)
1695 if (b == current_binding_level)
1696 break;
1697 if (b)
1698 b = class_binding_level;
1699 else
1700 b = current_binding_level;
1702 else
1703 b = current_binding_level;
1704 print_other_binding_stack (b);
1705 fprintf (stderr, "global:\n");
1706 print_binding_level (NAMESPACE_LEVEL (global_namespace));
1709 /* Return the type associated with id. */
1711 tree
1712 identifier_type_value (tree id)
1714 timevar_push (TV_NAME_LOOKUP);
1715 /* There is no type with that name, anywhere. */
1716 if (REAL_IDENTIFIER_TYPE_VALUE (id) == NULL_TREE)
1717 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
1718 /* This is not the type marker, but the real thing. */
1719 if (REAL_IDENTIFIER_TYPE_VALUE (id) != global_type_node)
1720 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, REAL_IDENTIFIER_TYPE_VALUE (id));
1721 /* Have to search for it. It must be on the global level, now.
1722 Ask lookup_name not to return non-types. */
1723 id = lookup_name_real (id, 2, 1, /*block_p=*/true, 0, LOOKUP_COMPLAIN);
1724 if (id)
1725 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, TREE_TYPE (id));
1726 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
1729 /* Return the IDENTIFIER_GLOBAL_VALUE of T, for use in common code, since
1730 the definition of IDENTIFIER_GLOBAL_VALUE is different for C and C++. */
1732 tree
1733 identifier_global_value (tree t)
1735 return IDENTIFIER_GLOBAL_VALUE (t);
1738 /* Push a definition of struct, union or enum tag named ID. into
1739 binding_level B. DECL is a TYPE_DECL for the type. We assume that
1740 the tag ID is not already defined. */
1742 static void
1743 set_identifier_type_value_with_scope (tree id, tree decl, cxx_scope *b)
1745 tree type;
1747 if (b->kind != sk_namespace)
1749 /* Shadow the marker, not the real thing, so that the marker
1750 gets restored later. */
1751 tree old_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
1752 b->type_shadowed
1753 = tree_cons (id, old_type_value, b->type_shadowed);
1754 type = decl ? TREE_TYPE (decl) : NULL_TREE;
1755 TREE_TYPE (b->type_shadowed) = type;
1757 else
1759 cxx_binding *binding =
1760 binding_for_name (NAMESPACE_LEVEL (current_namespace), id);
1761 gcc_assert (decl);
1762 if (binding->value)
1763 supplement_binding (binding, decl);
1764 else
1765 binding->value = decl;
1767 /* Store marker instead of real type. */
1768 type = global_type_node;
1770 SET_IDENTIFIER_TYPE_VALUE (id, type);
1773 /* As set_identifier_type_value_with_scope, but using
1774 current_binding_level. */
1776 void
1777 set_identifier_type_value (tree id, tree decl)
1779 set_identifier_type_value_with_scope (id, decl, current_binding_level);
1782 /* Return the name for the constructor (or destructor) for the
1783 specified class TYPE. When given a template, this routine doesn't
1784 lose the specialization. */
1786 static inline tree
1787 constructor_name_full (tree type)
1789 return TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
1792 /* Return the name for the constructor (or destructor) for the
1793 specified class. When given a template, return the plain
1794 unspecialized name. */
1796 tree
1797 constructor_name (tree type)
1799 tree name;
1800 name = constructor_name_full (type);
1801 if (IDENTIFIER_TEMPLATE (name))
1802 name = IDENTIFIER_TEMPLATE (name);
1803 return name;
1806 /* Returns TRUE if NAME is the name for the constructor for TYPE,
1807 which must be a class type. */
1809 bool
1810 constructor_name_p (tree name, tree type)
1812 tree ctor_name;
1814 gcc_assert (MAYBE_CLASS_TYPE_P (type));
1816 if (!name)
1817 return false;
1819 if (TREE_CODE (name) != IDENTIFIER_NODE)
1820 return false;
1822 ctor_name = constructor_name_full (type);
1823 if (name == ctor_name)
1824 return true;
1825 if (IDENTIFIER_TEMPLATE (ctor_name)
1826 && name == IDENTIFIER_TEMPLATE (ctor_name))
1827 return true;
1828 return false;
1831 /* Counter used to create anonymous type names. */
1833 static GTY(()) int anon_cnt;
1835 /* Return an IDENTIFIER which can be used as a name for
1836 anonymous structs and unions. */
1838 tree
1839 make_anon_name (void)
1841 char buf[32];
1843 sprintf (buf, ANON_AGGRNAME_FORMAT, anon_cnt++);
1844 return get_identifier (buf);
1847 /* Return (from the stack of) the BINDING, if any, established at SCOPE. */
1849 static inline cxx_binding *
1850 find_binding (cxx_scope *scope, cxx_binding *binding)
1852 timevar_push (TV_NAME_LOOKUP);
1854 for (; binding != NULL; binding = binding->previous)
1855 if (binding->scope == scope)
1856 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, binding);
1858 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, (cxx_binding *)0);
1861 /* Return the binding for NAME in SCOPE, if any. Otherwise, return NULL. */
1863 static inline cxx_binding *
1864 cxx_scope_find_binding_for_name (cxx_scope *scope, tree name)
1866 cxx_binding *b = IDENTIFIER_NAMESPACE_BINDINGS (name);
1867 if (b)
1869 /* Fold-in case where NAME is used only once. */
1870 if (scope == b->scope && b->previous == NULL)
1871 return b;
1872 return find_binding (scope, b);
1874 return NULL;
1877 /* Always returns a binding for name in scope. If no binding is
1878 found, make a new one. */
1880 static cxx_binding *
1881 binding_for_name (cxx_scope *scope, tree name)
1883 cxx_binding *result;
1885 result = cxx_scope_find_binding_for_name (scope, name);
1886 if (result)
1887 return result;
1888 /* Not found, make a new one. */
1889 result = cxx_binding_make (NULL, NULL);
1890 result->previous = IDENTIFIER_NAMESPACE_BINDINGS (name);
1891 result->scope = scope;
1892 result->is_local = false;
1893 result->value_is_inherited = false;
1894 IDENTIFIER_NAMESPACE_BINDINGS (name) = result;
1895 return result;
1898 /* Walk through the bindings associated to the name of FUNCTION,
1899 and return the first binding that declares a function with a
1900 "C" linkage specification, a.k.a 'extern "C"'.
1901 This function looks for the binding, regardless of which scope it
1902 has been defined in. It basically looks in all the known scopes.
1903 Note that this function does not lookup for bindings of builtin functions
1904 or for functions declared in system headers. */
1905 static cxx_binding*
1906 lookup_extern_c_fun_binding_in_all_ns (tree function)
1908 tree name;
1909 cxx_binding *iter;
1911 gcc_assert (function && TREE_CODE (function) == FUNCTION_DECL);
1913 name = DECL_NAME (function);
1914 gcc_assert (name && TREE_CODE (name) == IDENTIFIER_NODE);
1916 for (iter = IDENTIFIER_NAMESPACE_BINDINGS (name);
1917 iter;
1918 iter = iter->previous)
1920 if (iter->value
1921 && TREE_CODE (iter->value) == FUNCTION_DECL
1922 && DECL_EXTERN_C_P (iter->value)
1923 && !DECL_ARTIFICIAL (iter->value))
1925 return iter;
1928 return NULL;
1931 /* Insert another USING_DECL into the current binding level, returning
1932 this declaration. If this is a redeclaration, do nothing, and
1933 return NULL_TREE if this not in namespace scope (in namespace
1934 scope, a using decl might extend any previous bindings). */
1936 static tree
1937 push_using_decl (tree scope, tree name)
1939 tree decl;
1941 timevar_push (TV_NAME_LOOKUP);
1942 gcc_assert (TREE_CODE (scope) == NAMESPACE_DECL);
1943 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
1944 for (decl = current_binding_level->usings; decl; decl = TREE_CHAIN (decl))
1945 if (USING_DECL_SCOPE (decl) == scope && DECL_NAME (decl) == name)
1946 break;
1947 if (decl)
1948 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP,
1949 namespace_bindings_p () ? decl : NULL_TREE);
1950 decl = build_lang_decl (USING_DECL, name, NULL_TREE);
1951 USING_DECL_SCOPE (decl) = scope;
1952 TREE_CHAIN (decl) = current_binding_level->usings;
1953 current_binding_level->usings = decl;
1954 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
1957 /* Same as pushdecl, but define X in binding-level LEVEL. We rely on the
1958 caller to set DECL_CONTEXT properly. */
1960 tree
1961 pushdecl_with_scope (tree x, cxx_scope *level, bool is_friend)
1963 struct cp_binding_level *b;
1964 tree function_decl = current_function_decl;
1966 timevar_push (TV_NAME_LOOKUP);
1967 current_function_decl = NULL_TREE;
1968 if (level->kind == sk_class)
1970 b = class_binding_level;
1971 class_binding_level = level;
1972 pushdecl_class_level (x);
1973 class_binding_level = b;
1975 else
1977 b = current_binding_level;
1978 current_binding_level = level;
1979 x = pushdecl_maybe_friend (x, is_friend);
1980 current_binding_level = b;
1982 current_function_decl = function_decl;
1983 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
1986 /* DECL is a FUNCTION_DECL for a non-member function, which may have
1987 other definitions already in place. We get around this by making
1988 the value of the identifier point to a list of all the things that
1989 want to be referenced by that name. It is then up to the users of
1990 that name to decide what to do with that list.
1992 DECL may also be a TEMPLATE_DECL, with a FUNCTION_DECL in its
1993 DECL_TEMPLATE_RESULT. It is dealt with the same way.
1995 FLAGS is a bitwise-or of the following values:
1996 PUSH_LOCAL: Bind DECL in the current scope, rather than at
1997 namespace scope.
1998 PUSH_USING: DECL is being pushed as the result of a using
1999 declaration.
2001 IS_FRIEND is true if this is a friend declaration.
2003 The value returned may be a previous declaration if we guessed wrong
2004 about what language DECL should belong to (C or C++). Otherwise,
2005 it's always DECL (and never something that's not a _DECL). */
2007 static tree
2008 push_overloaded_decl (tree decl, int flags, bool is_friend)
2010 tree name = DECL_NAME (decl);
2011 tree old;
2012 tree new_binding;
2013 int doing_global = (namespace_bindings_p () || !(flags & PUSH_LOCAL));
2015 timevar_push (TV_NAME_LOOKUP);
2016 if (doing_global)
2017 old = namespace_binding (name, DECL_CONTEXT (decl));
2018 else
2019 old = lookup_name_innermost_nonclass_level (name);
2021 if (old)
2023 if (TREE_CODE (old) == TYPE_DECL && DECL_ARTIFICIAL (old))
2025 tree t = TREE_TYPE (old);
2026 if (MAYBE_CLASS_TYPE_P (t) && warn_shadow
2027 && (! DECL_IN_SYSTEM_HEADER (decl)
2028 || ! DECL_IN_SYSTEM_HEADER (old)))
2029 warning (OPT_Wshadow, "%q#D hides constructor for %q#T", decl, t);
2030 old = NULL_TREE;
2032 else if (is_overloaded_fn (old))
2034 tree tmp;
2036 for (tmp = old; tmp; tmp = OVL_NEXT (tmp))
2038 tree fn = OVL_CURRENT (tmp);
2039 tree dup;
2041 if (TREE_CODE (tmp) == OVERLOAD && OVL_USED (tmp)
2042 && !(flags & PUSH_USING)
2043 && compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
2044 TYPE_ARG_TYPES (TREE_TYPE (decl)))
2045 && ! decls_match (fn, decl))
2046 error ("%q#D conflicts with previous using declaration %q#D",
2047 decl, fn);
2049 dup = duplicate_decls (decl, fn, is_friend);
2050 /* If DECL was a redeclaration of FN -- even an invalid
2051 one -- pass that information along to our caller. */
2052 if (dup == fn || dup == error_mark_node)
2053 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, dup);
2056 /* We don't overload implicit built-ins. duplicate_decls()
2057 may fail to merge the decls if the new decl is e.g. a
2058 template function. */
2059 if (TREE_CODE (old) == FUNCTION_DECL
2060 && DECL_ANTICIPATED (old)
2061 && !DECL_HIDDEN_FRIEND_P (old))
2062 old = NULL;
2064 else if (old == error_mark_node)
2065 /* Ignore the undefined symbol marker. */
2066 old = NULL_TREE;
2067 else
2069 error ("previous non-function declaration %q+#D", old);
2070 error ("conflicts with function declaration %q#D", decl);
2071 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
2075 if (old || TREE_CODE (decl) == TEMPLATE_DECL
2076 /* If it's a using declaration, we always need to build an OVERLOAD,
2077 because it's the only way to remember that the declaration comes
2078 from 'using', and have the lookup behave correctly. */
2079 || (flags & PUSH_USING))
2081 if (old && TREE_CODE (old) != OVERLOAD)
2082 new_binding = ovl_cons (decl, ovl_cons (old, NULL_TREE));
2083 else
2084 new_binding = ovl_cons (decl, old);
2085 if (flags & PUSH_USING)
2086 OVL_USED (new_binding) = 1;
2088 else
2089 /* NAME is not ambiguous. */
2090 new_binding = decl;
2092 if (doing_global)
2093 set_namespace_binding (name, current_namespace, new_binding);
2094 else
2096 /* We only create an OVERLOAD if there was a previous binding at
2097 this level, or if decl is a template. In the former case, we
2098 need to remove the old binding and replace it with the new
2099 binding. We must also run through the NAMES on the binding
2100 level where the name was bound to update the chain. */
2102 if (TREE_CODE (new_binding) == OVERLOAD && old)
2104 tree *d;
2106 for (d = &IDENTIFIER_BINDING (name)->scope->names;
2108 d = &TREE_CHAIN (*d))
2109 if (*d == old
2110 || (TREE_CODE (*d) == TREE_LIST
2111 && TREE_VALUE (*d) == old))
2113 if (TREE_CODE (*d) == TREE_LIST)
2114 /* Just replace the old binding with the new. */
2115 TREE_VALUE (*d) = new_binding;
2116 else
2117 /* Build a TREE_LIST to wrap the OVERLOAD. */
2118 *d = tree_cons (NULL_TREE, new_binding,
2119 TREE_CHAIN (*d));
2121 /* And update the cxx_binding node. */
2122 IDENTIFIER_BINDING (name)->value = new_binding;
2123 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
2126 /* We should always find a previous binding in this case. */
2127 gcc_unreachable ();
2130 /* Install the new binding. */
2131 push_local_binding (name, new_binding, flags);
2134 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
2137 /* Check a non-member using-declaration. Return the name and scope
2138 being used, and the USING_DECL, or NULL_TREE on failure. */
2140 static tree
2141 validate_nonmember_using_decl (tree decl, tree scope, tree name)
2143 /* [namespace.udecl]
2144 A using-declaration for a class member shall be a
2145 member-declaration. */
2146 if (TYPE_P (scope))
2148 error ("%qT is not a namespace", scope);
2149 return NULL_TREE;
2151 else if (scope == error_mark_node)
2152 return NULL_TREE;
2154 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR)
2156 /* 7.3.3/5
2157 A using-declaration shall not name a template-id. */
2158 error ("a using-declaration cannot specify a template-id. "
2159 "Try %<using %D%>", name);
2160 return NULL_TREE;
2163 if (TREE_CODE (decl) == NAMESPACE_DECL)
2165 error ("namespace %qD not allowed in using-declaration", decl);
2166 return NULL_TREE;
2169 if (TREE_CODE (decl) == SCOPE_REF)
2171 /* It's a nested name with template parameter dependent scope.
2172 This can only be using-declaration for class member. */
2173 error ("%qT is not a namespace", TREE_OPERAND (decl, 0));
2174 return NULL_TREE;
2177 if (is_overloaded_fn (decl))
2178 decl = get_first_fn (decl);
2180 gcc_assert (DECL_P (decl));
2182 /* Make a USING_DECL. */
2183 return push_using_decl (scope, name);
2186 /* Process local and global using-declarations. */
2188 static void
2189 do_nonmember_using_decl (tree scope, tree name, tree oldval, tree oldtype,
2190 tree *newval, tree *newtype)
2192 struct scope_binding decls = EMPTY_SCOPE_BINDING;
2194 *newval = *newtype = NULL_TREE;
2195 if (!qualified_lookup_using_namespace (name, scope, &decls, 0))
2196 /* Lookup error */
2197 return;
2199 if (!decls.value && !decls.type)
2201 error ("%qD not declared", name);
2202 return;
2205 /* Shift the old and new bindings around so we're comparing class and
2206 enumeration names to each other. */
2207 if (oldval && DECL_IMPLICIT_TYPEDEF_P (oldval))
2209 oldtype = oldval;
2210 oldval = NULL_TREE;
2213 if (decls.value && DECL_IMPLICIT_TYPEDEF_P (decls.value))
2215 decls.type = decls.value;
2216 decls.value = NULL_TREE;
2219 /* It is impossible to overload a built-in function; any explicit
2220 declaration eliminates the built-in declaration. So, if OLDVAL
2221 is a built-in, then we can just pretend it isn't there. */
2222 if (oldval
2223 && TREE_CODE (oldval) == FUNCTION_DECL
2224 && DECL_ANTICIPATED (oldval)
2225 && !DECL_HIDDEN_FRIEND_P (oldval))
2226 oldval = NULL_TREE;
2228 if (decls.value)
2230 /* Check for using functions. */
2231 if (is_overloaded_fn (decls.value))
2233 tree tmp, tmp1;
2235 if (oldval && !is_overloaded_fn (oldval))
2237 error ("%qD is already declared in this scope", name);
2238 oldval = NULL_TREE;
2241 *newval = oldval;
2242 for (tmp = decls.value; tmp; tmp = OVL_NEXT (tmp))
2244 tree new_fn = OVL_CURRENT (tmp);
2246 /* [namespace.udecl]
2248 If a function declaration in namespace scope or block
2249 scope has the same name and the same parameter types as a
2250 function introduced by a using declaration the program is
2251 ill-formed. */
2252 for (tmp1 = oldval; tmp1; tmp1 = OVL_NEXT (tmp1))
2254 tree old_fn = OVL_CURRENT (tmp1);
2256 if (new_fn == old_fn)
2257 /* The function already exists in the current namespace. */
2258 break;
2259 else if (OVL_USED (tmp1))
2260 continue; /* this is a using decl */
2261 else if (compparms (TYPE_ARG_TYPES (TREE_TYPE (new_fn)),
2262 TYPE_ARG_TYPES (TREE_TYPE (old_fn))))
2264 gcc_assert (!DECL_ANTICIPATED (old_fn)
2265 || DECL_HIDDEN_FRIEND_P (old_fn));
2267 /* There was already a non-using declaration in
2268 this scope with the same parameter types. If both
2269 are the same extern "C" functions, that's ok. */
2270 if (decls_match (new_fn, old_fn))
2271 break;
2272 else
2274 error ("%qD is already declared in this scope", name);
2275 break;
2280 /* If we broke out of the loop, there's no reason to add
2281 this function to the using declarations for this
2282 scope. */
2283 if (tmp1)
2284 continue;
2286 /* If we are adding to an existing OVERLOAD, then we no
2287 longer know the type of the set of functions. */
2288 if (*newval && TREE_CODE (*newval) == OVERLOAD)
2289 TREE_TYPE (*newval) = unknown_type_node;
2290 /* Add this new function to the set. */
2291 *newval = build_overload (OVL_CURRENT (tmp), *newval);
2292 /* If there is only one function, then we use its type. (A
2293 using-declaration naming a single function can be used in
2294 contexts where overload resolution cannot be
2295 performed.) */
2296 if (TREE_CODE (*newval) != OVERLOAD)
2298 *newval = ovl_cons (*newval, NULL_TREE);
2299 TREE_TYPE (*newval) = TREE_TYPE (OVL_CURRENT (tmp));
2301 OVL_USED (*newval) = 1;
2304 else
2306 *newval = decls.value;
2307 if (oldval && !decls_match (*newval, oldval))
2308 error ("%qD is already declared in this scope", name);
2311 else
2312 *newval = oldval;
2314 if (decls.type && TREE_CODE (decls.type) == TREE_LIST)
2316 error ("reference to %qD is ambiguous", name);
2317 print_candidates (decls.type);
2319 else
2321 *newtype = decls.type;
2322 if (oldtype && *newtype && !decls_match (oldtype, *newtype))
2323 error ("%qD is already declared in this scope", name);
2326 /* If *newval is empty, shift any class or enumeration name down. */
2327 if (!*newval)
2329 *newval = *newtype;
2330 *newtype = NULL_TREE;
2334 /* Process a using-declaration at function scope. */
2336 void
2337 do_local_using_decl (tree decl, tree scope, tree name)
2339 tree oldval, oldtype, newval, newtype;
2340 tree orig_decl = decl;
2342 decl = validate_nonmember_using_decl (decl, scope, name);
2343 if (decl == NULL_TREE)
2344 return;
2346 if (building_stmt_tree ()
2347 && at_function_scope_p ())
2348 add_decl_expr (decl);
2350 oldval = lookup_name_innermost_nonclass_level (name);
2351 oldtype = lookup_type_current_level (name);
2353 do_nonmember_using_decl (scope, name, oldval, oldtype, &newval, &newtype);
2355 if (newval)
2357 if (is_overloaded_fn (newval))
2359 tree fn, term;
2361 /* We only need to push declarations for those functions
2362 that were not already bound in the current level.
2363 The old value might be NULL_TREE, it might be a single
2364 function, or an OVERLOAD. */
2365 if (oldval && TREE_CODE (oldval) == OVERLOAD)
2366 term = OVL_FUNCTION (oldval);
2367 else
2368 term = oldval;
2369 for (fn = newval; fn && OVL_CURRENT (fn) != term;
2370 fn = OVL_NEXT (fn))
2371 push_overloaded_decl (OVL_CURRENT (fn),
2372 PUSH_LOCAL | PUSH_USING,
2373 false);
2375 else
2376 push_local_binding (name, newval, PUSH_USING);
2378 if (newtype)
2380 push_local_binding (name, newtype, PUSH_USING);
2381 set_identifier_type_value (name, newtype);
2384 /* Emit debug info. */
2385 if (!processing_template_decl)
2386 cp_emit_debug_info_for_using (orig_decl, current_scope());
2389 /* Returns true if ROOT (a namespace, class, or function) encloses
2390 CHILD. CHILD may be either a class type or a namespace. */
2392 bool
2393 is_ancestor (tree root, tree child)
2395 gcc_assert ((TREE_CODE (root) == NAMESPACE_DECL
2396 || TREE_CODE (root) == FUNCTION_DECL
2397 || CLASS_TYPE_P (root)));
2398 gcc_assert ((TREE_CODE (child) == NAMESPACE_DECL
2399 || CLASS_TYPE_P (child)));
2401 /* The global namespace encloses everything. */
2402 if (root == global_namespace)
2403 return true;
2405 while (true)
2407 /* If we've run out of scopes, stop. */
2408 if (!child)
2409 return false;
2410 /* If we've reached the ROOT, it encloses CHILD. */
2411 if (root == child)
2412 return true;
2413 /* Go out one level. */
2414 if (TYPE_P (child))
2415 child = TYPE_NAME (child);
2416 child = DECL_CONTEXT (child);
2420 /* Enter the class or namespace scope indicated by T suitable for name
2421 lookup. T can be arbitrary scope, not necessary nested inside the
2422 current scope. Returns a non-null scope to pop iff pop_scope
2423 should be called later to exit this scope. */
2425 tree
2426 push_scope (tree t)
2428 if (TREE_CODE (t) == NAMESPACE_DECL)
2429 push_decl_namespace (t);
2430 else if (CLASS_TYPE_P (t))
2432 if (!at_class_scope_p ()
2433 || !same_type_p (current_class_type, t))
2434 push_nested_class (t);
2435 else
2436 /* T is the same as the current scope. There is therefore no
2437 need to re-enter the scope. Since we are not actually
2438 pushing a new scope, our caller should not call
2439 pop_scope. */
2440 t = NULL_TREE;
2443 return t;
2446 /* Leave scope pushed by push_scope. */
2448 void
2449 pop_scope (tree t)
2451 if (TREE_CODE (t) == NAMESPACE_DECL)
2452 pop_decl_namespace ();
2453 else if CLASS_TYPE_P (t)
2454 pop_nested_class ();
2457 /* Subroutine of push_inner_scope. */
2459 static void
2460 push_inner_scope_r (tree outer, tree inner)
2462 tree prev;
2464 if (outer == inner
2465 || (TREE_CODE (inner) != NAMESPACE_DECL && !CLASS_TYPE_P (inner)))
2466 return;
2468 prev = CP_DECL_CONTEXT (TREE_CODE (inner) == NAMESPACE_DECL ? inner : TYPE_NAME (inner));
2469 if (outer != prev)
2470 push_inner_scope_r (outer, prev);
2471 if (TREE_CODE (inner) == NAMESPACE_DECL)
2473 struct cp_binding_level *save_template_parm = 0;
2474 /* Temporary take out template parameter scopes. They are saved
2475 in reversed order in save_template_parm. */
2476 while (current_binding_level->kind == sk_template_parms)
2478 struct cp_binding_level *b = current_binding_level;
2479 current_binding_level = b->level_chain;
2480 b->level_chain = save_template_parm;
2481 save_template_parm = b;
2484 resume_scope (NAMESPACE_LEVEL (inner));
2485 current_namespace = inner;
2487 /* Restore template parameter scopes. */
2488 while (save_template_parm)
2490 struct cp_binding_level *b = save_template_parm;
2491 save_template_parm = b->level_chain;
2492 b->level_chain = current_binding_level;
2493 current_binding_level = b;
2496 else
2497 pushclass (inner);
2500 /* Enter the scope INNER from current scope. INNER must be a scope
2501 nested inside current scope. This works with both name lookup and
2502 pushing name into scope. In case a template parameter scope is present,
2503 namespace is pushed under the template parameter scope according to
2504 name lookup rule in 14.6.1/6.
2506 Return the former current scope suitable for pop_inner_scope. */
2508 tree
2509 push_inner_scope (tree inner)
2511 tree outer = current_scope ();
2512 if (!outer)
2513 outer = current_namespace;
2515 push_inner_scope_r (outer, inner);
2516 return outer;
2519 /* Exit the current scope INNER back to scope OUTER. */
2521 void
2522 pop_inner_scope (tree outer, tree inner)
2524 if (outer == inner
2525 || (TREE_CODE (inner) != NAMESPACE_DECL && !CLASS_TYPE_P (inner)))
2526 return;
2528 while (outer != inner)
2530 if (TREE_CODE (inner) == NAMESPACE_DECL)
2532 struct cp_binding_level *save_template_parm = 0;
2533 /* Temporary take out template parameter scopes. They are saved
2534 in reversed order in save_template_parm. */
2535 while (current_binding_level->kind == sk_template_parms)
2537 struct cp_binding_level *b = current_binding_level;
2538 current_binding_level = b->level_chain;
2539 b->level_chain = save_template_parm;
2540 save_template_parm = b;
2543 pop_namespace ();
2545 /* Restore template parameter scopes. */
2546 while (save_template_parm)
2548 struct cp_binding_level *b = save_template_parm;
2549 save_template_parm = b->level_chain;
2550 b->level_chain = current_binding_level;
2551 current_binding_level = b;
2554 else
2555 popclass ();
2557 inner = CP_DECL_CONTEXT (TREE_CODE (inner) == NAMESPACE_DECL ? inner : TYPE_NAME (inner));
2561 /* Do a pushlevel for class declarations. */
2563 void
2564 pushlevel_class (void)
2566 if (ENABLE_SCOPE_CHECKING)
2567 is_class_level = 1;
2569 class_binding_level = begin_scope (sk_class, current_class_type);
2572 /* ...and a poplevel for class declarations. */
2574 void
2575 poplevel_class (void)
2577 struct cp_binding_level *level = class_binding_level;
2578 cp_class_binding *cb;
2579 size_t i;
2580 tree shadowed;
2582 timevar_push (TV_NAME_LOOKUP);
2583 gcc_assert (level != 0);
2585 /* If we're leaving a toplevel class, cache its binding level. */
2586 if (current_class_depth == 1)
2587 previous_class_level = level;
2588 for (shadowed = level->type_shadowed;
2589 shadowed;
2590 shadowed = TREE_CHAIN (shadowed))
2591 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (shadowed), TREE_VALUE (shadowed));
2593 /* Remove the bindings for all of the class-level declarations. */
2594 if (level->class_shadowed)
2596 for (i = 0;
2597 VEC_iterate (cp_class_binding, level->class_shadowed, i, cb);
2598 ++i)
2599 IDENTIFIER_BINDING (cb->identifier) = cb->base.previous;
2600 ggc_free (level->class_shadowed);
2601 level->class_shadowed = NULL;
2604 /* Now, pop out of the binding level which we created up in the
2605 `pushlevel_class' routine. */
2606 if (ENABLE_SCOPE_CHECKING)
2607 is_class_level = 1;
2609 leave_scope ();
2610 timevar_pop (TV_NAME_LOOKUP);
2613 /* Set INHERITED_VALUE_BINDING_P on BINDING to true or false, as
2614 appropriate. DECL is the value to which a name has just been
2615 bound. CLASS_TYPE is the class in which the lookup occurred. */
2617 static void
2618 set_inherited_value_binding_p (cxx_binding *binding, tree decl,
2619 tree class_type)
2621 if (binding->value == decl && TREE_CODE (decl) != TREE_LIST)
2623 tree context;
2625 if (TREE_CODE (decl) == OVERLOAD)
2626 context = CP_DECL_CONTEXT (OVL_CURRENT (decl));
2627 else
2629 gcc_assert (DECL_P (decl));
2630 context = context_for_name_lookup (decl);
2633 if (is_properly_derived_from (class_type, context))
2634 INHERITED_VALUE_BINDING_P (binding) = 1;
2635 else
2636 INHERITED_VALUE_BINDING_P (binding) = 0;
2638 else if (binding->value == decl)
2639 /* We only encounter a TREE_LIST when there is an ambiguity in the
2640 base classes. Such an ambiguity can be overridden by a
2641 definition in this class. */
2642 INHERITED_VALUE_BINDING_P (binding) = 1;
2643 else
2644 INHERITED_VALUE_BINDING_P (binding) = 0;
2647 /* Make the declaration of X appear in CLASS scope. */
2649 bool
2650 pushdecl_class_level (tree x)
2652 tree name;
2653 bool is_valid = true;
2655 timevar_push (TV_NAME_LOOKUP);
2656 /* Get the name of X. */
2657 if (TREE_CODE (x) == OVERLOAD)
2658 name = DECL_NAME (get_first_fn (x));
2659 else
2660 name = DECL_NAME (x);
2662 if (name)
2664 is_valid = push_class_level_binding (name, x);
2665 if (TREE_CODE (x) == TYPE_DECL)
2666 set_identifier_type_value (name, x);
2668 else if (ANON_AGGR_TYPE_P (TREE_TYPE (x)))
2670 /* If X is an anonymous aggregate, all of its members are
2671 treated as if they were members of the class containing the
2672 aggregate, for naming purposes. */
2673 tree f;
2675 for (f = TYPE_FIELDS (TREE_TYPE (x)); f; f = TREE_CHAIN (f))
2677 location_t save_location = input_location;
2678 input_location = DECL_SOURCE_LOCATION (f);
2679 if (!pushdecl_class_level (f))
2680 is_valid = false;
2681 input_location = save_location;
2684 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, is_valid);
2687 /* Return the BINDING (if any) for NAME in SCOPE, which is a class
2688 scope. If the value returned is non-NULL, and the PREVIOUS field
2689 is not set, callers must set the PREVIOUS field explicitly. */
2691 static cxx_binding *
2692 get_class_binding (tree name, cxx_scope *scope)
2694 tree class_type;
2695 tree type_binding;
2696 tree value_binding;
2697 cxx_binding *binding;
2699 class_type = scope->this_entity;
2701 /* Get the type binding. */
2702 type_binding = lookup_member (class_type, name,
2703 /*protect=*/2, /*want_type=*/true);
2704 /* Get the value binding. */
2705 value_binding = lookup_member (class_type, name,
2706 /*protect=*/2, /*want_type=*/false);
2708 if (value_binding
2709 && (TREE_CODE (value_binding) == TYPE_DECL
2710 || DECL_CLASS_TEMPLATE_P (value_binding)
2711 || (TREE_CODE (value_binding) == TREE_LIST
2712 && TREE_TYPE (value_binding) == error_mark_node
2713 && (TREE_CODE (TREE_VALUE (value_binding))
2714 == TYPE_DECL))))
2715 /* We found a type binding, even when looking for a non-type
2716 binding. This means that we already processed this binding
2717 above. */
2719 else if (value_binding)
2721 if (TREE_CODE (value_binding) == TREE_LIST
2722 && TREE_TYPE (value_binding) == error_mark_node)
2723 /* NAME is ambiguous. */
2725 else if (BASELINK_P (value_binding))
2726 /* NAME is some overloaded functions. */
2727 value_binding = BASELINK_FUNCTIONS (value_binding);
2730 /* If we found either a type binding or a value binding, create a
2731 new binding object. */
2732 if (type_binding || value_binding)
2734 binding = new_class_binding (name,
2735 value_binding,
2736 type_binding,
2737 scope);
2738 /* This is a class-scope binding, not a block-scope binding. */
2739 LOCAL_BINDING_P (binding) = 0;
2740 set_inherited_value_binding_p (binding, value_binding, class_type);
2742 else
2743 binding = NULL;
2745 return binding;
2748 /* Make the declaration(s) of X appear in CLASS scope under the name
2749 NAME. Returns true if the binding is valid. */
2751 bool
2752 push_class_level_binding (tree name, tree x)
2754 cxx_binding *binding;
2755 tree decl = x;
2756 bool ok;
2758 timevar_push (TV_NAME_LOOKUP);
2759 /* The class_binding_level will be NULL if x is a template
2760 parameter name in a member template. */
2761 if (!class_binding_level)
2762 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, true);
2764 if (name == error_mark_node)
2765 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, false);
2767 /* Check for invalid member names. */
2768 gcc_assert (TYPE_BEING_DEFINED (current_class_type));
2769 /* We could have been passed a tree list if this is an ambiguous
2770 declaration. If so, pull the declaration out because
2771 check_template_shadow will not handle a TREE_LIST. */
2772 if (TREE_CODE (decl) == TREE_LIST
2773 && TREE_TYPE (decl) == error_mark_node)
2774 decl = TREE_VALUE (decl);
2776 if (!check_template_shadow (decl))
2777 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, false);
2779 /* [class.mem]
2781 If T is the name of a class, then each of the following shall
2782 have a name different from T:
2784 -- every static data member of class T;
2786 -- every member of class T that is itself a type;
2788 -- every enumerator of every member of class T that is an
2789 enumerated type;
2791 -- every member of every anonymous union that is a member of
2792 class T.
2794 (Non-static data members were also forbidden to have the same
2795 name as T until TC1.) */
2796 if ((TREE_CODE (x) == VAR_DECL
2797 || TREE_CODE (x) == CONST_DECL
2798 || (TREE_CODE (x) == TYPE_DECL
2799 && !DECL_SELF_REFERENCE_P (x))
2800 /* A data member of an anonymous union. */
2801 || (TREE_CODE (x) == FIELD_DECL
2802 && DECL_CONTEXT (x) != current_class_type))
2803 && DECL_NAME (x) == constructor_name (current_class_type))
2805 tree scope = context_for_name_lookup (x);
2806 if (TYPE_P (scope) && same_type_p (scope, current_class_type))
2808 error ("%qD has the same name as the class in which it is "
2809 "declared",
2811 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, false);
2815 /* Get the current binding for NAME in this class, if any. */
2816 binding = IDENTIFIER_BINDING (name);
2817 if (!binding || binding->scope != class_binding_level)
2819 binding = get_class_binding (name, class_binding_level);
2820 /* If a new binding was created, put it at the front of the
2821 IDENTIFIER_BINDING list. */
2822 if (binding)
2824 binding->previous = IDENTIFIER_BINDING (name);
2825 IDENTIFIER_BINDING (name) = binding;
2829 /* If there is already a binding, then we may need to update the
2830 current value. */
2831 if (binding && binding->value)
2833 tree bval = binding->value;
2834 tree old_decl = NULL_TREE;
2836 if (INHERITED_VALUE_BINDING_P (binding))
2838 /* If the old binding was from a base class, and was for a
2839 tag name, slide it over to make room for the new binding.
2840 The old binding is still visible if explicitly qualified
2841 with a class-key. */
2842 if (TREE_CODE (bval) == TYPE_DECL && DECL_ARTIFICIAL (bval)
2843 && !(TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x)))
2845 old_decl = binding->type;
2846 binding->type = bval;
2847 binding->value = NULL_TREE;
2848 INHERITED_VALUE_BINDING_P (binding) = 0;
2850 else
2852 old_decl = bval;
2853 /* Any inherited type declaration is hidden by the type
2854 declaration in the derived class. */
2855 if (TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x))
2856 binding->type = NULL_TREE;
2859 else if (TREE_CODE (x) == OVERLOAD && is_overloaded_fn (bval))
2860 old_decl = bval;
2861 else if (TREE_CODE (x) == USING_DECL && TREE_CODE (bval) == USING_DECL)
2862 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, true);
2863 else if (TREE_CODE (x) == USING_DECL && is_overloaded_fn (bval))
2864 old_decl = bval;
2865 else if (TREE_CODE (bval) == USING_DECL && is_overloaded_fn (x))
2866 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, true);
2868 if (old_decl && binding->scope == class_binding_level)
2870 binding->value = x;
2871 /* It is always safe to clear INHERITED_VALUE_BINDING_P
2872 here. This function is only used to register bindings
2873 from with the class definition itself. */
2874 INHERITED_VALUE_BINDING_P (binding) = 0;
2875 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, true);
2879 /* Note that we declared this value so that we can issue an error if
2880 this is an invalid redeclaration of a name already used for some
2881 other purpose. */
2882 note_name_declared_in_class (name, decl);
2884 /* If we didn't replace an existing binding, put the binding on the
2885 stack of bindings for the identifier, and update the shadowed
2886 list. */
2887 if (binding && binding->scope == class_binding_level)
2888 /* Supplement the existing binding. */
2889 ok = supplement_binding (binding, decl);
2890 else
2892 /* Create a new binding. */
2893 push_binding (name, decl, class_binding_level);
2894 ok = true;
2897 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, ok);
2900 /* Process "using SCOPE::NAME" in a class scope. Return the
2901 USING_DECL created. */
2903 tree
2904 do_class_using_decl (tree scope, tree name)
2906 /* The USING_DECL returned by this function. */
2907 tree value;
2908 /* The declaration (or declarations) name by this using
2909 declaration. NULL if we are in a template and cannot figure out
2910 what has been named. */
2911 tree decl;
2912 /* True if SCOPE is a dependent type. */
2913 bool scope_dependent_p;
2914 /* True if SCOPE::NAME is dependent. */
2915 bool name_dependent_p;
2916 /* True if any of the bases of CURRENT_CLASS_TYPE are dependent. */
2917 bool bases_dependent_p;
2918 tree binfo;
2919 tree base_binfo;
2920 int i;
2922 if (name == error_mark_node)
2923 return NULL_TREE;
2925 if (!scope || !TYPE_P (scope))
2927 error ("using-declaration for non-member at class scope");
2928 return NULL_TREE;
2931 /* Make sure the name is not invalid */
2932 if (TREE_CODE (name) == BIT_NOT_EXPR)
2934 error ("%<%T::%D%> names destructor", scope, name);
2935 return NULL_TREE;
2937 if (MAYBE_CLASS_TYPE_P (scope) && constructor_name_p (name, scope))
2939 error ("%<%T::%D%> names constructor", scope, name);
2940 return NULL_TREE;
2942 if (constructor_name_p (name, current_class_type))
2944 error ("%<%T::%D%> names constructor in %qT",
2945 scope, name, current_class_type);
2946 return NULL_TREE;
2949 scope_dependent_p = dependent_type_p (scope);
2950 name_dependent_p = (scope_dependent_p
2951 || (IDENTIFIER_TYPENAME_P (name)
2952 && dependent_type_p (TREE_TYPE (name))));
2954 bases_dependent_p = false;
2955 if (processing_template_decl)
2956 for (binfo = TYPE_BINFO (current_class_type), i = 0;
2957 BINFO_BASE_ITERATE (binfo, i, base_binfo);
2958 i++)
2959 if (dependent_type_p (TREE_TYPE (base_binfo)))
2961 bases_dependent_p = true;
2962 break;
2965 decl = NULL_TREE;
2967 /* From [namespace.udecl]:
2969 A using-declaration used as a member-declaration shall refer to a
2970 member of a base class of the class being defined.
2972 In general, we cannot check this constraint in a template because
2973 we do not know the entire set of base classes of the current
2974 class type. However, if all of the base classes are
2975 non-dependent, then we can avoid delaying the check until
2976 instantiation. */
2977 if (!scope_dependent_p)
2979 base_kind b_kind;
2980 binfo = lookup_base (current_class_type, scope, ba_any, &b_kind);
2981 if (b_kind < bk_proper_base)
2983 if (!bases_dependent_p)
2985 error_not_base_type (scope, current_class_type);
2986 return NULL_TREE;
2989 else if (!name_dependent_p)
2991 decl = lookup_member (binfo, name, 0, false);
2992 if (!decl)
2994 error ("no members matching %<%T::%D%> in %q#T", scope, name,
2995 scope);
2996 return NULL_TREE;
2998 /* The binfo from which the functions came does not matter. */
2999 if (BASELINK_P (decl))
3000 decl = BASELINK_FUNCTIONS (decl);
3004 value = build_lang_decl (USING_DECL, name, NULL_TREE);
3005 USING_DECL_DECLS (value) = decl;
3006 USING_DECL_SCOPE (value) = scope;
3007 DECL_DEPENDENT_P (value) = !decl;
3009 return value;
3013 /* Return the binding value for name in scope. */
3015 tree
3016 namespace_binding (tree name, tree scope)
3018 cxx_binding *binding;
3020 if (scope == NULL)
3021 scope = global_namespace;
3022 else
3023 /* Unnecessary for the global namespace because it can't be an alias. */
3024 scope = ORIGINAL_NAMESPACE (scope);
3026 binding = cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (scope), name);
3028 return binding ? binding->value : NULL_TREE;
3031 /* Set the binding value for name in scope. */
3033 void
3034 set_namespace_binding (tree name, tree scope, tree val)
3036 cxx_binding *b;
3038 timevar_push (TV_NAME_LOOKUP);
3039 if (scope == NULL_TREE)
3040 scope = global_namespace;
3041 b = binding_for_name (NAMESPACE_LEVEL (scope), name);
3042 if (!b->value || TREE_CODE (val) == OVERLOAD || val == error_mark_node)
3043 b->value = val;
3044 else
3045 supplement_binding (b, val);
3046 timevar_pop (TV_NAME_LOOKUP);
3049 /* Set the context of a declaration to scope. Complain if we are not
3050 outside scope. */
3052 void
3053 set_decl_namespace (tree decl, tree scope, bool friendp)
3055 tree old, fn;
3057 /* Get rid of namespace aliases. */
3058 scope = ORIGINAL_NAMESPACE (scope);
3060 /* It is ok for friends to be qualified in parallel space. */
3061 if (!friendp && !is_ancestor (current_namespace, scope))
3062 error ("declaration of %qD not in a namespace surrounding %qD",
3063 decl, scope);
3064 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
3066 /* Writing "int N::i" to declare a variable within "N" is invalid. */
3067 if (scope == current_namespace)
3069 if (at_namespace_scope_p ())
3070 error ("explicit qualification in declaration of %qD",
3071 decl);
3072 return;
3075 /* See whether this has been declared in the namespace. */
3076 old = lookup_qualified_name (scope, DECL_NAME (decl), false, true);
3077 if (old == error_mark_node)
3078 /* No old declaration at all. */
3079 goto complain;
3080 if (!is_overloaded_fn (decl))
3081 /* Don't compare non-function decls with decls_match here, since
3082 it can't check for the correct constness at this
3083 point. pushdecl will find those errors later. */
3084 return;
3085 /* Since decl is a function, old should contain a function decl. */
3086 if (!is_overloaded_fn (old))
3087 goto complain;
3088 fn = OVL_CURRENT (old);
3089 if (!is_associated_namespace (scope, CP_DECL_CONTEXT (fn)))
3090 goto complain;
3091 /* A template can be explicitly specialized in any namespace. */
3092 if (processing_explicit_instantiation)
3093 return;
3094 if (processing_template_decl || processing_specialization)
3095 /* We have not yet called push_template_decl to turn a
3096 FUNCTION_DECL into a TEMPLATE_DECL, so the declarations won't
3097 match. But, we'll check later, when we construct the
3098 template. */
3099 return;
3100 /* Instantiations or specializations of templates may be declared as
3101 friends in any namespace. */
3102 if (friendp && DECL_USE_TEMPLATE (decl))
3103 return;
3104 if (is_overloaded_fn (old))
3106 for (; old; old = OVL_NEXT (old))
3107 if (decls_match (decl, OVL_CURRENT (old)))
3108 return;
3110 else if (decls_match (decl, old))
3111 return;
3112 complain:
3113 error ("%qD should have been declared inside %qD", decl, scope);
3116 /* Return the namespace where the current declaration is declared. */
3118 static tree
3119 current_decl_namespace (void)
3121 tree result;
3122 /* If we have been pushed into a different namespace, use it. */
3123 if (decl_namespace_list)
3124 return TREE_PURPOSE (decl_namespace_list);
3126 if (current_class_type)
3127 result = decl_namespace_context (current_class_type);
3128 else if (current_function_decl)
3129 result = decl_namespace_context (current_function_decl);
3130 else
3131 result = current_namespace;
3132 return result;
3135 /* Process any ATTRIBUTES on a namespace definition. Currently only
3136 attribute visibility is meaningful, which is a property of the syntactic
3137 block rather than the namespace as a whole, so we don't touch the
3138 NAMESPACE_DECL at all. Returns true if attribute visibility is seen. */
3140 bool
3141 handle_namespace_attrs (tree ns, tree attributes)
3143 tree d;
3144 bool saw_vis = false;
3146 for (d = attributes; d; d = TREE_CHAIN (d))
3148 tree name = TREE_PURPOSE (d);
3149 tree args = TREE_VALUE (d);
3151 #ifdef HANDLE_PRAGMA_VISIBILITY
3152 if (is_attribute_p ("visibility", name))
3154 tree x = args ? TREE_VALUE (args) : NULL_TREE;
3155 if (x == NULL_TREE || TREE_CODE (x) != STRING_CST || TREE_CHAIN (args))
3157 warning (OPT_Wattributes,
3158 "%qD attribute requires a single NTBS argument",
3159 name);
3160 continue;
3163 if (!TREE_PUBLIC (ns))
3164 warning (OPT_Wattributes,
3165 "%qD attribute is meaningless since members of the "
3166 "anonymous namespace get local symbols", name);
3168 push_visibility (TREE_STRING_POINTER (x));
3169 saw_vis = true;
3171 else
3172 #endif
3174 warning (OPT_Wattributes, "%qD attribute directive ignored",
3175 name);
3176 continue;
3180 return saw_vis;
3183 /* Push into the scope of the NAME namespace. If NAME is NULL_TREE, then we
3184 select a name that is unique to this compilation unit. */
3186 void
3187 push_namespace (tree name)
3189 tree d = NULL_TREE;
3190 int need_new = 1;
3191 int implicit_use = 0;
3192 bool anon = !name;
3194 timevar_push (TV_NAME_LOOKUP);
3196 /* We should not get here if the global_namespace is not yet constructed
3197 nor if NAME designates the global namespace: The global scope is
3198 constructed elsewhere. */
3199 gcc_assert (global_namespace != NULL && name != global_scope_name);
3201 if (anon)
3203 name = get_anonymous_namespace_name();
3204 d = IDENTIFIER_NAMESPACE_VALUE (name);
3205 if (d)
3206 /* Reopening anonymous namespace. */
3207 need_new = 0;
3208 implicit_use = 1;
3210 else
3212 /* Check whether this is an extended namespace definition. */
3213 d = IDENTIFIER_NAMESPACE_VALUE (name);
3214 if (d != NULL_TREE && TREE_CODE (d) == NAMESPACE_DECL)
3216 need_new = 0;
3217 if (DECL_NAMESPACE_ALIAS (d))
3219 error ("namespace alias %qD not allowed here, assuming %qD",
3220 d, DECL_NAMESPACE_ALIAS (d));
3221 d = DECL_NAMESPACE_ALIAS (d);
3226 if (need_new)
3228 /* Make a new namespace, binding the name to it. */
3229 d = build_lang_decl (NAMESPACE_DECL, name, void_type_node);
3230 DECL_CONTEXT (d) = FROB_CONTEXT (current_namespace);
3231 /* The name of this namespace is not visible to other translation
3232 units if it is an anonymous namespace or member thereof. */
3233 if (anon || decl_anon_ns_mem_p (current_namespace))
3234 TREE_PUBLIC (d) = 0;
3235 else
3236 TREE_PUBLIC (d) = 1;
3237 pushdecl (d);
3238 if (anon)
3240 /* Clear DECL_NAME for the benefit of debugging back ends. */
3241 SET_DECL_ASSEMBLER_NAME (d, name);
3242 DECL_NAME (d) = NULL_TREE;
3244 begin_scope (sk_namespace, d);
3246 else
3247 resume_scope (NAMESPACE_LEVEL (d));
3249 if (implicit_use)
3250 do_using_directive (d);
3251 /* Enter the name space. */
3252 current_namespace = d;
3254 timevar_pop (TV_NAME_LOOKUP);
3257 /* Pop from the scope of the current namespace. */
3259 void
3260 pop_namespace (void)
3262 gcc_assert (current_namespace != global_namespace);
3263 current_namespace = CP_DECL_CONTEXT (current_namespace);
3264 /* The binding level is not popped, as it might be re-opened later. */
3265 leave_scope ();
3268 /* Push into the scope of the namespace NS, even if it is deeply
3269 nested within another namespace. */
3271 void
3272 push_nested_namespace (tree ns)
3274 if (ns == global_namespace)
3275 push_to_top_level ();
3276 else
3278 push_nested_namespace (CP_DECL_CONTEXT (ns));
3279 push_namespace (DECL_NAME (ns));
3283 /* Pop back from the scope of the namespace NS, which was previously
3284 entered with push_nested_namespace. */
3286 void
3287 pop_nested_namespace (tree ns)
3289 timevar_push (TV_NAME_LOOKUP);
3290 while (ns != global_namespace)
3292 pop_namespace ();
3293 ns = CP_DECL_CONTEXT (ns);
3296 pop_from_top_level ();
3297 timevar_pop (TV_NAME_LOOKUP);
3300 /* Temporarily set the namespace for the current declaration. */
3302 void
3303 push_decl_namespace (tree decl)
3305 if (TREE_CODE (decl) != NAMESPACE_DECL)
3306 decl = decl_namespace_context (decl);
3307 decl_namespace_list = tree_cons (ORIGINAL_NAMESPACE (decl),
3308 NULL_TREE, decl_namespace_list);
3311 /* [namespace.memdef]/2 */
3313 void
3314 pop_decl_namespace (void)
3316 decl_namespace_list = TREE_CHAIN (decl_namespace_list);
3319 /* Return the namespace that is the common ancestor
3320 of two given namespaces. */
3322 static tree
3323 namespace_ancestor (tree ns1, tree ns2)
3325 timevar_push (TV_NAME_LOOKUP);
3326 if (is_ancestor (ns1, ns2))
3327 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, ns1);
3328 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP,
3329 namespace_ancestor (CP_DECL_CONTEXT (ns1), ns2));
3332 /* Process a namespace-alias declaration. */
3334 void
3335 do_namespace_alias (tree alias, tree name_space)
3337 if (name_space == error_mark_node)
3338 return;
3340 gcc_assert (TREE_CODE (name_space) == NAMESPACE_DECL);
3342 name_space = ORIGINAL_NAMESPACE (name_space);
3344 /* Build the alias. */
3345 alias = build_lang_decl (NAMESPACE_DECL, alias, void_type_node);
3346 DECL_NAMESPACE_ALIAS (alias) = name_space;
3347 DECL_EXTERNAL (alias) = 1;
3348 DECL_CONTEXT (alias) = FROB_CONTEXT (current_scope ());
3349 pushdecl (alias);
3351 /* Emit debug info for namespace alias. */
3352 if (!building_stmt_tree ())
3353 (*debug_hooks->global_decl) (alias);
3356 /* Like pushdecl, only it places X in the current namespace,
3357 if appropriate. */
3359 tree
3360 pushdecl_namespace_level (tree x, bool is_friend)
3362 struct cp_binding_level *b = current_binding_level;
3363 tree t;
3365 timevar_push (TV_NAME_LOOKUP);
3366 t = pushdecl_with_scope (x, NAMESPACE_LEVEL (current_namespace), is_friend);
3368 /* Now, the type_shadowed stack may screw us. Munge it so it does
3369 what we want. */
3370 if (TREE_CODE (t) == TYPE_DECL)
3372 tree name = DECL_NAME (t);
3373 tree newval;
3374 tree *ptr = (tree *)0;
3375 for (; !global_scope_p (b); b = b->level_chain)
3377 tree shadowed = b->type_shadowed;
3378 for (; shadowed; shadowed = TREE_CHAIN (shadowed))
3379 if (TREE_PURPOSE (shadowed) == name)
3381 ptr = &TREE_VALUE (shadowed);
3382 /* Can't break out of the loop here because sometimes
3383 a binding level will have duplicate bindings for
3384 PT names. It's gross, but I haven't time to fix it. */
3387 newval = TREE_TYPE (t);
3388 if (ptr == (tree *)0)
3390 /* @@ This shouldn't be needed. My test case "zstring.cc" trips
3391 up here if this is changed to an assertion. --KR */
3392 SET_IDENTIFIER_TYPE_VALUE (name, t);
3394 else
3396 *ptr = newval;
3399 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
3402 /* Insert USED into the using list of USER. Set INDIRECT_flag if this
3403 directive is not directly from the source. Also find the common
3404 ancestor and let our users know about the new namespace */
3405 static void
3406 add_using_namespace (tree user, tree used, bool indirect)
3408 tree t;
3409 timevar_push (TV_NAME_LOOKUP);
3410 /* Using oneself is a no-op. */
3411 if (user == used)
3413 timevar_pop (TV_NAME_LOOKUP);
3414 return;
3416 gcc_assert (TREE_CODE (user) == NAMESPACE_DECL);
3417 gcc_assert (TREE_CODE (used) == NAMESPACE_DECL);
3418 /* Check if we already have this. */
3419 t = purpose_member (used, DECL_NAMESPACE_USING (user));
3420 if (t != NULL_TREE)
3422 if (!indirect)
3423 /* Promote to direct usage. */
3424 TREE_INDIRECT_USING (t) = 0;
3425 timevar_pop (TV_NAME_LOOKUP);
3426 return;
3429 /* Add used to the user's using list. */
3430 DECL_NAMESPACE_USING (user)
3431 = tree_cons (used, namespace_ancestor (user, used),
3432 DECL_NAMESPACE_USING (user));
3434 TREE_INDIRECT_USING (DECL_NAMESPACE_USING (user)) = indirect;
3436 /* Add user to the used's users list. */
3437 DECL_NAMESPACE_USERS (used)
3438 = tree_cons (user, 0, DECL_NAMESPACE_USERS (used));
3440 /* Recursively add all namespaces used. */
3441 for (t = DECL_NAMESPACE_USING (used); t; t = TREE_CHAIN (t))
3442 /* indirect usage */
3443 add_using_namespace (user, TREE_PURPOSE (t), 1);
3445 /* Tell everyone using us about the new used namespaces. */
3446 for (t = DECL_NAMESPACE_USERS (user); t; t = TREE_CHAIN (t))
3447 add_using_namespace (TREE_PURPOSE (t), used, 1);
3448 timevar_pop (TV_NAME_LOOKUP);
3451 /* Process a using-declaration not appearing in class or local scope. */
3453 void
3454 do_toplevel_using_decl (tree decl, tree scope, tree name)
3456 tree oldval, oldtype, newval, newtype;
3457 tree orig_decl = decl;
3458 cxx_binding *binding;
3460 decl = validate_nonmember_using_decl (decl, scope, name);
3461 if (decl == NULL_TREE)
3462 return;
3464 binding = binding_for_name (NAMESPACE_LEVEL (current_namespace), name);
3466 oldval = binding->value;
3467 oldtype = binding->type;
3469 do_nonmember_using_decl (scope, name, oldval, oldtype, &newval, &newtype);
3471 /* Emit debug info. */
3472 if (!processing_template_decl)
3473 cp_emit_debug_info_for_using (orig_decl, current_namespace);
3475 /* Copy declarations found. */
3476 if (newval)
3477 binding->value = newval;
3478 if (newtype)
3479 binding->type = newtype;
3482 /* Process a using-directive. */
3484 void
3485 do_using_directive (tree name_space)
3487 tree context = NULL_TREE;
3489 if (name_space == error_mark_node)
3490 return;
3492 gcc_assert (TREE_CODE (name_space) == NAMESPACE_DECL);
3494 if (building_stmt_tree ())
3495 add_stmt (build_stmt (USING_STMT, name_space));
3496 name_space = ORIGINAL_NAMESPACE (name_space);
3498 if (!toplevel_bindings_p ())
3500 push_using_directive (name_space);
3502 else
3504 /* direct usage */
3505 add_using_namespace (current_namespace, name_space, 0);
3506 if (current_namespace != global_namespace)
3507 context = current_namespace;
3509 /* Emit debugging info. */
3510 if (!processing_template_decl)
3511 (*debug_hooks->imported_module_or_decl) (name_space, NULL_TREE,
3512 context, false);
3516 /* Deal with a using-directive seen by the parser. Currently we only
3517 handle attributes here, since they cannot appear inside a template. */
3519 void
3520 parse_using_directive (tree name_space, tree attribs)
3522 tree a;
3524 do_using_directive (name_space);
3526 for (a = attribs; a; a = TREE_CHAIN (a))
3528 tree name = TREE_PURPOSE (a);
3529 if (is_attribute_p ("strong", name))
3531 if (!toplevel_bindings_p ())
3532 error ("strong using only meaningful at namespace scope");
3533 else if (name_space != error_mark_node)
3535 if (!is_ancestor (current_namespace, name_space))
3536 error ("current namespace %qD does not enclose strongly used namespace %qD",
3537 current_namespace, name_space);
3538 DECL_NAMESPACE_ASSOCIATIONS (name_space)
3539 = tree_cons (current_namespace, 0,
3540 DECL_NAMESPACE_ASSOCIATIONS (name_space));
3543 else
3544 warning (OPT_Wattributes, "%qD attribute directive ignored", name);
3548 /* Like pushdecl, only it places X in the global scope if appropriate.
3549 Calls cp_finish_decl to register the variable, initializing it with
3550 *INIT, if INIT is non-NULL. */
3552 static tree
3553 pushdecl_top_level_1 (tree x, tree *init, bool is_friend)
3555 timevar_push (TV_NAME_LOOKUP);
3556 push_to_top_level ();
3557 x = pushdecl_namespace_level (x, is_friend);
3558 if (init)
3559 finish_decl (x, *init, NULL_TREE, NULL_TREE);
3560 pop_from_top_level ();
3561 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
3564 /* Like pushdecl, only it places X in the global scope if appropriate. */
3566 tree
3567 pushdecl_top_level (tree x)
3569 return pushdecl_top_level_1 (x, NULL, false);
3572 /* Like pushdecl_top_level, but adding the IS_FRIEND parameter. */
3574 tree
3575 pushdecl_top_level_maybe_friend (tree x, bool is_friend)
3577 return pushdecl_top_level_1 (x, NULL, is_friend);
3580 /* Like pushdecl, only it places X in the global scope if
3581 appropriate. Calls cp_finish_decl to register the variable,
3582 initializing it with INIT. */
3584 tree
3585 pushdecl_top_level_and_finish (tree x, tree init)
3587 return pushdecl_top_level_1 (x, &init, false);
3590 /* Combines two sets of overloaded functions into an OVERLOAD chain, removing
3591 duplicates. The first list becomes the tail of the result.
3593 The algorithm is O(n^2). We could get this down to O(n log n) by
3594 doing a sort on the addresses of the functions, if that becomes
3595 necessary. */
3597 static tree
3598 merge_functions (tree s1, tree s2)
3600 for (; s2; s2 = OVL_NEXT (s2))
3602 tree fn2 = OVL_CURRENT (s2);
3603 tree fns1;
3605 for (fns1 = s1; fns1; fns1 = OVL_NEXT (fns1))
3607 tree fn1 = OVL_CURRENT (fns1);
3609 /* If the function from S2 is already in S1, there is no
3610 need to add it again. For `extern "C"' functions, we
3611 might have two FUNCTION_DECLs for the same function, in
3612 different namespaces, but let's leave them in in case
3613 they have different default arguments. */
3614 if (fn1 == fn2)
3615 break;
3618 /* If we exhausted all of the functions in S1, FN2 is new. */
3619 if (!fns1)
3620 s1 = build_overload (fn2, s1);
3622 return s1;
3625 /* This should return an error not all definitions define functions.
3626 It is not an error if we find two functions with exactly the
3627 same signature, only if these are selected in overload resolution.
3628 old is the current set of bindings, new_binding the freshly-found binding.
3629 XXX Do we want to give *all* candidates in case of ambiguity?
3630 XXX In what way should I treat extern declarations?
3631 XXX I don't want to repeat the entire duplicate_decls here */
3633 static void
3634 ambiguous_decl (struct scope_binding *old, cxx_binding *new_binding, int flags)
3636 tree val, type;
3637 gcc_assert (old != NULL);
3639 /* Copy the type. */
3640 type = new_binding->type;
3641 if (LOOKUP_NAMESPACES_ONLY (flags)
3642 || (type && hidden_name_p (type) && !(flags & LOOKUP_HIDDEN)))
3643 type = NULL_TREE;
3645 /* Copy the value. */
3646 val = new_binding->value;
3647 if (val)
3649 if (hidden_name_p (val) && !(flags & LOOKUP_HIDDEN))
3650 val = NULL_TREE;
3651 else
3652 switch (TREE_CODE (val))
3654 case TEMPLATE_DECL:
3655 /* If we expect types or namespaces, and not templates,
3656 or this is not a template class. */
3657 if ((LOOKUP_QUALIFIERS_ONLY (flags)
3658 && !DECL_CLASS_TEMPLATE_P (val)))
3659 val = NULL_TREE;
3660 break;
3661 case TYPE_DECL:
3662 if (LOOKUP_NAMESPACES_ONLY (flags)
3663 || (type && (flags & LOOKUP_PREFER_TYPES)))
3664 val = NULL_TREE;
3665 break;
3666 case NAMESPACE_DECL:
3667 if (LOOKUP_TYPES_ONLY (flags))
3668 val = NULL_TREE;
3669 break;
3670 case FUNCTION_DECL:
3671 /* Ignore built-in functions that are still anticipated. */
3672 if (LOOKUP_QUALIFIERS_ONLY (flags))
3673 val = NULL_TREE;
3674 break;
3675 default:
3676 if (LOOKUP_QUALIFIERS_ONLY (flags))
3677 val = NULL_TREE;
3681 /* If val is hidden, shift down any class or enumeration name. */
3682 if (!val)
3684 val = type;
3685 type = NULL_TREE;
3688 if (!old->value)
3689 old->value = val;
3690 else if (val && val != old->value)
3692 if (is_overloaded_fn (old->value) && is_overloaded_fn (val))
3693 old->value = merge_functions (old->value, val);
3694 else
3696 old->value = tree_cons (NULL_TREE, old->value,
3697 build_tree_list (NULL_TREE, val));
3698 TREE_TYPE (old->value) = error_mark_node;
3702 if (!old->type)
3703 old->type = type;
3704 else if (type && old->type != type)
3706 old->type = tree_cons (NULL_TREE, old->type,
3707 build_tree_list (NULL_TREE, type));
3708 TREE_TYPE (old->type) = error_mark_node;
3712 /* Return the declarations that are members of the namespace NS. */
3714 tree
3715 cp_namespace_decls (tree ns)
3717 return NAMESPACE_LEVEL (ns)->names;
3720 /* Combine prefer_type and namespaces_only into flags. */
3722 static int
3723 lookup_flags (int prefer_type, int namespaces_only)
3725 if (namespaces_only)
3726 return LOOKUP_PREFER_NAMESPACES;
3727 if (prefer_type > 1)
3728 return LOOKUP_PREFER_TYPES;
3729 if (prefer_type > 0)
3730 return LOOKUP_PREFER_BOTH;
3731 return 0;
3734 /* Given a lookup that returned VAL, use FLAGS to decide if we want to
3735 ignore it or not. Subroutine of lookup_name_real and
3736 lookup_type_scope. */
3738 static bool
3739 qualify_lookup (tree val, int flags)
3741 if (val == NULL_TREE)
3742 return false;
3743 if ((flags & LOOKUP_PREFER_NAMESPACES) && TREE_CODE (val) == NAMESPACE_DECL)
3744 return true;
3745 if ((flags & LOOKUP_PREFER_TYPES)
3746 && (TREE_CODE (val) == TYPE_DECL || TREE_CODE (val) == TEMPLATE_DECL))
3747 return true;
3748 if (flags & (LOOKUP_PREFER_NAMESPACES | LOOKUP_PREFER_TYPES))
3749 return false;
3750 return true;
3753 /* Given a lookup that returned VAL, decide if we want to ignore it or
3754 not based on DECL_ANTICIPATED. */
3756 bool
3757 hidden_name_p (tree val)
3759 if (DECL_P (val)
3760 && DECL_LANG_SPECIFIC (val)
3761 && DECL_ANTICIPATED (val))
3762 return true;
3763 return false;
3766 /* Remove any hidden friend functions from a possibly overloaded set
3767 of functions. */
3769 tree
3770 remove_hidden_names (tree fns)
3772 if (!fns)
3773 return fns;
3775 if (TREE_CODE (fns) == FUNCTION_DECL && hidden_name_p (fns))
3776 fns = NULL_TREE;
3777 else if (TREE_CODE (fns) == OVERLOAD)
3779 tree o;
3781 for (o = fns; o; o = OVL_NEXT (o))
3782 if (hidden_name_p (OVL_CURRENT (o)))
3783 break;
3784 if (o)
3786 tree n = NULL_TREE;
3788 for (o = fns; o; o = OVL_NEXT (o))
3789 if (!hidden_name_p (OVL_CURRENT (o)))
3790 n = build_overload (OVL_CURRENT (o), n);
3791 fns = n;
3795 return fns;
3798 /* Unscoped lookup of a global: iterate over current namespaces,
3799 considering using-directives. */
3801 static tree
3802 unqualified_namespace_lookup (tree name, int flags)
3804 tree initial = current_decl_namespace ();
3805 tree scope = initial;
3806 tree siter;
3807 struct cp_binding_level *level;
3808 tree val = NULL_TREE;
3810 timevar_push (TV_NAME_LOOKUP);
3812 for (; !val; scope = CP_DECL_CONTEXT (scope))
3814 struct scope_binding binding = EMPTY_SCOPE_BINDING;
3815 cxx_binding *b =
3816 cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (scope), name);
3818 if (b)
3819 ambiguous_decl (&binding, b, flags);
3821 /* Add all _DECLs seen through local using-directives. */
3822 for (level = current_binding_level;
3823 level->kind != sk_namespace;
3824 level = level->level_chain)
3825 if (!lookup_using_namespace (name, &binding, level->using_directives,
3826 scope, flags))
3827 /* Give up because of error. */
3828 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
3830 /* Add all _DECLs seen through global using-directives. */
3831 /* XXX local and global using lists should work equally. */
3832 siter = initial;
3833 while (1)
3835 if (!lookup_using_namespace (name, &binding,
3836 DECL_NAMESPACE_USING (siter),
3837 scope, flags))
3838 /* Give up because of error. */
3839 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
3840 if (siter == scope) break;
3841 siter = CP_DECL_CONTEXT (siter);
3844 val = binding.value;
3845 if (scope == global_namespace)
3846 break;
3848 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
3851 /* Look up NAME (an IDENTIFIER_NODE) in SCOPE (either a NAMESPACE_DECL
3852 or a class TYPE). If IS_TYPE_P is TRUE, then ignore non-type
3853 bindings.
3855 Returns a DECL (or OVERLOAD, or BASELINK) representing the
3856 declaration found. If no suitable declaration can be found,
3857 ERROR_MARK_NODE is returned. If COMPLAIN is true and SCOPE is
3858 neither a class-type nor a namespace a diagnostic is issued. */
3860 tree
3861 lookup_qualified_name (tree scope, tree name, bool is_type_p, bool complain)
3863 int flags = 0;
3864 tree t = NULL_TREE;
3866 if (TREE_CODE (scope) == NAMESPACE_DECL)
3868 struct scope_binding binding = EMPTY_SCOPE_BINDING;
3870 flags |= LOOKUP_COMPLAIN;
3871 if (is_type_p)
3872 flags |= LOOKUP_PREFER_TYPES;
3873 if (qualified_lookup_using_namespace (name, scope, &binding, flags))
3874 t = binding.value;
3876 else if (cxx_dialect != cxx98 && TREE_CODE (scope) == ENUMERAL_TYPE)
3877 t = lookup_enumerator (scope, name);
3878 else if (is_class_type (scope, complain))
3879 t = lookup_member (scope, name, 2, is_type_p);
3881 if (!t)
3882 return error_mark_node;
3883 return t;
3886 /* Subroutine of unqualified_namespace_lookup:
3887 Add the bindings of NAME in used namespaces to VAL.
3888 We are currently looking for names in namespace SCOPE, so we
3889 look through USINGS for using-directives of namespaces
3890 which have SCOPE as a common ancestor with the current scope.
3891 Returns false on errors. */
3893 static bool
3894 lookup_using_namespace (tree name, struct scope_binding *val,
3895 tree usings, tree scope, int flags)
3897 tree iter;
3898 timevar_push (TV_NAME_LOOKUP);
3899 /* Iterate over all used namespaces in current, searching for using
3900 directives of scope. */
3901 for (iter = usings; iter; iter = TREE_CHAIN (iter))
3902 if (TREE_VALUE (iter) == scope)
3904 tree used = ORIGINAL_NAMESPACE (TREE_PURPOSE (iter));
3905 cxx_binding *val1 =
3906 cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (used), name);
3907 /* Resolve ambiguities. */
3908 if (val1)
3909 ambiguous_decl (val, val1, flags);
3911 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val->value != error_mark_node);
3914 /* [namespace.qual]
3915 Accepts the NAME to lookup and its qualifying SCOPE.
3916 Returns the name/type pair found into the cxx_binding *RESULT,
3917 or false on error. */
3919 static bool
3920 qualified_lookup_using_namespace (tree name, tree scope,
3921 struct scope_binding *result, int flags)
3923 /* Maintain a list of namespaces visited... */
3924 tree seen = NULL_TREE;
3925 /* ... and a list of namespace yet to see. */
3926 tree todo = NULL_TREE;
3927 tree todo_maybe = NULL_TREE;
3928 tree usings;
3929 timevar_push (TV_NAME_LOOKUP);
3930 /* Look through namespace aliases. */
3931 scope = ORIGINAL_NAMESPACE (scope);
3932 while (scope && result->value != error_mark_node)
3934 cxx_binding *binding =
3935 cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (scope), name);
3936 seen = tree_cons (scope, NULL_TREE, seen);
3937 if (binding)
3938 ambiguous_decl (result, binding, flags);
3940 /* Consider strong using directives always, and non-strong ones
3941 if we haven't found a binding yet. ??? Shouldn't we consider
3942 non-strong ones if the initial RESULT is non-NULL, but the
3943 binding in the given namespace is? */
3944 for (usings = DECL_NAMESPACE_USING (scope); usings;
3945 usings = TREE_CHAIN (usings))
3946 /* If this was a real directive, and we have not seen it. */
3947 if (!TREE_INDIRECT_USING (usings))
3949 /* Try to avoid queuing the same namespace more than once,
3950 the exception being when a namespace was already
3951 enqueued for todo_maybe and then a strong using is
3952 found for it. We could try to remove it from
3953 todo_maybe, but it's probably not worth the effort. */
3954 if (is_associated_namespace (scope, TREE_PURPOSE (usings))
3955 && !purpose_member (TREE_PURPOSE (usings), seen)
3956 && !purpose_member (TREE_PURPOSE (usings), todo))
3957 todo = tree_cons (TREE_PURPOSE (usings), NULL_TREE, todo);
3958 else if ((!result->value && !result->type)
3959 && !purpose_member (TREE_PURPOSE (usings), seen)
3960 && !purpose_member (TREE_PURPOSE (usings), todo)
3961 && !purpose_member (TREE_PURPOSE (usings), todo_maybe))
3962 todo_maybe = tree_cons (TREE_PURPOSE (usings), NULL_TREE,
3963 todo_maybe);
3965 if (todo)
3967 scope = TREE_PURPOSE (todo);
3968 todo = TREE_CHAIN (todo);
3970 else if (todo_maybe
3971 && (!result->value && !result->type))
3973 scope = TREE_PURPOSE (todo_maybe);
3974 todo = TREE_CHAIN (todo_maybe);
3975 todo_maybe = NULL_TREE;
3977 else
3978 scope = NULL_TREE; /* If there never was a todo list. */
3980 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, result->value != error_mark_node);
3983 /* Subroutine of outer_binding.
3984 Returns TRUE if BINDING is a binding to a template parameter of SCOPE,
3985 FALSE otherwise. */
3987 static bool
3988 binding_to_template_parms_of_scope_p (cxx_binding *binding,
3989 cxx_scope *scope)
3991 tree binding_value;
3993 if (!binding || !scope)
3994 return false;
3996 binding_value = binding->value ? binding->value : binding->type;
3998 return (scope
3999 && scope->this_entity
4000 && get_template_info (scope->this_entity)
4001 && parameter_of_template_p (binding_value,
4002 TI_TEMPLATE (get_template_info \
4003 (scope->this_entity))));
4006 /* Return the innermost non-namespace binding for NAME from a scope
4007 containing BINDING, or, if BINDING is NULL, the current scope.
4008 Please note that for a given template, the template parameters are
4009 considered to be in the scope containing the current scope.
4010 If CLASS_P is false, then class bindings are ignored. */
4012 cxx_binding *
4013 outer_binding (tree name,
4014 cxx_binding *binding,
4015 bool class_p)
4017 cxx_binding *outer;
4018 cxx_scope *scope;
4019 cxx_scope *outer_scope;
4021 if (binding)
4023 scope = binding->scope->level_chain;
4024 outer = binding->previous;
4026 else
4028 scope = current_binding_level;
4029 outer = IDENTIFIER_BINDING (name);
4031 outer_scope = outer ? outer->scope : NULL;
4033 /* Because we create class bindings lazily, we might be missing a
4034 class binding for NAME. If there are any class binding levels
4035 between the LAST_BINDING_LEVEL and the scope in which OUTER was
4036 declared, we must lookup NAME in those class scopes. */
4037 if (class_p)
4038 while (scope && scope != outer_scope && scope->kind != sk_namespace)
4040 if (scope->kind == sk_class)
4042 cxx_binding *class_binding;
4044 class_binding = get_class_binding (name, scope);
4045 if (class_binding)
4047 /* Thread this new class-scope binding onto the
4048 IDENTIFIER_BINDING list so that future lookups
4049 find it quickly. */
4050 class_binding->previous = outer;
4051 if (binding)
4052 binding->previous = class_binding;
4053 else
4054 IDENTIFIER_BINDING (name) = class_binding;
4055 return class_binding;
4058 /* If we are in a member template, the template parms of the member
4059 template are considered to be inside the scope of the containing
4060 class, but within G++ the class bindings are all pushed between the
4061 template parms and the function body. So if the outer binding is
4062 a template parm for the current scope, return it now rather than
4063 look for a class binding. */
4064 if (outer_scope && outer_scope->kind == sk_template_parms
4065 && binding_to_template_parms_of_scope_p (outer, scope))
4066 return outer;
4068 scope = scope->level_chain;
4071 return outer;
4074 /* Return the innermost block-scope or class-scope value binding for
4075 NAME, or NULL_TREE if there is no such binding. */
4077 tree
4078 innermost_non_namespace_value (tree name)
4080 cxx_binding *binding;
4081 binding = outer_binding (name, /*binding=*/NULL, /*class_p=*/true);
4082 return binding ? binding->value : NULL_TREE;
4085 /* Look up NAME in the current binding level and its superiors in the
4086 namespace of variables, functions and typedefs. Return a ..._DECL
4087 node of some kind representing its definition if there is only one
4088 such declaration, or return a TREE_LIST with all the overloaded
4089 definitions if there are many, or return 0 if it is undefined.
4090 Hidden name, either friend declaration or built-in function, are
4091 not ignored.
4093 If PREFER_TYPE is > 0, we prefer TYPE_DECLs or namespaces.
4094 If PREFER_TYPE is > 1, we reject non-type decls (e.g. namespaces).
4095 Otherwise we prefer non-TYPE_DECLs.
4097 If NONCLASS is nonzero, bindings in class scopes are ignored. If
4098 BLOCK_P is false, bindings in block scopes are ignored. */
4100 tree
4101 lookup_name_real (tree name, int prefer_type, int nonclass, bool block_p,
4102 int namespaces_only, int flags)
4104 cxx_binding *iter;
4105 tree val = NULL_TREE;
4107 timevar_push (TV_NAME_LOOKUP);
4108 /* Conversion operators are handled specially because ordinary
4109 unqualified name lookup will not find template conversion
4110 operators. */
4111 if (IDENTIFIER_TYPENAME_P (name))
4113 struct cp_binding_level *level;
4115 for (level = current_binding_level;
4116 level && level->kind != sk_namespace;
4117 level = level->level_chain)
4119 tree class_type;
4120 tree operators;
4122 /* A conversion operator can only be declared in a class
4123 scope. */
4124 if (level->kind != sk_class)
4125 continue;
4127 /* Lookup the conversion operator in the class. */
4128 class_type = level->this_entity;
4129 operators = lookup_fnfields (class_type, name, /*protect=*/0);
4130 if (operators)
4131 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, operators);
4134 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
4137 flags |= lookup_flags (prefer_type, namespaces_only);
4139 /* First, look in non-namespace scopes. */
4141 if (current_class_type == NULL_TREE)
4142 nonclass = 1;
4144 if (block_p || !nonclass)
4145 for (iter = outer_binding (name, NULL, !nonclass);
4146 iter;
4147 iter = outer_binding (name, iter, !nonclass))
4149 tree binding;
4151 /* Skip entities we don't want. */
4152 if (LOCAL_BINDING_P (iter) ? !block_p : nonclass)
4153 continue;
4155 /* If this is the kind of thing we're looking for, we're done. */
4156 if (qualify_lookup (iter->value, flags))
4157 binding = iter->value;
4158 else if ((flags & LOOKUP_PREFER_TYPES)
4159 && qualify_lookup (iter->type, flags))
4160 binding = iter->type;
4161 else
4162 binding = NULL_TREE;
4164 if (binding)
4166 if (hidden_name_p (binding))
4168 /* A non namespace-scope binding can only be hidden in the
4169 presence of a local class, due to friend declarations.
4171 In particular, consider:
4173 struct C;
4174 void f() {
4175 struct A {
4176 friend struct B;
4177 friend struct C;
4178 void g() {
4179 B* b; // error: B is hidden
4180 C* c; // OK, finds ::C
4183 B *b; // error: B is hidden
4184 C *c; // OK, finds ::C
4185 struct B {};
4186 B *bb; // OK
4189 The standard says that "B" is a local class in "f"
4190 (but not nested within "A") -- but that name lookup
4191 for "B" does not find this declaration until it is
4192 declared directly with "f".
4194 In particular:
4196 [class.friend]
4198 If a friend declaration appears in a local class and
4199 the name specified is an unqualified name, a prior
4200 declaration is looked up without considering scopes
4201 that are outside the innermost enclosing non-class
4202 scope. For a friend function declaration, if there is
4203 no prior declaration, the program is ill-formed. For a
4204 friend class declaration, if there is no prior
4205 declaration, the class that is specified belongs to the
4206 innermost enclosing non-class scope, but if it is
4207 subsequently referenced, its name is not found by name
4208 lookup until a matching declaration is provided in the
4209 innermost enclosing nonclass scope.
4211 So just keep looking for a non-hidden binding.
4213 gcc_assert (TREE_CODE (binding) == TYPE_DECL);
4214 continue;
4216 val = binding;
4217 break;
4221 /* Now lookup in namespace scopes. */
4222 if (!val)
4223 val = unqualified_namespace_lookup (name, flags);
4225 /* If we have a single function from a using decl, pull it out. */
4226 if (val && TREE_CODE (val) == OVERLOAD && !really_overloaded_fn (val))
4227 val = OVL_FUNCTION (val);
4229 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
4232 tree
4233 lookup_name_nonclass (tree name)
4235 return lookup_name_real (name, 0, 1, /*block_p=*/true, 0, LOOKUP_COMPLAIN);
4238 tree
4239 lookup_function_nonclass (tree name, tree args, bool block_p)
4241 return
4242 lookup_arg_dependent (name,
4243 lookup_name_real (name, 0, 1, block_p, 0,
4244 LOOKUP_COMPLAIN),
4245 args);
4248 tree
4249 lookup_name (tree name)
4251 return lookup_name_real (name, 0, 0, /*block_p=*/true, 0, LOOKUP_COMPLAIN);
4254 tree
4255 lookup_name_prefer_type (tree name, int prefer_type)
4257 return lookup_name_real (name, prefer_type, 0, /*block_p=*/true,
4258 0, LOOKUP_COMPLAIN);
4261 /* Look up NAME for type used in elaborated name specifier in
4262 the scopes given by SCOPE. SCOPE can be either TS_CURRENT or
4263 TS_WITHIN_ENCLOSING_NON_CLASS. Although not implied by the
4264 name, more scopes are checked if cleanup or template parameter
4265 scope is encountered.
4267 Unlike lookup_name_real, we make sure that NAME is actually
4268 declared in the desired scope, not from inheritance, nor using
4269 directive. For using declaration, there is DR138 still waiting
4270 to be resolved. Hidden name coming from an earlier friend
4271 declaration is also returned.
4273 A TYPE_DECL best matching the NAME is returned. Catching error
4274 and issuing diagnostics are caller's responsibility. */
4276 tree
4277 lookup_type_scope (tree name, tag_scope scope)
4279 cxx_binding *iter = NULL;
4280 tree val = NULL_TREE;
4282 timevar_push (TV_NAME_LOOKUP);
4284 /* Look in non-namespace scope first. */
4285 if (current_binding_level->kind != sk_namespace)
4286 iter = outer_binding (name, NULL, /*class_p=*/ true);
4287 for (; iter; iter = outer_binding (name, iter, /*class_p=*/ true))
4289 /* Check if this is the kind of thing we're looking for.
4290 If SCOPE is TS_CURRENT, also make sure it doesn't come from
4291 base class. For ITER->VALUE, we can simply use
4292 INHERITED_VALUE_BINDING_P. For ITER->TYPE, we have to use
4293 our own check.
4295 We check ITER->TYPE before ITER->VALUE in order to handle
4296 typedef struct C {} C;
4297 correctly. */
4299 if (qualify_lookup (iter->type, LOOKUP_PREFER_TYPES)
4300 && (scope != ts_current
4301 || LOCAL_BINDING_P (iter)
4302 || DECL_CONTEXT (iter->type) == iter->scope->this_entity))
4303 val = iter->type;
4304 else if ((scope != ts_current
4305 || !INHERITED_VALUE_BINDING_P (iter))
4306 && qualify_lookup (iter->value, LOOKUP_PREFER_TYPES))
4307 val = iter->value;
4309 if (val)
4310 break;
4313 /* Look in namespace scope. */
4314 if (!val)
4316 iter = cxx_scope_find_binding_for_name
4317 (NAMESPACE_LEVEL (current_decl_namespace ()), name);
4319 if (iter)
4321 /* If this is the kind of thing we're looking for, we're done. */
4322 if (qualify_lookup (iter->type, LOOKUP_PREFER_TYPES))
4323 val = iter->type;
4324 else if (qualify_lookup (iter->value, LOOKUP_PREFER_TYPES))
4325 val = iter->value;
4330 /* Type found, check if it is in the allowed scopes, ignoring cleanup
4331 and template parameter scopes. */
4332 if (val)
4334 struct cp_binding_level *b = current_binding_level;
4335 while (b)
4337 if (iter->scope == b)
4338 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
4340 if (b->kind == sk_cleanup || b->kind == sk_template_parms
4341 || b->kind == sk_function_parms)
4342 b = b->level_chain;
4343 else if (b->kind == sk_class
4344 && scope == ts_within_enclosing_non_class)
4345 b = b->level_chain;
4346 else
4347 break;
4351 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
4354 /* Similar to `lookup_name' but look only in the innermost non-class
4355 binding level. */
4357 tree
4358 lookup_name_innermost_nonclass_level (tree name)
4360 struct cp_binding_level *b;
4361 tree t = NULL_TREE;
4363 timevar_push (TV_NAME_LOOKUP);
4364 b = innermost_nonclass_level ();
4366 if (b->kind == sk_namespace)
4368 t = IDENTIFIER_NAMESPACE_VALUE (name);
4370 /* extern "C" function() */
4371 if (t != NULL_TREE && TREE_CODE (t) == TREE_LIST)
4372 t = TREE_VALUE (t);
4374 else if (IDENTIFIER_BINDING (name)
4375 && LOCAL_BINDING_P (IDENTIFIER_BINDING (name)))
4377 cxx_binding *binding;
4378 binding = IDENTIFIER_BINDING (name);
4379 while (1)
4381 if (binding->scope == b
4382 && !(TREE_CODE (binding->value) == VAR_DECL
4383 && DECL_DEAD_FOR_LOCAL (binding->value)))
4384 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, binding->value);
4386 if (b->kind == sk_cleanup)
4387 b = b->level_chain;
4388 else
4389 break;
4393 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
4396 /* Like lookup_name_innermost_nonclass_level, but for types. */
4398 static tree
4399 lookup_type_current_level (tree name)
4401 tree t = NULL_TREE;
4403 timevar_push (TV_NAME_LOOKUP);
4404 gcc_assert (current_binding_level->kind != sk_namespace);
4406 if (REAL_IDENTIFIER_TYPE_VALUE (name) != NULL_TREE
4407 && REAL_IDENTIFIER_TYPE_VALUE (name) != global_type_node)
4409 struct cp_binding_level *b = current_binding_level;
4410 while (1)
4412 if (purpose_member (name, b->type_shadowed))
4413 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP,
4414 REAL_IDENTIFIER_TYPE_VALUE (name));
4415 if (b->kind == sk_cleanup)
4416 b = b->level_chain;
4417 else
4418 break;
4422 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
4425 /* [basic.lookup.koenig] */
4426 /* A nonzero return value in the functions below indicates an error. */
4428 struct arg_lookup
4430 tree name;
4431 tree args;
4432 tree namespaces;
4433 tree classes;
4434 tree functions;
4437 static bool arg_assoc (struct arg_lookup*, tree);
4438 static bool arg_assoc_args (struct arg_lookup*, tree);
4439 static bool arg_assoc_type (struct arg_lookup*, tree);
4440 static bool add_function (struct arg_lookup *, tree);
4441 static bool arg_assoc_namespace (struct arg_lookup *, tree);
4442 static bool arg_assoc_class (struct arg_lookup *, tree);
4443 static bool arg_assoc_template_arg (struct arg_lookup*, tree);
4445 /* Add a function to the lookup structure.
4446 Returns true on error. */
4448 static bool
4449 add_function (struct arg_lookup *k, tree fn)
4451 /* We used to check here to see if the function was already in the list,
4452 but that's O(n^2), which is just too expensive for function lookup.
4453 Now we deal with the occasional duplicate in joust. In doing this, we
4454 assume that the number of duplicates will be small compared to the
4455 total number of functions being compared, which should usually be the
4456 case. */
4458 /* We must find only functions, or exactly one non-function. */
4459 if (!k->functions)
4460 k->functions = fn;
4461 else if (fn == k->functions)
4463 else if (is_overloaded_fn (k->functions) && is_overloaded_fn (fn))
4464 k->functions = build_overload (fn, k->functions);
4465 else
4467 tree f1 = OVL_CURRENT (k->functions);
4468 tree f2 = fn;
4469 if (is_overloaded_fn (f1))
4471 fn = f1; f1 = f2; f2 = fn;
4473 error ("%q+D is not a function,", f1);
4474 error (" conflict with %q+D", f2);
4475 error (" in call to %qD", k->name);
4476 return true;
4479 return false;
4482 /* Returns true iff CURRENT has declared itself to be an associated
4483 namespace of SCOPE via a strong using-directive (or transitive chain
4484 thereof). Both are namespaces. */
4486 bool
4487 is_associated_namespace (tree current, tree scope)
4489 tree seen = NULL_TREE;
4490 tree todo = NULL_TREE;
4491 tree t;
4492 while (1)
4494 if (scope == current)
4495 return true;
4496 seen = tree_cons (scope, NULL_TREE, seen);
4497 for (t = DECL_NAMESPACE_ASSOCIATIONS (scope); t; t = TREE_CHAIN (t))
4498 if (!purpose_member (TREE_PURPOSE (t), seen))
4499 todo = tree_cons (TREE_PURPOSE (t), NULL_TREE, todo);
4500 if (todo)
4502 scope = TREE_PURPOSE (todo);
4503 todo = TREE_CHAIN (todo);
4505 else
4506 return false;
4510 /* Return whether FN is a friend of an associated class of ARG. */
4512 static bool
4513 friend_of_associated_class_p (tree arg, tree fn)
4515 tree type;
4517 if (TYPE_P (arg))
4518 type = arg;
4519 else if (type_unknown_p (arg))
4520 return false;
4521 else
4522 type = TREE_TYPE (arg);
4524 /* If TYPE is a class, the class itself and all base classes are
4525 associated classes. */
4526 if (CLASS_TYPE_P (type))
4528 if (is_friend (type, fn))
4529 return true;
4531 if (TYPE_BINFO (type))
4533 tree binfo, base_binfo;
4534 int i;
4536 for (binfo = TYPE_BINFO (type), i = 0;
4537 BINFO_BASE_ITERATE (binfo, i, base_binfo);
4538 i++)
4539 if (is_friend (BINFO_TYPE (base_binfo), fn))
4540 return true;
4544 /* If TYPE is a class member, the class of which it is a member is
4545 an associated class. */
4546 if ((CLASS_TYPE_P (type)
4547 || TREE_CODE (type) == UNION_TYPE
4548 || TREE_CODE (type) == ENUMERAL_TYPE)
4549 && TYPE_CONTEXT (type)
4550 && CLASS_TYPE_P (TYPE_CONTEXT (type))
4551 && is_friend (TYPE_CONTEXT (type), fn))
4552 return true;
4554 return false;
4557 /* Add functions of a namespace to the lookup structure.
4558 Returns true on error. */
4560 static bool
4561 arg_assoc_namespace (struct arg_lookup *k, tree scope)
4563 tree value;
4565 if (purpose_member (scope, k->namespaces))
4566 return 0;
4567 k->namespaces = tree_cons (scope, NULL_TREE, k->namespaces);
4569 /* Check out our super-users. */
4570 for (value = DECL_NAMESPACE_ASSOCIATIONS (scope); value;
4571 value = TREE_CHAIN (value))
4572 if (arg_assoc_namespace (k, TREE_PURPOSE (value)))
4573 return true;
4575 /* Also look down into inline namespaces. */
4576 for (value = DECL_NAMESPACE_USING (scope); value;
4577 value = TREE_CHAIN (value))
4578 if (is_associated_namespace (scope, TREE_PURPOSE (value)))
4579 if (arg_assoc_namespace (k, TREE_PURPOSE (value)))
4580 return true;
4582 value = namespace_binding (k->name, scope);
4583 if (!value)
4584 return false;
4586 for (; value; value = OVL_NEXT (value))
4588 /* We don't want to find arbitrary hidden functions via argument
4589 dependent lookup. We only want to find friends of associated
4590 classes. */
4591 if (hidden_name_p (OVL_CURRENT (value)))
4593 tree args;
4595 for (args = k->args; args; args = TREE_CHAIN (args))
4596 if (friend_of_associated_class_p (TREE_VALUE (args),
4597 OVL_CURRENT (value)))
4598 break;
4599 if (!args)
4600 continue;
4603 if (add_function (k, OVL_CURRENT (value)))
4604 return true;
4607 return false;
4610 /* Adds everything associated with a template argument to the lookup
4611 structure. Returns true on error. */
4613 static bool
4614 arg_assoc_template_arg (struct arg_lookup *k, tree arg)
4616 /* [basic.lookup.koenig]
4618 If T is a template-id, its associated namespaces and classes are
4619 ... the namespaces and classes associated with the types of the
4620 template arguments provided for template type parameters
4621 (excluding template template parameters); the namespaces in which
4622 any template template arguments are defined; and the classes in
4623 which any member templates used as template template arguments
4624 are defined. [Note: non-type template arguments do not
4625 contribute to the set of associated namespaces. ] */
4627 /* Consider first template template arguments. */
4628 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
4629 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
4630 return false;
4631 else if (TREE_CODE (arg) == TEMPLATE_DECL)
4633 tree ctx = CP_DECL_CONTEXT (arg);
4635 /* It's not a member template. */
4636 if (TREE_CODE (ctx) == NAMESPACE_DECL)
4637 return arg_assoc_namespace (k, ctx);
4638 /* Otherwise, it must be member template. */
4639 else
4640 return arg_assoc_class (k, ctx);
4642 /* It's an argument pack; handle it recursively. */
4643 else if (ARGUMENT_PACK_P (arg))
4645 tree args = ARGUMENT_PACK_ARGS (arg);
4646 int i, len = TREE_VEC_LENGTH (args);
4647 for (i = 0; i < len; ++i)
4648 if (arg_assoc_template_arg (k, TREE_VEC_ELT (args, i)))
4649 return true;
4651 return false;
4653 /* It's not a template template argument, but it is a type template
4654 argument. */
4655 else if (TYPE_P (arg))
4656 return arg_assoc_type (k, arg);
4657 /* It's a non-type template argument. */
4658 else
4659 return false;
4662 /* Adds everything associated with class to the lookup structure.
4663 Returns true on error. */
4665 static bool
4666 arg_assoc_class (struct arg_lookup *k, tree type)
4668 tree list, friends, context;
4669 int i;
4671 /* Backend build structures, such as __builtin_va_list, aren't
4672 affected by all this. */
4673 if (!CLASS_TYPE_P (type))
4674 return false;
4676 if (purpose_member (type, k->classes))
4677 return false;
4678 k->classes = tree_cons (type, NULL_TREE, k->classes);
4680 context = decl_namespace_context (type);
4681 if (arg_assoc_namespace (k, context))
4682 return true;
4684 if (TYPE_BINFO (type))
4686 /* Process baseclasses. */
4687 tree binfo, base_binfo;
4689 for (binfo = TYPE_BINFO (type), i = 0;
4690 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
4691 if (arg_assoc_class (k, BINFO_TYPE (base_binfo)))
4692 return true;
4695 /* Process friends. */
4696 for (list = DECL_FRIENDLIST (TYPE_MAIN_DECL (type)); list;
4697 list = TREE_CHAIN (list))
4698 if (k->name == FRIEND_NAME (list))
4699 for (friends = FRIEND_DECLS (list); friends;
4700 friends = TREE_CHAIN (friends))
4702 tree fn = TREE_VALUE (friends);
4704 /* Only interested in global functions with potentially hidden
4705 (i.e. unqualified) declarations. */
4706 if (CP_DECL_CONTEXT (fn) != context)
4707 continue;
4708 /* Template specializations are never found by name lookup.
4709 (Templates themselves can be found, but not template
4710 specializations.) */
4711 if (TREE_CODE (fn) == FUNCTION_DECL && DECL_USE_TEMPLATE (fn))
4712 continue;
4713 if (add_function (k, fn))
4714 return true;
4717 /* Process template arguments. */
4718 if (CLASSTYPE_TEMPLATE_INFO (type)
4719 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
4721 list = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
4722 for (i = 0; i < TREE_VEC_LENGTH (list); ++i)
4723 arg_assoc_template_arg (k, TREE_VEC_ELT (list, i));
4726 return false;
4729 /* Adds everything associated with a given type.
4730 Returns 1 on error. */
4732 static bool
4733 arg_assoc_type (struct arg_lookup *k, tree type)
4735 /* As we do not get the type of non-type dependent expressions
4736 right, we can end up with such things without a type. */
4737 if (!type)
4738 return false;
4740 if (TYPE_PTRMEM_P (type))
4742 /* Pointer to member: associate class type and value type. */
4743 if (arg_assoc_type (k, TYPE_PTRMEM_CLASS_TYPE (type)))
4744 return true;
4745 return arg_assoc_type (k, TYPE_PTRMEM_POINTED_TO_TYPE (type));
4747 else switch (TREE_CODE (type))
4749 case ERROR_MARK:
4750 return false;
4751 case VOID_TYPE:
4752 case INTEGER_TYPE:
4753 case REAL_TYPE:
4754 case COMPLEX_TYPE:
4755 case VECTOR_TYPE:
4756 case BOOLEAN_TYPE:
4757 case FIXED_POINT_TYPE:
4758 case DECLTYPE_TYPE:
4759 return false;
4760 case RECORD_TYPE:
4761 if (TYPE_PTRMEMFUNC_P (type))
4762 return arg_assoc_type (k, TYPE_PTRMEMFUNC_FN_TYPE (type));
4763 return arg_assoc_class (k, type);
4764 case POINTER_TYPE:
4765 case REFERENCE_TYPE:
4766 case ARRAY_TYPE:
4767 return arg_assoc_type (k, TREE_TYPE (type));
4768 case UNION_TYPE:
4769 case ENUMERAL_TYPE:
4770 return arg_assoc_namespace (k, decl_namespace_context (type));
4771 case METHOD_TYPE:
4772 /* The basetype is referenced in the first arg type, so just
4773 fall through. */
4774 case FUNCTION_TYPE:
4775 /* Associate the parameter types. */
4776 if (arg_assoc_args (k, TYPE_ARG_TYPES (type)))
4777 return true;
4778 /* Associate the return type. */
4779 return arg_assoc_type (k, TREE_TYPE (type));
4780 case TEMPLATE_TYPE_PARM:
4781 case BOUND_TEMPLATE_TEMPLATE_PARM:
4782 return false;
4783 case TYPENAME_TYPE:
4784 return false;
4785 case LANG_TYPE:
4786 gcc_assert (type == unknown_type_node
4787 || type == init_list_type_node);
4788 return false;
4789 case TYPE_PACK_EXPANSION:
4790 return arg_assoc_type (k, PACK_EXPANSION_PATTERN (type));
4792 default:
4793 gcc_unreachable ();
4795 return false;
4798 /* Adds everything associated with arguments. Returns true on error. */
4800 static bool
4801 arg_assoc_args (struct arg_lookup *k, tree args)
4803 for (; args; args = TREE_CHAIN (args))
4804 if (arg_assoc (k, TREE_VALUE (args)))
4805 return true;
4806 return false;
4809 /* Adds everything associated with a given tree_node. Returns 1 on error. */
4811 static bool
4812 arg_assoc (struct arg_lookup *k, tree n)
4814 if (n == error_mark_node)
4815 return false;
4817 if (TYPE_P (n))
4818 return arg_assoc_type (k, n);
4820 if (! type_unknown_p (n))
4821 return arg_assoc_type (k, TREE_TYPE (n));
4823 if (TREE_CODE (n) == ADDR_EXPR)
4824 n = TREE_OPERAND (n, 0);
4825 if (TREE_CODE (n) == COMPONENT_REF)
4826 n = TREE_OPERAND (n, 1);
4827 if (TREE_CODE (n) == OFFSET_REF)
4828 n = TREE_OPERAND (n, 1);
4829 while (TREE_CODE (n) == TREE_LIST)
4830 n = TREE_VALUE (n);
4831 if (TREE_CODE (n) == BASELINK)
4832 n = BASELINK_FUNCTIONS (n);
4834 if (TREE_CODE (n) == FUNCTION_DECL)
4835 return arg_assoc_type (k, TREE_TYPE (n));
4836 if (TREE_CODE (n) == TEMPLATE_ID_EXPR)
4838 /* [basic.lookup.koenig]
4840 If T is a template-id, its associated namespaces and classes
4841 are the namespace in which the template is defined; for
4842 member templates, the member template's class... */
4843 tree templ = TREE_OPERAND (n, 0);
4844 tree args = TREE_OPERAND (n, 1);
4845 tree ctx;
4846 int ix;
4848 if (TREE_CODE (templ) == COMPONENT_REF)
4849 templ = TREE_OPERAND (templ, 1);
4851 /* First, the template. There may actually be more than one if
4852 this is an overloaded function template. But, in that case,
4853 we only need the first; all the functions will be in the same
4854 namespace. */
4855 templ = OVL_CURRENT (templ);
4857 ctx = CP_DECL_CONTEXT (templ);
4859 if (TREE_CODE (ctx) == NAMESPACE_DECL)
4861 if (arg_assoc_namespace (k, ctx) == 1)
4862 return true;
4864 /* It must be a member template. */
4865 else if (arg_assoc_class (k, ctx) == 1)
4866 return true;
4868 /* Now the arguments. */
4869 if (args)
4870 for (ix = TREE_VEC_LENGTH (args); ix--;)
4871 if (arg_assoc_template_arg (k, TREE_VEC_ELT (args, ix)) == 1)
4872 return true;
4874 else if (TREE_CODE (n) == OVERLOAD)
4876 for (; n; n = OVL_CHAIN (n))
4877 if (arg_assoc_type (k, TREE_TYPE (OVL_FUNCTION (n))))
4878 return true;
4881 return false;
4884 /* Performs Koenig lookup depending on arguments, where fns
4885 are the functions found in normal lookup. */
4887 tree
4888 lookup_arg_dependent (tree name, tree fns, tree args)
4890 struct arg_lookup k;
4892 timevar_push (TV_NAME_LOOKUP);
4894 /* Remove any hidden friend functions from the list of functions
4895 found so far. They will be added back by arg_assoc_class as
4896 appropriate. */
4897 fns = remove_hidden_names (fns);
4899 k.name = name;
4900 k.args = args;
4901 k.functions = fns;
4902 k.classes = NULL_TREE;
4904 /* We previously performed an optimization here by setting
4905 NAMESPACES to the current namespace when it was safe. However, DR
4906 164 says that namespaces that were already searched in the first
4907 stage of template processing are searched again (potentially
4908 picking up later definitions) in the second stage. */
4909 k.namespaces = NULL_TREE;
4911 arg_assoc_args (&k, args);
4913 fns = k.functions;
4915 if (fns
4916 && TREE_CODE (fns) != VAR_DECL
4917 && !is_overloaded_fn (fns))
4919 error ("argument dependent lookup finds %q+D", fns);
4920 error (" in call to %qD", name);
4921 fns = error_mark_node;
4924 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, fns);
4927 /* Add namespace to using_directives. Return NULL_TREE if nothing was
4928 changed (i.e. there was already a directive), or the fresh
4929 TREE_LIST otherwise. */
4931 static tree
4932 push_using_directive (tree used)
4934 tree ud = current_binding_level->using_directives;
4935 tree iter, ancestor;
4937 timevar_push (TV_NAME_LOOKUP);
4938 /* Check if we already have this. */
4939 if (purpose_member (used, ud) != NULL_TREE)
4940 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
4942 ancestor = namespace_ancestor (current_decl_namespace (), used);
4943 ud = current_binding_level->using_directives;
4944 ud = tree_cons (used, ancestor, ud);
4945 current_binding_level->using_directives = ud;
4947 /* Recursively add all namespaces used. */
4948 for (iter = DECL_NAMESPACE_USING (used); iter; iter = TREE_CHAIN (iter))
4949 push_using_directive (TREE_PURPOSE (iter));
4951 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, ud);
4954 /* The type TYPE is being declared. If it is a class template, or a
4955 specialization of a class template, do any processing required and
4956 perform error-checking. If IS_FRIEND is nonzero, this TYPE is
4957 being declared a friend. B is the binding level at which this TYPE
4958 should be bound.
4960 Returns the TYPE_DECL for TYPE, which may have been altered by this
4961 processing. */
4963 static tree
4964 maybe_process_template_type_declaration (tree type, int is_friend,
4965 cxx_scope *b)
4967 tree decl = TYPE_NAME (type);
4969 if (processing_template_parmlist)
4970 /* You can't declare a new template type in a template parameter
4971 list. But, you can declare a non-template type:
4973 template <class A*> struct S;
4975 is a forward-declaration of `A'. */
4977 else if (b->kind == sk_namespace
4978 && current_binding_level->kind != sk_namespace)
4979 /* If this new type is being injected into a containing scope,
4980 then it's not a template type. */
4982 else
4984 gcc_assert (MAYBE_CLASS_TYPE_P (type)
4985 || TREE_CODE (type) == ENUMERAL_TYPE);
4987 if (processing_template_decl)
4989 /* This may change after the call to
4990 push_template_decl_real, but we want the original value. */
4991 tree name = DECL_NAME (decl);
4993 decl = push_template_decl_real (decl, is_friend);
4994 if (decl == error_mark_node)
4995 return error_mark_node;
4997 /* If the current binding level is the binding level for the
4998 template parameters (see the comment in
4999 begin_template_parm_list) and the enclosing level is a class
5000 scope, and we're not looking at a friend, push the
5001 declaration of the member class into the class scope. In the
5002 friend case, push_template_decl will already have put the
5003 friend into global scope, if appropriate. */
5004 if (TREE_CODE (type) != ENUMERAL_TYPE
5005 && !is_friend && b->kind == sk_template_parms
5006 && b->level_chain->kind == sk_class)
5008 finish_member_declaration (CLASSTYPE_TI_TEMPLATE (type));
5010 if (!COMPLETE_TYPE_P (current_class_type))
5012 maybe_add_class_template_decl_list (current_class_type,
5013 type, /*friend_p=*/0);
5014 /* Put this UTD in the table of UTDs for the class. */
5015 if (CLASSTYPE_NESTED_UTDS (current_class_type) == NULL)
5016 CLASSTYPE_NESTED_UTDS (current_class_type) =
5017 binding_table_new (SCOPE_DEFAULT_HT_SIZE);
5019 binding_table_insert
5020 (CLASSTYPE_NESTED_UTDS (current_class_type), name, type);
5026 return decl;
5029 /* Push a tag name NAME for struct/class/union/enum type TYPE. In case
5030 that the NAME is a class template, the tag is processed but not pushed.
5032 The pushed scope depend on the SCOPE parameter:
5033 - When SCOPE is TS_CURRENT, put it into the inner-most non-sk_cleanup
5034 scope.
5035 - When SCOPE is TS_GLOBAL, put it in the inner-most non-class and
5036 non-template-parameter scope. This case is needed for forward
5037 declarations.
5038 - When SCOPE is TS_WITHIN_ENCLOSING_NON_CLASS, this is similar to
5039 TS_GLOBAL case except that names within template-parameter scopes
5040 are not pushed at all.
5042 Returns TYPE upon success and ERROR_MARK_NODE otherwise. */
5044 tree
5045 pushtag (tree name, tree type, tag_scope scope)
5047 struct cp_binding_level *b;
5048 tree decl;
5050 timevar_push (TV_NAME_LOOKUP);
5051 b = current_binding_level;
5052 while (/* Cleanup scopes are not scopes from the point of view of
5053 the language. */
5054 b->kind == sk_cleanup
5055 /* Neither are function parameter scopes. */
5056 || b->kind == sk_function_parms
5057 /* Neither are the scopes used to hold template parameters
5058 for an explicit specialization. For an ordinary template
5059 declaration, these scopes are not scopes from the point of
5060 view of the language. */
5061 || (b->kind == sk_template_parms
5062 && (b->explicit_spec_p || scope == ts_global))
5063 || (b->kind == sk_class
5064 && (scope != ts_current
5065 /* We may be defining a new type in the initializer
5066 of a static member variable. We allow this when
5067 not pedantic, and it is particularly useful for
5068 type punning via an anonymous union. */
5069 || COMPLETE_TYPE_P (b->this_entity))))
5070 b = b->level_chain;
5072 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
5074 /* Do C++ gratuitous typedefing. */
5075 if (IDENTIFIER_TYPE_VALUE (name) != type)
5077 tree tdef;
5078 int in_class = 0;
5079 tree context = TYPE_CONTEXT (type);
5081 if (! context)
5083 tree cs = current_scope ();
5085 if (scope == ts_current)
5086 context = cs;
5087 else if (cs != NULL_TREE && TYPE_P (cs))
5088 /* When declaring a friend class of a local class, we want
5089 to inject the newly named class into the scope
5090 containing the local class, not the namespace
5091 scope. */
5092 context = decl_function_context (get_type_decl (cs));
5094 if (!context)
5095 context = current_namespace;
5097 if (b->kind == sk_class
5098 || (b->kind == sk_template_parms
5099 && b->level_chain->kind == sk_class))
5100 in_class = 1;
5102 if (current_lang_name == lang_name_java)
5103 TYPE_FOR_JAVA (type) = 1;
5105 tdef = create_implicit_typedef (name, type);
5106 DECL_CONTEXT (tdef) = FROB_CONTEXT (context);
5107 if (scope == ts_within_enclosing_non_class)
5109 /* This is a friend. Make this TYPE_DECL node hidden from
5110 ordinary name lookup. Its corresponding TEMPLATE_DECL
5111 will be marked in push_template_decl_real. */
5112 retrofit_lang_decl (tdef);
5113 DECL_ANTICIPATED (tdef) = 1;
5114 DECL_FRIEND_P (tdef) = 1;
5117 decl = maybe_process_template_type_declaration
5118 (type, scope == ts_within_enclosing_non_class, b);
5119 if (decl == error_mark_node)
5120 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
5122 if (b->kind == sk_class)
5124 if (!TYPE_BEING_DEFINED (current_class_type))
5125 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
5127 if (!PROCESSING_REAL_TEMPLATE_DECL_P ())
5128 /* Put this TYPE_DECL on the TYPE_FIELDS list for the
5129 class. But if it's a member template class, we want
5130 the TEMPLATE_DECL, not the TYPE_DECL, so this is done
5131 later. */
5132 finish_member_declaration (decl);
5133 else
5134 pushdecl_class_level (decl);
5136 else if (b->kind != sk_template_parms)
5138 decl = pushdecl_with_scope (decl, b, /*is_friend=*/false);
5139 if (decl == error_mark_node)
5140 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
5143 if (! in_class)
5144 set_identifier_type_value_with_scope (name, tdef, b);
5146 TYPE_CONTEXT (type) = DECL_CONTEXT (decl);
5148 /* If this is a local class, keep track of it. We need this
5149 information for name-mangling, and so that it is possible to
5150 find all function definitions in a translation unit in a
5151 convenient way. (It's otherwise tricky to find a member
5152 function definition it's only pointed to from within a local
5153 class.) */
5154 if (TYPE_CONTEXT (type)
5155 && TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL)
5156 VEC_safe_push (tree, gc, local_classes, type);
5158 if (b->kind == sk_class
5159 && !COMPLETE_TYPE_P (current_class_type))
5161 maybe_add_class_template_decl_list (current_class_type,
5162 type, /*friend_p=*/0);
5164 if (CLASSTYPE_NESTED_UTDS (current_class_type) == NULL)
5165 CLASSTYPE_NESTED_UTDS (current_class_type)
5166 = binding_table_new (SCOPE_DEFAULT_HT_SIZE);
5168 binding_table_insert
5169 (CLASSTYPE_NESTED_UTDS (current_class_type), name, type);
5172 decl = TYPE_NAME (type);
5173 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
5174 TYPE_STUB_DECL (type) = decl;
5176 /* Set type visibility now if this is a forward declaration. */
5177 TREE_PUBLIC (decl) = 1;
5178 determine_visibility (decl);
5180 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, type);
5183 /* Subroutines for reverting temporarily to top-level for instantiation
5184 of templates and such. We actually need to clear out the class- and
5185 local-value slots of all identifiers, so that only the global values
5186 are at all visible. Simply setting current_binding_level to the global
5187 scope isn't enough, because more binding levels may be pushed. */
5188 struct saved_scope *scope_chain;
5190 /* If ID has not already been marked, add an appropriate binding to
5191 *OLD_BINDINGS. */
5193 static void
5194 store_binding (tree id, VEC(cxx_saved_binding,gc) **old_bindings)
5196 cxx_saved_binding *saved;
5198 if (!id || !IDENTIFIER_BINDING (id))
5199 return;
5201 if (IDENTIFIER_MARKED (id))
5202 return;
5204 IDENTIFIER_MARKED (id) = 1;
5206 saved = VEC_safe_push (cxx_saved_binding, gc, *old_bindings, NULL);
5207 saved->identifier = id;
5208 saved->binding = IDENTIFIER_BINDING (id);
5209 saved->real_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
5210 IDENTIFIER_BINDING (id) = NULL;
5213 static void
5214 store_bindings (tree names, VEC(cxx_saved_binding,gc) **old_bindings)
5216 tree t;
5218 timevar_push (TV_NAME_LOOKUP);
5219 for (t = names; t; t = TREE_CHAIN (t))
5221 tree id;
5223 if (TREE_CODE (t) == TREE_LIST)
5224 id = TREE_PURPOSE (t);
5225 else
5226 id = DECL_NAME (t);
5228 store_binding (id, old_bindings);
5230 timevar_pop (TV_NAME_LOOKUP);
5233 /* Like store_bindings, but NAMES is a vector of cp_class_binding
5234 objects, rather than a TREE_LIST. */
5236 static void
5237 store_class_bindings (VEC(cp_class_binding,gc) *names,
5238 VEC(cxx_saved_binding,gc) **old_bindings)
5240 size_t i;
5241 cp_class_binding *cb;
5243 timevar_push (TV_NAME_LOOKUP);
5244 for (i = 0; VEC_iterate(cp_class_binding, names, i, cb); ++i)
5245 store_binding (cb->identifier, old_bindings);
5246 timevar_pop (TV_NAME_LOOKUP);
5249 void
5250 push_to_top_level (void)
5252 struct saved_scope *s;
5253 struct cp_binding_level *b;
5254 cxx_saved_binding *sb;
5255 size_t i;
5256 bool need_pop;
5258 timevar_push (TV_NAME_LOOKUP);
5259 s = GGC_CNEW (struct saved_scope);
5261 b = scope_chain ? current_binding_level : 0;
5263 /* If we're in the middle of some function, save our state. */
5264 if (cfun)
5266 need_pop = true;
5267 push_function_context ();
5269 else
5270 need_pop = false;
5272 if (scope_chain && previous_class_level)
5273 store_class_bindings (previous_class_level->class_shadowed,
5274 &s->old_bindings);
5276 /* Have to include the global scope, because class-scope decls
5277 aren't listed anywhere useful. */
5278 for (; b; b = b->level_chain)
5280 tree t;
5282 /* Template IDs are inserted into the global level. If they were
5283 inserted into namespace level, finish_file wouldn't find them
5284 when doing pending instantiations. Therefore, don't stop at
5285 namespace level, but continue until :: . */
5286 if (global_scope_p (b))
5287 break;
5289 store_bindings (b->names, &s->old_bindings);
5290 /* We also need to check class_shadowed to save class-level type
5291 bindings, since pushclass doesn't fill in b->names. */
5292 if (b->kind == sk_class)
5293 store_class_bindings (b->class_shadowed, &s->old_bindings);
5295 /* Unwind type-value slots back to top level. */
5296 for (t = b->type_shadowed; t; t = TREE_CHAIN (t))
5297 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (t), TREE_VALUE (t));
5300 for (i = 0; VEC_iterate (cxx_saved_binding, s->old_bindings, i, sb); ++i)
5301 IDENTIFIER_MARKED (sb->identifier) = 0;
5303 s->prev = scope_chain;
5304 s->bindings = b;
5305 s->need_pop_function_context = need_pop;
5306 s->function_decl = current_function_decl;
5307 s->skip_evaluation = skip_evaluation;
5309 scope_chain = s;
5310 current_function_decl = NULL_TREE;
5311 current_lang_base = VEC_alloc (tree, gc, 10);
5312 current_lang_name = lang_name_cplusplus;
5313 current_namespace = global_namespace;
5314 push_class_stack ();
5315 skip_evaluation = 0;
5316 timevar_pop (TV_NAME_LOOKUP);
5319 void
5320 pop_from_top_level (void)
5322 struct saved_scope *s = scope_chain;
5323 cxx_saved_binding *saved;
5324 size_t i;
5326 timevar_push (TV_NAME_LOOKUP);
5327 /* Clear out class-level bindings cache. */
5328 if (previous_class_level)
5329 invalidate_class_lookup_cache ();
5330 pop_class_stack ();
5332 current_lang_base = 0;
5334 scope_chain = s->prev;
5335 for (i = 0; VEC_iterate (cxx_saved_binding, s->old_bindings, i, saved); ++i)
5337 tree id = saved->identifier;
5339 IDENTIFIER_BINDING (id) = saved->binding;
5340 SET_IDENTIFIER_TYPE_VALUE (id, saved->real_type_value);
5343 /* If we were in the middle of compiling a function, restore our
5344 state. */
5345 if (s->need_pop_function_context)
5346 pop_function_context ();
5347 current_function_decl = s->function_decl;
5348 skip_evaluation = s->skip_evaluation;
5349 timevar_pop (TV_NAME_LOOKUP);
5352 /* Pop off extraneous binding levels left over due to syntax errors.
5354 We don't pop past namespaces, as they might be valid. */
5356 void
5357 pop_everything (void)
5359 if (ENABLE_SCOPE_CHECKING)
5360 verbatim ("XXX entering pop_everything ()\n");
5361 while (!toplevel_bindings_p ())
5363 if (current_binding_level->kind == sk_class)
5364 pop_nested_class ();
5365 else
5366 poplevel (0, 0, 0);
5368 if (ENABLE_SCOPE_CHECKING)
5369 verbatim ("XXX leaving pop_everything ()\n");
5372 /* Emit debugging information for using declarations and directives.
5373 If input tree is overloaded fn then emit debug info for all
5374 candidates. */
5376 void
5377 cp_emit_debug_info_for_using (tree t, tree context)
5379 /* Don't try to emit any debug information if we have errors. */
5380 if (sorrycount || errorcount)
5381 return;
5383 /* Ignore this FUNCTION_DECL if it refers to a builtin declaration
5384 of a builtin function. */
5385 if (TREE_CODE (t) == FUNCTION_DECL
5386 && DECL_EXTERNAL (t)
5387 && DECL_BUILT_IN (t))
5388 return;
5390 /* Do not supply context to imported_module_or_decl, if
5391 it is a global namespace. */
5392 if (context == global_namespace)
5393 context = NULL_TREE;
5395 if (BASELINK_P (t))
5396 t = BASELINK_FUNCTIONS (t);
5398 /* FIXME: Handle TEMPLATE_DECLs. */
5399 for (t = OVL_CURRENT (t); t; t = OVL_NEXT (t))
5400 if (TREE_CODE (t) != TEMPLATE_DECL)
5402 if (building_stmt_tree ())
5403 add_stmt (build_stmt (USING_STMT, t));
5404 else
5405 (*debug_hooks->imported_module_or_decl) (t, NULL_TREE, context, false);
5409 #include "gt-cp-name-lookup.h"