/cp
[official-gcc.git] / gcc / cp / name-lookup.c
blob53f14f3eee618eca2158cf9a55816f3ec28553a2
1 /* Definitions for C++ name lookup routines.
2 Copyright (C) 2003-2014 Free Software Foundation, Inc.
3 Contributed by Gabriel Dos Reis <gdr@integrable-solutions.net>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "flags.h"
26 #include "tree.h"
27 #include "stringpool.h"
28 #include "print-tree.h"
29 #include "attribs.h"
30 #include "cp-tree.h"
31 #include "name-lookup.h"
32 #include "timevar.h"
33 #include "diagnostic-core.h"
34 #include "intl.h"
35 #include "debug.h"
36 #include "c-family/c-pragma.h"
37 #include "params.h"
38 #include "pointer-set.h"
40 /* The bindings for a particular name in a particular scope. */
42 struct scope_binding {
43 tree value;
44 tree type;
46 #define EMPTY_SCOPE_BINDING { NULL_TREE, NULL_TREE }
48 static cp_binding_level *innermost_nonclass_level (void);
49 static cxx_binding *binding_for_name (cp_binding_level *, tree);
50 static tree push_overloaded_decl (tree, int, bool);
51 static bool lookup_using_namespace (tree, struct scope_binding *, tree,
52 tree, int);
53 static bool qualified_lookup_using_namespace (tree, tree,
54 struct scope_binding *, int);
55 static tree lookup_type_current_level (tree);
56 static tree push_using_directive (tree);
57 static tree lookup_extern_c_fun_in_all_ns (tree);
58 static void diagnose_name_conflict (tree, tree);
60 /* The :: namespace. */
62 tree global_namespace;
64 /* The name of the anonymous namespace, throughout this translation
65 unit. */
66 static GTY(()) tree anonymous_namespace_name;
68 /* Initialize anonymous_namespace_name if necessary, and return it. */
70 static tree
71 get_anonymous_namespace_name (void)
73 if (!anonymous_namespace_name)
75 /* We used to use get_file_function_name here, but that isn't
76 necessary now that anonymous namespace typeinfos
77 are !TREE_PUBLIC, and thus compared by address. */
78 /* The demangler expects anonymous namespaces to be called
79 something starting with '_GLOBAL__N_'. */
80 anonymous_namespace_name = get_identifier ("_GLOBAL__N_1");
82 return anonymous_namespace_name;
85 /* Compute the chain index of a binding_entry given the HASH value of its
86 name and the total COUNT of chains. COUNT is assumed to be a power
87 of 2. */
89 #define ENTRY_INDEX(HASH, COUNT) (((HASH) >> 3) & ((COUNT) - 1))
91 /* A free list of "binding_entry"s awaiting for re-use. */
93 static GTY((deletable)) binding_entry free_binding_entry = NULL;
95 /* Create a binding_entry object for (NAME, TYPE). */
97 static inline binding_entry
98 binding_entry_make (tree name, tree type)
100 binding_entry entry;
102 if (free_binding_entry)
104 entry = free_binding_entry;
105 free_binding_entry = entry->chain;
107 else
108 entry = ggc_alloc_binding_entry_s ();
110 entry->name = name;
111 entry->type = type;
112 entry->chain = NULL;
114 return entry;
117 /* Put ENTRY back on the free list. */
118 #if 0
119 static inline void
120 binding_entry_free (binding_entry entry)
122 entry->name = NULL;
123 entry->type = NULL;
124 entry->chain = free_binding_entry;
125 free_binding_entry = entry;
127 #endif
129 /* The datatype used to implement the mapping from names to types at
130 a given scope. */
131 struct GTY(()) binding_table_s {
132 /* Array of chains of "binding_entry"s */
133 binding_entry * GTY((length ("%h.chain_count"))) chain;
135 /* The number of chains in this table. This is the length of the
136 member "chain" considered as an array. */
137 size_t chain_count;
139 /* Number of "binding_entry"s in this table. */
140 size_t entry_count;
143 /* Construct TABLE with an initial CHAIN_COUNT. */
145 static inline void
146 binding_table_construct (binding_table table, size_t chain_count)
148 table->chain_count = chain_count;
149 table->entry_count = 0;
150 table->chain = ggc_alloc_cleared_vec_binding_entry (table->chain_count);
153 /* Make TABLE's entries ready for reuse. */
154 #if 0
155 static void
156 binding_table_free (binding_table table)
158 size_t i;
159 size_t count;
161 if (table == NULL)
162 return;
164 for (i = 0, count = table->chain_count; i < count; ++i)
166 binding_entry temp = table->chain[i];
167 while (temp != NULL)
169 binding_entry entry = temp;
170 temp = entry->chain;
171 binding_entry_free (entry);
173 table->chain[i] = NULL;
175 table->entry_count = 0;
177 #endif
179 /* Allocate a table with CHAIN_COUNT, assumed to be a power of two. */
181 static inline binding_table
182 binding_table_new (size_t chain_count)
184 binding_table table = ggc_alloc_binding_table_s ();
185 table->chain = NULL;
186 binding_table_construct (table, chain_count);
187 return table;
190 /* Expand TABLE to twice its current chain_count. */
192 static void
193 binding_table_expand (binding_table table)
195 const size_t old_chain_count = table->chain_count;
196 const size_t old_entry_count = table->entry_count;
197 const size_t new_chain_count = 2 * old_chain_count;
198 binding_entry *old_chains = table->chain;
199 size_t i;
201 binding_table_construct (table, new_chain_count);
202 for (i = 0; i < old_chain_count; ++i)
204 binding_entry entry = old_chains[i];
205 for (; entry != NULL; entry = old_chains[i])
207 const unsigned int hash = IDENTIFIER_HASH_VALUE (entry->name);
208 const size_t j = ENTRY_INDEX (hash, new_chain_count);
210 old_chains[i] = entry->chain;
211 entry->chain = table->chain[j];
212 table->chain[j] = entry;
215 table->entry_count = old_entry_count;
218 /* Insert a binding for NAME to TYPE into TABLE. */
220 static void
221 binding_table_insert (binding_table table, tree name, tree type)
223 const unsigned int hash = IDENTIFIER_HASH_VALUE (name);
224 const size_t i = ENTRY_INDEX (hash, table->chain_count);
225 binding_entry entry = binding_entry_make (name, type);
227 entry->chain = table->chain[i];
228 table->chain[i] = entry;
229 ++table->entry_count;
231 if (3 * table->chain_count < 5 * table->entry_count)
232 binding_table_expand (table);
235 /* Return the binding_entry, if any, that maps NAME. */
237 binding_entry
238 binding_table_find (binding_table table, tree name)
240 const unsigned int hash = IDENTIFIER_HASH_VALUE (name);
241 binding_entry entry = table->chain[ENTRY_INDEX (hash, table->chain_count)];
243 while (entry != NULL && entry->name != name)
244 entry = entry->chain;
246 return entry;
249 /* Apply PROC -- with DATA -- to all entries in TABLE. */
251 void
252 binding_table_foreach (binding_table table, bt_foreach_proc proc, void *data)
254 size_t chain_count;
255 size_t i;
257 if (!table)
258 return;
260 chain_count = table->chain_count;
261 for (i = 0; i < chain_count; ++i)
263 binding_entry entry = table->chain[i];
264 for (; entry != NULL; entry = entry->chain)
265 proc (entry, data);
269 #ifndef ENABLE_SCOPE_CHECKING
270 # define ENABLE_SCOPE_CHECKING 0
271 #else
272 # define ENABLE_SCOPE_CHECKING 1
273 #endif
275 /* A free list of "cxx_binding"s, connected by their PREVIOUS. */
277 static GTY((deletable)) cxx_binding *free_bindings;
279 /* Initialize VALUE and TYPE field for BINDING, and set the PREVIOUS
280 field to NULL. */
282 static inline void
283 cxx_binding_init (cxx_binding *binding, tree value, tree type)
285 binding->value = value;
286 binding->type = type;
287 binding->previous = NULL;
290 /* (GC)-allocate a binding object with VALUE and TYPE member initialized. */
292 static cxx_binding *
293 cxx_binding_make (tree value, tree type)
295 cxx_binding *binding;
296 if (free_bindings)
298 binding = free_bindings;
299 free_bindings = binding->previous;
301 else
302 binding = ggc_alloc_cxx_binding ();
304 cxx_binding_init (binding, value, type);
306 return binding;
309 /* Put BINDING back on the free list. */
311 static inline void
312 cxx_binding_free (cxx_binding *binding)
314 binding->scope = NULL;
315 binding->previous = free_bindings;
316 free_bindings = binding;
319 /* Create a new binding for NAME (with the indicated VALUE and TYPE
320 bindings) in the class scope indicated by SCOPE. */
322 static cxx_binding *
323 new_class_binding (tree name, tree value, tree type, cp_binding_level *scope)
325 cp_class_binding cb = {cxx_binding_make (value, type), name};
326 cxx_binding *binding = cb.base;
327 vec_safe_push (scope->class_shadowed, cb);
328 binding->scope = scope;
329 return binding;
332 /* Make DECL the innermost binding for ID. The LEVEL is the binding
333 level at which this declaration is being bound. */
335 static void
336 push_binding (tree id, tree decl, cp_binding_level* level)
338 cxx_binding *binding;
340 if (level != class_binding_level)
342 binding = cxx_binding_make (decl, NULL_TREE);
343 binding->scope = level;
345 else
346 binding = new_class_binding (id, decl, /*type=*/NULL_TREE, level);
348 /* Now, fill in the binding information. */
349 binding->previous = IDENTIFIER_BINDING (id);
350 INHERITED_VALUE_BINDING_P (binding) = 0;
351 LOCAL_BINDING_P (binding) = (level != class_binding_level);
353 /* And put it on the front of the list of bindings for ID. */
354 IDENTIFIER_BINDING (id) = binding;
357 /* Remove the binding for DECL which should be the innermost binding
358 for ID. */
360 void
361 pop_binding (tree id, tree decl)
363 cxx_binding *binding;
365 if (id == NULL_TREE)
366 /* It's easiest to write the loops that call this function without
367 checking whether or not the entities involved have names. We
368 get here for such an entity. */
369 return;
371 /* Get the innermost binding for ID. */
372 binding = IDENTIFIER_BINDING (id);
374 /* The name should be bound. */
375 gcc_assert (binding != NULL);
377 /* The DECL will be either the ordinary binding or the type
378 binding for this identifier. Remove that binding. */
379 if (binding->value == decl)
380 binding->value = NULL_TREE;
381 else
383 gcc_assert (binding->type == decl);
384 binding->type = NULL_TREE;
387 if (!binding->value && !binding->type)
389 /* We're completely done with the innermost binding for this
390 identifier. Unhook it from the list of bindings. */
391 IDENTIFIER_BINDING (id) = binding->previous;
393 /* Add it to the free list. */
394 cxx_binding_free (binding);
398 /* Remove the bindings for the decls of the current level and leave
399 the current scope. */
401 void
402 pop_bindings_and_leave_scope (void)
404 for (tree t = getdecls (); t; t = DECL_CHAIN (t))
405 pop_binding (DECL_NAME (t), t);
406 leave_scope ();
409 /* Strip non dependent using declarations. 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
566 diagnose_name_conflict (decl, bval);
567 ok = false;
570 return ok;
573 /* Diagnose a name conflict between DECL and BVAL. */
575 static void
576 diagnose_name_conflict (tree decl, tree bval)
578 if (TREE_CODE (decl) == TREE_CODE (bval)
579 && (TREE_CODE (decl) != TYPE_DECL
580 || (DECL_ARTIFICIAL (decl) && DECL_ARTIFICIAL (bval))
581 || (!DECL_ARTIFICIAL (decl) && !DECL_ARTIFICIAL (bval)))
582 && !is_overloaded_fn (decl))
583 error ("redeclaration of %q#D", decl);
584 else
585 error ("%q#D conflicts with a previous declaration", decl);
587 inform (input_location, "previous declaration %q+#D", bval);
590 /* Wrapper for supplement_binding_1. */
592 static bool
593 supplement_binding (cxx_binding *binding, tree decl)
595 bool ret;
596 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
597 ret = supplement_binding_1 (binding, decl);
598 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
599 return ret;
602 /* Add DECL to the list of things declared in B. */
604 static void
605 add_decl_to_level (tree decl, cp_binding_level *b)
607 /* We used to record virtual tables as if they were ordinary
608 variables, but no longer do so. */
609 gcc_assert (!(VAR_P (decl) && DECL_VIRTUAL_P (decl)));
611 if (TREE_CODE (decl) == NAMESPACE_DECL
612 && !DECL_NAMESPACE_ALIAS (decl))
614 DECL_CHAIN (decl) = b->namespaces;
615 b->namespaces = decl;
617 else
619 /* We build up the list in reverse order, and reverse it later if
620 necessary. */
621 TREE_CHAIN (decl) = b->names;
622 b->names = decl;
624 /* If appropriate, add decl to separate list of statics. We
625 include extern variables because they might turn out to be
626 static later. It's OK for this list to contain a few false
627 positives. */
628 if (b->kind == sk_namespace)
629 if ((VAR_P (decl)
630 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
631 || (TREE_CODE (decl) == FUNCTION_DECL
632 && (!TREE_PUBLIC (decl)
633 || decl_anon_ns_mem_p (decl)
634 || DECL_DECLARED_INLINE_P (decl))))
635 vec_safe_push (b->static_decls, decl);
639 /* Record a decl-node X as belonging to the current lexical scope.
640 Check for errors (such as an incompatible declaration for the same
641 name already seen in the same scope). IS_FRIEND is true if X is
642 declared as a friend.
644 Returns either X or an old decl for the same name.
645 If an old decl is returned, it may have been smashed
646 to agree with what X says. */
648 static tree
649 pushdecl_maybe_friend_1 (tree x, bool is_friend)
651 tree t;
652 tree name;
653 int need_new_binding;
655 if (x == error_mark_node)
656 return error_mark_node;
658 need_new_binding = 1;
660 if (DECL_TEMPLATE_PARM_P (x))
661 /* Template parameters have no context; they are not X::T even
662 when declared within a class or namespace. */
664 else
666 if (current_function_decl && x != current_function_decl
667 /* A local declaration for a function doesn't constitute
668 nesting. */
669 && TREE_CODE (x) != FUNCTION_DECL
670 /* A local declaration for an `extern' variable is in the
671 scope of the current namespace, not the current
672 function. */
673 && !(VAR_P (x) && DECL_EXTERNAL (x))
674 /* When parsing the parameter list of a function declarator,
675 don't set DECL_CONTEXT to an enclosing function. When we
676 push the PARM_DECLs in order to process the function body,
677 current_binding_level->this_entity will be set. */
678 && !(TREE_CODE (x) == PARM_DECL
679 && current_binding_level->kind == sk_function_parms
680 && current_binding_level->this_entity == NULL)
681 && !DECL_CONTEXT (x))
682 DECL_CONTEXT (x) = current_function_decl;
684 /* If this is the declaration for a namespace-scope function,
685 but the declaration itself is in a local scope, mark the
686 declaration. */
687 if (TREE_CODE (x) == FUNCTION_DECL
688 && DECL_NAMESPACE_SCOPE_P (x)
689 && current_function_decl
690 && x != current_function_decl)
691 DECL_LOCAL_FUNCTION_P (x) = 1;
694 name = DECL_NAME (x);
695 if (name)
697 int different_binding_level = 0;
699 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
700 name = TREE_OPERAND (name, 0);
702 /* In case this decl was explicitly namespace-qualified, look it
703 up in its namespace context. */
704 if (DECL_NAMESPACE_SCOPE_P (x) && namespace_bindings_p ())
705 t = namespace_binding (name, DECL_CONTEXT (x));
706 else
707 t = lookup_name_innermost_nonclass_level (name);
709 /* [basic.link] If there is a visible declaration of an entity
710 with linkage having the same name and type, ignoring entities
711 declared outside the innermost enclosing namespace scope, the
712 block scope declaration declares that same entity and
713 receives the linkage of the previous declaration. */
714 if (! t && current_function_decl && x != current_function_decl
715 && VAR_OR_FUNCTION_DECL_P (x)
716 && DECL_EXTERNAL (x))
718 /* Look in block scope. */
719 t = innermost_non_namespace_value (name);
720 /* Or in the innermost namespace. */
721 if (! t)
722 t = namespace_binding (name, DECL_CONTEXT (x));
723 /* Does it have linkage? Note that if this isn't a DECL, it's an
724 OVERLOAD, which is OK. */
725 if (t && DECL_P (t) && ! (TREE_STATIC (t) || DECL_EXTERNAL (t)))
726 t = NULL_TREE;
727 if (t)
728 different_binding_level = 1;
731 /* If we are declaring a function, and the result of name-lookup
732 was an OVERLOAD, look for an overloaded instance that is
733 actually the same as the function we are declaring. (If
734 there is one, we have to merge our declaration with the
735 previous declaration.) */
736 if (t && TREE_CODE (t) == OVERLOAD)
738 tree match;
740 if (TREE_CODE (x) == FUNCTION_DECL)
741 for (match = t; match; match = OVL_NEXT (match))
743 if (decls_match (OVL_CURRENT (match), x))
744 break;
746 else
747 /* Just choose one. */
748 match = t;
750 if (match)
751 t = OVL_CURRENT (match);
752 else
753 t = NULL_TREE;
756 if (t && t != error_mark_node)
758 if (different_binding_level)
760 if (decls_match (x, t))
761 /* The standard only says that the local extern
762 inherits linkage from the previous decl; in
763 particular, default args are not shared. Add
764 the decl into a hash table to make sure only
765 the previous decl in this case is seen by the
766 middle end. */
768 struct cxx_int_tree_map *h;
769 void **loc;
771 TREE_PUBLIC (x) = TREE_PUBLIC (t);
773 if (cp_function_chain->extern_decl_map == NULL)
774 cp_function_chain->extern_decl_map
775 = htab_create_ggc (20, cxx_int_tree_map_hash,
776 cxx_int_tree_map_eq, NULL);
778 h = ggc_alloc_cxx_int_tree_map ();
779 h->uid = DECL_UID (x);
780 h->to = t;
781 loc = htab_find_slot_with_hash
782 (cp_function_chain->extern_decl_map, h,
783 h->uid, INSERT);
784 *(struct cxx_int_tree_map **) loc = h;
787 else if (TREE_CODE (t) == PARM_DECL)
789 /* Check for duplicate params. */
790 tree d = duplicate_decls (x, t, is_friend);
791 if (d)
792 return d;
794 else if ((DECL_EXTERN_C_FUNCTION_P (x)
795 || DECL_FUNCTION_TEMPLATE_P (x))
796 && is_overloaded_fn (t))
797 /* Don't do anything just yet. */;
798 else if (t == wchar_decl_node)
800 if (! DECL_IN_SYSTEM_HEADER (x))
801 pedwarn (input_location, OPT_Wpedantic, "redeclaration of %<wchar_t%> as %qT",
802 TREE_TYPE (x));
804 /* Throw away the redeclaration. */
805 return t;
807 else
809 tree olddecl = duplicate_decls (x, t, is_friend);
811 /* If the redeclaration failed, we can stop at this
812 point. */
813 if (olddecl == error_mark_node)
814 return error_mark_node;
816 if (olddecl)
818 if (TREE_CODE (t) == TYPE_DECL)
819 SET_IDENTIFIER_TYPE_VALUE (name, TREE_TYPE (t));
821 return t;
823 else if (DECL_MAIN_P (x) && TREE_CODE (t) == FUNCTION_DECL)
825 /* A redeclaration of main, but not a duplicate of the
826 previous one.
828 [basic.start.main]
830 This function shall not be overloaded. */
831 error ("invalid redeclaration of %q+D", t);
832 error ("as %qD", x);
833 /* We don't try to push this declaration since that
834 causes a crash. */
835 return x;
840 /* If x has C linkage-specification, (extern "C"),
841 lookup its binding, in case it's already bound to an object.
842 The lookup is done in all namespaces.
843 If we find an existing binding, make sure it has the same
844 exception specification as x, otherwise, bail in error [7.5, 7.6]. */
845 if ((TREE_CODE (x) == FUNCTION_DECL)
846 && DECL_EXTERN_C_P (x)
847 /* We should ignore declarations happening in system headers. */
848 && !DECL_ARTIFICIAL (x)
849 && !DECL_IN_SYSTEM_HEADER (x))
851 tree previous = lookup_extern_c_fun_in_all_ns (x);
852 if (previous
853 && !DECL_ARTIFICIAL (previous)
854 && !DECL_IN_SYSTEM_HEADER (previous)
855 && DECL_CONTEXT (previous) != DECL_CONTEXT (x))
857 /* In case either x or previous is declared to throw an exception,
858 make sure both exception specifications are equal. */
859 if (decls_match (x, previous))
861 tree x_exception_spec = NULL_TREE;
862 tree previous_exception_spec = NULL_TREE;
864 x_exception_spec =
865 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (x));
866 previous_exception_spec =
867 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (previous));
868 if (!comp_except_specs (previous_exception_spec,
869 x_exception_spec,
870 ce_normal))
872 pedwarn (input_location, 0,
873 "declaration of %q#D with C language linkage",
875 pedwarn (input_location, 0,
876 "conflicts with previous declaration %q+#D",
877 previous);
878 pedwarn (input_location, 0,
879 "due to different exception specifications");
880 return error_mark_node;
882 if (DECL_ASSEMBLER_NAME_SET_P (previous))
883 SET_DECL_ASSEMBLER_NAME (x,
884 DECL_ASSEMBLER_NAME (previous));
886 else
888 pedwarn (input_location, 0,
889 "declaration of %q#D with C language linkage", x);
890 pedwarn (input_location, 0,
891 "conflicts with previous declaration %q+#D",
892 previous);
897 check_template_shadow (x);
899 /* If this is a function conjured up by the back end, massage it
900 so it looks friendly. */
901 if (DECL_NON_THUNK_FUNCTION_P (x) && ! DECL_LANG_SPECIFIC (x))
903 retrofit_lang_decl (x);
904 SET_DECL_LANGUAGE (x, lang_c);
907 t = x;
908 if (DECL_NON_THUNK_FUNCTION_P (x) && ! DECL_FUNCTION_MEMBER_P (x))
910 t = push_overloaded_decl (x, PUSH_LOCAL, is_friend);
911 if (!namespace_bindings_p ())
912 /* We do not need to create a binding for this name;
913 push_overloaded_decl will have already done so if
914 necessary. */
915 need_new_binding = 0;
917 else if (DECL_FUNCTION_TEMPLATE_P (x) && DECL_NAMESPACE_SCOPE_P (x))
919 t = push_overloaded_decl (x, PUSH_GLOBAL, is_friend);
920 if (t == x)
921 add_decl_to_level (x, NAMESPACE_LEVEL (CP_DECL_CONTEXT (t)));
924 if (DECL_DECLARES_FUNCTION_P (t))
925 check_default_args (t);
927 if (t != x || DECL_FUNCTION_TEMPLATE_P (t))
928 return t;
930 /* If declaring a type as a typedef, copy the type (unless we're
931 at line 0), and install this TYPE_DECL as the new type's typedef
932 name. See the extensive comment of set_underlying_type (). */
933 if (TREE_CODE (x) == TYPE_DECL)
935 tree type = TREE_TYPE (x);
937 if (DECL_IS_BUILTIN (x)
938 || (TREE_TYPE (x) != error_mark_node
939 && TYPE_NAME (type) != x
940 /* We don't want to copy the type when all we're
941 doing is making a TYPE_DECL for the purposes of
942 inlining. */
943 && (!TYPE_NAME (type)
944 || TYPE_NAME (type) != DECL_ABSTRACT_ORIGIN (x))))
945 set_underlying_type (x);
947 if (type != error_mark_node
948 && TYPE_NAME (type)
949 && TYPE_IDENTIFIER (type))
950 set_identifier_type_value (DECL_NAME (x), x);
952 /* If this is a locally defined typedef in a function that
953 is not a template instantation, record it to implement
954 -Wunused-local-typedefs. */
955 if (current_instantiation () == NULL
956 || (current_instantiation ()->decl != current_function_decl))
957 record_locally_defined_typedef (x);
960 /* Multiple external decls of the same identifier ought to match.
962 We get warnings about inline functions where they are defined.
963 We get warnings about other functions from push_overloaded_decl.
965 Avoid duplicate warnings where they are used. */
966 if (TREE_PUBLIC (x) && TREE_CODE (x) != FUNCTION_DECL)
968 tree decl;
970 decl = IDENTIFIER_NAMESPACE_VALUE (name);
971 if (decl && TREE_CODE (decl) == OVERLOAD)
972 decl = OVL_FUNCTION (decl);
974 if (decl && decl != error_mark_node
975 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl))
976 /* If different sort of thing, we already gave an error. */
977 && TREE_CODE (decl) == TREE_CODE (x)
978 && !same_type_p (TREE_TYPE (x), TREE_TYPE (decl)))
980 if (permerror (input_location, "type mismatch with previous "
981 "external decl of %q#D", x))
982 inform (input_location, "previous external decl of %q+#D",
983 decl);
987 if (TREE_CODE (x) == FUNCTION_DECL
988 && is_friend
989 && !flag_friend_injection)
991 /* This is a new declaration of a friend function, so hide
992 it from ordinary function lookup. */
993 DECL_ANTICIPATED (x) = 1;
994 DECL_HIDDEN_FRIEND_P (x) = 1;
997 /* This name is new in its binding level.
998 Install the new declaration and return it. */
999 if (namespace_bindings_p ())
1001 /* Install a global value. */
1003 /* If the first global decl has external linkage,
1004 warn if we later see static one. */
1005 if (IDENTIFIER_GLOBAL_VALUE (name) == NULL_TREE && TREE_PUBLIC (x))
1006 TREE_PUBLIC (name) = 1;
1008 /* Bind the name for the entity. */
1009 if (!(TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x)
1010 && t != NULL_TREE)
1011 && (TREE_CODE (x) == TYPE_DECL
1012 || VAR_P (x)
1013 || TREE_CODE (x) == NAMESPACE_DECL
1014 || TREE_CODE (x) == CONST_DECL
1015 || TREE_CODE (x) == TEMPLATE_DECL))
1016 SET_IDENTIFIER_NAMESPACE_VALUE (name, x);
1018 /* If new decl is `static' and an `extern' was seen previously,
1019 warn about it. */
1020 if (x != NULL_TREE && t != NULL_TREE && decls_match (x, t))
1021 warn_extern_redeclared_static (x, t);
1023 else
1025 /* Here to install a non-global value. */
1026 tree oldglobal = IDENTIFIER_NAMESPACE_VALUE (name);
1027 tree oldlocal = NULL_TREE;
1028 cp_binding_level *oldscope = NULL;
1029 cxx_binding *oldbinding = outer_binding (name, NULL, true);
1030 if (oldbinding)
1032 oldlocal = oldbinding->value;
1033 oldscope = oldbinding->scope;
1036 if (need_new_binding)
1038 push_local_binding (name, x, 0);
1039 /* Because push_local_binding will hook X on to the
1040 current_binding_level's name list, we don't want to
1041 do that again below. */
1042 need_new_binding = 0;
1045 /* If this is a TYPE_DECL, push it into the type value slot. */
1046 if (TREE_CODE (x) == TYPE_DECL)
1047 set_identifier_type_value (name, x);
1049 /* Clear out any TYPE_DECL shadowed by a namespace so that
1050 we won't think this is a type. The C struct hack doesn't
1051 go through namespaces. */
1052 if (TREE_CODE (x) == NAMESPACE_DECL)
1053 set_identifier_type_value (name, NULL_TREE);
1055 if (oldlocal)
1057 tree d = oldlocal;
1059 while (oldlocal
1060 && VAR_P (oldlocal)
1061 && DECL_DEAD_FOR_LOCAL (oldlocal))
1062 oldlocal = DECL_SHADOWED_FOR_VAR (oldlocal);
1064 if (oldlocal == NULL_TREE)
1065 oldlocal = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (d));
1068 /* If this is an extern function declaration, see if we
1069 have a global definition or declaration for the function. */
1070 if (oldlocal == NULL_TREE
1071 && DECL_EXTERNAL (x)
1072 && oldglobal != NULL_TREE
1073 && TREE_CODE (x) == FUNCTION_DECL
1074 && TREE_CODE (oldglobal) == FUNCTION_DECL)
1076 /* We have one. Their types must agree. */
1077 if (decls_match (x, oldglobal))
1078 /* OK */;
1079 else
1081 warning (0, "extern declaration of %q#D doesn%'t match", x);
1082 warning (0, "global declaration %q+#D", oldglobal);
1085 /* If we have a local external declaration,
1086 and no file-scope declaration has yet been seen,
1087 then if we later have a file-scope decl it must not be static. */
1088 if (oldlocal == NULL_TREE
1089 && oldglobal == NULL_TREE
1090 && DECL_EXTERNAL (x)
1091 && TREE_PUBLIC (x))
1092 TREE_PUBLIC (name) = 1;
1094 /* Don't complain about the parms we push and then pop
1095 while tentatively parsing a function declarator. */
1096 if (TREE_CODE (x) == PARM_DECL && DECL_CONTEXT (x) == NULL_TREE)
1097 /* Ignore. */;
1099 /* Warn if shadowing an argument at the top level of the body. */
1100 else if (oldlocal != NULL_TREE && !DECL_EXTERNAL (x)
1101 /* Inline decls shadow nothing. */
1102 && !DECL_FROM_INLINE (x)
1103 && (TREE_CODE (oldlocal) == PARM_DECL
1104 || VAR_P (oldlocal)
1105 /* If the old decl is a type decl, only warn if the
1106 old decl is an explicit typedef or if both the old
1107 and new decls are type decls. */
1108 || (TREE_CODE (oldlocal) == TYPE_DECL
1109 && (!DECL_ARTIFICIAL (oldlocal)
1110 || TREE_CODE (x) == TYPE_DECL)))
1111 /* Don't check for internally generated vars unless
1112 it's an implicit typedef (see create_implicit_typedef
1113 in decl.c). */
1114 && (!DECL_ARTIFICIAL (x) || DECL_IMPLICIT_TYPEDEF_P (x)))
1116 bool nowarn = false;
1118 /* Don't complain if it's from an enclosing function. */
1119 if (DECL_CONTEXT (oldlocal) == current_function_decl
1120 && TREE_CODE (x) != PARM_DECL
1121 && TREE_CODE (oldlocal) == PARM_DECL)
1123 /* Go to where the parms should be and see if we find
1124 them there. */
1125 cp_binding_level *b = current_binding_level->level_chain;
1127 if (FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
1128 /* Skip the ctor/dtor cleanup level. */
1129 b = b->level_chain;
1131 /* ARM $8.3 */
1132 if (b->kind == sk_function_parms)
1134 error ("declaration of %q#D shadows a parameter", x);
1135 nowarn = true;
1139 /* The local structure or class can't use parameters of
1140 the containing function anyway. */
1141 if (DECL_CONTEXT (oldlocal) != current_function_decl)
1143 cp_binding_level *scope = current_binding_level;
1144 tree context = DECL_CONTEXT (oldlocal);
1145 for (; scope; scope = scope->level_chain)
1147 if (scope->kind == sk_function_parms
1148 && scope->this_entity == context)
1149 break;
1150 if (scope->kind == sk_class
1151 && !LAMBDA_TYPE_P (scope->this_entity))
1153 nowarn = true;
1154 break;
1158 /* Error if redeclaring a local declared in a
1159 for-init-statement or in the condition of an if or
1160 switch statement when the new declaration is in the
1161 outermost block of the controlled statement.
1162 Redeclaring a variable from a for or while condition is
1163 detected elsewhere. */
1164 else if (VAR_P (oldlocal)
1165 && oldscope == current_binding_level->level_chain
1166 && (oldscope->kind == sk_cond
1167 || oldscope->kind == sk_for))
1169 error ("redeclaration of %q#D", x);
1170 inform (input_location, "%q+#D previously declared here",
1171 oldlocal);
1172 nowarn = true;
1174 /* C++11:
1175 3.3.3/3: The name declared in an exception-declaration (...)
1176 shall not be redeclared in the outermost block of the handler.
1177 3.3.3/2: A parameter name shall not be redeclared (...) in
1178 the outermost block of any handler associated with a
1179 function-try-block.
1180 3.4.1/15: The function parameter names shall not be redeclared
1181 in the exception-declaration nor in the outermost block of a
1182 handler for the function-try-block. */
1183 else if ((VAR_P (oldlocal)
1184 && oldscope == current_binding_level->level_chain
1185 && oldscope->kind == sk_catch)
1186 || (TREE_CODE (oldlocal) == PARM_DECL
1187 && (current_binding_level->kind == sk_catch
1188 || (current_binding_level->level_chain->kind
1189 == sk_catch))
1190 && in_function_try_handler))
1192 if (permerror (input_location, "redeclaration of %q#D", x))
1193 inform (input_location, "%q+#D previously declared here",
1194 oldlocal);
1195 nowarn = true;
1198 if (warn_shadow && !nowarn)
1200 bool warned;
1202 if (TREE_CODE (oldlocal) == PARM_DECL)
1203 warned = warning_at (input_location, OPT_Wshadow,
1204 "declaration of %q#D shadows a parameter", x);
1205 else if (is_capture_proxy (oldlocal))
1206 warned = warning_at (input_location, OPT_Wshadow,
1207 "declaration of %qD shadows a lambda capture",
1209 else
1210 warned = warning_at (input_location, OPT_Wshadow,
1211 "declaration of %qD shadows a previous local",
1214 if (warned)
1215 inform (DECL_SOURCE_LOCATION (oldlocal),
1216 "shadowed declaration is here");
1220 /* Maybe warn if shadowing something else. */
1221 else if (warn_shadow && !DECL_EXTERNAL (x)
1222 /* No shadow warnings for internally generated vars unless
1223 it's an implicit typedef (see create_implicit_typedef
1224 in decl.c). */
1225 && (! DECL_ARTIFICIAL (x) || DECL_IMPLICIT_TYPEDEF_P (x))
1226 /* No shadow warnings for vars made for inlining. */
1227 && ! DECL_FROM_INLINE (x))
1229 tree member;
1231 if (nonlambda_method_basetype ())
1232 member = lookup_member (current_nonlambda_class_type (),
1233 name,
1234 /*protect=*/0,
1235 /*want_type=*/false,
1236 tf_warning_or_error);
1237 else
1238 member = NULL_TREE;
1240 if (member && !TREE_STATIC (member))
1242 /* Location of previous decl is not useful in this case. */
1243 warning (OPT_Wshadow, "declaration of %qD shadows a member of 'this'",
1246 else if (oldglobal != NULL_TREE
1247 && (VAR_P (oldglobal)
1248 /* If the old decl is a type decl, only warn if the
1249 old decl is an explicit typedef or if both the
1250 old and new decls are type decls. */
1251 || (TREE_CODE (oldglobal) == TYPE_DECL
1252 && (!DECL_ARTIFICIAL (oldglobal)
1253 || TREE_CODE (x) == TYPE_DECL))))
1254 /* XXX shadow warnings in outer-more namespaces */
1256 if (warning_at (input_location, OPT_Wshadow,
1257 "declaration of %qD shadows a "
1258 "global declaration", x))
1259 inform (DECL_SOURCE_LOCATION (oldglobal),
1260 "shadowed declaration is here");
1265 if (VAR_P (x))
1266 maybe_register_incomplete_var (x);
1269 if (need_new_binding)
1270 add_decl_to_level (x,
1271 DECL_NAMESPACE_SCOPE_P (x)
1272 ? NAMESPACE_LEVEL (CP_DECL_CONTEXT (x))
1273 : current_binding_level);
1275 return x;
1278 /* Wrapper for pushdecl_maybe_friend_1. */
1280 tree
1281 pushdecl_maybe_friend (tree x, bool is_friend)
1283 tree ret;
1284 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
1285 ret = pushdecl_maybe_friend_1 (x, is_friend);
1286 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
1287 return ret;
1290 /* Record a decl-node X as belonging to the current lexical scope. */
1292 tree
1293 pushdecl (tree x)
1295 return pushdecl_maybe_friend (x, false);
1298 /* Enter DECL into the symbol table, if that's appropriate. Returns
1299 DECL, or a modified version thereof. */
1301 tree
1302 maybe_push_decl (tree decl)
1304 tree type = TREE_TYPE (decl);
1306 /* Add this decl to the current binding level, but not if it comes
1307 from another scope, e.g. a static member variable. TEM may equal
1308 DECL or it may be a previous decl of the same name. */
1309 if (decl == error_mark_node
1310 || (TREE_CODE (decl) != PARM_DECL
1311 && DECL_CONTEXT (decl) != NULL_TREE
1312 /* Definitions of namespace members outside their namespace are
1313 possible. */
1314 && !DECL_NAMESPACE_SCOPE_P (decl))
1315 || (TREE_CODE (decl) == TEMPLATE_DECL && !namespace_bindings_p ())
1316 || type == unknown_type_node
1317 /* The declaration of a template specialization does not affect
1318 the functions available for overload resolution, so we do not
1319 call pushdecl. */
1320 || (TREE_CODE (decl) == FUNCTION_DECL
1321 && DECL_TEMPLATE_SPECIALIZATION (decl)))
1322 return decl;
1323 else
1324 return pushdecl (decl);
1327 /* Bind DECL to ID in the current_binding_level, assumed to be a local
1328 binding level. If PUSH_USING is set in FLAGS, we know that DECL
1329 doesn't really belong to this binding level, that it got here
1330 through a using-declaration. */
1332 void
1333 push_local_binding (tree id, tree decl, int flags)
1335 cp_binding_level *b;
1337 /* Skip over any local classes. This makes sense if we call
1338 push_local_binding with a friend decl of a local class. */
1339 b = innermost_nonclass_level ();
1341 if (lookup_name_innermost_nonclass_level (id))
1343 /* Supplement the existing binding. */
1344 if (!supplement_binding (IDENTIFIER_BINDING (id), decl))
1345 /* It didn't work. Something else must be bound at this
1346 level. Do not add DECL to the list of things to pop
1347 later. */
1348 return;
1350 else
1351 /* Create a new binding. */
1352 push_binding (id, decl, b);
1354 if (TREE_CODE (decl) == OVERLOAD || (flags & PUSH_USING))
1355 /* We must put the OVERLOAD into a TREE_LIST since the
1356 TREE_CHAIN of an OVERLOAD is already used. Similarly for
1357 decls that got here through a using-declaration. */
1358 decl = build_tree_list (NULL_TREE, decl);
1360 /* And put DECL on the list of things declared by the current
1361 binding level. */
1362 add_decl_to_level (decl, b);
1365 /* Check to see whether or not DECL is a variable that would have been
1366 in scope under the ARM, but is not in scope under the ANSI/ISO
1367 standard. If so, issue an error message. If name lookup would
1368 work in both cases, but return a different result, this function
1369 returns the result of ANSI/ISO lookup. Otherwise, it returns
1370 DECL. */
1372 tree
1373 check_for_out_of_scope_variable (tree decl)
1375 tree shadowed;
1377 /* We only care about out of scope variables. */
1378 if (!(VAR_P (decl) && DECL_DEAD_FOR_LOCAL (decl)))
1379 return decl;
1381 shadowed = DECL_HAS_SHADOWED_FOR_VAR_P (decl)
1382 ? DECL_SHADOWED_FOR_VAR (decl) : NULL_TREE ;
1383 while (shadowed != NULL_TREE && VAR_P (shadowed)
1384 && DECL_DEAD_FOR_LOCAL (shadowed))
1385 shadowed = DECL_HAS_SHADOWED_FOR_VAR_P (shadowed)
1386 ? DECL_SHADOWED_FOR_VAR (shadowed) : NULL_TREE;
1387 if (!shadowed)
1388 shadowed = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (decl));
1389 if (shadowed)
1391 if (!DECL_ERROR_REPORTED (decl))
1393 warning (0, "name lookup of %qD changed", DECL_NAME (decl));
1394 warning (0, " matches this %q+D under ISO standard rules",
1395 shadowed);
1396 warning (0, " matches this %q+D under old rules", decl);
1397 DECL_ERROR_REPORTED (decl) = 1;
1399 return shadowed;
1402 /* If we have already complained about this declaration, there's no
1403 need to do it again. */
1404 if (DECL_ERROR_REPORTED (decl))
1405 return decl;
1407 DECL_ERROR_REPORTED (decl) = 1;
1409 if (TREE_TYPE (decl) == error_mark_node)
1410 return decl;
1412 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
1414 error ("name lookup of %qD changed for ISO %<for%> scoping",
1415 DECL_NAME (decl));
1416 error (" cannot use obsolete binding at %q+D because "
1417 "it has a destructor", decl);
1418 return error_mark_node;
1420 else
1422 permerror (input_location, "name lookup of %qD changed for ISO %<for%> scoping",
1423 DECL_NAME (decl));
1424 if (flag_permissive)
1425 permerror (input_location, " using obsolete binding at %q+D", decl);
1426 else
1428 static bool hint;
1429 if (!hint)
1431 inform (input_location, "(if you use %<-fpermissive%> G++ will accept your code)");
1432 hint = true;
1437 return decl;
1440 /* true means unconditionally make a BLOCK for the next level pushed. */
1442 static bool keep_next_level_flag;
1444 static int binding_depth = 0;
1446 static void
1447 indent (int depth)
1449 int i;
1451 for (i = 0; i < depth * 2; i++)
1452 putc (' ', stderr);
1455 /* Return a string describing the kind of SCOPE we have. */
1456 static const char *
1457 cp_binding_level_descriptor (cp_binding_level *scope)
1459 /* The order of this table must match the "scope_kind"
1460 enumerators. */
1461 static const char* scope_kind_names[] = {
1462 "block-scope",
1463 "cleanup-scope",
1464 "try-scope",
1465 "catch-scope",
1466 "for-scope",
1467 "function-parameter-scope",
1468 "class-scope",
1469 "namespace-scope",
1470 "template-parameter-scope",
1471 "template-explicit-spec-scope"
1473 const scope_kind kind = scope->explicit_spec_p
1474 ? sk_template_spec : scope->kind;
1476 return scope_kind_names[kind];
1479 /* Output a debugging information about SCOPE when performing
1480 ACTION at LINE. */
1481 static void
1482 cp_binding_level_debug (cp_binding_level *scope, int line, const char *action)
1484 const char *desc = cp_binding_level_descriptor (scope);
1485 if (scope->this_entity)
1486 verbatim ("%s %s(%E) %p %d\n", action, desc,
1487 scope->this_entity, (void *) scope, line);
1488 else
1489 verbatim ("%s %s %p %d\n", action, desc, (void *) scope, line);
1492 /* Return the estimated initial size of the hashtable of a NAMESPACE
1493 scope. */
1495 static inline size_t
1496 namespace_scope_ht_size (tree ns)
1498 tree name = DECL_NAME (ns);
1500 return name == std_identifier
1501 ? NAMESPACE_STD_HT_SIZE
1502 : (name == global_scope_name
1503 ? GLOBAL_SCOPE_HT_SIZE
1504 : NAMESPACE_ORDINARY_HT_SIZE);
1507 /* A chain of binding_level structures awaiting reuse. */
1509 static GTY((deletable)) cp_binding_level *free_binding_level;
1511 /* Insert SCOPE as the innermost binding level. */
1513 void
1514 push_binding_level (cp_binding_level *scope)
1516 /* Add it to the front of currently active scopes stack. */
1517 scope->level_chain = current_binding_level;
1518 current_binding_level = scope;
1519 keep_next_level_flag = false;
1521 if (ENABLE_SCOPE_CHECKING)
1523 scope->binding_depth = binding_depth;
1524 indent (binding_depth);
1525 cp_binding_level_debug (scope, LOCATION_LINE (input_location),
1526 "push");
1527 binding_depth++;
1531 /* Create a new KIND scope and make it the top of the active scopes stack.
1532 ENTITY is the scope of the associated C++ entity (namespace, class,
1533 function, C++0x enumeration); it is NULL otherwise. */
1535 cp_binding_level *
1536 begin_scope (scope_kind kind, tree entity)
1538 cp_binding_level *scope;
1540 /* Reuse or create a struct for this binding level. */
1541 if (!ENABLE_SCOPE_CHECKING && free_binding_level)
1543 scope = free_binding_level;
1544 memset (scope, 0, sizeof (cp_binding_level));
1545 free_binding_level = scope->level_chain;
1547 else
1548 scope = ggc_alloc_cleared_cp_binding_level ();
1550 scope->this_entity = entity;
1551 scope->more_cleanups_ok = true;
1552 switch (kind)
1554 case sk_cleanup:
1555 scope->keep = true;
1556 break;
1558 case sk_template_spec:
1559 scope->explicit_spec_p = true;
1560 kind = sk_template_parms;
1561 /* Fall through. */
1562 case sk_template_parms:
1563 case sk_block:
1564 case sk_try:
1565 case sk_catch:
1566 case sk_for:
1567 case sk_cond:
1568 case sk_class:
1569 case sk_scoped_enum:
1570 case sk_function_parms:
1571 case sk_omp:
1572 scope->keep = keep_next_level_flag;
1573 break;
1575 case sk_namespace:
1576 NAMESPACE_LEVEL (entity) = scope;
1577 vec_alloc (scope->static_decls,
1578 (DECL_NAME (entity) == std_identifier
1579 || DECL_NAME (entity) == global_scope_name) ? 200 : 10);
1580 break;
1582 default:
1583 /* Should not happen. */
1584 gcc_unreachable ();
1585 break;
1587 scope->kind = kind;
1589 push_binding_level (scope);
1591 return scope;
1594 /* We're about to leave current scope. Pop the top of the stack of
1595 currently active scopes. Return the enclosing scope, now active. */
1597 cp_binding_level *
1598 leave_scope (void)
1600 cp_binding_level *scope = current_binding_level;
1602 if (scope->kind == sk_namespace && class_binding_level)
1603 current_binding_level = class_binding_level;
1605 /* We cannot leave a scope, if there are none left. */
1606 if (NAMESPACE_LEVEL (global_namespace))
1607 gcc_assert (!global_scope_p (scope));
1609 if (ENABLE_SCOPE_CHECKING)
1611 indent (--binding_depth);
1612 cp_binding_level_debug (scope, LOCATION_LINE (input_location),
1613 "leave");
1616 /* Move one nesting level up. */
1617 current_binding_level = scope->level_chain;
1619 /* Namespace-scopes are left most probably temporarily, not
1620 completely; they can be reopened later, e.g. in namespace-extension
1621 or any name binding activity that requires us to resume a
1622 namespace. For classes, we cache some binding levels. For other
1623 scopes, we just make the structure available for reuse. */
1624 if (scope->kind != sk_namespace
1625 && scope->kind != sk_class)
1627 scope->level_chain = free_binding_level;
1628 gcc_assert (!ENABLE_SCOPE_CHECKING
1629 || scope->binding_depth == binding_depth);
1630 free_binding_level = scope;
1633 /* Find the innermost enclosing class scope, and reset
1634 CLASS_BINDING_LEVEL appropriately. */
1635 if (scope->kind == sk_class)
1637 class_binding_level = NULL;
1638 for (scope = current_binding_level; scope; scope = scope->level_chain)
1639 if (scope->kind == sk_class)
1641 class_binding_level = scope;
1642 break;
1646 return current_binding_level;
1649 static void
1650 resume_scope (cp_binding_level* b)
1652 /* Resuming binding levels is meant only for namespaces,
1653 and those cannot nest into classes. */
1654 gcc_assert (!class_binding_level);
1655 /* Also, resuming a non-directly nested namespace is a no-no. */
1656 gcc_assert (b->level_chain == current_binding_level);
1657 current_binding_level = b;
1658 if (ENABLE_SCOPE_CHECKING)
1660 b->binding_depth = binding_depth;
1661 indent (binding_depth);
1662 cp_binding_level_debug (b, LOCATION_LINE (input_location), "resume");
1663 binding_depth++;
1667 /* Return the innermost binding level that is not for a class scope. */
1669 static cp_binding_level *
1670 innermost_nonclass_level (void)
1672 cp_binding_level *b;
1674 b = current_binding_level;
1675 while (b->kind == sk_class)
1676 b = b->level_chain;
1678 return b;
1681 /* We're defining an object of type TYPE. If it needs a cleanup, but
1682 we're not allowed to add any more objects with cleanups to the current
1683 scope, create a new binding level. */
1685 void
1686 maybe_push_cleanup_level (tree type)
1688 if (type != error_mark_node
1689 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
1690 && current_binding_level->more_cleanups_ok == 0)
1692 begin_scope (sk_cleanup, NULL);
1693 current_binding_level->statement_list = push_stmt_list ();
1697 /* Return true if we are in the global binding level. */
1699 bool
1700 global_bindings_p (void)
1702 return global_scope_p (current_binding_level);
1705 /* True if we are currently in a toplevel binding level. This
1706 means either the global binding level or a namespace in a toplevel
1707 binding level. Since there are no non-toplevel namespace levels,
1708 this really means any namespace or template parameter level. We
1709 also include a class whose context is toplevel. */
1711 bool
1712 toplevel_bindings_p (void)
1714 cp_binding_level *b = innermost_nonclass_level ();
1716 return b->kind == sk_namespace || b->kind == sk_template_parms;
1719 /* True if this is a namespace scope, or if we are defining a class
1720 which is itself at namespace scope, or whose enclosing class is
1721 such a class, etc. */
1723 bool
1724 namespace_bindings_p (void)
1726 cp_binding_level *b = innermost_nonclass_level ();
1728 return b->kind == sk_namespace;
1731 /* True if the innermost non-class scope is a block scope. */
1733 bool
1734 local_bindings_p (void)
1736 cp_binding_level *b = innermost_nonclass_level ();
1737 return b->kind < sk_function_parms || b->kind == sk_omp;
1740 /* True if the current level needs to have a BLOCK made. */
1742 bool
1743 kept_level_p (void)
1745 return (current_binding_level->blocks != NULL_TREE
1746 || current_binding_level->keep
1747 || current_binding_level->kind == sk_cleanup
1748 || current_binding_level->names != NULL_TREE
1749 || current_binding_level->using_directives);
1752 /* Returns the kind of the innermost scope. */
1754 scope_kind
1755 innermost_scope_kind (void)
1757 return current_binding_level->kind;
1760 /* Returns true if this scope was created to store template parameters. */
1762 bool
1763 template_parm_scope_p (void)
1765 return innermost_scope_kind () == sk_template_parms;
1768 /* If KEEP is true, make a BLOCK node for the next binding level,
1769 unconditionally. Otherwise, use the normal logic to decide whether
1770 or not to create a BLOCK. */
1772 void
1773 keep_next_level (bool keep)
1775 keep_next_level_flag = keep;
1778 /* Return the list of declarations of the current level.
1779 Note that this list is in reverse order unless/until
1780 you nreverse it; and when you do nreverse it, you must
1781 store the result back using `storedecls' or you will lose. */
1783 tree
1784 getdecls (void)
1786 return current_binding_level->names;
1789 /* Return how many function prototypes we are currently nested inside. */
1792 function_parm_depth (void)
1794 int level = 0;
1795 cp_binding_level *b;
1797 for (b = current_binding_level;
1798 b->kind == sk_function_parms;
1799 b = b->level_chain)
1800 ++level;
1802 return level;
1805 /* For debugging. */
1806 static int no_print_functions = 0;
1807 static int no_print_builtins = 0;
1809 static void
1810 print_binding_level (cp_binding_level* lvl)
1812 tree t;
1813 int i = 0, len;
1814 fprintf (stderr, " blocks=%p", (void *) lvl->blocks);
1815 if (lvl->more_cleanups_ok)
1816 fprintf (stderr, " more-cleanups-ok");
1817 if (lvl->have_cleanups)
1818 fprintf (stderr, " have-cleanups");
1819 fprintf (stderr, "\n");
1820 if (lvl->names)
1822 fprintf (stderr, " names:\t");
1823 /* We can probably fit 3 names to a line? */
1824 for (t = lvl->names; t; t = TREE_CHAIN (t))
1826 if (no_print_functions && (TREE_CODE (t) == FUNCTION_DECL))
1827 continue;
1828 if (no_print_builtins
1829 && (TREE_CODE (t) == TYPE_DECL)
1830 && DECL_IS_BUILTIN (t))
1831 continue;
1833 /* Function decls tend to have longer names. */
1834 if (TREE_CODE (t) == FUNCTION_DECL)
1835 len = 3;
1836 else
1837 len = 2;
1838 i += len;
1839 if (i > 6)
1841 fprintf (stderr, "\n\t");
1842 i = len;
1844 print_node_brief (stderr, "", t, 0);
1845 if (t == error_mark_node)
1846 break;
1848 if (i)
1849 fprintf (stderr, "\n");
1851 if (vec_safe_length (lvl->class_shadowed))
1853 size_t i;
1854 cp_class_binding *b;
1855 fprintf (stderr, " class-shadowed:");
1856 FOR_EACH_VEC_ELT (*lvl->class_shadowed, i, b)
1857 fprintf (stderr, " %s ", IDENTIFIER_POINTER (b->identifier));
1858 fprintf (stderr, "\n");
1860 if (lvl->type_shadowed)
1862 fprintf (stderr, " type-shadowed:");
1863 for (t = lvl->type_shadowed; t; t = TREE_CHAIN (t))
1865 fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
1867 fprintf (stderr, "\n");
1871 DEBUG_FUNCTION void
1872 debug (cp_binding_level &ref)
1874 print_binding_level (&ref);
1877 DEBUG_FUNCTION void
1878 debug (cp_binding_level *ptr)
1880 if (ptr)
1881 debug (*ptr);
1882 else
1883 fprintf (stderr, "<nil>\n");
1887 void
1888 print_other_binding_stack (cp_binding_level *stack)
1890 cp_binding_level *level;
1891 for (level = stack; !global_scope_p (level); level = level->level_chain)
1893 fprintf (stderr, "binding level %p\n", (void *) level);
1894 print_binding_level (level);
1898 void
1899 print_binding_stack (void)
1901 cp_binding_level *b;
1902 fprintf (stderr, "current_binding_level=%p\n"
1903 "class_binding_level=%p\n"
1904 "NAMESPACE_LEVEL (global_namespace)=%p\n",
1905 (void *) current_binding_level, (void *) class_binding_level,
1906 (void *) NAMESPACE_LEVEL (global_namespace));
1907 if (class_binding_level)
1909 for (b = class_binding_level; b; b = b->level_chain)
1910 if (b == current_binding_level)
1911 break;
1912 if (b)
1913 b = class_binding_level;
1914 else
1915 b = current_binding_level;
1917 else
1918 b = current_binding_level;
1919 print_other_binding_stack (b);
1920 fprintf (stderr, "global:\n");
1921 print_binding_level (NAMESPACE_LEVEL (global_namespace));
1924 /* Return the type associated with ID. */
1926 static tree
1927 identifier_type_value_1 (tree id)
1929 /* There is no type with that name, anywhere. */
1930 if (REAL_IDENTIFIER_TYPE_VALUE (id) == NULL_TREE)
1931 return NULL_TREE;
1932 /* This is not the type marker, but the real thing. */
1933 if (REAL_IDENTIFIER_TYPE_VALUE (id) != global_type_node)
1934 return REAL_IDENTIFIER_TYPE_VALUE (id);
1935 /* Have to search for it. It must be on the global level, now.
1936 Ask lookup_name not to return non-types. */
1937 id = lookup_name_real (id, 2, 1, /*block_p=*/true, 0, 0);
1938 if (id)
1939 return TREE_TYPE (id);
1940 return NULL_TREE;
1943 /* Wrapper for identifier_type_value_1. */
1945 tree
1946 identifier_type_value (tree id)
1948 tree ret;
1949 timevar_start (TV_NAME_LOOKUP);
1950 ret = identifier_type_value_1 (id);
1951 timevar_stop (TV_NAME_LOOKUP);
1952 return ret;
1956 /* Return the IDENTIFIER_GLOBAL_VALUE of T, for use in common code, since
1957 the definition of IDENTIFIER_GLOBAL_VALUE is different for C and C++. */
1959 tree
1960 identifier_global_value (tree t)
1962 return IDENTIFIER_GLOBAL_VALUE (t);
1965 /* Push a definition of struct, union or enum tag named ID. into
1966 binding_level B. DECL is a TYPE_DECL for the type. We assume that
1967 the tag ID is not already defined. */
1969 static void
1970 set_identifier_type_value_with_scope (tree id, tree decl, cp_binding_level *b)
1972 tree type;
1974 if (b->kind != sk_namespace)
1976 /* Shadow the marker, not the real thing, so that the marker
1977 gets restored later. */
1978 tree old_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
1979 b->type_shadowed
1980 = tree_cons (id, old_type_value, b->type_shadowed);
1981 type = decl ? TREE_TYPE (decl) : NULL_TREE;
1982 TREE_TYPE (b->type_shadowed) = type;
1984 else
1986 cxx_binding *binding =
1987 binding_for_name (NAMESPACE_LEVEL (current_namespace), id);
1988 gcc_assert (decl);
1989 if (binding->value)
1990 supplement_binding (binding, decl);
1991 else
1992 binding->value = decl;
1994 /* Store marker instead of real type. */
1995 type = global_type_node;
1997 SET_IDENTIFIER_TYPE_VALUE (id, type);
2000 /* As set_identifier_type_value_with_scope, but using
2001 current_binding_level. */
2003 void
2004 set_identifier_type_value (tree id, tree decl)
2006 set_identifier_type_value_with_scope (id, decl, current_binding_level);
2009 /* Return the name for the constructor (or destructor) for the
2010 specified class TYPE. When given a template, this routine doesn't
2011 lose the specialization. */
2013 static inline tree
2014 constructor_name_full (tree type)
2016 return TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
2019 /* Return the name for the constructor (or destructor) for the
2020 specified class. When given a template, return the plain
2021 unspecialized name. */
2023 tree
2024 constructor_name (tree type)
2026 tree name;
2027 name = constructor_name_full (type);
2028 if (IDENTIFIER_TEMPLATE (name))
2029 name = IDENTIFIER_TEMPLATE (name);
2030 return name;
2033 /* Returns TRUE if NAME is the name for the constructor for TYPE,
2034 which must be a class type. */
2036 bool
2037 constructor_name_p (tree name, tree type)
2039 tree ctor_name;
2041 gcc_assert (MAYBE_CLASS_TYPE_P (type));
2043 if (!name)
2044 return false;
2046 if (!identifier_p (name))
2047 return false;
2049 /* These don't have names. */
2050 if (TREE_CODE (type) == DECLTYPE_TYPE
2051 || TREE_CODE (type) == TYPEOF_TYPE)
2052 return false;
2054 ctor_name = constructor_name_full (type);
2055 if (name == ctor_name)
2056 return true;
2057 if (IDENTIFIER_TEMPLATE (ctor_name)
2058 && name == IDENTIFIER_TEMPLATE (ctor_name))
2059 return true;
2060 return false;
2063 /* Counter used to create anonymous type names. */
2065 static GTY(()) int anon_cnt;
2067 /* Return an IDENTIFIER which can be used as a name for
2068 anonymous structs and unions. */
2070 tree
2071 make_anon_name (void)
2073 char buf[32];
2075 sprintf (buf, ANON_AGGRNAME_FORMAT, anon_cnt++);
2076 return get_identifier (buf);
2079 /* This code is practically identical to that for creating
2080 anonymous names, but is just used for lambdas instead. This isn't really
2081 necessary, but it's convenient to avoid treating lambdas like other
2082 anonymous types. */
2084 static GTY(()) int lambda_cnt = 0;
2086 tree
2087 make_lambda_name (void)
2089 char buf[32];
2091 sprintf (buf, LAMBDANAME_FORMAT, lambda_cnt++);
2092 return get_identifier (buf);
2095 /* Return (from the stack of) the BINDING, if any, established at SCOPE. */
2097 static inline cxx_binding *
2098 find_binding (cp_binding_level *scope, cxx_binding *binding)
2100 for (; binding != NULL; binding = binding->previous)
2101 if (binding->scope == scope)
2102 return binding;
2104 return (cxx_binding *)0;
2107 /* Return the binding for NAME in SCOPE, if any. Otherwise, return NULL. */
2109 static inline cxx_binding *
2110 cp_binding_level_find_binding_for_name (cp_binding_level *scope, tree name)
2112 cxx_binding *b = IDENTIFIER_NAMESPACE_BINDINGS (name);
2113 if (b)
2115 /* Fold-in case where NAME is used only once. */
2116 if (scope == b->scope && b->previous == NULL)
2117 return b;
2118 return find_binding (scope, b);
2120 return NULL;
2123 /* Always returns a binding for name in scope. If no binding is
2124 found, make a new one. */
2126 static cxx_binding *
2127 binding_for_name (cp_binding_level *scope, tree name)
2129 cxx_binding *result;
2131 result = cp_binding_level_find_binding_for_name (scope, name);
2132 if (result)
2133 return result;
2134 /* Not found, make a new one. */
2135 result = cxx_binding_make (NULL, NULL);
2136 result->previous = IDENTIFIER_NAMESPACE_BINDINGS (name);
2137 result->scope = scope;
2138 result->is_local = false;
2139 result->value_is_inherited = false;
2140 IDENTIFIER_NAMESPACE_BINDINGS (name) = result;
2141 return result;
2144 /* Walk through the bindings associated to the name of FUNCTION,
2145 and return the first declaration of a function with a
2146 "C" linkage specification, a.k.a 'extern "C"'.
2147 This function looks for the binding, regardless of which scope it
2148 has been defined in. It basically looks in all the known scopes.
2149 Note that this function does not lookup for bindings of builtin functions
2150 or for functions declared in system headers. */
2151 static tree
2152 lookup_extern_c_fun_in_all_ns (tree function)
2154 tree name;
2155 cxx_binding *iter;
2157 gcc_assert (function && TREE_CODE (function) == FUNCTION_DECL);
2159 name = DECL_NAME (function);
2160 gcc_assert (name && identifier_p (name));
2162 for (iter = IDENTIFIER_NAMESPACE_BINDINGS (name);
2163 iter;
2164 iter = iter->previous)
2166 tree ovl;
2167 for (ovl = iter->value; ovl; ovl = OVL_NEXT (ovl))
2169 tree decl = OVL_CURRENT (ovl);
2170 if (decl
2171 && TREE_CODE (decl) == FUNCTION_DECL
2172 && DECL_EXTERN_C_P (decl)
2173 && !DECL_ARTIFICIAL (decl))
2175 return decl;
2179 return NULL;
2182 /* Returns a list of C-linkage decls with the name NAME. */
2184 tree
2185 c_linkage_bindings (tree name)
2187 tree decls = NULL_TREE;
2188 cxx_binding *iter;
2190 for (iter = IDENTIFIER_NAMESPACE_BINDINGS (name);
2191 iter;
2192 iter = iter->previous)
2194 tree ovl;
2195 for (ovl = iter->value; ovl; ovl = OVL_NEXT (ovl))
2197 tree decl = OVL_CURRENT (ovl);
2198 if (decl
2199 && DECL_EXTERN_C_P (decl)
2200 && !DECL_ARTIFICIAL (decl))
2202 if (decls == NULL_TREE)
2203 decls = decl;
2204 else
2205 decls = tree_cons (NULL_TREE, decl, decls);
2209 return decls;
2212 /* Insert another USING_DECL into the current binding level, returning
2213 this declaration. If this is a redeclaration, do nothing, and
2214 return NULL_TREE if this not in namespace scope (in namespace
2215 scope, a using decl might extend any previous bindings). */
2217 static tree
2218 push_using_decl_1 (tree scope, tree name)
2220 tree decl;
2222 gcc_assert (TREE_CODE (scope) == NAMESPACE_DECL);
2223 gcc_assert (identifier_p (name));
2224 for (decl = current_binding_level->usings; decl; decl = DECL_CHAIN (decl))
2225 if (USING_DECL_SCOPE (decl) == scope && DECL_NAME (decl) == name)
2226 break;
2227 if (decl)
2228 return namespace_bindings_p () ? decl : NULL_TREE;
2229 decl = build_lang_decl (USING_DECL, name, NULL_TREE);
2230 USING_DECL_SCOPE (decl) = scope;
2231 DECL_CHAIN (decl) = current_binding_level->usings;
2232 current_binding_level->usings = decl;
2233 return decl;
2236 /* Wrapper for push_using_decl_1. */
2238 static tree
2239 push_using_decl (tree scope, tree name)
2241 tree ret;
2242 timevar_start (TV_NAME_LOOKUP);
2243 ret = push_using_decl_1 (scope, name);
2244 timevar_stop (TV_NAME_LOOKUP);
2245 return ret;
2248 /* Same as pushdecl, but define X in binding-level LEVEL. We rely on the
2249 caller to set DECL_CONTEXT properly.
2251 Note that this must only be used when X will be the new innermost
2252 binding for its name, as we tack it onto the front of IDENTIFIER_BINDING
2253 without checking to see if the current IDENTIFIER_BINDING comes from a
2254 closer binding level than LEVEL. */
2256 static tree
2257 pushdecl_with_scope_1 (tree x, cp_binding_level *level, bool is_friend)
2259 cp_binding_level *b;
2260 tree function_decl = current_function_decl;
2262 current_function_decl = NULL_TREE;
2263 if (level->kind == sk_class)
2265 b = class_binding_level;
2266 class_binding_level = level;
2267 pushdecl_class_level (x);
2268 class_binding_level = b;
2270 else
2272 b = current_binding_level;
2273 current_binding_level = level;
2274 x = pushdecl_maybe_friend (x, is_friend);
2275 current_binding_level = b;
2277 current_function_decl = function_decl;
2278 return x;
2281 /* Wrapper for pushdecl_with_scope_1. */
2283 tree
2284 pushdecl_with_scope (tree x, cp_binding_level *level, bool is_friend)
2286 tree ret;
2287 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
2288 ret = pushdecl_with_scope_1 (x, level, is_friend);
2289 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
2290 return ret;
2293 /* Helper function for push_overloaded_decl_1 and do_nonmember_using_decl.
2294 Compares the parameter-type-lists of DECL1 and DECL2 and returns false
2295 if they are different. If the DECLs are template functions, the return
2296 types and the template parameter lists are compared too (DR 565). */
2298 static bool
2299 compparms_for_decl_and_using_decl (tree decl1, tree decl2)
2301 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (decl1)),
2302 TYPE_ARG_TYPES (TREE_TYPE (decl2))))
2303 return false;
2305 if (! DECL_FUNCTION_TEMPLATE_P (decl1)
2306 || ! DECL_FUNCTION_TEMPLATE_P (decl2))
2307 return true;
2309 return (comp_template_parms (DECL_TEMPLATE_PARMS (decl1),
2310 DECL_TEMPLATE_PARMS (decl2))
2311 && same_type_p (TREE_TYPE (TREE_TYPE (decl1)),
2312 TREE_TYPE (TREE_TYPE (decl2))));
2315 /* DECL is a FUNCTION_DECL for a non-member function, which may have
2316 other definitions already in place. We get around this by making
2317 the value of the identifier point to a list of all the things that
2318 want to be referenced by that name. It is then up to the users of
2319 that name to decide what to do with that list.
2321 DECL may also be a TEMPLATE_DECL, with a FUNCTION_DECL in its
2322 DECL_TEMPLATE_RESULT. It is dealt with the same way.
2324 FLAGS is a bitwise-or of the following values:
2325 PUSH_LOCAL: Bind DECL in the current scope, rather than at
2326 namespace scope.
2327 PUSH_USING: DECL is being pushed as the result of a using
2328 declaration.
2330 IS_FRIEND is true if this is a friend declaration.
2332 The value returned may be a previous declaration if we guessed wrong
2333 about what language DECL should belong to (C or C++). Otherwise,
2334 it's always DECL (and never something that's not a _DECL). */
2336 static tree
2337 push_overloaded_decl_1 (tree decl, int flags, bool is_friend)
2339 tree name = DECL_NAME (decl);
2340 tree old;
2341 tree new_binding;
2342 int doing_global = (namespace_bindings_p () || !(flags & PUSH_LOCAL));
2344 if (doing_global)
2345 old = namespace_binding (name, DECL_CONTEXT (decl));
2346 else
2347 old = lookup_name_innermost_nonclass_level (name);
2349 if (old)
2351 if (TREE_CODE (old) == TYPE_DECL && DECL_ARTIFICIAL (old))
2353 tree t = TREE_TYPE (old);
2354 if (MAYBE_CLASS_TYPE_P (t) && warn_shadow
2355 && (! DECL_IN_SYSTEM_HEADER (decl)
2356 || ! DECL_IN_SYSTEM_HEADER (old)))
2357 warning (OPT_Wshadow, "%q#D hides constructor for %q#T", decl, t);
2358 old = NULL_TREE;
2360 else if (is_overloaded_fn (old))
2362 tree tmp;
2364 for (tmp = old; tmp; tmp = OVL_NEXT (tmp))
2366 tree fn = OVL_CURRENT (tmp);
2367 tree dup;
2369 if (TREE_CODE (tmp) == OVERLOAD && OVL_USED (tmp)
2370 && !(flags & PUSH_USING)
2371 && compparms_for_decl_and_using_decl (fn, decl)
2372 && ! decls_match (fn, decl))
2373 diagnose_name_conflict (decl, fn);
2375 dup = duplicate_decls (decl, fn, is_friend);
2376 /* If DECL was a redeclaration of FN -- even an invalid
2377 one -- pass that information along to our caller. */
2378 if (dup == fn || dup == error_mark_node)
2379 return dup;
2382 /* We don't overload implicit built-ins. duplicate_decls()
2383 may fail to merge the decls if the new decl is e.g. a
2384 template function. */
2385 if (TREE_CODE (old) == FUNCTION_DECL
2386 && DECL_ANTICIPATED (old)
2387 && !DECL_HIDDEN_FRIEND_P (old))
2388 old = NULL;
2390 else if (old == error_mark_node)
2391 /* Ignore the undefined symbol marker. */
2392 old = NULL_TREE;
2393 else
2395 error ("previous non-function declaration %q+#D", old);
2396 error ("conflicts with function declaration %q#D", decl);
2397 return decl;
2401 if (old || TREE_CODE (decl) == TEMPLATE_DECL
2402 /* If it's a using declaration, we always need to build an OVERLOAD,
2403 because it's the only way to remember that the declaration comes
2404 from 'using', and have the lookup behave correctly. */
2405 || (flags & PUSH_USING))
2407 if (old && TREE_CODE (old) != OVERLOAD)
2408 new_binding = ovl_cons (decl, ovl_cons (old, NULL_TREE));
2409 else
2410 new_binding = ovl_cons (decl, old);
2411 if (flags & PUSH_USING)
2412 OVL_USED (new_binding) = 1;
2414 else
2415 /* NAME is not ambiguous. */
2416 new_binding = decl;
2418 if (doing_global)
2419 set_namespace_binding (name, current_namespace, new_binding);
2420 else
2422 /* We only create an OVERLOAD if there was a previous binding at
2423 this level, or if decl is a template. In the former case, we
2424 need to remove the old binding and replace it with the new
2425 binding. We must also run through the NAMES on the binding
2426 level where the name was bound to update the chain. */
2428 if (TREE_CODE (new_binding) == OVERLOAD && old)
2430 tree *d;
2432 for (d = &IDENTIFIER_BINDING (name)->scope->names;
2434 d = &TREE_CHAIN (*d))
2435 if (*d == old
2436 || (TREE_CODE (*d) == TREE_LIST
2437 && TREE_VALUE (*d) == old))
2439 if (TREE_CODE (*d) == TREE_LIST)
2440 /* Just replace the old binding with the new. */
2441 TREE_VALUE (*d) = new_binding;
2442 else
2443 /* Build a TREE_LIST to wrap the OVERLOAD. */
2444 *d = tree_cons (NULL_TREE, new_binding,
2445 TREE_CHAIN (*d));
2447 /* And update the cxx_binding node. */
2448 IDENTIFIER_BINDING (name)->value = new_binding;
2449 return decl;
2452 /* We should always find a previous binding in this case. */
2453 gcc_unreachable ();
2456 /* Install the new binding. */
2457 push_local_binding (name, new_binding, flags);
2460 return decl;
2463 /* Wrapper for push_overloaded_decl_1. */
2465 static tree
2466 push_overloaded_decl (tree decl, int flags, bool is_friend)
2468 tree ret;
2469 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
2470 ret = push_overloaded_decl_1 (decl, flags, is_friend);
2471 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
2472 return ret;
2475 /* Check a non-member using-declaration. Return the name and scope
2476 being used, and the USING_DECL, or NULL_TREE on failure. */
2478 static tree
2479 validate_nonmember_using_decl (tree decl, tree scope, tree name)
2481 /* [namespace.udecl]
2482 A using-declaration for a class member shall be a
2483 member-declaration. */
2484 if (TYPE_P (scope))
2486 error ("%qT is not a namespace", scope);
2487 return NULL_TREE;
2489 else if (scope == error_mark_node)
2490 return NULL_TREE;
2492 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR)
2494 /* 7.3.3/5
2495 A using-declaration shall not name a template-id. */
2496 error ("a using-declaration cannot specify a template-id. "
2497 "Try %<using %D%>", name);
2498 return NULL_TREE;
2501 if (TREE_CODE (decl) == NAMESPACE_DECL)
2503 error ("namespace %qD not allowed in using-declaration", decl);
2504 return NULL_TREE;
2507 if (TREE_CODE (decl) == SCOPE_REF)
2509 /* It's a nested name with template parameter dependent scope.
2510 This can only be using-declaration for class member. */
2511 error ("%qT is not a namespace", TREE_OPERAND (decl, 0));
2512 return NULL_TREE;
2515 if (is_overloaded_fn (decl))
2516 decl = get_first_fn (decl);
2518 gcc_assert (DECL_P (decl));
2520 /* Make a USING_DECL. */
2521 tree using_decl = push_using_decl (scope, name);
2523 if (using_decl == NULL_TREE
2524 && at_function_scope_p ()
2525 && VAR_P (decl))
2526 /* C++11 7.3.3/10. */
2527 error ("%qD is already declared in this scope", name);
2529 return using_decl;
2532 /* Process local and global using-declarations. */
2534 static void
2535 do_nonmember_using_decl (tree scope, tree name, tree oldval, tree oldtype,
2536 tree *newval, tree *newtype)
2538 struct scope_binding decls = EMPTY_SCOPE_BINDING;
2540 *newval = *newtype = NULL_TREE;
2541 if (!qualified_lookup_using_namespace (name, scope, &decls, 0))
2542 /* Lookup error */
2543 return;
2545 if (!decls.value && !decls.type)
2547 error ("%qD not declared", name);
2548 return;
2551 /* Shift the old and new bindings around so we're comparing class and
2552 enumeration names to each other. */
2553 if (oldval && DECL_IMPLICIT_TYPEDEF_P (oldval))
2555 oldtype = oldval;
2556 oldval = NULL_TREE;
2559 if (decls.value && DECL_IMPLICIT_TYPEDEF_P (decls.value))
2561 decls.type = decls.value;
2562 decls.value = NULL_TREE;
2565 /* It is impossible to overload a built-in function; any explicit
2566 declaration eliminates the built-in declaration. So, if OLDVAL
2567 is a built-in, then we can just pretend it isn't there. */
2568 if (oldval
2569 && TREE_CODE (oldval) == FUNCTION_DECL
2570 && DECL_ANTICIPATED (oldval)
2571 && !DECL_HIDDEN_FRIEND_P (oldval))
2572 oldval = NULL_TREE;
2574 if (decls.value)
2576 /* Check for using functions. */
2577 if (is_overloaded_fn (decls.value))
2579 tree tmp, tmp1;
2581 if (oldval && !is_overloaded_fn (oldval))
2583 error ("%qD is already declared in this scope", name);
2584 oldval = NULL_TREE;
2587 *newval = oldval;
2588 for (tmp = decls.value; tmp; tmp = OVL_NEXT (tmp))
2590 tree new_fn = OVL_CURRENT (tmp);
2592 /* [namespace.udecl]
2594 If a function declaration in namespace scope or block
2595 scope has the same name and the same parameter types as a
2596 function introduced by a using declaration the program is
2597 ill-formed. */
2598 for (tmp1 = oldval; tmp1; tmp1 = OVL_NEXT (tmp1))
2600 tree old_fn = OVL_CURRENT (tmp1);
2602 if (new_fn == old_fn)
2603 /* The function already exists in the current namespace. */
2604 break;
2605 else if (TREE_CODE (tmp1) == OVERLOAD && OVL_USED (tmp1))
2606 continue; /* this is a using decl */
2607 else if (compparms_for_decl_and_using_decl (new_fn, old_fn))
2609 gcc_assert (!DECL_ANTICIPATED (old_fn)
2610 || DECL_HIDDEN_FRIEND_P (old_fn));
2612 /* There was already a non-using declaration in
2613 this scope with the same parameter types. If both
2614 are the same extern "C" functions, that's ok. */
2615 if (decls_match (new_fn, old_fn))
2616 break;
2617 else
2619 diagnose_name_conflict (new_fn, old_fn);
2620 break;
2625 /* If we broke out of the loop, there's no reason to add
2626 this function to the using declarations for this
2627 scope. */
2628 if (tmp1)
2629 continue;
2631 /* If we are adding to an existing OVERLOAD, then we no
2632 longer know the type of the set of functions. */
2633 if (*newval && TREE_CODE (*newval) == OVERLOAD)
2634 TREE_TYPE (*newval) = unknown_type_node;
2635 /* Add this new function to the set. */
2636 *newval = build_overload (OVL_CURRENT (tmp), *newval);
2637 /* If there is only one function, then we use its type. (A
2638 using-declaration naming a single function can be used in
2639 contexts where overload resolution cannot be
2640 performed.) */
2641 if (TREE_CODE (*newval) != OVERLOAD)
2643 *newval = ovl_cons (*newval, NULL_TREE);
2644 TREE_TYPE (*newval) = TREE_TYPE (OVL_CURRENT (tmp));
2646 OVL_USED (*newval) = 1;
2649 else
2651 *newval = decls.value;
2652 if (oldval && !decls_match (*newval, oldval))
2653 error ("%qD is already declared in this scope", name);
2656 else
2657 *newval = oldval;
2659 if (decls.type && TREE_CODE (decls.type) == TREE_LIST)
2661 error ("reference to %qD is ambiguous", name);
2662 print_candidates (decls.type);
2664 else
2666 *newtype = decls.type;
2667 if (oldtype && *newtype && !decls_match (oldtype, *newtype))
2668 error ("%qD is already declared in this scope", name);
2671 /* If *newval is empty, shift any class or enumeration name down. */
2672 if (!*newval)
2674 *newval = *newtype;
2675 *newtype = NULL_TREE;
2679 /* Process a using-declaration at function scope. */
2681 void
2682 do_local_using_decl (tree decl, tree scope, tree name)
2684 tree oldval, oldtype, newval, newtype;
2685 tree orig_decl = decl;
2687 decl = validate_nonmember_using_decl (decl, scope, name);
2688 if (decl == NULL_TREE)
2689 return;
2691 if (building_stmt_list_p ()
2692 && at_function_scope_p ())
2693 add_decl_expr (decl);
2695 oldval = lookup_name_innermost_nonclass_level (name);
2696 oldtype = lookup_type_current_level (name);
2698 do_nonmember_using_decl (scope, name, oldval, oldtype, &newval, &newtype);
2700 if (newval)
2702 if (is_overloaded_fn (newval))
2704 tree fn, term;
2706 /* We only need to push declarations for those functions
2707 that were not already bound in the current level.
2708 The old value might be NULL_TREE, it might be a single
2709 function, or an OVERLOAD. */
2710 if (oldval && TREE_CODE (oldval) == OVERLOAD)
2711 term = OVL_FUNCTION (oldval);
2712 else
2713 term = oldval;
2714 for (fn = newval; fn && OVL_CURRENT (fn) != term;
2715 fn = OVL_NEXT (fn))
2716 push_overloaded_decl (OVL_CURRENT (fn),
2717 PUSH_LOCAL | PUSH_USING,
2718 false);
2720 else
2721 push_local_binding (name, newval, PUSH_USING);
2723 if (newtype)
2725 push_local_binding (name, newtype, PUSH_USING);
2726 set_identifier_type_value (name, newtype);
2729 /* Emit debug info. */
2730 if (!processing_template_decl)
2731 cp_emit_debug_info_for_using (orig_decl, current_scope());
2734 /* Returns true if ROOT (a namespace, class, or function) encloses
2735 CHILD. CHILD may be either a class type or a namespace. */
2737 bool
2738 is_ancestor (tree root, tree child)
2740 gcc_assert ((TREE_CODE (root) == NAMESPACE_DECL
2741 || TREE_CODE (root) == FUNCTION_DECL
2742 || CLASS_TYPE_P (root)));
2743 gcc_assert ((TREE_CODE (child) == NAMESPACE_DECL
2744 || CLASS_TYPE_P (child)));
2746 /* The global namespace encloses everything. */
2747 if (root == global_namespace)
2748 return true;
2750 while (true)
2752 /* If we've run out of scopes, stop. */
2753 if (!child)
2754 return false;
2755 /* If we've reached the ROOT, it encloses CHILD. */
2756 if (root == child)
2757 return true;
2758 /* Go out one level. */
2759 if (TYPE_P (child))
2760 child = TYPE_NAME (child);
2761 child = DECL_CONTEXT (child);
2765 /* Enter the class or namespace scope indicated by T suitable for name
2766 lookup. T can be arbitrary scope, not necessary nested inside the
2767 current scope. Returns a non-null scope to pop iff pop_scope
2768 should be called later to exit this scope. */
2770 tree
2771 push_scope (tree t)
2773 if (TREE_CODE (t) == NAMESPACE_DECL)
2774 push_decl_namespace (t);
2775 else if (CLASS_TYPE_P (t))
2777 if (!at_class_scope_p ()
2778 || !same_type_p (current_class_type, t))
2779 push_nested_class (t);
2780 else
2781 /* T is the same as the current scope. There is therefore no
2782 need to re-enter the scope. Since we are not actually
2783 pushing a new scope, our caller should not call
2784 pop_scope. */
2785 t = NULL_TREE;
2788 return t;
2791 /* Leave scope pushed by push_scope. */
2793 void
2794 pop_scope (tree t)
2796 if (t == NULL_TREE)
2797 return;
2798 if (TREE_CODE (t) == NAMESPACE_DECL)
2799 pop_decl_namespace ();
2800 else if CLASS_TYPE_P (t)
2801 pop_nested_class ();
2804 /* Subroutine of push_inner_scope. */
2806 static void
2807 push_inner_scope_r (tree outer, tree inner)
2809 tree prev;
2811 if (outer == inner
2812 || (TREE_CODE (inner) != NAMESPACE_DECL && !CLASS_TYPE_P (inner)))
2813 return;
2815 prev = CP_DECL_CONTEXT (TREE_CODE (inner) == NAMESPACE_DECL ? inner : TYPE_NAME (inner));
2816 if (outer != prev)
2817 push_inner_scope_r (outer, prev);
2818 if (TREE_CODE (inner) == NAMESPACE_DECL)
2820 cp_binding_level *save_template_parm = 0;
2821 /* Temporary take out template parameter scopes. They are saved
2822 in reversed order in save_template_parm. */
2823 while (current_binding_level->kind == sk_template_parms)
2825 cp_binding_level *b = current_binding_level;
2826 current_binding_level = b->level_chain;
2827 b->level_chain = save_template_parm;
2828 save_template_parm = b;
2831 resume_scope (NAMESPACE_LEVEL (inner));
2832 current_namespace = inner;
2834 /* Restore template parameter scopes. */
2835 while (save_template_parm)
2837 cp_binding_level *b = save_template_parm;
2838 save_template_parm = b->level_chain;
2839 b->level_chain = current_binding_level;
2840 current_binding_level = b;
2843 else
2844 pushclass (inner);
2847 /* Enter the scope INNER from current scope. INNER must be a scope
2848 nested inside current scope. This works with both name lookup and
2849 pushing name into scope. In case a template parameter scope is present,
2850 namespace is pushed under the template parameter scope according to
2851 name lookup rule in 14.6.1/6.
2853 Return the former current scope suitable for pop_inner_scope. */
2855 tree
2856 push_inner_scope (tree inner)
2858 tree outer = current_scope ();
2859 if (!outer)
2860 outer = current_namespace;
2862 push_inner_scope_r (outer, inner);
2863 return outer;
2866 /* Exit the current scope INNER back to scope OUTER. */
2868 void
2869 pop_inner_scope (tree outer, tree inner)
2871 if (outer == inner
2872 || (TREE_CODE (inner) != NAMESPACE_DECL && !CLASS_TYPE_P (inner)))
2873 return;
2875 while (outer != inner)
2877 if (TREE_CODE (inner) == NAMESPACE_DECL)
2879 cp_binding_level *save_template_parm = 0;
2880 /* Temporary take out template parameter scopes. They are saved
2881 in reversed order in save_template_parm. */
2882 while (current_binding_level->kind == sk_template_parms)
2884 cp_binding_level *b = current_binding_level;
2885 current_binding_level = b->level_chain;
2886 b->level_chain = save_template_parm;
2887 save_template_parm = b;
2890 pop_namespace ();
2892 /* Restore template parameter scopes. */
2893 while (save_template_parm)
2895 cp_binding_level *b = save_template_parm;
2896 save_template_parm = b->level_chain;
2897 b->level_chain = current_binding_level;
2898 current_binding_level = b;
2901 else
2902 popclass ();
2904 inner = CP_DECL_CONTEXT (TREE_CODE (inner) == NAMESPACE_DECL ? inner : TYPE_NAME (inner));
2908 /* Do a pushlevel for class declarations. */
2910 void
2911 pushlevel_class (void)
2913 class_binding_level = begin_scope (sk_class, current_class_type);
2916 /* ...and a poplevel for class declarations. */
2918 void
2919 poplevel_class (void)
2921 cp_binding_level *level = class_binding_level;
2922 cp_class_binding *cb;
2923 size_t i;
2924 tree shadowed;
2926 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
2927 gcc_assert (level != 0);
2929 /* If we're leaving a toplevel class, cache its binding level. */
2930 if (current_class_depth == 1)
2931 previous_class_level = level;
2932 for (shadowed = level->type_shadowed;
2933 shadowed;
2934 shadowed = TREE_CHAIN (shadowed))
2935 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (shadowed), TREE_VALUE (shadowed));
2937 /* Remove the bindings for all of the class-level declarations. */
2938 if (level->class_shadowed)
2940 FOR_EACH_VEC_ELT (*level->class_shadowed, i, cb)
2942 IDENTIFIER_BINDING (cb->identifier) = cb->base->previous;
2943 cxx_binding_free (cb->base);
2945 ggc_free (level->class_shadowed);
2946 level->class_shadowed = NULL;
2949 /* Now, pop out of the binding level which we created up in the
2950 `pushlevel_class' routine. */
2951 gcc_assert (current_binding_level == level);
2952 leave_scope ();
2953 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
2956 /* Set INHERITED_VALUE_BINDING_P on BINDING to true or false, as
2957 appropriate. DECL is the value to which a name has just been
2958 bound. CLASS_TYPE is the class in which the lookup occurred. */
2960 static void
2961 set_inherited_value_binding_p (cxx_binding *binding, tree decl,
2962 tree class_type)
2964 if (binding->value == decl && TREE_CODE (decl) != TREE_LIST)
2966 tree context;
2968 if (TREE_CODE (decl) == OVERLOAD)
2969 context = ovl_scope (decl);
2970 else
2972 gcc_assert (DECL_P (decl));
2973 context = context_for_name_lookup (decl);
2976 if (is_properly_derived_from (class_type, context))
2977 INHERITED_VALUE_BINDING_P (binding) = 1;
2978 else
2979 INHERITED_VALUE_BINDING_P (binding) = 0;
2981 else if (binding->value == decl)
2982 /* We only encounter a TREE_LIST when there is an ambiguity in the
2983 base classes. Such an ambiguity can be overridden by a
2984 definition in this class. */
2985 INHERITED_VALUE_BINDING_P (binding) = 1;
2986 else
2987 INHERITED_VALUE_BINDING_P (binding) = 0;
2990 /* Make the declaration of X appear in CLASS scope. */
2992 bool
2993 pushdecl_class_level (tree x)
2995 tree name;
2996 bool is_valid = true;
2997 bool subtime;
2999 /* Do nothing if we're adding to an outer lambda closure type,
3000 outer_binding will add it later if it's needed. */
3001 if (current_class_type != class_binding_level->this_entity)
3002 return true;
3004 subtime = timevar_cond_start (TV_NAME_LOOKUP);
3005 /* Get the name of X. */
3006 if (TREE_CODE (x) == OVERLOAD)
3007 name = DECL_NAME (get_first_fn (x));
3008 else
3009 name = DECL_NAME (x);
3011 if (name)
3013 is_valid = push_class_level_binding (name, x);
3014 if (TREE_CODE (x) == TYPE_DECL)
3015 set_identifier_type_value (name, x);
3017 else if (ANON_AGGR_TYPE_P (TREE_TYPE (x)))
3019 /* If X is an anonymous aggregate, all of its members are
3020 treated as if they were members of the class containing the
3021 aggregate, for naming purposes. */
3022 tree f;
3024 for (f = TYPE_FIELDS (TREE_TYPE (x)); f; f = DECL_CHAIN (f))
3026 location_t save_location = input_location;
3027 input_location = DECL_SOURCE_LOCATION (f);
3028 if (!pushdecl_class_level (f))
3029 is_valid = false;
3030 input_location = save_location;
3033 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3034 return is_valid;
3037 /* Return the BINDING (if any) for NAME in SCOPE, which is a class
3038 scope. If the value returned is non-NULL, and the PREVIOUS field
3039 is not set, callers must set the PREVIOUS field explicitly. */
3041 static cxx_binding *
3042 get_class_binding (tree name, cp_binding_level *scope)
3044 tree class_type;
3045 tree type_binding;
3046 tree value_binding;
3047 cxx_binding *binding;
3049 class_type = scope->this_entity;
3051 /* Get the type binding. */
3052 type_binding = lookup_member (class_type, name,
3053 /*protect=*/2, /*want_type=*/true,
3054 tf_warning_or_error);
3055 /* Get the value binding. */
3056 value_binding = lookup_member (class_type, name,
3057 /*protect=*/2, /*want_type=*/false,
3058 tf_warning_or_error);
3060 if (value_binding
3061 && (TREE_CODE (value_binding) == TYPE_DECL
3062 || DECL_CLASS_TEMPLATE_P (value_binding)
3063 || (TREE_CODE (value_binding) == TREE_LIST
3064 && TREE_TYPE (value_binding) == error_mark_node
3065 && (TREE_CODE (TREE_VALUE (value_binding))
3066 == TYPE_DECL))))
3067 /* We found a type binding, even when looking for a non-type
3068 binding. This means that we already processed this binding
3069 above. */
3071 else if (value_binding)
3073 if (TREE_CODE (value_binding) == TREE_LIST
3074 && TREE_TYPE (value_binding) == error_mark_node)
3075 /* NAME is ambiguous. */
3077 else if (BASELINK_P (value_binding))
3078 /* NAME is some overloaded functions. */
3079 value_binding = BASELINK_FUNCTIONS (value_binding);
3082 /* If we found either a type binding or a value binding, create a
3083 new binding object. */
3084 if (type_binding || value_binding)
3086 binding = new_class_binding (name,
3087 value_binding,
3088 type_binding,
3089 scope);
3090 /* This is a class-scope binding, not a block-scope binding. */
3091 LOCAL_BINDING_P (binding) = 0;
3092 set_inherited_value_binding_p (binding, value_binding, class_type);
3094 else
3095 binding = NULL;
3097 return binding;
3100 /* Make the declaration(s) of X appear in CLASS scope under the name
3101 NAME. Returns true if the binding is valid. */
3103 static bool
3104 push_class_level_binding_1 (tree name, tree x)
3106 cxx_binding *binding;
3107 tree decl = x;
3108 bool ok;
3110 /* The class_binding_level will be NULL if x is a template
3111 parameter name in a member template. */
3112 if (!class_binding_level)
3113 return true;
3115 if (name == error_mark_node)
3116 return false;
3118 /* Can happen for an erroneous declaration (c++/60384). */
3119 if (!identifier_p (name))
3121 gcc_assert (errorcount || sorrycount);
3122 return false;
3125 /* Check for invalid member names. But don't worry about a default
3126 argument-scope lambda being pushed after the class is complete. */
3127 gcc_assert (TYPE_BEING_DEFINED (current_class_type)
3128 || LAMBDA_TYPE_P (TREE_TYPE (decl)));
3129 /* Check that we're pushing into the right binding level. */
3130 gcc_assert (current_class_type == class_binding_level->this_entity);
3132 /* We could have been passed a tree list if this is an ambiguous
3133 declaration. If so, pull the declaration out because
3134 check_template_shadow will not handle a TREE_LIST. */
3135 if (TREE_CODE (decl) == TREE_LIST
3136 && TREE_TYPE (decl) == error_mark_node)
3137 decl = TREE_VALUE (decl);
3139 if (!check_template_shadow (decl))
3140 return false;
3142 /* [class.mem]
3144 If T is the name of a class, then each of the following shall
3145 have a name different from T:
3147 -- every static data member of class T;
3149 -- every member of class T that is itself a type;
3151 -- every enumerator of every member of class T that is an
3152 enumerated type;
3154 -- every member of every anonymous union that is a member of
3155 class T.
3157 (Non-static data members were also forbidden to have the same
3158 name as T until TC1.) */
3159 if ((VAR_P (x)
3160 || TREE_CODE (x) == CONST_DECL
3161 || (TREE_CODE (x) == TYPE_DECL
3162 && !DECL_SELF_REFERENCE_P (x))
3163 /* A data member of an anonymous union. */
3164 || (TREE_CODE (x) == FIELD_DECL
3165 && DECL_CONTEXT (x) != current_class_type))
3166 && DECL_NAME (x) == constructor_name (current_class_type))
3168 tree scope = context_for_name_lookup (x);
3169 if (TYPE_P (scope) && same_type_p (scope, current_class_type))
3171 error ("%qD has the same name as the class in which it is "
3172 "declared",
3174 return false;
3178 /* Get the current binding for NAME in this class, if any. */
3179 binding = IDENTIFIER_BINDING (name);
3180 if (!binding || binding->scope != class_binding_level)
3182 binding = get_class_binding (name, class_binding_level);
3183 /* If a new binding was created, put it at the front of the
3184 IDENTIFIER_BINDING list. */
3185 if (binding)
3187 binding->previous = IDENTIFIER_BINDING (name);
3188 IDENTIFIER_BINDING (name) = binding;
3192 /* If there is already a binding, then we may need to update the
3193 current value. */
3194 if (binding && binding->value)
3196 tree bval = binding->value;
3197 tree old_decl = NULL_TREE;
3198 tree target_decl = strip_using_decl (decl);
3199 tree target_bval = strip_using_decl (bval);
3201 if (INHERITED_VALUE_BINDING_P (binding))
3203 /* If the old binding was from a base class, and was for a
3204 tag name, slide it over to make room for the new binding.
3205 The old binding is still visible if explicitly qualified
3206 with a class-key. */
3207 if (TREE_CODE (target_bval) == TYPE_DECL
3208 && DECL_ARTIFICIAL (target_bval)
3209 && !(TREE_CODE (target_decl) == TYPE_DECL
3210 && DECL_ARTIFICIAL (target_decl)))
3212 old_decl = binding->type;
3213 binding->type = bval;
3214 binding->value = NULL_TREE;
3215 INHERITED_VALUE_BINDING_P (binding) = 0;
3217 else
3219 old_decl = bval;
3220 /* Any inherited type declaration is hidden by the type
3221 declaration in the derived class. */
3222 if (TREE_CODE (target_decl) == TYPE_DECL
3223 && DECL_ARTIFICIAL (target_decl))
3224 binding->type = NULL_TREE;
3227 else if (TREE_CODE (target_decl) == OVERLOAD
3228 && is_overloaded_fn (target_bval))
3229 old_decl = bval;
3230 else if (TREE_CODE (decl) == USING_DECL
3231 && TREE_CODE (bval) == USING_DECL
3232 && same_type_p (USING_DECL_SCOPE (decl),
3233 USING_DECL_SCOPE (bval)))
3234 /* This is a using redeclaration that will be diagnosed later
3235 in supplement_binding */
3237 else if (TREE_CODE (decl) == USING_DECL
3238 && TREE_CODE (bval) == USING_DECL
3239 && DECL_DEPENDENT_P (decl)
3240 && DECL_DEPENDENT_P (bval))
3241 return true;
3242 else if (TREE_CODE (decl) == USING_DECL
3243 && is_overloaded_fn (target_bval))
3244 old_decl = bval;
3245 else if (TREE_CODE (bval) == USING_DECL
3246 && is_overloaded_fn (target_decl))
3247 return true;
3249 if (old_decl && binding->scope == class_binding_level)
3251 binding->value = x;
3252 /* It is always safe to clear INHERITED_VALUE_BINDING_P
3253 here. This function is only used to register bindings
3254 from with the class definition itself. */
3255 INHERITED_VALUE_BINDING_P (binding) = 0;
3256 return true;
3260 /* Note that we declared this value so that we can issue an error if
3261 this is an invalid redeclaration of a name already used for some
3262 other purpose. */
3263 note_name_declared_in_class (name, decl);
3265 /* If we didn't replace an existing binding, put the binding on the
3266 stack of bindings for the identifier, and update the shadowed
3267 list. */
3268 if (binding && binding->scope == class_binding_level)
3269 /* Supplement the existing binding. */
3270 ok = supplement_binding (binding, decl);
3271 else
3273 /* Create a new binding. */
3274 push_binding (name, decl, class_binding_level);
3275 ok = true;
3278 return ok;
3281 /* Wrapper for push_class_level_binding_1. */
3283 bool
3284 push_class_level_binding (tree name, tree x)
3286 bool ret;
3287 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3288 ret = push_class_level_binding_1 (name, x);
3289 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3290 return ret;
3293 /* Process "using SCOPE::NAME" in a class scope. Return the
3294 USING_DECL created. */
3296 tree
3297 do_class_using_decl (tree scope, tree name)
3299 /* The USING_DECL returned by this function. */
3300 tree value;
3301 /* The declaration (or declarations) name by this using
3302 declaration. NULL if we are in a template and cannot figure out
3303 what has been named. */
3304 tree decl;
3305 /* True if SCOPE is a dependent type. */
3306 bool scope_dependent_p;
3307 /* True if SCOPE::NAME is dependent. */
3308 bool name_dependent_p;
3309 /* True if any of the bases of CURRENT_CLASS_TYPE are dependent. */
3310 bool bases_dependent_p;
3311 tree binfo;
3312 tree base_binfo;
3313 int i;
3315 if (name == error_mark_node)
3316 return NULL_TREE;
3318 if (!scope || !TYPE_P (scope))
3320 error ("using-declaration for non-member at class scope");
3321 return NULL_TREE;
3324 /* Make sure the name is not invalid */
3325 if (TREE_CODE (name) == BIT_NOT_EXPR)
3327 error ("%<%T::%D%> names destructor", scope, name);
3328 return NULL_TREE;
3330 /* Using T::T declares inheriting ctors, even if T is a typedef. */
3331 if (MAYBE_CLASS_TYPE_P (scope)
3332 && ((TYPE_NAME (scope) && name == TYPE_IDENTIFIER (scope))
3333 || constructor_name_p (name, scope)))
3335 maybe_warn_cpp0x (CPP0X_INHERITING_CTORS);
3336 name = ctor_identifier;
3338 if (constructor_name_p (name, current_class_type))
3340 error ("%<%T::%D%> names constructor in %qT",
3341 scope, name, current_class_type);
3342 return NULL_TREE;
3345 scope_dependent_p = dependent_scope_p (scope);
3346 name_dependent_p = (scope_dependent_p
3347 || (IDENTIFIER_TYPENAME_P (name)
3348 && dependent_type_p (TREE_TYPE (name))));
3350 bases_dependent_p = false;
3351 if (processing_template_decl)
3352 for (binfo = TYPE_BINFO (current_class_type), i = 0;
3353 BINFO_BASE_ITERATE (binfo, i, base_binfo);
3354 i++)
3355 if (dependent_type_p (TREE_TYPE (base_binfo)))
3357 bases_dependent_p = true;
3358 break;
3361 decl = NULL_TREE;
3363 /* From [namespace.udecl]:
3365 A using-declaration used as a member-declaration shall refer to a
3366 member of a base class of the class being defined.
3368 In general, we cannot check this constraint in a template because
3369 we do not know the entire set of base classes of the current
3370 class type. Morover, if SCOPE is dependent, it might match a
3371 non-dependent base. */
3373 if (!scope_dependent_p)
3375 base_kind b_kind;
3376 binfo = lookup_base (current_class_type, scope, ba_any, &b_kind,
3377 tf_warning_or_error);
3378 if (b_kind < bk_proper_base)
3380 if (!bases_dependent_p)
3382 error_not_base_type (scope, current_class_type);
3383 return NULL_TREE;
3386 else if (!name_dependent_p)
3388 decl = lookup_member (binfo, name, 0, false, tf_warning_or_error);
3389 if (!decl)
3391 error ("no members matching %<%T::%D%> in %q#T", scope, name,
3392 scope);
3393 return NULL_TREE;
3395 /* The binfo from which the functions came does not matter. */
3396 if (BASELINK_P (decl))
3397 decl = BASELINK_FUNCTIONS (decl);
3401 value = build_lang_decl (USING_DECL, name, NULL_TREE);
3402 USING_DECL_DECLS (value) = decl;
3403 USING_DECL_SCOPE (value) = scope;
3404 DECL_DEPENDENT_P (value) = !decl;
3406 return value;
3410 /* Return the binding value for name in scope. */
3413 static tree
3414 namespace_binding_1 (tree name, tree scope)
3416 cxx_binding *binding;
3418 if (SCOPE_FILE_SCOPE_P (scope))
3419 scope = global_namespace;
3420 else
3421 /* Unnecessary for the global namespace because it can't be an alias. */
3422 scope = ORIGINAL_NAMESPACE (scope);
3424 binding = cp_binding_level_find_binding_for_name (NAMESPACE_LEVEL (scope), name);
3426 return binding ? binding->value : NULL_TREE;
3429 tree
3430 namespace_binding (tree name, tree scope)
3432 tree ret;
3433 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3434 ret = namespace_binding_1 (name, scope);
3435 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3436 return ret;
3439 /* Set the binding value for name in scope. */
3441 static void
3442 set_namespace_binding_1 (tree name, tree scope, tree val)
3444 cxx_binding *b;
3446 if (scope == NULL_TREE)
3447 scope = global_namespace;
3448 b = binding_for_name (NAMESPACE_LEVEL (scope), name);
3449 if (!b->value || TREE_CODE (val) == OVERLOAD || val == error_mark_node)
3450 b->value = val;
3451 else
3452 supplement_binding (b, val);
3455 /* Wrapper for set_namespace_binding_1. */
3457 void
3458 set_namespace_binding (tree name, tree scope, tree val)
3460 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3461 set_namespace_binding_1 (name, scope, val);
3462 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3465 /* Set the context of a declaration to scope. Complain if we are not
3466 outside scope. */
3468 void
3469 set_decl_namespace (tree decl, tree scope, bool friendp)
3471 tree old;
3473 /* Get rid of namespace aliases. */
3474 scope = ORIGINAL_NAMESPACE (scope);
3476 /* It is ok for friends to be qualified in parallel space. */
3477 if (!friendp && !is_ancestor (current_namespace, scope))
3478 error ("declaration of %qD not in a namespace surrounding %qD",
3479 decl, scope);
3480 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
3482 /* Writing "int N::i" to declare a variable within "N" is invalid. */
3483 if (scope == current_namespace)
3485 if (at_namespace_scope_p ())
3486 error ("explicit qualification in declaration of %qD",
3487 decl);
3488 return;
3491 /* See whether this has been declared in the namespace. */
3492 old = lookup_qualified_name (scope, DECL_NAME (decl), false, true);
3493 if (old == error_mark_node)
3494 /* No old declaration at all. */
3495 goto complain;
3496 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
3497 if (TREE_CODE (old) == TREE_LIST)
3499 error ("reference to %qD is ambiguous", decl);
3500 print_candidates (old);
3501 return;
3503 if (!is_overloaded_fn (decl))
3505 /* We might have found OLD in an inline namespace inside SCOPE. */
3506 if (TREE_CODE (decl) == TREE_CODE (old))
3507 DECL_CONTEXT (decl) = DECL_CONTEXT (old);
3508 /* Don't compare non-function decls with decls_match here, since
3509 it can't check for the correct constness at this
3510 point. pushdecl will find those errors later. */
3511 return;
3513 /* Since decl is a function, old should contain a function decl. */
3514 if (!is_overloaded_fn (old))
3515 goto complain;
3516 /* A template can be explicitly specialized in any namespace. */
3517 if (processing_explicit_instantiation)
3518 return;
3519 if (processing_template_decl || processing_specialization)
3520 /* We have not yet called push_template_decl to turn a
3521 FUNCTION_DECL into a TEMPLATE_DECL, so the declarations won't
3522 match. But, we'll check later, when we construct the
3523 template. */
3524 return;
3525 /* Instantiations or specializations of templates may be declared as
3526 friends in any namespace. */
3527 if (friendp && DECL_USE_TEMPLATE (decl))
3528 return;
3529 if (is_overloaded_fn (old))
3531 tree found = NULL_TREE;
3532 tree elt = old;
3533 for (; elt; elt = OVL_NEXT (elt))
3535 tree ofn = OVL_CURRENT (elt);
3536 /* Adjust DECL_CONTEXT first so decls_match will return true
3537 if DECL will match a declaration in an inline namespace. */
3538 DECL_CONTEXT (decl) = DECL_CONTEXT (ofn);
3539 if (decls_match (decl, ofn))
3541 if (found && !decls_match (found, ofn))
3543 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
3544 error ("reference to %qD is ambiguous", decl);
3545 print_candidates (old);
3546 return;
3548 found = ofn;
3551 if (found)
3553 if (!is_associated_namespace (scope, CP_DECL_CONTEXT (found)))
3554 goto complain;
3555 DECL_CONTEXT (decl) = DECL_CONTEXT (found);
3556 return;
3559 else
3561 DECL_CONTEXT (decl) = DECL_CONTEXT (old);
3562 if (decls_match (decl, old))
3563 return;
3566 /* It didn't work, go back to the explicit scope. */
3567 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
3568 complain:
3569 error ("%qD should have been declared inside %qD", decl, scope);
3572 /* Return the namespace where the current declaration is declared. */
3574 tree
3575 current_decl_namespace (void)
3577 tree result;
3578 /* If we have been pushed into a different namespace, use it. */
3579 if (!vec_safe_is_empty (decl_namespace_list))
3580 return decl_namespace_list->last ();
3582 if (current_class_type)
3583 result = decl_namespace_context (current_class_type);
3584 else if (current_function_decl)
3585 result = decl_namespace_context (current_function_decl);
3586 else
3587 result = current_namespace;
3588 return result;
3591 /* Process any ATTRIBUTES on a namespace definition. Currently only
3592 attribute visibility is meaningful, which is a property of the syntactic
3593 block rather than the namespace as a whole, so we don't touch the
3594 NAMESPACE_DECL at all. Returns true if attribute visibility is seen. */
3596 bool
3597 handle_namespace_attrs (tree ns, tree attributes)
3599 tree d;
3600 bool saw_vis = false;
3602 for (d = attributes; d; d = TREE_CHAIN (d))
3604 tree name = get_attribute_name (d);
3605 tree args = TREE_VALUE (d);
3607 if (is_attribute_p ("visibility", name))
3609 tree x = args ? TREE_VALUE (args) : NULL_TREE;
3610 if (x == NULL_TREE || TREE_CODE (x) != STRING_CST || TREE_CHAIN (args))
3612 warning (OPT_Wattributes,
3613 "%qD attribute requires a single NTBS argument",
3614 name);
3615 continue;
3618 if (!TREE_PUBLIC (ns))
3619 warning (OPT_Wattributes,
3620 "%qD attribute is meaningless since members of the "
3621 "anonymous namespace get local symbols", name);
3623 push_visibility (TREE_STRING_POINTER (x), 1);
3624 saw_vis = true;
3626 else
3628 warning (OPT_Wattributes, "%qD attribute directive ignored",
3629 name);
3630 continue;
3634 return saw_vis;
3637 /* Push into the scope of the NAME namespace. If NAME is NULL_TREE, then we
3638 select a name that is unique to this compilation unit. */
3640 void
3641 push_namespace (tree name)
3643 tree d = NULL_TREE;
3644 bool need_new = true;
3645 bool implicit_use = false;
3646 bool anon = !name;
3648 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3650 /* We should not get here if the global_namespace is not yet constructed
3651 nor if NAME designates the global namespace: The global scope is
3652 constructed elsewhere. */
3653 gcc_assert (global_namespace != NULL && name != global_scope_name);
3655 if (anon)
3657 name = get_anonymous_namespace_name();
3658 d = IDENTIFIER_NAMESPACE_VALUE (name);
3659 if (d)
3660 /* Reopening anonymous namespace. */
3661 need_new = false;
3662 implicit_use = true;
3664 else
3666 /* Check whether this is an extended namespace definition. */
3667 d = IDENTIFIER_NAMESPACE_VALUE (name);
3668 if (d != NULL_TREE && TREE_CODE (d) == NAMESPACE_DECL)
3670 tree dna = DECL_NAMESPACE_ALIAS (d);
3671 if (dna)
3673 /* We do some error recovery for, eg, the redeclaration
3674 of M here:
3676 namespace N {}
3677 namespace M = N;
3678 namespace M {}
3680 However, in nasty cases like:
3682 namespace N
3684 namespace M = N;
3685 namespace M {}
3688 we just error out below, in duplicate_decls. */
3689 if (NAMESPACE_LEVEL (dna)->level_chain
3690 == current_binding_level)
3692 error ("namespace alias %qD not allowed here, "
3693 "assuming %qD", d, dna);
3694 d = dna;
3695 need_new = false;
3698 else
3699 need_new = false;
3703 if (need_new)
3705 /* Make a new namespace, binding the name to it. */
3706 d = build_lang_decl (NAMESPACE_DECL, name, void_type_node);
3707 DECL_CONTEXT (d) = FROB_CONTEXT (current_namespace);
3708 /* The name of this namespace is not visible to other translation
3709 units if it is an anonymous namespace or member thereof. */
3710 if (anon || decl_anon_ns_mem_p (current_namespace))
3711 TREE_PUBLIC (d) = 0;
3712 else
3713 TREE_PUBLIC (d) = 1;
3714 pushdecl (d);
3715 if (anon)
3717 /* Clear DECL_NAME for the benefit of debugging back ends. */
3718 SET_DECL_ASSEMBLER_NAME (d, name);
3719 DECL_NAME (d) = NULL_TREE;
3721 begin_scope (sk_namespace, d);
3723 else
3724 resume_scope (NAMESPACE_LEVEL (d));
3726 if (implicit_use)
3727 do_using_directive (d);
3728 /* Enter the name space. */
3729 current_namespace = d;
3731 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3734 /* Pop from the scope of the current namespace. */
3736 void
3737 pop_namespace (void)
3739 gcc_assert (current_namespace != global_namespace);
3740 current_namespace = CP_DECL_CONTEXT (current_namespace);
3741 /* The binding level is not popped, as it might be re-opened later. */
3742 leave_scope ();
3745 /* Push into the scope of the namespace NS, even if it is deeply
3746 nested within another namespace. */
3748 void
3749 push_nested_namespace (tree ns)
3751 if (ns == global_namespace)
3752 push_to_top_level ();
3753 else
3755 push_nested_namespace (CP_DECL_CONTEXT (ns));
3756 push_namespace (DECL_NAME (ns));
3760 /* Pop back from the scope of the namespace NS, which was previously
3761 entered with push_nested_namespace. */
3763 void
3764 pop_nested_namespace (tree ns)
3766 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3767 gcc_assert (current_namespace == ns);
3768 while (ns != global_namespace)
3770 pop_namespace ();
3771 ns = CP_DECL_CONTEXT (ns);
3774 pop_from_top_level ();
3775 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3778 /* Temporarily set the namespace for the current declaration. */
3780 void
3781 push_decl_namespace (tree decl)
3783 if (TREE_CODE (decl) != NAMESPACE_DECL)
3784 decl = decl_namespace_context (decl);
3785 vec_safe_push (decl_namespace_list, ORIGINAL_NAMESPACE (decl));
3788 /* [namespace.memdef]/2 */
3790 void
3791 pop_decl_namespace (void)
3793 decl_namespace_list->pop ();
3796 /* Return the namespace that is the common ancestor
3797 of two given namespaces. */
3799 static tree
3800 namespace_ancestor_1 (tree ns1, tree ns2)
3802 tree nsr;
3803 if (is_ancestor (ns1, ns2))
3804 nsr = ns1;
3805 else
3806 nsr = namespace_ancestor_1 (CP_DECL_CONTEXT (ns1), ns2);
3807 return nsr;
3810 /* Wrapper for namespace_ancestor_1. */
3812 static tree
3813 namespace_ancestor (tree ns1, tree ns2)
3815 tree nsr;
3816 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3817 nsr = namespace_ancestor_1 (ns1, ns2);
3818 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3819 return nsr;
3822 /* Process a namespace-alias declaration. */
3824 void
3825 do_namespace_alias (tree alias, tree name_space)
3827 if (name_space == error_mark_node)
3828 return;
3830 gcc_assert (TREE_CODE (name_space) == NAMESPACE_DECL);
3832 name_space = ORIGINAL_NAMESPACE (name_space);
3834 /* Build the alias. */
3835 alias = build_lang_decl (NAMESPACE_DECL, alias, void_type_node);
3836 DECL_NAMESPACE_ALIAS (alias) = name_space;
3837 DECL_EXTERNAL (alias) = 1;
3838 DECL_CONTEXT (alias) = FROB_CONTEXT (current_scope ());
3839 pushdecl (alias);
3841 /* Emit debug info for namespace alias. */
3842 if (!building_stmt_list_p ())
3843 (*debug_hooks->global_decl) (alias);
3846 /* Like pushdecl, only it places X in the current namespace,
3847 if appropriate. */
3849 tree
3850 pushdecl_namespace_level (tree x, bool is_friend)
3852 cp_binding_level *b = current_binding_level;
3853 tree t;
3855 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3856 t = pushdecl_with_scope (x, NAMESPACE_LEVEL (current_namespace), is_friend);
3858 /* Now, the type_shadowed stack may screw us. Munge it so it does
3859 what we want. */
3860 if (TREE_CODE (t) == TYPE_DECL)
3862 tree name = DECL_NAME (t);
3863 tree newval;
3864 tree *ptr = (tree *)0;
3865 for (; !global_scope_p (b); b = b->level_chain)
3867 tree shadowed = b->type_shadowed;
3868 for (; shadowed; shadowed = TREE_CHAIN (shadowed))
3869 if (TREE_PURPOSE (shadowed) == name)
3871 ptr = &TREE_VALUE (shadowed);
3872 /* Can't break out of the loop here because sometimes
3873 a binding level will have duplicate bindings for
3874 PT names. It's gross, but I haven't time to fix it. */
3877 newval = TREE_TYPE (t);
3878 if (ptr == (tree *)0)
3880 /* @@ This shouldn't be needed. My test case "zstring.cc" trips
3881 up here if this is changed to an assertion. --KR */
3882 SET_IDENTIFIER_TYPE_VALUE (name, t);
3884 else
3886 *ptr = newval;
3889 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3890 return t;
3893 /* Insert USED into the using list of USER. Set INDIRECT_flag if this
3894 directive is not directly from the source. Also find the common
3895 ancestor and let our users know about the new namespace */
3897 static void
3898 add_using_namespace_1 (tree user, tree used, bool indirect)
3900 tree t;
3901 /* Using oneself is a no-op. */
3902 if (user == used)
3903 return;
3904 gcc_assert (TREE_CODE (user) == NAMESPACE_DECL);
3905 gcc_assert (TREE_CODE (used) == NAMESPACE_DECL);
3906 /* Check if we already have this. */
3907 t = purpose_member (used, DECL_NAMESPACE_USING (user));
3908 if (t != NULL_TREE)
3910 if (!indirect)
3911 /* Promote to direct usage. */
3912 TREE_INDIRECT_USING (t) = 0;
3913 return;
3916 /* Add used to the user's using list. */
3917 DECL_NAMESPACE_USING (user)
3918 = tree_cons (used, namespace_ancestor (user, used),
3919 DECL_NAMESPACE_USING (user));
3921 TREE_INDIRECT_USING (DECL_NAMESPACE_USING (user)) = indirect;
3923 /* Add user to the used's users list. */
3924 DECL_NAMESPACE_USERS (used)
3925 = tree_cons (user, 0, DECL_NAMESPACE_USERS (used));
3927 /* Recursively add all namespaces used. */
3928 for (t = DECL_NAMESPACE_USING (used); t; t = TREE_CHAIN (t))
3929 /* indirect usage */
3930 add_using_namespace_1 (user, TREE_PURPOSE (t), 1);
3932 /* Tell everyone using us about the new used namespaces. */
3933 for (t = DECL_NAMESPACE_USERS (user); t; t = TREE_CHAIN (t))
3934 add_using_namespace_1 (TREE_PURPOSE (t), used, 1);
3937 /* Wrapper for add_using_namespace_1. */
3939 static void
3940 add_using_namespace (tree user, tree used, bool indirect)
3942 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3943 add_using_namespace_1 (user, used, indirect);
3944 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3947 /* Process a using-declaration not appearing in class or local scope. */
3949 void
3950 do_toplevel_using_decl (tree decl, tree scope, tree name)
3952 tree oldval, oldtype, newval, newtype;
3953 tree orig_decl = decl;
3954 cxx_binding *binding;
3956 decl = validate_nonmember_using_decl (decl, scope, name);
3957 if (decl == NULL_TREE)
3958 return;
3960 binding = binding_for_name (NAMESPACE_LEVEL (current_namespace), name);
3962 oldval = binding->value;
3963 oldtype = binding->type;
3965 do_nonmember_using_decl (scope, name, oldval, oldtype, &newval, &newtype);
3967 /* Emit debug info. */
3968 if (!processing_template_decl)
3969 cp_emit_debug_info_for_using (orig_decl, current_namespace);
3971 /* Copy declarations found. */
3972 if (newval)
3973 binding->value = newval;
3974 if (newtype)
3975 binding->type = newtype;
3978 /* Process a using-directive. */
3980 void
3981 do_using_directive (tree name_space)
3983 tree context = NULL_TREE;
3985 if (name_space == error_mark_node)
3986 return;
3988 gcc_assert (TREE_CODE (name_space) == NAMESPACE_DECL);
3990 if (building_stmt_list_p ())
3991 add_stmt (build_stmt (input_location, USING_STMT, name_space));
3992 name_space = ORIGINAL_NAMESPACE (name_space);
3994 if (!toplevel_bindings_p ())
3996 push_using_directive (name_space);
3998 else
4000 /* direct usage */
4001 add_using_namespace (current_namespace, name_space, 0);
4002 if (current_namespace != global_namespace)
4003 context = current_namespace;
4005 /* Emit debugging info. */
4006 if (!processing_template_decl)
4007 (*debug_hooks->imported_module_or_decl) (name_space, NULL_TREE,
4008 context, false);
4012 /* Deal with a using-directive seen by the parser. Currently we only
4013 handle attributes here, since they cannot appear inside a template. */
4015 void
4016 parse_using_directive (tree name_space, tree attribs)
4018 tree a;
4020 do_using_directive (name_space);
4022 for (a = attribs; a; a = TREE_CHAIN (a))
4024 tree name = TREE_PURPOSE (a);
4025 if (is_attribute_p ("strong", name))
4027 if (!toplevel_bindings_p ())
4028 error ("strong using only meaningful at namespace scope");
4029 else if (name_space != error_mark_node)
4031 if (!is_ancestor (current_namespace, name_space))
4032 error ("current namespace %qD does not enclose strongly used namespace %qD",
4033 current_namespace, name_space);
4034 DECL_NAMESPACE_ASSOCIATIONS (name_space)
4035 = tree_cons (current_namespace, 0,
4036 DECL_NAMESPACE_ASSOCIATIONS (name_space));
4039 else
4040 warning (OPT_Wattributes, "%qD attribute directive ignored", name);
4044 /* Like pushdecl, only it places X in the global scope if appropriate.
4045 Calls cp_finish_decl to register the variable, initializing it with
4046 *INIT, if INIT is non-NULL. */
4048 static tree
4049 pushdecl_top_level_1 (tree x, tree *init, bool is_friend)
4051 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4052 push_to_top_level ();
4053 x = pushdecl_namespace_level (x, is_friend);
4054 if (init)
4055 cp_finish_decl (x, *init, false, NULL_TREE, 0);
4056 pop_from_top_level ();
4057 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4058 return x;
4061 /* Like pushdecl, only it places X in the global scope if appropriate. */
4063 tree
4064 pushdecl_top_level (tree x)
4066 return pushdecl_top_level_1 (x, NULL, false);
4069 /* Like pushdecl_top_level, but adding the IS_FRIEND parameter. */
4071 tree
4072 pushdecl_top_level_maybe_friend (tree x, bool is_friend)
4074 return pushdecl_top_level_1 (x, NULL, is_friend);
4077 /* Like pushdecl, only it places X in the global scope if
4078 appropriate. Calls cp_finish_decl to register the variable,
4079 initializing it with INIT. */
4081 tree
4082 pushdecl_top_level_and_finish (tree x, tree init)
4084 return pushdecl_top_level_1 (x, &init, false);
4087 /* Combines two sets of overloaded functions into an OVERLOAD chain, removing
4088 duplicates. The first list becomes the tail of the result.
4090 The algorithm is O(n^2). We could get this down to O(n log n) by
4091 doing a sort on the addresses of the functions, if that becomes
4092 necessary. */
4094 static tree
4095 merge_functions (tree s1, tree s2)
4097 for (; s2; s2 = OVL_NEXT (s2))
4099 tree fn2 = OVL_CURRENT (s2);
4100 tree fns1;
4102 for (fns1 = s1; fns1; fns1 = OVL_NEXT (fns1))
4104 tree fn1 = OVL_CURRENT (fns1);
4106 /* If the function from S2 is already in S1, there is no
4107 need to add it again. For `extern "C"' functions, we
4108 might have two FUNCTION_DECLs for the same function, in
4109 different namespaces, but let's leave them in in case
4110 they have different default arguments. */
4111 if (fn1 == fn2)
4112 break;
4115 /* If we exhausted all of the functions in S1, FN2 is new. */
4116 if (!fns1)
4117 s1 = build_overload (fn2, s1);
4119 return s1;
4122 /* Returns TRUE iff OLD and NEW are the same entity.
4124 3 [basic]/3: An entity is a value, object, reference, function,
4125 enumerator, type, class member, template, template specialization,
4126 namespace, parameter pack, or this.
4128 7.3.4 [namespace.udir]/4: If name lookup finds a declaration for a name
4129 in two different namespaces, and the declarations do not declare the
4130 same entity and do not declare functions, the use of the name is
4131 ill-formed. */
4133 static bool
4134 same_entity_p (tree one, tree two)
4136 if (one == two)
4137 return true;
4138 if (!one || !two)
4139 return false;
4140 if (TREE_CODE (one) == TYPE_DECL
4141 && TREE_CODE (two) == TYPE_DECL
4142 && same_type_p (TREE_TYPE (one), TREE_TYPE (two)))
4143 return true;
4144 return false;
4147 /* This should return an error not all definitions define functions.
4148 It is not an error if we find two functions with exactly the
4149 same signature, only if these are selected in overload resolution.
4150 old is the current set of bindings, new_binding the freshly-found binding.
4151 XXX Do we want to give *all* candidates in case of ambiguity?
4152 XXX In what way should I treat extern declarations?
4153 XXX I don't want to repeat the entire duplicate_decls here */
4155 static void
4156 ambiguous_decl (struct scope_binding *old, cxx_binding *new_binding, int flags)
4158 tree val, type;
4159 gcc_assert (old != NULL);
4161 /* Copy the type. */
4162 type = new_binding->type;
4163 if (LOOKUP_NAMESPACES_ONLY (flags)
4164 || (type && hidden_name_p (type) && !(flags & LOOKUP_HIDDEN)))
4165 type = NULL_TREE;
4167 /* Copy the value. */
4168 val = new_binding->value;
4169 if (val)
4171 if (hidden_name_p (val) && !(flags & LOOKUP_HIDDEN))
4172 val = NULL_TREE;
4173 else
4174 switch (TREE_CODE (val))
4176 case TEMPLATE_DECL:
4177 /* If we expect types or namespaces, and not templates,
4178 or this is not a template class. */
4179 if ((LOOKUP_QUALIFIERS_ONLY (flags)
4180 && !DECL_TYPE_TEMPLATE_P (val)))
4181 val = NULL_TREE;
4182 break;
4183 case TYPE_DECL:
4184 if (LOOKUP_NAMESPACES_ONLY (flags)
4185 || (type && (flags & LOOKUP_PREFER_TYPES)))
4186 val = NULL_TREE;
4187 break;
4188 case NAMESPACE_DECL:
4189 if (LOOKUP_TYPES_ONLY (flags))
4190 val = NULL_TREE;
4191 break;
4192 case FUNCTION_DECL:
4193 /* Ignore built-in functions that are still anticipated. */
4194 if (LOOKUP_QUALIFIERS_ONLY (flags))
4195 val = NULL_TREE;
4196 break;
4197 default:
4198 if (LOOKUP_QUALIFIERS_ONLY (flags))
4199 val = NULL_TREE;
4203 /* If val is hidden, shift down any class or enumeration name. */
4204 if (!val)
4206 val = type;
4207 type = NULL_TREE;
4210 if (!old->value)
4211 old->value = val;
4212 else if (val && !same_entity_p (val, old->value))
4214 if (is_overloaded_fn (old->value) && is_overloaded_fn (val))
4215 old->value = merge_functions (old->value, val);
4216 else
4218 old->value = tree_cons (NULL_TREE, old->value,
4219 build_tree_list (NULL_TREE, val));
4220 TREE_TYPE (old->value) = error_mark_node;
4224 if (!old->type)
4225 old->type = type;
4226 else if (type && old->type != type)
4228 old->type = tree_cons (NULL_TREE, old->type,
4229 build_tree_list (NULL_TREE, type));
4230 TREE_TYPE (old->type) = error_mark_node;
4234 /* Return the declarations that are members of the namespace NS. */
4236 tree
4237 cp_namespace_decls (tree ns)
4239 return NAMESPACE_LEVEL (ns)->names;
4242 /* Combine prefer_type and namespaces_only into flags. */
4244 static int
4245 lookup_flags (int prefer_type, int namespaces_only)
4247 if (namespaces_only)
4248 return LOOKUP_PREFER_NAMESPACES;
4249 if (prefer_type > 1)
4250 return LOOKUP_PREFER_TYPES;
4251 if (prefer_type > 0)
4252 return LOOKUP_PREFER_BOTH;
4253 return 0;
4256 /* Given a lookup that returned VAL, use FLAGS to decide if we want to
4257 ignore it or not. Subroutine of lookup_name_real and
4258 lookup_type_scope. */
4260 static bool
4261 qualify_lookup (tree val, int flags)
4263 if (val == NULL_TREE)
4264 return false;
4265 if ((flags & LOOKUP_PREFER_NAMESPACES) && TREE_CODE (val) == NAMESPACE_DECL)
4266 return true;
4267 if (flags & LOOKUP_PREFER_TYPES)
4269 tree target_val = strip_using_decl (val);
4270 if (TREE_CODE (target_val) == TYPE_DECL
4271 || TREE_CODE (target_val) == TEMPLATE_DECL)
4272 return true;
4274 if (flags & (LOOKUP_PREFER_NAMESPACES | LOOKUP_PREFER_TYPES))
4275 return false;
4276 /* Look through lambda things that we shouldn't be able to see. */
4277 if (is_lambda_ignored_entity (val))
4278 return false;
4279 return true;
4282 /* Given a lookup that returned VAL, decide if we want to ignore it or
4283 not based on DECL_ANTICIPATED. */
4285 bool
4286 hidden_name_p (tree val)
4288 if (DECL_P (val)
4289 && DECL_LANG_SPECIFIC (val)
4290 && TYPE_FUNCTION_OR_TEMPLATE_DECL_P (val)
4291 && DECL_ANTICIPATED (val))
4292 return true;
4293 return false;
4296 /* Remove any hidden friend functions from a possibly overloaded set
4297 of functions. */
4299 tree
4300 remove_hidden_names (tree fns)
4302 if (!fns)
4303 return fns;
4305 if (TREE_CODE (fns) == FUNCTION_DECL && hidden_name_p (fns))
4306 fns = NULL_TREE;
4307 else if (TREE_CODE (fns) == OVERLOAD)
4309 tree o;
4311 for (o = fns; o; o = OVL_NEXT (o))
4312 if (hidden_name_p (OVL_CURRENT (o)))
4313 break;
4314 if (o)
4316 tree n = NULL_TREE;
4318 for (o = fns; o; o = OVL_NEXT (o))
4319 if (!hidden_name_p (OVL_CURRENT (o)))
4320 n = build_overload (OVL_CURRENT (o), n);
4321 fns = n;
4325 return fns;
4328 /* Suggest alternatives for NAME, an IDENTIFIER_NODE for which name
4329 lookup failed. Search through all available namespaces and print out
4330 possible candidates. */
4332 void
4333 suggest_alternatives_for (location_t location, tree name)
4335 vec<tree> candidates = vNULL;
4336 vec<tree> namespaces_to_search = vNULL;
4337 int max_to_search = PARAM_VALUE (CXX_MAX_NAMESPACES_FOR_DIAGNOSTIC_HELP);
4338 int n_searched = 0;
4339 tree t;
4340 unsigned ix;
4342 namespaces_to_search.safe_push (global_namespace);
4344 while (!namespaces_to_search.is_empty ()
4345 && n_searched < max_to_search)
4347 tree scope = namespaces_to_search.pop ();
4348 struct scope_binding binding = EMPTY_SCOPE_BINDING;
4349 cp_binding_level *level = NAMESPACE_LEVEL (scope);
4351 /* Look in this namespace. */
4352 qualified_lookup_using_namespace (name, scope, &binding, 0);
4354 n_searched++;
4356 if (binding.value)
4357 candidates.safe_push (binding.value);
4359 /* Add child namespaces. */
4360 for (t = level->namespaces; t; t = DECL_CHAIN (t))
4361 namespaces_to_search.safe_push (t);
4364 /* If we stopped before we could examine all namespaces, inform the
4365 user. Do this even if we don't have any candidates, since there
4366 might be more candidates further down that we weren't able to
4367 find. */
4368 if (n_searched >= max_to_search
4369 && !namespaces_to_search.is_empty ())
4370 inform (location,
4371 "maximum limit of %d namespaces searched for %qE",
4372 max_to_search, name);
4374 namespaces_to_search.release ();
4376 /* Nothing useful to report. */
4377 if (candidates.is_empty ())
4378 return;
4380 inform_n (location, candidates.length (),
4381 "suggested alternative:",
4382 "suggested alternatives:");
4384 FOR_EACH_VEC_ELT (candidates, ix, t)
4385 inform (location_of (t), " %qE", t);
4387 candidates.release ();
4390 /* Unscoped lookup of a global: iterate over current namespaces,
4391 considering using-directives. */
4393 static tree
4394 unqualified_namespace_lookup_1 (tree name, int flags)
4396 tree initial = current_decl_namespace ();
4397 tree scope = initial;
4398 tree siter;
4399 cp_binding_level *level;
4400 tree val = NULL_TREE;
4402 for (; !val; scope = CP_DECL_CONTEXT (scope))
4404 struct scope_binding binding = EMPTY_SCOPE_BINDING;
4405 cxx_binding *b =
4406 cp_binding_level_find_binding_for_name (NAMESPACE_LEVEL (scope), name);
4408 if (b)
4409 ambiguous_decl (&binding, b, flags);
4411 /* Add all _DECLs seen through local using-directives. */
4412 for (level = current_binding_level;
4413 level->kind != sk_namespace;
4414 level = level->level_chain)
4415 if (!lookup_using_namespace (name, &binding, level->using_directives,
4416 scope, flags))
4417 /* Give up because of error. */
4418 return error_mark_node;
4420 /* Add all _DECLs seen through global using-directives. */
4421 /* XXX local and global using lists should work equally. */
4422 siter = initial;
4423 while (1)
4425 if (!lookup_using_namespace (name, &binding,
4426 DECL_NAMESPACE_USING (siter),
4427 scope, flags))
4428 /* Give up because of error. */
4429 return error_mark_node;
4430 if (siter == scope) break;
4431 siter = CP_DECL_CONTEXT (siter);
4434 val = binding.value;
4435 if (scope == global_namespace)
4436 break;
4438 return val;
4441 /* Wrapper for unqualified_namespace_lookup_1. */
4443 static tree
4444 unqualified_namespace_lookup (tree name, int flags)
4446 tree ret;
4447 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4448 ret = unqualified_namespace_lookup_1 (name, flags);
4449 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4450 return ret;
4453 /* Look up NAME (an IDENTIFIER_NODE) in SCOPE (either a NAMESPACE_DECL
4454 or a class TYPE). If IS_TYPE_P is TRUE, then ignore non-type
4455 bindings.
4457 Returns a DECL (or OVERLOAD, or BASELINK) representing the
4458 declaration found. If no suitable declaration can be found,
4459 ERROR_MARK_NODE is returned. If COMPLAIN is true and SCOPE is
4460 neither a class-type nor a namespace a diagnostic is issued. */
4462 tree
4463 lookup_qualified_name (tree scope, tree name, bool is_type_p, bool complain)
4465 int flags = 0;
4466 tree t = NULL_TREE;
4468 if (TREE_CODE (scope) == NAMESPACE_DECL)
4470 struct scope_binding binding = EMPTY_SCOPE_BINDING;
4472 if (is_type_p)
4473 flags |= LOOKUP_PREFER_TYPES;
4474 if (qualified_lookup_using_namespace (name, scope, &binding, flags))
4475 t = binding.value;
4477 else if (cxx_dialect != cxx98 && TREE_CODE (scope) == ENUMERAL_TYPE)
4478 t = lookup_enumerator (scope, name);
4479 else if (is_class_type (scope, complain))
4480 t = lookup_member (scope, name, 2, is_type_p, tf_warning_or_error);
4482 if (!t)
4483 return error_mark_node;
4484 return t;
4487 /* Subroutine of unqualified_namespace_lookup:
4488 Add the bindings of NAME in used namespaces to VAL.
4489 We are currently looking for names in namespace SCOPE, so we
4490 look through USINGS for using-directives of namespaces
4491 which have SCOPE as a common ancestor with the current scope.
4492 Returns false on errors. */
4494 static bool
4495 lookup_using_namespace (tree name, struct scope_binding *val,
4496 tree usings, tree scope, int flags)
4498 tree iter;
4499 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4500 /* Iterate over all used namespaces in current, searching for using
4501 directives of scope. */
4502 for (iter = usings; iter; iter = TREE_CHAIN (iter))
4503 if (TREE_VALUE (iter) == scope)
4505 tree used = ORIGINAL_NAMESPACE (TREE_PURPOSE (iter));
4506 cxx_binding *val1 =
4507 cp_binding_level_find_binding_for_name (NAMESPACE_LEVEL (used), name);
4508 /* Resolve ambiguities. */
4509 if (val1)
4510 ambiguous_decl (val, val1, flags);
4512 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4513 return val->value != error_mark_node;
4516 /* Returns true iff VEC contains TARGET. */
4518 static bool
4519 tree_vec_contains (vec<tree, va_gc> *vec, tree target)
4521 unsigned int i;
4522 tree elt;
4523 FOR_EACH_VEC_SAFE_ELT (vec,i,elt)
4524 if (elt == target)
4525 return true;
4526 return false;
4529 /* [namespace.qual]
4530 Accepts the NAME to lookup and its qualifying SCOPE.
4531 Returns the name/type pair found into the cxx_binding *RESULT,
4532 or false on error. */
4534 static bool
4535 qualified_lookup_using_namespace (tree name, tree scope,
4536 struct scope_binding *result, int flags)
4538 /* Maintain a list of namespaces visited... */
4539 vec<tree, va_gc> *seen = NULL;
4540 vec<tree, va_gc> *seen_inline = NULL;
4541 /* ... and a list of namespace yet to see. */
4542 vec<tree, va_gc> *todo = NULL;
4543 vec<tree, va_gc> *todo_maybe = NULL;
4544 vec<tree, va_gc> *todo_inline = NULL;
4545 tree usings;
4546 timevar_start (TV_NAME_LOOKUP);
4547 /* Look through namespace aliases. */
4548 scope = ORIGINAL_NAMESPACE (scope);
4550 /* Algorithm: Starting with SCOPE, walk through the set of used
4551 namespaces. For each used namespace, look through its inline
4552 namespace set for any bindings and usings. If no bindings are
4553 found, add any usings seen to the set of used namespaces. */
4554 vec_safe_push (todo, scope);
4556 while (todo->length ())
4558 bool found_here;
4559 scope = todo->pop ();
4560 if (tree_vec_contains (seen, scope))
4561 continue;
4562 vec_safe_push (seen, scope);
4563 vec_safe_push (todo_inline, scope);
4565 found_here = false;
4566 while (todo_inline->length ())
4568 cxx_binding *binding;
4570 scope = todo_inline->pop ();
4571 if (tree_vec_contains (seen_inline, scope))
4572 continue;
4573 vec_safe_push (seen_inline, scope);
4575 binding =
4576 cp_binding_level_find_binding_for_name (NAMESPACE_LEVEL (scope), name);
4577 if (binding)
4579 found_here = true;
4580 ambiguous_decl (result, binding, flags);
4583 for (usings = DECL_NAMESPACE_USING (scope); usings;
4584 usings = TREE_CHAIN (usings))
4585 if (!TREE_INDIRECT_USING (usings))
4587 if (is_associated_namespace (scope, TREE_PURPOSE (usings)))
4588 vec_safe_push (todo_inline, TREE_PURPOSE (usings));
4589 else
4590 vec_safe_push (todo_maybe, TREE_PURPOSE (usings));
4594 if (found_here)
4595 vec_safe_truncate (todo_maybe, 0);
4596 else
4597 while (vec_safe_length (todo_maybe))
4598 vec_safe_push (todo, todo_maybe->pop ());
4600 vec_free (todo);
4601 vec_free (todo_maybe);
4602 vec_free (todo_inline);
4603 vec_free (seen);
4604 vec_free (seen_inline);
4605 timevar_stop (TV_NAME_LOOKUP);
4606 return result->value != error_mark_node;
4609 /* Subroutine of outer_binding.
4611 Returns TRUE if BINDING is a binding to a template parameter of
4612 SCOPE. In that case SCOPE is the scope of a primary template
4613 parameter -- in the sense of G++, i.e, a template that has its own
4614 template header.
4616 Returns FALSE otherwise. */
4618 static bool
4619 binding_to_template_parms_of_scope_p (cxx_binding *binding,
4620 cp_binding_level *scope)
4622 tree binding_value, tmpl, tinfo;
4623 int level;
4625 if (!binding || !scope || !scope->this_entity)
4626 return false;
4628 binding_value = binding->value ? binding->value : binding->type;
4629 tinfo = get_template_info (scope->this_entity);
4631 /* BINDING_VALUE must be a template parm. */
4632 if (binding_value == NULL_TREE
4633 || (!DECL_P (binding_value)
4634 || !DECL_TEMPLATE_PARM_P (binding_value)))
4635 return false;
4637 /* The level of BINDING_VALUE. */
4638 level =
4639 template_type_parameter_p (binding_value)
4640 ? TEMPLATE_PARM_LEVEL (TEMPLATE_TYPE_PARM_INDEX
4641 (TREE_TYPE (binding_value)))
4642 : TEMPLATE_PARM_LEVEL (DECL_INITIAL (binding_value));
4644 /* The template of the current scope, iff said scope is a primary
4645 template. */
4646 tmpl = (tinfo
4647 && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
4648 ? TI_TEMPLATE (tinfo)
4649 : NULL_TREE);
4651 /* If the level of the parm BINDING_VALUE equals the depth of TMPL,
4652 then BINDING_VALUE is a parameter of TMPL. */
4653 return (tmpl && level == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)));
4656 /* Return the innermost non-namespace binding for NAME from a scope
4657 containing BINDING, or, if BINDING is NULL, the current scope.
4658 Please note that for a given template, the template parameters are
4659 considered to be in the scope containing the current scope.
4660 If CLASS_P is false, then class bindings are ignored. */
4662 cxx_binding *
4663 outer_binding (tree name,
4664 cxx_binding *binding,
4665 bool class_p)
4667 cxx_binding *outer;
4668 cp_binding_level *scope;
4669 cp_binding_level *outer_scope;
4671 if (binding)
4673 scope = binding->scope->level_chain;
4674 outer = binding->previous;
4676 else
4678 scope = current_binding_level;
4679 outer = IDENTIFIER_BINDING (name);
4681 outer_scope = outer ? outer->scope : NULL;
4683 /* Because we create class bindings lazily, we might be missing a
4684 class binding for NAME. If there are any class binding levels
4685 between the LAST_BINDING_LEVEL and the scope in which OUTER was
4686 declared, we must lookup NAME in those class scopes. */
4687 if (class_p)
4688 while (scope && scope != outer_scope && scope->kind != sk_namespace)
4690 if (scope->kind == sk_class)
4692 cxx_binding *class_binding;
4694 class_binding = get_class_binding (name, scope);
4695 if (class_binding)
4697 /* Thread this new class-scope binding onto the
4698 IDENTIFIER_BINDING list so that future lookups
4699 find it quickly. */
4700 class_binding->previous = outer;
4701 if (binding)
4702 binding->previous = class_binding;
4703 else
4704 IDENTIFIER_BINDING (name) = class_binding;
4705 return class_binding;
4708 /* If we are in a member template, the template parms of the member
4709 template are considered to be inside the scope of the containing
4710 class, but within G++ the class bindings are all pushed between the
4711 template parms and the function body. So if the outer binding is
4712 a template parm for the current scope, return it now rather than
4713 look for a class binding. */
4714 if (outer_scope && outer_scope->kind == sk_template_parms
4715 && binding_to_template_parms_of_scope_p (outer, scope))
4716 return outer;
4718 scope = scope->level_chain;
4721 return outer;
4724 /* Return the innermost block-scope or class-scope value binding for
4725 NAME, or NULL_TREE if there is no such binding. */
4727 tree
4728 innermost_non_namespace_value (tree name)
4730 cxx_binding *binding;
4731 binding = outer_binding (name, /*binding=*/NULL, /*class_p=*/true);
4732 return binding ? binding->value : NULL_TREE;
4735 /* Look up NAME in the current binding level and its superiors in the
4736 namespace of variables, functions and typedefs. Return a ..._DECL
4737 node of some kind representing its definition if there is only one
4738 such declaration, or return a TREE_LIST with all the overloaded
4739 definitions if there are many, or return 0 if it is undefined.
4740 Hidden name, either friend declaration or built-in function, are
4741 not ignored.
4743 If PREFER_TYPE is > 0, we prefer TYPE_DECLs or namespaces.
4744 If PREFER_TYPE is > 1, we reject non-type decls (e.g. namespaces).
4745 Otherwise we prefer non-TYPE_DECLs.
4747 If NONCLASS is nonzero, bindings in class scopes are ignored. If
4748 BLOCK_P is false, bindings in block scopes are ignored. */
4750 static tree
4751 lookup_name_real_1 (tree name, int prefer_type, int nonclass, bool block_p,
4752 int namespaces_only, int flags)
4754 cxx_binding *iter;
4755 tree val = NULL_TREE;
4757 /* Conversion operators are handled specially because ordinary
4758 unqualified name lookup will not find template conversion
4759 operators. */
4760 if (IDENTIFIER_TYPENAME_P (name))
4762 cp_binding_level *level;
4764 for (level = current_binding_level;
4765 level && level->kind != sk_namespace;
4766 level = level->level_chain)
4768 tree class_type;
4769 tree operators;
4771 /* A conversion operator can only be declared in a class
4772 scope. */
4773 if (level->kind != sk_class)
4774 continue;
4776 /* Lookup the conversion operator in the class. */
4777 class_type = level->this_entity;
4778 operators = lookup_fnfields (class_type, name, /*protect=*/0);
4779 if (operators)
4780 return operators;
4783 return NULL_TREE;
4786 flags |= lookup_flags (prefer_type, namespaces_only);
4788 /* First, look in non-namespace scopes. */
4790 if (current_class_type == NULL_TREE)
4791 nonclass = 1;
4793 if (block_p || !nonclass)
4794 for (iter = outer_binding (name, NULL, !nonclass);
4795 iter;
4796 iter = outer_binding (name, iter, !nonclass))
4798 tree binding;
4800 /* Skip entities we don't want. */
4801 if (LOCAL_BINDING_P (iter) ? !block_p : nonclass)
4802 continue;
4804 /* If this is the kind of thing we're looking for, we're done. */
4805 if (qualify_lookup (iter->value, flags))
4806 binding = iter->value;
4807 else if ((flags & LOOKUP_PREFER_TYPES)
4808 && qualify_lookup (iter->type, flags))
4809 binding = iter->type;
4810 else
4811 binding = NULL_TREE;
4813 if (binding)
4815 if (hidden_name_p (binding))
4817 /* A non namespace-scope binding can only be hidden in the
4818 presence of a local class, due to friend declarations.
4820 In particular, consider:
4822 struct C;
4823 void f() {
4824 struct A {
4825 friend struct B;
4826 friend struct C;
4827 void g() {
4828 B* b; // error: B is hidden
4829 C* c; // OK, finds ::C
4832 B *b; // error: B is hidden
4833 C *c; // OK, finds ::C
4834 struct B {};
4835 B *bb; // OK
4838 The standard says that "B" is a local class in "f"
4839 (but not nested within "A") -- but that name lookup
4840 for "B" does not find this declaration until it is
4841 declared directly with "f".
4843 In particular:
4845 [class.friend]
4847 If a friend declaration appears in a local class and
4848 the name specified is an unqualified name, a prior
4849 declaration is looked up without considering scopes
4850 that are outside the innermost enclosing non-class
4851 scope. For a friend function declaration, if there is
4852 no prior declaration, the program is ill-formed. For a
4853 friend class declaration, if there is no prior
4854 declaration, the class that is specified belongs to the
4855 innermost enclosing non-class scope, but if it is
4856 subsequently referenced, its name is not found by name
4857 lookup until a matching declaration is provided in the
4858 innermost enclosing nonclass scope.
4860 So just keep looking for a non-hidden binding.
4862 gcc_assert (TREE_CODE (binding) == TYPE_DECL);
4863 continue;
4865 val = binding;
4866 break;
4870 /* Now lookup in namespace scopes. */
4871 if (!val)
4872 val = unqualified_namespace_lookup (name, flags);
4874 /* If we have a single function from a using decl, pull it out. */
4875 if (val && TREE_CODE (val) == OVERLOAD && !really_overloaded_fn (val))
4876 val = OVL_FUNCTION (val);
4878 return val;
4881 /* Wrapper for lookup_name_real_1. */
4883 tree
4884 lookup_name_real (tree name, int prefer_type, int nonclass, bool block_p,
4885 int namespaces_only, int flags)
4887 tree ret;
4888 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4889 ret = lookup_name_real_1 (name, prefer_type, nonclass, block_p,
4890 namespaces_only, flags);
4891 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4892 return ret;
4895 tree
4896 lookup_name_nonclass (tree name)
4898 return lookup_name_real (name, 0, 1, /*block_p=*/true, 0, 0);
4901 tree
4902 lookup_function_nonclass (tree name, vec<tree, va_gc> *args, bool block_p)
4904 return
4905 lookup_arg_dependent (name,
4906 lookup_name_real (name, 0, 1, block_p, 0, 0),
4907 args);
4910 tree
4911 lookup_name (tree name)
4913 return lookup_name_real (name, 0, 0, /*block_p=*/true, 0, 0);
4916 tree
4917 lookup_name_prefer_type (tree name, int prefer_type)
4919 return lookup_name_real (name, prefer_type, 0, /*block_p=*/true, 0, 0);
4922 /* Look up NAME for type used in elaborated name specifier in
4923 the scopes given by SCOPE. SCOPE can be either TS_CURRENT or
4924 TS_WITHIN_ENCLOSING_NON_CLASS. Although not implied by the
4925 name, more scopes are checked if cleanup or template parameter
4926 scope is encountered.
4928 Unlike lookup_name_real, we make sure that NAME is actually
4929 declared in the desired scope, not from inheritance, nor using
4930 directive. For using declaration, there is DR138 still waiting
4931 to be resolved. Hidden name coming from an earlier friend
4932 declaration is also returned.
4934 A TYPE_DECL best matching the NAME is returned. Catching error
4935 and issuing diagnostics are caller's responsibility. */
4937 static tree
4938 lookup_type_scope_1 (tree name, tag_scope scope)
4940 cxx_binding *iter = NULL;
4941 tree val = NULL_TREE;
4943 /* Look in non-namespace scope first. */
4944 if (current_binding_level->kind != sk_namespace)
4945 iter = outer_binding (name, NULL, /*class_p=*/ true);
4946 for (; iter; iter = outer_binding (name, iter, /*class_p=*/ true))
4948 /* Check if this is the kind of thing we're looking for.
4949 If SCOPE is TS_CURRENT, also make sure it doesn't come from
4950 base class. For ITER->VALUE, we can simply use
4951 INHERITED_VALUE_BINDING_P. For ITER->TYPE, we have to use
4952 our own check.
4954 We check ITER->TYPE before ITER->VALUE in order to handle
4955 typedef struct C {} C;
4956 correctly. */
4958 if (qualify_lookup (iter->type, LOOKUP_PREFER_TYPES)
4959 && (scope != ts_current
4960 || LOCAL_BINDING_P (iter)
4961 || DECL_CONTEXT (iter->type) == iter->scope->this_entity))
4962 val = iter->type;
4963 else if ((scope != ts_current
4964 || !INHERITED_VALUE_BINDING_P (iter))
4965 && qualify_lookup (iter->value, LOOKUP_PREFER_TYPES))
4966 val = iter->value;
4968 if (val)
4969 break;
4972 /* Look in namespace scope. */
4973 if (!val)
4975 iter = cp_binding_level_find_binding_for_name
4976 (NAMESPACE_LEVEL (current_decl_namespace ()), name);
4978 if (iter)
4980 /* If this is the kind of thing we're looking for, we're done. */
4981 if (qualify_lookup (iter->type, LOOKUP_PREFER_TYPES))
4982 val = iter->type;
4983 else if (qualify_lookup (iter->value, LOOKUP_PREFER_TYPES))
4984 val = iter->value;
4989 /* Type found, check if it is in the allowed scopes, ignoring cleanup
4990 and template parameter scopes. */
4991 if (val)
4993 cp_binding_level *b = current_binding_level;
4994 while (b)
4996 if (iter->scope == b)
4997 return val;
4999 if (b->kind == sk_cleanup || b->kind == sk_template_parms
5000 || b->kind == sk_function_parms)
5001 b = b->level_chain;
5002 else if (b->kind == sk_class
5003 && scope == ts_within_enclosing_non_class)
5004 b = b->level_chain;
5005 else
5006 break;
5010 return NULL_TREE;
5013 /* Wrapper for lookup_type_scope_1. */
5015 tree
5016 lookup_type_scope (tree name, tag_scope scope)
5018 tree ret;
5019 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
5020 ret = lookup_type_scope_1 (name, scope);
5021 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
5022 return ret;
5026 /* Similar to `lookup_name' but look only in the innermost non-class
5027 binding level. */
5029 static tree
5030 lookup_name_innermost_nonclass_level_1 (tree name)
5032 cp_binding_level *b;
5033 tree t = NULL_TREE;
5035 b = innermost_nonclass_level ();
5037 if (b->kind == sk_namespace)
5039 t = IDENTIFIER_NAMESPACE_VALUE (name);
5041 /* extern "C" function() */
5042 if (t != NULL_TREE && TREE_CODE (t) == TREE_LIST)
5043 t = TREE_VALUE (t);
5045 else if (IDENTIFIER_BINDING (name)
5046 && LOCAL_BINDING_P (IDENTIFIER_BINDING (name)))
5048 cxx_binding *binding;
5049 binding = IDENTIFIER_BINDING (name);
5050 while (1)
5052 if (binding->scope == b
5053 && !(VAR_P (binding->value)
5054 && DECL_DEAD_FOR_LOCAL (binding->value)))
5055 return binding->value;
5057 if (b->kind == sk_cleanup)
5058 b = b->level_chain;
5059 else
5060 break;
5064 return t;
5067 /* Wrapper for lookup_name_innermost_nonclass_level_1. */
5069 tree
5070 lookup_name_innermost_nonclass_level (tree name)
5072 tree ret;
5073 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
5074 ret = lookup_name_innermost_nonclass_level_1 (name);
5075 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
5076 return ret;
5080 /* Returns true iff DECL is a block-scope extern declaration of a function
5081 or variable. */
5083 bool
5084 is_local_extern (tree decl)
5086 cxx_binding *binding;
5088 /* For functions, this is easy. */
5089 if (TREE_CODE (decl) == FUNCTION_DECL)
5090 return DECL_LOCAL_FUNCTION_P (decl);
5092 if (!VAR_P (decl))
5093 return false;
5094 if (!current_function_decl)
5095 return false;
5097 /* For variables, this is not easy. We need to look at the binding stack
5098 for the identifier to see whether the decl we have is a local. */
5099 for (binding = IDENTIFIER_BINDING (DECL_NAME (decl));
5100 binding && binding->scope->kind != sk_namespace;
5101 binding = binding->previous)
5102 if (binding->value == decl)
5103 return LOCAL_BINDING_P (binding);
5105 return false;
5108 /* Like lookup_name_innermost_nonclass_level, but for types. */
5110 static tree
5111 lookup_type_current_level (tree name)
5113 tree t = NULL_TREE;
5115 timevar_start (TV_NAME_LOOKUP);
5116 gcc_assert (current_binding_level->kind != sk_namespace);
5118 if (REAL_IDENTIFIER_TYPE_VALUE (name) != NULL_TREE
5119 && REAL_IDENTIFIER_TYPE_VALUE (name) != global_type_node)
5121 cp_binding_level *b = current_binding_level;
5122 while (1)
5124 if (purpose_member (name, b->type_shadowed))
5126 t = REAL_IDENTIFIER_TYPE_VALUE (name);
5127 break;
5129 if (b->kind == sk_cleanup)
5130 b = b->level_chain;
5131 else
5132 break;
5136 timevar_stop (TV_NAME_LOOKUP);
5137 return t;
5140 /* [basic.lookup.koenig] */
5141 /* A nonzero return value in the functions below indicates an error. */
5143 struct arg_lookup
5145 tree name;
5146 vec<tree, va_gc> *args;
5147 vec<tree, va_gc> *namespaces;
5148 vec<tree, va_gc> *classes;
5149 tree functions;
5150 struct pointer_set_t *fn_set;
5153 static bool arg_assoc (struct arg_lookup*, tree);
5154 static bool arg_assoc_args (struct arg_lookup*, tree);
5155 static bool arg_assoc_args_vec (struct arg_lookup*, vec<tree, va_gc> *);
5156 static bool arg_assoc_type (struct arg_lookup*, tree);
5157 static bool add_function (struct arg_lookup *, tree);
5158 static bool arg_assoc_namespace (struct arg_lookup *, tree);
5159 static bool arg_assoc_class_only (struct arg_lookup *, tree);
5160 static bool arg_assoc_bases (struct arg_lookup *, tree);
5161 static bool arg_assoc_class (struct arg_lookup *, tree);
5162 static bool arg_assoc_template_arg (struct arg_lookup*, tree);
5164 /* Add a function to the lookup structure.
5165 Returns true on error. */
5167 static bool
5168 add_function (struct arg_lookup *k, tree fn)
5170 if (!is_overloaded_fn (fn))
5171 /* All names except those of (possibly overloaded) functions and
5172 function templates are ignored. */;
5173 else if (k->fn_set && pointer_set_insert (k->fn_set, fn))
5174 /* It's already in the list. */;
5175 else if (!k->functions)
5176 k->functions = fn;
5177 else if (fn == k->functions)
5179 else
5181 k->functions = build_overload (fn, k->functions);
5182 if (TREE_CODE (k->functions) == OVERLOAD)
5183 OVL_ARG_DEPENDENT (k->functions) = true;
5186 return false;
5189 /* Returns true iff CURRENT has declared itself to be an associated
5190 namespace of SCOPE via a strong using-directive (or transitive chain
5191 thereof). Both are namespaces. */
5193 bool
5194 is_associated_namespace (tree current, tree scope)
5196 vec<tree, va_gc> *seen = make_tree_vector ();
5197 vec<tree, va_gc> *todo = make_tree_vector ();
5198 tree t;
5199 bool ret;
5201 while (1)
5203 if (scope == current)
5205 ret = true;
5206 break;
5208 vec_safe_push (seen, scope);
5209 for (t = DECL_NAMESPACE_ASSOCIATIONS (scope); t; t = TREE_CHAIN (t))
5210 if (!vec_member (TREE_PURPOSE (t), seen))
5211 vec_safe_push (todo, TREE_PURPOSE (t));
5212 if (!todo->is_empty ())
5214 scope = todo->last ();
5215 todo->pop ();
5217 else
5219 ret = false;
5220 break;
5224 release_tree_vector (seen);
5225 release_tree_vector (todo);
5227 return ret;
5230 /* Add functions of a namespace to the lookup structure.
5231 Returns true on error. */
5233 static bool
5234 arg_assoc_namespace (struct arg_lookup *k, tree scope)
5236 tree value;
5238 if (vec_member (scope, k->namespaces))
5239 return false;
5240 vec_safe_push (k->namespaces, scope);
5242 /* Check out our super-users. */
5243 for (value = DECL_NAMESPACE_ASSOCIATIONS (scope); value;
5244 value = TREE_CHAIN (value))
5245 if (arg_assoc_namespace (k, TREE_PURPOSE (value)))
5246 return true;
5248 /* Also look down into inline namespaces. */
5249 for (value = DECL_NAMESPACE_USING (scope); value;
5250 value = TREE_CHAIN (value))
5251 if (is_associated_namespace (scope, TREE_PURPOSE (value)))
5252 if (arg_assoc_namespace (k, TREE_PURPOSE (value)))
5253 return true;
5255 value = namespace_binding (k->name, scope);
5256 if (!value)
5257 return false;
5259 for (; value; value = OVL_NEXT (value))
5261 /* We don't want to find arbitrary hidden functions via argument
5262 dependent lookup. We only want to find friends of associated
5263 classes, which we'll do via arg_assoc_class. */
5264 if (hidden_name_p (OVL_CURRENT (value)))
5265 continue;
5267 if (add_function (k, OVL_CURRENT (value)))
5268 return true;
5271 return false;
5274 /* Adds everything associated with a template argument to the lookup
5275 structure. Returns true on error. */
5277 static bool
5278 arg_assoc_template_arg (struct arg_lookup *k, tree arg)
5280 /* [basic.lookup.koenig]
5282 If T is a template-id, its associated namespaces and classes are
5283 ... the namespaces and classes associated with the types of the
5284 template arguments provided for template type parameters
5285 (excluding template template parameters); the namespaces in which
5286 any template template arguments are defined; and the classes in
5287 which any member templates used as template template arguments
5288 are defined. [Note: non-type template arguments do not
5289 contribute to the set of associated namespaces. ] */
5291 /* Consider first template template arguments. */
5292 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
5293 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
5294 return false;
5295 else if (TREE_CODE (arg) == TEMPLATE_DECL)
5297 tree ctx = CP_DECL_CONTEXT (arg);
5299 /* It's not a member template. */
5300 if (TREE_CODE (ctx) == NAMESPACE_DECL)
5301 return arg_assoc_namespace (k, ctx);
5302 /* Otherwise, it must be member template. */
5303 else
5304 return arg_assoc_class_only (k, ctx);
5306 /* It's an argument pack; handle it recursively. */
5307 else if (ARGUMENT_PACK_P (arg))
5309 tree args = ARGUMENT_PACK_ARGS (arg);
5310 int i, len = TREE_VEC_LENGTH (args);
5311 for (i = 0; i < len; ++i)
5312 if (arg_assoc_template_arg (k, TREE_VEC_ELT (args, i)))
5313 return true;
5315 return false;
5317 /* It's not a template template argument, but it is a type template
5318 argument. */
5319 else if (TYPE_P (arg))
5320 return arg_assoc_type (k, arg);
5321 /* It's a non-type template argument. */
5322 else
5323 return false;
5326 /* Adds the class and its friends to the lookup structure.
5327 Returns true on error. */
5329 static bool
5330 arg_assoc_class_only (struct arg_lookup *k, tree type)
5332 tree list, friends, context;
5334 /* Backend-built structures, such as __builtin_va_list, aren't
5335 affected by all this. */
5336 if (!CLASS_TYPE_P (type))
5337 return false;
5339 context = decl_namespace_context (type);
5340 if (arg_assoc_namespace (k, context))
5341 return true;
5343 complete_type (type);
5345 /* Process friends. */
5346 for (list = DECL_FRIENDLIST (TYPE_MAIN_DECL (type)); list;
5347 list = TREE_CHAIN (list))
5348 if (k->name == FRIEND_NAME (list))
5349 for (friends = FRIEND_DECLS (list); friends;
5350 friends = TREE_CHAIN (friends))
5352 tree fn = TREE_VALUE (friends);
5354 /* Only interested in global functions with potentially hidden
5355 (i.e. unqualified) declarations. */
5356 if (CP_DECL_CONTEXT (fn) != context)
5357 continue;
5358 /* Template specializations are never found by name lookup.
5359 (Templates themselves can be found, but not template
5360 specializations.) */
5361 if (TREE_CODE (fn) == FUNCTION_DECL && DECL_USE_TEMPLATE (fn))
5362 continue;
5363 if (add_function (k, fn))
5364 return true;
5367 return false;
5370 /* Adds the class and its bases to the lookup structure.
5371 Returns true on error. */
5373 static bool
5374 arg_assoc_bases (struct arg_lookup *k, tree type)
5376 if (arg_assoc_class_only (k, type))
5377 return true;
5379 if (TYPE_BINFO (type))
5381 /* Process baseclasses. */
5382 tree binfo, base_binfo;
5383 int i;
5385 for (binfo = TYPE_BINFO (type), i = 0;
5386 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
5387 if (arg_assoc_bases (k, BINFO_TYPE (base_binfo)))
5388 return true;
5391 return false;
5394 /* Adds everything associated with a class argument type to the lookup
5395 structure. Returns true on error.
5397 If T is a class type (including unions), its associated classes are: the
5398 class itself; the class of which it is a member, if any; and its direct
5399 and indirect base classes. Its associated namespaces are the namespaces
5400 of which its associated classes are members. Furthermore, if T is a
5401 class template specialization, its associated namespaces and classes
5402 also include: the namespaces and classes associated with the types of
5403 the template arguments provided for template type parameters (excluding
5404 template template parameters); the namespaces of which any template
5405 template arguments are members; and the classes of which any member
5406 templates used as template template arguments are members. [ Note:
5407 non-type template arguments do not contribute to the set of associated
5408 namespaces. --end note] */
5410 static bool
5411 arg_assoc_class (struct arg_lookup *k, tree type)
5413 tree list;
5414 int i;
5416 /* Backend build structures, such as __builtin_va_list, aren't
5417 affected by all this. */
5418 if (!CLASS_TYPE_P (type))
5419 return false;
5421 if (vec_member (type, k->classes))
5422 return false;
5423 vec_safe_push (k->classes, type);
5425 if (TYPE_CLASS_SCOPE_P (type)
5426 && arg_assoc_class_only (k, TYPE_CONTEXT (type)))
5427 return true;
5429 if (arg_assoc_bases (k, type))
5430 return true;
5432 /* Process template arguments. */
5433 if (CLASSTYPE_TEMPLATE_INFO (type)
5434 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
5436 list = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
5437 for (i = 0; i < TREE_VEC_LENGTH (list); ++i)
5438 if (arg_assoc_template_arg (k, TREE_VEC_ELT (list, i)))
5439 return true;
5442 return false;
5445 /* Adds everything associated with a given type.
5446 Returns 1 on error. */
5448 static bool
5449 arg_assoc_type (struct arg_lookup *k, tree type)
5451 /* As we do not get the type of non-type dependent expressions
5452 right, we can end up with such things without a type. */
5453 if (!type)
5454 return false;
5456 if (TYPE_PTRDATAMEM_P (type))
5458 /* Pointer to member: associate class type and value type. */
5459 if (arg_assoc_type (k, TYPE_PTRMEM_CLASS_TYPE (type)))
5460 return true;
5461 return arg_assoc_type (k, TYPE_PTRMEM_POINTED_TO_TYPE (type));
5463 else switch (TREE_CODE (type))
5465 case ERROR_MARK:
5466 return false;
5467 case VOID_TYPE:
5468 case INTEGER_TYPE:
5469 case REAL_TYPE:
5470 case COMPLEX_TYPE:
5471 case VECTOR_TYPE:
5472 case BOOLEAN_TYPE:
5473 case FIXED_POINT_TYPE:
5474 case DECLTYPE_TYPE:
5475 case NULLPTR_TYPE:
5476 return false;
5477 case RECORD_TYPE:
5478 if (TYPE_PTRMEMFUNC_P (type))
5479 return arg_assoc_type (k, TYPE_PTRMEMFUNC_FN_TYPE (type));
5480 case UNION_TYPE:
5481 return arg_assoc_class (k, type);
5482 case POINTER_TYPE:
5483 case REFERENCE_TYPE:
5484 case ARRAY_TYPE:
5485 return arg_assoc_type (k, TREE_TYPE (type));
5486 case ENUMERAL_TYPE:
5487 if (TYPE_CLASS_SCOPE_P (type)
5488 && arg_assoc_class_only (k, TYPE_CONTEXT (type)))
5489 return true;
5490 return arg_assoc_namespace (k, decl_namespace_context (type));
5491 case METHOD_TYPE:
5492 /* The basetype is referenced in the first arg type, so just
5493 fall through. */
5494 case FUNCTION_TYPE:
5495 /* Associate the parameter types. */
5496 if (arg_assoc_args (k, TYPE_ARG_TYPES (type)))
5497 return true;
5498 /* Associate the return type. */
5499 return arg_assoc_type (k, TREE_TYPE (type));
5500 case TEMPLATE_TYPE_PARM:
5501 case BOUND_TEMPLATE_TEMPLATE_PARM:
5502 return false;
5503 case TYPENAME_TYPE:
5504 return false;
5505 case LANG_TYPE:
5506 gcc_assert (type == unknown_type_node
5507 || type == init_list_type_node);
5508 return false;
5509 case TYPE_PACK_EXPANSION:
5510 return arg_assoc_type (k, PACK_EXPANSION_PATTERN (type));
5512 default:
5513 gcc_unreachable ();
5515 return false;
5518 /* Adds everything associated with arguments. Returns true on error. */
5520 static bool
5521 arg_assoc_args (struct arg_lookup *k, tree args)
5523 for (; args; args = TREE_CHAIN (args))
5524 if (arg_assoc (k, TREE_VALUE (args)))
5525 return true;
5526 return false;
5529 /* Adds everything associated with an argument vector. Returns true
5530 on error. */
5532 static bool
5533 arg_assoc_args_vec (struct arg_lookup *k, vec<tree, va_gc> *args)
5535 unsigned int ix;
5536 tree arg;
5538 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
5539 if (arg_assoc (k, arg))
5540 return true;
5541 return false;
5544 /* Adds everything associated with a given tree_node. Returns 1 on error. */
5546 static bool
5547 arg_assoc (struct arg_lookup *k, tree n)
5549 if (n == error_mark_node)
5550 return false;
5552 if (TYPE_P (n))
5553 return arg_assoc_type (k, n);
5555 if (! type_unknown_p (n))
5556 return arg_assoc_type (k, TREE_TYPE (n));
5558 if (TREE_CODE (n) == ADDR_EXPR)
5559 n = TREE_OPERAND (n, 0);
5560 if (TREE_CODE (n) == COMPONENT_REF)
5561 n = TREE_OPERAND (n, 1);
5562 if (TREE_CODE (n) == OFFSET_REF)
5563 n = TREE_OPERAND (n, 1);
5564 while (TREE_CODE (n) == TREE_LIST)
5565 n = TREE_VALUE (n);
5566 if (BASELINK_P (n))
5567 n = BASELINK_FUNCTIONS (n);
5569 if (TREE_CODE (n) == FUNCTION_DECL)
5570 return arg_assoc_type (k, TREE_TYPE (n));
5571 if (TREE_CODE (n) == TEMPLATE_ID_EXPR)
5573 /* The working paper doesn't currently say how to handle template-id
5574 arguments. The sensible thing would seem to be to handle the list
5575 of template candidates like a normal overload set, and handle the
5576 template arguments like we do for class template
5577 specializations. */
5578 tree templ = TREE_OPERAND (n, 0);
5579 tree args = TREE_OPERAND (n, 1);
5580 int ix;
5582 /* First the templates. */
5583 if (arg_assoc (k, templ))
5584 return true;
5586 /* Now the arguments. */
5587 if (args)
5588 for (ix = TREE_VEC_LENGTH (args); ix--;)
5589 if (arg_assoc_template_arg (k, TREE_VEC_ELT (args, ix)) == 1)
5590 return true;
5592 else if (TREE_CODE (n) == OVERLOAD)
5594 for (; n; n = OVL_NEXT (n))
5595 if (arg_assoc_type (k, TREE_TYPE (OVL_CURRENT (n))))
5596 return true;
5599 return false;
5602 /* Performs Koenig lookup depending on arguments, where fns
5603 are the functions found in normal lookup. */
5605 static tree
5606 lookup_arg_dependent_1 (tree name, tree fns, vec<tree, va_gc> *args)
5608 struct arg_lookup k;
5610 /* Remove any hidden friend functions from the list of functions
5611 found so far. They will be added back by arg_assoc_class as
5612 appropriate. */
5613 fns = remove_hidden_names (fns);
5615 k.name = name;
5616 k.args = args;
5617 k.functions = fns;
5618 k.classes = make_tree_vector ();
5620 /* We previously performed an optimization here by setting
5621 NAMESPACES to the current namespace when it was safe. However, DR
5622 164 says that namespaces that were already searched in the first
5623 stage of template processing are searched again (potentially
5624 picking up later definitions) in the second stage. */
5625 k.namespaces = make_tree_vector ();
5627 /* We used to allow duplicates and let joust discard them, but
5628 since the above change for DR 164 we end up with duplicates of
5629 all the functions found by unqualified lookup. So keep track
5630 of which ones we've seen. */
5631 if (fns)
5633 tree ovl;
5634 /* We shouldn't be here if lookup found something other than
5635 namespace-scope functions. */
5636 gcc_assert (DECL_NAMESPACE_SCOPE_P (OVL_CURRENT (fns)));
5637 k.fn_set = pointer_set_create ();
5638 for (ovl = fns; ovl; ovl = OVL_NEXT (ovl))
5639 pointer_set_insert (k.fn_set, OVL_CURRENT (ovl));
5641 else
5642 k.fn_set = NULL;
5644 arg_assoc_args_vec (&k, args);
5646 fns = k.functions;
5648 if (fns
5649 && !VAR_P (fns)
5650 && !is_overloaded_fn (fns))
5652 error ("argument dependent lookup finds %q+D", fns);
5653 error (" in call to %qD", name);
5654 fns = error_mark_node;
5657 release_tree_vector (k.classes);
5658 release_tree_vector (k.namespaces);
5659 if (k.fn_set)
5660 pointer_set_destroy (k.fn_set);
5662 return fns;
5665 /* Wrapper for lookup_arg_dependent_1. */
5667 tree
5668 lookup_arg_dependent (tree name, tree fns, vec<tree, va_gc> *args)
5670 tree ret;
5671 bool subtime;
5672 subtime = timevar_cond_start (TV_NAME_LOOKUP);
5673 ret = lookup_arg_dependent_1 (name, fns, args);
5674 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
5675 return ret;
5679 /* Add namespace to using_directives. Return NULL_TREE if nothing was
5680 changed (i.e. there was already a directive), or the fresh
5681 TREE_LIST otherwise. */
5683 static tree
5684 push_using_directive_1 (tree used)
5686 tree ud = current_binding_level->using_directives;
5687 tree iter, ancestor;
5689 /* Check if we already have this. */
5690 if (purpose_member (used, ud) != NULL_TREE)
5691 return NULL_TREE;
5693 ancestor = namespace_ancestor (current_decl_namespace (), used);
5694 ud = current_binding_level->using_directives;
5695 ud = tree_cons (used, ancestor, ud);
5696 current_binding_level->using_directives = ud;
5698 /* Recursively add all namespaces used. */
5699 for (iter = DECL_NAMESPACE_USING (used); iter; iter = TREE_CHAIN (iter))
5700 push_using_directive (TREE_PURPOSE (iter));
5702 return ud;
5705 /* Wrapper for push_using_directive_1. */
5707 static tree
5708 push_using_directive (tree used)
5710 tree ret;
5711 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
5712 ret = push_using_directive_1 (used);
5713 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
5714 return ret;
5717 /* The type TYPE is being declared. If it is a class template, or a
5718 specialization of a class template, do any processing required and
5719 perform error-checking. If IS_FRIEND is nonzero, this TYPE is
5720 being declared a friend. B is the binding level at which this TYPE
5721 should be bound.
5723 Returns the TYPE_DECL for TYPE, which may have been altered by this
5724 processing. */
5726 static tree
5727 maybe_process_template_type_declaration (tree type, int is_friend,
5728 cp_binding_level *b)
5730 tree decl = TYPE_NAME (type);
5732 if (processing_template_parmlist)
5733 /* You can't declare a new template type in a template parameter
5734 list. But, you can declare a non-template type:
5736 template <class A*> struct S;
5738 is a forward-declaration of `A'. */
5740 else if (b->kind == sk_namespace
5741 && current_binding_level->kind != sk_namespace)
5742 /* If this new type is being injected into a containing scope,
5743 then it's not a template type. */
5745 else
5747 gcc_assert (MAYBE_CLASS_TYPE_P (type)
5748 || TREE_CODE (type) == ENUMERAL_TYPE);
5750 if (processing_template_decl)
5752 /* This may change after the call to
5753 push_template_decl_real, but we want the original value. */
5754 tree name = DECL_NAME (decl);
5756 decl = push_template_decl_real (decl, is_friend);
5757 if (decl == error_mark_node)
5758 return error_mark_node;
5760 /* If the current binding level is the binding level for the
5761 template parameters (see the comment in
5762 begin_template_parm_list) and the enclosing level is a class
5763 scope, and we're not looking at a friend, push the
5764 declaration of the member class into the class scope. In the
5765 friend case, push_template_decl will already have put the
5766 friend into global scope, if appropriate. */
5767 if (TREE_CODE (type) != ENUMERAL_TYPE
5768 && !is_friend && b->kind == sk_template_parms
5769 && b->level_chain->kind == sk_class)
5771 finish_member_declaration (CLASSTYPE_TI_TEMPLATE (type));
5773 if (!COMPLETE_TYPE_P (current_class_type))
5775 maybe_add_class_template_decl_list (current_class_type,
5776 type, /*friend_p=*/0);
5777 /* Put this UTD in the table of UTDs for the class. */
5778 if (CLASSTYPE_NESTED_UTDS (current_class_type) == NULL)
5779 CLASSTYPE_NESTED_UTDS (current_class_type) =
5780 binding_table_new (SCOPE_DEFAULT_HT_SIZE);
5782 binding_table_insert
5783 (CLASSTYPE_NESTED_UTDS (current_class_type), name, type);
5789 return decl;
5792 /* Push a tag name NAME for struct/class/union/enum type TYPE. In case
5793 that the NAME is a class template, the tag is processed but not pushed.
5795 The pushed scope depend on the SCOPE parameter:
5796 - When SCOPE is TS_CURRENT, put it into the inner-most non-sk_cleanup
5797 scope.
5798 - When SCOPE is TS_GLOBAL, put it in the inner-most non-class and
5799 non-template-parameter scope. This case is needed for forward
5800 declarations.
5801 - When SCOPE is TS_WITHIN_ENCLOSING_NON_CLASS, this is similar to
5802 TS_GLOBAL case except that names within template-parameter scopes
5803 are not pushed at all.
5805 Returns TYPE upon success and ERROR_MARK_NODE otherwise. */
5807 static tree
5808 pushtag_1 (tree name, tree type, tag_scope scope)
5810 cp_binding_level *b;
5811 tree decl;
5813 b = current_binding_level;
5814 while (/* Cleanup scopes are not scopes from the point of view of
5815 the language. */
5816 b->kind == sk_cleanup
5817 /* Neither are function parameter scopes. */
5818 || b->kind == sk_function_parms
5819 /* Neither are the scopes used to hold template parameters
5820 for an explicit specialization. For an ordinary template
5821 declaration, these scopes are not scopes from the point of
5822 view of the language. */
5823 || (b->kind == sk_template_parms
5824 && (b->explicit_spec_p || scope == ts_global))
5825 || (b->kind == sk_class
5826 && (scope != ts_current
5827 /* We may be defining a new type in the initializer
5828 of a static member variable. We allow this when
5829 not pedantic, and it is particularly useful for
5830 type punning via an anonymous union. */
5831 || COMPLETE_TYPE_P (b->this_entity))))
5832 b = b->level_chain;
5834 gcc_assert (identifier_p (name));
5836 /* Do C++ gratuitous typedefing. */
5837 if (identifier_type_value_1 (name) != type)
5839 tree tdef;
5840 int in_class = 0;
5841 tree context = TYPE_CONTEXT (type);
5843 if (! context)
5845 tree cs = current_scope ();
5847 if (scope == ts_current
5848 || (cs && TREE_CODE (cs) == FUNCTION_DECL))
5849 context = cs;
5850 else if (cs != NULL_TREE && TYPE_P (cs))
5851 /* When declaring a friend class of a local class, we want
5852 to inject the newly named class into the scope
5853 containing the local class, not the namespace
5854 scope. */
5855 context = decl_function_context (get_type_decl (cs));
5857 if (!context)
5858 context = current_namespace;
5860 if (b->kind == sk_class
5861 || (b->kind == sk_template_parms
5862 && b->level_chain->kind == sk_class))
5863 in_class = 1;
5865 if (current_lang_name == lang_name_java)
5866 TYPE_FOR_JAVA (type) = 1;
5868 tdef = create_implicit_typedef (name, type);
5869 DECL_CONTEXT (tdef) = FROB_CONTEXT (context);
5870 if (scope == ts_within_enclosing_non_class)
5872 /* This is a friend. Make this TYPE_DECL node hidden from
5873 ordinary name lookup. Its corresponding TEMPLATE_DECL
5874 will be marked in push_template_decl_real. */
5875 retrofit_lang_decl (tdef);
5876 DECL_ANTICIPATED (tdef) = 1;
5877 DECL_FRIEND_P (tdef) = 1;
5880 decl = maybe_process_template_type_declaration
5881 (type, scope == ts_within_enclosing_non_class, b);
5882 if (decl == error_mark_node)
5883 return decl;
5885 if (b->kind == sk_class)
5887 if (!TYPE_BEING_DEFINED (current_class_type))
5888 return error_mark_node;
5890 if (!PROCESSING_REAL_TEMPLATE_DECL_P ())
5891 /* Put this TYPE_DECL on the TYPE_FIELDS list for the
5892 class. But if it's a member template class, we want
5893 the TEMPLATE_DECL, not the TYPE_DECL, so this is done
5894 later. */
5895 finish_member_declaration (decl);
5896 else
5897 pushdecl_class_level (decl);
5899 else if (b->kind != sk_template_parms)
5901 decl = pushdecl_with_scope_1 (decl, b, /*is_friend=*/false);
5902 if (decl == error_mark_node)
5903 return decl;
5906 if (! in_class)
5907 set_identifier_type_value_with_scope (name, tdef, b);
5909 TYPE_CONTEXT (type) = DECL_CONTEXT (decl);
5911 /* If this is a local class, keep track of it. We need this
5912 information for name-mangling, and so that it is possible to
5913 find all function definitions in a translation unit in a
5914 convenient way. (It's otherwise tricky to find a member
5915 function definition it's only pointed to from within a local
5916 class.) */
5917 if (TYPE_FUNCTION_SCOPE_P (type))
5919 if (processing_template_decl)
5921 /* Push a DECL_EXPR so we call pushtag at the right time in
5922 template instantiation rather than in some nested context. */
5923 add_decl_expr (decl);
5925 else
5926 vec_safe_push (local_classes, type);
5929 if (b->kind == sk_class
5930 && !COMPLETE_TYPE_P (current_class_type))
5932 maybe_add_class_template_decl_list (current_class_type,
5933 type, /*friend_p=*/0);
5935 if (CLASSTYPE_NESTED_UTDS (current_class_type) == NULL)
5936 CLASSTYPE_NESTED_UTDS (current_class_type)
5937 = binding_table_new (SCOPE_DEFAULT_HT_SIZE);
5939 binding_table_insert
5940 (CLASSTYPE_NESTED_UTDS (current_class_type), name, type);
5943 decl = TYPE_NAME (type);
5944 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
5946 /* Set type visibility now if this is a forward declaration. */
5947 TREE_PUBLIC (decl) = 1;
5948 determine_visibility (decl);
5950 return type;
5953 /* Wrapper for pushtag_1. */
5955 tree
5956 pushtag (tree name, tree type, tag_scope scope)
5958 tree ret;
5959 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
5960 ret = pushtag_1 (name, type, scope);
5961 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
5962 return ret;
5965 /* Subroutines for reverting temporarily to top-level for instantiation
5966 of templates and such. We actually need to clear out the class- and
5967 local-value slots of all identifiers, so that only the global values
5968 are at all visible. Simply setting current_binding_level to the global
5969 scope isn't enough, because more binding levels may be pushed. */
5970 struct saved_scope *scope_chain;
5972 /* Return true if ID has not already been marked. */
5974 static inline bool
5975 store_binding_p (tree id)
5977 if (!id || !IDENTIFIER_BINDING (id))
5978 return false;
5980 if (IDENTIFIER_MARKED (id))
5981 return false;
5983 return true;
5986 /* Add an appropriate binding to *OLD_BINDINGS which needs to already
5987 have enough space reserved. */
5989 static void
5990 store_binding (tree id, vec<cxx_saved_binding, va_gc> **old_bindings)
5992 cxx_saved_binding saved;
5994 gcc_checking_assert (store_binding_p (id));
5996 IDENTIFIER_MARKED (id) = 1;
5998 saved.identifier = id;
5999 saved.binding = IDENTIFIER_BINDING (id);
6000 saved.real_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
6001 (*old_bindings)->quick_push (saved);
6002 IDENTIFIER_BINDING (id) = NULL;
6005 static void
6006 store_bindings (tree names, vec<cxx_saved_binding, va_gc> **old_bindings)
6008 static vec<tree> bindings_need_stored = vNULL;
6009 tree t, id;
6010 size_t i;
6012 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6013 for (t = names; t; t = TREE_CHAIN (t))
6015 if (TREE_CODE (t) == TREE_LIST)
6016 id = TREE_PURPOSE (t);
6017 else
6018 id = DECL_NAME (t);
6020 if (store_binding_p (id))
6021 bindings_need_stored.safe_push (id);
6023 if (!bindings_need_stored.is_empty ())
6025 vec_safe_reserve_exact (*old_bindings, bindings_need_stored.length ());
6026 for (i = 0; bindings_need_stored.iterate (i, &id); ++i)
6028 /* We can appearantly have duplicates in NAMES. */
6029 if (store_binding_p (id))
6030 store_binding (id, old_bindings);
6032 bindings_need_stored.truncate (0);
6034 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6037 /* Like store_bindings, but NAMES is a vector of cp_class_binding
6038 objects, rather than a TREE_LIST. */
6040 static void
6041 store_class_bindings (vec<cp_class_binding, va_gc> *names,
6042 vec<cxx_saved_binding, va_gc> **old_bindings)
6044 static vec<tree> bindings_need_stored = vNULL;
6045 size_t i;
6046 cp_class_binding *cb;
6048 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6049 for (i = 0; vec_safe_iterate (names, i, &cb); ++i)
6050 if (store_binding_p (cb->identifier))
6051 bindings_need_stored.safe_push (cb->identifier);
6052 if (!bindings_need_stored.is_empty ())
6054 tree id;
6055 vec_safe_reserve_exact (*old_bindings, bindings_need_stored.length ());
6056 for (i = 0; bindings_need_stored.iterate (i, &id); ++i)
6057 store_binding (id, old_bindings);
6058 bindings_need_stored.truncate (0);
6060 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6063 void
6064 push_to_top_level (void)
6066 struct saved_scope *s;
6067 cp_binding_level *b;
6068 cxx_saved_binding *sb;
6069 size_t i;
6070 bool need_pop;
6072 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6073 s = ggc_alloc_cleared_saved_scope ();
6075 b = scope_chain ? current_binding_level : 0;
6077 /* If we're in the middle of some function, save our state. */
6078 if (cfun)
6080 need_pop = true;
6081 push_function_context ();
6083 else
6084 need_pop = false;
6086 if (scope_chain && previous_class_level)
6087 store_class_bindings (previous_class_level->class_shadowed,
6088 &s->old_bindings);
6090 /* Have to include the global scope, because class-scope decls
6091 aren't listed anywhere useful. */
6092 for (; b; b = b->level_chain)
6094 tree t;
6096 /* Template IDs are inserted into the global level. If they were
6097 inserted into namespace level, finish_file wouldn't find them
6098 when doing pending instantiations. Therefore, don't stop at
6099 namespace level, but continue until :: . */
6100 if (global_scope_p (b))
6101 break;
6103 store_bindings (b->names, &s->old_bindings);
6104 /* We also need to check class_shadowed to save class-level type
6105 bindings, since pushclass doesn't fill in b->names. */
6106 if (b->kind == sk_class)
6107 store_class_bindings (b->class_shadowed, &s->old_bindings);
6109 /* Unwind type-value slots back to top level. */
6110 for (t = b->type_shadowed; t; t = TREE_CHAIN (t))
6111 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (t), TREE_VALUE (t));
6114 FOR_EACH_VEC_SAFE_ELT (s->old_bindings, i, sb)
6115 IDENTIFIER_MARKED (sb->identifier) = 0;
6117 s->prev = scope_chain;
6118 s->bindings = b;
6119 s->need_pop_function_context = need_pop;
6120 s->function_decl = current_function_decl;
6121 s->unevaluated_operand = cp_unevaluated_operand;
6122 s->inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
6123 s->x_stmt_tree.stmts_are_full_exprs_p = true;
6125 scope_chain = s;
6126 current_function_decl = NULL_TREE;
6127 vec_alloc (current_lang_base, 10);
6128 current_lang_name = lang_name_cplusplus;
6129 current_namespace = global_namespace;
6130 push_class_stack ();
6131 cp_unevaluated_operand = 0;
6132 c_inhibit_evaluation_warnings = 0;
6133 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6136 static void
6137 pop_from_top_level_1 (void)
6139 struct saved_scope *s = scope_chain;
6140 cxx_saved_binding *saved;
6141 size_t i;
6143 /* Clear out class-level bindings cache. */
6144 if (previous_class_level)
6145 invalidate_class_lookup_cache ();
6146 pop_class_stack ();
6148 current_lang_base = 0;
6150 scope_chain = s->prev;
6151 FOR_EACH_VEC_SAFE_ELT (s->old_bindings, i, saved)
6153 tree id = saved->identifier;
6155 IDENTIFIER_BINDING (id) = saved->binding;
6156 SET_IDENTIFIER_TYPE_VALUE (id, saved->real_type_value);
6159 /* If we were in the middle of compiling a function, restore our
6160 state. */
6161 if (s->need_pop_function_context)
6162 pop_function_context ();
6163 current_function_decl = s->function_decl;
6164 cp_unevaluated_operand = s->unevaluated_operand;
6165 c_inhibit_evaluation_warnings = s->inhibit_evaluation_warnings;
6168 /* Wrapper for pop_from_top_level_1. */
6170 void
6171 pop_from_top_level (void)
6173 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6174 pop_from_top_level_1 ();
6175 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6179 /* Pop off extraneous binding levels left over due to syntax errors.
6181 We don't pop past namespaces, as they might be valid. */
6183 void
6184 pop_everything (void)
6186 if (ENABLE_SCOPE_CHECKING)
6187 verbatim ("XXX entering pop_everything ()\n");
6188 while (!toplevel_bindings_p ())
6190 if (current_binding_level->kind == sk_class)
6191 pop_nested_class ();
6192 else
6193 poplevel (0, 0, 0);
6195 if (ENABLE_SCOPE_CHECKING)
6196 verbatim ("XXX leaving pop_everything ()\n");
6199 /* Emit debugging information for using declarations and directives.
6200 If input tree is overloaded fn then emit debug info for all
6201 candidates. */
6203 void
6204 cp_emit_debug_info_for_using (tree t, tree context)
6206 /* Don't try to emit any debug information if we have errors. */
6207 if (seen_error ())
6208 return;
6210 /* Ignore this FUNCTION_DECL if it refers to a builtin declaration
6211 of a builtin function. */
6212 if (TREE_CODE (t) == FUNCTION_DECL
6213 && DECL_EXTERNAL (t)
6214 && DECL_BUILT_IN (t))
6215 return;
6217 /* Do not supply context to imported_module_or_decl, if
6218 it is a global namespace. */
6219 if (context == global_namespace)
6220 context = NULL_TREE;
6222 if (BASELINK_P (t))
6223 t = BASELINK_FUNCTIONS (t);
6225 /* FIXME: Handle TEMPLATE_DECLs. */
6226 for (t = OVL_CURRENT (t); t; t = OVL_NEXT (t))
6227 if (TREE_CODE (t) != TEMPLATE_DECL)
6229 if (building_stmt_list_p ())
6230 add_stmt (build_stmt (input_location, USING_STMT, t));
6231 else
6232 (*debug_hooks->imported_module_or_decl) (t, NULL_TREE, context, false);
6236 #include "gt-cp-name-lookup.h"