* gcc.dg/vmx/unpack.c: Use dg-additional-options rather than
[official-gcc.git] / gcc / cp / name-lookup.c
bloba98788320c3c8de2c62e619e9ecc2e4b9dd83cdd
1 /* Definitions for C++ name lookup routines.
2 Copyright (C) 2003-2015 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 "alias.h"
27 #include "tree.h"
28 #include "stringpool.h"
29 #include "print-tree.h"
30 #include "attribs.h"
31 #include "cp-tree.h"
32 #include "name-lookup.h"
33 #include "timevar.h"
34 #include "diagnostic-core.h"
35 #include "intl.h"
36 #include "debug.h"
37 #include "c-family/c-pragma.h"
38 #include "params.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_cleared_vec_alloc<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. If DECL is dependent,
410 surreptitiously create a typename_type and return it. */
412 tree
413 strip_using_decl (tree decl)
415 if (decl == NULL_TREE)
416 return NULL_TREE;
418 while (TREE_CODE (decl) == USING_DECL && !DECL_DEPENDENT_P (decl))
419 decl = USING_DECL_DECLS (decl);
421 if (TREE_CODE (decl) == USING_DECL && DECL_DEPENDENT_P (decl)
422 && USING_DECL_TYPENAME_P (decl))
424 /* We have found a type introduced by a using
425 declaration at class scope that refers to a dependent
426 type.
428 using typename :: [opt] nested-name-specifier unqualified-id ;
430 decl = make_typename_type (TREE_TYPE (decl),
431 DECL_NAME (decl),
432 typename_type, tf_error);
433 if (decl != error_mark_node)
434 decl = TYPE_NAME (decl);
437 return decl;
440 /* BINDING records an existing declaration for a name in the current scope.
441 But, DECL is another declaration for that same identifier in the
442 same scope. This is the `struct stat' hack whereby a non-typedef
443 class name or enum-name can be bound at the same level as some other
444 kind of entity.
445 3.3.7/1
447 A class name (9.1) or enumeration name (7.2) can be hidden by the
448 name of an object, function, or enumerator declared in the same scope.
449 If a class or enumeration name and an object, function, or enumerator
450 are declared in the same scope (in any order) with the same name, the
451 class or enumeration name is hidden wherever the object, function, or
452 enumerator name is visible.
454 It's the responsibility of the caller to check that
455 inserting this name is valid here. Returns nonzero if the new binding
456 was successful. */
458 static bool
459 supplement_binding_1 (cxx_binding *binding, tree decl)
461 tree bval = binding->value;
462 bool ok = true;
463 tree target_bval = strip_using_decl (bval);
464 tree target_decl = strip_using_decl (decl);
466 if (TREE_CODE (target_decl) == TYPE_DECL && DECL_ARTIFICIAL (target_decl)
467 && target_decl != target_bval
468 && (TREE_CODE (target_bval) != TYPE_DECL
469 /* We allow pushing an enum multiple times in a class
470 template in order to handle late matching of underlying
471 type on an opaque-enum-declaration followed by an
472 enum-specifier. */
473 || (processing_template_decl
474 && TREE_CODE (TREE_TYPE (target_decl)) == ENUMERAL_TYPE
475 && TREE_CODE (TREE_TYPE (target_bval)) == ENUMERAL_TYPE
476 && (dependent_type_p (ENUM_UNDERLYING_TYPE
477 (TREE_TYPE (target_decl)))
478 || dependent_type_p (ENUM_UNDERLYING_TYPE
479 (TREE_TYPE (target_bval)))))))
480 /* The new name is the type name. */
481 binding->type = decl;
482 else if (/* TARGET_BVAL is null when push_class_level_binding moves
483 an inherited type-binding out of the way to make room
484 for a new value binding. */
485 !target_bval
486 /* TARGET_BVAL is error_mark_node when TARGET_DECL's name
487 has been used in a non-class scope prior declaration.
488 In that case, we should have already issued a
489 diagnostic; for graceful error recovery purpose, pretend
490 this was the intended declaration for that name. */
491 || target_bval == error_mark_node
492 /* If TARGET_BVAL is anticipated but has not yet been
493 declared, pretend it is not there at all. */
494 || (TREE_CODE (target_bval) == FUNCTION_DECL
495 && DECL_ANTICIPATED (target_bval)
496 && !DECL_HIDDEN_FRIEND_P (target_bval)))
497 binding->value = decl;
498 else if (TREE_CODE (target_bval) == TYPE_DECL
499 && DECL_ARTIFICIAL (target_bval)
500 && target_decl != target_bval
501 && (TREE_CODE (target_decl) != TYPE_DECL
502 || same_type_p (TREE_TYPE (target_decl),
503 TREE_TYPE (target_bval))))
505 /* The old binding was a type name. It was placed in
506 VALUE field because it was thought, at the point it was
507 declared, to be the only entity with such a name. Move the
508 type name into the type slot; it is now hidden by the new
509 binding. */
510 binding->type = bval;
511 binding->value = decl;
512 binding->value_is_inherited = false;
514 else if (TREE_CODE (target_bval) == TYPE_DECL
515 && TREE_CODE (target_decl) == TYPE_DECL
516 && DECL_NAME (target_decl) == DECL_NAME (target_bval)
517 && binding->scope->kind != sk_class
518 && (same_type_p (TREE_TYPE (target_decl), TREE_TYPE (target_bval))
519 /* If either type involves template parameters, we must
520 wait until instantiation. */
521 || uses_template_parms (TREE_TYPE (target_decl))
522 || uses_template_parms (TREE_TYPE (target_bval))))
523 /* We have two typedef-names, both naming the same type to have
524 the same name. In general, this is OK because of:
526 [dcl.typedef]
528 In a given scope, a typedef specifier can be used to redefine
529 the name of any type declared in that scope to refer to the
530 type to which it already refers.
532 However, in class scopes, this rule does not apply due to the
533 stricter language in [class.mem] prohibiting redeclarations of
534 members. */
535 ok = false;
536 /* There can be two block-scope declarations of the same variable,
537 so long as they are `extern' declarations. However, there cannot
538 be two declarations of the same static data member:
540 [class.mem]
542 A member shall not be declared twice in the
543 member-specification. */
544 else if (VAR_P (target_decl)
545 && VAR_P (target_bval)
546 && DECL_EXTERNAL (target_decl) && DECL_EXTERNAL (target_bval)
547 && !DECL_CLASS_SCOPE_P (target_decl))
549 duplicate_decls (decl, binding->value, /*newdecl_is_friend=*/false);
550 ok = false;
552 else if (TREE_CODE (decl) == NAMESPACE_DECL
553 && TREE_CODE (bval) == NAMESPACE_DECL
554 && DECL_NAMESPACE_ALIAS (decl)
555 && DECL_NAMESPACE_ALIAS (bval)
556 && ORIGINAL_NAMESPACE (bval) == ORIGINAL_NAMESPACE (decl))
557 /* [namespace.alias]
559 In a declarative region, a namespace-alias-definition can be
560 used to redefine a namespace-alias declared in that declarative
561 region to refer only to the namespace to which it already
562 refers. */
563 ok = false;
564 else if (maybe_remove_implicit_alias (bval))
566 /* There was a mangling compatibility alias using this mangled name,
567 but now we have a real decl that wants to use it instead. */
568 binding->value = decl;
570 else
572 diagnose_name_conflict (decl, bval);
573 ok = false;
576 return ok;
579 /* Diagnose a name conflict between DECL and BVAL. */
581 static void
582 diagnose_name_conflict (tree decl, tree bval)
584 if (TREE_CODE (decl) == TREE_CODE (bval)
585 && (TREE_CODE (decl) != TYPE_DECL
586 || (DECL_ARTIFICIAL (decl) && DECL_ARTIFICIAL (bval))
587 || (!DECL_ARTIFICIAL (decl) && !DECL_ARTIFICIAL (bval)))
588 && !is_overloaded_fn (decl))
589 error ("redeclaration of %q#D", decl);
590 else
591 error ("%q#D conflicts with a previous declaration", decl);
593 inform (input_location, "previous declaration %q+#D", bval);
596 /* Wrapper for supplement_binding_1. */
598 static bool
599 supplement_binding (cxx_binding *binding, tree decl)
601 bool ret;
602 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
603 ret = supplement_binding_1 (binding, decl);
604 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
605 return ret;
608 /* Add DECL to the list of things declared in B. */
610 static void
611 add_decl_to_level (tree decl, cp_binding_level *b)
613 /* We used to record virtual tables as if they were ordinary
614 variables, but no longer do so. */
615 gcc_assert (!(VAR_P (decl) && DECL_VIRTUAL_P (decl)));
617 if (TREE_CODE (decl) == NAMESPACE_DECL
618 && !DECL_NAMESPACE_ALIAS (decl))
620 DECL_CHAIN (decl) = b->namespaces;
621 b->namespaces = decl;
623 else
625 /* We build up the list in reverse order, and reverse it later if
626 necessary. */
627 TREE_CHAIN (decl) = b->names;
628 b->names = decl;
630 /* If appropriate, add decl to separate list of statics. We
631 include extern variables because they might turn out to be
632 static later. It's OK for this list to contain a few false
633 positives. */
634 if (b->kind == sk_namespace)
635 if ((VAR_P (decl)
636 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
637 || (TREE_CODE (decl) == FUNCTION_DECL
638 && (!TREE_PUBLIC (decl)
639 || decl_anon_ns_mem_p (decl)
640 || DECL_DECLARED_INLINE_P (decl))))
641 vec_safe_push (b->static_decls, decl);
645 /* Record a decl-node X as belonging to the current lexical scope.
646 Check for errors (such as an incompatible declaration for the same
647 name already seen in the same scope). IS_FRIEND is true if X is
648 declared as a friend.
650 Returns either X or an old decl for the same name.
651 If an old decl is returned, it may have been smashed
652 to agree with what X says. */
654 static tree
655 pushdecl_maybe_friend_1 (tree x, bool is_friend)
657 tree t;
658 tree name;
659 int need_new_binding;
661 if (x == error_mark_node)
662 return error_mark_node;
664 need_new_binding = 1;
666 if (DECL_TEMPLATE_PARM_P (x))
667 /* Template parameters have no context; they are not X::T even
668 when declared within a class or namespace. */
670 else
672 if (current_function_decl && x != current_function_decl
673 /* A local declaration for a function doesn't constitute
674 nesting. */
675 && TREE_CODE (x) != FUNCTION_DECL
676 /* A local declaration for an `extern' variable is in the
677 scope of the current namespace, not the current
678 function. */
679 && !(VAR_P (x) && DECL_EXTERNAL (x))
680 /* When parsing the parameter list of a function declarator,
681 don't set DECL_CONTEXT to an enclosing function. When we
682 push the PARM_DECLs in order to process the function body,
683 current_binding_level->this_entity will be set. */
684 && !(TREE_CODE (x) == PARM_DECL
685 && current_binding_level->kind == sk_function_parms
686 && current_binding_level->this_entity == NULL)
687 && !DECL_CONTEXT (x))
688 DECL_CONTEXT (x) = current_function_decl;
690 /* If this is the declaration for a namespace-scope function,
691 but the declaration itself is in a local scope, mark the
692 declaration. */
693 if (TREE_CODE (x) == FUNCTION_DECL
694 && DECL_NAMESPACE_SCOPE_P (x)
695 && current_function_decl
696 && x != current_function_decl)
697 DECL_LOCAL_FUNCTION_P (x) = 1;
700 name = DECL_NAME (x);
701 if (name)
703 int different_binding_level = 0;
705 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
706 name = TREE_OPERAND (name, 0);
708 /* In case this decl was explicitly namespace-qualified, look it
709 up in its namespace context. */
710 if (DECL_NAMESPACE_SCOPE_P (x) && namespace_bindings_p ())
711 t = namespace_binding (name, DECL_CONTEXT (x));
712 else
713 t = lookup_name_innermost_nonclass_level (name);
715 /* [basic.link] If there is a visible declaration of an entity
716 with linkage having the same name and type, ignoring entities
717 declared outside the innermost enclosing namespace scope, the
718 block scope declaration declares that same entity and
719 receives the linkage of the previous declaration. */
720 if (! t && current_function_decl && x != current_function_decl
721 && VAR_OR_FUNCTION_DECL_P (x)
722 && DECL_EXTERNAL (x))
724 /* Look in block scope. */
725 t = innermost_non_namespace_value (name);
726 /* Or in the innermost namespace. */
727 if (! t)
728 t = namespace_binding (name, DECL_CONTEXT (x));
729 /* Does it have linkage? Note that if this isn't a DECL, it's an
730 OVERLOAD, which is OK. */
731 if (t && DECL_P (t) && ! (TREE_STATIC (t) || DECL_EXTERNAL (t)))
732 t = NULL_TREE;
733 if (t)
734 different_binding_level = 1;
737 /* If we are declaring a function, and the result of name-lookup
738 was an OVERLOAD, look for an overloaded instance that is
739 actually the same as the function we are declaring. (If
740 there is one, we have to merge our declaration with the
741 previous declaration.) */
742 if (t && TREE_CODE (t) == OVERLOAD)
744 tree match;
746 if (TREE_CODE (x) == FUNCTION_DECL)
747 for (match = t; match; match = OVL_NEXT (match))
749 if (decls_match (OVL_CURRENT (match), x))
750 break;
752 else
753 /* Just choose one. */
754 match = t;
756 if (match)
757 t = OVL_CURRENT (match);
758 else
759 t = NULL_TREE;
762 if (t && t != error_mark_node)
764 if (different_binding_level)
766 if (decls_match (x, t))
767 /* The standard only says that the local extern
768 inherits linkage from the previous decl; in
769 particular, default args are not shared. Add
770 the decl into a hash table to make sure only
771 the previous decl in this case is seen by the
772 middle end. */
774 struct cxx_int_tree_map *h;
776 TREE_PUBLIC (x) = TREE_PUBLIC (t);
778 if (cp_function_chain->extern_decl_map == NULL)
779 cp_function_chain->extern_decl_map
780 = hash_table<cxx_int_tree_map_hasher>::create_ggc (20);
782 h = ggc_alloc<cxx_int_tree_map> ();
783 h->uid = DECL_UID (x);
784 h->to = t;
785 cxx_int_tree_map **loc = cp_function_chain->extern_decl_map
786 ->find_slot (h, INSERT);
787 *loc = h;
790 else if (TREE_CODE (t) == PARM_DECL)
792 /* Check for duplicate params. */
793 tree d = duplicate_decls (x, t, is_friend);
794 if (d)
795 return d;
797 else if ((DECL_EXTERN_C_FUNCTION_P (x)
798 || DECL_FUNCTION_TEMPLATE_P (x))
799 && is_overloaded_fn (t))
800 /* Don't do anything just yet. */;
801 else if (t == wchar_decl_node)
803 if (! DECL_IN_SYSTEM_HEADER (x))
804 pedwarn (input_location, OPT_Wpedantic, "redeclaration of %<wchar_t%> as %qT",
805 TREE_TYPE (x));
807 /* Throw away the redeclaration. */
808 return t;
810 else
812 tree olddecl = duplicate_decls (x, t, is_friend);
814 /* If the redeclaration failed, we can stop at this
815 point. */
816 if (olddecl == error_mark_node)
817 return error_mark_node;
819 if (olddecl)
821 if (TREE_CODE (t) == TYPE_DECL)
822 SET_IDENTIFIER_TYPE_VALUE (name, TREE_TYPE (t));
824 return t;
826 else if (DECL_MAIN_P (x) && TREE_CODE (t) == FUNCTION_DECL)
828 /* A redeclaration of main, but not a duplicate of the
829 previous one.
831 [basic.start.main]
833 This function shall not be overloaded. */
834 error ("invalid redeclaration of %q+D", t);
835 error ("as %qD", x);
836 /* We don't try to push this declaration since that
837 causes a crash. */
838 return x;
843 /* If x has C linkage-specification, (extern "C"),
844 lookup its binding, in case it's already bound to an object.
845 The lookup is done in all namespaces.
846 If we find an existing binding, make sure it has the same
847 exception specification as x, otherwise, bail in error [7.5, 7.6]. */
848 if ((TREE_CODE (x) == FUNCTION_DECL)
849 && DECL_EXTERN_C_P (x)
850 /* We should ignore declarations happening in system headers. */
851 && !DECL_ARTIFICIAL (x)
852 && !DECL_IN_SYSTEM_HEADER (x))
854 tree previous = lookup_extern_c_fun_in_all_ns (x);
855 if (previous
856 && !DECL_ARTIFICIAL (previous)
857 && !DECL_IN_SYSTEM_HEADER (previous)
858 && DECL_CONTEXT (previous) != DECL_CONTEXT (x))
860 /* In case either x or previous is declared to throw an exception,
861 make sure both exception specifications are equal. */
862 if (decls_match (x, previous))
864 tree x_exception_spec = NULL_TREE;
865 tree previous_exception_spec = NULL_TREE;
867 x_exception_spec =
868 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (x));
869 previous_exception_spec =
870 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (previous));
871 if (!comp_except_specs (previous_exception_spec,
872 x_exception_spec,
873 ce_normal))
875 pedwarn (input_location, 0,
876 "declaration of %q#D with C language linkage",
878 pedwarn (input_location, 0,
879 "conflicts with previous declaration %q+#D",
880 previous);
881 pedwarn (input_location, 0,
882 "due to different exception specifications");
883 return error_mark_node;
885 if (DECL_ASSEMBLER_NAME_SET_P (previous))
886 SET_DECL_ASSEMBLER_NAME (x,
887 DECL_ASSEMBLER_NAME (previous));
889 else
891 pedwarn (input_location, 0,
892 "declaration of %q#D with C language linkage", x);
893 pedwarn (input_location, 0,
894 "conflicts with previous declaration %q+#D",
895 previous);
900 check_template_shadow (x);
902 /* If this is a function conjured up by the back end, massage it
903 so it looks friendly. */
904 if (DECL_NON_THUNK_FUNCTION_P (x) && ! DECL_LANG_SPECIFIC (x))
906 retrofit_lang_decl (x);
907 SET_DECL_LANGUAGE (x, lang_c);
910 t = x;
911 if (DECL_NON_THUNK_FUNCTION_P (x) && ! DECL_FUNCTION_MEMBER_P (x))
913 t = push_overloaded_decl (x, PUSH_LOCAL, is_friend);
914 if (!namespace_bindings_p ())
915 /* We do not need to create a binding for this name;
916 push_overloaded_decl will have already done so if
917 necessary. */
918 need_new_binding = 0;
920 else if (DECL_FUNCTION_TEMPLATE_P (x) && DECL_NAMESPACE_SCOPE_P (x))
922 t = push_overloaded_decl (x, PUSH_GLOBAL, is_friend);
923 if (t == x)
924 add_decl_to_level (x, NAMESPACE_LEVEL (CP_DECL_CONTEXT (t)));
927 if (DECL_DECLARES_FUNCTION_P (t))
929 check_default_args (t);
931 if (is_friend && t == x && !flag_friend_injection)
933 /* This is a new friend declaration of a function or a
934 function template, so hide it from ordinary function
935 lookup. */
936 DECL_ANTICIPATED (t) = 1;
937 DECL_HIDDEN_FRIEND_P (t) = 1;
941 if (t != x || DECL_FUNCTION_TEMPLATE_P (t))
942 return t;
944 /* If declaring a type as a typedef, copy the type (unless we're
945 at line 0), and install this TYPE_DECL as the new type's typedef
946 name. See the extensive comment of set_underlying_type (). */
947 if (TREE_CODE (x) == TYPE_DECL)
949 tree type = TREE_TYPE (x);
951 if (DECL_IS_BUILTIN (x)
952 || (TREE_TYPE (x) != error_mark_node
953 && TYPE_NAME (type) != x
954 /* We don't want to copy the type when all we're
955 doing is making a TYPE_DECL for the purposes of
956 inlining. */
957 && (!TYPE_NAME (type)
958 || TYPE_NAME (type) != DECL_ABSTRACT_ORIGIN (x))))
959 set_underlying_type (x);
961 if (type != error_mark_node
962 && TYPE_IDENTIFIER (type))
963 set_identifier_type_value (DECL_NAME (x), x);
965 /* If this is a locally defined typedef in a function that
966 is not a template instantation, record it to implement
967 -Wunused-local-typedefs. */
968 if (!instantiating_current_function_p ())
969 record_locally_defined_typedef (x);
972 /* Multiple external decls of the same identifier ought to match.
974 We get warnings about inline functions where they are defined.
975 We get warnings about other functions from push_overloaded_decl.
977 Avoid duplicate warnings where they are used. */
978 if (TREE_PUBLIC (x) && TREE_CODE (x) != FUNCTION_DECL)
980 tree decl;
982 decl = IDENTIFIER_NAMESPACE_VALUE (name);
983 if (decl && TREE_CODE (decl) == OVERLOAD)
984 decl = OVL_FUNCTION (decl);
986 if (decl && decl != error_mark_node
987 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl))
988 /* If different sort of thing, we already gave an error. */
989 && TREE_CODE (decl) == TREE_CODE (x)
990 && !comptypes (TREE_TYPE (x), TREE_TYPE (decl),
991 COMPARE_REDECLARATION))
993 if (permerror (input_location, "type mismatch with previous "
994 "external decl of %q#D", x))
995 inform (input_location, "previous external decl of %q+#D",
996 decl);
1000 /* This name is new in its binding level.
1001 Install the new declaration and return it. */
1002 if (namespace_bindings_p ())
1004 /* Install a global value. */
1006 /* If the first global decl has external linkage,
1007 warn if we later see static one. */
1008 if (IDENTIFIER_GLOBAL_VALUE (name) == NULL_TREE && TREE_PUBLIC (x))
1009 TREE_PUBLIC (name) = 1;
1011 /* Bind the name for the entity. */
1012 if (!(TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x)
1013 && t != NULL_TREE)
1014 && (TREE_CODE (x) == TYPE_DECL
1015 || VAR_P (x)
1016 || TREE_CODE (x) == NAMESPACE_DECL
1017 || TREE_CODE (x) == CONST_DECL
1018 || TREE_CODE (x) == TEMPLATE_DECL))
1019 SET_IDENTIFIER_NAMESPACE_VALUE (name, x);
1021 /* If new decl is `static' and an `extern' was seen previously,
1022 warn about it. */
1023 if (x != NULL_TREE && t != NULL_TREE && decls_match (x, t))
1024 warn_extern_redeclared_static (x, t);
1026 else
1028 /* Here to install a non-global value. */
1029 tree oldglobal = IDENTIFIER_NAMESPACE_VALUE (name);
1030 tree oldlocal = NULL_TREE;
1031 cp_binding_level *oldscope = NULL;
1032 cxx_binding *oldbinding = outer_binding (name, NULL, true);
1033 if (oldbinding)
1035 oldlocal = oldbinding->value;
1036 oldscope = oldbinding->scope;
1039 if (need_new_binding)
1041 push_local_binding (name, x, 0);
1042 /* Because push_local_binding will hook X on to the
1043 current_binding_level's name list, we don't want to
1044 do that again below. */
1045 need_new_binding = 0;
1048 /* If this is a TYPE_DECL, push it into the type value slot. */
1049 if (TREE_CODE (x) == TYPE_DECL)
1050 set_identifier_type_value (name, x);
1052 /* Clear out any TYPE_DECL shadowed by a namespace so that
1053 we won't think this is a type. The C struct hack doesn't
1054 go through namespaces. */
1055 if (TREE_CODE (x) == NAMESPACE_DECL)
1056 set_identifier_type_value (name, NULL_TREE);
1058 if (oldlocal)
1060 tree d = oldlocal;
1062 while (oldlocal
1063 && VAR_P (oldlocal)
1064 && DECL_DEAD_FOR_LOCAL (oldlocal))
1065 oldlocal = DECL_SHADOWED_FOR_VAR (oldlocal);
1067 if (oldlocal == NULL_TREE)
1068 oldlocal = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (d));
1071 /* If this is an extern function declaration, see if we
1072 have a global definition or declaration for the function. */
1073 if (oldlocal == NULL_TREE
1074 && DECL_EXTERNAL (x)
1075 && oldglobal != NULL_TREE
1076 && TREE_CODE (x) == FUNCTION_DECL
1077 && TREE_CODE (oldglobal) == FUNCTION_DECL)
1079 /* We have one. Their types must agree. */
1080 if (decls_match (x, oldglobal))
1081 /* OK */;
1082 else
1084 warning (0, "extern declaration of %q#D doesn%'t match", x);
1085 warning (0, "global declaration %q+#D", oldglobal);
1088 /* If we have a local external declaration,
1089 and no file-scope declaration has yet been seen,
1090 then if we later have a file-scope decl it must not be static. */
1091 if (oldlocal == NULL_TREE
1092 && oldglobal == NULL_TREE
1093 && DECL_EXTERNAL (x)
1094 && TREE_PUBLIC (x))
1095 TREE_PUBLIC (name) = 1;
1097 /* Don't complain about the parms we push and then pop
1098 while tentatively parsing a function declarator. */
1099 if (TREE_CODE (x) == PARM_DECL && DECL_CONTEXT (x) == NULL_TREE)
1100 /* Ignore. */;
1102 /* Warn if shadowing an argument at the top level of the body. */
1103 else if (oldlocal != NULL_TREE && !DECL_EXTERNAL (x)
1104 /* Inline decls shadow nothing. */
1105 && !DECL_FROM_INLINE (x)
1106 && (TREE_CODE (oldlocal) == PARM_DECL
1107 || VAR_P (oldlocal)
1108 /* If the old decl is a type decl, only warn if the
1109 old decl is an explicit typedef or if both the old
1110 and new decls are type decls. */
1111 || (TREE_CODE (oldlocal) == TYPE_DECL
1112 && (!DECL_ARTIFICIAL (oldlocal)
1113 || TREE_CODE (x) == TYPE_DECL)))
1114 /* Don't check for internally generated vars unless
1115 it's an implicit typedef (see create_implicit_typedef
1116 in decl.c). */
1117 && (!DECL_ARTIFICIAL (x) || DECL_IMPLICIT_TYPEDEF_P (x)))
1119 bool nowarn = false;
1121 /* Don't complain if it's from an enclosing function. */
1122 if (DECL_CONTEXT (oldlocal) == current_function_decl
1123 && TREE_CODE (x) != PARM_DECL
1124 && TREE_CODE (oldlocal) == PARM_DECL)
1126 /* Go to where the parms should be and see if we find
1127 them there. */
1128 cp_binding_level *b = current_binding_level->level_chain;
1130 if (FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
1131 /* Skip the ctor/dtor cleanup level. */
1132 b = b->level_chain;
1134 /* ARM $8.3 */
1135 if (b->kind == sk_function_parms)
1137 error ("declaration of %q#D shadows a parameter", x);
1138 nowarn = true;
1142 /* The local structure or class can't use parameters of
1143 the containing function anyway. */
1144 if (DECL_CONTEXT (oldlocal) != current_function_decl)
1146 cp_binding_level *scope = current_binding_level;
1147 tree context = DECL_CONTEXT (oldlocal);
1148 for (; scope; scope = scope->level_chain)
1150 if (scope->kind == sk_function_parms
1151 && scope->this_entity == context)
1152 break;
1153 if (scope->kind == sk_class
1154 && !LAMBDA_TYPE_P (scope->this_entity))
1156 nowarn = true;
1157 break;
1161 /* Error if redeclaring a local declared in a
1162 for-init-statement or in the condition of an if or
1163 switch statement when the new declaration is in the
1164 outermost block of the controlled statement.
1165 Redeclaring a variable from a for or while condition is
1166 detected elsewhere. */
1167 else if (VAR_P (oldlocal)
1168 && oldscope == current_binding_level->level_chain
1169 && (oldscope->kind == sk_cond
1170 || oldscope->kind == sk_for))
1172 error ("redeclaration of %q#D", x);
1173 inform (input_location, "%q+#D previously declared here",
1174 oldlocal);
1175 nowarn = true;
1177 /* C++11:
1178 3.3.3/3: The name declared in an exception-declaration (...)
1179 shall not be redeclared in the outermost block of the handler.
1180 3.3.3/2: A parameter name shall not be redeclared (...) in
1181 the outermost block of any handler associated with a
1182 function-try-block.
1183 3.4.1/15: The function parameter names shall not be redeclared
1184 in the exception-declaration nor in the outermost block of a
1185 handler for the function-try-block. */
1186 else if ((VAR_P (oldlocal)
1187 && oldscope == current_binding_level->level_chain
1188 && oldscope->kind == sk_catch)
1189 || (TREE_CODE (oldlocal) == PARM_DECL
1190 && (current_binding_level->kind == sk_catch
1191 || (current_binding_level->level_chain->kind
1192 == sk_catch))
1193 && in_function_try_handler))
1195 if (permerror (input_location, "redeclaration of %q#D", x))
1196 inform (input_location, "%q+#D previously declared here",
1197 oldlocal);
1198 nowarn = true;
1201 if (warn_shadow && !nowarn)
1203 bool warned;
1205 if (TREE_CODE (oldlocal) == PARM_DECL)
1206 warned = warning_at (input_location, OPT_Wshadow,
1207 "declaration of %q#D shadows a parameter", x);
1208 else if (is_capture_proxy (oldlocal))
1209 warned = warning_at (input_location, OPT_Wshadow,
1210 "declaration of %qD shadows a lambda capture",
1212 else
1213 warned = warning_at (input_location, OPT_Wshadow,
1214 "declaration of %qD shadows a previous local",
1217 if (warned)
1218 inform (DECL_SOURCE_LOCATION (oldlocal),
1219 "shadowed declaration is here");
1223 /* Maybe warn if shadowing something else. */
1224 else if (warn_shadow && !DECL_EXTERNAL (x)
1225 /* No shadow warnings for internally generated vars unless
1226 it's an implicit typedef (see create_implicit_typedef
1227 in decl.c). */
1228 && (! DECL_ARTIFICIAL (x) || DECL_IMPLICIT_TYPEDEF_P (x))
1229 /* No shadow warnings for vars made for inlining. */
1230 && ! DECL_FROM_INLINE (x))
1232 tree member;
1234 if (nonlambda_method_basetype ())
1235 member = lookup_member (current_nonlambda_class_type (),
1236 name,
1237 /*protect=*/0,
1238 /*want_type=*/false,
1239 tf_warning_or_error);
1240 else
1241 member = NULL_TREE;
1243 if (member && !TREE_STATIC (member))
1245 if (BASELINK_P (member))
1246 member = BASELINK_FUNCTIONS (member);
1247 member = OVL_CURRENT (member);
1249 /* Do not warn if a variable shadows a function, unless
1250 the variable is a function or a pointer-to-function. */
1251 if (TREE_CODE (member) != FUNCTION_DECL
1252 || TREE_CODE (x) == FUNCTION_DECL
1253 || TYPE_PTRFN_P (TREE_TYPE (x))
1254 || TYPE_PTRMEMFUNC_P (TREE_TYPE (x)))
1256 if (warning_at (input_location, OPT_Wshadow,
1257 "declaration of %qD shadows a member of %qT",
1258 x, current_nonlambda_class_type ())
1259 && DECL_P (member))
1260 inform (DECL_SOURCE_LOCATION (member),
1261 "shadowed declaration is here");
1264 else if (oldglobal != NULL_TREE
1265 && (VAR_P (oldglobal)
1266 /* If the old decl is a type decl, only warn if the
1267 old decl is an explicit typedef or if both the
1268 old and new decls are type decls. */
1269 || (TREE_CODE (oldglobal) == TYPE_DECL
1270 && (!DECL_ARTIFICIAL (oldglobal)
1271 || TREE_CODE (x) == TYPE_DECL)))
1272 && !instantiating_current_function_p ())
1273 /* XXX shadow warnings in outer-more namespaces */
1275 if (warning_at (input_location, OPT_Wshadow,
1276 "declaration of %qD shadows a "
1277 "global declaration", x))
1278 inform (DECL_SOURCE_LOCATION (oldglobal),
1279 "shadowed declaration is here");
1284 if (VAR_P (x))
1285 maybe_register_incomplete_var (x);
1288 if (need_new_binding)
1289 add_decl_to_level (x,
1290 DECL_NAMESPACE_SCOPE_P (x)
1291 ? NAMESPACE_LEVEL (CP_DECL_CONTEXT (x))
1292 : current_binding_level);
1294 return x;
1297 /* Wrapper for pushdecl_maybe_friend_1. */
1299 tree
1300 pushdecl_maybe_friend (tree x, bool is_friend)
1302 tree ret;
1303 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
1304 ret = pushdecl_maybe_friend_1 (x, is_friend);
1305 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
1306 return ret;
1309 /* Record a decl-node X as belonging to the current lexical scope. */
1311 tree
1312 pushdecl (tree x)
1314 return pushdecl_maybe_friend (x, false);
1317 /* Enter DECL into the symbol table, if that's appropriate. Returns
1318 DECL, or a modified version thereof. */
1320 tree
1321 maybe_push_decl (tree decl)
1323 tree type = TREE_TYPE (decl);
1325 /* Add this decl to the current binding level, but not if it comes
1326 from another scope, e.g. a static member variable. TEM may equal
1327 DECL or it may be a previous decl of the same name. */
1328 if (decl == error_mark_node
1329 || (TREE_CODE (decl) != PARM_DECL
1330 && DECL_CONTEXT (decl) != NULL_TREE
1331 /* Definitions of namespace members outside their namespace are
1332 possible. */
1333 && !DECL_NAMESPACE_SCOPE_P (decl))
1334 || (TREE_CODE (decl) == TEMPLATE_DECL && !namespace_bindings_p ())
1335 || type == unknown_type_node
1336 /* The declaration of a template specialization does not affect
1337 the functions available for overload resolution, so we do not
1338 call pushdecl. */
1339 || (TREE_CODE (decl) == FUNCTION_DECL
1340 && DECL_TEMPLATE_SPECIALIZATION (decl)))
1341 return decl;
1342 else
1343 return pushdecl (decl);
1346 /* Bind DECL to ID in the current_binding_level, assumed to be a local
1347 binding level. If PUSH_USING is set in FLAGS, we know that DECL
1348 doesn't really belong to this binding level, that it got here
1349 through a using-declaration. */
1351 void
1352 push_local_binding (tree id, tree decl, int flags)
1354 cp_binding_level *b;
1356 /* Skip over any local classes. This makes sense if we call
1357 push_local_binding with a friend decl of a local class. */
1358 b = innermost_nonclass_level ();
1360 if (lookup_name_innermost_nonclass_level (id))
1362 /* Supplement the existing binding. */
1363 if (!supplement_binding (IDENTIFIER_BINDING (id), decl))
1364 /* It didn't work. Something else must be bound at this
1365 level. Do not add DECL to the list of things to pop
1366 later. */
1367 return;
1369 else
1370 /* Create a new binding. */
1371 push_binding (id, decl, b);
1373 if (TREE_CODE (decl) == OVERLOAD || (flags & PUSH_USING))
1374 /* We must put the OVERLOAD into a TREE_LIST since the
1375 TREE_CHAIN of an OVERLOAD is already used. Similarly for
1376 decls that got here through a using-declaration. */
1377 decl = build_tree_list (NULL_TREE, decl);
1379 /* And put DECL on the list of things declared by the current
1380 binding level. */
1381 add_decl_to_level (decl, b);
1384 /* Check to see whether or not DECL is a variable that would have been
1385 in scope under the ARM, but is not in scope under the ANSI/ISO
1386 standard. If so, issue an error message. If name lookup would
1387 work in both cases, but return a different result, this function
1388 returns the result of ANSI/ISO lookup. Otherwise, it returns
1389 DECL. */
1391 tree
1392 check_for_out_of_scope_variable (tree decl)
1394 tree shadowed;
1396 /* We only care about out of scope variables. */
1397 if (!(VAR_P (decl) && DECL_DEAD_FOR_LOCAL (decl)))
1398 return decl;
1400 shadowed = DECL_HAS_SHADOWED_FOR_VAR_P (decl)
1401 ? DECL_SHADOWED_FOR_VAR (decl) : NULL_TREE ;
1402 while (shadowed != NULL_TREE && VAR_P (shadowed)
1403 && DECL_DEAD_FOR_LOCAL (shadowed))
1404 shadowed = DECL_HAS_SHADOWED_FOR_VAR_P (shadowed)
1405 ? DECL_SHADOWED_FOR_VAR (shadowed) : NULL_TREE;
1406 if (!shadowed)
1407 shadowed = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (decl));
1408 if (shadowed)
1410 if (!DECL_ERROR_REPORTED (decl))
1412 warning (0, "name lookup of %qD changed", DECL_NAME (decl));
1413 warning (0, " matches this %q+D under ISO standard rules",
1414 shadowed);
1415 warning (0, " matches this %q+D under old rules", decl);
1416 DECL_ERROR_REPORTED (decl) = 1;
1418 return shadowed;
1421 /* If we have already complained about this declaration, there's no
1422 need to do it again. */
1423 if (DECL_ERROR_REPORTED (decl))
1424 return decl;
1426 DECL_ERROR_REPORTED (decl) = 1;
1428 if (TREE_TYPE (decl) == error_mark_node)
1429 return decl;
1431 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
1433 error ("name lookup of %qD changed for ISO %<for%> scoping",
1434 DECL_NAME (decl));
1435 error (" cannot use obsolete binding at %q+D because "
1436 "it has a destructor", decl);
1437 return error_mark_node;
1439 else
1441 permerror (input_location, "name lookup of %qD changed for ISO %<for%> scoping",
1442 DECL_NAME (decl));
1443 if (flag_permissive)
1444 permerror (input_location, " using obsolete binding at %q+D", decl);
1445 else
1447 static bool hint;
1448 if (!hint)
1450 inform (input_location, "(if you use %<-fpermissive%> G++ will accept your code)");
1451 hint = true;
1456 return decl;
1459 /* true means unconditionally make a BLOCK for the next level pushed. */
1461 static bool keep_next_level_flag;
1463 static int binding_depth = 0;
1465 static void
1466 indent (int depth)
1468 int i;
1470 for (i = 0; i < depth * 2; i++)
1471 putc (' ', stderr);
1474 /* Return a string describing the kind of SCOPE we have. */
1475 static const char *
1476 cp_binding_level_descriptor (cp_binding_level *scope)
1478 /* The order of this table must match the "scope_kind"
1479 enumerators. */
1480 static const char* scope_kind_names[] = {
1481 "block-scope",
1482 "cleanup-scope",
1483 "try-scope",
1484 "catch-scope",
1485 "for-scope",
1486 "function-parameter-scope",
1487 "class-scope",
1488 "namespace-scope",
1489 "template-parameter-scope",
1490 "template-explicit-spec-scope"
1492 const scope_kind kind = scope->explicit_spec_p
1493 ? sk_template_spec : scope->kind;
1495 return scope_kind_names[kind];
1498 /* Output a debugging information about SCOPE when performing
1499 ACTION at LINE. */
1500 static void
1501 cp_binding_level_debug (cp_binding_level *scope, int line, const char *action)
1503 const char *desc = cp_binding_level_descriptor (scope);
1504 if (scope->this_entity)
1505 verbatim ("%s %s(%E) %p %d\n", action, desc,
1506 scope->this_entity, (void *) scope, line);
1507 else
1508 verbatim ("%s %s %p %d\n", action, desc, (void *) scope, line);
1511 /* Return the estimated initial size of the hashtable of a NAMESPACE
1512 scope. */
1514 static inline size_t
1515 namespace_scope_ht_size (tree ns)
1517 tree name = DECL_NAME (ns);
1519 return name == std_identifier
1520 ? NAMESPACE_STD_HT_SIZE
1521 : (name == global_scope_name
1522 ? GLOBAL_SCOPE_HT_SIZE
1523 : NAMESPACE_ORDINARY_HT_SIZE);
1526 /* A chain of binding_level structures awaiting reuse. */
1528 static GTY((deletable)) cp_binding_level *free_binding_level;
1530 /* Insert SCOPE as the innermost binding level. */
1532 void
1533 push_binding_level (cp_binding_level *scope)
1535 /* Add it to the front of currently active scopes stack. */
1536 scope->level_chain = current_binding_level;
1537 current_binding_level = scope;
1538 keep_next_level_flag = false;
1540 if (ENABLE_SCOPE_CHECKING)
1542 scope->binding_depth = binding_depth;
1543 indent (binding_depth);
1544 cp_binding_level_debug (scope, LOCATION_LINE (input_location),
1545 "push");
1546 binding_depth++;
1550 /* Create a new KIND scope and make it the top of the active scopes stack.
1551 ENTITY is the scope of the associated C++ entity (namespace, class,
1552 function, C++0x enumeration); it is NULL otherwise. */
1554 cp_binding_level *
1555 begin_scope (scope_kind kind, tree entity)
1557 cp_binding_level *scope;
1559 /* Reuse or create a struct for this binding level. */
1560 if (!ENABLE_SCOPE_CHECKING && free_binding_level)
1562 scope = free_binding_level;
1563 memset (scope, 0, sizeof (cp_binding_level));
1564 free_binding_level = scope->level_chain;
1566 else
1567 scope = ggc_cleared_alloc<cp_binding_level> ();
1569 scope->this_entity = entity;
1570 scope->more_cleanups_ok = true;
1571 switch (kind)
1573 case sk_cleanup:
1574 scope->keep = true;
1575 break;
1577 case sk_template_spec:
1578 scope->explicit_spec_p = true;
1579 kind = sk_template_parms;
1580 /* Fall through. */
1581 case sk_template_parms:
1582 case sk_block:
1583 case sk_try:
1584 case sk_catch:
1585 case sk_for:
1586 case sk_cond:
1587 case sk_class:
1588 case sk_scoped_enum:
1589 case sk_function_parms:
1590 case sk_omp:
1591 scope->keep = keep_next_level_flag;
1592 break;
1594 case sk_namespace:
1595 NAMESPACE_LEVEL (entity) = scope;
1596 vec_alloc (scope->static_decls,
1597 (DECL_NAME (entity) == std_identifier
1598 || DECL_NAME (entity) == global_scope_name) ? 200 : 10);
1599 break;
1601 default:
1602 /* Should not happen. */
1603 gcc_unreachable ();
1604 break;
1606 scope->kind = kind;
1608 push_binding_level (scope);
1610 return scope;
1613 /* We're about to leave current scope. Pop the top of the stack of
1614 currently active scopes. Return the enclosing scope, now active. */
1616 cp_binding_level *
1617 leave_scope (void)
1619 cp_binding_level *scope = current_binding_level;
1621 if (scope->kind == sk_namespace && class_binding_level)
1622 current_binding_level = class_binding_level;
1624 /* We cannot leave a scope, if there are none left. */
1625 if (NAMESPACE_LEVEL (global_namespace))
1626 gcc_assert (!global_scope_p (scope));
1628 if (ENABLE_SCOPE_CHECKING)
1630 indent (--binding_depth);
1631 cp_binding_level_debug (scope, LOCATION_LINE (input_location),
1632 "leave");
1635 /* Move one nesting level up. */
1636 current_binding_level = scope->level_chain;
1638 /* Namespace-scopes are left most probably temporarily, not
1639 completely; they can be reopened later, e.g. in namespace-extension
1640 or any name binding activity that requires us to resume a
1641 namespace. For classes, we cache some binding levels. For other
1642 scopes, we just make the structure available for reuse. */
1643 if (scope->kind != sk_namespace
1644 && scope->kind != sk_class)
1646 scope->level_chain = free_binding_level;
1647 gcc_assert (!ENABLE_SCOPE_CHECKING
1648 || scope->binding_depth == binding_depth);
1649 free_binding_level = scope;
1652 if (scope->kind == sk_class)
1654 /* Reset DEFINING_CLASS_P to allow for reuse of a
1655 class-defining scope in a non-defining context. */
1656 scope->defining_class_p = 0;
1658 /* Find the innermost enclosing class scope, and reset
1659 CLASS_BINDING_LEVEL appropriately. */
1660 class_binding_level = NULL;
1661 for (scope = current_binding_level; scope; scope = scope->level_chain)
1662 if (scope->kind == sk_class)
1664 class_binding_level = scope;
1665 break;
1669 return current_binding_level;
1672 static void
1673 resume_scope (cp_binding_level* b)
1675 /* Resuming binding levels is meant only for namespaces,
1676 and those cannot nest into classes. */
1677 gcc_assert (!class_binding_level);
1678 /* Also, resuming a non-directly nested namespace is a no-no. */
1679 gcc_assert (b->level_chain == current_binding_level);
1680 current_binding_level = b;
1681 if (ENABLE_SCOPE_CHECKING)
1683 b->binding_depth = binding_depth;
1684 indent (binding_depth);
1685 cp_binding_level_debug (b, LOCATION_LINE (input_location), "resume");
1686 binding_depth++;
1690 /* Return the innermost binding level that is not for a class scope. */
1692 static cp_binding_level *
1693 innermost_nonclass_level (void)
1695 cp_binding_level *b;
1697 b = current_binding_level;
1698 while (b->kind == sk_class)
1699 b = b->level_chain;
1701 return b;
1704 /* We're defining an object of type TYPE. If it needs a cleanup, but
1705 we're not allowed to add any more objects with cleanups to the current
1706 scope, create a new binding level. */
1708 void
1709 maybe_push_cleanup_level (tree type)
1711 if (type != error_mark_node
1712 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
1713 && current_binding_level->more_cleanups_ok == 0)
1715 begin_scope (sk_cleanup, NULL);
1716 current_binding_level->statement_list = push_stmt_list ();
1720 /* Return true if we are in the global binding level. */
1722 bool
1723 global_bindings_p (void)
1725 return global_scope_p (current_binding_level);
1728 /* True if we are currently in a toplevel binding level. This
1729 means either the global binding level or a namespace in a toplevel
1730 binding level. Since there are no non-toplevel namespace levels,
1731 this really means any namespace or template parameter level. We
1732 also include a class whose context is toplevel. */
1734 bool
1735 toplevel_bindings_p (void)
1737 cp_binding_level *b = innermost_nonclass_level ();
1739 return b->kind == sk_namespace || b->kind == sk_template_parms;
1742 /* True if this is a namespace scope, or if we are defining a class
1743 which is itself at namespace scope, or whose enclosing class is
1744 such a class, etc. */
1746 bool
1747 namespace_bindings_p (void)
1749 cp_binding_level *b = innermost_nonclass_level ();
1751 return b->kind == sk_namespace;
1754 /* True if the innermost non-class scope is a block scope. */
1756 bool
1757 local_bindings_p (void)
1759 cp_binding_level *b = innermost_nonclass_level ();
1760 return b->kind < sk_function_parms || b->kind == sk_omp;
1763 /* True if the current level needs to have a BLOCK made. */
1765 bool
1766 kept_level_p (void)
1768 return (current_binding_level->blocks != NULL_TREE
1769 || current_binding_level->keep
1770 || current_binding_level->kind == sk_cleanup
1771 || current_binding_level->names != NULL_TREE
1772 || current_binding_level->using_directives);
1775 /* Returns the kind of the innermost scope. */
1777 scope_kind
1778 innermost_scope_kind (void)
1780 return current_binding_level->kind;
1783 /* Returns true if this scope was created to store template parameters. */
1785 bool
1786 template_parm_scope_p (void)
1788 return innermost_scope_kind () == sk_template_parms;
1791 /* If KEEP is true, make a BLOCK node for the next binding level,
1792 unconditionally. Otherwise, use the normal logic to decide whether
1793 or not to create a BLOCK. */
1795 void
1796 keep_next_level (bool keep)
1798 keep_next_level_flag = keep;
1801 /* Return the list of declarations of the current level.
1802 Note that this list is in reverse order unless/until
1803 you nreverse it; and when you do nreverse it, you must
1804 store the result back using `storedecls' or you will lose. */
1806 tree
1807 getdecls (void)
1809 return current_binding_level->names;
1812 /* Return how many function prototypes we are currently nested inside. */
1815 function_parm_depth (void)
1817 int level = 0;
1818 cp_binding_level *b;
1820 for (b = current_binding_level;
1821 b->kind == sk_function_parms;
1822 b = b->level_chain)
1823 ++level;
1825 return level;
1828 /* For debugging. */
1829 static int no_print_functions = 0;
1830 static int no_print_builtins = 0;
1832 static void
1833 print_binding_level (cp_binding_level* lvl)
1835 tree t;
1836 int i = 0, len;
1837 fprintf (stderr, " blocks=%p", (void *) lvl->blocks);
1838 if (lvl->more_cleanups_ok)
1839 fprintf (stderr, " more-cleanups-ok");
1840 if (lvl->have_cleanups)
1841 fprintf (stderr, " have-cleanups");
1842 fprintf (stderr, "\n");
1843 if (lvl->names)
1845 fprintf (stderr, " names:\t");
1846 /* We can probably fit 3 names to a line? */
1847 for (t = lvl->names; t; t = TREE_CHAIN (t))
1849 if (no_print_functions && (TREE_CODE (t) == FUNCTION_DECL))
1850 continue;
1851 if (no_print_builtins
1852 && (TREE_CODE (t) == TYPE_DECL)
1853 && DECL_IS_BUILTIN (t))
1854 continue;
1856 /* Function decls tend to have longer names. */
1857 if (TREE_CODE (t) == FUNCTION_DECL)
1858 len = 3;
1859 else
1860 len = 2;
1861 i += len;
1862 if (i > 6)
1864 fprintf (stderr, "\n\t");
1865 i = len;
1867 print_node_brief (stderr, "", t, 0);
1868 if (t == error_mark_node)
1869 break;
1871 if (i)
1872 fprintf (stderr, "\n");
1874 if (vec_safe_length (lvl->class_shadowed))
1876 size_t i;
1877 cp_class_binding *b;
1878 fprintf (stderr, " class-shadowed:");
1879 FOR_EACH_VEC_ELT (*lvl->class_shadowed, i, b)
1880 fprintf (stderr, " %s ", IDENTIFIER_POINTER (b->identifier));
1881 fprintf (stderr, "\n");
1883 if (lvl->type_shadowed)
1885 fprintf (stderr, " type-shadowed:");
1886 for (t = lvl->type_shadowed; t; t = TREE_CHAIN (t))
1888 fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
1890 fprintf (stderr, "\n");
1894 DEBUG_FUNCTION void
1895 debug (cp_binding_level &ref)
1897 print_binding_level (&ref);
1900 DEBUG_FUNCTION void
1901 debug (cp_binding_level *ptr)
1903 if (ptr)
1904 debug (*ptr);
1905 else
1906 fprintf (stderr, "<nil>\n");
1910 void
1911 print_other_binding_stack (cp_binding_level *stack)
1913 cp_binding_level *level;
1914 for (level = stack; !global_scope_p (level); level = level->level_chain)
1916 fprintf (stderr, "binding level %p\n", (void *) level);
1917 print_binding_level (level);
1921 void
1922 print_binding_stack (void)
1924 cp_binding_level *b;
1925 fprintf (stderr, "current_binding_level=%p\n"
1926 "class_binding_level=%p\n"
1927 "NAMESPACE_LEVEL (global_namespace)=%p\n",
1928 (void *) current_binding_level, (void *) class_binding_level,
1929 (void *) NAMESPACE_LEVEL (global_namespace));
1930 if (class_binding_level)
1932 for (b = class_binding_level; b; b = b->level_chain)
1933 if (b == current_binding_level)
1934 break;
1935 if (b)
1936 b = class_binding_level;
1937 else
1938 b = current_binding_level;
1940 else
1941 b = current_binding_level;
1942 print_other_binding_stack (b);
1943 fprintf (stderr, "global:\n");
1944 print_binding_level (NAMESPACE_LEVEL (global_namespace));
1947 /* Return the type associated with ID. */
1949 static tree
1950 identifier_type_value_1 (tree id)
1952 /* There is no type with that name, anywhere. */
1953 if (REAL_IDENTIFIER_TYPE_VALUE (id) == NULL_TREE)
1954 return NULL_TREE;
1955 /* This is not the type marker, but the real thing. */
1956 if (REAL_IDENTIFIER_TYPE_VALUE (id) != global_type_node)
1957 return REAL_IDENTIFIER_TYPE_VALUE (id);
1958 /* Have to search for it. It must be on the global level, now.
1959 Ask lookup_name not to return non-types. */
1960 id = lookup_name_real (id, 2, 1, /*block_p=*/true, 0, 0);
1961 if (id)
1962 return TREE_TYPE (id);
1963 return NULL_TREE;
1966 /* Wrapper for identifier_type_value_1. */
1968 tree
1969 identifier_type_value (tree id)
1971 tree ret;
1972 timevar_start (TV_NAME_LOOKUP);
1973 ret = identifier_type_value_1 (id);
1974 timevar_stop (TV_NAME_LOOKUP);
1975 return ret;
1979 /* Return the IDENTIFIER_GLOBAL_VALUE of T, for use in common code, since
1980 the definition of IDENTIFIER_GLOBAL_VALUE is different for C and C++. */
1982 tree
1983 identifier_global_value (tree t)
1985 return IDENTIFIER_GLOBAL_VALUE (t);
1988 /* Push a definition of struct, union or enum tag named ID. into
1989 binding_level B. DECL is a TYPE_DECL for the type. We assume that
1990 the tag ID is not already defined. */
1992 static void
1993 set_identifier_type_value_with_scope (tree id, tree decl, cp_binding_level *b)
1995 tree type;
1997 if (b->kind != sk_namespace)
1999 /* Shadow the marker, not the real thing, so that the marker
2000 gets restored later. */
2001 tree old_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
2002 b->type_shadowed
2003 = tree_cons (id, old_type_value, b->type_shadowed);
2004 type = decl ? TREE_TYPE (decl) : NULL_TREE;
2005 TREE_TYPE (b->type_shadowed) = type;
2007 else
2009 cxx_binding *binding =
2010 binding_for_name (NAMESPACE_LEVEL (current_namespace), id);
2011 gcc_assert (decl);
2012 if (binding->value)
2013 supplement_binding (binding, decl);
2014 else
2015 binding->value = decl;
2017 /* Store marker instead of real type. */
2018 type = global_type_node;
2020 SET_IDENTIFIER_TYPE_VALUE (id, type);
2023 /* As set_identifier_type_value_with_scope, but using
2024 current_binding_level. */
2026 void
2027 set_identifier_type_value (tree id, tree decl)
2029 set_identifier_type_value_with_scope (id, decl, current_binding_level);
2032 /* Return the name for the constructor (or destructor) for the
2033 specified class TYPE. When given a template, this routine doesn't
2034 lose the specialization. */
2036 static inline tree
2037 constructor_name_full (tree type)
2039 return TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
2042 /* Return the name for the constructor (or destructor) for the
2043 specified class. When given a template, return the plain
2044 unspecialized name. */
2046 tree
2047 constructor_name (tree type)
2049 tree name;
2050 name = constructor_name_full (type);
2051 if (IDENTIFIER_TEMPLATE (name))
2052 name = IDENTIFIER_TEMPLATE (name);
2053 return name;
2056 /* Returns TRUE if NAME is the name for the constructor for TYPE,
2057 which must be a class type. */
2059 bool
2060 constructor_name_p (tree name, tree type)
2062 tree ctor_name;
2064 gcc_assert (MAYBE_CLASS_TYPE_P (type));
2066 if (!name)
2067 return false;
2069 if (!identifier_p (name))
2070 return false;
2072 /* These don't have names. */
2073 if (TREE_CODE (type) == DECLTYPE_TYPE
2074 || TREE_CODE (type) == TYPEOF_TYPE)
2075 return false;
2077 ctor_name = constructor_name_full (type);
2078 if (name == ctor_name)
2079 return true;
2080 if (IDENTIFIER_TEMPLATE (ctor_name)
2081 && name == IDENTIFIER_TEMPLATE (ctor_name))
2082 return true;
2083 return false;
2086 /* Counter used to create anonymous type names. */
2088 static GTY(()) int anon_cnt;
2090 /* Return an IDENTIFIER which can be used as a name for
2091 anonymous structs and unions. */
2093 tree
2094 make_anon_name (void)
2096 char buf[32];
2098 sprintf (buf, anon_aggrname_format (), anon_cnt++);
2099 return get_identifier (buf);
2102 /* This code is practically identical to that for creating
2103 anonymous names, but is just used for lambdas instead. This isn't really
2104 necessary, but it's convenient to avoid treating lambdas like other
2105 anonymous types. */
2107 static GTY(()) int lambda_cnt = 0;
2109 tree
2110 make_lambda_name (void)
2112 char buf[32];
2114 sprintf (buf, LAMBDANAME_FORMAT, lambda_cnt++);
2115 return get_identifier (buf);
2118 /* Return (from the stack of) the BINDING, if any, established at SCOPE. */
2120 static inline cxx_binding *
2121 find_binding (cp_binding_level *scope, cxx_binding *binding)
2123 for (; binding != NULL; binding = binding->previous)
2124 if (binding->scope == scope)
2125 return binding;
2127 return (cxx_binding *)0;
2130 /* Return the binding for NAME in SCOPE, if any. Otherwise, return NULL. */
2132 static inline cxx_binding *
2133 cp_binding_level_find_binding_for_name (cp_binding_level *scope, tree name)
2135 cxx_binding *b = IDENTIFIER_NAMESPACE_BINDINGS (name);
2136 if (b)
2138 /* Fold-in case where NAME is used only once. */
2139 if (scope == b->scope && b->previous == NULL)
2140 return b;
2141 return find_binding (scope, b);
2143 return NULL;
2146 /* Always returns a binding for name in scope. If no binding is
2147 found, make a new one. */
2149 static cxx_binding *
2150 binding_for_name (cp_binding_level *scope, tree name)
2152 cxx_binding *result;
2154 result = cp_binding_level_find_binding_for_name (scope, name);
2155 if (result)
2156 return result;
2157 /* Not found, make a new one. */
2158 result = cxx_binding_make (NULL, NULL);
2159 result->previous = IDENTIFIER_NAMESPACE_BINDINGS (name);
2160 result->scope = scope;
2161 result->is_local = false;
2162 result->value_is_inherited = false;
2163 IDENTIFIER_NAMESPACE_BINDINGS (name) = result;
2164 return result;
2167 /* Walk through the bindings associated to the name of FUNCTION,
2168 and return the first declaration of a function with a
2169 "C" linkage specification, a.k.a 'extern "C"'.
2170 This function looks for the binding, regardless of which scope it
2171 has been defined in. It basically looks in all the known scopes.
2172 Note that this function does not lookup for bindings of builtin functions
2173 or for functions declared in system headers. */
2174 static tree
2175 lookup_extern_c_fun_in_all_ns (tree function)
2177 tree name;
2178 cxx_binding *iter;
2180 gcc_assert (function && TREE_CODE (function) == FUNCTION_DECL);
2182 name = DECL_NAME (function);
2183 gcc_assert (name && identifier_p (name));
2185 for (iter = IDENTIFIER_NAMESPACE_BINDINGS (name);
2186 iter;
2187 iter = iter->previous)
2189 tree ovl;
2190 for (ovl = iter->value; ovl; ovl = OVL_NEXT (ovl))
2192 tree decl = OVL_CURRENT (ovl);
2193 if (decl
2194 && TREE_CODE (decl) == FUNCTION_DECL
2195 && DECL_EXTERN_C_P (decl)
2196 && !DECL_ARTIFICIAL (decl))
2198 return decl;
2202 return NULL;
2205 /* Returns a list of C-linkage decls with the name NAME. */
2207 tree
2208 c_linkage_bindings (tree name)
2210 tree decls = NULL_TREE;
2211 cxx_binding *iter;
2213 for (iter = IDENTIFIER_NAMESPACE_BINDINGS (name);
2214 iter;
2215 iter = iter->previous)
2217 tree ovl;
2218 for (ovl = iter->value; ovl; ovl = OVL_NEXT (ovl))
2220 tree decl = OVL_CURRENT (ovl);
2221 if (decl
2222 && DECL_EXTERN_C_P (decl)
2223 && !DECL_ARTIFICIAL (decl))
2225 if (decls == NULL_TREE)
2226 decls = decl;
2227 else
2228 decls = tree_cons (NULL_TREE, decl, decls);
2232 return decls;
2235 /* Insert another USING_DECL into the current binding level, returning
2236 this declaration. If this is a redeclaration, do nothing, and
2237 return NULL_TREE if this not in namespace scope (in namespace
2238 scope, a using decl might extend any previous bindings). */
2240 static tree
2241 push_using_decl_1 (tree scope, tree name)
2243 tree decl;
2245 gcc_assert (TREE_CODE (scope) == NAMESPACE_DECL);
2246 gcc_assert (identifier_p (name));
2247 for (decl = current_binding_level->usings; decl; decl = DECL_CHAIN (decl))
2248 if (USING_DECL_SCOPE (decl) == scope && DECL_NAME (decl) == name)
2249 break;
2250 if (decl)
2251 return namespace_bindings_p () ? decl : NULL_TREE;
2252 decl = build_lang_decl (USING_DECL, name, NULL_TREE);
2253 USING_DECL_SCOPE (decl) = scope;
2254 DECL_CHAIN (decl) = current_binding_level->usings;
2255 current_binding_level->usings = decl;
2256 return decl;
2259 /* Wrapper for push_using_decl_1. */
2261 static tree
2262 push_using_decl (tree scope, tree name)
2264 tree ret;
2265 timevar_start (TV_NAME_LOOKUP);
2266 ret = push_using_decl_1 (scope, name);
2267 timevar_stop (TV_NAME_LOOKUP);
2268 return ret;
2271 /* Same as pushdecl, but define X in binding-level LEVEL. We rely on the
2272 caller to set DECL_CONTEXT properly.
2274 Note that this must only be used when X will be the new innermost
2275 binding for its name, as we tack it onto the front of IDENTIFIER_BINDING
2276 without checking to see if the current IDENTIFIER_BINDING comes from a
2277 closer binding level than LEVEL. */
2279 static tree
2280 pushdecl_with_scope_1 (tree x, cp_binding_level *level, bool is_friend)
2282 cp_binding_level *b;
2283 tree function_decl = current_function_decl;
2285 current_function_decl = NULL_TREE;
2286 if (level->kind == sk_class)
2288 b = class_binding_level;
2289 class_binding_level = level;
2290 pushdecl_class_level (x);
2291 class_binding_level = b;
2293 else
2295 b = current_binding_level;
2296 current_binding_level = level;
2297 x = pushdecl_maybe_friend (x, is_friend);
2298 current_binding_level = b;
2300 current_function_decl = function_decl;
2301 return x;
2304 /* Wrapper for pushdecl_with_scope_1. */
2306 tree
2307 pushdecl_with_scope (tree x, cp_binding_level *level, bool is_friend)
2309 tree ret;
2310 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
2311 ret = pushdecl_with_scope_1 (x, level, is_friend);
2312 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
2313 return ret;
2316 /* Helper function for push_overloaded_decl_1 and do_nonmember_using_decl.
2317 Compares the parameter-type-lists of DECL1 and DECL2 and returns false
2318 if they are different. If the DECLs are template functions, the return
2319 types and the template parameter lists are compared too (DR 565). */
2321 static bool
2322 compparms_for_decl_and_using_decl (tree decl1, tree decl2)
2324 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (decl1)),
2325 TYPE_ARG_TYPES (TREE_TYPE (decl2))))
2326 return false;
2328 if (! DECL_FUNCTION_TEMPLATE_P (decl1)
2329 || ! DECL_FUNCTION_TEMPLATE_P (decl2))
2330 return true;
2332 return (comp_template_parms (DECL_TEMPLATE_PARMS (decl1),
2333 DECL_TEMPLATE_PARMS (decl2))
2334 && same_type_p (TREE_TYPE (TREE_TYPE (decl1)),
2335 TREE_TYPE (TREE_TYPE (decl2))));
2338 /* DECL is a FUNCTION_DECL for a non-member function, which may have
2339 other definitions already in place. We get around this by making
2340 the value of the identifier point to a list of all the things that
2341 want to be referenced by that name. It is then up to the users of
2342 that name to decide what to do with that list.
2344 DECL may also be a TEMPLATE_DECL, with a FUNCTION_DECL in its
2345 DECL_TEMPLATE_RESULT. It is dealt with the same way.
2347 FLAGS is a bitwise-or of the following values:
2348 PUSH_LOCAL: Bind DECL in the current scope, rather than at
2349 namespace scope.
2350 PUSH_USING: DECL is being pushed as the result of a using
2351 declaration.
2353 IS_FRIEND is true if this is a friend declaration.
2355 The value returned may be a previous declaration if we guessed wrong
2356 about what language DECL should belong to (C or C++). Otherwise,
2357 it's always DECL (and never something that's not a _DECL). */
2359 static tree
2360 push_overloaded_decl_1 (tree decl, int flags, bool is_friend)
2362 tree name = DECL_NAME (decl);
2363 tree old;
2364 tree new_binding;
2365 int doing_global = (namespace_bindings_p () || !(flags & PUSH_LOCAL));
2367 if (doing_global)
2368 old = namespace_binding (name, DECL_CONTEXT (decl));
2369 else
2370 old = lookup_name_innermost_nonclass_level (name);
2372 if (old)
2374 if (TREE_CODE (old) == TYPE_DECL && DECL_ARTIFICIAL (old))
2376 tree t = TREE_TYPE (old);
2377 if (MAYBE_CLASS_TYPE_P (t) && warn_shadow
2378 && (! DECL_IN_SYSTEM_HEADER (decl)
2379 || ! DECL_IN_SYSTEM_HEADER (old)))
2380 warning (OPT_Wshadow, "%q#D hides constructor for %q#T", decl, t);
2381 old = NULL_TREE;
2383 else if (is_overloaded_fn (old))
2385 tree tmp;
2387 for (tmp = old; tmp; tmp = OVL_NEXT (tmp))
2389 tree fn = OVL_CURRENT (tmp);
2390 tree dup;
2392 if (TREE_CODE (tmp) == OVERLOAD && OVL_USED (tmp)
2393 && !(flags & PUSH_USING)
2394 && compparms_for_decl_and_using_decl (fn, decl)
2395 && ! decls_match (fn, decl))
2396 diagnose_name_conflict (decl, fn);
2398 dup = duplicate_decls (decl, fn, is_friend);
2399 /* If DECL was a redeclaration of FN -- even an invalid
2400 one -- pass that information along to our caller. */
2401 if (dup == fn || dup == error_mark_node)
2402 return dup;
2405 /* We don't overload implicit built-ins. duplicate_decls()
2406 may fail to merge the decls if the new decl is e.g. a
2407 template function. */
2408 if (TREE_CODE (old) == FUNCTION_DECL
2409 && DECL_ANTICIPATED (old)
2410 && !DECL_HIDDEN_FRIEND_P (old))
2411 old = NULL;
2413 else if (old == error_mark_node)
2414 /* Ignore the undefined symbol marker. */
2415 old = NULL_TREE;
2416 else
2418 error ("previous non-function declaration %q+#D", old);
2419 error ("conflicts with function declaration %q#D", decl);
2420 return decl;
2424 if (old || TREE_CODE (decl) == TEMPLATE_DECL
2425 /* If it's a using declaration, we always need to build an OVERLOAD,
2426 because it's the only way to remember that the declaration comes
2427 from 'using', and have the lookup behave correctly. */
2428 || (flags & PUSH_USING))
2430 if (old && TREE_CODE (old) != OVERLOAD)
2431 new_binding = ovl_cons (decl, ovl_cons (old, NULL_TREE));
2432 else
2433 new_binding = ovl_cons (decl, old);
2434 if (flags & PUSH_USING)
2435 OVL_USED (new_binding) = 1;
2437 else
2438 /* NAME is not ambiguous. */
2439 new_binding = decl;
2441 if (doing_global)
2442 set_namespace_binding (name, current_namespace, new_binding);
2443 else
2445 /* We only create an OVERLOAD if there was a previous binding at
2446 this level, or if decl is a template. In the former case, we
2447 need to remove the old binding and replace it with the new
2448 binding. We must also run through the NAMES on the binding
2449 level where the name was bound to update the chain. */
2451 if (TREE_CODE (new_binding) == OVERLOAD && old)
2453 tree *d;
2455 for (d = &IDENTIFIER_BINDING (name)->scope->names;
2457 d = &TREE_CHAIN (*d))
2458 if (*d == old
2459 || (TREE_CODE (*d) == TREE_LIST
2460 && TREE_VALUE (*d) == old))
2462 if (TREE_CODE (*d) == TREE_LIST)
2463 /* Just replace the old binding with the new. */
2464 TREE_VALUE (*d) = new_binding;
2465 else
2466 /* Build a TREE_LIST to wrap the OVERLOAD. */
2467 *d = tree_cons (NULL_TREE, new_binding,
2468 TREE_CHAIN (*d));
2470 /* And update the cxx_binding node. */
2471 IDENTIFIER_BINDING (name)->value = new_binding;
2472 return decl;
2475 /* We should always find a previous binding in this case. */
2476 gcc_unreachable ();
2479 /* Install the new binding. */
2480 push_local_binding (name, new_binding, flags);
2483 return decl;
2486 /* Wrapper for push_overloaded_decl_1. */
2488 static tree
2489 push_overloaded_decl (tree decl, int flags, bool is_friend)
2491 tree ret;
2492 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
2493 ret = push_overloaded_decl_1 (decl, flags, is_friend);
2494 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
2495 return ret;
2498 /* Check a non-member using-declaration. Return the name and scope
2499 being used, and the USING_DECL, or NULL_TREE on failure. */
2501 static tree
2502 validate_nonmember_using_decl (tree decl, tree scope, tree name)
2504 /* [namespace.udecl]
2505 A using-declaration for a class member shall be a
2506 member-declaration. */
2507 if (TYPE_P (scope))
2509 error ("%qT is not a namespace or unscoped enum", scope);
2510 return NULL_TREE;
2512 else if (scope == error_mark_node)
2513 return NULL_TREE;
2515 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR)
2517 /* 7.3.3/5
2518 A using-declaration shall not name a template-id. */
2519 error ("a using-declaration cannot specify a template-id. "
2520 "Try %<using %D%>", name);
2521 return NULL_TREE;
2524 if (TREE_CODE (decl) == NAMESPACE_DECL)
2526 error ("namespace %qD not allowed in using-declaration", decl);
2527 return NULL_TREE;
2530 if (TREE_CODE (decl) == SCOPE_REF)
2532 /* It's a nested name with template parameter dependent scope.
2533 This can only be using-declaration for class member. */
2534 error ("%qT is not a namespace", TREE_OPERAND (decl, 0));
2535 return NULL_TREE;
2538 if (is_overloaded_fn (decl))
2539 decl = get_first_fn (decl);
2541 gcc_assert (DECL_P (decl));
2543 /* Make a USING_DECL. */
2544 tree using_decl = push_using_decl (scope, name);
2546 if (using_decl == NULL_TREE
2547 && at_function_scope_p ()
2548 && VAR_P (decl))
2549 /* C++11 7.3.3/10. */
2550 error ("%qD is already declared in this scope", name);
2552 return using_decl;
2555 /* Process local and global using-declarations. */
2557 static void
2558 do_nonmember_using_decl (tree scope, tree name, tree oldval, tree oldtype,
2559 tree *newval, tree *newtype)
2561 struct scope_binding decls = EMPTY_SCOPE_BINDING;
2563 *newval = *newtype = NULL_TREE;
2564 if (!qualified_lookup_using_namespace (name, scope, &decls, 0))
2565 /* Lookup error */
2566 return;
2568 if (!decls.value && !decls.type)
2570 error ("%qD not declared", name);
2571 return;
2574 /* Shift the old and new bindings around so we're comparing class and
2575 enumeration names to each other. */
2576 if (oldval && DECL_IMPLICIT_TYPEDEF_P (oldval))
2578 oldtype = oldval;
2579 oldval = NULL_TREE;
2582 if (decls.value && DECL_IMPLICIT_TYPEDEF_P (decls.value))
2584 decls.type = decls.value;
2585 decls.value = NULL_TREE;
2588 /* It is impossible to overload a built-in function; any explicit
2589 declaration eliminates the built-in declaration. So, if OLDVAL
2590 is a built-in, then we can just pretend it isn't there. */
2591 if (oldval
2592 && TREE_CODE (oldval) == FUNCTION_DECL
2593 && DECL_ANTICIPATED (oldval)
2594 && !DECL_HIDDEN_FRIEND_P (oldval))
2595 oldval = NULL_TREE;
2597 if (decls.value)
2599 /* Check for using functions. */
2600 if (is_overloaded_fn (decls.value))
2602 tree tmp, tmp1;
2604 if (oldval && !is_overloaded_fn (oldval))
2606 error ("%qD is already declared in this scope", name);
2607 oldval = NULL_TREE;
2610 *newval = oldval;
2611 for (tmp = decls.value; tmp; tmp = OVL_NEXT (tmp))
2613 tree new_fn = OVL_CURRENT (tmp);
2615 /* [namespace.udecl]
2617 If a function declaration in namespace scope or block
2618 scope has the same name and the same parameter types as a
2619 function introduced by a using declaration the program is
2620 ill-formed. */
2621 for (tmp1 = oldval; tmp1; tmp1 = OVL_NEXT (tmp1))
2623 tree old_fn = OVL_CURRENT (tmp1);
2625 if (new_fn == old_fn)
2626 /* The function already exists in the current namespace. */
2627 break;
2628 else if (TREE_CODE (tmp1) == OVERLOAD && OVL_USED (tmp1))
2629 continue; /* this is a using decl */
2630 else if (compparms_for_decl_and_using_decl (new_fn, old_fn))
2632 gcc_assert (!DECL_ANTICIPATED (old_fn)
2633 || DECL_HIDDEN_FRIEND_P (old_fn));
2635 /* There was already a non-using declaration in
2636 this scope with the same parameter types. If both
2637 are the same extern "C" functions, that's ok. */
2638 if (decls_match (new_fn, old_fn))
2639 break;
2640 else
2642 diagnose_name_conflict (new_fn, old_fn);
2643 break;
2648 /* If we broke out of the loop, there's no reason to add
2649 this function to the using declarations for this
2650 scope. */
2651 if (tmp1)
2652 continue;
2654 /* If we are adding to an existing OVERLOAD, then we no
2655 longer know the type of the set of functions. */
2656 if (*newval && TREE_CODE (*newval) == OVERLOAD)
2657 TREE_TYPE (*newval) = unknown_type_node;
2658 /* Add this new function to the set. */
2659 *newval = build_overload (OVL_CURRENT (tmp), *newval);
2660 /* If there is only one function, then we use its type. (A
2661 using-declaration naming a single function can be used in
2662 contexts where overload resolution cannot be
2663 performed.) */
2664 if (TREE_CODE (*newval) != OVERLOAD)
2666 *newval = ovl_cons (*newval, NULL_TREE);
2667 TREE_TYPE (*newval) = TREE_TYPE (OVL_CURRENT (tmp));
2669 OVL_USED (*newval) = 1;
2672 else
2674 *newval = decls.value;
2675 if (oldval && !decls_match (*newval, oldval))
2676 error ("%qD is already declared in this scope", name);
2679 else
2680 *newval = oldval;
2682 if (decls.type && TREE_CODE (decls.type) == TREE_LIST)
2684 error ("reference to %qD is ambiguous", name);
2685 print_candidates (decls.type);
2687 else
2689 *newtype = decls.type;
2690 if (oldtype && *newtype && !decls_match (oldtype, *newtype))
2691 error ("%qD is already declared in this scope", name);
2694 /* If *newval is empty, shift any class or enumeration name down. */
2695 if (!*newval)
2697 *newval = *newtype;
2698 *newtype = NULL_TREE;
2702 /* Process a using-declaration at function scope. */
2704 void
2705 do_local_using_decl (tree decl, tree scope, tree name)
2707 tree oldval, oldtype, newval, newtype;
2708 tree orig_decl = decl;
2710 decl = validate_nonmember_using_decl (decl, scope, name);
2711 if (decl == NULL_TREE)
2712 return;
2714 if (building_stmt_list_p ()
2715 && at_function_scope_p ())
2716 add_decl_expr (decl);
2718 oldval = lookup_name_innermost_nonclass_level (name);
2719 oldtype = lookup_type_current_level (name);
2721 do_nonmember_using_decl (scope, name, oldval, oldtype, &newval, &newtype);
2723 if (newval)
2725 if (is_overloaded_fn (newval))
2727 tree fn, term;
2729 /* We only need to push declarations for those functions
2730 that were not already bound in the current level.
2731 The old value might be NULL_TREE, it might be a single
2732 function, or an OVERLOAD. */
2733 if (oldval && TREE_CODE (oldval) == OVERLOAD)
2734 term = OVL_FUNCTION (oldval);
2735 else
2736 term = oldval;
2737 for (fn = newval; fn && OVL_CURRENT (fn) != term;
2738 fn = OVL_NEXT (fn))
2739 push_overloaded_decl (OVL_CURRENT (fn),
2740 PUSH_LOCAL | PUSH_USING,
2741 false);
2743 else
2744 push_local_binding (name, newval, PUSH_USING);
2746 if (newtype)
2748 push_local_binding (name, newtype, PUSH_USING);
2749 set_identifier_type_value (name, newtype);
2752 /* Emit debug info. */
2753 if (!processing_template_decl)
2754 cp_emit_debug_info_for_using (orig_decl, current_scope());
2757 /* Returns true if ROOT (a namespace, class, or function) encloses
2758 CHILD. CHILD may be either a class type or a namespace. */
2760 bool
2761 is_ancestor (tree root, tree child)
2763 gcc_assert ((TREE_CODE (root) == NAMESPACE_DECL
2764 || TREE_CODE (root) == FUNCTION_DECL
2765 || CLASS_TYPE_P (root)));
2766 gcc_assert ((TREE_CODE (child) == NAMESPACE_DECL
2767 || CLASS_TYPE_P (child)));
2769 /* The global namespace encloses everything. */
2770 if (root == global_namespace)
2771 return true;
2773 while (true)
2775 /* If we've run out of scopes, stop. */
2776 if (!child)
2777 return false;
2778 /* If we've reached the ROOT, it encloses CHILD. */
2779 if (root == child)
2780 return true;
2781 /* Go out one level. */
2782 if (TYPE_P (child))
2783 child = TYPE_NAME (child);
2784 child = DECL_CONTEXT (child);
2788 /* Enter the class or namespace scope indicated by T suitable for name
2789 lookup. T can be arbitrary scope, not necessary nested inside the
2790 current scope. Returns a non-null scope to pop iff pop_scope
2791 should be called later to exit this scope. */
2793 tree
2794 push_scope (tree t)
2796 if (TREE_CODE (t) == NAMESPACE_DECL)
2797 push_decl_namespace (t);
2798 else if (CLASS_TYPE_P (t))
2800 if (!at_class_scope_p ()
2801 || !same_type_p (current_class_type, t))
2802 push_nested_class (t);
2803 else
2804 /* T is the same as the current scope. There is therefore no
2805 need to re-enter the scope. Since we are not actually
2806 pushing a new scope, our caller should not call
2807 pop_scope. */
2808 t = NULL_TREE;
2811 return t;
2814 /* Leave scope pushed by push_scope. */
2816 void
2817 pop_scope (tree t)
2819 if (t == NULL_TREE)
2820 return;
2821 if (TREE_CODE (t) == NAMESPACE_DECL)
2822 pop_decl_namespace ();
2823 else if CLASS_TYPE_P (t)
2824 pop_nested_class ();
2827 /* Subroutine of push_inner_scope. */
2829 static void
2830 push_inner_scope_r (tree outer, tree inner)
2832 tree prev;
2834 if (outer == inner
2835 || (TREE_CODE (inner) != NAMESPACE_DECL && !CLASS_TYPE_P (inner)))
2836 return;
2838 prev = CP_DECL_CONTEXT (TREE_CODE (inner) == NAMESPACE_DECL ? inner : TYPE_NAME (inner));
2839 if (outer != prev)
2840 push_inner_scope_r (outer, prev);
2841 if (TREE_CODE (inner) == NAMESPACE_DECL)
2843 cp_binding_level *save_template_parm = 0;
2844 /* Temporary take out template parameter scopes. They are saved
2845 in reversed order in save_template_parm. */
2846 while (current_binding_level->kind == sk_template_parms)
2848 cp_binding_level *b = current_binding_level;
2849 current_binding_level = b->level_chain;
2850 b->level_chain = save_template_parm;
2851 save_template_parm = b;
2854 resume_scope (NAMESPACE_LEVEL (inner));
2855 current_namespace = inner;
2857 /* Restore template parameter scopes. */
2858 while (save_template_parm)
2860 cp_binding_level *b = save_template_parm;
2861 save_template_parm = b->level_chain;
2862 b->level_chain = current_binding_level;
2863 current_binding_level = b;
2866 else
2867 pushclass (inner);
2870 /* Enter the scope INNER from current scope. INNER must be a scope
2871 nested inside current scope. This works with both name lookup and
2872 pushing name into scope. In case a template parameter scope is present,
2873 namespace is pushed under the template parameter scope according to
2874 name lookup rule in 14.6.1/6.
2876 Return the former current scope suitable for pop_inner_scope. */
2878 tree
2879 push_inner_scope (tree inner)
2881 tree outer = current_scope ();
2882 if (!outer)
2883 outer = current_namespace;
2885 push_inner_scope_r (outer, inner);
2886 return outer;
2889 /* Exit the current scope INNER back to scope OUTER. */
2891 void
2892 pop_inner_scope (tree outer, tree inner)
2894 if (outer == inner
2895 || (TREE_CODE (inner) != NAMESPACE_DECL && !CLASS_TYPE_P (inner)))
2896 return;
2898 while (outer != inner)
2900 if (TREE_CODE (inner) == NAMESPACE_DECL)
2902 cp_binding_level *save_template_parm = 0;
2903 /* Temporary take out template parameter scopes. They are saved
2904 in reversed order in save_template_parm. */
2905 while (current_binding_level->kind == sk_template_parms)
2907 cp_binding_level *b = current_binding_level;
2908 current_binding_level = b->level_chain;
2909 b->level_chain = save_template_parm;
2910 save_template_parm = b;
2913 pop_namespace ();
2915 /* Restore template parameter scopes. */
2916 while (save_template_parm)
2918 cp_binding_level *b = save_template_parm;
2919 save_template_parm = b->level_chain;
2920 b->level_chain = current_binding_level;
2921 current_binding_level = b;
2924 else
2925 popclass ();
2927 inner = CP_DECL_CONTEXT (TREE_CODE (inner) == NAMESPACE_DECL ? inner : TYPE_NAME (inner));
2931 /* Do a pushlevel for class declarations. */
2933 void
2934 pushlevel_class (void)
2936 class_binding_level = begin_scope (sk_class, current_class_type);
2939 /* ...and a poplevel for class declarations. */
2941 void
2942 poplevel_class (void)
2944 cp_binding_level *level = class_binding_level;
2945 cp_class_binding *cb;
2946 size_t i;
2947 tree shadowed;
2949 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
2950 gcc_assert (level != 0);
2952 /* If we're leaving a toplevel class, cache its binding level. */
2953 if (current_class_depth == 1)
2954 previous_class_level = level;
2955 for (shadowed = level->type_shadowed;
2956 shadowed;
2957 shadowed = TREE_CHAIN (shadowed))
2958 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (shadowed), TREE_VALUE (shadowed));
2960 /* Remove the bindings for all of the class-level declarations. */
2961 if (level->class_shadowed)
2963 FOR_EACH_VEC_ELT (*level->class_shadowed, i, cb)
2965 IDENTIFIER_BINDING (cb->identifier) = cb->base->previous;
2966 cxx_binding_free (cb->base);
2968 ggc_free (level->class_shadowed);
2969 level->class_shadowed = NULL;
2972 /* Now, pop out of the binding level which we created up in the
2973 `pushlevel_class' routine. */
2974 gcc_assert (current_binding_level == level);
2975 leave_scope ();
2976 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
2979 /* Set INHERITED_VALUE_BINDING_P on BINDING to true or false, as
2980 appropriate. DECL is the value to which a name has just been
2981 bound. CLASS_TYPE is the class in which the lookup occurred. */
2983 static void
2984 set_inherited_value_binding_p (cxx_binding *binding, tree decl,
2985 tree class_type)
2987 if (binding->value == decl && TREE_CODE (decl) != TREE_LIST)
2989 tree context;
2991 if (TREE_CODE (decl) == OVERLOAD)
2992 context = ovl_scope (decl);
2993 else
2995 gcc_assert (DECL_P (decl));
2996 context = context_for_name_lookup (decl);
2999 if (is_properly_derived_from (class_type, context))
3000 INHERITED_VALUE_BINDING_P (binding) = 1;
3001 else
3002 INHERITED_VALUE_BINDING_P (binding) = 0;
3004 else if (binding->value == decl)
3005 /* We only encounter a TREE_LIST when there is an ambiguity in the
3006 base classes. Such an ambiguity can be overridden by a
3007 definition in this class. */
3008 INHERITED_VALUE_BINDING_P (binding) = 1;
3009 else
3010 INHERITED_VALUE_BINDING_P (binding) = 0;
3013 /* Make the declaration of X appear in CLASS scope. */
3015 bool
3016 pushdecl_class_level (tree x)
3018 tree name;
3019 bool is_valid = true;
3020 bool subtime;
3022 /* Do nothing if we're adding to an outer lambda closure type,
3023 outer_binding will add it later if it's needed. */
3024 if (current_class_type != class_binding_level->this_entity)
3025 return true;
3027 subtime = timevar_cond_start (TV_NAME_LOOKUP);
3028 /* Get the name of X. */
3029 if (TREE_CODE (x) == OVERLOAD)
3030 name = DECL_NAME (get_first_fn (x));
3031 else
3032 name = DECL_NAME (x);
3034 if (name)
3036 is_valid = push_class_level_binding (name, x);
3037 if (TREE_CODE (x) == TYPE_DECL)
3038 set_identifier_type_value (name, x);
3040 else if (ANON_AGGR_TYPE_P (TREE_TYPE (x)))
3042 /* If X is an anonymous aggregate, all of its members are
3043 treated as if they were members of the class containing the
3044 aggregate, for naming purposes. */
3045 tree f;
3047 for (f = TYPE_FIELDS (TREE_TYPE (x)); f; f = DECL_CHAIN (f))
3049 location_t save_location = input_location;
3050 input_location = DECL_SOURCE_LOCATION (f);
3051 if (!pushdecl_class_level (f))
3052 is_valid = false;
3053 input_location = save_location;
3056 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3057 return is_valid;
3060 /* Return the BINDING (if any) for NAME in SCOPE, which is a class
3061 scope. If the value returned is non-NULL, and the PREVIOUS field
3062 is not set, callers must set the PREVIOUS field explicitly. */
3064 static cxx_binding *
3065 get_class_binding (tree name, cp_binding_level *scope)
3067 tree class_type;
3068 tree type_binding;
3069 tree value_binding;
3070 cxx_binding *binding;
3072 class_type = scope->this_entity;
3074 /* Get the type binding. */
3075 type_binding = lookup_member (class_type, name,
3076 /*protect=*/2, /*want_type=*/true,
3077 tf_warning_or_error);
3078 /* Get the value binding. */
3079 value_binding = lookup_member (class_type, name,
3080 /*protect=*/2, /*want_type=*/false,
3081 tf_warning_or_error);
3083 if (value_binding
3084 && (TREE_CODE (value_binding) == TYPE_DECL
3085 || DECL_CLASS_TEMPLATE_P (value_binding)
3086 || (TREE_CODE (value_binding) == TREE_LIST
3087 && TREE_TYPE (value_binding) == error_mark_node
3088 && (TREE_CODE (TREE_VALUE (value_binding))
3089 == TYPE_DECL))))
3090 /* We found a type binding, even when looking for a non-type
3091 binding. This means that we already processed this binding
3092 above. */
3094 else if (value_binding)
3096 if (TREE_CODE (value_binding) == TREE_LIST
3097 && TREE_TYPE (value_binding) == error_mark_node)
3098 /* NAME is ambiguous. */
3100 else if (BASELINK_P (value_binding))
3101 /* NAME is some overloaded functions. */
3102 value_binding = BASELINK_FUNCTIONS (value_binding);
3105 /* If we found either a type binding or a value binding, create a
3106 new binding object. */
3107 if (type_binding || value_binding)
3109 binding = new_class_binding (name,
3110 value_binding,
3111 type_binding,
3112 scope);
3113 /* This is a class-scope binding, not a block-scope binding. */
3114 LOCAL_BINDING_P (binding) = 0;
3115 set_inherited_value_binding_p (binding, value_binding, class_type);
3117 else
3118 binding = NULL;
3120 return binding;
3123 /* Make the declaration(s) of X appear in CLASS scope under the name
3124 NAME. Returns true if the binding is valid. */
3126 static bool
3127 push_class_level_binding_1 (tree name, tree x)
3129 cxx_binding *binding;
3130 tree decl = x;
3131 bool ok;
3133 /* The class_binding_level will be NULL if x is a template
3134 parameter name in a member template. */
3135 if (!class_binding_level)
3136 return true;
3138 if (name == error_mark_node)
3139 return false;
3141 /* Can happen for an erroneous declaration (c++/60384). */
3142 if (!identifier_p (name))
3144 gcc_assert (errorcount || sorrycount);
3145 return false;
3148 /* Check for invalid member names. But don't worry about a default
3149 argument-scope lambda being pushed after the class is complete. */
3150 gcc_assert (TYPE_BEING_DEFINED (current_class_type)
3151 || LAMBDA_TYPE_P (TREE_TYPE (decl)));
3152 /* Check that we're pushing into the right binding level. */
3153 gcc_assert (current_class_type == class_binding_level->this_entity);
3155 /* We could have been passed a tree list if this is an ambiguous
3156 declaration. If so, pull the declaration out because
3157 check_template_shadow will not handle a TREE_LIST. */
3158 if (TREE_CODE (decl) == TREE_LIST
3159 && TREE_TYPE (decl) == error_mark_node)
3160 decl = TREE_VALUE (decl);
3162 if (!check_template_shadow (decl))
3163 return false;
3165 /* [class.mem]
3167 If T is the name of a class, then each of the following shall
3168 have a name different from T:
3170 -- every static data member of class T;
3172 -- every member of class T that is itself a type;
3174 -- every enumerator of every member of class T that is an
3175 enumerated type;
3177 -- every member of every anonymous union that is a member of
3178 class T.
3180 (Non-static data members were also forbidden to have the same
3181 name as T until TC1.) */
3182 if ((VAR_P (x)
3183 || TREE_CODE (x) == CONST_DECL
3184 || (TREE_CODE (x) == TYPE_DECL
3185 && !DECL_SELF_REFERENCE_P (x))
3186 /* A data member of an anonymous union. */
3187 || (TREE_CODE (x) == FIELD_DECL
3188 && DECL_CONTEXT (x) != current_class_type))
3189 && DECL_NAME (x) == constructor_name (current_class_type))
3191 tree scope = context_for_name_lookup (x);
3192 if (TYPE_P (scope) && same_type_p (scope, current_class_type))
3194 error ("%qD has the same name as the class in which it is "
3195 "declared",
3197 return false;
3201 /* Get the current binding for NAME in this class, if any. */
3202 binding = IDENTIFIER_BINDING (name);
3203 if (!binding || binding->scope != class_binding_level)
3205 binding = get_class_binding (name, class_binding_level);
3206 /* If a new binding was created, put it at the front of the
3207 IDENTIFIER_BINDING list. */
3208 if (binding)
3210 binding->previous = IDENTIFIER_BINDING (name);
3211 IDENTIFIER_BINDING (name) = binding;
3215 /* If there is already a binding, then we may need to update the
3216 current value. */
3217 if (binding && binding->value)
3219 tree bval = binding->value;
3220 tree old_decl = NULL_TREE;
3221 tree target_decl = strip_using_decl (decl);
3222 tree target_bval = strip_using_decl (bval);
3224 if (INHERITED_VALUE_BINDING_P (binding))
3226 /* If the old binding was from a base class, and was for a
3227 tag name, slide it over to make room for the new binding.
3228 The old binding is still visible if explicitly qualified
3229 with a class-key. */
3230 if (TREE_CODE (target_bval) == TYPE_DECL
3231 && DECL_ARTIFICIAL (target_bval)
3232 && !(TREE_CODE (target_decl) == TYPE_DECL
3233 && DECL_ARTIFICIAL (target_decl)))
3235 old_decl = binding->type;
3236 binding->type = bval;
3237 binding->value = NULL_TREE;
3238 INHERITED_VALUE_BINDING_P (binding) = 0;
3240 else
3242 old_decl = bval;
3243 /* Any inherited type declaration is hidden by the type
3244 declaration in the derived class. */
3245 if (TREE_CODE (target_decl) == TYPE_DECL
3246 && DECL_ARTIFICIAL (target_decl))
3247 binding->type = NULL_TREE;
3250 else if (TREE_CODE (target_decl) == OVERLOAD
3251 && is_overloaded_fn (target_bval))
3252 old_decl = bval;
3253 else if (TREE_CODE (decl) == USING_DECL
3254 && TREE_CODE (bval) == USING_DECL
3255 && same_type_p (USING_DECL_SCOPE (decl),
3256 USING_DECL_SCOPE (bval)))
3257 /* This is a using redeclaration that will be diagnosed later
3258 in supplement_binding */
3260 else if (TREE_CODE (decl) == USING_DECL
3261 && TREE_CODE (bval) == USING_DECL
3262 && DECL_DEPENDENT_P (decl)
3263 && DECL_DEPENDENT_P (bval))
3264 return true;
3265 else if (TREE_CODE (decl) == USING_DECL
3266 && is_overloaded_fn (target_bval))
3267 old_decl = bval;
3268 else if (TREE_CODE (bval) == USING_DECL
3269 && is_overloaded_fn (target_decl))
3270 return true;
3272 if (old_decl && binding->scope == class_binding_level)
3274 binding->value = x;
3275 /* It is always safe to clear INHERITED_VALUE_BINDING_P
3276 here. This function is only used to register bindings
3277 from with the class definition itself. */
3278 INHERITED_VALUE_BINDING_P (binding) = 0;
3279 return true;
3283 /* Note that we declared this value so that we can issue an error if
3284 this is an invalid redeclaration of a name already used for some
3285 other purpose. */
3286 note_name_declared_in_class (name, decl);
3288 /* If we didn't replace an existing binding, put the binding on the
3289 stack of bindings for the identifier, and update the shadowed
3290 list. */
3291 if (binding && binding->scope == class_binding_level)
3292 /* Supplement the existing binding. */
3293 ok = supplement_binding (binding, decl);
3294 else
3296 /* Create a new binding. */
3297 push_binding (name, decl, class_binding_level);
3298 ok = true;
3301 return ok;
3304 /* Wrapper for push_class_level_binding_1. */
3306 bool
3307 push_class_level_binding (tree name, tree x)
3309 bool ret;
3310 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3311 ret = push_class_level_binding_1 (name, x);
3312 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3313 return ret;
3316 /* Process "using SCOPE::NAME" in a class scope. Return the
3317 USING_DECL created. */
3319 tree
3320 do_class_using_decl (tree scope, tree name)
3322 /* The USING_DECL returned by this function. */
3323 tree value;
3324 /* The declaration (or declarations) name by this using
3325 declaration. NULL if we are in a template and cannot figure out
3326 what has been named. */
3327 tree decl;
3328 /* True if SCOPE is a dependent type. */
3329 bool scope_dependent_p;
3330 /* True if SCOPE::NAME is dependent. */
3331 bool name_dependent_p;
3332 /* True if any of the bases of CURRENT_CLASS_TYPE are dependent. */
3333 bool bases_dependent_p;
3334 tree binfo;
3335 tree base_binfo;
3336 int i;
3338 if (name == error_mark_node)
3339 return NULL_TREE;
3341 if (!scope || !TYPE_P (scope))
3343 error ("using-declaration for non-member at class scope");
3344 return NULL_TREE;
3347 /* Make sure the name is not invalid */
3348 if (TREE_CODE (name) == BIT_NOT_EXPR)
3350 error ("%<%T::%D%> names destructor", scope, name);
3351 return NULL_TREE;
3353 /* Using T::T declares inheriting ctors, even if T is a typedef. */
3354 if (MAYBE_CLASS_TYPE_P (scope)
3355 && (name == TYPE_IDENTIFIER (scope)
3356 || constructor_name_p (name, scope)))
3358 maybe_warn_cpp0x (CPP0X_INHERITING_CTORS);
3359 name = ctor_identifier;
3361 if (constructor_name_p (name, current_class_type))
3363 error ("%<%T::%D%> names constructor in %qT",
3364 scope, name, current_class_type);
3365 return NULL_TREE;
3368 scope_dependent_p = dependent_scope_p (scope);
3369 name_dependent_p = (scope_dependent_p
3370 || (IDENTIFIER_TYPENAME_P (name)
3371 && dependent_type_p (TREE_TYPE (name))));
3373 bases_dependent_p = false;
3374 if (processing_template_decl)
3375 for (binfo = TYPE_BINFO (current_class_type), i = 0;
3376 BINFO_BASE_ITERATE (binfo, i, base_binfo);
3377 i++)
3378 if (dependent_type_p (TREE_TYPE (base_binfo)))
3380 bases_dependent_p = true;
3381 break;
3384 decl = NULL_TREE;
3386 /* From [namespace.udecl]:
3388 A using-declaration used as a member-declaration shall refer to a
3389 member of a base class of the class being defined.
3391 In general, we cannot check this constraint in a template because
3392 we do not know the entire set of base classes of the current
3393 class type. Morover, if SCOPE is dependent, it might match a
3394 non-dependent base. */
3396 if (!scope_dependent_p)
3398 base_kind b_kind;
3399 binfo = lookup_base (current_class_type, scope, ba_any, &b_kind,
3400 tf_warning_or_error);
3401 if (b_kind < bk_proper_base)
3403 if (!bases_dependent_p || b_kind == bk_same_type)
3405 error_not_base_type (scope, current_class_type);
3406 return NULL_TREE;
3409 else if (!name_dependent_p)
3411 decl = lookup_member (binfo, name, 0, false, tf_warning_or_error);
3412 if (!decl)
3414 error ("no members matching %<%T::%D%> in %q#T", scope, name,
3415 scope);
3416 return NULL_TREE;
3418 /* The binfo from which the functions came does not matter. */
3419 if (BASELINK_P (decl))
3420 decl = BASELINK_FUNCTIONS (decl);
3424 value = build_lang_decl (USING_DECL, name, NULL_TREE);
3425 USING_DECL_DECLS (value) = decl;
3426 USING_DECL_SCOPE (value) = scope;
3427 DECL_DEPENDENT_P (value) = !decl;
3429 return value;
3433 /* Return the binding value for name in scope. */
3436 static tree
3437 namespace_binding_1 (tree name, tree scope)
3439 cxx_binding *binding;
3441 if (SCOPE_FILE_SCOPE_P (scope))
3442 scope = global_namespace;
3443 else
3444 /* Unnecessary for the global namespace because it can't be an alias. */
3445 scope = ORIGINAL_NAMESPACE (scope);
3447 binding = cp_binding_level_find_binding_for_name (NAMESPACE_LEVEL (scope), name);
3449 return binding ? binding->value : NULL_TREE;
3452 tree
3453 namespace_binding (tree name, tree scope)
3455 tree ret;
3456 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3457 ret = namespace_binding_1 (name, scope);
3458 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3459 return ret;
3462 /* Set the binding value for name in scope. */
3464 static void
3465 set_namespace_binding_1 (tree name, tree scope, tree val)
3467 cxx_binding *b;
3469 if (scope == NULL_TREE)
3470 scope = global_namespace;
3471 b = binding_for_name (NAMESPACE_LEVEL (scope), name);
3472 if (!b->value || TREE_CODE (val) == OVERLOAD || val == error_mark_node)
3473 b->value = val;
3474 else
3475 supplement_binding (b, val);
3478 /* Wrapper for set_namespace_binding_1. */
3480 void
3481 set_namespace_binding (tree name, tree scope, tree val)
3483 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3484 set_namespace_binding_1 (name, scope, val);
3485 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3488 /* Set the context of a declaration to scope. Complain if we are not
3489 outside scope. */
3491 void
3492 set_decl_namespace (tree decl, tree scope, bool friendp)
3494 tree old;
3496 /* Get rid of namespace aliases. */
3497 scope = ORIGINAL_NAMESPACE (scope);
3499 /* It is ok for friends to be qualified in parallel space. */
3500 if (!friendp && !is_ancestor (current_namespace, scope))
3501 error ("declaration of %qD not in a namespace surrounding %qD",
3502 decl, scope);
3503 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
3505 /* Writing "int N::i" to declare a variable within "N" is invalid. */
3506 if (scope == current_namespace)
3508 if (at_namespace_scope_p ())
3509 error ("explicit qualification in declaration of %qD",
3510 decl);
3511 return;
3514 /* See whether this has been declared in the namespace. */
3515 old = lookup_qualified_name (scope, DECL_NAME (decl), false, true);
3516 if (old == error_mark_node)
3517 /* No old declaration at all. */
3518 goto complain;
3519 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
3520 if (TREE_CODE (old) == TREE_LIST)
3522 error ("reference to %qD is ambiguous", decl);
3523 print_candidates (old);
3524 return;
3526 if (!is_overloaded_fn (decl))
3528 /* We might have found OLD in an inline namespace inside SCOPE. */
3529 if (TREE_CODE (decl) == TREE_CODE (old))
3530 DECL_CONTEXT (decl) = DECL_CONTEXT (old);
3531 /* Don't compare non-function decls with decls_match here, since
3532 it can't check for the correct constness at this
3533 point. pushdecl will find those errors later. */
3534 return;
3536 /* Since decl is a function, old should contain a function decl. */
3537 if (!is_overloaded_fn (old))
3538 goto complain;
3539 /* A template can be explicitly specialized in any namespace. */
3540 if (processing_explicit_instantiation)
3541 return;
3542 if (processing_template_decl || processing_specialization)
3543 /* We have not yet called push_template_decl to turn a
3544 FUNCTION_DECL into a TEMPLATE_DECL, so the declarations won't
3545 match. But, we'll check later, when we construct the
3546 template. */
3547 return;
3548 /* Instantiations or specializations of templates may be declared as
3549 friends in any namespace. */
3550 if (friendp && DECL_USE_TEMPLATE (decl))
3551 return;
3552 if (is_overloaded_fn (old))
3554 tree found = NULL_TREE;
3555 tree elt = old;
3556 for (; elt; elt = OVL_NEXT (elt))
3558 tree ofn = OVL_CURRENT (elt);
3559 /* Adjust DECL_CONTEXT first so decls_match will return true
3560 if DECL will match a declaration in an inline namespace. */
3561 DECL_CONTEXT (decl) = DECL_CONTEXT (ofn);
3562 if (decls_match (decl, ofn))
3564 if (found && !decls_match (found, ofn))
3566 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
3567 error ("reference to %qD is ambiguous", decl);
3568 print_candidates (old);
3569 return;
3571 found = ofn;
3574 if (found)
3576 if (!is_associated_namespace (scope, CP_DECL_CONTEXT (found)))
3577 goto complain;
3578 DECL_CONTEXT (decl) = DECL_CONTEXT (found);
3579 return;
3582 else
3584 DECL_CONTEXT (decl) = DECL_CONTEXT (old);
3585 if (decls_match (decl, old))
3586 return;
3589 /* It didn't work, go back to the explicit scope. */
3590 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
3591 complain:
3592 error ("%qD should have been declared inside %qD", decl, scope);
3595 /* Return the namespace where the current declaration is declared. */
3597 tree
3598 current_decl_namespace (void)
3600 tree result;
3601 /* If we have been pushed into a different namespace, use it. */
3602 if (!vec_safe_is_empty (decl_namespace_list))
3603 return decl_namespace_list->last ();
3605 if (current_class_type)
3606 result = decl_namespace_context (current_class_type);
3607 else if (current_function_decl)
3608 result = decl_namespace_context (current_function_decl);
3609 else
3610 result = current_namespace;
3611 return result;
3614 /* Process any ATTRIBUTES on a namespace definition. Returns true if
3615 attribute visibility is seen. */
3617 bool
3618 handle_namespace_attrs (tree ns, tree attributes)
3620 tree d;
3621 bool saw_vis = false;
3623 for (d = attributes; d; d = TREE_CHAIN (d))
3625 tree name = get_attribute_name (d);
3626 tree args = TREE_VALUE (d);
3628 if (is_attribute_p ("visibility", name))
3630 /* attribute visibility is a property of the syntactic block
3631 rather than the namespace as a whole, so we don't touch the
3632 NAMESPACE_DECL at all. */
3633 tree x = args ? TREE_VALUE (args) : NULL_TREE;
3634 if (x == NULL_TREE || TREE_CODE (x) != STRING_CST || TREE_CHAIN (args))
3636 warning (OPT_Wattributes,
3637 "%qD attribute requires a single NTBS argument",
3638 name);
3639 continue;
3642 if (!TREE_PUBLIC (ns))
3643 warning (OPT_Wattributes,
3644 "%qD attribute is meaningless since members of the "
3645 "anonymous namespace get local symbols", name);
3647 push_visibility (TREE_STRING_POINTER (x), 1);
3648 saw_vis = true;
3650 else if (is_attribute_p ("abi_tag", name))
3652 if (!DECL_NAMESPACE_ASSOCIATIONS (ns))
3654 warning (OPT_Wattributes, "ignoring %qD attribute on non-inline "
3655 "namespace", name);
3656 continue;
3658 if (!DECL_NAME (ns))
3660 warning (OPT_Wattributes, "ignoring %qD attribute on anonymous "
3661 "namespace", name);
3662 continue;
3664 if (!args)
3666 tree dn = DECL_NAME (ns);
3667 args = build_string (IDENTIFIER_LENGTH (dn) + 1,
3668 IDENTIFIER_POINTER (dn));
3669 TREE_TYPE (args) = char_array_type_node;
3670 args = fix_string_type (args);
3671 args = build_tree_list (NULL_TREE, args);
3673 if (check_abi_tag_args (args, name))
3674 DECL_ATTRIBUTES (ns) = tree_cons (name, args,
3675 DECL_ATTRIBUTES (ns));
3677 else
3679 warning (OPT_Wattributes, "%qD attribute directive ignored",
3680 name);
3681 continue;
3685 return saw_vis;
3688 /* Push into the scope of the NAME namespace. If NAME is NULL_TREE, then we
3689 select a name that is unique to this compilation unit. */
3691 void
3692 push_namespace (tree name)
3694 tree d = NULL_TREE;
3695 bool need_new = true;
3696 bool implicit_use = false;
3697 bool anon = !name;
3699 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3701 /* We should not get here if the global_namespace is not yet constructed
3702 nor if NAME designates the global namespace: The global scope is
3703 constructed elsewhere. */
3704 gcc_assert (global_namespace != NULL && name != global_scope_name);
3706 if (anon)
3708 name = get_anonymous_namespace_name();
3709 d = IDENTIFIER_NAMESPACE_VALUE (name);
3710 if (d)
3711 /* Reopening anonymous namespace. */
3712 need_new = false;
3713 implicit_use = true;
3715 else
3717 /* Check whether this is an extended namespace definition. */
3718 d = IDENTIFIER_NAMESPACE_VALUE (name);
3719 if (d != NULL_TREE && TREE_CODE (d) == NAMESPACE_DECL)
3721 tree dna = DECL_NAMESPACE_ALIAS (d);
3722 if (dna)
3724 /* We do some error recovery for, eg, the redeclaration
3725 of M here:
3727 namespace N {}
3728 namespace M = N;
3729 namespace M {}
3731 However, in nasty cases like:
3733 namespace N
3735 namespace M = N;
3736 namespace M {}
3739 we just error out below, in duplicate_decls. */
3740 if (NAMESPACE_LEVEL (dna)->level_chain
3741 == current_binding_level)
3743 error ("namespace alias %qD not allowed here, "
3744 "assuming %qD", d, dna);
3745 d = dna;
3746 need_new = false;
3749 else
3750 need_new = false;
3754 if (need_new)
3756 /* Make a new namespace, binding the name to it. */
3757 d = build_lang_decl (NAMESPACE_DECL, name, void_type_node);
3758 DECL_CONTEXT (d) = FROB_CONTEXT (current_namespace);
3759 /* The name of this namespace is not visible to other translation
3760 units if it is an anonymous namespace or member thereof. */
3761 if (anon || decl_anon_ns_mem_p (current_namespace))
3762 TREE_PUBLIC (d) = 0;
3763 else
3764 TREE_PUBLIC (d) = 1;
3765 pushdecl (d);
3766 if (anon)
3768 /* Clear DECL_NAME for the benefit of debugging back ends. */
3769 SET_DECL_ASSEMBLER_NAME (d, name);
3770 DECL_NAME (d) = NULL_TREE;
3772 begin_scope (sk_namespace, d);
3774 else
3775 resume_scope (NAMESPACE_LEVEL (d));
3777 if (implicit_use)
3778 do_using_directive (d);
3779 /* Enter the name space. */
3780 current_namespace = d;
3782 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3785 /* Pop from the scope of the current namespace. */
3787 void
3788 pop_namespace (void)
3790 gcc_assert (current_namespace != global_namespace);
3791 current_namespace = CP_DECL_CONTEXT (current_namespace);
3792 /* The binding level is not popped, as it might be re-opened later. */
3793 leave_scope ();
3796 /* Push into the scope of the namespace NS, even if it is deeply
3797 nested within another namespace. */
3799 void
3800 push_nested_namespace (tree ns)
3802 if (ns == global_namespace)
3803 push_to_top_level ();
3804 else
3806 push_nested_namespace (CP_DECL_CONTEXT (ns));
3807 push_namespace (DECL_NAME (ns));
3811 /* Pop back from the scope of the namespace NS, which was previously
3812 entered with push_nested_namespace. */
3814 void
3815 pop_nested_namespace (tree ns)
3817 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3818 gcc_assert (current_namespace == ns);
3819 while (ns != global_namespace)
3821 pop_namespace ();
3822 ns = CP_DECL_CONTEXT (ns);
3825 pop_from_top_level ();
3826 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3829 /* Temporarily set the namespace for the current declaration. */
3831 void
3832 push_decl_namespace (tree decl)
3834 if (TREE_CODE (decl) != NAMESPACE_DECL)
3835 decl = decl_namespace_context (decl);
3836 vec_safe_push (decl_namespace_list, ORIGINAL_NAMESPACE (decl));
3839 /* [namespace.memdef]/2 */
3841 void
3842 pop_decl_namespace (void)
3844 decl_namespace_list->pop ();
3847 /* Return the namespace that is the common ancestor
3848 of two given namespaces. */
3850 static tree
3851 namespace_ancestor_1 (tree ns1, tree ns2)
3853 tree nsr;
3854 if (is_ancestor (ns1, ns2))
3855 nsr = ns1;
3856 else
3857 nsr = namespace_ancestor_1 (CP_DECL_CONTEXT (ns1), ns2);
3858 return nsr;
3861 /* Wrapper for namespace_ancestor_1. */
3863 static tree
3864 namespace_ancestor (tree ns1, tree ns2)
3866 tree nsr;
3867 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3868 nsr = namespace_ancestor_1 (ns1, ns2);
3869 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3870 return nsr;
3873 /* Process a namespace-alias declaration. */
3875 void
3876 do_namespace_alias (tree alias, tree name_space)
3878 if (name_space == error_mark_node)
3879 return;
3881 gcc_assert (TREE_CODE (name_space) == NAMESPACE_DECL);
3883 name_space = ORIGINAL_NAMESPACE (name_space);
3885 /* Build the alias. */
3886 alias = build_lang_decl (NAMESPACE_DECL, alias, void_type_node);
3887 DECL_NAMESPACE_ALIAS (alias) = name_space;
3888 DECL_EXTERNAL (alias) = 1;
3889 DECL_CONTEXT (alias) = FROB_CONTEXT (current_scope ());
3890 pushdecl (alias);
3892 /* Emit debug info for namespace alias. */
3893 if (!building_stmt_list_p ())
3894 (*debug_hooks->early_global_decl) (alias);
3897 /* Like pushdecl, only it places X in the current namespace,
3898 if appropriate. */
3900 tree
3901 pushdecl_namespace_level (tree x, bool is_friend)
3903 cp_binding_level *b = current_binding_level;
3904 tree t;
3906 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3907 t = pushdecl_with_scope (x, NAMESPACE_LEVEL (current_namespace), is_friend);
3909 /* Now, the type_shadowed stack may screw us. Munge it so it does
3910 what we want. */
3911 if (TREE_CODE (t) == TYPE_DECL)
3913 tree name = DECL_NAME (t);
3914 tree newval;
3915 tree *ptr = (tree *)0;
3916 for (; !global_scope_p (b); b = b->level_chain)
3918 tree shadowed = b->type_shadowed;
3919 for (; shadowed; shadowed = TREE_CHAIN (shadowed))
3920 if (TREE_PURPOSE (shadowed) == name)
3922 ptr = &TREE_VALUE (shadowed);
3923 /* Can't break out of the loop here because sometimes
3924 a binding level will have duplicate bindings for
3925 PT names. It's gross, but I haven't time to fix it. */
3928 newval = TREE_TYPE (t);
3929 if (ptr == (tree *)0)
3931 /* @@ This shouldn't be needed. My test case "zstring.cc" trips
3932 up here if this is changed to an assertion. --KR */
3933 SET_IDENTIFIER_TYPE_VALUE (name, t);
3935 else
3937 *ptr = newval;
3940 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3941 return t;
3944 /* Insert USED into the using list of USER. Set INDIRECT_flag if this
3945 directive is not directly from the source. Also find the common
3946 ancestor and let our users know about the new namespace */
3948 static void
3949 add_using_namespace_1 (tree user, tree used, bool indirect)
3951 tree t;
3952 /* Using oneself is a no-op. */
3953 if (user == used)
3954 return;
3955 gcc_assert (TREE_CODE (user) == NAMESPACE_DECL);
3956 gcc_assert (TREE_CODE (used) == NAMESPACE_DECL);
3957 /* Check if we already have this. */
3958 t = purpose_member (used, DECL_NAMESPACE_USING (user));
3959 if (t != NULL_TREE)
3961 if (!indirect)
3962 /* Promote to direct usage. */
3963 TREE_INDIRECT_USING (t) = 0;
3964 return;
3967 /* Add used to the user's using list. */
3968 DECL_NAMESPACE_USING (user)
3969 = tree_cons (used, namespace_ancestor (user, used),
3970 DECL_NAMESPACE_USING (user));
3972 TREE_INDIRECT_USING (DECL_NAMESPACE_USING (user)) = indirect;
3974 /* Add user to the used's users list. */
3975 DECL_NAMESPACE_USERS (used)
3976 = tree_cons (user, 0, DECL_NAMESPACE_USERS (used));
3978 /* Recursively add all namespaces used. */
3979 for (t = DECL_NAMESPACE_USING (used); t; t = TREE_CHAIN (t))
3980 /* indirect usage */
3981 add_using_namespace_1 (user, TREE_PURPOSE (t), 1);
3983 /* Tell everyone using us about the new used namespaces. */
3984 for (t = DECL_NAMESPACE_USERS (user); t; t = TREE_CHAIN (t))
3985 add_using_namespace_1 (TREE_PURPOSE (t), used, 1);
3988 /* Wrapper for add_using_namespace_1. */
3990 static void
3991 add_using_namespace (tree user, tree used, bool indirect)
3993 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3994 add_using_namespace_1 (user, used, indirect);
3995 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3998 /* Process a using-declaration not appearing in class or local scope. */
4000 void
4001 do_toplevel_using_decl (tree decl, tree scope, tree name)
4003 tree oldval, oldtype, newval, newtype;
4004 tree orig_decl = decl;
4005 cxx_binding *binding;
4007 decl = validate_nonmember_using_decl (decl, scope, name);
4008 if (decl == NULL_TREE)
4009 return;
4011 binding = binding_for_name (NAMESPACE_LEVEL (current_namespace), name);
4013 oldval = binding->value;
4014 oldtype = binding->type;
4016 do_nonmember_using_decl (scope, name, oldval, oldtype, &newval, &newtype);
4018 /* Emit debug info. */
4019 if (!processing_template_decl)
4020 cp_emit_debug_info_for_using (orig_decl, current_namespace);
4022 /* Copy declarations found. */
4023 if (newval)
4024 binding->value = newval;
4025 if (newtype)
4026 binding->type = newtype;
4029 /* Process a using-directive. */
4031 void
4032 do_using_directive (tree name_space)
4034 tree context = NULL_TREE;
4036 if (name_space == error_mark_node)
4037 return;
4039 gcc_assert (TREE_CODE (name_space) == NAMESPACE_DECL);
4041 if (building_stmt_list_p ())
4042 add_stmt (build_stmt (input_location, USING_STMT, name_space));
4043 name_space = ORIGINAL_NAMESPACE (name_space);
4045 if (!toplevel_bindings_p ())
4047 push_using_directive (name_space);
4049 else
4051 /* direct usage */
4052 add_using_namespace (current_namespace, name_space, 0);
4053 if (current_namespace != global_namespace)
4054 context = current_namespace;
4056 /* Emit debugging info. */
4057 if (!processing_template_decl)
4058 (*debug_hooks->imported_module_or_decl) (name_space, NULL_TREE,
4059 context, false);
4063 /* Deal with a using-directive seen by the parser. Currently we only
4064 handle attributes here, since they cannot appear inside a template. */
4066 void
4067 parse_using_directive (tree name_space, tree attribs)
4069 do_using_directive (name_space);
4071 if (attribs == error_mark_node)
4072 return;
4074 for (tree a = attribs; a; a = TREE_CHAIN (a))
4076 tree name = get_attribute_name (a);
4077 if (is_attribute_p ("strong", name))
4079 if (!toplevel_bindings_p ())
4080 error ("strong using only meaningful at namespace scope");
4081 else if (name_space != error_mark_node)
4083 if (!is_ancestor (current_namespace, name_space))
4084 error ("current namespace %qD does not enclose strongly used namespace %qD",
4085 current_namespace, name_space);
4086 DECL_NAMESPACE_ASSOCIATIONS (name_space)
4087 = tree_cons (current_namespace, 0,
4088 DECL_NAMESPACE_ASSOCIATIONS (name_space));
4091 else
4092 warning (OPT_Wattributes, "%qD attribute directive ignored", name);
4096 /* Like pushdecl, only it places X in the global scope if appropriate.
4097 Calls cp_finish_decl to register the variable, initializing it with
4098 *INIT, if INIT is non-NULL. */
4100 static tree
4101 pushdecl_top_level_1 (tree x, tree *init, bool is_friend)
4103 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4104 push_to_top_level ();
4105 x = pushdecl_namespace_level (x, is_friend);
4106 if (init)
4107 cp_finish_decl (x, *init, false, NULL_TREE, 0);
4108 pop_from_top_level ();
4109 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4110 return x;
4113 /* Like pushdecl, only it places X in the global scope if appropriate. */
4115 tree
4116 pushdecl_top_level (tree x)
4118 return pushdecl_top_level_1 (x, NULL, false);
4121 /* Like pushdecl_top_level, but adding the IS_FRIEND parameter. */
4123 tree
4124 pushdecl_top_level_maybe_friend (tree x, bool is_friend)
4126 return pushdecl_top_level_1 (x, NULL, is_friend);
4129 /* Like pushdecl, only it places X in the global scope if
4130 appropriate. Calls cp_finish_decl to register the variable,
4131 initializing it with INIT. */
4133 tree
4134 pushdecl_top_level_and_finish (tree x, tree init)
4136 return pushdecl_top_level_1 (x, &init, false);
4139 /* Combines two sets of overloaded functions into an OVERLOAD chain, removing
4140 duplicates. The first list becomes the tail of the result.
4142 The algorithm is O(n^2). We could get this down to O(n log n) by
4143 doing a sort on the addresses of the functions, if that becomes
4144 necessary. */
4146 static tree
4147 merge_functions (tree s1, tree s2)
4149 for (; s2; s2 = OVL_NEXT (s2))
4151 tree fn2 = OVL_CURRENT (s2);
4152 tree fns1;
4154 for (fns1 = s1; fns1; fns1 = OVL_NEXT (fns1))
4156 tree fn1 = OVL_CURRENT (fns1);
4158 /* If the function from S2 is already in S1, there is no
4159 need to add it again. For `extern "C"' functions, we
4160 might have two FUNCTION_DECLs for the same function, in
4161 different namespaces, but let's leave them in case
4162 they have different default arguments. */
4163 if (fn1 == fn2)
4164 break;
4167 /* If we exhausted all of the functions in S1, FN2 is new. */
4168 if (!fns1)
4169 s1 = build_overload (fn2, s1);
4171 return s1;
4174 /* Returns TRUE iff OLD and NEW are the same entity.
4176 3 [basic]/3: An entity is a value, object, reference, function,
4177 enumerator, type, class member, template, template specialization,
4178 namespace, parameter pack, or this.
4180 7.3.4 [namespace.udir]/4: If name lookup finds a declaration for a name
4181 in two different namespaces, and the declarations do not declare the
4182 same entity and do not declare functions, the use of the name is
4183 ill-formed. */
4185 static bool
4186 same_entity_p (tree one, tree two)
4188 if (one == two)
4189 return true;
4190 if (!one || !two)
4191 return false;
4192 if (TREE_CODE (one) == TYPE_DECL
4193 && TREE_CODE (two) == TYPE_DECL
4194 && same_type_p (TREE_TYPE (one), TREE_TYPE (two)))
4195 return true;
4196 return false;
4199 /* This should return an error not all definitions define functions.
4200 It is not an error if we find two functions with exactly the
4201 same signature, only if these are selected in overload resolution.
4202 old is the current set of bindings, new_binding the freshly-found binding.
4203 XXX Do we want to give *all* candidates in case of ambiguity?
4204 XXX In what way should I treat extern declarations?
4205 XXX I don't want to repeat the entire duplicate_decls here */
4207 static void
4208 ambiguous_decl (struct scope_binding *old, cxx_binding *new_binding, int flags)
4210 tree val, type;
4211 gcc_assert (old != NULL);
4213 /* Copy the type. */
4214 type = new_binding->type;
4215 if (LOOKUP_NAMESPACES_ONLY (flags)
4216 || (type && hidden_name_p (type) && !(flags & LOOKUP_HIDDEN)))
4217 type = NULL_TREE;
4219 /* Copy the value. */
4220 val = new_binding->value;
4221 if (val)
4223 if (hidden_name_p (val) && !(flags & LOOKUP_HIDDEN))
4224 val = NULL_TREE;
4225 else
4226 switch (TREE_CODE (val))
4228 case TEMPLATE_DECL:
4229 /* If we expect types or namespaces, and not templates,
4230 or this is not a template class. */
4231 if ((LOOKUP_QUALIFIERS_ONLY (flags)
4232 && !DECL_TYPE_TEMPLATE_P (val)))
4233 val = NULL_TREE;
4234 break;
4235 case TYPE_DECL:
4236 if (LOOKUP_NAMESPACES_ONLY (flags)
4237 || (type && (flags & LOOKUP_PREFER_TYPES)))
4238 val = NULL_TREE;
4239 break;
4240 case NAMESPACE_DECL:
4241 if (LOOKUP_TYPES_ONLY (flags))
4242 val = NULL_TREE;
4243 break;
4244 case FUNCTION_DECL:
4245 /* Ignore built-in functions that are still anticipated. */
4246 if (LOOKUP_QUALIFIERS_ONLY (flags))
4247 val = NULL_TREE;
4248 break;
4249 default:
4250 if (LOOKUP_QUALIFIERS_ONLY (flags))
4251 val = NULL_TREE;
4255 /* If val is hidden, shift down any class or enumeration name. */
4256 if (!val)
4258 val = type;
4259 type = NULL_TREE;
4262 if (!old->value)
4263 old->value = val;
4264 else if (val && !same_entity_p (val, old->value))
4266 if (is_overloaded_fn (old->value) && is_overloaded_fn (val))
4267 old->value = merge_functions (old->value, val);
4268 else
4270 old->value = tree_cons (NULL_TREE, old->value,
4271 build_tree_list (NULL_TREE, val));
4272 TREE_TYPE (old->value) = error_mark_node;
4276 if (!old->type)
4277 old->type = type;
4278 else if (type && old->type != type)
4280 old->type = tree_cons (NULL_TREE, old->type,
4281 build_tree_list (NULL_TREE, type));
4282 TREE_TYPE (old->type) = error_mark_node;
4286 /* Return the declarations that are members of the namespace NS. */
4288 tree
4289 cp_namespace_decls (tree ns)
4291 return NAMESPACE_LEVEL (ns)->names;
4294 /* Combine prefer_type and namespaces_only into flags. */
4296 static int
4297 lookup_flags (int prefer_type, int namespaces_only)
4299 if (namespaces_only)
4300 return LOOKUP_PREFER_NAMESPACES;
4301 if (prefer_type > 1)
4302 return LOOKUP_PREFER_TYPES;
4303 if (prefer_type > 0)
4304 return LOOKUP_PREFER_BOTH;
4305 return 0;
4308 /* Given a lookup that returned VAL, use FLAGS to decide if we want to
4309 ignore it or not. Subroutine of lookup_name_real and
4310 lookup_type_scope. */
4312 static bool
4313 qualify_lookup (tree val, int flags)
4315 if (val == NULL_TREE)
4316 return false;
4317 if ((flags & LOOKUP_PREFER_NAMESPACES) && TREE_CODE (val) == NAMESPACE_DECL)
4318 return true;
4319 if (flags & LOOKUP_PREFER_TYPES)
4321 tree target_val = strip_using_decl (val);
4322 if (TREE_CODE (target_val) == TYPE_DECL
4323 || TREE_CODE (target_val) == TEMPLATE_DECL)
4324 return true;
4326 if (flags & (LOOKUP_PREFER_NAMESPACES | LOOKUP_PREFER_TYPES))
4327 return false;
4328 /* Look through lambda things that we shouldn't be able to see. */
4329 if (is_lambda_ignored_entity (val))
4330 return false;
4331 return true;
4334 /* Given a lookup that returned VAL, decide if we want to ignore it or
4335 not based on DECL_ANTICIPATED. */
4337 bool
4338 hidden_name_p (tree val)
4340 if (DECL_P (val)
4341 && DECL_LANG_SPECIFIC (val)
4342 && TYPE_FUNCTION_OR_TEMPLATE_DECL_P (val)
4343 && DECL_ANTICIPATED (val))
4344 return true;
4345 return false;
4348 /* Remove any hidden friend functions from a possibly overloaded set
4349 of functions. */
4351 tree
4352 remove_hidden_names (tree fns)
4354 if (!fns)
4355 return fns;
4357 if (TREE_CODE (fns) == FUNCTION_DECL && hidden_name_p (fns))
4358 fns = NULL_TREE;
4359 else if (TREE_CODE (fns) == OVERLOAD)
4361 tree o;
4363 for (o = fns; o; o = OVL_NEXT (o))
4364 if (hidden_name_p (OVL_CURRENT (o)))
4365 break;
4366 if (o)
4368 tree n = NULL_TREE;
4370 for (o = fns; o; o = OVL_NEXT (o))
4371 if (!hidden_name_p (OVL_CURRENT (o)))
4372 n = build_overload (OVL_CURRENT (o), n);
4373 fns = n;
4377 return fns;
4380 /* Suggest alternatives for NAME, an IDENTIFIER_NODE for which name
4381 lookup failed. Search through all available namespaces and print out
4382 possible candidates. */
4384 void
4385 suggest_alternatives_for (location_t location, tree name)
4387 vec<tree> candidates = vNULL;
4388 vec<tree> namespaces_to_search = vNULL;
4389 int max_to_search = PARAM_VALUE (CXX_MAX_NAMESPACES_FOR_DIAGNOSTIC_HELP);
4390 int n_searched = 0;
4391 tree t;
4392 unsigned ix;
4394 namespaces_to_search.safe_push (global_namespace);
4396 while (!namespaces_to_search.is_empty ()
4397 && n_searched < max_to_search)
4399 tree scope = namespaces_to_search.pop ();
4400 struct scope_binding binding = EMPTY_SCOPE_BINDING;
4401 cp_binding_level *level = NAMESPACE_LEVEL (scope);
4403 /* Look in this namespace. */
4404 qualified_lookup_using_namespace (name, scope, &binding, 0);
4406 n_searched++;
4408 if (binding.value)
4409 candidates.safe_push (binding.value);
4411 /* Add child namespaces. */
4412 for (t = level->namespaces; t; t = DECL_CHAIN (t))
4413 namespaces_to_search.safe_push (t);
4416 /* If we stopped before we could examine all namespaces, inform the
4417 user. Do this even if we don't have any candidates, since there
4418 might be more candidates further down that we weren't able to
4419 find. */
4420 if (n_searched >= max_to_search
4421 && !namespaces_to_search.is_empty ())
4422 inform (location,
4423 "maximum limit of %d namespaces searched for %qE",
4424 max_to_search, name);
4426 namespaces_to_search.release ();
4428 /* Nothing useful to report. */
4429 if (candidates.is_empty ())
4430 return;
4432 inform_n (location, candidates.length (),
4433 "suggested alternative:",
4434 "suggested alternatives:");
4436 FOR_EACH_VEC_ELT (candidates, ix, t)
4437 inform (location_of (t), " %qE", t);
4439 candidates.release ();
4442 /* Unscoped lookup of a global: iterate over current namespaces,
4443 considering using-directives. */
4445 static tree
4446 unqualified_namespace_lookup_1 (tree name, int flags)
4448 tree initial = current_decl_namespace ();
4449 tree scope = initial;
4450 tree siter;
4451 cp_binding_level *level;
4452 tree val = NULL_TREE;
4454 for (; !val; scope = CP_DECL_CONTEXT (scope))
4456 struct scope_binding binding = EMPTY_SCOPE_BINDING;
4457 cxx_binding *b =
4458 cp_binding_level_find_binding_for_name (NAMESPACE_LEVEL (scope), name);
4460 if (b)
4461 ambiguous_decl (&binding, b, flags);
4463 /* Add all _DECLs seen through local using-directives. */
4464 for (level = current_binding_level;
4465 level->kind != sk_namespace;
4466 level = level->level_chain)
4467 if (!lookup_using_namespace (name, &binding, level->using_directives,
4468 scope, flags))
4469 /* Give up because of error. */
4470 return error_mark_node;
4472 /* Add all _DECLs seen through global using-directives. */
4473 /* XXX local and global using lists should work equally. */
4474 siter = initial;
4475 while (1)
4477 if (!lookup_using_namespace (name, &binding,
4478 DECL_NAMESPACE_USING (siter),
4479 scope, flags))
4480 /* Give up because of error. */
4481 return error_mark_node;
4482 if (siter == scope) break;
4483 siter = CP_DECL_CONTEXT (siter);
4486 val = binding.value;
4487 if (scope == global_namespace)
4488 break;
4490 return val;
4493 /* Wrapper for unqualified_namespace_lookup_1. */
4495 static tree
4496 unqualified_namespace_lookup (tree name, int flags)
4498 tree ret;
4499 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4500 ret = unqualified_namespace_lookup_1 (name, flags);
4501 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4502 return ret;
4505 /* Look up NAME (an IDENTIFIER_NODE) in SCOPE (either a NAMESPACE_DECL
4506 or a class TYPE). If IS_TYPE_P is TRUE, then ignore non-type
4507 bindings.
4509 Returns a DECL (or OVERLOAD, or BASELINK) representing the
4510 declaration found. If no suitable declaration can be found,
4511 ERROR_MARK_NODE is returned. If COMPLAIN is true and SCOPE is
4512 neither a class-type nor a namespace a diagnostic is issued. */
4514 tree
4515 lookup_qualified_name (tree scope, tree name, bool is_type_p, bool complain)
4517 int flags = 0;
4518 tree t = NULL_TREE;
4520 if (TREE_CODE (scope) == NAMESPACE_DECL)
4522 struct scope_binding binding = EMPTY_SCOPE_BINDING;
4524 if (is_type_p)
4525 flags |= LOOKUP_PREFER_TYPES;
4526 if (qualified_lookup_using_namespace (name, scope, &binding, flags))
4527 t = binding.value;
4529 else if (cxx_dialect != cxx98 && TREE_CODE (scope) == ENUMERAL_TYPE)
4530 t = lookup_enumerator (scope, name);
4531 else if (is_class_type (scope, complain))
4532 t = lookup_member (scope, name, 2, is_type_p, tf_warning_or_error);
4534 if (!t)
4535 return error_mark_node;
4536 return t;
4539 /* Subroutine of unqualified_namespace_lookup:
4540 Add the bindings of NAME in used namespaces to VAL.
4541 We are currently looking for names in namespace SCOPE, so we
4542 look through USINGS for using-directives of namespaces
4543 which have SCOPE as a common ancestor with the current scope.
4544 Returns false on errors. */
4546 static bool
4547 lookup_using_namespace (tree name, struct scope_binding *val,
4548 tree usings, tree scope, int flags)
4550 tree iter;
4551 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4552 /* Iterate over all used namespaces in current, searching for using
4553 directives of scope. */
4554 for (iter = usings; iter; iter = TREE_CHAIN (iter))
4555 if (TREE_VALUE (iter) == scope)
4557 tree used = ORIGINAL_NAMESPACE (TREE_PURPOSE (iter));
4558 cxx_binding *val1 =
4559 cp_binding_level_find_binding_for_name (NAMESPACE_LEVEL (used), name);
4560 /* Resolve ambiguities. */
4561 if (val1)
4562 ambiguous_decl (val, val1, flags);
4564 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4565 return val->value != error_mark_node;
4568 /* Returns true iff VEC contains TARGET. */
4570 static bool
4571 tree_vec_contains (vec<tree, va_gc> *vec, tree target)
4573 unsigned int i;
4574 tree elt;
4575 FOR_EACH_VEC_SAFE_ELT (vec,i,elt)
4576 if (elt == target)
4577 return true;
4578 return false;
4581 /* [namespace.qual]
4582 Accepts the NAME to lookup and its qualifying SCOPE.
4583 Returns the name/type pair found into the cxx_binding *RESULT,
4584 or false on error. */
4586 static bool
4587 qualified_lookup_using_namespace (tree name, tree scope,
4588 struct scope_binding *result, int flags)
4590 /* Maintain a list of namespaces visited... */
4591 vec<tree, va_gc> *seen = NULL;
4592 vec<tree, va_gc> *seen_inline = NULL;
4593 /* ... and a list of namespace yet to see. */
4594 vec<tree, va_gc> *todo = NULL;
4595 vec<tree, va_gc> *todo_maybe = NULL;
4596 vec<tree, va_gc> *todo_inline = NULL;
4597 tree usings;
4598 timevar_start (TV_NAME_LOOKUP);
4599 /* Look through namespace aliases. */
4600 scope = ORIGINAL_NAMESPACE (scope);
4602 /* Algorithm: Starting with SCOPE, walk through the set of used
4603 namespaces. For each used namespace, look through its inline
4604 namespace set for any bindings and usings. If no bindings are
4605 found, add any usings seen to the set of used namespaces. */
4606 vec_safe_push (todo, scope);
4608 while (todo->length ())
4610 bool found_here;
4611 scope = todo->pop ();
4612 if (tree_vec_contains (seen, scope))
4613 continue;
4614 vec_safe_push (seen, scope);
4615 vec_safe_push (todo_inline, scope);
4617 found_here = false;
4618 while (todo_inline->length ())
4620 cxx_binding *binding;
4622 scope = todo_inline->pop ();
4623 if (tree_vec_contains (seen_inline, scope))
4624 continue;
4625 vec_safe_push (seen_inline, scope);
4627 binding =
4628 cp_binding_level_find_binding_for_name (NAMESPACE_LEVEL (scope), name);
4629 if (binding)
4631 found_here = true;
4632 ambiguous_decl (result, binding, flags);
4635 for (usings = DECL_NAMESPACE_USING (scope); usings;
4636 usings = TREE_CHAIN (usings))
4637 if (!TREE_INDIRECT_USING (usings))
4639 if (is_associated_namespace (scope, TREE_PURPOSE (usings)))
4640 vec_safe_push (todo_inline, TREE_PURPOSE (usings));
4641 else
4642 vec_safe_push (todo_maybe, TREE_PURPOSE (usings));
4646 if (found_here)
4647 vec_safe_truncate (todo_maybe, 0);
4648 else
4649 while (vec_safe_length (todo_maybe))
4650 vec_safe_push (todo, todo_maybe->pop ());
4652 vec_free (todo);
4653 vec_free (todo_maybe);
4654 vec_free (todo_inline);
4655 vec_free (seen);
4656 vec_free (seen_inline);
4657 timevar_stop (TV_NAME_LOOKUP);
4658 return result->value != error_mark_node;
4661 /* Subroutine of outer_binding.
4663 Returns TRUE if BINDING is a binding to a template parameter of
4664 SCOPE. In that case SCOPE is the scope of a primary template
4665 parameter -- in the sense of G++, i.e, a template that has its own
4666 template header.
4668 Returns FALSE otherwise. */
4670 static bool
4671 binding_to_template_parms_of_scope_p (cxx_binding *binding,
4672 cp_binding_level *scope)
4674 tree binding_value, tmpl, tinfo;
4675 int level;
4677 if (!binding || !scope || !scope->this_entity)
4678 return false;
4680 binding_value = binding->value ? binding->value : binding->type;
4681 tinfo = get_template_info (scope->this_entity);
4683 /* BINDING_VALUE must be a template parm. */
4684 if (binding_value == NULL_TREE
4685 || (!DECL_P (binding_value)
4686 || !DECL_TEMPLATE_PARM_P (binding_value)))
4687 return false;
4689 /* The level of BINDING_VALUE. */
4690 level =
4691 template_type_parameter_p (binding_value)
4692 ? TEMPLATE_PARM_LEVEL (TEMPLATE_TYPE_PARM_INDEX
4693 (TREE_TYPE (binding_value)))
4694 : TEMPLATE_PARM_LEVEL (DECL_INITIAL (binding_value));
4696 /* The template of the current scope, iff said scope is a primary
4697 template. */
4698 tmpl = (tinfo
4699 && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
4700 ? TI_TEMPLATE (tinfo)
4701 : NULL_TREE);
4703 /* If the level of the parm BINDING_VALUE equals the depth of TMPL,
4704 then BINDING_VALUE is a parameter of TMPL. */
4705 return (tmpl && level == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)));
4708 /* Return the innermost non-namespace binding for NAME from a scope
4709 containing BINDING, or, if BINDING is NULL, the current scope.
4710 Please note that for a given template, the template parameters are
4711 considered to be in the scope containing the current scope.
4712 If CLASS_P is false, then class bindings are ignored. */
4714 cxx_binding *
4715 outer_binding (tree name,
4716 cxx_binding *binding,
4717 bool class_p)
4719 cxx_binding *outer;
4720 cp_binding_level *scope;
4721 cp_binding_level *outer_scope;
4723 if (binding)
4725 scope = binding->scope->level_chain;
4726 outer = binding->previous;
4728 else
4730 scope = current_binding_level;
4731 outer = IDENTIFIER_BINDING (name);
4733 outer_scope = outer ? outer->scope : NULL;
4735 /* Because we create class bindings lazily, we might be missing a
4736 class binding for NAME. If there are any class binding levels
4737 between the LAST_BINDING_LEVEL and the scope in which OUTER was
4738 declared, we must lookup NAME in those class scopes. */
4739 if (class_p)
4740 while (scope && scope != outer_scope && scope->kind != sk_namespace)
4742 if (scope->kind == sk_class)
4744 cxx_binding *class_binding;
4746 class_binding = get_class_binding (name, scope);
4747 if (class_binding)
4749 /* Thread this new class-scope binding onto the
4750 IDENTIFIER_BINDING list so that future lookups
4751 find it quickly. */
4752 class_binding->previous = outer;
4753 if (binding)
4754 binding->previous = class_binding;
4755 else
4756 IDENTIFIER_BINDING (name) = class_binding;
4757 return class_binding;
4760 /* If we are in a member template, the template parms of the member
4761 template are considered to be inside the scope of the containing
4762 class, but within G++ the class bindings are all pushed between the
4763 template parms and the function body. So if the outer binding is
4764 a template parm for the current scope, return it now rather than
4765 look for a class binding. */
4766 if (outer_scope && outer_scope->kind == sk_template_parms
4767 && binding_to_template_parms_of_scope_p (outer, scope))
4768 return outer;
4770 scope = scope->level_chain;
4773 return outer;
4776 /* Return the innermost block-scope or class-scope value binding for
4777 NAME, or NULL_TREE if there is no such binding. */
4779 tree
4780 innermost_non_namespace_value (tree name)
4782 cxx_binding *binding;
4783 binding = outer_binding (name, /*binding=*/NULL, /*class_p=*/true);
4784 return binding ? binding->value : NULL_TREE;
4787 /* Look up NAME in the current binding level and its superiors in the
4788 namespace of variables, functions and typedefs. Return a ..._DECL
4789 node of some kind representing its definition if there is only one
4790 such declaration, or return a TREE_LIST with all the overloaded
4791 definitions if there are many, or return 0 if it is undefined.
4792 Hidden name, either friend declaration or built-in function, are
4793 not ignored.
4795 If PREFER_TYPE is > 0, we prefer TYPE_DECLs or namespaces.
4796 If PREFER_TYPE is > 1, we reject non-type decls (e.g. namespaces).
4797 Otherwise we prefer non-TYPE_DECLs.
4799 If NONCLASS is nonzero, bindings in class scopes are ignored. If
4800 BLOCK_P is false, bindings in block scopes are ignored. */
4802 static tree
4803 lookup_name_real_1 (tree name, int prefer_type, int nonclass, bool block_p,
4804 int namespaces_only, int flags)
4806 cxx_binding *iter;
4807 tree val = NULL_TREE;
4809 /* Conversion operators are handled specially because ordinary
4810 unqualified name lookup will not find template conversion
4811 operators. */
4812 if (IDENTIFIER_TYPENAME_P (name))
4814 cp_binding_level *level;
4816 for (level = current_binding_level;
4817 level && level->kind != sk_namespace;
4818 level = level->level_chain)
4820 tree class_type;
4821 tree operators;
4823 /* A conversion operator can only be declared in a class
4824 scope. */
4825 if (level->kind != sk_class)
4826 continue;
4828 /* Lookup the conversion operator in the class. */
4829 class_type = level->this_entity;
4830 operators = lookup_fnfields (class_type, name, /*protect=*/0);
4831 if (operators)
4832 return operators;
4835 return NULL_TREE;
4838 flags |= lookup_flags (prefer_type, namespaces_only);
4840 /* First, look in non-namespace scopes. */
4842 if (current_class_type == NULL_TREE)
4843 nonclass = 1;
4845 if (block_p || !nonclass)
4846 for (iter = outer_binding (name, NULL, !nonclass);
4847 iter;
4848 iter = outer_binding (name, iter, !nonclass))
4850 tree binding;
4852 /* Skip entities we don't want. */
4853 if (LOCAL_BINDING_P (iter) ? !block_p : nonclass)
4854 continue;
4856 /* If this is the kind of thing we're looking for, we're done. */
4857 if (qualify_lookup (iter->value, flags))
4858 binding = iter->value;
4859 else if ((flags & LOOKUP_PREFER_TYPES)
4860 && qualify_lookup (iter->type, flags))
4861 binding = iter->type;
4862 else
4863 binding = NULL_TREE;
4865 if (binding)
4867 if (hidden_name_p (binding))
4869 /* A non namespace-scope binding can only be hidden in the
4870 presence of a local class, due to friend declarations.
4872 In particular, consider:
4874 struct C;
4875 void f() {
4876 struct A {
4877 friend struct B;
4878 friend struct C;
4879 void g() {
4880 B* b; // error: B is hidden
4881 C* c; // OK, finds ::C
4884 B *b; // error: B is hidden
4885 C *c; // OK, finds ::C
4886 struct B {};
4887 B *bb; // OK
4890 The standard says that "B" is a local class in "f"
4891 (but not nested within "A") -- but that name lookup
4892 for "B" does not find this declaration until it is
4893 declared directly with "f".
4895 In particular:
4897 [class.friend]
4899 If a friend declaration appears in a local class and
4900 the name specified is an unqualified name, a prior
4901 declaration is looked up without considering scopes
4902 that are outside the innermost enclosing non-class
4903 scope. For a friend function declaration, if there is
4904 no prior declaration, the program is ill-formed. For a
4905 friend class declaration, if there is no prior
4906 declaration, the class that is specified belongs to the
4907 innermost enclosing non-class scope, but if it is
4908 subsequently referenced, its name is not found by name
4909 lookup until a matching declaration is provided in the
4910 innermost enclosing nonclass scope.
4912 So just keep looking for a non-hidden binding.
4914 gcc_assert (TREE_CODE (binding) == TYPE_DECL);
4915 continue;
4917 val = binding;
4918 break;
4922 /* Now lookup in namespace scopes. */
4923 if (!val)
4924 val = unqualified_namespace_lookup (name, flags);
4926 /* If we have a single function from a using decl, pull it out. */
4927 if (val && TREE_CODE (val) == OVERLOAD && !really_overloaded_fn (val))
4928 val = OVL_FUNCTION (val);
4930 return val;
4933 /* Wrapper for lookup_name_real_1. */
4935 tree
4936 lookup_name_real (tree name, int prefer_type, int nonclass, bool block_p,
4937 int namespaces_only, int flags)
4939 tree ret;
4940 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4941 ret = lookup_name_real_1 (name, prefer_type, nonclass, block_p,
4942 namespaces_only, flags);
4943 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4944 return ret;
4947 tree
4948 lookup_name_nonclass (tree name)
4950 return lookup_name_real (name, 0, 1, /*block_p=*/true, 0, 0);
4953 tree
4954 lookup_function_nonclass (tree name, vec<tree, va_gc> *args, bool block_p)
4956 return
4957 lookup_arg_dependent (name,
4958 lookup_name_real (name, 0, 1, block_p, 0, 0),
4959 args);
4962 tree
4963 lookup_name (tree name)
4965 return lookup_name_real (name, 0, 0, /*block_p=*/true, 0, 0);
4968 tree
4969 lookup_name_prefer_type (tree name, int prefer_type)
4971 return lookup_name_real (name, prefer_type, 0, /*block_p=*/true, 0, 0);
4974 /* Look up NAME for type used in elaborated name specifier in
4975 the scopes given by SCOPE. SCOPE can be either TS_CURRENT or
4976 TS_WITHIN_ENCLOSING_NON_CLASS. Although not implied by the
4977 name, more scopes are checked if cleanup or template parameter
4978 scope is encountered.
4980 Unlike lookup_name_real, we make sure that NAME is actually
4981 declared in the desired scope, not from inheritance, nor using
4982 directive. For using declaration, there is DR138 still waiting
4983 to be resolved. Hidden name coming from an earlier friend
4984 declaration is also returned.
4986 A TYPE_DECL best matching the NAME is returned. Catching error
4987 and issuing diagnostics are caller's responsibility. */
4989 static tree
4990 lookup_type_scope_1 (tree name, tag_scope scope)
4992 cxx_binding *iter = NULL;
4993 tree val = NULL_TREE;
4995 /* Look in non-namespace scope first. */
4996 if (current_binding_level->kind != sk_namespace)
4997 iter = outer_binding (name, NULL, /*class_p=*/ true);
4998 for (; iter; iter = outer_binding (name, iter, /*class_p=*/ true))
5000 /* Check if this is the kind of thing we're looking for.
5001 If SCOPE is TS_CURRENT, also make sure it doesn't come from
5002 base class. For ITER->VALUE, we can simply use
5003 INHERITED_VALUE_BINDING_P. For ITER->TYPE, we have to use
5004 our own check.
5006 We check ITER->TYPE before ITER->VALUE in order to handle
5007 typedef struct C {} C;
5008 correctly. */
5010 if (qualify_lookup (iter->type, LOOKUP_PREFER_TYPES)
5011 && (scope != ts_current
5012 || LOCAL_BINDING_P (iter)
5013 || DECL_CONTEXT (iter->type) == iter->scope->this_entity))
5014 val = iter->type;
5015 else if ((scope != ts_current
5016 || !INHERITED_VALUE_BINDING_P (iter))
5017 && qualify_lookup (iter->value, LOOKUP_PREFER_TYPES))
5018 val = iter->value;
5020 if (val)
5021 break;
5024 /* Look in namespace scope. */
5025 if (!val)
5027 iter = cp_binding_level_find_binding_for_name
5028 (NAMESPACE_LEVEL (current_decl_namespace ()), name);
5030 if (iter)
5032 /* If this is the kind of thing we're looking for, we're done. */
5033 if (qualify_lookup (iter->type, LOOKUP_PREFER_TYPES))
5034 val = iter->type;
5035 else if (qualify_lookup (iter->value, LOOKUP_PREFER_TYPES))
5036 val = iter->value;
5041 /* Type found, check if it is in the allowed scopes, ignoring cleanup
5042 and template parameter scopes. */
5043 if (val)
5045 cp_binding_level *b = current_binding_level;
5046 while (b)
5048 if (iter->scope == b)
5049 return val;
5051 if (b->kind == sk_cleanup || b->kind == sk_template_parms
5052 || b->kind == sk_function_parms)
5053 b = b->level_chain;
5054 else if (b->kind == sk_class
5055 && scope == ts_within_enclosing_non_class)
5056 b = b->level_chain;
5057 else
5058 break;
5062 return NULL_TREE;
5065 /* Wrapper for lookup_type_scope_1. */
5067 tree
5068 lookup_type_scope (tree name, tag_scope scope)
5070 tree ret;
5071 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
5072 ret = lookup_type_scope_1 (name, scope);
5073 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
5074 return ret;
5078 /* Similar to `lookup_name' but look only in the innermost non-class
5079 binding level. */
5081 static tree
5082 lookup_name_innermost_nonclass_level_1 (tree name)
5084 cp_binding_level *b;
5085 tree t = NULL_TREE;
5087 b = innermost_nonclass_level ();
5089 if (b->kind == sk_namespace)
5091 t = IDENTIFIER_NAMESPACE_VALUE (name);
5093 /* extern "C" function() */
5094 if (t != NULL_TREE && TREE_CODE (t) == TREE_LIST)
5095 t = TREE_VALUE (t);
5097 else if (IDENTIFIER_BINDING (name)
5098 && LOCAL_BINDING_P (IDENTIFIER_BINDING (name)))
5100 cxx_binding *binding;
5101 binding = IDENTIFIER_BINDING (name);
5102 while (1)
5104 if (binding->scope == b
5105 && !(VAR_P (binding->value)
5106 && DECL_DEAD_FOR_LOCAL (binding->value)))
5107 return binding->value;
5109 if (b->kind == sk_cleanup)
5110 b = b->level_chain;
5111 else
5112 break;
5116 return t;
5119 /* Wrapper for lookup_name_innermost_nonclass_level_1. */
5121 tree
5122 lookup_name_innermost_nonclass_level (tree name)
5124 tree ret;
5125 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
5126 ret = lookup_name_innermost_nonclass_level_1 (name);
5127 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
5128 return ret;
5132 /* Returns true iff DECL is a block-scope extern declaration of a function
5133 or variable. */
5135 bool
5136 is_local_extern (tree decl)
5138 cxx_binding *binding;
5140 /* For functions, this is easy. */
5141 if (TREE_CODE (decl) == FUNCTION_DECL)
5142 return DECL_LOCAL_FUNCTION_P (decl);
5144 if (!VAR_P (decl))
5145 return false;
5146 if (!current_function_decl)
5147 return false;
5149 /* For variables, this is not easy. We need to look at the binding stack
5150 for the identifier to see whether the decl we have is a local. */
5151 for (binding = IDENTIFIER_BINDING (DECL_NAME (decl));
5152 binding && binding->scope->kind != sk_namespace;
5153 binding = binding->previous)
5154 if (binding->value == decl)
5155 return LOCAL_BINDING_P (binding);
5157 return false;
5160 /* Like lookup_name_innermost_nonclass_level, but for types. */
5162 static tree
5163 lookup_type_current_level (tree name)
5165 tree t = NULL_TREE;
5167 timevar_start (TV_NAME_LOOKUP);
5168 gcc_assert (current_binding_level->kind != sk_namespace);
5170 if (REAL_IDENTIFIER_TYPE_VALUE (name) != NULL_TREE
5171 && REAL_IDENTIFIER_TYPE_VALUE (name) != global_type_node)
5173 cp_binding_level *b = current_binding_level;
5174 while (1)
5176 if (purpose_member (name, b->type_shadowed))
5178 t = REAL_IDENTIFIER_TYPE_VALUE (name);
5179 break;
5181 if (b->kind == sk_cleanup)
5182 b = b->level_chain;
5183 else
5184 break;
5188 timevar_stop (TV_NAME_LOOKUP);
5189 return t;
5192 /* [basic.lookup.koenig] */
5193 /* A nonzero return value in the functions below indicates an error. */
5195 struct arg_lookup
5197 tree name;
5198 vec<tree, va_gc> *args;
5199 vec<tree, va_gc> *namespaces;
5200 vec<tree, va_gc> *classes;
5201 tree functions;
5202 hash_set<tree> *fn_set;
5205 static bool arg_assoc (struct arg_lookup*, tree);
5206 static bool arg_assoc_args (struct arg_lookup*, tree);
5207 static bool arg_assoc_args_vec (struct arg_lookup*, vec<tree, va_gc> *);
5208 static bool arg_assoc_type (struct arg_lookup*, tree);
5209 static bool add_function (struct arg_lookup *, tree);
5210 static bool arg_assoc_namespace (struct arg_lookup *, tree);
5211 static bool arg_assoc_class_only (struct arg_lookup *, tree);
5212 static bool arg_assoc_bases (struct arg_lookup *, tree);
5213 static bool arg_assoc_class (struct arg_lookup *, tree);
5214 static bool arg_assoc_template_arg (struct arg_lookup*, tree);
5216 /* Add a function to the lookup structure.
5217 Returns true on error. */
5219 static bool
5220 add_function (struct arg_lookup *k, tree fn)
5222 if (!is_overloaded_fn (fn))
5223 /* All names except those of (possibly overloaded) functions and
5224 function templates are ignored. */;
5225 else if (k->fn_set && k->fn_set->add (fn))
5226 /* It's already in the list. */;
5227 else if (!k->functions)
5228 k->functions = fn;
5229 else if (fn == k->functions)
5231 else
5233 k->functions = build_overload (fn, k->functions);
5234 if (TREE_CODE (k->functions) == OVERLOAD)
5235 OVL_ARG_DEPENDENT (k->functions) = true;
5238 return false;
5241 /* Returns true iff CURRENT has declared itself to be an associated
5242 namespace of SCOPE via a strong using-directive (or transitive chain
5243 thereof). Both are namespaces. */
5245 bool
5246 is_associated_namespace (tree current, tree scope)
5248 vec<tree, va_gc> *seen = make_tree_vector ();
5249 vec<tree, va_gc> *todo = make_tree_vector ();
5250 tree t;
5251 bool ret;
5253 while (1)
5255 if (scope == current)
5257 ret = true;
5258 break;
5260 vec_safe_push (seen, scope);
5261 for (t = DECL_NAMESPACE_ASSOCIATIONS (scope); t; t = TREE_CHAIN (t))
5262 if (!vec_member (TREE_PURPOSE (t), seen))
5263 vec_safe_push (todo, TREE_PURPOSE (t));
5264 if (!todo->is_empty ())
5266 scope = todo->last ();
5267 todo->pop ();
5269 else
5271 ret = false;
5272 break;
5276 release_tree_vector (seen);
5277 release_tree_vector (todo);
5279 return ret;
5282 /* Add functions of a namespace to the lookup structure.
5283 Returns true on error. */
5285 static bool
5286 arg_assoc_namespace (struct arg_lookup *k, tree scope)
5288 tree value;
5290 if (vec_member (scope, k->namespaces))
5291 return false;
5292 vec_safe_push (k->namespaces, scope);
5294 /* Check out our super-users. */
5295 for (value = DECL_NAMESPACE_ASSOCIATIONS (scope); value;
5296 value = TREE_CHAIN (value))
5297 if (arg_assoc_namespace (k, TREE_PURPOSE (value)))
5298 return true;
5300 /* Also look down into inline namespaces. */
5301 for (value = DECL_NAMESPACE_USING (scope); value;
5302 value = TREE_CHAIN (value))
5303 if (is_associated_namespace (scope, TREE_PURPOSE (value)))
5304 if (arg_assoc_namespace (k, TREE_PURPOSE (value)))
5305 return true;
5307 value = namespace_binding (k->name, scope);
5308 if (!value)
5309 return false;
5311 for (; value; value = OVL_NEXT (value))
5313 /* We don't want to find arbitrary hidden functions via argument
5314 dependent lookup. We only want to find friends of associated
5315 classes, which we'll do via arg_assoc_class. */
5316 if (hidden_name_p (OVL_CURRENT (value)))
5317 continue;
5319 if (add_function (k, OVL_CURRENT (value)))
5320 return true;
5323 return false;
5326 /* Adds everything associated with a template argument to the lookup
5327 structure. Returns true on error. */
5329 static bool
5330 arg_assoc_template_arg (struct arg_lookup *k, tree arg)
5332 /* [basic.lookup.koenig]
5334 If T is a template-id, its associated namespaces and classes are
5335 ... the namespaces and classes associated with the types of the
5336 template arguments provided for template type parameters
5337 (excluding template template parameters); the namespaces in which
5338 any template template arguments are defined; and the classes in
5339 which any member templates used as template template arguments
5340 are defined. [Note: non-type template arguments do not
5341 contribute to the set of associated namespaces. ] */
5343 /* Consider first template template arguments. */
5344 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
5345 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
5346 return false;
5347 else if (TREE_CODE (arg) == TEMPLATE_DECL)
5349 tree ctx = CP_DECL_CONTEXT (arg);
5351 /* It's not a member template. */
5352 if (TREE_CODE (ctx) == NAMESPACE_DECL)
5353 return arg_assoc_namespace (k, ctx);
5354 /* Otherwise, it must be member template. */
5355 else
5356 return arg_assoc_class_only (k, ctx);
5358 /* It's an argument pack; handle it recursively. */
5359 else if (ARGUMENT_PACK_P (arg))
5361 tree args = ARGUMENT_PACK_ARGS (arg);
5362 int i, len = TREE_VEC_LENGTH (args);
5363 for (i = 0; i < len; ++i)
5364 if (arg_assoc_template_arg (k, TREE_VEC_ELT (args, i)))
5365 return true;
5367 return false;
5369 /* It's not a template template argument, but it is a type template
5370 argument. */
5371 else if (TYPE_P (arg))
5372 return arg_assoc_type (k, arg);
5373 /* It's a non-type template argument. */
5374 else
5375 return false;
5378 /* Adds the class and its friends to the lookup structure.
5379 Returns true on error. */
5381 static bool
5382 arg_assoc_class_only (struct arg_lookup *k, tree type)
5384 tree list, friends, context;
5386 /* Backend-built structures, such as __builtin_va_list, aren't
5387 affected by all this. */
5388 if (!CLASS_TYPE_P (type))
5389 return false;
5391 context = decl_namespace_context (type);
5392 if (arg_assoc_namespace (k, context))
5393 return true;
5395 complete_type (type);
5397 /* Process friends. */
5398 for (list = DECL_FRIENDLIST (TYPE_MAIN_DECL (type)); list;
5399 list = TREE_CHAIN (list))
5400 if (k->name == FRIEND_NAME (list))
5401 for (friends = FRIEND_DECLS (list); friends;
5402 friends = TREE_CHAIN (friends))
5404 tree fn = TREE_VALUE (friends);
5406 /* Only interested in global functions with potentially hidden
5407 (i.e. unqualified) declarations. */
5408 if (CP_DECL_CONTEXT (fn) != context)
5409 continue;
5410 /* Template specializations are never found by name lookup.
5411 (Templates themselves can be found, but not template
5412 specializations.) */
5413 if (TREE_CODE (fn) == FUNCTION_DECL && DECL_USE_TEMPLATE (fn))
5414 continue;
5415 if (add_function (k, fn))
5416 return true;
5419 return false;
5422 /* Adds the class and its bases to the lookup structure.
5423 Returns true on error. */
5425 static bool
5426 arg_assoc_bases (struct arg_lookup *k, tree type)
5428 if (arg_assoc_class_only (k, type))
5429 return true;
5431 if (TYPE_BINFO (type))
5433 /* Process baseclasses. */
5434 tree binfo, base_binfo;
5435 int i;
5437 for (binfo = TYPE_BINFO (type), i = 0;
5438 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
5439 if (arg_assoc_bases (k, BINFO_TYPE (base_binfo)))
5440 return true;
5443 return false;
5446 /* Adds everything associated with a class argument type to the lookup
5447 structure. Returns true on error.
5449 If T is a class type (including unions), its associated classes are: the
5450 class itself; the class of which it is a member, if any; and its direct
5451 and indirect base classes. Its associated namespaces are the namespaces
5452 of which its associated classes are members. Furthermore, if T is a
5453 class template specialization, its associated namespaces and classes
5454 also include: the namespaces and classes associated with the types of
5455 the template arguments provided for template type parameters (excluding
5456 template template parameters); the namespaces of which any template
5457 template arguments are members; and the classes of which any member
5458 templates used as template template arguments are members. [ Note:
5459 non-type template arguments do not contribute to the set of associated
5460 namespaces. --end note] */
5462 static bool
5463 arg_assoc_class (struct arg_lookup *k, tree type)
5465 tree list;
5466 int i;
5468 /* Backend build structures, such as __builtin_va_list, aren't
5469 affected by all this. */
5470 if (!CLASS_TYPE_P (type))
5471 return false;
5473 if (vec_member (type, k->classes))
5474 return false;
5475 vec_safe_push (k->classes, type);
5477 if (TYPE_CLASS_SCOPE_P (type)
5478 && arg_assoc_class_only (k, TYPE_CONTEXT (type)))
5479 return true;
5481 if (arg_assoc_bases (k, type))
5482 return true;
5484 /* Process template arguments. */
5485 if (CLASSTYPE_TEMPLATE_INFO (type)
5486 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
5488 list = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
5489 for (i = 0; i < TREE_VEC_LENGTH (list); ++i)
5490 if (arg_assoc_template_arg (k, TREE_VEC_ELT (list, i)))
5491 return true;
5494 return false;
5497 /* Adds everything associated with a given type.
5498 Returns 1 on error. */
5500 static bool
5501 arg_assoc_type (struct arg_lookup *k, tree type)
5503 /* As we do not get the type of non-type dependent expressions
5504 right, we can end up with such things without a type. */
5505 if (!type)
5506 return false;
5508 if (TYPE_PTRDATAMEM_P (type))
5510 /* Pointer to member: associate class type and value type. */
5511 if (arg_assoc_type (k, TYPE_PTRMEM_CLASS_TYPE (type)))
5512 return true;
5513 return arg_assoc_type (k, TYPE_PTRMEM_POINTED_TO_TYPE (type));
5515 else switch (TREE_CODE (type))
5517 case ERROR_MARK:
5518 return false;
5519 case VOID_TYPE:
5520 case INTEGER_TYPE:
5521 case REAL_TYPE:
5522 case COMPLEX_TYPE:
5523 case VECTOR_TYPE:
5524 case BOOLEAN_TYPE:
5525 case FIXED_POINT_TYPE:
5526 case DECLTYPE_TYPE:
5527 case NULLPTR_TYPE:
5528 return false;
5529 case RECORD_TYPE:
5530 if (TYPE_PTRMEMFUNC_P (type))
5531 return arg_assoc_type (k, TYPE_PTRMEMFUNC_FN_TYPE (type));
5532 case UNION_TYPE:
5533 return arg_assoc_class (k, type);
5534 case POINTER_TYPE:
5535 case REFERENCE_TYPE:
5536 case ARRAY_TYPE:
5537 return arg_assoc_type (k, TREE_TYPE (type));
5538 case ENUMERAL_TYPE:
5539 if (TYPE_CLASS_SCOPE_P (type)
5540 && arg_assoc_class_only (k, TYPE_CONTEXT (type)))
5541 return true;
5542 return arg_assoc_namespace (k, decl_namespace_context (type));
5543 case METHOD_TYPE:
5544 /* The basetype is referenced in the first arg type, so just
5545 fall through. */
5546 case FUNCTION_TYPE:
5547 /* Associate the parameter types. */
5548 if (arg_assoc_args (k, TYPE_ARG_TYPES (type)))
5549 return true;
5550 /* Associate the return type. */
5551 return arg_assoc_type (k, TREE_TYPE (type));
5552 case TEMPLATE_TYPE_PARM:
5553 case BOUND_TEMPLATE_TEMPLATE_PARM:
5554 return false;
5555 case TYPENAME_TYPE:
5556 return false;
5557 case LANG_TYPE:
5558 gcc_assert (type == unknown_type_node
5559 || type == init_list_type_node);
5560 return false;
5561 case TYPE_PACK_EXPANSION:
5562 return arg_assoc_type (k, PACK_EXPANSION_PATTERN (type));
5564 default:
5565 gcc_unreachable ();
5567 return false;
5570 /* Adds everything associated with arguments. Returns true on error. */
5572 static bool
5573 arg_assoc_args (struct arg_lookup *k, tree args)
5575 for (; args; args = TREE_CHAIN (args))
5576 if (arg_assoc (k, TREE_VALUE (args)))
5577 return true;
5578 return false;
5581 /* Adds everything associated with an argument vector. Returns true
5582 on error. */
5584 static bool
5585 arg_assoc_args_vec (struct arg_lookup *k, vec<tree, va_gc> *args)
5587 unsigned int ix;
5588 tree arg;
5590 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
5591 if (arg_assoc (k, arg))
5592 return true;
5593 return false;
5596 /* Adds everything associated with a given tree_node. Returns 1 on error. */
5598 static bool
5599 arg_assoc (struct arg_lookup *k, tree n)
5601 if (n == error_mark_node)
5602 return false;
5604 if (TYPE_P (n))
5605 return arg_assoc_type (k, n);
5607 if (! type_unknown_p (n))
5608 return arg_assoc_type (k, TREE_TYPE (n));
5610 if (TREE_CODE (n) == ADDR_EXPR)
5611 n = TREE_OPERAND (n, 0);
5612 if (TREE_CODE (n) == COMPONENT_REF)
5613 n = TREE_OPERAND (n, 1);
5614 if (TREE_CODE (n) == OFFSET_REF)
5615 n = TREE_OPERAND (n, 1);
5616 while (TREE_CODE (n) == TREE_LIST)
5617 n = TREE_VALUE (n);
5618 if (BASELINK_P (n))
5619 n = BASELINK_FUNCTIONS (n);
5621 if (TREE_CODE (n) == FUNCTION_DECL)
5622 return arg_assoc_type (k, TREE_TYPE (n));
5623 if (TREE_CODE (n) == TEMPLATE_ID_EXPR)
5625 /* The working paper doesn't currently say how to handle template-id
5626 arguments. The sensible thing would seem to be to handle the list
5627 of template candidates like a normal overload set, and handle the
5628 template arguments like we do for class template
5629 specializations. */
5630 tree templ = TREE_OPERAND (n, 0);
5631 tree args = TREE_OPERAND (n, 1);
5632 int ix;
5634 /* First the templates. */
5635 if (arg_assoc (k, templ))
5636 return true;
5638 /* Now the arguments. */
5639 if (args)
5640 for (ix = TREE_VEC_LENGTH (args); ix--;)
5641 if (arg_assoc_template_arg (k, TREE_VEC_ELT (args, ix)) == 1)
5642 return true;
5644 else if (TREE_CODE (n) == OVERLOAD)
5646 for (; n; n = OVL_NEXT (n))
5647 if (arg_assoc_type (k, TREE_TYPE (OVL_CURRENT (n))))
5648 return true;
5651 return false;
5654 /* Performs Koenig lookup depending on arguments, where fns
5655 are the functions found in normal lookup. */
5657 static tree
5658 lookup_arg_dependent_1 (tree name, tree fns, vec<tree, va_gc> *args)
5660 struct arg_lookup k;
5662 /* Remove any hidden friend functions from the list of functions
5663 found so far. They will be added back by arg_assoc_class as
5664 appropriate. */
5665 fns = remove_hidden_names (fns);
5667 k.name = name;
5668 k.args = args;
5669 k.functions = fns;
5670 k.classes = make_tree_vector ();
5672 /* We previously performed an optimization here by setting
5673 NAMESPACES to the current namespace when it was safe. However, DR
5674 164 says that namespaces that were already searched in the first
5675 stage of template processing are searched again (potentially
5676 picking up later definitions) in the second stage. */
5677 k.namespaces = make_tree_vector ();
5679 /* We used to allow duplicates and let joust discard them, but
5680 since the above change for DR 164 we end up with duplicates of
5681 all the functions found by unqualified lookup. So keep track
5682 of which ones we've seen. */
5683 if (fns)
5685 tree ovl;
5686 /* We shouldn't be here if lookup found something other than
5687 namespace-scope functions. */
5688 gcc_assert (DECL_NAMESPACE_SCOPE_P (OVL_CURRENT (fns)));
5689 k.fn_set = new hash_set<tree>;
5690 for (ovl = fns; ovl; ovl = OVL_NEXT (ovl))
5691 k.fn_set->add (OVL_CURRENT (ovl));
5693 else
5694 k.fn_set = NULL;
5696 arg_assoc_args_vec (&k, args);
5698 fns = k.functions;
5700 if (fns
5701 && !VAR_P (fns)
5702 && !is_overloaded_fn (fns))
5704 error ("argument dependent lookup finds %q+D", fns);
5705 error (" in call to %qD", name);
5706 fns = error_mark_node;
5709 release_tree_vector (k.classes);
5710 release_tree_vector (k.namespaces);
5711 delete k.fn_set;
5713 return fns;
5716 /* Wrapper for lookup_arg_dependent_1. */
5718 tree
5719 lookup_arg_dependent (tree name, tree fns, vec<tree, va_gc> *args)
5721 tree ret;
5722 bool subtime;
5723 subtime = timevar_cond_start (TV_NAME_LOOKUP);
5724 ret = lookup_arg_dependent_1 (name, fns, args);
5725 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
5726 return ret;
5730 /* Add namespace to using_directives. Return NULL_TREE if nothing was
5731 changed (i.e. there was already a directive), or the fresh
5732 TREE_LIST otherwise. */
5734 static tree
5735 push_using_directive_1 (tree used)
5737 tree ud = current_binding_level->using_directives;
5738 tree iter, ancestor;
5740 /* Check if we already have this. */
5741 if (purpose_member (used, ud) != NULL_TREE)
5742 return NULL_TREE;
5744 ancestor = namespace_ancestor (current_decl_namespace (), used);
5745 ud = current_binding_level->using_directives;
5746 ud = tree_cons (used, ancestor, ud);
5747 current_binding_level->using_directives = ud;
5749 /* Recursively add all namespaces used. */
5750 for (iter = DECL_NAMESPACE_USING (used); iter; iter = TREE_CHAIN (iter))
5751 push_using_directive (TREE_PURPOSE (iter));
5753 return ud;
5756 /* Wrapper for push_using_directive_1. */
5758 static tree
5759 push_using_directive (tree used)
5761 tree ret;
5762 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
5763 ret = push_using_directive_1 (used);
5764 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
5765 return ret;
5768 /* The type TYPE is being declared. If it is a class template, or a
5769 specialization of a class template, do any processing required and
5770 perform error-checking. If IS_FRIEND is nonzero, this TYPE is
5771 being declared a friend. B is the binding level at which this TYPE
5772 should be bound.
5774 Returns the TYPE_DECL for TYPE, which may have been altered by this
5775 processing. */
5777 static tree
5778 maybe_process_template_type_declaration (tree type, int is_friend,
5779 cp_binding_level *b)
5781 tree decl = TYPE_NAME (type);
5783 if (processing_template_parmlist)
5784 /* You can't declare a new template type in a template parameter
5785 list. But, you can declare a non-template type:
5787 template <class A*> struct S;
5789 is a forward-declaration of `A'. */
5791 else if (b->kind == sk_namespace
5792 && current_binding_level->kind != sk_namespace)
5793 /* If this new type is being injected into a containing scope,
5794 then it's not a template type. */
5796 else
5798 gcc_assert (MAYBE_CLASS_TYPE_P (type)
5799 || TREE_CODE (type) == ENUMERAL_TYPE);
5801 if (processing_template_decl)
5803 /* This may change after the call to
5804 push_template_decl_real, but we want the original value. */
5805 tree name = DECL_NAME (decl);
5807 decl = push_template_decl_real (decl, is_friend);
5808 if (decl == error_mark_node)
5809 return error_mark_node;
5811 /* If the current binding level is the binding level for the
5812 template parameters (see the comment in
5813 begin_template_parm_list) and the enclosing level is a class
5814 scope, and we're not looking at a friend, push the
5815 declaration of the member class into the class scope. In the
5816 friend case, push_template_decl will already have put the
5817 friend into global scope, if appropriate. */
5818 if (TREE_CODE (type) != ENUMERAL_TYPE
5819 && !is_friend && b->kind == sk_template_parms
5820 && b->level_chain->kind == sk_class)
5822 finish_member_declaration (CLASSTYPE_TI_TEMPLATE (type));
5824 if (!COMPLETE_TYPE_P (current_class_type))
5826 maybe_add_class_template_decl_list (current_class_type,
5827 type, /*friend_p=*/0);
5828 /* Put this UTD in the table of UTDs for the class. */
5829 if (CLASSTYPE_NESTED_UTDS (current_class_type) == NULL)
5830 CLASSTYPE_NESTED_UTDS (current_class_type) =
5831 binding_table_new (SCOPE_DEFAULT_HT_SIZE);
5833 binding_table_insert
5834 (CLASSTYPE_NESTED_UTDS (current_class_type), name, type);
5840 return decl;
5843 /* Push a tag name NAME for struct/class/union/enum type TYPE. In case
5844 that the NAME is a class template, the tag is processed but not pushed.
5846 The pushed scope depend on the SCOPE parameter:
5847 - When SCOPE is TS_CURRENT, put it into the inner-most non-sk_cleanup
5848 scope.
5849 - When SCOPE is TS_GLOBAL, put it in the inner-most non-class and
5850 non-template-parameter scope. This case is needed for forward
5851 declarations.
5852 - When SCOPE is TS_WITHIN_ENCLOSING_NON_CLASS, this is similar to
5853 TS_GLOBAL case except that names within template-parameter scopes
5854 are not pushed at all.
5856 Returns TYPE upon success and ERROR_MARK_NODE otherwise. */
5858 static tree
5859 pushtag_1 (tree name, tree type, tag_scope scope)
5861 cp_binding_level *b;
5862 tree decl;
5864 b = current_binding_level;
5865 while (/* Cleanup scopes are not scopes from the point of view of
5866 the language. */
5867 b->kind == sk_cleanup
5868 /* Neither are function parameter scopes. */
5869 || b->kind == sk_function_parms
5870 /* Neither are the scopes used to hold template parameters
5871 for an explicit specialization. For an ordinary template
5872 declaration, these scopes are not scopes from the point of
5873 view of the language. */
5874 || (b->kind == sk_template_parms
5875 && (b->explicit_spec_p || scope == ts_global))
5876 || (b->kind == sk_class
5877 && (scope != ts_current
5878 /* We may be defining a new type in the initializer
5879 of a static member variable. We allow this when
5880 not pedantic, and it is particularly useful for
5881 type punning via an anonymous union. */
5882 || COMPLETE_TYPE_P (b->this_entity))))
5883 b = b->level_chain;
5885 gcc_assert (identifier_p (name));
5887 /* Do C++ gratuitous typedefing. */
5888 if (identifier_type_value_1 (name) != type)
5890 tree tdef;
5891 int in_class = 0;
5892 tree context = TYPE_CONTEXT (type);
5894 if (! context)
5896 tree cs = current_scope ();
5898 if (scope == ts_current
5899 || (cs && TREE_CODE (cs) == FUNCTION_DECL))
5900 context = cs;
5901 else if (cs != NULL_TREE && TYPE_P (cs))
5902 /* When declaring a friend class of a local class, we want
5903 to inject the newly named class into the scope
5904 containing the local class, not the namespace
5905 scope. */
5906 context = decl_function_context (get_type_decl (cs));
5908 if (!context)
5909 context = current_namespace;
5911 if (b->kind == sk_class
5912 || (b->kind == sk_template_parms
5913 && b->level_chain->kind == sk_class))
5914 in_class = 1;
5916 if (current_lang_name == lang_name_java)
5917 TYPE_FOR_JAVA (type) = 1;
5919 tdef = create_implicit_typedef (name, type);
5920 DECL_CONTEXT (tdef) = FROB_CONTEXT (context);
5921 if (scope == ts_within_enclosing_non_class)
5923 /* This is a friend. Make this TYPE_DECL node hidden from
5924 ordinary name lookup. Its corresponding TEMPLATE_DECL
5925 will be marked in push_template_decl_real. */
5926 retrofit_lang_decl (tdef);
5927 DECL_ANTICIPATED (tdef) = 1;
5928 DECL_FRIEND_P (tdef) = 1;
5931 decl = maybe_process_template_type_declaration
5932 (type, scope == ts_within_enclosing_non_class, b);
5933 if (decl == error_mark_node)
5934 return decl;
5936 if (b->kind == sk_class)
5938 if (!TYPE_BEING_DEFINED (current_class_type))
5939 return error_mark_node;
5941 if (!PROCESSING_REAL_TEMPLATE_DECL_P ())
5942 /* Put this TYPE_DECL on the TYPE_FIELDS list for the
5943 class. But if it's a member template class, we want
5944 the TEMPLATE_DECL, not the TYPE_DECL, so this is done
5945 later. */
5946 finish_member_declaration (decl);
5947 else
5948 pushdecl_class_level (decl);
5950 else if (b->kind != sk_template_parms)
5952 decl = pushdecl_with_scope_1 (decl, b, /*is_friend=*/false);
5953 if (decl == error_mark_node)
5954 return decl;
5957 if (! in_class)
5958 set_identifier_type_value_with_scope (name, tdef, b);
5960 TYPE_CONTEXT (type) = DECL_CONTEXT (decl);
5962 /* If this is a local class, keep track of it. We need this
5963 information for name-mangling, and so that it is possible to
5964 find all function definitions in a translation unit in a
5965 convenient way. (It's otherwise tricky to find a member
5966 function definition it's only pointed to from within a local
5967 class.) */
5968 if (TYPE_FUNCTION_SCOPE_P (type))
5970 if (processing_template_decl)
5972 /* Push a DECL_EXPR so we call pushtag at the right time in
5973 template instantiation rather than in some nested context. */
5974 add_decl_expr (decl);
5976 else
5977 vec_safe_push (local_classes, type);
5980 if (b->kind == sk_class
5981 && !COMPLETE_TYPE_P (current_class_type))
5983 maybe_add_class_template_decl_list (current_class_type,
5984 type, /*friend_p=*/0);
5986 if (CLASSTYPE_NESTED_UTDS (current_class_type) == NULL)
5987 CLASSTYPE_NESTED_UTDS (current_class_type)
5988 = binding_table_new (SCOPE_DEFAULT_HT_SIZE);
5990 binding_table_insert
5991 (CLASSTYPE_NESTED_UTDS (current_class_type), name, type);
5994 decl = TYPE_NAME (type);
5995 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
5997 /* Set type visibility now if this is a forward declaration. */
5998 TREE_PUBLIC (decl) = 1;
5999 determine_visibility (decl);
6001 return type;
6004 /* Wrapper for pushtag_1. */
6006 tree
6007 pushtag (tree name, tree type, tag_scope scope)
6009 tree ret;
6010 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6011 ret = pushtag_1 (name, type, scope);
6012 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6013 return ret;
6016 /* Subroutines for reverting temporarily to top-level for instantiation
6017 of templates and such. We actually need to clear out the class- and
6018 local-value slots of all identifiers, so that only the global values
6019 are at all visible. Simply setting current_binding_level to the global
6020 scope isn't enough, because more binding levels may be pushed. */
6021 struct saved_scope *scope_chain;
6023 /* Return true if ID has not already been marked. */
6025 static inline bool
6026 store_binding_p (tree id)
6028 if (!id || !IDENTIFIER_BINDING (id))
6029 return false;
6031 if (IDENTIFIER_MARKED (id))
6032 return false;
6034 return true;
6037 /* Add an appropriate binding to *OLD_BINDINGS which needs to already
6038 have enough space reserved. */
6040 static void
6041 store_binding (tree id, vec<cxx_saved_binding, va_gc> **old_bindings)
6043 cxx_saved_binding saved;
6045 gcc_checking_assert (store_binding_p (id));
6047 IDENTIFIER_MARKED (id) = 1;
6049 saved.identifier = id;
6050 saved.binding = IDENTIFIER_BINDING (id);
6051 saved.real_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
6052 (*old_bindings)->quick_push (saved);
6053 IDENTIFIER_BINDING (id) = NULL;
6056 static void
6057 store_bindings (tree names, vec<cxx_saved_binding, va_gc> **old_bindings)
6059 static vec<tree> bindings_need_stored = vNULL;
6060 tree t, id;
6061 size_t i;
6063 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6064 for (t = names; t; t = TREE_CHAIN (t))
6066 if (TREE_CODE (t) == TREE_LIST)
6067 id = TREE_PURPOSE (t);
6068 else
6069 id = DECL_NAME (t);
6071 if (store_binding_p (id))
6072 bindings_need_stored.safe_push (id);
6074 if (!bindings_need_stored.is_empty ())
6076 vec_safe_reserve_exact (*old_bindings, bindings_need_stored.length ());
6077 for (i = 0; bindings_need_stored.iterate (i, &id); ++i)
6079 /* We can appearantly have duplicates in NAMES. */
6080 if (store_binding_p (id))
6081 store_binding (id, old_bindings);
6083 bindings_need_stored.truncate (0);
6085 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6088 /* Like store_bindings, but NAMES is a vector of cp_class_binding
6089 objects, rather than a TREE_LIST. */
6091 static void
6092 store_class_bindings (vec<cp_class_binding, va_gc> *names,
6093 vec<cxx_saved_binding, va_gc> **old_bindings)
6095 static vec<tree> bindings_need_stored = vNULL;
6096 size_t i;
6097 cp_class_binding *cb;
6099 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6100 for (i = 0; vec_safe_iterate (names, i, &cb); ++i)
6101 if (store_binding_p (cb->identifier))
6102 bindings_need_stored.safe_push (cb->identifier);
6103 if (!bindings_need_stored.is_empty ())
6105 tree id;
6106 vec_safe_reserve_exact (*old_bindings, bindings_need_stored.length ());
6107 for (i = 0; bindings_need_stored.iterate (i, &id); ++i)
6108 store_binding (id, old_bindings);
6109 bindings_need_stored.truncate (0);
6111 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6114 void
6115 push_to_top_level (void)
6117 struct saved_scope *s;
6118 cp_binding_level *b;
6119 cxx_saved_binding *sb;
6120 size_t i;
6121 bool need_pop;
6123 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6124 s = ggc_cleared_alloc<saved_scope> ();
6126 b = scope_chain ? current_binding_level : 0;
6128 /* If we're in the middle of some function, save our state. */
6129 if (cfun)
6131 need_pop = true;
6132 push_function_context ();
6134 else
6135 need_pop = false;
6137 if (scope_chain && previous_class_level)
6138 store_class_bindings (previous_class_level->class_shadowed,
6139 &s->old_bindings);
6141 /* Have to include the global scope, because class-scope decls
6142 aren't listed anywhere useful. */
6143 for (; b; b = b->level_chain)
6145 tree t;
6147 /* Template IDs are inserted into the global level. If they were
6148 inserted into namespace level, finish_file wouldn't find them
6149 when doing pending instantiations. Therefore, don't stop at
6150 namespace level, but continue until :: . */
6151 if (global_scope_p (b))
6152 break;
6154 store_bindings (b->names, &s->old_bindings);
6155 /* We also need to check class_shadowed to save class-level type
6156 bindings, since pushclass doesn't fill in b->names. */
6157 if (b->kind == sk_class)
6158 store_class_bindings (b->class_shadowed, &s->old_bindings);
6160 /* Unwind type-value slots back to top level. */
6161 for (t = b->type_shadowed; t; t = TREE_CHAIN (t))
6162 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (t), TREE_VALUE (t));
6165 FOR_EACH_VEC_SAFE_ELT (s->old_bindings, i, sb)
6166 IDENTIFIER_MARKED (sb->identifier) = 0;
6168 s->prev = scope_chain;
6169 s->bindings = b;
6170 s->need_pop_function_context = need_pop;
6171 s->function_decl = current_function_decl;
6172 s->unevaluated_operand = cp_unevaluated_operand;
6173 s->inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
6174 s->x_stmt_tree.stmts_are_full_exprs_p = true;
6176 scope_chain = s;
6177 current_function_decl = NULL_TREE;
6178 vec_alloc (current_lang_base, 10);
6179 current_lang_name = lang_name_cplusplus;
6180 current_namespace = global_namespace;
6181 push_class_stack ();
6182 cp_unevaluated_operand = 0;
6183 c_inhibit_evaluation_warnings = 0;
6184 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6187 static void
6188 pop_from_top_level_1 (void)
6190 struct saved_scope *s = scope_chain;
6191 cxx_saved_binding *saved;
6192 size_t i;
6194 /* Clear out class-level bindings cache. */
6195 if (previous_class_level)
6196 invalidate_class_lookup_cache ();
6197 pop_class_stack ();
6199 current_lang_base = 0;
6201 scope_chain = s->prev;
6202 FOR_EACH_VEC_SAFE_ELT (s->old_bindings, i, saved)
6204 tree id = saved->identifier;
6206 IDENTIFIER_BINDING (id) = saved->binding;
6207 SET_IDENTIFIER_TYPE_VALUE (id, saved->real_type_value);
6210 /* If we were in the middle of compiling a function, restore our
6211 state. */
6212 if (s->need_pop_function_context)
6213 pop_function_context ();
6214 current_function_decl = s->function_decl;
6215 cp_unevaluated_operand = s->unevaluated_operand;
6216 c_inhibit_evaluation_warnings = s->inhibit_evaluation_warnings;
6219 /* Wrapper for pop_from_top_level_1. */
6221 void
6222 pop_from_top_level (void)
6224 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6225 pop_from_top_level_1 ();
6226 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6230 /* Pop off extraneous binding levels left over due to syntax errors.
6232 We don't pop past namespaces, as they might be valid. */
6234 void
6235 pop_everything (void)
6237 if (ENABLE_SCOPE_CHECKING)
6238 verbatim ("XXX entering pop_everything ()\n");
6239 while (!toplevel_bindings_p ())
6241 if (current_binding_level->kind == sk_class)
6242 pop_nested_class ();
6243 else
6244 poplevel (0, 0, 0);
6246 if (ENABLE_SCOPE_CHECKING)
6247 verbatim ("XXX leaving pop_everything ()\n");
6250 /* Emit debugging information for using declarations and directives.
6251 If input tree is overloaded fn then emit debug info for all
6252 candidates. */
6254 void
6255 cp_emit_debug_info_for_using (tree t, tree context)
6257 /* Don't try to emit any debug information if we have errors. */
6258 if (seen_error ())
6259 return;
6261 /* Ignore this FUNCTION_DECL if it refers to a builtin declaration
6262 of a builtin function. */
6263 if (TREE_CODE (t) == FUNCTION_DECL
6264 && DECL_EXTERNAL (t)
6265 && DECL_BUILT_IN (t))
6266 return;
6268 /* Do not supply context to imported_module_or_decl, if
6269 it is a global namespace. */
6270 if (context == global_namespace)
6271 context = NULL_TREE;
6273 if (BASELINK_P (t))
6274 t = BASELINK_FUNCTIONS (t);
6276 /* FIXME: Handle TEMPLATE_DECLs. */
6277 for (t = OVL_CURRENT (t); t; t = OVL_NEXT (t))
6278 if (TREE_CODE (t) != TEMPLATE_DECL)
6280 if (building_stmt_list_p ())
6281 add_stmt (build_stmt (input_location, USING_STMT, t));
6282 else
6283 (*debug_hooks->imported_module_or_decl) (t, NULL_TREE, context, false);
6287 #include "gt-cp-name-lookup.h"