tree-ssa-threadupdate.c: Include tree-cfg.h and tree-pass.h
[official-gcc.git] / gcc / cp / name-lookup.c
blobd0c024a120ffbf42e2b3cebb48eb987ecad4fdcc
1 /* Definitions for C++ name lookup routines.
2 Copyright (C) 2003-2013 Free Software Foundation, Inc.
3 Contributed by Gabriel Dos Reis <gdr@integrable-solutions.net>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "flags.h"
26 #include "tree.h"
27 #include "stringpool.h"
28 #include "print-tree.h"
29 #include "attribs.h"
30 #include "cp-tree.h"
31 #include "name-lookup.h"
32 #include "timevar.h"
33 #include "diagnostic-core.h"
34 #include "intl.h"
35 #include "debug.h"
36 #include "c-family/c-pragma.h"
37 #include "params.h"
38 #include "pointer-set.h"
40 /* The bindings for a particular name in a particular scope. */
42 struct scope_binding {
43 tree value;
44 tree type;
46 #define EMPTY_SCOPE_BINDING { NULL_TREE, NULL_TREE }
48 static cp_binding_level *innermost_nonclass_level (void);
49 static cxx_binding *binding_for_name (cp_binding_level *, tree);
50 static tree push_overloaded_decl (tree, int, bool);
51 static bool lookup_using_namespace (tree, struct scope_binding *, tree,
52 tree, int);
53 static bool qualified_lookup_using_namespace (tree, tree,
54 struct scope_binding *, int);
55 static tree lookup_type_current_level (tree);
56 static tree push_using_directive (tree);
57 static tree lookup_extern_c_fun_in_all_ns (tree);
58 static void diagnose_name_conflict (tree, tree);
60 /* The :: namespace. */
62 tree global_namespace;
64 /* The name of the anonymous namespace, throughout this translation
65 unit. */
66 static GTY(()) tree anonymous_namespace_name;
68 /* Initialize anonymous_namespace_name if necessary, and return it. */
70 static tree
71 get_anonymous_namespace_name (void)
73 if (!anonymous_namespace_name)
75 /* We used to use get_file_function_name here, but that isn't
76 necessary now that anonymous namespace typeinfos
77 are !TREE_PUBLIC, and thus compared by address. */
78 /* The demangler expects anonymous namespaces to be called
79 something starting with '_GLOBAL__N_'. */
80 anonymous_namespace_name = get_identifier ("_GLOBAL__N_1");
82 return anonymous_namespace_name;
85 /* Compute the chain index of a binding_entry given the HASH value of its
86 name and the total COUNT of chains. COUNT is assumed to be a power
87 of 2. */
89 #define ENTRY_INDEX(HASH, COUNT) (((HASH) >> 3) & ((COUNT) - 1))
91 /* A free list of "binding_entry"s awaiting for re-use. */
93 static GTY((deletable)) binding_entry free_binding_entry = NULL;
95 /* Create a binding_entry object for (NAME, TYPE). */
97 static inline binding_entry
98 binding_entry_make (tree name, tree type)
100 binding_entry entry;
102 if (free_binding_entry)
104 entry = free_binding_entry;
105 free_binding_entry = entry->chain;
107 else
108 entry = ggc_alloc_binding_entry_s ();
110 entry->name = name;
111 entry->type = type;
112 entry->chain = NULL;
114 return entry;
117 /* Put ENTRY back on the free list. */
118 #if 0
119 static inline void
120 binding_entry_free (binding_entry entry)
122 entry->name = NULL;
123 entry->type = NULL;
124 entry->chain = free_binding_entry;
125 free_binding_entry = entry;
127 #endif
129 /* The datatype used to implement the mapping from names to types at
130 a given scope. */
131 struct GTY(()) binding_table_s {
132 /* Array of chains of "binding_entry"s */
133 binding_entry * GTY((length ("%h.chain_count"))) chain;
135 /* The number of chains in this table. This is the length of the
136 member "chain" considered as an array. */
137 size_t chain_count;
139 /* Number of "binding_entry"s in this table. */
140 size_t entry_count;
143 /* Construct TABLE with an initial CHAIN_COUNT. */
145 static inline void
146 binding_table_construct (binding_table table, size_t chain_count)
148 table->chain_count = chain_count;
149 table->entry_count = 0;
150 table->chain = ggc_alloc_cleared_vec_binding_entry (table->chain_count);
153 /* Make TABLE's entries ready for reuse. */
154 #if 0
155 static void
156 binding_table_free (binding_table table)
158 size_t i;
159 size_t count;
161 if (table == NULL)
162 return;
164 for (i = 0, count = table->chain_count; i < count; ++i)
166 binding_entry temp = table->chain[i];
167 while (temp != NULL)
169 binding_entry entry = temp;
170 temp = entry->chain;
171 binding_entry_free (entry);
173 table->chain[i] = NULL;
175 table->entry_count = 0;
177 #endif
179 /* Allocate a table with CHAIN_COUNT, assumed to be a power of two. */
181 static inline binding_table
182 binding_table_new (size_t chain_count)
184 binding_table table = ggc_alloc_binding_table_s ();
185 table->chain = NULL;
186 binding_table_construct (table, chain_count);
187 return table;
190 /* Expand TABLE to twice its current chain_count. */
192 static void
193 binding_table_expand (binding_table table)
195 const size_t old_chain_count = table->chain_count;
196 const size_t old_entry_count = table->entry_count;
197 const size_t new_chain_count = 2 * old_chain_count;
198 binding_entry *old_chains = table->chain;
199 size_t i;
201 binding_table_construct (table, new_chain_count);
202 for (i = 0; i < old_chain_count; ++i)
204 binding_entry entry = old_chains[i];
205 for (; entry != NULL; entry = old_chains[i])
207 const unsigned int hash = IDENTIFIER_HASH_VALUE (entry->name);
208 const size_t j = ENTRY_INDEX (hash, new_chain_count);
210 old_chains[i] = entry->chain;
211 entry->chain = table->chain[j];
212 table->chain[j] = entry;
215 table->entry_count = old_entry_count;
218 /* Insert a binding for NAME to TYPE into TABLE. */
220 static void
221 binding_table_insert (binding_table table, tree name, tree type)
223 const unsigned int hash = IDENTIFIER_HASH_VALUE (name);
224 const size_t i = ENTRY_INDEX (hash, table->chain_count);
225 binding_entry entry = binding_entry_make (name, type);
227 entry->chain = table->chain[i];
228 table->chain[i] = entry;
229 ++table->entry_count;
231 if (3 * table->chain_count < 5 * table->entry_count)
232 binding_table_expand (table);
235 /* Return the binding_entry, if any, that maps NAME. */
237 binding_entry
238 binding_table_find (binding_table table, tree name)
240 const unsigned int hash = IDENTIFIER_HASH_VALUE (name);
241 binding_entry entry = table->chain[ENTRY_INDEX (hash, table->chain_count)];
243 while (entry != NULL && entry->name != name)
244 entry = entry->chain;
246 return entry;
249 /* Apply PROC -- with DATA -- to all entries in TABLE. */
251 void
252 binding_table_foreach (binding_table table, bt_foreach_proc proc, void *data)
254 size_t chain_count;
255 size_t i;
257 if (!table)
258 return;
260 chain_count = table->chain_count;
261 for (i = 0; i < chain_count; ++i)
263 binding_entry entry = table->chain[i];
264 for (; entry != NULL; entry = entry->chain)
265 proc (entry, data);
269 #ifndef ENABLE_SCOPE_CHECKING
270 # define ENABLE_SCOPE_CHECKING 0
271 #else
272 # define ENABLE_SCOPE_CHECKING 1
273 #endif
275 /* A free list of "cxx_binding"s, connected by their PREVIOUS. */
277 static GTY((deletable)) cxx_binding *free_bindings;
279 /* Initialize VALUE and TYPE field for BINDING, and set the PREVIOUS
280 field to NULL. */
282 static inline void
283 cxx_binding_init (cxx_binding *binding, tree value, tree type)
285 binding->value = value;
286 binding->type = type;
287 binding->previous = NULL;
290 /* (GC)-allocate a binding object with VALUE and TYPE member initialized. */
292 static cxx_binding *
293 cxx_binding_make (tree value, tree type)
295 cxx_binding *binding;
296 if (free_bindings)
298 binding = free_bindings;
299 free_bindings = binding->previous;
301 else
302 binding = ggc_alloc_cxx_binding ();
304 cxx_binding_init (binding, value, type);
306 return binding;
309 /* Put BINDING back on the free list. */
311 static inline void
312 cxx_binding_free (cxx_binding *binding)
314 binding->scope = NULL;
315 binding->previous = free_bindings;
316 free_bindings = binding;
319 /* Create a new binding for NAME (with the indicated VALUE and TYPE
320 bindings) in the class scope indicated by SCOPE. */
322 static cxx_binding *
323 new_class_binding (tree name, tree value, tree type, cp_binding_level *scope)
325 cp_class_binding cb = {cxx_binding_make (value, type), name};
326 cxx_binding *binding = cb.base;
327 vec_safe_push (scope->class_shadowed, cb);
328 binding->scope = scope;
329 return binding;
332 /* Make DECL the innermost binding for ID. The LEVEL is the binding
333 level at which this declaration is being bound. */
335 static void
336 push_binding (tree id, tree decl, cp_binding_level* level)
338 cxx_binding *binding;
340 if (level != class_binding_level)
342 binding = cxx_binding_make (decl, NULL_TREE);
343 binding->scope = level;
345 else
346 binding = new_class_binding (id, decl, /*type=*/NULL_TREE, level);
348 /* Now, fill in the binding information. */
349 binding->previous = IDENTIFIER_BINDING (id);
350 INHERITED_VALUE_BINDING_P (binding) = 0;
351 LOCAL_BINDING_P (binding) = (level != class_binding_level);
353 /* And put it on the front of the list of bindings for ID. */
354 IDENTIFIER_BINDING (id) = binding;
357 /* Remove the binding for DECL which should be the innermost binding
358 for ID. */
360 void
361 pop_binding (tree id, tree decl)
363 cxx_binding *binding;
365 if (id == NULL_TREE)
366 /* It's easiest to write the loops that call this function without
367 checking whether or not the entities involved have names. We
368 get here for such an entity. */
369 return;
371 /* Get the innermost binding for ID. */
372 binding = IDENTIFIER_BINDING (id);
374 /* The name should be bound. */
375 gcc_assert (binding != NULL);
377 /* The DECL will be either the ordinary binding or the type
378 binding for this identifier. Remove that binding. */
379 if (binding->value == decl)
380 binding->value = NULL_TREE;
381 else
383 gcc_assert (binding->type == decl);
384 binding->type = NULL_TREE;
387 if (!binding->value && !binding->type)
389 /* We're completely done with the innermost binding for this
390 identifier. Unhook it from the list of bindings. */
391 IDENTIFIER_BINDING (id) = binding->previous;
393 /* Add it to the free list. */
394 cxx_binding_free (binding);
398 /* Remove the bindings for the decls of the current level and leave
399 the current scope. */
401 void
402 pop_bindings_and_leave_scope (void)
404 for (tree t = getdecls (); t; t = DECL_CHAIN (t))
405 pop_binding (DECL_NAME (t), t);
406 leave_scope ();
409 /* Strip non dependent using declarations. */
411 tree
412 strip_using_decl (tree decl)
414 if (decl == NULL_TREE)
415 return NULL_TREE;
417 while (TREE_CODE (decl) == USING_DECL && !DECL_DEPENDENT_P (decl))
418 decl = USING_DECL_DECLS (decl);
419 return decl;
422 /* BINDING records an existing declaration for a name in the current scope.
423 But, DECL is another declaration for that same identifier in the
424 same scope. This is the `struct stat' hack whereby a non-typedef
425 class name or enum-name can be bound at the same level as some other
426 kind of entity.
427 3.3.7/1
429 A class name (9.1) or enumeration name (7.2) can be hidden by the
430 name of an object, function, or enumerator declared in the same scope.
431 If a class or enumeration name and an object, function, or enumerator
432 are declared in the same scope (in any order) with the same name, the
433 class or enumeration name is hidden wherever the object, function, or
434 enumerator name is visible.
436 It's the responsibility of the caller to check that
437 inserting this name is valid here. Returns nonzero if the new binding
438 was successful. */
440 static bool
441 supplement_binding_1 (cxx_binding *binding, tree decl)
443 tree bval = binding->value;
444 bool ok = true;
445 tree target_bval = strip_using_decl (bval);
446 tree target_decl = strip_using_decl (decl);
448 if (TREE_CODE (target_decl) == TYPE_DECL && DECL_ARTIFICIAL (target_decl)
449 && target_decl != target_bval
450 && (TREE_CODE (target_bval) != TYPE_DECL
451 /* We allow pushing an enum multiple times in a class
452 template in order to handle late matching of underlying
453 type on an opaque-enum-declaration followed by an
454 enum-specifier. */
455 || (processing_template_decl
456 && TREE_CODE (TREE_TYPE (target_decl)) == ENUMERAL_TYPE
457 && TREE_CODE (TREE_TYPE (target_bval)) == ENUMERAL_TYPE
458 && (dependent_type_p (ENUM_UNDERLYING_TYPE
459 (TREE_TYPE (target_decl)))
460 || dependent_type_p (ENUM_UNDERLYING_TYPE
461 (TREE_TYPE (target_bval)))))))
462 /* The new name is the type name. */
463 binding->type = decl;
464 else if (/* TARGET_BVAL is null when push_class_level_binding moves
465 an inherited type-binding out of the way to make room
466 for a new value binding. */
467 !target_bval
468 /* TARGET_BVAL is error_mark_node when TARGET_DECL's name
469 has been used in a non-class scope prior declaration.
470 In that case, we should have already issued a
471 diagnostic; for graceful error recovery purpose, pretend
472 this was the intended declaration for that name. */
473 || target_bval == error_mark_node
474 /* If TARGET_BVAL is anticipated but has not yet been
475 declared, pretend it is not there at all. */
476 || (TREE_CODE (target_bval) == FUNCTION_DECL
477 && DECL_ANTICIPATED (target_bval)
478 && !DECL_HIDDEN_FRIEND_P (target_bval)))
479 binding->value = decl;
480 else if (TREE_CODE (target_bval) == TYPE_DECL
481 && DECL_ARTIFICIAL (target_bval)
482 && target_decl != target_bval
483 && (TREE_CODE (target_decl) != TYPE_DECL
484 || same_type_p (TREE_TYPE (target_decl),
485 TREE_TYPE (target_bval))))
487 /* The old binding was a type name. It was placed in
488 VALUE field because it was thought, at the point it was
489 declared, to be the only entity with such a name. Move the
490 type name into the type slot; it is now hidden by the new
491 binding. */
492 binding->type = bval;
493 binding->value = decl;
494 binding->value_is_inherited = false;
496 else if (TREE_CODE (target_bval) == TYPE_DECL
497 && TREE_CODE (target_decl) == TYPE_DECL
498 && DECL_NAME (target_decl) == DECL_NAME (target_bval)
499 && binding->scope->kind != sk_class
500 && (same_type_p (TREE_TYPE (target_decl), TREE_TYPE (target_bval))
501 /* If either type involves template parameters, we must
502 wait until instantiation. */
503 || uses_template_parms (TREE_TYPE (target_decl))
504 || uses_template_parms (TREE_TYPE (target_bval))))
505 /* We have two typedef-names, both naming the same type to have
506 the same name. In general, this is OK because of:
508 [dcl.typedef]
510 In a given scope, a typedef specifier can be used to redefine
511 the name of any type declared in that scope to refer to the
512 type to which it already refers.
514 However, in class scopes, this rule does not apply due to the
515 stricter language in [class.mem] prohibiting redeclarations of
516 members. */
517 ok = false;
518 /* There can be two block-scope declarations of the same variable,
519 so long as they are `extern' declarations. However, there cannot
520 be two declarations of the same static data member:
522 [class.mem]
524 A member shall not be declared twice in the
525 member-specification. */
526 else if (VAR_P (target_decl)
527 && VAR_P (target_bval)
528 && DECL_EXTERNAL (target_decl) && DECL_EXTERNAL (target_bval)
529 && !DECL_CLASS_SCOPE_P (target_decl))
531 duplicate_decls (decl, binding->value, /*newdecl_is_friend=*/false);
532 ok = false;
534 else if (TREE_CODE (decl) == NAMESPACE_DECL
535 && TREE_CODE (bval) == NAMESPACE_DECL
536 && DECL_NAMESPACE_ALIAS (decl)
537 && DECL_NAMESPACE_ALIAS (bval)
538 && ORIGINAL_NAMESPACE (bval) == ORIGINAL_NAMESPACE (decl))
539 /* [namespace.alias]
541 In a declarative region, a namespace-alias-definition can be
542 used to redefine a namespace-alias declared in that declarative
543 region to refer only to the namespace to which it already
544 refers. */
545 ok = false;
546 else
548 diagnose_name_conflict (decl, bval);
549 ok = false;
552 return ok;
555 /* Diagnose a name conflict between DECL and BVAL. */
557 static void
558 diagnose_name_conflict (tree decl, tree bval)
560 if (TREE_CODE (decl) == TREE_CODE (bval)
561 && (TREE_CODE (decl) != TYPE_DECL
562 || (DECL_ARTIFICIAL (decl) && DECL_ARTIFICIAL (bval))
563 || (!DECL_ARTIFICIAL (decl) && !DECL_ARTIFICIAL (bval)))
564 && !is_overloaded_fn (decl))
565 error ("redeclaration of %q#D", decl);
566 else
567 error ("%q#D conflicts with a previous declaration", decl);
569 inform (input_location, "previous declaration %q+#D", bval);
572 /* Wrapper for supplement_binding_1. */
574 static bool
575 supplement_binding (cxx_binding *binding, tree decl)
577 bool ret;
578 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
579 ret = supplement_binding_1 (binding, decl);
580 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
581 return ret;
584 /* Add DECL to the list of things declared in B. */
586 static void
587 add_decl_to_level (tree decl, cp_binding_level *b)
589 /* We used to record virtual tables as if they were ordinary
590 variables, but no longer do so. */
591 gcc_assert (!(VAR_P (decl) && DECL_VIRTUAL_P (decl)));
593 if (TREE_CODE (decl) == NAMESPACE_DECL
594 && !DECL_NAMESPACE_ALIAS (decl))
596 DECL_CHAIN (decl) = b->namespaces;
597 b->namespaces = decl;
599 else
601 /* We build up the list in reverse order, and reverse it later if
602 necessary. */
603 TREE_CHAIN (decl) = b->names;
604 b->names = decl;
606 /* If appropriate, add decl to separate list of statics. We
607 include extern variables because they might turn out to be
608 static later. It's OK for this list to contain a few false
609 positives. */
610 if (b->kind == sk_namespace)
611 if ((VAR_P (decl)
612 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
613 || (TREE_CODE (decl) == FUNCTION_DECL
614 && (!TREE_PUBLIC (decl)
615 || decl_anon_ns_mem_p (decl)
616 || DECL_DECLARED_INLINE_P (decl))))
617 vec_safe_push (b->static_decls, decl);
621 /* Record a decl-node X as belonging to the current lexical scope.
622 Check for errors (such as an incompatible declaration for the same
623 name already seen in the same scope). IS_FRIEND is true if X is
624 declared as a friend.
626 Returns either X or an old decl for the same name.
627 If an old decl is returned, it may have been smashed
628 to agree with what X says. */
630 static tree
631 pushdecl_maybe_friend_1 (tree x, bool is_friend)
633 tree t;
634 tree name;
635 int need_new_binding;
637 if (x == error_mark_node)
638 return error_mark_node;
640 need_new_binding = 1;
642 if (DECL_TEMPLATE_PARM_P (x))
643 /* Template parameters have no context; they are not X::T even
644 when declared within a class or namespace. */
646 else
648 if (current_function_decl && x != current_function_decl
649 /* A local declaration for a function doesn't constitute
650 nesting. */
651 && TREE_CODE (x) != FUNCTION_DECL
652 /* A local declaration for an `extern' variable is in the
653 scope of the current namespace, not the current
654 function. */
655 && !(VAR_P (x) && DECL_EXTERNAL (x))
656 /* When parsing the parameter list of a function declarator,
657 don't set DECL_CONTEXT to an enclosing function. When we
658 push the PARM_DECLs in order to process the function body,
659 current_binding_level->this_entity will be set. */
660 && !(TREE_CODE (x) == PARM_DECL
661 && current_binding_level->kind == sk_function_parms
662 && current_binding_level->this_entity == NULL)
663 && !DECL_CONTEXT (x))
664 DECL_CONTEXT (x) = current_function_decl;
666 /* If this is the declaration for a namespace-scope function,
667 but the declaration itself is in a local scope, mark the
668 declaration. */
669 if (TREE_CODE (x) == FUNCTION_DECL
670 && DECL_NAMESPACE_SCOPE_P (x)
671 && current_function_decl
672 && x != current_function_decl)
673 DECL_LOCAL_FUNCTION_P (x) = 1;
676 name = DECL_NAME (x);
677 if (name)
679 int different_binding_level = 0;
681 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
682 name = TREE_OPERAND (name, 0);
684 /* In case this decl was explicitly namespace-qualified, look it
685 up in its namespace context. */
686 if (DECL_NAMESPACE_SCOPE_P (x) && namespace_bindings_p ())
687 t = namespace_binding (name, DECL_CONTEXT (x));
688 else
689 t = lookup_name_innermost_nonclass_level (name);
691 /* [basic.link] If there is a visible declaration of an entity
692 with linkage having the same name and type, ignoring entities
693 declared outside the innermost enclosing namespace scope, the
694 block scope declaration declares that same entity and
695 receives the linkage of the previous declaration. */
696 if (! t && current_function_decl && x != current_function_decl
697 && VAR_OR_FUNCTION_DECL_P (x)
698 && DECL_EXTERNAL (x))
700 /* Look in block scope. */
701 t = innermost_non_namespace_value (name);
702 /* Or in the innermost namespace. */
703 if (! t)
704 t = namespace_binding (name, DECL_CONTEXT (x));
705 /* Does it have linkage? Note that if this isn't a DECL, it's an
706 OVERLOAD, which is OK. */
707 if (t && DECL_P (t) && ! (TREE_STATIC (t) || DECL_EXTERNAL (t)))
708 t = NULL_TREE;
709 if (t)
710 different_binding_level = 1;
713 /* If we are declaring a function, and the result of name-lookup
714 was an OVERLOAD, look for an overloaded instance that is
715 actually the same as the function we are declaring. (If
716 there is one, we have to merge our declaration with the
717 previous declaration.) */
718 if (t && TREE_CODE (t) == OVERLOAD)
720 tree match;
722 if (TREE_CODE (x) == FUNCTION_DECL)
723 for (match = t; match; match = OVL_NEXT (match))
725 if (decls_match (OVL_CURRENT (match), x))
726 break;
728 else
729 /* Just choose one. */
730 match = t;
732 if (match)
733 t = OVL_CURRENT (match);
734 else
735 t = NULL_TREE;
738 if (t && t != error_mark_node)
740 if (different_binding_level)
742 if (decls_match (x, t))
743 /* The standard only says that the local extern
744 inherits linkage from the previous decl; in
745 particular, default args are not shared. Add
746 the decl into a hash table to make sure only
747 the previous decl in this case is seen by the
748 middle end. */
750 struct cxx_int_tree_map *h;
751 void **loc;
753 TREE_PUBLIC (x) = TREE_PUBLIC (t);
755 if (cp_function_chain->extern_decl_map == NULL)
756 cp_function_chain->extern_decl_map
757 = htab_create_ggc (20, cxx_int_tree_map_hash,
758 cxx_int_tree_map_eq, NULL);
760 h = ggc_alloc_cxx_int_tree_map ();
761 h->uid = DECL_UID (x);
762 h->to = t;
763 loc = htab_find_slot_with_hash
764 (cp_function_chain->extern_decl_map, h,
765 h->uid, INSERT);
766 *(struct cxx_int_tree_map **) loc = h;
769 else if (TREE_CODE (t) == PARM_DECL)
771 /* Check for duplicate params. */
772 tree d = duplicate_decls (x, t, is_friend);
773 if (d)
774 return d;
776 else if ((DECL_EXTERN_C_FUNCTION_P (x)
777 || DECL_FUNCTION_TEMPLATE_P (x))
778 && is_overloaded_fn (t))
779 /* Don't do anything just yet. */;
780 else if (t == wchar_decl_node)
782 if (! DECL_IN_SYSTEM_HEADER (x))
783 pedwarn (input_location, OPT_Wpedantic, "redeclaration of %<wchar_t%> as %qT",
784 TREE_TYPE (x));
786 /* Throw away the redeclaration. */
787 return t;
789 else
791 tree olddecl = duplicate_decls (x, t, is_friend);
793 /* If the redeclaration failed, we can stop at this
794 point. */
795 if (olddecl == error_mark_node)
796 return error_mark_node;
798 if (olddecl)
800 if (TREE_CODE (t) == TYPE_DECL)
801 SET_IDENTIFIER_TYPE_VALUE (name, TREE_TYPE (t));
803 return t;
805 else if (DECL_MAIN_P (x) && TREE_CODE (t) == FUNCTION_DECL)
807 /* A redeclaration of main, but not a duplicate of the
808 previous one.
810 [basic.start.main]
812 This function shall not be overloaded. */
813 error ("invalid redeclaration of %q+D", t);
814 error ("as %qD", x);
815 /* We don't try to push this declaration since that
816 causes a crash. */
817 return x;
822 /* If x has C linkage-specification, (extern "C"),
823 lookup its binding, in case it's already bound to an object.
824 The lookup is done in all namespaces.
825 If we find an existing binding, make sure it has the same
826 exception specification as x, otherwise, bail in error [7.5, 7.6]. */
827 if ((TREE_CODE (x) == FUNCTION_DECL)
828 && DECL_EXTERN_C_P (x)
829 /* We should ignore declarations happening in system headers. */
830 && !DECL_ARTIFICIAL (x)
831 && !DECL_IN_SYSTEM_HEADER (x))
833 tree previous = lookup_extern_c_fun_in_all_ns (x);
834 if (previous
835 && !DECL_ARTIFICIAL (previous)
836 && !DECL_IN_SYSTEM_HEADER (previous)
837 && DECL_CONTEXT (previous) != DECL_CONTEXT (x))
839 /* In case either x or previous is declared to throw an exception,
840 make sure both exception specifications are equal. */
841 if (decls_match (x, previous))
843 tree x_exception_spec = NULL_TREE;
844 tree previous_exception_spec = NULL_TREE;
846 x_exception_spec =
847 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (x));
848 previous_exception_spec =
849 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (previous));
850 if (!comp_except_specs (previous_exception_spec,
851 x_exception_spec,
852 ce_normal))
854 pedwarn (input_location, 0,
855 "declaration of %q#D with C language linkage",
857 pedwarn (input_location, 0,
858 "conflicts with previous declaration %q+#D",
859 previous);
860 pedwarn (input_location, 0,
861 "due to different exception specifications");
862 return error_mark_node;
864 if (DECL_ASSEMBLER_NAME_SET_P (previous))
865 SET_DECL_ASSEMBLER_NAME (x,
866 DECL_ASSEMBLER_NAME (previous));
868 else
870 pedwarn (input_location, 0,
871 "declaration of %q#D with C language linkage", x);
872 pedwarn (input_location, 0,
873 "conflicts with previous declaration %q+#D",
874 previous);
879 check_template_shadow (x);
881 /* If this is a function conjured up by the back end, massage it
882 so it looks friendly. */
883 if (DECL_NON_THUNK_FUNCTION_P (x) && ! DECL_LANG_SPECIFIC (x))
885 retrofit_lang_decl (x);
886 SET_DECL_LANGUAGE (x, lang_c);
889 t = x;
890 if (DECL_NON_THUNK_FUNCTION_P (x) && ! DECL_FUNCTION_MEMBER_P (x))
892 t = push_overloaded_decl (x, PUSH_LOCAL, is_friend);
893 if (!namespace_bindings_p ())
894 /* We do not need to create a binding for this name;
895 push_overloaded_decl will have already done so if
896 necessary. */
897 need_new_binding = 0;
899 else if (DECL_FUNCTION_TEMPLATE_P (x) && DECL_NAMESPACE_SCOPE_P (x))
901 t = push_overloaded_decl (x, PUSH_GLOBAL, is_friend);
902 if (t == x)
903 add_decl_to_level (x, NAMESPACE_LEVEL (CP_DECL_CONTEXT (t)));
906 if (DECL_DECLARES_FUNCTION_P (t))
907 check_default_args (t);
909 if (t != x || DECL_FUNCTION_TEMPLATE_P (t))
910 return t;
912 /* If declaring a type as a typedef, copy the type (unless we're
913 at line 0), and install this TYPE_DECL as the new type's typedef
914 name. See the extensive comment of set_underlying_type (). */
915 if (TREE_CODE (x) == TYPE_DECL)
917 tree type = TREE_TYPE (x);
919 if (DECL_IS_BUILTIN (x)
920 || (TREE_TYPE (x) != error_mark_node
921 && TYPE_NAME (type) != x
922 /* We don't want to copy the type when all we're
923 doing is making a TYPE_DECL for the purposes of
924 inlining. */
925 && (!TYPE_NAME (type)
926 || TYPE_NAME (type) != DECL_ABSTRACT_ORIGIN (x))))
927 set_underlying_type (x);
929 if (type != error_mark_node
930 && TYPE_NAME (type)
931 && TYPE_IDENTIFIER (type))
932 set_identifier_type_value (DECL_NAME (x), x);
934 /* If this is a locally defined typedef in a function that
935 is not a template instantation, record it to implement
936 -Wunused-local-typedefs. */
937 if (current_instantiation () == NULL
938 || (current_instantiation ()->decl != current_function_decl))
939 record_locally_defined_typedef (x);
942 /* Multiple external decls of the same identifier ought to match.
944 We get warnings about inline functions where they are defined.
945 We get warnings about other functions from push_overloaded_decl.
947 Avoid duplicate warnings where they are used. */
948 if (TREE_PUBLIC (x) && TREE_CODE (x) != FUNCTION_DECL)
950 tree decl;
952 decl = IDENTIFIER_NAMESPACE_VALUE (name);
953 if (decl && TREE_CODE (decl) == OVERLOAD)
954 decl = OVL_FUNCTION (decl);
956 if (decl && decl != error_mark_node
957 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl))
958 /* If different sort of thing, we already gave an error. */
959 && TREE_CODE (decl) == TREE_CODE (x)
960 && !same_type_p (TREE_TYPE (x), TREE_TYPE (decl)))
962 if (permerror (input_location, "type mismatch with previous "
963 "external decl of %q#D", x))
964 inform (input_location, "previous external decl of %q+#D",
965 decl);
969 if (TREE_CODE (x) == FUNCTION_DECL
970 && is_friend
971 && !flag_friend_injection)
973 /* This is a new declaration of a friend function, so hide
974 it from ordinary function lookup. */
975 DECL_ANTICIPATED (x) = 1;
976 DECL_HIDDEN_FRIEND_P (x) = 1;
979 /* This name is new in its binding level.
980 Install the new declaration and return it. */
981 if (namespace_bindings_p ())
983 /* Install a global value. */
985 /* If the first global decl has external linkage,
986 warn if we later see static one. */
987 if (IDENTIFIER_GLOBAL_VALUE (name) == NULL_TREE && TREE_PUBLIC (x))
988 TREE_PUBLIC (name) = 1;
990 /* Bind the name for the entity. */
991 if (!(TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x)
992 && t != NULL_TREE)
993 && (TREE_CODE (x) == TYPE_DECL
994 || VAR_P (x)
995 || TREE_CODE (x) == NAMESPACE_DECL
996 || TREE_CODE (x) == CONST_DECL
997 || TREE_CODE (x) == TEMPLATE_DECL))
998 SET_IDENTIFIER_NAMESPACE_VALUE (name, x);
1000 /* If new decl is `static' and an `extern' was seen previously,
1001 warn about it. */
1002 if (x != NULL_TREE && t != NULL_TREE && decls_match (x, t))
1003 warn_extern_redeclared_static (x, t);
1005 else
1007 /* Here to install a non-global value. */
1008 tree oldglobal = IDENTIFIER_NAMESPACE_VALUE (name);
1009 tree oldlocal = NULL_TREE;
1010 cp_binding_level *oldscope = NULL;
1011 cxx_binding *oldbinding = outer_binding (name, NULL, true);
1012 if (oldbinding)
1014 oldlocal = oldbinding->value;
1015 oldscope = oldbinding->scope;
1018 if (need_new_binding)
1020 push_local_binding (name, x, 0);
1021 /* Because push_local_binding will hook X on to the
1022 current_binding_level's name list, we don't want to
1023 do that again below. */
1024 need_new_binding = 0;
1027 /* If this is a TYPE_DECL, push it into the type value slot. */
1028 if (TREE_CODE (x) == TYPE_DECL)
1029 set_identifier_type_value (name, x);
1031 /* Clear out any TYPE_DECL shadowed by a namespace so that
1032 we won't think this is a type. The C struct hack doesn't
1033 go through namespaces. */
1034 if (TREE_CODE (x) == NAMESPACE_DECL)
1035 set_identifier_type_value (name, NULL_TREE);
1037 if (oldlocal)
1039 tree d = oldlocal;
1041 while (oldlocal
1042 && VAR_P (oldlocal)
1043 && DECL_DEAD_FOR_LOCAL (oldlocal))
1044 oldlocal = DECL_SHADOWED_FOR_VAR (oldlocal);
1046 if (oldlocal == NULL_TREE)
1047 oldlocal = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (d));
1050 /* If this is an extern function declaration, see if we
1051 have a global definition or declaration for the function. */
1052 if (oldlocal == NULL_TREE
1053 && DECL_EXTERNAL (x)
1054 && oldglobal != NULL_TREE
1055 && TREE_CODE (x) == FUNCTION_DECL
1056 && TREE_CODE (oldglobal) == FUNCTION_DECL)
1058 /* We have one. Their types must agree. */
1059 if (decls_match (x, oldglobal))
1060 /* OK */;
1061 else
1063 warning (0, "extern declaration of %q#D doesn%'t match", x);
1064 warning (0, "global declaration %q+#D", oldglobal);
1067 /* If we have a local external declaration,
1068 and no file-scope declaration has yet been seen,
1069 then if we later have a file-scope decl it must not be static. */
1070 if (oldlocal == NULL_TREE
1071 && oldglobal == NULL_TREE
1072 && DECL_EXTERNAL (x)
1073 && TREE_PUBLIC (x))
1074 TREE_PUBLIC (name) = 1;
1076 /* Don't complain about the parms we push and then pop
1077 while tentatively parsing a function declarator. */
1078 if (TREE_CODE (x) == PARM_DECL && DECL_CONTEXT (x) == NULL_TREE)
1079 /* Ignore. */;
1081 /* Warn if shadowing an argument at the top level of the body. */
1082 else if (oldlocal != NULL_TREE && !DECL_EXTERNAL (x)
1083 /* Inline decls shadow nothing. */
1084 && !DECL_FROM_INLINE (x)
1085 && (TREE_CODE (oldlocal) == PARM_DECL
1086 || VAR_P (oldlocal)
1087 /* If the old decl is a type decl, only warn if the
1088 old decl is an explicit typedef or if both the old
1089 and new decls are type decls. */
1090 || (TREE_CODE (oldlocal) == TYPE_DECL
1091 && (!DECL_ARTIFICIAL (oldlocal)
1092 || TREE_CODE (x) == TYPE_DECL)))
1093 /* Don't check for internally generated vars unless
1094 it's an implicit typedef (see create_implicit_typedef
1095 in decl.c). */
1096 && (!DECL_ARTIFICIAL (x) || DECL_IMPLICIT_TYPEDEF_P (x)))
1098 bool nowarn = false;
1100 /* Don't complain if it's from an enclosing function. */
1101 if (DECL_CONTEXT (oldlocal) == current_function_decl
1102 && TREE_CODE (x) != PARM_DECL
1103 && TREE_CODE (oldlocal) == PARM_DECL)
1105 /* Go to where the parms should be and see if we find
1106 them there. */
1107 cp_binding_level *b = current_binding_level->level_chain;
1109 if (FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
1110 /* Skip the ctor/dtor cleanup level. */
1111 b = b->level_chain;
1113 /* ARM $8.3 */
1114 if (b->kind == sk_function_parms)
1116 error ("declaration of %q#D shadows a parameter", x);
1117 nowarn = true;
1121 /* The local structure or class can't use parameters of
1122 the containing function anyway. */
1123 if (DECL_CONTEXT (oldlocal) != current_function_decl)
1125 cp_binding_level *scope = current_binding_level;
1126 tree context = DECL_CONTEXT (oldlocal);
1127 for (; scope; scope = scope->level_chain)
1129 if (scope->kind == sk_function_parms
1130 && scope->this_entity == context)
1131 break;
1132 if (scope->kind == sk_class
1133 && !LAMBDA_TYPE_P (scope->this_entity))
1135 nowarn = true;
1136 break;
1140 /* Error if redeclaring a local declared in a
1141 for-init-statement or in the condition of an if or
1142 switch statement when the new declaration is in the
1143 outermost block of the controlled statement.
1144 Redeclaring a variable from a for or while condition is
1145 detected elsewhere. */
1146 else if (VAR_P (oldlocal)
1147 && oldscope == current_binding_level->level_chain
1148 && (oldscope->kind == sk_cond
1149 || oldscope->kind == sk_for))
1151 error ("redeclaration of %q#D", x);
1152 inform (input_location, "%q+#D previously declared here",
1153 oldlocal);
1154 nowarn = true;
1156 /* C++11:
1157 3.3.3/3: The name declared in an exception-declaration (...)
1158 shall not be redeclared in the outermost block of the handler.
1159 3.3.3/2: A parameter name shall not be redeclared (...) in
1160 the outermost block of any handler associated with a
1161 function-try-block.
1162 3.4.1/15: The function parameter names shall not be redeclared
1163 in the exception-declaration nor in the outermost block of a
1164 handler for the function-try-block. */
1165 else if ((VAR_P (oldlocal)
1166 && oldscope == current_binding_level->level_chain
1167 && oldscope->kind == sk_catch)
1168 || (TREE_CODE (oldlocal) == PARM_DECL
1169 && (current_binding_level->kind == sk_catch
1170 || (current_binding_level->level_chain->kind
1171 == sk_catch))
1172 && in_function_try_handler))
1174 if (permerror (input_location, "redeclaration of %q#D", x))
1175 inform (input_location, "%q+#D previously declared here",
1176 oldlocal);
1177 nowarn = true;
1180 if (warn_shadow && !nowarn)
1182 bool warned;
1184 if (TREE_CODE (oldlocal) == PARM_DECL)
1185 warned = warning_at (input_location, OPT_Wshadow,
1186 "declaration of %q#D shadows a parameter", x);
1187 else if (is_capture_proxy (oldlocal))
1188 warned = warning_at (input_location, OPT_Wshadow,
1189 "declaration of %qD shadows a lambda capture",
1191 else
1192 warned = warning_at (input_location, OPT_Wshadow,
1193 "declaration of %qD shadows a previous local",
1196 if (warned)
1197 inform (DECL_SOURCE_LOCATION (oldlocal),
1198 "shadowed declaration is here");
1202 /* Maybe warn if shadowing something else. */
1203 else if (warn_shadow && !DECL_EXTERNAL (x)
1204 /* No shadow warnings for internally generated vars unless
1205 it's an implicit typedef (see create_implicit_typedef
1206 in decl.c). */
1207 && (! DECL_ARTIFICIAL (x) || DECL_IMPLICIT_TYPEDEF_P (x))
1208 /* No shadow warnings for vars made for inlining. */
1209 && ! DECL_FROM_INLINE (x))
1211 tree member;
1213 if (nonlambda_method_basetype ())
1214 member = lookup_member (current_nonlambda_class_type (),
1215 name,
1216 /*protect=*/0,
1217 /*want_type=*/false,
1218 tf_warning_or_error);
1219 else
1220 member = NULL_TREE;
1222 if (member && !TREE_STATIC (member))
1224 /* Location of previous decl is not useful in this case. */
1225 warning (OPT_Wshadow, "declaration of %qD shadows a member of 'this'",
1228 else if (oldglobal != NULL_TREE
1229 && (VAR_P (oldglobal)
1230 /* If the old decl is a type decl, only warn if the
1231 old decl is an explicit typedef or if both the
1232 old and new decls are type decls. */
1233 || (TREE_CODE (oldglobal) == TYPE_DECL
1234 && (!DECL_ARTIFICIAL (oldglobal)
1235 || TREE_CODE (x) == TYPE_DECL))))
1236 /* XXX shadow warnings in outer-more namespaces */
1238 if (warning_at (input_location, OPT_Wshadow,
1239 "declaration of %qD shadows a "
1240 "global declaration", x))
1241 inform (DECL_SOURCE_LOCATION (oldglobal),
1242 "shadowed declaration is here");
1247 if (VAR_P (x))
1248 maybe_register_incomplete_var (x);
1251 if (need_new_binding)
1252 add_decl_to_level (x,
1253 DECL_NAMESPACE_SCOPE_P (x)
1254 ? NAMESPACE_LEVEL (CP_DECL_CONTEXT (x))
1255 : current_binding_level);
1257 return x;
1260 /* Wrapper for pushdecl_maybe_friend_1. */
1262 tree
1263 pushdecl_maybe_friend (tree x, bool is_friend)
1265 tree ret;
1266 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
1267 ret = pushdecl_maybe_friend_1 (x, is_friend);
1268 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
1269 return ret;
1272 /* Record a decl-node X as belonging to the current lexical scope. */
1274 tree
1275 pushdecl (tree x)
1277 return pushdecl_maybe_friend (x, false);
1280 /* Enter DECL into the symbol table, if that's appropriate. Returns
1281 DECL, or a modified version thereof. */
1283 tree
1284 maybe_push_decl (tree decl)
1286 tree type = TREE_TYPE (decl);
1288 /* Add this decl to the current binding level, but not if it comes
1289 from another scope, e.g. a static member variable. TEM may equal
1290 DECL or it may be a previous decl of the same name. */
1291 if (decl == error_mark_node
1292 || (TREE_CODE (decl) != PARM_DECL
1293 && DECL_CONTEXT (decl) != NULL_TREE
1294 /* Definitions of namespace members outside their namespace are
1295 possible. */
1296 && !DECL_NAMESPACE_SCOPE_P (decl))
1297 || (TREE_CODE (decl) == TEMPLATE_DECL && !namespace_bindings_p ())
1298 || type == unknown_type_node
1299 /* The declaration of a template specialization does not affect
1300 the functions available for overload resolution, so we do not
1301 call pushdecl. */
1302 || (TREE_CODE (decl) == FUNCTION_DECL
1303 && DECL_TEMPLATE_SPECIALIZATION (decl)))
1304 return decl;
1305 else
1306 return pushdecl (decl);
1309 /* Bind DECL to ID in the current_binding_level, assumed to be a local
1310 binding level. If PUSH_USING is set in FLAGS, we know that DECL
1311 doesn't really belong to this binding level, that it got here
1312 through a using-declaration. */
1314 void
1315 push_local_binding (tree id, tree decl, int flags)
1317 cp_binding_level *b;
1319 /* Skip over any local classes. This makes sense if we call
1320 push_local_binding with a friend decl of a local class. */
1321 b = innermost_nonclass_level ();
1323 if (lookup_name_innermost_nonclass_level (id))
1325 /* Supplement the existing binding. */
1326 if (!supplement_binding (IDENTIFIER_BINDING (id), decl))
1327 /* It didn't work. Something else must be bound at this
1328 level. Do not add DECL to the list of things to pop
1329 later. */
1330 return;
1332 else
1333 /* Create a new binding. */
1334 push_binding (id, decl, b);
1336 if (TREE_CODE (decl) == OVERLOAD || (flags & PUSH_USING))
1337 /* We must put the OVERLOAD into a TREE_LIST since the
1338 TREE_CHAIN of an OVERLOAD is already used. Similarly for
1339 decls that got here through a using-declaration. */
1340 decl = build_tree_list (NULL_TREE, decl);
1342 /* And put DECL on the list of things declared by the current
1343 binding level. */
1344 add_decl_to_level (decl, b);
1347 /* Check to see whether or not DECL is a variable that would have been
1348 in scope under the ARM, but is not in scope under the ANSI/ISO
1349 standard. If so, issue an error message. If name lookup would
1350 work in both cases, but return a different result, this function
1351 returns the result of ANSI/ISO lookup. Otherwise, it returns
1352 DECL. */
1354 tree
1355 check_for_out_of_scope_variable (tree decl)
1357 tree shadowed;
1359 /* We only care about out of scope variables. */
1360 if (!(VAR_P (decl) && DECL_DEAD_FOR_LOCAL (decl)))
1361 return decl;
1363 shadowed = DECL_HAS_SHADOWED_FOR_VAR_P (decl)
1364 ? DECL_SHADOWED_FOR_VAR (decl) : NULL_TREE ;
1365 while (shadowed != NULL_TREE && VAR_P (shadowed)
1366 && DECL_DEAD_FOR_LOCAL (shadowed))
1367 shadowed = DECL_HAS_SHADOWED_FOR_VAR_P (shadowed)
1368 ? DECL_SHADOWED_FOR_VAR (shadowed) : NULL_TREE;
1369 if (!shadowed)
1370 shadowed = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (decl));
1371 if (shadowed)
1373 if (!DECL_ERROR_REPORTED (decl))
1375 warning (0, "name lookup of %qD changed", DECL_NAME (decl));
1376 warning (0, " matches this %q+D under ISO standard rules",
1377 shadowed);
1378 warning (0, " matches this %q+D under old rules", decl);
1379 DECL_ERROR_REPORTED (decl) = 1;
1381 return shadowed;
1384 /* If we have already complained about this declaration, there's no
1385 need to do it again. */
1386 if (DECL_ERROR_REPORTED (decl))
1387 return decl;
1389 DECL_ERROR_REPORTED (decl) = 1;
1391 if (TREE_TYPE (decl) == error_mark_node)
1392 return decl;
1394 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
1396 error ("name lookup of %qD changed for ISO %<for%> scoping",
1397 DECL_NAME (decl));
1398 error (" cannot use obsolete binding at %q+D because "
1399 "it has a destructor", decl);
1400 return error_mark_node;
1402 else
1404 permerror (input_location, "name lookup of %qD changed for ISO %<for%> scoping",
1405 DECL_NAME (decl));
1406 if (flag_permissive)
1407 permerror (input_location, " using obsolete binding at %q+D", decl);
1408 else
1410 static bool hint;
1411 if (!hint)
1413 inform (input_location, "(if you use %<-fpermissive%> G++ will accept your code)");
1414 hint = true;
1419 return decl;
1422 /* true means unconditionally make a BLOCK for the next level pushed. */
1424 static bool keep_next_level_flag;
1426 static int binding_depth = 0;
1428 static void
1429 indent (int depth)
1431 int i;
1433 for (i = 0; i < depth * 2; i++)
1434 putc (' ', stderr);
1437 /* Return a string describing the kind of SCOPE we have. */
1438 static const char *
1439 cp_binding_level_descriptor (cp_binding_level *scope)
1441 /* The order of this table must match the "scope_kind"
1442 enumerators. */
1443 static const char* scope_kind_names[] = {
1444 "block-scope",
1445 "cleanup-scope",
1446 "try-scope",
1447 "catch-scope",
1448 "for-scope",
1449 "function-parameter-scope",
1450 "class-scope",
1451 "namespace-scope",
1452 "template-parameter-scope",
1453 "template-explicit-spec-scope"
1455 const scope_kind kind = scope->explicit_spec_p
1456 ? sk_template_spec : scope->kind;
1458 return scope_kind_names[kind];
1461 /* Output a debugging information about SCOPE when performing
1462 ACTION at LINE. */
1463 static void
1464 cp_binding_level_debug (cp_binding_level *scope, int line, const char *action)
1466 const char *desc = cp_binding_level_descriptor (scope);
1467 if (scope->this_entity)
1468 verbatim ("%s %s(%E) %p %d\n", action, desc,
1469 scope->this_entity, (void *) scope, line);
1470 else
1471 verbatim ("%s %s %p %d\n", action, desc, (void *) scope, line);
1474 /* Return the estimated initial size of the hashtable of a NAMESPACE
1475 scope. */
1477 static inline size_t
1478 namespace_scope_ht_size (tree ns)
1480 tree name = DECL_NAME (ns);
1482 return name == std_identifier
1483 ? NAMESPACE_STD_HT_SIZE
1484 : (name == global_scope_name
1485 ? GLOBAL_SCOPE_HT_SIZE
1486 : NAMESPACE_ORDINARY_HT_SIZE);
1489 /* A chain of binding_level structures awaiting reuse. */
1491 static GTY((deletable)) cp_binding_level *free_binding_level;
1493 /* Insert SCOPE as the innermost binding level. */
1495 void
1496 push_binding_level (cp_binding_level *scope)
1498 /* Add it to the front of currently active scopes stack. */
1499 scope->level_chain = current_binding_level;
1500 current_binding_level = scope;
1501 keep_next_level_flag = false;
1503 if (ENABLE_SCOPE_CHECKING)
1505 scope->binding_depth = binding_depth;
1506 indent (binding_depth);
1507 cp_binding_level_debug (scope, input_line, "push");
1508 binding_depth++;
1512 /* Create a new KIND scope and make it the top of the active scopes stack.
1513 ENTITY is the scope of the associated C++ entity (namespace, class,
1514 function, C++0x enumeration); it is NULL otherwise. */
1516 cp_binding_level *
1517 begin_scope (scope_kind kind, tree entity)
1519 cp_binding_level *scope;
1521 /* Reuse or create a struct for this binding level. */
1522 if (!ENABLE_SCOPE_CHECKING && free_binding_level)
1524 scope = free_binding_level;
1525 memset (scope, 0, sizeof (cp_binding_level));
1526 free_binding_level = scope->level_chain;
1528 else
1529 scope = ggc_alloc_cleared_cp_binding_level ();
1531 scope->this_entity = entity;
1532 scope->more_cleanups_ok = true;
1533 switch (kind)
1535 case sk_cleanup:
1536 scope->keep = true;
1537 break;
1539 case sk_template_spec:
1540 scope->explicit_spec_p = true;
1541 kind = sk_template_parms;
1542 /* Fall through. */
1543 case sk_template_parms:
1544 case sk_block:
1545 case sk_try:
1546 case sk_catch:
1547 case sk_for:
1548 case sk_cond:
1549 case sk_class:
1550 case sk_scoped_enum:
1551 case sk_function_parms:
1552 case sk_omp:
1553 scope->keep = keep_next_level_flag;
1554 break;
1556 case sk_namespace:
1557 NAMESPACE_LEVEL (entity) = scope;
1558 vec_alloc (scope->static_decls,
1559 (DECL_NAME (entity) == std_identifier
1560 || DECL_NAME (entity) == global_scope_name) ? 200 : 10);
1561 break;
1563 default:
1564 /* Should not happen. */
1565 gcc_unreachable ();
1566 break;
1568 scope->kind = kind;
1570 push_binding_level (scope);
1572 return scope;
1575 /* We're about to leave current scope. Pop the top of the stack of
1576 currently active scopes. Return the enclosing scope, now active. */
1578 cp_binding_level *
1579 leave_scope (void)
1581 cp_binding_level *scope = current_binding_level;
1583 if (scope->kind == sk_namespace && class_binding_level)
1584 current_binding_level = class_binding_level;
1586 /* We cannot leave a scope, if there are none left. */
1587 if (NAMESPACE_LEVEL (global_namespace))
1588 gcc_assert (!global_scope_p (scope));
1590 if (ENABLE_SCOPE_CHECKING)
1592 indent (--binding_depth);
1593 cp_binding_level_debug (scope, input_line, "leave");
1596 /* Move one nesting level up. */
1597 current_binding_level = scope->level_chain;
1599 /* Namespace-scopes are left most probably temporarily, not
1600 completely; they can be reopened later, e.g. in namespace-extension
1601 or any name binding activity that requires us to resume a
1602 namespace. For classes, we cache some binding levels. For other
1603 scopes, we just make the structure available for reuse. */
1604 if (scope->kind != sk_namespace
1605 && scope->kind != sk_class)
1607 scope->level_chain = free_binding_level;
1608 gcc_assert (!ENABLE_SCOPE_CHECKING
1609 || scope->binding_depth == binding_depth);
1610 free_binding_level = scope;
1613 /* Find the innermost enclosing class scope, and reset
1614 CLASS_BINDING_LEVEL appropriately. */
1615 if (scope->kind == sk_class)
1617 class_binding_level = NULL;
1618 for (scope = current_binding_level; scope; scope = scope->level_chain)
1619 if (scope->kind == sk_class)
1621 class_binding_level = scope;
1622 break;
1626 return current_binding_level;
1629 static void
1630 resume_scope (cp_binding_level* b)
1632 /* Resuming binding levels is meant only for namespaces,
1633 and those cannot nest into classes. */
1634 gcc_assert (!class_binding_level);
1635 /* Also, resuming a non-directly nested namespace is a no-no. */
1636 gcc_assert (b->level_chain == current_binding_level);
1637 current_binding_level = b;
1638 if (ENABLE_SCOPE_CHECKING)
1640 b->binding_depth = binding_depth;
1641 indent (binding_depth);
1642 cp_binding_level_debug (b, input_line, "resume");
1643 binding_depth++;
1647 /* Return the innermost binding level that is not for a class scope. */
1649 static cp_binding_level *
1650 innermost_nonclass_level (void)
1652 cp_binding_level *b;
1654 b = current_binding_level;
1655 while (b->kind == sk_class)
1656 b = b->level_chain;
1658 return b;
1661 /* We're defining an object of type TYPE. If it needs a cleanup, but
1662 we're not allowed to add any more objects with cleanups to the current
1663 scope, create a new binding level. */
1665 void
1666 maybe_push_cleanup_level (tree type)
1668 if (type != error_mark_node
1669 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
1670 && current_binding_level->more_cleanups_ok == 0)
1672 begin_scope (sk_cleanup, NULL);
1673 current_binding_level->statement_list = push_stmt_list ();
1677 /* Return true if we are in the global binding level. */
1679 bool
1680 global_bindings_p (void)
1682 return global_scope_p (current_binding_level);
1685 /* True if we are currently in a toplevel binding level. This
1686 means either the global binding level or a namespace in a toplevel
1687 binding level. Since there are no non-toplevel namespace levels,
1688 this really means any namespace or template parameter level. We
1689 also include a class whose context is toplevel. */
1691 bool
1692 toplevel_bindings_p (void)
1694 cp_binding_level *b = innermost_nonclass_level ();
1696 return b->kind == sk_namespace || b->kind == sk_template_parms;
1699 /* True if this is a namespace scope, or if we are defining a class
1700 which is itself at namespace scope, or whose enclosing class is
1701 such a class, etc. */
1703 bool
1704 namespace_bindings_p (void)
1706 cp_binding_level *b = innermost_nonclass_level ();
1708 return b->kind == sk_namespace;
1711 /* True if the innermost non-class scope is a block scope. */
1713 bool
1714 local_bindings_p (void)
1716 cp_binding_level *b = innermost_nonclass_level ();
1717 return b->kind < sk_function_parms || b->kind == sk_omp;
1720 /* True if the current level needs to have a BLOCK made. */
1722 bool
1723 kept_level_p (void)
1725 return (current_binding_level->blocks != NULL_TREE
1726 || current_binding_level->keep
1727 || current_binding_level->kind == sk_cleanup
1728 || current_binding_level->names != NULL_TREE
1729 || current_binding_level->using_directives);
1732 /* Returns the kind of the innermost scope. */
1734 scope_kind
1735 innermost_scope_kind (void)
1737 return current_binding_level->kind;
1740 /* Returns true if this scope was created to store template parameters. */
1742 bool
1743 template_parm_scope_p (void)
1745 return innermost_scope_kind () == sk_template_parms;
1748 /* If KEEP is true, make a BLOCK node for the next binding level,
1749 unconditionally. Otherwise, use the normal logic to decide whether
1750 or not to create a BLOCK. */
1752 void
1753 keep_next_level (bool keep)
1755 keep_next_level_flag = keep;
1758 /* Return the list of declarations of the current level.
1759 Note that this list is in reverse order unless/until
1760 you nreverse it; and when you do nreverse it, you must
1761 store the result back using `storedecls' or you will lose. */
1763 tree
1764 getdecls (void)
1766 return current_binding_level->names;
1769 /* Return how many function prototypes we are currently nested inside. */
1772 function_parm_depth (void)
1774 int level = 0;
1775 cp_binding_level *b;
1777 for (b = current_binding_level;
1778 b->kind == sk_function_parms;
1779 b = b->level_chain)
1780 ++level;
1782 return level;
1785 /* For debugging. */
1786 static int no_print_functions = 0;
1787 static int no_print_builtins = 0;
1789 static void
1790 print_binding_level (cp_binding_level* lvl)
1792 tree t;
1793 int i = 0, len;
1794 fprintf (stderr, " blocks=%p", (void *) lvl->blocks);
1795 if (lvl->more_cleanups_ok)
1796 fprintf (stderr, " more-cleanups-ok");
1797 if (lvl->have_cleanups)
1798 fprintf (stderr, " have-cleanups");
1799 fprintf (stderr, "\n");
1800 if (lvl->names)
1802 fprintf (stderr, " names:\t");
1803 /* We can probably fit 3 names to a line? */
1804 for (t = lvl->names; t; t = TREE_CHAIN (t))
1806 if (no_print_functions && (TREE_CODE (t) == FUNCTION_DECL))
1807 continue;
1808 if (no_print_builtins
1809 && (TREE_CODE (t) == TYPE_DECL)
1810 && DECL_IS_BUILTIN (t))
1811 continue;
1813 /* Function decls tend to have longer names. */
1814 if (TREE_CODE (t) == FUNCTION_DECL)
1815 len = 3;
1816 else
1817 len = 2;
1818 i += len;
1819 if (i > 6)
1821 fprintf (stderr, "\n\t");
1822 i = len;
1824 print_node_brief (stderr, "", t, 0);
1825 if (t == error_mark_node)
1826 break;
1828 if (i)
1829 fprintf (stderr, "\n");
1831 if (vec_safe_length (lvl->class_shadowed))
1833 size_t i;
1834 cp_class_binding *b;
1835 fprintf (stderr, " class-shadowed:");
1836 FOR_EACH_VEC_ELT (*lvl->class_shadowed, i, b)
1837 fprintf (stderr, " %s ", IDENTIFIER_POINTER (b->identifier));
1838 fprintf (stderr, "\n");
1840 if (lvl->type_shadowed)
1842 fprintf (stderr, " type-shadowed:");
1843 for (t = lvl->type_shadowed; t; t = TREE_CHAIN (t))
1845 fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
1847 fprintf (stderr, "\n");
1851 DEBUG_FUNCTION void
1852 debug (cp_binding_level &ref)
1854 print_binding_level (&ref);
1857 DEBUG_FUNCTION void
1858 debug (cp_binding_level *ptr)
1860 if (ptr)
1861 debug (*ptr);
1862 else
1863 fprintf (stderr, "<nil>\n");
1867 void
1868 print_other_binding_stack (cp_binding_level *stack)
1870 cp_binding_level *level;
1871 for (level = stack; !global_scope_p (level); level = level->level_chain)
1873 fprintf (stderr, "binding level %p\n", (void *) level);
1874 print_binding_level (level);
1878 void
1879 print_binding_stack (void)
1881 cp_binding_level *b;
1882 fprintf (stderr, "current_binding_level=%p\n"
1883 "class_binding_level=%p\n"
1884 "NAMESPACE_LEVEL (global_namespace)=%p\n",
1885 (void *) current_binding_level, (void *) class_binding_level,
1886 (void *) NAMESPACE_LEVEL (global_namespace));
1887 if (class_binding_level)
1889 for (b = class_binding_level; b; b = b->level_chain)
1890 if (b == current_binding_level)
1891 break;
1892 if (b)
1893 b = class_binding_level;
1894 else
1895 b = current_binding_level;
1897 else
1898 b = current_binding_level;
1899 print_other_binding_stack (b);
1900 fprintf (stderr, "global:\n");
1901 print_binding_level (NAMESPACE_LEVEL (global_namespace));
1904 /* Return the type associated with ID. */
1906 static tree
1907 identifier_type_value_1 (tree id)
1909 /* There is no type with that name, anywhere. */
1910 if (REAL_IDENTIFIER_TYPE_VALUE (id) == NULL_TREE)
1911 return NULL_TREE;
1912 /* This is not the type marker, but the real thing. */
1913 if (REAL_IDENTIFIER_TYPE_VALUE (id) != global_type_node)
1914 return REAL_IDENTIFIER_TYPE_VALUE (id);
1915 /* Have to search for it. It must be on the global level, now.
1916 Ask lookup_name not to return non-types. */
1917 id = lookup_name_real (id, 2, 1, /*block_p=*/true, 0, 0);
1918 if (id)
1919 return TREE_TYPE (id);
1920 return NULL_TREE;
1923 /* Wrapper for identifier_type_value_1. */
1925 tree
1926 identifier_type_value (tree id)
1928 tree ret;
1929 timevar_start (TV_NAME_LOOKUP);
1930 ret = identifier_type_value_1 (id);
1931 timevar_stop (TV_NAME_LOOKUP);
1932 return ret;
1936 /* Return the IDENTIFIER_GLOBAL_VALUE of T, for use in common code, since
1937 the definition of IDENTIFIER_GLOBAL_VALUE is different for C and C++. */
1939 tree
1940 identifier_global_value (tree t)
1942 return IDENTIFIER_GLOBAL_VALUE (t);
1945 /* Push a definition of struct, union or enum tag named ID. into
1946 binding_level B. DECL is a TYPE_DECL for the type. We assume that
1947 the tag ID is not already defined. */
1949 static void
1950 set_identifier_type_value_with_scope (tree id, tree decl, cp_binding_level *b)
1952 tree type;
1954 if (b->kind != sk_namespace)
1956 /* Shadow the marker, not the real thing, so that the marker
1957 gets restored later. */
1958 tree old_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
1959 b->type_shadowed
1960 = tree_cons (id, old_type_value, b->type_shadowed);
1961 type = decl ? TREE_TYPE (decl) : NULL_TREE;
1962 TREE_TYPE (b->type_shadowed) = type;
1964 else
1966 cxx_binding *binding =
1967 binding_for_name (NAMESPACE_LEVEL (current_namespace), id);
1968 gcc_assert (decl);
1969 if (binding->value)
1970 supplement_binding (binding, decl);
1971 else
1972 binding->value = decl;
1974 /* Store marker instead of real type. */
1975 type = global_type_node;
1977 SET_IDENTIFIER_TYPE_VALUE (id, type);
1980 /* As set_identifier_type_value_with_scope, but using
1981 current_binding_level. */
1983 void
1984 set_identifier_type_value (tree id, tree decl)
1986 set_identifier_type_value_with_scope (id, decl, current_binding_level);
1989 /* Return the name for the constructor (or destructor) for the
1990 specified class TYPE. When given a template, this routine doesn't
1991 lose the specialization. */
1993 static inline tree
1994 constructor_name_full (tree type)
1996 return TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
1999 /* Return the name for the constructor (or destructor) for the
2000 specified class. When given a template, return the plain
2001 unspecialized name. */
2003 tree
2004 constructor_name (tree type)
2006 tree name;
2007 name = constructor_name_full (type);
2008 if (IDENTIFIER_TEMPLATE (name))
2009 name = IDENTIFIER_TEMPLATE (name);
2010 return name;
2013 /* Returns TRUE if NAME is the name for the constructor for TYPE,
2014 which must be a class type. */
2016 bool
2017 constructor_name_p (tree name, tree type)
2019 tree ctor_name;
2021 gcc_assert (MAYBE_CLASS_TYPE_P (type));
2023 if (!name)
2024 return false;
2026 if (!identifier_p (name))
2027 return false;
2029 /* These don't have names. */
2030 if (TREE_CODE (type) == DECLTYPE_TYPE
2031 || TREE_CODE (type) == TYPEOF_TYPE)
2032 return false;
2034 ctor_name = constructor_name_full (type);
2035 if (name == ctor_name)
2036 return true;
2037 if (IDENTIFIER_TEMPLATE (ctor_name)
2038 && name == IDENTIFIER_TEMPLATE (ctor_name))
2039 return true;
2040 return false;
2043 /* Counter used to create anonymous type names. */
2045 static GTY(()) int anon_cnt;
2047 /* Return an IDENTIFIER which can be used as a name for
2048 anonymous structs and unions. */
2050 tree
2051 make_anon_name (void)
2053 char buf[32];
2055 sprintf (buf, ANON_AGGRNAME_FORMAT, anon_cnt++);
2056 return get_identifier (buf);
2059 /* This code is practically identical to that for creating
2060 anonymous names, but is just used for lambdas instead. This isn't really
2061 necessary, but it's convenient to avoid treating lambdas like other
2062 anonymous types. */
2064 static GTY(()) int lambda_cnt = 0;
2066 tree
2067 make_lambda_name (void)
2069 char buf[32];
2071 sprintf (buf, LAMBDANAME_FORMAT, lambda_cnt++);
2072 return get_identifier (buf);
2075 /* Return (from the stack of) the BINDING, if any, established at SCOPE. */
2077 static inline cxx_binding *
2078 find_binding (cp_binding_level *scope, cxx_binding *binding)
2080 for (; binding != NULL; binding = binding->previous)
2081 if (binding->scope == scope)
2082 return binding;
2084 return (cxx_binding *)0;
2087 /* Return the binding for NAME in SCOPE, if any. Otherwise, return NULL. */
2089 static inline cxx_binding *
2090 cp_binding_level_find_binding_for_name (cp_binding_level *scope, tree name)
2092 cxx_binding *b = IDENTIFIER_NAMESPACE_BINDINGS (name);
2093 if (b)
2095 /* Fold-in case where NAME is used only once. */
2096 if (scope == b->scope && b->previous == NULL)
2097 return b;
2098 return find_binding (scope, b);
2100 return NULL;
2103 /* Always returns a binding for name in scope. If no binding is
2104 found, make a new one. */
2106 static cxx_binding *
2107 binding_for_name (cp_binding_level *scope, tree name)
2109 cxx_binding *result;
2111 result = cp_binding_level_find_binding_for_name (scope, name);
2112 if (result)
2113 return result;
2114 /* Not found, make a new one. */
2115 result = cxx_binding_make (NULL, NULL);
2116 result->previous = IDENTIFIER_NAMESPACE_BINDINGS (name);
2117 result->scope = scope;
2118 result->is_local = false;
2119 result->value_is_inherited = false;
2120 IDENTIFIER_NAMESPACE_BINDINGS (name) = result;
2121 return result;
2124 /* Walk through the bindings associated to the name of FUNCTION,
2125 and return the first declaration of a function with a
2126 "C" linkage specification, a.k.a 'extern "C"'.
2127 This function looks for the binding, regardless of which scope it
2128 has been defined in. It basically looks in all the known scopes.
2129 Note that this function does not lookup for bindings of builtin functions
2130 or for functions declared in system headers. */
2131 static tree
2132 lookup_extern_c_fun_in_all_ns (tree function)
2134 tree name;
2135 cxx_binding *iter;
2137 gcc_assert (function && TREE_CODE (function) == FUNCTION_DECL);
2139 name = DECL_NAME (function);
2140 gcc_assert (name && identifier_p (name));
2142 for (iter = IDENTIFIER_NAMESPACE_BINDINGS (name);
2143 iter;
2144 iter = iter->previous)
2146 tree ovl;
2147 for (ovl = iter->value; ovl; ovl = OVL_NEXT (ovl))
2149 tree decl = OVL_CURRENT (ovl);
2150 if (decl
2151 && TREE_CODE (decl) == FUNCTION_DECL
2152 && DECL_EXTERN_C_P (decl)
2153 && !DECL_ARTIFICIAL (decl))
2155 return decl;
2159 return NULL;
2162 /* Returns a list of C-linkage decls with the name NAME. */
2164 tree
2165 c_linkage_bindings (tree name)
2167 tree decls = NULL_TREE;
2168 cxx_binding *iter;
2170 for (iter = IDENTIFIER_NAMESPACE_BINDINGS (name);
2171 iter;
2172 iter = iter->previous)
2174 tree ovl;
2175 for (ovl = iter->value; ovl; ovl = OVL_NEXT (ovl))
2177 tree decl = OVL_CURRENT (ovl);
2178 if (decl
2179 && DECL_EXTERN_C_P (decl)
2180 && !DECL_ARTIFICIAL (decl))
2182 if (decls == NULL_TREE)
2183 decls = decl;
2184 else
2185 decls = tree_cons (NULL_TREE, decl, decls);
2189 return decls;
2192 /* Insert another USING_DECL into the current binding level, returning
2193 this declaration. If this is a redeclaration, do nothing, and
2194 return NULL_TREE if this not in namespace scope (in namespace
2195 scope, a using decl might extend any previous bindings). */
2197 static tree
2198 push_using_decl_1 (tree scope, tree name)
2200 tree decl;
2202 gcc_assert (TREE_CODE (scope) == NAMESPACE_DECL);
2203 gcc_assert (identifier_p (name));
2204 for (decl = current_binding_level->usings; decl; decl = DECL_CHAIN (decl))
2205 if (USING_DECL_SCOPE (decl) == scope && DECL_NAME (decl) == name)
2206 break;
2207 if (decl)
2208 return namespace_bindings_p () ? decl : NULL_TREE;
2209 decl = build_lang_decl (USING_DECL, name, NULL_TREE);
2210 USING_DECL_SCOPE (decl) = scope;
2211 DECL_CHAIN (decl) = current_binding_level->usings;
2212 current_binding_level->usings = decl;
2213 return decl;
2216 /* Wrapper for push_using_decl_1. */
2218 static tree
2219 push_using_decl (tree scope, tree name)
2221 tree ret;
2222 timevar_start (TV_NAME_LOOKUP);
2223 ret = push_using_decl_1 (scope, name);
2224 timevar_stop (TV_NAME_LOOKUP);
2225 return ret;
2228 /* Same as pushdecl, but define X in binding-level LEVEL. We rely on the
2229 caller to set DECL_CONTEXT properly.
2231 Note that this must only be used when X will be the new innermost
2232 binding for its name, as we tack it onto the front of IDENTIFIER_BINDING
2233 without checking to see if the current IDENTIFIER_BINDING comes from a
2234 closer binding level than LEVEL. */
2236 static tree
2237 pushdecl_with_scope_1 (tree x, cp_binding_level *level, bool is_friend)
2239 cp_binding_level *b;
2240 tree function_decl = current_function_decl;
2242 current_function_decl = NULL_TREE;
2243 if (level->kind == sk_class)
2245 b = class_binding_level;
2246 class_binding_level = level;
2247 pushdecl_class_level (x);
2248 class_binding_level = b;
2250 else
2252 b = current_binding_level;
2253 current_binding_level = level;
2254 x = pushdecl_maybe_friend (x, is_friend);
2255 current_binding_level = b;
2257 current_function_decl = function_decl;
2258 return x;
2261 /* Wrapper for pushdecl_with_scope_1. */
2263 tree
2264 pushdecl_with_scope (tree x, cp_binding_level *level, bool is_friend)
2266 tree ret;
2267 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
2268 ret = pushdecl_with_scope_1 (x, level, is_friend);
2269 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
2270 return ret;
2273 /* Helper function for push_overloaded_decl_1 and do_nonmember_using_decl.
2274 Compares the parameter-type-lists of DECL1 and DECL2 and returns false
2275 if they are different. If the DECLs are template functions, the return
2276 types and the template parameter lists are compared too (DR 565). */
2278 static bool
2279 compparms_for_decl_and_using_decl (tree decl1, tree decl2)
2281 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (decl1)),
2282 TYPE_ARG_TYPES (TREE_TYPE (decl2))))
2283 return false;
2285 if (! DECL_FUNCTION_TEMPLATE_P (decl1)
2286 || ! DECL_FUNCTION_TEMPLATE_P (decl2))
2287 return true;
2289 return (comp_template_parms (DECL_TEMPLATE_PARMS (decl1),
2290 DECL_TEMPLATE_PARMS (decl2))
2291 && same_type_p (TREE_TYPE (TREE_TYPE (decl1)),
2292 TREE_TYPE (TREE_TYPE (decl2))));
2295 /* DECL is a FUNCTION_DECL for a non-member function, which may have
2296 other definitions already in place. We get around this by making
2297 the value of the identifier point to a list of all the things that
2298 want to be referenced by that name. It is then up to the users of
2299 that name to decide what to do with that list.
2301 DECL may also be a TEMPLATE_DECL, with a FUNCTION_DECL in its
2302 DECL_TEMPLATE_RESULT. It is dealt with the same way.
2304 FLAGS is a bitwise-or of the following values:
2305 PUSH_LOCAL: Bind DECL in the current scope, rather than at
2306 namespace scope.
2307 PUSH_USING: DECL is being pushed as the result of a using
2308 declaration.
2310 IS_FRIEND is true if this is a friend declaration.
2312 The value returned may be a previous declaration if we guessed wrong
2313 about what language DECL should belong to (C or C++). Otherwise,
2314 it's always DECL (and never something that's not a _DECL). */
2316 static tree
2317 push_overloaded_decl_1 (tree decl, int flags, bool is_friend)
2319 tree name = DECL_NAME (decl);
2320 tree old;
2321 tree new_binding;
2322 int doing_global = (namespace_bindings_p () || !(flags & PUSH_LOCAL));
2324 if (doing_global)
2325 old = namespace_binding (name, DECL_CONTEXT (decl));
2326 else
2327 old = lookup_name_innermost_nonclass_level (name);
2329 if (old)
2331 if (TREE_CODE (old) == TYPE_DECL && DECL_ARTIFICIAL (old))
2333 tree t = TREE_TYPE (old);
2334 if (MAYBE_CLASS_TYPE_P (t) && warn_shadow
2335 && (! DECL_IN_SYSTEM_HEADER (decl)
2336 || ! DECL_IN_SYSTEM_HEADER (old)))
2337 warning (OPT_Wshadow, "%q#D hides constructor for %q#T", decl, t);
2338 old = NULL_TREE;
2340 else if (is_overloaded_fn (old))
2342 tree tmp;
2344 for (tmp = old; tmp; tmp = OVL_NEXT (tmp))
2346 tree fn = OVL_CURRENT (tmp);
2347 tree dup;
2349 if (TREE_CODE (tmp) == OVERLOAD && OVL_USED (tmp)
2350 && !(flags & PUSH_USING)
2351 && compparms_for_decl_and_using_decl (fn, decl)
2352 && ! decls_match (fn, decl))
2353 diagnose_name_conflict (decl, fn);
2355 dup = duplicate_decls (decl, fn, is_friend);
2356 /* If DECL was a redeclaration of FN -- even an invalid
2357 one -- pass that information along to our caller. */
2358 if (dup == fn || dup == error_mark_node)
2359 return dup;
2362 /* We don't overload implicit built-ins. duplicate_decls()
2363 may fail to merge the decls if the new decl is e.g. a
2364 template function. */
2365 if (TREE_CODE (old) == FUNCTION_DECL
2366 && DECL_ANTICIPATED (old)
2367 && !DECL_HIDDEN_FRIEND_P (old))
2368 old = NULL;
2370 else if (old == error_mark_node)
2371 /* Ignore the undefined symbol marker. */
2372 old = NULL_TREE;
2373 else
2375 error ("previous non-function declaration %q+#D", old);
2376 error ("conflicts with function declaration %q#D", decl);
2377 return decl;
2381 if (old || TREE_CODE (decl) == TEMPLATE_DECL
2382 /* If it's a using declaration, we always need to build an OVERLOAD,
2383 because it's the only way to remember that the declaration comes
2384 from 'using', and have the lookup behave correctly. */
2385 || (flags & PUSH_USING))
2387 if (old && TREE_CODE (old) != OVERLOAD)
2388 new_binding = ovl_cons (decl, ovl_cons (old, NULL_TREE));
2389 else
2390 new_binding = ovl_cons (decl, old);
2391 if (flags & PUSH_USING)
2392 OVL_USED (new_binding) = 1;
2394 else
2395 /* NAME is not ambiguous. */
2396 new_binding = decl;
2398 if (doing_global)
2399 set_namespace_binding (name, current_namespace, new_binding);
2400 else
2402 /* We only create an OVERLOAD if there was a previous binding at
2403 this level, or if decl is a template. In the former case, we
2404 need to remove the old binding and replace it with the new
2405 binding. We must also run through the NAMES on the binding
2406 level where the name was bound to update the chain. */
2408 if (TREE_CODE (new_binding) == OVERLOAD && old)
2410 tree *d;
2412 for (d = &IDENTIFIER_BINDING (name)->scope->names;
2414 d = &TREE_CHAIN (*d))
2415 if (*d == old
2416 || (TREE_CODE (*d) == TREE_LIST
2417 && TREE_VALUE (*d) == old))
2419 if (TREE_CODE (*d) == TREE_LIST)
2420 /* Just replace the old binding with the new. */
2421 TREE_VALUE (*d) = new_binding;
2422 else
2423 /* Build a TREE_LIST to wrap the OVERLOAD. */
2424 *d = tree_cons (NULL_TREE, new_binding,
2425 TREE_CHAIN (*d));
2427 /* And update the cxx_binding node. */
2428 IDENTIFIER_BINDING (name)->value = new_binding;
2429 return decl;
2432 /* We should always find a previous binding in this case. */
2433 gcc_unreachable ();
2436 /* Install the new binding. */
2437 push_local_binding (name, new_binding, flags);
2440 return decl;
2443 /* Wrapper for push_overloaded_decl_1. */
2445 static tree
2446 push_overloaded_decl (tree decl, int flags, bool is_friend)
2448 tree ret;
2449 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
2450 ret = push_overloaded_decl_1 (decl, flags, is_friend);
2451 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
2452 return ret;
2455 /* Check a non-member using-declaration. Return the name and scope
2456 being used, and the USING_DECL, or NULL_TREE on failure. */
2458 static tree
2459 validate_nonmember_using_decl (tree decl, tree scope, tree name)
2461 /* [namespace.udecl]
2462 A using-declaration for a class member shall be a
2463 member-declaration. */
2464 if (TYPE_P (scope))
2466 error ("%qT is not a namespace", scope);
2467 return NULL_TREE;
2469 else if (scope == error_mark_node)
2470 return NULL_TREE;
2472 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR)
2474 /* 7.3.3/5
2475 A using-declaration shall not name a template-id. */
2476 error ("a using-declaration cannot specify a template-id. "
2477 "Try %<using %D%>", name);
2478 return NULL_TREE;
2481 if (TREE_CODE (decl) == NAMESPACE_DECL)
2483 error ("namespace %qD not allowed in using-declaration", decl);
2484 return NULL_TREE;
2487 if (TREE_CODE (decl) == SCOPE_REF)
2489 /* It's a nested name with template parameter dependent scope.
2490 This can only be using-declaration for class member. */
2491 error ("%qT is not a namespace", TREE_OPERAND (decl, 0));
2492 return NULL_TREE;
2495 if (is_overloaded_fn (decl))
2496 decl = get_first_fn (decl);
2498 gcc_assert (DECL_P (decl));
2500 /* Make a USING_DECL. */
2501 tree using_decl = push_using_decl (scope, name);
2503 if (using_decl == NULL_TREE
2504 && at_function_scope_p ()
2505 && VAR_P (decl))
2506 /* C++11 7.3.3/10. */
2507 error ("%qD is already declared in this scope", name);
2509 return using_decl;
2512 /* Process local and global using-declarations. */
2514 static void
2515 do_nonmember_using_decl (tree scope, tree name, tree oldval, tree oldtype,
2516 tree *newval, tree *newtype)
2518 struct scope_binding decls = EMPTY_SCOPE_BINDING;
2520 *newval = *newtype = NULL_TREE;
2521 if (!qualified_lookup_using_namespace (name, scope, &decls, 0))
2522 /* Lookup error */
2523 return;
2525 if (!decls.value && !decls.type)
2527 error ("%qD not declared", name);
2528 return;
2531 /* Shift the old and new bindings around so we're comparing class and
2532 enumeration names to each other. */
2533 if (oldval && DECL_IMPLICIT_TYPEDEF_P (oldval))
2535 oldtype = oldval;
2536 oldval = NULL_TREE;
2539 if (decls.value && DECL_IMPLICIT_TYPEDEF_P (decls.value))
2541 decls.type = decls.value;
2542 decls.value = NULL_TREE;
2545 /* It is impossible to overload a built-in function; any explicit
2546 declaration eliminates the built-in declaration. So, if OLDVAL
2547 is a built-in, then we can just pretend it isn't there. */
2548 if (oldval
2549 && TREE_CODE (oldval) == FUNCTION_DECL
2550 && DECL_ANTICIPATED (oldval)
2551 && !DECL_HIDDEN_FRIEND_P (oldval))
2552 oldval = NULL_TREE;
2554 if (decls.value)
2556 /* Check for using functions. */
2557 if (is_overloaded_fn (decls.value))
2559 tree tmp, tmp1;
2561 if (oldval && !is_overloaded_fn (oldval))
2563 error ("%qD is already declared in this scope", name);
2564 oldval = NULL_TREE;
2567 *newval = oldval;
2568 for (tmp = decls.value; tmp; tmp = OVL_NEXT (tmp))
2570 tree new_fn = OVL_CURRENT (tmp);
2572 /* [namespace.udecl]
2574 If a function declaration in namespace scope or block
2575 scope has the same name and the same parameter types as a
2576 function introduced by a using declaration the program is
2577 ill-formed. */
2578 for (tmp1 = oldval; tmp1; tmp1 = OVL_NEXT (tmp1))
2580 tree old_fn = OVL_CURRENT (tmp1);
2582 if (new_fn == old_fn)
2583 /* The function already exists in the current namespace. */
2584 break;
2585 else if (TREE_CODE (tmp1) == OVERLOAD && OVL_USED (tmp1))
2586 continue; /* this is a using decl */
2587 else if (compparms_for_decl_and_using_decl (new_fn, old_fn))
2589 gcc_assert (!DECL_ANTICIPATED (old_fn)
2590 || DECL_HIDDEN_FRIEND_P (old_fn));
2592 /* There was already a non-using declaration in
2593 this scope with the same parameter types. If both
2594 are the same extern "C" functions, that's ok. */
2595 if (decls_match (new_fn, old_fn))
2596 break;
2597 else
2599 diagnose_name_conflict (new_fn, old_fn);
2600 break;
2605 /* If we broke out of the loop, there's no reason to add
2606 this function to the using declarations for this
2607 scope. */
2608 if (tmp1)
2609 continue;
2611 /* If we are adding to an existing OVERLOAD, then we no
2612 longer know the type of the set of functions. */
2613 if (*newval && TREE_CODE (*newval) == OVERLOAD)
2614 TREE_TYPE (*newval) = unknown_type_node;
2615 /* Add this new function to the set. */
2616 *newval = build_overload (OVL_CURRENT (tmp), *newval);
2617 /* If there is only one function, then we use its type. (A
2618 using-declaration naming a single function can be used in
2619 contexts where overload resolution cannot be
2620 performed.) */
2621 if (TREE_CODE (*newval) != OVERLOAD)
2623 *newval = ovl_cons (*newval, NULL_TREE);
2624 TREE_TYPE (*newval) = TREE_TYPE (OVL_CURRENT (tmp));
2626 OVL_USED (*newval) = 1;
2629 else
2631 *newval = decls.value;
2632 if (oldval && !decls_match (*newval, oldval))
2633 error ("%qD is already declared in this scope", name);
2636 else
2637 *newval = oldval;
2639 if (decls.type && TREE_CODE (decls.type) == TREE_LIST)
2641 error ("reference to %qD is ambiguous", name);
2642 print_candidates (decls.type);
2644 else
2646 *newtype = decls.type;
2647 if (oldtype && *newtype && !decls_match (oldtype, *newtype))
2648 error ("%qD is already declared in this scope", name);
2651 /* If *newval is empty, shift any class or enumeration name down. */
2652 if (!*newval)
2654 *newval = *newtype;
2655 *newtype = NULL_TREE;
2659 /* Process a using-declaration at function scope. */
2661 void
2662 do_local_using_decl (tree decl, tree scope, tree name)
2664 tree oldval, oldtype, newval, newtype;
2665 tree orig_decl = decl;
2667 decl = validate_nonmember_using_decl (decl, scope, name);
2668 if (decl == NULL_TREE)
2669 return;
2671 if (building_stmt_list_p ()
2672 && at_function_scope_p ())
2673 add_decl_expr (decl);
2675 oldval = lookup_name_innermost_nonclass_level (name);
2676 oldtype = lookup_type_current_level (name);
2678 do_nonmember_using_decl (scope, name, oldval, oldtype, &newval, &newtype);
2680 if (newval)
2682 if (is_overloaded_fn (newval))
2684 tree fn, term;
2686 /* We only need to push declarations for those functions
2687 that were not already bound in the current level.
2688 The old value might be NULL_TREE, it might be a single
2689 function, or an OVERLOAD. */
2690 if (oldval && TREE_CODE (oldval) == OVERLOAD)
2691 term = OVL_FUNCTION (oldval);
2692 else
2693 term = oldval;
2694 for (fn = newval; fn && OVL_CURRENT (fn) != term;
2695 fn = OVL_NEXT (fn))
2696 push_overloaded_decl (OVL_CURRENT (fn),
2697 PUSH_LOCAL | PUSH_USING,
2698 false);
2700 else
2701 push_local_binding (name, newval, PUSH_USING);
2703 if (newtype)
2705 push_local_binding (name, newtype, PUSH_USING);
2706 set_identifier_type_value (name, newtype);
2709 /* Emit debug info. */
2710 if (!processing_template_decl)
2711 cp_emit_debug_info_for_using (orig_decl, current_scope());
2714 /* Returns true if ROOT (a namespace, class, or function) encloses
2715 CHILD. CHILD may be either a class type or a namespace. */
2717 bool
2718 is_ancestor (tree root, tree child)
2720 gcc_assert ((TREE_CODE (root) == NAMESPACE_DECL
2721 || TREE_CODE (root) == FUNCTION_DECL
2722 || CLASS_TYPE_P (root)));
2723 gcc_assert ((TREE_CODE (child) == NAMESPACE_DECL
2724 || CLASS_TYPE_P (child)));
2726 /* The global namespace encloses everything. */
2727 if (root == global_namespace)
2728 return true;
2730 while (true)
2732 /* If we've run out of scopes, stop. */
2733 if (!child)
2734 return false;
2735 /* If we've reached the ROOT, it encloses CHILD. */
2736 if (root == child)
2737 return true;
2738 /* Go out one level. */
2739 if (TYPE_P (child))
2740 child = TYPE_NAME (child);
2741 child = DECL_CONTEXT (child);
2745 /* Enter the class or namespace scope indicated by T suitable for name
2746 lookup. T can be arbitrary scope, not necessary nested inside the
2747 current scope. Returns a non-null scope to pop iff pop_scope
2748 should be called later to exit this scope. */
2750 tree
2751 push_scope (tree t)
2753 if (TREE_CODE (t) == NAMESPACE_DECL)
2754 push_decl_namespace (t);
2755 else if (CLASS_TYPE_P (t))
2757 if (!at_class_scope_p ()
2758 || !same_type_p (current_class_type, t))
2759 push_nested_class (t);
2760 else
2761 /* T is the same as the current scope. There is therefore no
2762 need to re-enter the scope. Since we are not actually
2763 pushing a new scope, our caller should not call
2764 pop_scope. */
2765 t = NULL_TREE;
2768 return t;
2771 /* Leave scope pushed by push_scope. */
2773 void
2774 pop_scope (tree t)
2776 if (t == NULL_TREE)
2777 return;
2778 if (TREE_CODE (t) == NAMESPACE_DECL)
2779 pop_decl_namespace ();
2780 else if CLASS_TYPE_P (t)
2781 pop_nested_class ();
2784 /* Subroutine of push_inner_scope. */
2786 static void
2787 push_inner_scope_r (tree outer, tree inner)
2789 tree prev;
2791 if (outer == inner
2792 || (TREE_CODE (inner) != NAMESPACE_DECL && !CLASS_TYPE_P (inner)))
2793 return;
2795 prev = CP_DECL_CONTEXT (TREE_CODE (inner) == NAMESPACE_DECL ? inner : TYPE_NAME (inner));
2796 if (outer != prev)
2797 push_inner_scope_r (outer, prev);
2798 if (TREE_CODE (inner) == NAMESPACE_DECL)
2800 cp_binding_level *save_template_parm = 0;
2801 /* Temporary take out template parameter scopes. They are saved
2802 in reversed order in save_template_parm. */
2803 while (current_binding_level->kind == sk_template_parms)
2805 cp_binding_level *b = current_binding_level;
2806 current_binding_level = b->level_chain;
2807 b->level_chain = save_template_parm;
2808 save_template_parm = b;
2811 resume_scope (NAMESPACE_LEVEL (inner));
2812 current_namespace = inner;
2814 /* Restore template parameter scopes. */
2815 while (save_template_parm)
2817 cp_binding_level *b = save_template_parm;
2818 save_template_parm = b->level_chain;
2819 b->level_chain = current_binding_level;
2820 current_binding_level = b;
2823 else
2824 pushclass (inner);
2827 /* Enter the scope INNER from current scope. INNER must be a scope
2828 nested inside current scope. This works with both name lookup and
2829 pushing name into scope. In case a template parameter scope is present,
2830 namespace is pushed under the template parameter scope according to
2831 name lookup rule in 14.6.1/6.
2833 Return the former current scope suitable for pop_inner_scope. */
2835 tree
2836 push_inner_scope (tree inner)
2838 tree outer = current_scope ();
2839 if (!outer)
2840 outer = current_namespace;
2842 push_inner_scope_r (outer, inner);
2843 return outer;
2846 /* Exit the current scope INNER back to scope OUTER. */
2848 void
2849 pop_inner_scope (tree outer, tree inner)
2851 if (outer == inner
2852 || (TREE_CODE (inner) != NAMESPACE_DECL && !CLASS_TYPE_P (inner)))
2853 return;
2855 while (outer != inner)
2857 if (TREE_CODE (inner) == NAMESPACE_DECL)
2859 cp_binding_level *save_template_parm = 0;
2860 /* Temporary take out template parameter scopes. They are saved
2861 in reversed order in save_template_parm. */
2862 while (current_binding_level->kind == sk_template_parms)
2864 cp_binding_level *b = current_binding_level;
2865 current_binding_level = b->level_chain;
2866 b->level_chain = save_template_parm;
2867 save_template_parm = b;
2870 pop_namespace ();
2872 /* Restore template parameter scopes. */
2873 while (save_template_parm)
2875 cp_binding_level *b = save_template_parm;
2876 save_template_parm = b->level_chain;
2877 b->level_chain = current_binding_level;
2878 current_binding_level = b;
2881 else
2882 popclass ();
2884 inner = CP_DECL_CONTEXT (TREE_CODE (inner) == NAMESPACE_DECL ? inner : TYPE_NAME (inner));
2888 /* Do a pushlevel for class declarations. */
2890 void
2891 pushlevel_class (void)
2893 class_binding_level = begin_scope (sk_class, current_class_type);
2896 /* ...and a poplevel for class declarations. */
2898 void
2899 poplevel_class (void)
2901 cp_binding_level *level = class_binding_level;
2902 cp_class_binding *cb;
2903 size_t i;
2904 tree shadowed;
2906 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
2907 gcc_assert (level != 0);
2909 /* If we're leaving a toplevel class, cache its binding level. */
2910 if (current_class_depth == 1)
2911 previous_class_level = level;
2912 for (shadowed = level->type_shadowed;
2913 shadowed;
2914 shadowed = TREE_CHAIN (shadowed))
2915 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (shadowed), TREE_VALUE (shadowed));
2917 /* Remove the bindings for all of the class-level declarations. */
2918 if (level->class_shadowed)
2920 FOR_EACH_VEC_ELT (*level->class_shadowed, i, cb)
2922 IDENTIFIER_BINDING (cb->identifier) = cb->base->previous;
2923 cxx_binding_free (cb->base);
2925 ggc_free (level->class_shadowed);
2926 level->class_shadowed = NULL;
2929 /* Now, pop out of the binding level which we created up in the
2930 `pushlevel_class' routine. */
2931 gcc_assert (current_binding_level == level);
2932 leave_scope ();
2933 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
2936 /* Set INHERITED_VALUE_BINDING_P on BINDING to true or false, as
2937 appropriate. DECL is the value to which a name has just been
2938 bound. CLASS_TYPE is the class in which the lookup occurred. */
2940 static void
2941 set_inherited_value_binding_p (cxx_binding *binding, tree decl,
2942 tree class_type)
2944 if (binding->value == decl && TREE_CODE (decl) != TREE_LIST)
2946 tree context;
2948 if (TREE_CODE (decl) == OVERLOAD)
2949 context = ovl_scope (decl);
2950 else
2952 gcc_assert (DECL_P (decl));
2953 context = context_for_name_lookup (decl);
2956 if (is_properly_derived_from (class_type, context))
2957 INHERITED_VALUE_BINDING_P (binding) = 1;
2958 else
2959 INHERITED_VALUE_BINDING_P (binding) = 0;
2961 else if (binding->value == decl)
2962 /* We only encounter a TREE_LIST when there is an ambiguity in the
2963 base classes. Such an ambiguity can be overridden by a
2964 definition in this class. */
2965 INHERITED_VALUE_BINDING_P (binding) = 1;
2966 else
2967 INHERITED_VALUE_BINDING_P (binding) = 0;
2970 /* Make the declaration of X appear in CLASS scope. */
2972 bool
2973 pushdecl_class_level (tree x)
2975 tree name;
2976 bool is_valid = true;
2977 bool subtime;
2979 /* Do nothing if we're adding to an outer lambda closure type,
2980 outer_binding will add it later if it's needed. */
2981 if (current_class_type != class_binding_level->this_entity)
2982 return true;
2984 subtime = timevar_cond_start (TV_NAME_LOOKUP);
2985 /* Get the name of X. */
2986 if (TREE_CODE (x) == OVERLOAD)
2987 name = DECL_NAME (get_first_fn (x));
2988 else
2989 name = DECL_NAME (x);
2991 if (name)
2993 is_valid = push_class_level_binding (name, x);
2994 if (TREE_CODE (x) == TYPE_DECL)
2995 set_identifier_type_value (name, x);
2997 else if (ANON_AGGR_TYPE_P (TREE_TYPE (x)))
2999 /* If X is an anonymous aggregate, all of its members are
3000 treated as if they were members of the class containing the
3001 aggregate, for naming purposes. */
3002 tree f;
3004 for (f = TYPE_FIELDS (TREE_TYPE (x)); f; f = DECL_CHAIN (f))
3006 location_t save_location = input_location;
3007 input_location = DECL_SOURCE_LOCATION (f);
3008 if (!pushdecl_class_level (f))
3009 is_valid = false;
3010 input_location = save_location;
3013 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3014 return is_valid;
3017 /* Return the BINDING (if any) for NAME in SCOPE, which is a class
3018 scope. If the value returned is non-NULL, and the PREVIOUS field
3019 is not set, callers must set the PREVIOUS field explicitly. */
3021 static cxx_binding *
3022 get_class_binding (tree name, cp_binding_level *scope)
3024 tree class_type;
3025 tree type_binding;
3026 tree value_binding;
3027 cxx_binding *binding;
3029 class_type = scope->this_entity;
3031 /* Get the type binding. */
3032 type_binding = lookup_member (class_type, name,
3033 /*protect=*/2, /*want_type=*/true,
3034 tf_warning_or_error);
3035 /* Get the value binding. */
3036 value_binding = lookup_member (class_type, name,
3037 /*protect=*/2, /*want_type=*/false,
3038 tf_warning_or_error);
3040 if (value_binding
3041 && (TREE_CODE (value_binding) == TYPE_DECL
3042 || DECL_CLASS_TEMPLATE_P (value_binding)
3043 || (TREE_CODE (value_binding) == TREE_LIST
3044 && TREE_TYPE (value_binding) == error_mark_node
3045 && (TREE_CODE (TREE_VALUE (value_binding))
3046 == TYPE_DECL))))
3047 /* We found a type binding, even when looking for a non-type
3048 binding. This means that we already processed this binding
3049 above. */
3051 else if (value_binding)
3053 if (TREE_CODE (value_binding) == TREE_LIST
3054 && TREE_TYPE (value_binding) == error_mark_node)
3055 /* NAME is ambiguous. */
3057 else if (BASELINK_P (value_binding))
3058 /* NAME is some overloaded functions. */
3059 value_binding = BASELINK_FUNCTIONS (value_binding);
3062 /* If we found either a type binding or a value binding, create a
3063 new binding object. */
3064 if (type_binding || value_binding)
3066 binding = new_class_binding (name,
3067 value_binding,
3068 type_binding,
3069 scope);
3070 /* This is a class-scope binding, not a block-scope binding. */
3071 LOCAL_BINDING_P (binding) = 0;
3072 set_inherited_value_binding_p (binding, value_binding, class_type);
3074 else
3075 binding = NULL;
3077 return binding;
3080 /* Make the declaration(s) of X appear in CLASS scope under the name
3081 NAME. Returns true if the binding is valid. */
3083 static bool
3084 push_class_level_binding_1 (tree name, tree x)
3086 cxx_binding *binding;
3087 tree decl = x;
3088 bool ok;
3090 /* The class_binding_level will be NULL if x is a template
3091 parameter name in a member template. */
3092 if (!class_binding_level)
3093 return true;
3095 if (name == error_mark_node)
3096 return false;
3098 /* Check for invalid member names. But don't worry about a default
3099 argument-scope lambda being pushed after the class is complete. */
3100 gcc_assert (TYPE_BEING_DEFINED (current_class_type)
3101 || LAMBDA_TYPE_P (TREE_TYPE (decl)));
3102 /* Check that we're pushing into the right binding level. */
3103 gcc_assert (current_class_type == class_binding_level->this_entity);
3105 /* We could have been passed a tree list if this is an ambiguous
3106 declaration. If so, pull the declaration out because
3107 check_template_shadow will not handle a TREE_LIST. */
3108 if (TREE_CODE (decl) == TREE_LIST
3109 && TREE_TYPE (decl) == error_mark_node)
3110 decl = TREE_VALUE (decl);
3112 if (!check_template_shadow (decl))
3113 return false;
3115 /* [class.mem]
3117 If T is the name of a class, then each of the following shall
3118 have a name different from T:
3120 -- every static data member of class T;
3122 -- every member of class T that is itself a type;
3124 -- every enumerator of every member of class T that is an
3125 enumerated type;
3127 -- every member of every anonymous union that is a member of
3128 class T.
3130 (Non-static data members were also forbidden to have the same
3131 name as T until TC1.) */
3132 if ((VAR_P (x)
3133 || TREE_CODE (x) == CONST_DECL
3134 || (TREE_CODE (x) == TYPE_DECL
3135 && !DECL_SELF_REFERENCE_P (x))
3136 /* A data member of an anonymous union. */
3137 || (TREE_CODE (x) == FIELD_DECL
3138 && DECL_CONTEXT (x) != current_class_type))
3139 && DECL_NAME (x) == constructor_name (current_class_type))
3141 tree scope = context_for_name_lookup (x);
3142 if (TYPE_P (scope) && same_type_p (scope, current_class_type))
3144 error ("%qD has the same name as the class in which it is "
3145 "declared",
3147 return false;
3151 /* Get the current binding for NAME in this class, if any. */
3152 binding = IDENTIFIER_BINDING (name);
3153 if (!binding || binding->scope != class_binding_level)
3155 binding = get_class_binding (name, class_binding_level);
3156 /* If a new binding was created, put it at the front of the
3157 IDENTIFIER_BINDING list. */
3158 if (binding)
3160 binding->previous = IDENTIFIER_BINDING (name);
3161 IDENTIFIER_BINDING (name) = binding;
3165 /* If there is already a binding, then we may need to update the
3166 current value. */
3167 if (binding && binding->value)
3169 tree bval = binding->value;
3170 tree old_decl = NULL_TREE;
3171 tree target_decl = strip_using_decl (decl);
3172 tree target_bval = strip_using_decl (bval);
3174 if (INHERITED_VALUE_BINDING_P (binding))
3176 /* If the old binding was from a base class, and was for a
3177 tag name, slide it over to make room for the new binding.
3178 The old binding is still visible if explicitly qualified
3179 with a class-key. */
3180 if (TREE_CODE (target_bval) == TYPE_DECL
3181 && DECL_ARTIFICIAL (target_bval)
3182 && !(TREE_CODE (target_decl) == TYPE_DECL
3183 && DECL_ARTIFICIAL (target_decl)))
3185 old_decl = binding->type;
3186 binding->type = bval;
3187 binding->value = NULL_TREE;
3188 INHERITED_VALUE_BINDING_P (binding) = 0;
3190 else
3192 old_decl = bval;
3193 /* Any inherited type declaration is hidden by the type
3194 declaration in the derived class. */
3195 if (TREE_CODE (target_decl) == TYPE_DECL
3196 && DECL_ARTIFICIAL (target_decl))
3197 binding->type = NULL_TREE;
3200 else if (TREE_CODE (target_decl) == OVERLOAD
3201 && is_overloaded_fn (target_bval))
3202 old_decl = bval;
3203 else if (TREE_CODE (decl) == USING_DECL
3204 && TREE_CODE (bval) == USING_DECL
3205 && same_type_p (USING_DECL_SCOPE (decl),
3206 USING_DECL_SCOPE (bval)))
3207 /* This is a using redeclaration that will be diagnosed later
3208 in supplement_binding */
3210 else if (TREE_CODE (decl) == USING_DECL
3211 && TREE_CODE (bval) == USING_DECL
3212 && DECL_DEPENDENT_P (decl)
3213 && DECL_DEPENDENT_P (bval))
3214 return true;
3215 else if (TREE_CODE (decl) == USING_DECL
3216 && is_overloaded_fn (target_bval))
3217 old_decl = bval;
3218 else if (TREE_CODE (bval) == USING_DECL
3219 && is_overloaded_fn (target_decl))
3220 return true;
3222 if (old_decl && binding->scope == class_binding_level)
3224 binding->value = x;
3225 /* It is always safe to clear INHERITED_VALUE_BINDING_P
3226 here. This function is only used to register bindings
3227 from with the class definition itself. */
3228 INHERITED_VALUE_BINDING_P (binding) = 0;
3229 return true;
3233 /* Note that we declared this value so that we can issue an error if
3234 this is an invalid redeclaration of a name already used for some
3235 other purpose. */
3236 note_name_declared_in_class (name, decl);
3238 /* If we didn't replace an existing binding, put the binding on the
3239 stack of bindings for the identifier, and update the shadowed
3240 list. */
3241 if (binding && binding->scope == class_binding_level)
3242 /* Supplement the existing binding. */
3243 ok = supplement_binding (binding, decl);
3244 else
3246 /* Create a new binding. */
3247 push_binding (name, decl, class_binding_level);
3248 ok = true;
3251 return ok;
3254 /* Wrapper for push_class_level_binding_1. */
3256 bool
3257 push_class_level_binding (tree name, tree x)
3259 bool ret;
3260 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3261 ret = push_class_level_binding_1 (name, x);
3262 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3263 return ret;
3266 /* Process "using SCOPE::NAME" in a class scope. Return the
3267 USING_DECL created. */
3269 tree
3270 do_class_using_decl (tree scope, tree name)
3272 /* The USING_DECL returned by this function. */
3273 tree value;
3274 /* The declaration (or declarations) name by this using
3275 declaration. NULL if we are in a template and cannot figure out
3276 what has been named. */
3277 tree decl;
3278 /* True if SCOPE is a dependent type. */
3279 bool scope_dependent_p;
3280 /* True if SCOPE::NAME is dependent. */
3281 bool name_dependent_p;
3282 /* True if any of the bases of CURRENT_CLASS_TYPE are dependent. */
3283 bool bases_dependent_p;
3284 tree binfo;
3285 tree base_binfo;
3286 int i;
3288 if (name == error_mark_node)
3289 return NULL_TREE;
3291 if (!scope || !TYPE_P (scope))
3293 error ("using-declaration for non-member at class scope");
3294 return NULL_TREE;
3297 /* Make sure the name is not invalid */
3298 if (TREE_CODE (name) == BIT_NOT_EXPR)
3300 error ("%<%T::%D%> names destructor", scope, name);
3301 return NULL_TREE;
3303 /* Using T::T declares inheriting ctors, even if T is a typedef. */
3304 if (MAYBE_CLASS_TYPE_P (scope)
3305 && ((TYPE_NAME (scope) && name == TYPE_IDENTIFIER (scope))
3306 || constructor_name_p (name, scope)))
3308 maybe_warn_cpp0x (CPP0X_INHERITING_CTORS);
3309 name = ctor_identifier;
3311 if (constructor_name_p (name, current_class_type))
3313 error ("%<%T::%D%> names constructor in %qT",
3314 scope, name, current_class_type);
3315 return NULL_TREE;
3318 scope_dependent_p = dependent_scope_p (scope);
3319 name_dependent_p = (scope_dependent_p
3320 || (IDENTIFIER_TYPENAME_P (name)
3321 && dependent_type_p (TREE_TYPE (name))));
3323 bases_dependent_p = false;
3324 if (processing_template_decl)
3325 for (binfo = TYPE_BINFO (current_class_type), i = 0;
3326 BINFO_BASE_ITERATE (binfo, i, base_binfo);
3327 i++)
3328 if (dependent_type_p (TREE_TYPE (base_binfo)))
3330 bases_dependent_p = true;
3331 break;
3334 decl = NULL_TREE;
3336 /* From [namespace.udecl]:
3338 A using-declaration used as a member-declaration shall refer to a
3339 member of a base class of the class being defined.
3341 In general, we cannot check this constraint in a template because
3342 we do not know the entire set of base classes of the current
3343 class type. Morover, if SCOPE is dependent, it might match a
3344 non-dependent base. */
3346 if (!scope_dependent_p)
3348 base_kind b_kind;
3349 binfo = lookup_base (current_class_type, scope, ba_any, &b_kind,
3350 tf_warning_or_error);
3351 if (b_kind < bk_proper_base)
3353 if (!bases_dependent_p)
3355 error_not_base_type (scope, current_class_type);
3356 return NULL_TREE;
3359 else if (!name_dependent_p)
3361 decl = lookup_member (binfo, name, 0, false, tf_warning_or_error);
3362 if (!decl)
3364 error ("no members matching %<%T::%D%> in %q#T", scope, name,
3365 scope);
3366 return NULL_TREE;
3368 /* The binfo from which the functions came does not matter. */
3369 if (BASELINK_P (decl))
3370 decl = BASELINK_FUNCTIONS (decl);
3374 value = build_lang_decl (USING_DECL, name, NULL_TREE);
3375 USING_DECL_DECLS (value) = decl;
3376 USING_DECL_SCOPE (value) = scope;
3377 DECL_DEPENDENT_P (value) = !decl;
3379 return value;
3383 /* Return the binding value for name in scope. */
3386 static tree
3387 namespace_binding_1 (tree name, tree scope)
3389 cxx_binding *binding;
3391 if (SCOPE_FILE_SCOPE_P (scope))
3392 scope = global_namespace;
3393 else
3394 /* Unnecessary for the global namespace because it can't be an alias. */
3395 scope = ORIGINAL_NAMESPACE (scope);
3397 binding = cp_binding_level_find_binding_for_name (NAMESPACE_LEVEL (scope), name);
3399 return binding ? binding->value : NULL_TREE;
3402 tree
3403 namespace_binding (tree name, tree scope)
3405 tree ret;
3406 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3407 ret = namespace_binding_1 (name, scope);
3408 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3409 return ret;
3412 /* Set the binding value for name in scope. */
3414 static void
3415 set_namespace_binding_1 (tree name, tree scope, tree val)
3417 cxx_binding *b;
3419 if (scope == NULL_TREE)
3420 scope = global_namespace;
3421 b = binding_for_name (NAMESPACE_LEVEL (scope), name);
3422 if (!b->value || TREE_CODE (val) == OVERLOAD || val == error_mark_node)
3423 b->value = val;
3424 else
3425 supplement_binding (b, val);
3428 /* Wrapper for set_namespace_binding_1. */
3430 void
3431 set_namespace_binding (tree name, tree scope, tree val)
3433 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3434 set_namespace_binding_1 (name, scope, val);
3435 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3438 /* Set the context of a declaration to scope. Complain if we are not
3439 outside scope. */
3441 void
3442 set_decl_namespace (tree decl, tree scope, bool friendp)
3444 tree old;
3446 /* Get rid of namespace aliases. */
3447 scope = ORIGINAL_NAMESPACE (scope);
3449 /* It is ok for friends to be qualified in parallel space. */
3450 if (!friendp && !is_ancestor (current_namespace, scope))
3451 error ("declaration of %qD not in a namespace surrounding %qD",
3452 decl, scope);
3453 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
3455 /* Writing "int N::i" to declare a variable within "N" is invalid. */
3456 if (scope == current_namespace)
3458 if (at_namespace_scope_p ())
3459 error ("explicit qualification in declaration of %qD",
3460 decl);
3461 return;
3464 /* See whether this has been declared in the namespace. */
3465 old = lookup_qualified_name (scope, DECL_NAME (decl), false, true);
3466 if (old == error_mark_node)
3467 /* No old declaration at all. */
3468 goto complain;
3469 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
3470 if (TREE_CODE (old) == TREE_LIST)
3472 error ("reference to %qD is ambiguous", decl);
3473 print_candidates (old);
3474 return;
3476 if (!is_overloaded_fn (decl))
3478 /* We might have found OLD in an inline namespace inside SCOPE. */
3479 if (TREE_CODE (decl) == TREE_CODE (old))
3480 DECL_CONTEXT (decl) = DECL_CONTEXT (old);
3481 /* Don't compare non-function decls with decls_match here, since
3482 it can't check for the correct constness at this
3483 point. pushdecl will find those errors later. */
3484 return;
3486 /* Since decl is a function, old should contain a function decl. */
3487 if (!is_overloaded_fn (old))
3488 goto complain;
3489 /* A template can be explicitly specialized in any namespace. */
3490 if (processing_explicit_instantiation)
3491 return;
3492 if (processing_template_decl || processing_specialization)
3493 /* We have not yet called push_template_decl to turn a
3494 FUNCTION_DECL into a TEMPLATE_DECL, so the declarations won't
3495 match. But, we'll check later, when we construct the
3496 template. */
3497 return;
3498 /* Instantiations or specializations of templates may be declared as
3499 friends in any namespace. */
3500 if (friendp && DECL_USE_TEMPLATE (decl))
3501 return;
3502 if (is_overloaded_fn (old))
3504 tree found = NULL_TREE;
3505 tree elt = old;
3506 for (; elt; elt = OVL_NEXT (elt))
3508 tree ofn = OVL_CURRENT (elt);
3509 /* Adjust DECL_CONTEXT first so decls_match will return true
3510 if DECL will match a declaration in an inline namespace. */
3511 DECL_CONTEXT (decl) = DECL_CONTEXT (ofn);
3512 if (decls_match (decl, ofn))
3514 if (found && !decls_match (found, ofn))
3516 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
3517 error ("reference to %qD is ambiguous", decl);
3518 print_candidates (old);
3519 return;
3521 found = ofn;
3524 if (found)
3526 if (!is_associated_namespace (scope, CP_DECL_CONTEXT (found)))
3527 goto complain;
3528 DECL_CONTEXT (decl) = DECL_CONTEXT (found);
3529 return;
3532 else
3534 DECL_CONTEXT (decl) = DECL_CONTEXT (old);
3535 if (decls_match (decl, old))
3536 return;
3539 /* It didn't work, go back to the explicit scope. */
3540 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
3541 complain:
3542 error ("%qD should have been declared inside %qD", decl, scope);
3545 /* Return the namespace where the current declaration is declared. */
3547 tree
3548 current_decl_namespace (void)
3550 tree result;
3551 /* If we have been pushed into a different namespace, use it. */
3552 if (!vec_safe_is_empty (decl_namespace_list))
3553 return decl_namespace_list->last ();
3555 if (current_class_type)
3556 result = decl_namespace_context (current_class_type);
3557 else if (current_function_decl)
3558 result = decl_namespace_context (current_function_decl);
3559 else
3560 result = current_namespace;
3561 return result;
3564 /* Process any ATTRIBUTES on a namespace definition. Currently only
3565 attribute visibility is meaningful, which is a property of the syntactic
3566 block rather than the namespace as a whole, so we don't touch the
3567 NAMESPACE_DECL at all. Returns true if attribute visibility is seen. */
3569 bool
3570 handle_namespace_attrs (tree ns, tree attributes)
3572 tree d;
3573 bool saw_vis = false;
3575 for (d = attributes; d; d = TREE_CHAIN (d))
3577 tree name = get_attribute_name (d);
3578 tree args = TREE_VALUE (d);
3580 if (is_attribute_p ("visibility", name))
3582 tree x = args ? TREE_VALUE (args) : NULL_TREE;
3583 if (x == NULL_TREE || TREE_CODE (x) != STRING_CST || TREE_CHAIN (args))
3585 warning (OPT_Wattributes,
3586 "%qD attribute requires a single NTBS argument",
3587 name);
3588 continue;
3591 if (!TREE_PUBLIC (ns))
3592 warning (OPT_Wattributes,
3593 "%qD attribute is meaningless since members of the "
3594 "anonymous namespace get local symbols", name);
3596 push_visibility (TREE_STRING_POINTER (x), 1);
3597 saw_vis = true;
3599 else
3601 warning (OPT_Wattributes, "%qD attribute directive ignored",
3602 name);
3603 continue;
3607 return saw_vis;
3610 /* Push into the scope of the NAME namespace. If NAME is NULL_TREE, then we
3611 select a name that is unique to this compilation unit. */
3613 void
3614 push_namespace (tree name)
3616 tree d = NULL_TREE;
3617 bool need_new = true;
3618 bool implicit_use = false;
3619 bool anon = !name;
3621 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3623 /* We should not get here if the global_namespace is not yet constructed
3624 nor if NAME designates the global namespace: The global scope is
3625 constructed elsewhere. */
3626 gcc_assert (global_namespace != NULL && name != global_scope_name);
3628 if (anon)
3630 name = get_anonymous_namespace_name();
3631 d = IDENTIFIER_NAMESPACE_VALUE (name);
3632 if (d)
3633 /* Reopening anonymous namespace. */
3634 need_new = false;
3635 implicit_use = true;
3637 else
3639 /* Check whether this is an extended namespace definition. */
3640 d = IDENTIFIER_NAMESPACE_VALUE (name);
3641 if (d != NULL_TREE && TREE_CODE (d) == NAMESPACE_DECL)
3643 tree dna = DECL_NAMESPACE_ALIAS (d);
3644 if (dna)
3646 /* We do some error recovery for, eg, the redeclaration
3647 of M here:
3649 namespace N {}
3650 namespace M = N;
3651 namespace M {}
3653 However, in nasty cases like:
3655 namespace N
3657 namespace M = N;
3658 namespace M {}
3661 we just error out below, in duplicate_decls. */
3662 if (NAMESPACE_LEVEL (dna)->level_chain
3663 == current_binding_level)
3665 error ("namespace alias %qD not allowed here, "
3666 "assuming %qD", d, dna);
3667 d = dna;
3668 need_new = false;
3671 else
3672 need_new = false;
3676 if (need_new)
3678 /* Make a new namespace, binding the name to it. */
3679 d = build_lang_decl (NAMESPACE_DECL, name, void_type_node);
3680 DECL_CONTEXT (d) = FROB_CONTEXT (current_namespace);
3681 /* The name of this namespace is not visible to other translation
3682 units if it is an anonymous namespace or member thereof. */
3683 if (anon || decl_anon_ns_mem_p (current_namespace))
3684 TREE_PUBLIC (d) = 0;
3685 else
3686 TREE_PUBLIC (d) = 1;
3687 pushdecl (d);
3688 if (anon)
3690 /* Clear DECL_NAME for the benefit of debugging back ends. */
3691 SET_DECL_ASSEMBLER_NAME (d, name);
3692 DECL_NAME (d) = NULL_TREE;
3694 begin_scope (sk_namespace, d);
3696 else
3697 resume_scope (NAMESPACE_LEVEL (d));
3699 if (implicit_use)
3700 do_using_directive (d);
3701 /* Enter the name space. */
3702 current_namespace = d;
3704 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3707 /* Pop from the scope of the current namespace. */
3709 void
3710 pop_namespace (void)
3712 gcc_assert (current_namespace != global_namespace);
3713 current_namespace = CP_DECL_CONTEXT (current_namespace);
3714 /* The binding level is not popped, as it might be re-opened later. */
3715 leave_scope ();
3718 /* Push into the scope of the namespace NS, even if it is deeply
3719 nested within another namespace. */
3721 void
3722 push_nested_namespace (tree ns)
3724 if (ns == global_namespace)
3725 push_to_top_level ();
3726 else
3728 push_nested_namespace (CP_DECL_CONTEXT (ns));
3729 push_namespace (DECL_NAME (ns));
3733 /* Pop back from the scope of the namespace NS, which was previously
3734 entered with push_nested_namespace. */
3736 void
3737 pop_nested_namespace (tree ns)
3739 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3740 gcc_assert (current_namespace == ns);
3741 while (ns != global_namespace)
3743 pop_namespace ();
3744 ns = CP_DECL_CONTEXT (ns);
3747 pop_from_top_level ();
3748 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3751 /* Temporarily set the namespace for the current declaration. */
3753 void
3754 push_decl_namespace (tree decl)
3756 if (TREE_CODE (decl) != NAMESPACE_DECL)
3757 decl = decl_namespace_context (decl);
3758 vec_safe_push (decl_namespace_list, ORIGINAL_NAMESPACE (decl));
3761 /* [namespace.memdef]/2 */
3763 void
3764 pop_decl_namespace (void)
3766 decl_namespace_list->pop ();
3769 /* Return the namespace that is the common ancestor
3770 of two given namespaces. */
3772 static tree
3773 namespace_ancestor_1 (tree ns1, tree ns2)
3775 tree nsr;
3776 if (is_ancestor (ns1, ns2))
3777 nsr = ns1;
3778 else
3779 nsr = namespace_ancestor_1 (CP_DECL_CONTEXT (ns1), ns2);
3780 return nsr;
3783 /* Wrapper for namespace_ancestor_1. */
3785 static tree
3786 namespace_ancestor (tree ns1, tree ns2)
3788 tree nsr;
3789 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3790 nsr = namespace_ancestor_1 (ns1, ns2);
3791 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3792 return nsr;
3795 /* Process a namespace-alias declaration. */
3797 void
3798 do_namespace_alias (tree alias, tree name_space)
3800 if (name_space == error_mark_node)
3801 return;
3803 gcc_assert (TREE_CODE (name_space) == NAMESPACE_DECL);
3805 name_space = ORIGINAL_NAMESPACE (name_space);
3807 /* Build the alias. */
3808 alias = build_lang_decl (NAMESPACE_DECL, alias, void_type_node);
3809 DECL_NAMESPACE_ALIAS (alias) = name_space;
3810 DECL_EXTERNAL (alias) = 1;
3811 DECL_CONTEXT (alias) = FROB_CONTEXT (current_scope ());
3812 pushdecl (alias);
3814 /* Emit debug info for namespace alias. */
3815 if (!building_stmt_list_p ())
3816 (*debug_hooks->global_decl) (alias);
3819 /* Like pushdecl, only it places X in the current namespace,
3820 if appropriate. */
3822 tree
3823 pushdecl_namespace_level (tree x, bool is_friend)
3825 cp_binding_level *b = current_binding_level;
3826 tree t;
3828 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3829 t = pushdecl_with_scope (x, NAMESPACE_LEVEL (current_namespace), is_friend);
3831 /* Now, the type_shadowed stack may screw us. Munge it so it does
3832 what we want. */
3833 if (TREE_CODE (t) == TYPE_DECL)
3835 tree name = DECL_NAME (t);
3836 tree newval;
3837 tree *ptr = (tree *)0;
3838 for (; !global_scope_p (b); b = b->level_chain)
3840 tree shadowed = b->type_shadowed;
3841 for (; shadowed; shadowed = TREE_CHAIN (shadowed))
3842 if (TREE_PURPOSE (shadowed) == name)
3844 ptr = &TREE_VALUE (shadowed);
3845 /* Can't break out of the loop here because sometimes
3846 a binding level will have duplicate bindings for
3847 PT names. It's gross, but I haven't time to fix it. */
3850 newval = TREE_TYPE (t);
3851 if (ptr == (tree *)0)
3853 /* @@ This shouldn't be needed. My test case "zstring.cc" trips
3854 up here if this is changed to an assertion. --KR */
3855 SET_IDENTIFIER_TYPE_VALUE (name, t);
3857 else
3859 *ptr = newval;
3862 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3863 return t;
3866 /* Insert USED into the using list of USER. Set INDIRECT_flag if this
3867 directive is not directly from the source. Also find the common
3868 ancestor and let our users know about the new namespace */
3870 static void
3871 add_using_namespace_1 (tree user, tree used, bool indirect)
3873 tree t;
3874 /* Using oneself is a no-op. */
3875 if (user == used)
3876 return;
3877 gcc_assert (TREE_CODE (user) == NAMESPACE_DECL);
3878 gcc_assert (TREE_CODE (used) == NAMESPACE_DECL);
3879 /* Check if we already have this. */
3880 t = purpose_member (used, DECL_NAMESPACE_USING (user));
3881 if (t != NULL_TREE)
3883 if (!indirect)
3884 /* Promote to direct usage. */
3885 TREE_INDIRECT_USING (t) = 0;
3886 return;
3889 /* Add used to the user's using list. */
3890 DECL_NAMESPACE_USING (user)
3891 = tree_cons (used, namespace_ancestor (user, used),
3892 DECL_NAMESPACE_USING (user));
3894 TREE_INDIRECT_USING (DECL_NAMESPACE_USING (user)) = indirect;
3896 /* Add user to the used's users list. */
3897 DECL_NAMESPACE_USERS (used)
3898 = tree_cons (user, 0, DECL_NAMESPACE_USERS (used));
3900 /* Recursively add all namespaces used. */
3901 for (t = DECL_NAMESPACE_USING (used); t; t = TREE_CHAIN (t))
3902 /* indirect usage */
3903 add_using_namespace_1 (user, TREE_PURPOSE (t), 1);
3905 /* Tell everyone using us about the new used namespaces. */
3906 for (t = DECL_NAMESPACE_USERS (user); t; t = TREE_CHAIN (t))
3907 add_using_namespace_1 (TREE_PURPOSE (t), used, 1);
3910 /* Wrapper for add_using_namespace_1. */
3912 static void
3913 add_using_namespace (tree user, tree used, bool indirect)
3915 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3916 add_using_namespace_1 (user, used, indirect);
3917 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3920 /* Process a using-declaration not appearing in class or local scope. */
3922 void
3923 do_toplevel_using_decl (tree decl, tree scope, tree name)
3925 tree oldval, oldtype, newval, newtype;
3926 tree orig_decl = decl;
3927 cxx_binding *binding;
3929 decl = validate_nonmember_using_decl (decl, scope, name);
3930 if (decl == NULL_TREE)
3931 return;
3933 binding = binding_for_name (NAMESPACE_LEVEL (current_namespace), name);
3935 oldval = binding->value;
3936 oldtype = binding->type;
3938 do_nonmember_using_decl (scope, name, oldval, oldtype, &newval, &newtype);
3940 /* Emit debug info. */
3941 if (!processing_template_decl)
3942 cp_emit_debug_info_for_using (orig_decl, current_namespace);
3944 /* Copy declarations found. */
3945 if (newval)
3946 binding->value = newval;
3947 if (newtype)
3948 binding->type = newtype;
3951 /* Process a using-directive. */
3953 void
3954 do_using_directive (tree name_space)
3956 tree context = NULL_TREE;
3958 if (name_space == error_mark_node)
3959 return;
3961 gcc_assert (TREE_CODE (name_space) == NAMESPACE_DECL);
3963 if (building_stmt_list_p ())
3964 add_stmt (build_stmt (input_location, USING_STMT, name_space));
3965 name_space = ORIGINAL_NAMESPACE (name_space);
3967 if (!toplevel_bindings_p ())
3969 push_using_directive (name_space);
3971 else
3973 /* direct usage */
3974 add_using_namespace (current_namespace, name_space, 0);
3975 if (current_namespace != global_namespace)
3976 context = current_namespace;
3978 /* Emit debugging info. */
3979 if (!processing_template_decl)
3980 (*debug_hooks->imported_module_or_decl) (name_space, NULL_TREE,
3981 context, false);
3985 /* Deal with a using-directive seen by the parser. Currently we only
3986 handle attributes here, since they cannot appear inside a template. */
3988 void
3989 parse_using_directive (tree name_space, tree attribs)
3991 tree a;
3993 do_using_directive (name_space);
3995 for (a = attribs; a; a = TREE_CHAIN (a))
3997 tree name = TREE_PURPOSE (a);
3998 if (is_attribute_p ("strong", name))
4000 if (!toplevel_bindings_p ())
4001 error ("strong using only meaningful at namespace scope");
4002 else if (name_space != error_mark_node)
4004 if (!is_ancestor (current_namespace, name_space))
4005 error ("current namespace %qD does not enclose strongly used namespace %qD",
4006 current_namespace, name_space);
4007 DECL_NAMESPACE_ASSOCIATIONS (name_space)
4008 = tree_cons (current_namespace, 0,
4009 DECL_NAMESPACE_ASSOCIATIONS (name_space));
4012 else
4013 warning (OPT_Wattributes, "%qD attribute directive ignored", name);
4017 /* Like pushdecl, only it places X in the global scope if appropriate.
4018 Calls cp_finish_decl to register the variable, initializing it with
4019 *INIT, if INIT is non-NULL. */
4021 static tree
4022 pushdecl_top_level_1 (tree x, tree *init, bool is_friend)
4024 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4025 push_to_top_level ();
4026 x = pushdecl_namespace_level (x, is_friend);
4027 if (init)
4028 cp_finish_decl (x, *init, false, NULL_TREE, 0);
4029 pop_from_top_level ();
4030 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4031 return x;
4034 /* Like pushdecl, only it places X in the global scope if appropriate. */
4036 tree
4037 pushdecl_top_level (tree x)
4039 return pushdecl_top_level_1 (x, NULL, false);
4042 /* Like pushdecl_top_level, but adding the IS_FRIEND parameter. */
4044 tree
4045 pushdecl_top_level_maybe_friend (tree x, bool is_friend)
4047 return pushdecl_top_level_1 (x, NULL, is_friend);
4050 /* Like pushdecl, only it places X in the global scope if
4051 appropriate. Calls cp_finish_decl to register the variable,
4052 initializing it with INIT. */
4054 tree
4055 pushdecl_top_level_and_finish (tree x, tree init)
4057 return pushdecl_top_level_1 (x, &init, false);
4060 /* Combines two sets of overloaded functions into an OVERLOAD chain, removing
4061 duplicates. The first list becomes the tail of the result.
4063 The algorithm is O(n^2). We could get this down to O(n log n) by
4064 doing a sort on the addresses of the functions, if that becomes
4065 necessary. */
4067 static tree
4068 merge_functions (tree s1, tree s2)
4070 for (; s2; s2 = OVL_NEXT (s2))
4072 tree fn2 = OVL_CURRENT (s2);
4073 tree fns1;
4075 for (fns1 = s1; fns1; fns1 = OVL_NEXT (fns1))
4077 tree fn1 = OVL_CURRENT (fns1);
4079 /* If the function from S2 is already in S1, there is no
4080 need to add it again. For `extern "C"' functions, we
4081 might have two FUNCTION_DECLs for the same function, in
4082 different namespaces, but let's leave them in in case
4083 they have different default arguments. */
4084 if (fn1 == fn2)
4085 break;
4088 /* If we exhausted all of the functions in S1, FN2 is new. */
4089 if (!fns1)
4090 s1 = build_overload (fn2, s1);
4092 return s1;
4095 /* Returns TRUE iff OLD and NEW are the same entity.
4097 3 [basic]/3: An entity is a value, object, reference, function,
4098 enumerator, type, class member, template, template specialization,
4099 namespace, parameter pack, or this.
4101 7.3.4 [namespace.udir]/4: If name lookup finds a declaration for a name
4102 in two different namespaces, and the declarations do not declare the
4103 same entity and do not declare functions, the use of the name is
4104 ill-formed. */
4106 static bool
4107 same_entity_p (tree one, tree two)
4109 if (one == two)
4110 return true;
4111 if (!one || !two)
4112 return false;
4113 if (TREE_CODE (one) == TYPE_DECL
4114 && TREE_CODE (two) == TYPE_DECL
4115 && same_type_p (TREE_TYPE (one), TREE_TYPE (two)))
4116 return true;
4117 return false;
4120 /* This should return an error not all definitions define functions.
4121 It is not an error if we find two functions with exactly the
4122 same signature, only if these are selected in overload resolution.
4123 old is the current set of bindings, new_binding the freshly-found binding.
4124 XXX Do we want to give *all* candidates in case of ambiguity?
4125 XXX In what way should I treat extern declarations?
4126 XXX I don't want to repeat the entire duplicate_decls here */
4128 static void
4129 ambiguous_decl (struct scope_binding *old, cxx_binding *new_binding, int flags)
4131 tree val, type;
4132 gcc_assert (old != NULL);
4134 /* Copy the type. */
4135 type = new_binding->type;
4136 if (LOOKUP_NAMESPACES_ONLY (flags)
4137 || (type && hidden_name_p (type) && !(flags & LOOKUP_HIDDEN)))
4138 type = NULL_TREE;
4140 /* Copy the value. */
4141 val = new_binding->value;
4142 if (val)
4144 if (hidden_name_p (val) && !(flags & LOOKUP_HIDDEN))
4145 val = NULL_TREE;
4146 else
4147 switch (TREE_CODE (val))
4149 case TEMPLATE_DECL:
4150 /* If we expect types or namespaces, and not templates,
4151 or this is not a template class. */
4152 if ((LOOKUP_QUALIFIERS_ONLY (flags)
4153 && !DECL_TYPE_TEMPLATE_P (val)))
4154 val = NULL_TREE;
4155 break;
4156 case TYPE_DECL:
4157 if (LOOKUP_NAMESPACES_ONLY (flags)
4158 || (type && (flags & LOOKUP_PREFER_TYPES)))
4159 val = NULL_TREE;
4160 break;
4161 case NAMESPACE_DECL:
4162 if (LOOKUP_TYPES_ONLY (flags))
4163 val = NULL_TREE;
4164 break;
4165 case FUNCTION_DECL:
4166 /* Ignore built-in functions that are still anticipated. */
4167 if (LOOKUP_QUALIFIERS_ONLY (flags))
4168 val = NULL_TREE;
4169 break;
4170 default:
4171 if (LOOKUP_QUALIFIERS_ONLY (flags))
4172 val = NULL_TREE;
4176 /* If val is hidden, shift down any class or enumeration name. */
4177 if (!val)
4179 val = type;
4180 type = NULL_TREE;
4183 if (!old->value)
4184 old->value = val;
4185 else if (val && !same_entity_p (val, old->value))
4187 if (is_overloaded_fn (old->value) && is_overloaded_fn (val))
4188 old->value = merge_functions (old->value, val);
4189 else
4191 old->value = tree_cons (NULL_TREE, old->value,
4192 build_tree_list (NULL_TREE, val));
4193 TREE_TYPE (old->value) = error_mark_node;
4197 if (!old->type)
4198 old->type = type;
4199 else if (type && old->type != type)
4201 old->type = tree_cons (NULL_TREE, old->type,
4202 build_tree_list (NULL_TREE, type));
4203 TREE_TYPE (old->type) = error_mark_node;
4207 /* Return the declarations that are members of the namespace NS. */
4209 tree
4210 cp_namespace_decls (tree ns)
4212 return NAMESPACE_LEVEL (ns)->names;
4215 /* Combine prefer_type and namespaces_only into flags. */
4217 static int
4218 lookup_flags (int prefer_type, int namespaces_only)
4220 if (namespaces_only)
4221 return LOOKUP_PREFER_NAMESPACES;
4222 if (prefer_type > 1)
4223 return LOOKUP_PREFER_TYPES;
4224 if (prefer_type > 0)
4225 return LOOKUP_PREFER_BOTH;
4226 return 0;
4229 /* Given a lookup that returned VAL, use FLAGS to decide if we want to
4230 ignore it or not. Subroutine of lookup_name_real and
4231 lookup_type_scope. */
4233 static bool
4234 qualify_lookup (tree val, int flags)
4236 if (val == NULL_TREE)
4237 return false;
4238 if ((flags & LOOKUP_PREFER_NAMESPACES) && TREE_CODE (val) == NAMESPACE_DECL)
4239 return true;
4240 if (flags & LOOKUP_PREFER_TYPES)
4242 tree target_val = strip_using_decl (val);
4243 if (TREE_CODE (target_val) == TYPE_DECL
4244 || TREE_CODE (target_val) == TEMPLATE_DECL)
4245 return true;
4247 if (flags & (LOOKUP_PREFER_NAMESPACES | LOOKUP_PREFER_TYPES))
4248 return false;
4249 /* Look through lambda things that we shouldn't be able to see. */
4250 if (is_lambda_ignored_entity (val))
4251 return false;
4252 return true;
4255 /* Given a lookup that returned VAL, decide if we want to ignore it or
4256 not based on DECL_ANTICIPATED. */
4258 bool
4259 hidden_name_p (tree val)
4261 if (DECL_P (val)
4262 && DECL_LANG_SPECIFIC (val)
4263 && TYPE_FUNCTION_OR_TEMPLATE_DECL_P (val)
4264 && DECL_ANTICIPATED (val))
4265 return true;
4266 return false;
4269 /* Remove any hidden friend functions from a possibly overloaded set
4270 of functions. */
4272 tree
4273 remove_hidden_names (tree fns)
4275 if (!fns)
4276 return fns;
4278 if (TREE_CODE (fns) == FUNCTION_DECL && hidden_name_p (fns))
4279 fns = NULL_TREE;
4280 else if (TREE_CODE (fns) == OVERLOAD)
4282 tree o;
4284 for (o = fns; o; o = OVL_NEXT (o))
4285 if (hidden_name_p (OVL_CURRENT (o)))
4286 break;
4287 if (o)
4289 tree n = NULL_TREE;
4291 for (o = fns; o; o = OVL_NEXT (o))
4292 if (!hidden_name_p (OVL_CURRENT (o)))
4293 n = build_overload (OVL_CURRENT (o), n);
4294 fns = n;
4298 return fns;
4301 /* Suggest alternatives for NAME, an IDENTIFIER_NODE for which name
4302 lookup failed. Search through all available namespaces and print out
4303 possible candidates. */
4305 void
4306 suggest_alternatives_for (location_t location, tree name)
4308 vec<tree> candidates = vNULL;
4309 vec<tree> namespaces_to_search = vNULL;
4310 int max_to_search = PARAM_VALUE (CXX_MAX_NAMESPACES_FOR_DIAGNOSTIC_HELP);
4311 int n_searched = 0;
4312 tree t;
4313 unsigned ix;
4315 namespaces_to_search.safe_push (global_namespace);
4317 while (!namespaces_to_search.is_empty ()
4318 && n_searched < max_to_search)
4320 tree scope = namespaces_to_search.pop ();
4321 struct scope_binding binding = EMPTY_SCOPE_BINDING;
4322 cp_binding_level *level = NAMESPACE_LEVEL (scope);
4324 /* Look in this namespace. */
4325 qualified_lookup_using_namespace (name, scope, &binding, 0);
4327 n_searched++;
4329 if (binding.value)
4330 candidates.safe_push (binding.value);
4332 /* Add child namespaces. */
4333 for (t = level->namespaces; t; t = DECL_CHAIN (t))
4334 namespaces_to_search.safe_push (t);
4337 /* If we stopped before we could examine all namespaces, inform the
4338 user. Do this even if we don't have any candidates, since there
4339 might be more candidates further down that we weren't able to
4340 find. */
4341 if (n_searched >= max_to_search
4342 && !namespaces_to_search.is_empty ())
4343 inform (location,
4344 "maximum limit of %d namespaces searched for %qE",
4345 max_to_search, name);
4347 namespaces_to_search.release ();
4349 /* Nothing useful to report. */
4350 if (candidates.is_empty ())
4351 return;
4353 inform_n (location, candidates.length (),
4354 "suggested alternative:",
4355 "suggested alternatives:");
4357 FOR_EACH_VEC_ELT (candidates, ix, t)
4358 inform (location_of (t), " %qE", t);
4360 candidates.release ();
4363 /* Unscoped lookup of a global: iterate over current namespaces,
4364 considering using-directives. */
4366 static tree
4367 unqualified_namespace_lookup_1 (tree name, int flags)
4369 tree initial = current_decl_namespace ();
4370 tree scope = initial;
4371 tree siter;
4372 cp_binding_level *level;
4373 tree val = NULL_TREE;
4375 for (; !val; scope = CP_DECL_CONTEXT (scope))
4377 struct scope_binding binding = EMPTY_SCOPE_BINDING;
4378 cxx_binding *b =
4379 cp_binding_level_find_binding_for_name (NAMESPACE_LEVEL (scope), name);
4381 if (b)
4382 ambiguous_decl (&binding, b, flags);
4384 /* Add all _DECLs seen through local using-directives. */
4385 for (level = current_binding_level;
4386 level->kind != sk_namespace;
4387 level = level->level_chain)
4388 if (!lookup_using_namespace (name, &binding, level->using_directives,
4389 scope, flags))
4390 /* Give up because of error. */
4391 return error_mark_node;
4393 /* Add all _DECLs seen through global using-directives. */
4394 /* XXX local and global using lists should work equally. */
4395 siter = initial;
4396 while (1)
4398 if (!lookup_using_namespace (name, &binding,
4399 DECL_NAMESPACE_USING (siter),
4400 scope, flags))
4401 /* Give up because of error. */
4402 return error_mark_node;
4403 if (siter == scope) break;
4404 siter = CP_DECL_CONTEXT (siter);
4407 val = binding.value;
4408 if (scope == global_namespace)
4409 break;
4411 return val;
4414 /* Wrapper for unqualified_namespace_lookup_1. */
4416 static tree
4417 unqualified_namespace_lookup (tree name, int flags)
4419 tree ret;
4420 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4421 ret = unqualified_namespace_lookup_1 (name, flags);
4422 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4423 return ret;
4426 /* Look up NAME (an IDENTIFIER_NODE) in SCOPE (either a NAMESPACE_DECL
4427 or a class TYPE). If IS_TYPE_P is TRUE, then ignore non-type
4428 bindings.
4430 Returns a DECL (or OVERLOAD, or BASELINK) representing the
4431 declaration found. If no suitable declaration can be found,
4432 ERROR_MARK_NODE is returned. If COMPLAIN is true and SCOPE is
4433 neither a class-type nor a namespace a diagnostic is issued. */
4435 tree
4436 lookup_qualified_name (tree scope, tree name, bool is_type_p, bool complain)
4438 int flags = 0;
4439 tree t = NULL_TREE;
4441 if (TREE_CODE (scope) == NAMESPACE_DECL)
4443 struct scope_binding binding = EMPTY_SCOPE_BINDING;
4445 if (is_type_p)
4446 flags |= LOOKUP_PREFER_TYPES;
4447 if (qualified_lookup_using_namespace (name, scope, &binding, flags))
4448 t = binding.value;
4450 else if (cxx_dialect != cxx98 && TREE_CODE (scope) == ENUMERAL_TYPE)
4451 t = lookup_enumerator (scope, name);
4452 else if (is_class_type (scope, complain))
4453 t = lookup_member (scope, name, 2, is_type_p, tf_warning_or_error);
4455 if (!t)
4456 return error_mark_node;
4457 return t;
4460 /* Subroutine of unqualified_namespace_lookup:
4461 Add the bindings of NAME in used namespaces to VAL.
4462 We are currently looking for names in namespace SCOPE, so we
4463 look through USINGS for using-directives of namespaces
4464 which have SCOPE as a common ancestor with the current scope.
4465 Returns false on errors. */
4467 static bool
4468 lookup_using_namespace (tree name, struct scope_binding *val,
4469 tree usings, tree scope, int flags)
4471 tree iter;
4472 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4473 /* Iterate over all used namespaces in current, searching for using
4474 directives of scope. */
4475 for (iter = usings; iter; iter = TREE_CHAIN (iter))
4476 if (TREE_VALUE (iter) == scope)
4478 tree used = ORIGINAL_NAMESPACE (TREE_PURPOSE (iter));
4479 cxx_binding *val1 =
4480 cp_binding_level_find_binding_for_name (NAMESPACE_LEVEL (used), name);
4481 /* Resolve ambiguities. */
4482 if (val1)
4483 ambiguous_decl (val, val1, flags);
4485 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4486 return val->value != error_mark_node;
4489 /* Returns true iff VEC contains TARGET. */
4491 static bool
4492 tree_vec_contains (vec<tree, va_gc> *vec, tree target)
4494 unsigned int i;
4495 tree elt;
4496 FOR_EACH_VEC_SAFE_ELT (vec,i,elt)
4497 if (elt == target)
4498 return true;
4499 return false;
4502 /* [namespace.qual]
4503 Accepts the NAME to lookup and its qualifying SCOPE.
4504 Returns the name/type pair found into the cxx_binding *RESULT,
4505 or false on error. */
4507 static bool
4508 qualified_lookup_using_namespace (tree name, tree scope,
4509 struct scope_binding *result, int flags)
4511 /* Maintain a list of namespaces visited... */
4512 vec<tree, va_gc> *seen = NULL;
4513 vec<tree, va_gc> *seen_inline = NULL;
4514 /* ... and a list of namespace yet to see. */
4515 vec<tree, va_gc> *todo = NULL;
4516 vec<tree, va_gc> *todo_maybe = NULL;
4517 vec<tree, va_gc> *todo_inline = NULL;
4518 tree usings;
4519 timevar_start (TV_NAME_LOOKUP);
4520 /* Look through namespace aliases. */
4521 scope = ORIGINAL_NAMESPACE (scope);
4523 /* Algorithm: Starting with SCOPE, walk through the set of used
4524 namespaces. For each used namespace, look through its inline
4525 namespace set for any bindings and usings. If no bindings are
4526 found, add any usings seen to the set of used namespaces. */
4527 vec_safe_push (todo, scope);
4529 while (todo->length ())
4531 bool found_here;
4532 scope = todo->pop ();
4533 if (tree_vec_contains (seen, scope))
4534 continue;
4535 vec_safe_push (seen, scope);
4536 vec_safe_push (todo_inline, scope);
4538 found_here = false;
4539 while (todo_inline->length ())
4541 cxx_binding *binding;
4543 scope = todo_inline->pop ();
4544 if (tree_vec_contains (seen_inline, scope))
4545 continue;
4546 vec_safe_push (seen_inline, scope);
4548 binding =
4549 cp_binding_level_find_binding_for_name (NAMESPACE_LEVEL (scope), name);
4550 if (binding)
4552 found_here = true;
4553 ambiguous_decl (result, binding, flags);
4556 for (usings = DECL_NAMESPACE_USING (scope); usings;
4557 usings = TREE_CHAIN (usings))
4558 if (!TREE_INDIRECT_USING (usings))
4560 if (is_associated_namespace (scope, TREE_PURPOSE (usings)))
4561 vec_safe_push (todo_inline, TREE_PURPOSE (usings));
4562 else
4563 vec_safe_push (todo_maybe, TREE_PURPOSE (usings));
4567 if (found_here)
4568 vec_safe_truncate (todo_maybe, 0);
4569 else
4570 while (vec_safe_length (todo_maybe))
4571 vec_safe_push (todo, todo_maybe->pop ());
4573 vec_free (todo);
4574 vec_free (todo_maybe);
4575 vec_free (todo_inline);
4576 vec_free (seen);
4577 vec_free (seen_inline);
4578 timevar_stop (TV_NAME_LOOKUP);
4579 return result->value != error_mark_node;
4582 /* Subroutine of outer_binding.
4584 Returns TRUE if BINDING is a binding to a template parameter of
4585 SCOPE. In that case SCOPE is the scope of a primary template
4586 parameter -- in the sense of G++, i.e, a template that has its own
4587 template header.
4589 Returns FALSE otherwise. */
4591 static bool
4592 binding_to_template_parms_of_scope_p (cxx_binding *binding,
4593 cp_binding_level *scope)
4595 tree binding_value, tmpl, tinfo;
4596 int level;
4598 if (!binding || !scope || !scope->this_entity)
4599 return false;
4601 binding_value = binding->value ? binding->value : binding->type;
4602 tinfo = get_template_info (scope->this_entity);
4604 /* BINDING_VALUE must be a template parm. */
4605 if (binding_value == NULL_TREE
4606 || (!DECL_P (binding_value)
4607 || !DECL_TEMPLATE_PARM_P (binding_value)))
4608 return false;
4610 /* The level of BINDING_VALUE. */
4611 level =
4612 template_type_parameter_p (binding_value)
4613 ? TEMPLATE_PARM_LEVEL (TEMPLATE_TYPE_PARM_INDEX
4614 (TREE_TYPE (binding_value)))
4615 : TEMPLATE_PARM_LEVEL (DECL_INITIAL (binding_value));
4617 /* The template of the current scope, iff said scope is a primary
4618 template. */
4619 tmpl = (tinfo
4620 && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
4621 ? TI_TEMPLATE (tinfo)
4622 : NULL_TREE);
4624 /* If the level of the parm BINDING_VALUE equals the depth of TMPL,
4625 then BINDING_VALUE is a parameter of TMPL. */
4626 return (tmpl && level == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)));
4629 /* Return the innermost non-namespace binding for NAME from a scope
4630 containing BINDING, or, if BINDING is NULL, the current scope.
4631 Please note that for a given template, the template parameters are
4632 considered to be in the scope containing the current scope.
4633 If CLASS_P is false, then class bindings are ignored. */
4635 cxx_binding *
4636 outer_binding (tree name,
4637 cxx_binding *binding,
4638 bool class_p)
4640 cxx_binding *outer;
4641 cp_binding_level *scope;
4642 cp_binding_level *outer_scope;
4644 if (binding)
4646 scope = binding->scope->level_chain;
4647 outer = binding->previous;
4649 else
4651 scope = current_binding_level;
4652 outer = IDENTIFIER_BINDING (name);
4654 outer_scope = outer ? outer->scope : NULL;
4656 /* Because we create class bindings lazily, we might be missing a
4657 class binding for NAME. If there are any class binding levels
4658 between the LAST_BINDING_LEVEL and the scope in which OUTER was
4659 declared, we must lookup NAME in those class scopes. */
4660 if (class_p)
4661 while (scope && scope != outer_scope && scope->kind != sk_namespace)
4663 if (scope->kind == sk_class)
4665 cxx_binding *class_binding;
4667 class_binding = get_class_binding (name, scope);
4668 if (class_binding)
4670 /* Thread this new class-scope binding onto the
4671 IDENTIFIER_BINDING list so that future lookups
4672 find it quickly. */
4673 class_binding->previous = outer;
4674 if (binding)
4675 binding->previous = class_binding;
4676 else
4677 IDENTIFIER_BINDING (name) = class_binding;
4678 return class_binding;
4681 /* If we are in a member template, the template parms of the member
4682 template are considered to be inside the scope of the containing
4683 class, but within G++ the class bindings are all pushed between the
4684 template parms and the function body. So if the outer binding is
4685 a template parm for the current scope, return it now rather than
4686 look for a class binding. */
4687 if (outer_scope && outer_scope->kind == sk_template_parms
4688 && binding_to_template_parms_of_scope_p (outer, scope))
4689 return outer;
4691 scope = scope->level_chain;
4694 return outer;
4697 /* Return the innermost block-scope or class-scope value binding for
4698 NAME, or NULL_TREE if there is no such binding. */
4700 tree
4701 innermost_non_namespace_value (tree name)
4703 cxx_binding *binding;
4704 binding = outer_binding (name, /*binding=*/NULL, /*class_p=*/true);
4705 return binding ? binding->value : NULL_TREE;
4708 /* Look up NAME in the current binding level and its superiors in the
4709 namespace of variables, functions and typedefs. Return a ..._DECL
4710 node of some kind representing its definition if there is only one
4711 such declaration, or return a TREE_LIST with all the overloaded
4712 definitions if there are many, or return 0 if it is undefined.
4713 Hidden name, either friend declaration or built-in function, are
4714 not ignored.
4716 If PREFER_TYPE is > 0, we prefer TYPE_DECLs or namespaces.
4717 If PREFER_TYPE is > 1, we reject non-type decls (e.g. namespaces).
4718 Otherwise we prefer non-TYPE_DECLs.
4720 If NONCLASS is nonzero, bindings in class scopes are ignored. If
4721 BLOCK_P is false, bindings in block scopes are ignored. */
4723 static tree
4724 lookup_name_real_1 (tree name, int prefer_type, int nonclass, bool block_p,
4725 int namespaces_only, int flags)
4727 cxx_binding *iter;
4728 tree val = NULL_TREE;
4730 /* Conversion operators are handled specially because ordinary
4731 unqualified name lookup will not find template conversion
4732 operators. */
4733 if (IDENTIFIER_TYPENAME_P (name))
4735 cp_binding_level *level;
4737 for (level = current_binding_level;
4738 level && level->kind != sk_namespace;
4739 level = level->level_chain)
4741 tree class_type;
4742 tree operators;
4744 /* A conversion operator can only be declared in a class
4745 scope. */
4746 if (level->kind != sk_class)
4747 continue;
4749 /* Lookup the conversion operator in the class. */
4750 class_type = level->this_entity;
4751 operators = lookup_fnfields (class_type, name, /*protect=*/0);
4752 if (operators)
4753 return operators;
4756 return NULL_TREE;
4759 flags |= lookup_flags (prefer_type, namespaces_only);
4761 /* First, look in non-namespace scopes. */
4763 if (current_class_type == NULL_TREE)
4764 nonclass = 1;
4766 if (block_p || !nonclass)
4767 for (iter = outer_binding (name, NULL, !nonclass);
4768 iter;
4769 iter = outer_binding (name, iter, !nonclass))
4771 tree binding;
4773 /* Skip entities we don't want. */
4774 if (LOCAL_BINDING_P (iter) ? !block_p : nonclass)
4775 continue;
4777 /* If this is the kind of thing we're looking for, we're done. */
4778 if (qualify_lookup (iter->value, flags))
4779 binding = iter->value;
4780 else if ((flags & LOOKUP_PREFER_TYPES)
4781 && qualify_lookup (iter->type, flags))
4782 binding = iter->type;
4783 else
4784 binding = NULL_TREE;
4786 if (binding)
4788 if (hidden_name_p (binding))
4790 /* A non namespace-scope binding can only be hidden in the
4791 presence of a local class, due to friend declarations.
4793 In particular, consider:
4795 struct C;
4796 void f() {
4797 struct A {
4798 friend struct B;
4799 friend struct C;
4800 void g() {
4801 B* b; // error: B is hidden
4802 C* c; // OK, finds ::C
4805 B *b; // error: B is hidden
4806 C *c; // OK, finds ::C
4807 struct B {};
4808 B *bb; // OK
4811 The standard says that "B" is a local class in "f"
4812 (but not nested within "A") -- but that name lookup
4813 for "B" does not find this declaration until it is
4814 declared directly with "f".
4816 In particular:
4818 [class.friend]
4820 If a friend declaration appears in a local class and
4821 the name specified is an unqualified name, a prior
4822 declaration is looked up without considering scopes
4823 that are outside the innermost enclosing non-class
4824 scope. For a friend function declaration, if there is
4825 no prior declaration, the program is ill-formed. For a
4826 friend class declaration, if there is no prior
4827 declaration, the class that is specified belongs to the
4828 innermost enclosing non-class scope, but if it is
4829 subsequently referenced, its name is not found by name
4830 lookup until a matching declaration is provided in the
4831 innermost enclosing nonclass scope.
4833 So just keep looking for a non-hidden binding.
4835 gcc_assert (TREE_CODE (binding) == TYPE_DECL);
4836 continue;
4838 val = binding;
4839 break;
4843 /* Now lookup in namespace scopes. */
4844 if (!val)
4845 val = unqualified_namespace_lookup (name, flags);
4847 /* If we have a single function from a using decl, pull it out. */
4848 if (val && TREE_CODE (val) == OVERLOAD && !really_overloaded_fn (val))
4849 val = OVL_FUNCTION (val);
4851 return val;
4854 /* Wrapper for lookup_name_real_1. */
4856 tree
4857 lookup_name_real (tree name, int prefer_type, int nonclass, bool block_p,
4858 int namespaces_only, int flags)
4860 tree ret;
4861 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4862 ret = lookup_name_real_1 (name, prefer_type, nonclass, block_p,
4863 namespaces_only, flags);
4864 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4865 return ret;
4868 tree
4869 lookup_name_nonclass (tree name)
4871 return lookup_name_real (name, 0, 1, /*block_p=*/true, 0, 0);
4874 tree
4875 lookup_function_nonclass (tree name, vec<tree, va_gc> *args, bool block_p)
4877 return
4878 lookup_arg_dependent (name,
4879 lookup_name_real (name, 0, 1, block_p, 0, 0),
4880 args, false);
4883 tree
4884 lookup_name (tree name)
4886 return lookup_name_real (name, 0, 0, /*block_p=*/true, 0, 0);
4889 tree
4890 lookup_name_prefer_type (tree name, int prefer_type)
4892 return lookup_name_real (name, prefer_type, 0, /*block_p=*/true, 0, 0);
4895 /* Look up NAME for type used in elaborated name specifier in
4896 the scopes given by SCOPE. SCOPE can be either TS_CURRENT or
4897 TS_WITHIN_ENCLOSING_NON_CLASS. Although not implied by the
4898 name, more scopes are checked if cleanup or template parameter
4899 scope is encountered.
4901 Unlike lookup_name_real, we make sure that NAME is actually
4902 declared in the desired scope, not from inheritance, nor using
4903 directive. For using declaration, there is DR138 still waiting
4904 to be resolved. Hidden name coming from an earlier friend
4905 declaration is also returned.
4907 A TYPE_DECL best matching the NAME is returned. Catching error
4908 and issuing diagnostics are caller's responsibility. */
4910 static tree
4911 lookup_type_scope_1 (tree name, tag_scope scope)
4913 cxx_binding *iter = NULL;
4914 tree val = NULL_TREE;
4916 /* Look in non-namespace scope first. */
4917 if (current_binding_level->kind != sk_namespace)
4918 iter = outer_binding (name, NULL, /*class_p=*/ true);
4919 for (; iter; iter = outer_binding (name, iter, /*class_p=*/ true))
4921 /* Check if this is the kind of thing we're looking for.
4922 If SCOPE is TS_CURRENT, also make sure it doesn't come from
4923 base class. For ITER->VALUE, we can simply use
4924 INHERITED_VALUE_BINDING_P. For ITER->TYPE, we have to use
4925 our own check.
4927 We check ITER->TYPE before ITER->VALUE in order to handle
4928 typedef struct C {} C;
4929 correctly. */
4931 if (qualify_lookup (iter->type, LOOKUP_PREFER_TYPES)
4932 && (scope != ts_current
4933 || LOCAL_BINDING_P (iter)
4934 || DECL_CONTEXT (iter->type) == iter->scope->this_entity))
4935 val = iter->type;
4936 else if ((scope != ts_current
4937 || !INHERITED_VALUE_BINDING_P (iter))
4938 && qualify_lookup (iter->value, LOOKUP_PREFER_TYPES))
4939 val = iter->value;
4941 if (val)
4942 break;
4945 /* Look in namespace scope. */
4946 if (!val)
4948 iter = cp_binding_level_find_binding_for_name
4949 (NAMESPACE_LEVEL (current_decl_namespace ()), name);
4951 if (iter)
4953 /* If this is the kind of thing we're looking for, we're done. */
4954 if (qualify_lookup (iter->type, LOOKUP_PREFER_TYPES))
4955 val = iter->type;
4956 else if (qualify_lookup (iter->value, LOOKUP_PREFER_TYPES))
4957 val = iter->value;
4962 /* Type found, check if it is in the allowed scopes, ignoring cleanup
4963 and template parameter scopes. */
4964 if (val)
4966 cp_binding_level *b = current_binding_level;
4967 while (b)
4969 if (iter->scope == b)
4970 return val;
4972 if (b->kind == sk_cleanup || b->kind == sk_template_parms
4973 || b->kind == sk_function_parms)
4974 b = b->level_chain;
4975 else if (b->kind == sk_class
4976 && scope == ts_within_enclosing_non_class)
4977 b = b->level_chain;
4978 else
4979 break;
4983 return NULL_TREE;
4986 /* Wrapper for lookup_type_scope_1. */
4988 tree
4989 lookup_type_scope (tree name, tag_scope scope)
4991 tree ret;
4992 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4993 ret = lookup_type_scope_1 (name, scope);
4994 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4995 return ret;
4999 /* Similar to `lookup_name' but look only in the innermost non-class
5000 binding level. */
5002 static tree
5003 lookup_name_innermost_nonclass_level_1 (tree name)
5005 cp_binding_level *b;
5006 tree t = NULL_TREE;
5008 b = innermost_nonclass_level ();
5010 if (b->kind == sk_namespace)
5012 t = IDENTIFIER_NAMESPACE_VALUE (name);
5014 /* extern "C" function() */
5015 if (t != NULL_TREE && TREE_CODE (t) == TREE_LIST)
5016 t = TREE_VALUE (t);
5018 else if (IDENTIFIER_BINDING (name)
5019 && LOCAL_BINDING_P (IDENTIFIER_BINDING (name)))
5021 cxx_binding *binding;
5022 binding = IDENTIFIER_BINDING (name);
5023 while (1)
5025 if (binding->scope == b
5026 && !(VAR_P (binding->value)
5027 && DECL_DEAD_FOR_LOCAL (binding->value)))
5028 return binding->value;
5030 if (b->kind == sk_cleanup)
5031 b = b->level_chain;
5032 else
5033 break;
5037 return t;
5040 /* Wrapper for lookup_name_innermost_nonclass_level_1. */
5042 tree
5043 lookup_name_innermost_nonclass_level (tree name)
5045 tree ret;
5046 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
5047 ret = lookup_name_innermost_nonclass_level_1 (name);
5048 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
5049 return ret;
5053 /* Returns true iff DECL is a block-scope extern declaration of a function
5054 or variable. */
5056 bool
5057 is_local_extern (tree decl)
5059 cxx_binding *binding;
5061 /* For functions, this is easy. */
5062 if (TREE_CODE (decl) == FUNCTION_DECL)
5063 return DECL_LOCAL_FUNCTION_P (decl);
5065 if (!VAR_P (decl))
5066 return false;
5067 if (!current_function_decl)
5068 return false;
5070 /* For variables, this is not easy. We need to look at the binding stack
5071 for the identifier to see whether the decl we have is a local. */
5072 for (binding = IDENTIFIER_BINDING (DECL_NAME (decl));
5073 binding && binding->scope->kind != sk_namespace;
5074 binding = binding->previous)
5075 if (binding->value == decl)
5076 return LOCAL_BINDING_P (binding);
5078 return false;
5081 /* Like lookup_name_innermost_nonclass_level, but for types. */
5083 static tree
5084 lookup_type_current_level (tree name)
5086 tree t = NULL_TREE;
5088 timevar_start (TV_NAME_LOOKUP);
5089 gcc_assert (current_binding_level->kind != sk_namespace);
5091 if (REAL_IDENTIFIER_TYPE_VALUE (name) != NULL_TREE
5092 && REAL_IDENTIFIER_TYPE_VALUE (name) != global_type_node)
5094 cp_binding_level *b = current_binding_level;
5095 while (1)
5097 if (purpose_member (name, b->type_shadowed))
5099 t = REAL_IDENTIFIER_TYPE_VALUE (name);
5100 break;
5102 if (b->kind == sk_cleanup)
5103 b = b->level_chain;
5104 else
5105 break;
5109 timevar_stop (TV_NAME_LOOKUP);
5110 return t;
5113 /* [basic.lookup.koenig] */
5114 /* A nonzero return value in the functions below indicates an error. */
5116 struct arg_lookup
5118 tree name;
5119 vec<tree, va_gc> *args;
5120 vec<tree, va_gc> *namespaces;
5121 vec<tree, va_gc> *classes;
5122 tree functions;
5123 struct pointer_set_t *fn_set;
5126 static bool arg_assoc (struct arg_lookup*, tree);
5127 static bool arg_assoc_args (struct arg_lookup*, tree);
5128 static bool arg_assoc_args_vec (struct arg_lookup*, vec<tree, va_gc> *);
5129 static bool arg_assoc_type (struct arg_lookup*, tree);
5130 static bool add_function (struct arg_lookup *, tree);
5131 static bool arg_assoc_namespace (struct arg_lookup *, tree);
5132 static bool arg_assoc_class_only (struct arg_lookup *, tree);
5133 static bool arg_assoc_bases (struct arg_lookup *, tree);
5134 static bool arg_assoc_class (struct arg_lookup *, tree);
5135 static bool arg_assoc_template_arg (struct arg_lookup*, tree);
5137 /* Add a function to the lookup structure.
5138 Returns true on error. */
5140 static bool
5141 add_function (struct arg_lookup *k, tree fn)
5143 if (!is_overloaded_fn (fn))
5144 /* All names except those of (possibly overloaded) functions and
5145 function templates are ignored. */;
5146 else if (k->fn_set && pointer_set_insert (k->fn_set, fn))
5147 /* It's already in the list. */;
5148 else if (!k->functions)
5149 k->functions = fn;
5150 else if (fn == k->functions)
5152 else
5154 k->functions = build_overload (fn, k->functions);
5155 if (TREE_CODE (k->functions) == OVERLOAD)
5156 OVL_ARG_DEPENDENT (k->functions) = true;
5159 return false;
5162 /* Returns true iff CURRENT has declared itself to be an associated
5163 namespace of SCOPE via a strong using-directive (or transitive chain
5164 thereof). Both are namespaces. */
5166 bool
5167 is_associated_namespace (tree current, tree scope)
5169 vec<tree, va_gc> *seen = make_tree_vector ();
5170 vec<tree, va_gc> *todo = make_tree_vector ();
5171 tree t;
5172 bool ret;
5174 while (1)
5176 if (scope == current)
5178 ret = true;
5179 break;
5181 vec_safe_push (seen, scope);
5182 for (t = DECL_NAMESPACE_ASSOCIATIONS (scope); t; t = TREE_CHAIN (t))
5183 if (!vec_member (TREE_PURPOSE (t), seen))
5184 vec_safe_push (todo, TREE_PURPOSE (t));
5185 if (!todo->is_empty ())
5187 scope = todo->last ();
5188 todo->pop ();
5190 else
5192 ret = false;
5193 break;
5197 release_tree_vector (seen);
5198 release_tree_vector (todo);
5200 return ret;
5203 /* Add functions of a namespace to the lookup structure.
5204 Returns true on error. */
5206 static bool
5207 arg_assoc_namespace (struct arg_lookup *k, tree scope)
5209 tree value;
5211 if (vec_member (scope, k->namespaces))
5212 return false;
5213 vec_safe_push (k->namespaces, scope);
5215 /* Check out our super-users. */
5216 for (value = DECL_NAMESPACE_ASSOCIATIONS (scope); value;
5217 value = TREE_CHAIN (value))
5218 if (arg_assoc_namespace (k, TREE_PURPOSE (value)))
5219 return true;
5221 /* Also look down into inline namespaces. */
5222 for (value = DECL_NAMESPACE_USING (scope); value;
5223 value = TREE_CHAIN (value))
5224 if (is_associated_namespace (scope, TREE_PURPOSE (value)))
5225 if (arg_assoc_namespace (k, TREE_PURPOSE (value)))
5226 return true;
5228 value = namespace_binding (k->name, scope);
5229 if (!value)
5230 return false;
5232 for (; value; value = OVL_NEXT (value))
5234 /* We don't want to find arbitrary hidden functions via argument
5235 dependent lookup. We only want to find friends of associated
5236 classes, which we'll do via arg_assoc_class. */
5237 if (hidden_name_p (OVL_CURRENT (value)))
5238 continue;
5240 if (add_function (k, OVL_CURRENT (value)))
5241 return true;
5244 return false;
5247 /* Adds everything associated with a template argument to the lookup
5248 structure. Returns true on error. */
5250 static bool
5251 arg_assoc_template_arg (struct arg_lookup *k, tree arg)
5253 /* [basic.lookup.koenig]
5255 If T is a template-id, its associated namespaces and classes are
5256 ... the namespaces and classes associated with the types of the
5257 template arguments provided for template type parameters
5258 (excluding template template parameters); the namespaces in which
5259 any template template arguments are defined; and the classes in
5260 which any member templates used as template template arguments
5261 are defined. [Note: non-type template arguments do not
5262 contribute to the set of associated namespaces. ] */
5264 /* Consider first template template arguments. */
5265 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
5266 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
5267 return false;
5268 else if (TREE_CODE (arg) == TEMPLATE_DECL)
5270 tree ctx = CP_DECL_CONTEXT (arg);
5272 /* It's not a member template. */
5273 if (TREE_CODE (ctx) == NAMESPACE_DECL)
5274 return arg_assoc_namespace (k, ctx);
5275 /* Otherwise, it must be member template. */
5276 else
5277 return arg_assoc_class_only (k, ctx);
5279 /* It's an argument pack; handle it recursively. */
5280 else if (ARGUMENT_PACK_P (arg))
5282 tree args = ARGUMENT_PACK_ARGS (arg);
5283 int i, len = TREE_VEC_LENGTH (args);
5284 for (i = 0; i < len; ++i)
5285 if (arg_assoc_template_arg (k, TREE_VEC_ELT (args, i)))
5286 return true;
5288 return false;
5290 /* It's not a template template argument, but it is a type template
5291 argument. */
5292 else if (TYPE_P (arg))
5293 return arg_assoc_type (k, arg);
5294 /* It's a non-type template argument. */
5295 else
5296 return false;
5299 /* Adds the class and its friends to the lookup structure.
5300 Returns true on error. */
5302 static bool
5303 arg_assoc_class_only (struct arg_lookup *k, tree type)
5305 tree list, friends, context;
5307 /* Backend-built structures, such as __builtin_va_list, aren't
5308 affected by all this. */
5309 if (!CLASS_TYPE_P (type))
5310 return false;
5312 context = decl_namespace_context (type);
5313 if (arg_assoc_namespace (k, context))
5314 return true;
5316 complete_type (type);
5318 /* Process friends. */
5319 for (list = DECL_FRIENDLIST (TYPE_MAIN_DECL (type)); list;
5320 list = TREE_CHAIN (list))
5321 if (k->name == FRIEND_NAME (list))
5322 for (friends = FRIEND_DECLS (list); friends;
5323 friends = TREE_CHAIN (friends))
5325 tree fn = TREE_VALUE (friends);
5327 /* Only interested in global functions with potentially hidden
5328 (i.e. unqualified) declarations. */
5329 if (CP_DECL_CONTEXT (fn) != context)
5330 continue;
5331 /* Template specializations are never found by name lookup.
5332 (Templates themselves can be found, but not template
5333 specializations.) */
5334 if (TREE_CODE (fn) == FUNCTION_DECL && DECL_USE_TEMPLATE (fn))
5335 continue;
5336 if (add_function (k, fn))
5337 return true;
5340 return false;
5343 /* Adds the class and its bases to the lookup structure.
5344 Returns true on error. */
5346 static bool
5347 arg_assoc_bases (struct arg_lookup *k, tree type)
5349 if (arg_assoc_class_only (k, type))
5350 return true;
5352 if (TYPE_BINFO (type))
5354 /* Process baseclasses. */
5355 tree binfo, base_binfo;
5356 int i;
5358 for (binfo = TYPE_BINFO (type), i = 0;
5359 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
5360 if (arg_assoc_bases (k, BINFO_TYPE (base_binfo)))
5361 return true;
5364 return false;
5367 /* Adds everything associated with a class argument type to the lookup
5368 structure. Returns true on error.
5370 If T is a class type (including unions), its associated classes are: the
5371 class itself; the class of which it is a member, if any; and its direct
5372 and indirect base classes. Its associated namespaces are the namespaces
5373 of which its associated classes are members. Furthermore, if T is a
5374 class template specialization, its associated namespaces and classes
5375 also include: the namespaces and classes associated with the types of
5376 the template arguments provided for template type parameters (excluding
5377 template template parameters); the namespaces of which any template
5378 template arguments are members; and the classes of which any member
5379 templates used as template template arguments are members. [ Note:
5380 non-type template arguments do not contribute to the set of associated
5381 namespaces. --end note] */
5383 static bool
5384 arg_assoc_class (struct arg_lookup *k, tree type)
5386 tree list;
5387 int i;
5389 /* Backend build structures, such as __builtin_va_list, aren't
5390 affected by all this. */
5391 if (!CLASS_TYPE_P (type))
5392 return false;
5394 if (vec_member (type, k->classes))
5395 return false;
5396 vec_safe_push (k->classes, type);
5398 if (TYPE_CLASS_SCOPE_P (type)
5399 && arg_assoc_class_only (k, TYPE_CONTEXT (type)))
5400 return true;
5402 if (arg_assoc_bases (k, type))
5403 return true;
5405 /* Process template arguments. */
5406 if (CLASSTYPE_TEMPLATE_INFO (type)
5407 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
5409 list = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
5410 for (i = 0; i < TREE_VEC_LENGTH (list); ++i)
5411 if (arg_assoc_template_arg (k, TREE_VEC_ELT (list, i)))
5412 return true;
5415 return false;
5418 /* Adds everything associated with a given type.
5419 Returns 1 on error. */
5421 static bool
5422 arg_assoc_type (struct arg_lookup *k, tree type)
5424 /* As we do not get the type of non-type dependent expressions
5425 right, we can end up with such things without a type. */
5426 if (!type)
5427 return false;
5429 if (TYPE_PTRDATAMEM_P (type))
5431 /* Pointer to member: associate class type and value type. */
5432 if (arg_assoc_type (k, TYPE_PTRMEM_CLASS_TYPE (type)))
5433 return true;
5434 return arg_assoc_type (k, TYPE_PTRMEM_POINTED_TO_TYPE (type));
5436 else switch (TREE_CODE (type))
5438 case ERROR_MARK:
5439 return false;
5440 case VOID_TYPE:
5441 case INTEGER_TYPE:
5442 case REAL_TYPE:
5443 case COMPLEX_TYPE:
5444 case VECTOR_TYPE:
5445 case BOOLEAN_TYPE:
5446 case FIXED_POINT_TYPE:
5447 case DECLTYPE_TYPE:
5448 case NULLPTR_TYPE:
5449 return false;
5450 case RECORD_TYPE:
5451 if (TYPE_PTRMEMFUNC_P (type))
5452 return arg_assoc_type (k, TYPE_PTRMEMFUNC_FN_TYPE (type));
5453 case UNION_TYPE:
5454 return arg_assoc_class (k, type);
5455 case POINTER_TYPE:
5456 case REFERENCE_TYPE:
5457 case ARRAY_TYPE:
5458 return arg_assoc_type (k, TREE_TYPE (type));
5459 case ENUMERAL_TYPE:
5460 if (TYPE_CLASS_SCOPE_P (type)
5461 && arg_assoc_class_only (k, TYPE_CONTEXT (type)))
5462 return true;
5463 return arg_assoc_namespace (k, decl_namespace_context (type));
5464 case METHOD_TYPE:
5465 /* The basetype is referenced in the first arg type, so just
5466 fall through. */
5467 case FUNCTION_TYPE:
5468 /* Associate the parameter types. */
5469 if (arg_assoc_args (k, TYPE_ARG_TYPES (type)))
5470 return true;
5471 /* Associate the return type. */
5472 return arg_assoc_type (k, TREE_TYPE (type));
5473 case TEMPLATE_TYPE_PARM:
5474 case BOUND_TEMPLATE_TEMPLATE_PARM:
5475 return false;
5476 case TYPENAME_TYPE:
5477 return false;
5478 case LANG_TYPE:
5479 gcc_assert (type == unknown_type_node
5480 || type == init_list_type_node);
5481 return false;
5482 case TYPE_PACK_EXPANSION:
5483 return arg_assoc_type (k, PACK_EXPANSION_PATTERN (type));
5485 default:
5486 gcc_unreachable ();
5488 return false;
5491 /* Adds everything associated with arguments. Returns true on error. */
5493 static bool
5494 arg_assoc_args (struct arg_lookup *k, tree args)
5496 for (; args; args = TREE_CHAIN (args))
5497 if (arg_assoc (k, TREE_VALUE (args)))
5498 return true;
5499 return false;
5502 /* Adds everything associated with an argument vector. Returns true
5503 on error. */
5505 static bool
5506 arg_assoc_args_vec (struct arg_lookup *k, vec<tree, va_gc> *args)
5508 unsigned int ix;
5509 tree arg;
5511 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
5512 if (arg_assoc (k, arg))
5513 return true;
5514 return false;
5517 /* Adds everything associated with a given tree_node. Returns 1 on error. */
5519 static bool
5520 arg_assoc (struct arg_lookup *k, tree n)
5522 if (n == error_mark_node)
5523 return false;
5525 if (TYPE_P (n))
5526 return arg_assoc_type (k, n);
5528 if (! type_unknown_p (n))
5529 return arg_assoc_type (k, TREE_TYPE (n));
5531 if (TREE_CODE (n) == ADDR_EXPR)
5532 n = TREE_OPERAND (n, 0);
5533 if (TREE_CODE (n) == COMPONENT_REF)
5534 n = TREE_OPERAND (n, 1);
5535 if (TREE_CODE (n) == OFFSET_REF)
5536 n = TREE_OPERAND (n, 1);
5537 while (TREE_CODE (n) == TREE_LIST)
5538 n = TREE_VALUE (n);
5539 if (BASELINK_P (n))
5540 n = BASELINK_FUNCTIONS (n);
5542 if (TREE_CODE (n) == FUNCTION_DECL)
5543 return arg_assoc_type (k, TREE_TYPE (n));
5544 if (TREE_CODE (n) == TEMPLATE_ID_EXPR)
5546 /* The working paper doesn't currently say how to handle template-id
5547 arguments. The sensible thing would seem to be to handle the list
5548 of template candidates like a normal overload set, and handle the
5549 template arguments like we do for class template
5550 specializations. */
5551 tree templ = TREE_OPERAND (n, 0);
5552 tree args = TREE_OPERAND (n, 1);
5553 int ix;
5555 /* First the templates. */
5556 if (arg_assoc (k, templ))
5557 return true;
5559 /* Now the arguments. */
5560 if (args)
5561 for (ix = TREE_VEC_LENGTH (args); ix--;)
5562 if (arg_assoc_template_arg (k, TREE_VEC_ELT (args, ix)) == 1)
5563 return true;
5565 else if (TREE_CODE (n) == OVERLOAD)
5567 for (; n; n = OVL_NEXT (n))
5568 if (arg_assoc_type (k, TREE_TYPE (OVL_CURRENT (n))))
5569 return true;
5572 return false;
5575 /* Performs Koenig lookup depending on arguments, where fns
5576 are the functions found in normal lookup. */
5578 static tree
5579 lookup_arg_dependent_1 (tree name, tree fns, vec<tree, va_gc> *args,
5580 bool include_std)
5582 struct arg_lookup k;
5584 /* Remove any hidden friend functions from the list of functions
5585 found so far. They will be added back by arg_assoc_class as
5586 appropriate. */
5587 fns = remove_hidden_names (fns);
5589 k.name = name;
5590 k.args = args;
5591 k.functions = fns;
5592 k.classes = make_tree_vector ();
5594 /* We previously performed an optimization here by setting
5595 NAMESPACES to the current namespace when it was safe. However, DR
5596 164 says that namespaces that were already searched in the first
5597 stage of template processing are searched again (potentially
5598 picking up later definitions) in the second stage. */
5599 k.namespaces = make_tree_vector ();
5601 /* We used to allow duplicates and let joust discard them, but
5602 since the above change for DR 164 we end up with duplicates of
5603 all the functions found by unqualified lookup. So keep track
5604 of which ones we've seen. */
5605 if (fns)
5607 tree ovl;
5608 /* We shouldn't be here if lookup found something other than
5609 namespace-scope functions. */
5610 gcc_assert (DECL_NAMESPACE_SCOPE_P (OVL_CURRENT (fns)));
5611 k.fn_set = pointer_set_create ();
5612 for (ovl = fns; ovl; ovl = OVL_NEXT (ovl))
5613 pointer_set_insert (k.fn_set, OVL_CURRENT (ovl));
5615 else
5616 k.fn_set = NULL;
5618 if (include_std)
5619 arg_assoc_namespace (&k, std_node);
5620 arg_assoc_args_vec (&k, args);
5622 fns = k.functions;
5624 if (fns
5625 && !VAR_P (fns)
5626 && !is_overloaded_fn (fns))
5628 error ("argument dependent lookup finds %q+D", fns);
5629 error (" in call to %qD", name);
5630 fns = error_mark_node;
5633 release_tree_vector (k.classes);
5634 release_tree_vector (k.namespaces);
5635 if (k.fn_set)
5636 pointer_set_destroy (k.fn_set);
5638 return fns;
5641 /* Wrapper for lookup_arg_dependent_1. */
5643 tree
5644 lookup_arg_dependent (tree name, tree fns, vec<tree, va_gc> *args,
5645 bool include_std)
5647 tree ret;
5648 bool subtime;
5649 subtime = timevar_cond_start (TV_NAME_LOOKUP);
5650 ret = lookup_arg_dependent_1 (name, fns, args, include_std);
5651 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
5652 return ret;
5656 /* Add namespace to using_directives. Return NULL_TREE if nothing was
5657 changed (i.e. there was already a directive), or the fresh
5658 TREE_LIST otherwise. */
5660 static tree
5661 push_using_directive_1 (tree used)
5663 tree ud = current_binding_level->using_directives;
5664 tree iter, ancestor;
5666 /* Check if we already have this. */
5667 if (purpose_member (used, ud) != NULL_TREE)
5668 return NULL_TREE;
5670 ancestor = namespace_ancestor (current_decl_namespace (), used);
5671 ud = current_binding_level->using_directives;
5672 ud = tree_cons (used, ancestor, ud);
5673 current_binding_level->using_directives = ud;
5675 /* Recursively add all namespaces used. */
5676 for (iter = DECL_NAMESPACE_USING (used); iter; iter = TREE_CHAIN (iter))
5677 push_using_directive (TREE_PURPOSE (iter));
5679 return ud;
5682 /* Wrapper for push_using_directive_1. */
5684 static tree
5685 push_using_directive (tree used)
5687 tree ret;
5688 timevar_start (TV_NAME_LOOKUP);
5689 ret = push_using_directive_1 (used);
5690 timevar_stop (TV_NAME_LOOKUP);
5691 return ret;
5694 /* The type TYPE is being declared. If it is a class template, or a
5695 specialization of a class template, do any processing required and
5696 perform error-checking. If IS_FRIEND is nonzero, this TYPE is
5697 being declared a friend. B is the binding level at which this TYPE
5698 should be bound.
5700 Returns the TYPE_DECL for TYPE, which may have been altered by this
5701 processing. */
5703 static tree
5704 maybe_process_template_type_declaration (tree type, int is_friend,
5705 cp_binding_level *b)
5707 tree decl = TYPE_NAME (type);
5709 if (processing_template_parmlist)
5710 /* You can't declare a new template type in a template parameter
5711 list. But, you can declare a non-template type:
5713 template <class A*> struct S;
5715 is a forward-declaration of `A'. */
5717 else if (b->kind == sk_namespace
5718 && current_binding_level->kind != sk_namespace)
5719 /* If this new type is being injected into a containing scope,
5720 then it's not a template type. */
5722 else
5724 gcc_assert (MAYBE_CLASS_TYPE_P (type)
5725 || TREE_CODE (type) == ENUMERAL_TYPE);
5727 if (processing_template_decl)
5729 /* This may change after the call to
5730 push_template_decl_real, but we want the original value. */
5731 tree name = DECL_NAME (decl);
5733 decl = push_template_decl_real (decl, is_friend);
5734 if (decl == error_mark_node)
5735 return error_mark_node;
5737 /* If the current binding level is the binding level for the
5738 template parameters (see the comment in
5739 begin_template_parm_list) and the enclosing level is a class
5740 scope, and we're not looking at a friend, push the
5741 declaration of the member class into the class scope. In the
5742 friend case, push_template_decl will already have put the
5743 friend into global scope, if appropriate. */
5744 if (TREE_CODE (type) != ENUMERAL_TYPE
5745 && !is_friend && b->kind == sk_template_parms
5746 && b->level_chain->kind == sk_class)
5748 finish_member_declaration (CLASSTYPE_TI_TEMPLATE (type));
5750 if (!COMPLETE_TYPE_P (current_class_type))
5752 maybe_add_class_template_decl_list (current_class_type,
5753 type, /*friend_p=*/0);
5754 /* Put this UTD in the table of UTDs for the class. */
5755 if (CLASSTYPE_NESTED_UTDS (current_class_type) == NULL)
5756 CLASSTYPE_NESTED_UTDS (current_class_type) =
5757 binding_table_new (SCOPE_DEFAULT_HT_SIZE);
5759 binding_table_insert
5760 (CLASSTYPE_NESTED_UTDS (current_class_type), name, type);
5766 return decl;
5769 /* Push a tag name NAME for struct/class/union/enum type TYPE. In case
5770 that the NAME is a class template, the tag is processed but not pushed.
5772 The pushed scope depend on the SCOPE parameter:
5773 - When SCOPE is TS_CURRENT, put it into the inner-most non-sk_cleanup
5774 scope.
5775 - When SCOPE is TS_GLOBAL, put it in the inner-most non-class and
5776 non-template-parameter scope. This case is needed for forward
5777 declarations.
5778 - When SCOPE is TS_WITHIN_ENCLOSING_NON_CLASS, this is similar to
5779 TS_GLOBAL case except that names within template-parameter scopes
5780 are not pushed at all.
5782 Returns TYPE upon success and ERROR_MARK_NODE otherwise. */
5784 static tree
5785 pushtag_1 (tree name, tree type, tag_scope scope)
5787 cp_binding_level *b;
5788 tree decl;
5790 b = current_binding_level;
5791 while (/* Cleanup scopes are not scopes from the point of view of
5792 the language. */
5793 b->kind == sk_cleanup
5794 /* Neither are function parameter scopes. */
5795 || b->kind == sk_function_parms
5796 /* Neither are the scopes used to hold template parameters
5797 for an explicit specialization. For an ordinary template
5798 declaration, these scopes are not scopes from the point of
5799 view of the language. */
5800 || (b->kind == sk_template_parms
5801 && (b->explicit_spec_p || scope == ts_global))
5802 || (b->kind == sk_class
5803 && (scope != ts_current
5804 /* We may be defining a new type in the initializer
5805 of a static member variable. We allow this when
5806 not pedantic, and it is particularly useful for
5807 type punning via an anonymous union. */
5808 || COMPLETE_TYPE_P (b->this_entity))))
5809 b = b->level_chain;
5811 gcc_assert (identifier_p (name));
5813 /* Do C++ gratuitous typedefing. */
5814 if (identifier_type_value_1 (name) != type)
5816 tree tdef;
5817 int in_class = 0;
5818 tree context = TYPE_CONTEXT (type);
5820 if (! context)
5822 tree cs = current_scope ();
5824 if (scope == ts_current
5825 || (cs && TREE_CODE (cs) == FUNCTION_DECL))
5826 context = cs;
5827 else if (cs != NULL_TREE && TYPE_P (cs))
5828 /* When declaring a friend class of a local class, we want
5829 to inject the newly named class into the scope
5830 containing the local class, not the namespace
5831 scope. */
5832 context = decl_function_context (get_type_decl (cs));
5834 if (!context)
5835 context = current_namespace;
5837 if (b->kind == sk_class
5838 || (b->kind == sk_template_parms
5839 && b->level_chain->kind == sk_class))
5840 in_class = 1;
5842 if (current_lang_name == lang_name_java)
5843 TYPE_FOR_JAVA (type) = 1;
5845 tdef = create_implicit_typedef (name, type);
5846 DECL_CONTEXT (tdef) = FROB_CONTEXT (context);
5847 if (scope == ts_within_enclosing_non_class)
5849 /* This is a friend. Make this TYPE_DECL node hidden from
5850 ordinary name lookup. Its corresponding TEMPLATE_DECL
5851 will be marked in push_template_decl_real. */
5852 retrofit_lang_decl (tdef);
5853 DECL_ANTICIPATED (tdef) = 1;
5854 DECL_FRIEND_P (tdef) = 1;
5857 decl = maybe_process_template_type_declaration
5858 (type, scope == ts_within_enclosing_non_class, b);
5859 if (decl == error_mark_node)
5860 return decl;
5862 if (b->kind == sk_class)
5864 if (!TYPE_BEING_DEFINED (current_class_type))
5865 return error_mark_node;
5867 if (!PROCESSING_REAL_TEMPLATE_DECL_P ())
5868 /* Put this TYPE_DECL on the TYPE_FIELDS list for the
5869 class. But if it's a member template class, we want
5870 the TEMPLATE_DECL, not the TYPE_DECL, so this is done
5871 later. */
5872 finish_member_declaration (decl);
5873 else
5874 pushdecl_class_level (decl);
5876 else if (b->kind != sk_template_parms)
5878 decl = pushdecl_with_scope_1 (decl, b, /*is_friend=*/false);
5879 if (decl == error_mark_node)
5880 return decl;
5883 if (! in_class)
5884 set_identifier_type_value_with_scope (name, tdef, b);
5886 TYPE_CONTEXT (type) = DECL_CONTEXT (decl);
5888 /* If this is a local class, keep track of it. We need this
5889 information for name-mangling, and so that it is possible to
5890 find all function definitions in a translation unit in a
5891 convenient way. (It's otherwise tricky to find a member
5892 function definition it's only pointed to from within a local
5893 class.) */
5894 if (TYPE_FUNCTION_SCOPE_P (type))
5896 if (processing_template_decl)
5898 /* Push a DECL_EXPR so we call pushtag at the right time in
5899 template instantiation rather than in some nested context. */
5900 add_decl_expr (decl);
5902 else
5903 vec_safe_push (local_classes, type);
5906 if (b->kind == sk_class
5907 && !COMPLETE_TYPE_P (current_class_type))
5909 maybe_add_class_template_decl_list (current_class_type,
5910 type, /*friend_p=*/0);
5912 if (CLASSTYPE_NESTED_UTDS (current_class_type) == NULL)
5913 CLASSTYPE_NESTED_UTDS (current_class_type)
5914 = binding_table_new (SCOPE_DEFAULT_HT_SIZE);
5916 binding_table_insert
5917 (CLASSTYPE_NESTED_UTDS (current_class_type), name, type);
5920 decl = TYPE_NAME (type);
5921 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
5923 /* Set type visibility now if this is a forward declaration. */
5924 TREE_PUBLIC (decl) = 1;
5925 determine_visibility (decl);
5927 return type;
5930 /* Wrapper for pushtag_1. */
5932 tree
5933 pushtag (tree name, tree type, tag_scope scope)
5935 tree ret;
5936 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
5937 ret = pushtag_1 (name, type, scope);
5938 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
5939 return ret;
5942 /* Subroutines for reverting temporarily to top-level for instantiation
5943 of templates and such. We actually need to clear out the class- and
5944 local-value slots of all identifiers, so that only the global values
5945 are at all visible. Simply setting current_binding_level to the global
5946 scope isn't enough, because more binding levels may be pushed. */
5947 struct saved_scope *scope_chain;
5949 /* Return true if ID has not already been marked. */
5951 static inline bool
5952 store_binding_p (tree id)
5954 if (!id || !IDENTIFIER_BINDING (id))
5955 return false;
5957 if (IDENTIFIER_MARKED (id))
5958 return false;
5960 return true;
5963 /* Add an appropriate binding to *OLD_BINDINGS which needs to already
5964 have enough space reserved. */
5966 static void
5967 store_binding (tree id, vec<cxx_saved_binding, va_gc> **old_bindings)
5969 cxx_saved_binding saved;
5971 gcc_checking_assert (store_binding_p (id));
5973 IDENTIFIER_MARKED (id) = 1;
5975 saved.identifier = id;
5976 saved.binding = IDENTIFIER_BINDING (id);
5977 saved.real_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
5978 (*old_bindings)->quick_push (saved);
5979 IDENTIFIER_BINDING (id) = NULL;
5982 static void
5983 store_bindings (tree names, vec<cxx_saved_binding, va_gc> **old_bindings)
5985 static vec<tree> bindings_need_stored = vNULL;
5986 tree t, id;
5987 size_t i;
5989 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
5990 for (t = names; t; t = TREE_CHAIN (t))
5992 if (TREE_CODE (t) == TREE_LIST)
5993 id = TREE_PURPOSE (t);
5994 else
5995 id = DECL_NAME (t);
5997 if (store_binding_p (id))
5998 bindings_need_stored.safe_push (id);
6000 if (!bindings_need_stored.is_empty ())
6002 vec_safe_reserve_exact (*old_bindings, bindings_need_stored.length ());
6003 for (i = 0; bindings_need_stored.iterate (i, &id); ++i)
6005 /* We can appearantly have duplicates in NAMES. */
6006 if (store_binding_p (id))
6007 store_binding (id, old_bindings);
6009 bindings_need_stored.truncate (0);
6011 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6014 /* Like store_bindings, but NAMES is a vector of cp_class_binding
6015 objects, rather than a TREE_LIST. */
6017 static void
6018 store_class_bindings (vec<cp_class_binding, va_gc> *names,
6019 vec<cxx_saved_binding, va_gc> **old_bindings)
6021 static vec<tree> bindings_need_stored = vNULL;
6022 size_t i;
6023 cp_class_binding *cb;
6025 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6026 for (i = 0; vec_safe_iterate (names, i, &cb); ++i)
6027 if (store_binding_p (cb->identifier))
6028 bindings_need_stored.safe_push (cb->identifier);
6029 if (!bindings_need_stored.is_empty ())
6031 tree id;
6032 vec_safe_reserve_exact (*old_bindings, bindings_need_stored.length ());
6033 for (i = 0; bindings_need_stored.iterate (i, &id); ++i)
6034 store_binding (id, old_bindings);
6035 bindings_need_stored.truncate (0);
6037 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6040 void
6041 push_to_top_level (void)
6043 struct saved_scope *s;
6044 cp_binding_level *b;
6045 cxx_saved_binding *sb;
6046 size_t i;
6047 bool need_pop;
6049 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6050 s = ggc_alloc_cleared_saved_scope ();
6052 b = scope_chain ? current_binding_level : 0;
6054 /* If we're in the middle of some function, save our state. */
6055 if (cfun)
6057 need_pop = true;
6058 push_function_context ();
6060 else
6061 need_pop = false;
6063 if (scope_chain && previous_class_level)
6064 store_class_bindings (previous_class_level->class_shadowed,
6065 &s->old_bindings);
6067 /* Have to include the global scope, because class-scope decls
6068 aren't listed anywhere useful. */
6069 for (; b; b = b->level_chain)
6071 tree t;
6073 /* Template IDs are inserted into the global level. If they were
6074 inserted into namespace level, finish_file wouldn't find them
6075 when doing pending instantiations. Therefore, don't stop at
6076 namespace level, but continue until :: . */
6077 if (global_scope_p (b))
6078 break;
6080 store_bindings (b->names, &s->old_bindings);
6081 /* We also need to check class_shadowed to save class-level type
6082 bindings, since pushclass doesn't fill in b->names. */
6083 if (b->kind == sk_class)
6084 store_class_bindings (b->class_shadowed, &s->old_bindings);
6086 /* Unwind type-value slots back to top level. */
6087 for (t = b->type_shadowed; t; t = TREE_CHAIN (t))
6088 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (t), TREE_VALUE (t));
6091 FOR_EACH_VEC_SAFE_ELT (s->old_bindings, i, sb)
6092 IDENTIFIER_MARKED (sb->identifier) = 0;
6094 s->prev = scope_chain;
6095 s->bindings = b;
6096 s->need_pop_function_context = need_pop;
6097 s->function_decl = current_function_decl;
6098 s->unevaluated_operand = cp_unevaluated_operand;
6099 s->inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
6100 s->x_stmt_tree.stmts_are_full_exprs_p = true;
6102 scope_chain = s;
6103 current_function_decl = NULL_TREE;
6104 vec_alloc (current_lang_base, 10);
6105 current_lang_name = lang_name_cplusplus;
6106 current_namespace = global_namespace;
6107 push_class_stack ();
6108 cp_unevaluated_operand = 0;
6109 c_inhibit_evaluation_warnings = 0;
6110 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6113 static void
6114 pop_from_top_level_1 (void)
6116 struct saved_scope *s = scope_chain;
6117 cxx_saved_binding *saved;
6118 size_t i;
6120 /* Clear out class-level bindings cache. */
6121 if (previous_class_level)
6122 invalidate_class_lookup_cache ();
6123 pop_class_stack ();
6125 current_lang_base = 0;
6127 scope_chain = s->prev;
6128 FOR_EACH_VEC_SAFE_ELT (s->old_bindings, i, saved)
6130 tree id = saved->identifier;
6132 IDENTIFIER_BINDING (id) = saved->binding;
6133 SET_IDENTIFIER_TYPE_VALUE (id, saved->real_type_value);
6136 /* If we were in the middle of compiling a function, restore our
6137 state. */
6138 if (s->need_pop_function_context)
6139 pop_function_context ();
6140 current_function_decl = s->function_decl;
6141 cp_unevaluated_operand = s->unevaluated_operand;
6142 c_inhibit_evaluation_warnings = s->inhibit_evaluation_warnings;
6145 /* Wrapper for pop_from_top_level_1. */
6147 void
6148 pop_from_top_level (void)
6150 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6151 pop_from_top_level_1 ();
6152 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6156 /* Pop off extraneous binding levels left over due to syntax errors.
6158 We don't pop past namespaces, as they might be valid. */
6160 void
6161 pop_everything (void)
6163 if (ENABLE_SCOPE_CHECKING)
6164 verbatim ("XXX entering pop_everything ()\n");
6165 while (!toplevel_bindings_p ())
6167 if (current_binding_level->kind == sk_class)
6168 pop_nested_class ();
6169 else
6170 poplevel (0, 0, 0);
6172 if (ENABLE_SCOPE_CHECKING)
6173 verbatim ("XXX leaving pop_everything ()\n");
6176 /* Emit debugging information for using declarations and directives.
6177 If input tree is overloaded fn then emit debug info for all
6178 candidates. */
6180 void
6181 cp_emit_debug_info_for_using (tree t, tree context)
6183 /* Don't try to emit any debug information if we have errors. */
6184 if (seen_error ())
6185 return;
6187 /* Ignore this FUNCTION_DECL if it refers to a builtin declaration
6188 of a builtin function. */
6189 if (TREE_CODE (t) == FUNCTION_DECL
6190 && DECL_EXTERNAL (t)
6191 && DECL_BUILT_IN (t))
6192 return;
6194 /* Do not supply context to imported_module_or_decl, if
6195 it is a global namespace. */
6196 if (context == global_namespace)
6197 context = NULL_TREE;
6199 if (BASELINK_P (t))
6200 t = BASELINK_FUNCTIONS (t);
6202 /* FIXME: Handle TEMPLATE_DECLs. */
6203 for (t = OVL_CURRENT (t); t; t = OVL_NEXT (t))
6204 if (TREE_CODE (t) != TEMPLATE_DECL)
6206 if (building_stmt_list_p ())
6207 add_stmt (build_stmt (input_location, USING_STMT, t));
6208 else
6209 (*debug_hooks->imported_module_or_decl) (t, NULL_TREE, context, false);
6213 #include "gt-cp-name-lookup.h"