2015-07-30 Paolo Carlini <paolo.carlini@oracle.com>
[official-gcc.git] / gcc / cp / name-lookup.c
blob79e28637cae0260741df89c06af4c1149a2ce778
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 (location_of (bval), "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 (DECL_SOURCE_LOCATION (previous), 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 (DECL_SOURCE_LOCATION (previous), 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 (DECL_SOURCE_LOCATION (decl),
996 "previous external decl of %q#D", 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_at (DECL_SOURCE_LOCATION (oldglobal), 0,
1086 "global declaration %q#D", oldglobal);
1089 /* If we have a local external declaration,
1090 and no file-scope declaration has yet been seen,
1091 then if we later have a file-scope decl it must not be static. */
1092 if (oldlocal == NULL_TREE
1093 && oldglobal == NULL_TREE
1094 && DECL_EXTERNAL (x)
1095 && TREE_PUBLIC (x))
1096 TREE_PUBLIC (name) = 1;
1098 /* Don't complain about the parms we push and then pop
1099 while tentatively parsing a function declarator. */
1100 if (TREE_CODE (x) == PARM_DECL && DECL_CONTEXT (x) == NULL_TREE)
1101 /* Ignore. */;
1103 /* Warn if shadowing an argument at the top level of the body. */
1104 else if (oldlocal != NULL_TREE && !DECL_EXTERNAL (x)
1105 /* Inline decls shadow nothing. */
1106 && !DECL_FROM_INLINE (x)
1107 && (TREE_CODE (oldlocal) == PARM_DECL
1108 || VAR_P (oldlocal)
1109 /* If the old decl is a type decl, only warn if the
1110 old decl is an explicit typedef or if both the old
1111 and new decls are type decls. */
1112 || (TREE_CODE (oldlocal) == TYPE_DECL
1113 && (!DECL_ARTIFICIAL (oldlocal)
1114 || TREE_CODE (x) == TYPE_DECL)))
1115 /* Don't check for internally generated vars unless
1116 it's an implicit typedef (see create_implicit_typedef
1117 in decl.c). */
1118 && (!DECL_ARTIFICIAL (x) || DECL_IMPLICIT_TYPEDEF_P (x)))
1120 bool nowarn = false;
1122 /* Don't complain if it's from an enclosing function. */
1123 if (DECL_CONTEXT (oldlocal) == current_function_decl
1124 && TREE_CODE (x) != PARM_DECL
1125 && TREE_CODE (oldlocal) == PARM_DECL)
1127 /* Go to where the parms should be and see if we find
1128 them there. */
1129 cp_binding_level *b = current_binding_level->level_chain;
1131 if (FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
1132 /* Skip the ctor/dtor cleanup level. */
1133 b = b->level_chain;
1135 /* ARM $8.3 */
1136 if (b->kind == sk_function_parms)
1138 error ("declaration of %q#D shadows a parameter", x);
1139 nowarn = true;
1143 /* The local structure or class can't use parameters of
1144 the containing function anyway. */
1145 if (DECL_CONTEXT (oldlocal) != current_function_decl)
1147 cp_binding_level *scope = current_binding_level;
1148 tree context = DECL_CONTEXT (oldlocal);
1149 for (; scope; scope = scope->level_chain)
1151 if (scope->kind == sk_function_parms
1152 && scope->this_entity == context)
1153 break;
1154 if (scope->kind == sk_class
1155 && !LAMBDA_TYPE_P (scope->this_entity))
1157 nowarn = true;
1158 break;
1162 /* Error if redeclaring a local declared in a
1163 for-init-statement or in the condition of an if or
1164 switch statement when the new declaration is in the
1165 outermost block of the controlled statement.
1166 Redeclaring a variable from a for or while condition is
1167 detected elsewhere. */
1168 else if (VAR_P (oldlocal)
1169 && oldscope == current_binding_level->level_chain
1170 && (oldscope->kind == sk_cond
1171 || oldscope->kind == sk_for))
1173 error ("redeclaration of %q#D", x);
1174 inform (DECL_SOURCE_LOCATION (oldlocal),
1175 "%q#D previously declared here", oldlocal);
1176 nowarn = true;
1178 /* C++11:
1179 3.3.3/3: The name declared in an exception-declaration (...)
1180 shall not be redeclared in the outermost block of the handler.
1181 3.3.3/2: A parameter name shall not be redeclared (...) in
1182 the outermost block of any handler associated with a
1183 function-try-block.
1184 3.4.1/15: The function parameter names shall not be redeclared
1185 in the exception-declaration nor in the outermost block of a
1186 handler for the function-try-block. */
1187 else if ((VAR_P (oldlocal)
1188 && oldscope == current_binding_level->level_chain
1189 && oldscope->kind == sk_catch)
1190 || (TREE_CODE (oldlocal) == PARM_DECL
1191 && (current_binding_level->kind == sk_catch
1192 || (current_binding_level->level_chain->kind
1193 == sk_catch))
1194 && in_function_try_handler))
1196 if (permerror (input_location, "redeclaration of %q#D", x))
1197 inform (DECL_SOURCE_LOCATION (oldlocal),
1198 "%q#D previously declared here", oldlocal);
1199 nowarn = true;
1202 if (warn_shadow && !nowarn)
1204 bool warned;
1206 if (TREE_CODE (oldlocal) == PARM_DECL)
1207 warned = warning_at (input_location, OPT_Wshadow,
1208 "declaration of %q#D shadows a parameter", x);
1209 else if (is_capture_proxy (oldlocal))
1210 warned = warning_at (input_location, OPT_Wshadow,
1211 "declaration of %qD shadows a lambda capture",
1213 else
1214 warned = warning_at (input_location, OPT_Wshadow,
1215 "declaration of %qD shadows a previous local",
1218 if (warned)
1219 inform (DECL_SOURCE_LOCATION (oldlocal),
1220 "shadowed declaration is here");
1224 /* Maybe warn if shadowing something else. */
1225 else if (warn_shadow && !DECL_EXTERNAL (x)
1226 /* No shadow warnings for internally generated vars unless
1227 it's an implicit typedef (see create_implicit_typedef
1228 in decl.c). */
1229 && (! DECL_ARTIFICIAL (x) || DECL_IMPLICIT_TYPEDEF_P (x))
1230 /* No shadow warnings for vars made for inlining. */
1231 && ! DECL_FROM_INLINE (x))
1233 tree member;
1235 if (nonlambda_method_basetype ())
1236 member = lookup_member (current_nonlambda_class_type (),
1237 name,
1238 /*protect=*/0,
1239 /*want_type=*/false,
1240 tf_warning_or_error);
1241 else
1242 member = NULL_TREE;
1244 if (member && !TREE_STATIC (member))
1246 if (BASELINK_P (member))
1247 member = BASELINK_FUNCTIONS (member);
1248 member = OVL_CURRENT (member);
1250 /* Do not warn if a variable shadows a function, unless
1251 the variable is a function or a pointer-to-function. */
1252 if (TREE_CODE (member) != FUNCTION_DECL
1253 || TREE_CODE (x) == FUNCTION_DECL
1254 || TYPE_PTRFN_P (TREE_TYPE (x))
1255 || TYPE_PTRMEMFUNC_P (TREE_TYPE (x)))
1257 if (warning_at (input_location, OPT_Wshadow,
1258 "declaration of %qD shadows a member of %qT",
1259 x, current_nonlambda_class_type ())
1260 && DECL_P (member))
1261 inform (DECL_SOURCE_LOCATION (member),
1262 "shadowed declaration is here");
1265 else if (oldglobal != NULL_TREE
1266 && (VAR_P (oldglobal)
1267 /* If the old decl is a type decl, only warn if the
1268 old decl is an explicit typedef or if both the
1269 old and new decls are type decls. */
1270 || (TREE_CODE (oldglobal) == TYPE_DECL
1271 && (!DECL_ARTIFICIAL (oldglobal)
1272 || TREE_CODE (x) == TYPE_DECL)))
1273 && !instantiating_current_function_p ())
1274 /* XXX shadow warnings in outer-more namespaces */
1276 if (warning_at (input_location, OPT_Wshadow,
1277 "declaration of %qD shadows a "
1278 "global declaration", x))
1279 inform (DECL_SOURCE_LOCATION (oldglobal),
1280 "shadowed declaration is here");
1285 if (VAR_P (x))
1286 maybe_register_incomplete_var (x);
1289 if (need_new_binding)
1290 add_decl_to_level (x,
1291 DECL_NAMESPACE_SCOPE_P (x)
1292 ? NAMESPACE_LEVEL (CP_DECL_CONTEXT (x))
1293 : current_binding_level);
1295 return x;
1298 /* Wrapper for pushdecl_maybe_friend_1. */
1300 tree
1301 pushdecl_maybe_friend (tree x, bool is_friend)
1303 tree ret;
1304 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
1305 ret = pushdecl_maybe_friend_1 (x, is_friend);
1306 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
1307 return ret;
1310 /* Record a decl-node X as belonging to the current lexical scope. */
1312 tree
1313 pushdecl (tree x)
1315 return pushdecl_maybe_friend (x, false);
1318 /* Enter DECL into the symbol table, if that's appropriate. Returns
1319 DECL, or a modified version thereof. */
1321 tree
1322 maybe_push_decl (tree decl)
1324 tree type = TREE_TYPE (decl);
1326 /* Add this decl to the current binding level, but not if it comes
1327 from another scope, e.g. a static member variable. TEM may equal
1328 DECL or it may be a previous decl of the same name. */
1329 if (decl == error_mark_node
1330 || (TREE_CODE (decl) != PARM_DECL
1331 && DECL_CONTEXT (decl) != NULL_TREE
1332 /* Definitions of namespace members outside their namespace are
1333 possible. */
1334 && !DECL_NAMESPACE_SCOPE_P (decl))
1335 || (TREE_CODE (decl) == TEMPLATE_DECL && !namespace_bindings_p ())
1336 || type == unknown_type_node
1337 /* The declaration of a template specialization does not affect
1338 the functions available for overload resolution, so we do not
1339 call pushdecl. */
1340 || (TREE_CODE (decl) == FUNCTION_DECL
1341 && DECL_TEMPLATE_SPECIALIZATION (decl)))
1342 return decl;
1343 else
1344 return pushdecl (decl);
1347 /* Bind DECL to ID in the current_binding_level, assumed to be a local
1348 binding level. If PUSH_USING is set in FLAGS, we know that DECL
1349 doesn't really belong to this binding level, that it got here
1350 through a using-declaration. */
1352 void
1353 push_local_binding (tree id, tree decl, int flags)
1355 cp_binding_level *b;
1357 /* Skip over any local classes. This makes sense if we call
1358 push_local_binding with a friend decl of a local class. */
1359 b = innermost_nonclass_level ();
1361 if (lookup_name_innermost_nonclass_level (id))
1363 /* Supplement the existing binding. */
1364 if (!supplement_binding (IDENTIFIER_BINDING (id), decl))
1365 /* It didn't work. Something else must be bound at this
1366 level. Do not add DECL to the list of things to pop
1367 later. */
1368 return;
1370 else
1371 /* Create a new binding. */
1372 push_binding (id, decl, b);
1374 if (TREE_CODE (decl) == OVERLOAD || (flags & PUSH_USING))
1375 /* We must put the OVERLOAD into a TREE_LIST since the
1376 TREE_CHAIN of an OVERLOAD is already used. Similarly for
1377 decls that got here through a using-declaration. */
1378 decl = build_tree_list (NULL_TREE, decl);
1380 /* And put DECL on the list of things declared by the current
1381 binding level. */
1382 add_decl_to_level (decl, b);
1385 /* Check to see whether or not DECL is a variable that would have been
1386 in scope under the ARM, but is not in scope under the ANSI/ISO
1387 standard. If so, issue an error message. If name lookup would
1388 work in both cases, but return a different result, this function
1389 returns the result of ANSI/ISO lookup. Otherwise, it returns
1390 DECL. */
1392 tree
1393 check_for_out_of_scope_variable (tree decl)
1395 tree shadowed;
1397 /* We only care about out of scope variables. */
1398 if (!(VAR_P (decl) && DECL_DEAD_FOR_LOCAL (decl)))
1399 return decl;
1401 shadowed = DECL_HAS_SHADOWED_FOR_VAR_P (decl)
1402 ? DECL_SHADOWED_FOR_VAR (decl) : NULL_TREE ;
1403 while (shadowed != NULL_TREE && VAR_P (shadowed)
1404 && DECL_DEAD_FOR_LOCAL (shadowed))
1405 shadowed = DECL_HAS_SHADOWED_FOR_VAR_P (shadowed)
1406 ? DECL_SHADOWED_FOR_VAR (shadowed) : NULL_TREE;
1407 if (!shadowed)
1408 shadowed = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (decl));
1409 if (shadowed)
1411 if (!DECL_ERROR_REPORTED (decl))
1413 warning (0, "name lookup of %qD changed", DECL_NAME (decl));
1414 warning_at (DECL_SOURCE_LOCATION (shadowed), 0,
1415 " matches this %qD under ISO standard rules",
1416 shadowed);
1417 warning_at (DECL_SOURCE_LOCATION (decl), 0,
1418 " matches this %qD under old rules", decl);
1419 DECL_ERROR_REPORTED (decl) = 1;
1421 return shadowed;
1424 /* If we have already complained about this declaration, there's no
1425 need to do it again. */
1426 if (DECL_ERROR_REPORTED (decl))
1427 return decl;
1429 DECL_ERROR_REPORTED (decl) = 1;
1431 if (TREE_TYPE (decl) == error_mark_node)
1432 return decl;
1434 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
1436 error ("name lookup of %qD changed for ISO %<for%> scoping",
1437 DECL_NAME (decl));
1438 error (" cannot use obsolete binding at %q+D because "
1439 "it has a destructor", decl);
1440 return error_mark_node;
1442 else
1444 permerror (input_location, "name lookup of %qD changed for ISO %<for%> scoping",
1445 DECL_NAME (decl));
1446 if (flag_permissive)
1447 permerror (DECL_SOURCE_LOCATION (decl),
1448 " using obsolete binding at %qD", decl);
1449 else
1451 static bool hint;
1452 if (!hint)
1454 inform (input_location, "(if you use %<-fpermissive%> G++ will accept your code)");
1455 hint = true;
1460 return decl;
1463 /* true means unconditionally make a BLOCK for the next level pushed. */
1465 static bool keep_next_level_flag;
1467 static int binding_depth = 0;
1469 static void
1470 indent (int depth)
1472 int i;
1474 for (i = 0; i < depth * 2; i++)
1475 putc (' ', stderr);
1478 /* Return a string describing the kind of SCOPE we have. */
1479 static const char *
1480 cp_binding_level_descriptor (cp_binding_level *scope)
1482 /* The order of this table must match the "scope_kind"
1483 enumerators. */
1484 static const char* scope_kind_names[] = {
1485 "block-scope",
1486 "cleanup-scope",
1487 "try-scope",
1488 "catch-scope",
1489 "for-scope",
1490 "function-parameter-scope",
1491 "class-scope",
1492 "namespace-scope",
1493 "template-parameter-scope",
1494 "template-explicit-spec-scope"
1496 const scope_kind kind = scope->explicit_spec_p
1497 ? sk_template_spec : scope->kind;
1499 return scope_kind_names[kind];
1502 /* Output a debugging information about SCOPE when performing
1503 ACTION at LINE. */
1504 static void
1505 cp_binding_level_debug (cp_binding_level *scope, int line, const char *action)
1507 const char *desc = cp_binding_level_descriptor (scope);
1508 if (scope->this_entity)
1509 verbatim ("%s %s(%E) %p %d\n", action, desc,
1510 scope->this_entity, (void *) scope, line);
1511 else
1512 verbatim ("%s %s %p %d\n", action, desc, (void *) scope, line);
1515 /* Return the estimated initial size of the hashtable of a NAMESPACE
1516 scope. */
1518 static inline size_t
1519 namespace_scope_ht_size (tree ns)
1521 tree name = DECL_NAME (ns);
1523 return name == std_identifier
1524 ? NAMESPACE_STD_HT_SIZE
1525 : (name == global_scope_name
1526 ? GLOBAL_SCOPE_HT_SIZE
1527 : NAMESPACE_ORDINARY_HT_SIZE);
1530 /* A chain of binding_level structures awaiting reuse. */
1532 static GTY((deletable)) cp_binding_level *free_binding_level;
1534 /* Insert SCOPE as the innermost binding level. */
1536 void
1537 push_binding_level (cp_binding_level *scope)
1539 /* Add it to the front of currently active scopes stack. */
1540 scope->level_chain = current_binding_level;
1541 current_binding_level = scope;
1542 keep_next_level_flag = false;
1544 if (ENABLE_SCOPE_CHECKING)
1546 scope->binding_depth = binding_depth;
1547 indent (binding_depth);
1548 cp_binding_level_debug (scope, LOCATION_LINE (input_location),
1549 "push");
1550 binding_depth++;
1554 /* Create a new KIND scope and make it the top of the active scopes stack.
1555 ENTITY is the scope of the associated C++ entity (namespace, class,
1556 function, C++0x enumeration); it is NULL otherwise. */
1558 cp_binding_level *
1559 begin_scope (scope_kind kind, tree entity)
1561 cp_binding_level *scope;
1563 /* Reuse or create a struct for this binding level. */
1564 if (!ENABLE_SCOPE_CHECKING && free_binding_level)
1566 scope = free_binding_level;
1567 memset (scope, 0, sizeof (cp_binding_level));
1568 free_binding_level = scope->level_chain;
1570 else
1571 scope = ggc_cleared_alloc<cp_binding_level> ();
1573 scope->this_entity = entity;
1574 scope->more_cleanups_ok = true;
1575 switch (kind)
1577 case sk_cleanup:
1578 scope->keep = true;
1579 break;
1581 case sk_template_spec:
1582 scope->explicit_spec_p = true;
1583 kind = sk_template_parms;
1584 /* Fall through. */
1585 case sk_template_parms:
1586 case sk_block:
1587 case sk_try:
1588 case sk_catch:
1589 case sk_for:
1590 case sk_cond:
1591 case sk_class:
1592 case sk_scoped_enum:
1593 case sk_function_parms:
1594 case sk_omp:
1595 scope->keep = keep_next_level_flag;
1596 break;
1598 case sk_namespace:
1599 NAMESPACE_LEVEL (entity) = scope;
1600 vec_alloc (scope->static_decls,
1601 (DECL_NAME (entity) == std_identifier
1602 || DECL_NAME (entity) == global_scope_name) ? 200 : 10);
1603 break;
1605 default:
1606 /* Should not happen. */
1607 gcc_unreachable ();
1608 break;
1610 scope->kind = kind;
1612 push_binding_level (scope);
1614 return scope;
1617 /* We're about to leave current scope. Pop the top of the stack of
1618 currently active scopes. Return the enclosing scope, now active. */
1620 cp_binding_level *
1621 leave_scope (void)
1623 cp_binding_level *scope = current_binding_level;
1625 if (scope->kind == sk_namespace && class_binding_level)
1626 current_binding_level = class_binding_level;
1628 /* We cannot leave a scope, if there are none left. */
1629 if (NAMESPACE_LEVEL (global_namespace))
1630 gcc_assert (!global_scope_p (scope));
1632 if (ENABLE_SCOPE_CHECKING)
1634 indent (--binding_depth);
1635 cp_binding_level_debug (scope, LOCATION_LINE (input_location),
1636 "leave");
1639 /* Move one nesting level up. */
1640 current_binding_level = scope->level_chain;
1642 /* Namespace-scopes are left most probably temporarily, not
1643 completely; they can be reopened later, e.g. in namespace-extension
1644 or any name binding activity that requires us to resume a
1645 namespace. For classes, we cache some binding levels. For other
1646 scopes, we just make the structure available for reuse. */
1647 if (scope->kind != sk_namespace
1648 && scope->kind != sk_class)
1650 scope->level_chain = free_binding_level;
1651 gcc_assert (!ENABLE_SCOPE_CHECKING
1652 || scope->binding_depth == binding_depth);
1653 free_binding_level = scope;
1656 if (scope->kind == sk_class)
1658 /* Reset DEFINING_CLASS_P to allow for reuse of a
1659 class-defining scope in a non-defining context. */
1660 scope->defining_class_p = 0;
1662 /* Find the innermost enclosing class scope, and reset
1663 CLASS_BINDING_LEVEL appropriately. */
1664 class_binding_level = NULL;
1665 for (scope = current_binding_level; scope; scope = scope->level_chain)
1666 if (scope->kind == sk_class)
1668 class_binding_level = scope;
1669 break;
1673 return current_binding_level;
1676 static void
1677 resume_scope (cp_binding_level* b)
1679 /* Resuming binding levels is meant only for namespaces,
1680 and those cannot nest into classes. */
1681 gcc_assert (!class_binding_level);
1682 /* Also, resuming a non-directly nested namespace is a no-no. */
1683 gcc_assert (b->level_chain == current_binding_level);
1684 current_binding_level = b;
1685 if (ENABLE_SCOPE_CHECKING)
1687 b->binding_depth = binding_depth;
1688 indent (binding_depth);
1689 cp_binding_level_debug (b, LOCATION_LINE (input_location), "resume");
1690 binding_depth++;
1694 /* Return the innermost binding level that is not for a class scope. */
1696 static cp_binding_level *
1697 innermost_nonclass_level (void)
1699 cp_binding_level *b;
1701 b = current_binding_level;
1702 while (b->kind == sk_class)
1703 b = b->level_chain;
1705 return b;
1708 /* We're defining an object of type TYPE. If it needs a cleanup, but
1709 we're not allowed to add any more objects with cleanups to the current
1710 scope, create a new binding level. */
1712 void
1713 maybe_push_cleanup_level (tree type)
1715 if (type != error_mark_node
1716 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
1717 && current_binding_level->more_cleanups_ok == 0)
1719 begin_scope (sk_cleanup, NULL);
1720 current_binding_level->statement_list = push_stmt_list ();
1724 /* Return true if we are in the global binding level. */
1726 bool
1727 global_bindings_p (void)
1729 return global_scope_p (current_binding_level);
1732 /* True if we are currently in a toplevel binding level. This
1733 means either the global binding level or a namespace in a toplevel
1734 binding level. Since there are no non-toplevel namespace levels,
1735 this really means any namespace or template parameter level. We
1736 also include a class whose context is toplevel. */
1738 bool
1739 toplevel_bindings_p (void)
1741 cp_binding_level *b = innermost_nonclass_level ();
1743 return b->kind == sk_namespace || b->kind == sk_template_parms;
1746 /* True if this is a namespace scope, or if we are defining a class
1747 which is itself at namespace scope, or whose enclosing class is
1748 such a class, etc. */
1750 bool
1751 namespace_bindings_p (void)
1753 cp_binding_level *b = innermost_nonclass_level ();
1755 return b->kind == sk_namespace;
1758 /* True if the innermost non-class scope is a block scope. */
1760 bool
1761 local_bindings_p (void)
1763 cp_binding_level *b = innermost_nonclass_level ();
1764 return b->kind < sk_function_parms || b->kind == sk_omp;
1767 /* True if the current level needs to have a BLOCK made. */
1769 bool
1770 kept_level_p (void)
1772 return (current_binding_level->blocks != NULL_TREE
1773 || current_binding_level->keep
1774 || current_binding_level->kind == sk_cleanup
1775 || current_binding_level->names != NULL_TREE
1776 || current_binding_level->using_directives);
1779 /* Returns the kind of the innermost scope. */
1781 scope_kind
1782 innermost_scope_kind (void)
1784 return current_binding_level->kind;
1787 /* Returns true if this scope was created to store template parameters. */
1789 bool
1790 template_parm_scope_p (void)
1792 return innermost_scope_kind () == sk_template_parms;
1795 /* If KEEP is true, make a BLOCK node for the next binding level,
1796 unconditionally. Otherwise, use the normal logic to decide whether
1797 or not to create a BLOCK. */
1799 void
1800 keep_next_level (bool keep)
1802 keep_next_level_flag = keep;
1805 /* Return the list of declarations of the current level.
1806 Note that this list is in reverse order unless/until
1807 you nreverse it; and when you do nreverse it, you must
1808 store the result back using `storedecls' or you will lose. */
1810 tree
1811 getdecls (void)
1813 return current_binding_level->names;
1816 /* Return how many function prototypes we are currently nested inside. */
1819 function_parm_depth (void)
1821 int level = 0;
1822 cp_binding_level *b;
1824 for (b = current_binding_level;
1825 b->kind == sk_function_parms;
1826 b = b->level_chain)
1827 ++level;
1829 return level;
1832 /* For debugging. */
1833 static int no_print_functions = 0;
1834 static int no_print_builtins = 0;
1836 static void
1837 print_binding_level (cp_binding_level* lvl)
1839 tree t;
1840 int i = 0, len;
1841 fprintf (stderr, " blocks=%p", (void *) lvl->blocks);
1842 if (lvl->more_cleanups_ok)
1843 fprintf (stderr, " more-cleanups-ok");
1844 if (lvl->have_cleanups)
1845 fprintf (stderr, " have-cleanups");
1846 fprintf (stderr, "\n");
1847 if (lvl->names)
1849 fprintf (stderr, " names:\t");
1850 /* We can probably fit 3 names to a line? */
1851 for (t = lvl->names; t; t = TREE_CHAIN (t))
1853 if (no_print_functions && (TREE_CODE (t) == FUNCTION_DECL))
1854 continue;
1855 if (no_print_builtins
1856 && (TREE_CODE (t) == TYPE_DECL)
1857 && DECL_IS_BUILTIN (t))
1858 continue;
1860 /* Function decls tend to have longer names. */
1861 if (TREE_CODE (t) == FUNCTION_DECL)
1862 len = 3;
1863 else
1864 len = 2;
1865 i += len;
1866 if (i > 6)
1868 fprintf (stderr, "\n\t");
1869 i = len;
1871 print_node_brief (stderr, "", t, 0);
1872 if (t == error_mark_node)
1873 break;
1875 if (i)
1876 fprintf (stderr, "\n");
1878 if (vec_safe_length (lvl->class_shadowed))
1880 size_t i;
1881 cp_class_binding *b;
1882 fprintf (stderr, " class-shadowed:");
1883 FOR_EACH_VEC_ELT (*lvl->class_shadowed, i, b)
1884 fprintf (stderr, " %s ", IDENTIFIER_POINTER (b->identifier));
1885 fprintf (stderr, "\n");
1887 if (lvl->type_shadowed)
1889 fprintf (stderr, " type-shadowed:");
1890 for (t = lvl->type_shadowed; t; t = TREE_CHAIN (t))
1892 fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
1894 fprintf (stderr, "\n");
1898 DEBUG_FUNCTION void
1899 debug (cp_binding_level &ref)
1901 print_binding_level (&ref);
1904 DEBUG_FUNCTION void
1905 debug (cp_binding_level *ptr)
1907 if (ptr)
1908 debug (*ptr);
1909 else
1910 fprintf (stderr, "<nil>\n");
1914 void
1915 print_other_binding_stack (cp_binding_level *stack)
1917 cp_binding_level *level;
1918 for (level = stack; !global_scope_p (level); level = level->level_chain)
1920 fprintf (stderr, "binding level %p\n", (void *) level);
1921 print_binding_level (level);
1925 void
1926 print_binding_stack (void)
1928 cp_binding_level *b;
1929 fprintf (stderr, "current_binding_level=%p\n"
1930 "class_binding_level=%p\n"
1931 "NAMESPACE_LEVEL (global_namespace)=%p\n",
1932 (void *) current_binding_level, (void *) class_binding_level,
1933 (void *) NAMESPACE_LEVEL (global_namespace));
1934 if (class_binding_level)
1936 for (b = class_binding_level; b; b = b->level_chain)
1937 if (b == current_binding_level)
1938 break;
1939 if (b)
1940 b = class_binding_level;
1941 else
1942 b = current_binding_level;
1944 else
1945 b = current_binding_level;
1946 print_other_binding_stack (b);
1947 fprintf (stderr, "global:\n");
1948 print_binding_level (NAMESPACE_LEVEL (global_namespace));
1951 /* Return the type associated with ID. */
1953 static tree
1954 identifier_type_value_1 (tree id)
1956 /* There is no type with that name, anywhere. */
1957 if (REAL_IDENTIFIER_TYPE_VALUE (id) == NULL_TREE)
1958 return NULL_TREE;
1959 /* This is not the type marker, but the real thing. */
1960 if (REAL_IDENTIFIER_TYPE_VALUE (id) != global_type_node)
1961 return REAL_IDENTIFIER_TYPE_VALUE (id);
1962 /* Have to search for it. It must be on the global level, now.
1963 Ask lookup_name not to return non-types. */
1964 id = lookup_name_real (id, 2, 1, /*block_p=*/true, 0, 0);
1965 if (id)
1966 return TREE_TYPE (id);
1967 return NULL_TREE;
1970 /* Wrapper for identifier_type_value_1. */
1972 tree
1973 identifier_type_value (tree id)
1975 tree ret;
1976 timevar_start (TV_NAME_LOOKUP);
1977 ret = identifier_type_value_1 (id);
1978 timevar_stop (TV_NAME_LOOKUP);
1979 return ret;
1983 /* Return the IDENTIFIER_GLOBAL_VALUE of T, for use in common code, since
1984 the definition of IDENTIFIER_GLOBAL_VALUE is different for C and C++. */
1986 tree
1987 identifier_global_value (tree t)
1989 return IDENTIFIER_GLOBAL_VALUE (t);
1992 /* Push a definition of struct, union or enum tag named ID. into
1993 binding_level B. DECL is a TYPE_DECL for the type. We assume that
1994 the tag ID is not already defined. */
1996 static void
1997 set_identifier_type_value_with_scope (tree id, tree decl, cp_binding_level *b)
1999 tree type;
2001 if (b->kind != sk_namespace)
2003 /* Shadow the marker, not the real thing, so that the marker
2004 gets restored later. */
2005 tree old_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
2006 b->type_shadowed
2007 = tree_cons (id, old_type_value, b->type_shadowed);
2008 type = decl ? TREE_TYPE (decl) : NULL_TREE;
2009 TREE_TYPE (b->type_shadowed) = type;
2011 else
2013 cxx_binding *binding =
2014 binding_for_name (NAMESPACE_LEVEL (current_namespace), id);
2015 gcc_assert (decl);
2016 if (binding->value)
2017 supplement_binding (binding, decl);
2018 else
2019 binding->value = decl;
2021 /* Store marker instead of real type. */
2022 type = global_type_node;
2024 SET_IDENTIFIER_TYPE_VALUE (id, type);
2027 /* As set_identifier_type_value_with_scope, but using
2028 current_binding_level. */
2030 void
2031 set_identifier_type_value (tree id, tree decl)
2033 set_identifier_type_value_with_scope (id, decl, current_binding_level);
2036 /* Return the name for the constructor (or destructor) for the
2037 specified class TYPE. When given a template, this routine doesn't
2038 lose the specialization. */
2040 static inline tree
2041 constructor_name_full (tree type)
2043 return TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
2046 /* Return the name for the constructor (or destructor) for the
2047 specified class. When given a template, return the plain
2048 unspecialized name. */
2050 tree
2051 constructor_name (tree type)
2053 tree name;
2054 name = constructor_name_full (type);
2055 if (IDENTIFIER_TEMPLATE (name))
2056 name = IDENTIFIER_TEMPLATE (name);
2057 return name;
2060 /* Returns TRUE if NAME is the name for the constructor for TYPE,
2061 which must be a class type. */
2063 bool
2064 constructor_name_p (tree name, tree type)
2066 tree ctor_name;
2068 gcc_assert (MAYBE_CLASS_TYPE_P (type));
2070 if (!name)
2071 return false;
2073 if (!identifier_p (name))
2074 return false;
2076 /* These don't have names. */
2077 if (TREE_CODE (type) == DECLTYPE_TYPE
2078 || TREE_CODE (type) == TYPEOF_TYPE)
2079 return false;
2081 ctor_name = constructor_name_full (type);
2082 if (name == ctor_name)
2083 return true;
2084 if (IDENTIFIER_TEMPLATE (ctor_name)
2085 && name == IDENTIFIER_TEMPLATE (ctor_name))
2086 return true;
2087 return false;
2090 /* Counter used to create anonymous type names. */
2092 static GTY(()) int anon_cnt;
2094 /* Return an IDENTIFIER which can be used as a name for
2095 anonymous structs and unions. */
2097 tree
2098 make_anon_name (void)
2100 char buf[32];
2102 sprintf (buf, anon_aggrname_format (), anon_cnt++);
2103 return get_identifier (buf);
2106 /* This code is practically identical to that for creating
2107 anonymous names, but is just used for lambdas instead. This isn't really
2108 necessary, but it's convenient to avoid treating lambdas like other
2109 anonymous types. */
2111 static GTY(()) int lambda_cnt = 0;
2113 tree
2114 make_lambda_name (void)
2116 char buf[32];
2118 sprintf (buf, LAMBDANAME_FORMAT, lambda_cnt++);
2119 return get_identifier (buf);
2122 /* Return (from the stack of) the BINDING, if any, established at SCOPE. */
2124 static inline cxx_binding *
2125 find_binding (cp_binding_level *scope, cxx_binding *binding)
2127 for (; binding != NULL; binding = binding->previous)
2128 if (binding->scope == scope)
2129 return binding;
2131 return (cxx_binding *)0;
2134 /* Return the binding for NAME in SCOPE, if any. Otherwise, return NULL. */
2136 static inline cxx_binding *
2137 cp_binding_level_find_binding_for_name (cp_binding_level *scope, tree name)
2139 cxx_binding *b = IDENTIFIER_NAMESPACE_BINDINGS (name);
2140 if (b)
2142 /* Fold-in case where NAME is used only once. */
2143 if (scope == b->scope && b->previous == NULL)
2144 return b;
2145 return find_binding (scope, b);
2147 return NULL;
2150 /* Always returns a binding for name in scope. If no binding is
2151 found, make a new one. */
2153 static cxx_binding *
2154 binding_for_name (cp_binding_level *scope, tree name)
2156 cxx_binding *result;
2158 result = cp_binding_level_find_binding_for_name (scope, name);
2159 if (result)
2160 return result;
2161 /* Not found, make a new one. */
2162 result = cxx_binding_make (NULL, NULL);
2163 result->previous = IDENTIFIER_NAMESPACE_BINDINGS (name);
2164 result->scope = scope;
2165 result->is_local = false;
2166 result->value_is_inherited = false;
2167 IDENTIFIER_NAMESPACE_BINDINGS (name) = result;
2168 return result;
2171 /* Walk through the bindings associated to the name of FUNCTION,
2172 and return the first declaration of a function with a
2173 "C" linkage specification, a.k.a 'extern "C"'.
2174 This function looks for the binding, regardless of which scope it
2175 has been defined in. It basically looks in all the known scopes.
2176 Note that this function does not lookup for bindings of builtin functions
2177 or for functions declared in system headers. */
2178 static tree
2179 lookup_extern_c_fun_in_all_ns (tree function)
2181 tree name;
2182 cxx_binding *iter;
2184 gcc_assert (function && TREE_CODE (function) == FUNCTION_DECL);
2186 name = DECL_NAME (function);
2187 gcc_assert (name && identifier_p (name));
2189 for (iter = IDENTIFIER_NAMESPACE_BINDINGS (name);
2190 iter;
2191 iter = iter->previous)
2193 tree ovl;
2194 for (ovl = iter->value; ovl; ovl = OVL_NEXT (ovl))
2196 tree decl = OVL_CURRENT (ovl);
2197 if (decl
2198 && TREE_CODE (decl) == FUNCTION_DECL
2199 && DECL_EXTERN_C_P (decl)
2200 && !DECL_ARTIFICIAL (decl))
2202 return decl;
2206 return NULL;
2209 /* Returns a list of C-linkage decls with the name NAME. */
2211 tree
2212 c_linkage_bindings (tree name)
2214 tree decls = NULL_TREE;
2215 cxx_binding *iter;
2217 for (iter = IDENTIFIER_NAMESPACE_BINDINGS (name);
2218 iter;
2219 iter = iter->previous)
2221 tree ovl;
2222 for (ovl = iter->value; ovl; ovl = OVL_NEXT (ovl))
2224 tree decl = OVL_CURRENT (ovl);
2225 if (decl
2226 && DECL_EXTERN_C_P (decl)
2227 && !DECL_ARTIFICIAL (decl))
2229 if (decls == NULL_TREE)
2230 decls = decl;
2231 else
2232 decls = tree_cons (NULL_TREE, decl, decls);
2236 return decls;
2239 /* Insert another USING_DECL into the current binding level, returning
2240 this declaration. If this is a redeclaration, do nothing, and
2241 return NULL_TREE if this not in namespace scope (in namespace
2242 scope, a using decl might extend any previous bindings). */
2244 static tree
2245 push_using_decl_1 (tree scope, tree name)
2247 tree decl;
2249 gcc_assert (TREE_CODE (scope) == NAMESPACE_DECL);
2250 gcc_assert (identifier_p (name));
2251 for (decl = current_binding_level->usings; decl; decl = DECL_CHAIN (decl))
2252 if (USING_DECL_SCOPE (decl) == scope && DECL_NAME (decl) == name)
2253 break;
2254 if (decl)
2255 return namespace_bindings_p () ? decl : NULL_TREE;
2256 decl = build_lang_decl (USING_DECL, name, NULL_TREE);
2257 USING_DECL_SCOPE (decl) = scope;
2258 DECL_CHAIN (decl) = current_binding_level->usings;
2259 current_binding_level->usings = decl;
2260 return decl;
2263 /* Wrapper for push_using_decl_1. */
2265 static tree
2266 push_using_decl (tree scope, tree name)
2268 tree ret;
2269 timevar_start (TV_NAME_LOOKUP);
2270 ret = push_using_decl_1 (scope, name);
2271 timevar_stop (TV_NAME_LOOKUP);
2272 return ret;
2275 /* Same as pushdecl, but define X in binding-level LEVEL. We rely on the
2276 caller to set DECL_CONTEXT properly.
2278 Note that this must only be used when X will be the new innermost
2279 binding for its name, as we tack it onto the front of IDENTIFIER_BINDING
2280 without checking to see if the current IDENTIFIER_BINDING comes from a
2281 closer binding level than LEVEL. */
2283 static tree
2284 pushdecl_with_scope_1 (tree x, cp_binding_level *level, bool is_friend)
2286 cp_binding_level *b;
2287 tree function_decl = current_function_decl;
2289 current_function_decl = NULL_TREE;
2290 if (level->kind == sk_class)
2292 b = class_binding_level;
2293 class_binding_level = level;
2294 pushdecl_class_level (x);
2295 class_binding_level = b;
2297 else
2299 b = current_binding_level;
2300 current_binding_level = level;
2301 x = pushdecl_maybe_friend (x, is_friend);
2302 current_binding_level = b;
2304 current_function_decl = function_decl;
2305 return x;
2308 /* Wrapper for pushdecl_with_scope_1. */
2310 tree
2311 pushdecl_with_scope (tree x, cp_binding_level *level, bool is_friend)
2313 tree ret;
2314 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
2315 ret = pushdecl_with_scope_1 (x, level, is_friend);
2316 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
2317 return ret;
2320 /* Helper function for push_overloaded_decl_1 and do_nonmember_using_decl.
2321 Compares the parameter-type-lists of DECL1 and DECL2 and returns false
2322 if they are different. If the DECLs are template functions, the return
2323 types and the template parameter lists are compared too (DR 565). */
2325 static bool
2326 compparms_for_decl_and_using_decl (tree decl1, tree decl2)
2328 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (decl1)),
2329 TYPE_ARG_TYPES (TREE_TYPE (decl2))))
2330 return false;
2332 if (! DECL_FUNCTION_TEMPLATE_P (decl1)
2333 || ! DECL_FUNCTION_TEMPLATE_P (decl2))
2334 return true;
2336 return (comp_template_parms (DECL_TEMPLATE_PARMS (decl1),
2337 DECL_TEMPLATE_PARMS (decl2))
2338 && same_type_p (TREE_TYPE (TREE_TYPE (decl1)),
2339 TREE_TYPE (TREE_TYPE (decl2))));
2342 /* DECL is a FUNCTION_DECL for a non-member function, which may have
2343 other definitions already in place. We get around this by making
2344 the value of the identifier point to a list of all the things that
2345 want to be referenced by that name. It is then up to the users of
2346 that name to decide what to do with that list.
2348 DECL may also be a TEMPLATE_DECL, with a FUNCTION_DECL in its
2349 DECL_TEMPLATE_RESULT. It is dealt with the same way.
2351 FLAGS is a bitwise-or of the following values:
2352 PUSH_LOCAL: Bind DECL in the current scope, rather than at
2353 namespace scope.
2354 PUSH_USING: DECL is being pushed as the result of a using
2355 declaration.
2357 IS_FRIEND is true if this is a friend declaration.
2359 The value returned may be a previous declaration if we guessed wrong
2360 about what language DECL should belong to (C or C++). Otherwise,
2361 it's always DECL (and never something that's not a _DECL). */
2363 static tree
2364 push_overloaded_decl_1 (tree decl, int flags, bool is_friend)
2366 tree name = DECL_NAME (decl);
2367 tree old;
2368 tree new_binding;
2369 int doing_global = (namespace_bindings_p () || !(flags & PUSH_LOCAL));
2371 if (doing_global)
2372 old = namespace_binding (name, DECL_CONTEXT (decl));
2373 else
2374 old = lookup_name_innermost_nonclass_level (name);
2376 if (old)
2378 if (TREE_CODE (old) == TYPE_DECL && DECL_ARTIFICIAL (old))
2380 tree t = TREE_TYPE (old);
2381 if (MAYBE_CLASS_TYPE_P (t) && warn_shadow
2382 && (! DECL_IN_SYSTEM_HEADER (decl)
2383 || ! DECL_IN_SYSTEM_HEADER (old)))
2384 warning (OPT_Wshadow, "%q#D hides constructor for %q#T", decl, t);
2385 old = NULL_TREE;
2387 else if (is_overloaded_fn (old))
2389 tree tmp;
2391 for (tmp = old; tmp; tmp = OVL_NEXT (tmp))
2393 tree fn = OVL_CURRENT (tmp);
2394 tree dup;
2396 if (TREE_CODE (tmp) == OVERLOAD && OVL_USED (tmp)
2397 && !(flags & PUSH_USING)
2398 && compparms_for_decl_and_using_decl (fn, decl)
2399 && ! decls_match (fn, decl))
2400 diagnose_name_conflict (decl, fn);
2402 dup = duplicate_decls (decl, fn, is_friend);
2403 /* If DECL was a redeclaration of FN -- even an invalid
2404 one -- pass that information along to our caller. */
2405 if (dup == fn || dup == error_mark_node)
2406 return dup;
2409 /* We don't overload implicit built-ins. duplicate_decls()
2410 may fail to merge the decls if the new decl is e.g. a
2411 template function. */
2412 if (TREE_CODE (old) == FUNCTION_DECL
2413 && DECL_ANTICIPATED (old)
2414 && !DECL_HIDDEN_FRIEND_P (old))
2415 old = NULL;
2417 else if (old == error_mark_node)
2418 /* Ignore the undefined symbol marker. */
2419 old = NULL_TREE;
2420 else
2422 error ("previous non-function declaration %q+#D", old);
2423 error ("conflicts with function declaration %q#D", decl);
2424 return decl;
2428 if (old || TREE_CODE (decl) == TEMPLATE_DECL
2429 /* If it's a using declaration, we always need to build an OVERLOAD,
2430 because it's the only way to remember that the declaration comes
2431 from 'using', and have the lookup behave correctly. */
2432 || (flags & PUSH_USING))
2434 if (old && TREE_CODE (old) != OVERLOAD)
2435 new_binding = ovl_cons (decl, ovl_cons (old, NULL_TREE));
2436 else
2437 new_binding = ovl_cons (decl, old);
2438 if (flags & PUSH_USING)
2439 OVL_USED (new_binding) = 1;
2441 else
2442 /* NAME is not ambiguous. */
2443 new_binding = decl;
2445 if (doing_global)
2446 set_namespace_binding (name, current_namespace, new_binding);
2447 else
2449 /* We only create an OVERLOAD if there was a previous binding at
2450 this level, or if decl is a template. In the former case, we
2451 need to remove the old binding and replace it with the new
2452 binding. We must also run through the NAMES on the binding
2453 level where the name was bound to update the chain. */
2455 if (TREE_CODE (new_binding) == OVERLOAD && old)
2457 tree *d;
2459 for (d = &IDENTIFIER_BINDING (name)->scope->names;
2461 d = &TREE_CHAIN (*d))
2462 if (*d == old
2463 || (TREE_CODE (*d) == TREE_LIST
2464 && TREE_VALUE (*d) == old))
2466 if (TREE_CODE (*d) == TREE_LIST)
2467 /* Just replace the old binding with the new. */
2468 TREE_VALUE (*d) = new_binding;
2469 else
2470 /* Build a TREE_LIST to wrap the OVERLOAD. */
2471 *d = tree_cons (NULL_TREE, new_binding,
2472 TREE_CHAIN (*d));
2474 /* And update the cxx_binding node. */
2475 IDENTIFIER_BINDING (name)->value = new_binding;
2476 return decl;
2479 /* We should always find a previous binding in this case. */
2480 gcc_unreachable ();
2483 /* Install the new binding. */
2484 push_local_binding (name, new_binding, flags);
2487 return decl;
2490 /* Wrapper for push_overloaded_decl_1. */
2492 static tree
2493 push_overloaded_decl (tree decl, int flags, bool is_friend)
2495 tree ret;
2496 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
2497 ret = push_overloaded_decl_1 (decl, flags, is_friend);
2498 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
2499 return ret;
2502 /* Check a non-member using-declaration. Return the name and scope
2503 being used, and the USING_DECL, or NULL_TREE on failure. */
2505 static tree
2506 validate_nonmember_using_decl (tree decl, tree scope, tree name)
2508 /* [namespace.udecl]
2509 A using-declaration for a class member shall be a
2510 member-declaration. */
2511 if (TYPE_P (scope))
2513 error ("%qT is not a namespace or unscoped enum", scope);
2514 return NULL_TREE;
2516 else if (scope == error_mark_node)
2517 return NULL_TREE;
2519 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR)
2521 /* 7.3.3/5
2522 A using-declaration shall not name a template-id. */
2523 error ("a using-declaration cannot specify a template-id. "
2524 "Try %<using %D%>", name);
2525 return NULL_TREE;
2528 if (TREE_CODE (decl) == NAMESPACE_DECL)
2530 error ("namespace %qD not allowed in using-declaration", decl);
2531 return NULL_TREE;
2534 if (TREE_CODE (decl) == SCOPE_REF)
2536 /* It's a nested name with template parameter dependent scope.
2537 This can only be using-declaration for class member. */
2538 error ("%qT is not a namespace", TREE_OPERAND (decl, 0));
2539 return NULL_TREE;
2542 if (is_overloaded_fn (decl))
2543 decl = get_first_fn (decl);
2545 gcc_assert (DECL_P (decl));
2547 /* Make a USING_DECL. */
2548 tree using_decl = push_using_decl (scope, name);
2550 if (using_decl == NULL_TREE
2551 && at_function_scope_p ()
2552 && VAR_P (decl))
2553 /* C++11 7.3.3/10. */
2554 error ("%qD is already declared in this scope", name);
2556 return using_decl;
2559 /* Process local and global using-declarations. */
2561 static void
2562 do_nonmember_using_decl (tree scope, tree name, tree oldval, tree oldtype,
2563 tree *newval, tree *newtype)
2565 struct scope_binding decls = EMPTY_SCOPE_BINDING;
2567 *newval = *newtype = NULL_TREE;
2568 if (!qualified_lookup_using_namespace (name, scope, &decls, 0))
2569 /* Lookup error */
2570 return;
2572 if (!decls.value && !decls.type)
2574 error ("%qD not declared", name);
2575 return;
2578 /* Shift the old and new bindings around so we're comparing class and
2579 enumeration names to each other. */
2580 if (oldval && DECL_IMPLICIT_TYPEDEF_P (oldval))
2582 oldtype = oldval;
2583 oldval = NULL_TREE;
2586 if (decls.value && DECL_IMPLICIT_TYPEDEF_P (decls.value))
2588 decls.type = decls.value;
2589 decls.value = NULL_TREE;
2592 /* It is impossible to overload a built-in function; any explicit
2593 declaration eliminates the built-in declaration. So, if OLDVAL
2594 is a built-in, then we can just pretend it isn't there. */
2595 if (oldval
2596 && TREE_CODE (oldval) == FUNCTION_DECL
2597 && DECL_ANTICIPATED (oldval)
2598 && !DECL_HIDDEN_FRIEND_P (oldval))
2599 oldval = NULL_TREE;
2601 if (decls.value)
2603 /* Check for using functions. */
2604 if (is_overloaded_fn (decls.value))
2606 tree tmp, tmp1;
2608 if (oldval && !is_overloaded_fn (oldval))
2610 error ("%qD is already declared in this scope", name);
2611 oldval = NULL_TREE;
2614 *newval = oldval;
2615 for (tmp = decls.value; tmp; tmp = OVL_NEXT (tmp))
2617 tree new_fn = OVL_CURRENT (tmp);
2619 /* [namespace.udecl]
2621 If a function declaration in namespace scope or block
2622 scope has the same name and the same parameter types as a
2623 function introduced by a using declaration the program is
2624 ill-formed. */
2625 for (tmp1 = oldval; tmp1; tmp1 = OVL_NEXT (tmp1))
2627 tree old_fn = OVL_CURRENT (tmp1);
2629 if (new_fn == old_fn)
2630 /* The function already exists in the current namespace. */
2631 break;
2632 else if (TREE_CODE (tmp1) == OVERLOAD && OVL_USED (tmp1))
2633 continue; /* this is a using decl */
2634 else if (compparms_for_decl_and_using_decl (new_fn, old_fn))
2636 gcc_assert (!DECL_ANTICIPATED (old_fn)
2637 || DECL_HIDDEN_FRIEND_P (old_fn));
2639 /* There was already a non-using declaration in
2640 this scope with the same parameter types. If both
2641 are the same extern "C" functions, that's ok. */
2642 if (decls_match (new_fn, old_fn))
2643 break;
2644 else
2646 diagnose_name_conflict (new_fn, old_fn);
2647 break;
2652 /* If we broke out of the loop, there's no reason to add
2653 this function to the using declarations for this
2654 scope. */
2655 if (tmp1)
2656 continue;
2658 /* If we are adding to an existing OVERLOAD, then we no
2659 longer know the type of the set of functions. */
2660 if (*newval && TREE_CODE (*newval) == OVERLOAD)
2661 TREE_TYPE (*newval) = unknown_type_node;
2662 /* Add this new function to the set. */
2663 *newval = build_overload (OVL_CURRENT (tmp), *newval);
2664 /* If there is only one function, then we use its type. (A
2665 using-declaration naming a single function can be used in
2666 contexts where overload resolution cannot be
2667 performed.) */
2668 if (TREE_CODE (*newval) != OVERLOAD)
2670 *newval = ovl_cons (*newval, NULL_TREE);
2671 TREE_TYPE (*newval) = TREE_TYPE (OVL_CURRENT (tmp));
2673 OVL_USED (*newval) = 1;
2676 else
2678 *newval = decls.value;
2679 if (oldval && !decls_match (*newval, oldval))
2680 error ("%qD is already declared in this scope", name);
2683 else
2684 *newval = oldval;
2686 if (decls.type && TREE_CODE (decls.type) == TREE_LIST)
2688 error ("reference to %qD is ambiguous", name);
2689 print_candidates (decls.type);
2691 else
2693 *newtype = decls.type;
2694 if (oldtype && *newtype && !decls_match (oldtype, *newtype))
2695 error ("%qD is already declared in this scope", name);
2698 /* If *newval is empty, shift any class or enumeration name down. */
2699 if (!*newval)
2701 *newval = *newtype;
2702 *newtype = NULL_TREE;
2706 /* Process a using-declaration at function scope. */
2708 void
2709 do_local_using_decl (tree decl, tree scope, tree name)
2711 tree oldval, oldtype, newval, newtype;
2712 tree orig_decl = decl;
2714 decl = validate_nonmember_using_decl (decl, scope, name);
2715 if (decl == NULL_TREE)
2716 return;
2718 if (building_stmt_list_p ()
2719 && at_function_scope_p ())
2720 add_decl_expr (decl);
2722 oldval = lookup_name_innermost_nonclass_level (name);
2723 oldtype = lookup_type_current_level (name);
2725 do_nonmember_using_decl (scope, name, oldval, oldtype, &newval, &newtype);
2727 if (newval)
2729 if (is_overloaded_fn (newval))
2731 tree fn, term;
2733 /* We only need to push declarations for those functions
2734 that were not already bound in the current level.
2735 The old value might be NULL_TREE, it might be a single
2736 function, or an OVERLOAD. */
2737 if (oldval && TREE_CODE (oldval) == OVERLOAD)
2738 term = OVL_FUNCTION (oldval);
2739 else
2740 term = oldval;
2741 for (fn = newval; fn && OVL_CURRENT (fn) != term;
2742 fn = OVL_NEXT (fn))
2743 push_overloaded_decl (OVL_CURRENT (fn),
2744 PUSH_LOCAL | PUSH_USING,
2745 false);
2747 else
2748 push_local_binding (name, newval, PUSH_USING);
2750 if (newtype)
2752 push_local_binding (name, newtype, PUSH_USING);
2753 set_identifier_type_value (name, newtype);
2756 /* Emit debug info. */
2757 if (!processing_template_decl)
2758 cp_emit_debug_info_for_using (orig_decl, current_scope());
2761 /* Returns true if ROOT (a namespace, class, or function) encloses
2762 CHILD. CHILD may be either a class type or a namespace. */
2764 bool
2765 is_ancestor (tree root, tree child)
2767 gcc_assert ((TREE_CODE (root) == NAMESPACE_DECL
2768 || TREE_CODE (root) == FUNCTION_DECL
2769 || CLASS_TYPE_P (root)));
2770 gcc_assert ((TREE_CODE (child) == NAMESPACE_DECL
2771 || CLASS_TYPE_P (child)));
2773 /* The global namespace encloses everything. */
2774 if (root == global_namespace)
2775 return true;
2777 while (true)
2779 /* If we've run out of scopes, stop. */
2780 if (!child)
2781 return false;
2782 /* If we've reached the ROOT, it encloses CHILD. */
2783 if (root == child)
2784 return true;
2785 /* Go out one level. */
2786 if (TYPE_P (child))
2787 child = TYPE_NAME (child);
2788 child = DECL_CONTEXT (child);
2792 /* Enter the class or namespace scope indicated by T suitable for name
2793 lookup. T can be arbitrary scope, not necessary nested inside the
2794 current scope. Returns a non-null scope to pop iff pop_scope
2795 should be called later to exit this scope. */
2797 tree
2798 push_scope (tree t)
2800 if (TREE_CODE (t) == NAMESPACE_DECL)
2801 push_decl_namespace (t);
2802 else if (CLASS_TYPE_P (t))
2804 if (!at_class_scope_p ()
2805 || !same_type_p (current_class_type, t))
2806 push_nested_class (t);
2807 else
2808 /* T is the same as the current scope. There is therefore no
2809 need to re-enter the scope. Since we are not actually
2810 pushing a new scope, our caller should not call
2811 pop_scope. */
2812 t = NULL_TREE;
2815 return t;
2818 /* Leave scope pushed by push_scope. */
2820 void
2821 pop_scope (tree t)
2823 if (t == NULL_TREE)
2824 return;
2825 if (TREE_CODE (t) == NAMESPACE_DECL)
2826 pop_decl_namespace ();
2827 else if CLASS_TYPE_P (t)
2828 pop_nested_class ();
2831 /* Subroutine of push_inner_scope. */
2833 static void
2834 push_inner_scope_r (tree outer, tree inner)
2836 tree prev;
2838 if (outer == inner
2839 || (TREE_CODE (inner) != NAMESPACE_DECL && !CLASS_TYPE_P (inner)))
2840 return;
2842 prev = CP_DECL_CONTEXT (TREE_CODE (inner) == NAMESPACE_DECL ? inner : TYPE_NAME (inner));
2843 if (outer != prev)
2844 push_inner_scope_r (outer, prev);
2845 if (TREE_CODE (inner) == NAMESPACE_DECL)
2847 cp_binding_level *save_template_parm = 0;
2848 /* Temporary take out template parameter scopes. They are saved
2849 in reversed order in save_template_parm. */
2850 while (current_binding_level->kind == sk_template_parms)
2852 cp_binding_level *b = current_binding_level;
2853 current_binding_level = b->level_chain;
2854 b->level_chain = save_template_parm;
2855 save_template_parm = b;
2858 resume_scope (NAMESPACE_LEVEL (inner));
2859 current_namespace = inner;
2861 /* Restore template parameter scopes. */
2862 while (save_template_parm)
2864 cp_binding_level *b = save_template_parm;
2865 save_template_parm = b->level_chain;
2866 b->level_chain = current_binding_level;
2867 current_binding_level = b;
2870 else
2871 pushclass (inner);
2874 /* Enter the scope INNER from current scope. INNER must be a scope
2875 nested inside current scope. This works with both name lookup and
2876 pushing name into scope. In case a template parameter scope is present,
2877 namespace is pushed under the template parameter scope according to
2878 name lookup rule in 14.6.1/6.
2880 Return the former current scope suitable for pop_inner_scope. */
2882 tree
2883 push_inner_scope (tree inner)
2885 tree outer = current_scope ();
2886 if (!outer)
2887 outer = current_namespace;
2889 push_inner_scope_r (outer, inner);
2890 return outer;
2893 /* Exit the current scope INNER back to scope OUTER. */
2895 void
2896 pop_inner_scope (tree outer, tree inner)
2898 if (outer == inner
2899 || (TREE_CODE (inner) != NAMESPACE_DECL && !CLASS_TYPE_P (inner)))
2900 return;
2902 while (outer != inner)
2904 if (TREE_CODE (inner) == NAMESPACE_DECL)
2906 cp_binding_level *save_template_parm = 0;
2907 /* Temporary take out template parameter scopes. They are saved
2908 in reversed order in save_template_parm. */
2909 while (current_binding_level->kind == sk_template_parms)
2911 cp_binding_level *b = current_binding_level;
2912 current_binding_level = b->level_chain;
2913 b->level_chain = save_template_parm;
2914 save_template_parm = b;
2917 pop_namespace ();
2919 /* Restore template parameter scopes. */
2920 while (save_template_parm)
2922 cp_binding_level *b = save_template_parm;
2923 save_template_parm = b->level_chain;
2924 b->level_chain = current_binding_level;
2925 current_binding_level = b;
2928 else
2929 popclass ();
2931 inner = CP_DECL_CONTEXT (TREE_CODE (inner) == NAMESPACE_DECL ? inner : TYPE_NAME (inner));
2935 /* Do a pushlevel for class declarations. */
2937 void
2938 pushlevel_class (void)
2940 class_binding_level = begin_scope (sk_class, current_class_type);
2943 /* ...and a poplevel for class declarations. */
2945 void
2946 poplevel_class (void)
2948 cp_binding_level *level = class_binding_level;
2949 cp_class_binding *cb;
2950 size_t i;
2951 tree shadowed;
2953 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
2954 gcc_assert (level != 0);
2956 /* If we're leaving a toplevel class, cache its binding level. */
2957 if (current_class_depth == 1)
2958 previous_class_level = level;
2959 for (shadowed = level->type_shadowed;
2960 shadowed;
2961 shadowed = TREE_CHAIN (shadowed))
2962 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (shadowed), TREE_VALUE (shadowed));
2964 /* Remove the bindings for all of the class-level declarations. */
2965 if (level->class_shadowed)
2967 FOR_EACH_VEC_ELT (*level->class_shadowed, i, cb)
2969 IDENTIFIER_BINDING (cb->identifier) = cb->base->previous;
2970 cxx_binding_free (cb->base);
2972 ggc_free (level->class_shadowed);
2973 level->class_shadowed = NULL;
2976 /* Now, pop out of the binding level which we created up in the
2977 `pushlevel_class' routine. */
2978 gcc_assert (current_binding_level == level);
2979 leave_scope ();
2980 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
2983 /* Set INHERITED_VALUE_BINDING_P on BINDING to true or false, as
2984 appropriate. DECL is the value to which a name has just been
2985 bound. CLASS_TYPE is the class in which the lookup occurred. */
2987 static void
2988 set_inherited_value_binding_p (cxx_binding *binding, tree decl,
2989 tree class_type)
2991 if (binding->value == decl && TREE_CODE (decl) != TREE_LIST)
2993 tree context;
2995 if (TREE_CODE (decl) == OVERLOAD)
2996 context = ovl_scope (decl);
2997 else
2999 gcc_assert (DECL_P (decl));
3000 context = context_for_name_lookup (decl);
3003 if (is_properly_derived_from (class_type, context))
3004 INHERITED_VALUE_BINDING_P (binding) = 1;
3005 else
3006 INHERITED_VALUE_BINDING_P (binding) = 0;
3008 else if (binding->value == decl)
3009 /* We only encounter a TREE_LIST when there is an ambiguity in the
3010 base classes. Such an ambiguity can be overridden by a
3011 definition in this class. */
3012 INHERITED_VALUE_BINDING_P (binding) = 1;
3013 else
3014 INHERITED_VALUE_BINDING_P (binding) = 0;
3017 /* Make the declaration of X appear in CLASS scope. */
3019 bool
3020 pushdecl_class_level (tree x)
3022 tree name;
3023 bool is_valid = true;
3024 bool subtime;
3026 /* Do nothing if we're adding to an outer lambda closure type,
3027 outer_binding will add it later if it's needed. */
3028 if (current_class_type != class_binding_level->this_entity)
3029 return true;
3031 subtime = timevar_cond_start (TV_NAME_LOOKUP);
3032 /* Get the name of X. */
3033 if (TREE_CODE (x) == OVERLOAD)
3034 name = DECL_NAME (get_first_fn (x));
3035 else
3036 name = DECL_NAME (x);
3038 if (name)
3040 is_valid = push_class_level_binding (name, x);
3041 if (TREE_CODE (x) == TYPE_DECL)
3042 set_identifier_type_value (name, x);
3044 else if (ANON_AGGR_TYPE_P (TREE_TYPE (x)))
3046 /* If X is an anonymous aggregate, all of its members are
3047 treated as if they were members of the class containing the
3048 aggregate, for naming purposes. */
3049 tree f;
3051 for (f = TYPE_FIELDS (TREE_TYPE (x)); f; f = DECL_CHAIN (f))
3053 location_t save_location = input_location;
3054 input_location = DECL_SOURCE_LOCATION (f);
3055 if (!pushdecl_class_level (f))
3056 is_valid = false;
3057 input_location = save_location;
3060 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3061 return is_valid;
3064 /* Return the BINDING (if any) for NAME in SCOPE, which is a class
3065 scope. If the value returned is non-NULL, and the PREVIOUS field
3066 is not set, callers must set the PREVIOUS field explicitly. */
3068 static cxx_binding *
3069 get_class_binding (tree name, cp_binding_level *scope)
3071 tree class_type;
3072 tree type_binding;
3073 tree value_binding;
3074 cxx_binding *binding;
3076 class_type = scope->this_entity;
3078 /* Get the type binding. */
3079 type_binding = lookup_member (class_type, name,
3080 /*protect=*/2, /*want_type=*/true,
3081 tf_warning_or_error);
3082 /* Get the value binding. */
3083 value_binding = lookup_member (class_type, name,
3084 /*protect=*/2, /*want_type=*/false,
3085 tf_warning_or_error);
3087 if (value_binding
3088 && (TREE_CODE (value_binding) == TYPE_DECL
3089 || DECL_CLASS_TEMPLATE_P (value_binding)
3090 || (TREE_CODE (value_binding) == TREE_LIST
3091 && TREE_TYPE (value_binding) == error_mark_node
3092 && (TREE_CODE (TREE_VALUE (value_binding))
3093 == TYPE_DECL))))
3094 /* We found a type binding, even when looking for a non-type
3095 binding. This means that we already processed this binding
3096 above. */
3098 else if (value_binding)
3100 if (TREE_CODE (value_binding) == TREE_LIST
3101 && TREE_TYPE (value_binding) == error_mark_node)
3102 /* NAME is ambiguous. */
3104 else if (BASELINK_P (value_binding))
3105 /* NAME is some overloaded functions. */
3106 value_binding = BASELINK_FUNCTIONS (value_binding);
3109 /* If we found either a type binding or a value binding, create a
3110 new binding object. */
3111 if (type_binding || value_binding)
3113 binding = new_class_binding (name,
3114 value_binding,
3115 type_binding,
3116 scope);
3117 /* This is a class-scope binding, not a block-scope binding. */
3118 LOCAL_BINDING_P (binding) = 0;
3119 set_inherited_value_binding_p (binding, value_binding, class_type);
3121 else
3122 binding = NULL;
3124 return binding;
3127 /* Make the declaration(s) of X appear in CLASS scope under the name
3128 NAME. Returns true if the binding is valid. */
3130 static bool
3131 push_class_level_binding_1 (tree name, tree x)
3133 cxx_binding *binding;
3134 tree decl = x;
3135 bool ok;
3137 /* The class_binding_level will be NULL if x is a template
3138 parameter name in a member template. */
3139 if (!class_binding_level)
3140 return true;
3142 if (name == error_mark_node)
3143 return false;
3145 /* Can happen for an erroneous declaration (c++/60384). */
3146 if (!identifier_p (name))
3148 gcc_assert (errorcount || sorrycount);
3149 return false;
3152 /* Check for invalid member names. But don't worry about a default
3153 argument-scope lambda being pushed after the class is complete. */
3154 gcc_assert (TYPE_BEING_DEFINED (current_class_type)
3155 || LAMBDA_TYPE_P (TREE_TYPE (decl)));
3156 /* Check that we're pushing into the right binding level. */
3157 gcc_assert (current_class_type == class_binding_level->this_entity);
3159 /* We could have been passed a tree list if this is an ambiguous
3160 declaration. If so, pull the declaration out because
3161 check_template_shadow will not handle a TREE_LIST. */
3162 if (TREE_CODE (decl) == TREE_LIST
3163 && TREE_TYPE (decl) == error_mark_node)
3164 decl = TREE_VALUE (decl);
3166 if (!check_template_shadow (decl))
3167 return false;
3169 /* [class.mem]
3171 If T is the name of a class, then each of the following shall
3172 have a name different from T:
3174 -- every static data member of class T;
3176 -- every member of class T that is itself a type;
3178 -- every enumerator of every member of class T that is an
3179 enumerated type;
3181 -- every member of every anonymous union that is a member of
3182 class T.
3184 (Non-static data members were also forbidden to have the same
3185 name as T until TC1.) */
3186 if ((VAR_P (x)
3187 || TREE_CODE (x) == CONST_DECL
3188 || (TREE_CODE (x) == TYPE_DECL
3189 && !DECL_SELF_REFERENCE_P (x))
3190 /* A data member of an anonymous union. */
3191 || (TREE_CODE (x) == FIELD_DECL
3192 && DECL_CONTEXT (x) != current_class_type))
3193 && DECL_NAME (x) == constructor_name (current_class_type))
3195 tree scope = context_for_name_lookup (x);
3196 if (TYPE_P (scope) && same_type_p (scope, current_class_type))
3198 error ("%qD has the same name as the class in which it is "
3199 "declared",
3201 return false;
3205 /* Get the current binding for NAME in this class, if any. */
3206 binding = IDENTIFIER_BINDING (name);
3207 if (!binding || binding->scope != class_binding_level)
3209 binding = get_class_binding (name, class_binding_level);
3210 /* If a new binding was created, put it at the front of the
3211 IDENTIFIER_BINDING list. */
3212 if (binding)
3214 binding->previous = IDENTIFIER_BINDING (name);
3215 IDENTIFIER_BINDING (name) = binding;
3219 /* If there is already a binding, then we may need to update the
3220 current value. */
3221 if (binding && binding->value)
3223 tree bval = binding->value;
3224 tree old_decl = NULL_TREE;
3225 tree target_decl = strip_using_decl (decl);
3226 tree target_bval = strip_using_decl (bval);
3228 if (INHERITED_VALUE_BINDING_P (binding))
3230 /* If the old binding was from a base class, and was for a
3231 tag name, slide it over to make room for the new binding.
3232 The old binding is still visible if explicitly qualified
3233 with a class-key. */
3234 if (TREE_CODE (target_bval) == TYPE_DECL
3235 && DECL_ARTIFICIAL (target_bval)
3236 && !(TREE_CODE (target_decl) == TYPE_DECL
3237 && DECL_ARTIFICIAL (target_decl)))
3239 old_decl = binding->type;
3240 binding->type = bval;
3241 binding->value = NULL_TREE;
3242 INHERITED_VALUE_BINDING_P (binding) = 0;
3244 else
3246 old_decl = bval;
3247 /* Any inherited type declaration is hidden by the type
3248 declaration in the derived class. */
3249 if (TREE_CODE (target_decl) == TYPE_DECL
3250 && DECL_ARTIFICIAL (target_decl))
3251 binding->type = NULL_TREE;
3254 else if (TREE_CODE (target_decl) == OVERLOAD
3255 && is_overloaded_fn (target_bval))
3256 old_decl = bval;
3257 else if (TREE_CODE (decl) == USING_DECL
3258 && TREE_CODE (bval) == USING_DECL
3259 && same_type_p (USING_DECL_SCOPE (decl),
3260 USING_DECL_SCOPE (bval)))
3261 /* This is a using redeclaration that will be diagnosed later
3262 in supplement_binding */
3264 else if (TREE_CODE (decl) == USING_DECL
3265 && TREE_CODE (bval) == USING_DECL
3266 && DECL_DEPENDENT_P (decl)
3267 && DECL_DEPENDENT_P (bval))
3268 return true;
3269 else if (TREE_CODE (decl) == USING_DECL
3270 && is_overloaded_fn (target_bval))
3271 old_decl = bval;
3272 else if (TREE_CODE (bval) == USING_DECL
3273 && is_overloaded_fn (target_decl))
3274 return true;
3276 if (old_decl && binding->scope == class_binding_level)
3278 binding->value = x;
3279 /* It is always safe to clear INHERITED_VALUE_BINDING_P
3280 here. This function is only used to register bindings
3281 from with the class definition itself. */
3282 INHERITED_VALUE_BINDING_P (binding) = 0;
3283 return true;
3287 /* Note that we declared this value so that we can issue an error if
3288 this is an invalid redeclaration of a name already used for some
3289 other purpose. */
3290 note_name_declared_in_class (name, decl);
3292 /* If we didn't replace an existing binding, put the binding on the
3293 stack of bindings for the identifier, and update the shadowed
3294 list. */
3295 if (binding && binding->scope == class_binding_level)
3296 /* Supplement the existing binding. */
3297 ok = supplement_binding (binding, decl);
3298 else
3300 /* Create a new binding. */
3301 push_binding (name, decl, class_binding_level);
3302 ok = true;
3305 return ok;
3308 /* Wrapper for push_class_level_binding_1. */
3310 bool
3311 push_class_level_binding (tree name, tree x)
3313 bool ret;
3314 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3315 ret = push_class_level_binding_1 (name, x);
3316 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3317 return ret;
3320 /* Process "using SCOPE::NAME" in a class scope. Return the
3321 USING_DECL created. */
3323 tree
3324 do_class_using_decl (tree scope, tree name)
3326 /* The USING_DECL returned by this function. */
3327 tree value;
3328 /* The declaration (or declarations) name by this using
3329 declaration. NULL if we are in a template and cannot figure out
3330 what has been named. */
3331 tree decl;
3332 /* True if SCOPE is a dependent type. */
3333 bool scope_dependent_p;
3334 /* True if SCOPE::NAME is dependent. */
3335 bool name_dependent_p;
3336 /* True if any of the bases of CURRENT_CLASS_TYPE are dependent. */
3337 bool bases_dependent_p;
3338 tree binfo;
3339 tree base_binfo;
3340 int i;
3342 if (name == error_mark_node)
3343 return NULL_TREE;
3345 if (!scope || !TYPE_P (scope))
3347 error ("using-declaration for non-member at class scope");
3348 return NULL_TREE;
3351 /* Make sure the name is not invalid */
3352 if (TREE_CODE (name) == BIT_NOT_EXPR)
3354 error ("%<%T::%D%> names destructor", scope, name);
3355 return NULL_TREE;
3357 /* Using T::T declares inheriting ctors, even if T is a typedef. */
3358 if (MAYBE_CLASS_TYPE_P (scope)
3359 && (name == TYPE_IDENTIFIER (scope)
3360 || constructor_name_p (name, scope)))
3362 maybe_warn_cpp0x (CPP0X_INHERITING_CTORS);
3363 name = ctor_identifier;
3365 if (constructor_name_p (name, current_class_type))
3367 error ("%<%T::%D%> names constructor in %qT",
3368 scope, name, current_class_type);
3369 return NULL_TREE;
3372 scope_dependent_p = dependent_scope_p (scope);
3373 name_dependent_p = (scope_dependent_p
3374 || (IDENTIFIER_TYPENAME_P (name)
3375 && dependent_type_p (TREE_TYPE (name))));
3377 bases_dependent_p = false;
3378 if (processing_template_decl)
3379 for (binfo = TYPE_BINFO (current_class_type), i = 0;
3380 BINFO_BASE_ITERATE (binfo, i, base_binfo);
3381 i++)
3382 if (dependent_type_p (TREE_TYPE (base_binfo)))
3384 bases_dependent_p = true;
3385 break;
3388 decl = NULL_TREE;
3390 /* From [namespace.udecl]:
3392 A using-declaration used as a member-declaration shall refer to a
3393 member of a base class of the class being defined.
3395 In general, we cannot check this constraint in a template because
3396 we do not know the entire set of base classes of the current
3397 class type. Morover, if SCOPE is dependent, it might match a
3398 non-dependent base. */
3400 if (!scope_dependent_p)
3402 base_kind b_kind;
3403 binfo = lookup_base (current_class_type, scope, ba_any, &b_kind,
3404 tf_warning_or_error);
3405 if (b_kind < bk_proper_base)
3407 if (!bases_dependent_p || b_kind == bk_same_type)
3409 error_not_base_type (scope, current_class_type);
3410 return NULL_TREE;
3413 else if (!name_dependent_p)
3415 decl = lookup_member (binfo, name, 0, false, tf_warning_or_error);
3416 if (!decl)
3418 error ("no members matching %<%T::%D%> in %q#T", scope, name,
3419 scope);
3420 return NULL_TREE;
3422 /* The binfo from which the functions came does not matter. */
3423 if (BASELINK_P (decl))
3424 decl = BASELINK_FUNCTIONS (decl);
3428 value = build_lang_decl (USING_DECL, name, NULL_TREE);
3429 USING_DECL_DECLS (value) = decl;
3430 USING_DECL_SCOPE (value) = scope;
3431 DECL_DEPENDENT_P (value) = !decl;
3433 return value;
3437 /* Return the binding value for name in scope. */
3440 static tree
3441 namespace_binding_1 (tree name, tree scope)
3443 cxx_binding *binding;
3445 if (SCOPE_FILE_SCOPE_P (scope))
3446 scope = global_namespace;
3447 else
3448 /* Unnecessary for the global namespace because it can't be an alias. */
3449 scope = ORIGINAL_NAMESPACE (scope);
3451 binding = cp_binding_level_find_binding_for_name (NAMESPACE_LEVEL (scope), name);
3453 return binding ? binding->value : NULL_TREE;
3456 tree
3457 namespace_binding (tree name, tree scope)
3459 tree ret;
3460 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3461 ret = namespace_binding_1 (name, scope);
3462 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3463 return ret;
3466 /* Set the binding value for name in scope. */
3468 static void
3469 set_namespace_binding_1 (tree name, tree scope, tree val)
3471 cxx_binding *b;
3473 if (scope == NULL_TREE)
3474 scope = global_namespace;
3475 b = binding_for_name (NAMESPACE_LEVEL (scope), name);
3476 if (!b->value || TREE_CODE (val) == OVERLOAD || val == error_mark_node)
3477 b->value = val;
3478 else
3479 supplement_binding (b, val);
3482 /* Wrapper for set_namespace_binding_1. */
3484 void
3485 set_namespace_binding (tree name, tree scope, tree val)
3487 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3488 set_namespace_binding_1 (name, scope, val);
3489 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3492 /* Set the context of a declaration to scope. Complain if we are not
3493 outside scope. */
3495 void
3496 set_decl_namespace (tree decl, tree scope, bool friendp)
3498 tree old;
3500 /* Get rid of namespace aliases. */
3501 scope = ORIGINAL_NAMESPACE (scope);
3503 /* It is ok for friends to be qualified in parallel space. */
3504 if (!friendp && !is_ancestor (current_namespace, scope))
3505 error ("declaration of %qD not in a namespace surrounding %qD",
3506 decl, scope);
3507 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
3509 /* Writing "int N::i" to declare a variable within "N" is invalid. */
3510 if (scope == current_namespace)
3512 if (at_namespace_scope_p ())
3513 error ("explicit qualification in declaration of %qD",
3514 decl);
3515 return;
3518 /* See whether this has been declared in the namespace. */
3519 old = lookup_qualified_name (scope, DECL_NAME (decl), false, true);
3520 if (old == error_mark_node)
3521 /* No old declaration at all. */
3522 goto complain;
3523 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
3524 if (TREE_CODE (old) == TREE_LIST)
3526 error ("reference to %qD is ambiguous", decl);
3527 print_candidates (old);
3528 return;
3530 if (!is_overloaded_fn (decl))
3532 /* We might have found OLD in an inline namespace inside SCOPE. */
3533 if (TREE_CODE (decl) == TREE_CODE (old))
3534 DECL_CONTEXT (decl) = DECL_CONTEXT (old);
3535 /* Don't compare non-function decls with decls_match here, since
3536 it can't check for the correct constness at this
3537 point. pushdecl will find those errors later. */
3538 return;
3540 /* Since decl is a function, old should contain a function decl. */
3541 if (!is_overloaded_fn (old))
3542 goto complain;
3543 /* A template can be explicitly specialized in any namespace. */
3544 if (processing_explicit_instantiation)
3545 return;
3546 if (processing_template_decl || processing_specialization)
3547 /* We have not yet called push_template_decl to turn a
3548 FUNCTION_DECL into a TEMPLATE_DECL, so the declarations won't
3549 match. But, we'll check later, when we construct the
3550 template. */
3551 return;
3552 /* Instantiations or specializations of templates may be declared as
3553 friends in any namespace. */
3554 if (friendp && DECL_USE_TEMPLATE (decl))
3555 return;
3556 if (is_overloaded_fn (old))
3558 tree found = NULL_TREE;
3559 tree elt = old;
3560 for (; elt; elt = OVL_NEXT (elt))
3562 tree ofn = OVL_CURRENT (elt);
3563 /* Adjust DECL_CONTEXT first so decls_match will return true
3564 if DECL will match a declaration in an inline namespace. */
3565 DECL_CONTEXT (decl) = DECL_CONTEXT (ofn);
3566 if (decls_match (decl, ofn))
3568 if (found && !decls_match (found, ofn))
3570 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
3571 error ("reference to %qD is ambiguous", decl);
3572 print_candidates (old);
3573 return;
3575 found = ofn;
3578 if (found)
3580 if (!is_associated_namespace (scope, CP_DECL_CONTEXT (found)))
3581 goto complain;
3582 DECL_CONTEXT (decl) = DECL_CONTEXT (found);
3583 return;
3586 else
3588 DECL_CONTEXT (decl) = DECL_CONTEXT (old);
3589 if (decls_match (decl, old))
3590 return;
3593 /* It didn't work, go back to the explicit scope. */
3594 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
3595 complain:
3596 error ("%qD should have been declared inside %qD", decl, scope);
3599 /* Return the namespace where the current declaration is declared. */
3601 tree
3602 current_decl_namespace (void)
3604 tree result;
3605 /* If we have been pushed into a different namespace, use it. */
3606 if (!vec_safe_is_empty (decl_namespace_list))
3607 return decl_namespace_list->last ();
3609 if (current_class_type)
3610 result = decl_namespace_context (current_class_type);
3611 else if (current_function_decl)
3612 result = decl_namespace_context (current_function_decl);
3613 else
3614 result = current_namespace;
3615 return result;
3618 /* Process any ATTRIBUTES on a namespace definition. Returns true if
3619 attribute visibility is seen. */
3621 bool
3622 handle_namespace_attrs (tree ns, tree attributes)
3624 tree d;
3625 bool saw_vis = false;
3627 for (d = attributes; d; d = TREE_CHAIN (d))
3629 tree name = get_attribute_name (d);
3630 tree args = TREE_VALUE (d);
3632 if (is_attribute_p ("visibility", name))
3634 /* attribute visibility is a property of the syntactic block
3635 rather than the namespace as a whole, so we don't touch the
3636 NAMESPACE_DECL at all. */
3637 tree x = args ? TREE_VALUE (args) : NULL_TREE;
3638 if (x == NULL_TREE || TREE_CODE (x) != STRING_CST || TREE_CHAIN (args))
3640 warning (OPT_Wattributes,
3641 "%qD attribute requires a single NTBS argument",
3642 name);
3643 continue;
3646 if (!TREE_PUBLIC (ns))
3647 warning (OPT_Wattributes,
3648 "%qD attribute is meaningless since members of the "
3649 "anonymous namespace get local symbols", name);
3651 push_visibility (TREE_STRING_POINTER (x), 1);
3652 saw_vis = true;
3654 else if (is_attribute_p ("abi_tag", name))
3656 if (!DECL_NAMESPACE_ASSOCIATIONS (ns))
3658 warning (OPT_Wattributes, "ignoring %qD attribute on non-inline "
3659 "namespace", name);
3660 continue;
3662 if (!DECL_NAME (ns))
3664 warning (OPT_Wattributes, "ignoring %qD attribute on anonymous "
3665 "namespace", name);
3666 continue;
3668 if (!args)
3670 tree dn = DECL_NAME (ns);
3671 args = build_string (IDENTIFIER_LENGTH (dn) + 1,
3672 IDENTIFIER_POINTER (dn));
3673 TREE_TYPE (args) = char_array_type_node;
3674 args = fix_string_type (args);
3675 args = build_tree_list (NULL_TREE, args);
3677 if (check_abi_tag_args (args, name))
3678 DECL_ATTRIBUTES (ns) = tree_cons (name, args,
3679 DECL_ATTRIBUTES (ns));
3681 else
3683 warning (OPT_Wattributes, "%qD attribute directive ignored",
3684 name);
3685 continue;
3689 return saw_vis;
3692 /* Push into the scope of the NAME namespace. If NAME is NULL_TREE, then we
3693 select a name that is unique to this compilation unit. */
3695 void
3696 push_namespace (tree name)
3698 tree d = NULL_TREE;
3699 bool need_new = true;
3700 bool implicit_use = false;
3701 bool anon = !name;
3703 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3705 /* We should not get here if the global_namespace is not yet constructed
3706 nor if NAME designates the global namespace: The global scope is
3707 constructed elsewhere. */
3708 gcc_assert (global_namespace != NULL && name != global_scope_name);
3710 if (anon)
3712 name = get_anonymous_namespace_name();
3713 d = IDENTIFIER_NAMESPACE_VALUE (name);
3714 if (d)
3715 /* Reopening anonymous namespace. */
3716 need_new = false;
3717 implicit_use = true;
3719 else
3721 /* Check whether this is an extended namespace definition. */
3722 d = IDENTIFIER_NAMESPACE_VALUE (name);
3723 if (d != NULL_TREE && TREE_CODE (d) == NAMESPACE_DECL)
3725 tree dna = DECL_NAMESPACE_ALIAS (d);
3726 if (dna)
3728 /* We do some error recovery for, eg, the redeclaration
3729 of M here:
3731 namespace N {}
3732 namespace M = N;
3733 namespace M {}
3735 However, in nasty cases like:
3737 namespace N
3739 namespace M = N;
3740 namespace M {}
3743 we just error out below, in duplicate_decls. */
3744 if (NAMESPACE_LEVEL (dna)->level_chain
3745 == current_binding_level)
3747 error ("namespace alias %qD not allowed here, "
3748 "assuming %qD", d, dna);
3749 d = dna;
3750 need_new = false;
3753 else
3754 need_new = false;
3758 if (need_new)
3760 /* Make a new namespace, binding the name to it. */
3761 d = build_lang_decl (NAMESPACE_DECL, name, void_type_node);
3762 DECL_CONTEXT (d) = FROB_CONTEXT (current_namespace);
3763 /* The name of this namespace is not visible to other translation
3764 units if it is an anonymous namespace or member thereof. */
3765 if (anon || decl_anon_ns_mem_p (current_namespace))
3766 TREE_PUBLIC (d) = 0;
3767 else
3768 TREE_PUBLIC (d) = 1;
3769 pushdecl (d);
3770 if (anon)
3772 /* Clear DECL_NAME for the benefit of debugging back ends. */
3773 SET_DECL_ASSEMBLER_NAME (d, name);
3774 DECL_NAME (d) = NULL_TREE;
3776 begin_scope (sk_namespace, d);
3778 else
3779 resume_scope (NAMESPACE_LEVEL (d));
3781 if (implicit_use)
3782 do_using_directive (d);
3783 /* Enter the name space. */
3784 current_namespace = d;
3786 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3789 /* Pop from the scope of the current namespace. */
3791 void
3792 pop_namespace (void)
3794 gcc_assert (current_namespace != global_namespace);
3795 current_namespace = CP_DECL_CONTEXT (current_namespace);
3796 /* The binding level is not popped, as it might be re-opened later. */
3797 leave_scope ();
3800 /* Push into the scope of the namespace NS, even if it is deeply
3801 nested within another namespace. */
3803 void
3804 push_nested_namespace (tree ns)
3806 if (ns == global_namespace)
3807 push_to_top_level ();
3808 else
3810 push_nested_namespace (CP_DECL_CONTEXT (ns));
3811 push_namespace (DECL_NAME (ns));
3815 /* Pop back from the scope of the namespace NS, which was previously
3816 entered with push_nested_namespace. */
3818 void
3819 pop_nested_namespace (tree ns)
3821 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3822 gcc_assert (current_namespace == ns);
3823 while (ns != global_namespace)
3825 pop_namespace ();
3826 ns = CP_DECL_CONTEXT (ns);
3829 pop_from_top_level ();
3830 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3833 /* Temporarily set the namespace for the current declaration. */
3835 void
3836 push_decl_namespace (tree decl)
3838 if (TREE_CODE (decl) != NAMESPACE_DECL)
3839 decl = decl_namespace_context (decl);
3840 vec_safe_push (decl_namespace_list, ORIGINAL_NAMESPACE (decl));
3843 /* [namespace.memdef]/2 */
3845 void
3846 pop_decl_namespace (void)
3848 decl_namespace_list->pop ();
3851 /* Return the namespace that is the common ancestor
3852 of two given namespaces. */
3854 static tree
3855 namespace_ancestor_1 (tree ns1, tree ns2)
3857 tree nsr;
3858 if (is_ancestor (ns1, ns2))
3859 nsr = ns1;
3860 else
3861 nsr = namespace_ancestor_1 (CP_DECL_CONTEXT (ns1), ns2);
3862 return nsr;
3865 /* Wrapper for namespace_ancestor_1. */
3867 static tree
3868 namespace_ancestor (tree ns1, tree ns2)
3870 tree nsr;
3871 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3872 nsr = namespace_ancestor_1 (ns1, ns2);
3873 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3874 return nsr;
3877 /* Process a namespace-alias declaration. */
3879 void
3880 do_namespace_alias (tree alias, tree name_space)
3882 if (name_space == error_mark_node)
3883 return;
3885 gcc_assert (TREE_CODE (name_space) == NAMESPACE_DECL);
3887 name_space = ORIGINAL_NAMESPACE (name_space);
3889 /* Build the alias. */
3890 alias = build_lang_decl (NAMESPACE_DECL, alias, void_type_node);
3891 DECL_NAMESPACE_ALIAS (alias) = name_space;
3892 DECL_EXTERNAL (alias) = 1;
3893 DECL_CONTEXT (alias) = FROB_CONTEXT (current_scope ());
3894 pushdecl (alias);
3896 /* Emit debug info for namespace alias. */
3897 if (!building_stmt_list_p ())
3898 (*debug_hooks->early_global_decl) (alias);
3901 /* Like pushdecl, only it places X in the current namespace,
3902 if appropriate. */
3904 tree
3905 pushdecl_namespace_level (tree x, bool is_friend)
3907 cp_binding_level *b = current_binding_level;
3908 tree t;
3910 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3911 t = pushdecl_with_scope (x, NAMESPACE_LEVEL (current_namespace), is_friend);
3913 /* Now, the type_shadowed stack may screw us. Munge it so it does
3914 what we want. */
3915 if (TREE_CODE (t) == TYPE_DECL)
3917 tree name = DECL_NAME (t);
3918 tree newval;
3919 tree *ptr = (tree *)0;
3920 for (; !global_scope_p (b); b = b->level_chain)
3922 tree shadowed = b->type_shadowed;
3923 for (; shadowed; shadowed = TREE_CHAIN (shadowed))
3924 if (TREE_PURPOSE (shadowed) == name)
3926 ptr = &TREE_VALUE (shadowed);
3927 /* Can't break out of the loop here because sometimes
3928 a binding level will have duplicate bindings for
3929 PT names. It's gross, but I haven't time to fix it. */
3932 newval = TREE_TYPE (t);
3933 if (ptr == (tree *)0)
3935 /* @@ This shouldn't be needed. My test case "zstring.cc" trips
3936 up here if this is changed to an assertion. --KR */
3937 SET_IDENTIFIER_TYPE_VALUE (name, t);
3939 else
3941 *ptr = newval;
3944 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3945 return t;
3948 /* Insert USED into the using list of USER. Set INDIRECT_flag if this
3949 directive is not directly from the source. Also find the common
3950 ancestor and let our users know about the new namespace */
3952 static void
3953 add_using_namespace_1 (tree user, tree used, bool indirect)
3955 tree t;
3956 /* Using oneself is a no-op. */
3957 if (user == used)
3958 return;
3959 gcc_assert (TREE_CODE (user) == NAMESPACE_DECL);
3960 gcc_assert (TREE_CODE (used) == NAMESPACE_DECL);
3961 /* Check if we already have this. */
3962 t = purpose_member (used, DECL_NAMESPACE_USING (user));
3963 if (t != NULL_TREE)
3965 if (!indirect)
3966 /* Promote to direct usage. */
3967 TREE_INDIRECT_USING (t) = 0;
3968 return;
3971 /* Add used to the user's using list. */
3972 DECL_NAMESPACE_USING (user)
3973 = tree_cons (used, namespace_ancestor (user, used),
3974 DECL_NAMESPACE_USING (user));
3976 TREE_INDIRECT_USING (DECL_NAMESPACE_USING (user)) = indirect;
3978 /* Add user to the used's users list. */
3979 DECL_NAMESPACE_USERS (used)
3980 = tree_cons (user, 0, DECL_NAMESPACE_USERS (used));
3982 /* Recursively add all namespaces used. */
3983 for (t = DECL_NAMESPACE_USING (used); t; t = TREE_CHAIN (t))
3984 /* indirect usage */
3985 add_using_namespace_1 (user, TREE_PURPOSE (t), 1);
3987 /* Tell everyone using us about the new used namespaces. */
3988 for (t = DECL_NAMESPACE_USERS (user); t; t = TREE_CHAIN (t))
3989 add_using_namespace_1 (TREE_PURPOSE (t), used, 1);
3992 /* Wrapper for add_using_namespace_1. */
3994 static void
3995 add_using_namespace (tree user, tree used, bool indirect)
3997 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3998 add_using_namespace_1 (user, used, indirect);
3999 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4002 /* Process a using-declaration not appearing in class or local scope. */
4004 void
4005 do_toplevel_using_decl (tree decl, tree scope, tree name)
4007 tree oldval, oldtype, newval, newtype;
4008 tree orig_decl = decl;
4009 cxx_binding *binding;
4011 decl = validate_nonmember_using_decl (decl, scope, name);
4012 if (decl == NULL_TREE)
4013 return;
4015 binding = binding_for_name (NAMESPACE_LEVEL (current_namespace), name);
4017 oldval = binding->value;
4018 oldtype = binding->type;
4020 do_nonmember_using_decl (scope, name, oldval, oldtype, &newval, &newtype);
4022 /* Emit debug info. */
4023 if (!processing_template_decl)
4024 cp_emit_debug_info_for_using (orig_decl, current_namespace);
4026 /* Copy declarations found. */
4027 if (newval)
4028 binding->value = newval;
4029 if (newtype)
4030 binding->type = newtype;
4033 /* Process a using-directive. */
4035 void
4036 do_using_directive (tree name_space)
4038 tree context = NULL_TREE;
4040 if (name_space == error_mark_node)
4041 return;
4043 gcc_assert (TREE_CODE (name_space) == NAMESPACE_DECL);
4045 if (building_stmt_list_p ())
4046 add_stmt (build_stmt (input_location, USING_STMT, name_space));
4047 name_space = ORIGINAL_NAMESPACE (name_space);
4049 if (!toplevel_bindings_p ())
4051 push_using_directive (name_space);
4053 else
4055 /* direct usage */
4056 add_using_namespace (current_namespace, name_space, 0);
4057 if (current_namespace != global_namespace)
4058 context = current_namespace;
4060 /* Emit debugging info. */
4061 if (!processing_template_decl)
4062 (*debug_hooks->imported_module_or_decl) (name_space, NULL_TREE,
4063 context, false);
4067 /* Deal with a using-directive seen by the parser. Currently we only
4068 handle attributes here, since they cannot appear inside a template. */
4070 void
4071 parse_using_directive (tree name_space, tree attribs)
4073 do_using_directive (name_space);
4075 if (attribs == error_mark_node)
4076 return;
4078 for (tree a = attribs; a; a = TREE_CHAIN (a))
4080 tree name = get_attribute_name (a);
4081 if (is_attribute_p ("strong", name))
4083 if (!toplevel_bindings_p ())
4084 error ("strong using only meaningful at namespace scope");
4085 else if (name_space != error_mark_node)
4087 if (!is_ancestor (current_namespace, name_space))
4088 error ("current namespace %qD does not enclose strongly used namespace %qD",
4089 current_namespace, name_space);
4090 DECL_NAMESPACE_ASSOCIATIONS (name_space)
4091 = tree_cons (current_namespace, 0,
4092 DECL_NAMESPACE_ASSOCIATIONS (name_space));
4095 else
4096 warning (OPT_Wattributes, "%qD attribute directive ignored", name);
4100 /* Like pushdecl, only it places X in the global scope if appropriate.
4101 Calls cp_finish_decl to register the variable, initializing it with
4102 *INIT, if INIT is non-NULL. */
4104 static tree
4105 pushdecl_top_level_1 (tree x, tree *init, bool is_friend)
4107 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4108 push_to_top_level ();
4109 x = pushdecl_namespace_level (x, is_friend);
4110 if (init)
4111 cp_finish_decl (x, *init, false, NULL_TREE, 0);
4112 pop_from_top_level ();
4113 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4114 return x;
4117 /* Like pushdecl, only it places X in the global scope if appropriate. */
4119 tree
4120 pushdecl_top_level (tree x)
4122 return pushdecl_top_level_1 (x, NULL, false);
4125 /* Like pushdecl_top_level, but adding the IS_FRIEND parameter. */
4127 tree
4128 pushdecl_top_level_maybe_friend (tree x, bool is_friend)
4130 return pushdecl_top_level_1 (x, NULL, is_friend);
4133 /* Like pushdecl, only it places X in the global scope if
4134 appropriate. Calls cp_finish_decl to register the variable,
4135 initializing it with INIT. */
4137 tree
4138 pushdecl_top_level_and_finish (tree x, tree init)
4140 return pushdecl_top_level_1 (x, &init, false);
4143 /* Combines two sets of overloaded functions into an OVERLOAD chain, removing
4144 duplicates. The first list becomes the tail of the result.
4146 The algorithm is O(n^2). We could get this down to O(n log n) by
4147 doing a sort on the addresses of the functions, if that becomes
4148 necessary. */
4150 static tree
4151 merge_functions (tree s1, tree s2)
4153 for (; s2; s2 = OVL_NEXT (s2))
4155 tree fn2 = OVL_CURRENT (s2);
4156 tree fns1;
4158 for (fns1 = s1; fns1; fns1 = OVL_NEXT (fns1))
4160 tree fn1 = OVL_CURRENT (fns1);
4162 /* If the function from S2 is already in S1, there is no
4163 need to add it again. For `extern "C"' functions, we
4164 might have two FUNCTION_DECLs for the same function, in
4165 different namespaces, but let's leave them in case
4166 they have different default arguments. */
4167 if (fn1 == fn2)
4168 break;
4171 /* If we exhausted all of the functions in S1, FN2 is new. */
4172 if (!fns1)
4173 s1 = build_overload (fn2, s1);
4175 return s1;
4178 /* Returns TRUE iff OLD and NEW are the same entity.
4180 3 [basic]/3: An entity is a value, object, reference, function,
4181 enumerator, type, class member, template, template specialization,
4182 namespace, parameter pack, or this.
4184 7.3.4 [namespace.udir]/4: If name lookup finds a declaration for a name
4185 in two different namespaces, and the declarations do not declare the
4186 same entity and do not declare functions, the use of the name is
4187 ill-formed. */
4189 static bool
4190 same_entity_p (tree one, tree two)
4192 if (one == two)
4193 return true;
4194 if (!one || !two)
4195 return false;
4196 if (TREE_CODE (one) == TYPE_DECL
4197 && TREE_CODE (two) == TYPE_DECL
4198 && same_type_p (TREE_TYPE (one), TREE_TYPE (two)))
4199 return true;
4200 return false;
4203 /* This should return an error not all definitions define functions.
4204 It is not an error if we find two functions with exactly the
4205 same signature, only if these are selected in overload resolution.
4206 old is the current set of bindings, new_binding the freshly-found binding.
4207 XXX Do we want to give *all* candidates in case of ambiguity?
4208 XXX In what way should I treat extern declarations?
4209 XXX I don't want to repeat the entire duplicate_decls here */
4211 static void
4212 ambiguous_decl (struct scope_binding *old, cxx_binding *new_binding, int flags)
4214 tree val, type;
4215 gcc_assert (old != NULL);
4217 /* Copy the type. */
4218 type = new_binding->type;
4219 if (LOOKUP_NAMESPACES_ONLY (flags)
4220 || (type && hidden_name_p (type) && !(flags & LOOKUP_HIDDEN)))
4221 type = NULL_TREE;
4223 /* Copy the value. */
4224 val = new_binding->value;
4225 if (val)
4227 if (hidden_name_p (val) && !(flags & LOOKUP_HIDDEN))
4228 val = NULL_TREE;
4229 else
4230 switch (TREE_CODE (val))
4232 case TEMPLATE_DECL:
4233 /* If we expect types or namespaces, and not templates,
4234 or this is not a template class. */
4235 if ((LOOKUP_QUALIFIERS_ONLY (flags)
4236 && !DECL_TYPE_TEMPLATE_P (val)))
4237 val = NULL_TREE;
4238 break;
4239 case TYPE_DECL:
4240 if (LOOKUP_NAMESPACES_ONLY (flags)
4241 || (type && (flags & LOOKUP_PREFER_TYPES)))
4242 val = NULL_TREE;
4243 break;
4244 case NAMESPACE_DECL:
4245 if (LOOKUP_TYPES_ONLY (flags))
4246 val = NULL_TREE;
4247 break;
4248 case FUNCTION_DECL:
4249 /* Ignore built-in functions that are still anticipated. */
4250 if (LOOKUP_QUALIFIERS_ONLY (flags))
4251 val = NULL_TREE;
4252 break;
4253 default:
4254 if (LOOKUP_QUALIFIERS_ONLY (flags))
4255 val = NULL_TREE;
4259 /* If val is hidden, shift down any class or enumeration name. */
4260 if (!val)
4262 val = type;
4263 type = NULL_TREE;
4266 if (!old->value)
4267 old->value = val;
4268 else if (val && !same_entity_p (val, old->value))
4270 if (is_overloaded_fn (old->value) && is_overloaded_fn (val))
4271 old->value = merge_functions (old->value, val);
4272 else
4274 old->value = tree_cons (NULL_TREE, old->value,
4275 build_tree_list (NULL_TREE, val));
4276 TREE_TYPE (old->value) = error_mark_node;
4280 if (!old->type)
4281 old->type = type;
4282 else if (type && old->type != type)
4284 old->type = tree_cons (NULL_TREE, old->type,
4285 build_tree_list (NULL_TREE, type));
4286 TREE_TYPE (old->type) = error_mark_node;
4290 /* Return the declarations that are members of the namespace NS. */
4292 tree
4293 cp_namespace_decls (tree ns)
4295 return NAMESPACE_LEVEL (ns)->names;
4298 /* Combine prefer_type and namespaces_only into flags. */
4300 static int
4301 lookup_flags (int prefer_type, int namespaces_only)
4303 if (namespaces_only)
4304 return LOOKUP_PREFER_NAMESPACES;
4305 if (prefer_type > 1)
4306 return LOOKUP_PREFER_TYPES;
4307 if (prefer_type > 0)
4308 return LOOKUP_PREFER_BOTH;
4309 return 0;
4312 /* Given a lookup that returned VAL, use FLAGS to decide if we want to
4313 ignore it or not. Subroutine of lookup_name_real and
4314 lookup_type_scope. */
4316 static bool
4317 qualify_lookup (tree val, int flags)
4319 if (val == NULL_TREE)
4320 return false;
4321 if ((flags & LOOKUP_PREFER_NAMESPACES) && TREE_CODE (val) == NAMESPACE_DECL)
4322 return true;
4323 if (flags & LOOKUP_PREFER_TYPES)
4325 tree target_val = strip_using_decl (val);
4326 if (TREE_CODE (target_val) == TYPE_DECL
4327 || TREE_CODE (target_val) == TEMPLATE_DECL)
4328 return true;
4330 if (flags & (LOOKUP_PREFER_NAMESPACES | LOOKUP_PREFER_TYPES))
4331 return false;
4332 /* Look through lambda things that we shouldn't be able to see. */
4333 if (is_lambda_ignored_entity (val))
4334 return false;
4335 return true;
4338 /* Given a lookup that returned VAL, decide if we want to ignore it or
4339 not based on DECL_ANTICIPATED. */
4341 bool
4342 hidden_name_p (tree val)
4344 if (DECL_P (val)
4345 && DECL_LANG_SPECIFIC (val)
4346 && TYPE_FUNCTION_OR_TEMPLATE_DECL_P (val)
4347 && DECL_ANTICIPATED (val))
4348 return true;
4349 return false;
4352 /* Remove any hidden friend functions from a possibly overloaded set
4353 of functions. */
4355 tree
4356 remove_hidden_names (tree fns)
4358 if (!fns)
4359 return fns;
4361 if (TREE_CODE (fns) == FUNCTION_DECL && hidden_name_p (fns))
4362 fns = NULL_TREE;
4363 else if (TREE_CODE (fns) == OVERLOAD)
4365 tree o;
4367 for (o = fns; o; o = OVL_NEXT (o))
4368 if (hidden_name_p (OVL_CURRENT (o)))
4369 break;
4370 if (o)
4372 tree n = NULL_TREE;
4374 for (o = fns; o; o = OVL_NEXT (o))
4375 if (!hidden_name_p (OVL_CURRENT (o)))
4376 n = build_overload (OVL_CURRENT (o), n);
4377 fns = n;
4381 return fns;
4384 /* Suggest alternatives for NAME, an IDENTIFIER_NODE for which name
4385 lookup failed. Search through all available namespaces and print out
4386 possible candidates. */
4388 void
4389 suggest_alternatives_for (location_t location, tree name)
4391 vec<tree> candidates = vNULL;
4392 vec<tree> namespaces_to_search = vNULL;
4393 int max_to_search = PARAM_VALUE (CXX_MAX_NAMESPACES_FOR_DIAGNOSTIC_HELP);
4394 int n_searched = 0;
4395 tree t;
4396 unsigned ix;
4398 namespaces_to_search.safe_push (global_namespace);
4400 while (!namespaces_to_search.is_empty ()
4401 && n_searched < max_to_search)
4403 tree scope = namespaces_to_search.pop ();
4404 struct scope_binding binding = EMPTY_SCOPE_BINDING;
4405 cp_binding_level *level = NAMESPACE_LEVEL (scope);
4407 /* Look in this namespace. */
4408 qualified_lookup_using_namespace (name, scope, &binding, 0);
4410 n_searched++;
4412 if (binding.value)
4413 candidates.safe_push (binding.value);
4415 /* Add child namespaces. */
4416 for (t = level->namespaces; t; t = DECL_CHAIN (t))
4417 namespaces_to_search.safe_push (t);
4420 /* If we stopped before we could examine all namespaces, inform the
4421 user. Do this even if we don't have any candidates, since there
4422 might be more candidates further down that we weren't able to
4423 find. */
4424 if (n_searched >= max_to_search
4425 && !namespaces_to_search.is_empty ())
4426 inform (location,
4427 "maximum limit of %d namespaces searched for %qE",
4428 max_to_search, name);
4430 namespaces_to_search.release ();
4432 /* Nothing useful to report. */
4433 if (candidates.is_empty ())
4434 return;
4436 inform_n (location, candidates.length (),
4437 "suggested alternative:",
4438 "suggested alternatives:");
4440 FOR_EACH_VEC_ELT (candidates, ix, t)
4441 inform (location_of (t), " %qE", t);
4443 candidates.release ();
4446 /* Unscoped lookup of a global: iterate over current namespaces,
4447 considering using-directives. */
4449 static tree
4450 unqualified_namespace_lookup_1 (tree name, int flags)
4452 tree initial = current_decl_namespace ();
4453 tree scope = initial;
4454 tree siter;
4455 cp_binding_level *level;
4456 tree val = NULL_TREE;
4458 for (; !val; scope = CP_DECL_CONTEXT (scope))
4460 struct scope_binding binding = EMPTY_SCOPE_BINDING;
4461 cxx_binding *b =
4462 cp_binding_level_find_binding_for_name (NAMESPACE_LEVEL (scope), name);
4464 if (b)
4465 ambiguous_decl (&binding, b, flags);
4467 /* Add all _DECLs seen through local using-directives. */
4468 for (level = current_binding_level;
4469 level->kind != sk_namespace;
4470 level = level->level_chain)
4471 if (!lookup_using_namespace (name, &binding, level->using_directives,
4472 scope, flags))
4473 /* Give up because of error. */
4474 return error_mark_node;
4476 /* Add all _DECLs seen through global using-directives. */
4477 /* XXX local and global using lists should work equally. */
4478 siter = initial;
4479 while (1)
4481 if (!lookup_using_namespace (name, &binding,
4482 DECL_NAMESPACE_USING (siter),
4483 scope, flags))
4484 /* Give up because of error. */
4485 return error_mark_node;
4486 if (siter == scope) break;
4487 siter = CP_DECL_CONTEXT (siter);
4490 val = binding.value;
4491 if (scope == global_namespace)
4492 break;
4494 return val;
4497 /* Wrapper for unqualified_namespace_lookup_1. */
4499 static tree
4500 unqualified_namespace_lookup (tree name, int flags)
4502 tree ret;
4503 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4504 ret = unqualified_namespace_lookup_1 (name, flags);
4505 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4506 return ret;
4509 /* Look up NAME (an IDENTIFIER_NODE) in SCOPE (either a NAMESPACE_DECL
4510 or a class TYPE). If IS_TYPE_P is TRUE, then ignore non-type
4511 bindings.
4513 Returns a DECL (or OVERLOAD, or BASELINK) representing the
4514 declaration found. If no suitable declaration can be found,
4515 ERROR_MARK_NODE is returned. If COMPLAIN is true and SCOPE is
4516 neither a class-type nor a namespace a diagnostic is issued. */
4518 tree
4519 lookup_qualified_name (tree scope, tree name, bool is_type_p, bool complain)
4521 int flags = 0;
4522 tree t = NULL_TREE;
4524 if (TREE_CODE (scope) == NAMESPACE_DECL)
4526 struct scope_binding binding = EMPTY_SCOPE_BINDING;
4528 if (is_type_p)
4529 flags |= LOOKUP_PREFER_TYPES;
4530 if (qualified_lookup_using_namespace (name, scope, &binding, flags))
4531 t = binding.value;
4533 else if (cxx_dialect != cxx98 && TREE_CODE (scope) == ENUMERAL_TYPE)
4534 t = lookup_enumerator (scope, name);
4535 else if (is_class_type (scope, complain))
4536 t = lookup_member (scope, name, 2, is_type_p, tf_warning_or_error);
4538 if (!t)
4539 return error_mark_node;
4540 return t;
4543 /* Subroutine of unqualified_namespace_lookup:
4544 Add the bindings of NAME in used namespaces to VAL.
4545 We are currently looking for names in namespace SCOPE, so we
4546 look through USINGS for using-directives of namespaces
4547 which have SCOPE as a common ancestor with the current scope.
4548 Returns false on errors. */
4550 static bool
4551 lookup_using_namespace (tree name, struct scope_binding *val,
4552 tree usings, tree scope, int flags)
4554 tree iter;
4555 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4556 /* Iterate over all used namespaces in current, searching for using
4557 directives of scope. */
4558 for (iter = usings; iter; iter = TREE_CHAIN (iter))
4559 if (TREE_VALUE (iter) == scope)
4561 tree used = ORIGINAL_NAMESPACE (TREE_PURPOSE (iter));
4562 cxx_binding *val1 =
4563 cp_binding_level_find_binding_for_name (NAMESPACE_LEVEL (used), name);
4564 /* Resolve ambiguities. */
4565 if (val1)
4566 ambiguous_decl (val, val1, flags);
4568 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4569 return val->value != error_mark_node;
4572 /* Returns true iff VEC contains TARGET. */
4574 static bool
4575 tree_vec_contains (vec<tree, va_gc> *vec, tree target)
4577 unsigned int i;
4578 tree elt;
4579 FOR_EACH_VEC_SAFE_ELT (vec,i,elt)
4580 if (elt == target)
4581 return true;
4582 return false;
4585 /* [namespace.qual]
4586 Accepts the NAME to lookup and its qualifying SCOPE.
4587 Returns the name/type pair found into the cxx_binding *RESULT,
4588 or false on error. */
4590 static bool
4591 qualified_lookup_using_namespace (tree name, tree scope,
4592 struct scope_binding *result, int flags)
4594 /* Maintain a list of namespaces visited... */
4595 vec<tree, va_gc> *seen = NULL;
4596 vec<tree, va_gc> *seen_inline = NULL;
4597 /* ... and a list of namespace yet to see. */
4598 vec<tree, va_gc> *todo = NULL;
4599 vec<tree, va_gc> *todo_maybe = NULL;
4600 vec<tree, va_gc> *todo_inline = NULL;
4601 tree usings;
4602 timevar_start (TV_NAME_LOOKUP);
4603 /* Look through namespace aliases. */
4604 scope = ORIGINAL_NAMESPACE (scope);
4606 /* Algorithm: Starting with SCOPE, walk through the set of used
4607 namespaces. For each used namespace, look through its inline
4608 namespace set for any bindings and usings. If no bindings are
4609 found, add any usings seen to the set of used namespaces. */
4610 vec_safe_push (todo, scope);
4612 while (todo->length ())
4614 bool found_here;
4615 scope = todo->pop ();
4616 if (tree_vec_contains (seen, scope))
4617 continue;
4618 vec_safe_push (seen, scope);
4619 vec_safe_push (todo_inline, scope);
4621 found_here = false;
4622 while (todo_inline->length ())
4624 cxx_binding *binding;
4626 scope = todo_inline->pop ();
4627 if (tree_vec_contains (seen_inline, scope))
4628 continue;
4629 vec_safe_push (seen_inline, scope);
4631 binding =
4632 cp_binding_level_find_binding_for_name (NAMESPACE_LEVEL (scope), name);
4633 if (binding)
4635 found_here = true;
4636 ambiguous_decl (result, binding, flags);
4639 for (usings = DECL_NAMESPACE_USING (scope); usings;
4640 usings = TREE_CHAIN (usings))
4641 if (!TREE_INDIRECT_USING (usings))
4643 if (is_associated_namespace (scope, TREE_PURPOSE (usings)))
4644 vec_safe_push (todo_inline, TREE_PURPOSE (usings));
4645 else
4646 vec_safe_push (todo_maybe, TREE_PURPOSE (usings));
4650 if (found_here)
4651 vec_safe_truncate (todo_maybe, 0);
4652 else
4653 while (vec_safe_length (todo_maybe))
4654 vec_safe_push (todo, todo_maybe->pop ());
4656 vec_free (todo);
4657 vec_free (todo_maybe);
4658 vec_free (todo_inline);
4659 vec_free (seen);
4660 vec_free (seen_inline);
4661 timevar_stop (TV_NAME_LOOKUP);
4662 return result->value != error_mark_node;
4665 /* Subroutine of outer_binding.
4667 Returns TRUE if BINDING is a binding to a template parameter of
4668 SCOPE. In that case SCOPE is the scope of a primary template
4669 parameter -- in the sense of G++, i.e, a template that has its own
4670 template header.
4672 Returns FALSE otherwise. */
4674 static bool
4675 binding_to_template_parms_of_scope_p (cxx_binding *binding,
4676 cp_binding_level *scope)
4678 tree binding_value, tmpl, tinfo;
4679 int level;
4681 if (!binding || !scope || !scope->this_entity)
4682 return false;
4684 binding_value = binding->value ? binding->value : binding->type;
4685 tinfo = get_template_info (scope->this_entity);
4687 /* BINDING_VALUE must be a template parm. */
4688 if (binding_value == NULL_TREE
4689 || (!DECL_P (binding_value)
4690 || !DECL_TEMPLATE_PARM_P (binding_value)))
4691 return false;
4693 /* The level of BINDING_VALUE. */
4694 level =
4695 template_type_parameter_p (binding_value)
4696 ? TEMPLATE_PARM_LEVEL (TEMPLATE_TYPE_PARM_INDEX
4697 (TREE_TYPE (binding_value)))
4698 : TEMPLATE_PARM_LEVEL (DECL_INITIAL (binding_value));
4700 /* The template of the current scope, iff said scope is a primary
4701 template. */
4702 tmpl = (tinfo
4703 && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
4704 ? TI_TEMPLATE (tinfo)
4705 : NULL_TREE);
4707 /* If the level of the parm BINDING_VALUE equals the depth of TMPL,
4708 then BINDING_VALUE is a parameter of TMPL. */
4709 return (tmpl && level == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)));
4712 /* Return the innermost non-namespace binding for NAME from a scope
4713 containing BINDING, or, if BINDING is NULL, the current scope.
4714 Please note that for a given template, the template parameters are
4715 considered to be in the scope containing the current scope.
4716 If CLASS_P is false, then class bindings are ignored. */
4718 cxx_binding *
4719 outer_binding (tree name,
4720 cxx_binding *binding,
4721 bool class_p)
4723 cxx_binding *outer;
4724 cp_binding_level *scope;
4725 cp_binding_level *outer_scope;
4727 if (binding)
4729 scope = binding->scope->level_chain;
4730 outer = binding->previous;
4732 else
4734 scope = current_binding_level;
4735 outer = IDENTIFIER_BINDING (name);
4737 outer_scope = outer ? outer->scope : NULL;
4739 /* Because we create class bindings lazily, we might be missing a
4740 class binding for NAME. If there are any class binding levels
4741 between the LAST_BINDING_LEVEL and the scope in which OUTER was
4742 declared, we must lookup NAME in those class scopes. */
4743 if (class_p)
4744 while (scope && scope != outer_scope && scope->kind != sk_namespace)
4746 if (scope->kind == sk_class)
4748 cxx_binding *class_binding;
4750 class_binding = get_class_binding (name, scope);
4751 if (class_binding)
4753 /* Thread this new class-scope binding onto the
4754 IDENTIFIER_BINDING list so that future lookups
4755 find it quickly. */
4756 class_binding->previous = outer;
4757 if (binding)
4758 binding->previous = class_binding;
4759 else
4760 IDENTIFIER_BINDING (name) = class_binding;
4761 return class_binding;
4764 /* If we are in a member template, the template parms of the member
4765 template are considered to be inside the scope of the containing
4766 class, but within G++ the class bindings are all pushed between the
4767 template parms and the function body. So if the outer binding is
4768 a template parm for the current scope, return it now rather than
4769 look for a class binding. */
4770 if (outer_scope && outer_scope->kind == sk_template_parms
4771 && binding_to_template_parms_of_scope_p (outer, scope))
4772 return outer;
4774 scope = scope->level_chain;
4777 return outer;
4780 /* Return the innermost block-scope or class-scope value binding for
4781 NAME, or NULL_TREE if there is no such binding. */
4783 tree
4784 innermost_non_namespace_value (tree name)
4786 cxx_binding *binding;
4787 binding = outer_binding (name, /*binding=*/NULL, /*class_p=*/true);
4788 return binding ? binding->value : NULL_TREE;
4791 /* Look up NAME in the current binding level and its superiors in the
4792 namespace of variables, functions and typedefs. Return a ..._DECL
4793 node of some kind representing its definition if there is only one
4794 such declaration, or return a TREE_LIST with all the overloaded
4795 definitions if there are many, or return 0 if it is undefined.
4796 Hidden name, either friend declaration or built-in function, are
4797 not ignored.
4799 If PREFER_TYPE is > 0, we prefer TYPE_DECLs or namespaces.
4800 If PREFER_TYPE is > 1, we reject non-type decls (e.g. namespaces).
4801 Otherwise we prefer non-TYPE_DECLs.
4803 If NONCLASS is nonzero, bindings in class scopes are ignored. If
4804 BLOCK_P is false, bindings in block scopes are ignored. */
4806 static tree
4807 lookup_name_real_1 (tree name, int prefer_type, int nonclass, bool block_p,
4808 int namespaces_only, int flags)
4810 cxx_binding *iter;
4811 tree val = NULL_TREE;
4813 /* Conversion operators are handled specially because ordinary
4814 unqualified name lookup will not find template conversion
4815 operators. */
4816 if (IDENTIFIER_TYPENAME_P (name))
4818 cp_binding_level *level;
4820 for (level = current_binding_level;
4821 level && level->kind != sk_namespace;
4822 level = level->level_chain)
4824 tree class_type;
4825 tree operators;
4827 /* A conversion operator can only be declared in a class
4828 scope. */
4829 if (level->kind != sk_class)
4830 continue;
4832 /* Lookup the conversion operator in the class. */
4833 class_type = level->this_entity;
4834 operators = lookup_fnfields (class_type, name, /*protect=*/0);
4835 if (operators)
4836 return operators;
4839 return NULL_TREE;
4842 flags |= lookup_flags (prefer_type, namespaces_only);
4844 /* First, look in non-namespace scopes. */
4846 if (current_class_type == NULL_TREE)
4847 nonclass = 1;
4849 if (block_p || !nonclass)
4850 for (iter = outer_binding (name, NULL, !nonclass);
4851 iter;
4852 iter = outer_binding (name, iter, !nonclass))
4854 tree binding;
4856 /* Skip entities we don't want. */
4857 if (LOCAL_BINDING_P (iter) ? !block_p : nonclass)
4858 continue;
4860 /* If this is the kind of thing we're looking for, we're done. */
4861 if (qualify_lookup (iter->value, flags))
4862 binding = iter->value;
4863 else if ((flags & LOOKUP_PREFER_TYPES)
4864 && qualify_lookup (iter->type, flags))
4865 binding = iter->type;
4866 else
4867 binding = NULL_TREE;
4869 if (binding)
4871 if (hidden_name_p (binding))
4873 /* A non namespace-scope binding can only be hidden in the
4874 presence of a local class, due to friend declarations.
4876 In particular, consider:
4878 struct C;
4879 void f() {
4880 struct A {
4881 friend struct B;
4882 friend struct C;
4883 void g() {
4884 B* b; // error: B is hidden
4885 C* c; // OK, finds ::C
4888 B *b; // error: B is hidden
4889 C *c; // OK, finds ::C
4890 struct B {};
4891 B *bb; // OK
4894 The standard says that "B" is a local class in "f"
4895 (but not nested within "A") -- but that name lookup
4896 for "B" does not find this declaration until it is
4897 declared directly with "f".
4899 In particular:
4901 [class.friend]
4903 If a friend declaration appears in a local class and
4904 the name specified is an unqualified name, a prior
4905 declaration is looked up without considering scopes
4906 that are outside the innermost enclosing non-class
4907 scope. For a friend function declaration, if there is
4908 no prior declaration, the program is ill-formed. For a
4909 friend class declaration, if there is no prior
4910 declaration, the class that is specified belongs to the
4911 innermost enclosing non-class scope, but if it is
4912 subsequently referenced, its name is not found by name
4913 lookup until a matching declaration is provided in the
4914 innermost enclosing nonclass scope.
4916 So just keep looking for a non-hidden binding.
4918 gcc_assert (TREE_CODE (binding) == TYPE_DECL);
4919 continue;
4921 val = binding;
4922 break;
4926 /* Now lookup in namespace scopes. */
4927 if (!val)
4928 val = unqualified_namespace_lookup (name, flags);
4930 /* If we have a single function from a using decl, pull it out. */
4931 if (val && TREE_CODE (val) == OVERLOAD && !really_overloaded_fn (val))
4932 val = OVL_FUNCTION (val);
4934 return val;
4937 /* Wrapper for lookup_name_real_1. */
4939 tree
4940 lookup_name_real (tree name, int prefer_type, int nonclass, bool block_p,
4941 int namespaces_only, int flags)
4943 tree ret;
4944 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4945 ret = lookup_name_real_1 (name, prefer_type, nonclass, block_p,
4946 namespaces_only, flags);
4947 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4948 return ret;
4951 tree
4952 lookup_name_nonclass (tree name)
4954 return lookup_name_real (name, 0, 1, /*block_p=*/true, 0, 0);
4957 tree
4958 lookup_function_nonclass (tree name, vec<tree, va_gc> *args, bool block_p)
4960 return
4961 lookup_arg_dependent (name,
4962 lookup_name_real (name, 0, 1, block_p, 0, 0),
4963 args);
4966 tree
4967 lookup_name (tree name)
4969 return lookup_name_real (name, 0, 0, /*block_p=*/true, 0, 0);
4972 tree
4973 lookup_name_prefer_type (tree name, int prefer_type)
4975 return lookup_name_real (name, prefer_type, 0, /*block_p=*/true, 0, 0);
4978 /* Look up NAME for type used in elaborated name specifier in
4979 the scopes given by SCOPE. SCOPE can be either TS_CURRENT or
4980 TS_WITHIN_ENCLOSING_NON_CLASS. Although not implied by the
4981 name, more scopes are checked if cleanup or template parameter
4982 scope is encountered.
4984 Unlike lookup_name_real, we make sure that NAME is actually
4985 declared in the desired scope, not from inheritance, nor using
4986 directive. For using declaration, there is DR138 still waiting
4987 to be resolved. Hidden name coming from an earlier friend
4988 declaration is also returned.
4990 A TYPE_DECL best matching the NAME is returned. Catching error
4991 and issuing diagnostics are caller's responsibility. */
4993 static tree
4994 lookup_type_scope_1 (tree name, tag_scope scope)
4996 cxx_binding *iter = NULL;
4997 tree val = NULL_TREE;
4999 /* Look in non-namespace scope first. */
5000 if (current_binding_level->kind != sk_namespace)
5001 iter = outer_binding (name, NULL, /*class_p=*/ true);
5002 for (; iter; iter = outer_binding (name, iter, /*class_p=*/ true))
5004 /* Check if this is the kind of thing we're looking for.
5005 If SCOPE is TS_CURRENT, also make sure it doesn't come from
5006 base class. For ITER->VALUE, we can simply use
5007 INHERITED_VALUE_BINDING_P. For ITER->TYPE, we have to use
5008 our own check.
5010 We check ITER->TYPE before ITER->VALUE in order to handle
5011 typedef struct C {} C;
5012 correctly. */
5014 if (qualify_lookup (iter->type, LOOKUP_PREFER_TYPES)
5015 && (scope != ts_current
5016 || LOCAL_BINDING_P (iter)
5017 || DECL_CONTEXT (iter->type) == iter->scope->this_entity))
5018 val = iter->type;
5019 else if ((scope != ts_current
5020 || !INHERITED_VALUE_BINDING_P (iter))
5021 && qualify_lookup (iter->value, LOOKUP_PREFER_TYPES))
5022 val = iter->value;
5024 if (val)
5025 break;
5028 /* Look in namespace scope. */
5029 if (!val)
5031 iter = cp_binding_level_find_binding_for_name
5032 (NAMESPACE_LEVEL (current_decl_namespace ()), name);
5034 if (iter)
5036 /* If this is the kind of thing we're looking for, we're done. */
5037 if (qualify_lookup (iter->type, LOOKUP_PREFER_TYPES))
5038 val = iter->type;
5039 else if (qualify_lookup (iter->value, LOOKUP_PREFER_TYPES))
5040 val = iter->value;
5045 /* Type found, check if it is in the allowed scopes, ignoring cleanup
5046 and template parameter scopes. */
5047 if (val)
5049 cp_binding_level *b = current_binding_level;
5050 while (b)
5052 if (iter->scope == b)
5053 return val;
5055 if (b->kind == sk_cleanup || b->kind == sk_template_parms
5056 || b->kind == sk_function_parms)
5057 b = b->level_chain;
5058 else if (b->kind == sk_class
5059 && scope == ts_within_enclosing_non_class)
5060 b = b->level_chain;
5061 else
5062 break;
5066 return NULL_TREE;
5069 /* Wrapper for lookup_type_scope_1. */
5071 tree
5072 lookup_type_scope (tree name, tag_scope scope)
5074 tree ret;
5075 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
5076 ret = lookup_type_scope_1 (name, scope);
5077 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
5078 return ret;
5082 /* Similar to `lookup_name' but look only in the innermost non-class
5083 binding level. */
5085 static tree
5086 lookup_name_innermost_nonclass_level_1 (tree name)
5088 cp_binding_level *b;
5089 tree t = NULL_TREE;
5091 b = innermost_nonclass_level ();
5093 if (b->kind == sk_namespace)
5095 t = IDENTIFIER_NAMESPACE_VALUE (name);
5097 /* extern "C" function() */
5098 if (t != NULL_TREE && TREE_CODE (t) == TREE_LIST)
5099 t = TREE_VALUE (t);
5101 else if (IDENTIFIER_BINDING (name)
5102 && LOCAL_BINDING_P (IDENTIFIER_BINDING (name)))
5104 cxx_binding *binding;
5105 binding = IDENTIFIER_BINDING (name);
5106 while (1)
5108 if (binding->scope == b
5109 && !(VAR_P (binding->value)
5110 && DECL_DEAD_FOR_LOCAL (binding->value)))
5111 return binding->value;
5113 if (b->kind == sk_cleanup)
5114 b = b->level_chain;
5115 else
5116 break;
5120 return t;
5123 /* Wrapper for lookup_name_innermost_nonclass_level_1. */
5125 tree
5126 lookup_name_innermost_nonclass_level (tree name)
5128 tree ret;
5129 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
5130 ret = lookup_name_innermost_nonclass_level_1 (name);
5131 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
5132 return ret;
5136 /* Returns true iff DECL is a block-scope extern declaration of a function
5137 or variable. */
5139 bool
5140 is_local_extern (tree decl)
5142 cxx_binding *binding;
5144 /* For functions, this is easy. */
5145 if (TREE_CODE (decl) == FUNCTION_DECL)
5146 return DECL_LOCAL_FUNCTION_P (decl);
5148 if (!VAR_P (decl))
5149 return false;
5150 if (!current_function_decl)
5151 return false;
5153 /* For variables, this is not easy. We need to look at the binding stack
5154 for the identifier to see whether the decl we have is a local. */
5155 for (binding = IDENTIFIER_BINDING (DECL_NAME (decl));
5156 binding && binding->scope->kind != sk_namespace;
5157 binding = binding->previous)
5158 if (binding->value == decl)
5159 return LOCAL_BINDING_P (binding);
5161 return false;
5164 /* Like lookup_name_innermost_nonclass_level, but for types. */
5166 static tree
5167 lookup_type_current_level (tree name)
5169 tree t = NULL_TREE;
5171 timevar_start (TV_NAME_LOOKUP);
5172 gcc_assert (current_binding_level->kind != sk_namespace);
5174 if (REAL_IDENTIFIER_TYPE_VALUE (name) != NULL_TREE
5175 && REAL_IDENTIFIER_TYPE_VALUE (name) != global_type_node)
5177 cp_binding_level *b = current_binding_level;
5178 while (1)
5180 if (purpose_member (name, b->type_shadowed))
5182 t = REAL_IDENTIFIER_TYPE_VALUE (name);
5183 break;
5185 if (b->kind == sk_cleanup)
5186 b = b->level_chain;
5187 else
5188 break;
5192 timevar_stop (TV_NAME_LOOKUP);
5193 return t;
5196 /* [basic.lookup.koenig] */
5197 /* A nonzero return value in the functions below indicates an error. */
5199 struct arg_lookup
5201 tree name;
5202 vec<tree, va_gc> *args;
5203 vec<tree, va_gc> *namespaces;
5204 vec<tree, va_gc> *classes;
5205 tree functions;
5206 hash_set<tree> *fn_set;
5209 static bool arg_assoc (struct arg_lookup*, tree);
5210 static bool arg_assoc_args (struct arg_lookup*, tree);
5211 static bool arg_assoc_args_vec (struct arg_lookup*, vec<tree, va_gc> *);
5212 static bool arg_assoc_type (struct arg_lookup*, tree);
5213 static bool add_function (struct arg_lookup *, tree);
5214 static bool arg_assoc_namespace (struct arg_lookup *, tree);
5215 static bool arg_assoc_class_only (struct arg_lookup *, tree);
5216 static bool arg_assoc_bases (struct arg_lookup *, tree);
5217 static bool arg_assoc_class (struct arg_lookup *, tree);
5218 static bool arg_assoc_template_arg (struct arg_lookup*, tree);
5220 /* Add a function to the lookup structure.
5221 Returns true on error. */
5223 static bool
5224 add_function (struct arg_lookup *k, tree fn)
5226 if (!is_overloaded_fn (fn))
5227 /* All names except those of (possibly overloaded) functions and
5228 function templates are ignored. */;
5229 else if (k->fn_set && k->fn_set->add (fn))
5230 /* It's already in the list. */;
5231 else if (!k->functions)
5232 k->functions = fn;
5233 else if (fn == k->functions)
5235 else
5237 k->functions = build_overload (fn, k->functions);
5238 if (TREE_CODE (k->functions) == OVERLOAD)
5239 OVL_ARG_DEPENDENT (k->functions) = true;
5242 return false;
5245 /* Returns true iff CURRENT has declared itself to be an associated
5246 namespace of SCOPE via a strong using-directive (or transitive chain
5247 thereof). Both are namespaces. */
5249 bool
5250 is_associated_namespace (tree current, tree scope)
5252 vec<tree, va_gc> *seen = make_tree_vector ();
5253 vec<tree, va_gc> *todo = make_tree_vector ();
5254 tree t;
5255 bool ret;
5257 while (1)
5259 if (scope == current)
5261 ret = true;
5262 break;
5264 vec_safe_push (seen, scope);
5265 for (t = DECL_NAMESPACE_ASSOCIATIONS (scope); t; t = TREE_CHAIN (t))
5266 if (!vec_member (TREE_PURPOSE (t), seen))
5267 vec_safe_push (todo, TREE_PURPOSE (t));
5268 if (!todo->is_empty ())
5270 scope = todo->last ();
5271 todo->pop ();
5273 else
5275 ret = false;
5276 break;
5280 release_tree_vector (seen);
5281 release_tree_vector (todo);
5283 return ret;
5286 /* Add functions of a namespace to the lookup structure.
5287 Returns true on error. */
5289 static bool
5290 arg_assoc_namespace (struct arg_lookup *k, tree scope)
5292 tree value;
5294 if (vec_member (scope, k->namespaces))
5295 return false;
5296 vec_safe_push (k->namespaces, scope);
5298 /* Check out our super-users. */
5299 for (value = DECL_NAMESPACE_ASSOCIATIONS (scope); value;
5300 value = TREE_CHAIN (value))
5301 if (arg_assoc_namespace (k, TREE_PURPOSE (value)))
5302 return true;
5304 /* Also look down into inline namespaces. */
5305 for (value = DECL_NAMESPACE_USING (scope); value;
5306 value = TREE_CHAIN (value))
5307 if (is_associated_namespace (scope, TREE_PURPOSE (value)))
5308 if (arg_assoc_namespace (k, TREE_PURPOSE (value)))
5309 return true;
5311 value = namespace_binding (k->name, scope);
5312 if (!value)
5313 return false;
5315 for (; value; value = OVL_NEXT (value))
5317 /* We don't want to find arbitrary hidden functions via argument
5318 dependent lookup. We only want to find friends of associated
5319 classes, which we'll do via arg_assoc_class. */
5320 if (hidden_name_p (OVL_CURRENT (value)))
5321 continue;
5323 if (add_function (k, OVL_CURRENT (value)))
5324 return true;
5327 return false;
5330 /* Adds everything associated with a template argument to the lookup
5331 structure. Returns true on error. */
5333 static bool
5334 arg_assoc_template_arg (struct arg_lookup *k, tree arg)
5336 /* [basic.lookup.koenig]
5338 If T is a template-id, its associated namespaces and classes are
5339 ... the namespaces and classes associated with the types of the
5340 template arguments provided for template type parameters
5341 (excluding template template parameters); the namespaces in which
5342 any template template arguments are defined; and the classes in
5343 which any member templates used as template template arguments
5344 are defined. [Note: non-type template arguments do not
5345 contribute to the set of associated namespaces. ] */
5347 /* Consider first template template arguments. */
5348 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
5349 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
5350 return false;
5351 else if (TREE_CODE (arg) == TEMPLATE_DECL)
5353 tree ctx = CP_DECL_CONTEXT (arg);
5355 /* It's not a member template. */
5356 if (TREE_CODE (ctx) == NAMESPACE_DECL)
5357 return arg_assoc_namespace (k, ctx);
5358 /* Otherwise, it must be member template. */
5359 else
5360 return arg_assoc_class_only (k, ctx);
5362 /* It's an argument pack; handle it recursively. */
5363 else if (ARGUMENT_PACK_P (arg))
5365 tree args = ARGUMENT_PACK_ARGS (arg);
5366 int i, len = TREE_VEC_LENGTH (args);
5367 for (i = 0; i < len; ++i)
5368 if (arg_assoc_template_arg (k, TREE_VEC_ELT (args, i)))
5369 return true;
5371 return false;
5373 /* It's not a template template argument, but it is a type template
5374 argument. */
5375 else if (TYPE_P (arg))
5376 return arg_assoc_type (k, arg);
5377 /* It's a non-type template argument. */
5378 else
5379 return false;
5382 /* Adds the class and its friends to the lookup structure.
5383 Returns true on error. */
5385 static bool
5386 arg_assoc_class_only (struct arg_lookup *k, tree type)
5388 tree list, friends, context;
5390 /* Backend-built structures, such as __builtin_va_list, aren't
5391 affected by all this. */
5392 if (!CLASS_TYPE_P (type))
5393 return false;
5395 context = decl_namespace_context (type);
5396 if (arg_assoc_namespace (k, context))
5397 return true;
5399 complete_type (type);
5401 /* Process friends. */
5402 for (list = DECL_FRIENDLIST (TYPE_MAIN_DECL (type)); list;
5403 list = TREE_CHAIN (list))
5404 if (k->name == FRIEND_NAME (list))
5405 for (friends = FRIEND_DECLS (list); friends;
5406 friends = TREE_CHAIN (friends))
5408 tree fn = TREE_VALUE (friends);
5410 /* Only interested in global functions with potentially hidden
5411 (i.e. unqualified) declarations. */
5412 if (CP_DECL_CONTEXT (fn) != context)
5413 continue;
5414 /* Template specializations are never found by name lookup.
5415 (Templates themselves can be found, but not template
5416 specializations.) */
5417 if (TREE_CODE (fn) == FUNCTION_DECL && DECL_USE_TEMPLATE (fn))
5418 continue;
5419 if (add_function (k, fn))
5420 return true;
5423 return false;
5426 /* Adds the class and its bases to the lookup structure.
5427 Returns true on error. */
5429 static bool
5430 arg_assoc_bases (struct arg_lookup *k, tree type)
5432 if (arg_assoc_class_only (k, type))
5433 return true;
5435 if (TYPE_BINFO (type))
5437 /* Process baseclasses. */
5438 tree binfo, base_binfo;
5439 int i;
5441 for (binfo = TYPE_BINFO (type), i = 0;
5442 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
5443 if (arg_assoc_bases (k, BINFO_TYPE (base_binfo)))
5444 return true;
5447 return false;
5450 /* Adds everything associated with a class argument type to the lookup
5451 structure. Returns true on error.
5453 If T is a class type (including unions), its associated classes are: the
5454 class itself; the class of which it is a member, if any; and its direct
5455 and indirect base classes. Its associated namespaces are the namespaces
5456 of which its associated classes are members. Furthermore, if T is a
5457 class template specialization, its associated namespaces and classes
5458 also include: the namespaces and classes associated with the types of
5459 the template arguments provided for template type parameters (excluding
5460 template template parameters); the namespaces of which any template
5461 template arguments are members; and the classes of which any member
5462 templates used as template template arguments are members. [ Note:
5463 non-type template arguments do not contribute to the set of associated
5464 namespaces. --end note] */
5466 static bool
5467 arg_assoc_class (struct arg_lookup *k, tree type)
5469 tree list;
5470 int i;
5472 /* Backend build structures, such as __builtin_va_list, aren't
5473 affected by all this. */
5474 if (!CLASS_TYPE_P (type))
5475 return false;
5477 if (vec_member (type, k->classes))
5478 return false;
5479 vec_safe_push (k->classes, type);
5481 if (TYPE_CLASS_SCOPE_P (type)
5482 && arg_assoc_class_only (k, TYPE_CONTEXT (type)))
5483 return true;
5485 if (arg_assoc_bases (k, type))
5486 return true;
5488 /* Process template arguments. */
5489 if (CLASSTYPE_TEMPLATE_INFO (type)
5490 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
5492 list = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
5493 for (i = 0; i < TREE_VEC_LENGTH (list); ++i)
5494 if (arg_assoc_template_arg (k, TREE_VEC_ELT (list, i)))
5495 return true;
5498 return false;
5501 /* Adds everything associated with a given type.
5502 Returns 1 on error. */
5504 static bool
5505 arg_assoc_type (struct arg_lookup *k, tree type)
5507 /* As we do not get the type of non-type dependent expressions
5508 right, we can end up with such things without a type. */
5509 if (!type)
5510 return false;
5512 if (TYPE_PTRDATAMEM_P (type))
5514 /* Pointer to member: associate class type and value type. */
5515 if (arg_assoc_type (k, TYPE_PTRMEM_CLASS_TYPE (type)))
5516 return true;
5517 return arg_assoc_type (k, TYPE_PTRMEM_POINTED_TO_TYPE (type));
5519 else switch (TREE_CODE (type))
5521 case ERROR_MARK:
5522 return false;
5523 case VOID_TYPE:
5524 case INTEGER_TYPE:
5525 case REAL_TYPE:
5526 case COMPLEX_TYPE:
5527 case VECTOR_TYPE:
5528 case BOOLEAN_TYPE:
5529 case FIXED_POINT_TYPE:
5530 case DECLTYPE_TYPE:
5531 case NULLPTR_TYPE:
5532 return false;
5533 case RECORD_TYPE:
5534 if (TYPE_PTRMEMFUNC_P (type))
5535 return arg_assoc_type (k, TYPE_PTRMEMFUNC_FN_TYPE (type));
5536 case UNION_TYPE:
5537 return arg_assoc_class (k, type);
5538 case POINTER_TYPE:
5539 case REFERENCE_TYPE:
5540 case ARRAY_TYPE:
5541 return arg_assoc_type (k, TREE_TYPE (type));
5542 case ENUMERAL_TYPE:
5543 if (TYPE_CLASS_SCOPE_P (type)
5544 && arg_assoc_class_only (k, TYPE_CONTEXT (type)))
5545 return true;
5546 return arg_assoc_namespace (k, decl_namespace_context (type));
5547 case METHOD_TYPE:
5548 /* The basetype is referenced in the first arg type, so just
5549 fall through. */
5550 case FUNCTION_TYPE:
5551 /* Associate the parameter types. */
5552 if (arg_assoc_args (k, TYPE_ARG_TYPES (type)))
5553 return true;
5554 /* Associate the return type. */
5555 return arg_assoc_type (k, TREE_TYPE (type));
5556 case TEMPLATE_TYPE_PARM:
5557 case BOUND_TEMPLATE_TEMPLATE_PARM:
5558 return false;
5559 case TYPENAME_TYPE:
5560 return false;
5561 case LANG_TYPE:
5562 gcc_assert (type == unknown_type_node
5563 || type == init_list_type_node);
5564 return false;
5565 case TYPE_PACK_EXPANSION:
5566 return arg_assoc_type (k, PACK_EXPANSION_PATTERN (type));
5568 default:
5569 gcc_unreachable ();
5571 return false;
5574 /* Adds everything associated with arguments. Returns true on error. */
5576 static bool
5577 arg_assoc_args (struct arg_lookup *k, tree args)
5579 for (; args; args = TREE_CHAIN (args))
5580 if (arg_assoc (k, TREE_VALUE (args)))
5581 return true;
5582 return false;
5585 /* Adds everything associated with an argument vector. Returns true
5586 on error. */
5588 static bool
5589 arg_assoc_args_vec (struct arg_lookup *k, vec<tree, va_gc> *args)
5591 unsigned int ix;
5592 tree arg;
5594 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
5595 if (arg_assoc (k, arg))
5596 return true;
5597 return false;
5600 /* Adds everything associated with a given tree_node. Returns 1 on error. */
5602 static bool
5603 arg_assoc (struct arg_lookup *k, tree n)
5605 if (n == error_mark_node)
5606 return false;
5608 if (TYPE_P (n))
5609 return arg_assoc_type (k, n);
5611 if (! type_unknown_p (n))
5612 return arg_assoc_type (k, TREE_TYPE (n));
5614 if (TREE_CODE (n) == ADDR_EXPR)
5615 n = TREE_OPERAND (n, 0);
5616 if (TREE_CODE (n) == COMPONENT_REF)
5617 n = TREE_OPERAND (n, 1);
5618 if (TREE_CODE (n) == OFFSET_REF)
5619 n = TREE_OPERAND (n, 1);
5620 while (TREE_CODE (n) == TREE_LIST)
5621 n = TREE_VALUE (n);
5622 if (BASELINK_P (n))
5623 n = BASELINK_FUNCTIONS (n);
5625 if (TREE_CODE (n) == FUNCTION_DECL)
5626 return arg_assoc_type (k, TREE_TYPE (n));
5627 if (TREE_CODE (n) == TEMPLATE_ID_EXPR)
5629 /* The working paper doesn't currently say how to handle template-id
5630 arguments. The sensible thing would seem to be to handle the list
5631 of template candidates like a normal overload set, and handle the
5632 template arguments like we do for class template
5633 specializations. */
5634 tree templ = TREE_OPERAND (n, 0);
5635 tree args = TREE_OPERAND (n, 1);
5636 int ix;
5638 /* First the templates. */
5639 if (arg_assoc (k, templ))
5640 return true;
5642 /* Now the arguments. */
5643 if (args)
5644 for (ix = TREE_VEC_LENGTH (args); ix--;)
5645 if (arg_assoc_template_arg (k, TREE_VEC_ELT (args, ix)) == 1)
5646 return true;
5648 else if (TREE_CODE (n) == OVERLOAD)
5650 for (; n; n = OVL_NEXT (n))
5651 if (arg_assoc_type (k, TREE_TYPE (OVL_CURRENT (n))))
5652 return true;
5655 return false;
5658 /* Performs Koenig lookup depending on arguments, where fns
5659 are the functions found in normal lookup. */
5661 static tree
5662 lookup_arg_dependent_1 (tree name, tree fns, vec<tree, va_gc> *args)
5664 struct arg_lookup k;
5666 /* Remove any hidden friend functions from the list of functions
5667 found so far. They will be added back by arg_assoc_class as
5668 appropriate. */
5669 fns = remove_hidden_names (fns);
5671 k.name = name;
5672 k.args = args;
5673 k.functions = fns;
5674 k.classes = make_tree_vector ();
5676 /* We previously performed an optimization here by setting
5677 NAMESPACES to the current namespace when it was safe. However, DR
5678 164 says that namespaces that were already searched in the first
5679 stage of template processing are searched again (potentially
5680 picking up later definitions) in the second stage. */
5681 k.namespaces = make_tree_vector ();
5683 /* We used to allow duplicates and let joust discard them, but
5684 since the above change for DR 164 we end up with duplicates of
5685 all the functions found by unqualified lookup. So keep track
5686 of which ones we've seen. */
5687 if (fns)
5689 tree ovl;
5690 /* We shouldn't be here if lookup found something other than
5691 namespace-scope functions. */
5692 gcc_assert (DECL_NAMESPACE_SCOPE_P (OVL_CURRENT (fns)));
5693 k.fn_set = new hash_set<tree>;
5694 for (ovl = fns; ovl; ovl = OVL_NEXT (ovl))
5695 k.fn_set->add (OVL_CURRENT (ovl));
5697 else
5698 k.fn_set = NULL;
5700 arg_assoc_args_vec (&k, args);
5702 fns = k.functions;
5704 if (fns
5705 && !VAR_P (fns)
5706 && !is_overloaded_fn (fns))
5708 error ("argument dependent lookup finds %q+D", fns);
5709 error (" in call to %qD", name);
5710 fns = error_mark_node;
5713 release_tree_vector (k.classes);
5714 release_tree_vector (k.namespaces);
5715 delete k.fn_set;
5717 return fns;
5720 /* Wrapper for lookup_arg_dependent_1. */
5722 tree
5723 lookup_arg_dependent (tree name, tree fns, vec<tree, va_gc> *args)
5725 tree ret;
5726 bool subtime;
5727 subtime = timevar_cond_start (TV_NAME_LOOKUP);
5728 ret = lookup_arg_dependent_1 (name, fns, args);
5729 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
5730 return ret;
5734 /* Add namespace to using_directives. Return NULL_TREE if nothing was
5735 changed (i.e. there was already a directive), or the fresh
5736 TREE_LIST otherwise. */
5738 static tree
5739 push_using_directive_1 (tree used)
5741 tree ud = current_binding_level->using_directives;
5742 tree iter, ancestor;
5744 /* Check if we already have this. */
5745 if (purpose_member (used, ud) != NULL_TREE)
5746 return NULL_TREE;
5748 ancestor = namespace_ancestor (current_decl_namespace (), used);
5749 ud = current_binding_level->using_directives;
5750 ud = tree_cons (used, ancestor, ud);
5751 current_binding_level->using_directives = ud;
5753 /* Recursively add all namespaces used. */
5754 for (iter = DECL_NAMESPACE_USING (used); iter; iter = TREE_CHAIN (iter))
5755 push_using_directive (TREE_PURPOSE (iter));
5757 return ud;
5760 /* Wrapper for push_using_directive_1. */
5762 static tree
5763 push_using_directive (tree used)
5765 tree ret;
5766 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
5767 ret = push_using_directive_1 (used);
5768 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
5769 return ret;
5772 /* The type TYPE is being declared. If it is a class template, or a
5773 specialization of a class template, do any processing required and
5774 perform error-checking. If IS_FRIEND is nonzero, this TYPE is
5775 being declared a friend. B is the binding level at which this TYPE
5776 should be bound.
5778 Returns the TYPE_DECL for TYPE, which may have been altered by this
5779 processing. */
5781 static tree
5782 maybe_process_template_type_declaration (tree type, int is_friend,
5783 cp_binding_level *b)
5785 tree decl = TYPE_NAME (type);
5787 if (processing_template_parmlist)
5788 /* You can't declare a new template type in a template parameter
5789 list. But, you can declare a non-template type:
5791 template <class A*> struct S;
5793 is a forward-declaration of `A'. */
5795 else if (b->kind == sk_namespace
5796 && current_binding_level->kind != sk_namespace)
5797 /* If this new type is being injected into a containing scope,
5798 then it's not a template type. */
5800 else
5802 gcc_assert (MAYBE_CLASS_TYPE_P (type)
5803 || TREE_CODE (type) == ENUMERAL_TYPE);
5805 if (processing_template_decl)
5807 /* This may change after the call to
5808 push_template_decl_real, but we want the original value. */
5809 tree name = DECL_NAME (decl);
5811 decl = push_template_decl_real (decl, is_friend);
5812 if (decl == error_mark_node)
5813 return error_mark_node;
5815 /* If the current binding level is the binding level for the
5816 template parameters (see the comment in
5817 begin_template_parm_list) and the enclosing level is a class
5818 scope, and we're not looking at a friend, push the
5819 declaration of the member class into the class scope. In the
5820 friend case, push_template_decl will already have put the
5821 friend into global scope, if appropriate. */
5822 if (TREE_CODE (type) != ENUMERAL_TYPE
5823 && !is_friend && b->kind == sk_template_parms
5824 && b->level_chain->kind == sk_class)
5826 finish_member_declaration (CLASSTYPE_TI_TEMPLATE (type));
5828 if (!COMPLETE_TYPE_P (current_class_type))
5830 maybe_add_class_template_decl_list (current_class_type,
5831 type, /*friend_p=*/0);
5832 /* Put this UTD in the table of UTDs for the class. */
5833 if (CLASSTYPE_NESTED_UTDS (current_class_type) == NULL)
5834 CLASSTYPE_NESTED_UTDS (current_class_type) =
5835 binding_table_new (SCOPE_DEFAULT_HT_SIZE);
5837 binding_table_insert
5838 (CLASSTYPE_NESTED_UTDS (current_class_type), name, type);
5844 return decl;
5847 /* Push a tag name NAME for struct/class/union/enum type TYPE. In case
5848 that the NAME is a class template, the tag is processed but not pushed.
5850 The pushed scope depend on the SCOPE parameter:
5851 - When SCOPE is TS_CURRENT, put it into the inner-most non-sk_cleanup
5852 scope.
5853 - When SCOPE is TS_GLOBAL, put it in the inner-most non-class and
5854 non-template-parameter scope. This case is needed for forward
5855 declarations.
5856 - When SCOPE is TS_WITHIN_ENCLOSING_NON_CLASS, this is similar to
5857 TS_GLOBAL case except that names within template-parameter scopes
5858 are not pushed at all.
5860 Returns TYPE upon success and ERROR_MARK_NODE otherwise. */
5862 static tree
5863 pushtag_1 (tree name, tree type, tag_scope scope)
5865 cp_binding_level *b;
5866 tree decl;
5868 b = current_binding_level;
5869 while (/* Cleanup scopes are not scopes from the point of view of
5870 the language. */
5871 b->kind == sk_cleanup
5872 /* Neither are function parameter scopes. */
5873 || b->kind == sk_function_parms
5874 /* Neither are the scopes used to hold template parameters
5875 for an explicit specialization. For an ordinary template
5876 declaration, these scopes are not scopes from the point of
5877 view of the language. */
5878 || (b->kind == sk_template_parms
5879 && (b->explicit_spec_p || scope == ts_global))
5880 || (b->kind == sk_class
5881 && (scope != ts_current
5882 /* We may be defining a new type in the initializer
5883 of a static member variable. We allow this when
5884 not pedantic, and it is particularly useful for
5885 type punning via an anonymous union. */
5886 || COMPLETE_TYPE_P (b->this_entity))))
5887 b = b->level_chain;
5889 gcc_assert (identifier_p (name));
5891 /* Do C++ gratuitous typedefing. */
5892 if (identifier_type_value_1 (name) != type)
5894 tree tdef;
5895 int in_class = 0;
5896 tree context = TYPE_CONTEXT (type);
5898 if (! context)
5900 tree cs = current_scope ();
5902 if (scope == ts_current
5903 || (cs && TREE_CODE (cs) == FUNCTION_DECL))
5904 context = cs;
5905 else if (cs != NULL_TREE && TYPE_P (cs))
5906 /* When declaring a friend class of a local class, we want
5907 to inject the newly named class into the scope
5908 containing the local class, not the namespace
5909 scope. */
5910 context = decl_function_context (get_type_decl (cs));
5912 if (!context)
5913 context = current_namespace;
5915 if (b->kind == sk_class
5916 || (b->kind == sk_template_parms
5917 && b->level_chain->kind == sk_class))
5918 in_class = 1;
5920 if (current_lang_name == lang_name_java)
5921 TYPE_FOR_JAVA (type) = 1;
5923 tdef = create_implicit_typedef (name, type);
5924 DECL_CONTEXT (tdef) = FROB_CONTEXT (context);
5925 if (scope == ts_within_enclosing_non_class)
5927 /* This is a friend. Make this TYPE_DECL node hidden from
5928 ordinary name lookup. Its corresponding TEMPLATE_DECL
5929 will be marked in push_template_decl_real. */
5930 retrofit_lang_decl (tdef);
5931 DECL_ANTICIPATED (tdef) = 1;
5932 DECL_FRIEND_P (tdef) = 1;
5935 decl = maybe_process_template_type_declaration
5936 (type, scope == ts_within_enclosing_non_class, b);
5937 if (decl == error_mark_node)
5938 return decl;
5940 if (b->kind == sk_class)
5942 if (!TYPE_BEING_DEFINED (current_class_type))
5943 return error_mark_node;
5945 if (!PROCESSING_REAL_TEMPLATE_DECL_P ())
5946 /* Put this TYPE_DECL on the TYPE_FIELDS list for the
5947 class. But if it's a member template class, we want
5948 the TEMPLATE_DECL, not the TYPE_DECL, so this is done
5949 later. */
5950 finish_member_declaration (decl);
5951 else
5952 pushdecl_class_level (decl);
5954 else if (b->kind != sk_template_parms)
5956 decl = pushdecl_with_scope_1 (decl, b, /*is_friend=*/false);
5957 if (decl == error_mark_node)
5958 return decl;
5961 if (! in_class)
5962 set_identifier_type_value_with_scope (name, tdef, b);
5964 TYPE_CONTEXT (type) = DECL_CONTEXT (decl);
5966 /* If this is a local class, keep track of it. We need this
5967 information for name-mangling, and so that it is possible to
5968 find all function definitions in a translation unit in a
5969 convenient way. (It's otherwise tricky to find a member
5970 function definition it's only pointed to from within a local
5971 class.) */
5972 if (TYPE_FUNCTION_SCOPE_P (type))
5974 if (processing_template_decl)
5976 /* Push a DECL_EXPR so we call pushtag at the right time in
5977 template instantiation rather than in some nested context. */
5978 add_decl_expr (decl);
5980 else
5981 vec_safe_push (local_classes, type);
5984 if (b->kind == sk_class
5985 && !COMPLETE_TYPE_P (current_class_type))
5987 maybe_add_class_template_decl_list (current_class_type,
5988 type, /*friend_p=*/0);
5990 if (CLASSTYPE_NESTED_UTDS (current_class_type) == NULL)
5991 CLASSTYPE_NESTED_UTDS (current_class_type)
5992 = binding_table_new (SCOPE_DEFAULT_HT_SIZE);
5994 binding_table_insert
5995 (CLASSTYPE_NESTED_UTDS (current_class_type), name, type);
5998 decl = TYPE_NAME (type);
5999 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
6001 /* Set type visibility now if this is a forward declaration. */
6002 TREE_PUBLIC (decl) = 1;
6003 determine_visibility (decl);
6005 return type;
6008 /* Wrapper for pushtag_1. */
6010 tree
6011 pushtag (tree name, tree type, tag_scope scope)
6013 tree ret;
6014 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6015 ret = pushtag_1 (name, type, scope);
6016 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6017 return ret;
6020 /* Subroutines for reverting temporarily to top-level for instantiation
6021 of templates and such. We actually need to clear out the class- and
6022 local-value slots of all identifiers, so that only the global values
6023 are at all visible. Simply setting current_binding_level to the global
6024 scope isn't enough, because more binding levels may be pushed. */
6025 struct saved_scope *scope_chain;
6027 /* Return true if ID has not already been marked. */
6029 static inline bool
6030 store_binding_p (tree id)
6032 if (!id || !IDENTIFIER_BINDING (id))
6033 return false;
6035 if (IDENTIFIER_MARKED (id))
6036 return false;
6038 return true;
6041 /* Add an appropriate binding to *OLD_BINDINGS which needs to already
6042 have enough space reserved. */
6044 static void
6045 store_binding (tree id, vec<cxx_saved_binding, va_gc> **old_bindings)
6047 cxx_saved_binding saved;
6049 gcc_checking_assert (store_binding_p (id));
6051 IDENTIFIER_MARKED (id) = 1;
6053 saved.identifier = id;
6054 saved.binding = IDENTIFIER_BINDING (id);
6055 saved.real_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
6056 (*old_bindings)->quick_push (saved);
6057 IDENTIFIER_BINDING (id) = NULL;
6060 static void
6061 store_bindings (tree names, vec<cxx_saved_binding, va_gc> **old_bindings)
6063 static vec<tree> bindings_need_stored = vNULL;
6064 tree t, id;
6065 size_t i;
6067 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6068 for (t = names; t; t = TREE_CHAIN (t))
6070 if (TREE_CODE (t) == TREE_LIST)
6071 id = TREE_PURPOSE (t);
6072 else
6073 id = DECL_NAME (t);
6075 if (store_binding_p (id))
6076 bindings_need_stored.safe_push (id);
6078 if (!bindings_need_stored.is_empty ())
6080 vec_safe_reserve_exact (*old_bindings, bindings_need_stored.length ());
6081 for (i = 0; bindings_need_stored.iterate (i, &id); ++i)
6083 /* We can appearantly have duplicates in NAMES. */
6084 if (store_binding_p (id))
6085 store_binding (id, old_bindings);
6087 bindings_need_stored.truncate (0);
6089 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6092 /* Like store_bindings, but NAMES is a vector of cp_class_binding
6093 objects, rather than a TREE_LIST. */
6095 static void
6096 store_class_bindings (vec<cp_class_binding, va_gc> *names,
6097 vec<cxx_saved_binding, va_gc> **old_bindings)
6099 static vec<tree> bindings_need_stored = vNULL;
6100 size_t i;
6101 cp_class_binding *cb;
6103 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6104 for (i = 0; vec_safe_iterate (names, i, &cb); ++i)
6105 if (store_binding_p (cb->identifier))
6106 bindings_need_stored.safe_push (cb->identifier);
6107 if (!bindings_need_stored.is_empty ())
6109 tree id;
6110 vec_safe_reserve_exact (*old_bindings, bindings_need_stored.length ());
6111 for (i = 0; bindings_need_stored.iterate (i, &id); ++i)
6112 store_binding (id, old_bindings);
6113 bindings_need_stored.truncate (0);
6115 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6118 void
6119 push_to_top_level (void)
6121 struct saved_scope *s;
6122 cp_binding_level *b;
6123 cxx_saved_binding *sb;
6124 size_t i;
6125 bool need_pop;
6127 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6128 s = ggc_cleared_alloc<saved_scope> ();
6130 b = scope_chain ? current_binding_level : 0;
6132 /* If we're in the middle of some function, save our state. */
6133 if (cfun)
6135 need_pop = true;
6136 push_function_context ();
6138 else
6139 need_pop = false;
6141 if (scope_chain && previous_class_level)
6142 store_class_bindings (previous_class_level->class_shadowed,
6143 &s->old_bindings);
6145 /* Have to include the global scope, because class-scope decls
6146 aren't listed anywhere useful. */
6147 for (; b; b = b->level_chain)
6149 tree t;
6151 /* Template IDs are inserted into the global level. If they were
6152 inserted into namespace level, finish_file wouldn't find them
6153 when doing pending instantiations. Therefore, don't stop at
6154 namespace level, but continue until :: . */
6155 if (global_scope_p (b))
6156 break;
6158 store_bindings (b->names, &s->old_bindings);
6159 /* We also need to check class_shadowed to save class-level type
6160 bindings, since pushclass doesn't fill in b->names. */
6161 if (b->kind == sk_class)
6162 store_class_bindings (b->class_shadowed, &s->old_bindings);
6164 /* Unwind type-value slots back to top level. */
6165 for (t = b->type_shadowed; t; t = TREE_CHAIN (t))
6166 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (t), TREE_VALUE (t));
6169 FOR_EACH_VEC_SAFE_ELT (s->old_bindings, i, sb)
6170 IDENTIFIER_MARKED (sb->identifier) = 0;
6172 s->prev = scope_chain;
6173 s->bindings = b;
6174 s->need_pop_function_context = need_pop;
6175 s->function_decl = current_function_decl;
6176 s->unevaluated_operand = cp_unevaluated_operand;
6177 s->inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
6178 s->x_stmt_tree.stmts_are_full_exprs_p = true;
6180 scope_chain = s;
6181 current_function_decl = NULL_TREE;
6182 vec_alloc (current_lang_base, 10);
6183 current_lang_name = lang_name_cplusplus;
6184 current_namespace = global_namespace;
6185 push_class_stack ();
6186 cp_unevaluated_operand = 0;
6187 c_inhibit_evaluation_warnings = 0;
6188 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6191 static void
6192 pop_from_top_level_1 (void)
6194 struct saved_scope *s = scope_chain;
6195 cxx_saved_binding *saved;
6196 size_t i;
6198 /* Clear out class-level bindings cache. */
6199 if (previous_class_level)
6200 invalidate_class_lookup_cache ();
6201 pop_class_stack ();
6203 current_lang_base = 0;
6205 scope_chain = s->prev;
6206 FOR_EACH_VEC_SAFE_ELT (s->old_bindings, i, saved)
6208 tree id = saved->identifier;
6210 IDENTIFIER_BINDING (id) = saved->binding;
6211 SET_IDENTIFIER_TYPE_VALUE (id, saved->real_type_value);
6214 /* If we were in the middle of compiling a function, restore our
6215 state. */
6216 if (s->need_pop_function_context)
6217 pop_function_context ();
6218 current_function_decl = s->function_decl;
6219 cp_unevaluated_operand = s->unevaluated_operand;
6220 c_inhibit_evaluation_warnings = s->inhibit_evaluation_warnings;
6223 /* Wrapper for pop_from_top_level_1. */
6225 void
6226 pop_from_top_level (void)
6228 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6229 pop_from_top_level_1 ();
6230 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6234 /* Pop off extraneous binding levels left over due to syntax errors.
6236 We don't pop past namespaces, as they might be valid. */
6238 void
6239 pop_everything (void)
6241 if (ENABLE_SCOPE_CHECKING)
6242 verbatim ("XXX entering pop_everything ()\n");
6243 while (!toplevel_bindings_p ())
6245 if (current_binding_level->kind == sk_class)
6246 pop_nested_class ();
6247 else
6248 poplevel (0, 0, 0);
6250 if (ENABLE_SCOPE_CHECKING)
6251 verbatim ("XXX leaving pop_everything ()\n");
6254 /* Emit debugging information for using declarations and directives.
6255 If input tree is overloaded fn then emit debug info for all
6256 candidates. */
6258 void
6259 cp_emit_debug_info_for_using (tree t, tree context)
6261 /* Don't try to emit any debug information if we have errors. */
6262 if (seen_error ())
6263 return;
6265 /* Ignore this FUNCTION_DECL if it refers to a builtin declaration
6266 of a builtin function. */
6267 if (TREE_CODE (t) == FUNCTION_DECL
6268 && DECL_EXTERNAL (t)
6269 && DECL_BUILT_IN (t))
6270 return;
6272 /* Do not supply context to imported_module_or_decl, if
6273 it is a global namespace. */
6274 if (context == global_namespace)
6275 context = NULL_TREE;
6277 if (BASELINK_P (t))
6278 t = BASELINK_FUNCTIONS (t);
6280 /* FIXME: Handle TEMPLATE_DECLs. */
6281 for (t = OVL_CURRENT (t); t; t = OVL_NEXT (t))
6282 if (TREE_CODE (t) != TEMPLATE_DECL)
6284 if (building_stmt_list_p ())
6285 add_stmt (build_stmt (input_location, USING_STMT, t));
6286 else
6287 (*debug_hooks->imported_module_or_decl) (t, NULL_TREE, context, false);
6291 #include "gt-cp-name-lookup.h"