Introduce gimple_phi and use it in various places
[official-gcc.git] / gcc / cp / name-lookup.c
blobd42bcac92197b3669e255126eac690132ea0e80d
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 "hash-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_cleared_vec_alloc<binding_entry> (table->chain_count);
153 /* Make TABLE's entries ready for reuse. */
154 #if 0
155 static void
156 binding_table_free (binding_table table)
158 size_t i;
159 size_t count;
161 if (table == NULL)
162 return;
164 for (i = 0, count = table->chain_count; i < count; ++i)
166 binding_entry temp = table->chain[i];
167 while (temp != NULL)
169 binding_entry entry = temp;
170 temp = entry->chain;
171 binding_entry_free (entry);
173 table->chain[i] = NULL;
175 table->entry_count = 0;
177 #endif
179 /* Allocate a table with CHAIN_COUNT, assumed to be a power of two. */
181 static inline binding_table
182 binding_table_new (size_t chain_count)
184 binding_table table = ggc_alloc<binding_table_s> ();
185 table->chain = NULL;
186 binding_table_construct (table, chain_count);
187 return table;
190 /* Expand TABLE to twice its current chain_count. */
192 static void
193 binding_table_expand (binding_table table)
195 const size_t old_chain_count = table->chain_count;
196 const size_t old_entry_count = table->entry_count;
197 const size_t new_chain_count = 2 * old_chain_count;
198 binding_entry *old_chains = table->chain;
199 size_t i;
201 binding_table_construct (table, new_chain_count);
202 for (i = 0; i < old_chain_count; ++i)
204 binding_entry entry = old_chains[i];
205 for (; entry != NULL; entry = old_chains[i])
207 const unsigned int hash = IDENTIFIER_HASH_VALUE (entry->name);
208 const size_t j = ENTRY_INDEX (hash, new_chain_count);
210 old_chains[i] = entry->chain;
211 entry->chain = table->chain[j];
212 table->chain[j] = entry;
215 table->entry_count = old_entry_count;
218 /* Insert a binding for NAME to TYPE into TABLE. */
220 static void
221 binding_table_insert (binding_table table, tree name, tree type)
223 const unsigned int hash = IDENTIFIER_HASH_VALUE (name);
224 const size_t i = ENTRY_INDEX (hash, table->chain_count);
225 binding_entry entry = binding_entry_make (name, type);
227 entry->chain = table->chain[i];
228 table->chain[i] = entry;
229 ++table->entry_count;
231 if (3 * table->chain_count < 5 * table->entry_count)
232 binding_table_expand (table);
235 /* Return the binding_entry, if any, that maps NAME. */
237 binding_entry
238 binding_table_find (binding_table table, tree name)
240 const unsigned int hash = IDENTIFIER_HASH_VALUE (name);
241 binding_entry entry = table->chain[ENTRY_INDEX (hash, table->chain_count)];
243 while (entry != NULL && entry->name != name)
244 entry = entry->chain;
246 return entry;
249 /* Apply PROC -- with DATA -- to all entries in TABLE. */
251 void
252 binding_table_foreach (binding_table table, bt_foreach_proc proc, void *data)
254 size_t chain_count;
255 size_t i;
257 if (!table)
258 return;
260 chain_count = table->chain_count;
261 for (i = 0; i < chain_count; ++i)
263 binding_entry entry = table->chain[i];
264 for (; entry != NULL; entry = entry->chain)
265 proc (entry, data);
269 #ifndef ENABLE_SCOPE_CHECKING
270 # define ENABLE_SCOPE_CHECKING 0
271 #else
272 # define ENABLE_SCOPE_CHECKING 1
273 #endif
275 /* A free list of "cxx_binding"s, connected by their PREVIOUS. */
277 static GTY((deletable)) cxx_binding *free_bindings;
279 /* Initialize VALUE and TYPE field for BINDING, and set the PREVIOUS
280 field to NULL. */
282 static inline void
283 cxx_binding_init (cxx_binding *binding, tree value, tree type)
285 binding->value = value;
286 binding->type = type;
287 binding->previous = NULL;
290 /* (GC)-allocate a binding object with VALUE and TYPE member initialized. */
292 static cxx_binding *
293 cxx_binding_make (tree value, tree type)
295 cxx_binding *binding;
296 if (free_bindings)
298 binding = free_bindings;
299 free_bindings = binding->previous;
301 else
302 binding = ggc_alloc<cxx_binding> ();
304 cxx_binding_init (binding, value, type);
306 return binding;
309 /* Put BINDING back on the free list. */
311 static inline void
312 cxx_binding_free (cxx_binding *binding)
314 binding->scope = NULL;
315 binding->previous = free_bindings;
316 free_bindings = binding;
319 /* Create a new binding for NAME (with the indicated VALUE and TYPE
320 bindings) in the class scope indicated by SCOPE. */
322 static cxx_binding *
323 new_class_binding (tree name, tree value, tree type, cp_binding_level *scope)
325 cp_class_binding cb = {cxx_binding_make (value, type), name};
326 cxx_binding *binding = cb.base;
327 vec_safe_push (scope->class_shadowed, cb);
328 binding->scope = scope;
329 return binding;
332 /* Make DECL the innermost binding for ID. The LEVEL is the binding
333 level at which this declaration is being bound. */
335 static void
336 push_binding (tree id, tree decl, cp_binding_level* level)
338 cxx_binding *binding;
340 if (level != class_binding_level)
342 binding = cxx_binding_make (decl, NULL_TREE);
343 binding->scope = level;
345 else
346 binding = new_class_binding (id, decl, /*type=*/NULL_TREE, level);
348 /* Now, fill in the binding information. */
349 binding->previous = IDENTIFIER_BINDING (id);
350 INHERITED_VALUE_BINDING_P (binding) = 0;
351 LOCAL_BINDING_P (binding) = (level != class_binding_level);
353 /* And put it on the front of the list of bindings for ID. */
354 IDENTIFIER_BINDING (id) = binding;
357 /* Remove the binding for DECL which should be the innermost binding
358 for ID. */
360 void
361 pop_binding (tree id, tree decl)
363 cxx_binding *binding;
365 if (id == NULL_TREE)
366 /* It's easiest to write the loops that call this function without
367 checking whether or not the entities involved have names. We
368 get here for such an entity. */
369 return;
371 /* Get the innermost binding for ID. */
372 binding = IDENTIFIER_BINDING (id);
374 /* The name should be bound. */
375 gcc_assert (binding != NULL);
377 /* The DECL will be either the ordinary binding or the type
378 binding for this identifier. Remove that binding. */
379 if (binding->value == decl)
380 binding->value = NULL_TREE;
381 else
383 gcc_assert (binding->type == decl);
384 binding->type = NULL_TREE;
387 if (!binding->value && !binding->type)
389 /* We're completely done with the innermost binding for this
390 identifier. Unhook it from the list of bindings. */
391 IDENTIFIER_BINDING (id) = binding->previous;
393 /* Add it to the free list. */
394 cxx_binding_free (binding);
398 /* Remove the bindings for the decls of the current level and leave
399 the current scope. */
401 void
402 pop_bindings_and_leave_scope (void)
404 for (tree t = getdecls (); t; t = DECL_CHAIN (t))
405 pop_binding (DECL_NAME (t), t);
406 leave_scope ();
409 /* Strip non dependent using declarations. If DECL is dependent,
410 surreptitiously create a typename_type and return it. */
412 tree
413 strip_using_decl (tree decl)
415 if (decl == NULL_TREE)
416 return NULL_TREE;
418 while (TREE_CODE (decl) == USING_DECL && !DECL_DEPENDENT_P (decl))
419 decl = USING_DECL_DECLS (decl);
421 if (TREE_CODE (decl) == USING_DECL && DECL_DEPENDENT_P (decl)
422 && USING_DECL_TYPENAME_P (decl))
424 /* We have found a type introduced by a using
425 declaration at class scope that refers to a dependent
426 type.
428 using typename :: [opt] nested-name-specifier unqualified-id ;
430 decl = make_typename_type (TREE_TYPE (decl),
431 DECL_NAME (decl),
432 typename_type, tf_error);
433 if (decl != error_mark_node)
434 decl = TYPE_NAME (decl);
437 return decl;
440 /* BINDING records an existing declaration for a name in the current scope.
441 But, DECL is another declaration for that same identifier in the
442 same scope. This is the `struct stat' hack whereby a non-typedef
443 class name or enum-name can be bound at the same level as some other
444 kind of entity.
445 3.3.7/1
447 A class name (9.1) or enumeration name (7.2) can be hidden by the
448 name of an object, function, or enumerator declared in the same scope.
449 If a class or enumeration name and an object, function, or enumerator
450 are declared in the same scope (in any order) with the same name, the
451 class or enumeration name is hidden wherever the object, function, or
452 enumerator name is visible.
454 It's the responsibility of the caller to check that
455 inserting this name is valid here. Returns nonzero if the new binding
456 was successful. */
458 static bool
459 supplement_binding_1 (cxx_binding *binding, tree decl)
461 tree bval = binding->value;
462 bool ok = true;
463 tree target_bval = strip_using_decl (bval);
464 tree target_decl = strip_using_decl (decl);
466 if (TREE_CODE (target_decl) == TYPE_DECL && DECL_ARTIFICIAL (target_decl)
467 && target_decl != target_bval
468 && (TREE_CODE (target_bval) != TYPE_DECL
469 /* We allow pushing an enum multiple times in a class
470 template in order to handle late matching of underlying
471 type on an opaque-enum-declaration followed by an
472 enum-specifier. */
473 || (processing_template_decl
474 && TREE_CODE (TREE_TYPE (target_decl)) == ENUMERAL_TYPE
475 && TREE_CODE (TREE_TYPE (target_bval)) == ENUMERAL_TYPE
476 && (dependent_type_p (ENUM_UNDERLYING_TYPE
477 (TREE_TYPE (target_decl)))
478 || dependent_type_p (ENUM_UNDERLYING_TYPE
479 (TREE_TYPE (target_bval)))))))
480 /* The new name is the type name. */
481 binding->type = decl;
482 else if (/* TARGET_BVAL is null when push_class_level_binding moves
483 an inherited type-binding out of the way to make room
484 for a new value binding. */
485 !target_bval
486 /* TARGET_BVAL is error_mark_node when TARGET_DECL's name
487 has been used in a non-class scope prior declaration.
488 In that case, we should have already issued a
489 diagnostic; for graceful error recovery purpose, pretend
490 this was the intended declaration for that name. */
491 || target_bval == error_mark_node
492 /* If TARGET_BVAL is anticipated but has not yet been
493 declared, pretend it is not there at all. */
494 || (TREE_CODE (target_bval) == FUNCTION_DECL
495 && DECL_ANTICIPATED (target_bval)
496 && !DECL_HIDDEN_FRIEND_P (target_bval)))
497 binding->value = decl;
498 else if (TREE_CODE (target_bval) == TYPE_DECL
499 && DECL_ARTIFICIAL (target_bval)
500 && target_decl != target_bval
501 && (TREE_CODE (target_decl) != TYPE_DECL
502 || same_type_p (TREE_TYPE (target_decl),
503 TREE_TYPE (target_bval))))
505 /* The old binding was a type name. It was placed in
506 VALUE field because it was thought, at the point it was
507 declared, to be the only entity with such a name. Move the
508 type name into the type slot; it is now hidden by the new
509 binding. */
510 binding->type = bval;
511 binding->value = decl;
512 binding->value_is_inherited = false;
514 else if (TREE_CODE (target_bval) == TYPE_DECL
515 && TREE_CODE (target_decl) == TYPE_DECL
516 && DECL_NAME (target_decl) == DECL_NAME (target_bval)
517 && binding->scope->kind != sk_class
518 && (same_type_p (TREE_TYPE (target_decl), TREE_TYPE (target_bval))
519 /* If either type involves template parameters, we must
520 wait until instantiation. */
521 || uses_template_parms (TREE_TYPE (target_decl))
522 || uses_template_parms (TREE_TYPE (target_bval))))
523 /* We have two typedef-names, both naming the same type to have
524 the same name. In general, this is OK because of:
526 [dcl.typedef]
528 In a given scope, a typedef specifier can be used to redefine
529 the name of any type declared in that scope to refer to the
530 type to which it already refers.
532 However, in class scopes, this rule does not apply due to the
533 stricter language in [class.mem] prohibiting redeclarations of
534 members. */
535 ok = false;
536 /* There can be two block-scope declarations of the same variable,
537 so long as they are `extern' declarations. However, there cannot
538 be two declarations of the same static data member:
540 [class.mem]
542 A member shall not be declared twice in the
543 member-specification. */
544 else if (VAR_P (target_decl)
545 && VAR_P (target_bval)
546 && DECL_EXTERNAL (target_decl) && DECL_EXTERNAL (target_bval)
547 && !DECL_CLASS_SCOPE_P (target_decl))
549 duplicate_decls (decl, binding->value, /*newdecl_is_friend=*/false);
550 ok = false;
552 else if (TREE_CODE (decl) == NAMESPACE_DECL
553 && TREE_CODE (bval) == NAMESPACE_DECL
554 && DECL_NAMESPACE_ALIAS (decl)
555 && DECL_NAMESPACE_ALIAS (bval)
556 && ORIGINAL_NAMESPACE (bval) == ORIGINAL_NAMESPACE (decl))
557 /* [namespace.alias]
559 In a declarative region, a namespace-alias-definition can be
560 used to redefine a namespace-alias declared in that declarative
561 region to refer only to the namespace to which it already
562 refers. */
563 ok = false;
564 else
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;
770 TREE_PUBLIC (x) = TREE_PUBLIC (t);
772 if (cp_function_chain->extern_decl_map == NULL)
773 cp_function_chain->extern_decl_map
774 = hash_table<cxx_int_tree_map_hasher>::create_ggc (20);
776 h = ggc_alloc<cxx_int_tree_map> ();
777 h->uid = DECL_UID (x);
778 h->to = t;
779 cxx_int_tree_map **loc = cp_function_chain->extern_decl_map
780 ->find_slot (h, INSERT);
781 *loc = h;
784 else if (TREE_CODE (t) == PARM_DECL)
786 /* Check for duplicate params. */
787 tree d = duplicate_decls (x, t, is_friend);
788 if (d)
789 return d;
791 else if ((DECL_EXTERN_C_FUNCTION_P (x)
792 || DECL_FUNCTION_TEMPLATE_P (x))
793 && is_overloaded_fn (t))
794 /* Don't do anything just yet. */;
795 else if (t == wchar_decl_node)
797 if (! DECL_IN_SYSTEM_HEADER (x))
798 pedwarn (input_location, OPT_Wpedantic, "redeclaration of %<wchar_t%> as %qT",
799 TREE_TYPE (x));
801 /* Throw away the redeclaration. */
802 return t;
804 else
806 tree olddecl = duplicate_decls (x, t, is_friend);
808 /* If the redeclaration failed, we can stop at this
809 point. */
810 if (olddecl == error_mark_node)
811 return error_mark_node;
813 if (olddecl)
815 if (TREE_CODE (t) == TYPE_DECL)
816 SET_IDENTIFIER_TYPE_VALUE (name, TREE_TYPE (t));
818 return t;
820 else if (DECL_MAIN_P (x) && TREE_CODE (t) == FUNCTION_DECL)
822 /* A redeclaration of main, but not a duplicate of the
823 previous one.
825 [basic.start.main]
827 This function shall not be overloaded. */
828 error ("invalid redeclaration of %q+D", t);
829 error ("as %qD", x);
830 /* We don't try to push this declaration since that
831 causes a crash. */
832 return x;
837 /* If x has C linkage-specification, (extern "C"),
838 lookup its binding, in case it's already bound to an object.
839 The lookup is done in all namespaces.
840 If we find an existing binding, make sure it has the same
841 exception specification as x, otherwise, bail in error [7.5, 7.6]. */
842 if ((TREE_CODE (x) == FUNCTION_DECL)
843 && DECL_EXTERN_C_P (x)
844 /* We should ignore declarations happening in system headers. */
845 && !DECL_ARTIFICIAL (x)
846 && !DECL_IN_SYSTEM_HEADER (x))
848 tree previous = lookup_extern_c_fun_in_all_ns (x);
849 if (previous
850 && !DECL_ARTIFICIAL (previous)
851 && !DECL_IN_SYSTEM_HEADER (previous)
852 && DECL_CONTEXT (previous) != DECL_CONTEXT (x))
854 /* In case either x or previous is declared to throw an exception,
855 make sure both exception specifications are equal. */
856 if (decls_match (x, previous))
858 tree x_exception_spec = NULL_TREE;
859 tree previous_exception_spec = NULL_TREE;
861 x_exception_spec =
862 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (x));
863 previous_exception_spec =
864 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (previous));
865 if (!comp_except_specs (previous_exception_spec,
866 x_exception_spec,
867 ce_normal))
869 pedwarn (input_location, 0,
870 "declaration of %q#D with C language linkage",
872 pedwarn (input_location, 0,
873 "conflicts with previous declaration %q+#D",
874 previous);
875 pedwarn (input_location, 0,
876 "due to different exception specifications");
877 return error_mark_node;
879 if (DECL_ASSEMBLER_NAME_SET_P (previous))
880 SET_DECL_ASSEMBLER_NAME (x,
881 DECL_ASSEMBLER_NAME (previous));
883 else
885 pedwarn (input_location, 0,
886 "declaration of %q#D with C language linkage", x);
887 pedwarn (input_location, 0,
888 "conflicts with previous declaration %q+#D",
889 previous);
894 check_template_shadow (x);
896 /* If this is a function conjured up by the back end, massage it
897 so it looks friendly. */
898 if (DECL_NON_THUNK_FUNCTION_P (x) && ! DECL_LANG_SPECIFIC (x))
900 retrofit_lang_decl (x);
901 SET_DECL_LANGUAGE (x, lang_c);
904 t = x;
905 if (DECL_NON_THUNK_FUNCTION_P (x) && ! DECL_FUNCTION_MEMBER_P (x))
907 t = push_overloaded_decl (x, PUSH_LOCAL, is_friend);
908 if (!namespace_bindings_p ())
909 /* We do not need to create a binding for this name;
910 push_overloaded_decl will have already done so if
911 necessary. */
912 need_new_binding = 0;
914 else if (DECL_FUNCTION_TEMPLATE_P (x) && DECL_NAMESPACE_SCOPE_P (x))
916 t = push_overloaded_decl (x, PUSH_GLOBAL, is_friend);
917 if (t == x)
918 add_decl_to_level (x, NAMESPACE_LEVEL (CP_DECL_CONTEXT (t)));
921 if (DECL_DECLARES_FUNCTION_P (t))
922 check_default_args (t);
924 if (t != x || DECL_FUNCTION_TEMPLATE_P (t))
925 return t;
927 /* If declaring a type as a typedef, copy the type (unless we're
928 at line 0), and install this TYPE_DECL as the new type's typedef
929 name. See the extensive comment of set_underlying_type (). */
930 if (TREE_CODE (x) == TYPE_DECL)
932 tree type = TREE_TYPE (x);
934 if (DECL_IS_BUILTIN (x)
935 || (TREE_TYPE (x) != error_mark_node
936 && TYPE_NAME (type) != x
937 /* We don't want to copy the type when all we're
938 doing is making a TYPE_DECL for the purposes of
939 inlining. */
940 && (!TYPE_NAME (type)
941 || TYPE_NAME (type) != DECL_ABSTRACT_ORIGIN (x))))
942 set_underlying_type (x);
944 if (type != error_mark_node
945 && TYPE_IDENTIFIER (type))
946 set_identifier_type_value (DECL_NAME (x), x);
948 /* If this is a locally defined typedef in a function that
949 is not a template instantation, record it to implement
950 -Wunused-local-typedefs. */
951 if (current_instantiation () == NULL
952 || (current_instantiation ()->decl != current_function_decl))
953 record_locally_defined_typedef (x);
956 /* Multiple external decls of the same identifier ought to match.
958 We get warnings about inline functions where they are defined.
959 We get warnings about other functions from push_overloaded_decl.
961 Avoid duplicate warnings where they are used. */
962 if (TREE_PUBLIC (x) && TREE_CODE (x) != FUNCTION_DECL)
964 tree decl;
966 decl = IDENTIFIER_NAMESPACE_VALUE (name);
967 if (decl && TREE_CODE (decl) == OVERLOAD)
968 decl = OVL_FUNCTION (decl);
970 if (decl && decl != error_mark_node
971 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl))
972 /* If different sort of thing, we already gave an error. */
973 && TREE_CODE (decl) == TREE_CODE (x)
974 && !comptypes (TREE_TYPE (x), TREE_TYPE (decl),
975 COMPARE_REDECLARATION))
977 if (permerror (input_location, "type mismatch with previous "
978 "external decl of %q#D", x))
979 inform (input_location, "previous external decl of %q+#D",
980 decl);
984 if (TREE_CODE (x) == FUNCTION_DECL
985 && is_friend
986 && !flag_friend_injection)
988 /* This is a new declaration of a friend function, so hide
989 it from ordinary function lookup. */
990 DECL_ANTICIPATED (x) = 1;
991 DECL_HIDDEN_FRIEND_P (x) = 1;
994 /* This name is new in its binding level.
995 Install the new declaration and return it. */
996 if (namespace_bindings_p ())
998 /* Install a global value. */
1000 /* If the first global decl has external linkage,
1001 warn if we later see static one. */
1002 if (IDENTIFIER_GLOBAL_VALUE (name) == NULL_TREE && TREE_PUBLIC (x))
1003 TREE_PUBLIC (name) = 1;
1005 /* Bind the name for the entity. */
1006 if (!(TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x)
1007 && t != NULL_TREE)
1008 && (TREE_CODE (x) == TYPE_DECL
1009 || VAR_P (x)
1010 || TREE_CODE (x) == NAMESPACE_DECL
1011 || TREE_CODE (x) == CONST_DECL
1012 || TREE_CODE (x) == TEMPLATE_DECL))
1013 SET_IDENTIFIER_NAMESPACE_VALUE (name, x);
1015 /* If new decl is `static' and an `extern' was seen previously,
1016 warn about it. */
1017 if (x != NULL_TREE && t != NULL_TREE && decls_match (x, t))
1018 warn_extern_redeclared_static (x, t);
1020 else
1022 /* Here to install a non-global value. */
1023 tree oldglobal = IDENTIFIER_NAMESPACE_VALUE (name);
1024 tree oldlocal = NULL_TREE;
1025 cp_binding_level *oldscope = NULL;
1026 cxx_binding *oldbinding = outer_binding (name, NULL, true);
1027 if (oldbinding)
1029 oldlocal = oldbinding->value;
1030 oldscope = oldbinding->scope;
1033 if (need_new_binding)
1035 push_local_binding (name, x, 0);
1036 /* Because push_local_binding will hook X on to the
1037 current_binding_level's name list, we don't want to
1038 do that again below. */
1039 need_new_binding = 0;
1042 /* If this is a TYPE_DECL, push it into the type value slot. */
1043 if (TREE_CODE (x) == TYPE_DECL)
1044 set_identifier_type_value (name, x);
1046 /* Clear out any TYPE_DECL shadowed by a namespace so that
1047 we won't think this is a type. The C struct hack doesn't
1048 go through namespaces. */
1049 if (TREE_CODE (x) == NAMESPACE_DECL)
1050 set_identifier_type_value (name, NULL_TREE);
1052 if (oldlocal)
1054 tree d = oldlocal;
1056 while (oldlocal
1057 && VAR_P (oldlocal)
1058 && DECL_DEAD_FOR_LOCAL (oldlocal))
1059 oldlocal = DECL_SHADOWED_FOR_VAR (oldlocal);
1061 if (oldlocal == NULL_TREE)
1062 oldlocal = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (d));
1065 /* If this is an extern function declaration, see if we
1066 have a global definition or declaration for the function. */
1067 if (oldlocal == NULL_TREE
1068 && DECL_EXTERNAL (x)
1069 && oldglobal != NULL_TREE
1070 && TREE_CODE (x) == FUNCTION_DECL
1071 && TREE_CODE (oldglobal) == FUNCTION_DECL)
1073 /* We have one. Their types must agree. */
1074 if (decls_match (x, oldglobal))
1075 /* OK */;
1076 else
1078 warning (0, "extern declaration of %q#D doesn%'t match", x);
1079 warning (0, "global declaration %q+#D", oldglobal);
1082 /* If we have a local external declaration,
1083 and no file-scope declaration has yet been seen,
1084 then if we later have a file-scope decl it must not be static. */
1085 if (oldlocal == NULL_TREE
1086 && oldglobal == NULL_TREE
1087 && DECL_EXTERNAL (x)
1088 && TREE_PUBLIC (x))
1089 TREE_PUBLIC (name) = 1;
1091 /* Don't complain about the parms we push and then pop
1092 while tentatively parsing a function declarator. */
1093 if (TREE_CODE (x) == PARM_DECL && DECL_CONTEXT (x) == NULL_TREE)
1094 /* Ignore. */;
1096 /* Warn if shadowing an argument at the top level of the body. */
1097 else if (oldlocal != NULL_TREE && !DECL_EXTERNAL (x)
1098 /* Inline decls shadow nothing. */
1099 && !DECL_FROM_INLINE (x)
1100 && (TREE_CODE (oldlocal) == PARM_DECL
1101 || VAR_P (oldlocal)
1102 /* If the old decl is a type decl, only warn if the
1103 old decl is an explicit typedef or if both the old
1104 and new decls are type decls. */
1105 || (TREE_CODE (oldlocal) == TYPE_DECL
1106 && (!DECL_ARTIFICIAL (oldlocal)
1107 || TREE_CODE (x) == TYPE_DECL)))
1108 /* Don't check for internally generated vars unless
1109 it's an implicit typedef (see create_implicit_typedef
1110 in decl.c). */
1111 && (!DECL_ARTIFICIAL (x) || DECL_IMPLICIT_TYPEDEF_P (x)))
1113 bool nowarn = false;
1115 /* Don't complain if it's from an enclosing function. */
1116 if (DECL_CONTEXT (oldlocal) == current_function_decl
1117 && TREE_CODE (x) != PARM_DECL
1118 && TREE_CODE (oldlocal) == PARM_DECL)
1120 /* Go to where the parms should be and see if we find
1121 them there. */
1122 cp_binding_level *b = current_binding_level->level_chain;
1124 if (FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
1125 /* Skip the ctor/dtor cleanup level. */
1126 b = b->level_chain;
1128 /* ARM $8.3 */
1129 if (b->kind == sk_function_parms)
1131 error ("declaration of %q#D shadows a parameter", x);
1132 nowarn = true;
1136 /* The local structure or class can't use parameters of
1137 the containing function anyway. */
1138 if (DECL_CONTEXT (oldlocal) != current_function_decl)
1140 cp_binding_level *scope = current_binding_level;
1141 tree context = DECL_CONTEXT (oldlocal);
1142 for (; scope; scope = scope->level_chain)
1144 if (scope->kind == sk_function_parms
1145 && scope->this_entity == context)
1146 break;
1147 if (scope->kind == sk_class
1148 && !LAMBDA_TYPE_P (scope->this_entity))
1150 nowarn = true;
1151 break;
1155 /* Error if redeclaring a local declared in a
1156 for-init-statement or in the condition of an if or
1157 switch statement when the new declaration is in the
1158 outermost block of the controlled statement.
1159 Redeclaring a variable from a for or while condition is
1160 detected elsewhere. */
1161 else if (VAR_P (oldlocal)
1162 && oldscope == current_binding_level->level_chain
1163 && (oldscope->kind == sk_cond
1164 || oldscope->kind == sk_for))
1166 error ("redeclaration of %q#D", x);
1167 inform (input_location, "%q+#D previously declared here",
1168 oldlocal);
1169 nowarn = true;
1171 /* C++11:
1172 3.3.3/3: The name declared in an exception-declaration (...)
1173 shall not be redeclared in the outermost block of the handler.
1174 3.3.3/2: A parameter name shall not be redeclared (...) in
1175 the outermost block of any handler associated with a
1176 function-try-block.
1177 3.4.1/15: The function parameter names shall not be redeclared
1178 in the exception-declaration nor in the outermost block of a
1179 handler for the function-try-block. */
1180 else if ((VAR_P (oldlocal)
1181 && oldscope == current_binding_level->level_chain
1182 && oldscope->kind == sk_catch)
1183 || (TREE_CODE (oldlocal) == PARM_DECL
1184 && (current_binding_level->kind == sk_catch
1185 || (current_binding_level->level_chain->kind
1186 == sk_catch))
1187 && in_function_try_handler))
1189 if (permerror (input_location, "redeclaration of %q#D", x))
1190 inform (input_location, "%q+#D previously declared here",
1191 oldlocal);
1192 nowarn = true;
1195 if (warn_shadow && !nowarn)
1197 bool warned;
1199 if (TREE_CODE (oldlocal) == PARM_DECL)
1200 warned = warning_at (input_location, OPT_Wshadow,
1201 "declaration of %q#D shadows a parameter", x);
1202 else if (is_capture_proxy (oldlocal))
1203 warned = warning_at (input_location, OPT_Wshadow,
1204 "declaration of %qD shadows a lambda capture",
1206 else
1207 warned = warning_at (input_location, OPT_Wshadow,
1208 "declaration of %qD shadows a previous local",
1211 if (warned)
1212 inform (DECL_SOURCE_LOCATION (oldlocal),
1213 "shadowed declaration is here");
1217 /* Maybe warn if shadowing something else. */
1218 else if (warn_shadow && !DECL_EXTERNAL (x)
1219 /* No shadow warnings for internally generated vars unless
1220 it's an implicit typedef (see create_implicit_typedef
1221 in decl.c). */
1222 && (! DECL_ARTIFICIAL (x) || DECL_IMPLICIT_TYPEDEF_P (x))
1223 /* No shadow warnings for vars made for inlining. */
1224 && ! DECL_FROM_INLINE (x))
1226 tree member;
1228 if (nonlambda_method_basetype ())
1229 member = lookup_member (current_nonlambda_class_type (),
1230 name,
1231 /*protect=*/0,
1232 /*want_type=*/false,
1233 tf_warning_or_error);
1234 else
1235 member = NULL_TREE;
1237 if (member && !TREE_STATIC (member))
1239 if (BASELINK_P (member))
1240 member = BASELINK_FUNCTIONS (member);
1241 member = OVL_CURRENT (member);
1243 /* Do not warn if a variable shadows a function, unless
1244 the variable is a function or a pointer-to-function. */
1245 if (TREE_CODE (member) != FUNCTION_DECL
1246 || TREE_CODE (x) == FUNCTION_DECL
1247 || TYPE_PTRFN_P (TREE_TYPE (x))
1248 || TYPE_PTRMEMFUNC_P (TREE_TYPE (x)))
1250 if (warning_at (input_location, OPT_Wshadow,
1251 "declaration of %qD shadows a member of %qT",
1252 x, current_nonlambda_class_type ())
1253 && DECL_P (member))
1254 inform (DECL_SOURCE_LOCATION (member),
1255 "shadowed declaration is here");
1258 else if (oldglobal != NULL_TREE
1259 && (VAR_P (oldglobal)
1260 /* If the old decl is a type decl, only warn if the
1261 old decl is an explicit typedef or if both the
1262 old and new decls are type decls. */
1263 || (TREE_CODE (oldglobal) == TYPE_DECL
1264 && (!DECL_ARTIFICIAL (oldglobal)
1265 || TREE_CODE (x) == TYPE_DECL))))
1266 /* XXX shadow warnings in outer-more namespaces */
1268 if (warning_at (input_location, OPT_Wshadow,
1269 "declaration of %qD shadows a "
1270 "global declaration", x))
1271 inform (DECL_SOURCE_LOCATION (oldglobal),
1272 "shadowed declaration is here");
1277 if (VAR_P (x))
1278 maybe_register_incomplete_var (x);
1281 if (need_new_binding)
1282 add_decl_to_level (x,
1283 DECL_NAMESPACE_SCOPE_P (x)
1284 ? NAMESPACE_LEVEL (CP_DECL_CONTEXT (x))
1285 : current_binding_level);
1287 return x;
1290 /* Wrapper for pushdecl_maybe_friend_1. */
1292 tree
1293 pushdecl_maybe_friend (tree x, bool is_friend)
1295 tree ret;
1296 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
1297 ret = pushdecl_maybe_friend_1 (x, is_friend);
1298 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
1299 return ret;
1302 /* Record a decl-node X as belonging to the current lexical scope. */
1304 tree
1305 pushdecl (tree x)
1307 return pushdecl_maybe_friend (x, false);
1310 /* Enter DECL into the symbol table, if that's appropriate. Returns
1311 DECL, or a modified version thereof. */
1313 tree
1314 maybe_push_decl (tree decl)
1316 tree type = TREE_TYPE (decl);
1318 /* Add this decl to the current binding level, but not if it comes
1319 from another scope, e.g. a static member variable. TEM may equal
1320 DECL or it may be a previous decl of the same name. */
1321 if (decl == error_mark_node
1322 || (TREE_CODE (decl) != PARM_DECL
1323 && DECL_CONTEXT (decl) != NULL_TREE
1324 /* Definitions of namespace members outside their namespace are
1325 possible. */
1326 && !DECL_NAMESPACE_SCOPE_P (decl))
1327 || (TREE_CODE (decl) == TEMPLATE_DECL && !namespace_bindings_p ())
1328 || type == unknown_type_node
1329 /* The declaration of a template specialization does not affect
1330 the functions available for overload resolution, so we do not
1331 call pushdecl. */
1332 || (TREE_CODE (decl) == FUNCTION_DECL
1333 && DECL_TEMPLATE_SPECIALIZATION (decl)))
1334 return decl;
1335 else
1336 return pushdecl (decl);
1339 /* Bind DECL to ID in the current_binding_level, assumed to be a local
1340 binding level. If PUSH_USING is set in FLAGS, we know that DECL
1341 doesn't really belong to this binding level, that it got here
1342 through a using-declaration. */
1344 void
1345 push_local_binding (tree id, tree decl, int flags)
1347 cp_binding_level *b;
1349 /* Skip over any local classes. This makes sense if we call
1350 push_local_binding with a friend decl of a local class. */
1351 b = innermost_nonclass_level ();
1353 if (lookup_name_innermost_nonclass_level (id))
1355 /* Supplement the existing binding. */
1356 if (!supplement_binding (IDENTIFIER_BINDING (id), decl))
1357 /* It didn't work. Something else must be bound at this
1358 level. Do not add DECL to the list of things to pop
1359 later. */
1360 return;
1362 else
1363 /* Create a new binding. */
1364 push_binding (id, decl, b);
1366 if (TREE_CODE (decl) == OVERLOAD || (flags & PUSH_USING))
1367 /* We must put the OVERLOAD into a TREE_LIST since the
1368 TREE_CHAIN of an OVERLOAD is already used. Similarly for
1369 decls that got here through a using-declaration. */
1370 decl = build_tree_list (NULL_TREE, decl);
1372 /* And put DECL on the list of things declared by the current
1373 binding level. */
1374 add_decl_to_level (decl, b);
1377 /* Check to see whether or not DECL is a variable that would have been
1378 in scope under the ARM, but is not in scope under the ANSI/ISO
1379 standard. If so, issue an error message. If name lookup would
1380 work in both cases, but return a different result, this function
1381 returns the result of ANSI/ISO lookup. Otherwise, it returns
1382 DECL. */
1384 tree
1385 check_for_out_of_scope_variable (tree decl)
1387 tree shadowed;
1389 /* We only care about out of scope variables. */
1390 if (!(VAR_P (decl) && DECL_DEAD_FOR_LOCAL (decl)))
1391 return decl;
1393 shadowed = DECL_HAS_SHADOWED_FOR_VAR_P (decl)
1394 ? DECL_SHADOWED_FOR_VAR (decl) : NULL_TREE ;
1395 while (shadowed != NULL_TREE && VAR_P (shadowed)
1396 && DECL_DEAD_FOR_LOCAL (shadowed))
1397 shadowed = DECL_HAS_SHADOWED_FOR_VAR_P (shadowed)
1398 ? DECL_SHADOWED_FOR_VAR (shadowed) : NULL_TREE;
1399 if (!shadowed)
1400 shadowed = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (decl));
1401 if (shadowed)
1403 if (!DECL_ERROR_REPORTED (decl))
1405 warning (0, "name lookup of %qD changed", DECL_NAME (decl));
1406 warning (0, " matches this %q+D under ISO standard rules",
1407 shadowed);
1408 warning (0, " matches this %q+D under old rules", decl);
1409 DECL_ERROR_REPORTED (decl) = 1;
1411 return shadowed;
1414 /* If we have already complained about this declaration, there's no
1415 need to do it again. */
1416 if (DECL_ERROR_REPORTED (decl))
1417 return decl;
1419 DECL_ERROR_REPORTED (decl) = 1;
1421 if (TREE_TYPE (decl) == error_mark_node)
1422 return decl;
1424 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
1426 error ("name lookup of %qD changed for ISO %<for%> scoping",
1427 DECL_NAME (decl));
1428 error (" cannot use obsolete binding at %q+D because "
1429 "it has a destructor", decl);
1430 return error_mark_node;
1432 else
1434 permerror (input_location, "name lookup of %qD changed for ISO %<for%> scoping",
1435 DECL_NAME (decl));
1436 if (flag_permissive)
1437 permerror (input_location, " using obsolete binding at %q+D", decl);
1438 else
1440 static bool hint;
1441 if (!hint)
1443 inform (input_location, "(if you use %<-fpermissive%> G++ will accept your code)");
1444 hint = true;
1449 return decl;
1452 /* true means unconditionally make a BLOCK for the next level pushed. */
1454 static bool keep_next_level_flag;
1456 static int binding_depth = 0;
1458 static void
1459 indent (int depth)
1461 int i;
1463 for (i = 0; i < depth * 2; i++)
1464 putc (' ', stderr);
1467 /* Return a string describing the kind of SCOPE we have. */
1468 static const char *
1469 cp_binding_level_descriptor (cp_binding_level *scope)
1471 /* The order of this table must match the "scope_kind"
1472 enumerators. */
1473 static const char* scope_kind_names[] = {
1474 "block-scope",
1475 "cleanup-scope",
1476 "try-scope",
1477 "catch-scope",
1478 "for-scope",
1479 "function-parameter-scope",
1480 "class-scope",
1481 "namespace-scope",
1482 "template-parameter-scope",
1483 "template-explicit-spec-scope"
1485 const scope_kind kind = scope->explicit_spec_p
1486 ? sk_template_spec : scope->kind;
1488 return scope_kind_names[kind];
1491 /* Output a debugging information about SCOPE when performing
1492 ACTION at LINE. */
1493 static void
1494 cp_binding_level_debug (cp_binding_level *scope, int line, const char *action)
1496 const char *desc = cp_binding_level_descriptor (scope);
1497 if (scope->this_entity)
1498 verbatim ("%s %s(%E) %p %d\n", action, desc,
1499 scope->this_entity, (void *) scope, line);
1500 else
1501 verbatim ("%s %s %p %d\n", action, desc, (void *) scope, line);
1504 /* Return the estimated initial size of the hashtable of a NAMESPACE
1505 scope. */
1507 static inline size_t
1508 namespace_scope_ht_size (tree ns)
1510 tree name = DECL_NAME (ns);
1512 return name == std_identifier
1513 ? NAMESPACE_STD_HT_SIZE
1514 : (name == global_scope_name
1515 ? GLOBAL_SCOPE_HT_SIZE
1516 : NAMESPACE_ORDINARY_HT_SIZE);
1519 /* A chain of binding_level structures awaiting reuse. */
1521 static GTY((deletable)) cp_binding_level *free_binding_level;
1523 /* Insert SCOPE as the innermost binding level. */
1525 void
1526 push_binding_level (cp_binding_level *scope)
1528 /* Add it to the front of currently active scopes stack. */
1529 scope->level_chain = current_binding_level;
1530 current_binding_level = scope;
1531 keep_next_level_flag = false;
1533 if (ENABLE_SCOPE_CHECKING)
1535 scope->binding_depth = binding_depth;
1536 indent (binding_depth);
1537 cp_binding_level_debug (scope, LOCATION_LINE (input_location),
1538 "push");
1539 binding_depth++;
1543 /* Create a new KIND scope and make it the top of the active scopes stack.
1544 ENTITY is the scope of the associated C++ entity (namespace, class,
1545 function, C++0x enumeration); it is NULL otherwise. */
1547 cp_binding_level *
1548 begin_scope (scope_kind kind, tree entity)
1550 cp_binding_level *scope;
1552 /* Reuse or create a struct for this binding level. */
1553 if (!ENABLE_SCOPE_CHECKING && free_binding_level)
1555 scope = free_binding_level;
1556 memset (scope, 0, sizeof (cp_binding_level));
1557 free_binding_level = scope->level_chain;
1559 else
1560 scope = ggc_cleared_alloc<cp_binding_level> ();
1562 scope->this_entity = entity;
1563 scope->more_cleanups_ok = true;
1564 switch (kind)
1566 case sk_cleanup:
1567 scope->keep = true;
1568 break;
1570 case sk_template_spec:
1571 scope->explicit_spec_p = true;
1572 kind = sk_template_parms;
1573 /* Fall through. */
1574 case sk_template_parms:
1575 case sk_block:
1576 case sk_try:
1577 case sk_catch:
1578 case sk_for:
1579 case sk_cond:
1580 case sk_class:
1581 case sk_scoped_enum:
1582 case sk_function_parms:
1583 case sk_omp:
1584 scope->keep = keep_next_level_flag;
1585 break;
1587 case sk_namespace:
1588 NAMESPACE_LEVEL (entity) = scope;
1589 vec_alloc (scope->static_decls,
1590 (DECL_NAME (entity) == std_identifier
1591 || DECL_NAME (entity) == global_scope_name) ? 200 : 10);
1592 break;
1594 default:
1595 /* Should not happen. */
1596 gcc_unreachable ();
1597 break;
1599 scope->kind = kind;
1601 push_binding_level (scope);
1603 return scope;
1606 /* We're about to leave current scope. Pop the top of the stack of
1607 currently active scopes. Return the enclosing scope, now active. */
1609 cp_binding_level *
1610 leave_scope (void)
1612 cp_binding_level *scope = current_binding_level;
1614 if (scope->kind == sk_namespace && class_binding_level)
1615 current_binding_level = class_binding_level;
1617 /* We cannot leave a scope, if there are none left. */
1618 if (NAMESPACE_LEVEL (global_namespace))
1619 gcc_assert (!global_scope_p (scope));
1621 if (ENABLE_SCOPE_CHECKING)
1623 indent (--binding_depth);
1624 cp_binding_level_debug (scope, LOCATION_LINE (input_location),
1625 "leave");
1628 /* Move one nesting level up. */
1629 current_binding_level = scope->level_chain;
1631 /* Namespace-scopes are left most probably temporarily, not
1632 completely; they can be reopened later, e.g. in namespace-extension
1633 or any name binding activity that requires us to resume a
1634 namespace. For classes, we cache some binding levels. For other
1635 scopes, we just make the structure available for reuse. */
1636 if (scope->kind != sk_namespace
1637 && scope->kind != sk_class)
1639 scope->level_chain = free_binding_level;
1640 gcc_assert (!ENABLE_SCOPE_CHECKING
1641 || scope->binding_depth == binding_depth);
1642 free_binding_level = scope;
1645 if (scope->kind == sk_class)
1647 /* Reset DEFINING_CLASS_P to allow for reuse of a
1648 class-defining scope in a non-defining context. */
1649 scope->defining_class_p = 0;
1651 /* Find the innermost enclosing class scope, and reset
1652 CLASS_BINDING_LEVEL appropriately. */
1653 class_binding_level = NULL;
1654 for (scope = current_binding_level; scope; scope = scope->level_chain)
1655 if (scope->kind == sk_class)
1657 class_binding_level = scope;
1658 break;
1662 return current_binding_level;
1665 static void
1666 resume_scope (cp_binding_level* b)
1668 /* Resuming binding levels is meant only for namespaces,
1669 and those cannot nest into classes. */
1670 gcc_assert (!class_binding_level);
1671 /* Also, resuming a non-directly nested namespace is a no-no. */
1672 gcc_assert (b->level_chain == current_binding_level);
1673 current_binding_level = b;
1674 if (ENABLE_SCOPE_CHECKING)
1676 b->binding_depth = binding_depth;
1677 indent (binding_depth);
1678 cp_binding_level_debug (b, LOCATION_LINE (input_location), "resume");
1679 binding_depth++;
1683 /* Return the innermost binding level that is not for a class scope. */
1685 static cp_binding_level *
1686 innermost_nonclass_level (void)
1688 cp_binding_level *b;
1690 b = current_binding_level;
1691 while (b->kind == sk_class)
1692 b = b->level_chain;
1694 return b;
1697 /* We're defining an object of type TYPE. If it needs a cleanup, but
1698 we're not allowed to add any more objects with cleanups to the current
1699 scope, create a new binding level. */
1701 void
1702 maybe_push_cleanup_level (tree type)
1704 if (type != error_mark_node
1705 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
1706 && current_binding_level->more_cleanups_ok == 0)
1708 begin_scope (sk_cleanup, NULL);
1709 current_binding_level->statement_list = push_stmt_list ();
1713 /* Return true if we are in the global binding level. */
1715 bool
1716 global_bindings_p (void)
1718 return global_scope_p (current_binding_level);
1721 /* True if we are currently in a toplevel binding level. This
1722 means either the global binding level or a namespace in a toplevel
1723 binding level. Since there are no non-toplevel namespace levels,
1724 this really means any namespace or template parameter level. We
1725 also include a class whose context is toplevel. */
1727 bool
1728 toplevel_bindings_p (void)
1730 cp_binding_level *b = innermost_nonclass_level ();
1732 return b->kind == sk_namespace || b->kind == sk_template_parms;
1735 /* True if this is a namespace scope, or if we are defining a class
1736 which is itself at namespace scope, or whose enclosing class is
1737 such a class, etc. */
1739 bool
1740 namespace_bindings_p (void)
1742 cp_binding_level *b = innermost_nonclass_level ();
1744 return b->kind == sk_namespace;
1747 /* True if the innermost non-class scope is a block scope. */
1749 bool
1750 local_bindings_p (void)
1752 cp_binding_level *b = innermost_nonclass_level ();
1753 return b->kind < sk_function_parms || b->kind == sk_omp;
1756 /* True if the current level needs to have a BLOCK made. */
1758 bool
1759 kept_level_p (void)
1761 return (current_binding_level->blocks != NULL_TREE
1762 || current_binding_level->keep
1763 || current_binding_level->kind == sk_cleanup
1764 || current_binding_level->names != NULL_TREE
1765 || current_binding_level->using_directives);
1768 /* Returns the kind of the innermost scope. */
1770 scope_kind
1771 innermost_scope_kind (void)
1773 return current_binding_level->kind;
1776 /* Returns true if this scope was created to store template parameters. */
1778 bool
1779 template_parm_scope_p (void)
1781 return innermost_scope_kind () == sk_template_parms;
1784 /* If KEEP is true, make a BLOCK node for the next binding level,
1785 unconditionally. Otherwise, use the normal logic to decide whether
1786 or not to create a BLOCK. */
1788 void
1789 keep_next_level (bool keep)
1791 keep_next_level_flag = keep;
1794 /* Return the list of declarations of the current level.
1795 Note that this list is in reverse order unless/until
1796 you nreverse it; and when you do nreverse it, you must
1797 store the result back using `storedecls' or you will lose. */
1799 tree
1800 getdecls (void)
1802 return current_binding_level->names;
1805 /* Return how many function prototypes we are currently nested inside. */
1808 function_parm_depth (void)
1810 int level = 0;
1811 cp_binding_level *b;
1813 for (b = current_binding_level;
1814 b->kind == sk_function_parms;
1815 b = b->level_chain)
1816 ++level;
1818 return level;
1821 /* For debugging. */
1822 static int no_print_functions = 0;
1823 static int no_print_builtins = 0;
1825 static void
1826 print_binding_level (cp_binding_level* lvl)
1828 tree t;
1829 int i = 0, len;
1830 fprintf (stderr, " blocks=%p", (void *) lvl->blocks);
1831 if (lvl->more_cleanups_ok)
1832 fprintf (stderr, " more-cleanups-ok");
1833 if (lvl->have_cleanups)
1834 fprintf (stderr, " have-cleanups");
1835 fprintf (stderr, "\n");
1836 if (lvl->names)
1838 fprintf (stderr, " names:\t");
1839 /* We can probably fit 3 names to a line? */
1840 for (t = lvl->names; t; t = TREE_CHAIN (t))
1842 if (no_print_functions && (TREE_CODE (t) == FUNCTION_DECL))
1843 continue;
1844 if (no_print_builtins
1845 && (TREE_CODE (t) == TYPE_DECL)
1846 && DECL_IS_BUILTIN (t))
1847 continue;
1849 /* Function decls tend to have longer names. */
1850 if (TREE_CODE (t) == FUNCTION_DECL)
1851 len = 3;
1852 else
1853 len = 2;
1854 i += len;
1855 if (i > 6)
1857 fprintf (stderr, "\n\t");
1858 i = len;
1860 print_node_brief (stderr, "", t, 0);
1861 if (t == error_mark_node)
1862 break;
1864 if (i)
1865 fprintf (stderr, "\n");
1867 if (vec_safe_length (lvl->class_shadowed))
1869 size_t i;
1870 cp_class_binding *b;
1871 fprintf (stderr, " class-shadowed:");
1872 FOR_EACH_VEC_ELT (*lvl->class_shadowed, i, b)
1873 fprintf (stderr, " %s ", IDENTIFIER_POINTER (b->identifier));
1874 fprintf (stderr, "\n");
1876 if (lvl->type_shadowed)
1878 fprintf (stderr, " type-shadowed:");
1879 for (t = lvl->type_shadowed; t; t = TREE_CHAIN (t))
1881 fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
1883 fprintf (stderr, "\n");
1887 DEBUG_FUNCTION void
1888 debug (cp_binding_level &ref)
1890 print_binding_level (&ref);
1893 DEBUG_FUNCTION void
1894 debug (cp_binding_level *ptr)
1896 if (ptr)
1897 debug (*ptr);
1898 else
1899 fprintf (stderr, "<nil>\n");
1903 void
1904 print_other_binding_stack (cp_binding_level *stack)
1906 cp_binding_level *level;
1907 for (level = stack; !global_scope_p (level); level = level->level_chain)
1909 fprintf (stderr, "binding level %p\n", (void *) level);
1910 print_binding_level (level);
1914 void
1915 print_binding_stack (void)
1917 cp_binding_level *b;
1918 fprintf (stderr, "current_binding_level=%p\n"
1919 "class_binding_level=%p\n"
1920 "NAMESPACE_LEVEL (global_namespace)=%p\n",
1921 (void *) current_binding_level, (void *) class_binding_level,
1922 (void *) NAMESPACE_LEVEL (global_namespace));
1923 if (class_binding_level)
1925 for (b = class_binding_level; b; b = b->level_chain)
1926 if (b == current_binding_level)
1927 break;
1928 if (b)
1929 b = class_binding_level;
1930 else
1931 b = current_binding_level;
1933 else
1934 b = current_binding_level;
1935 print_other_binding_stack (b);
1936 fprintf (stderr, "global:\n");
1937 print_binding_level (NAMESPACE_LEVEL (global_namespace));
1940 /* Return the type associated with ID. */
1942 static tree
1943 identifier_type_value_1 (tree id)
1945 /* There is no type with that name, anywhere. */
1946 if (REAL_IDENTIFIER_TYPE_VALUE (id) == NULL_TREE)
1947 return NULL_TREE;
1948 /* This is not the type marker, but the real thing. */
1949 if (REAL_IDENTIFIER_TYPE_VALUE (id) != global_type_node)
1950 return REAL_IDENTIFIER_TYPE_VALUE (id);
1951 /* Have to search for it. It must be on the global level, now.
1952 Ask lookup_name not to return non-types. */
1953 id = lookup_name_real (id, 2, 1, /*block_p=*/true, 0, 0);
1954 if (id)
1955 return TREE_TYPE (id);
1956 return NULL_TREE;
1959 /* Wrapper for identifier_type_value_1. */
1961 tree
1962 identifier_type_value (tree id)
1964 tree ret;
1965 timevar_start (TV_NAME_LOOKUP);
1966 ret = identifier_type_value_1 (id);
1967 timevar_stop (TV_NAME_LOOKUP);
1968 return ret;
1972 /* Return the IDENTIFIER_GLOBAL_VALUE of T, for use in common code, since
1973 the definition of IDENTIFIER_GLOBAL_VALUE is different for C and C++. */
1975 tree
1976 identifier_global_value (tree t)
1978 return IDENTIFIER_GLOBAL_VALUE (t);
1981 /* Push a definition of struct, union or enum tag named ID. into
1982 binding_level B. DECL is a TYPE_DECL for the type. We assume that
1983 the tag ID is not already defined. */
1985 static void
1986 set_identifier_type_value_with_scope (tree id, tree decl, cp_binding_level *b)
1988 tree type;
1990 if (b->kind != sk_namespace)
1992 /* Shadow the marker, not the real thing, so that the marker
1993 gets restored later. */
1994 tree old_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
1995 b->type_shadowed
1996 = tree_cons (id, old_type_value, b->type_shadowed);
1997 type = decl ? TREE_TYPE (decl) : NULL_TREE;
1998 TREE_TYPE (b->type_shadowed) = type;
2000 else
2002 cxx_binding *binding =
2003 binding_for_name (NAMESPACE_LEVEL (current_namespace), id);
2004 gcc_assert (decl);
2005 if (binding->value)
2006 supplement_binding (binding, decl);
2007 else
2008 binding->value = decl;
2010 /* Store marker instead of real type. */
2011 type = global_type_node;
2013 SET_IDENTIFIER_TYPE_VALUE (id, type);
2016 /* As set_identifier_type_value_with_scope, but using
2017 current_binding_level. */
2019 void
2020 set_identifier_type_value (tree id, tree decl)
2022 set_identifier_type_value_with_scope (id, decl, current_binding_level);
2025 /* Return the name for the constructor (or destructor) for the
2026 specified class TYPE. When given a template, this routine doesn't
2027 lose the specialization. */
2029 static inline tree
2030 constructor_name_full (tree type)
2032 return TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
2035 /* Return the name for the constructor (or destructor) for the
2036 specified class. When given a template, return the plain
2037 unspecialized name. */
2039 tree
2040 constructor_name (tree type)
2042 tree name;
2043 name = constructor_name_full (type);
2044 if (IDENTIFIER_TEMPLATE (name))
2045 name = IDENTIFIER_TEMPLATE (name);
2046 return name;
2049 /* Returns TRUE if NAME is the name for the constructor for TYPE,
2050 which must be a class type. */
2052 bool
2053 constructor_name_p (tree name, tree type)
2055 tree ctor_name;
2057 gcc_assert (MAYBE_CLASS_TYPE_P (type));
2059 if (!name)
2060 return false;
2062 if (!identifier_p (name))
2063 return false;
2065 /* These don't have names. */
2066 if (TREE_CODE (type) == DECLTYPE_TYPE
2067 || TREE_CODE (type) == TYPEOF_TYPE)
2068 return false;
2070 ctor_name = constructor_name_full (type);
2071 if (name == ctor_name)
2072 return true;
2073 if (IDENTIFIER_TEMPLATE (ctor_name)
2074 && name == IDENTIFIER_TEMPLATE (ctor_name))
2075 return true;
2076 return false;
2079 /* Counter used to create anonymous type names. */
2081 static GTY(()) int anon_cnt;
2083 /* Return an IDENTIFIER which can be used as a name for
2084 anonymous structs and unions. */
2086 tree
2087 make_anon_name (void)
2089 char buf[32];
2091 sprintf (buf, ANON_AGGRNAME_FORMAT, anon_cnt++);
2092 return get_identifier (buf);
2095 /* This code is practically identical to that for creating
2096 anonymous names, but is just used for lambdas instead. This isn't really
2097 necessary, but it's convenient to avoid treating lambdas like other
2098 anonymous types. */
2100 static GTY(()) int lambda_cnt = 0;
2102 tree
2103 make_lambda_name (void)
2105 char buf[32];
2107 sprintf (buf, LAMBDANAME_FORMAT, lambda_cnt++);
2108 return get_identifier (buf);
2111 /* Return (from the stack of) the BINDING, if any, established at SCOPE. */
2113 static inline cxx_binding *
2114 find_binding (cp_binding_level *scope, cxx_binding *binding)
2116 for (; binding != NULL; binding = binding->previous)
2117 if (binding->scope == scope)
2118 return binding;
2120 return (cxx_binding *)0;
2123 /* Return the binding for NAME in SCOPE, if any. Otherwise, return NULL. */
2125 static inline cxx_binding *
2126 cp_binding_level_find_binding_for_name (cp_binding_level *scope, tree name)
2128 cxx_binding *b = IDENTIFIER_NAMESPACE_BINDINGS (name);
2129 if (b)
2131 /* Fold-in case where NAME is used only once. */
2132 if (scope == b->scope && b->previous == NULL)
2133 return b;
2134 return find_binding (scope, b);
2136 return NULL;
2139 /* Always returns a binding for name in scope. If no binding is
2140 found, make a new one. */
2142 static cxx_binding *
2143 binding_for_name (cp_binding_level *scope, tree name)
2145 cxx_binding *result;
2147 result = cp_binding_level_find_binding_for_name (scope, name);
2148 if (result)
2149 return result;
2150 /* Not found, make a new one. */
2151 result = cxx_binding_make (NULL, NULL);
2152 result->previous = IDENTIFIER_NAMESPACE_BINDINGS (name);
2153 result->scope = scope;
2154 result->is_local = false;
2155 result->value_is_inherited = false;
2156 IDENTIFIER_NAMESPACE_BINDINGS (name) = result;
2157 return result;
2160 /* Walk through the bindings associated to the name of FUNCTION,
2161 and return the first declaration of a function with a
2162 "C" linkage specification, a.k.a 'extern "C"'.
2163 This function looks for the binding, regardless of which scope it
2164 has been defined in. It basically looks in all the known scopes.
2165 Note that this function does not lookup for bindings of builtin functions
2166 or for functions declared in system headers. */
2167 static tree
2168 lookup_extern_c_fun_in_all_ns (tree function)
2170 tree name;
2171 cxx_binding *iter;
2173 gcc_assert (function && TREE_CODE (function) == FUNCTION_DECL);
2175 name = DECL_NAME (function);
2176 gcc_assert (name && identifier_p (name));
2178 for (iter = IDENTIFIER_NAMESPACE_BINDINGS (name);
2179 iter;
2180 iter = iter->previous)
2182 tree ovl;
2183 for (ovl = iter->value; ovl; ovl = OVL_NEXT (ovl))
2185 tree decl = OVL_CURRENT (ovl);
2186 if (decl
2187 && TREE_CODE (decl) == FUNCTION_DECL
2188 && DECL_EXTERN_C_P (decl)
2189 && !DECL_ARTIFICIAL (decl))
2191 return decl;
2195 return NULL;
2198 /* Returns a list of C-linkage decls with the name NAME. */
2200 tree
2201 c_linkage_bindings (tree name)
2203 tree decls = NULL_TREE;
2204 cxx_binding *iter;
2206 for (iter = IDENTIFIER_NAMESPACE_BINDINGS (name);
2207 iter;
2208 iter = iter->previous)
2210 tree ovl;
2211 for (ovl = iter->value; ovl; ovl = OVL_NEXT (ovl))
2213 tree decl = OVL_CURRENT (ovl);
2214 if (decl
2215 && DECL_EXTERN_C_P (decl)
2216 && !DECL_ARTIFICIAL (decl))
2218 if (decls == NULL_TREE)
2219 decls = decl;
2220 else
2221 decls = tree_cons (NULL_TREE, decl, decls);
2225 return decls;
2228 /* Insert another USING_DECL into the current binding level, returning
2229 this declaration. If this is a redeclaration, do nothing, and
2230 return NULL_TREE if this not in namespace scope (in namespace
2231 scope, a using decl might extend any previous bindings). */
2233 static tree
2234 push_using_decl_1 (tree scope, tree name)
2236 tree decl;
2238 gcc_assert (TREE_CODE (scope) == NAMESPACE_DECL);
2239 gcc_assert (identifier_p (name));
2240 for (decl = current_binding_level->usings; decl; decl = DECL_CHAIN (decl))
2241 if (USING_DECL_SCOPE (decl) == scope && DECL_NAME (decl) == name)
2242 break;
2243 if (decl)
2244 return namespace_bindings_p () ? decl : NULL_TREE;
2245 decl = build_lang_decl (USING_DECL, name, NULL_TREE);
2246 USING_DECL_SCOPE (decl) = scope;
2247 DECL_CHAIN (decl) = current_binding_level->usings;
2248 current_binding_level->usings = decl;
2249 return decl;
2252 /* Wrapper for push_using_decl_1. */
2254 static tree
2255 push_using_decl (tree scope, tree name)
2257 tree ret;
2258 timevar_start (TV_NAME_LOOKUP);
2259 ret = push_using_decl_1 (scope, name);
2260 timevar_stop (TV_NAME_LOOKUP);
2261 return ret;
2264 /* Same as pushdecl, but define X in binding-level LEVEL. We rely on the
2265 caller to set DECL_CONTEXT properly.
2267 Note that this must only be used when X will be the new innermost
2268 binding for its name, as we tack it onto the front of IDENTIFIER_BINDING
2269 without checking to see if the current IDENTIFIER_BINDING comes from a
2270 closer binding level than LEVEL. */
2272 static tree
2273 pushdecl_with_scope_1 (tree x, cp_binding_level *level, bool is_friend)
2275 cp_binding_level *b;
2276 tree function_decl = current_function_decl;
2278 current_function_decl = NULL_TREE;
2279 if (level->kind == sk_class)
2281 b = class_binding_level;
2282 class_binding_level = level;
2283 pushdecl_class_level (x);
2284 class_binding_level = b;
2286 else
2288 b = current_binding_level;
2289 current_binding_level = level;
2290 x = pushdecl_maybe_friend (x, is_friend);
2291 current_binding_level = b;
2293 current_function_decl = function_decl;
2294 return x;
2297 /* Wrapper for pushdecl_with_scope_1. */
2299 tree
2300 pushdecl_with_scope (tree x, cp_binding_level *level, bool is_friend)
2302 tree ret;
2303 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
2304 ret = pushdecl_with_scope_1 (x, level, is_friend);
2305 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
2306 return ret;
2309 /* Helper function for push_overloaded_decl_1 and do_nonmember_using_decl.
2310 Compares the parameter-type-lists of DECL1 and DECL2 and returns false
2311 if they are different. If the DECLs are template functions, the return
2312 types and the template parameter lists are compared too (DR 565). */
2314 static bool
2315 compparms_for_decl_and_using_decl (tree decl1, tree decl2)
2317 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (decl1)),
2318 TYPE_ARG_TYPES (TREE_TYPE (decl2))))
2319 return false;
2321 if (! DECL_FUNCTION_TEMPLATE_P (decl1)
2322 || ! DECL_FUNCTION_TEMPLATE_P (decl2))
2323 return true;
2325 return (comp_template_parms (DECL_TEMPLATE_PARMS (decl1),
2326 DECL_TEMPLATE_PARMS (decl2))
2327 && same_type_p (TREE_TYPE (TREE_TYPE (decl1)),
2328 TREE_TYPE (TREE_TYPE (decl2))));
2331 /* DECL is a FUNCTION_DECL for a non-member function, which may have
2332 other definitions already in place. We get around this by making
2333 the value of the identifier point to a list of all the things that
2334 want to be referenced by that name. It is then up to the users of
2335 that name to decide what to do with that list.
2337 DECL may also be a TEMPLATE_DECL, with a FUNCTION_DECL in its
2338 DECL_TEMPLATE_RESULT. It is dealt with the same way.
2340 FLAGS is a bitwise-or of the following values:
2341 PUSH_LOCAL: Bind DECL in the current scope, rather than at
2342 namespace scope.
2343 PUSH_USING: DECL is being pushed as the result of a using
2344 declaration.
2346 IS_FRIEND is true if this is a friend declaration.
2348 The value returned may be a previous declaration if we guessed wrong
2349 about what language DECL should belong to (C or C++). Otherwise,
2350 it's always DECL (and never something that's not a _DECL). */
2352 static tree
2353 push_overloaded_decl_1 (tree decl, int flags, bool is_friend)
2355 tree name = DECL_NAME (decl);
2356 tree old;
2357 tree new_binding;
2358 int doing_global = (namespace_bindings_p () || !(flags & PUSH_LOCAL));
2360 if (doing_global)
2361 old = namespace_binding (name, DECL_CONTEXT (decl));
2362 else
2363 old = lookup_name_innermost_nonclass_level (name);
2365 if (old)
2367 if (TREE_CODE (old) == TYPE_DECL && DECL_ARTIFICIAL (old))
2369 tree t = TREE_TYPE (old);
2370 if (MAYBE_CLASS_TYPE_P (t) && warn_shadow
2371 && (! DECL_IN_SYSTEM_HEADER (decl)
2372 || ! DECL_IN_SYSTEM_HEADER (old)))
2373 warning (OPT_Wshadow, "%q#D hides constructor for %q#T", decl, t);
2374 old = NULL_TREE;
2376 else if (is_overloaded_fn (old))
2378 tree tmp;
2380 for (tmp = old; tmp; tmp = OVL_NEXT (tmp))
2382 tree fn = OVL_CURRENT (tmp);
2383 tree dup;
2385 if (TREE_CODE (tmp) == OVERLOAD && OVL_USED (tmp)
2386 && !(flags & PUSH_USING)
2387 && compparms_for_decl_and_using_decl (fn, decl)
2388 && ! decls_match (fn, decl))
2389 diagnose_name_conflict (decl, fn);
2391 dup = duplicate_decls (decl, fn, is_friend);
2392 /* If DECL was a redeclaration of FN -- even an invalid
2393 one -- pass that information along to our caller. */
2394 if (dup == fn || dup == error_mark_node)
2395 return dup;
2398 /* We don't overload implicit built-ins. duplicate_decls()
2399 may fail to merge the decls if the new decl is e.g. a
2400 template function. */
2401 if (TREE_CODE (old) == FUNCTION_DECL
2402 && DECL_ANTICIPATED (old)
2403 && !DECL_HIDDEN_FRIEND_P (old))
2404 old = NULL;
2406 else if (old == error_mark_node)
2407 /* Ignore the undefined symbol marker. */
2408 old = NULL_TREE;
2409 else
2411 error ("previous non-function declaration %q+#D", old);
2412 error ("conflicts with function declaration %q#D", decl);
2413 return decl;
2417 if (old || TREE_CODE (decl) == TEMPLATE_DECL
2418 /* If it's a using declaration, we always need to build an OVERLOAD,
2419 because it's the only way to remember that the declaration comes
2420 from 'using', and have the lookup behave correctly. */
2421 || (flags & PUSH_USING))
2423 if (old && TREE_CODE (old) != OVERLOAD)
2424 new_binding = ovl_cons (decl, ovl_cons (old, NULL_TREE));
2425 else
2426 new_binding = ovl_cons (decl, old);
2427 if (flags & PUSH_USING)
2428 OVL_USED (new_binding) = 1;
2430 else
2431 /* NAME is not ambiguous. */
2432 new_binding = decl;
2434 if (doing_global)
2435 set_namespace_binding (name, current_namespace, new_binding);
2436 else
2438 /* We only create an OVERLOAD if there was a previous binding at
2439 this level, or if decl is a template. In the former case, we
2440 need to remove the old binding and replace it with the new
2441 binding. We must also run through the NAMES on the binding
2442 level where the name was bound to update the chain. */
2444 if (TREE_CODE (new_binding) == OVERLOAD && old)
2446 tree *d;
2448 for (d = &IDENTIFIER_BINDING (name)->scope->names;
2450 d = &TREE_CHAIN (*d))
2451 if (*d == old
2452 || (TREE_CODE (*d) == TREE_LIST
2453 && TREE_VALUE (*d) == old))
2455 if (TREE_CODE (*d) == TREE_LIST)
2456 /* Just replace the old binding with the new. */
2457 TREE_VALUE (*d) = new_binding;
2458 else
2459 /* Build a TREE_LIST to wrap the OVERLOAD. */
2460 *d = tree_cons (NULL_TREE, new_binding,
2461 TREE_CHAIN (*d));
2463 /* And update the cxx_binding node. */
2464 IDENTIFIER_BINDING (name)->value = new_binding;
2465 return decl;
2468 /* We should always find a previous binding in this case. */
2469 gcc_unreachable ();
2472 /* Install the new binding. */
2473 push_local_binding (name, new_binding, flags);
2476 return decl;
2479 /* Wrapper for push_overloaded_decl_1. */
2481 static tree
2482 push_overloaded_decl (tree decl, int flags, bool is_friend)
2484 tree ret;
2485 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
2486 ret = push_overloaded_decl_1 (decl, flags, is_friend);
2487 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
2488 return ret;
2491 /* Check a non-member using-declaration. Return the name and scope
2492 being used, and the USING_DECL, or NULL_TREE on failure. */
2494 static tree
2495 validate_nonmember_using_decl (tree decl, tree scope, tree name)
2497 /* [namespace.udecl]
2498 A using-declaration for a class member shall be a
2499 member-declaration. */
2500 if (TYPE_P (scope))
2502 error ("%qT is not a namespace or unscoped enum", scope);
2503 return NULL_TREE;
2505 else if (scope == error_mark_node)
2506 return NULL_TREE;
2508 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR)
2510 /* 7.3.3/5
2511 A using-declaration shall not name a template-id. */
2512 error ("a using-declaration cannot specify a template-id. "
2513 "Try %<using %D%>", name);
2514 return NULL_TREE;
2517 if (TREE_CODE (decl) == NAMESPACE_DECL)
2519 error ("namespace %qD not allowed in using-declaration", decl);
2520 return NULL_TREE;
2523 if (TREE_CODE (decl) == SCOPE_REF)
2525 /* It's a nested name with template parameter dependent scope.
2526 This can only be using-declaration for class member. */
2527 error ("%qT is not a namespace", TREE_OPERAND (decl, 0));
2528 return NULL_TREE;
2531 if (is_overloaded_fn (decl))
2532 decl = get_first_fn (decl);
2534 gcc_assert (DECL_P (decl));
2536 /* Make a USING_DECL. */
2537 tree using_decl = push_using_decl (scope, name);
2539 if (using_decl == NULL_TREE
2540 && at_function_scope_p ()
2541 && VAR_P (decl))
2542 /* C++11 7.3.3/10. */
2543 error ("%qD is already declared in this scope", name);
2545 return using_decl;
2548 /* Process local and global using-declarations. */
2550 static void
2551 do_nonmember_using_decl (tree scope, tree name, tree oldval, tree oldtype,
2552 tree *newval, tree *newtype)
2554 struct scope_binding decls = EMPTY_SCOPE_BINDING;
2556 *newval = *newtype = NULL_TREE;
2557 if (!qualified_lookup_using_namespace (name, scope, &decls, 0))
2558 /* Lookup error */
2559 return;
2561 if (!decls.value && !decls.type)
2563 error ("%qD not declared", name);
2564 return;
2567 /* Shift the old and new bindings around so we're comparing class and
2568 enumeration names to each other. */
2569 if (oldval && DECL_IMPLICIT_TYPEDEF_P (oldval))
2571 oldtype = oldval;
2572 oldval = NULL_TREE;
2575 if (decls.value && DECL_IMPLICIT_TYPEDEF_P (decls.value))
2577 decls.type = decls.value;
2578 decls.value = NULL_TREE;
2581 /* It is impossible to overload a built-in function; any explicit
2582 declaration eliminates the built-in declaration. So, if OLDVAL
2583 is a built-in, then we can just pretend it isn't there. */
2584 if (oldval
2585 && TREE_CODE (oldval) == FUNCTION_DECL
2586 && DECL_ANTICIPATED (oldval)
2587 && !DECL_HIDDEN_FRIEND_P (oldval))
2588 oldval = NULL_TREE;
2590 if (decls.value)
2592 /* Check for using functions. */
2593 if (is_overloaded_fn (decls.value))
2595 tree tmp, tmp1;
2597 if (oldval && !is_overloaded_fn (oldval))
2599 error ("%qD is already declared in this scope", name);
2600 oldval = NULL_TREE;
2603 *newval = oldval;
2604 for (tmp = decls.value; tmp; tmp = OVL_NEXT (tmp))
2606 tree new_fn = OVL_CURRENT (tmp);
2608 /* [namespace.udecl]
2610 If a function declaration in namespace scope or block
2611 scope has the same name and the same parameter types as a
2612 function introduced by a using declaration the program is
2613 ill-formed. */
2614 for (tmp1 = oldval; tmp1; tmp1 = OVL_NEXT (tmp1))
2616 tree old_fn = OVL_CURRENT (tmp1);
2618 if (new_fn == old_fn)
2619 /* The function already exists in the current namespace. */
2620 break;
2621 else if (TREE_CODE (tmp1) == OVERLOAD && OVL_USED (tmp1))
2622 continue; /* this is a using decl */
2623 else if (compparms_for_decl_and_using_decl (new_fn, old_fn))
2625 gcc_assert (!DECL_ANTICIPATED (old_fn)
2626 || DECL_HIDDEN_FRIEND_P (old_fn));
2628 /* There was already a non-using declaration in
2629 this scope with the same parameter types. If both
2630 are the same extern "C" functions, that's ok. */
2631 if (decls_match (new_fn, old_fn))
2632 break;
2633 else
2635 diagnose_name_conflict (new_fn, old_fn);
2636 break;
2641 /* If we broke out of the loop, there's no reason to add
2642 this function to the using declarations for this
2643 scope. */
2644 if (tmp1)
2645 continue;
2647 /* If we are adding to an existing OVERLOAD, then we no
2648 longer know the type of the set of functions. */
2649 if (*newval && TREE_CODE (*newval) == OVERLOAD)
2650 TREE_TYPE (*newval) = unknown_type_node;
2651 /* Add this new function to the set. */
2652 *newval = build_overload (OVL_CURRENT (tmp), *newval);
2653 /* If there is only one function, then we use its type. (A
2654 using-declaration naming a single function can be used in
2655 contexts where overload resolution cannot be
2656 performed.) */
2657 if (TREE_CODE (*newval) != OVERLOAD)
2659 *newval = ovl_cons (*newval, NULL_TREE);
2660 TREE_TYPE (*newval) = TREE_TYPE (OVL_CURRENT (tmp));
2662 OVL_USED (*newval) = 1;
2665 else
2667 *newval = decls.value;
2668 if (oldval && !decls_match (*newval, oldval))
2669 error ("%qD is already declared in this scope", name);
2672 else
2673 *newval = oldval;
2675 if (decls.type && TREE_CODE (decls.type) == TREE_LIST)
2677 error ("reference to %qD is ambiguous", name);
2678 print_candidates (decls.type);
2680 else
2682 *newtype = decls.type;
2683 if (oldtype && *newtype && !decls_match (oldtype, *newtype))
2684 error ("%qD is already declared in this scope", name);
2687 /* If *newval is empty, shift any class or enumeration name down. */
2688 if (!*newval)
2690 *newval = *newtype;
2691 *newtype = NULL_TREE;
2695 /* Process a using-declaration at function scope. */
2697 void
2698 do_local_using_decl (tree decl, tree scope, tree name)
2700 tree oldval, oldtype, newval, newtype;
2701 tree orig_decl = decl;
2703 decl = validate_nonmember_using_decl (decl, scope, name);
2704 if (decl == NULL_TREE)
2705 return;
2707 if (building_stmt_list_p ()
2708 && at_function_scope_p ())
2709 add_decl_expr (decl);
2711 oldval = lookup_name_innermost_nonclass_level (name);
2712 oldtype = lookup_type_current_level (name);
2714 do_nonmember_using_decl (scope, name, oldval, oldtype, &newval, &newtype);
2716 if (newval)
2718 if (is_overloaded_fn (newval))
2720 tree fn, term;
2722 /* We only need to push declarations for those functions
2723 that were not already bound in the current level.
2724 The old value might be NULL_TREE, it might be a single
2725 function, or an OVERLOAD. */
2726 if (oldval && TREE_CODE (oldval) == OVERLOAD)
2727 term = OVL_FUNCTION (oldval);
2728 else
2729 term = oldval;
2730 for (fn = newval; fn && OVL_CURRENT (fn) != term;
2731 fn = OVL_NEXT (fn))
2732 push_overloaded_decl (OVL_CURRENT (fn),
2733 PUSH_LOCAL | PUSH_USING,
2734 false);
2736 else
2737 push_local_binding (name, newval, PUSH_USING);
2739 if (newtype)
2741 push_local_binding (name, newtype, PUSH_USING);
2742 set_identifier_type_value (name, newtype);
2745 /* Emit debug info. */
2746 if (!processing_template_decl)
2747 cp_emit_debug_info_for_using (orig_decl, current_scope());
2750 /* Returns true if ROOT (a namespace, class, or function) encloses
2751 CHILD. CHILD may be either a class type or a namespace. */
2753 bool
2754 is_ancestor (tree root, tree child)
2756 gcc_assert ((TREE_CODE (root) == NAMESPACE_DECL
2757 || TREE_CODE (root) == FUNCTION_DECL
2758 || CLASS_TYPE_P (root)));
2759 gcc_assert ((TREE_CODE (child) == NAMESPACE_DECL
2760 || CLASS_TYPE_P (child)));
2762 /* The global namespace encloses everything. */
2763 if (root == global_namespace)
2764 return true;
2766 while (true)
2768 /* If we've run out of scopes, stop. */
2769 if (!child)
2770 return false;
2771 /* If we've reached the ROOT, it encloses CHILD. */
2772 if (root == child)
2773 return true;
2774 /* Go out one level. */
2775 if (TYPE_P (child))
2776 child = TYPE_NAME (child);
2777 child = DECL_CONTEXT (child);
2781 /* Enter the class or namespace scope indicated by T suitable for name
2782 lookup. T can be arbitrary scope, not necessary nested inside the
2783 current scope. Returns a non-null scope to pop iff pop_scope
2784 should be called later to exit this scope. */
2786 tree
2787 push_scope (tree t)
2789 if (TREE_CODE (t) == NAMESPACE_DECL)
2790 push_decl_namespace (t);
2791 else if (CLASS_TYPE_P (t))
2793 if (!at_class_scope_p ()
2794 || !same_type_p (current_class_type, t))
2795 push_nested_class (t);
2796 else
2797 /* T is the same as the current scope. There is therefore no
2798 need to re-enter the scope. Since we are not actually
2799 pushing a new scope, our caller should not call
2800 pop_scope. */
2801 t = NULL_TREE;
2804 return t;
2807 /* Leave scope pushed by push_scope. */
2809 void
2810 pop_scope (tree t)
2812 if (t == NULL_TREE)
2813 return;
2814 if (TREE_CODE (t) == NAMESPACE_DECL)
2815 pop_decl_namespace ();
2816 else if CLASS_TYPE_P (t)
2817 pop_nested_class ();
2820 /* Subroutine of push_inner_scope. */
2822 static void
2823 push_inner_scope_r (tree outer, tree inner)
2825 tree prev;
2827 if (outer == inner
2828 || (TREE_CODE (inner) != NAMESPACE_DECL && !CLASS_TYPE_P (inner)))
2829 return;
2831 prev = CP_DECL_CONTEXT (TREE_CODE (inner) == NAMESPACE_DECL ? inner : TYPE_NAME (inner));
2832 if (outer != prev)
2833 push_inner_scope_r (outer, prev);
2834 if (TREE_CODE (inner) == NAMESPACE_DECL)
2836 cp_binding_level *save_template_parm = 0;
2837 /* Temporary take out template parameter scopes. They are saved
2838 in reversed order in save_template_parm. */
2839 while (current_binding_level->kind == sk_template_parms)
2841 cp_binding_level *b = current_binding_level;
2842 current_binding_level = b->level_chain;
2843 b->level_chain = save_template_parm;
2844 save_template_parm = b;
2847 resume_scope (NAMESPACE_LEVEL (inner));
2848 current_namespace = inner;
2850 /* Restore template parameter scopes. */
2851 while (save_template_parm)
2853 cp_binding_level *b = save_template_parm;
2854 save_template_parm = b->level_chain;
2855 b->level_chain = current_binding_level;
2856 current_binding_level = b;
2859 else
2860 pushclass (inner);
2863 /* Enter the scope INNER from current scope. INNER must be a scope
2864 nested inside current scope. This works with both name lookup and
2865 pushing name into scope. In case a template parameter scope is present,
2866 namespace is pushed under the template parameter scope according to
2867 name lookup rule in 14.6.1/6.
2869 Return the former current scope suitable for pop_inner_scope. */
2871 tree
2872 push_inner_scope (tree inner)
2874 tree outer = current_scope ();
2875 if (!outer)
2876 outer = current_namespace;
2878 push_inner_scope_r (outer, inner);
2879 return outer;
2882 /* Exit the current scope INNER back to scope OUTER. */
2884 void
2885 pop_inner_scope (tree outer, tree inner)
2887 if (outer == inner
2888 || (TREE_CODE (inner) != NAMESPACE_DECL && !CLASS_TYPE_P (inner)))
2889 return;
2891 while (outer != inner)
2893 if (TREE_CODE (inner) == NAMESPACE_DECL)
2895 cp_binding_level *save_template_parm = 0;
2896 /* Temporary take out template parameter scopes. They are saved
2897 in reversed order in save_template_parm. */
2898 while (current_binding_level->kind == sk_template_parms)
2900 cp_binding_level *b = current_binding_level;
2901 current_binding_level = b->level_chain;
2902 b->level_chain = save_template_parm;
2903 save_template_parm = b;
2906 pop_namespace ();
2908 /* Restore template parameter scopes. */
2909 while (save_template_parm)
2911 cp_binding_level *b = save_template_parm;
2912 save_template_parm = b->level_chain;
2913 b->level_chain = current_binding_level;
2914 current_binding_level = b;
2917 else
2918 popclass ();
2920 inner = CP_DECL_CONTEXT (TREE_CODE (inner) == NAMESPACE_DECL ? inner : TYPE_NAME (inner));
2924 /* Do a pushlevel for class declarations. */
2926 void
2927 pushlevel_class (void)
2929 class_binding_level = begin_scope (sk_class, current_class_type);
2932 /* ...and a poplevel for class declarations. */
2934 void
2935 poplevel_class (void)
2937 cp_binding_level *level = class_binding_level;
2938 cp_class_binding *cb;
2939 size_t i;
2940 tree shadowed;
2942 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
2943 gcc_assert (level != 0);
2945 /* If we're leaving a toplevel class, cache its binding level. */
2946 if (current_class_depth == 1)
2947 previous_class_level = level;
2948 for (shadowed = level->type_shadowed;
2949 shadowed;
2950 shadowed = TREE_CHAIN (shadowed))
2951 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (shadowed), TREE_VALUE (shadowed));
2953 /* Remove the bindings for all of the class-level declarations. */
2954 if (level->class_shadowed)
2956 FOR_EACH_VEC_ELT (*level->class_shadowed, i, cb)
2958 IDENTIFIER_BINDING (cb->identifier) = cb->base->previous;
2959 cxx_binding_free (cb->base);
2961 ggc_free (level->class_shadowed);
2962 level->class_shadowed = NULL;
2965 /* Now, pop out of the binding level which we created up in the
2966 `pushlevel_class' routine. */
2967 gcc_assert (current_binding_level == level);
2968 leave_scope ();
2969 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
2972 /* Set INHERITED_VALUE_BINDING_P on BINDING to true or false, as
2973 appropriate. DECL is the value to which a name has just been
2974 bound. CLASS_TYPE is the class in which the lookup occurred. */
2976 static void
2977 set_inherited_value_binding_p (cxx_binding *binding, tree decl,
2978 tree class_type)
2980 if (binding->value == decl && TREE_CODE (decl) != TREE_LIST)
2982 tree context;
2984 if (TREE_CODE (decl) == OVERLOAD)
2985 context = ovl_scope (decl);
2986 else
2988 gcc_assert (DECL_P (decl));
2989 context = context_for_name_lookup (decl);
2992 if (is_properly_derived_from (class_type, context))
2993 INHERITED_VALUE_BINDING_P (binding) = 1;
2994 else
2995 INHERITED_VALUE_BINDING_P (binding) = 0;
2997 else if (binding->value == decl)
2998 /* We only encounter a TREE_LIST when there is an ambiguity in the
2999 base classes. Such an ambiguity can be overridden by a
3000 definition in this class. */
3001 INHERITED_VALUE_BINDING_P (binding) = 1;
3002 else
3003 INHERITED_VALUE_BINDING_P (binding) = 0;
3006 /* Make the declaration of X appear in CLASS scope. */
3008 bool
3009 pushdecl_class_level (tree x)
3011 tree name;
3012 bool is_valid = true;
3013 bool subtime;
3015 /* Do nothing if we're adding to an outer lambda closure type,
3016 outer_binding will add it later if it's needed. */
3017 if (current_class_type != class_binding_level->this_entity)
3018 return true;
3020 subtime = timevar_cond_start (TV_NAME_LOOKUP);
3021 /* Get the name of X. */
3022 if (TREE_CODE (x) == OVERLOAD)
3023 name = DECL_NAME (get_first_fn (x));
3024 else
3025 name = DECL_NAME (x);
3027 if (name)
3029 is_valid = push_class_level_binding (name, x);
3030 if (TREE_CODE (x) == TYPE_DECL)
3031 set_identifier_type_value (name, x);
3033 else if (ANON_AGGR_TYPE_P (TREE_TYPE (x)))
3035 /* If X is an anonymous aggregate, all of its members are
3036 treated as if they were members of the class containing the
3037 aggregate, for naming purposes. */
3038 tree f;
3040 for (f = TYPE_FIELDS (TREE_TYPE (x)); f; f = DECL_CHAIN (f))
3042 location_t save_location = input_location;
3043 input_location = DECL_SOURCE_LOCATION (f);
3044 if (!pushdecl_class_level (f))
3045 is_valid = false;
3046 input_location = save_location;
3049 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3050 return is_valid;
3053 /* Return the BINDING (if any) for NAME in SCOPE, which is a class
3054 scope. If the value returned is non-NULL, and the PREVIOUS field
3055 is not set, callers must set the PREVIOUS field explicitly. */
3057 static cxx_binding *
3058 get_class_binding (tree name, cp_binding_level *scope)
3060 tree class_type;
3061 tree type_binding;
3062 tree value_binding;
3063 cxx_binding *binding;
3065 class_type = scope->this_entity;
3067 /* Get the type binding. */
3068 type_binding = lookup_member (class_type, name,
3069 /*protect=*/2, /*want_type=*/true,
3070 tf_warning_or_error);
3071 /* Get the value binding. */
3072 value_binding = lookup_member (class_type, name,
3073 /*protect=*/2, /*want_type=*/false,
3074 tf_warning_or_error);
3076 if (value_binding
3077 && (TREE_CODE (value_binding) == TYPE_DECL
3078 || DECL_CLASS_TEMPLATE_P (value_binding)
3079 || (TREE_CODE (value_binding) == TREE_LIST
3080 && TREE_TYPE (value_binding) == error_mark_node
3081 && (TREE_CODE (TREE_VALUE (value_binding))
3082 == TYPE_DECL))))
3083 /* We found a type binding, even when looking for a non-type
3084 binding. This means that we already processed this binding
3085 above. */
3087 else if (value_binding)
3089 if (TREE_CODE (value_binding) == TREE_LIST
3090 && TREE_TYPE (value_binding) == error_mark_node)
3091 /* NAME is ambiguous. */
3093 else if (BASELINK_P (value_binding))
3094 /* NAME is some overloaded functions. */
3095 value_binding = BASELINK_FUNCTIONS (value_binding);
3098 /* If we found either a type binding or a value binding, create a
3099 new binding object. */
3100 if (type_binding || value_binding)
3102 binding = new_class_binding (name,
3103 value_binding,
3104 type_binding,
3105 scope);
3106 /* This is a class-scope binding, not a block-scope binding. */
3107 LOCAL_BINDING_P (binding) = 0;
3108 set_inherited_value_binding_p (binding, value_binding, class_type);
3110 else
3111 binding = NULL;
3113 return binding;
3116 /* Make the declaration(s) of X appear in CLASS scope under the name
3117 NAME. Returns true if the binding is valid. */
3119 static bool
3120 push_class_level_binding_1 (tree name, tree x)
3122 cxx_binding *binding;
3123 tree decl = x;
3124 bool ok;
3126 /* The class_binding_level will be NULL if x is a template
3127 parameter name in a member template. */
3128 if (!class_binding_level)
3129 return true;
3131 if (name == error_mark_node)
3132 return false;
3134 /* Can happen for an erroneous declaration (c++/60384). */
3135 if (!identifier_p (name))
3137 gcc_assert (errorcount || sorrycount);
3138 return false;
3141 /* Check for invalid member names. But don't worry about a default
3142 argument-scope lambda being pushed after the class is complete. */
3143 gcc_assert (TYPE_BEING_DEFINED (current_class_type)
3144 || LAMBDA_TYPE_P (TREE_TYPE (decl)));
3145 /* Check that we're pushing into the right binding level. */
3146 gcc_assert (current_class_type == class_binding_level->this_entity);
3148 /* We could have been passed a tree list if this is an ambiguous
3149 declaration. If so, pull the declaration out because
3150 check_template_shadow will not handle a TREE_LIST. */
3151 if (TREE_CODE (decl) == TREE_LIST
3152 && TREE_TYPE (decl) == error_mark_node)
3153 decl = TREE_VALUE (decl);
3155 if (!check_template_shadow (decl))
3156 return false;
3158 /* [class.mem]
3160 If T is the name of a class, then each of the following shall
3161 have a name different from T:
3163 -- every static data member of class T;
3165 -- every member of class T that is itself a type;
3167 -- every enumerator of every member of class T that is an
3168 enumerated type;
3170 -- every member of every anonymous union that is a member of
3171 class T.
3173 (Non-static data members were also forbidden to have the same
3174 name as T until TC1.) */
3175 if ((VAR_P (x)
3176 || TREE_CODE (x) == CONST_DECL
3177 || (TREE_CODE (x) == TYPE_DECL
3178 && !DECL_SELF_REFERENCE_P (x))
3179 /* A data member of an anonymous union. */
3180 || (TREE_CODE (x) == FIELD_DECL
3181 && DECL_CONTEXT (x) != current_class_type))
3182 && DECL_NAME (x) == constructor_name (current_class_type))
3184 tree scope = context_for_name_lookup (x);
3185 if (TYPE_P (scope) && same_type_p (scope, current_class_type))
3187 error ("%qD has the same name as the class in which it is "
3188 "declared",
3190 return false;
3194 /* Get the current binding for NAME in this class, if any. */
3195 binding = IDENTIFIER_BINDING (name);
3196 if (!binding || binding->scope != class_binding_level)
3198 binding = get_class_binding (name, class_binding_level);
3199 /* If a new binding was created, put it at the front of the
3200 IDENTIFIER_BINDING list. */
3201 if (binding)
3203 binding->previous = IDENTIFIER_BINDING (name);
3204 IDENTIFIER_BINDING (name) = binding;
3208 /* If there is already a binding, then we may need to update the
3209 current value. */
3210 if (binding && binding->value)
3212 tree bval = binding->value;
3213 tree old_decl = NULL_TREE;
3214 tree target_decl = strip_using_decl (decl);
3215 tree target_bval = strip_using_decl (bval);
3217 if (INHERITED_VALUE_BINDING_P (binding))
3219 /* If the old binding was from a base class, and was for a
3220 tag name, slide it over to make room for the new binding.
3221 The old binding is still visible if explicitly qualified
3222 with a class-key. */
3223 if (TREE_CODE (target_bval) == TYPE_DECL
3224 && DECL_ARTIFICIAL (target_bval)
3225 && !(TREE_CODE (target_decl) == TYPE_DECL
3226 && DECL_ARTIFICIAL (target_decl)))
3228 old_decl = binding->type;
3229 binding->type = bval;
3230 binding->value = NULL_TREE;
3231 INHERITED_VALUE_BINDING_P (binding) = 0;
3233 else
3235 old_decl = bval;
3236 /* Any inherited type declaration is hidden by the type
3237 declaration in the derived class. */
3238 if (TREE_CODE (target_decl) == TYPE_DECL
3239 && DECL_ARTIFICIAL (target_decl))
3240 binding->type = NULL_TREE;
3243 else if (TREE_CODE (target_decl) == OVERLOAD
3244 && is_overloaded_fn (target_bval))
3245 old_decl = bval;
3246 else if (TREE_CODE (decl) == USING_DECL
3247 && TREE_CODE (bval) == USING_DECL
3248 && same_type_p (USING_DECL_SCOPE (decl),
3249 USING_DECL_SCOPE (bval)))
3250 /* This is a using redeclaration that will be diagnosed later
3251 in supplement_binding */
3253 else if (TREE_CODE (decl) == USING_DECL
3254 && TREE_CODE (bval) == USING_DECL
3255 && DECL_DEPENDENT_P (decl)
3256 && DECL_DEPENDENT_P (bval))
3257 return true;
3258 else if (TREE_CODE (decl) == USING_DECL
3259 && is_overloaded_fn (target_bval))
3260 old_decl = bval;
3261 else if (TREE_CODE (bval) == USING_DECL
3262 && is_overloaded_fn (target_decl))
3263 return true;
3265 if (old_decl && binding->scope == class_binding_level)
3267 binding->value = x;
3268 /* It is always safe to clear INHERITED_VALUE_BINDING_P
3269 here. This function is only used to register bindings
3270 from with the class definition itself. */
3271 INHERITED_VALUE_BINDING_P (binding) = 0;
3272 return true;
3276 /* Note that we declared this value so that we can issue an error if
3277 this is an invalid redeclaration of a name already used for some
3278 other purpose. */
3279 note_name_declared_in_class (name, decl);
3281 /* If we didn't replace an existing binding, put the binding on the
3282 stack of bindings for the identifier, and update the shadowed
3283 list. */
3284 if (binding && binding->scope == class_binding_level)
3285 /* Supplement the existing binding. */
3286 ok = supplement_binding (binding, decl);
3287 else
3289 /* Create a new binding. */
3290 push_binding (name, decl, class_binding_level);
3291 ok = true;
3294 return ok;
3297 /* Wrapper for push_class_level_binding_1. */
3299 bool
3300 push_class_level_binding (tree name, tree x)
3302 bool ret;
3303 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3304 ret = push_class_level_binding_1 (name, x);
3305 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3306 return ret;
3309 /* Process "using SCOPE::NAME" in a class scope. Return the
3310 USING_DECL created. */
3312 tree
3313 do_class_using_decl (tree scope, tree name)
3315 /* The USING_DECL returned by this function. */
3316 tree value;
3317 /* The declaration (or declarations) name by this using
3318 declaration. NULL if we are in a template and cannot figure out
3319 what has been named. */
3320 tree decl;
3321 /* True if SCOPE is a dependent type. */
3322 bool scope_dependent_p;
3323 /* True if SCOPE::NAME is dependent. */
3324 bool name_dependent_p;
3325 /* True if any of the bases of CURRENT_CLASS_TYPE are dependent. */
3326 bool bases_dependent_p;
3327 tree binfo;
3328 tree base_binfo;
3329 int i;
3331 if (name == error_mark_node)
3332 return NULL_TREE;
3334 if (!scope || !TYPE_P (scope))
3336 error ("using-declaration for non-member at class scope");
3337 return NULL_TREE;
3340 /* Make sure the name is not invalid */
3341 if (TREE_CODE (name) == BIT_NOT_EXPR)
3343 error ("%<%T::%D%> names destructor", scope, name);
3344 return NULL_TREE;
3346 /* Using T::T declares inheriting ctors, even if T is a typedef. */
3347 if (MAYBE_CLASS_TYPE_P (scope)
3348 && (name == TYPE_IDENTIFIER (scope)
3349 || constructor_name_p (name, scope)))
3351 maybe_warn_cpp0x (CPP0X_INHERITING_CTORS);
3352 name = ctor_identifier;
3354 if (constructor_name_p (name, current_class_type))
3356 error ("%<%T::%D%> names constructor in %qT",
3357 scope, name, current_class_type);
3358 return NULL_TREE;
3361 scope_dependent_p = dependent_scope_p (scope);
3362 name_dependent_p = (scope_dependent_p
3363 || (IDENTIFIER_TYPENAME_P (name)
3364 && dependent_type_p (TREE_TYPE (name))));
3366 bases_dependent_p = false;
3367 if (processing_template_decl)
3368 for (binfo = TYPE_BINFO (current_class_type), i = 0;
3369 BINFO_BASE_ITERATE (binfo, i, base_binfo);
3370 i++)
3371 if (dependent_type_p (TREE_TYPE (base_binfo)))
3373 bases_dependent_p = true;
3374 break;
3377 decl = NULL_TREE;
3379 /* From [namespace.udecl]:
3381 A using-declaration used as a member-declaration shall refer to a
3382 member of a base class of the class being defined.
3384 In general, we cannot check this constraint in a template because
3385 we do not know the entire set of base classes of the current
3386 class type. Morover, if SCOPE is dependent, it might match a
3387 non-dependent base. */
3389 if (!scope_dependent_p)
3391 base_kind b_kind;
3392 binfo = lookup_base (current_class_type, scope, ba_any, &b_kind,
3393 tf_warning_or_error);
3394 if (b_kind < bk_proper_base)
3396 if (!bases_dependent_p)
3398 error_not_base_type (scope, current_class_type);
3399 return NULL_TREE;
3402 else if (!name_dependent_p)
3404 decl = lookup_member (binfo, name, 0, false, tf_warning_or_error);
3405 if (!decl)
3407 error ("no members matching %<%T::%D%> in %q#T", scope, name,
3408 scope);
3409 return NULL_TREE;
3411 /* The binfo from which the functions came does not matter. */
3412 if (BASELINK_P (decl))
3413 decl = BASELINK_FUNCTIONS (decl);
3417 value = build_lang_decl (USING_DECL, name, NULL_TREE);
3418 USING_DECL_DECLS (value) = decl;
3419 USING_DECL_SCOPE (value) = scope;
3420 DECL_DEPENDENT_P (value) = !decl;
3422 return value;
3426 /* Return the binding value for name in scope. */
3429 static tree
3430 namespace_binding_1 (tree name, tree scope)
3432 cxx_binding *binding;
3434 if (SCOPE_FILE_SCOPE_P (scope))
3435 scope = global_namespace;
3436 else
3437 /* Unnecessary for the global namespace because it can't be an alias. */
3438 scope = ORIGINAL_NAMESPACE (scope);
3440 binding = cp_binding_level_find_binding_for_name (NAMESPACE_LEVEL (scope), name);
3442 return binding ? binding->value : NULL_TREE;
3445 tree
3446 namespace_binding (tree name, tree scope)
3448 tree ret;
3449 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3450 ret = namespace_binding_1 (name, scope);
3451 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3452 return ret;
3455 /* Set the binding value for name in scope. */
3457 static void
3458 set_namespace_binding_1 (tree name, tree scope, tree val)
3460 cxx_binding *b;
3462 if (scope == NULL_TREE)
3463 scope = global_namespace;
3464 b = binding_for_name (NAMESPACE_LEVEL (scope), name);
3465 if (!b->value || TREE_CODE (val) == OVERLOAD || val == error_mark_node)
3466 b->value = val;
3467 else
3468 supplement_binding (b, val);
3471 /* Wrapper for set_namespace_binding_1. */
3473 void
3474 set_namespace_binding (tree name, tree scope, tree val)
3476 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3477 set_namespace_binding_1 (name, scope, val);
3478 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3481 /* Set the context of a declaration to scope. Complain if we are not
3482 outside scope. */
3484 void
3485 set_decl_namespace (tree decl, tree scope, bool friendp)
3487 tree old;
3489 /* Get rid of namespace aliases. */
3490 scope = ORIGINAL_NAMESPACE (scope);
3492 /* It is ok for friends to be qualified in parallel space. */
3493 if (!friendp && !is_ancestor (current_namespace, scope))
3494 error ("declaration of %qD not in a namespace surrounding %qD",
3495 decl, scope);
3496 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
3498 /* Writing "int N::i" to declare a variable within "N" is invalid. */
3499 if (scope == current_namespace)
3501 if (at_namespace_scope_p ())
3502 error ("explicit qualification in declaration of %qD",
3503 decl);
3504 return;
3507 /* See whether this has been declared in the namespace. */
3508 old = lookup_qualified_name (scope, DECL_NAME (decl), false, true);
3509 if (old == error_mark_node)
3510 /* No old declaration at all. */
3511 goto complain;
3512 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
3513 if (TREE_CODE (old) == TREE_LIST)
3515 error ("reference to %qD is ambiguous", decl);
3516 print_candidates (old);
3517 return;
3519 if (!is_overloaded_fn (decl))
3521 /* We might have found OLD in an inline namespace inside SCOPE. */
3522 if (TREE_CODE (decl) == TREE_CODE (old))
3523 DECL_CONTEXT (decl) = DECL_CONTEXT (old);
3524 /* Don't compare non-function decls with decls_match here, since
3525 it can't check for the correct constness at this
3526 point. pushdecl will find those errors later. */
3527 return;
3529 /* Since decl is a function, old should contain a function decl. */
3530 if (!is_overloaded_fn (old))
3531 goto complain;
3532 /* A template can be explicitly specialized in any namespace. */
3533 if (processing_explicit_instantiation)
3534 return;
3535 if (processing_template_decl || processing_specialization)
3536 /* We have not yet called push_template_decl to turn a
3537 FUNCTION_DECL into a TEMPLATE_DECL, so the declarations won't
3538 match. But, we'll check later, when we construct the
3539 template. */
3540 return;
3541 /* Instantiations or specializations of templates may be declared as
3542 friends in any namespace. */
3543 if (friendp && DECL_USE_TEMPLATE (decl))
3544 return;
3545 if (is_overloaded_fn (old))
3547 tree found = NULL_TREE;
3548 tree elt = old;
3549 for (; elt; elt = OVL_NEXT (elt))
3551 tree ofn = OVL_CURRENT (elt);
3552 /* Adjust DECL_CONTEXT first so decls_match will return true
3553 if DECL will match a declaration in an inline namespace. */
3554 DECL_CONTEXT (decl) = DECL_CONTEXT (ofn);
3555 if (decls_match (decl, ofn))
3557 if (found && !decls_match (found, ofn))
3559 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
3560 error ("reference to %qD is ambiguous", decl);
3561 print_candidates (old);
3562 return;
3564 found = ofn;
3567 if (found)
3569 if (!is_associated_namespace (scope, CP_DECL_CONTEXT (found)))
3570 goto complain;
3571 DECL_CONTEXT (decl) = DECL_CONTEXT (found);
3572 return;
3575 else
3577 DECL_CONTEXT (decl) = DECL_CONTEXT (old);
3578 if (decls_match (decl, old))
3579 return;
3582 /* It didn't work, go back to the explicit scope. */
3583 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
3584 complain:
3585 error ("%qD should have been declared inside %qD", decl, scope);
3588 /* Return the namespace where the current declaration is declared. */
3590 tree
3591 current_decl_namespace (void)
3593 tree result;
3594 /* If we have been pushed into a different namespace, use it. */
3595 if (!vec_safe_is_empty (decl_namespace_list))
3596 return decl_namespace_list->last ();
3598 if (current_class_type)
3599 result = decl_namespace_context (current_class_type);
3600 else if (current_function_decl)
3601 result = decl_namespace_context (current_function_decl);
3602 else
3603 result = current_namespace;
3604 return result;
3607 /* Process any ATTRIBUTES on a namespace definition. Currently only
3608 attribute visibility is meaningful, which is a property of the syntactic
3609 block rather than the namespace as a whole, so we don't touch the
3610 NAMESPACE_DECL at all. Returns true if attribute visibility is seen. */
3612 bool
3613 handle_namespace_attrs (tree ns, tree attributes)
3615 tree d;
3616 bool saw_vis = false;
3618 for (d = attributes; d; d = TREE_CHAIN (d))
3620 tree name = get_attribute_name (d);
3621 tree args = TREE_VALUE (d);
3623 if (is_attribute_p ("visibility", name))
3625 tree x = args ? TREE_VALUE (args) : NULL_TREE;
3626 if (x == NULL_TREE || TREE_CODE (x) != STRING_CST || TREE_CHAIN (args))
3628 warning (OPT_Wattributes,
3629 "%qD attribute requires a single NTBS argument",
3630 name);
3631 continue;
3634 if (!TREE_PUBLIC (ns))
3635 warning (OPT_Wattributes,
3636 "%qD attribute is meaningless since members of the "
3637 "anonymous namespace get local symbols", name);
3639 push_visibility (TREE_STRING_POINTER (x), 1);
3640 saw_vis = true;
3642 else
3644 warning (OPT_Wattributes, "%qD attribute directive ignored",
3645 name);
3646 continue;
3650 return saw_vis;
3653 /* Push into the scope of the NAME namespace. If NAME is NULL_TREE, then we
3654 select a name that is unique to this compilation unit. */
3656 void
3657 push_namespace (tree name)
3659 tree d = NULL_TREE;
3660 bool need_new = true;
3661 bool implicit_use = false;
3662 bool anon = !name;
3664 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3666 /* We should not get here if the global_namespace is not yet constructed
3667 nor if NAME designates the global namespace: The global scope is
3668 constructed elsewhere. */
3669 gcc_assert (global_namespace != NULL && name != global_scope_name);
3671 if (anon)
3673 name = get_anonymous_namespace_name();
3674 d = IDENTIFIER_NAMESPACE_VALUE (name);
3675 if (d)
3676 /* Reopening anonymous namespace. */
3677 need_new = false;
3678 implicit_use = true;
3680 else
3682 /* Check whether this is an extended namespace definition. */
3683 d = IDENTIFIER_NAMESPACE_VALUE (name);
3684 if (d != NULL_TREE && TREE_CODE (d) == NAMESPACE_DECL)
3686 tree dna = DECL_NAMESPACE_ALIAS (d);
3687 if (dna)
3689 /* We do some error recovery for, eg, the redeclaration
3690 of M here:
3692 namespace N {}
3693 namespace M = N;
3694 namespace M {}
3696 However, in nasty cases like:
3698 namespace N
3700 namespace M = N;
3701 namespace M {}
3704 we just error out below, in duplicate_decls. */
3705 if (NAMESPACE_LEVEL (dna)->level_chain
3706 == current_binding_level)
3708 error ("namespace alias %qD not allowed here, "
3709 "assuming %qD", d, dna);
3710 d = dna;
3711 need_new = false;
3714 else
3715 need_new = false;
3719 if (need_new)
3721 /* Make a new namespace, binding the name to it. */
3722 d = build_lang_decl (NAMESPACE_DECL, name, void_type_node);
3723 DECL_CONTEXT (d) = FROB_CONTEXT (current_namespace);
3724 /* The name of this namespace is not visible to other translation
3725 units if it is an anonymous namespace or member thereof. */
3726 if (anon || decl_anon_ns_mem_p (current_namespace))
3727 TREE_PUBLIC (d) = 0;
3728 else
3729 TREE_PUBLIC (d) = 1;
3730 pushdecl (d);
3731 if (anon)
3733 /* Clear DECL_NAME for the benefit of debugging back ends. */
3734 SET_DECL_ASSEMBLER_NAME (d, name);
3735 DECL_NAME (d) = NULL_TREE;
3737 begin_scope (sk_namespace, d);
3739 else
3740 resume_scope (NAMESPACE_LEVEL (d));
3742 if (implicit_use)
3743 do_using_directive (d);
3744 /* Enter the name space. */
3745 current_namespace = d;
3747 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3750 /* Pop from the scope of the current namespace. */
3752 void
3753 pop_namespace (void)
3755 gcc_assert (current_namespace != global_namespace);
3756 current_namespace = CP_DECL_CONTEXT (current_namespace);
3757 /* The binding level is not popped, as it might be re-opened later. */
3758 leave_scope ();
3761 /* Push into the scope of the namespace NS, even if it is deeply
3762 nested within another namespace. */
3764 void
3765 push_nested_namespace (tree ns)
3767 if (ns == global_namespace)
3768 push_to_top_level ();
3769 else
3771 push_nested_namespace (CP_DECL_CONTEXT (ns));
3772 push_namespace (DECL_NAME (ns));
3776 /* Pop back from the scope of the namespace NS, which was previously
3777 entered with push_nested_namespace. */
3779 void
3780 pop_nested_namespace (tree ns)
3782 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3783 gcc_assert (current_namespace == ns);
3784 while (ns != global_namespace)
3786 pop_namespace ();
3787 ns = CP_DECL_CONTEXT (ns);
3790 pop_from_top_level ();
3791 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3794 /* Temporarily set the namespace for the current declaration. */
3796 void
3797 push_decl_namespace (tree decl)
3799 if (TREE_CODE (decl) != NAMESPACE_DECL)
3800 decl = decl_namespace_context (decl);
3801 vec_safe_push (decl_namespace_list, ORIGINAL_NAMESPACE (decl));
3804 /* [namespace.memdef]/2 */
3806 void
3807 pop_decl_namespace (void)
3809 decl_namespace_list->pop ();
3812 /* Return the namespace that is the common ancestor
3813 of two given namespaces. */
3815 static tree
3816 namespace_ancestor_1 (tree ns1, tree ns2)
3818 tree nsr;
3819 if (is_ancestor (ns1, ns2))
3820 nsr = ns1;
3821 else
3822 nsr = namespace_ancestor_1 (CP_DECL_CONTEXT (ns1), ns2);
3823 return nsr;
3826 /* Wrapper for namespace_ancestor_1. */
3828 static tree
3829 namespace_ancestor (tree ns1, tree ns2)
3831 tree nsr;
3832 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3833 nsr = namespace_ancestor_1 (ns1, ns2);
3834 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3835 return nsr;
3838 /* Process a namespace-alias declaration. */
3840 void
3841 do_namespace_alias (tree alias, tree name_space)
3843 if (name_space == error_mark_node)
3844 return;
3846 gcc_assert (TREE_CODE (name_space) == NAMESPACE_DECL);
3848 name_space = ORIGINAL_NAMESPACE (name_space);
3850 /* Build the alias. */
3851 alias = build_lang_decl (NAMESPACE_DECL, alias, void_type_node);
3852 DECL_NAMESPACE_ALIAS (alias) = name_space;
3853 DECL_EXTERNAL (alias) = 1;
3854 DECL_CONTEXT (alias) = FROB_CONTEXT (current_scope ());
3855 pushdecl (alias);
3857 /* Emit debug info for namespace alias. */
3858 if (!building_stmt_list_p ())
3859 (*debug_hooks->global_decl) (alias);
3862 /* Like pushdecl, only it places X in the current namespace,
3863 if appropriate. */
3865 tree
3866 pushdecl_namespace_level (tree x, bool is_friend)
3868 cp_binding_level *b = current_binding_level;
3869 tree t;
3871 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3872 t = pushdecl_with_scope (x, NAMESPACE_LEVEL (current_namespace), is_friend);
3874 /* Now, the type_shadowed stack may screw us. Munge it so it does
3875 what we want. */
3876 if (TREE_CODE (t) == TYPE_DECL)
3878 tree name = DECL_NAME (t);
3879 tree newval;
3880 tree *ptr = (tree *)0;
3881 for (; !global_scope_p (b); b = b->level_chain)
3883 tree shadowed = b->type_shadowed;
3884 for (; shadowed; shadowed = TREE_CHAIN (shadowed))
3885 if (TREE_PURPOSE (shadowed) == name)
3887 ptr = &TREE_VALUE (shadowed);
3888 /* Can't break out of the loop here because sometimes
3889 a binding level will have duplicate bindings for
3890 PT names. It's gross, but I haven't time to fix it. */
3893 newval = TREE_TYPE (t);
3894 if (ptr == (tree *)0)
3896 /* @@ This shouldn't be needed. My test case "zstring.cc" trips
3897 up here if this is changed to an assertion. --KR */
3898 SET_IDENTIFIER_TYPE_VALUE (name, t);
3900 else
3902 *ptr = newval;
3905 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3906 return t;
3909 /* Insert USED into the using list of USER. Set INDIRECT_flag if this
3910 directive is not directly from the source. Also find the common
3911 ancestor and let our users know about the new namespace */
3913 static void
3914 add_using_namespace_1 (tree user, tree used, bool indirect)
3916 tree t;
3917 /* Using oneself is a no-op. */
3918 if (user == used)
3919 return;
3920 gcc_assert (TREE_CODE (user) == NAMESPACE_DECL);
3921 gcc_assert (TREE_CODE (used) == NAMESPACE_DECL);
3922 /* Check if we already have this. */
3923 t = purpose_member (used, DECL_NAMESPACE_USING (user));
3924 if (t != NULL_TREE)
3926 if (!indirect)
3927 /* Promote to direct usage. */
3928 TREE_INDIRECT_USING (t) = 0;
3929 return;
3932 /* Add used to the user's using list. */
3933 DECL_NAMESPACE_USING (user)
3934 = tree_cons (used, namespace_ancestor (user, used),
3935 DECL_NAMESPACE_USING (user));
3937 TREE_INDIRECT_USING (DECL_NAMESPACE_USING (user)) = indirect;
3939 /* Add user to the used's users list. */
3940 DECL_NAMESPACE_USERS (used)
3941 = tree_cons (user, 0, DECL_NAMESPACE_USERS (used));
3943 /* Recursively add all namespaces used. */
3944 for (t = DECL_NAMESPACE_USING (used); t; t = TREE_CHAIN (t))
3945 /* indirect usage */
3946 add_using_namespace_1 (user, TREE_PURPOSE (t), 1);
3948 /* Tell everyone using us about the new used namespaces. */
3949 for (t = DECL_NAMESPACE_USERS (user); t; t = TREE_CHAIN (t))
3950 add_using_namespace_1 (TREE_PURPOSE (t), used, 1);
3953 /* Wrapper for add_using_namespace_1. */
3955 static void
3956 add_using_namespace (tree user, tree used, bool indirect)
3958 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3959 add_using_namespace_1 (user, used, indirect);
3960 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3963 /* Process a using-declaration not appearing in class or local scope. */
3965 void
3966 do_toplevel_using_decl (tree decl, tree scope, tree name)
3968 tree oldval, oldtype, newval, newtype;
3969 tree orig_decl = decl;
3970 cxx_binding *binding;
3972 decl = validate_nonmember_using_decl (decl, scope, name);
3973 if (decl == NULL_TREE)
3974 return;
3976 binding = binding_for_name (NAMESPACE_LEVEL (current_namespace), name);
3978 oldval = binding->value;
3979 oldtype = binding->type;
3981 do_nonmember_using_decl (scope, name, oldval, oldtype, &newval, &newtype);
3983 /* Emit debug info. */
3984 if (!processing_template_decl)
3985 cp_emit_debug_info_for_using (orig_decl, current_namespace);
3987 /* Copy declarations found. */
3988 if (newval)
3989 binding->value = newval;
3990 if (newtype)
3991 binding->type = newtype;
3994 /* Process a using-directive. */
3996 void
3997 do_using_directive (tree name_space)
3999 tree context = NULL_TREE;
4001 if (name_space == error_mark_node)
4002 return;
4004 gcc_assert (TREE_CODE (name_space) == NAMESPACE_DECL);
4006 if (building_stmt_list_p ())
4007 add_stmt (build_stmt (input_location, USING_STMT, name_space));
4008 name_space = ORIGINAL_NAMESPACE (name_space);
4010 if (!toplevel_bindings_p ())
4012 push_using_directive (name_space);
4014 else
4016 /* direct usage */
4017 add_using_namespace (current_namespace, name_space, 0);
4018 if (current_namespace != global_namespace)
4019 context = current_namespace;
4021 /* Emit debugging info. */
4022 if (!processing_template_decl)
4023 (*debug_hooks->imported_module_or_decl) (name_space, NULL_TREE,
4024 context, false);
4028 /* Deal with a using-directive seen by the parser. Currently we only
4029 handle attributes here, since they cannot appear inside a template. */
4031 void
4032 parse_using_directive (tree name_space, tree attribs)
4034 do_using_directive (name_space);
4036 if (attribs == error_mark_node)
4037 return;
4039 for (tree a = attribs; a; a = TREE_CHAIN (a))
4041 tree name = get_attribute_name (a);
4042 if (is_attribute_p ("strong", name))
4044 if (!toplevel_bindings_p ())
4045 error ("strong using only meaningful at namespace scope");
4046 else if (name_space != error_mark_node)
4048 if (!is_ancestor (current_namespace, name_space))
4049 error ("current namespace %qD does not enclose strongly used namespace %qD",
4050 current_namespace, name_space);
4051 DECL_NAMESPACE_ASSOCIATIONS (name_space)
4052 = tree_cons (current_namespace, 0,
4053 DECL_NAMESPACE_ASSOCIATIONS (name_space));
4056 else
4057 warning (OPT_Wattributes, "%qD attribute directive ignored", name);
4061 /* Like pushdecl, only it places X in the global scope if appropriate.
4062 Calls cp_finish_decl to register the variable, initializing it with
4063 *INIT, if INIT is non-NULL. */
4065 static tree
4066 pushdecl_top_level_1 (tree x, tree *init, bool is_friend)
4068 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4069 push_to_top_level ();
4070 x = pushdecl_namespace_level (x, is_friend);
4071 if (init)
4072 cp_finish_decl (x, *init, false, NULL_TREE, 0);
4073 pop_from_top_level ();
4074 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4075 return x;
4078 /* Like pushdecl, only it places X in the global scope if appropriate. */
4080 tree
4081 pushdecl_top_level (tree x)
4083 return pushdecl_top_level_1 (x, NULL, false);
4086 /* Like pushdecl_top_level, but adding the IS_FRIEND parameter. */
4088 tree
4089 pushdecl_top_level_maybe_friend (tree x, bool is_friend)
4091 return pushdecl_top_level_1 (x, NULL, is_friend);
4094 /* Like pushdecl, only it places X in the global scope if
4095 appropriate. Calls cp_finish_decl to register the variable,
4096 initializing it with INIT. */
4098 tree
4099 pushdecl_top_level_and_finish (tree x, tree init)
4101 return pushdecl_top_level_1 (x, &init, false);
4104 /* Combines two sets of overloaded functions into an OVERLOAD chain, removing
4105 duplicates. The first list becomes the tail of the result.
4107 The algorithm is O(n^2). We could get this down to O(n log n) by
4108 doing a sort on the addresses of the functions, if that becomes
4109 necessary. */
4111 static tree
4112 merge_functions (tree s1, tree s2)
4114 for (; s2; s2 = OVL_NEXT (s2))
4116 tree fn2 = OVL_CURRENT (s2);
4117 tree fns1;
4119 for (fns1 = s1; fns1; fns1 = OVL_NEXT (fns1))
4121 tree fn1 = OVL_CURRENT (fns1);
4123 /* If the function from S2 is already in S1, there is no
4124 need to add it again. For `extern "C"' functions, we
4125 might have two FUNCTION_DECLs for the same function, in
4126 different namespaces, but let's leave them in in case
4127 they have different default arguments. */
4128 if (fn1 == fn2)
4129 break;
4132 /* If we exhausted all of the functions in S1, FN2 is new. */
4133 if (!fns1)
4134 s1 = build_overload (fn2, s1);
4136 return s1;
4139 /* Returns TRUE iff OLD and NEW are the same entity.
4141 3 [basic]/3: An entity is a value, object, reference, function,
4142 enumerator, type, class member, template, template specialization,
4143 namespace, parameter pack, or this.
4145 7.3.4 [namespace.udir]/4: If name lookup finds a declaration for a name
4146 in two different namespaces, and the declarations do not declare the
4147 same entity and do not declare functions, the use of the name is
4148 ill-formed. */
4150 static bool
4151 same_entity_p (tree one, tree two)
4153 if (one == two)
4154 return true;
4155 if (!one || !two)
4156 return false;
4157 if (TREE_CODE (one) == TYPE_DECL
4158 && TREE_CODE (two) == TYPE_DECL
4159 && same_type_p (TREE_TYPE (one), TREE_TYPE (two)))
4160 return true;
4161 return false;
4164 /* This should return an error not all definitions define functions.
4165 It is not an error if we find two functions with exactly the
4166 same signature, only if these are selected in overload resolution.
4167 old is the current set of bindings, new_binding the freshly-found binding.
4168 XXX Do we want to give *all* candidates in case of ambiguity?
4169 XXX In what way should I treat extern declarations?
4170 XXX I don't want to repeat the entire duplicate_decls here */
4172 static void
4173 ambiguous_decl (struct scope_binding *old, cxx_binding *new_binding, int flags)
4175 tree val, type;
4176 gcc_assert (old != NULL);
4178 /* Copy the type. */
4179 type = new_binding->type;
4180 if (LOOKUP_NAMESPACES_ONLY (flags)
4181 || (type && hidden_name_p (type) && !(flags & LOOKUP_HIDDEN)))
4182 type = NULL_TREE;
4184 /* Copy the value. */
4185 val = new_binding->value;
4186 if (val)
4188 if (hidden_name_p (val) && !(flags & LOOKUP_HIDDEN))
4189 val = NULL_TREE;
4190 else
4191 switch (TREE_CODE (val))
4193 case TEMPLATE_DECL:
4194 /* If we expect types or namespaces, and not templates,
4195 or this is not a template class. */
4196 if ((LOOKUP_QUALIFIERS_ONLY (flags)
4197 && !DECL_TYPE_TEMPLATE_P (val)))
4198 val = NULL_TREE;
4199 break;
4200 case TYPE_DECL:
4201 if (LOOKUP_NAMESPACES_ONLY (flags)
4202 || (type && (flags & LOOKUP_PREFER_TYPES)))
4203 val = NULL_TREE;
4204 break;
4205 case NAMESPACE_DECL:
4206 if (LOOKUP_TYPES_ONLY (flags))
4207 val = NULL_TREE;
4208 break;
4209 case FUNCTION_DECL:
4210 /* Ignore built-in functions that are still anticipated. */
4211 if (LOOKUP_QUALIFIERS_ONLY (flags))
4212 val = NULL_TREE;
4213 break;
4214 default:
4215 if (LOOKUP_QUALIFIERS_ONLY (flags))
4216 val = NULL_TREE;
4220 /* If val is hidden, shift down any class or enumeration name. */
4221 if (!val)
4223 val = type;
4224 type = NULL_TREE;
4227 if (!old->value)
4228 old->value = val;
4229 else if (val && !same_entity_p (val, old->value))
4231 if (is_overloaded_fn (old->value) && is_overloaded_fn (val))
4232 old->value = merge_functions (old->value, val);
4233 else
4235 old->value = tree_cons (NULL_TREE, old->value,
4236 build_tree_list (NULL_TREE, val));
4237 TREE_TYPE (old->value) = error_mark_node;
4241 if (!old->type)
4242 old->type = type;
4243 else if (type && old->type != type)
4245 old->type = tree_cons (NULL_TREE, old->type,
4246 build_tree_list (NULL_TREE, type));
4247 TREE_TYPE (old->type) = error_mark_node;
4251 /* Return the declarations that are members of the namespace NS. */
4253 tree
4254 cp_namespace_decls (tree ns)
4256 return NAMESPACE_LEVEL (ns)->names;
4259 /* Combine prefer_type and namespaces_only into flags. */
4261 static int
4262 lookup_flags (int prefer_type, int namespaces_only)
4264 if (namespaces_only)
4265 return LOOKUP_PREFER_NAMESPACES;
4266 if (prefer_type > 1)
4267 return LOOKUP_PREFER_TYPES;
4268 if (prefer_type > 0)
4269 return LOOKUP_PREFER_BOTH;
4270 return 0;
4273 /* Given a lookup that returned VAL, use FLAGS to decide if we want to
4274 ignore it or not. Subroutine of lookup_name_real and
4275 lookup_type_scope. */
4277 static bool
4278 qualify_lookup (tree val, int flags)
4280 if (val == NULL_TREE)
4281 return false;
4282 if ((flags & LOOKUP_PREFER_NAMESPACES) && TREE_CODE (val) == NAMESPACE_DECL)
4283 return true;
4284 if (flags & LOOKUP_PREFER_TYPES)
4286 tree target_val = strip_using_decl (val);
4287 if (TREE_CODE (target_val) == TYPE_DECL
4288 || TREE_CODE (target_val) == TEMPLATE_DECL)
4289 return true;
4291 if (flags & (LOOKUP_PREFER_NAMESPACES | LOOKUP_PREFER_TYPES))
4292 return false;
4293 /* Look through lambda things that we shouldn't be able to see. */
4294 if (is_lambda_ignored_entity (val))
4295 return false;
4296 return true;
4299 /* Given a lookup that returned VAL, decide if we want to ignore it or
4300 not based on DECL_ANTICIPATED. */
4302 bool
4303 hidden_name_p (tree val)
4305 if (DECL_P (val)
4306 && DECL_LANG_SPECIFIC (val)
4307 && TYPE_FUNCTION_OR_TEMPLATE_DECL_P (val)
4308 && DECL_ANTICIPATED (val))
4309 return true;
4310 return false;
4313 /* Remove any hidden friend functions from a possibly overloaded set
4314 of functions. */
4316 tree
4317 remove_hidden_names (tree fns)
4319 if (!fns)
4320 return fns;
4322 if (TREE_CODE (fns) == FUNCTION_DECL && hidden_name_p (fns))
4323 fns = NULL_TREE;
4324 else if (TREE_CODE (fns) == OVERLOAD)
4326 tree o;
4328 for (o = fns; o; o = OVL_NEXT (o))
4329 if (hidden_name_p (OVL_CURRENT (o)))
4330 break;
4331 if (o)
4333 tree n = NULL_TREE;
4335 for (o = fns; o; o = OVL_NEXT (o))
4336 if (!hidden_name_p (OVL_CURRENT (o)))
4337 n = build_overload (OVL_CURRENT (o), n);
4338 fns = n;
4342 return fns;
4345 /* Suggest alternatives for NAME, an IDENTIFIER_NODE for which name
4346 lookup failed. Search through all available namespaces and print out
4347 possible candidates. */
4349 void
4350 suggest_alternatives_for (location_t location, tree name)
4352 vec<tree> candidates = vNULL;
4353 vec<tree> namespaces_to_search = vNULL;
4354 int max_to_search = PARAM_VALUE (CXX_MAX_NAMESPACES_FOR_DIAGNOSTIC_HELP);
4355 int n_searched = 0;
4356 tree t;
4357 unsigned ix;
4359 namespaces_to_search.safe_push (global_namespace);
4361 while (!namespaces_to_search.is_empty ()
4362 && n_searched < max_to_search)
4364 tree scope = namespaces_to_search.pop ();
4365 struct scope_binding binding = EMPTY_SCOPE_BINDING;
4366 cp_binding_level *level = NAMESPACE_LEVEL (scope);
4368 /* Look in this namespace. */
4369 qualified_lookup_using_namespace (name, scope, &binding, 0);
4371 n_searched++;
4373 if (binding.value)
4374 candidates.safe_push (binding.value);
4376 /* Add child namespaces. */
4377 for (t = level->namespaces; t; t = DECL_CHAIN (t))
4378 namespaces_to_search.safe_push (t);
4381 /* If we stopped before we could examine all namespaces, inform the
4382 user. Do this even if we don't have any candidates, since there
4383 might be more candidates further down that we weren't able to
4384 find. */
4385 if (n_searched >= max_to_search
4386 && !namespaces_to_search.is_empty ())
4387 inform (location,
4388 "maximum limit of %d namespaces searched for %qE",
4389 max_to_search, name);
4391 namespaces_to_search.release ();
4393 /* Nothing useful to report. */
4394 if (candidates.is_empty ())
4395 return;
4397 inform_n (location, candidates.length (),
4398 "suggested alternative:",
4399 "suggested alternatives:");
4401 FOR_EACH_VEC_ELT (candidates, ix, t)
4402 inform (location_of (t), " %qE", t);
4404 candidates.release ();
4407 /* Unscoped lookup of a global: iterate over current namespaces,
4408 considering using-directives. */
4410 static tree
4411 unqualified_namespace_lookup_1 (tree name, int flags)
4413 tree initial = current_decl_namespace ();
4414 tree scope = initial;
4415 tree siter;
4416 cp_binding_level *level;
4417 tree val = NULL_TREE;
4419 for (; !val; scope = CP_DECL_CONTEXT (scope))
4421 struct scope_binding binding = EMPTY_SCOPE_BINDING;
4422 cxx_binding *b =
4423 cp_binding_level_find_binding_for_name (NAMESPACE_LEVEL (scope), name);
4425 if (b)
4426 ambiguous_decl (&binding, b, flags);
4428 /* Add all _DECLs seen through local using-directives. */
4429 for (level = current_binding_level;
4430 level->kind != sk_namespace;
4431 level = level->level_chain)
4432 if (!lookup_using_namespace (name, &binding, level->using_directives,
4433 scope, flags))
4434 /* Give up because of error. */
4435 return error_mark_node;
4437 /* Add all _DECLs seen through global using-directives. */
4438 /* XXX local and global using lists should work equally. */
4439 siter = initial;
4440 while (1)
4442 if (!lookup_using_namespace (name, &binding,
4443 DECL_NAMESPACE_USING (siter),
4444 scope, flags))
4445 /* Give up because of error. */
4446 return error_mark_node;
4447 if (siter == scope) break;
4448 siter = CP_DECL_CONTEXT (siter);
4451 val = binding.value;
4452 if (scope == global_namespace)
4453 break;
4455 return val;
4458 /* Wrapper for unqualified_namespace_lookup_1. */
4460 static tree
4461 unqualified_namespace_lookup (tree name, int flags)
4463 tree ret;
4464 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4465 ret = unqualified_namespace_lookup_1 (name, flags);
4466 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4467 return ret;
4470 /* Look up NAME (an IDENTIFIER_NODE) in SCOPE (either a NAMESPACE_DECL
4471 or a class TYPE). If IS_TYPE_P is TRUE, then ignore non-type
4472 bindings.
4474 Returns a DECL (or OVERLOAD, or BASELINK) representing the
4475 declaration found. If no suitable declaration can be found,
4476 ERROR_MARK_NODE is returned. If COMPLAIN is true and SCOPE is
4477 neither a class-type nor a namespace a diagnostic is issued. */
4479 tree
4480 lookup_qualified_name (tree scope, tree name, bool is_type_p, bool complain)
4482 int flags = 0;
4483 tree t = NULL_TREE;
4485 if (TREE_CODE (scope) == NAMESPACE_DECL)
4487 struct scope_binding binding = EMPTY_SCOPE_BINDING;
4489 if (is_type_p)
4490 flags |= LOOKUP_PREFER_TYPES;
4491 if (qualified_lookup_using_namespace (name, scope, &binding, flags))
4492 t = binding.value;
4494 else if (cxx_dialect != cxx98 && TREE_CODE (scope) == ENUMERAL_TYPE)
4495 t = lookup_enumerator (scope, name);
4496 else if (is_class_type (scope, complain))
4497 t = lookup_member (scope, name, 2, is_type_p, tf_warning_or_error);
4499 if (!t)
4500 return error_mark_node;
4501 return t;
4504 /* Subroutine of unqualified_namespace_lookup:
4505 Add the bindings of NAME in used namespaces to VAL.
4506 We are currently looking for names in namespace SCOPE, so we
4507 look through USINGS for using-directives of namespaces
4508 which have SCOPE as a common ancestor with the current scope.
4509 Returns false on errors. */
4511 static bool
4512 lookup_using_namespace (tree name, struct scope_binding *val,
4513 tree usings, tree scope, int flags)
4515 tree iter;
4516 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4517 /* Iterate over all used namespaces in current, searching for using
4518 directives of scope. */
4519 for (iter = usings; iter; iter = TREE_CHAIN (iter))
4520 if (TREE_VALUE (iter) == scope)
4522 tree used = ORIGINAL_NAMESPACE (TREE_PURPOSE (iter));
4523 cxx_binding *val1 =
4524 cp_binding_level_find_binding_for_name (NAMESPACE_LEVEL (used), name);
4525 /* Resolve ambiguities. */
4526 if (val1)
4527 ambiguous_decl (val, val1, flags);
4529 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4530 return val->value != error_mark_node;
4533 /* Returns true iff VEC contains TARGET. */
4535 static bool
4536 tree_vec_contains (vec<tree, va_gc> *vec, tree target)
4538 unsigned int i;
4539 tree elt;
4540 FOR_EACH_VEC_SAFE_ELT (vec,i,elt)
4541 if (elt == target)
4542 return true;
4543 return false;
4546 /* [namespace.qual]
4547 Accepts the NAME to lookup and its qualifying SCOPE.
4548 Returns the name/type pair found into the cxx_binding *RESULT,
4549 or false on error. */
4551 static bool
4552 qualified_lookup_using_namespace (tree name, tree scope,
4553 struct scope_binding *result, int flags)
4555 /* Maintain a list of namespaces visited... */
4556 vec<tree, va_gc> *seen = NULL;
4557 vec<tree, va_gc> *seen_inline = NULL;
4558 /* ... and a list of namespace yet to see. */
4559 vec<tree, va_gc> *todo = NULL;
4560 vec<tree, va_gc> *todo_maybe = NULL;
4561 vec<tree, va_gc> *todo_inline = NULL;
4562 tree usings;
4563 timevar_start (TV_NAME_LOOKUP);
4564 /* Look through namespace aliases. */
4565 scope = ORIGINAL_NAMESPACE (scope);
4567 /* Algorithm: Starting with SCOPE, walk through the set of used
4568 namespaces. For each used namespace, look through its inline
4569 namespace set for any bindings and usings. If no bindings are
4570 found, add any usings seen to the set of used namespaces. */
4571 vec_safe_push (todo, scope);
4573 while (todo->length ())
4575 bool found_here;
4576 scope = todo->pop ();
4577 if (tree_vec_contains (seen, scope))
4578 continue;
4579 vec_safe_push (seen, scope);
4580 vec_safe_push (todo_inline, scope);
4582 found_here = false;
4583 while (todo_inline->length ())
4585 cxx_binding *binding;
4587 scope = todo_inline->pop ();
4588 if (tree_vec_contains (seen_inline, scope))
4589 continue;
4590 vec_safe_push (seen_inline, scope);
4592 binding =
4593 cp_binding_level_find_binding_for_name (NAMESPACE_LEVEL (scope), name);
4594 if (binding)
4596 found_here = true;
4597 ambiguous_decl (result, binding, flags);
4600 for (usings = DECL_NAMESPACE_USING (scope); usings;
4601 usings = TREE_CHAIN (usings))
4602 if (!TREE_INDIRECT_USING (usings))
4604 if (is_associated_namespace (scope, TREE_PURPOSE (usings)))
4605 vec_safe_push (todo_inline, TREE_PURPOSE (usings));
4606 else
4607 vec_safe_push (todo_maybe, TREE_PURPOSE (usings));
4611 if (found_here)
4612 vec_safe_truncate (todo_maybe, 0);
4613 else
4614 while (vec_safe_length (todo_maybe))
4615 vec_safe_push (todo, todo_maybe->pop ());
4617 vec_free (todo);
4618 vec_free (todo_maybe);
4619 vec_free (todo_inline);
4620 vec_free (seen);
4621 vec_free (seen_inline);
4622 timevar_stop (TV_NAME_LOOKUP);
4623 return result->value != error_mark_node;
4626 /* Subroutine of outer_binding.
4628 Returns TRUE if BINDING is a binding to a template parameter of
4629 SCOPE. In that case SCOPE is the scope of a primary template
4630 parameter -- in the sense of G++, i.e, a template that has its own
4631 template header.
4633 Returns FALSE otherwise. */
4635 static bool
4636 binding_to_template_parms_of_scope_p (cxx_binding *binding,
4637 cp_binding_level *scope)
4639 tree binding_value, tmpl, tinfo;
4640 int level;
4642 if (!binding || !scope || !scope->this_entity)
4643 return false;
4645 binding_value = binding->value ? binding->value : binding->type;
4646 tinfo = get_template_info (scope->this_entity);
4648 /* BINDING_VALUE must be a template parm. */
4649 if (binding_value == NULL_TREE
4650 || (!DECL_P (binding_value)
4651 || !DECL_TEMPLATE_PARM_P (binding_value)))
4652 return false;
4654 /* The level of BINDING_VALUE. */
4655 level =
4656 template_type_parameter_p (binding_value)
4657 ? TEMPLATE_PARM_LEVEL (TEMPLATE_TYPE_PARM_INDEX
4658 (TREE_TYPE (binding_value)))
4659 : TEMPLATE_PARM_LEVEL (DECL_INITIAL (binding_value));
4661 /* The template of the current scope, iff said scope is a primary
4662 template. */
4663 tmpl = (tinfo
4664 && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
4665 ? TI_TEMPLATE (tinfo)
4666 : NULL_TREE);
4668 /* If the level of the parm BINDING_VALUE equals the depth of TMPL,
4669 then BINDING_VALUE is a parameter of TMPL. */
4670 return (tmpl && level == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)));
4673 /* Return the innermost non-namespace binding for NAME from a scope
4674 containing BINDING, or, if BINDING is NULL, the current scope.
4675 Please note that for a given template, the template parameters are
4676 considered to be in the scope containing the current scope.
4677 If CLASS_P is false, then class bindings are ignored. */
4679 cxx_binding *
4680 outer_binding (tree name,
4681 cxx_binding *binding,
4682 bool class_p)
4684 cxx_binding *outer;
4685 cp_binding_level *scope;
4686 cp_binding_level *outer_scope;
4688 if (binding)
4690 scope = binding->scope->level_chain;
4691 outer = binding->previous;
4693 else
4695 scope = current_binding_level;
4696 outer = IDENTIFIER_BINDING (name);
4698 outer_scope = outer ? outer->scope : NULL;
4700 /* Because we create class bindings lazily, we might be missing a
4701 class binding for NAME. If there are any class binding levels
4702 between the LAST_BINDING_LEVEL and the scope in which OUTER was
4703 declared, we must lookup NAME in those class scopes. */
4704 if (class_p)
4705 while (scope && scope != outer_scope && scope->kind != sk_namespace)
4707 if (scope->kind == sk_class)
4709 cxx_binding *class_binding;
4711 class_binding = get_class_binding (name, scope);
4712 if (class_binding)
4714 /* Thread this new class-scope binding onto the
4715 IDENTIFIER_BINDING list so that future lookups
4716 find it quickly. */
4717 class_binding->previous = outer;
4718 if (binding)
4719 binding->previous = class_binding;
4720 else
4721 IDENTIFIER_BINDING (name) = class_binding;
4722 return class_binding;
4725 /* If we are in a member template, the template parms of the member
4726 template are considered to be inside the scope of the containing
4727 class, but within G++ the class bindings are all pushed between the
4728 template parms and the function body. So if the outer binding is
4729 a template parm for the current scope, return it now rather than
4730 look for a class binding. */
4731 if (outer_scope && outer_scope->kind == sk_template_parms
4732 && binding_to_template_parms_of_scope_p (outer, scope))
4733 return outer;
4735 scope = scope->level_chain;
4738 return outer;
4741 /* Return the innermost block-scope or class-scope value binding for
4742 NAME, or NULL_TREE if there is no such binding. */
4744 tree
4745 innermost_non_namespace_value (tree name)
4747 cxx_binding *binding;
4748 binding = outer_binding (name, /*binding=*/NULL, /*class_p=*/true);
4749 return binding ? binding->value : NULL_TREE;
4752 /* Look up NAME in the current binding level and its superiors in the
4753 namespace of variables, functions and typedefs. Return a ..._DECL
4754 node of some kind representing its definition if there is only one
4755 such declaration, or return a TREE_LIST with all the overloaded
4756 definitions if there are many, or return 0 if it is undefined.
4757 Hidden name, either friend declaration or built-in function, are
4758 not ignored.
4760 If PREFER_TYPE is > 0, we prefer TYPE_DECLs or namespaces.
4761 If PREFER_TYPE is > 1, we reject non-type decls (e.g. namespaces).
4762 Otherwise we prefer non-TYPE_DECLs.
4764 If NONCLASS is nonzero, bindings in class scopes are ignored. If
4765 BLOCK_P is false, bindings in block scopes are ignored. */
4767 static tree
4768 lookup_name_real_1 (tree name, int prefer_type, int nonclass, bool block_p,
4769 int namespaces_only, int flags)
4771 cxx_binding *iter;
4772 tree val = NULL_TREE;
4774 /* Conversion operators are handled specially because ordinary
4775 unqualified name lookup will not find template conversion
4776 operators. */
4777 if (IDENTIFIER_TYPENAME_P (name))
4779 cp_binding_level *level;
4781 for (level = current_binding_level;
4782 level && level->kind != sk_namespace;
4783 level = level->level_chain)
4785 tree class_type;
4786 tree operators;
4788 /* A conversion operator can only be declared in a class
4789 scope. */
4790 if (level->kind != sk_class)
4791 continue;
4793 /* Lookup the conversion operator in the class. */
4794 class_type = level->this_entity;
4795 operators = lookup_fnfields (class_type, name, /*protect=*/0);
4796 if (operators)
4797 return operators;
4800 return NULL_TREE;
4803 flags |= lookup_flags (prefer_type, namespaces_only);
4805 /* First, look in non-namespace scopes. */
4807 if (current_class_type == NULL_TREE)
4808 nonclass = 1;
4810 if (block_p || !nonclass)
4811 for (iter = outer_binding (name, NULL, !nonclass);
4812 iter;
4813 iter = outer_binding (name, iter, !nonclass))
4815 tree binding;
4817 /* Skip entities we don't want. */
4818 if (LOCAL_BINDING_P (iter) ? !block_p : nonclass)
4819 continue;
4821 /* If this is the kind of thing we're looking for, we're done. */
4822 if (qualify_lookup (iter->value, flags))
4823 binding = iter->value;
4824 else if ((flags & LOOKUP_PREFER_TYPES)
4825 && qualify_lookup (iter->type, flags))
4826 binding = iter->type;
4827 else
4828 binding = NULL_TREE;
4830 if (binding)
4832 if (hidden_name_p (binding))
4834 /* A non namespace-scope binding can only be hidden in the
4835 presence of a local class, due to friend declarations.
4837 In particular, consider:
4839 struct C;
4840 void f() {
4841 struct A {
4842 friend struct B;
4843 friend struct C;
4844 void g() {
4845 B* b; // error: B is hidden
4846 C* c; // OK, finds ::C
4849 B *b; // error: B is hidden
4850 C *c; // OK, finds ::C
4851 struct B {};
4852 B *bb; // OK
4855 The standard says that "B" is a local class in "f"
4856 (but not nested within "A") -- but that name lookup
4857 for "B" does not find this declaration until it is
4858 declared directly with "f".
4860 In particular:
4862 [class.friend]
4864 If a friend declaration appears in a local class and
4865 the name specified is an unqualified name, a prior
4866 declaration is looked up without considering scopes
4867 that are outside the innermost enclosing non-class
4868 scope. For a friend function declaration, if there is
4869 no prior declaration, the program is ill-formed. For a
4870 friend class declaration, if there is no prior
4871 declaration, the class that is specified belongs to the
4872 innermost enclosing non-class scope, but if it is
4873 subsequently referenced, its name is not found by name
4874 lookup until a matching declaration is provided in the
4875 innermost enclosing nonclass scope.
4877 So just keep looking for a non-hidden binding.
4879 gcc_assert (TREE_CODE (binding) == TYPE_DECL);
4880 continue;
4882 val = binding;
4883 break;
4887 /* Now lookup in namespace scopes. */
4888 if (!val)
4889 val = unqualified_namespace_lookup (name, flags);
4891 /* If we have a single function from a using decl, pull it out. */
4892 if (val && TREE_CODE (val) == OVERLOAD && !really_overloaded_fn (val))
4893 val = OVL_FUNCTION (val);
4895 return val;
4898 /* Wrapper for lookup_name_real_1. */
4900 tree
4901 lookup_name_real (tree name, int prefer_type, int nonclass, bool block_p,
4902 int namespaces_only, int flags)
4904 tree ret;
4905 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4906 ret = lookup_name_real_1 (name, prefer_type, nonclass, block_p,
4907 namespaces_only, flags);
4908 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4909 return ret;
4912 tree
4913 lookup_name_nonclass (tree name)
4915 return lookup_name_real (name, 0, 1, /*block_p=*/true, 0, 0);
4918 tree
4919 lookup_function_nonclass (tree name, vec<tree, va_gc> *args, bool block_p)
4921 return
4922 lookup_arg_dependent (name,
4923 lookup_name_real (name, 0, 1, block_p, 0, 0),
4924 args);
4927 tree
4928 lookup_name (tree name)
4930 return lookup_name_real (name, 0, 0, /*block_p=*/true, 0, 0);
4933 tree
4934 lookup_name_prefer_type (tree name, int prefer_type)
4936 return lookup_name_real (name, prefer_type, 0, /*block_p=*/true, 0, 0);
4939 /* Look up NAME for type used in elaborated name specifier in
4940 the scopes given by SCOPE. SCOPE can be either TS_CURRENT or
4941 TS_WITHIN_ENCLOSING_NON_CLASS. Although not implied by the
4942 name, more scopes are checked if cleanup or template parameter
4943 scope is encountered.
4945 Unlike lookup_name_real, we make sure that NAME is actually
4946 declared in the desired scope, not from inheritance, nor using
4947 directive. For using declaration, there is DR138 still waiting
4948 to be resolved. Hidden name coming from an earlier friend
4949 declaration is also returned.
4951 A TYPE_DECL best matching the NAME is returned. Catching error
4952 and issuing diagnostics are caller's responsibility. */
4954 static tree
4955 lookup_type_scope_1 (tree name, tag_scope scope)
4957 cxx_binding *iter = NULL;
4958 tree val = NULL_TREE;
4960 /* Look in non-namespace scope first. */
4961 if (current_binding_level->kind != sk_namespace)
4962 iter = outer_binding (name, NULL, /*class_p=*/ true);
4963 for (; iter; iter = outer_binding (name, iter, /*class_p=*/ true))
4965 /* Check if this is the kind of thing we're looking for.
4966 If SCOPE is TS_CURRENT, also make sure it doesn't come from
4967 base class. For ITER->VALUE, we can simply use
4968 INHERITED_VALUE_BINDING_P. For ITER->TYPE, we have to use
4969 our own check.
4971 We check ITER->TYPE before ITER->VALUE in order to handle
4972 typedef struct C {} C;
4973 correctly. */
4975 if (qualify_lookup (iter->type, LOOKUP_PREFER_TYPES)
4976 && (scope != ts_current
4977 || LOCAL_BINDING_P (iter)
4978 || DECL_CONTEXT (iter->type) == iter->scope->this_entity))
4979 val = iter->type;
4980 else if ((scope != ts_current
4981 || !INHERITED_VALUE_BINDING_P (iter))
4982 && qualify_lookup (iter->value, LOOKUP_PREFER_TYPES))
4983 val = iter->value;
4985 if (val)
4986 break;
4989 /* Look in namespace scope. */
4990 if (!val)
4992 iter = cp_binding_level_find_binding_for_name
4993 (NAMESPACE_LEVEL (current_decl_namespace ()), name);
4995 if (iter)
4997 /* If this is the kind of thing we're looking for, we're done. */
4998 if (qualify_lookup (iter->type, LOOKUP_PREFER_TYPES))
4999 val = iter->type;
5000 else if (qualify_lookup (iter->value, LOOKUP_PREFER_TYPES))
5001 val = iter->value;
5006 /* Type found, check if it is in the allowed scopes, ignoring cleanup
5007 and template parameter scopes. */
5008 if (val)
5010 cp_binding_level *b = current_binding_level;
5011 while (b)
5013 if (iter->scope == b)
5014 return val;
5016 if (b->kind == sk_cleanup || b->kind == sk_template_parms
5017 || b->kind == sk_function_parms)
5018 b = b->level_chain;
5019 else if (b->kind == sk_class
5020 && scope == ts_within_enclosing_non_class)
5021 b = b->level_chain;
5022 else
5023 break;
5027 return NULL_TREE;
5030 /* Wrapper for lookup_type_scope_1. */
5032 tree
5033 lookup_type_scope (tree name, tag_scope scope)
5035 tree ret;
5036 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
5037 ret = lookup_type_scope_1 (name, scope);
5038 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
5039 return ret;
5043 /* Similar to `lookup_name' but look only in the innermost non-class
5044 binding level. */
5046 static tree
5047 lookup_name_innermost_nonclass_level_1 (tree name)
5049 cp_binding_level *b;
5050 tree t = NULL_TREE;
5052 b = innermost_nonclass_level ();
5054 if (b->kind == sk_namespace)
5056 t = IDENTIFIER_NAMESPACE_VALUE (name);
5058 /* extern "C" function() */
5059 if (t != NULL_TREE && TREE_CODE (t) == TREE_LIST)
5060 t = TREE_VALUE (t);
5062 else if (IDENTIFIER_BINDING (name)
5063 && LOCAL_BINDING_P (IDENTIFIER_BINDING (name)))
5065 cxx_binding *binding;
5066 binding = IDENTIFIER_BINDING (name);
5067 while (1)
5069 if (binding->scope == b
5070 && !(VAR_P (binding->value)
5071 && DECL_DEAD_FOR_LOCAL (binding->value)))
5072 return binding->value;
5074 if (b->kind == sk_cleanup)
5075 b = b->level_chain;
5076 else
5077 break;
5081 return t;
5084 /* Wrapper for lookup_name_innermost_nonclass_level_1. */
5086 tree
5087 lookup_name_innermost_nonclass_level (tree name)
5089 tree ret;
5090 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
5091 ret = lookup_name_innermost_nonclass_level_1 (name);
5092 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
5093 return ret;
5097 /* Returns true iff DECL is a block-scope extern declaration of a function
5098 or variable. */
5100 bool
5101 is_local_extern (tree decl)
5103 cxx_binding *binding;
5105 /* For functions, this is easy. */
5106 if (TREE_CODE (decl) == FUNCTION_DECL)
5107 return DECL_LOCAL_FUNCTION_P (decl);
5109 if (!VAR_P (decl))
5110 return false;
5111 if (!current_function_decl)
5112 return false;
5114 /* For variables, this is not easy. We need to look at the binding stack
5115 for the identifier to see whether the decl we have is a local. */
5116 for (binding = IDENTIFIER_BINDING (DECL_NAME (decl));
5117 binding && binding->scope->kind != sk_namespace;
5118 binding = binding->previous)
5119 if (binding->value == decl)
5120 return LOCAL_BINDING_P (binding);
5122 return false;
5125 /* Like lookup_name_innermost_nonclass_level, but for types. */
5127 static tree
5128 lookup_type_current_level (tree name)
5130 tree t = NULL_TREE;
5132 timevar_start (TV_NAME_LOOKUP);
5133 gcc_assert (current_binding_level->kind != sk_namespace);
5135 if (REAL_IDENTIFIER_TYPE_VALUE (name) != NULL_TREE
5136 && REAL_IDENTIFIER_TYPE_VALUE (name) != global_type_node)
5138 cp_binding_level *b = current_binding_level;
5139 while (1)
5141 if (purpose_member (name, b->type_shadowed))
5143 t = REAL_IDENTIFIER_TYPE_VALUE (name);
5144 break;
5146 if (b->kind == sk_cleanup)
5147 b = b->level_chain;
5148 else
5149 break;
5153 timevar_stop (TV_NAME_LOOKUP);
5154 return t;
5157 /* [basic.lookup.koenig] */
5158 /* A nonzero return value in the functions below indicates an error. */
5160 struct arg_lookup
5162 tree name;
5163 vec<tree, va_gc> *args;
5164 vec<tree, va_gc> *namespaces;
5165 vec<tree, va_gc> *classes;
5166 tree functions;
5167 hash_set<tree> *fn_set;
5170 static bool arg_assoc (struct arg_lookup*, tree);
5171 static bool arg_assoc_args (struct arg_lookup*, tree);
5172 static bool arg_assoc_args_vec (struct arg_lookup*, vec<tree, va_gc> *);
5173 static bool arg_assoc_type (struct arg_lookup*, tree);
5174 static bool add_function (struct arg_lookup *, tree);
5175 static bool arg_assoc_namespace (struct arg_lookup *, tree);
5176 static bool arg_assoc_class_only (struct arg_lookup *, tree);
5177 static bool arg_assoc_bases (struct arg_lookup *, tree);
5178 static bool arg_assoc_class (struct arg_lookup *, tree);
5179 static bool arg_assoc_template_arg (struct arg_lookup*, tree);
5181 /* Add a function to the lookup structure.
5182 Returns true on error. */
5184 static bool
5185 add_function (struct arg_lookup *k, tree fn)
5187 if (!is_overloaded_fn (fn))
5188 /* All names except those of (possibly overloaded) functions and
5189 function templates are ignored. */;
5190 else if (k->fn_set && k->fn_set->add (fn))
5191 /* It's already in the list. */;
5192 else if (!k->functions)
5193 k->functions = fn;
5194 else if (fn == k->functions)
5196 else
5198 k->functions = build_overload (fn, k->functions);
5199 if (TREE_CODE (k->functions) == OVERLOAD)
5200 OVL_ARG_DEPENDENT (k->functions) = true;
5203 return false;
5206 /* Returns true iff CURRENT has declared itself to be an associated
5207 namespace of SCOPE via a strong using-directive (or transitive chain
5208 thereof). Both are namespaces. */
5210 bool
5211 is_associated_namespace (tree current, tree scope)
5213 vec<tree, va_gc> *seen = make_tree_vector ();
5214 vec<tree, va_gc> *todo = make_tree_vector ();
5215 tree t;
5216 bool ret;
5218 while (1)
5220 if (scope == current)
5222 ret = true;
5223 break;
5225 vec_safe_push (seen, scope);
5226 for (t = DECL_NAMESPACE_ASSOCIATIONS (scope); t; t = TREE_CHAIN (t))
5227 if (!vec_member (TREE_PURPOSE (t), seen))
5228 vec_safe_push (todo, TREE_PURPOSE (t));
5229 if (!todo->is_empty ())
5231 scope = todo->last ();
5232 todo->pop ();
5234 else
5236 ret = false;
5237 break;
5241 release_tree_vector (seen);
5242 release_tree_vector (todo);
5244 return ret;
5247 /* Add functions of a namespace to the lookup structure.
5248 Returns true on error. */
5250 static bool
5251 arg_assoc_namespace (struct arg_lookup *k, tree scope)
5253 tree value;
5255 if (vec_member (scope, k->namespaces))
5256 return false;
5257 vec_safe_push (k->namespaces, scope);
5259 /* Check out our super-users. */
5260 for (value = DECL_NAMESPACE_ASSOCIATIONS (scope); value;
5261 value = TREE_CHAIN (value))
5262 if (arg_assoc_namespace (k, TREE_PURPOSE (value)))
5263 return true;
5265 /* Also look down into inline namespaces. */
5266 for (value = DECL_NAMESPACE_USING (scope); value;
5267 value = TREE_CHAIN (value))
5268 if (is_associated_namespace (scope, TREE_PURPOSE (value)))
5269 if (arg_assoc_namespace (k, TREE_PURPOSE (value)))
5270 return true;
5272 value = namespace_binding (k->name, scope);
5273 if (!value)
5274 return false;
5276 for (; value; value = OVL_NEXT (value))
5278 /* We don't want to find arbitrary hidden functions via argument
5279 dependent lookup. We only want to find friends of associated
5280 classes, which we'll do via arg_assoc_class. */
5281 if (hidden_name_p (OVL_CURRENT (value)))
5282 continue;
5284 if (add_function (k, OVL_CURRENT (value)))
5285 return true;
5288 return false;
5291 /* Adds everything associated with a template argument to the lookup
5292 structure. Returns true on error. */
5294 static bool
5295 arg_assoc_template_arg (struct arg_lookup *k, tree arg)
5297 /* [basic.lookup.koenig]
5299 If T is a template-id, its associated namespaces and classes are
5300 ... the namespaces and classes associated with the types of the
5301 template arguments provided for template type parameters
5302 (excluding template template parameters); the namespaces in which
5303 any template template arguments are defined; and the classes in
5304 which any member templates used as template template arguments
5305 are defined. [Note: non-type template arguments do not
5306 contribute to the set of associated namespaces. ] */
5308 /* Consider first template template arguments. */
5309 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
5310 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
5311 return false;
5312 else if (TREE_CODE (arg) == TEMPLATE_DECL)
5314 tree ctx = CP_DECL_CONTEXT (arg);
5316 /* It's not a member template. */
5317 if (TREE_CODE (ctx) == NAMESPACE_DECL)
5318 return arg_assoc_namespace (k, ctx);
5319 /* Otherwise, it must be member template. */
5320 else
5321 return arg_assoc_class_only (k, ctx);
5323 /* It's an argument pack; handle it recursively. */
5324 else if (ARGUMENT_PACK_P (arg))
5326 tree args = ARGUMENT_PACK_ARGS (arg);
5327 int i, len = TREE_VEC_LENGTH (args);
5328 for (i = 0; i < len; ++i)
5329 if (arg_assoc_template_arg (k, TREE_VEC_ELT (args, i)))
5330 return true;
5332 return false;
5334 /* It's not a template template argument, but it is a type template
5335 argument. */
5336 else if (TYPE_P (arg))
5337 return arg_assoc_type (k, arg);
5338 /* It's a non-type template argument. */
5339 else
5340 return false;
5343 /* Adds the class and its friends to the lookup structure.
5344 Returns true on error. */
5346 static bool
5347 arg_assoc_class_only (struct arg_lookup *k, tree type)
5349 tree list, friends, context;
5351 /* Backend-built structures, such as __builtin_va_list, aren't
5352 affected by all this. */
5353 if (!CLASS_TYPE_P (type))
5354 return false;
5356 context = decl_namespace_context (type);
5357 if (arg_assoc_namespace (k, context))
5358 return true;
5360 complete_type (type);
5362 /* Process friends. */
5363 for (list = DECL_FRIENDLIST (TYPE_MAIN_DECL (type)); list;
5364 list = TREE_CHAIN (list))
5365 if (k->name == FRIEND_NAME (list))
5366 for (friends = FRIEND_DECLS (list); friends;
5367 friends = TREE_CHAIN (friends))
5369 tree fn = TREE_VALUE (friends);
5371 /* Only interested in global functions with potentially hidden
5372 (i.e. unqualified) declarations. */
5373 if (CP_DECL_CONTEXT (fn) != context)
5374 continue;
5375 /* Template specializations are never found by name lookup.
5376 (Templates themselves can be found, but not template
5377 specializations.) */
5378 if (TREE_CODE (fn) == FUNCTION_DECL && DECL_USE_TEMPLATE (fn))
5379 continue;
5380 if (add_function (k, fn))
5381 return true;
5384 return false;
5387 /* Adds the class and its bases to the lookup structure.
5388 Returns true on error. */
5390 static bool
5391 arg_assoc_bases (struct arg_lookup *k, tree type)
5393 if (arg_assoc_class_only (k, type))
5394 return true;
5396 if (TYPE_BINFO (type))
5398 /* Process baseclasses. */
5399 tree binfo, base_binfo;
5400 int i;
5402 for (binfo = TYPE_BINFO (type), i = 0;
5403 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
5404 if (arg_assoc_bases (k, BINFO_TYPE (base_binfo)))
5405 return true;
5408 return false;
5411 /* Adds everything associated with a class argument type to the lookup
5412 structure. Returns true on error.
5414 If T is a class type (including unions), its associated classes are: the
5415 class itself; the class of which it is a member, if any; and its direct
5416 and indirect base classes. Its associated namespaces are the namespaces
5417 of which its associated classes are members. Furthermore, if T is a
5418 class template specialization, its associated namespaces and classes
5419 also include: the namespaces and classes associated with the types of
5420 the template arguments provided for template type parameters (excluding
5421 template template parameters); the namespaces of which any template
5422 template arguments are members; and the classes of which any member
5423 templates used as template template arguments are members. [ Note:
5424 non-type template arguments do not contribute to the set of associated
5425 namespaces. --end note] */
5427 static bool
5428 arg_assoc_class (struct arg_lookup *k, tree type)
5430 tree list;
5431 int i;
5433 /* Backend build structures, such as __builtin_va_list, aren't
5434 affected by all this. */
5435 if (!CLASS_TYPE_P (type))
5436 return false;
5438 if (vec_member (type, k->classes))
5439 return false;
5440 vec_safe_push (k->classes, type);
5442 if (TYPE_CLASS_SCOPE_P (type)
5443 && arg_assoc_class_only (k, TYPE_CONTEXT (type)))
5444 return true;
5446 if (arg_assoc_bases (k, type))
5447 return true;
5449 /* Process template arguments. */
5450 if (CLASSTYPE_TEMPLATE_INFO (type)
5451 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
5453 list = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
5454 for (i = 0; i < TREE_VEC_LENGTH (list); ++i)
5455 if (arg_assoc_template_arg (k, TREE_VEC_ELT (list, i)))
5456 return true;
5459 return false;
5462 /* Adds everything associated with a given type.
5463 Returns 1 on error. */
5465 static bool
5466 arg_assoc_type (struct arg_lookup *k, tree type)
5468 /* As we do not get the type of non-type dependent expressions
5469 right, we can end up with such things without a type. */
5470 if (!type)
5471 return false;
5473 if (TYPE_PTRDATAMEM_P (type))
5475 /* Pointer to member: associate class type and value type. */
5476 if (arg_assoc_type (k, TYPE_PTRMEM_CLASS_TYPE (type)))
5477 return true;
5478 return arg_assoc_type (k, TYPE_PTRMEM_POINTED_TO_TYPE (type));
5480 else switch (TREE_CODE (type))
5482 case ERROR_MARK:
5483 return false;
5484 case VOID_TYPE:
5485 case INTEGER_TYPE:
5486 case REAL_TYPE:
5487 case COMPLEX_TYPE:
5488 case VECTOR_TYPE:
5489 case BOOLEAN_TYPE:
5490 case FIXED_POINT_TYPE:
5491 case DECLTYPE_TYPE:
5492 case NULLPTR_TYPE:
5493 return false;
5494 case RECORD_TYPE:
5495 if (TYPE_PTRMEMFUNC_P (type))
5496 return arg_assoc_type (k, TYPE_PTRMEMFUNC_FN_TYPE (type));
5497 case UNION_TYPE:
5498 return arg_assoc_class (k, type);
5499 case POINTER_TYPE:
5500 case REFERENCE_TYPE:
5501 case ARRAY_TYPE:
5502 return arg_assoc_type (k, TREE_TYPE (type));
5503 case ENUMERAL_TYPE:
5504 if (TYPE_CLASS_SCOPE_P (type)
5505 && arg_assoc_class_only (k, TYPE_CONTEXT (type)))
5506 return true;
5507 return arg_assoc_namespace (k, decl_namespace_context (type));
5508 case METHOD_TYPE:
5509 /* The basetype is referenced in the first arg type, so just
5510 fall through. */
5511 case FUNCTION_TYPE:
5512 /* Associate the parameter types. */
5513 if (arg_assoc_args (k, TYPE_ARG_TYPES (type)))
5514 return true;
5515 /* Associate the return type. */
5516 return arg_assoc_type (k, TREE_TYPE (type));
5517 case TEMPLATE_TYPE_PARM:
5518 case BOUND_TEMPLATE_TEMPLATE_PARM:
5519 return false;
5520 case TYPENAME_TYPE:
5521 return false;
5522 case LANG_TYPE:
5523 gcc_assert (type == unknown_type_node
5524 || type == init_list_type_node);
5525 return false;
5526 case TYPE_PACK_EXPANSION:
5527 return arg_assoc_type (k, PACK_EXPANSION_PATTERN (type));
5529 default:
5530 gcc_unreachable ();
5532 return false;
5535 /* Adds everything associated with arguments. Returns true on error. */
5537 static bool
5538 arg_assoc_args (struct arg_lookup *k, tree args)
5540 for (; args; args = TREE_CHAIN (args))
5541 if (arg_assoc (k, TREE_VALUE (args)))
5542 return true;
5543 return false;
5546 /* Adds everything associated with an argument vector. Returns true
5547 on error. */
5549 static bool
5550 arg_assoc_args_vec (struct arg_lookup *k, vec<tree, va_gc> *args)
5552 unsigned int ix;
5553 tree arg;
5555 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
5556 if (arg_assoc (k, arg))
5557 return true;
5558 return false;
5561 /* Adds everything associated with a given tree_node. Returns 1 on error. */
5563 static bool
5564 arg_assoc (struct arg_lookup *k, tree n)
5566 if (n == error_mark_node)
5567 return false;
5569 if (TYPE_P (n))
5570 return arg_assoc_type (k, n);
5572 if (! type_unknown_p (n))
5573 return arg_assoc_type (k, TREE_TYPE (n));
5575 if (TREE_CODE (n) == ADDR_EXPR)
5576 n = TREE_OPERAND (n, 0);
5577 if (TREE_CODE (n) == COMPONENT_REF)
5578 n = TREE_OPERAND (n, 1);
5579 if (TREE_CODE (n) == OFFSET_REF)
5580 n = TREE_OPERAND (n, 1);
5581 while (TREE_CODE (n) == TREE_LIST)
5582 n = TREE_VALUE (n);
5583 if (BASELINK_P (n))
5584 n = BASELINK_FUNCTIONS (n);
5586 if (TREE_CODE (n) == FUNCTION_DECL)
5587 return arg_assoc_type (k, TREE_TYPE (n));
5588 if (TREE_CODE (n) == TEMPLATE_ID_EXPR)
5590 /* The working paper doesn't currently say how to handle template-id
5591 arguments. The sensible thing would seem to be to handle the list
5592 of template candidates like a normal overload set, and handle the
5593 template arguments like we do for class template
5594 specializations. */
5595 tree templ = TREE_OPERAND (n, 0);
5596 tree args = TREE_OPERAND (n, 1);
5597 int ix;
5599 /* First the templates. */
5600 if (arg_assoc (k, templ))
5601 return true;
5603 /* Now the arguments. */
5604 if (args)
5605 for (ix = TREE_VEC_LENGTH (args); ix--;)
5606 if (arg_assoc_template_arg (k, TREE_VEC_ELT (args, ix)) == 1)
5607 return true;
5609 else if (TREE_CODE (n) == OVERLOAD)
5611 for (; n; n = OVL_NEXT (n))
5612 if (arg_assoc_type (k, TREE_TYPE (OVL_CURRENT (n))))
5613 return true;
5616 return false;
5619 /* Performs Koenig lookup depending on arguments, where fns
5620 are the functions found in normal lookup. */
5622 static tree
5623 lookup_arg_dependent_1 (tree name, tree fns, vec<tree, va_gc> *args)
5625 struct arg_lookup k;
5627 /* Remove any hidden friend functions from the list of functions
5628 found so far. They will be added back by arg_assoc_class as
5629 appropriate. */
5630 fns = remove_hidden_names (fns);
5632 k.name = name;
5633 k.args = args;
5634 k.functions = fns;
5635 k.classes = make_tree_vector ();
5637 /* We previously performed an optimization here by setting
5638 NAMESPACES to the current namespace when it was safe. However, DR
5639 164 says that namespaces that were already searched in the first
5640 stage of template processing are searched again (potentially
5641 picking up later definitions) in the second stage. */
5642 k.namespaces = make_tree_vector ();
5644 /* We used to allow duplicates and let joust discard them, but
5645 since the above change for DR 164 we end up with duplicates of
5646 all the functions found by unqualified lookup. So keep track
5647 of which ones we've seen. */
5648 if (fns)
5650 tree ovl;
5651 /* We shouldn't be here if lookup found something other than
5652 namespace-scope functions. */
5653 gcc_assert (DECL_NAMESPACE_SCOPE_P (OVL_CURRENT (fns)));
5654 k.fn_set = new hash_set<tree>;
5655 for (ovl = fns; ovl; ovl = OVL_NEXT (ovl))
5656 k.fn_set->add (OVL_CURRENT (ovl));
5658 else
5659 k.fn_set = NULL;
5661 arg_assoc_args_vec (&k, args);
5663 fns = k.functions;
5665 if (fns
5666 && !VAR_P (fns)
5667 && !is_overloaded_fn (fns))
5669 error ("argument dependent lookup finds %q+D", fns);
5670 error (" in call to %qD", name);
5671 fns = error_mark_node;
5674 release_tree_vector (k.classes);
5675 release_tree_vector (k.namespaces);
5676 delete k.fn_set;
5678 return fns;
5681 /* Wrapper for lookup_arg_dependent_1. */
5683 tree
5684 lookup_arg_dependent (tree name, tree fns, vec<tree, va_gc> *args)
5686 tree ret;
5687 bool subtime;
5688 subtime = timevar_cond_start (TV_NAME_LOOKUP);
5689 ret = lookup_arg_dependent_1 (name, fns, args);
5690 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
5691 return ret;
5695 /* Add namespace to using_directives. Return NULL_TREE if nothing was
5696 changed (i.e. there was already a directive), or the fresh
5697 TREE_LIST otherwise. */
5699 static tree
5700 push_using_directive_1 (tree used)
5702 tree ud = current_binding_level->using_directives;
5703 tree iter, ancestor;
5705 /* Check if we already have this. */
5706 if (purpose_member (used, ud) != NULL_TREE)
5707 return NULL_TREE;
5709 ancestor = namespace_ancestor (current_decl_namespace (), used);
5710 ud = current_binding_level->using_directives;
5711 ud = tree_cons (used, ancestor, ud);
5712 current_binding_level->using_directives = ud;
5714 /* Recursively add all namespaces used. */
5715 for (iter = DECL_NAMESPACE_USING (used); iter; iter = TREE_CHAIN (iter))
5716 push_using_directive (TREE_PURPOSE (iter));
5718 return ud;
5721 /* Wrapper for push_using_directive_1. */
5723 static tree
5724 push_using_directive (tree used)
5726 tree ret;
5727 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
5728 ret = push_using_directive_1 (used);
5729 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
5730 return ret;
5733 /* The type TYPE is being declared. If it is a class template, or a
5734 specialization of a class template, do any processing required and
5735 perform error-checking. If IS_FRIEND is nonzero, this TYPE is
5736 being declared a friend. B is the binding level at which this TYPE
5737 should be bound.
5739 Returns the TYPE_DECL for TYPE, which may have been altered by this
5740 processing. */
5742 static tree
5743 maybe_process_template_type_declaration (tree type, int is_friend,
5744 cp_binding_level *b)
5746 tree decl = TYPE_NAME (type);
5748 if (processing_template_parmlist)
5749 /* You can't declare a new template type in a template parameter
5750 list. But, you can declare a non-template type:
5752 template <class A*> struct S;
5754 is a forward-declaration of `A'. */
5756 else if (b->kind == sk_namespace
5757 && current_binding_level->kind != sk_namespace)
5758 /* If this new type is being injected into a containing scope,
5759 then it's not a template type. */
5761 else
5763 gcc_assert (MAYBE_CLASS_TYPE_P (type)
5764 || TREE_CODE (type) == ENUMERAL_TYPE);
5766 if (processing_template_decl)
5768 /* This may change after the call to
5769 push_template_decl_real, but we want the original value. */
5770 tree name = DECL_NAME (decl);
5772 decl = push_template_decl_real (decl, is_friend);
5773 if (decl == error_mark_node)
5774 return error_mark_node;
5776 /* If the current binding level is the binding level for the
5777 template parameters (see the comment in
5778 begin_template_parm_list) and the enclosing level is a class
5779 scope, and we're not looking at a friend, push the
5780 declaration of the member class into the class scope. In the
5781 friend case, push_template_decl will already have put the
5782 friend into global scope, if appropriate. */
5783 if (TREE_CODE (type) != ENUMERAL_TYPE
5784 && !is_friend && b->kind == sk_template_parms
5785 && b->level_chain->kind == sk_class)
5787 finish_member_declaration (CLASSTYPE_TI_TEMPLATE (type));
5789 if (!COMPLETE_TYPE_P (current_class_type))
5791 maybe_add_class_template_decl_list (current_class_type,
5792 type, /*friend_p=*/0);
5793 /* Put this UTD in the table of UTDs for the class. */
5794 if (CLASSTYPE_NESTED_UTDS (current_class_type) == NULL)
5795 CLASSTYPE_NESTED_UTDS (current_class_type) =
5796 binding_table_new (SCOPE_DEFAULT_HT_SIZE);
5798 binding_table_insert
5799 (CLASSTYPE_NESTED_UTDS (current_class_type), name, type);
5805 return decl;
5808 /* Push a tag name NAME for struct/class/union/enum type TYPE. In case
5809 that the NAME is a class template, the tag is processed but not pushed.
5811 The pushed scope depend on the SCOPE parameter:
5812 - When SCOPE is TS_CURRENT, put it into the inner-most non-sk_cleanup
5813 scope.
5814 - When SCOPE is TS_GLOBAL, put it in the inner-most non-class and
5815 non-template-parameter scope. This case is needed for forward
5816 declarations.
5817 - When SCOPE is TS_WITHIN_ENCLOSING_NON_CLASS, this is similar to
5818 TS_GLOBAL case except that names within template-parameter scopes
5819 are not pushed at all.
5821 Returns TYPE upon success and ERROR_MARK_NODE otherwise. */
5823 static tree
5824 pushtag_1 (tree name, tree type, tag_scope scope)
5826 cp_binding_level *b;
5827 tree decl;
5829 b = current_binding_level;
5830 while (/* Cleanup scopes are not scopes from the point of view of
5831 the language. */
5832 b->kind == sk_cleanup
5833 /* Neither are function parameter scopes. */
5834 || b->kind == sk_function_parms
5835 /* Neither are the scopes used to hold template parameters
5836 for an explicit specialization. For an ordinary template
5837 declaration, these scopes are not scopes from the point of
5838 view of the language. */
5839 || (b->kind == sk_template_parms
5840 && (b->explicit_spec_p || scope == ts_global))
5841 || (b->kind == sk_class
5842 && (scope != ts_current
5843 /* We may be defining a new type in the initializer
5844 of a static member variable. We allow this when
5845 not pedantic, and it is particularly useful for
5846 type punning via an anonymous union. */
5847 || COMPLETE_TYPE_P (b->this_entity))))
5848 b = b->level_chain;
5850 gcc_assert (identifier_p (name));
5852 /* Do C++ gratuitous typedefing. */
5853 if (identifier_type_value_1 (name) != type)
5855 tree tdef;
5856 int in_class = 0;
5857 tree context = TYPE_CONTEXT (type);
5859 if (! context)
5861 tree cs = current_scope ();
5863 if (scope == ts_current
5864 || (cs && TREE_CODE (cs) == FUNCTION_DECL))
5865 context = cs;
5866 else if (cs != NULL_TREE && TYPE_P (cs))
5867 /* When declaring a friend class of a local class, we want
5868 to inject the newly named class into the scope
5869 containing the local class, not the namespace
5870 scope. */
5871 context = decl_function_context (get_type_decl (cs));
5873 if (!context)
5874 context = current_namespace;
5876 if (b->kind == sk_class
5877 || (b->kind == sk_template_parms
5878 && b->level_chain->kind == sk_class))
5879 in_class = 1;
5881 if (current_lang_name == lang_name_java)
5882 TYPE_FOR_JAVA (type) = 1;
5884 tdef = create_implicit_typedef (name, type);
5885 DECL_CONTEXT (tdef) = FROB_CONTEXT (context);
5886 if (scope == ts_within_enclosing_non_class)
5888 /* This is a friend. Make this TYPE_DECL node hidden from
5889 ordinary name lookup. Its corresponding TEMPLATE_DECL
5890 will be marked in push_template_decl_real. */
5891 retrofit_lang_decl (tdef);
5892 DECL_ANTICIPATED (tdef) = 1;
5893 DECL_FRIEND_P (tdef) = 1;
5896 decl = maybe_process_template_type_declaration
5897 (type, scope == ts_within_enclosing_non_class, b);
5898 if (decl == error_mark_node)
5899 return decl;
5901 if (b->kind == sk_class)
5903 if (!TYPE_BEING_DEFINED (current_class_type))
5904 return error_mark_node;
5906 if (!PROCESSING_REAL_TEMPLATE_DECL_P ())
5907 /* Put this TYPE_DECL on the TYPE_FIELDS list for the
5908 class. But if it's a member template class, we want
5909 the TEMPLATE_DECL, not the TYPE_DECL, so this is done
5910 later. */
5911 finish_member_declaration (decl);
5912 else
5913 pushdecl_class_level (decl);
5915 else if (b->kind != sk_template_parms)
5917 decl = pushdecl_with_scope_1 (decl, b, /*is_friend=*/false);
5918 if (decl == error_mark_node)
5919 return decl;
5922 if (! in_class)
5923 set_identifier_type_value_with_scope (name, tdef, b);
5925 TYPE_CONTEXT (type) = DECL_CONTEXT (decl);
5927 /* If this is a local class, keep track of it. We need this
5928 information for name-mangling, and so that it is possible to
5929 find all function definitions in a translation unit in a
5930 convenient way. (It's otherwise tricky to find a member
5931 function definition it's only pointed to from within a local
5932 class.) */
5933 if (TYPE_FUNCTION_SCOPE_P (type))
5935 if (processing_template_decl)
5937 /* Push a DECL_EXPR so we call pushtag at the right time in
5938 template instantiation rather than in some nested context. */
5939 add_decl_expr (decl);
5941 else
5942 vec_safe_push (local_classes, type);
5945 if (b->kind == sk_class
5946 && !COMPLETE_TYPE_P (current_class_type))
5948 maybe_add_class_template_decl_list (current_class_type,
5949 type, /*friend_p=*/0);
5951 if (CLASSTYPE_NESTED_UTDS (current_class_type) == NULL)
5952 CLASSTYPE_NESTED_UTDS (current_class_type)
5953 = binding_table_new (SCOPE_DEFAULT_HT_SIZE);
5955 binding_table_insert
5956 (CLASSTYPE_NESTED_UTDS (current_class_type), name, type);
5959 decl = TYPE_NAME (type);
5960 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
5962 /* Set type visibility now if this is a forward declaration. */
5963 TREE_PUBLIC (decl) = 1;
5964 determine_visibility (decl);
5966 return type;
5969 /* Wrapper for pushtag_1. */
5971 tree
5972 pushtag (tree name, tree type, tag_scope scope)
5974 tree ret;
5975 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
5976 ret = pushtag_1 (name, type, scope);
5977 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
5978 return ret;
5981 /* Subroutines for reverting temporarily to top-level for instantiation
5982 of templates and such. We actually need to clear out the class- and
5983 local-value slots of all identifiers, so that only the global values
5984 are at all visible. Simply setting current_binding_level to the global
5985 scope isn't enough, because more binding levels may be pushed. */
5986 struct saved_scope *scope_chain;
5988 /* Return true if ID has not already been marked. */
5990 static inline bool
5991 store_binding_p (tree id)
5993 if (!id || !IDENTIFIER_BINDING (id))
5994 return false;
5996 if (IDENTIFIER_MARKED (id))
5997 return false;
5999 return true;
6002 /* Add an appropriate binding to *OLD_BINDINGS which needs to already
6003 have enough space reserved. */
6005 static void
6006 store_binding (tree id, vec<cxx_saved_binding, va_gc> **old_bindings)
6008 cxx_saved_binding saved;
6010 gcc_checking_assert (store_binding_p (id));
6012 IDENTIFIER_MARKED (id) = 1;
6014 saved.identifier = id;
6015 saved.binding = IDENTIFIER_BINDING (id);
6016 saved.real_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
6017 (*old_bindings)->quick_push (saved);
6018 IDENTIFIER_BINDING (id) = NULL;
6021 static void
6022 store_bindings (tree names, vec<cxx_saved_binding, va_gc> **old_bindings)
6024 static vec<tree> bindings_need_stored = vNULL;
6025 tree t, id;
6026 size_t i;
6028 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6029 for (t = names; t; t = TREE_CHAIN (t))
6031 if (TREE_CODE (t) == TREE_LIST)
6032 id = TREE_PURPOSE (t);
6033 else
6034 id = DECL_NAME (t);
6036 if (store_binding_p (id))
6037 bindings_need_stored.safe_push (id);
6039 if (!bindings_need_stored.is_empty ())
6041 vec_safe_reserve_exact (*old_bindings, bindings_need_stored.length ());
6042 for (i = 0; bindings_need_stored.iterate (i, &id); ++i)
6044 /* We can appearantly have duplicates in NAMES. */
6045 if (store_binding_p (id))
6046 store_binding (id, old_bindings);
6048 bindings_need_stored.truncate (0);
6050 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6053 /* Like store_bindings, but NAMES is a vector of cp_class_binding
6054 objects, rather than a TREE_LIST. */
6056 static void
6057 store_class_bindings (vec<cp_class_binding, va_gc> *names,
6058 vec<cxx_saved_binding, va_gc> **old_bindings)
6060 static vec<tree> bindings_need_stored = vNULL;
6061 size_t i;
6062 cp_class_binding *cb;
6064 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6065 for (i = 0; vec_safe_iterate (names, i, &cb); ++i)
6066 if (store_binding_p (cb->identifier))
6067 bindings_need_stored.safe_push (cb->identifier);
6068 if (!bindings_need_stored.is_empty ())
6070 tree id;
6071 vec_safe_reserve_exact (*old_bindings, bindings_need_stored.length ());
6072 for (i = 0; bindings_need_stored.iterate (i, &id); ++i)
6073 store_binding (id, old_bindings);
6074 bindings_need_stored.truncate (0);
6076 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6079 void
6080 push_to_top_level (void)
6082 struct saved_scope *s;
6083 cp_binding_level *b;
6084 cxx_saved_binding *sb;
6085 size_t i;
6086 bool need_pop;
6088 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6089 s = ggc_cleared_alloc<saved_scope> ();
6091 b = scope_chain ? current_binding_level : 0;
6093 /* If we're in the middle of some function, save our state. */
6094 if (cfun)
6096 need_pop = true;
6097 push_function_context ();
6099 else
6100 need_pop = false;
6102 if (scope_chain && previous_class_level)
6103 store_class_bindings (previous_class_level->class_shadowed,
6104 &s->old_bindings);
6106 /* Have to include the global scope, because class-scope decls
6107 aren't listed anywhere useful. */
6108 for (; b; b = b->level_chain)
6110 tree t;
6112 /* Template IDs are inserted into the global level. If they were
6113 inserted into namespace level, finish_file wouldn't find them
6114 when doing pending instantiations. Therefore, don't stop at
6115 namespace level, but continue until :: . */
6116 if (global_scope_p (b))
6117 break;
6119 store_bindings (b->names, &s->old_bindings);
6120 /* We also need to check class_shadowed to save class-level type
6121 bindings, since pushclass doesn't fill in b->names. */
6122 if (b->kind == sk_class)
6123 store_class_bindings (b->class_shadowed, &s->old_bindings);
6125 /* Unwind type-value slots back to top level. */
6126 for (t = b->type_shadowed; t; t = TREE_CHAIN (t))
6127 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (t), TREE_VALUE (t));
6130 FOR_EACH_VEC_SAFE_ELT (s->old_bindings, i, sb)
6131 IDENTIFIER_MARKED (sb->identifier) = 0;
6133 s->prev = scope_chain;
6134 s->bindings = b;
6135 s->need_pop_function_context = need_pop;
6136 s->function_decl = current_function_decl;
6137 s->unevaluated_operand = cp_unevaluated_operand;
6138 s->inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
6139 s->x_stmt_tree.stmts_are_full_exprs_p = true;
6141 scope_chain = s;
6142 current_function_decl = NULL_TREE;
6143 vec_alloc (current_lang_base, 10);
6144 current_lang_name = lang_name_cplusplus;
6145 current_namespace = global_namespace;
6146 push_class_stack ();
6147 cp_unevaluated_operand = 0;
6148 c_inhibit_evaluation_warnings = 0;
6149 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6152 static void
6153 pop_from_top_level_1 (void)
6155 struct saved_scope *s = scope_chain;
6156 cxx_saved_binding *saved;
6157 size_t i;
6159 /* Clear out class-level bindings cache. */
6160 if (previous_class_level)
6161 invalidate_class_lookup_cache ();
6162 pop_class_stack ();
6164 current_lang_base = 0;
6166 scope_chain = s->prev;
6167 FOR_EACH_VEC_SAFE_ELT (s->old_bindings, i, saved)
6169 tree id = saved->identifier;
6171 IDENTIFIER_BINDING (id) = saved->binding;
6172 SET_IDENTIFIER_TYPE_VALUE (id, saved->real_type_value);
6175 /* If we were in the middle of compiling a function, restore our
6176 state. */
6177 if (s->need_pop_function_context)
6178 pop_function_context ();
6179 current_function_decl = s->function_decl;
6180 cp_unevaluated_operand = s->unevaluated_operand;
6181 c_inhibit_evaluation_warnings = s->inhibit_evaluation_warnings;
6184 /* Wrapper for pop_from_top_level_1. */
6186 void
6187 pop_from_top_level (void)
6189 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6190 pop_from_top_level_1 ();
6191 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6195 /* Pop off extraneous binding levels left over due to syntax errors.
6197 We don't pop past namespaces, as they might be valid. */
6199 void
6200 pop_everything (void)
6202 if (ENABLE_SCOPE_CHECKING)
6203 verbatim ("XXX entering pop_everything ()\n");
6204 while (!toplevel_bindings_p ())
6206 if (current_binding_level->kind == sk_class)
6207 pop_nested_class ();
6208 else
6209 poplevel (0, 0, 0);
6211 if (ENABLE_SCOPE_CHECKING)
6212 verbatim ("XXX leaving pop_everything ()\n");
6215 /* Emit debugging information for using declarations and directives.
6216 If input tree is overloaded fn then emit debug info for all
6217 candidates. */
6219 void
6220 cp_emit_debug_info_for_using (tree t, tree context)
6222 /* Don't try to emit any debug information if we have errors. */
6223 if (seen_error ())
6224 return;
6226 /* Ignore this FUNCTION_DECL if it refers to a builtin declaration
6227 of a builtin function. */
6228 if (TREE_CODE (t) == FUNCTION_DECL
6229 && DECL_EXTERNAL (t)
6230 && DECL_BUILT_IN (t))
6231 return;
6233 /* Do not supply context to imported_module_or_decl, if
6234 it is a global namespace. */
6235 if (context == global_namespace)
6236 context = NULL_TREE;
6238 if (BASELINK_P (t))
6239 t = BASELINK_FUNCTIONS (t);
6241 /* FIXME: Handle TEMPLATE_DECLs. */
6242 for (t = OVL_CURRENT (t); t; t = OVL_NEXT (t))
6243 if (TREE_CODE (t) != TEMPLATE_DECL)
6245 if (building_stmt_list_p ())
6246 add_stmt (build_stmt (input_location, USING_STMT, t));
6247 else
6248 (*debug_hooks->imported_module_or_decl) (t, NULL_TREE, context, false);
6252 #include "gt-cp-name-lookup.h"