PR c++/72707
[official-gcc.git] / gcc / cp / name-lookup.c
blob223838c3ddf71899b21678150ac60be209f0bb89
1 /* Definitions for C++ name lookup routines.
2 Copyright (C) 2003-2016 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 "cp-tree.h"
25 #include "timevar.h"
26 #include "stringpool.h"
27 #include "print-tree.h"
28 #include "attribs.h"
29 #include "debug.h"
30 #include "c-family/c-pragma.h"
31 #include "params.h"
32 #include "gcc-rich-location.h"
33 #include "spellcheck-tree.h"
34 #include "parser.h"
36 /* The bindings for a particular name in a particular scope. */
38 struct scope_binding {
39 tree value;
40 tree type;
42 #define EMPTY_SCOPE_BINDING { NULL_TREE, NULL_TREE }
44 static cp_binding_level *innermost_nonclass_level (void);
45 static cxx_binding *binding_for_name (cp_binding_level *, tree);
46 static tree push_overloaded_decl (tree, int, bool);
47 static bool lookup_using_namespace (tree, struct scope_binding *, tree,
48 tree, int);
49 static bool qualified_lookup_using_namespace (tree, tree,
50 struct scope_binding *, int);
51 static tree lookup_type_current_level (tree);
52 static tree push_using_directive (tree);
53 static tree lookup_extern_c_fun_in_all_ns (tree);
54 static void diagnose_name_conflict (tree, tree);
56 /* The :: namespace. */
58 tree global_namespace;
60 /* The name of the anonymous namespace, throughout this translation
61 unit. */
62 static GTY(()) tree anonymous_namespace_name;
64 /* Initialize anonymous_namespace_name if necessary, and return it. */
66 static tree
67 get_anonymous_namespace_name (void)
69 if (!anonymous_namespace_name)
71 /* We used to use get_file_function_name here, but that isn't
72 necessary now that anonymous namespace typeinfos
73 are !TREE_PUBLIC, and thus compared by address. */
74 /* The demangler expects anonymous namespaces to be called
75 something starting with '_GLOBAL__N_'. */
76 anonymous_namespace_name = get_identifier ("_GLOBAL__N_1");
78 return anonymous_namespace_name;
81 /* Compute the chain index of a binding_entry given the HASH value of its
82 name and the total COUNT of chains. COUNT is assumed to be a power
83 of 2. */
85 #define ENTRY_INDEX(HASH, COUNT) (((HASH) >> 3) & ((COUNT) - 1))
87 /* A free list of "binding_entry"s awaiting for re-use. */
89 static GTY((deletable)) binding_entry free_binding_entry = NULL;
91 /* Create a binding_entry object for (NAME, TYPE). */
93 static inline binding_entry
94 binding_entry_make (tree name, tree type)
96 binding_entry entry;
98 if (free_binding_entry)
100 entry = free_binding_entry;
101 free_binding_entry = entry->chain;
103 else
104 entry = ggc_alloc<binding_entry_s> ();
106 entry->name = name;
107 entry->type = type;
108 entry->chain = NULL;
110 return entry;
113 /* Put ENTRY back on the free list. */
114 #if 0
115 static inline void
116 binding_entry_free (binding_entry entry)
118 entry->name = NULL;
119 entry->type = NULL;
120 entry->chain = free_binding_entry;
121 free_binding_entry = entry;
123 #endif
125 /* The datatype used to implement the mapping from names to types at
126 a given scope. */
127 struct GTY(()) binding_table_s {
128 /* Array of chains of "binding_entry"s */
129 binding_entry * GTY((length ("%h.chain_count"))) chain;
131 /* The number of chains in this table. This is the length of the
132 member "chain" considered as an array. */
133 size_t chain_count;
135 /* Number of "binding_entry"s in this table. */
136 size_t entry_count;
139 /* Construct TABLE with an initial CHAIN_COUNT. */
141 static inline void
142 binding_table_construct (binding_table table, size_t chain_count)
144 table->chain_count = chain_count;
145 table->entry_count = 0;
146 table->chain = ggc_cleared_vec_alloc<binding_entry> (table->chain_count);
149 /* Make TABLE's entries ready for reuse. */
150 #if 0
151 static void
152 binding_table_free (binding_table table)
154 size_t i;
155 size_t count;
157 if (table == NULL)
158 return;
160 for (i = 0, count = table->chain_count; i < count; ++i)
162 binding_entry temp = table->chain[i];
163 while (temp != NULL)
165 binding_entry entry = temp;
166 temp = entry->chain;
167 binding_entry_free (entry);
169 table->chain[i] = NULL;
171 table->entry_count = 0;
173 #endif
175 /* Allocate a table with CHAIN_COUNT, assumed to be a power of two. */
177 static inline binding_table
178 binding_table_new (size_t chain_count)
180 binding_table table = ggc_alloc<binding_table_s> ();
181 table->chain = NULL;
182 binding_table_construct (table, chain_count);
183 return table;
186 /* Expand TABLE to twice its current chain_count. */
188 static void
189 binding_table_expand (binding_table table)
191 const size_t old_chain_count = table->chain_count;
192 const size_t old_entry_count = table->entry_count;
193 const size_t new_chain_count = 2 * old_chain_count;
194 binding_entry *old_chains = table->chain;
195 size_t i;
197 binding_table_construct (table, new_chain_count);
198 for (i = 0; i < old_chain_count; ++i)
200 binding_entry entry = old_chains[i];
201 for (; entry != NULL; entry = old_chains[i])
203 const unsigned int hash = IDENTIFIER_HASH_VALUE (entry->name);
204 const size_t j = ENTRY_INDEX (hash, new_chain_count);
206 old_chains[i] = entry->chain;
207 entry->chain = table->chain[j];
208 table->chain[j] = entry;
211 table->entry_count = old_entry_count;
214 /* Insert a binding for NAME to TYPE into TABLE. */
216 static void
217 binding_table_insert (binding_table table, tree name, tree type)
219 const unsigned int hash = IDENTIFIER_HASH_VALUE (name);
220 const size_t i = ENTRY_INDEX (hash, table->chain_count);
221 binding_entry entry = binding_entry_make (name, type);
223 entry->chain = table->chain[i];
224 table->chain[i] = entry;
225 ++table->entry_count;
227 if (3 * table->chain_count < 5 * table->entry_count)
228 binding_table_expand (table);
231 /* Return the binding_entry, if any, that maps NAME. */
233 binding_entry
234 binding_table_find (binding_table table, tree name)
236 const unsigned int hash = IDENTIFIER_HASH_VALUE (name);
237 binding_entry entry = table->chain[ENTRY_INDEX (hash, table->chain_count)];
239 while (entry != NULL && entry->name != name)
240 entry = entry->chain;
242 return entry;
245 /* Apply PROC -- with DATA -- to all entries in TABLE. */
247 void
248 binding_table_foreach (binding_table table, bt_foreach_proc proc, void *data)
250 size_t chain_count;
251 size_t i;
253 if (!table)
254 return;
256 chain_count = table->chain_count;
257 for (i = 0; i < chain_count; ++i)
259 binding_entry entry = table->chain[i];
260 for (; entry != NULL; entry = entry->chain)
261 proc (entry, data);
265 #ifndef ENABLE_SCOPE_CHECKING
266 # define ENABLE_SCOPE_CHECKING 0
267 #else
268 # define ENABLE_SCOPE_CHECKING 1
269 #endif
271 /* A free list of "cxx_binding"s, connected by their PREVIOUS. */
273 static GTY((deletable)) cxx_binding *free_bindings;
275 /* Initialize VALUE and TYPE field for BINDING, and set the PREVIOUS
276 field to NULL. */
278 static inline void
279 cxx_binding_init (cxx_binding *binding, tree value, tree type)
281 binding->value = value;
282 binding->type = type;
283 binding->previous = NULL;
286 /* (GC)-allocate a binding object with VALUE and TYPE member initialized. */
288 static cxx_binding *
289 cxx_binding_make (tree value, tree type)
291 cxx_binding *binding;
292 if (free_bindings)
294 binding = free_bindings;
295 free_bindings = binding->previous;
297 else
298 binding = ggc_alloc<cxx_binding> ();
300 cxx_binding_init (binding, value, type);
302 return binding;
305 /* Put BINDING back on the free list. */
307 static inline void
308 cxx_binding_free (cxx_binding *binding)
310 binding->scope = NULL;
311 binding->previous = free_bindings;
312 free_bindings = binding;
315 /* Create a new binding for NAME (with the indicated VALUE and TYPE
316 bindings) in the class scope indicated by SCOPE. */
318 static cxx_binding *
319 new_class_binding (tree name, tree value, tree type, cp_binding_level *scope)
321 cp_class_binding cb = {cxx_binding_make (value, type), name};
322 cxx_binding *binding = cb.base;
323 vec_safe_push (scope->class_shadowed, cb);
324 binding->scope = scope;
325 return binding;
328 /* Make DECL the innermost binding for ID. The LEVEL is the binding
329 level at which this declaration is being bound. */
331 void
332 push_binding (tree id, tree decl, cp_binding_level* level)
334 cxx_binding *binding;
336 if (level != class_binding_level)
338 binding = cxx_binding_make (decl, NULL_TREE);
339 binding->scope = level;
341 else
342 binding = new_class_binding (id, decl, /*type=*/NULL_TREE, level);
344 /* Now, fill in the binding information. */
345 binding->previous = IDENTIFIER_BINDING (id);
346 INHERITED_VALUE_BINDING_P (binding) = 0;
347 LOCAL_BINDING_P (binding) = (level != class_binding_level);
349 /* And put it on the front of the list of bindings for ID. */
350 IDENTIFIER_BINDING (id) = binding;
353 /* Remove the binding for DECL which should be the innermost binding
354 for ID. */
356 void
357 pop_binding (tree id, tree decl)
359 cxx_binding *binding;
361 if (id == NULL_TREE)
362 /* It's easiest to write the loops that call this function without
363 checking whether or not the entities involved have names. We
364 get here for such an entity. */
365 return;
367 /* Get the innermost binding for ID. */
368 binding = IDENTIFIER_BINDING (id);
370 /* The name should be bound. */
371 gcc_assert (binding != NULL);
373 /* The DECL will be either the ordinary binding or the type
374 binding for this identifier. Remove that binding. */
375 if (binding->value == decl)
376 binding->value = NULL_TREE;
377 else
379 gcc_assert (binding->type == decl);
380 binding->type = NULL_TREE;
383 if (!binding->value && !binding->type)
385 /* We're completely done with the innermost binding for this
386 identifier. Unhook it from the list of bindings. */
387 IDENTIFIER_BINDING (id) = binding->previous;
389 /* Add it to the free list. */
390 cxx_binding_free (binding);
394 /* Remove the bindings for the decls of the current level and leave
395 the current scope. */
397 void
398 pop_bindings_and_leave_scope (void)
400 for (tree t = getdecls (); t; t = DECL_CHAIN (t))
401 pop_binding (DECL_NAME (t), t);
402 leave_scope ();
405 /* Strip non dependent using declarations. If DECL is dependent,
406 surreptitiously create a typename_type and return it. */
408 tree
409 strip_using_decl (tree decl)
411 if (decl == NULL_TREE)
412 return NULL_TREE;
414 while (TREE_CODE (decl) == USING_DECL && !DECL_DEPENDENT_P (decl))
415 decl = USING_DECL_DECLS (decl);
417 if (TREE_CODE (decl) == USING_DECL && DECL_DEPENDENT_P (decl)
418 && USING_DECL_TYPENAME_P (decl))
420 /* We have found a type introduced by a using
421 declaration at class scope that refers to a dependent
422 type.
424 using typename :: [opt] nested-name-specifier unqualified-id ;
426 decl = make_typename_type (TREE_TYPE (decl),
427 DECL_NAME (decl),
428 typename_type, tf_error);
429 if (decl != error_mark_node)
430 decl = TYPE_NAME (decl);
433 return decl;
436 /* BINDING records an existing declaration for a name in the current scope.
437 But, DECL is another declaration for that same identifier in the
438 same scope. This is the `struct stat' hack whereby a non-typedef
439 class name or enum-name can be bound at the same level as some other
440 kind of entity.
441 3.3.7/1
443 A class name (9.1) or enumeration name (7.2) can be hidden by the
444 name of an object, function, or enumerator declared in the same scope.
445 If a class or enumeration name and an object, function, or enumerator
446 are declared in the same scope (in any order) with the same name, the
447 class or enumeration name is hidden wherever the object, function, or
448 enumerator name is visible.
450 It's the responsibility of the caller to check that
451 inserting this name is valid here. Returns nonzero if the new binding
452 was successful. */
454 static bool
455 supplement_binding_1 (cxx_binding *binding, tree decl)
457 tree bval = binding->value;
458 bool ok = true;
459 tree target_bval = strip_using_decl (bval);
460 tree target_decl = strip_using_decl (decl);
462 if (TREE_CODE (target_decl) == TYPE_DECL && DECL_ARTIFICIAL (target_decl)
463 && target_decl != target_bval
464 && (TREE_CODE (target_bval) != TYPE_DECL
465 /* We allow pushing an enum multiple times in a class
466 template in order to handle late matching of underlying
467 type on an opaque-enum-declaration followed by an
468 enum-specifier. */
469 || (processing_template_decl
470 && TREE_CODE (TREE_TYPE (target_decl)) == ENUMERAL_TYPE
471 && TREE_CODE (TREE_TYPE (target_bval)) == ENUMERAL_TYPE
472 && (dependent_type_p (ENUM_UNDERLYING_TYPE
473 (TREE_TYPE (target_decl)))
474 || dependent_type_p (ENUM_UNDERLYING_TYPE
475 (TREE_TYPE (target_bval)))))))
476 /* The new name is the type name. */
477 binding->type = decl;
478 else if (/* TARGET_BVAL is null when push_class_level_binding moves
479 an inherited type-binding out of the way to make room
480 for a new value binding. */
481 !target_bval
482 /* TARGET_BVAL is error_mark_node when TARGET_DECL's name
483 has been used in a non-class scope prior declaration.
484 In that case, we should have already issued a
485 diagnostic; for graceful error recovery purpose, pretend
486 this was the intended declaration for that name. */
487 || target_bval == error_mark_node
488 /* If TARGET_BVAL is anticipated but has not yet been
489 declared, pretend it is not there at all. */
490 || (TREE_CODE (target_bval) == FUNCTION_DECL
491 && DECL_ANTICIPATED (target_bval)
492 && !DECL_HIDDEN_FRIEND_P (target_bval)))
493 binding->value = decl;
494 else if (TREE_CODE (target_bval) == TYPE_DECL
495 && DECL_ARTIFICIAL (target_bval)
496 && target_decl != target_bval
497 && (TREE_CODE (target_decl) != TYPE_DECL
498 || same_type_p (TREE_TYPE (target_decl),
499 TREE_TYPE (target_bval))))
501 /* The old binding was a type name. It was placed in
502 VALUE field because it was thought, at the point it was
503 declared, to be the only entity with such a name. Move the
504 type name into the type slot; it is now hidden by the new
505 binding. */
506 binding->type = bval;
507 binding->value = decl;
508 binding->value_is_inherited = false;
510 else if (TREE_CODE (target_bval) == TYPE_DECL
511 && TREE_CODE (target_decl) == TYPE_DECL
512 && DECL_NAME (target_decl) == DECL_NAME (target_bval)
513 && binding->scope->kind != sk_class
514 && (same_type_p (TREE_TYPE (target_decl), TREE_TYPE (target_bval))
515 /* If either type involves template parameters, we must
516 wait until instantiation. */
517 || uses_template_parms (TREE_TYPE (target_decl))
518 || uses_template_parms (TREE_TYPE (target_bval))))
519 /* We have two typedef-names, both naming the same type to have
520 the same name. In general, this is OK because of:
522 [dcl.typedef]
524 In a given scope, a typedef specifier can be used to redefine
525 the name of any type declared in that scope to refer to the
526 type to which it already refers.
528 However, in class scopes, this rule does not apply due to the
529 stricter language in [class.mem] prohibiting redeclarations of
530 members. */
531 ok = false;
532 /* There can be two block-scope declarations of the same variable,
533 so long as they are `extern' declarations. However, there cannot
534 be two declarations of the same static data member:
536 [class.mem]
538 A member shall not be declared twice in the
539 member-specification. */
540 else if (VAR_P (target_decl)
541 && VAR_P (target_bval)
542 && DECL_EXTERNAL (target_decl) && DECL_EXTERNAL (target_bval)
543 && !DECL_CLASS_SCOPE_P (target_decl))
545 duplicate_decls (decl, binding->value, /*newdecl_is_friend=*/false);
546 ok = false;
548 else if (TREE_CODE (decl) == NAMESPACE_DECL
549 && TREE_CODE (bval) == NAMESPACE_DECL
550 && DECL_NAMESPACE_ALIAS (decl)
551 && DECL_NAMESPACE_ALIAS (bval)
552 && ORIGINAL_NAMESPACE (bval) == ORIGINAL_NAMESPACE (decl))
553 /* [namespace.alias]
555 In a declarative region, a namespace-alias-definition can be
556 used to redefine a namespace-alias declared in that declarative
557 region to refer only to the namespace to which it already
558 refers. */
559 ok = false;
560 else if (maybe_remove_implicit_alias (bval))
562 /* There was a mangling compatibility alias using this mangled name,
563 but now we have a real decl that wants to use it instead. */
564 binding->value = decl;
566 else
568 if (!error_operand_p (bval))
569 diagnose_name_conflict (decl, bval);
570 ok = false;
573 return ok;
576 /* Diagnose a name conflict between DECL and BVAL. */
578 static void
579 diagnose_name_conflict (tree decl, tree bval)
581 if (TREE_CODE (decl) == TREE_CODE (bval)
582 && (TREE_CODE (decl) != TYPE_DECL
583 || (DECL_ARTIFICIAL (decl) && DECL_ARTIFICIAL (bval))
584 || (!DECL_ARTIFICIAL (decl) && !DECL_ARTIFICIAL (bval)))
585 && !is_overloaded_fn (decl))
586 error ("redeclaration of %q#D", decl);
587 else
588 error ("%q#D conflicts with a previous declaration", decl);
590 inform (location_of (bval), "previous declaration %q#D", bval);
593 /* Wrapper for supplement_binding_1. */
595 static bool
596 supplement_binding (cxx_binding *binding, tree decl)
598 bool ret;
599 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
600 ret = supplement_binding_1 (binding, decl);
601 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
602 return ret;
605 /* Add DECL to the list of things declared in B. */
607 static void
608 add_decl_to_level (tree decl, cp_binding_level *b)
610 /* We used to record virtual tables as if they were ordinary
611 variables, but no longer do so. */
612 gcc_assert (!(VAR_P (decl) && DECL_VIRTUAL_P (decl)));
614 if (TREE_CODE (decl) == NAMESPACE_DECL
615 && !DECL_NAMESPACE_ALIAS (decl))
617 DECL_CHAIN (decl) = b->namespaces;
618 b->namespaces = decl;
620 else
622 /* We build up the list in reverse order, and reverse it later if
623 necessary. */
624 TREE_CHAIN (decl) = b->names;
625 b->names = decl;
627 /* If appropriate, add decl to separate list of statics. We
628 include extern variables because they might turn out to be
629 static later. It's OK for this list to contain a few false
630 positives. */
631 if (b->kind == sk_namespace)
632 if ((VAR_P (decl)
633 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
634 || (TREE_CODE (decl) == FUNCTION_DECL
635 && (!TREE_PUBLIC (decl)
636 || decl_anon_ns_mem_p (decl)
637 || DECL_DECLARED_INLINE_P (decl))))
638 vec_safe_push (b->static_decls, decl);
642 /* Record a decl-node X as belonging to the current lexical scope.
643 Check for errors (such as an incompatible declaration for the same
644 name already seen in the same scope). IS_FRIEND is true if X is
645 declared as a friend.
647 Returns either X or an old decl for the same name.
648 If an old decl is returned, it may have been smashed
649 to agree with what X says. */
651 static tree
652 pushdecl_maybe_friend_1 (tree x, bool is_friend)
654 tree t;
655 tree name;
656 int need_new_binding;
658 if (x == error_mark_node)
659 return error_mark_node;
661 need_new_binding = 1;
663 if (DECL_TEMPLATE_PARM_P (x))
664 /* Template parameters have no context; they are not X::T even
665 when declared within a class or namespace. */
667 else
669 if (current_function_decl && x != current_function_decl
670 /* A local declaration for a function doesn't constitute
671 nesting. */
672 && TREE_CODE (x) != FUNCTION_DECL
673 /* A local declaration for an `extern' variable is in the
674 scope of the current namespace, not the current
675 function. */
676 && !(VAR_P (x) && DECL_EXTERNAL (x))
677 /* When parsing the parameter list of a function declarator,
678 don't set DECL_CONTEXT to an enclosing function. When we
679 push the PARM_DECLs in order to process the function body,
680 current_binding_level->this_entity will be set. */
681 && !(TREE_CODE (x) == PARM_DECL
682 && current_binding_level->kind == sk_function_parms
683 && current_binding_level->this_entity == NULL)
684 && !DECL_CONTEXT (x))
685 DECL_CONTEXT (x) = current_function_decl;
687 /* If this is the declaration for a namespace-scope function,
688 but the declaration itself is in a local scope, mark the
689 declaration. */
690 if (TREE_CODE (x) == FUNCTION_DECL
691 && DECL_NAMESPACE_SCOPE_P (x)
692 && current_function_decl
693 && x != current_function_decl)
694 DECL_LOCAL_FUNCTION_P (x) = 1;
697 name = DECL_NAME (x);
698 if (name)
700 int different_binding_level = 0;
702 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
703 name = TREE_OPERAND (name, 0);
705 /* In case this decl was explicitly namespace-qualified, look it
706 up in its namespace context. */
707 if (DECL_NAMESPACE_SCOPE_P (x) && namespace_bindings_p ())
708 t = namespace_binding (name, DECL_CONTEXT (x));
709 else
710 t = lookup_name_innermost_nonclass_level (name);
712 /* [basic.link] If there is a visible declaration of an entity
713 with linkage having the same name and type, ignoring entities
714 declared outside the innermost enclosing namespace scope, the
715 block scope declaration declares that same entity and
716 receives the linkage of the previous declaration. */
717 if (! t && current_function_decl && x != current_function_decl
718 && VAR_OR_FUNCTION_DECL_P (x)
719 && DECL_EXTERNAL (x))
721 /* Look in block scope. */
722 t = innermost_non_namespace_value (name);
723 /* Or in the innermost namespace. */
724 if (! t)
725 t = namespace_binding (name, DECL_CONTEXT (x));
726 /* Does it have linkage? Note that if this isn't a DECL, it's an
727 OVERLOAD, which is OK. */
728 if (t && DECL_P (t) && ! (TREE_STATIC (t) || DECL_EXTERNAL (t)))
729 t = NULL_TREE;
730 if (t)
731 different_binding_level = 1;
734 /* If we are declaring a function, and the result of name-lookup
735 was an OVERLOAD, look for an overloaded instance that is
736 actually the same as the function we are declaring. (If
737 there is one, we have to merge our declaration with the
738 previous declaration.) */
739 if (t && TREE_CODE (t) == OVERLOAD)
741 tree match;
743 if (TREE_CODE (x) == FUNCTION_DECL)
744 for (match = t; match; match = OVL_NEXT (match))
746 if (decls_match (OVL_CURRENT (match), x))
747 break;
749 else
750 /* Just choose one. */
751 match = t;
753 if (match)
754 t = OVL_CURRENT (match);
755 else
756 t = NULL_TREE;
759 if (t && t != error_mark_node)
761 if (different_binding_level)
763 if (decls_match (x, t))
764 /* The standard only says that the local extern
765 inherits linkage from the previous decl; in
766 particular, default args are not shared. Add
767 the decl into a hash table to make sure only
768 the previous decl in this case is seen by the
769 middle end. */
771 struct cxx_int_tree_map *h;
773 TREE_PUBLIC (x) = TREE_PUBLIC (t);
775 if (cp_function_chain->extern_decl_map == NULL)
776 cp_function_chain->extern_decl_map
777 = hash_table<cxx_int_tree_map_hasher>::create_ggc (20);
779 h = ggc_alloc<cxx_int_tree_map> ();
780 h->uid = DECL_UID (x);
781 h->to = t;
782 cxx_int_tree_map **loc = cp_function_chain->extern_decl_map
783 ->find_slot (h, INSERT);
784 *loc = h;
787 else if (TREE_CODE (t) == PARM_DECL)
789 /* Check for duplicate params. */
790 tree d = duplicate_decls (x, t, is_friend);
791 if (d)
792 return d;
794 else if ((DECL_EXTERN_C_FUNCTION_P (x)
795 || DECL_FUNCTION_TEMPLATE_P (x))
796 && is_overloaded_fn (t))
797 /* Don't do anything just yet. */;
798 else if (t == wchar_decl_node)
800 if (! DECL_IN_SYSTEM_HEADER (x))
801 pedwarn (input_location, OPT_Wpedantic, "redeclaration of %<wchar_t%> as %qT",
802 TREE_TYPE (x));
804 /* Throw away the redeclaration. */
805 return t;
807 else
809 tree olddecl = duplicate_decls (x, t, is_friend);
811 /* If the redeclaration failed, we can stop at this
812 point. */
813 if (olddecl == error_mark_node)
814 return error_mark_node;
816 if (olddecl)
818 if (TREE_CODE (t) == TYPE_DECL)
819 SET_IDENTIFIER_TYPE_VALUE (name, TREE_TYPE (t));
821 return t;
823 else if (DECL_MAIN_P (x) && TREE_CODE (t) == FUNCTION_DECL)
825 /* A redeclaration of main, but not a duplicate of the
826 previous one.
828 [basic.start.main]
830 This function shall not be overloaded. */
831 error ("invalid redeclaration of %q+D", t);
832 error ("as %qD", x);
833 /* We don't try to push this declaration since that
834 causes a crash. */
835 return x;
840 /* If x has C linkage-specification, (extern "C"),
841 lookup its binding, in case it's already bound to an object.
842 The lookup is done in all namespaces.
843 If we find an existing binding, make sure it has the same
844 exception specification as x, otherwise, bail in error [7.5, 7.6]. */
845 if ((TREE_CODE (x) == FUNCTION_DECL)
846 && DECL_EXTERN_C_P (x)
847 /* We should ignore declarations happening in system headers. */
848 && !DECL_ARTIFICIAL (x)
849 && !DECL_IN_SYSTEM_HEADER (x))
851 tree previous = lookup_extern_c_fun_in_all_ns (x);
852 if (previous
853 && !DECL_ARTIFICIAL (previous)
854 && !DECL_IN_SYSTEM_HEADER (previous)
855 && DECL_CONTEXT (previous) != DECL_CONTEXT (x))
857 /* In case either x or previous is declared to throw an exception,
858 make sure both exception specifications are equal. */
859 if (decls_match (x, previous))
861 tree x_exception_spec = NULL_TREE;
862 tree previous_exception_spec = NULL_TREE;
864 x_exception_spec =
865 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (x));
866 previous_exception_spec =
867 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (previous));
868 if (!comp_except_specs (previous_exception_spec,
869 x_exception_spec,
870 ce_normal))
872 pedwarn (input_location, 0,
873 "declaration of %q#D with C language linkage",
875 pedwarn (DECL_SOURCE_LOCATION (previous), 0,
876 "conflicts with previous declaration %q#D",
877 previous);
878 pedwarn (input_location, 0,
879 "due to different exception specifications");
880 return error_mark_node;
882 if (DECL_ASSEMBLER_NAME_SET_P (previous))
883 SET_DECL_ASSEMBLER_NAME (x,
884 DECL_ASSEMBLER_NAME (previous));
886 else
888 pedwarn (input_location, 0,
889 "declaration of %q#D with C language linkage", x);
890 pedwarn (DECL_SOURCE_LOCATION (previous), 0,
891 "conflicts with previous declaration %q#D",
892 previous);
897 check_template_shadow (x);
899 /* If this is a function conjured up by the back end, massage it
900 so it looks friendly. */
901 if (DECL_NON_THUNK_FUNCTION_P (x) && ! DECL_LANG_SPECIFIC (x))
903 retrofit_lang_decl (x);
904 SET_DECL_LANGUAGE (x, lang_c);
907 t = x;
908 if (DECL_NON_THUNK_FUNCTION_P (x) && ! DECL_FUNCTION_MEMBER_P (x))
910 t = push_overloaded_decl (x, PUSH_LOCAL, is_friend);
911 if (!namespace_bindings_p ())
912 /* We do not need to create a binding for this name;
913 push_overloaded_decl will have already done so if
914 necessary. */
915 need_new_binding = 0;
917 else if (DECL_FUNCTION_TEMPLATE_P (x) && DECL_NAMESPACE_SCOPE_P (x))
919 t = push_overloaded_decl (x, PUSH_GLOBAL, is_friend);
920 if (t == x)
921 add_decl_to_level (x, NAMESPACE_LEVEL (CP_DECL_CONTEXT (t)));
924 if (DECL_DECLARES_FUNCTION_P (t))
926 check_default_args (t);
928 if (is_friend && t == x && !flag_friend_injection)
930 /* This is a new friend declaration of a function or a
931 function template, so hide it from ordinary function
932 lookup. */
933 DECL_ANTICIPATED (t) = 1;
934 DECL_HIDDEN_FRIEND_P (t) = 1;
938 if (t != x || DECL_FUNCTION_TEMPLATE_P (t))
939 return t;
941 /* If declaring a type as a typedef, copy the type (unless we're
942 at line 0), and install this TYPE_DECL as the new type's typedef
943 name. See the extensive comment of set_underlying_type (). */
944 if (TREE_CODE (x) == TYPE_DECL)
946 tree type = TREE_TYPE (x);
948 if (DECL_IS_BUILTIN (x)
949 || (TREE_TYPE (x) != error_mark_node
950 && TYPE_NAME (type) != x
951 /* We don't want to copy the type when all we're
952 doing is making a TYPE_DECL for the purposes of
953 inlining. */
954 && (!TYPE_NAME (type)
955 || TYPE_NAME (type) != DECL_ABSTRACT_ORIGIN (x))))
956 set_underlying_type (x);
958 if (type != error_mark_node
959 && TYPE_IDENTIFIER (type))
960 set_identifier_type_value (DECL_NAME (x), x);
962 /* If this is a locally defined typedef in a function that
963 is not a template instantation, record it to implement
964 -Wunused-local-typedefs. */
965 if (!instantiating_current_function_p ())
966 record_locally_defined_typedef (x);
969 /* Multiple external decls of the same identifier ought to match.
971 We get warnings about inline functions where they are defined.
972 We get warnings about other functions from push_overloaded_decl.
974 Avoid duplicate warnings where they are used. */
975 if (TREE_PUBLIC (x) && TREE_CODE (x) != FUNCTION_DECL)
977 tree decl;
979 decl = IDENTIFIER_NAMESPACE_VALUE (name);
980 if (decl && TREE_CODE (decl) == OVERLOAD)
981 decl = OVL_FUNCTION (decl);
983 if (decl && decl != error_mark_node
984 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl))
985 /* If different sort of thing, we already gave an error. */
986 && TREE_CODE (decl) == TREE_CODE (x)
987 && !comptypes (TREE_TYPE (x), TREE_TYPE (decl),
988 COMPARE_REDECLARATION))
990 if (permerror (input_location, "type mismatch with previous "
991 "external decl of %q#D", x))
992 inform (DECL_SOURCE_LOCATION (decl),
993 "previous external decl of %q#D", decl);
997 /* This name is new in its binding level.
998 Install the new declaration and return it. */
999 if (namespace_bindings_p ())
1001 /* Install a global value. */
1003 /* If the first global decl has external linkage,
1004 warn if we later see static one. */
1005 if (IDENTIFIER_GLOBAL_VALUE (name) == NULL_TREE && TREE_PUBLIC (x))
1006 TREE_PUBLIC (name) = 1;
1008 /* Bind the name for the entity. */
1009 if (!(TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x)
1010 && t != NULL_TREE)
1011 && (TREE_CODE (x) == TYPE_DECL
1012 || VAR_P (x)
1013 || TREE_CODE (x) == NAMESPACE_DECL
1014 || TREE_CODE (x) == CONST_DECL
1015 || TREE_CODE (x) == TEMPLATE_DECL))
1016 SET_IDENTIFIER_NAMESPACE_VALUE (name, x);
1018 /* If new decl is `static' and an `extern' was seen previously,
1019 warn about it. */
1020 if (x != NULL_TREE && t != NULL_TREE && decls_match (x, t))
1021 warn_extern_redeclared_static (x, t);
1023 else
1025 /* Here to install a non-global value. */
1026 tree oldglobal = IDENTIFIER_NAMESPACE_VALUE (name);
1027 tree oldlocal = NULL_TREE;
1028 cp_binding_level *oldscope = NULL;
1029 cxx_binding *oldbinding = outer_binding (name, NULL, true);
1030 if (oldbinding)
1032 oldlocal = oldbinding->value;
1033 oldscope = oldbinding->scope;
1036 if (need_new_binding)
1038 push_local_binding (name, x, 0);
1039 /* Because push_local_binding will hook X on to the
1040 current_binding_level's name list, we don't want to
1041 do that again below. */
1042 need_new_binding = 0;
1045 /* If this is a TYPE_DECL, push it into the type value slot. */
1046 if (TREE_CODE (x) == TYPE_DECL)
1047 set_identifier_type_value (name, x);
1049 /* Clear out any TYPE_DECL shadowed by a namespace so that
1050 we won't think this is a type. The C struct hack doesn't
1051 go through namespaces. */
1052 if (TREE_CODE (x) == NAMESPACE_DECL)
1053 set_identifier_type_value (name, NULL_TREE);
1055 if (oldlocal)
1057 tree d = oldlocal;
1059 while (oldlocal
1060 && VAR_P (oldlocal)
1061 && DECL_DEAD_FOR_LOCAL (oldlocal))
1062 oldlocal = DECL_SHADOWED_FOR_VAR (oldlocal);
1064 if (oldlocal == NULL_TREE)
1065 oldlocal = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (d));
1068 /* If this is an extern function declaration, see if we
1069 have a global definition or declaration for the function. */
1070 if (oldlocal == NULL_TREE
1071 && DECL_EXTERNAL (x)
1072 && oldglobal != NULL_TREE
1073 && TREE_CODE (x) == FUNCTION_DECL
1074 && TREE_CODE (oldglobal) == FUNCTION_DECL)
1076 /* We have one. Their types must agree. */
1077 if (decls_match (x, oldglobal))
1078 /* OK */;
1079 else
1081 warning (0, "extern declaration of %q#D doesn%'t match", x);
1082 warning_at (DECL_SOURCE_LOCATION (oldglobal), 0,
1083 "global declaration %q#D", oldglobal);
1086 /* If we have a local external declaration,
1087 and no file-scope declaration has yet been seen,
1088 then if we later have a file-scope decl it must not be static. */
1089 if (oldlocal == NULL_TREE
1090 && oldglobal == NULL_TREE
1091 && DECL_EXTERNAL (x)
1092 && TREE_PUBLIC (x))
1093 TREE_PUBLIC (name) = 1;
1095 /* Don't complain about the parms we push and then pop
1096 while tentatively parsing a function declarator. */
1097 if (TREE_CODE (x) == PARM_DECL && DECL_CONTEXT (x) == NULL_TREE)
1098 /* Ignore. */;
1100 /* Warn if shadowing an argument at the top level of the body. */
1101 else if (oldlocal != NULL_TREE && !DECL_EXTERNAL (x)
1102 /* Inline decls shadow nothing. */
1103 && !DECL_FROM_INLINE (x)
1104 && (TREE_CODE (oldlocal) == PARM_DECL
1105 || VAR_P (oldlocal)
1106 /* If the old decl is a type decl, only warn if the
1107 old decl is an explicit typedef or if both the old
1108 and new decls are type decls. */
1109 || (TREE_CODE (oldlocal) == TYPE_DECL
1110 && (!DECL_ARTIFICIAL (oldlocal)
1111 || TREE_CODE (x) == TYPE_DECL)))
1112 /* Don't check for internally generated vars unless
1113 it's an implicit typedef (see create_implicit_typedef
1114 in decl.c) or anonymous union variable. */
1115 && (!DECL_ARTIFICIAL (x)
1116 || DECL_IMPLICIT_TYPEDEF_P (x)
1117 || (VAR_P (x) && DECL_ANON_UNION_VAR_P (x))))
1119 bool nowarn = false;
1121 /* Don't complain if it's from an enclosing function. */
1122 if (DECL_CONTEXT (oldlocal) == current_function_decl
1123 && TREE_CODE (x) != PARM_DECL
1124 && TREE_CODE (oldlocal) == PARM_DECL)
1126 /* Go to where the parms should be and see if we find
1127 them there. */
1128 cp_binding_level *b = current_binding_level->level_chain;
1130 if (FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
1131 /* Skip the ctor/dtor cleanup level. */
1132 b = b->level_chain;
1134 /* ARM $8.3 */
1135 if (b->kind == sk_function_parms)
1137 error ("declaration of %q#D shadows a parameter", x);
1138 nowarn = true;
1142 /* The local structure or class can't use parameters of
1143 the containing function anyway. */
1144 if (DECL_CONTEXT (oldlocal) != current_function_decl)
1146 cp_binding_level *scope = current_binding_level;
1147 tree context = DECL_CONTEXT (oldlocal);
1148 for (; scope; scope = scope->level_chain)
1150 if (scope->kind == sk_function_parms
1151 && scope->this_entity == context)
1152 break;
1153 if (scope->kind == sk_class
1154 && !LAMBDA_TYPE_P (scope->this_entity))
1156 nowarn = true;
1157 break;
1161 /* Error if redeclaring a local declared in a
1162 init-statement or in the condition of an if or
1163 switch statement when the new declaration is in the
1164 outermost block of the controlled statement.
1165 Redeclaring a variable from a for or while condition is
1166 detected elsewhere. */
1167 else if (VAR_P (oldlocal)
1168 && oldscope == current_binding_level->level_chain
1169 && (oldscope->kind == sk_cond
1170 || oldscope->kind == sk_for))
1172 error ("redeclaration of %q#D", x);
1173 inform (DECL_SOURCE_LOCATION (oldlocal),
1174 "%q#D previously declared here", oldlocal);
1175 nowarn = true;
1177 /* C++11:
1178 3.3.3/3: The name declared in an exception-declaration (...)
1179 shall not be redeclared in the outermost block of the handler.
1180 3.3.3/2: A parameter name shall not be redeclared (...) in
1181 the outermost block of any handler associated with a
1182 function-try-block.
1183 3.4.1/15: The function parameter names shall not be redeclared
1184 in the exception-declaration nor in the outermost block of a
1185 handler for the function-try-block. */
1186 else if ((VAR_P (oldlocal)
1187 && oldscope == current_binding_level->level_chain
1188 && oldscope->kind == sk_catch)
1189 || (TREE_CODE (oldlocal) == PARM_DECL
1190 && (current_binding_level->kind == sk_catch
1191 || (current_binding_level->level_chain->kind
1192 == sk_catch))
1193 && in_function_try_handler))
1195 if (permerror (input_location, "redeclaration of %q#D", x))
1196 inform (DECL_SOURCE_LOCATION (oldlocal),
1197 "%q#D previously declared here", oldlocal);
1198 nowarn = true;
1201 if ((warn_shadow
1202 || warn_shadow_local
1203 || warn_shadow_compatible_local)
1204 && !nowarn)
1206 bool warned;
1207 enum opt_code warning_code;
1208 /* If '-Wshadow=compatible-local' is specified without other
1209 -Wshadow= flags, we will warn only when the type of the
1210 shadowing variable (i.e. x) can be converted to that of
1211 the shadowed parameter (oldlocal). The reason why we only
1212 check if x's type can be converted to oldlocal's type
1213 (but not the other way around) is because when users
1214 accidentally shadow a parameter, more than often they
1215 would use the variable thinking (mistakenly) it's still
1216 the parameter. It would be rare that users would use the
1217 variable in the place that expects the parameter but
1218 thinking it's a new decl. */
1219 if (warn_shadow)
1220 warning_code = OPT_Wshadow;
1221 else if (can_convert (TREE_TYPE (oldlocal), TREE_TYPE (x),
1222 tf_none))
1223 warning_code = OPT_Wshadow_compatible_local;
1224 else
1225 warning_code = OPT_Wshadow_local;
1227 if (TREE_CODE (oldlocal) == PARM_DECL)
1228 warned = warning_at (input_location, warning_code,
1229 "declaration of %q#D shadows a parameter", x);
1230 else if (is_capture_proxy (oldlocal))
1231 warned = warning_at (input_location, warning_code,
1232 "declaration of %qD shadows a lambda capture",
1234 else
1235 warned = warning_at (input_location, warning_code,
1236 "declaration of %qD shadows a previous local",
1239 if (warned)
1240 inform (DECL_SOURCE_LOCATION (oldlocal),
1241 "shadowed declaration is here");
1245 /* Maybe warn if shadowing something else. */
1246 else if (warn_shadow && !DECL_EXTERNAL (x)
1247 /* No shadow warnings for internally generated vars unless
1248 it's an implicit typedef (see create_implicit_typedef
1249 in decl.c). */
1250 && (! DECL_ARTIFICIAL (x) || DECL_IMPLICIT_TYPEDEF_P (x))
1251 /* No shadow warnings for vars made for inlining. */
1252 && ! DECL_FROM_INLINE (x))
1254 tree member;
1256 if (nonlambda_method_basetype ())
1257 member = lookup_member (current_nonlambda_class_type (),
1258 name,
1259 /*protect=*/0,
1260 /*want_type=*/false,
1261 tf_warning_or_error);
1262 else
1263 member = NULL_TREE;
1265 if (member && !TREE_STATIC (member))
1267 if (BASELINK_P (member))
1268 member = BASELINK_FUNCTIONS (member);
1269 member = OVL_CURRENT (member);
1271 /* Do not warn if a variable shadows a function, unless
1272 the variable is a function or a pointer-to-function. */
1273 if (TREE_CODE (member) != FUNCTION_DECL
1274 || TREE_CODE (x) == FUNCTION_DECL
1275 || TYPE_PTRFN_P (TREE_TYPE (x))
1276 || TYPE_PTRMEMFUNC_P (TREE_TYPE (x)))
1278 if (warning_at (input_location, OPT_Wshadow,
1279 "declaration of %qD shadows a member of %qT",
1280 x, current_nonlambda_class_type ())
1281 && DECL_P (member))
1282 inform (DECL_SOURCE_LOCATION (member),
1283 "shadowed declaration is here");
1286 else if (oldglobal != NULL_TREE
1287 && (VAR_P (oldglobal)
1288 /* If the old decl is a type decl, only warn if the
1289 old decl is an explicit typedef or if both the
1290 old and new decls are type decls. */
1291 || (TREE_CODE (oldglobal) == TYPE_DECL
1292 && (!DECL_ARTIFICIAL (oldglobal)
1293 || TREE_CODE (x) == TYPE_DECL)))
1294 && !instantiating_current_function_p ())
1295 /* XXX shadow warnings in outer-more namespaces */
1297 if (warning_at (input_location, OPT_Wshadow,
1298 "declaration of %qD shadows a "
1299 "global declaration", x))
1300 inform (DECL_SOURCE_LOCATION (oldglobal),
1301 "shadowed declaration is here");
1306 if (VAR_P (x))
1307 maybe_register_incomplete_var (x);
1310 if (need_new_binding)
1311 add_decl_to_level (x,
1312 DECL_NAMESPACE_SCOPE_P (x)
1313 ? NAMESPACE_LEVEL (CP_DECL_CONTEXT (x))
1314 : current_binding_level);
1316 return x;
1319 /* Wrapper for pushdecl_maybe_friend_1. */
1321 tree
1322 pushdecl_maybe_friend (tree x, bool is_friend)
1324 tree ret;
1325 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
1326 ret = pushdecl_maybe_friend_1 (x, is_friend);
1327 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
1328 return ret;
1331 /* Record a decl-node X as belonging to the current lexical scope. */
1333 tree
1334 pushdecl (tree x)
1336 return pushdecl_maybe_friend (x, false);
1339 /* Enter DECL into the symbol table, if that's appropriate. Returns
1340 DECL, or a modified version thereof. */
1342 tree
1343 maybe_push_decl (tree decl)
1345 tree type = TREE_TYPE (decl);
1347 /* Add this decl to the current binding level, but not if it comes
1348 from another scope, e.g. a static member variable. TEM may equal
1349 DECL or it may be a previous decl of the same name. */
1350 if (decl == error_mark_node
1351 || (TREE_CODE (decl) != PARM_DECL
1352 && DECL_CONTEXT (decl) != NULL_TREE
1353 /* Definitions of namespace members outside their namespace are
1354 possible. */
1355 && !DECL_NAMESPACE_SCOPE_P (decl))
1356 || (TREE_CODE (decl) == TEMPLATE_DECL && !namespace_bindings_p ())
1357 || type == unknown_type_node
1358 /* The declaration of a template specialization does not affect
1359 the functions available for overload resolution, so we do not
1360 call pushdecl. */
1361 || (TREE_CODE (decl) == FUNCTION_DECL
1362 && DECL_TEMPLATE_SPECIALIZATION (decl)))
1363 return decl;
1364 else
1365 return pushdecl (decl);
1368 /* Bind DECL to ID in the current_binding_level, assumed to be a local
1369 binding level. If PUSH_USING is set in FLAGS, we know that DECL
1370 doesn't really belong to this binding level, that it got here
1371 through a using-declaration. */
1373 void
1374 push_local_binding (tree id, tree decl, int flags)
1376 cp_binding_level *b;
1378 /* Skip over any local classes. This makes sense if we call
1379 push_local_binding with a friend decl of a local class. */
1380 b = innermost_nonclass_level ();
1382 if (lookup_name_innermost_nonclass_level (id))
1384 /* Supplement the existing binding. */
1385 if (!supplement_binding (IDENTIFIER_BINDING (id), decl))
1386 /* It didn't work. Something else must be bound at this
1387 level. Do not add DECL to the list of things to pop
1388 later. */
1389 return;
1391 else
1392 /* Create a new binding. */
1393 push_binding (id, decl, b);
1395 if (TREE_CODE (decl) == OVERLOAD || (flags & PUSH_USING))
1396 /* We must put the OVERLOAD into a TREE_LIST since the
1397 TREE_CHAIN of an OVERLOAD is already used. Similarly for
1398 decls that got here through a using-declaration. */
1399 decl = build_tree_list (NULL_TREE, decl);
1401 /* And put DECL on the list of things declared by the current
1402 binding level. */
1403 add_decl_to_level (decl, b);
1406 /* Check to see whether or not DECL is a variable that would have been
1407 in scope under the ARM, but is not in scope under the ANSI/ISO
1408 standard. If so, issue an error message. If name lookup would
1409 work in both cases, but return a different result, this function
1410 returns the result of ANSI/ISO lookup. Otherwise, it returns
1411 DECL. */
1413 tree
1414 check_for_out_of_scope_variable (tree decl)
1416 tree shadowed;
1418 /* We only care about out of scope variables. */
1419 if (!(VAR_P (decl) && DECL_DEAD_FOR_LOCAL (decl)))
1420 return decl;
1422 shadowed = DECL_HAS_SHADOWED_FOR_VAR_P (decl)
1423 ? DECL_SHADOWED_FOR_VAR (decl) : NULL_TREE ;
1424 while (shadowed != NULL_TREE && VAR_P (shadowed)
1425 && DECL_DEAD_FOR_LOCAL (shadowed))
1426 shadowed = DECL_HAS_SHADOWED_FOR_VAR_P (shadowed)
1427 ? DECL_SHADOWED_FOR_VAR (shadowed) : NULL_TREE;
1428 if (!shadowed)
1429 shadowed = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (decl));
1430 if (shadowed)
1432 if (!DECL_ERROR_REPORTED (decl))
1434 warning (0, "name lookup of %qD changed", DECL_NAME (decl));
1435 warning_at (DECL_SOURCE_LOCATION (shadowed), 0,
1436 " matches this %qD under ISO standard rules",
1437 shadowed);
1438 warning_at (DECL_SOURCE_LOCATION (decl), 0,
1439 " matches this %qD under old rules", decl);
1440 DECL_ERROR_REPORTED (decl) = 1;
1442 return shadowed;
1445 /* If we have already complained about this declaration, there's no
1446 need to do it again. */
1447 if (DECL_ERROR_REPORTED (decl))
1448 return decl;
1450 DECL_ERROR_REPORTED (decl) = 1;
1452 if (TREE_TYPE (decl) == error_mark_node)
1453 return decl;
1455 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
1457 error ("name lookup of %qD changed for ISO %<for%> scoping",
1458 DECL_NAME (decl));
1459 error (" cannot use obsolete binding at %q+D because "
1460 "it has a destructor", decl);
1461 return error_mark_node;
1463 else
1465 permerror (input_location, "name lookup of %qD changed for ISO %<for%> scoping",
1466 DECL_NAME (decl));
1467 if (flag_permissive)
1468 permerror (DECL_SOURCE_LOCATION (decl),
1469 " using obsolete binding at %qD", decl);
1470 else
1472 static bool hint;
1473 if (!hint)
1475 inform (input_location, "(if you use %<-fpermissive%> G++ will accept your code)");
1476 hint = true;
1481 return decl;
1484 /* true means unconditionally make a BLOCK for the next level pushed. */
1486 static bool keep_next_level_flag;
1488 static int binding_depth = 0;
1490 static void
1491 indent (int depth)
1493 int i;
1495 for (i = 0; i < depth * 2; i++)
1496 putc (' ', stderr);
1499 /* Return a string describing the kind of SCOPE we have. */
1500 static const char *
1501 cp_binding_level_descriptor (cp_binding_level *scope)
1503 /* The order of this table must match the "scope_kind"
1504 enumerators. */
1505 static const char* scope_kind_names[] = {
1506 "block-scope",
1507 "cleanup-scope",
1508 "try-scope",
1509 "catch-scope",
1510 "for-scope",
1511 "function-parameter-scope",
1512 "class-scope",
1513 "namespace-scope",
1514 "template-parameter-scope",
1515 "template-explicit-spec-scope"
1517 const scope_kind kind = scope->explicit_spec_p
1518 ? sk_template_spec : scope->kind;
1520 return scope_kind_names[kind];
1523 /* Output a debugging information about SCOPE when performing
1524 ACTION at LINE. */
1525 static void
1526 cp_binding_level_debug (cp_binding_level *scope, int line, const char *action)
1528 const char *desc = cp_binding_level_descriptor (scope);
1529 if (scope->this_entity)
1530 verbatim ("%s %s(%E) %p %d\n", action, desc,
1531 scope->this_entity, (void *) scope, line);
1532 else
1533 verbatim ("%s %s %p %d\n", action, desc, (void *) scope, line);
1536 /* Return the estimated initial size of the hashtable of a NAMESPACE
1537 scope. */
1539 static inline size_t
1540 namespace_scope_ht_size (tree ns)
1542 tree name = DECL_NAME (ns);
1544 return name == std_identifier
1545 ? NAMESPACE_STD_HT_SIZE
1546 : (name == global_scope_name
1547 ? GLOBAL_SCOPE_HT_SIZE
1548 : NAMESPACE_ORDINARY_HT_SIZE);
1551 /* A chain of binding_level structures awaiting reuse. */
1553 static GTY((deletable)) cp_binding_level *free_binding_level;
1555 /* Insert SCOPE as the innermost binding level. */
1557 void
1558 push_binding_level (cp_binding_level *scope)
1560 /* Add it to the front of currently active scopes stack. */
1561 scope->level_chain = current_binding_level;
1562 current_binding_level = scope;
1563 keep_next_level_flag = false;
1565 if (ENABLE_SCOPE_CHECKING)
1567 scope->binding_depth = binding_depth;
1568 indent (binding_depth);
1569 cp_binding_level_debug (scope, LOCATION_LINE (input_location),
1570 "push");
1571 binding_depth++;
1575 /* Create a new KIND scope and make it the top of the active scopes stack.
1576 ENTITY is the scope of the associated C++ entity (namespace, class,
1577 function, C++0x enumeration); it is NULL otherwise. */
1579 cp_binding_level *
1580 begin_scope (scope_kind kind, tree entity)
1582 cp_binding_level *scope;
1584 /* Reuse or create a struct for this binding level. */
1585 if (!ENABLE_SCOPE_CHECKING && free_binding_level)
1587 scope = free_binding_level;
1588 free_binding_level = scope->level_chain;
1589 memset (scope, 0, sizeof (cp_binding_level));
1591 else
1592 scope = ggc_cleared_alloc<cp_binding_level> ();
1594 scope->this_entity = entity;
1595 scope->more_cleanups_ok = true;
1596 switch (kind)
1598 case sk_cleanup:
1599 scope->keep = true;
1600 break;
1602 case sk_template_spec:
1603 scope->explicit_spec_p = true;
1604 kind = sk_template_parms;
1605 /* Fall through. */
1606 case sk_template_parms:
1607 case sk_block:
1608 case sk_try:
1609 case sk_catch:
1610 case sk_for:
1611 case sk_cond:
1612 case sk_class:
1613 case sk_scoped_enum:
1614 case sk_function_parms:
1615 case sk_transaction:
1616 case sk_omp:
1617 scope->keep = keep_next_level_flag;
1618 break;
1620 case sk_namespace:
1621 NAMESPACE_LEVEL (entity) = scope;
1622 vec_alloc (scope->static_decls,
1623 (DECL_NAME (entity) == std_identifier
1624 || DECL_NAME (entity) == global_scope_name) ? 200 : 10);
1625 break;
1627 default:
1628 /* Should not happen. */
1629 gcc_unreachable ();
1630 break;
1632 scope->kind = kind;
1634 push_binding_level (scope);
1636 return scope;
1639 /* We're about to leave current scope. Pop the top of the stack of
1640 currently active scopes. Return the enclosing scope, now active. */
1642 cp_binding_level *
1643 leave_scope (void)
1645 cp_binding_level *scope = current_binding_level;
1647 if (scope->kind == sk_namespace && class_binding_level)
1648 current_binding_level = class_binding_level;
1650 /* We cannot leave a scope, if there are none left. */
1651 if (NAMESPACE_LEVEL (global_namespace))
1652 gcc_assert (!global_scope_p (scope));
1654 if (ENABLE_SCOPE_CHECKING)
1656 indent (--binding_depth);
1657 cp_binding_level_debug (scope, LOCATION_LINE (input_location),
1658 "leave");
1661 /* Move one nesting level up. */
1662 current_binding_level = scope->level_chain;
1664 /* Namespace-scopes are left most probably temporarily, not
1665 completely; they can be reopened later, e.g. in namespace-extension
1666 or any name binding activity that requires us to resume a
1667 namespace. For classes, we cache some binding levels. For other
1668 scopes, we just make the structure available for reuse. */
1669 if (scope->kind != sk_namespace
1670 && scope->kind != sk_class)
1672 scope->level_chain = free_binding_level;
1673 gcc_assert (!ENABLE_SCOPE_CHECKING
1674 || scope->binding_depth == binding_depth);
1675 free_binding_level = scope;
1678 if (scope->kind == sk_class)
1680 /* Reset DEFINING_CLASS_P to allow for reuse of a
1681 class-defining scope in a non-defining context. */
1682 scope->defining_class_p = 0;
1684 /* Find the innermost enclosing class scope, and reset
1685 CLASS_BINDING_LEVEL appropriately. */
1686 class_binding_level = NULL;
1687 for (scope = current_binding_level; scope; scope = scope->level_chain)
1688 if (scope->kind == sk_class)
1690 class_binding_level = scope;
1691 break;
1695 return current_binding_level;
1698 static void
1699 resume_scope (cp_binding_level* b)
1701 /* Resuming binding levels is meant only for namespaces,
1702 and those cannot nest into classes. */
1703 gcc_assert (!class_binding_level);
1704 /* Also, resuming a non-directly nested namespace is a no-no. */
1705 gcc_assert (b->level_chain == current_binding_level);
1706 current_binding_level = b;
1707 if (ENABLE_SCOPE_CHECKING)
1709 b->binding_depth = binding_depth;
1710 indent (binding_depth);
1711 cp_binding_level_debug (b, LOCATION_LINE (input_location), "resume");
1712 binding_depth++;
1716 /* Return the innermost binding level that is not for a class scope. */
1718 static cp_binding_level *
1719 innermost_nonclass_level (void)
1721 cp_binding_level *b;
1723 b = current_binding_level;
1724 while (b->kind == sk_class)
1725 b = b->level_chain;
1727 return b;
1730 /* We're defining an object of type TYPE. If it needs a cleanup, but
1731 we're not allowed to add any more objects with cleanups to the current
1732 scope, create a new binding level. */
1734 void
1735 maybe_push_cleanup_level (tree type)
1737 if (type != error_mark_node
1738 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
1739 && current_binding_level->more_cleanups_ok == 0)
1741 begin_scope (sk_cleanup, NULL);
1742 current_binding_level->statement_list = push_stmt_list ();
1746 /* Return true if we are in the global binding level. */
1748 bool
1749 global_bindings_p (void)
1751 return global_scope_p (current_binding_level);
1754 /* True if we are currently in a toplevel binding level. This
1755 means either the global binding level or a namespace in a toplevel
1756 binding level. Since there are no non-toplevel namespace levels,
1757 this really means any namespace or template parameter level. We
1758 also include a class whose context is toplevel. */
1760 bool
1761 toplevel_bindings_p (void)
1763 cp_binding_level *b = innermost_nonclass_level ();
1765 return b->kind == sk_namespace || b->kind == sk_template_parms;
1768 /* True if this is a namespace scope, or if we are defining a class
1769 which is itself at namespace scope, or whose enclosing class is
1770 such a class, etc. */
1772 bool
1773 namespace_bindings_p (void)
1775 cp_binding_level *b = innermost_nonclass_level ();
1777 return b->kind == sk_namespace;
1780 /* True if the innermost non-class scope is a block scope. */
1782 bool
1783 local_bindings_p (void)
1785 cp_binding_level *b = innermost_nonclass_level ();
1786 return b->kind < sk_function_parms || b->kind == sk_omp;
1789 /* True if the current level needs to have a BLOCK made. */
1791 bool
1792 kept_level_p (void)
1794 return (current_binding_level->blocks != NULL_TREE
1795 || current_binding_level->keep
1796 || current_binding_level->kind == sk_cleanup
1797 || current_binding_level->names != NULL_TREE
1798 || current_binding_level->using_directives);
1801 /* Returns the kind of the innermost scope. */
1803 scope_kind
1804 innermost_scope_kind (void)
1806 return current_binding_level->kind;
1809 /* Returns true if this scope was created to store template parameters. */
1811 bool
1812 template_parm_scope_p (void)
1814 return innermost_scope_kind () == sk_template_parms;
1817 /* If KEEP is true, make a BLOCK node for the next binding level,
1818 unconditionally. Otherwise, use the normal logic to decide whether
1819 or not to create a BLOCK. */
1821 void
1822 keep_next_level (bool keep)
1824 keep_next_level_flag = keep;
1827 /* Return the list of declarations of the current level.
1828 Note that this list is in reverse order unless/until
1829 you nreverse it; and when you do nreverse it, you must
1830 store the result back using `storedecls' or you will lose. */
1832 tree
1833 getdecls (void)
1835 return current_binding_level->names;
1838 /* Return how many function prototypes we are currently nested inside. */
1841 function_parm_depth (void)
1843 int level = 0;
1844 cp_binding_level *b;
1846 for (b = current_binding_level;
1847 b->kind == sk_function_parms;
1848 b = b->level_chain)
1849 ++level;
1851 return level;
1854 /* For debugging. */
1855 static int no_print_functions = 0;
1856 static int no_print_builtins = 0;
1858 static void
1859 print_binding_level (cp_binding_level* lvl)
1861 tree t;
1862 int i = 0, len;
1863 fprintf (stderr, " blocks=%p", (void *) lvl->blocks);
1864 if (lvl->more_cleanups_ok)
1865 fprintf (stderr, " more-cleanups-ok");
1866 if (lvl->have_cleanups)
1867 fprintf (stderr, " have-cleanups");
1868 fprintf (stderr, "\n");
1869 if (lvl->names)
1871 fprintf (stderr, " names:\t");
1872 /* We can probably fit 3 names to a line? */
1873 for (t = lvl->names; t; t = TREE_CHAIN (t))
1875 if (no_print_functions && (TREE_CODE (t) == FUNCTION_DECL))
1876 continue;
1877 if (no_print_builtins
1878 && (TREE_CODE (t) == TYPE_DECL)
1879 && DECL_IS_BUILTIN (t))
1880 continue;
1882 /* Function decls tend to have longer names. */
1883 if (TREE_CODE (t) == FUNCTION_DECL)
1884 len = 3;
1885 else
1886 len = 2;
1887 i += len;
1888 if (i > 6)
1890 fprintf (stderr, "\n\t");
1891 i = len;
1893 print_node_brief (stderr, "", t, 0);
1894 if (t == error_mark_node)
1895 break;
1897 if (i)
1898 fprintf (stderr, "\n");
1900 if (vec_safe_length (lvl->class_shadowed))
1902 size_t i;
1903 cp_class_binding *b;
1904 fprintf (stderr, " class-shadowed:");
1905 FOR_EACH_VEC_ELT (*lvl->class_shadowed, i, b)
1906 fprintf (stderr, " %s ", IDENTIFIER_POINTER (b->identifier));
1907 fprintf (stderr, "\n");
1909 if (lvl->type_shadowed)
1911 fprintf (stderr, " type-shadowed:");
1912 for (t = lvl->type_shadowed; t; t = TREE_CHAIN (t))
1914 fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
1916 fprintf (stderr, "\n");
1920 DEBUG_FUNCTION void
1921 debug (cp_binding_level &ref)
1923 print_binding_level (&ref);
1926 DEBUG_FUNCTION void
1927 debug (cp_binding_level *ptr)
1929 if (ptr)
1930 debug (*ptr);
1931 else
1932 fprintf (stderr, "<nil>\n");
1936 void
1937 print_other_binding_stack (cp_binding_level *stack)
1939 cp_binding_level *level;
1940 for (level = stack; !global_scope_p (level); level = level->level_chain)
1942 fprintf (stderr, "binding level %p\n", (void *) level);
1943 print_binding_level (level);
1947 void
1948 print_binding_stack (void)
1950 cp_binding_level *b;
1951 fprintf (stderr, "current_binding_level=%p\n"
1952 "class_binding_level=%p\n"
1953 "NAMESPACE_LEVEL (global_namespace)=%p\n",
1954 (void *) current_binding_level, (void *) class_binding_level,
1955 (void *) NAMESPACE_LEVEL (global_namespace));
1956 if (class_binding_level)
1958 for (b = class_binding_level; b; b = b->level_chain)
1959 if (b == current_binding_level)
1960 break;
1961 if (b)
1962 b = class_binding_level;
1963 else
1964 b = current_binding_level;
1966 else
1967 b = current_binding_level;
1968 print_other_binding_stack (b);
1969 fprintf (stderr, "global:\n");
1970 print_binding_level (NAMESPACE_LEVEL (global_namespace));
1973 /* Return the type associated with ID. */
1975 static tree
1976 identifier_type_value_1 (tree id)
1978 /* There is no type with that name, anywhere. */
1979 if (REAL_IDENTIFIER_TYPE_VALUE (id) == NULL_TREE)
1980 return NULL_TREE;
1981 /* This is not the type marker, but the real thing. */
1982 if (REAL_IDENTIFIER_TYPE_VALUE (id) != global_type_node)
1983 return REAL_IDENTIFIER_TYPE_VALUE (id);
1984 /* Have to search for it. It must be on the global level, now.
1985 Ask lookup_name not to return non-types. */
1986 id = lookup_name_real (id, 2, 1, /*block_p=*/true, 0, 0);
1987 if (id)
1988 return TREE_TYPE (id);
1989 return NULL_TREE;
1992 /* Wrapper for identifier_type_value_1. */
1994 tree
1995 identifier_type_value (tree id)
1997 tree ret;
1998 timevar_start (TV_NAME_LOOKUP);
1999 ret = identifier_type_value_1 (id);
2000 timevar_stop (TV_NAME_LOOKUP);
2001 return ret;
2005 /* Return the IDENTIFIER_GLOBAL_VALUE of T, for use in common code, since
2006 the definition of IDENTIFIER_GLOBAL_VALUE is different for C and C++. */
2008 tree
2009 identifier_global_value (tree t)
2011 return IDENTIFIER_GLOBAL_VALUE (t);
2014 /* Push a definition of struct, union or enum tag named ID. into
2015 binding_level B. DECL is a TYPE_DECL for the type. We assume that
2016 the tag ID is not already defined. */
2018 static void
2019 set_identifier_type_value_with_scope (tree id, tree decl, cp_binding_level *b)
2021 tree type;
2023 if (b->kind != sk_namespace)
2025 /* Shadow the marker, not the real thing, so that the marker
2026 gets restored later. */
2027 tree old_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
2028 b->type_shadowed
2029 = tree_cons (id, old_type_value, b->type_shadowed);
2030 type = decl ? TREE_TYPE (decl) : NULL_TREE;
2031 TREE_TYPE (b->type_shadowed) = type;
2033 else
2035 cxx_binding *binding =
2036 binding_for_name (NAMESPACE_LEVEL (current_namespace), id);
2037 gcc_assert (decl);
2038 if (binding->value)
2039 supplement_binding (binding, decl);
2040 else
2041 binding->value = decl;
2043 /* Store marker instead of real type. */
2044 type = global_type_node;
2046 SET_IDENTIFIER_TYPE_VALUE (id, type);
2049 /* As set_identifier_type_value_with_scope, but using
2050 current_binding_level. */
2052 void
2053 set_identifier_type_value (tree id, tree decl)
2055 set_identifier_type_value_with_scope (id, decl, current_binding_level);
2058 /* Return the name for the constructor (or destructor) for the
2059 specified class TYPE. When given a template, this routine doesn't
2060 lose the specialization. */
2062 static inline tree
2063 constructor_name_full (tree type)
2065 return TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
2068 /* Return the name for the constructor (or destructor) for the
2069 specified class. When given a template, return the plain
2070 unspecialized name. */
2072 tree
2073 constructor_name (tree type)
2075 tree name;
2076 name = constructor_name_full (type);
2077 if (IDENTIFIER_TEMPLATE (name))
2078 name = IDENTIFIER_TEMPLATE (name);
2079 return name;
2082 /* Returns TRUE if NAME is the name for the constructor for TYPE,
2083 which must be a class type. */
2085 bool
2086 constructor_name_p (tree name, tree type)
2088 tree ctor_name;
2090 gcc_assert (MAYBE_CLASS_TYPE_P (type));
2092 if (!name)
2093 return false;
2095 if (!identifier_p (name))
2096 return false;
2098 /* These don't have names. */
2099 if (TREE_CODE (type) == DECLTYPE_TYPE
2100 || TREE_CODE (type) == TYPEOF_TYPE)
2101 return false;
2103 ctor_name = constructor_name_full (type);
2104 if (name == ctor_name)
2105 return true;
2106 if (IDENTIFIER_TEMPLATE (ctor_name)
2107 && name == IDENTIFIER_TEMPLATE (ctor_name))
2108 return true;
2109 return false;
2112 /* Counter used to create anonymous type names. */
2114 static GTY(()) int anon_cnt;
2116 /* Return an IDENTIFIER which can be used as a name for
2117 unnamed structs and unions. */
2119 tree
2120 make_anon_name (void)
2122 char buf[32];
2124 sprintf (buf, anon_aggrname_format (), anon_cnt++);
2125 return get_identifier (buf);
2128 /* This code is practically identical to that for creating
2129 anonymous names, but is just used for lambdas instead. This isn't really
2130 necessary, but it's convenient to avoid treating lambdas like other
2131 unnamed types. */
2133 static GTY(()) int lambda_cnt = 0;
2135 tree
2136 make_lambda_name (void)
2138 char buf[32];
2140 sprintf (buf, LAMBDANAME_FORMAT, lambda_cnt++);
2141 return get_identifier (buf);
2144 /* Return (from the stack of) the BINDING, if any, established at SCOPE. */
2146 static inline cxx_binding *
2147 find_binding (cp_binding_level *scope, cxx_binding *binding)
2149 for (; binding != NULL; binding = binding->previous)
2150 if (binding->scope == scope)
2151 return binding;
2153 return (cxx_binding *)0;
2156 /* Return the binding for NAME in SCOPE, if any. Otherwise, return NULL. */
2158 static inline cxx_binding *
2159 cp_binding_level_find_binding_for_name (cp_binding_level *scope, tree name)
2161 cxx_binding *b = IDENTIFIER_NAMESPACE_BINDINGS (name);
2162 if (b)
2164 /* Fold-in case where NAME is used only once. */
2165 if (scope == b->scope && b->previous == NULL)
2166 return b;
2167 return find_binding (scope, b);
2169 return NULL;
2172 /* Always returns a binding for name in scope. If no binding is
2173 found, make a new one. */
2175 static cxx_binding *
2176 binding_for_name (cp_binding_level *scope, tree name)
2178 cxx_binding *result;
2180 result = cp_binding_level_find_binding_for_name (scope, name);
2181 if (result)
2182 return result;
2183 /* Not found, make a new one. */
2184 result = cxx_binding_make (NULL, NULL);
2185 result->previous = IDENTIFIER_NAMESPACE_BINDINGS (name);
2186 result->scope = scope;
2187 result->is_local = false;
2188 result->value_is_inherited = false;
2189 IDENTIFIER_NAMESPACE_BINDINGS (name) = result;
2190 return result;
2193 /* Walk through the bindings associated to the name of FUNCTION,
2194 and return the first declaration of a function with a
2195 "C" linkage specification, a.k.a 'extern "C"'.
2196 This function looks for the binding, regardless of which scope it
2197 has been defined in. It basically looks in all the known scopes.
2198 Note that this function does not lookup for bindings of builtin functions
2199 or for functions declared in system headers. */
2200 static tree
2201 lookup_extern_c_fun_in_all_ns (tree function)
2203 tree name;
2204 cxx_binding *iter;
2206 gcc_assert (function && TREE_CODE (function) == FUNCTION_DECL);
2208 name = DECL_NAME (function);
2209 gcc_assert (name && identifier_p (name));
2211 for (iter = IDENTIFIER_NAMESPACE_BINDINGS (name);
2212 iter;
2213 iter = iter->previous)
2215 tree ovl;
2216 for (ovl = iter->value; ovl; ovl = OVL_NEXT (ovl))
2218 tree decl = OVL_CURRENT (ovl);
2219 if (decl
2220 && TREE_CODE (decl) == FUNCTION_DECL
2221 && DECL_EXTERN_C_P (decl)
2222 && !DECL_ARTIFICIAL (decl))
2224 return decl;
2228 return NULL;
2231 /* Returns a list of C-linkage decls with the name NAME. */
2233 tree
2234 c_linkage_bindings (tree name)
2236 tree decls = NULL_TREE;
2237 cxx_binding *iter;
2239 for (iter = IDENTIFIER_NAMESPACE_BINDINGS (name);
2240 iter;
2241 iter = iter->previous)
2243 tree ovl;
2244 for (ovl = iter->value; ovl; ovl = OVL_NEXT (ovl))
2246 tree decl = OVL_CURRENT (ovl);
2247 if (decl
2248 && DECL_EXTERN_C_P (decl)
2249 && !DECL_ARTIFICIAL (decl))
2251 if (decls == NULL_TREE)
2252 decls = decl;
2253 else
2254 decls = tree_cons (NULL_TREE, decl, decls);
2258 return decls;
2261 /* Insert another USING_DECL into the current binding level, returning
2262 this declaration. If this is a redeclaration, do nothing, and
2263 return NULL_TREE if this not in namespace scope (in namespace
2264 scope, a using decl might extend any previous bindings). */
2266 static tree
2267 push_using_decl_1 (tree scope, tree name)
2269 tree decl;
2271 gcc_assert (TREE_CODE (scope) == NAMESPACE_DECL);
2272 gcc_assert (identifier_p (name));
2273 for (decl = current_binding_level->usings; decl; decl = DECL_CHAIN (decl))
2274 if (USING_DECL_SCOPE (decl) == scope && DECL_NAME (decl) == name)
2275 break;
2276 if (decl)
2277 return namespace_bindings_p () ? decl : NULL_TREE;
2278 decl = build_lang_decl (USING_DECL, name, NULL_TREE);
2279 USING_DECL_SCOPE (decl) = scope;
2280 DECL_CHAIN (decl) = current_binding_level->usings;
2281 current_binding_level->usings = decl;
2282 return decl;
2285 /* Wrapper for push_using_decl_1. */
2287 static tree
2288 push_using_decl (tree scope, tree name)
2290 tree ret;
2291 timevar_start (TV_NAME_LOOKUP);
2292 ret = push_using_decl_1 (scope, name);
2293 timevar_stop (TV_NAME_LOOKUP);
2294 return ret;
2297 /* Same as pushdecl, but define X in binding-level LEVEL. We rely on the
2298 caller to set DECL_CONTEXT properly.
2300 Note that this must only be used when X will be the new innermost
2301 binding for its name, as we tack it onto the front of IDENTIFIER_BINDING
2302 without checking to see if the current IDENTIFIER_BINDING comes from a
2303 closer binding level than LEVEL. */
2305 static tree
2306 pushdecl_with_scope_1 (tree x, cp_binding_level *level, bool is_friend)
2308 cp_binding_level *b;
2309 tree function_decl = current_function_decl;
2311 current_function_decl = NULL_TREE;
2312 if (level->kind == sk_class)
2314 b = class_binding_level;
2315 class_binding_level = level;
2316 pushdecl_class_level (x);
2317 class_binding_level = b;
2319 else
2321 b = current_binding_level;
2322 current_binding_level = level;
2323 x = pushdecl_maybe_friend (x, is_friend);
2324 current_binding_level = b;
2326 current_function_decl = function_decl;
2327 return x;
2330 /* Wrapper for pushdecl_with_scope_1. */
2332 tree
2333 pushdecl_with_scope (tree x, cp_binding_level *level, bool is_friend)
2335 tree ret;
2336 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
2337 ret = pushdecl_with_scope_1 (x, level, is_friend);
2338 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
2339 return ret;
2342 /* Helper function for push_overloaded_decl_1 and do_nonmember_using_decl.
2343 Compares the parameter-type-lists of DECL1 and DECL2 and returns false
2344 if they are different. If the DECLs are template functions, the return
2345 types and the template parameter lists are compared too (DR 565). */
2347 static bool
2348 compparms_for_decl_and_using_decl (tree decl1, tree decl2)
2350 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (decl1)),
2351 TYPE_ARG_TYPES (TREE_TYPE (decl2))))
2352 return false;
2354 if (! DECL_FUNCTION_TEMPLATE_P (decl1)
2355 || ! DECL_FUNCTION_TEMPLATE_P (decl2))
2356 return true;
2358 return (comp_template_parms (DECL_TEMPLATE_PARMS (decl1),
2359 DECL_TEMPLATE_PARMS (decl2))
2360 && same_type_p (TREE_TYPE (TREE_TYPE (decl1)),
2361 TREE_TYPE (TREE_TYPE (decl2))));
2364 /* DECL is a FUNCTION_DECL for a non-member function, which may have
2365 other definitions already in place. We get around this by making
2366 the value of the identifier point to a list of all the things that
2367 want to be referenced by that name. It is then up to the users of
2368 that name to decide what to do with that list.
2370 DECL may also be a TEMPLATE_DECL, with a FUNCTION_DECL in its
2371 DECL_TEMPLATE_RESULT. It is dealt with the same way.
2373 FLAGS is a bitwise-or of the following values:
2374 PUSH_LOCAL: Bind DECL in the current scope, rather than at
2375 namespace scope.
2376 PUSH_USING: DECL is being pushed as the result of a using
2377 declaration.
2379 IS_FRIEND is true if this is a friend declaration.
2381 The value returned may be a previous declaration if we guessed wrong
2382 about what language DECL should belong to (C or C++). Otherwise,
2383 it's always DECL (and never something that's not a _DECL). */
2385 static tree
2386 push_overloaded_decl_1 (tree decl, int flags, bool is_friend)
2388 tree name = DECL_NAME (decl);
2389 tree old;
2390 tree new_binding;
2391 int doing_global = (namespace_bindings_p () || !(flags & PUSH_LOCAL));
2393 if (doing_global)
2394 old = namespace_binding (name, DECL_CONTEXT (decl));
2395 else
2396 old = lookup_name_innermost_nonclass_level (name);
2398 if (old)
2400 if (TREE_CODE (old) == TYPE_DECL && DECL_ARTIFICIAL (old))
2402 tree t = TREE_TYPE (old);
2403 if (MAYBE_CLASS_TYPE_P (t) && warn_shadow
2404 && (! DECL_IN_SYSTEM_HEADER (decl)
2405 || ! DECL_IN_SYSTEM_HEADER (old)))
2406 warning (OPT_Wshadow, "%q#D hides constructor for %q#T", decl, t);
2407 old = NULL_TREE;
2409 else if (is_overloaded_fn (old))
2411 tree tmp;
2413 for (tmp = old; tmp; tmp = OVL_NEXT (tmp))
2415 tree fn = OVL_CURRENT (tmp);
2416 tree dup;
2418 if (TREE_CODE (tmp) == OVERLOAD && OVL_USED (tmp)
2419 && !(flags & PUSH_USING)
2420 && compparms_for_decl_and_using_decl (fn, decl)
2421 && ! decls_match (fn, decl))
2422 diagnose_name_conflict (decl, fn);
2424 dup = duplicate_decls (decl, fn, is_friend);
2425 /* If DECL was a redeclaration of FN -- even an invalid
2426 one -- pass that information along to our caller. */
2427 if (dup == fn || dup == error_mark_node)
2428 return dup;
2431 /* We don't overload implicit built-ins. duplicate_decls()
2432 may fail to merge the decls if the new decl is e.g. a
2433 template function. */
2434 if (TREE_CODE (old) == FUNCTION_DECL
2435 && DECL_ANTICIPATED (old)
2436 && !DECL_HIDDEN_FRIEND_P (old))
2437 old = NULL;
2439 else if (old == error_mark_node)
2440 /* Ignore the undefined symbol marker. */
2441 old = NULL_TREE;
2442 else
2444 error ("previous non-function declaration %q+#D", old);
2445 error ("conflicts with function declaration %q#D", decl);
2446 return decl;
2450 if (old || TREE_CODE (decl) == TEMPLATE_DECL
2451 /* If it's a using declaration, we always need to build an OVERLOAD,
2452 because it's the only way to remember that the declaration comes
2453 from 'using', and have the lookup behave correctly. */
2454 || (flags & PUSH_USING))
2456 if (old && TREE_CODE (old) != OVERLOAD)
2457 new_binding = ovl_cons (decl, ovl_cons (old, NULL_TREE));
2458 else
2459 new_binding = ovl_cons (decl, old);
2460 if (flags & PUSH_USING)
2461 OVL_USED (new_binding) = 1;
2463 else
2464 /* NAME is not ambiguous. */
2465 new_binding = decl;
2467 if (doing_global)
2468 set_namespace_binding (name, current_namespace, new_binding);
2469 else
2471 /* We only create an OVERLOAD if there was a previous binding at
2472 this level, or if decl is a template. In the former case, we
2473 need to remove the old binding and replace it with the new
2474 binding. We must also run through the NAMES on the binding
2475 level where the name was bound to update the chain. */
2477 if (TREE_CODE (new_binding) == OVERLOAD && old)
2479 tree *d;
2481 for (d = &IDENTIFIER_BINDING (name)->scope->names;
2483 d = &TREE_CHAIN (*d))
2484 if (*d == old
2485 || (TREE_CODE (*d) == TREE_LIST
2486 && TREE_VALUE (*d) == old))
2488 if (TREE_CODE (*d) == TREE_LIST)
2489 /* Just replace the old binding with the new. */
2490 TREE_VALUE (*d) = new_binding;
2491 else
2492 /* Build a TREE_LIST to wrap the OVERLOAD. */
2493 *d = tree_cons (NULL_TREE, new_binding,
2494 TREE_CHAIN (*d));
2496 /* And update the cxx_binding node. */
2497 IDENTIFIER_BINDING (name)->value = new_binding;
2498 return decl;
2501 /* We should always find a previous binding in this case. */
2502 gcc_unreachable ();
2505 /* Install the new binding. */
2506 push_local_binding (name, new_binding, flags);
2509 return decl;
2512 /* Wrapper for push_overloaded_decl_1. */
2514 static tree
2515 push_overloaded_decl (tree decl, int flags, bool is_friend)
2517 tree ret;
2518 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
2519 ret = push_overloaded_decl_1 (decl, flags, is_friend);
2520 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
2521 return ret;
2524 /* Check a non-member using-declaration. Return the name and scope
2525 being used, and the USING_DECL, or NULL_TREE on failure. */
2527 static tree
2528 validate_nonmember_using_decl (tree decl, tree scope, tree name)
2530 /* [namespace.udecl]
2531 A using-declaration for a class member shall be a
2532 member-declaration. */
2533 if (TYPE_P (scope))
2535 error ("%qT is not a namespace or unscoped enum", scope);
2536 return NULL_TREE;
2538 else if (scope == error_mark_node)
2539 return NULL_TREE;
2541 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR)
2543 /* 7.3.3/5
2544 A using-declaration shall not name a template-id. */
2545 error ("a using-declaration cannot specify a template-id. "
2546 "Try %<using %D%>", name);
2547 return NULL_TREE;
2550 if (TREE_CODE (decl) == NAMESPACE_DECL)
2552 error ("namespace %qD not allowed in using-declaration", decl);
2553 return NULL_TREE;
2556 if (TREE_CODE (decl) == SCOPE_REF)
2558 /* It's a nested name with template parameter dependent scope.
2559 This can only be using-declaration for class member. */
2560 error ("%qT is not a namespace", TREE_OPERAND (decl, 0));
2561 return NULL_TREE;
2564 if (is_overloaded_fn (decl))
2565 decl = get_first_fn (decl);
2567 gcc_assert (DECL_P (decl));
2569 /* Make a USING_DECL. */
2570 tree using_decl = push_using_decl (scope, name);
2572 if (using_decl == NULL_TREE
2573 && at_function_scope_p ()
2574 && VAR_P (decl))
2575 /* C++11 7.3.3/10. */
2576 error ("%qD is already declared in this scope", name);
2578 return using_decl;
2581 /* Process local and global using-declarations. */
2583 static void
2584 do_nonmember_using_decl (tree scope, tree name, tree oldval, tree oldtype,
2585 tree *newval, tree *newtype)
2587 struct scope_binding decls = EMPTY_SCOPE_BINDING;
2589 *newval = *newtype = NULL_TREE;
2590 if (!qualified_lookup_using_namespace (name, scope, &decls, 0))
2591 /* Lookup error */
2592 return;
2594 if (!decls.value && !decls.type)
2596 error ("%qD not declared", name);
2597 return;
2600 /* Shift the old and new bindings around so we're comparing class and
2601 enumeration names to each other. */
2602 if (oldval && DECL_IMPLICIT_TYPEDEF_P (oldval))
2604 oldtype = oldval;
2605 oldval = NULL_TREE;
2608 if (decls.value && DECL_IMPLICIT_TYPEDEF_P (decls.value))
2610 decls.type = decls.value;
2611 decls.value = NULL_TREE;
2614 if (decls.value)
2616 /* Check for using functions. */
2617 if (is_overloaded_fn (decls.value))
2619 tree tmp, tmp1;
2621 if (oldval && !is_overloaded_fn (oldval))
2623 error ("%qD is already declared in this scope", name);
2624 oldval = NULL_TREE;
2627 *newval = oldval;
2628 for (tmp = decls.value; tmp; tmp = OVL_NEXT (tmp))
2630 tree new_fn = OVL_CURRENT (tmp);
2632 /* Don't import functions that haven't been declared. */
2633 if (DECL_ANTICIPATED (new_fn))
2634 continue;
2636 /* [namespace.udecl]
2638 If a function declaration in namespace scope or block
2639 scope has the same name and the same parameter types as a
2640 function introduced by a using declaration the program is
2641 ill-formed. */
2642 for (tmp1 = oldval; tmp1; tmp1 = OVL_NEXT (tmp1))
2644 tree old_fn = OVL_CURRENT (tmp1);
2646 if (new_fn == old_fn)
2647 /* The function already exists in the current namespace. */
2648 break;
2649 else if (TREE_CODE (tmp1) == OVERLOAD && OVL_USED (tmp1))
2650 continue; /* this is a using decl */
2651 else if (compparms_for_decl_and_using_decl (new_fn, old_fn))
2653 /* There was already a non-using declaration in
2654 this scope with the same parameter types. If both
2655 are the same extern "C" functions, that's ok. */
2656 if (DECL_ANTICIPATED (old_fn)
2657 && !DECL_HIDDEN_FRIEND_P (old_fn))
2658 /* Ignore anticipated built-ins. */;
2659 else if (decls_match (new_fn, old_fn))
2660 break;
2661 else
2663 diagnose_name_conflict (new_fn, old_fn);
2664 break;
2669 /* If we broke out of the loop, there's no reason to add
2670 this function to the using declarations for this
2671 scope. */
2672 if (tmp1)
2673 continue;
2675 /* If we are adding to an existing OVERLOAD, then we no
2676 longer know the type of the set of functions. */
2677 if (*newval && TREE_CODE (*newval) == OVERLOAD)
2678 TREE_TYPE (*newval) = unknown_type_node;
2679 /* Add this new function to the set. */
2680 *newval = build_overload (OVL_CURRENT (tmp), *newval);
2681 /* If there is only one function, then we use its type. (A
2682 using-declaration naming a single function can be used in
2683 contexts where overload resolution cannot be
2684 performed.) */
2685 if (TREE_CODE (*newval) != OVERLOAD)
2687 *newval = ovl_cons (*newval, NULL_TREE);
2688 TREE_TYPE (*newval) = TREE_TYPE (OVL_CURRENT (tmp));
2690 OVL_USED (*newval) = 1;
2693 else
2695 /* If we're declaring a non-function and OLDVAL is an anticipated
2696 built-in, just pretend it isn't there. */
2697 if (oldval
2698 && TREE_CODE (oldval) == FUNCTION_DECL
2699 && DECL_ANTICIPATED (oldval)
2700 && !DECL_HIDDEN_FRIEND_P (oldval))
2701 oldval = NULL_TREE;
2703 *newval = decls.value;
2704 if (oldval && !decls_match (*newval, oldval))
2705 error ("%qD is already declared in this scope", name);
2708 else
2709 *newval = oldval;
2711 if (decls.type && TREE_CODE (decls.type) == TREE_LIST)
2713 error ("reference to %qD is ambiguous", name);
2714 print_candidates (decls.type);
2716 else
2718 *newtype = decls.type;
2719 if (oldtype && *newtype && !decls_match (oldtype, *newtype))
2720 error ("%qD is already declared in this scope", name);
2723 /* If *newval is empty, shift any class or enumeration name down. */
2724 if (!*newval)
2726 *newval = *newtype;
2727 *newtype = NULL_TREE;
2731 /* Process a using-declaration at function scope. */
2733 void
2734 do_local_using_decl (tree decl, tree scope, tree name)
2736 tree oldval, oldtype, newval, newtype;
2737 tree orig_decl = decl;
2739 decl = validate_nonmember_using_decl (decl, scope, name);
2740 if (decl == NULL_TREE)
2741 return;
2743 if (building_stmt_list_p ()
2744 && at_function_scope_p ())
2745 add_decl_expr (decl);
2747 oldval = lookup_name_innermost_nonclass_level (name);
2748 oldtype = lookup_type_current_level (name);
2750 do_nonmember_using_decl (scope, name, oldval, oldtype, &newval, &newtype);
2752 if (newval)
2754 if (is_overloaded_fn (newval))
2756 tree fn, term;
2758 /* We only need to push declarations for those functions
2759 that were not already bound in the current level.
2760 The old value might be NULL_TREE, it might be a single
2761 function, or an OVERLOAD. */
2762 if (oldval && TREE_CODE (oldval) == OVERLOAD)
2763 term = OVL_FUNCTION (oldval);
2764 else
2765 term = oldval;
2766 for (fn = newval; fn && OVL_CURRENT (fn) != term;
2767 fn = OVL_NEXT (fn))
2768 push_overloaded_decl (OVL_CURRENT (fn),
2769 PUSH_LOCAL | PUSH_USING,
2770 false);
2772 else
2773 push_local_binding (name, newval, PUSH_USING);
2775 if (newtype)
2777 push_local_binding (name, newtype, PUSH_USING);
2778 set_identifier_type_value (name, newtype);
2781 /* Emit debug info. */
2782 if (!processing_template_decl)
2783 cp_emit_debug_info_for_using (orig_decl, current_scope());
2786 /* Returns true if ROOT (a namespace, class, or function) encloses
2787 CHILD. CHILD may be either a class type or a namespace. */
2789 bool
2790 is_ancestor (tree root, tree child)
2792 gcc_assert ((TREE_CODE (root) == NAMESPACE_DECL
2793 || TREE_CODE (root) == FUNCTION_DECL
2794 || CLASS_TYPE_P (root)));
2795 gcc_assert ((TREE_CODE (child) == NAMESPACE_DECL
2796 || CLASS_TYPE_P (child)));
2798 /* The global namespace encloses everything. */
2799 if (root == global_namespace)
2800 return true;
2802 while (true)
2804 /* If we've run out of scopes, stop. */
2805 if (!child)
2806 return false;
2807 /* If we've reached the ROOT, it encloses CHILD. */
2808 if (root == child)
2809 return true;
2810 /* Go out one level. */
2811 if (TYPE_P (child))
2812 child = TYPE_NAME (child);
2813 child = DECL_CONTEXT (child);
2817 /* Enter the class or namespace scope indicated by T suitable for name
2818 lookup. T can be arbitrary scope, not necessary nested inside the
2819 current scope. Returns a non-null scope to pop iff pop_scope
2820 should be called later to exit this scope. */
2822 tree
2823 push_scope (tree t)
2825 if (TREE_CODE (t) == NAMESPACE_DECL)
2826 push_decl_namespace (t);
2827 else if (CLASS_TYPE_P (t))
2829 if (!at_class_scope_p ()
2830 || !same_type_p (current_class_type, t))
2831 push_nested_class (t);
2832 else
2833 /* T is the same as the current scope. There is therefore no
2834 need to re-enter the scope. Since we are not actually
2835 pushing a new scope, our caller should not call
2836 pop_scope. */
2837 t = NULL_TREE;
2840 return t;
2843 /* Leave scope pushed by push_scope. */
2845 void
2846 pop_scope (tree t)
2848 if (t == NULL_TREE)
2849 return;
2850 if (TREE_CODE (t) == NAMESPACE_DECL)
2851 pop_decl_namespace ();
2852 else if CLASS_TYPE_P (t)
2853 pop_nested_class ();
2856 /* Subroutine of push_inner_scope. */
2858 static void
2859 push_inner_scope_r (tree outer, tree inner)
2861 tree prev;
2863 if (outer == inner
2864 || (TREE_CODE (inner) != NAMESPACE_DECL && !CLASS_TYPE_P (inner)))
2865 return;
2867 prev = CP_DECL_CONTEXT (TREE_CODE (inner) == NAMESPACE_DECL ? inner : TYPE_NAME (inner));
2868 if (outer != prev)
2869 push_inner_scope_r (outer, prev);
2870 if (TREE_CODE (inner) == NAMESPACE_DECL)
2872 cp_binding_level *save_template_parm = 0;
2873 /* Temporary take out template parameter scopes. They are saved
2874 in reversed order in save_template_parm. */
2875 while (current_binding_level->kind == sk_template_parms)
2877 cp_binding_level *b = current_binding_level;
2878 current_binding_level = b->level_chain;
2879 b->level_chain = save_template_parm;
2880 save_template_parm = b;
2883 resume_scope (NAMESPACE_LEVEL (inner));
2884 current_namespace = inner;
2886 /* Restore template parameter scopes. */
2887 while (save_template_parm)
2889 cp_binding_level *b = save_template_parm;
2890 save_template_parm = b->level_chain;
2891 b->level_chain = current_binding_level;
2892 current_binding_level = b;
2895 else
2896 pushclass (inner);
2899 /* Enter the scope INNER from current scope. INNER must be a scope
2900 nested inside current scope. This works with both name lookup and
2901 pushing name into scope. In case a template parameter scope is present,
2902 namespace is pushed under the template parameter scope according to
2903 name lookup rule in 14.6.1/6.
2905 Return the former current scope suitable for pop_inner_scope. */
2907 tree
2908 push_inner_scope (tree inner)
2910 tree outer = current_scope ();
2911 if (!outer)
2912 outer = current_namespace;
2914 push_inner_scope_r (outer, inner);
2915 return outer;
2918 /* Exit the current scope INNER back to scope OUTER. */
2920 void
2921 pop_inner_scope (tree outer, tree inner)
2923 if (outer == inner
2924 || (TREE_CODE (inner) != NAMESPACE_DECL && !CLASS_TYPE_P (inner)))
2925 return;
2927 while (outer != inner)
2929 if (TREE_CODE (inner) == NAMESPACE_DECL)
2931 cp_binding_level *save_template_parm = 0;
2932 /* Temporary take out template parameter scopes. They are saved
2933 in reversed order in save_template_parm. */
2934 while (current_binding_level->kind == sk_template_parms)
2936 cp_binding_level *b = current_binding_level;
2937 current_binding_level = b->level_chain;
2938 b->level_chain = save_template_parm;
2939 save_template_parm = b;
2942 pop_namespace ();
2944 /* Restore template parameter scopes. */
2945 while (save_template_parm)
2947 cp_binding_level *b = save_template_parm;
2948 save_template_parm = b->level_chain;
2949 b->level_chain = current_binding_level;
2950 current_binding_level = b;
2953 else
2954 popclass ();
2956 inner = CP_DECL_CONTEXT (TREE_CODE (inner) == NAMESPACE_DECL ? inner : TYPE_NAME (inner));
2960 /* Do a pushlevel for class declarations. */
2962 void
2963 pushlevel_class (void)
2965 class_binding_level = begin_scope (sk_class, current_class_type);
2968 /* ...and a poplevel for class declarations. */
2970 void
2971 poplevel_class (void)
2973 cp_binding_level *level = class_binding_level;
2974 cp_class_binding *cb;
2975 size_t i;
2976 tree shadowed;
2978 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
2979 gcc_assert (level != 0);
2981 /* If we're leaving a toplevel class, cache its binding level. */
2982 if (current_class_depth == 1)
2983 previous_class_level = level;
2984 for (shadowed = level->type_shadowed;
2985 shadowed;
2986 shadowed = TREE_CHAIN (shadowed))
2987 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (shadowed), TREE_VALUE (shadowed));
2989 /* Remove the bindings for all of the class-level declarations. */
2990 if (level->class_shadowed)
2992 FOR_EACH_VEC_ELT (*level->class_shadowed, i, cb)
2994 IDENTIFIER_BINDING (cb->identifier) = cb->base->previous;
2995 cxx_binding_free (cb->base);
2997 ggc_free (level->class_shadowed);
2998 level->class_shadowed = NULL;
3001 /* Now, pop out of the binding level which we created up in the
3002 `pushlevel_class' routine. */
3003 gcc_assert (current_binding_level == level);
3004 leave_scope ();
3005 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3008 /* Set INHERITED_VALUE_BINDING_P on BINDING to true or false, as
3009 appropriate. DECL is the value to which a name has just been
3010 bound. CLASS_TYPE is the class in which the lookup occurred. */
3012 static void
3013 set_inherited_value_binding_p (cxx_binding *binding, tree decl,
3014 tree class_type)
3016 if (binding->value == decl && TREE_CODE (decl) != TREE_LIST)
3018 tree context;
3020 if (TREE_CODE (decl) == OVERLOAD)
3021 context = ovl_scope (decl);
3022 else
3024 gcc_assert (DECL_P (decl));
3025 context = context_for_name_lookup (decl);
3028 if (is_properly_derived_from (class_type, context))
3029 INHERITED_VALUE_BINDING_P (binding) = 1;
3030 else
3031 INHERITED_VALUE_BINDING_P (binding) = 0;
3033 else if (binding->value == decl)
3034 /* We only encounter a TREE_LIST when there is an ambiguity in the
3035 base classes. Such an ambiguity can be overridden by a
3036 definition in this class. */
3037 INHERITED_VALUE_BINDING_P (binding) = 1;
3038 else
3039 INHERITED_VALUE_BINDING_P (binding) = 0;
3042 /* Make the declaration of X appear in CLASS scope. */
3044 bool
3045 pushdecl_class_level (tree x)
3047 tree name;
3048 bool is_valid = true;
3049 bool subtime;
3051 /* Do nothing if we're adding to an outer lambda closure type,
3052 outer_binding will add it later if it's needed. */
3053 if (current_class_type != class_binding_level->this_entity)
3054 return true;
3056 subtime = timevar_cond_start (TV_NAME_LOOKUP);
3057 /* Get the name of X. */
3058 if (TREE_CODE (x) == OVERLOAD)
3059 name = DECL_NAME (get_first_fn (x));
3060 else
3061 name = DECL_NAME (x);
3063 if (name)
3065 is_valid = push_class_level_binding (name, x);
3066 if (TREE_CODE (x) == TYPE_DECL)
3067 set_identifier_type_value (name, x);
3069 else if (ANON_AGGR_TYPE_P (TREE_TYPE (x)))
3071 /* If X is an anonymous aggregate, all of its members are
3072 treated as if they were members of the class containing the
3073 aggregate, for naming purposes. */
3074 tree f;
3076 for (f = TYPE_FIELDS (TREE_TYPE (x)); f; f = DECL_CHAIN (f))
3078 location_t save_location = input_location;
3079 input_location = DECL_SOURCE_LOCATION (f);
3080 if (!pushdecl_class_level (f))
3081 is_valid = false;
3082 input_location = save_location;
3085 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3086 return is_valid;
3089 /* Return the BINDING (if any) for NAME in SCOPE, which is a class
3090 scope. If the value returned is non-NULL, and the PREVIOUS field
3091 is not set, callers must set the PREVIOUS field explicitly. */
3093 static cxx_binding *
3094 get_class_binding (tree name, cp_binding_level *scope)
3096 tree class_type;
3097 tree type_binding;
3098 tree value_binding;
3099 cxx_binding *binding;
3101 class_type = scope->this_entity;
3103 /* Get the type binding. */
3104 type_binding = lookup_member (class_type, name,
3105 /*protect=*/2, /*want_type=*/true,
3106 tf_warning_or_error);
3107 /* Get the value binding. */
3108 value_binding = lookup_member (class_type, name,
3109 /*protect=*/2, /*want_type=*/false,
3110 tf_warning_or_error);
3112 if (value_binding
3113 && (TREE_CODE (value_binding) == TYPE_DECL
3114 || DECL_CLASS_TEMPLATE_P (value_binding)
3115 || (TREE_CODE (value_binding) == TREE_LIST
3116 && TREE_TYPE (value_binding) == error_mark_node
3117 && (TREE_CODE (TREE_VALUE (value_binding))
3118 == TYPE_DECL))))
3119 /* We found a type binding, even when looking for a non-type
3120 binding. This means that we already processed this binding
3121 above. */
3123 else if (value_binding)
3125 if (TREE_CODE (value_binding) == TREE_LIST
3126 && TREE_TYPE (value_binding) == error_mark_node)
3127 /* NAME is ambiguous. */
3129 else if (BASELINK_P (value_binding))
3130 /* NAME is some overloaded functions. */
3131 value_binding = BASELINK_FUNCTIONS (value_binding);
3134 /* If we found either a type binding or a value binding, create a
3135 new binding object. */
3136 if (type_binding || value_binding)
3138 binding = new_class_binding (name,
3139 value_binding,
3140 type_binding,
3141 scope);
3142 /* This is a class-scope binding, not a block-scope binding. */
3143 LOCAL_BINDING_P (binding) = 0;
3144 set_inherited_value_binding_p (binding, value_binding, class_type);
3146 else
3147 binding = NULL;
3149 return binding;
3152 /* Make the declaration(s) of X appear in CLASS scope under the name
3153 NAME. Returns true if the binding is valid. */
3155 static bool
3156 push_class_level_binding_1 (tree name, tree x)
3158 cxx_binding *binding;
3159 tree decl = x;
3160 bool ok;
3162 /* The class_binding_level will be NULL if x is a template
3163 parameter name in a member template. */
3164 if (!class_binding_level)
3165 return true;
3167 if (name == error_mark_node)
3168 return false;
3170 /* Can happen for an erroneous declaration (c++/60384). */
3171 if (!identifier_p (name))
3173 gcc_assert (errorcount || sorrycount);
3174 return false;
3177 /* Check for invalid member names. But don't worry about a default
3178 argument-scope lambda being pushed after the class is complete. */
3179 gcc_assert (TYPE_BEING_DEFINED (current_class_type)
3180 || LAMBDA_TYPE_P (TREE_TYPE (decl)));
3181 /* Check that we're pushing into the right binding level. */
3182 gcc_assert (current_class_type == class_binding_level->this_entity);
3184 /* We could have been passed a tree list if this is an ambiguous
3185 declaration. If so, pull the declaration out because
3186 check_template_shadow will not handle a TREE_LIST. */
3187 if (TREE_CODE (decl) == TREE_LIST
3188 && TREE_TYPE (decl) == error_mark_node)
3189 decl = TREE_VALUE (decl);
3191 if (!check_template_shadow (decl))
3192 return false;
3194 /* [class.mem]
3196 If T is the name of a class, then each of the following shall
3197 have a name different from T:
3199 -- every static data member of class T;
3201 -- every member of class T that is itself a type;
3203 -- every enumerator of every member of class T that is an
3204 enumerated type;
3206 -- every member of every anonymous union that is a member of
3207 class T.
3209 (Non-static data members were also forbidden to have the same
3210 name as T until TC1.) */
3211 if ((VAR_P (x)
3212 || TREE_CODE (x) == CONST_DECL
3213 || (TREE_CODE (x) == TYPE_DECL
3214 && !DECL_SELF_REFERENCE_P (x))
3215 /* A data member of an anonymous union. */
3216 || (TREE_CODE (x) == FIELD_DECL
3217 && DECL_CONTEXT (x) != current_class_type))
3218 && DECL_NAME (x) == constructor_name (current_class_type))
3220 tree scope = context_for_name_lookup (x);
3221 if (TYPE_P (scope) && same_type_p (scope, current_class_type))
3223 error ("%qD has the same name as the class in which it is "
3224 "declared",
3226 return false;
3230 /* Get the current binding for NAME in this class, if any. */
3231 binding = IDENTIFIER_BINDING (name);
3232 if (!binding || binding->scope != class_binding_level)
3234 binding = get_class_binding (name, class_binding_level);
3235 /* If a new binding was created, put it at the front of the
3236 IDENTIFIER_BINDING list. */
3237 if (binding)
3239 binding->previous = IDENTIFIER_BINDING (name);
3240 IDENTIFIER_BINDING (name) = binding;
3244 /* If there is already a binding, then we may need to update the
3245 current value. */
3246 if (binding && binding->value)
3248 tree bval = binding->value;
3249 tree old_decl = NULL_TREE;
3250 tree target_decl = strip_using_decl (decl);
3251 tree target_bval = strip_using_decl (bval);
3253 if (INHERITED_VALUE_BINDING_P (binding))
3255 /* If the old binding was from a base class, and was for a
3256 tag name, slide it over to make room for the new binding.
3257 The old binding is still visible if explicitly qualified
3258 with a class-key. */
3259 if (TREE_CODE (target_bval) == TYPE_DECL
3260 && DECL_ARTIFICIAL (target_bval)
3261 && !(TREE_CODE (target_decl) == TYPE_DECL
3262 && DECL_ARTIFICIAL (target_decl)))
3264 old_decl = binding->type;
3265 binding->type = bval;
3266 binding->value = NULL_TREE;
3267 INHERITED_VALUE_BINDING_P (binding) = 0;
3269 else
3271 old_decl = bval;
3272 /* Any inherited type declaration is hidden by the type
3273 declaration in the derived class. */
3274 if (TREE_CODE (target_decl) == TYPE_DECL
3275 && DECL_ARTIFICIAL (target_decl))
3276 binding->type = NULL_TREE;
3279 else if (TREE_CODE (target_decl) == OVERLOAD
3280 && is_overloaded_fn (target_bval))
3281 old_decl = bval;
3282 else if (TREE_CODE (decl) == USING_DECL
3283 && TREE_CODE (bval) == USING_DECL
3284 && same_type_p (USING_DECL_SCOPE (decl),
3285 USING_DECL_SCOPE (bval)))
3286 /* This is a using redeclaration that will be diagnosed later
3287 in supplement_binding */
3289 else if (TREE_CODE (decl) == USING_DECL
3290 && TREE_CODE (bval) == USING_DECL
3291 && DECL_DEPENDENT_P (decl)
3292 && DECL_DEPENDENT_P (bval))
3293 return true;
3294 else if (TREE_CODE (decl) == USING_DECL
3295 && is_overloaded_fn (target_bval))
3296 old_decl = bval;
3297 else if (TREE_CODE (bval) == USING_DECL
3298 && is_overloaded_fn (target_decl))
3299 return true;
3301 if (old_decl && binding->scope == class_binding_level)
3303 binding->value = x;
3304 /* It is always safe to clear INHERITED_VALUE_BINDING_P
3305 here. This function is only used to register bindings
3306 from with the class definition itself. */
3307 INHERITED_VALUE_BINDING_P (binding) = 0;
3308 return true;
3312 /* Note that we declared this value so that we can issue an error if
3313 this is an invalid redeclaration of a name already used for some
3314 other purpose. */
3315 note_name_declared_in_class (name, decl);
3317 /* If we didn't replace an existing binding, put the binding on the
3318 stack of bindings for the identifier, and update the shadowed
3319 list. */
3320 if (binding && binding->scope == class_binding_level)
3321 /* Supplement the existing binding. */
3322 ok = supplement_binding (binding, decl);
3323 else
3325 /* Create a new binding. */
3326 push_binding (name, decl, class_binding_level);
3327 ok = true;
3330 return ok;
3333 /* Wrapper for push_class_level_binding_1. */
3335 bool
3336 push_class_level_binding (tree name, tree x)
3338 bool ret;
3339 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3340 ret = push_class_level_binding_1 (name, x);
3341 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3342 return ret;
3345 /* Process "using SCOPE::NAME" in a class scope. Return the
3346 USING_DECL created. */
3348 tree
3349 do_class_using_decl (tree scope, tree name)
3351 /* The USING_DECL returned by this function. */
3352 tree value;
3353 /* The declaration (or declarations) name by this using
3354 declaration. NULL if we are in a template and cannot figure out
3355 what has been named. */
3356 tree decl;
3357 /* True if SCOPE is a dependent type. */
3358 bool scope_dependent_p;
3359 /* True if SCOPE::NAME is dependent. */
3360 bool name_dependent_p;
3361 /* True if any of the bases of CURRENT_CLASS_TYPE are dependent. */
3362 bool bases_dependent_p;
3363 tree binfo;
3365 if (name == error_mark_node)
3366 return NULL_TREE;
3368 if (!scope || !TYPE_P (scope))
3370 error ("using-declaration for non-member at class scope");
3371 return NULL_TREE;
3374 /* Make sure the name is not invalid */
3375 if (TREE_CODE (name) == BIT_NOT_EXPR)
3377 error ("%<%T::%D%> names destructor", scope, name);
3378 return NULL_TREE;
3380 /* Using T::T declares inheriting ctors, even if T is a typedef. */
3381 if (MAYBE_CLASS_TYPE_P (scope)
3382 && (name == TYPE_IDENTIFIER (scope)
3383 || constructor_name_p (name, scope)))
3385 maybe_warn_cpp0x (CPP0X_INHERITING_CTORS);
3386 name = ctor_identifier;
3387 CLASSTYPE_NON_AGGREGATE (current_class_type) = true;
3389 if (constructor_name_p (name, current_class_type))
3391 error ("%<%T::%D%> names constructor in %qT",
3392 scope, name, current_class_type);
3393 return NULL_TREE;
3396 scope_dependent_p = dependent_scope_p (scope);
3397 name_dependent_p = (scope_dependent_p
3398 || (IDENTIFIER_TYPENAME_P (name)
3399 && dependent_type_p (TREE_TYPE (name))));
3401 bases_dependent_p = any_dependent_bases_p ();
3403 decl = NULL_TREE;
3405 /* From [namespace.udecl]:
3407 A using-declaration used as a member-declaration shall refer to a
3408 member of a base class of the class being defined.
3410 In general, we cannot check this constraint in a template because
3411 we do not know the entire set of base classes of the current
3412 class type. Morover, if SCOPE is dependent, it might match a
3413 non-dependent base. */
3415 if (!scope_dependent_p)
3417 base_kind b_kind;
3418 binfo = lookup_base (current_class_type, scope, ba_any, &b_kind,
3419 tf_warning_or_error);
3420 if (b_kind < bk_proper_base)
3422 if (!bases_dependent_p || b_kind == bk_same_type)
3424 error_not_base_type (scope, current_class_type);
3425 return NULL_TREE;
3428 else if (name == ctor_identifier
3429 && BINFO_INHERITANCE_CHAIN (BINFO_INHERITANCE_CHAIN (binfo)))
3431 error ("cannot inherit constructors from indirect base %qT", scope);
3432 return NULL_TREE;
3434 else if (!name_dependent_p)
3436 decl = lookup_member (binfo, name, 0, false, tf_warning_or_error);
3437 if (!decl)
3439 error ("no members matching %<%T::%D%> in %q#T", scope, name,
3440 scope);
3441 return NULL_TREE;
3443 /* The binfo from which the functions came does not matter. */
3444 if (BASELINK_P (decl))
3445 decl = BASELINK_FUNCTIONS (decl);
3449 value = build_lang_decl (USING_DECL, name, NULL_TREE);
3450 USING_DECL_DECLS (value) = decl;
3451 USING_DECL_SCOPE (value) = scope;
3452 DECL_DEPENDENT_P (value) = !decl;
3454 return value;
3458 /* Return the binding value for name in scope. */
3461 static tree
3462 namespace_binding_1 (tree name, tree scope)
3464 cxx_binding *binding;
3466 if (SCOPE_FILE_SCOPE_P (scope))
3467 scope = global_namespace;
3468 else
3469 /* Unnecessary for the global namespace because it can't be an alias. */
3470 scope = ORIGINAL_NAMESPACE (scope);
3472 binding = cp_binding_level_find_binding_for_name (NAMESPACE_LEVEL (scope), name);
3474 return binding ? binding->value : NULL_TREE;
3477 tree
3478 namespace_binding (tree name, tree scope)
3480 tree ret;
3481 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3482 ret = namespace_binding_1 (name, scope);
3483 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3484 return ret;
3487 /* Set the binding value for name in scope. */
3489 static void
3490 set_namespace_binding_1 (tree name, tree scope, tree val)
3492 cxx_binding *b;
3494 if (scope == NULL_TREE)
3495 scope = global_namespace;
3496 b = binding_for_name (NAMESPACE_LEVEL (scope), name);
3497 if (!b->value || TREE_CODE (val) == OVERLOAD || val == error_mark_node)
3498 b->value = val;
3499 else
3500 supplement_binding (b, val);
3503 /* Wrapper for set_namespace_binding_1. */
3505 void
3506 set_namespace_binding (tree name, tree scope, tree val)
3508 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3509 set_namespace_binding_1 (name, scope, val);
3510 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3513 /* Set the context of a declaration to scope. Complain if we are not
3514 outside scope. */
3516 void
3517 set_decl_namespace (tree decl, tree scope, bool friendp)
3519 tree old;
3521 /* Get rid of namespace aliases. */
3522 scope = ORIGINAL_NAMESPACE (scope);
3524 /* It is ok for friends to be qualified in parallel space. */
3525 if (!friendp && !is_ancestor (current_namespace, scope))
3526 error ("declaration of %qD not in a namespace surrounding %qD",
3527 decl, scope);
3528 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
3530 /* Writing "int N::i" to declare a variable within "N" is invalid. */
3531 if (scope == current_namespace)
3533 if (at_namespace_scope_p ())
3534 error ("explicit qualification in declaration of %qD",
3535 decl);
3536 return;
3539 /* See whether this has been declared in the namespace. */
3540 old = lookup_qualified_name (scope, DECL_NAME (decl), /*type*/false,
3541 /*complain*/true, /*hidden*/true);
3542 if (old == error_mark_node)
3543 /* No old declaration at all. */
3544 goto complain;
3545 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
3546 if (TREE_CODE (old) == TREE_LIST)
3548 error ("reference to %qD is ambiguous", decl);
3549 print_candidates (old);
3550 return;
3552 if (!is_overloaded_fn (decl))
3554 /* We might have found OLD in an inline namespace inside SCOPE. */
3555 if (TREE_CODE (decl) == TREE_CODE (old))
3556 DECL_CONTEXT (decl) = DECL_CONTEXT (old);
3557 /* Don't compare non-function decls with decls_match here, since
3558 it can't check for the correct constness at this
3559 point. pushdecl will find those errors later. */
3560 return;
3562 /* Since decl is a function, old should contain a function decl. */
3563 if (!is_overloaded_fn (old))
3564 goto complain;
3565 /* We handle these in check_explicit_instantiation_namespace. */
3566 if (processing_explicit_instantiation)
3567 return;
3568 if (processing_template_decl || processing_specialization)
3569 /* We have not yet called push_template_decl to turn a
3570 FUNCTION_DECL into a TEMPLATE_DECL, so the declarations won't
3571 match. But, we'll check later, when we construct the
3572 template. */
3573 return;
3574 /* Instantiations or specializations of templates may be declared as
3575 friends in any namespace. */
3576 if (friendp && DECL_USE_TEMPLATE (decl))
3577 return;
3578 if (is_overloaded_fn (old))
3580 tree found = NULL_TREE;
3581 tree elt = old;
3582 for (; elt; elt = OVL_NEXT (elt))
3584 tree ofn = OVL_CURRENT (elt);
3585 /* Adjust DECL_CONTEXT first so decls_match will return true
3586 if DECL will match a declaration in an inline namespace. */
3587 DECL_CONTEXT (decl) = DECL_CONTEXT (ofn);
3588 if (decls_match (decl, ofn))
3590 if (found && !decls_match (found, ofn))
3592 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
3593 error ("reference to %qD is ambiguous", decl);
3594 print_candidates (old);
3595 return;
3597 found = ofn;
3600 if (found)
3602 if (!is_associated_namespace (scope, CP_DECL_CONTEXT (found)))
3603 goto complain;
3604 if (DECL_HIDDEN_FRIEND_P (found))
3606 pedwarn (DECL_SOURCE_LOCATION (decl), 0,
3607 "%qD has not been declared within %D", decl, scope);
3608 inform (DECL_SOURCE_LOCATION (found), "only here as a friend");
3610 DECL_CONTEXT (decl) = DECL_CONTEXT (found);
3611 return;
3614 else
3616 DECL_CONTEXT (decl) = DECL_CONTEXT (old);
3617 if (decls_match (decl, old))
3618 return;
3621 /* It didn't work, go back to the explicit scope. */
3622 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
3623 complain:
3624 error ("%qD should have been declared inside %qD", decl, scope);
3627 /* Return the namespace where the current declaration is declared. */
3629 tree
3630 current_decl_namespace (void)
3632 tree result;
3633 /* If we have been pushed into a different namespace, use it. */
3634 if (!vec_safe_is_empty (decl_namespace_list))
3635 return decl_namespace_list->last ();
3637 if (current_class_type)
3638 result = decl_namespace_context (current_class_type);
3639 else if (current_function_decl)
3640 result = decl_namespace_context (current_function_decl);
3641 else
3642 result = current_namespace;
3643 return result;
3646 /* Process any ATTRIBUTES on a namespace definition. Returns true if
3647 attribute visibility is seen. */
3649 bool
3650 handle_namespace_attrs (tree ns, tree attributes)
3652 tree d;
3653 bool saw_vis = false;
3655 for (d = attributes; d; d = TREE_CHAIN (d))
3657 tree name = get_attribute_name (d);
3658 tree args = TREE_VALUE (d);
3660 if (is_attribute_p ("visibility", name))
3662 /* attribute visibility is a property of the syntactic block
3663 rather than the namespace as a whole, so we don't touch the
3664 NAMESPACE_DECL at all. */
3665 tree x = args ? TREE_VALUE (args) : NULL_TREE;
3666 if (x == NULL_TREE || TREE_CODE (x) != STRING_CST || TREE_CHAIN (args))
3668 warning (OPT_Wattributes,
3669 "%qD attribute requires a single NTBS argument",
3670 name);
3671 continue;
3674 if (!TREE_PUBLIC (ns))
3675 warning (OPT_Wattributes,
3676 "%qD attribute is meaningless since members of the "
3677 "anonymous namespace get local symbols", name);
3679 push_visibility (TREE_STRING_POINTER (x), 1);
3680 saw_vis = true;
3682 else if (is_attribute_p ("abi_tag", name))
3684 if (!DECL_NAMESPACE_ASSOCIATIONS (ns))
3686 warning (OPT_Wattributes, "ignoring %qD attribute on non-inline "
3687 "namespace", name);
3688 continue;
3690 if (!DECL_NAME (ns))
3692 warning (OPT_Wattributes, "ignoring %qD attribute on anonymous "
3693 "namespace", name);
3694 continue;
3696 if (!args)
3698 tree dn = DECL_NAME (ns);
3699 args = build_string (IDENTIFIER_LENGTH (dn) + 1,
3700 IDENTIFIER_POINTER (dn));
3701 TREE_TYPE (args) = char_array_type_node;
3702 args = fix_string_type (args);
3703 args = build_tree_list (NULL_TREE, args);
3705 if (check_abi_tag_args (args, name))
3706 DECL_ATTRIBUTES (ns) = tree_cons (name, args,
3707 DECL_ATTRIBUTES (ns));
3709 else
3711 warning (OPT_Wattributes, "%qD attribute directive ignored",
3712 name);
3713 continue;
3717 return saw_vis;
3720 /* Push into the scope of the NAME namespace. If NAME is NULL_TREE, then we
3721 select a name that is unique to this compilation unit. Returns FALSE if
3722 pushdecl fails, TRUE otherwise. */
3724 bool
3725 push_namespace (tree name)
3727 tree d = NULL_TREE;
3728 bool need_new = true;
3729 bool implicit_use = false;
3730 bool anon = !name;
3732 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3734 /* We should not get here if the global_namespace is not yet constructed
3735 nor if NAME designates the global namespace: The global scope is
3736 constructed elsewhere. */
3737 gcc_assert (global_namespace != NULL && name != global_scope_name);
3739 if (anon)
3741 name = get_anonymous_namespace_name();
3742 d = IDENTIFIER_NAMESPACE_VALUE (name);
3743 if (d)
3744 /* Reopening anonymous namespace. */
3745 need_new = false;
3746 implicit_use = true;
3748 else
3750 /* Check whether this is an extended namespace definition. */
3751 d = IDENTIFIER_NAMESPACE_VALUE (name);
3752 if (d != NULL_TREE && TREE_CODE (d) == NAMESPACE_DECL)
3754 tree dna = DECL_NAMESPACE_ALIAS (d);
3755 if (dna)
3757 /* We do some error recovery for, eg, the redeclaration
3758 of M here:
3760 namespace N {}
3761 namespace M = N;
3762 namespace M {}
3764 However, in nasty cases like:
3766 namespace N
3768 namespace M = N;
3769 namespace M {}
3772 we just error out below, in duplicate_decls. */
3773 if (NAMESPACE_LEVEL (dna)->level_chain
3774 == current_binding_level)
3776 error ("namespace alias %qD not allowed here, "
3777 "assuming %qD", d, dna);
3778 d = dna;
3779 need_new = false;
3782 else
3783 need_new = false;
3787 if (need_new)
3789 /* Make a new namespace, binding the name to it. */
3790 d = build_lang_decl (NAMESPACE_DECL, name, void_type_node);
3791 DECL_CONTEXT (d) = FROB_CONTEXT (current_namespace);
3792 /* The name of this namespace is not visible to other translation
3793 units if it is an anonymous namespace or member thereof. */
3794 if (anon || decl_anon_ns_mem_p (current_namespace))
3795 TREE_PUBLIC (d) = 0;
3796 else
3797 TREE_PUBLIC (d) = 1;
3798 if (pushdecl (d) == error_mark_node)
3800 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3801 return false;
3803 if (anon)
3805 /* Clear DECL_NAME for the benefit of debugging back ends. */
3806 SET_DECL_ASSEMBLER_NAME (d, name);
3807 DECL_NAME (d) = NULL_TREE;
3809 begin_scope (sk_namespace, d);
3811 else
3812 resume_scope (NAMESPACE_LEVEL (d));
3814 if (implicit_use)
3815 do_using_directive (d);
3816 /* Enter the name space. */
3817 current_namespace = d;
3819 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3820 return true;
3823 /* Pop from the scope of the current namespace. */
3825 void
3826 pop_namespace (void)
3828 gcc_assert (current_namespace != global_namespace);
3829 current_namespace = CP_DECL_CONTEXT (current_namespace);
3830 /* The binding level is not popped, as it might be re-opened later. */
3831 leave_scope ();
3834 /* Push into the scope of the namespace NS, even if it is deeply
3835 nested within another namespace. */
3837 void
3838 push_nested_namespace (tree ns)
3840 if (ns == global_namespace)
3841 push_to_top_level ();
3842 else
3844 push_nested_namespace (CP_DECL_CONTEXT (ns));
3845 push_namespace (DECL_NAME (ns));
3849 /* Pop back from the scope of the namespace NS, which was previously
3850 entered with push_nested_namespace. */
3852 void
3853 pop_nested_namespace (tree ns)
3855 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3856 gcc_assert (current_namespace == ns);
3857 while (ns != global_namespace)
3859 pop_namespace ();
3860 ns = CP_DECL_CONTEXT (ns);
3863 pop_from_top_level ();
3864 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3867 /* Temporarily set the namespace for the current declaration. */
3869 void
3870 push_decl_namespace (tree decl)
3872 if (TREE_CODE (decl) != NAMESPACE_DECL)
3873 decl = decl_namespace_context (decl);
3874 vec_safe_push (decl_namespace_list, ORIGINAL_NAMESPACE (decl));
3877 /* [namespace.memdef]/2 */
3879 void
3880 pop_decl_namespace (void)
3882 decl_namespace_list->pop ();
3885 /* Return the namespace that is the common ancestor
3886 of two given namespaces. */
3888 static tree
3889 namespace_ancestor_1 (tree ns1, tree ns2)
3891 tree nsr;
3892 if (is_ancestor (ns1, ns2))
3893 nsr = ns1;
3894 else
3895 nsr = namespace_ancestor_1 (CP_DECL_CONTEXT (ns1), ns2);
3896 return nsr;
3899 /* Wrapper for namespace_ancestor_1. */
3901 static tree
3902 namespace_ancestor (tree ns1, tree ns2)
3904 tree nsr;
3905 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3906 nsr = namespace_ancestor_1 (ns1, ns2);
3907 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3908 return nsr;
3911 /* Process a namespace-alias declaration. */
3913 void
3914 do_namespace_alias (tree alias, tree name_space)
3916 if (name_space == error_mark_node)
3917 return;
3919 gcc_assert (TREE_CODE (name_space) == NAMESPACE_DECL);
3921 name_space = ORIGINAL_NAMESPACE (name_space);
3923 /* Build the alias. */
3924 alias = build_lang_decl (NAMESPACE_DECL, alias, void_type_node);
3925 DECL_NAMESPACE_ALIAS (alias) = name_space;
3926 DECL_EXTERNAL (alias) = 1;
3927 DECL_CONTEXT (alias) = FROB_CONTEXT (current_scope ());
3928 pushdecl (alias);
3930 /* Emit debug info for namespace alias. */
3931 if (!building_stmt_list_p ())
3932 (*debug_hooks->early_global_decl) (alias);
3935 /* Like pushdecl, only it places X in the current namespace,
3936 if appropriate. */
3938 tree
3939 pushdecl_namespace_level (tree x, bool is_friend)
3941 cp_binding_level *b = current_binding_level;
3942 tree t;
3944 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3945 t = pushdecl_with_scope (x, NAMESPACE_LEVEL (current_namespace), is_friend);
3947 /* Now, the type_shadowed stack may screw us. Munge it so it does
3948 what we want. */
3949 if (TREE_CODE (t) == TYPE_DECL)
3951 tree name = DECL_NAME (t);
3952 tree newval;
3953 tree *ptr = (tree *)0;
3954 for (; !global_scope_p (b); b = b->level_chain)
3956 tree shadowed = b->type_shadowed;
3957 for (; shadowed; shadowed = TREE_CHAIN (shadowed))
3958 if (TREE_PURPOSE (shadowed) == name)
3960 ptr = &TREE_VALUE (shadowed);
3961 /* Can't break out of the loop here because sometimes
3962 a binding level will have duplicate bindings for
3963 PT names. It's gross, but I haven't time to fix it. */
3966 newval = TREE_TYPE (t);
3967 if (ptr == (tree *)0)
3969 /* @@ This shouldn't be needed. My test case "zstring.cc" trips
3970 up here if this is changed to an assertion. --KR */
3971 SET_IDENTIFIER_TYPE_VALUE (name, t);
3973 else
3975 *ptr = newval;
3978 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3979 return t;
3982 /* Insert USED into the using list of USER. Set INDIRECT_flag if this
3983 directive is not directly from the source. Also find the common
3984 ancestor and let our users know about the new namespace */
3986 static void
3987 add_using_namespace_1 (tree user, tree used, bool indirect)
3989 tree t;
3990 /* Using oneself is a no-op. */
3991 if (user == used)
3992 return;
3993 gcc_assert (TREE_CODE (user) == NAMESPACE_DECL);
3994 gcc_assert (TREE_CODE (used) == NAMESPACE_DECL);
3995 /* Check if we already have this. */
3996 t = purpose_member (used, DECL_NAMESPACE_USING (user));
3997 if (t != NULL_TREE)
3999 if (!indirect)
4000 /* Promote to direct usage. */
4001 TREE_INDIRECT_USING (t) = 0;
4002 return;
4005 /* Add used to the user's using list. */
4006 DECL_NAMESPACE_USING (user)
4007 = tree_cons (used, namespace_ancestor (user, used),
4008 DECL_NAMESPACE_USING (user));
4010 TREE_INDIRECT_USING (DECL_NAMESPACE_USING (user)) = indirect;
4012 /* Add user to the used's users list. */
4013 DECL_NAMESPACE_USERS (used)
4014 = tree_cons (user, 0, DECL_NAMESPACE_USERS (used));
4016 /* Recursively add all namespaces used. */
4017 for (t = DECL_NAMESPACE_USING (used); t; t = TREE_CHAIN (t))
4018 /* indirect usage */
4019 add_using_namespace_1 (user, TREE_PURPOSE (t), 1);
4021 /* Tell everyone using us about the new used namespaces. */
4022 for (t = DECL_NAMESPACE_USERS (user); t; t = TREE_CHAIN (t))
4023 add_using_namespace_1 (TREE_PURPOSE (t), used, 1);
4026 /* Wrapper for add_using_namespace_1. */
4028 static void
4029 add_using_namespace (tree user, tree used, bool indirect)
4031 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4032 add_using_namespace_1 (user, used, indirect);
4033 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4036 /* Process a using-declaration not appearing in class or local scope. */
4038 void
4039 do_toplevel_using_decl (tree decl, tree scope, tree name)
4041 tree oldval, oldtype, newval, newtype;
4042 tree orig_decl = decl;
4043 cxx_binding *binding;
4045 decl = validate_nonmember_using_decl (decl, scope, name);
4046 if (decl == NULL_TREE)
4047 return;
4049 binding = binding_for_name (NAMESPACE_LEVEL (current_namespace), name);
4051 oldval = binding->value;
4052 oldtype = binding->type;
4054 do_nonmember_using_decl (scope, name, oldval, oldtype, &newval, &newtype);
4056 /* Emit debug info. */
4057 if (!processing_template_decl)
4058 cp_emit_debug_info_for_using (orig_decl, current_namespace);
4060 /* Copy declarations found. */
4061 if (newval)
4062 binding->value = newval;
4063 if (newtype)
4064 binding->type = newtype;
4067 /* Process a using-directive. */
4069 void
4070 do_using_directive (tree name_space)
4072 tree context = NULL_TREE;
4074 if (name_space == error_mark_node)
4075 return;
4077 gcc_assert (TREE_CODE (name_space) == NAMESPACE_DECL);
4079 if (building_stmt_list_p ())
4080 add_stmt (build_stmt (input_location, USING_STMT, name_space));
4081 name_space = ORIGINAL_NAMESPACE (name_space);
4083 if (!toplevel_bindings_p ())
4085 push_using_directive (name_space);
4087 else
4089 /* direct usage */
4090 add_using_namespace (current_namespace, name_space, 0);
4091 if (current_namespace != global_namespace)
4092 context = current_namespace;
4094 /* Emit debugging info. */
4095 if (!processing_template_decl)
4096 (*debug_hooks->imported_module_or_decl) (name_space, NULL_TREE,
4097 context, false);
4101 /* Deal with a using-directive seen by the parser. Currently we only
4102 handle attributes here, since they cannot appear inside a template. */
4104 void
4105 parse_using_directive (tree name_space, tree attribs)
4107 do_using_directive (name_space);
4109 if (attribs == error_mark_node)
4110 return;
4112 for (tree a = attribs; a; a = TREE_CHAIN (a))
4114 tree name = get_attribute_name (a);
4115 if (is_attribute_p ("strong", name))
4117 if (!toplevel_bindings_p ())
4118 error ("strong using only meaningful at namespace scope");
4119 else if (name_space != error_mark_node)
4121 if (!is_ancestor (current_namespace, name_space))
4122 error ("current namespace %qD does not enclose strongly used namespace %qD",
4123 current_namespace, name_space);
4124 DECL_NAMESPACE_ASSOCIATIONS (name_space)
4125 = tree_cons (current_namespace, 0,
4126 DECL_NAMESPACE_ASSOCIATIONS (name_space));
4129 else
4130 warning (OPT_Wattributes, "%qD attribute directive ignored", name);
4134 /* Like pushdecl, only it places X in the global scope if appropriate.
4135 Calls cp_finish_decl to register the variable, initializing it with
4136 *INIT, if INIT is non-NULL. */
4138 static tree
4139 pushdecl_top_level_1 (tree x, tree *init, bool is_friend)
4141 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4142 push_to_top_level ();
4143 x = pushdecl_namespace_level (x, is_friend);
4144 if (init)
4145 cp_finish_decl (x, *init, false, NULL_TREE, 0);
4146 pop_from_top_level ();
4147 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4148 return x;
4151 /* Like pushdecl, only it places X in the global scope if appropriate. */
4153 tree
4154 pushdecl_top_level (tree x)
4156 return pushdecl_top_level_1 (x, NULL, false);
4159 /* Like pushdecl_top_level, but adding the IS_FRIEND parameter. */
4161 tree
4162 pushdecl_top_level_maybe_friend (tree x, bool is_friend)
4164 return pushdecl_top_level_1 (x, NULL, is_friend);
4167 /* Like pushdecl, only it places X in the global scope if
4168 appropriate. Calls cp_finish_decl to register the variable,
4169 initializing it with INIT. */
4171 tree
4172 pushdecl_top_level_and_finish (tree x, tree init)
4174 return pushdecl_top_level_1 (x, &init, false);
4177 /* Combines two sets of overloaded functions into an OVERLOAD chain, removing
4178 duplicates. The first list becomes the tail of the result.
4180 The algorithm is O(n^2). We could get this down to O(n log n) by
4181 doing a sort on the addresses of the functions, if that becomes
4182 necessary. */
4184 static tree
4185 merge_functions (tree s1, tree s2)
4187 for (; s2; s2 = OVL_NEXT (s2))
4189 tree fn2 = OVL_CURRENT (s2);
4190 tree fns1;
4192 for (fns1 = s1; fns1; fns1 = OVL_NEXT (fns1))
4194 tree fn1 = OVL_CURRENT (fns1);
4196 /* If the function from S2 is already in S1, there is no
4197 need to add it again. For `extern "C"' functions, we
4198 might have two FUNCTION_DECLs for the same function, in
4199 different namespaces, but let's leave them in case
4200 they have different default arguments. */
4201 if (fn1 == fn2)
4202 break;
4205 /* If we exhausted all of the functions in S1, FN2 is new. */
4206 if (!fns1)
4207 s1 = build_overload (fn2, s1);
4209 return s1;
4212 /* Returns TRUE iff OLD and NEW are the same entity.
4214 3 [basic]/3: An entity is a value, object, reference, function,
4215 enumerator, type, class member, template, template specialization,
4216 namespace, parameter pack, or this.
4218 7.3.4 [namespace.udir]/4: If name lookup finds a declaration for a name
4219 in two different namespaces, and the declarations do not declare the
4220 same entity and do not declare functions, the use of the name is
4221 ill-formed. */
4223 static bool
4224 same_entity_p (tree one, tree two)
4226 if (one == two)
4227 return true;
4228 if (!one || !two)
4229 return false;
4230 if (TREE_CODE (one) == TYPE_DECL
4231 && TREE_CODE (two) == TYPE_DECL
4232 && same_type_p (TREE_TYPE (one), TREE_TYPE (two)))
4233 return true;
4234 return false;
4237 /* This should return an error not all definitions define functions.
4238 It is not an error if we find two functions with exactly the
4239 same signature, only if these are selected in overload resolution.
4240 old is the current set of bindings, new_binding the freshly-found binding.
4241 XXX Do we want to give *all* candidates in case of ambiguity?
4242 XXX In what way should I treat extern declarations?
4243 XXX I don't want to repeat the entire duplicate_decls here */
4245 static void
4246 ambiguous_decl (struct scope_binding *old, cxx_binding *new_binding, int flags)
4248 tree val, type;
4249 gcc_assert (old != NULL);
4251 /* Copy the type. */
4252 type = new_binding->type;
4253 if (LOOKUP_NAMESPACES_ONLY (flags)
4254 || (type && hidden_name_p (type) && !(flags & LOOKUP_HIDDEN)))
4255 type = NULL_TREE;
4257 /* Copy the value. */
4258 val = new_binding->value;
4259 if (val)
4261 if (!(flags & LOOKUP_HIDDEN))
4262 val = remove_hidden_names (val);
4263 if (val)
4264 switch (TREE_CODE (val))
4266 case TEMPLATE_DECL:
4267 /* If we expect types or namespaces, and not templates,
4268 or this is not a template class. */
4269 if ((LOOKUP_QUALIFIERS_ONLY (flags)
4270 && !DECL_TYPE_TEMPLATE_P (val)))
4271 val = NULL_TREE;
4272 break;
4273 case TYPE_DECL:
4274 if (LOOKUP_NAMESPACES_ONLY (flags)
4275 || (type && (flags & LOOKUP_PREFER_TYPES)))
4276 val = NULL_TREE;
4277 break;
4278 case NAMESPACE_DECL:
4279 if (LOOKUP_TYPES_ONLY (flags))
4280 val = NULL_TREE;
4281 break;
4282 case FUNCTION_DECL:
4283 /* Ignore built-in functions that are still anticipated. */
4284 if (LOOKUP_QUALIFIERS_ONLY (flags))
4285 val = NULL_TREE;
4286 break;
4287 default:
4288 if (LOOKUP_QUALIFIERS_ONLY (flags))
4289 val = NULL_TREE;
4293 /* If val is hidden, shift down any class or enumeration name. */
4294 if (!val)
4296 val = type;
4297 type = NULL_TREE;
4300 if (!old->value)
4301 old->value = val;
4302 else if (val && !same_entity_p (val, old->value))
4304 if (is_overloaded_fn (old->value) && is_overloaded_fn (val))
4305 old->value = merge_functions (old->value, val);
4306 else
4308 old->value = tree_cons (NULL_TREE, old->value,
4309 build_tree_list (NULL_TREE, val));
4310 TREE_TYPE (old->value) = error_mark_node;
4314 if (!old->type)
4315 old->type = type;
4316 else if (type && old->type != type)
4318 old->type = tree_cons (NULL_TREE, old->type,
4319 build_tree_list (NULL_TREE, type));
4320 TREE_TYPE (old->type) = error_mark_node;
4324 /* Return the declarations that are members of the namespace NS. */
4326 tree
4327 cp_namespace_decls (tree ns)
4329 return NAMESPACE_LEVEL (ns)->names;
4332 /* Combine prefer_type and namespaces_only into flags. */
4334 static int
4335 lookup_flags (int prefer_type, int namespaces_only)
4337 if (namespaces_only)
4338 return LOOKUP_PREFER_NAMESPACES;
4339 if (prefer_type > 1)
4340 return LOOKUP_PREFER_TYPES;
4341 if (prefer_type > 0)
4342 return LOOKUP_PREFER_BOTH;
4343 return 0;
4346 /* Given a lookup that returned VAL, use FLAGS to decide if we want to
4347 ignore it or not. Subroutine of lookup_name_real and
4348 lookup_type_scope. */
4350 static bool
4351 qualify_lookup (tree val, int flags)
4353 if (val == NULL_TREE)
4354 return false;
4355 if ((flags & LOOKUP_PREFER_NAMESPACES) && TREE_CODE (val) == NAMESPACE_DECL)
4356 return true;
4357 if (flags & LOOKUP_PREFER_TYPES)
4359 tree target_val = strip_using_decl (val);
4360 if (TREE_CODE (target_val) == TYPE_DECL
4361 || TREE_CODE (target_val) == TEMPLATE_DECL)
4362 return true;
4364 if (flags & (LOOKUP_PREFER_NAMESPACES | LOOKUP_PREFER_TYPES))
4365 return false;
4366 /* Look through lambda things that we shouldn't be able to see. */
4367 if (is_lambda_ignored_entity (val))
4368 return false;
4369 return true;
4372 /* Given a lookup that returned VAL, decide if we want to ignore it or
4373 not based on DECL_ANTICIPATED. */
4375 bool
4376 hidden_name_p (tree val)
4378 if (DECL_P (val)
4379 && DECL_LANG_SPECIFIC (val)
4380 && TYPE_FUNCTION_OR_TEMPLATE_DECL_P (val)
4381 && DECL_ANTICIPATED (val))
4382 return true;
4383 if (TREE_CODE (val) == OVERLOAD)
4385 for (tree o = val; o; o = OVL_CHAIN (o))
4386 if (!hidden_name_p (OVL_FUNCTION (o)))
4387 return false;
4388 return true;
4390 return false;
4393 /* Remove any hidden declarations from a possibly overloaded set
4394 of functions. */
4396 tree
4397 remove_hidden_names (tree fns)
4399 if (!fns)
4400 return fns;
4402 if (DECL_P (fns) && hidden_name_p (fns))
4403 fns = NULL_TREE;
4404 else if (TREE_CODE (fns) == OVERLOAD)
4406 tree o;
4408 for (o = fns; o; o = OVL_NEXT (o))
4409 if (hidden_name_p (OVL_CURRENT (o)))
4410 break;
4411 if (o)
4413 tree n = NULL_TREE;
4415 for (o = fns; o; o = OVL_NEXT (o))
4416 if (!hidden_name_p (OVL_CURRENT (o)))
4417 n = build_overload (OVL_CURRENT (o), n);
4418 fns = n;
4422 return fns;
4425 /* Suggest alternatives for NAME, an IDENTIFIER_NODE for which name
4426 lookup failed. Search through all available namespaces and print out
4427 possible candidates. */
4429 void
4430 suggest_alternatives_for (location_t location, tree name)
4432 vec<tree> candidates = vNULL;
4433 vec<tree> namespaces_to_search = vNULL;
4434 int max_to_search = PARAM_VALUE (CXX_MAX_NAMESPACES_FOR_DIAGNOSTIC_HELP);
4435 int n_searched = 0;
4436 tree t;
4437 unsigned ix;
4439 namespaces_to_search.safe_push (global_namespace);
4441 while (!namespaces_to_search.is_empty ()
4442 && n_searched < max_to_search)
4444 tree scope = namespaces_to_search.pop ();
4445 struct scope_binding binding = EMPTY_SCOPE_BINDING;
4446 cp_binding_level *level = NAMESPACE_LEVEL (scope);
4448 /* Look in this namespace. */
4449 qualified_lookup_using_namespace (name, scope, &binding, 0);
4451 n_searched++;
4453 if (binding.value)
4454 candidates.safe_push (binding.value);
4456 /* Add child namespaces. */
4457 for (t = level->namespaces; t; t = DECL_CHAIN (t))
4458 namespaces_to_search.safe_push (t);
4461 /* If we stopped before we could examine all namespaces, inform the
4462 user. Do this even if we don't have any candidates, since there
4463 might be more candidates further down that we weren't able to
4464 find. */
4465 if (n_searched >= max_to_search
4466 && !namespaces_to_search.is_empty ())
4467 inform (location,
4468 "maximum limit of %d namespaces searched for %qE",
4469 max_to_search, name);
4471 namespaces_to_search.release ();
4473 /* Nothing useful to report for NAME. Report on likely misspellings,
4474 or do nothing. */
4475 if (candidates.is_empty ())
4477 const char *fuzzy_name = lookup_name_fuzzy (name, FUZZY_LOOKUP_NAME);
4478 if (fuzzy_name)
4480 gcc_rich_location richloc (location);
4481 richloc.add_fixit_replace (fuzzy_name);
4482 inform_at_rich_loc (&richloc, "suggested alternative: %qs",
4483 fuzzy_name);
4485 return;
4488 inform_n (location, candidates.length (),
4489 "suggested alternative:",
4490 "suggested alternatives:");
4492 FOR_EACH_VEC_ELT (candidates, ix, t)
4493 inform (location_of (t), " %qE", t);
4495 candidates.release ();
4498 /* Unscoped lookup of a global: iterate over current namespaces,
4499 considering using-directives. */
4501 static tree
4502 unqualified_namespace_lookup_1 (tree name, int flags)
4504 tree initial = current_decl_namespace ();
4505 tree scope = initial;
4506 tree siter;
4507 cp_binding_level *level;
4508 tree val = NULL_TREE;
4510 for (; !val; scope = CP_DECL_CONTEXT (scope))
4512 struct scope_binding binding = EMPTY_SCOPE_BINDING;
4513 cxx_binding *b =
4514 cp_binding_level_find_binding_for_name (NAMESPACE_LEVEL (scope), name);
4516 if (b)
4517 ambiguous_decl (&binding, b, flags);
4519 /* Add all _DECLs seen through local using-directives. */
4520 for (level = current_binding_level;
4521 level->kind != sk_namespace;
4522 level = level->level_chain)
4523 if (!lookup_using_namespace (name, &binding, level->using_directives,
4524 scope, flags))
4525 /* Give up because of error. */
4526 return error_mark_node;
4528 /* Add all _DECLs seen through global using-directives. */
4529 /* XXX local and global using lists should work equally. */
4530 siter = initial;
4531 while (1)
4533 if (!lookup_using_namespace (name, &binding,
4534 DECL_NAMESPACE_USING (siter),
4535 scope, flags))
4536 /* Give up because of error. */
4537 return error_mark_node;
4538 if (siter == scope) break;
4539 siter = CP_DECL_CONTEXT (siter);
4542 val = binding.value;
4543 if (scope == global_namespace)
4544 break;
4546 return val;
4549 /* Wrapper for unqualified_namespace_lookup_1. */
4551 static tree
4552 unqualified_namespace_lookup (tree name, int flags)
4554 tree ret;
4555 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4556 ret = unqualified_namespace_lookup_1 (name, flags);
4557 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4558 return ret;
4561 /* Look up NAME (an IDENTIFIER_NODE) in SCOPE (either a NAMESPACE_DECL
4562 or a class TYPE).
4564 If PREFER_TYPE is > 0, we only return TYPE_DECLs or namespaces.
4565 If PREFER_TYPE is > 1, we only return TYPE_DECLs.
4567 Returns a DECL (or OVERLOAD, or BASELINK) representing the
4568 declaration found. If no suitable declaration can be found,
4569 ERROR_MARK_NODE is returned. If COMPLAIN is true and SCOPE is
4570 neither a class-type nor a namespace a diagnostic is issued. */
4572 tree
4573 lookup_qualified_name (tree scope, tree name, int prefer_type, bool complain,
4574 bool find_hidden)
4576 tree t = NULL_TREE;
4578 if (TREE_CODE (scope) == NAMESPACE_DECL)
4580 struct scope_binding binding = EMPTY_SCOPE_BINDING;
4582 int flags = lookup_flags (prefer_type, /*namespaces_only*/false);
4583 if (find_hidden)
4584 flags |= LOOKUP_HIDDEN;
4585 if (qualified_lookup_using_namespace (name, scope, &binding, flags))
4586 t = binding.value;
4588 else if (cxx_dialect != cxx98 && TREE_CODE (scope) == ENUMERAL_TYPE)
4589 t = lookup_enumerator (scope, name);
4590 else if (is_class_type (scope, complain))
4591 t = lookup_member (scope, name, 2, prefer_type, tf_warning_or_error);
4593 if (!t)
4594 return error_mark_node;
4595 return t;
4598 /* Subroutine of unqualified_namespace_lookup:
4599 Add the bindings of NAME in used namespaces to VAL.
4600 We are currently looking for names in namespace SCOPE, so we
4601 look through USINGS for using-directives of namespaces
4602 which have SCOPE as a common ancestor with the current scope.
4603 Returns false on errors. */
4605 static bool
4606 lookup_using_namespace (tree name, struct scope_binding *val,
4607 tree usings, tree scope, int flags)
4609 tree iter;
4610 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4611 /* Iterate over all used namespaces in current, searching for using
4612 directives of scope. */
4613 for (iter = usings; iter; iter = TREE_CHAIN (iter))
4614 if (TREE_VALUE (iter) == scope)
4616 tree used = ORIGINAL_NAMESPACE (TREE_PURPOSE (iter));
4617 cxx_binding *val1 =
4618 cp_binding_level_find_binding_for_name (NAMESPACE_LEVEL (used), name);
4619 /* Resolve ambiguities. */
4620 if (val1)
4621 ambiguous_decl (val, val1, flags);
4623 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4624 return val->value != error_mark_node;
4627 /* Returns true iff VEC contains TARGET. */
4629 static bool
4630 tree_vec_contains (vec<tree, va_gc> *vec, tree target)
4632 unsigned int i;
4633 tree elt;
4634 FOR_EACH_VEC_SAFE_ELT (vec,i,elt)
4635 if (elt == target)
4636 return true;
4637 return false;
4640 /* [namespace.qual]
4641 Accepts the NAME to lookup and its qualifying SCOPE.
4642 Returns the name/type pair found into the cxx_binding *RESULT,
4643 or false on error. */
4645 static bool
4646 qualified_lookup_using_namespace (tree name, tree scope,
4647 struct scope_binding *result, int flags)
4649 /* Maintain a list of namespaces visited... */
4650 vec<tree, va_gc> *seen = NULL;
4651 vec<tree, va_gc> *seen_inline = NULL;
4652 /* ... and a list of namespace yet to see. */
4653 vec<tree, va_gc> *todo = NULL;
4654 vec<tree, va_gc> *todo_maybe = NULL;
4655 vec<tree, va_gc> *todo_inline = NULL;
4656 tree usings;
4657 timevar_start (TV_NAME_LOOKUP);
4658 /* Look through namespace aliases. */
4659 scope = ORIGINAL_NAMESPACE (scope);
4661 /* Algorithm: Starting with SCOPE, walk through the set of used
4662 namespaces. For each used namespace, look through its inline
4663 namespace set for any bindings and usings. If no bindings are
4664 found, add any usings seen to the set of used namespaces. */
4665 vec_safe_push (todo, scope);
4667 while (todo->length ())
4669 bool found_here;
4670 scope = todo->pop ();
4671 if (tree_vec_contains (seen, scope))
4672 continue;
4673 vec_safe_push (seen, scope);
4674 vec_safe_push (todo_inline, scope);
4676 found_here = false;
4677 while (todo_inline->length ())
4679 cxx_binding *binding;
4681 scope = todo_inline->pop ();
4682 if (tree_vec_contains (seen_inline, scope))
4683 continue;
4684 vec_safe_push (seen_inline, scope);
4686 binding =
4687 cp_binding_level_find_binding_for_name (NAMESPACE_LEVEL (scope), name);
4688 if (binding)
4690 ambiguous_decl (result, binding, flags);
4691 if (result->type || result->value)
4692 found_here = true;
4695 for (usings = DECL_NAMESPACE_USING (scope); usings;
4696 usings = TREE_CHAIN (usings))
4697 if (!TREE_INDIRECT_USING (usings))
4699 if (is_associated_namespace (scope, TREE_PURPOSE (usings)))
4700 vec_safe_push (todo_inline, TREE_PURPOSE (usings));
4701 else
4702 vec_safe_push (todo_maybe, TREE_PURPOSE (usings));
4706 if (found_here)
4707 vec_safe_truncate (todo_maybe, 0);
4708 else
4709 while (vec_safe_length (todo_maybe))
4710 vec_safe_push (todo, todo_maybe->pop ());
4712 vec_free (todo);
4713 vec_free (todo_maybe);
4714 vec_free (todo_inline);
4715 vec_free (seen);
4716 vec_free (seen_inline);
4717 timevar_stop (TV_NAME_LOOKUP);
4718 return result->value != error_mark_node;
4721 /* Helper function for lookup_name_fuzzy.
4722 Traverse binding level LVL, looking for good name matches for NAME
4723 (and BM). */
4724 static void
4725 consider_binding_level (tree name, best_match <tree, tree> &bm,
4726 cp_binding_level *lvl, bool look_within_fields,
4727 enum lookup_name_fuzzy_kind kind)
4729 if (look_within_fields)
4730 if (lvl->this_entity && TREE_CODE (lvl->this_entity) == RECORD_TYPE)
4732 tree type = lvl->this_entity;
4733 bool want_type_p = (kind == FUZZY_LOOKUP_TYPENAME);
4734 tree best_matching_field
4735 = lookup_member_fuzzy (type, name, want_type_p);
4736 if (best_matching_field)
4737 bm.consider (best_matching_field);
4740 for (tree t = lvl->names; t; t = TREE_CHAIN (t))
4742 tree d = t;
4744 /* OVERLOADs or decls from using declaration are wrapped into
4745 TREE_LIST. */
4746 if (TREE_CODE (d) == TREE_LIST)
4748 d = TREE_VALUE (d);
4749 d = OVL_CURRENT (d);
4752 /* Don't use bindings from implicitly declared functions,
4753 as they were likely misspellings themselves. */
4754 if (TREE_TYPE (d) == error_mark_node)
4755 continue;
4757 /* Skip anticipated decls of builtin functions. */
4758 if (TREE_CODE (d) == FUNCTION_DECL
4759 && DECL_BUILT_IN (d)
4760 && DECL_ANTICIPATED (d))
4761 continue;
4763 if (tree name = DECL_NAME (d))
4764 /* Ignore internal names with spaces in them. */
4765 if (!strchr (IDENTIFIER_POINTER (name), ' '))
4766 bm.consider (DECL_NAME (d));
4770 /* Search for near-matches for NAME within the current bindings, and within
4771 macro names, returning the best match as a const char *, or NULL if
4772 no reasonable match is found. */
4774 const char *
4775 lookup_name_fuzzy (tree name, enum lookup_name_fuzzy_kind kind)
4777 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
4779 best_match <tree, tree> bm (name);
4781 cp_binding_level *lvl;
4782 for (lvl = scope_chain->class_bindings; lvl; lvl = lvl->level_chain)
4783 consider_binding_level (name, bm, lvl, true, kind);
4785 for (lvl = current_binding_level; lvl; lvl = lvl->level_chain)
4786 consider_binding_level (name, bm, lvl, false, kind);
4788 /* Consider macros: if the user misspelled a macro name e.g. "SOME_MACRO"
4790 x = SOME_OTHER_MACRO (y);
4791 then "SOME_OTHER_MACRO" will survive to the frontend and show up
4792 as a misspelled identifier.
4794 Use the best distance so far so that a candidate is only set if
4795 a macro is better than anything so far. This allows early rejection
4796 (without calculating the edit distance) of macro names that must have
4797 distance >= bm.get_best_distance (), and means that we only get a
4798 non-NULL result for best_macro_match if it's better than any of
4799 the identifiers already checked. */
4800 best_macro_match bmm (name, bm.get_best_distance (), parse_in);
4801 cpp_hashnode *best_macro = bmm.get_best_meaningful_candidate ();
4802 /* If a macro is the closest so far to NAME, suggest it. */
4803 if (best_macro)
4804 return (const char *)best_macro->ident.str;
4806 /* Try the "starts_decl_specifier_p" keywords to detect
4807 "singed" vs "signed" typos. */
4808 for (unsigned i = 0; i < num_c_common_reswords; i++)
4810 const c_common_resword *resword = &c_common_reswords[i];
4812 if (!cp_keyword_starts_decl_specifier_p (resword->rid))
4813 continue;
4815 tree resword_identifier = ridpointers [resword->rid];
4816 if (!resword_identifier)
4817 continue;
4818 gcc_assert (TREE_CODE (resword_identifier) == IDENTIFIER_NODE);
4820 /* Only consider reserved words that survived the
4821 filtering in init_reswords (e.g. for -std). */
4822 if (!C_IS_RESERVED_WORD (resword_identifier))
4823 continue;
4825 bm.consider (resword_identifier);
4828 /* See if we have a good suggesion for the user. */
4829 tree best_id = bm.get_best_meaningful_candidate ();
4830 if (best_id)
4831 return IDENTIFIER_POINTER (best_id);
4833 /* No meaningful suggestion available. */
4834 return NULL;
4837 /* Subroutine of outer_binding.
4839 Returns TRUE if BINDING is a binding to a template parameter of
4840 SCOPE. In that case SCOPE is the scope of a primary template
4841 parameter -- in the sense of G++, i.e, a template that has its own
4842 template header.
4844 Returns FALSE otherwise. */
4846 static bool
4847 binding_to_template_parms_of_scope_p (cxx_binding *binding,
4848 cp_binding_level *scope)
4850 tree binding_value, tmpl, tinfo;
4851 int level;
4853 if (!binding || !scope || !scope->this_entity)
4854 return false;
4856 binding_value = binding->value ? binding->value : binding->type;
4857 tinfo = get_template_info (scope->this_entity);
4859 /* BINDING_VALUE must be a template parm. */
4860 if (binding_value == NULL_TREE
4861 || (!DECL_P (binding_value)
4862 || !DECL_TEMPLATE_PARM_P (binding_value)))
4863 return false;
4865 /* The level of BINDING_VALUE. */
4866 level =
4867 template_type_parameter_p (binding_value)
4868 ? TEMPLATE_PARM_LEVEL (TEMPLATE_TYPE_PARM_INDEX
4869 (TREE_TYPE (binding_value)))
4870 : TEMPLATE_PARM_LEVEL (DECL_INITIAL (binding_value));
4872 /* The template of the current scope, iff said scope is a primary
4873 template. */
4874 tmpl = (tinfo
4875 && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
4876 ? TI_TEMPLATE (tinfo)
4877 : NULL_TREE);
4879 /* If the level of the parm BINDING_VALUE equals the depth of TMPL,
4880 then BINDING_VALUE is a parameter of TMPL. */
4881 return (tmpl && level == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)));
4884 /* Return the innermost non-namespace binding for NAME from a scope
4885 containing BINDING, or, if BINDING is NULL, the current scope.
4886 Please note that for a given template, the template parameters are
4887 considered to be in the scope containing the current scope.
4888 If CLASS_P is false, then class bindings are ignored. */
4890 cxx_binding *
4891 outer_binding (tree name,
4892 cxx_binding *binding,
4893 bool class_p)
4895 cxx_binding *outer;
4896 cp_binding_level *scope;
4897 cp_binding_level *outer_scope;
4899 if (binding)
4901 scope = binding->scope->level_chain;
4902 outer = binding->previous;
4904 else
4906 scope = current_binding_level;
4907 outer = IDENTIFIER_BINDING (name);
4909 outer_scope = outer ? outer->scope : NULL;
4911 /* Because we create class bindings lazily, we might be missing a
4912 class binding for NAME. If there are any class binding levels
4913 between the LAST_BINDING_LEVEL and the scope in which OUTER was
4914 declared, we must lookup NAME in those class scopes. */
4915 if (class_p)
4916 while (scope && scope != outer_scope && scope->kind != sk_namespace)
4918 if (scope->kind == sk_class)
4920 cxx_binding *class_binding;
4922 class_binding = get_class_binding (name, scope);
4923 if (class_binding)
4925 /* Thread this new class-scope binding onto the
4926 IDENTIFIER_BINDING list so that future lookups
4927 find it quickly. */
4928 class_binding->previous = outer;
4929 if (binding)
4930 binding->previous = class_binding;
4931 else
4932 IDENTIFIER_BINDING (name) = class_binding;
4933 return class_binding;
4936 /* If we are in a member template, the template parms of the member
4937 template are considered to be inside the scope of the containing
4938 class, but within G++ the class bindings are all pushed between the
4939 template parms and the function body. So if the outer binding is
4940 a template parm for the current scope, return it now rather than
4941 look for a class binding. */
4942 if (outer_scope && outer_scope->kind == sk_template_parms
4943 && binding_to_template_parms_of_scope_p (outer, scope))
4944 return outer;
4946 scope = scope->level_chain;
4949 return outer;
4952 /* Return the innermost block-scope or class-scope value binding for
4953 NAME, or NULL_TREE if there is no such binding. */
4955 tree
4956 innermost_non_namespace_value (tree name)
4958 cxx_binding *binding;
4959 binding = outer_binding (name, /*binding=*/NULL, /*class_p=*/true);
4960 return binding ? binding->value : NULL_TREE;
4963 /* Look up NAME in the current binding level and its superiors in the
4964 namespace of variables, functions and typedefs. Return a ..._DECL
4965 node of some kind representing its definition if there is only one
4966 such declaration, or return a TREE_LIST with all the overloaded
4967 definitions if there are many, or return 0 if it is undefined.
4968 Hidden name, either friend declaration or built-in function, are
4969 not ignored.
4971 If PREFER_TYPE is > 0, we prefer TYPE_DECLs or namespaces.
4972 If PREFER_TYPE is > 1, we reject non-type decls (e.g. namespaces).
4973 Otherwise we prefer non-TYPE_DECLs.
4975 If NONCLASS is nonzero, bindings in class scopes are ignored. If
4976 BLOCK_P is false, bindings in block scopes are ignored. */
4978 static tree
4979 lookup_name_real_1 (tree name, int prefer_type, int nonclass, bool block_p,
4980 int namespaces_only, int flags)
4982 cxx_binding *iter;
4983 tree val = NULL_TREE;
4985 /* Conversion operators are handled specially because ordinary
4986 unqualified name lookup will not find template conversion
4987 operators. */
4988 if (IDENTIFIER_TYPENAME_P (name))
4990 cp_binding_level *level;
4992 for (level = current_binding_level;
4993 level && level->kind != sk_namespace;
4994 level = level->level_chain)
4996 tree class_type;
4997 tree operators;
4999 /* A conversion operator can only be declared in a class
5000 scope. */
5001 if (level->kind != sk_class)
5002 continue;
5004 /* Lookup the conversion operator in the class. */
5005 class_type = level->this_entity;
5006 operators = lookup_fnfields (class_type, name, /*protect=*/0);
5007 if (operators)
5008 return operators;
5011 return NULL_TREE;
5014 flags |= lookup_flags (prefer_type, namespaces_only);
5016 /* First, look in non-namespace scopes. */
5018 if (current_class_type == NULL_TREE)
5019 nonclass = 1;
5021 if (block_p || !nonclass)
5022 for (iter = outer_binding (name, NULL, !nonclass);
5023 iter;
5024 iter = outer_binding (name, iter, !nonclass))
5026 tree binding;
5028 /* Skip entities we don't want. */
5029 if (LOCAL_BINDING_P (iter) ? !block_p : nonclass)
5030 continue;
5032 /* If this is the kind of thing we're looking for, we're done. */
5033 if (qualify_lookup (iter->value, flags))
5034 binding = iter->value;
5035 else if ((flags & LOOKUP_PREFER_TYPES)
5036 && qualify_lookup (iter->type, flags))
5037 binding = iter->type;
5038 else
5039 binding = NULL_TREE;
5041 if (binding)
5043 if (hidden_name_p (binding))
5045 /* A non namespace-scope binding can only be hidden in the
5046 presence of a local class, due to friend declarations.
5048 In particular, consider:
5050 struct C;
5051 void f() {
5052 struct A {
5053 friend struct B;
5054 friend struct C;
5055 void g() {
5056 B* b; // error: B is hidden
5057 C* c; // OK, finds ::C
5060 B *b; // error: B is hidden
5061 C *c; // OK, finds ::C
5062 struct B {};
5063 B *bb; // OK
5066 The standard says that "B" is a local class in "f"
5067 (but not nested within "A") -- but that name lookup
5068 for "B" does not find this declaration until it is
5069 declared directly with "f".
5071 In particular:
5073 [class.friend]
5075 If a friend declaration appears in a local class and
5076 the name specified is an unqualified name, a prior
5077 declaration is looked up without considering scopes
5078 that are outside the innermost enclosing non-class
5079 scope. For a friend function declaration, if there is
5080 no prior declaration, the program is ill-formed. For a
5081 friend class declaration, if there is no prior
5082 declaration, the class that is specified belongs to the
5083 innermost enclosing non-class scope, but if it is
5084 subsequently referenced, its name is not found by name
5085 lookup until a matching declaration is provided in the
5086 innermost enclosing nonclass scope.
5088 So just keep looking for a non-hidden binding.
5090 gcc_assert (TREE_CODE (binding) == TYPE_DECL);
5091 continue;
5093 val = binding;
5094 break;
5098 /* Now lookup in namespace scopes. */
5099 if (!val)
5100 val = unqualified_namespace_lookup (name, flags);
5102 /* Anticipated built-ins and friends aren't found by normal lookup. */
5103 if (val && !(flags & LOOKUP_HIDDEN))
5104 val = remove_hidden_names (val);
5106 /* If we have a single function from a using decl, pull it out. */
5107 if (val && TREE_CODE (val) == OVERLOAD && !really_overloaded_fn (val))
5108 val = OVL_FUNCTION (val);
5110 return val;
5113 /* Wrapper for lookup_name_real_1. */
5115 tree
5116 lookup_name_real (tree name, int prefer_type, int nonclass, bool block_p,
5117 int namespaces_only, int flags)
5119 tree ret;
5120 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
5121 ret = lookup_name_real_1 (name, prefer_type, nonclass, block_p,
5122 namespaces_only, flags);
5123 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
5124 return ret;
5127 tree
5128 lookup_name_nonclass (tree name)
5130 return lookup_name_real (name, 0, 1, /*block_p=*/true, 0, 0);
5133 tree
5134 lookup_function_nonclass (tree name, vec<tree, va_gc> *args, bool block_p)
5136 return
5137 lookup_arg_dependent (name,
5138 lookup_name_real (name, 0, 1, block_p, 0, 0),
5139 args);
5142 tree
5143 lookup_name (tree name)
5145 return lookup_name_real (name, 0, 0, /*block_p=*/true, 0, 0);
5148 tree
5149 lookup_name_prefer_type (tree name, int prefer_type)
5151 return lookup_name_real (name, prefer_type, 0, /*block_p=*/true, 0, 0);
5154 /* Look up NAME for type used in elaborated name specifier in
5155 the scopes given by SCOPE. SCOPE can be either TS_CURRENT or
5156 TS_WITHIN_ENCLOSING_NON_CLASS. Although not implied by the
5157 name, more scopes are checked if cleanup or template parameter
5158 scope is encountered.
5160 Unlike lookup_name_real, we make sure that NAME is actually
5161 declared in the desired scope, not from inheritance, nor using
5162 directive. For using declaration, there is DR138 still waiting
5163 to be resolved. Hidden name coming from an earlier friend
5164 declaration is also returned.
5166 A TYPE_DECL best matching the NAME is returned. Catching error
5167 and issuing diagnostics are caller's responsibility. */
5169 static tree
5170 lookup_type_scope_1 (tree name, tag_scope scope)
5172 cxx_binding *iter = NULL;
5173 tree val = NULL_TREE;
5175 /* Look in non-namespace scope first. */
5176 if (current_binding_level->kind != sk_namespace)
5177 iter = outer_binding (name, NULL, /*class_p=*/ true);
5178 for (; iter; iter = outer_binding (name, iter, /*class_p=*/ true))
5180 /* Check if this is the kind of thing we're looking for.
5181 If SCOPE is TS_CURRENT, also make sure it doesn't come from
5182 base class. For ITER->VALUE, we can simply use
5183 INHERITED_VALUE_BINDING_P. For ITER->TYPE, we have to use
5184 our own check.
5186 We check ITER->TYPE before ITER->VALUE in order to handle
5187 typedef struct C {} C;
5188 correctly. */
5190 if (qualify_lookup (iter->type, LOOKUP_PREFER_TYPES)
5191 && (scope != ts_current
5192 || LOCAL_BINDING_P (iter)
5193 || DECL_CONTEXT (iter->type) == iter->scope->this_entity))
5194 val = iter->type;
5195 else if ((scope != ts_current
5196 || !INHERITED_VALUE_BINDING_P (iter))
5197 && qualify_lookup (iter->value, LOOKUP_PREFER_TYPES))
5198 val = iter->value;
5200 if (val)
5201 break;
5204 /* Look in namespace scope. */
5205 if (!val)
5207 iter = cp_binding_level_find_binding_for_name
5208 (NAMESPACE_LEVEL (current_decl_namespace ()), name);
5210 if (iter)
5212 /* If this is the kind of thing we're looking for, we're done. */
5213 if (qualify_lookup (iter->type, LOOKUP_PREFER_TYPES))
5214 val = iter->type;
5215 else if (qualify_lookup (iter->value, LOOKUP_PREFER_TYPES))
5216 val = iter->value;
5221 /* Type found, check if it is in the allowed scopes, ignoring cleanup
5222 and template parameter scopes. */
5223 if (val)
5225 cp_binding_level *b = current_binding_level;
5226 while (b)
5228 if (iter->scope == b)
5229 return val;
5231 if (b->kind == sk_cleanup || b->kind == sk_template_parms
5232 || b->kind == sk_function_parms)
5233 b = b->level_chain;
5234 else if (b->kind == sk_class
5235 && scope == ts_within_enclosing_non_class)
5236 b = b->level_chain;
5237 else
5238 break;
5242 return NULL_TREE;
5245 /* Wrapper for lookup_type_scope_1. */
5247 tree
5248 lookup_type_scope (tree name, tag_scope scope)
5250 tree ret;
5251 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
5252 ret = lookup_type_scope_1 (name, scope);
5253 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
5254 return ret;
5258 /* Similar to `lookup_name' but look only in the innermost non-class
5259 binding level. */
5261 static tree
5262 lookup_name_innermost_nonclass_level_1 (tree name)
5264 cp_binding_level *b;
5265 tree t = NULL_TREE;
5267 b = innermost_nonclass_level ();
5269 if (b->kind == sk_namespace)
5271 t = IDENTIFIER_NAMESPACE_VALUE (name);
5273 /* extern "C" function() */
5274 if (t != NULL_TREE && TREE_CODE (t) == TREE_LIST)
5275 t = TREE_VALUE (t);
5277 else if (IDENTIFIER_BINDING (name)
5278 && LOCAL_BINDING_P (IDENTIFIER_BINDING (name)))
5280 cxx_binding *binding;
5281 binding = IDENTIFIER_BINDING (name);
5282 while (1)
5284 if (binding->scope == b
5285 && !(VAR_P (binding->value)
5286 && DECL_DEAD_FOR_LOCAL (binding->value)))
5287 return binding->value;
5289 if (b->kind == sk_cleanup)
5290 b = b->level_chain;
5291 else
5292 break;
5296 return t;
5299 /* Wrapper for lookup_name_innermost_nonclass_level_1. */
5301 tree
5302 lookup_name_innermost_nonclass_level (tree name)
5304 tree ret;
5305 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
5306 ret = lookup_name_innermost_nonclass_level_1 (name);
5307 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
5308 return ret;
5312 /* Returns true iff DECL is a block-scope extern declaration of a function
5313 or variable. */
5315 bool
5316 is_local_extern (tree decl)
5318 cxx_binding *binding;
5320 /* For functions, this is easy. */
5321 if (TREE_CODE (decl) == FUNCTION_DECL)
5322 return DECL_LOCAL_FUNCTION_P (decl);
5324 if (!VAR_P (decl))
5325 return false;
5326 if (!current_function_decl)
5327 return false;
5329 /* For variables, this is not easy. We need to look at the binding stack
5330 for the identifier to see whether the decl we have is a local. */
5331 for (binding = IDENTIFIER_BINDING (DECL_NAME (decl));
5332 binding && binding->scope->kind != sk_namespace;
5333 binding = binding->previous)
5334 if (binding->value == decl)
5335 return LOCAL_BINDING_P (binding);
5337 return false;
5340 /* Like lookup_name_innermost_nonclass_level, but for types. */
5342 static tree
5343 lookup_type_current_level (tree name)
5345 tree t = NULL_TREE;
5347 timevar_start (TV_NAME_LOOKUP);
5348 gcc_assert (current_binding_level->kind != sk_namespace);
5350 if (REAL_IDENTIFIER_TYPE_VALUE (name) != NULL_TREE
5351 && REAL_IDENTIFIER_TYPE_VALUE (name) != global_type_node)
5353 cp_binding_level *b = current_binding_level;
5354 while (1)
5356 if (purpose_member (name, b->type_shadowed))
5358 t = REAL_IDENTIFIER_TYPE_VALUE (name);
5359 break;
5361 if (b->kind == sk_cleanup)
5362 b = b->level_chain;
5363 else
5364 break;
5368 timevar_stop (TV_NAME_LOOKUP);
5369 return t;
5372 /* [basic.lookup.koenig] */
5373 /* A nonzero return value in the functions below indicates an error. */
5375 struct arg_lookup
5377 tree name;
5378 vec<tree, va_gc> *args;
5379 vec<tree, va_gc> *namespaces;
5380 vec<tree, va_gc> *classes;
5381 tree functions;
5382 hash_set<tree> *fn_set;
5385 static bool arg_assoc (struct arg_lookup*, tree);
5386 static bool arg_assoc_args (struct arg_lookup*, tree);
5387 static bool arg_assoc_args_vec (struct arg_lookup*, vec<tree, va_gc> *);
5388 static bool arg_assoc_type (struct arg_lookup*, tree);
5389 static bool add_function (struct arg_lookup *, tree);
5390 static bool arg_assoc_namespace (struct arg_lookup *, tree);
5391 static bool arg_assoc_class_only (struct arg_lookup *, tree);
5392 static bool arg_assoc_bases (struct arg_lookup *, tree);
5393 static bool arg_assoc_class (struct arg_lookup *, tree);
5394 static bool arg_assoc_template_arg (struct arg_lookup*, tree);
5396 /* Add a function to the lookup structure.
5397 Returns true on error. */
5399 static bool
5400 add_function (struct arg_lookup *k, tree fn)
5402 if (!is_overloaded_fn (fn))
5403 /* All names except those of (possibly overloaded) functions and
5404 function templates are ignored. */;
5405 else if (k->fn_set && k->fn_set->add (fn))
5406 /* It's already in the list. */;
5407 else if (!k->functions && TREE_CODE (fn) != TEMPLATE_DECL)
5408 k->functions = fn;
5409 else if (fn == k->functions)
5411 else
5413 k->functions = build_overload (fn, k->functions);
5414 if (TREE_CODE (k->functions) == OVERLOAD)
5415 OVL_ARG_DEPENDENT (k->functions) = true;
5418 return false;
5421 /* Returns true iff CURRENT has declared itself to be an associated
5422 namespace of SCOPE via a strong using-directive (or transitive chain
5423 thereof). Both are namespaces. */
5425 bool
5426 is_associated_namespace (tree current, tree scope)
5428 vec<tree, va_gc> *seen = make_tree_vector ();
5429 vec<tree, va_gc> *todo = make_tree_vector ();
5430 tree t;
5431 bool ret;
5433 while (1)
5435 if (scope == current)
5437 ret = true;
5438 break;
5440 vec_safe_push (seen, scope);
5441 for (t = DECL_NAMESPACE_ASSOCIATIONS (scope); t; t = TREE_CHAIN (t))
5442 if (!vec_member (TREE_PURPOSE (t), seen))
5443 vec_safe_push (todo, TREE_PURPOSE (t));
5444 if (!todo->is_empty ())
5446 scope = todo->last ();
5447 todo->pop ();
5449 else
5451 ret = false;
5452 break;
5456 release_tree_vector (seen);
5457 release_tree_vector (todo);
5459 return ret;
5462 /* Add functions of a namespace to the lookup structure.
5463 Returns true on error. */
5465 static bool
5466 arg_assoc_namespace (struct arg_lookup *k, tree scope)
5468 tree value;
5470 if (vec_member (scope, k->namespaces))
5471 return false;
5472 vec_safe_push (k->namespaces, scope);
5474 /* Check out our super-users. */
5475 for (value = DECL_NAMESPACE_ASSOCIATIONS (scope); value;
5476 value = TREE_CHAIN (value))
5477 if (arg_assoc_namespace (k, TREE_PURPOSE (value)))
5478 return true;
5480 /* Also look down into inline namespaces. */
5481 for (value = DECL_NAMESPACE_USING (scope); value;
5482 value = TREE_CHAIN (value))
5483 if (is_associated_namespace (scope, TREE_PURPOSE (value)))
5484 if (arg_assoc_namespace (k, TREE_PURPOSE (value)))
5485 return true;
5487 value = namespace_binding (k->name, scope);
5488 if (!value)
5489 return false;
5491 for (; value; value = OVL_NEXT (value))
5493 /* We don't want to find arbitrary hidden functions via argument
5494 dependent lookup. We only want to find friends of associated
5495 classes, which we'll do via arg_assoc_class. */
5496 if (hidden_name_p (OVL_CURRENT (value)))
5497 continue;
5499 if (add_function (k, OVL_CURRENT (value)))
5500 return true;
5503 return false;
5506 /* Adds everything associated with a template argument to the lookup
5507 structure. Returns true on error. */
5509 static bool
5510 arg_assoc_template_arg (struct arg_lookup *k, tree arg)
5512 /* [basic.lookup.koenig]
5514 If T is a template-id, its associated namespaces and classes are
5515 ... the namespaces and classes associated with the types of the
5516 template arguments provided for template type parameters
5517 (excluding template template parameters); the namespaces in which
5518 any template template arguments are defined; and the classes in
5519 which any member templates used as template template arguments
5520 are defined. [Note: non-type template arguments do not
5521 contribute to the set of associated namespaces. ] */
5523 /* Consider first template template arguments. */
5524 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
5525 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
5526 return false;
5527 else if (TREE_CODE (arg) == TEMPLATE_DECL)
5529 tree ctx = CP_DECL_CONTEXT (arg);
5531 /* It's not a member template. */
5532 if (TREE_CODE (ctx) == NAMESPACE_DECL)
5533 return arg_assoc_namespace (k, ctx);
5534 /* Otherwise, it must be member template. */
5535 else
5536 return arg_assoc_class_only (k, ctx);
5538 /* It's an argument pack; handle it recursively. */
5539 else if (ARGUMENT_PACK_P (arg))
5541 tree args = ARGUMENT_PACK_ARGS (arg);
5542 int i, len = TREE_VEC_LENGTH (args);
5543 for (i = 0; i < len; ++i)
5544 if (arg_assoc_template_arg (k, TREE_VEC_ELT (args, i)))
5545 return true;
5547 return false;
5549 /* It's not a template template argument, but it is a type template
5550 argument. */
5551 else if (TYPE_P (arg))
5552 return arg_assoc_type (k, arg);
5553 /* It's a non-type template argument. */
5554 else
5555 return false;
5558 /* Adds the class and its friends to the lookup structure.
5559 Returns true on error. */
5561 static bool
5562 arg_assoc_class_only (struct arg_lookup *k, tree type)
5564 tree list, friends, context;
5566 /* Backend-built structures, such as __builtin_va_list, aren't
5567 affected by all this. */
5568 if (!CLASS_TYPE_P (type))
5569 return false;
5571 context = decl_namespace_context (type);
5572 if (arg_assoc_namespace (k, context))
5573 return true;
5575 complete_type (type);
5577 /* Process friends. */
5578 for (list = DECL_FRIENDLIST (TYPE_MAIN_DECL (type)); list;
5579 list = TREE_CHAIN (list))
5580 if (k->name == FRIEND_NAME (list))
5581 for (friends = FRIEND_DECLS (list); friends;
5582 friends = TREE_CHAIN (friends))
5584 tree fn = TREE_VALUE (friends);
5586 /* Only interested in global functions with potentially hidden
5587 (i.e. unqualified) declarations. */
5588 if (CP_DECL_CONTEXT (fn) != context)
5589 continue;
5590 /* Template specializations are never found by name lookup.
5591 (Templates themselves can be found, but not template
5592 specializations.) */
5593 if (TREE_CODE (fn) == FUNCTION_DECL && DECL_USE_TEMPLATE (fn))
5594 continue;
5595 if (add_function (k, fn))
5596 return true;
5599 return false;
5602 /* Adds the class and its bases to the lookup structure.
5603 Returns true on error. */
5605 static bool
5606 arg_assoc_bases (struct arg_lookup *k, tree type)
5608 if (arg_assoc_class_only (k, type))
5609 return true;
5611 if (TYPE_BINFO (type))
5613 /* Process baseclasses. */
5614 tree binfo, base_binfo;
5615 int i;
5617 for (binfo = TYPE_BINFO (type), i = 0;
5618 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
5619 if (arg_assoc_bases (k, BINFO_TYPE (base_binfo)))
5620 return true;
5623 return false;
5626 /* Adds everything associated with a class argument type to the lookup
5627 structure. Returns true on error.
5629 If T is a class type (including unions), its associated classes are: the
5630 class itself; the class of which it is a member, if any; and its direct
5631 and indirect base classes. Its associated namespaces are the namespaces
5632 of which its associated classes are members. Furthermore, if T is a
5633 class template specialization, its associated namespaces and classes
5634 also include: the namespaces and classes associated with the types of
5635 the template arguments provided for template type parameters (excluding
5636 template template parameters); the namespaces of which any template
5637 template arguments are members; and the classes of which any member
5638 templates used as template template arguments are members. [ Note:
5639 non-type template arguments do not contribute to the set of associated
5640 namespaces. --end note] */
5642 static bool
5643 arg_assoc_class (struct arg_lookup *k, tree type)
5645 tree list;
5646 int i;
5648 /* Backend build structures, such as __builtin_va_list, aren't
5649 affected by all this. */
5650 if (!CLASS_TYPE_P (type))
5651 return false;
5653 if (vec_member (type, k->classes))
5654 return false;
5655 vec_safe_push (k->classes, type);
5657 if (TYPE_CLASS_SCOPE_P (type)
5658 && arg_assoc_class_only (k, TYPE_CONTEXT (type)))
5659 return true;
5661 if (arg_assoc_bases (k, type))
5662 return true;
5664 /* Process template arguments. */
5665 if (CLASSTYPE_TEMPLATE_INFO (type)
5666 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
5668 list = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
5669 for (i = 0; i < TREE_VEC_LENGTH (list); ++i)
5670 if (arg_assoc_template_arg (k, TREE_VEC_ELT (list, i)))
5671 return true;
5674 return false;
5677 /* Adds everything associated with a given type.
5678 Returns 1 on error. */
5680 static bool
5681 arg_assoc_type (struct arg_lookup *k, tree type)
5683 /* As we do not get the type of non-type dependent expressions
5684 right, we can end up with such things without a type. */
5685 if (!type)
5686 return false;
5688 if (TYPE_PTRDATAMEM_P (type))
5690 /* Pointer to member: associate class type and value type. */
5691 if (arg_assoc_type (k, TYPE_PTRMEM_CLASS_TYPE (type)))
5692 return true;
5693 return arg_assoc_type (k, TYPE_PTRMEM_POINTED_TO_TYPE (type));
5695 else switch (TREE_CODE (type))
5697 case ERROR_MARK:
5698 return false;
5699 case VOID_TYPE:
5700 case INTEGER_TYPE:
5701 case REAL_TYPE:
5702 case COMPLEX_TYPE:
5703 case VECTOR_TYPE:
5704 case BOOLEAN_TYPE:
5705 case FIXED_POINT_TYPE:
5706 case DECLTYPE_TYPE:
5707 case NULLPTR_TYPE:
5708 return false;
5709 case RECORD_TYPE:
5710 if (TYPE_PTRMEMFUNC_P (type))
5711 return arg_assoc_type (k, TYPE_PTRMEMFUNC_FN_TYPE (type));
5712 /* FALLTHRU */
5713 case UNION_TYPE:
5714 return arg_assoc_class (k, type);
5715 case POINTER_TYPE:
5716 case REFERENCE_TYPE:
5717 case ARRAY_TYPE:
5718 return arg_assoc_type (k, TREE_TYPE (type));
5719 case ENUMERAL_TYPE:
5720 if (TYPE_CLASS_SCOPE_P (type)
5721 && arg_assoc_class_only (k, TYPE_CONTEXT (type)))
5722 return true;
5723 return arg_assoc_namespace (k, decl_namespace_context (type));
5724 case METHOD_TYPE:
5725 /* The basetype is referenced in the first arg type, so just
5726 fall through. */
5727 case FUNCTION_TYPE:
5728 /* Associate the parameter types. */
5729 if (arg_assoc_args (k, TYPE_ARG_TYPES (type)))
5730 return true;
5731 /* Associate the return type. */
5732 return arg_assoc_type (k, TREE_TYPE (type));
5733 case TEMPLATE_TYPE_PARM:
5734 case BOUND_TEMPLATE_TEMPLATE_PARM:
5735 return false;
5736 case TYPENAME_TYPE:
5737 return false;
5738 case LANG_TYPE:
5739 gcc_assert (type == unknown_type_node
5740 || type == init_list_type_node);
5741 return false;
5742 case TYPE_PACK_EXPANSION:
5743 return arg_assoc_type (k, PACK_EXPANSION_PATTERN (type));
5745 default:
5746 gcc_unreachable ();
5748 return false;
5751 /* Adds everything associated with arguments. Returns true on error. */
5753 static bool
5754 arg_assoc_args (struct arg_lookup *k, tree args)
5756 for (; args; args = TREE_CHAIN (args))
5757 if (arg_assoc (k, TREE_VALUE (args)))
5758 return true;
5759 return false;
5762 /* Adds everything associated with an argument vector. Returns true
5763 on error. */
5765 static bool
5766 arg_assoc_args_vec (struct arg_lookup *k, vec<tree, va_gc> *args)
5768 unsigned int ix;
5769 tree arg;
5771 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
5772 if (arg_assoc (k, arg))
5773 return true;
5774 return false;
5777 /* Adds everything associated with a given tree_node. Returns 1 on error. */
5779 static bool
5780 arg_assoc (struct arg_lookup *k, tree n)
5782 if (n == error_mark_node)
5783 return false;
5785 if (TYPE_P (n))
5786 return arg_assoc_type (k, n);
5788 if (! type_unknown_p (n))
5789 return arg_assoc_type (k, TREE_TYPE (n));
5791 if (TREE_CODE (n) == ADDR_EXPR)
5792 n = TREE_OPERAND (n, 0);
5793 if (TREE_CODE (n) == COMPONENT_REF)
5794 n = TREE_OPERAND (n, 1);
5795 if (TREE_CODE (n) == OFFSET_REF)
5796 n = TREE_OPERAND (n, 1);
5797 while (TREE_CODE (n) == TREE_LIST)
5798 n = TREE_VALUE (n);
5799 if (BASELINK_P (n))
5800 n = BASELINK_FUNCTIONS (n);
5802 if (TREE_CODE (n) == FUNCTION_DECL)
5803 return arg_assoc_type (k, TREE_TYPE (n));
5804 if (TREE_CODE (n) == TEMPLATE_ID_EXPR)
5806 /* The working paper doesn't currently say how to handle template-id
5807 arguments. The sensible thing would seem to be to handle the list
5808 of template candidates like a normal overload set, and handle the
5809 template arguments like we do for class template
5810 specializations. */
5811 tree templ = TREE_OPERAND (n, 0);
5812 tree args = TREE_OPERAND (n, 1);
5813 int ix;
5815 /* First the templates. */
5816 if (arg_assoc (k, templ))
5817 return true;
5819 /* Now the arguments. */
5820 if (args)
5821 for (ix = TREE_VEC_LENGTH (args); ix--;)
5822 if (arg_assoc_template_arg (k, TREE_VEC_ELT (args, ix)) == 1)
5823 return true;
5825 else if (TREE_CODE (n) == OVERLOAD)
5827 for (; n; n = OVL_NEXT (n))
5828 if (arg_assoc_type (k, TREE_TYPE (OVL_CURRENT (n))))
5829 return true;
5832 return false;
5835 /* Performs Koenig lookup depending on arguments, where fns
5836 are the functions found in normal lookup. */
5838 static cp_expr
5839 lookup_arg_dependent_1 (tree name, tree fns, vec<tree, va_gc> *args)
5841 struct arg_lookup k;
5843 /* Remove any hidden friend functions from the list of functions
5844 found so far. They will be added back by arg_assoc_class as
5845 appropriate. */
5846 fns = remove_hidden_names (fns);
5848 k.name = name;
5849 k.args = args;
5850 k.functions = fns;
5851 k.classes = make_tree_vector ();
5853 /* We previously performed an optimization here by setting
5854 NAMESPACES to the current namespace when it was safe. However, DR
5855 164 says that namespaces that were already searched in the first
5856 stage of template processing are searched again (potentially
5857 picking up later definitions) in the second stage. */
5858 k.namespaces = make_tree_vector ();
5860 /* We used to allow duplicates and let joust discard them, but
5861 since the above change for DR 164 we end up with duplicates of
5862 all the functions found by unqualified lookup. So keep track
5863 of which ones we've seen. */
5864 if (fns)
5866 tree ovl;
5867 /* We shouldn't be here if lookup found something other than
5868 namespace-scope functions. */
5869 gcc_assert (DECL_NAMESPACE_SCOPE_P (OVL_CURRENT (fns)));
5870 k.fn_set = new hash_set<tree>;
5871 for (ovl = fns; ovl; ovl = OVL_NEXT (ovl))
5872 k.fn_set->add (OVL_CURRENT (ovl));
5874 else
5875 k.fn_set = NULL;
5877 arg_assoc_args_vec (&k, args);
5879 fns = k.functions;
5881 if (fns
5882 && !VAR_P (fns)
5883 && !is_overloaded_fn (fns))
5885 error ("argument dependent lookup finds %q+D", fns);
5886 error (" in call to %qD", name);
5887 fns = error_mark_node;
5890 release_tree_vector (k.classes);
5891 release_tree_vector (k.namespaces);
5892 delete k.fn_set;
5894 return fns;
5897 /* Wrapper for lookup_arg_dependent_1. */
5899 cp_expr
5900 lookup_arg_dependent (tree name, tree fns, vec<tree, va_gc> *args)
5902 cp_expr ret;
5903 bool subtime;
5904 subtime = timevar_cond_start (TV_NAME_LOOKUP);
5905 ret = lookup_arg_dependent_1 (name, fns, args);
5906 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
5907 return ret;
5911 /* Add namespace to using_directives. Return NULL_TREE if nothing was
5912 changed (i.e. there was already a directive), or the fresh
5913 TREE_LIST otherwise. */
5915 static tree
5916 push_using_directive_1 (tree used)
5918 tree ud = current_binding_level->using_directives;
5919 tree iter, ancestor;
5921 /* Check if we already have this. */
5922 if (purpose_member (used, ud) != NULL_TREE)
5923 return NULL_TREE;
5925 ancestor = namespace_ancestor (current_decl_namespace (), used);
5926 ud = current_binding_level->using_directives;
5927 ud = tree_cons (used, ancestor, ud);
5928 current_binding_level->using_directives = ud;
5930 /* Recursively add all namespaces used. */
5931 for (iter = DECL_NAMESPACE_USING (used); iter; iter = TREE_CHAIN (iter))
5932 push_using_directive (TREE_PURPOSE (iter));
5934 return ud;
5937 /* Wrapper for push_using_directive_1. */
5939 static tree
5940 push_using_directive (tree used)
5942 tree ret;
5943 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
5944 ret = push_using_directive_1 (used);
5945 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
5946 return ret;
5949 /* The type TYPE is being declared. If it is a class template, or a
5950 specialization of a class template, do any processing required and
5951 perform error-checking. If IS_FRIEND is nonzero, this TYPE is
5952 being declared a friend. B is the binding level at which this TYPE
5953 should be bound.
5955 Returns the TYPE_DECL for TYPE, which may have been altered by this
5956 processing. */
5958 static tree
5959 maybe_process_template_type_declaration (tree type, int is_friend,
5960 cp_binding_level *b)
5962 tree decl = TYPE_NAME (type);
5964 if (processing_template_parmlist)
5965 /* You can't declare a new template type in a template parameter
5966 list. But, you can declare a non-template type:
5968 template <class A*> struct S;
5970 is a forward-declaration of `A'. */
5972 else if (b->kind == sk_namespace
5973 && current_binding_level->kind != sk_namespace)
5974 /* If this new type is being injected into a containing scope,
5975 then it's not a template type. */
5977 else
5979 gcc_assert (MAYBE_CLASS_TYPE_P (type)
5980 || TREE_CODE (type) == ENUMERAL_TYPE);
5982 if (processing_template_decl)
5984 /* This may change after the call to
5985 push_template_decl_real, but we want the original value. */
5986 tree name = DECL_NAME (decl);
5988 decl = push_template_decl_real (decl, is_friend);
5989 if (decl == error_mark_node)
5990 return error_mark_node;
5992 /* If the current binding level is the binding level for the
5993 template parameters (see the comment in
5994 begin_template_parm_list) and the enclosing level is a class
5995 scope, and we're not looking at a friend, push the
5996 declaration of the member class into the class scope. In the
5997 friend case, push_template_decl will already have put the
5998 friend into global scope, if appropriate. */
5999 if (TREE_CODE (type) != ENUMERAL_TYPE
6000 && !is_friend && b->kind == sk_template_parms
6001 && b->level_chain->kind == sk_class)
6003 finish_member_declaration (CLASSTYPE_TI_TEMPLATE (type));
6005 if (!COMPLETE_TYPE_P (current_class_type))
6007 maybe_add_class_template_decl_list (current_class_type,
6008 type, /*friend_p=*/0);
6009 /* Put this UTD in the table of UTDs for the class. */
6010 if (CLASSTYPE_NESTED_UTDS (current_class_type) == NULL)
6011 CLASSTYPE_NESTED_UTDS (current_class_type) =
6012 binding_table_new (SCOPE_DEFAULT_HT_SIZE);
6014 binding_table_insert
6015 (CLASSTYPE_NESTED_UTDS (current_class_type), name, type);
6021 return decl;
6024 /* Push a tag name NAME for struct/class/union/enum type TYPE. In case
6025 that the NAME is a class template, the tag is processed but not pushed.
6027 The pushed scope depend on the SCOPE parameter:
6028 - When SCOPE is TS_CURRENT, put it into the inner-most non-sk_cleanup
6029 scope.
6030 - When SCOPE is TS_GLOBAL, put it in the inner-most non-class and
6031 non-template-parameter scope. This case is needed for forward
6032 declarations.
6033 - When SCOPE is TS_WITHIN_ENCLOSING_NON_CLASS, this is similar to
6034 TS_GLOBAL case except that names within template-parameter scopes
6035 are not pushed at all.
6037 Returns TYPE upon success and ERROR_MARK_NODE otherwise. */
6039 static tree
6040 pushtag_1 (tree name, tree type, tag_scope scope)
6042 cp_binding_level *b;
6043 tree decl;
6045 b = current_binding_level;
6046 while (/* Cleanup scopes are not scopes from the point of view of
6047 the language. */
6048 b->kind == sk_cleanup
6049 /* Neither are function parameter scopes. */
6050 || b->kind == sk_function_parms
6051 /* Neither are the scopes used to hold template parameters
6052 for an explicit specialization. For an ordinary template
6053 declaration, these scopes are not scopes from the point of
6054 view of the language. */
6055 || (b->kind == sk_template_parms
6056 && (b->explicit_spec_p || scope == ts_global))
6057 || (b->kind == sk_class
6058 && (scope != ts_current
6059 /* We may be defining a new type in the initializer
6060 of a static member variable. We allow this when
6061 not pedantic, and it is particularly useful for
6062 type punning via an anonymous union. */
6063 || COMPLETE_TYPE_P (b->this_entity))))
6064 b = b->level_chain;
6066 gcc_assert (identifier_p (name));
6068 /* Do C++ gratuitous typedefing. */
6069 if (identifier_type_value_1 (name) != type)
6071 tree tdef;
6072 int in_class = 0;
6073 tree context = TYPE_CONTEXT (type);
6075 if (! context)
6077 tree cs = current_scope ();
6079 if (scope == ts_current
6080 || (cs && TREE_CODE (cs) == FUNCTION_DECL))
6081 context = cs;
6082 else if (cs != NULL_TREE && TYPE_P (cs))
6083 /* When declaring a friend class of a local class, we want
6084 to inject the newly named class into the scope
6085 containing the local class, not the namespace
6086 scope. */
6087 context = decl_function_context (get_type_decl (cs));
6089 if (!context)
6090 context = current_namespace;
6092 if (b->kind == sk_class
6093 || (b->kind == sk_template_parms
6094 && b->level_chain->kind == sk_class))
6095 in_class = 1;
6097 tdef = create_implicit_typedef (name, type);
6098 DECL_CONTEXT (tdef) = FROB_CONTEXT (context);
6099 if (scope == ts_within_enclosing_non_class)
6101 /* This is a friend. Make this TYPE_DECL node hidden from
6102 ordinary name lookup. Its corresponding TEMPLATE_DECL
6103 will be marked in push_template_decl_real. */
6104 retrofit_lang_decl (tdef);
6105 DECL_ANTICIPATED (tdef) = 1;
6106 DECL_FRIEND_P (tdef) = 1;
6109 decl = maybe_process_template_type_declaration
6110 (type, scope == ts_within_enclosing_non_class, b);
6111 if (decl == error_mark_node)
6112 return decl;
6114 if (b->kind == sk_class)
6116 if (!TYPE_BEING_DEFINED (current_class_type))
6117 return error_mark_node;
6119 if (!PROCESSING_REAL_TEMPLATE_DECL_P ())
6120 /* Put this TYPE_DECL on the TYPE_FIELDS list for the
6121 class. But if it's a member template class, we want
6122 the TEMPLATE_DECL, not the TYPE_DECL, so this is done
6123 later. */
6124 finish_member_declaration (decl);
6125 else
6126 pushdecl_class_level (decl);
6128 else if (b->kind != sk_template_parms)
6130 decl = pushdecl_with_scope_1 (decl, b, /*is_friend=*/false);
6131 if (decl == error_mark_node)
6132 return decl;
6135 if (! in_class)
6136 set_identifier_type_value_with_scope (name, tdef, b);
6138 TYPE_CONTEXT (type) = DECL_CONTEXT (decl);
6140 /* If this is a local class, keep track of it. We need this
6141 information for name-mangling, and so that it is possible to
6142 find all function definitions in a translation unit in a
6143 convenient way. (It's otherwise tricky to find a member
6144 function definition it's only pointed to from within a local
6145 class.) */
6146 if (TYPE_FUNCTION_SCOPE_P (type))
6148 if (processing_template_decl)
6150 /* Push a DECL_EXPR so we call pushtag at the right time in
6151 template instantiation rather than in some nested context. */
6152 add_decl_expr (decl);
6154 else
6155 vec_safe_push (local_classes, type);
6158 if (b->kind == sk_class
6159 && !COMPLETE_TYPE_P (current_class_type))
6161 maybe_add_class_template_decl_list (current_class_type,
6162 type, /*friend_p=*/0);
6164 if (CLASSTYPE_NESTED_UTDS (current_class_type) == NULL)
6165 CLASSTYPE_NESTED_UTDS (current_class_type)
6166 = binding_table_new (SCOPE_DEFAULT_HT_SIZE);
6168 binding_table_insert
6169 (CLASSTYPE_NESTED_UTDS (current_class_type), name, type);
6172 decl = TYPE_NAME (type);
6173 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
6175 /* Set type visibility now if this is a forward declaration. */
6176 TREE_PUBLIC (decl) = 1;
6177 determine_visibility (decl);
6179 return type;
6182 /* Wrapper for pushtag_1. */
6184 tree
6185 pushtag (tree name, tree type, tag_scope scope)
6187 tree ret;
6188 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6189 ret = pushtag_1 (name, type, scope);
6190 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6191 return ret;
6194 /* Subroutines for reverting temporarily to top-level for instantiation
6195 of templates and such. We actually need to clear out the class- and
6196 local-value slots of all identifiers, so that only the global values
6197 are at all visible. Simply setting current_binding_level to the global
6198 scope isn't enough, because more binding levels may be pushed. */
6199 struct saved_scope *scope_chain;
6201 /* Return true if ID has not already been marked. */
6203 static inline bool
6204 store_binding_p (tree id)
6206 if (!id || !IDENTIFIER_BINDING (id))
6207 return false;
6209 if (IDENTIFIER_MARKED (id))
6210 return false;
6212 return true;
6215 /* Add an appropriate binding to *OLD_BINDINGS which needs to already
6216 have enough space reserved. */
6218 static void
6219 store_binding (tree id, vec<cxx_saved_binding, va_gc> **old_bindings)
6221 cxx_saved_binding saved;
6223 gcc_checking_assert (store_binding_p (id));
6225 IDENTIFIER_MARKED (id) = 1;
6227 saved.identifier = id;
6228 saved.binding = IDENTIFIER_BINDING (id);
6229 saved.real_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
6230 (*old_bindings)->quick_push (saved);
6231 IDENTIFIER_BINDING (id) = NULL;
6234 static void
6235 store_bindings (tree names, vec<cxx_saved_binding, va_gc> **old_bindings)
6237 static vec<tree> bindings_need_stored;
6238 tree t, id;
6239 size_t i;
6241 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6242 for (t = names; t; t = TREE_CHAIN (t))
6244 if (TREE_CODE (t) == TREE_LIST)
6245 id = TREE_PURPOSE (t);
6246 else
6247 id = DECL_NAME (t);
6249 if (store_binding_p (id))
6250 bindings_need_stored.safe_push (id);
6252 if (!bindings_need_stored.is_empty ())
6254 vec_safe_reserve_exact (*old_bindings, bindings_need_stored.length ());
6255 for (i = 0; bindings_need_stored.iterate (i, &id); ++i)
6257 /* We can appearantly have duplicates in NAMES. */
6258 if (store_binding_p (id))
6259 store_binding (id, old_bindings);
6261 bindings_need_stored.truncate (0);
6263 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6266 /* Like store_bindings, but NAMES is a vector of cp_class_binding
6267 objects, rather than a TREE_LIST. */
6269 static void
6270 store_class_bindings (vec<cp_class_binding, va_gc> *names,
6271 vec<cxx_saved_binding, va_gc> **old_bindings)
6273 static vec<tree> bindings_need_stored;
6274 size_t i;
6275 cp_class_binding *cb;
6277 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6278 for (i = 0; vec_safe_iterate (names, i, &cb); ++i)
6279 if (store_binding_p (cb->identifier))
6280 bindings_need_stored.safe_push (cb->identifier);
6281 if (!bindings_need_stored.is_empty ())
6283 tree id;
6284 vec_safe_reserve_exact (*old_bindings, bindings_need_stored.length ());
6285 for (i = 0; bindings_need_stored.iterate (i, &id); ++i)
6286 store_binding (id, old_bindings);
6287 bindings_need_stored.truncate (0);
6289 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6292 /* A chain of saved_scope structures awaiting reuse. */
6294 static GTY((deletable)) struct saved_scope *free_saved_scope;
6296 void
6297 push_to_top_level (void)
6299 struct saved_scope *s;
6300 cp_binding_level *b;
6301 cxx_saved_binding *sb;
6302 size_t i;
6303 bool need_pop;
6305 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6307 /* Reuse or create a new structure for this saved scope. */
6308 if (free_saved_scope != NULL)
6310 s = free_saved_scope;
6311 free_saved_scope = s->prev;
6313 vec<cxx_saved_binding, va_gc> *old_bindings = s->old_bindings;
6314 memset (s, 0, sizeof (*s));
6315 /* Also reuse the structure's old_bindings vector. */
6316 vec_safe_truncate (old_bindings, 0);
6317 s->old_bindings = old_bindings;
6319 else
6320 s = ggc_cleared_alloc<saved_scope> ();
6322 b = scope_chain ? current_binding_level : 0;
6324 /* If we're in the middle of some function, save our state. */
6325 if (cfun)
6327 need_pop = true;
6328 push_function_context ();
6330 else
6331 need_pop = false;
6333 if (scope_chain && previous_class_level)
6334 store_class_bindings (previous_class_level->class_shadowed,
6335 &s->old_bindings);
6337 /* Have to include the global scope, because class-scope decls
6338 aren't listed anywhere useful. */
6339 for (; b; b = b->level_chain)
6341 tree t;
6343 /* Template IDs are inserted into the global level. If they were
6344 inserted into namespace level, finish_file wouldn't find them
6345 when doing pending instantiations. Therefore, don't stop at
6346 namespace level, but continue until :: . */
6347 if (global_scope_p (b))
6348 break;
6350 store_bindings (b->names, &s->old_bindings);
6351 /* We also need to check class_shadowed to save class-level type
6352 bindings, since pushclass doesn't fill in b->names. */
6353 if (b->kind == sk_class)
6354 store_class_bindings (b->class_shadowed, &s->old_bindings);
6356 /* Unwind type-value slots back to top level. */
6357 for (t = b->type_shadowed; t; t = TREE_CHAIN (t))
6358 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (t), TREE_VALUE (t));
6361 FOR_EACH_VEC_SAFE_ELT (s->old_bindings, i, sb)
6362 IDENTIFIER_MARKED (sb->identifier) = 0;
6364 s->prev = scope_chain;
6365 s->bindings = b;
6366 s->need_pop_function_context = need_pop;
6367 s->function_decl = current_function_decl;
6368 s->unevaluated_operand = cp_unevaluated_operand;
6369 s->inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
6370 s->x_stmt_tree.stmts_are_full_exprs_p = true;
6372 scope_chain = s;
6373 current_function_decl = NULL_TREE;
6374 vec_alloc (current_lang_base, 10);
6375 current_lang_name = lang_name_cplusplus;
6376 current_namespace = global_namespace;
6377 push_class_stack ();
6378 cp_unevaluated_operand = 0;
6379 c_inhibit_evaluation_warnings = 0;
6380 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6383 static void
6384 pop_from_top_level_1 (void)
6386 struct saved_scope *s = scope_chain;
6387 cxx_saved_binding *saved;
6388 size_t i;
6390 /* Clear out class-level bindings cache. */
6391 if (previous_class_level)
6392 invalidate_class_lookup_cache ();
6393 pop_class_stack ();
6395 current_lang_base = 0;
6397 scope_chain = s->prev;
6398 FOR_EACH_VEC_SAFE_ELT (s->old_bindings, i, saved)
6400 tree id = saved->identifier;
6402 IDENTIFIER_BINDING (id) = saved->binding;
6403 SET_IDENTIFIER_TYPE_VALUE (id, saved->real_type_value);
6406 /* If we were in the middle of compiling a function, restore our
6407 state. */
6408 if (s->need_pop_function_context)
6409 pop_function_context ();
6410 current_function_decl = s->function_decl;
6411 cp_unevaluated_operand = s->unevaluated_operand;
6412 c_inhibit_evaluation_warnings = s->inhibit_evaluation_warnings;
6414 /* Make this saved_scope structure available for reuse by
6415 push_to_top_level. */
6416 s->prev = free_saved_scope;
6417 free_saved_scope = s;
6420 /* Wrapper for pop_from_top_level_1. */
6422 void
6423 pop_from_top_level (void)
6425 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
6426 pop_from_top_level_1 ();
6427 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
6431 /* Pop off extraneous binding levels left over due to syntax errors.
6433 We don't pop past namespaces, as they might be valid. */
6435 void
6436 pop_everything (void)
6438 if (ENABLE_SCOPE_CHECKING)
6439 verbatim ("XXX entering pop_everything ()\n");
6440 while (!toplevel_bindings_p ())
6442 if (current_binding_level->kind == sk_class)
6443 pop_nested_class ();
6444 else
6445 poplevel (0, 0, 0);
6447 if (ENABLE_SCOPE_CHECKING)
6448 verbatim ("XXX leaving pop_everything ()\n");
6451 /* Emit debugging information for using declarations and directives.
6452 If input tree is overloaded fn then emit debug info for all
6453 candidates. */
6455 void
6456 cp_emit_debug_info_for_using (tree t, tree context)
6458 /* Don't try to emit any debug information if we have errors. */
6459 if (seen_error ())
6460 return;
6462 /* Ignore this FUNCTION_DECL if it refers to a builtin declaration
6463 of a builtin function. */
6464 if (TREE_CODE (t) == FUNCTION_DECL
6465 && DECL_EXTERNAL (t)
6466 && DECL_BUILT_IN (t))
6467 return;
6469 /* Do not supply context to imported_module_or_decl, if
6470 it is a global namespace. */
6471 if (context == global_namespace)
6472 context = NULL_TREE;
6474 if (BASELINK_P (t))
6475 t = BASELINK_FUNCTIONS (t);
6477 /* FIXME: Handle TEMPLATE_DECLs. */
6478 for (t = OVL_CURRENT (t); t; t = OVL_NEXT (t))
6479 if (TREE_CODE (t) != TEMPLATE_DECL)
6481 if (building_stmt_list_p ())
6482 add_stmt (build_stmt (input_location, USING_STMT, t));
6483 else
6484 (*debug_hooks->imported_module_or_decl) (t, NULL_TREE, context, false);
6488 #include "gt-cp-name-lookup.h"